Sebastian Redl | 4ee2ad0 | 2010-08-18 23:56:31 +0000 | [diff] [blame] | 1 | //===--- ASTWriter.cpp - AST File Writer ----------------------------------===// |
Douglas Gregor | 2cf2634 | 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 | a4232eb | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 10 | // This file defines the ASTWriter class, which writes AST files. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Sebastian Redl | 7faa2ec | 2010-08-18 23:56:37 +0000 | [diff] [blame] | 14 | #include "clang/Serialization/ASTWriter.h" |
Argyrios Kyrtzidis | 0eca89e | 2010-08-20 16:03:52 +0000 | [diff] [blame] | 15 | #include "ASTCommon.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Sema.h" |
| 17 | #include "clang/Sema/IdentifierResolver.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/Decl.h" |
| 20 | #include "clang/AST/DeclContextInternals.h" |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclTemplate.h" |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclFriend.h" |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 23 | #include "clang/AST/Expr.h" |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 24 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 25 | #include "clang/AST/Type.h" |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 26 | #include "clang/AST/TypeLocVisitor.h" |
Sebastian Redl | 6ab7cd8 | 2010-08-18 23:57:17 +0000 | [diff] [blame] | 27 | #include "clang/Serialization/ASTReader.h" |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 28 | #include "clang/Lex/MacroInfo.h" |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 29 | #include "clang/Lex/PreprocessingRecord.h" |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 30 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 31 | #include "clang/Lex/HeaderSearch.h" |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 32 | #include "clang/Basic/FileManager.h" |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 33 | #include "clang/Basic/FileSystemStatCache.h" |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 34 | #include "clang/Basic/OnDiskHashTable.h" |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 35 | #include "clang/Basic/SourceManager.h" |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 36 | #include "clang/Basic/SourceManagerInternals.h" |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 37 | #include "clang/Basic/TargetInfo.h" |
Douglas Gregor | ab41e63 | 2009-04-27 22:23:34 +0000 | [diff] [blame] | 38 | #include "clang/Basic/Version.h" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 39 | #include "clang/Basic/VersionTuple.h" |
Douglas Gregor | 17fc223 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/APFloat.h" |
| 41 | #include "llvm/ADT/APInt.h" |
Daniel Dunbar | 2596e42 | 2009-10-17 23:52:28 +0000 | [diff] [blame] | 42 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 43 | #include "llvm/Bitcode/BitstreamWriter.h" |
Michael J. Spencer | fbfd180 | 2010-12-21 16:45:57 +0000 | [diff] [blame] | 44 | #include "llvm/Support/FileSystem.h" |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 45 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Path.h" |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 47 | #include <algorithm> |
Chris Lattner | 3c304bd | 2009-04-11 18:40:46 +0000 | [diff] [blame] | 48 | #include <cstdio> |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 49 | #include <string.h> |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 50 | #include <utility> |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 51 | using namespace clang; |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 52 | using namespace clang::serialization; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 53 | |
Sebastian Redl | ade5000 | 2010-07-30 17:03:48 +0000 | [diff] [blame] | 54 | template <typename T, typename Allocator> |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 55 | static StringRef data(const std::vector<T, Allocator> &v) { |
| 56 | if (v.empty()) return StringRef(); |
| 57 | return StringRef(reinterpret_cast<const char*>(&v[0]), |
Benjamin Kramer | 6e089c6 | 2011-04-24 17:44:50 +0000 | [diff] [blame] | 58 | sizeof(T) * v.size()); |
Sebastian Redl | ade5000 | 2010-07-30 17:03:48 +0000 | [diff] [blame] | 59 | } |
Benjamin Kramer | 6e089c6 | 2011-04-24 17:44:50 +0000 | [diff] [blame] | 60 | |
| 61 | template <typename T> |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 62 | static StringRef data(const SmallVectorImpl<T> &v) { |
| 63 | return StringRef(reinterpret_cast<const char*>(v.data()), |
Benjamin Kramer | 6e089c6 | 2011-04-24 17:44:50 +0000 | [diff] [blame] | 64 | sizeof(T) * v.size()); |
Sebastian Redl | ade5000 | 2010-07-30 17:03:48 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 67 | //===----------------------------------------------------------------------===// |
| 68 | // Type serialization |
| 69 | //===----------------------------------------------------------------------===// |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 70 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 71 | namespace { |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 72 | class ASTTypeWriter { |
Sebastian Redl | a4232eb | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 73 | ASTWriter &Writer; |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 74 | ASTWriter::RecordDataImpl &Record; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 75 | |
| 76 | public: |
| 77 | /// \brief Type code that corresponds to the record generated. |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 78 | TypeCode Code; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 79 | |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 80 | ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record) |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 81 | : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 82 | |
| 83 | void VisitArrayType(const ArrayType *T); |
| 84 | void VisitFunctionType(const FunctionType *T); |
| 85 | void VisitTagType(const TagType *T); |
| 86 | |
| 87 | #define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T); |
| 88 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 89 | #include "clang/AST/TypeNodes.def" |
| 90 | }; |
| 91 | } |
| 92 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 93 | void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 94 | llvm_unreachable("Built-in types are never serialized"); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 97 | void ASTTypeWriter::VisitComplexType(const ComplexType *T) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 98 | Writer.AddTypeRef(T->getElementType(), Record); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 99 | Code = TYPE_COMPLEX; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 102 | void ASTTypeWriter::VisitPointerType(const PointerType *T) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 103 | Writer.AddTypeRef(T->getPointeeType(), Record); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 104 | Code = TYPE_POINTER; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 107 | void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | Writer.AddTypeRef(T->getPointeeType(), Record); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 109 | Code = TYPE_BLOCK_POINTER; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 112 | void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) { |
Richard Smith | df1550f | 2011-04-12 10:38:03 +0000 | [diff] [blame] | 113 | Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record); |
| 114 | Record.push_back(T->isSpelledAsLValue()); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 115 | Code = TYPE_LVALUE_REFERENCE; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 118 | void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) { |
Richard Smith | df1550f | 2011-04-12 10:38:03 +0000 | [diff] [blame] | 119 | Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 120 | Code = TYPE_RVALUE_REFERENCE; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 123 | void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 124 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 125 | Writer.AddTypeRef(QualType(T->getClass(), 0), Record); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 126 | Code = TYPE_MEMBER_POINTER; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 129 | void ASTTypeWriter::VisitArrayType(const ArrayType *T) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 130 | Writer.AddTypeRef(T->getElementType(), Record); |
| 131 | Record.push_back(T->getSizeModifier()); // FIXME: stable values |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 132 | Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 135 | void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 136 | VisitArrayType(T); |
| 137 | Writer.AddAPInt(T->getSize(), Record); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 138 | Code = TYPE_CONSTANT_ARRAY; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 141 | void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 142 | VisitArrayType(T); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 143 | Code = TYPE_INCOMPLETE_ARRAY; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 146 | void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 147 | VisitArrayType(T); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 148 | Writer.AddSourceLocation(T->getLBracketLoc(), Record); |
| 149 | Writer.AddSourceLocation(T->getRBracketLoc(), Record); |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 150 | Writer.AddStmt(T->getSizeExpr()); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 151 | Code = TYPE_VARIABLE_ARRAY; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 154 | void ASTTypeWriter::VisitVectorType(const VectorType *T) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 155 | Writer.AddTypeRef(T->getElementType(), Record); |
| 156 | Record.push_back(T->getNumElements()); |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 157 | Record.push_back(T->getVectorKind()); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 158 | Code = TYPE_VECTOR; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 161 | void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 162 | VisitVectorType(T); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 163 | Code = TYPE_EXT_VECTOR; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 166 | void ASTTypeWriter::VisitFunctionType(const FunctionType *T) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 167 | Writer.AddTypeRef(T->getResultType(), Record); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 168 | FunctionType::ExtInfo C = T->getExtInfo(); |
| 169 | Record.push_back(C.getNoReturn()); |
Eli Friedman | a49218e | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 170 | Record.push_back(C.getHasRegParm()); |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 171 | Record.push_back(C.getRegParm()); |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 172 | // FIXME: need to stabilize encoding of calling convention... |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 173 | Record.push_back(C.getCC()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 174 | Record.push_back(C.getProducesResult()); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 177 | void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 178 | VisitFunctionType(T); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 179 | Code = TYPE_FUNCTION_NO_PROTO; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 182 | void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 183 | VisitFunctionType(T); |
| 184 | Record.push_back(T->getNumArgs()); |
| 185 | for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I) |
| 186 | Writer.AddTypeRef(T->getArgType(I), Record); |
| 187 | Record.push_back(T->isVariadic()); |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 188 | Record.push_back(T->hasTrailingReturn()); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 189 | Record.push_back(T->getTypeQuals()); |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 190 | Record.push_back(static_cast<unsigned>(T->getRefQualifier())); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 191 | Record.push_back(T->getExceptionSpecType()); |
| 192 | if (T->getExceptionSpecType() == EST_Dynamic) { |
| 193 | Record.push_back(T->getNumExceptions()); |
| 194 | for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I) |
| 195 | Writer.AddTypeRef(T->getExceptionType(I), Record); |
| 196 | } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) { |
| 197 | Writer.AddStmt(T->getNoexceptExpr()); |
| 198 | } |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 199 | Code = TYPE_FUNCTION_PROTO; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 202 | void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 203 | Writer.AddDeclRef(T->getDecl(), Record); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 204 | Code = TYPE_UNRESOLVED_USING; |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 205 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 206 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 207 | void ASTTypeWriter::VisitTypedefType(const TypedefType *T) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 208 | Writer.AddDeclRef(T->getDecl(), Record); |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 209 | assert(!T->isCanonicalUnqualified() && "Invalid typedef ?"); |
| 210 | Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 211 | Code = TYPE_TYPEDEF; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 214 | void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) { |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 215 | Writer.AddStmt(T->getUnderlyingExpr()); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 216 | Code = TYPE_TYPEOF_EXPR; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 219 | void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 220 | Writer.AddTypeRef(T->getUnderlyingType(), Record); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 221 | Code = TYPE_TYPEOF; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 224 | void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 225 | Writer.AddTypeRef(T->getUnderlyingType(), Record); |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 226 | Writer.AddStmt(T->getUnderlyingExpr()); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 227 | Code = TYPE_DECLTYPE; |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 230 | void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) { |
| 231 | Writer.AddTypeRef(T->getBaseType(), Record); |
| 232 | Writer.AddTypeRef(T->getUnderlyingType(), Record); |
| 233 | Record.push_back(T->getUTTKind()); |
| 234 | Code = TYPE_UNARY_TRANSFORM; |
| 235 | } |
| 236 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 237 | void ASTTypeWriter::VisitAutoType(const AutoType *T) { |
| 238 | Writer.AddTypeRef(T->getDeducedType(), Record); |
| 239 | Code = TYPE_AUTO; |
| 240 | } |
| 241 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 242 | void ASTTypeWriter::VisitTagType(const TagType *T) { |
Argyrios Kyrtzidis | be19110 | 2010-07-08 13:09:53 +0000 | [diff] [blame] | 243 | Record.push_back(T->isDependentType()); |
Douglas Gregor | 56ca8a9 | 2012-01-17 19:21:53 +0000 | [diff] [blame] | 244 | Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | assert(!T->isBeingDefined() && |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 246 | "Cannot serialize in the middle of a type definition"); |
| 247 | } |
| 248 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 249 | void ASTTypeWriter::VisitRecordType(const RecordType *T) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 250 | VisitTagType(T); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 251 | Code = TYPE_RECORD; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 254 | void ASTTypeWriter::VisitEnumType(const EnumType *T) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 255 | VisitTagType(T); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 256 | Code = TYPE_ENUM; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 257 | } |
| 258 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 259 | void ASTTypeWriter::VisitAttributedType(const AttributedType *T) { |
| 260 | Writer.AddTypeRef(T->getModifiedType(), Record); |
| 261 | Writer.AddTypeRef(T->getEquivalentType(), Record); |
| 262 | Record.push_back(T->getAttrKind()); |
| 263 | Code = TYPE_ATTRIBUTED; |
| 264 | } |
| 265 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 266 | void |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 267 | ASTTypeWriter::VisitSubstTemplateTypeParmType( |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 268 | const SubstTemplateTypeParmType *T) { |
| 269 | Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record); |
| 270 | Writer.AddTypeRef(T->getReplacementType(), Record); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 271 | Code = TYPE_SUBST_TEMPLATE_TYPE_PARM; |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | void |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 275 | ASTTypeWriter::VisitSubstTemplateTypeParmPackType( |
| 276 | const SubstTemplateTypeParmPackType *T) { |
| 277 | Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record); |
| 278 | Writer.AddTemplateArgument(T->getArgumentPack(), Record); |
| 279 | Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK; |
| 280 | } |
| 281 | |
| 282 | void |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 283 | ASTTypeWriter::VisitTemplateSpecializationType( |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 284 | const TemplateSpecializationType *T) { |
Argyrios Kyrtzidis | be19110 | 2010-07-08 13:09:53 +0000 | [diff] [blame] | 285 | Record.push_back(T->isDependentType()); |
Argyrios Kyrtzidis | 90b715e | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 286 | Writer.AddTemplateName(T->getTemplateName(), Record); |
| 287 | Record.push_back(T->getNumArgs()); |
| 288 | for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end(); |
| 289 | ArgI != ArgE; ++ArgI) |
| 290 | Writer.AddTemplateArgument(*ArgI, Record); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 291 | Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() : |
| 292 | T->isCanonicalUnqualified() ? QualType() |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 293 | : T->getCanonicalTypeInternal(), |
| 294 | Record); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 295 | Code = TYPE_TEMPLATE_SPECIALIZATION; |
Argyrios Kyrtzidis | 90b715e | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | void |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 299 | ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) { |
Argyrios Kyrtzidis | ae8b17f | 2010-06-30 08:49:25 +0000 | [diff] [blame] | 300 | VisitArrayType(T); |
| 301 | Writer.AddStmt(T->getSizeExpr()); |
| 302 | Writer.AddSourceRange(T->getBracketsRange(), Record); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 303 | Code = TYPE_DEPENDENT_SIZED_ARRAY; |
Argyrios Kyrtzidis | 90b715e | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | void |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 307 | ASTTypeWriter::VisitDependentSizedExtVectorType( |
Argyrios Kyrtzidis | 90b715e | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 308 | const DependentSizedExtVectorType *T) { |
| 309 | // FIXME: Serialize this type (C++ only) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 310 | llvm_unreachable("Cannot serialize dependent sized extended vector types"); |
Argyrios Kyrtzidis | 90b715e | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | void |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 314 | ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
Argyrios Kyrtzidis | 90b715e | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 315 | Record.push_back(T->getDepth()); |
| 316 | Record.push_back(T->getIndex()); |
| 317 | Record.push_back(T->isParameterPack()); |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 318 | Writer.AddDeclRef(T->getDecl(), Record); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 319 | Code = TYPE_TEMPLATE_TYPE_PARM; |
Argyrios Kyrtzidis | 90b715e | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | void |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 323 | ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) { |
Argyrios Kyrtzidis | 8dfbd8b | 2010-06-24 08:57:31 +0000 | [diff] [blame] | 324 | Record.push_back(T->getKeyword()); |
| 325 | Writer.AddNestedNameSpecifier(T->getQualifier(), Record); |
| 326 | Writer.AddIdentifierRef(T->getIdentifier(), Record); |
Argyrios Kyrtzidis | f48d45e | 2010-07-02 11:55:24 +0000 | [diff] [blame] | 327 | Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType() |
| 328 | : T->getCanonicalTypeInternal(), |
| 329 | Record); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 330 | Code = TYPE_DEPENDENT_NAME; |
Argyrios Kyrtzidis | 90b715e | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | void |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 334 | ASTTypeWriter::VisitDependentTemplateSpecializationType( |
Argyrios Kyrtzidis | 90b715e | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 335 | const DependentTemplateSpecializationType *T) { |
Argyrios Kyrtzidis | 3acad62 | 2010-06-25 16:24:58 +0000 | [diff] [blame] | 336 | Record.push_back(T->getKeyword()); |
| 337 | Writer.AddNestedNameSpecifier(T->getQualifier(), Record); |
| 338 | Writer.AddIdentifierRef(T->getIdentifier(), Record); |
| 339 | Record.push_back(T->getNumArgs()); |
| 340 | for (DependentTemplateSpecializationType::iterator |
| 341 | I = T->begin(), E = T->end(); I != E; ++I) |
| 342 | Writer.AddTemplateArgument(*I, Record); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 343 | Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 346 | void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) { |
| 347 | Writer.AddTypeRef(T->getPattern(), Record); |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 348 | if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions()) |
| 349 | Record.push_back(*NumExpansions + 1); |
| 350 | else |
| 351 | Record.push_back(0); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 352 | Code = TYPE_PACK_EXPANSION; |
| 353 | } |
| 354 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 355 | void ASTTypeWriter::VisitParenType(const ParenType *T) { |
| 356 | Writer.AddTypeRef(T->getInnerType(), Record); |
| 357 | Code = TYPE_PAREN; |
| 358 | } |
| 359 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 360 | void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 361 | Record.push_back(T->getKeyword()); |
Argyrios Kyrtzidis | 3acad62 | 2010-06-25 16:24:58 +0000 | [diff] [blame] | 362 | Writer.AddNestedNameSpecifier(T->getQualifier(), Record); |
| 363 | Writer.AddTypeRef(T->getNamedType(), Record); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 364 | Code = TYPE_ELABORATED; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 367 | void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 368 | Writer.AddDeclRef(T->getDecl(), Record); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 369 | Writer.AddTypeRef(T->getInjectedSpecializationType(), Record); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 370 | Code = TYPE_INJECTED_CLASS_NAME; |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 373 | void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { |
Douglas Gregor | 56ca8a9 | 2012-01-17 19:21:53 +0000 | [diff] [blame] | 374 | Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 375 | Code = TYPE_OBJC_INTERFACE; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 378 | void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 379 | Writer.AddTypeRef(T->getBaseType(), Record); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 380 | Record.push_back(T->getNumProtocols()); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 381 | for (ObjCObjectType::qual_iterator I = T->qual_begin(), |
Steve Naroff | 446ee4e | 2009-05-27 16:21:00 +0000 | [diff] [blame] | 382 | E = T->qual_end(); I != E; ++I) |
| 383 | Writer.AddDeclRef(*I, Record); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 384 | Code = TYPE_OBJC_OBJECT; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 387 | void |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 388 | ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 389 | Writer.AddTypeRef(T->getPointeeType(), Record); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 390 | Code = TYPE_OBJC_OBJECT_POINTER; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 393 | void |
| 394 | ASTTypeWriter::VisitAtomicType(const AtomicType *T) { |
| 395 | Writer.AddTypeRef(T->getValueType(), Record); |
| 396 | Code = TYPE_ATOMIC; |
| 397 | } |
| 398 | |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 399 | namespace { |
| 400 | |
| 401 | class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> { |
Sebastian Redl | a4232eb | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 402 | ASTWriter &Writer; |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 403 | ASTWriter::RecordDataImpl &Record; |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 404 | |
| 405 | public: |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 406 | TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record) |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 407 | : Writer(Writer), Record(Record) { } |
| 408 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 409 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 410 | #define TYPELOC(CLASS, PARENT) \ |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 411 | void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 412 | #include "clang/AST/TypeLocNodes.def" |
| 413 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 414 | void VisitArrayTypeLoc(ArrayTypeLoc TyLoc); |
| 415 | void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 416 | }; |
| 417 | |
| 418 | } |
| 419 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 420 | void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
| 421 | // nothing to do |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 422 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 423 | void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
Douglas Gregor | ddf889a | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 424 | Writer.AddSourceLocation(TL.getBuiltinLoc(), Record); |
| 425 | if (TL.needsExtraLocalData()) { |
| 426 | Record.push_back(TL.getWrittenTypeSpec()); |
| 427 | Record.push_back(TL.getWrittenSignSpec()); |
| 428 | Record.push_back(TL.getWrittenWidthSpec()); |
| 429 | Record.push_back(TL.hasModeAttr()); |
| 430 | } |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 431 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 432 | void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) { |
| 433 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 434 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 435 | void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 436 | Writer.AddSourceLocation(TL.getStarLoc(), Record); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 437 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 438 | void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 439 | Writer.AddSourceLocation(TL.getCaretLoc(), Record); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 440 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 441 | void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
| 442 | Writer.AddSourceLocation(TL.getAmpLoc(), Record); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 443 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 444 | void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
| 445 | Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 446 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 447 | void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 448 | Writer.AddSourceLocation(TL.getStarLoc(), Record); |
Abramo Bagnara | b6ab6c1 | 2011-03-05 14:42:21 +0000 | [diff] [blame] | 449 | Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 450 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 451 | void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 452 | Writer.AddSourceLocation(TL.getLBracketLoc(), Record); |
| 453 | Writer.AddSourceLocation(TL.getRBracketLoc(), Record); |
| 454 | Record.push_back(TL.getSizeExpr() ? 1 : 0); |
| 455 | if (TL.getSizeExpr()) |
| 456 | Writer.AddStmt(TL.getSizeExpr()); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 457 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 458 | void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { |
| 459 | VisitArrayTypeLoc(TL); |
| 460 | } |
| 461 | void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { |
| 462 | VisitArrayTypeLoc(TL); |
| 463 | } |
| 464 | void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { |
| 465 | VisitArrayTypeLoc(TL); |
| 466 | } |
| 467 | void TypeLocWriter::VisitDependentSizedArrayTypeLoc( |
| 468 | DependentSizedArrayTypeLoc TL) { |
| 469 | VisitArrayTypeLoc(TL); |
| 470 | } |
| 471 | void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc( |
| 472 | DependentSizedExtVectorTypeLoc TL) { |
| 473 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 474 | } |
| 475 | void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) { |
| 476 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 477 | } |
| 478 | void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { |
| 479 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 480 | } |
| 481 | void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) { |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 482 | Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record); |
| 483 | Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record); |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 484 | Record.push_back(TL.getTrailingReturn()); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 485 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 486 | Writer.AddDeclRef(TL.getArg(i), Record); |
| 487 | } |
| 488 | void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { |
| 489 | VisitFunctionTypeLoc(TL); |
| 490 | } |
| 491 | void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { |
| 492 | VisitFunctionTypeLoc(TL); |
| 493 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 494 | void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { |
| 495 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 496 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 497 | void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
| 498 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 499 | } |
| 500 | void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 501 | Writer.AddSourceLocation(TL.getTypeofLoc(), Record); |
| 502 | Writer.AddSourceLocation(TL.getLParenLoc(), Record); |
| 503 | Writer.AddSourceLocation(TL.getRParenLoc(), Record); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 504 | } |
| 505 | void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 506 | Writer.AddSourceLocation(TL.getTypeofLoc(), Record); |
| 507 | Writer.AddSourceLocation(TL.getLParenLoc(), Record); |
| 508 | Writer.AddSourceLocation(TL.getRParenLoc(), Record); |
| 509 | Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 510 | } |
| 511 | void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { |
| 512 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 513 | } |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 514 | void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { |
| 515 | Writer.AddSourceLocation(TL.getKWLoc(), Record); |
| 516 | Writer.AddSourceLocation(TL.getLParenLoc(), Record); |
| 517 | Writer.AddSourceLocation(TL.getRParenLoc(), Record); |
| 518 | Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record); |
| 519 | } |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 520 | void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) { |
| 521 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 522 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 523 | void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) { |
| 524 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 525 | } |
| 526 | void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) { |
| 527 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 528 | } |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 529 | void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) { |
| 530 | Writer.AddSourceLocation(TL.getAttrNameLoc(), Record); |
| 531 | if (TL.hasAttrOperand()) { |
| 532 | SourceRange range = TL.getAttrOperandParensRange(); |
| 533 | Writer.AddSourceLocation(range.getBegin(), Record); |
| 534 | Writer.AddSourceLocation(range.getEnd(), Record); |
| 535 | } |
| 536 | if (TL.hasAttrExprOperand()) { |
| 537 | Expr *operand = TL.getAttrExprOperand(); |
| 538 | Record.push_back(operand ? 1 : 0); |
| 539 | if (operand) Writer.AddStmt(operand); |
| 540 | } else if (TL.hasAttrEnumOperand()) { |
| 541 | Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record); |
| 542 | } |
| 543 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 544 | void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 545 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 546 | } |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 547 | void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc( |
| 548 | SubstTemplateTypeParmTypeLoc TL) { |
| 549 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 550 | } |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 551 | void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc( |
| 552 | SubstTemplateTypeParmPackTypeLoc TL) { |
| 553 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 554 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 555 | void TypeLocWriter::VisitTemplateSpecializationTypeLoc( |
| 556 | TemplateSpecializationTypeLoc TL) { |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 557 | Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 558 | Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record); |
| 559 | Writer.AddSourceLocation(TL.getLAngleLoc(), Record); |
| 560 | Writer.AddSourceLocation(TL.getRAngleLoc(), Record); |
| 561 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
Argyrios Kyrtzidis | 44f8c37 | 2010-06-22 09:54:59 +0000 | [diff] [blame] | 562 | Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(), |
| 563 | TL.getArgLoc(i).getLocInfo(), Record); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 564 | } |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 565 | void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) { |
| 566 | Writer.AddSourceLocation(TL.getLParenLoc(), Record); |
| 567 | Writer.AddSourceLocation(TL.getRParenLoc(), Record); |
| 568 | } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 569 | void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 570 | Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 571 | Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 572 | } |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 573 | void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { |
| 574 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 575 | } |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 576 | void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 577 | Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 578 | Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 579 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 580 | } |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 581 | void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc( |
| 582 | DependentTemplateSpecializationTypeLoc TL) { |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 583 | Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record); |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 584 | Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record); |
Abramo Bagnara | 66581d4 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 585 | Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 586 | Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 587 | Writer.AddSourceLocation(TL.getLAngleLoc(), Record); |
| 588 | Writer.AddSourceLocation(TL.getRAngleLoc(), Record); |
| 589 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) |
Argyrios Kyrtzidis | 44f8c37 | 2010-06-22 09:54:59 +0000 | [diff] [blame] | 590 | Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(), |
| 591 | TL.getArgLoc(I).getLocInfo(), Record); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 592 | } |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 593 | void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { |
| 594 | Writer.AddSourceLocation(TL.getEllipsisLoc(), Record); |
| 595 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 596 | void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
| 597 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 598 | } |
| 599 | void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { |
| 600 | Record.push_back(TL.hasBaseTypeAsWritten()); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 601 | Writer.AddSourceLocation(TL.getLAngleLoc(), Record); |
| 602 | Writer.AddSourceLocation(TL.getRAngleLoc(), Record); |
| 603 | for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) |
| 604 | Writer.AddSourceLocation(TL.getProtocolLoc(i), Record); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 605 | } |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 606 | void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
| 607 | Writer.AddSourceLocation(TL.getStarLoc(), Record); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 608 | } |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 609 | void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) { |
| 610 | Writer.AddSourceLocation(TL.getKWLoc(), Record); |
| 611 | Writer.AddSourceLocation(TL.getLParenLoc(), Record); |
| 612 | Writer.AddSourceLocation(TL.getRParenLoc(), Record); |
| 613 | } |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 614 | |
Chris Lattner | 4dcf151a | 2009-04-22 05:57:30 +0000 | [diff] [blame] | 615 | //===----------------------------------------------------------------------===// |
Sebastian Redl | a4232eb | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 616 | // ASTWriter Implementation |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 617 | //===----------------------------------------------------------------------===// |
| 618 | |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 619 | static void EmitBlockID(unsigned ID, const char *Name, |
| 620 | llvm::BitstreamWriter &Stream, |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 621 | ASTWriter::RecordDataImpl &Record) { |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 622 | Record.clear(); |
| 623 | Record.push_back(ID); |
| 624 | Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record); |
| 625 | |
| 626 | // Emit the block name if present. |
| 627 | if (Name == 0 || Name[0] == 0) return; |
| 628 | Record.clear(); |
| 629 | while (*Name) |
| 630 | Record.push_back(*Name++); |
| 631 | Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record); |
| 632 | } |
| 633 | |
| 634 | static void EmitRecordID(unsigned ID, const char *Name, |
| 635 | llvm::BitstreamWriter &Stream, |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 636 | ASTWriter::RecordDataImpl &Record) { |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 637 | Record.clear(); |
| 638 | Record.push_back(ID); |
| 639 | while (*Name) |
| 640 | Record.push_back(*Name++); |
| 641 | Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record); |
Chris Lattner | 0558df2 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | static void AddStmtsExprs(llvm::BitstreamWriter &Stream, |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 645 | ASTWriter::RecordDataImpl &Record) { |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 646 | #define RECORD(X) EmitRecordID(X, #X, Stream, Record) |
Chris Lattner | 0558df2 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 647 | RECORD(STMT_STOP); |
| 648 | RECORD(STMT_NULL_PTR); |
| 649 | RECORD(STMT_NULL); |
| 650 | RECORD(STMT_COMPOUND); |
| 651 | RECORD(STMT_CASE); |
| 652 | RECORD(STMT_DEFAULT); |
| 653 | RECORD(STMT_LABEL); |
| 654 | RECORD(STMT_IF); |
| 655 | RECORD(STMT_SWITCH); |
| 656 | RECORD(STMT_WHILE); |
| 657 | RECORD(STMT_DO); |
| 658 | RECORD(STMT_FOR); |
| 659 | RECORD(STMT_GOTO); |
| 660 | RECORD(STMT_INDIRECT_GOTO); |
| 661 | RECORD(STMT_CONTINUE); |
| 662 | RECORD(STMT_BREAK); |
| 663 | RECORD(STMT_RETURN); |
| 664 | RECORD(STMT_DECL); |
| 665 | RECORD(STMT_ASM); |
| 666 | RECORD(EXPR_PREDEFINED); |
| 667 | RECORD(EXPR_DECL_REF); |
| 668 | RECORD(EXPR_INTEGER_LITERAL); |
| 669 | RECORD(EXPR_FLOATING_LITERAL); |
| 670 | RECORD(EXPR_IMAGINARY_LITERAL); |
| 671 | RECORD(EXPR_STRING_LITERAL); |
| 672 | RECORD(EXPR_CHARACTER_LITERAL); |
| 673 | RECORD(EXPR_PAREN); |
| 674 | RECORD(EXPR_UNARY_OPERATOR); |
| 675 | RECORD(EXPR_SIZEOF_ALIGN_OF); |
| 676 | RECORD(EXPR_ARRAY_SUBSCRIPT); |
| 677 | RECORD(EXPR_CALL); |
| 678 | RECORD(EXPR_MEMBER); |
| 679 | RECORD(EXPR_BINARY_OPERATOR); |
| 680 | RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR); |
| 681 | RECORD(EXPR_CONDITIONAL_OPERATOR); |
| 682 | RECORD(EXPR_IMPLICIT_CAST); |
| 683 | RECORD(EXPR_CSTYLE_CAST); |
| 684 | RECORD(EXPR_COMPOUND_LITERAL); |
| 685 | RECORD(EXPR_EXT_VECTOR_ELEMENT); |
| 686 | RECORD(EXPR_INIT_LIST); |
| 687 | RECORD(EXPR_DESIGNATED_INIT); |
| 688 | RECORD(EXPR_IMPLICIT_VALUE_INIT); |
| 689 | RECORD(EXPR_VA_ARG); |
| 690 | RECORD(EXPR_ADDR_LABEL); |
| 691 | RECORD(EXPR_STMT); |
Chris Lattner | 0558df2 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 692 | RECORD(EXPR_CHOOSE); |
| 693 | RECORD(EXPR_GNU_NULL); |
| 694 | RECORD(EXPR_SHUFFLE_VECTOR); |
| 695 | RECORD(EXPR_BLOCK); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 696 | RECORD(EXPR_GENERIC_SELECTION); |
Chris Lattner | 0558df2 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 697 | RECORD(EXPR_OBJC_STRING_LITERAL); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 698 | RECORD(EXPR_OBJC_NUMERIC_LITERAL); |
| 699 | RECORD(EXPR_OBJC_ARRAY_LITERAL); |
| 700 | RECORD(EXPR_OBJC_DICTIONARY_LITERAL); |
Chris Lattner | 0558df2 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 701 | RECORD(EXPR_OBJC_ENCODE); |
| 702 | RECORD(EXPR_OBJC_SELECTOR_EXPR); |
| 703 | RECORD(EXPR_OBJC_PROTOCOL_EXPR); |
| 704 | RECORD(EXPR_OBJC_IVAR_REF_EXPR); |
| 705 | RECORD(EXPR_OBJC_PROPERTY_REF_EXPR); |
| 706 | RECORD(EXPR_OBJC_KVC_REF_EXPR); |
| 707 | RECORD(EXPR_OBJC_MESSAGE_EXPR); |
Chris Lattner | 0558df2 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 708 | RECORD(STMT_OBJC_FOR_COLLECTION); |
| 709 | RECORD(STMT_OBJC_CATCH); |
| 710 | RECORD(STMT_OBJC_FINALLY); |
| 711 | RECORD(STMT_OBJC_AT_TRY); |
| 712 | RECORD(STMT_OBJC_AT_SYNCHRONIZED); |
| 713 | RECORD(STMT_OBJC_AT_THROW); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 714 | RECORD(EXPR_OBJC_BOOL_LITERAL); |
Sam Weinig | eb7f961 | 2010-02-07 06:32:43 +0000 | [diff] [blame] | 715 | RECORD(EXPR_CXX_OPERATOR_CALL); |
| 716 | RECORD(EXPR_CXX_CONSTRUCT); |
| 717 | RECORD(EXPR_CXX_STATIC_CAST); |
| 718 | RECORD(EXPR_CXX_DYNAMIC_CAST); |
| 719 | RECORD(EXPR_CXX_REINTERPRET_CAST); |
| 720 | RECORD(EXPR_CXX_CONST_CAST); |
| 721 | RECORD(EXPR_CXX_FUNCTIONAL_CAST); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 722 | RECORD(EXPR_USER_DEFINED_LITERAL); |
Sam Weinig | eb7f961 | 2010-02-07 06:32:43 +0000 | [diff] [blame] | 723 | RECORD(EXPR_CXX_BOOL_LITERAL); |
| 724 | RECORD(EXPR_CXX_NULL_PTR_LITERAL); |
Douglas Gregor | b6c2b3f | 2011-02-08 16:34:17 +0000 | [diff] [blame] | 725 | RECORD(EXPR_CXX_TYPEID_EXPR); |
| 726 | RECORD(EXPR_CXX_TYPEID_TYPE); |
| 727 | RECORD(EXPR_CXX_UUIDOF_EXPR); |
| 728 | RECORD(EXPR_CXX_UUIDOF_TYPE); |
| 729 | RECORD(EXPR_CXX_THIS); |
| 730 | RECORD(EXPR_CXX_THROW); |
| 731 | RECORD(EXPR_CXX_DEFAULT_ARG); |
| 732 | RECORD(EXPR_CXX_BIND_TEMPORARY); |
| 733 | RECORD(EXPR_CXX_SCALAR_VALUE_INIT); |
| 734 | RECORD(EXPR_CXX_NEW); |
| 735 | RECORD(EXPR_CXX_DELETE); |
| 736 | RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR); |
| 737 | RECORD(EXPR_EXPR_WITH_CLEANUPS); |
| 738 | RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER); |
| 739 | RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF); |
| 740 | RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT); |
| 741 | RECORD(EXPR_CXX_UNRESOLVED_MEMBER); |
| 742 | RECORD(EXPR_CXX_UNRESOLVED_LOOKUP); |
| 743 | RECORD(EXPR_CXX_UNARY_TYPE_TRAIT); |
| 744 | RECORD(EXPR_CXX_NOEXCEPT); |
| 745 | RECORD(EXPR_OPAQUE_VALUE); |
| 746 | RECORD(EXPR_BINARY_TYPE_TRAIT); |
| 747 | RECORD(EXPR_PACK_EXPANSION); |
| 748 | RECORD(EXPR_SIZEOF_PACK); |
| 749 | RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK); |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 750 | RECORD(EXPR_CUDA_KERNEL_CALL); |
Chris Lattner | 0558df2 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 751 | #undef RECORD |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 752 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 753 | |
Sebastian Redl | a4232eb | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 754 | void ASTWriter::WriteBlockInfoBlock() { |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 755 | RecordData Record; |
| 756 | Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 757 | |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 758 | #define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record) |
| 759 | #define RECORD(X) EmitRecordID(X, #X, Stream, Record) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 760 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 761 | // AST Top-Level Block. |
Sebastian Redl | f29f0a2 | 2010-08-18 23:57:22 +0000 | [diff] [blame] | 762 | BLOCK(AST_BLOCK); |
Zhongxing Xu | 51e774d | 2009-06-03 09:23:28 +0000 | [diff] [blame] | 763 | RECORD(ORIGINAL_FILE_NAME); |
Douglas Gregor | 31d375f | 2011-05-06 21:43:30 +0000 | [diff] [blame] | 764 | RECORD(ORIGINAL_FILE_ID); |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 765 | RECORD(TYPE_OFFSET); |
| 766 | RECORD(DECL_OFFSET); |
| 767 | RECORD(LANGUAGE_OPTIONS); |
Douglas Gregor | ab41e63 | 2009-04-27 22:23:34 +0000 | [diff] [blame] | 768 | RECORD(METADATA); |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 769 | RECORD(IDENTIFIER_OFFSET); |
| 770 | RECORD(IDENTIFIER_TABLE); |
| 771 | RECORD(EXTERNAL_DEFINITIONS); |
| 772 | RECORD(SPECIAL_TYPES); |
| 773 | RECORD(STATISTICS); |
| 774 | RECORD(TENTATIVE_DEFINITIONS); |
Argyrios Kyrtzidis | 49b96d1 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 775 | RECORD(UNUSED_FILESCOPED_DECLS); |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 776 | RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS); |
| 777 | RECORD(SELECTOR_OFFSETS); |
| 778 | RECORD(METHOD_POOL); |
| 779 | RECORD(PP_COUNTER_VALUE); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 780 | RECORD(SOURCE_LOCATION_OFFSETS); |
| 781 | RECORD(SOURCE_LOCATION_PRELOADS); |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 782 | RECORD(STAT_CACHE); |
Douglas Gregor | b81c170 | 2009-04-27 20:06:05 +0000 | [diff] [blame] | 783 | RECORD(EXT_VECTOR_DECLS); |
Ted Kremenek | 5b4ec63 | 2010-01-22 20:59:36 +0000 | [diff] [blame] | 784 | RECORD(VERSION_CONTROL_BRANCH_REVISION); |
Argyrios Kyrtzidis | e24692b | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 785 | RECORD(PPD_ENTITIES_OFFSETS); |
Douglas Gregor | e95b919 | 2011-08-17 21:07:30 +0000 | [diff] [blame] | 786 | RECORD(IMPORTS); |
Fariborz Jahanian | 3201983 | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 787 | RECORD(REFERENCED_SELECTOR_POOL); |
Douglas Gregor | b6c2b3f | 2011-02-08 16:34:17 +0000 | [diff] [blame] | 788 | RECORD(TU_UPDATE_LEXICAL); |
Douglas Gregor | 2171bf1 | 2012-01-15 16:58:34 +0000 | [diff] [blame] | 789 | RECORD(LOCAL_REDECLARATIONS_MAP); |
Douglas Gregor | b6c2b3f | 2011-02-08 16:34:17 +0000 | [diff] [blame] | 790 | RECORD(SEMA_DECL_REFS); |
| 791 | RECORD(WEAK_UNDECLARED_IDENTIFIERS); |
| 792 | RECORD(PENDING_IMPLICIT_INSTANTIATIONS); |
| 793 | RECORD(DECL_REPLACEMENTS); |
| 794 | RECORD(UPDATE_VISIBLE); |
| 795 | RECORD(DECL_UPDATE_OFFSETS); |
| 796 | RECORD(DECL_UPDATES); |
| 797 | RECORD(CXX_BASE_SPECIFIER_OFFSETS); |
| 798 | RECORD(DIAG_PRAGMA_MAPPINGS); |
Peter Collingbourne | 84bccea | 2011-02-15 19:46:30 +0000 | [diff] [blame] | 799 | RECORD(CUDA_SPECIAL_DECL_REFS); |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 800 | RECORD(HEADER_SEARCH_TABLE); |
Douglas Gregor | 837593f | 2011-08-04 16:39:39 +0000 | [diff] [blame] | 801 | RECORD(ORIGINAL_PCH_DIR); |
Peter Collingbourne | 84bccea | 2011-02-15 19:46:30 +0000 | [diff] [blame] | 802 | RECORD(FP_PRAGMA_OPTIONS); |
| 803 | RECORD(OPENCL_EXTENSIONS); |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 804 | RECORD(DELEGATING_CTORS); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 805 | RECORD(FILE_SOURCE_LOCATION_OFFSETS); |
| 806 | RECORD(KNOWN_NAMESPACES); |
Douglas Gregor | 837593f | 2011-08-04 16:39:39 +0000 | [diff] [blame] | 807 | RECORD(MODULE_OFFSET_MAP); |
| 808 | RECORD(SOURCE_MANAGER_LINE_TABLE); |
Douglas Gregor | cff9f26 | 2012-01-27 01:47:08 +0000 | [diff] [blame] | 809 | RECORD(OBJC_CATEGORIES_MAP); |
Douglas Gregor | a126651 | 2011-12-19 21:09:25 +0000 | [diff] [blame] | 810 | RECORD(FILE_SORTED_DECLS); |
| 811 | RECORD(IMPORTED_MODULES); |
Douglas Gregor | 2171bf1 | 2012-01-15 16:58:34 +0000 | [diff] [blame] | 812 | RECORD(MERGED_DECLARATIONS); |
| 813 | RECORD(LOCAL_REDECLARATIONS); |
Douglas Gregor | cff9f26 | 2012-01-27 01:47:08 +0000 | [diff] [blame] | 814 | RECORD(OBJC_CATEGORIES); |
Douglas Gregor | 2171bf1 | 2012-01-15 16:58:34 +0000 | [diff] [blame] | 815 | |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 816 | // SourceManager Block. |
Chris Lattner | 2f4efd1 | 2009-04-27 00:40:25 +0000 | [diff] [blame] | 817 | BLOCK(SOURCE_MANAGER_BLOCK); |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 818 | RECORD(SM_SLOC_FILE_ENTRY); |
| 819 | RECORD(SM_SLOC_BUFFER_ENTRY); |
| 820 | RECORD(SM_SLOC_BUFFER_BLOB); |
Chandler Carruth | f70d12d | 2011-07-15 07:25:21 +0000 | [diff] [blame] | 821 | RECORD(SM_SLOC_EXPANSION_ENTRY); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 822 | |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 823 | // Preprocessor Block. |
Chris Lattner | 2f4efd1 | 2009-04-27 00:40:25 +0000 | [diff] [blame] | 824 | BLOCK(PREPROCESSOR_BLOCK); |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 825 | RECORD(PP_MACRO_OBJECT_LIKE); |
| 826 | RECORD(PP_MACRO_FUNCTION_LIKE); |
| 827 | RECORD(PP_TOKEN); |
Douglas Gregor | b6c2b3f | 2011-02-08 16:34:17 +0000 | [diff] [blame] | 828 | |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 829 | // Decls and Types block. |
| 830 | BLOCK(DECLTYPES_BLOCK); |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 831 | RECORD(TYPE_EXT_QUAL); |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 832 | RECORD(TYPE_COMPLEX); |
| 833 | RECORD(TYPE_POINTER); |
| 834 | RECORD(TYPE_BLOCK_POINTER); |
| 835 | RECORD(TYPE_LVALUE_REFERENCE); |
| 836 | RECORD(TYPE_RVALUE_REFERENCE); |
| 837 | RECORD(TYPE_MEMBER_POINTER); |
| 838 | RECORD(TYPE_CONSTANT_ARRAY); |
| 839 | RECORD(TYPE_INCOMPLETE_ARRAY); |
| 840 | RECORD(TYPE_VARIABLE_ARRAY); |
| 841 | RECORD(TYPE_VECTOR); |
| 842 | RECORD(TYPE_EXT_VECTOR); |
| 843 | RECORD(TYPE_FUNCTION_PROTO); |
| 844 | RECORD(TYPE_FUNCTION_NO_PROTO); |
| 845 | RECORD(TYPE_TYPEDEF); |
| 846 | RECORD(TYPE_TYPEOF_EXPR); |
| 847 | RECORD(TYPE_TYPEOF); |
| 848 | RECORD(TYPE_RECORD); |
| 849 | RECORD(TYPE_ENUM); |
| 850 | RECORD(TYPE_OBJC_INTERFACE); |
John McCall | a53d2cb | 2010-05-16 02:12:35 +0000 | [diff] [blame] | 851 | RECORD(TYPE_OBJC_OBJECT); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 852 | RECORD(TYPE_OBJC_OBJECT_POINTER); |
Douglas Gregor | b6c2b3f | 2011-02-08 16:34:17 +0000 | [diff] [blame] | 853 | RECORD(TYPE_DECLTYPE); |
| 854 | RECORD(TYPE_ELABORATED); |
| 855 | RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM); |
| 856 | RECORD(TYPE_UNRESOLVED_USING); |
| 857 | RECORD(TYPE_INJECTED_CLASS_NAME); |
| 858 | RECORD(TYPE_OBJC_OBJECT); |
| 859 | RECORD(TYPE_TEMPLATE_TYPE_PARM); |
| 860 | RECORD(TYPE_TEMPLATE_SPECIALIZATION); |
| 861 | RECORD(TYPE_DEPENDENT_NAME); |
| 862 | RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION); |
| 863 | RECORD(TYPE_DEPENDENT_SIZED_ARRAY); |
| 864 | RECORD(TYPE_PAREN); |
| 865 | RECORD(TYPE_PACK_EXPANSION); |
| 866 | RECORD(TYPE_ATTRIBUTED); |
| 867 | RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 868 | RECORD(TYPE_ATOMIC); |
Chris Lattner | 0ff8cda | 2009-04-26 22:32:16 +0000 | [diff] [blame] | 869 | RECORD(DECL_TYPEDEF); |
| 870 | RECORD(DECL_ENUM); |
| 871 | RECORD(DECL_RECORD); |
| 872 | RECORD(DECL_ENUM_CONSTANT); |
| 873 | RECORD(DECL_FUNCTION); |
| 874 | RECORD(DECL_OBJC_METHOD); |
| 875 | RECORD(DECL_OBJC_INTERFACE); |
| 876 | RECORD(DECL_OBJC_PROTOCOL); |
| 877 | RECORD(DECL_OBJC_IVAR); |
| 878 | RECORD(DECL_OBJC_AT_DEFS_FIELD); |
Chris Lattner | 0ff8cda | 2009-04-26 22:32:16 +0000 | [diff] [blame] | 879 | RECORD(DECL_OBJC_CATEGORY); |
| 880 | RECORD(DECL_OBJC_CATEGORY_IMPL); |
| 881 | RECORD(DECL_OBJC_IMPLEMENTATION); |
| 882 | RECORD(DECL_OBJC_COMPATIBLE_ALIAS); |
| 883 | RECORD(DECL_OBJC_PROPERTY); |
| 884 | RECORD(DECL_OBJC_PROPERTY_IMPL); |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 885 | RECORD(DECL_FIELD); |
| 886 | RECORD(DECL_VAR); |
Chris Lattner | 0ff8cda | 2009-04-26 22:32:16 +0000 | [diff] [blame] | 887 | RECORD(DECL_IMPLICIT_PARAM); |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 888 | RECORD(DECL_PARM_VAR); |
Chris Lattner | 0ff8cda | 2009-04-26 22:32:16 +0000 | [diff] [blame] | 889 | RECORD(DECL_FILE_SCOPE_ASM); |
| 890 | RECORD(DECL_BLOCK); |
| 891 | RECORD(DECL_CONTEXT_LEXICAL); |
| 892 | RECORD(DECL_CONTEXT_VISIBLE); |
Douglas Gregor | b6c2b3f | 2011-02-08 16:34:17 +0000 | [diff] [blame] | 893 | RECORD(DECL_NAMESPACE); |
| 894 | RECORD(DECL_NAMESPACE_ALIAS); |
| 895 | RECORD(DECL_USING); |
| 896 | RECORD(DECL_USING_SHADOW); |
| 897 | RECORD(DECL_USING_DIRECTIVE); |
| 898 | RECORD(DECL_UNRESOLVED_USING_VALUE); |
| 899 | RECORD(DECL_UNRESOLVED_USING_TYPENAME); |
| 900 | RECORD(DECL_LINKAGE_SPEC); |
| 901 | RECORD(DECL_CXX_RECORD); |
| 902 | RECORD(DECL_CXX_METHOD); |
| 903 | RECORD(DECL_CXX_CONSTRUCTOR); |
| 904 | RECORD(DECL_CXX_DESTRUCTOR); |
| 905 | RECORD(DECL_CXX_CONVERSION); |
| 906 | RECORD(DECL_ACCESS_SPEC); |
| 907 | RECORD(DECL_FRIEND); |
| 908 | RECORD(DECL_FRIEND_TEMPLATE); |
| 909 | RECORD(DECL_CLASS_TEMPLATE); |
| 910 | RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION); |
| 911 | RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION); |
| 912 | RECORD(DECL_FUNCTION_TEMPLATE); |
| 913 | RECORD(DECL_TEMPLATE_TYPE_PARM); |
| 914 | RECORD(DECL_NON_TYPE_TEMPLATE_PARM); |
| 915 | RECORD(DECL_TEMPLATE_TEMPLATE_PARM); |
| 916 | RECORD(DECL_STATIC_ASSERT); |
| 917 | RECORD(DECL_CXX_BASE_SPECIFIERS); |
| 918 | RECORD(DECL_INDIRECTFIELD); |
| 919 | RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK); |
| 920 | |
Douglas Gregor | a72d8c4 | 2011-06-03 02:27:19 +0000 | [diff] [blame] | 921 | // Statements and Exprs can occur in the Decls and Types block. |
| 922 | AddStmtsExprs(Stream, Record); |
| 923 | |
Douglas Gregor | 4800a5c | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 924 | BLOCK(PREPROCESSOR_DETAIL_BLOCK); |
Chandler Carruth | f70d12d | 2011-07-15 07:25:21 +0000 | [diff] [blame] | 925 | RECORD(PPD_MACRO_EXPANSION); |
Douglas Gregor | 4800a5c | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 926 | RECORD(PPD_MACRO_DEFINITION); |
| 927 | RECORD(PPD_INCLUSION_DIRECTIVE); |
| 928 | |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 929 | #undef RECORD |
| 930 | #undef BLOCK |
| 931 | Stream.ExitBlock(); |
| 932 | } |
| 933 | |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 934 | /// \brief Adjusts the given filename to only write out the portion of the |
| 935 | /// filename that is not part of the system root directory. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 936 | /// |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 937 | /// \param Filename the file name to adjust. |
| 938 | /// |
| 939 | /// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and |
| 940 | /// the returned filename will be adjusted by this system root. |
| 941 | /// |
| 942 | /// \returns either the original filename (if it needs no adjustment) or the |
| 943 | /// adjusted filename (which points into the @p Filename parameter). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 944 | static const char * |
Douglas Gregor | 832d620 | 2011-07-22 16:35:34 +0000 | [diff] [blame] | 945 | adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) { |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 946 | assert(Filename && "No file name to adjust?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 947 | |
Douglas Gregor | 832d620 | 2011-07-22 16:35:34 +0000 | [diff] [blame] | 948 | if (isysroot.empty()) |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 949 | return Filename; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 950 | |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 951 | // Verify that the filename and the system root have the same prefix. |
| 952 | unsigned Pos = 0; |
Douglas Gregor | 832d620 | 2011-07-22 16:35:34 +0000 | [diff] [blame] | 953 | for (; Filename[Pos] && Pos < isysroot.size(); ++Pos) |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 954 | if (Filename[Pos] != isysroot[Pos]) |
| 955 | return Filename; // Prefixes don't match. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 956 | |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 957 | // We hit the end of the filename before we hit the end of the system root. |
| 958 | if (!Filename[Pos]) |
| 959 | return Filename; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 960 | |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 961 | // If the file name has a '/' at the current position, skip over the '/'. |
| 962 | // We distinguish sysroot-based includes from absolute includes by the |
| 963 | // absence of '/' at the beginning of sysroot-based includes. |
| 964 | if (Filename[Pos] == '/') |
| 965 | ++Pos; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 966 | |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 967 | return Filename + Pos; |
| 968 | } |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 969 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 970 | /// \brief Write the AST metadata (e.g., i686-apple-darwin9). |
Douglas Gregor | 832d620 | 2011-07-22 16:35:34 +0000 | [diff] [blame] | 971 | void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot, |
Argyrios Kyrtzidis | 8e3df4d | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 972 | const std::string &OutputFile) { |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 973 | using namespace llvm; |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 974 | |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 975 | // Metadata |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 976 | const TargetInfo &Target = Context.getTargetInfo(); |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 977 | BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev(); |
Douglas Gregor | e95b919 | 2011-08-17 21:07:30 +0000 | [diff] [blame] | 978 | MetaAbbrev->Add(BitCodeAbbrevOp(METADATA)); |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 979 | MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major |
| 980 | MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 981 | MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major |
| 982 | MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor |
| 983 | MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable |
Argyrios Kyrtzidis | bef35c9 | 2012-03-07 01:51:17 +0000 | [diff] [blame] | 984 | MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Has errors |
Douglas Gregor | e95b919 | 2011-08-17 21:07:30 +0000 | [diff] [blame] | 985 | MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 986 | unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 987 | |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 988 | RecordData Record; |
Douglas Gregor | e95b919 | 2011-08-17 21:07:30 +0000 | [diff] [blame] | 989 | Record.push_back(METADATA); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 990 | Record.push_back(VERSION_MAJOR); |
| 991 | Record.push_back(VERSION_MINOR); |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 992 | Record.push_back(CLANG_VERSION_MAJOR); |
| 993 | Record.push_back(CLANG_VERSION_MINOR); |
Douglas Gregor | 832d620 | 2011-07-22 16:35:34 +0000 | [diff] [blame] | 994 | Record.push_back(!isysroot.empty()); |
Argyrios Kyrtzidis | bef35c9 | 2012-03-07 01:51:17 +0000 | [diff] [blame] | 995 | Record.push_back(ASTHasCompilerErrors); |
Douglas Gregor | e95b919 | 2011-08-17 21:07:30 +0000 | [diff] [blame] | 996 | const std::string &Triple = Target.getTriple().getTriple(); |
| 997 | Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, Triple); |
| 998 | |
| 999 | if (Chain) { |
Douglas Gregor | e95b919 | 2011-08-17 21:07:30 +0000 | [diff] [blame] | 1000 | serialization::ModuleManager &Mgr = Chain->getModuleManager(); |
| 1001 | llvm::SmallVector<char, 128> ModulePaths; |
| 1002 | Record.clear(); |
Douglas Gregor | 10bc00f | 2011-08-18 04:12:04 +0000 | [diff] [blame] | 1003 | |
| 1004 | for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end(); |
| 1005 | M != MEnd; ++M) { |
| 1006 | // Skip modules that weren't directly imported. |
| 1007 | if (!(*M)->isDirectlyImported()) |
| 1008 | continue; |
| 1009 | |
| 1010 | Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding |
| 1011 | // FIXME: Write import location, once it matters. |
| 1012 | // FIXME: This writes the absolute path for AST files we depend on. |
| 1013 | const std::string &FileName = (*M)->FileName; |
| 1014 | Record.push_back(FileName.size()); |
| 1015 | Record.append(FileName.begin(), FileName.end()); |
| 1016 | } |
Douglas Gregor | e95b919 | 2011-08-17 21:07:30 +0000 | [diff] [blame] | 1017 | Stream.EmitRecord(IMPORTS, Record); |
| 1018 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1019 | |
Douglas Gregor | 31d375f | 2011-05-06 21:43:30 +0000 | [diff] [blame] | 1020 | // Original file name and file ID |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1021 | SourceManager &SM = Context.getSourceManager(); |
| 1022 | if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) { |
| 1023 | BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev(); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1024 | FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME)); |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1025 | FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name |
| 1026 | unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev); |
| 1027 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1028 | SmallString<128> MainFilePath(MainFile->getName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1029 | |
Michael J. Spencer | fbfd180 | 2010-12-21 16:45:57 +0000 | [diff] [blame] | 1030 | llvm::sys::fs::make_absolute(MainFilePath); |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1031 | |
Kovarththanan Rajaratnam | aba54a9 | 2010-03-14 07:15:57 +0000 | [diff] [blame] | 1032 | const char *MainFileNameStr = MainFilePath.c_str(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1033 | MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr, |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1034 | isysroot); |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1035 | RecordData Record; |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1036 | Record.push_back(ORIGINAL_FILE_NAME); |
Daniel Dunbar | ec312a1 | 2009-08-24 09:31:37 +0000 | [diff] [blame] | 1037 | Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr); |
Douglas Gregor | 31d375f | 2011-05-06 21:43:30 +0000 | [diff] [blame] | 1038 | |
| 1039 | Record.clear(); |
| 1040 | Record.push_back(SM.getMainFileID().getOpaqueValue()); |
| 1041 | Stream.EmitRecord(ORIGINAL_FILE_ID, Record); |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1042 | } |
Kovarththanan Rajaratnam | 11a18f1 | 2010-03-14 07:06:50 +0000 | [diff] [blame] | 1043 | |
Argyrios Kyrtzidis | 8e3df4d | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 1044 | // Original PCH directory |
| 1045 | if (!OutputFile.empty() && OutputFile != "-") { |
| 1046 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 1047 | Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR)); |
| 1048 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name |
| 1049 | unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev); |
| 1050 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1051 | SmallString<128> OutputPath(OutputFile); |
Argyrios Kyrtzidis | 8e3df4d | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 1052 | |
| 1053 | llvm::sys::fs::make_absolute(OutputPath); |
| 1054 | StringRef origDir = llvm::sys::path::parent_path(OutputPath); |
| 1055 | |
| 1056 | RecordData Record; |
| 1057 | Record.push_back(ORIGINAL_PCH_DIR); |
| 1058 | Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir); |
| 1059 | } |
| 1060 | |
Ted Kremenek | f7a96a3 | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 1061 | // Repository branch/version information. |
| 1062 | BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev(); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1063 | RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION)); |
Ted Kremenek | f7a96a3 | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 1064 | RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag |
| 1065 | unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev); |
Douglas Gregor | 445e23e | 2009-10-05 21:07:28 +0000 | [diff] [blame] | 1066 | Record.clear(); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1067 | Record.push_back(VERSION_CONTROL_BRANCH_REVISION); |
Ted Kremenek | f7a96a3 | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 1068 | Stream.EmitRecordWithBlob(RepoAbbrevCode, Record, |
| 1069 | getClangFullRepositoryVersion()); |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | /// \brief Write the LangOptions structure. |
Sebastian Redl | a4232eb | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 1073 | void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) { |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1074 | RecordData Record; |
Douglas Gregor | 7d5e81b | 2011-09-13 18:26:39 +0000 | [diff] [blame] | 1075 | #define LANGOPT(Name, Bits, Default, Description) \ |
| 1076 | Record.push_back(LangOpts.Name); |
| 1077 | #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ |
| 1078 | Record.push_back(static_cast<unsigned>(LangOpts.get##Name())); |
| 1079 | #include "clang/Basic/LangOptions.def" |
Douglas Gregor | b86b8dc | 2011-11-15 19:35:01 +0000 | [diff] [blame] | 1080 | |
| 1081 | Record.push_back(LangOpts.CurrentModule.size()); |
| 1082 | Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end()); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1083 | Stream.EmitRecord(LANGUAGE_OPTIONS, Record); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1086 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1087 | // stat cache Serialization |
| 1088 | //===----------------------------------------------------------------------===// |
| 1089 | |
| 1090 | namespace { |
| 1091 | // Trait used for the on-disk hash table of stat cache results. |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 1092 | class ASTStatCacheTrait { |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1093 | public: |
| 1094 | typedef const char * key_type; |
| 1095 | typedef key_type key_type_ref; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1096 | |
Chris Lattner | 74e976b | 2010-11-23 19:28:12 +0000 | [diff] [blame] | 1097 | typedef struct stat data_type; |
| 1098 | typedef const data_type &data_type_ref; |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1099 | |
| 1100 | static unsigned ComputeHash(const char *path) { |
Daniel Dunbar | 2596e42 | 2009-10-17 23:52:28 +0000 | [diff] [blame] | 1101 | return llvm::HashString(path); |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1102 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1103 | |
| 1104 | std::pair<unsigned,unsigned> |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1105 | EmitKeyDataLength(raw_ostream& Out, const char *path, |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1106 | data_type_ref Data) { |
| 1107 | unsigned StrLen = strlen(path); |
| 1108 | clang::io::Emit16(Out, StrLen); |
Chris Lattner | 74e976b | 2010-11-23 19:28:12 +0000 | [diff] [blame] | 1109 | unsigned DataLen = 4 + 4 + 2 + 8 + 8; |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1110 | clang::io::Emit8(Out, DataLen); |
| 1111 | return std::make_pair(StrLen + 1, DataLen); |
| 1112 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1113 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1114 | void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) { |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1115 | Out.write(path, KeyLen); |
| 1116 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1117 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1118 | void EmitData(raw_ostream &Out, key_type_ref, |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1119 | data_type_ref Data, unsigned DataLen) { |
| 1120 | using namespace clang::io; |
| 1121 | uint64_t Start = Out.tell(); (void)Start; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1122 | |
Chris Lattner | 74e976b | 2010-11-23 19:28:12 +0000 | [diff] [blame] | 1123 | Emit32(Out, (uint32_t) Data.st_ino); |
| 1124 | Emit32(Out, (uint32_t) Data.st_dev); |
| 1125 | Emit16(Out, (uint16_t) Data.st_mode); |
| 1126 | Emit64(Out, (uint64_t) Data.st_mtime); |
| 1127 | Emit64(Out, (uint64_t) Data.st_size); |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1128 | |
| 1129 | assert(Out.tell() - Start == DataLen && "Wrong data length"); |
| 1130 | } |
| 1131 | }; |
| 1132 | } // end anonymous namespace |
| 1133 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 1134 | /// \brief Write the stat() system call cache to the AST file. |
Sebastian Redl | a4232eb | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 1135 | void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) { |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1136 | // Build the on-disk hash table containing information about every |
| 1137 | // stat() call. |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 1138 | OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator; |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1139 | unsigned NumStatEntries = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1140 | for (MemorizeStatCalls::iterator Stat = StatCalls.begin(), |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1141 | StatEnd = StatCalls.end(); |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1142 | Stat != StatEnd; ++Stat, ++NumStatEntries) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1143 | StringRef Filename = Stat->first(); |
Chris Lattner | 1e5f83b | 2011-07-14 18:24:21 +0000 | [diff] [blame] | 1144 | Generator.insert(Filename.data(), Stat->second); |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1145 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1146 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1147 | // Create the on-disk hash table in a buffer. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1148 | SmallString<4096> StatCacheData; |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1149 | uint32_t BucketOffset; |
| 1150 | { |
| 1151 | llvm::raw_svector_ostream Out(StatCacheData); |
| 1152 | // Make sure that no bucket is at offset 0 |
| 1153 | clang::io::Emit32(Out, 0); |
| 1154 | BucketOffset = Generator.Emit(Out); |
| 1155 | } |
| 1156 | |
| 1157 | // Create a blob abbreviation |
| 1158 | using namespace llvm; |
| 1159 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1160 | Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE)); |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1161 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
| 1162 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
| 1163 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 1164 | unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev); |
| 1165 | |
| 1166 | // Write the stat cache |
| 1167 | RecordData Record; |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1168 | Record.push_back(STAT_CACHE); |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1169 | Record.push_back(BucketOffset); |
| 1170 | Record.push_back(NumStatEntries); |
Daniel Dunbar | ec312a1 | 2009-08-24 09:31:37 +0000 | [diff] [blame] | 1171 | Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str()); |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1175 | // Source Manager Serialization |
| 1176 | //===----------------------------------------------------------------------===// |
| 1177 | |
| 1178 | /// \brief Create an abbreviation for the SLocEntry that refers to a |
| 1179 | /// file. |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1180 | static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) { |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1181 | using namespace llvm; |
| 1182 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1183 | Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY)); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1184 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset |
| 1185 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location |
| 1186 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic |
| 1187 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives |
Douglas Gregor | 2d52be5 | 2010-03-21 22:49:54 +0000 | [diff] [blame] | 1188 | // FileEntry fields. |
| 1189 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size |
| 1190 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time |
Douglas Gregor | a081da5 | 2011-11-16 20:05:18 +0000 | [diff] [blame] | 1191 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // BufferOverridden |
Argyrios Kyrtzidis | d9d2b67 | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 1192 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs |
Argyrios Kyrtzidis | 10f3df5 | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 1193 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex |
| 1194 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1195 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1196 | return Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | /// \brief Create an abbreviation for the SLocEntry that refers to a |
| 1200 | /// buffer. |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1201 | static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) { |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1202 | using namespace llvm; |
| 1203 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1204 | Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY)); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1205 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset |
| 1206 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location |
| 1207 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic |
| 1208 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives |
| 1209 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1210 | return Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | /// \brief Create an abbreviation for the SLocEntry that refers to a |
| 1214 | /// buffer's blob. |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1215 | static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) { |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1216 | using namespace llvm; |
| 1217 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1218 | Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB)); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1219 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1220 | return Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
Chandler Carruth | f70d12d | 2011-07-15 07:25:21 +0000 | [diff] [blame] | 1223 | /// \brief Create an abbreviation for the SLocEntry that refers to a macro |
| 1224 | /// expansion. |
| 1225 | static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) { |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1226 | using namespace llvm; |
| 1227 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
Chandler Carruth | f70d12d | 2011-07-15 07:25:21 +0000 | [diff] [blame] | 1228 | Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY)); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1229 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset |
| 1230 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location |
| 1231 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location |
| 1232 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location |
Douglas Gregor | f60e991 | 2009-04-15 18:05:10 +0000 | [diff] [blame] | 1233 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1234 | return Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1237 | namespace { |
| 1238 | // Trait used for the on-disk hash table of header search information. |
| 1239 | class HeaderFileInfoTrait { |
| 1240 | ASTWriter &Writer; |
Argyrios Kyrtzidis | 590ad93 | 2011-11-13 22:08:39 +0000 | [diff] [blame] | 1241 | const HeaderSearch &HS; |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1242 | |
Douglas Gregor | b4dc485 | 2011-07-28 04:50:02 +0000 | [diff] [blame] | 1243 | // Keep track of the framework names we've used during serialization. |
| 1244 | SmallVector<char, 128> FrameworkStringData; |
| 1245 | llvm::StringMap<unsigned> FrameworkNameOffset; |
| 1246 | |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1247 | public: |
Argyrios Kyrtzidis | 590ad93 | 2011-11-13 22:08:39 +0000 | [diff] [blame] | 1248 | HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS) |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1249 | : Writer(Writer), HS(HS) { } |
| 1250 | |
| 1251 | typedef const char *key_type; |
| 1252 | typedef key_type key_type_ref; |
| 1253 | |
| 1254 | typedef HeaderFileInfo data_type; |
| 1255 | typedef const data_type &data_type_ref; |
| 1256 | |
| 1257 | static unsigned ComputeHash(const char *path) { |
| 1258 | // The hash is based only on the filename portion of the key, so that the |
| 1259 | // reader can match based on filenames when symlinking or excess path |
| 1260 | // elements ("foo/../", "../") change the form of the name. However, |
| 1261 | // complete path is still the key. |
| 1262 | return llvm::HashString(llvm::sys::path::filename(path)); |
| 1263 | } |
| 1264 | |
| 1265 | std::pair<unsigned,unsigned> |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1266 | EmitKeyDataLength(raw_ostream& Out, const char *path, |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1267 | data_type_ref Data) { |
| 1268 | unsigned StrLen = strlen(path); |
| 1269 | clang::io::Emit16(Out, StrLen); |
Douglas Gregor | b4dc485 | 2011-07-28 04:50:02 +0000 | [diff] [blame] | 1270 | unsigned DataLen = 1 + 2 + 4 + 4; |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1271 | clang::io::Emit8(Out, DataLen); |
| 1272 | return std::make_pair(StrLen + 1, DataLen); |
| 1273 | } |
| 1274 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1275 | void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) { |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1276 | Out.write(path, KeyLen); |
| 1277 | } |
| 1278 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1279 | void EmitData(raw_ostream &Out, key_type_ref, |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1280 | data_type_ref Data, unsigned DataLen) { |
| 1281 | using namespace clang::io; |
| 1282 | uint64_t Start = Out.tell(); (void)Start; |
| 1283 | |
Douglas Gregor | b4dc485 | 2011-07-28 04:50:02 +0000 | [diff] [blame] | 1284 | unsigned char Flags = (Data.isImport << 5) |
| 1285 | | (Data.isPragmaOnce << 4) |
| 1286 | | (Data.DirInfo << 2) |
| 1287 | | (Data.Resolved << 1) |
| 1288 | | Data.IndexHeaderMapHeader; |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1289 | Emit8(Out, (uint8_t)Flags); |
| 1290 | Emit16(Out, (uint16_t) Data.NumIncludes); |
| 1291 | |
| 1292 | if (!Data.ControllingMacro) |
| 1293 | Emit32(Out, (uint32_t)Data.ControllingMacroID); |
| 1294 | else |
| 1295 | Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro)); |
Douglas Gregor | b4dc485 | 2011-07-28 04:50:02 +0000 | [diff] [blame] | 1296 | |
| 1297 | unsigned Offset = 0; |
| 1298 | if (!Data.Framework.empty()) { |
| 1299 | // If this header refers into a framework, save the framework name. |
| 1300 | llvm::StringMap<unsigned>::iterator Pos |
| 1301 | = FrameworkNameOffset.find(Data.Framework); |
| 1302 | if (Pos == FrameworkNameOffset.end()) { |
| 1303 | Offset = FrameworkStringData.size() + 1; |
| 1304 | FrameworkStringData.append(Data.Framework.begin(), |
| 1305 | Data.Framework.end()); |
| 1306 | FrameworkStringData.push_back(0); |
| 1307 | |
| 1308 | FrameworkNameOffset[Data.Framework] = Offset; |
| 1309 | } else |
| 1310 | Offset = Pos->second; |
| 1311 | } |
| 1312 | Emit32(Out, Offset); |
| 1313 | |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1314 | assert(Out.tell() - Start == DataLen && "Wrong data length"); |
| 1315 | } |
Douglas Gregor | b4dc485 | 2011-07-28 04:50:02 +0000 | [diff] [blame] | 1316 | |
| 1317 | const char *strings_begin() const { return FrameworkStringData.begin(); } |
| 1318 | const char *strings_end() const { return FrameworkStringData.end(); } |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1319 | }; |
| 1320 | } // end anonymous namespace |
| 1321 | |
| 1322 | /// \brief Write the header search block for the list of files that |
| 1323 | /// |
| 1324 | /// \param HS The header search structure to save. |
| 1325 | /// |
| 1326 | /// \param Chain Whether we're creating a chained AST file. |
Argyrios Kyrtzidis | 590ad93 | 2011-11-13 22:08:39 +0000 | [diff] [blame] | 1327 | void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1328 | SmallVector<const FileEntry *, 16> FilesByUID; |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1329 | HS.getFileMgr().GetUniqueIDMapping(FilesByUID); |
| 1330 | |
| 1331 | if (FilesByUID.size() > HS.header_file_size()) |
| 1332 | FilesByUID.resize(HS.header_file_size()); |
| 1333 | |
| 1334 | HeaderFileInfoTrait GeneratorTrait(*this, HS); |
| 1335 | OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1336 | SmallVector<const char *, 4> SavedStrings; |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1337 | unsigned NumHeaderSearchEntries = 0; |
| 1338 | for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) { |
| 1339 | const FileEntry *File = FilesByUID[UID]; |
| 1340 | if (!File) |
| 1341 | continue; |
| 1342 | |
Argyrios Kyrtzidis | 590ad93 | 2011-11-13 22:08:39 +0000 | [diff] [blame] | 1343 | // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo |
| 1344 | // from the external source if it was not provided already. |
| 1345 | const HeaderFileInfo &HFI = HS.getFileInfo(File); |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1346 | if (HFI.External && Chain) |
| 1347 | continue; |
| 1348 | |
| 1349 | // Turn the file name into an absolute path, if it isn't already. |
| 1350 | const char *Filename = File->getName(); |
| 1351 | Filename = adjustFilenameForRelocatablePCH(Filename, isysroot); |
| 1352 | |
| 1353 | // If we performed any translation on the file name at all, we need to |
| 1354 | // save this string, since the generator will refer to it later. |
| 1355 | if (Filename != File->getName()) { |
| 1356 | Filename = strdup(Filename); |
| 1357 | SavedStrings.push_back(Filename); |
| 1358 | } |
| 1359 | |
| 1360 | Generator.insert(Filename, HFI, GeneratorTrait); |
| 1361 | ++NumHeaderSearchEntries; |
| 1362 | } |
| 1363 | |
| 1364 | // Create the on-disk hash table in a buffer. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1365 | SmallString<4096> TableData; |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1366 | uint32_t BucketOffset; |
| 1367 | { |
| 1368 | llvm::raw_svector_ostream Out(TableData); |
| 1369 | // Make sure that no bucket is at offset 0 |
| 1370 | clang::io::Emit32(Out, 0); |
| 1371 | BucketOffset = Generator.Emit(Out, GeneratorTrait); |
| 1372 | } |
| 1373 | |
| 1374 | // Create a blob abbreviation |
| 1375 | using namespace llvm; |
| 1376 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 1377 | Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE)); |
| 1378 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
| 1379 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
Douglas Gregor | b4dc485 | 2011-07-28 04:50:02 +0000 | [diff] [blame] | 1380 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1381 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 1382 | unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev); |
| 1383 | |
Douglas Gregor | b4dc485 | 2011-07-28 04:50:02 +0000 | [diff] [blame] | 1384 | // Write the header search table |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1385 | RecordData Record; |
| 1386 | Record.push_back(HEADER_SEARCH_TABLE); |
| 1387 | Record.push_back(BucketOffset); |
| 1388 | Record.push_back(NumHeaderSearchEntries); |
Douglas Gregor | b4dc485 | 2011-07-28 04:50:02 +0000 | [diff] [blame] | 1389 | Record.push_back(TableData.size()); |
| 1390 | TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end()); |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1391 | Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str()); |
| 1392 | |
| 1393 | // Free all of the strings we had to duplicate. |
| 1394 | for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I) |
| 1395 | free((void*)SavedStrings[I]); |
| 1396 | } |
| 1397 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1398 | /// \brief Writes the block containing the serialized form of the |
| 1399 | /// source manager. |
| 1400 | /// |
| 1401 | /// TODO: We should probably use an on-disk hash table (stored in a |
| 1402 | /// blob), indexed based on the file name, so that we only create |
| 1403 | /// entries for files that we actually need. In the common case (no |
| 1404 | /// errors), we probably won't have to create file entries for any of |
| 1405 | /// the files in the AST. |
Sebastian Redl | a4232eb | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 1406 | void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr, |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1407 | const Preprocessor &PP, |
Douglas Gregor | 832d620 | 2011-07-22 16:35:34 +0000 | [diff] [blame] | 1408 | StringRef isysroot) { |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1409 | RecordData Record; |
| 1410 | |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 1411 | // Enter the source manager block. |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1412 | Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1413 | |
| 1414 | // Abbreviations for the various kinds of source-location entries. |
Chris Lattner | 828e18c | 2009-04-27 19:03:22 +0000 | [diff] [blame] | 1415 | unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream); |
| 1416 | unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream); |
| 1417 | unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream); |
Chandler Carruth | f70d12d | 2011-07-15 07:25:21 +0000 | [diff] [blame] | 1418 | unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1419 | |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1420 | // Write out the source location entry table. We skip the first |
| 1421 | // entry, which is always the same dummy entry. |
Chris Lattner | 090d9b5 | 2009-04-27 19:01:47 +0000 | [diff] [blame] | 1422 | std::vector<uint32_t> SLocEntryOffsets; |
Argyrios Kyrtzidis | 4cdb0e2 | 2011-06-02 20:01:46 +0000 | [diff] [blame] | 1423 | // Write out the offsets of only source location file entries. |
| 1424 | // We will go through them in ASTReader::validateFileEntries(). |
| 1425 | std::vector<uint32_t> SLocFileEntryOffsets; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1426 | RecordData PreloadSLocs; |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1427 | SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1); |
| 1428 | for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); |
Sebastian Redl | 0fa7d0b | 2010-07-22 17:01:13 +0000 | [diff] [blame] | 1429 | I != N; ++I) { |
Douglas Gregor | bdfe48a | 2009-10-16 22:46:09 +0000 | [diff] [blame] | 1430 | // Get this source location entry. |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1431 | const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I); |
Kovarththanan Rajaratnam | 11a18f1 | 2010-03-14 07:06:50 +0000 | [diff] [blame] | 1432 | |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1433 | // Record the offset of this source-location entry. |
| 1434 | SLocEntryOffsets.push_back(Stream.GetCurrentBitNo()); |
| 1435 | |
| 1436 | // Figure out which record code to use. |
| 1437 | unsigned Code; |
| 1438 | if (SLoc->isFile()) { |
Douglas Gregor | a081da5 | 2011-11-16 20:05:18 +0000 | [diff] [blame] | 1439 | const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache(); |
| 1440 | if (Cache->OrigEntry) { |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1441 | Code = SM_SLOC_FILE_ENTRY; |
Argyrios Kyrtzidis | 4cdb0e2 | 2011-06-02 20:01:46 +0000 | [diff] [blame] | 1442 | SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo()); |
| 1443 | } else |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1444 | Code = SM_SLOC_BUFFER_ENTRY; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1445 | } else |
Chandler Carruth | f70d12d | 2011-07-15 07:25:21 +0000 | [diff] [blame] | 1446 | Code = SM_SLOC_EXPANSION_ENTRY; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1447 | Record.clear(); |
| 1448 | Record.push_back(Code); |
| 1449 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1450 | // Starting offset of this entry within this module, so skip the dummy. |
| 1451 | Record.push_back(SLoc->getOffset() - 2); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1452 | if (SLoc->isFile()) { |
| 1453 | const SrcMgr::FileInfo &File = SLoc->getFile(); |
| 1454 | Record.push_back(File.getIncludeLoc().getRawEncoding()); |
| 1455 | Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding |
| 1456 | Record.push_back(File.hasLineDirectives()); |
| 1457 | |
| 1458 | const SrcMgr::ContentCache *Content = File.getContentCache(); |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 1459 | if (Content->OrigEntry) { |
| 1460 | assert(Content->OrigEntry == Content->ContentsEntry && |
Douglas Gregor | a081da5 | 2011-11-16 20:05:18 +0000 | [diff] [blame] | 1461 | "Writing to AST an overridden file is not supported"); |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 1462 | |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1463 | // The source location entry is a file. The blob associated |
| 1464 | // with this entry is the file name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1465 | |
Douglas Gregor | 2d52be5 | 2010-03-21 22:49:54 +0000 | [diff] [blame] | 1466 | // Emit size/modification time for this file. |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 1467 | Record.push_back(Content->OrigEntry->getSize()); |
| 1468 | Record.push_back(Content->OrigEntry->getModificationTime()); |
Douglas Gregor | a081da5 | 2011-11-16 20:05:18 +0000 | [diff] [blame] | 1469 | Record.push_back(Content->BufferOverridden); |
Argyrios Kyrtzidis | d9d2b67 | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 1470 | Record.push_back(File.NumCreatedFIDs); |
Argyrios Kyrtzidis | 10f3df5 | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 1471 | |
| 1472 | FileDeclIDsTy::iterator FDI = FileDeclIDs.find(SLoc); |
| 1473 | if (FDI != FileDeclIDs.end()) { |
| 1474 | Record.push_back(FDI->second->FirstDeclIndex); |
| 1475 | Record.push_back(FDI->second->DeclIDs.size()); |
| 1476 | } else { |
| 1477 | Record.push_back(0); |
| 1478 | Record.push_back(0); |
| 1479 | } |
Douglas Gregor | a081da5 | 2011-11-16 20:05:18 +0000 | [diff] [blame] | 1480 | |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1481 | // Turn the file name into an absolute path, if it isn't already. |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 1482 | const char *Filename = Content->OrigEntry->getName(); |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1483 | SmallString<128> FilePath(Filename); |
Anders Carlsson | 2c10c80 | 2011-03-08 16:04:35 +0000 | [diff] [blame] | 1484 | |
| 1485 | // Ask the file manager to fixup the relative path for us. This will |
| 1486 | // honor the working directory. |
| 1487 | SourceMgr.getFileManager().FixupRelativePath(FilePath); |
| 1488 | |
| 1489 | // FIXME: This call to make_absolute shouldn't be necessary, the |
| 1490 | // call to FixupRelativePath should always return an absolute path. |
Michael J. Spencer | fbfd180 | 2010-12-21 16:45:57 +0000 | [diff] [blame] | 1491 | llvm::sys::fs::make_absolute(FilePath); |
Kovarththanan Rajaratnam | aba54a9 | 2010-03-14 07:15:57 +0000 | [diff] [blame] | 1492 | Filename = FilePath.c_str(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1493 | |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1494 | Filename = adjustFilenameForRelocatablePCH(Filename, isysroot); |
Daniel Dunbar | ec312a1 | 2009-08-24 09:31:37 +0000 | [diff] [blame] | 1495 | Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename); |
Douglas Gregor | a081da5 | 2011-11-16 20:05:18 +0000 | [diff] [blame] | 1496 | |
| 1497 | if (Content->BufferOverridden) { |
| 1498 | Record.clear(); |
| 1499 | Record.push_back(SM_SLOC_BUFFER_BLOB); |
| 1500 | const llvm::MemoryBuffer *Buffer |
| 1501 | = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager()); |
| 1502 | Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, |
| 1503 | StringRef(Buffer->getBufferStart(), |
| 1504 | Buffer->getBufferSize() + 1)); |
| 1505 | } |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1506 | } else { |
| 1507 | // The source location entry is a buffer. The blob associated |
| 1508 | // with this entry contains the contents of the buffer. |
| 1509 | |
| 1510 | // We add one to the size so that we capture the trailing NULL |
| 1511 | // that is required by llvm::MemoryBuffer::getMemBuffer (on |
| 1512 | // the reader side). |
Douglas Gregor | 36c35ba | 2010-03-16 00:35:39 +0000 | [diff] [blame] | 1513 | const llvm::MemoryBuffer *Buffer |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1514 | = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager()); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1515 | const char *Name = Buffer->getBufferIdentifier(); |
Daniel Dunbar | ec312a1 | 2009-08-24 09:31:37 +0000 | [diff] [blame] | 1516 | Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1517 | StringRef(Name, strlen(Name) + 1)); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1518 | Record.clear(); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1519 | Record.push_back(SM_SLOC_BUFFER_BLOB); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1520 | Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1521 | StringRef(Buffer->getBufferStart(), |
Daniel Dunbar | ec312a1 | 2009-08-24 09:31:37 +0000 | [diff] [blame] | 1522 | Buffer->getBufferSize() + 1)); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1523 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1524 | if (strcmp(Name, "<built-in>") == 0) { |
| 1525 | PreloadSLocs.push_back(SLocEntryOffsets.size()); |
| 1526 | } |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1527 | } |
| 1528 | } else { |
Chandler Carruth | f70d12d | 2011-07-15 07:25:21 +0000 | [diff] [blame] | 1529 | // The source location entry is a macro expansion. |
Chandler Carruth | 1728762 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 1530 | const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion(); |
Chandler Carruth | 78df836 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 1531 | Record.push_back(Expansion.getSpellingLoc().getRawEncoding()); |
| 1532 | Record.push_back(Expansion.getExpansionLocStart().getRawEncoding()); |
Argyrios Kyrtzidis | d21683c | 2011-08-17 00:31:14 +0000 | [diff] [blame] | 1533 | Record.push_back(Expansion.isMacroArgExpansion() ? 0 |
| 1534 | : Expansion.getExpansionLocEnd().getRawEncoding()); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1535 | |
| 1536 | // Compute the token length for this macro expansion. |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1537 | unsigned NextOffset = SourceMgr.getNextLocalOffset(); |
Douglas Gregor | bdfe48a | 2009-10-16 22:46:09 +0000 | [diff] [blame] | 1538 | if (I + 1 != N) |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1539 | NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset(); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1540 | Record.push_back(NextOffset - SLoc->getOffset() - 1); |
Chandler Carruth | f70d12d | 2011-07-15 07:25:21 +0000 | [diff] [blame] | 1541 | Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1542 | } |
| 1543 | } |
| 1544 | |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1545 | Stream.ExitBlock(); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1546 | |
| 1547 | if (SLocEntryOffsets.empty()) |
| 1548 | return; |
| 1549 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 1550 | // Write the source-location offsets table into the AST block. This |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1551 | // table is used for lazily loading source-location information. |
| 1552 | using namespace llvm; |
| 1553 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1554 | Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS)); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1555 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1556 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1557 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets |
| 1558 | unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1559 | |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1560 | Record.clear(); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1561 | Record.push_back(SOURCE_LOCATION_OFFSETS); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1562 | Record.push_back(SLocEntryOffsets.size()); |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1563 | Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy |
Benjamin Kramer | 6e089c6 | 2011-04-24 17:44:50 +0000 | [diff] [blame] | 1564 | Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets)); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1565 | |
Argyrios Kyrtzidis | 4cdb0e2 | 2011-06-02 20:01:46 +0000 | [diff] [blame] | 1566 | Abbrev = new BitCodeAbbrev(); |
| 1567 | Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS)); |
| 1568 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs |
| 1569 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets |
| 1570 | unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev); |
| 1571 | |
| 1572 | Record.clear(); |
| 1573 | Record.push_back(FILE_SOURCE_LOCATION_OFFSETS); |
| 1574 | Record.push_back(SLocFileEntryOffsets.size()); |
| 1575 | Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record, |
| 1576 | data(SLocFileEntryOffsets)); |
| 1577 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 1578 | // Write the source location entry preloads array, telling the AST |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1579 | // reader which source locations entries it should load eagerly. |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1580 | Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs); |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1581 | |
| 1582 | // Write the line table. It depends on remapping working, so it must come |
| 1583 | // after the source location offsets. |
| 1584 | if (SourceMgr.hasLineTable()) { |
| 1585 | LineTableInfo &LineTable = SourceMgr.getLineTable(); |
| 1586 | |
| 1587 | Record.clear(); |
| 1588 | // Emit the file names |
| 1589 | Record.push_back(LineTable.getNumFilenames()); |
| 1590 | for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) { |
| 1591 | // Emit the file name |
| 1592 | const char *Filename = LineTable.getFilename(I); |
| 1593 | Filename = adjustFilenameForRelocatablePCH(Filename, isysroot); |
| 1594 | unsigned FilenameLen = Filename? strlen(Filename) : 0; |
| 1595 | Record.push_back(FilenameLen); |
| 1596 | if (FilenameLen) |
| 1597 | Record.insert(Record.end(), Filename, Filename + FilenameLen); |
| 1598 | } |
| 1599 | |
| 1600 | // Emit the line entries |
| 1601 | for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end(); |
| 1602 | L != LEnd; ++L) { |
| 1603 | // Only emit entries for local files. |
| 1604 | if (L->first < 0) |
| 1605 | continue; |
| 1606 | |
| 1607 | // Emit the file ID |
| 1608 | Record.push_back(L->first); |
| 1609 | |
| 1610 | // Emit the line entries |
| 1611 | Record.push_back(L->second.size()); |
| 1612 | for (std::vector<LineEntry>::iterator LE = L->second.begin(), |
| 1613 | LEEnd = L->second.end(); |
| 1614 | LE != LEEnd; ++LE) { |
| 1615 | Record.push_back(LE->FileOffset); |
| 1616 | Record.push_back(LE->LineNo); |
| 1617 | Record.push_back(LE->FilenameID); |
| 1618 | Record.push_back((unsigned)LE->FileKind); |
| 1619 | Record.push_back(LE->IncludeOffset); |
| 1620 | } |
| 1621 | } |
| 1622 | Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record); |
| 1623 | } |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1626 | //===----------------------------------------------------------------------===// |
| 1627 | // Preprocessor Serialization |
| 1628 | //===----------------------------------------------------------------------===// |
| 1629 | |
Douglas Gregor | 9c73610 | 2011-02-10 18:20:09 +0000 | [diff] [blame] | 1630 | static int compareMacroDefinitions(const void *XPtr, const void *YPtr) { |
| 1631 | const std::pair<const IdentifierInfo *, MacroInfo *> &X = |
| 1632 | *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr; |
| 1633 | const std::pair<const IdentifierInfo *, MacroInfo *> &Y = |
| 1634 | *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr; |
| 1635 | return X.first->getName().compare(Y.first->getName()); |
| 1636 | } |
| 1637 | |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 1638 | /// \brief Writes the block containing the serialized form of the |
| 1639 | /// preprocessor. |
| 1640 | /// |
Douglas Gregor | 7143aab | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 1641 | void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) { |
Argyrios Kyrtzidis | e24692b | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 1642 | PreprocessingRecord *PPRec = PP.getPreprocessingRecord(); |
| 1643 | if (PPRec) |
| 1644 | WritePreprocessorDetail(*PPRec); |
| 1645 | |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1646 | RecordData Record; |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 1647 | |
Chris Lattner | c1f9d82 | 2009-04-13 01:29:17 +0000 | [diff] [blame] | 1648 | // If the preprocessor __COUNTER__ value has been bumped, remember it. |
| 1649 | if (PP.getCounterValue() != 0) { |
| 1650 | Record.push_back(PP.getCounterValue()); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1651 | Stream.EmitRecord(PP_COUNTER_VALUE, Record); |
Chris Lattner | c1f9d82 | 2009-04-13 01:29:17 +0000 | [diff] [blame] | 1652 | Record.clear(); |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | // Enter the preprocessor block. |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1656 | Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1657 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 1658 | // If the AST file contains __DATE__ or __TIME__ emit a warning about this. |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 1659 | // FIXME: use diagnostics subsystem for localization etc. |
| 1660 | if (PP.SawDateOrTime()) |
| 1661 | fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1662 | |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1663 | |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1664 | // Loop over all the macro definitions that are live at the end of the file, |
| 1665 | // emitting each to the PP section. |
Michael J. Spencer | 20249a1 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 1666 | |
Douglas Gregor | 9c73610 | 2011-02-10 18:20:09 +0000 | [diff] [blame] | 1667 | // Construct the list of macro definitions that need to be serialized. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1668 | SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2> |
Douglas Gregor | 9c73610 | 2011-02-10 18:20:09 +0000 | [diff] [blame] | 1669 | MacrosToEmit; |
| 1670 | llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen; |
Douglas Gregor | 040a804 | 2011-02-11 00:26:14 +0000 | [diff] [blame] | 1671 | for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0), |
| 1672 | E = PP.macro_end(Chain == 0); |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1673 | I != E; ++I) { |
Douglas Gregor | 1d4c113 | 2011-12-20 22:06:13 +0000 | [diff] [blame] | 1674 | const IdentifierInfo *Name = I->first; |
Douglas Gregor | aa93a87 | 2011-10-17 15:32:29 +0000 | [diff] [blame] | 1675 | if (!IsModule || I->second->isPublic()) { |
Douglas Gregor | 1d4c113 | 2011-12-20 22:06:13 +0000 | [diff] [blame] | 1676 | MacroDefinitionsSeen.insert(Name); |
Douglas Gregor | 7143aab | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 1677 | MacrosToEmit.push_back(std::make_pair(I->first, I->second)); |
| 1678 | } |
Douglas Gregor | 9c73610 | 2011-02-10 18:20:09 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
| 1681 | // Sort the set of macro definitions that need to be serialized by the |
| 1682 | // name of the macro, to provide a stable ordering. |
| 1683 | llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(), |
| 1684 | &compareMacroDefinitions); |
| 1685 | |
Douglas Gregor | 040a804 | 2011-02-11 00:26:14 +0000 | [diff] [blame] | 1686 | // Resolve any identifiers that defined macros at the time they were |
| 1687 | // deserialized, adding them to the list of macros to emit (if appropriate). |
| 1688 | for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) { |
| 1689 | IdentifierInfo *Name |
| 1690 | = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]); |
| 1691 | if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name)) |
| 1692 | MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name))); |
| 1693 | } |
| 1694 | |
Douglas Gregor | 9c73610 | 2011-02-10 18:20:09 +0000 | [diff] [blame] | 1695 | for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) { |
| 1696 | const IdentifierInfo *Name = MacrosToEmit[I].first; |
| 1697 | MacroInfo *MI = MacrosToEmit[I].second; |
Douglas Gregor | 040a804 | 2011-02-11 00:26:14 +0000 | [diff] [blame] | 1698 | if (!MI) |
| 1699 | continue; |
| 1700 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 1701 | // Don't emit builtin macros like __LINE__ to the AST file unless they have |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1702 | // been redefined by the header (in which case they are not isBuiltinMacro). |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 1703 | // Also skip macros from a AST file if we're chaining. |
Douglas Gregor | ee9b0ba | 2010-10-01 01:03:07 +0000 | [diff] [blame] | 1704 | |
| 1705 | // FIXME: There is a (probably minor) optimization we could do here, if |
| 1706 | // the macro comes from the original PCH but the identifier comes from a |
| 1707 | // chained PCH, by storing the offset into the original PCH rather than |
| 1708 | // writing the macro definition a second time. |
Michael J. Spencer | 20249a1 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 1709 | if (MI->isBuiltinMacro() || |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 1710 | (Chain && |
| 1711 | Name->isFromAST() && !Name->hasChangedSinceDeserialization() && |
| 1712 | MI->isFromAST() && !MI->hasChangedAfterLoad())) |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1713 | continue; |
| 1714 | |
Douglas Gregor | 9c73610 | 2011-02-10 18:20:09 +0000 | [diff] [blame] | 1715 | AddIdentifierRef(Name, Record); |
| 1716 | MacroOffsets[Name] = Stream.GetCurrentBitNo(); |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1717 | Record.push_back(MI->getDefinitionLoc().getRawEncoding()); |
| 1718 | Record.push_back(MI->isUsed()); |
Douglas Gregor | aa93a87 | 2011-10-17 15:32:29 +0000 | [diff] [blame] | 1719 | Record.push_back(MI->isPublic()); |
| 1720 | AddSourceLocation(MI->getVisibilityLocation(), Record); |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1721 | unsigned Code; |
| 1722 | if (MI->isObjectLike()) { |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1723 | Code = PP_MACRO_OBJECT_LIKE; |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1724 | } else { |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1725 | Code = PP_MACRO_FUNCTION_LIKE; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1726 | |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1727 | Record.push_back(MI->isC99Varargs()); |
| 1728 | Record.push_back(MI->isGNUVarargs()); |
| 1729 | Record.push_back(MI->getNumArgs()); |
| 1730 | for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end(); |
| 1731 | I != E; ++I) |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1732 | AddIdentifierRef(*I, Record); |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1733 | } |
Michael J. Spencer | 20249a1 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 1734 | |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1735 | // If we have a detailed preprocessing record, record the macro definition |
| 1736 | // ID that corresponds to this macro. |
| 1737 | if (PPRec) |
Argyrios Kyrtzidis | e24692b | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 1738 | Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]); |
Michael J. Spencer | 20249a1 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 1739 | |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1740 | Stream.EmitRecord(Code, Record); |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1741 | Record.clear(); |
| 1742 | |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 1743 | // Emit the tokens array. |
| 1744 | for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) { |
| 1745 | // Note that we know that the preprocessor does not have any annotation |
| 1746 | // tokens in it because they are created by the parser, and thus can't be |
| 1747 | // in a macro definition. |
| 1748 | const Token &Tok = MI->getReplacementToken(TokNo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1749 | |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 1750 | Record.push_back(Tok.getLocation().getRawEncoding()); |
| 1751 | Record.push_back(Tok.getLength()); |
| 1752 | |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 1753 | // FIXME: When reading literal tokens, reconstruct the literal pointer if |
| 1754 | // it is needed. |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1755 | AddIdentifierRef(Tok.getIdentifierInfo(), Record); |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 1756 | // FIXME: Should translate token kind to a stable encoding. |
| 1757 | Record.push_back(Tok.getKind()); |
| 1758 | // FIXME: Should translate token flags to a stable encoding. |
| 1759 | Record.push_back(Tok.getFlags()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1760 | |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1761 | Stream.EmitRecord(PP_TOKEN, Record); |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 1762 | Record.clear(); |
| 1763 | } |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1764 | ++NumMacros; |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1765 | } |
Douglas Gregor | 4800a5c | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 1766 | Stream.ExitBlock(); |
Douglas Gregor | 4800a5c | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) { |
Argyrios Kyrtzidis | b6441ef | 2011-09-19 20:40:42 +0000 | [diff] [blame] | 1770 | if (PPRec.local_begin() == PPRec.local_end()) |
Douglas Gregor | 4800a5c | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 1771 | return; |
Argyrios Kyrtzidis | e24692b | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 1772 | |
Argyrios Kyrtzidis | 2dbaca7 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 1773 | SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets; |
Argyrios Kyrtzidis | e24692b | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 1774 | |
Douglas Gregor | 4800a5c | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 1775 | // Enter the preprocessor block. |
| 1776 | Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3); |
Michael J. Spencer | 20249a1 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 1777 | |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1778 | // If the preprocessor has a preprocessing record, emit it. |
| 1779 | unsigned NumPreprocessingRecords = 0; |
Douglas Gregor | 4800a5c | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 1780 | using namespace llvm; |
| 1781 | |
| 1782 | // Set up the abbreviation for |
| 1783 | unsigned InclusionAbbrev = 0; |
| 1784 | { |
| 1785 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 1786 | Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE)); |
Douglas Gregor | 4800a5c | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 1787 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length |
| 1788 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes |
| 1789 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind |
| 1790 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 1791 | InclusionAbbrev = Stream.EmitAbbrev(Abbrev); |
| 1792 | } |
| 1793 | |
Douglas Gregor | 272b6bc | 2011-08-04 18:56:47 +0000 | [diff] [blame] | 1794 | unsigned FirstPreprocessorEntityID |
| 1795 | = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0) |
| 1796 | + NUM_PREDEF_PP_ENTITY_IDS; |
| 1797 | unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID; |
Douglas Gregor | 4800a5c | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 1798 | RecordData Record; |
Argyrios Kyrtzidis | b6441ef | 2011-09-19 20:40:42 +0000 | [diff] [blame] | 1799 | for (PreprocessingRecord::iterator E = PPRec.local_begin(), |
| 1800 | EEnd = PPRec.local_end(); |
Douglas Gregor | 7338a92 | 2011-08-04 17:06:18 +0000 | [diff] [blame] | 1801 | E != EEnd; |
| 1802 | (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) { |
Douglas Gregor | 4800a5c | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 1803 | Record.clear(); |
Michael J. Spencer | 20249a1 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 1804 | |
Argyrios Kyrtzidis | 2dbaca7 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 1805 | PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(), |
| 1806 | Stream.GetCurrentBitNo())); |
Argyrios Kyrtzidis | e24692b | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 1807 | |
Douglas Gregor | 4800a5c | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 1808 | if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) { |
Argyrios Kyrtzidis | e24692b | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 1809 | // Record this macro definition's ID. |
| 1810 | MacroDefinitions[MD] = NextPreprocessorEntityID; |
Douglas Gregor | 4800a5c | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 1811 | |
Douglas Gregor | 4800a5c | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 1812 | AddIdentifierRef(MD->getName(), Record); |
Douglas Gregor | 4800a5c | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 1813 | Stream.EmitRecord(PPD_MACRO_DEFINITION, Record); |
| 1814 | continue; |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1815 | } |
Michael J. Spencer | 20249a1 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 1816 | |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 1817 | if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) { |
Argyrios Kyrtzidis | 8f7c540 | 2011-09-08 17:18:41 +0000 | [diff] [blame] | 1818 | Record.push_back(ME->isBuiltinMacro()); |
| 1819 | if (ME->isBuiltinMacro()) |
| 1820 | AddIdentifierRef(ME->getName(), Record); |
| 1821 | else |
Argyrios Kyrtzidis | e24692b | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 1822 | Record.push_back(MacroDefinitions[ME->getDefinition()]); |
Chandler Carruth | f70d12d | 2011-07-15 07:25:21 +0000 | [diff] [blame] | 1823 | Stream.EmitRecord(PPD_MACRO_EXPANSION, Record); |
Douglas Gregor | 4800a5c | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 1824 | continue; |
| 1825 | } |
| 1826 | |
| 1827 | if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) { |
| 1828 | Record.push_back(PPD_INCLUSION_DIRECTIVE); |
Douglas Gregor | 4800a5c | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 1829 | Record.push_back(ID->getFileName().size()); |
| 1830 | Record.push_back(ID->wasInQuotes()); |
| 1831 | Record.push_back(static_cast<unsigned>(ID->getKind())); |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1832 | SmallString<64> Buffer; |
Douglas Gregor | 4800a5c | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 1833 | Buffer += ID->getFileName(); |
Argyrios Kyrtzidis | 29f98b4 | 2012-03-08 01:08:28 +0000 | [diff] [blame] | 1834 | // Check that the FileEntry is not null because it was not resolved and |
| 1835 | // we create a PCH even with compiler errors. |
| 1836 | if (ID->getFile()) |
| 1837 | Buffer += ID->getFile()->getName(); |
Douglas Gregor | 4800a5c | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 1838 | Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer); |
| 1839 | continue; |
| 1840 | } |
| 1841 | |
| 1842 | llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter"); |
| 1843 | } |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1844 | Stream.ExitBlock(); |
Michael J. Spencer | 20249a1 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 1845 | |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1846 | // Write the offsets table for the preprocessing record. |
| 1847 | if (NumPreprocessingRecords > 0) { |
Argyrios Kyrtzidis | e24692b | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 1848 | assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords); |
| 1849 | |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1850 | // Write the offsets table for identifier IDs. |
| 1851 | using namespace llvm; |
| 1852 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
Argyrios Kyrtzidis | e24692b | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 1853 | Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS)); |
Douglas Gregor | 272b6bc | 2011-08-04 18:56:47 +0000 | [diff] [blame] | 1854 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1855 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
Argyrios Kyrtzidis | e24692b | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 1856 | unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev); |
Michael J. Spencer | 20249a1 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 1857 | |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1858 | Record.clear(); |
Argyrios Kyrtzidis | e24692b | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 1859 | Record.push_back(PPD_ENTITIES_OFFSETS); |
Douglas Gregor | 272b6bc | 2011-08-04 18:56:47 +0000 | [diff] [blame] | 1860 | Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS); |
Argyrios Kyrtzidis | e24692b | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 1861 | Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record, |
| 1862 | data(PreprocessedEntityOffsets)); |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1863 | } |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 1866 | unsigned ASTWriter::getSubmoduleID(Module *Mod) { |
| 1867 | llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod); |
| 1868 | if (Known != SubmoduleIDs.end()) |
| 1869 | return Known->second; |
| 1870 | |
| 1871 | return SubmoduleIDs[Mod] = NextSubmoduleID++; |
| 1872 | } |
| 1873 | |
Douglas Gregor | 26ced12 | 2011-12-01 00:59:36 +0000 | [diff] [blame] | 1874 | /// \brief Compute the number of modules within the given tree (including the |
| 1875 | /// given module). |
| 1876 | static unsigned getNumberOfModules(Module *Mod) { |
| 1877 | unsigned ChildModules = 0; |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 1878 | for (Module::submodule_iterator Sub = Mod->submodule_begin(), |
| 1879 | SubEnd = Mod->submodule_end(); |
Douglas Gregor | 26ced12 | 2011-12-01 00:59:36 +0000 | [diff] [blame] | 1880 | Sub != SubEnd; ++Sub) |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 1881 | ChildModules += getNumberOfModules(*Sub); |
Douglas Gregor | 26ced12 | 2011-12-01 00:59:36 +0000 | [diff] [blame] | 1882 | |
| 1883 | return ChildModules + 1; |
| 1884 | } |
| 1885 | |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 1886 | void ASTWriter::WriteSubmodules(Module *WritingModule) { |
Douglas Gregor | 4bc8738d | 2011-12-05 16:35:23 +0000 | [diff] [blame] | 1887 | // Determine the dependencies of our module and each of it's submodules. |
Douglas Gregor | 5598868 | 2011-12-05 16:33:54 +0000 | [diff] [blame] | 1888 | // FIXME: This feels like it belongs somewhere else, but there are no |
| 1889 | // other consumers of this information. |
| 1890 | SourceManager &SrcMgr = PP->getSourceManager(); |
| 1891 | ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap(); |
| 1892 | for (ASTContext::import_iterator I = Context->local_import_begin(), |
| 1893 | IEnd = Context->local_import_end(); |
| 1894 | I != IEnd; ++I) { |
Douglas Gregor | 5598868 | 2011-12-05 16:33:54 +0000 | [diff] [blame] | 1895 | if (Module *ImportedFrom |
| 1896 | = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(), |
| 1897 | SrcMgr))) { |
| 1898 | ImportedFrom->Imports.push_back(I->getImportedModule()); |
| 1899 | } |
| 1900 | } |
| 1901 | |
Douglas Gregor | 392ed2b | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1902 | // Enter the submodule description block. |
| 1903 | Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE); |
| 1904 | |
| 1905 | // Write the abbreviations needed for the submodules block. |
| 1906 | using namespace llvm; |
| 1907 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 1908 | Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION)); |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 1909 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID |
Douglas Gregor | 392ed2b | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1910 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent |
| 1911 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework |
| 1912 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 1913 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem |
| 1914 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules... |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 1915 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit... |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 1916 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild... |
Douglas Gregor | 392ed2b | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1917 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name |
| 1918 | unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev); |
| 1919 | |
| 1920 | Abbrev = new BitCodeAbbrev(); |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 1921 | Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER)); |
Douglas Gregor | 392ed2b | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1922 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name |
| 1923 | unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev); |
| 1924 | |
| 1925 | Abbrev = new BitCodeAbbrev(); |
| 1926 | Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER)); |
| 1927 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name |
| 1928 | unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 1929 | |
| 1930 | Abbrev = new BitCodeAbbrev(); |
| 1931 | Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR)); |
| 1932 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name |
| 1933 | unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev); |
| 1934 | |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 1935 | Abbrev = new BitCodeAbbrev(); |
| 1936 | Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES)); |
| 1937 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature |
| 1938 | unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev); |
| 1939 | |
Douglas Gregor | 26ced12 | 2011-12-01 00:59:36 +0000 | [diff] [blame] | 1940 | // Write the submodule metadata block. |
| 1941 | RecordData Record; |
| 1942 | Record.push_back(getNumberOfModules(WritingModule)); |
| 1943 | Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS); |
| 1944 | Stream.EmitRecord(SUBMODULE_METADATA, Record); |
| 1945 | |
Douglas Gregor | 392ed2b | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1946 | // Write all of the submodules. |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 1947 | std::queue<Module *> Q; |
Douglas Gregor | 392ed2b | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1948 | Q.push(WritingModule); |
Douglas Gregor | 392ed2b | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1949 | while (!Q.empty()) { |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 1950 | Module *Mod = Q.front(); |
Douglas Gregor | 392ed2b | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1951 | Q.pop(); |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 1952 | unsigned ID = getSubmoduleID(Mod); |
Douglas Gregor | 392ed2b | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1953 | |
| 1954 | // Emit the definition of the block. |
| 1955 | Record.clear(); |
| 1956 | Record.push_back(SUBMODULE_DEFINITION); |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 1957 | Record.push_back(ID); |
Douglas Gregor | 392ed2b | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1958 | if (Mod->Parent) { |
| 1959 | assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?"); |
| 1960 | Record.push_back(SubmoduleIDs[Mod->Parent]); |
| 1961 | } else { |
| 1962 | Record.push_back(0); |
| 1963 | } |
| 1964 | Record.push_back(Mod->IsFramework); |
| 1965 | Record.push_back(Mod->IsExplicit); |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 1966 | Record.push_back(Mod->IsSystem); |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 1967 | Record.push_back(Mod->InferSubmodules); |
| 1968 | Record.push_back(Mod->InferExplicitSubmodules); |
| 1969 | Record.push_back(Mod->InferExportWildcard); |
Douglas Gregor | 392ed2b | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1970 | Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name); |
| 1971 | |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 1972 | // Emit the requirements. |
| 1973 | for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) { |
| 1974 | Record.clear(); |
| 1975 | Record.push_back(SUBMODULE_REQUIRES); |
| 1976 | Stream.EmitRecordWithBlob(RequiresAbbrev, Record, |
| 1977 | Mod->Requires[I].data(), |
| 1978 | Mod->Requires[I].size()); |
| 1979 | } |
| 1980 | |
Douglas Gregor | 392ed2b | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1981 | // Emit the umbrella header, if there is one. |
Douglas Gregor | 10694ce | 2011-12-08 17:39:04 +0000 | [diff] [blame] | 1982 | if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) { |
Douglas Gregor | 392ed2b | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1983 | Record.clear(); |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 1984 | Record.push_back(SUBMODULE_UMBRELLA_HEADER); |
Douglas Gregor | 392ed2b | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1985 | Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record, |
Douglas Gregor | 10694ce | 2011-12-08 17:39:04 +0000 | [diff] [blame] | 1986 | UmbrellaHeader->getName()); |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 1987 | } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) { |
| 1988 | Record.clear(); |
| 1989 | Record.push_back(SUBMODULE_UMBRELLA_DIR); |
| 1990 | Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record, |
| 1991 | UmbrellaDir->getName()); |
Douglas Gregor | 392ed2b | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | // Emit the headers. |
| 1995 | for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) { |
| 1996 | Record.clear(); |
| 1997 | Record.push_back(SUBMODULE_HEADER); |
| 1998 | Stream.EmitRecordWithBlob(HeaderAbbrev, Record, |
| 1999 | Mod->Headers[I]->getName()); |
| 2000 | } |
Douglas Gregor | 5598868 | 2011-12-05 16:33:54 +0000 | [diff] [blame] | 2001 | |
| 2002 | // Emit the imports. |
| 2003 | if (!Mod->Imports.empty()) { |
| 2004 | Record.clear(); |
| 2005 | for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) { |
Douglas Gregor | bab9f4a | 2011-12-12 23:17:57 +0000 | [diff] [blame] | 2006 | unsigned ImportedID = getSubmoduleID(Mod->Imports[I]); |
Douglas Gregor | 5598868 | 2011-12-05 16:33:54 +0000 | [diff] [blame] | 2007 | assert(ImportedID && "Unknown submodule!"); |
| 2008 | Record.push_back(ImportedID); |
| 2009 | } |
| 2010 | Stream.EmitRecord(SUBMODULE_IMPORTS, Record); |
| 2011 | } |
| 2012 | |
Douglas Gregor | af13bfc | 2011-12-02 18:58:38 +0000 | [diff] [blame] | 2013 | // Emit the exports. |
| 2014 | if (!Mod->Exports.empty()) { |
| 2015 | Record.clear(); |
| 2016 | for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) { |
Douglas Gregor | bab9f4a | 2011-12-12 23:17:57 +0000 | [diff] [blame] | 2017 | if (Module *Exported = Mod->Exports[I].getPointer()) { |
| 2018 | unsigned ExportedID = SubmoduleIDs[Exported]; |
| 2019 | assert(ExportedID > 0 && "Unknown submodule ID?"); |
| 2020 | Record.push_back(ExportedID); |
| 2021 | } else { |
| 2022 | Record.push_back(0); |
| 2023 | } |
| 2024 | |
Douglas Gregor | af13bfc | 2011-12-02 18:58:38 +0000 | [diff] [blame] | 2025 | Record.push_back(Mod->Exports[I].getInt()); |
| 2026 | } |
| 2027 | Stream.EmitRecord(SUBMODULE_EXPORTS, Record); |
| 2028 | } |
| 2029 | |
Douglas Gregor | 392ed2b | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 2030 | // Queue up the submodules of this module. |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 2031 | for (Module::submodule_iterator Sub = Mod->submodule_begin(), |
| 2032 | SubEnd = Mod->submodule_end(); |
Douglas Gregor | 392ed2b | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 2033 | Sub != SubEnd; ++Sub) |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 2034 | Q.push(*Sub); |
Douglas Gregor | 392ed2b | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 2035 | } |
| 2036 | |
| 2037 | Stream.ExitBlock(); |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 2038 | |
| 2039 | assert((NextSubmoduleID - FirstSubmoduleID |
| 2040 | == getNumberOfModules(WritingModule)) && "Wrong # of submodules"); |
Douglas Gregor | 392ed2b | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 2041 | } |
| 2042 | |
Douglas Gregor | 185dbd7 | 2011-12-01 02:07:58 +0000 | [diff] [blame] | 2043 | serialization::SubmoduleID |
| 2044 | ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) { |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 2045 | if (Loc.isInvalid() || !WritingModule) |
Douglas Gregor | 185dbd7 | 2011-12-01 02:07:58 +0000 | [diff] [blame] | 2046 | return 0; // No submodule |
Douglas Gregor | 5598868 | 2011-12-05 16:33:54 +0000 | [diff] [blame] | 2047 | |
| 2048 | // Find the module that owns this location. |
Douglas Gregor | 185dbd7 | 2011-12-01 02:07:58 +0000 | [diff] [blame] | 2049 | ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap(); |
Douglas Gregor | 5598868 | 2011-12-05 16:33:54 +0000 | [diff] [blame] | 2050 | Module *OwningMod |
| 2051 | = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager())); |
Douglas Gregor | 185dbd7 | 2011-12-01 02:07:58 +0000 | [diff] [blame] | 2052 | if (!OwningMod) |
| 2053 | return 0; |
| 2054 | |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 2055 | // Check whether this submodule is part of our own module. |
| 2056 | if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule)) |
Douglas Gregor | 185dbd7 | 2011-12-01 02:07:58 +0000 | [diff] [blame] | 2057 | return 0; |
| 2058 | |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 2059 | return getSubmoduleID(OwningMod); |
Douglas Gregor | 185dbd7 | 2011-12-01 02:07:58 +0000 | [diff] [blame] | 2060 | } |
| 2061 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 2062 | void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) { |
Argyrios Kyrtzidis | f41d3be | 2010-11-05 22:10:18 +0000 | [diff] [blame] | 2063 | RecordData Record; |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 2064 | for (DiagnosticsEngine::DiagStatePointsTy::const_iterator |
Argyrios Kyrtzidis | 3efd52c | 2011-01-14 20:54:07 +0000 | [diff] [blame] | 2065 | I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end(); |
| 2066 | I != E; ++I) { |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 2067 | const DiagnosticsEngine::DiagStatePoint &point = *I; |
Argyrios Kyrtzidis | 3efd52c | 2011-01-14 20:54:07 +0000 | [diff] [blame] | 2068 | if (point.Loc.isInvalid()) |
| 2069 | continue; |
| 2070 | |
| 2071 | Record.push_back(point.Loc.getRawEncoding()); |
Daniel Dunbar | ba494c6 | 2011-09-29 01:42:25 +0000 | [diff] [blame] | 2072 | for (DiagnosticsEngine::DiagState::const_iterator |
Argyrios Kyrtzidis | 3efd52c | 2011-01-14 20:54:07 +0000 | [diff] [blame] | 2073 | I = point.State->begin(), E = point.State->end(); I != E; ++I) { |
Daniel Dunbar | b1c99c6 | 2011-09-29 01:30:00 +0000 | [diff] [blame] | 2074 | if (I->second.isPragma()) { |
| 2075 | Record.push_back(I->first); |
| 2076 | Record.push_back(I->second.getMapping()); |
Argyrios Kyrtzidis | 3efd52c | 2011-01-14 20:54:07 +0000 | [diff] [blame] | 2077 | } |
Argyrios Kyrtzidis | f41d3be | 2010-11-05 22:10:18 +0000 | [diff] [blame] | 2078 | } |
Argyrios Kyrtzidis | 3efd52c | 2011-01-14 20:54:07 +0000 | [diff] [blame] | 2079 | Record.push_back(-1); // mark the end of the diag/map pairs for this |
| 2080 | // location. |
Argyrios Kyrtzidis | f41d3be | 2010-11-05 22:10:18 +0000 | [diff] [blame] | 2081 | } |
| 2082 | |
Argyrios Kyrtzidis | 60f7684 | 2010-11-05 22:20:49 +0000 | [diff] [blame] | 2083 | if (!Record.empty()) |
Argyrios Kyrtzidis | 3efd52c | 2011-01-14 20:54:07 +0000 | [diff] [blame] | 2084 | Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record); |
Argyrios Kyrtzidis | f41d3be | 2010-11-05 22:10:18 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
Anders Carlsson | c850578 | 2011-03-06 18:41:18 +0000 | [diff] [blame] | 2087 | void ASTWriter::WriteCXXBaseSpecifiersOffsets() { |
| 2088 | if (CXXBaseSpecifiersOffsets.empty()) |
| 2089 | return; |
| 2090 | |
| 2091 | RecordData Record; |
| 2092 | |
| 2093 | // Create a blob abbreviation for the C++ base specifiers offsets. |
| 2094 | using namespace llvm; |
| 2095 | |
| 2096 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 2097 | Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS)); |
| 2098 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size |
| 2099 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 2100 | unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev); |
| 2101 | |
Douglas Gregor | e92b8a1 | 2011-08-04 00:01:48 +0000 | [diff] [blame] | 2102 | // Write the base specifier offsets table. |
Anders Carlsson | c850578 | 2011-03-06 18:41:18 +0000 | [diff] [blame] | 2103 | Record.clear(); |
| 2104 | Record.push_back(CXX_BASE_SPECIFIER_OFFSETS); |
| 2105 | Record.push_back(CXXBaseSpecifiersOffsets.size()); |
| 2106 | Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record, |
Benjamin Kramer | 6e089c6 | 2011-04-24 17:44:50 +0000 | [diff] [blame] | 2107 | data(CXXBaseSpecifiersOffsets)); |
Anders Carlsson | c850578 | 2011-03-06 18:41:18 +0000 | [diff] [blame] | 2108 | } |
| 2109 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 2110 | //===----------------------------------------------------------------------===// |
| 2111 | // Type Serialization |
| 2112 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 2113 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2114 | /// \brief Write the representation of a type to the AST stream. |
Sebastian Redl | a4232eb | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 2115 | void ASTWriter::WriteType(QualType T) { |
Argyrios Kyrtzidis | 01b81c4 | 2010-08-20 16:04:04 +0000 | [diff] [blame] | 2116 | TypeIdx &Idx = TypeIdxs[T]; |
Argyrios Kyrtzidis | c8e5d51 | 2010-08-20 16:03:59 +0000 | [diff] [blame] | 2117 | if (Idx.getIndex() == 0) // we haven't seen this type before. |
| 2118 | Idx = TypeIdx(NextTypeID++); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2119 | |
Douglas Gregor | 9747583 | 2010-10-05 18:37:06 +0000 | [diff] [blame] | 2120 | assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST"); |
Douglas Gregor | 55f48de | 2010-10-04 18:21:45 +0000 | [diff] [blame] | 2121 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2122 | // Record the offset for this type. |
Argyrios Kyrtzidis | c8e5d51 | 2010-08-20 16:03:59 +0000 | [diff] [blame] | 2123 | unsigned Index = Idx.getIndex() - FirstTypeID; |
Sebastian Redl | 681d723 | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 2124 | if (TypeOffsets.size() == Index) |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 2125 | TypeOffsets.push_back(Stream.GetCurrentBitNo()); |
Sebastian Redl | 681d723 | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 2126 | else if (TypeOffsets.size() < Index) { |
| 2127 | TypeOffsets.resize(Index + 1); |
| 2128 | TypeOffsets[Index] = Stream.GetCurrentBitNo(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
| 2131 | RecordData Record; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2132 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2133 | // Emit the type's representation. |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2134 | ASTTypeWriter W(*this, Record); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2135 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2136 | if (T.hasLocalNonFastQualifiers()) { |
| 2137 | Qualifiers Qs = T.getLocalQualifiers(); |
| 2138 | AddTypeRef(T.getLocalUnqualifiedType(), Record); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2139 | Record.push_back(Qs.getAsOpaqueValue()); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2140 | W.Code = TYPE_EXT_QUAL; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2141 | } else { |
| 2142 | switch (T->getTypeClass()) { |
| 2143 | // For all of the concrete, non-dependent types, call the |
| 2144 | // appropriate visitor function. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2145 | #define TYPE(Class, Base) \ |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 2146 | case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2147 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2148 | #include "clang/AST/TypeNodes.def" |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2149 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2150 | } |
| 2151 | |
| 2152 | // Emit the serialized record. |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 2153 | Stream.EmitRecord(W.Code, Record); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 2154 | |
| 2155 | // Flush any expressions that were written as part of this type. |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 2156 | FlushStmts(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 2159 | //===----------------------------------------------------------------------===// |
| 2160 | // Declaration Serialization |
| 2161 | //===----------------------------------------------------------------------===// |
| 2162 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2163 | /// \brief Write the block containing all of the declaration IDs |
| 2164 | /// lexically declared within the given DeclContext. |
| 2165 | /// |
| 2166 | /// \returns the offset of the DECL_CONTEXT_LEXICAL block within the |
| 2167 | /// bistream, or 0 if no block was written. |
Sebastian Redl | a4232eb | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 2168 | uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context, |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2169 | DeclContext *DC) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2170 | if (DC->decls_empty()) |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2171 | return 0; |
| 2172 | |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 2173 | uint64_t Offset = Stream.GetCurrentBitNo(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2174 | RecordData Record; |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2175 | Record.push_back(DECL_CONTEXT_LEXICAL); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2176 | SmallVector<KindDeclIDPair, 64> Decls; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2177 | for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end(); |
| 2178 | D != DEnd; ++D) |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2179 | Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D))); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2180 | |
Douglas Gregor | 2512308 | 2009-04-22 22:34:57 +0000 | [diff] [blame] | 2181 | ++NumLexicalDeclContexts; |
Benjamin Kramer | 6e089c6 | 2011-04-24 17:44:50 +0000 | [diff] [blame] | 2182 | Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls)); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2183 | return Offset; |
| 2184 | } |
| 2185 | |
Sebastian Redl | a4232eb | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 2186 | void ASTWriter::WriteTypeDeclOffsets() { |
Sebastian Redl | 1476ed4 | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 2187 | using namespace llvm; |
| 2188 | RecordData Record; |
| 2189 | |
| 2190 | // Write the type offsets array |
| 2191 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2192 | Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET)); |
Sebastian Redl | 1476ed4 | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 2193 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types |
Douglas Gregor | a119da0 | 2011-08-02 16:26:37 +0000 | [diff] [blame] | 2194 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index |
Sebastian Redl | 1476ed4 | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 2195 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block |
| 2196 | unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev); |
| 2197 | Record.clear(); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2198 | Record.push_back(TYPE_OFFSET); |
Sebastian Redl | 1476ed4 | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 2199 | Record.push_back(TypeOffsets.size()); |
Douglas Gregor | a119da0 | 2011-08-02 16:26:37 +0000 | [diff] [blame] | 2200 | Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS); |
Benjamin Kramer | 6e089c6 | 2011-04-24 17:44:50 +0000 | [diff] [blame] | 2201 | Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets)); |
Sebastian Redl | 1476ed4 | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 2202 | |
| 2203 | // Write the declaration offsets array |
| 2204 | Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2205 | Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET)); |
Sebastian Redl | 1476ed4 | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 2206 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations |
Douglas Gregor | 496c709 | 2011-08-03 15:48:04 +0000 | [diff] [blame] | 2207 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID |
Sebastian Redl | 1476ed4 | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 2208 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block |
| 2209 | unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev); |
| 2210 | Record.clear(); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2211 | Record.push_back(DECL_OFFSET); |
Sebastian Redl | 1476ed4 | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 2212 | Record.push_back(DeclOffsets.size()); |
Douglas Gregor | 0a14e4b | 2011-08-03 16:05:40 +0000 | [diff] [blame] | 2213 | Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS); |
Benjamin Kramer | 6e089c6 | 2011-04-24 17:44:50 +0000 | [diff] [blame] | 2214 | Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets)); |
Sebastian Redl | 1476ed4 | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
Argyrios Kyrtzidis | 10f3df5 | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 2217 | void ASTWriter::WriteFileDeclIDsMap() { |
| 2218 | using namespace llvm; |
| 2219 | RecordData Record; |
| 2220 | |
| 2221 | // Join the vectors of DeclIDs from all files. |
| 2222 | SmallVector<DeclID, 256> FileSortedIDs; |
| 2223 | for (FileDeclIDsTy::iterator |
| 2224 | FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) { |
| 2225 | DeclIDInFileInfo &Info = *FI->second; |
| 2226 | Info.FirstDeclIndex = FileSortedIDs.size(); |
| 2227 | for (LocDeclIDsTy::iterator |
| 2228 | DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI) |
| 2229 | FileSortedIDs.push_back(DI->second); |
| 2230 | } |
| 2231 | |
| 2232 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 2233 | Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS)); |
| 2234 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 2235 | unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev); |
| 2236 | Record.push_back(FILE_SORTED_DECLS); |
| 2237 | Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs)); |
| 2238 | } |
| 2239 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 2240 | //===----------------------------------------------------------------------===// |
| 2241 | // Global Method Pool and Selector Serialization |
| 2242 | //===----------------------------------------------------------------------===// |
| 2243 | |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2244 | namespace { |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2245 | // Trait used for the on-disk hash table used in the method pool. |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2246 | class ASTMethodPoolTrait { |
Sebastian Redl | a4232eb | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 2247 | ASTWriter &Writer; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2248 | |
| 2249 | public: |
| 2250 | typedef Selector key_type; |
| 2251 | typedef key_type key_type_ref; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2252 | |
Sebastian Redl | 5d05007 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 2253 | struct data_type { |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2254 | SelectorID ID; |
Sebastian Redl | 5d05007 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 2255 | ObjCMethodList Instance, Factory; |
| 2256 | }; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2257 | typedef const data_type& data_type_ref; |
| 2258 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2259 | explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2260 | |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2261 | static unsigned ComputeHash(Selector Sel) { |
Argyrios Kyrtzidis | 0eca89e | 2010-08-20 16:03:52 +0000 | [diff] [blame] | 2262 | return serialization::ComputeHash(Sel); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2263 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2264 | |
| 2265 | std::pair<unsigned,unsigned> |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2266 | EmitKeyDataLength(raw_ostream& Out, Selector Sel, |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2267 | data_type_ref Methods) { |
| 2268 | unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4); |
| 2269 | clang::io::Emit16(Out, KeyLen); |
Sebastian Redl | 5d05007 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 2270 | unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts |
| 2271 | for (const ObjCMethodList *Method = &Methods.Instance; Method; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2272 | Method = Method->Next) |
| 2273 | if (Method->Method) |
| 2274 | DataLen += 4; |
Sebastian Redl | 5d05007 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 2275 | for (const ObjCMethodList *Method = &Methods.Factory; Method; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2276 | Method = Method->Next) |
| 2277 | if (Method->Method) |
| 2278 | DataLen += 4; |
| 2279 | clang::io::Emit16(Out, DataLen); |
| 2280 | return std::make_pair(KeyLen, DataLen); |
| 2281 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2282 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2283 | void EmitKey(raw_ostream& Out, Selector Sel, unsigned) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2284 | uint64_t Start = Out.tell(); |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2285 | assert((Start >> 32) == 0 && "Selector key offset too large"); |
| 2286 | Writer.SetSelectorOffset(Sel, Start); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2287 | unsigned N = Sel.getNumArgs(); |
| 2288 | clang::io::Emit16(Out, N); |
| 2289 | if (N == 0) |
| 2290 | N = 1; |
| 2291 | for (unsigned I = 0; I != N; ++I) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2292 | clang::io::Emit32(Out, |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2293 | Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I))); |
| 2294 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2295 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2296 | void EmitData(raw_ostream& Out, key_type_ref, |
Douglas Gregor | a67e58c | 2009-04-24 21:49:02 +0000 | [diff] [blame] | 2297 | data_type_ref Methods, unsigned DataLen) { |
| 2298 | uint64_t Start = Out.tell(); (void)Start; |
Sebastian Redl | 5d05007 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 2299 | clang::io::Emit32(Out, Methods.ID); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2300 | unsigned NumInstanceMethods = 0; |
Sebastian Redl | 5d05007 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 2301 | for (const ObjCMethodList *Method = &Methods.Instance; Method; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2302 | Method = Method->Next) |
| 2303 | if (Method->Method) |
| 2304 | ++NumInstanceMethods; |
| 2305 | |
| 2306 | unsigned NumFactoryMethods = 0; |
Sebastian Redl | 5d05007 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 2307 | for (const ObjCMethodList *Method = &Methods.Factory; Method; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2308 | Method = Method->Next) |
| 2309 | if (Method->Method) |
| 2310 | ++NumFactoryMethods; |
| 2311 | |
| 2312 | clang::io::Emit16(Out, NumInstanceMethods); |
| 2313 | clang::io::Emit16(Out, NumFactoryMethods); |
Sebastian Redl | 5d05007 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 2314 | for (const ObjCMethodList *Method = &Methods.Instance; Method; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2315 | Method = Method->Next) |
| 2316 | if (Method->Method) |
| 2317 | clang::io::Emit32(Out, Writer.getDeclID(Method->Method)); |
Sebastian Redl | 5d05007 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 2318 | for (const ObjCMethodList *Method = &Methods.Factory; Method; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2319 | Method = Method->Next) |
| 2320 | if (Method->Method) |
| 2321 | clang::io::Emit32(Out, Writer.getDeclID(Method->Method)); |
Douglas Gregor | a67e58c | 2009-04-24 21:49:02 +0000 | [diff] [blame] | 2322 | |
| 2323 | assert(Out.tell() - Start == DataLen && "Data length is wrong"); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2324 | } |
| 2325 | }; |
| 2326 | } // end anonymous namespace |
| 2327 | |
Sebastian Redl | 059612d | 2010-08-03 21:58:15 +0000 | [diff] [blame] | 2328 | /// \brief Write ObjC data: selectors and the method pool. |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2329 | /// |
| 2330 | /// The method pool contains both instance and factory methods, stored |
Sebastian Redl | 059612d | 2010-08-03 21:58:15 +0000 | [diff] [blame] | 2331 | /// in an on-disk hash table indexed by the selector. The hash table also |
| 2332 | /// contains an empty entry for every other selector known to Sema. |
Sebastian Redl | a4232eb | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 2333 | void ASTWriter::WriteSelectors(Sema &SemaRef) { |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2334 | using namespace llvm; |
| 2335 | |
Sebastian Redl | 059612d | 2010-08-03 21:58:15 +0000 | [diff] [blame] | 2336 | // Do we have to do anything at all? |
Sebastian Redl | 5d05007 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 2337 | if (SemaRef.MethodPool.empty() && SelectorIDs.empty()) |
Sebastian Redl | 059612d | 2010-08-03 21:58:15 +0000 | [diff] [blame] | 2338 | return; |
Sebastian Redl | e58aa89 | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 2339 | unsigned NumTableEntries = 0; |
Sebastian Redl | 059612d | 2010-08-03 21:58:15 +0000 | [diff] [blame] | 2340 | // Create and write out the blob that contains selectors and the method pool. |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2341 | { |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2342 | OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator; |
Argyrios Kyrtzidis | 5d26768 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 2343 | ASTMethodPoolTrait Trait(*this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2344 | |
Sebastian Redl | 059612d | 2010-08-03 21:58:15 +0000 | [diff] [blame] | 2345 | // Create the on-disk hash table representation. We walk through every |
| 2346 | // selector we've seen and look it up in the method pool. |
Sebastian Redl | e58aa89 | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 2347 | SelectorOffsets.resize(NextSelectorID - FirstSelectorID); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2348 | for (llvm::DenseMap<Selector, SelectorID>::iterator |
Sebastian Redl | 5d05007 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 2349 | I = SelectorIDs.begin(), E = SelectorIDs.end(); |
| 2350 | I != E; ++I) { |
| 2351 | Selector S = I->first; |
Sebastian Redl | 059612d | 2010-08-03 21:58:15 +0000 | [diff] [blame] | 2352 | Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S); |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2353 | ASTMethodPoolTrait::data_type Data = { |
Sebastian Redl | 5d05007 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 2354 | I->second, |
| 2355 | ObjCMethodList(), |
| 2356 | ObjCMethodList() |
| 2357 | }; |
| 2358 | if (F != SemaRef.MethodPool.end()) { |
| 2359 | Data.Instance = F->second.first; |
| 2360 | Data.Factory = F->second.second; |
| 2361 | } |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2362 | // Only write this selector if it's not in an existing AST or something |
Sebastian Redl | e58aa89 | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 2363 | // changed. |
| 2364 | if (Chain && I->second < FirstSelectorID) { |
| 2365 | // Selector already exists. Did it change? |
| 2366 | bool changed = false; |
| 2367 | for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method; |
| 2368 | M = M->Next) { |
Douglas Gregor | 919814d | 2011-09-09 23:01:35 +0000 | [diff] [blame] | 2369 | if (!M->Method->isFromASTFile()) |
Sebastian Redl | e58aa89 | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 2370 | changed = true; |
| 2371 | } |
| 2372 | for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method; |
| 2373 | M = M->Next) { |
Douglas Gregor | 919814d | 2011-09-09 23:01:35 +0000 | [diff] [blame] | 2374 | if (!M->Method->isFromASTFile()) |
Sebastian Redl | e58aa89 | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 2375 | changed = true; |
| 2376 | } |
| 2377 | if (!changed) |
| 2378 | continue; |
Sebastian Redl | fa78dec | 2010-08-04 21:22:45 +0000 | [diff] [blame] | 2379 | } else if (Data.Instance.Method || Data.Factory.Method) { |
| 2380 | // A new method pool entry. |
| 2381 | ++NumTableEntries; |
Sebastian Redl | e58aa89 | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 2382 | } |
Argyrios Kyrtzidis | 5d26768 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 2383 | Generator.insert(S, Data, Trait); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2384 | } |
| 2385 | |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2386 | // Create the on-disk hash table in a buffer. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2387 | SmallString<4096> MethodPool; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2388 | uint32_t BucketOffset; |
| 2389 | { |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2390 | ASTMethodPoolTrait Trait(*this); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2391 | llvm::raw_svector_ostream Out(MethodPool); |
| 2392 | // Make sure that no bucket is at offset 0 |
Douglas Gregor | a67e58c | 2009-04-24 21:49:02 +0000 | [diff] [blame] | 2393 | clang::io::Emit32(Out, 0); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2394 | BucketOffset = Generator.Emit(Out, Trait); |
| 2395 | } |
| 2396 | |
| 2397 | // Create a blob abbreviation |
| 2398 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2399 | Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL)); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2400 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2401 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2402 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 2403 | unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev); |
| 2404 | |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2405 | // Write the method pool |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2406 | RecordData Record; |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2407 | Record.push_back(METHOD_POOL); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2408 | Record.push_back(BucketOffset); |
Sebastian Redl | e58aa89 | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 2409 | Record.push_back(NumTableEntries); |
Daniel Dunbar | ec312a1 | 2009-08-24 09:31:37 +0000 | [diff] [blame] | 2410 | Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str()); |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2411 | |
| 2412 | // Create a blob abbreviation for the selector table offsets. |
| 2413 | Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2414 | Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS)); |
Douglas Gregor | 7c789c1 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 2415 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size |
Douglas Gregor | b18b1fd | 2011-08-03 23:28:44 +0000 | [diff] [blame] | 2416 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2417 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 2418 | unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev); |
| 2419 | |
| 2420 | // Write the selector offsets table. |
| 2421 | Record.clear(); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2422 | Record.push_back(SELECTOR_OFFSETS); |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2423 | Record.push_back(SelectorOffsets.size()); |
Douglas Gregor | b18b1fd | 2011-08-03 23:28:44 +0000 | [diff] [blame] | 2424 | Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS); |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2425 | Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record, |
Benjamin Kramer | 6e089c6 | 2011-04-24 17:44:50 +0000 | [diff] [blame] | 2426 | data(SelectorOffsets)); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2427 | } |
| 2428 | } |
| 2429 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2430 | /// \brief Write the selectors referenced in @selector expression into AST file. |
Sebastian Redl | a4232eb | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 2431 | void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) { |
Fariborz Jahanian | 3201983 | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 2432 | using namespace llvm; |
| 2433 | if (SemaRef.ReferencedSelectors.empty()) |
| 2434 | return; |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 2435 | |
Fariborz Jahanian | 3201983 | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 2436 | RecordData Record; |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 2437 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2438 | // Note: this writes out all references even for a dependent AST. But it is |
Sebastian Redl | a68340f | 2010-08-04 22:21:29 +0000 | [diff] [blame] | 2439 | // very tricky to fix, and given that @selector shouldn't really appear in |
| 2440 | // headers, probably not worth it. It's not a correctness issue. |
Fariborz Jahanian | 3201983 | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 2441 | for (DenseMap<Selector, SourceLocation>::iterator S = |
| 2442 | SemaRef.ReferencedSelectors.begin(), |
| 2443 | E = SemaRef.ReferencedSelectors.end(); S != E; ++S) { |
| 2444 | Selector Sel = (*S).first; |
| 2445 | SourceLocation Loc = (*S).second; |
| 2446 | AddSelectorRef(Sel, Record); |
| 2447 | AddSourceLocation(Loc, Record); |
| 2448 | } |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2449 | Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record); |
Fariborz Jahanian | 3201983 | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 2450 | } |
| 2451 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 2452 | //===----------------------------------------------------------------------===// |
| 2453 | // Identifier Table Serialization |
| 2454 | //===----------------------------------------------------------------------===// |
| 2455 | |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2456 | namespace { |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2457 | class ASTIdentifierTableTrait { |
Sebastian Redl | a4232eb | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 2458 | ASTWriter &Writer; |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 2459 | Preprocessor &PP; |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 2460 | IdentifierResolver &IdResolver; |
Douglas Gregor | 7143aab | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 2461 | bool IsModule; |
| 2462 | |
Douglas Gregor | a92193e | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 2463 | /// \brief Determines whether this is an "interesting" identifier |
| 2464 | /// that needs a full IdentifierInfo structure written into the hash |
| 2465 | /// table. |
Douglas Gregor | 7143aab | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 2466 | bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) { |
Douglas Gregor | 7143aab | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 2467 | if (II->isPoisoned() || |
| 2468 | II->isExtensionToken() || |
| 2469 | II->getObjCOrBuiltinID() || |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 2470 | II->hasRevertedTokenIDToIdentifier() || |
Douglas Gregor | 7143aab | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 2471 | II->getFETokenInfo<void>()) |
| 2472 | return true; |
| 2473 | |
Douglas Gregor | ce835df | 2011-09-14 22:14:14 +0000 | [diff] [blame] | 2474 | return hasMacroDefinition(II, Macro); |
| 2475 | } |
| 2476 | |
| 2477 | bool hasMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) { |
Douglas Gregor | 7143aab | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 2478 | if (!II->hasMacroDefinition()) |
| 2479 | return false; |
| 2480 | |
Douglas Gregor | ce835df | 2011-09-14 22:14:14 +0000 | [diff] [blame] | 2481 | if (Macro || (Macro = PP.getMacroInfo(II))) |
Douglas Gregor | aa93a87 | 2011-10-17 15:32:29 +0000 | [diff] [blame] | 2482 | return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic()); |
Douglas Gregor | 7143aab | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 2483 | |
Douglas Gregor | ce835df | 2011-09-14 22:14:14 +0000 | [diff] [blame] | 2484 | return false; |
Douglas Gregor | a92193e | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 2485 | } |
| 2486 | |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2487 | public: |
Douglas Gregor | 7143aab | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 2488 | typedef IdentifierInfo* key_type; |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2489 | typedef key_type key_type_ref; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2490 | |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2491 | typedef IdentID data_type; |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2492 | typedef data_type data_type_ref; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2493 | |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 2494 | ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP, |
| 2495 | IdentifierResolver &IdResolver, bool IsModule) |
| 2496 | : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { } |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2497 | |
| 2498 | static unsigned ComputeHash(const IdentifierInfo* II) { |
Daniel Dunbar | 2596e42 | 2009-10-17 23:52:28 +0000 | [diff] [blame] | 2499 | return llvm::HashString(II->getName()); |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2500 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2501 | |
| 2502 | std::pair<unsigned,unsigned> |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 2503 | EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) { |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2504 | unsigned KeyLen = II->getLength() + 1; |
Douglas Gregor | a92193e | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 2505 | unsigned DataLen = 4; // 4 bytes for the persistent ID << 1 |
Douglas Gregor | ce835df | 2011-09-14 22:14:14 +0000 | [diff] [blame] | 2506 | MacroInfo *Macro = 0; |
Douglas Gregor | 7143aab | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 2507 | if (isInterestingIdentifier(II, Macro)) { |
Douglas Gregor | 5998da5 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 2508 | DataLen += 2; // 2 bytes for builtin ID, flags |
Douglas Gregor | ce835df | 2011-09-14 22:14:14 +0000 | [diff] [blame] | 2509 | if (hasMacroDefinition(II, Macro)) |
Douglas Gregor | 1329264 | 2011-12-02 15:45:10 +0000 | [diff] [blame] | 2510 | DataLen += 8; |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 2511 | |
| 2512 | for (IdentifierResolver::iterator D = IdResolver.begin(II), |
| 2513 | DEnd = IdResolver.end(); |
Douglas Gregor | a92193e | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 2514 | D != DEnd; ++D) |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2515 | DataLen += sizeof(DeclID); |
Douglas Gregor | a92193e | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 2516 | } |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2517 | clang::io::Emit16(Out, DataLen); |
Douglas Gregor | 02fc751 | 2009-04-28 20:01:51 +0000 | [diff] [blame] | 2518 | // We emit the key length after the data length so that every |
| 2519 | // string is preceded by a 16-bit length. This matches the PTH |
| 2520 | // format for storing identifiers. |
Douglas Gregor | d6595a4 | 2009-04-25 21:04:17 +0000 | [diff] [blame] | 2521 | clang::io::Emit16(Out, KeyLen); |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2522 | return std::make_pair(KeyLen, DataLen); |
| 2523 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2524 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2525 | void EmitKey(raw_ostream& Out, const IdentifierInfo* II, |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2526 | unsigned KeyLen) { |
| 2527 | // Record the location of the key data. This is used when generating |
| 2528 | // the mapping from persistent IDs to strings. |
| 2529 | Writer.SetIdentifierOffset(II, Out.tell()); |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2530 | Out.write(II->getNameStart(), KeyLen); |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2531 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2532 | |
Douglas Gregor | 7143aab | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 2533 | void EmitData(raw_ostream& Out, IdentifierInfo* II, |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2534 | IdentID ID, unsigned) { |
Douglas Gregor | ce835df | 2011-09-14 22:14:14 +0000 | [diff] [blame] | 2535 | MacroInfo *Macro = 0; |
Douglas Gregor | 7143aab | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 2536 | if (!isInterestingIdentifier(II, Macro)) { |
Douglas Gregor | a92193e | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 2537 | clang::io::Emit32(Out, ID << 1); |
| 2538 | return; |
| 2539 | } |
Douglas Gregor | 5998da5 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 2540 | |
Douglas Gregor | a92193e | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 2541 | clang::io::Emit32(Out, (ID << 1) | 0x01); |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2542 | uint32_t Bits = 0; |
Douglas Gregor | ce835df | 2011-09-14 22:14:14 +0000 | [diff] [blame] | 2543 | bool HasMacroDefinition = hasMacroDefinition(II, Macro); |
Douglas Gregor | 5998da5 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 2544 | Bits = (uint32_t)II->getObjCOrBuiltinID(); |
Craig Topper | 925be54 | 2011-12-19 05:04:33 +0000 | [diff] [blame] | 2545 | assert((Bits & 0x7ff) == Bits && "ObjCOrBuiltinID too big for ASTReader."); |
Douglas Gregor | ce835df | 2011-09-14 22:14:14 +0000 | [diff] [blame] | 2546 | Bits = (Bits << 1) | unsigned(HasMacroDefinition); |
Daniel Dunbar | b0b8438 | 2009-12-18 20:58:47 +0000 | [diff] [blame] | 2547 | Bits = (Bits << 1) | unsigned(II->isExtensionToken()); |
| 2548 | Bits = (Bits << 1) | unsigned(II->isPoisoned()); |
Argyrios Kyrtzidis | 646395b | 2010-08-11 22:55:12 +0000 | [diff] [blame] | 2549 | Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier()); |
Daniel Dunbar | b0b8438 | 2009-12-18 20:58:47 +0000 | [diff] [blame] | 2550 | Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword()); |
Douglas Gregor | 5998da5 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 2551 | clang::io::Emit16(Out, Bits); |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2552 | |
Douglas Gregor | 1329264 | 2011-12-02 15:45:10 +0000 | [diff] [blame] | 2553 | if (HasMacroDefinition) { |
Douglas Gregor | 5998da5 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 2554 | clang::io::Emit32(Out, Writer.getMacroOffset(II)); |
Douglas Gregor | 1329264 | 2011-12-02 15:45:10 +0000 | [diff] [blame] | 2555 | clang::io::Emit32(Out, |
| 2556 | Writer.inferSubmoduleIDFromLocation(Macro->getDefinitionLoc())); |
| 2557 | } |
| 2558 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2559 | // Emit the declaration IDs in reverse order, because the |
| 2560 | // IdentifierResolver provides the declarations as they would be |
| 2561 | // visible (e.g., the function "stat" would come before the struct |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 2562 | // "stat"), but the ASTReader adds declarations to the end of the list |
| 2563 | // (so we need to see the struct "status" before the function "status"). |
Sebastian Redl | f2f0f03 | 2010-07-23 23:49:55 +0000 | [diff] [blame] | 2564 | // Only emit declarations that aren't from a chained PCH, though. |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 2565 | SmallVector<Decl *, 16> Decls(IdResolver.begin(II), |
| 2566 | IdResolver.end()); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2567 | for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(), |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 2568 | DEnd = Decls.rend(); |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2569 | D != DEnd; ++D) |
Sebastian Redl | d8c5abb | 2010-08-02 18:30:12 +0000 | [diff] [blame] | 2570 | clang::io::Emit32(Out, Writer.getDeclID(*D)); |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2571 | } |
| 2572 | }; |
| 2573 | } // end anonymous namespace |
| 2574 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2575 | /// \brief Write the identifier table into the AST file. |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 2576 | /// |
| 2577 | /// The identifier table consists of a blob containing string data |
| 2578 | /// (the actual identifiers themselves) and a separate "offsets" index |
| 2579 | /// that maps identifier IDs to locations within the blob. |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 2580 | void ASTWriter::WriteIdentifierTable(Preprocessor &PP, |
| 2581 | IdentifierResolver &IdResolver, |
| 2582 | bool IsModule) { |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 2583 | using namespace llvm; |
| 2584 | |
| 2585 | // Create and write out the blob that contains the identifier |
| 2586 | // strings. |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 2587 | { |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2588 | OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator; |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 2589 | ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2590 | |
Douglas Gregor | 92b059e | 2009-04-28 20:33:11 +0000 | [diff] [blame] | 2591 | // Look for any identifiers that were named while processing the |
| 2592 | // headers, but are otherwise not needed. We add these to the hash |
| 2593 | // table to enable checking of the predefines buffer in the case |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2594 | // where the user adds new macro definitions when building the AST |
Douglas Gregor | 92b059e | 2009-04-28 20:33:11 +0000 | [diff] [blame] | 2595 | // file. |
| 2596 | for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(), |
| 2597 | IDEnd = PP.getIdentifierTable().end(); |
| 2598 | ID != IDEnd; ++ID) |
| 2599 | getIdentifierRef(ID->second); |
| 2600 | |
Sebastian Redl | f2f0f03 | 2010-07-23 23:49:55 +0000 | [diff] [blame] | 2601 | // Create the on-disk hash table representation. We only store offsets |
| 2602 | // for identifiers that appear here for the first time. |
| 2603 | IdentifierOffsets.resize(NextIdentID - FirstIdentID); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2604 | for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 2605 | ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end(); |
| 2606 | ID != IDEnd; ++ID) { |
| 2607 | assert(ID->first && "NULL identifier in identifier table"); |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 2608 | if (!Chain || !ID->first->isFromAST() || |
| 2609 | ID->first->hasChangedSinceDeserialization()) |
Douglas Gregor | 7143aab | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 2610 | Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second, |
| 2611 | Trait); |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2612 | } |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 2613 | |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2614 | // Create the on-disk hash table in a buffer. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2615 | SmallString<4096> IdentifierTable; |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2616 | uint32_t BucketOffset; |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2617 | { |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 2618 | ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule); |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2619 | llvm::raw_svector_ostream Out(IdentifierTable); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2620 | // Make sure that no bucket is at offset 0 |
Douglas Gregor | a67e58c | 2009-04-24 21:49:02 +0000 | [diff] [blame] | 2621 | clang::io::Emit32(Out, 0); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2622 | BucketOffset = Generator.Emit(Out, Trait); |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 2623 | } |
| 2624 | |
| 2625 | // Create a blob abbreviation |
| 2626 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2627 | Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE)); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2628 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2629 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 2630 | unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 2631 | |
| 2632 | // Write the identifier table |
| 2633 | RecordData Record; |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2634 | Record.push_back(IDENTIFIER_TABLE); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2635 | Record.push_back(BucketOffset); |
Daniel Dunbar | ec312a1 | 2009-08-24 09:31:37 +0000 | [diff] [blame] | 2636 | Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str()); |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 2637 | } |
| 2638 | |
| 2639 | // Write the offsets table for identifier IDs. |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2640 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2641 | Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET)); |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2642 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers |
Douglas Gregor | 6ec60e0 | 2011-08-03 21:49:18 +0000 | [diff] [blame] | 2643 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2644 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 2645 | unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev); |
| 2646 | |
| 2647 | RecordData Record; |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2648 | Record.push_back(IDENTIFIER_OFFSET); |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2649 | Record.push_back(IdentifierOffsets.size()); |
Douglas Gregor | 6ec60e0 | 2011-08-03 21:49:18 +0000 | [diff] [blame] | 2650 | Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS); |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2651 | Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record, |
Benjamin Kramer | 6e089c6 | 2011-04-24 17:44:50 +0000 | [diff] [blame] | 2652 | data(IdentifierOffsets)); |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 2653 | } |
| 2654 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 2655 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 5d26768 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 2656 | // DeclContext's Name Lookup Table Serialization |
| 2657 | //===----------------------------------------------------------------------===// |
| 2658 | |
| 2659 | namespace { |
| 2660 | // Trait used for the on-disk hash table used in the method pool. |
| 2661 | class ASTDeclContextNameLookupTrait { |
| 2662 | ASTWriter &Writer; |
| 2663 | |
| 2664 | public: |
| 2665 | typedef DeclarationName key_type; |
| 2666 | typedef key_type key_type_ref; |
| 2667 | |
| 2668 | typedef DeclContext::lookup_result data_type; |
| 2669 | typedef const data_type& data_type_ref; |
| 2670 | |
| 2671 | explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { } |
| 2672 | |
| 2673 | unsigned ComputeHash(DeclarationName Name) { |
| 2674 | llvm::FoldingSetNodeID ID; |
| 2675 | ID.AddInteger(Name.getNameKind()); |
| 2676 | |
| 2677 | switch (Name.getNameKind()) { |
| 2678 | case DeclarationName::Identifier: |
| 2679 | ID.AddString(Name.getAsIdentifierInfo()->getName()); |
| 2680 | break; |
| 2681 | case DeclarationName::ObjCZeroArgSelector: |
| 2682 | case DeclarationName::ObjCOneArgSelector: |
| 2683 | case DeclarationName::ObjCMultiArgSelector: |
| 2684 | ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector())); |
| 2685 | break; |
| 2686 | case DeclarationName::CXXConstructorName: |
| 2687 | case DeclarationName::CXXDestructorName: |
| 2688 | case DeclarationName::CXXConversionFunctionName: |
Argyrios Kyrtzidis | 5d26768 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 2689 | break; |
| 2690 | case DeclarationName::CXXOperatorName: |
| 2691 | ID.AddInteger(Name.getCXXOverloadedOperator()); |
| 2692 | break; |
| 2693 | case DeclarationName::CXXLiteralOperatorName: |
| 2694 | ID.AddString(Name.getCXXLiteralIdentifier()->getName()); |
| 2695 | case DeclarationName::CXXUsingDirective: |
| 2696 | break; |
| 2697 | } |
| 2698 | |
| 2699 | return ID.ComputeHash(); |
| 2700 | } |
| 2701 | |
| 2702 | std::pair<unsigned,unsigned> |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2703 | EmitKeyDataLength(raw_ostream& Out, DeclarationName Name, |
Argyrios Kyrtzidis | 5d26768 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 2704 | data_type_ref Lookup) { |
| 2705 | unsigned KeyLen = 1; |
| 2706 | switch (Name.getNameKind()) { |
| 2707 | case DeclarationName::Identifier: |
| 2708 | case DeclarationName::ObjCZeroArgSelector: |
| 2709 | case DeclarationName::ObjCOneArgSelector: |
| 2710 | case DeclarationName::ObjCMultiArgSelector: |
Argyrios Kyrtzidis | 5d26768 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 2711 | case DeclarationName::CXXLiteralOperatorName: |
| 2712 | KeyLen += 4; |
| 2713 | break; |
| 2714 | case DeclarationName::CXXOperatorName: |
| 2715 | KeyLen += 1; |
| 2716 | break; |
Douglas Gregor | e360501 | 2011-08-02 18:32:54 +0000 | [diff] [blame] | 2717 | case DeclarationName::CXXConstructorName: |
| 2718 | case DeclarationName::CXXDestructorName: |
| 2719 | case DeclarationName::CXXConversionFunctionName: |
Argyrios Kyrtzidis | 5d26768 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 2720 | case DeclarationName::CXXUsingDirective: |
| 2721 | break; |
| 2722 | } |
| 2723 | clang::io::Emit16(Out, KeyLen); |
| 2724 | |
| 2725 | // 2 bytes for num of decls and 4 for each DeclID. |
| 2726 | unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first); |
| 2727 | clang::io::Emit16(Out, DataLen); |
| 2728 | |
| 2729 | return std::make_pair(KeyLen, DataLen); |
| 2730 | } |
| 2731 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2732 | void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) { |
Argyrios Kyrtzidis | 5d26768 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 2733 | using namespace clang::io; |
| 2734 | |
| 2735 | assert(Name.getNameKind() < 0x100 && "Invalid name kind ?"); |
| 2736 | Emit8(Out, Name.getNameKind()); |
| 2737 | switch (Name.getNameKind()) { |
| 2738 | case DeclarationName::Identifier: |
| 2739 | Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo())); |
| 2740 | break; |
| 2741 | case DeclarationName::ObjCZeroArgSelector: |
| 2742 | case DeclarationName::ObjCOneArgSelector: |
| 2743 | case DeclarationName::ObjCMultiArgSelector: |
| 2744 | Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector())); |
| 2745 | break; |
Argyrios Kyrtzidis | 5d26768 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 2746 | case DeclarationName::CXXOperatorName: |
| 2747 | assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?"); |
| 2748 | Emit8(Out, Name.getCXXOverloadedOperator()); |
| 2749 | break; |
| 2750 | case DeclarationName::CXXLiteralOperatorName: |
| 2751 | Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier())); |
| 2752 | break; |
Douglas Gregor | e360501 | 2011-08-02 18:32:54 +0000 | [diff] [blame] | 2753 | case DeclarationName::CXXConstructorName: |
| 2754 | case DeclarationName::CXXDestructorName: |
| 2755 | case DeclarationName::CXXConversionFunctionName: |
Argyrios Kyrtzidis | 5d26768 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 2756 | case DeclarationName::CXXUsingDirective: |
| 2757 | break; |
| 2758 | } |
| 2759 | } |
| 2760 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2761 | void EmitData(raw_ostream& Out, key_type_ref, |
Argyrios Kyrtzidis | 5d26768 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 2762 | data_type Lookup, unsigned DataLen) { |
| 2763 | uint64_t Start = Out.tell(); (void)Start; |
| 2764 | clang::io::Emit16(Out, Lookup.second - Lookup.first); |
| 2765 | for (; Lookup.first != Lookup.second; ++Lookup.first) |
| 2766 | clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first)); |
| 2767 | |
| 2768 | assert(Out.tell() - Start == DataLen && "Data length is wrong"); |
| 2769 | } |
| 2770 | }; |
| 2771 | } // end anonymous namespace |
| 2772 | |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 2773 | /// \brief Write the block containing all of the declaration IDs |
| 2774 | /// visible from the given DeclContext. |
| 2775 | /// |
| 2776 | /// \returns the offset of the DECL_CONTEXT_VISIBLE block within the |
Sebastian Redl | 1d1e42b | 2010-08-24 00:50:09 +0000 | [diff] [blame] | 2777 | /// bitstream, or 0 if no block was written. |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 2778 | uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context, |
| 2779 | DeclContext *DC) { |
| 2780 | if (DC->getPrimaryContext() != DC) |
| 2781 | return 0; |
| 2782 | |
| 2783 | // Since there is no name lookup into functions or methods, don't bother to |
| 2784 | // build a visible-declarations table for these entities. |
| 2785 | if (DC->isFunctionOrMethod()) |
| 2786 | return 0; |
| 2787 | |
| 2788 | // If not in C++, we perform name lookup for the translation unit via the |
| 2789 | // IdentifierInfo chains, don't bother to build a visible-declarations table. |
| 2790 | // FIXME: In C++ we need the visible declarations in order to "see" the |
| 2791 | // friend declarations, is there a way to do this without writing the table ? |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 2792 | if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus) |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 2793 | return 0; |
| 2794 | |
| 2795 | // Force the DeclContext to build a its name-lookup table. |
Douglas Gregor | c266de9 | 2011-08-24 21:56:08 +0000 | [diff] [blame] | 2796 | if (!DC->hasExternalVisibleStorage()) |
Argyrios Kyrtzidis | a60786b | 2010-08-20 23:35:55 +0000 | [diff] [blame] | 2797 | DC->lookup(DeclarationName()); |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 2798 | |
| 2799 | // Serialize the contents of the mapping used for lookup. Note that, |
| 2800 | // although we have two very different code paths, the serialized |
| 2801 | // representation is the same for both cases: a declaration name, |
| 2802 | // followed by a size, followed by references to the visible |
| 2803 | // declarations that have that name. |
| 2804 | uint64_t Offset = Stream.GetCurrentBitNo(); |
| 2805 | StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr()); |
| 2806 | if (!Map || Map->empty()) |
| 2807 | return 0; |
| 2808 | |
| 2809 | OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator; |
| 2810 | ASTDeclContextNameLookupTrait Trait(*this); |
| 2811 | |
| 2812 | // Create the on-disk hash table representation. |
Douglas Gregor | e5a54b6 | 2011-08-30 20:49:19 +0000 | [diff] [blame] | 2813 | DeclarationName ConversionName; |
| 2814 | llvm::SmallVector<NamedDecl *, 4> ConversionDecls; |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 2815 | for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end(); |
| 2816 | D != DEnd; ++D) { |
| 2817 | DeclarationName Name = D->first; |
| 2818 | DeclContext::lookup_result Result = D->second.getLookupResult(); |
Douglas Gregor | e5a54b6 | 2011-08-30 20:49:19 +0000 | [diff] [blame] | 2819 | if (Result.first != Result.second) { |
| 2820 | if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
| 2821 | // Hash all conversion function names to the same name. The actual |
| 2822 | // type information in conversion function name is not used in the |
| 2823 | // key (since such type information is not stable across different |
| 2824 | // modules), so the intended effect is to coalesce all of the conversion |
| 2825 | // functions under a single key. |
| 2826 | if (!ConversionName) |
| 2827 | ConversionName = Name; |
| 2828 | ConversionDecls.append(Result.first, Result.second); |
| 2829 | continue; |
| 2830 | } |
| 2831 | |
Argyrios Kyrtzidis | 45118d8 | 2011-08-30 19:43:23 +0000 | [diff] [blame] | 2832 | Generator.insert(Name, Result, Trait); |
Douglas Gregor | e5a54b6 | 2011-08-30 20:49:19 +0000 | [diff] [blame] | 2833 | } |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
Douglas Gregor | e5a54b6 | 2011-08-30 20:49:19 +0000 | [diff] [blame] | 2836 | // Add the conversion functions |
| 2837 | if (!ConversionDecls.empty()) { |
| 2838 | Generator.insert(ConversionName, |
| 2839 | DeclContext::lookup_result(ConversionDecls.begin(), |
| 2840 | ConversionDecls.end()), |
| 2841 | Trait); |
| 2842 | } |
| 2843 | |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 2844 | // Create the on-disk hash table in a buffer. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2845 | SmallString<4096> LookupTable; |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 2846 | uint32_t BucketOffset; |
| 2847 | { |
| 2848 | llvm::raw_svector_ostream Out(LookupTable); |
| 2849 | // Make sure that no bucket is at offset 0 |
| 2850 | clang::io::Emit32(Out, 0); |
| 2851 | BucketOffset = Generator.Emit(Out, Trait); |
| 2852 | } |
| 2853 | |
| 2854 | // Write the lookup table |
| 2855 | RecordData Record; |
| 2856 | Record.push_back(DECL_CONTEXT_VISIBLE); |
| 2857 | Record.push_back(BucketOffset); |
| 2858 | Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record, |
| 2859 | LookupTable.str()); |
| 2860 | |
| 2861 | Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record); |
| 2862 | ++NumVisibleDeclContexts; |
| 2863 | return Offset; |
| 2864 | } |
| 2865 | |
Sebastian Redl | 1d1e42b | 2010-08-24 00:50:09 +0000 | [diff] [blame] | 2866 | /// \brief Write an UPDATE_VISIBLE block for the given context. |
| 2867 | /// |
| 2868 | /// UPDATE_VISIBLE blocks contain the declarations that are added to an existing |
| 2869 | /// DeclContext in a dependent AST file. As such, they only exist for the TU |
| 2870 | /// (in C++) and for namespaces. |
| 2871 | void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) { |
Sebastian Redl | 1d1e42b | 2010-08-24 00:50:09 +0000 | [diff] [blame] | 2872 | StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr()); |
| 2873 | if (!Map || Map->empty()) |
| 2874 | return; |
| 2875 | |
| 2876 | OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator; |
| 2877 | ASTDeclContextNameLookupTrait Trait(*this); |
| 2878 | |
| 2879 | // Create the hash table. |
Sebastian Redl | 1d1e42b | 2010-08-24 00:50:09 +0000 | [diff] [blame] | 2880 | for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end(); |
| 2881 | D != DEnd; ++D) { |
| 2882 | DeclarationName Name = D->first; |
| 2883 | DeclContext::lookup_result Result = D->second.getLookupResult(); |
Sebastian Redl | 5967d62 | 2010-08-24 00:50:16 +0000 | [diff] [blame] | 2884 | // For any name that appears in this table, the results are complete, i.e. |
| 2885 | // they overwrite results from previous PCHs. Merging is always a mess. |
Argyrios Kyrtzidis | 45118d8 | 2011-08-30 19:43:23 +0000 | [diff] [blame] | 2886 | if (Result.first != Result.second) |
| 2887 | Generator.insert(Name, Result, Trait); |
Sebastian Redl | 1d1e42b | 2010-08-24 00:50:09 +0000 | [diff] [blame] | 2888 | } |
| 2889 | |
| 2890 | // Create the on-disk hash table in a buffer. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2891 | SmallString<4096> LookupTable; |
Sebastian Redl | 1d1e42b | 2010-08-24 00:50:09 +0000 | [diff] [blame] | 2892 | uint32_t BucketOffset; |
| 2893 | { |
| 2894 | llvm::raw_svector_ostream Out(LookupTable); |
| 2895 | // Make sure that no bucket is at offset 0 |
| 2896 | clang::io::Emit32(Out, 0); |
| 2897 | BucketOffset = Generator.Emit(Out, Trait); |
| 2898 | } |
| 2899 | |
| 2900 | // Write the lookup table |
| 2901 | RecordData Record; |
| 2902 | Record.push_back(UPDATE_VISIBLE); |
| 2903 | Record.push_back(getDeclID(cast<Decl>(DC))); |
| 2904 | Record.push_back(BucketOffset); |
| 2905 | Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str()); |
| 2906 | } |
| 2907 | |
Peter Collingbourne | 84bccea | 2011-02-15 19:46:30 +0000 | [diff] [blame] | 2908 | /// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions. |
| 2909 | void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) { |
| 2910 | RecordData Record; |
| 2911 | Record.push_back(Opts.fp_contract); |
| 2912 | Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record); |
| 2913 | } |
| 2914 | |
| 2915 | /// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions. |
| 2916 | void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 2917 | if (!SemaRef.Context.getLangOpts().OpenCL) |
Peter Collingbourne | 84bccea | 2011-02-15 19:46:30 +0000 | [diff] [blame] | 2918 | return; |
| 2919 | |
| 2920 | const OpenCLOptions &Opts = SemaRef.getOpenCLOptions(); |
| 2921 | RecordData Record; |
| 2922 | #define OPENCLEXT(nm) Record.push_back(Opts.nm); |
| 2923 | #include "clang/Basic/OpenCLExtensions.def" |
| 2924 | Stream.EmitRecord(OPENCL_EXTENSIONS, Record); |
| 2925 | } |
| 2926 | |
Douglas Gregor | 2171bf1 | 2012-01-15 16:58:34 +0000 | [diff] [blame] | 2927 | void ASTWriter::WriteRedeclarations() { |
| 2928 | RecordData LocalRedeclChains; |
| 2929 | SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap; |
| 2930 | |
| 2931 | for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) { |
| 2932 | Decl *First = Redeclarations[I]; |
| 2933 | assert(First->getPreviousDecl() == 0 && "Not the first declaration?"); |
| 2934 | |
| 2935 | Decl *MostRecent = First->getMostRecentDecl(); |
| 2936 | |
| 2937 | // If we only have a single declaration, there is no point in storing |
| 2938 | // a redeclaration chain. |
| 2939 | if (First == MostRecent) |
| 2940 | continue; |
| 2941 | |
| 2942 | unsigned Offset = LocalRedeclChains.size(); |
| 2943 | unsigned Size = 0; |
| 2944 | LocalRedeclChains.push_back(0); // Placeholder for the size. |
| 2945 | |
| 2946 | // Collect the set of local redeclarations of this declaration. |
| 2947 | for (Decl *Prev = MostRecent; Prev != First; |
| 2948 | Prev = Prev->getPreviousDecl()) { |
| 2949 | if (!Prev->isFromASTFile()) { |
| 2950 | AddDeclRef(Prev, LocalRedeclChains); |
| 2951 | ++Size; |
| 2952 | } |
| 2953 | } |
| 2954 | LocalRedeclChains[Offset] = Size; |
| 2955 | |
| 2956 | // Reverse the set of local redeclarations, so that we store them in |
| 2957 | // order (since we found them in reverse order). |
| 2958 | std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end()); |
| 2959 | |
| 2960 | // Add the mapping from the first ID to the set of local declarations. |
| 2961 | LocalRedeclarationsInfo Info = { getDeclID(First), Offset }; |
| 2962 | LocalRedeclsMap.push_back(Info); |
| 2963 | |
| 2964 | assert(N == Redeclarations.size() && |
| 2965 | "Deserialized a declaration we shouldn't have"); |
| 2966 | } |
| 2967 | |
| 2968 | if (LocalRedeclChains.empty()) |
| 2969 | return; |
| 2970 | |
| 2971 | // Sort the local redeclarations map by the first declaration ID, |
| 2972 | // since the reader will be performing binary searches on this information. |
| 2973 | llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end()); |
| 2974 | |
| 2975 | // Emit the local redeclarations map. |
| 2976 | using namespace llvm; |
| 2977 | llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 2978 | Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP)); |
| 2979 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries |
| 2980 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 2981 | unsigned AbbrevID = Stream.EmitAbbrev(Abbrev); |
| 2982 | |
| 2983 | RecordData Record; |
| 2984 | Record.push_back(LOCAL_REDECLARATIONS_MAP); |
| 2985 | Record.push_back(LocalRedeclsMap.size()); |
| 2986 | Stream.EmitRecordWithBlob(AbbrevID, Record, |
| 2987 | reinterpret_cast<char*>(LocalRedeclsMap.data()), |
| 2988 | LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo)); |
| 2989 | |
| 2990 | // Emit the redeclaration chains. |
| 2991 | Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains); |
| 2992 | } |
| 2993 | |
Douglas Gregor | cff9f26 | 2012-01-27 01:47:08 +0000 | [diff] [blame] | 2994 | void ASTWriter::WriteObjCCategories() { |
| 2995 | llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap; |
| 2996 | RecordData Categories; |
| 2997 | |
| 2998 | for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) { |
| 2999 | unsigned Size = 0; |
| 3000 | unsigned StartIndex = Categories.size(); |
| 3001 | |
| 3002 | ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I]; |
| 3003 | |
| 3004 | // Allocate space for the size. |
| 3005 | Categories.push_back(0); |
| 3006 | |
| 3007 | // Add the categories. |
| 3008 | for (ObjCCategoryDecl *Cat = Class->getCategoryList(); |
| 3009 | Cat; Cat = Cat->getNextClassCategory(), ++Size) { |
| 3010 | assert(getDeclID(Cat) != 0 && "Bogus category"); |
| 3011 | AddDeclRef(Cat, Categories); |
| 3012 | } |
| 3013 | |
| 3014 | // Update the size. |
| 3015 | Categories[StartIndex] = Size; |
| 3016 | |
| 3017 | // Record this interface -> category map. |
| 3018 | ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex }; |
| 3019 | CategoriesMap.push_back(CatInfo); |
| 3020 | } |
| 3021 | |
| 3022 | // Sort the categories map by the definition ID, since the reader will be |
| 3023 | // performing binary searches on this information. |
| 3024 | llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end()); |
| 3025 | |
| 3026 | // Emit the categories map. |
| 3027 | using namespace llvm; |
| 3028 | llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 3029 | Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP)); |
| 3030 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries |
| 3031 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 3032 | unsigned AbbrevID = Stream.EmitAbbrev(Abbrev); |
| 3033 | |
| 3034 | RecordData Record; |
| 3035 | Record.push_back(OBJC_CATEGORIES_MAP); |
| 3036 | Record.push_back(CategoriesMap.size()); |
| 3037 | Stream.EmitRecordWithBlob(AbbrevID, Record, |
| 3038 | reinterpret_cast<char*>(CategoriesMap.data()), |
| 3039 | CategoriesMap.size() * sizeof(ObjCCategoriesInfo)); |
| 3040 | |
| 3041 | // Emit the category lists. |
| 3042 | Stream.EmitRecord(OBJC_CATEGORIES, Categories); |
| 3043 | } |
| 3044 | |
Douglas Gregor | c3cfd2a | 2011-12-22 21:40:42 +0000 | [diff] [blame] | 3045 | void ASTWriter::WriteMergedDecls() { |
| 3046 | if (!Chain || Chain->MergedDecls.empty()) |
| 3047 | return; |
| 3048 | |
| 3049 | RecordData Record; |
| 3050 | for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(), |
| 3051 | IEnd = Chain->MergedDecls.end(); |
| 3052 | I != IEnd; ++I) { |
Douglas Gregor | b6b60c1 | 2012-01-05 22:27:05 +0000 | [diff] [blame] | 3053 | DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID() |
Douglas Gregor | c3cfd2a | 2011-12-22 21:40:42 +0000 | [diff] [blame] | 3054 | : getDeclID(I->first); |
| 3055 | assert(CanonID && "Merged declaration not known?"); |
| 3056 | |
| 3057 | Record.push_back(CanonID); |
| 3058 | Record.push_back(I->second.size()); |
| 3059 | Record.append(I->second.begin(), I->second.end()); |
| 3060 | } |
| 3061 | Stream.EmitRecord(MERGED_DECLARATIONS, Record); |
| 3062 | } |
| 3063 | |
Argyrios Kyrtzidis | 5d26768 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3064 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 3065 | // General Serialization Routines |
| 3066 | //===----------------------------------------------------------------------===// |
| 3067 | |
Douglas Gregor | 68a2eb0 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 3068 | /// \brief Write a record containing the given attributes. |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3069 | void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) { |
Argyrios Kyrtzidis | 4eb9fc0 | 2010-10-18 19:20:11 +0000 | [diff] [blame] | 3070 | Record.push_back(Attrs.size()); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3071 | for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){ |
| 3072 | const Attr * A = *i; |
| 3073 | Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs |
Argyrios Kyrtzidis | 768d6ca | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3074 | AddSourceRange(A->getRange(), Record); |
Douglas Gregor | 68a2eb0 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 3075 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3076 | #include "clang/Serialization/AttrPCHWrite.inc" |
Daniel Dunbar | 4e9255f | 2010-05-27 02:25:39 +0000 | [diff] [blame] | 3077 | |
Douglas Gregor | 68a2eb0 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 3078 | } |
Douglas Gregor | 68a2eb0 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 3079 | } |
| 3080 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3081 | void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) { |
Douglas Gregor | 68a2eb0 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 3082 | Record.push_back(Str.size()); |
| 3083 | Record.insert(Record.end(), Str.begin(), Str.end()); |
| 3084 | } |
| 3085 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 3086 | void ASTWriter::AddVersionTuple(const VersionTuple &Version, |
| 3087 | RecordDataImpl &Record) { |
| 3088 | Record.push_back(Version.getMajor()); |
| 3089 | if (llvm::Optional<unsigned> Minor = Version.getMinor()) |
| 3090 | Record.push_back(*Minor + 1); |
| 3091 | else |
| 3092 | Record.push_back(0); |
| 3093 | if (llvm::Optional<unsigned> Subminor = Version.getSubminor()) |
| 3094 | Record.push_back(*Subminor + 1); |
| 3095 | else |
| 3096 | Record.push_back(0); |
| 3097 | } |
| 3098 | |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 3099 | /// \brief Note that the identifier II occurs at the given offset |
| 3100 | /// within the identifier table. |
Sebastian Redl | a4232eb | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 3101 | void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) { |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3102 | IdentID ID = IdentifierIDs[II]; |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 3103 | // Only store offsets new to this AST file. Other identifier names are looked |
Sebastian Redl | f2f0f03 | 2010-07-23 23:49:55 +0000 | [diff] [blame] | 3104 | // up earlier in the chain and thus don't need an offset. |
| 3105 | if (ID >= FirstIdentID) |
| 3106 | IdentifierOffsets[ID - FirstIdentID] = Offset; |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 3107 | } |
| 3108 | |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3109 | /// \brief Note that the selector Sel occurs at the given offset |
| 3110 | /// within the method pool/selector table. |
Sebastian Redl | a4232eb | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 3111 | void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) { |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3112 | unsigned ID = SelectorIDs[Sel]; |
| 3113 | assert(ID && "Unknown selector"); |
Sebastian Redl | e58aa89 | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 3114 | // Don't record offsets for selectors that are also available in a different |
| 3115 | // file. |
| 3116 | if (ID < FirstSelectorID) |
| 3117 | return; |
| 3118 | SelectorOffsets[ID - FirstSelectorID] = Offset; |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3119 | } |
| 3120 | |
Sebastian Redl | a4232eb | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 3121 | ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream) |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 3122 | : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0), |
Argyrios Kyrtzidis | bef35c9 | 2012-03-07 01:51:17 +0000 | [diff] [blame] | 3123 | WritingAST(false), ASTHasCompilerErrors(false), |
Douglas Gregor | 0a14e4b | 2011-08-03 16:05:40 +0000 | [diff] [blame] | 3124 | FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID), |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3125 | FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID), |
Douglas Gregor | 6ec60e0 | 2011-08-03 21:49:18 +0000 | [diff] [blame] | 3126 | FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID), |
Douglas Gregor | 26ced12 | 2011-12-01 00:59:36 +0000 | [diff] [blame] | 3127 | FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS), |
| 3128 | NextSubmoduleID(FirstSubmoduleID), |
Douglas Gregor | b18b1fd | 2011-08-03 23:28:44 +0000 | [diff] [blame] | 3129 | FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID), |
Douglas Gregor | 77424bc | 2010-10-02 19:29:26 +0000 | [diff] [blame] | 3130 | CollectedStmts(&StmtsToEmit), |
Sebastian Redl | e58aa89 | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 3131 | NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0), |
Douglas Gregor | a72d8c4 | 2011-06-03 02:27:19 +0000 | [diff] [blame] | 3132 | NumVisibleDeclContexts(0), |
Douglas Gregor | e92b8a1 | 2011-08-04 00:01:48 +0000 | [diff] [blame] | 3133 | NextCXXBaseSpecifiersID(1), |
Jonathan D. Turner | 953c564 | 2011-06-03 23:11:16 +0000 | [diff] [blame] | 3134 | DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0), |
Douglas Gregor | a72d8c4 | 2011-06-03 02:27:19 +0000 | [diff] [blame] | 3135 | DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0), |
| 3136 | DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0), |
| 3137 | DeclRecordAbbrev(0), IntegerLiteralAbbrev(0), |
Jonathan D. Turner | 953c564 | 2011-06-03 23:11:16 +0000 | [diff] [blame] | 3138 | DeclTypedefAbbrev(0), |
| 3139 | DeclVarAbbrev(0), DeclFieldAbbrev(0), |
| 3140 | DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0) |
Douglas Gregor | 7c789c1 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 3141 | { |
Sebastian Redl | 30c514c | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 3142 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3143 | |
Argyrios Kyrtzidis | 10f3df5 | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 3144 | ASTWriter::~ASTWriter() { |
| 3145 | for (FileDeclIDsTy::iterator |
| 3146 | I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I) |
| 3147 | delete I->second; |
| 3148 | } |
| 3149 | |
Sebastian Redl | a4232eb | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 3150 | void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls, |
Argyrios Kyrtzidis | 8e3df4d | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 3151 | const std::string &OutputFile, |
Argyrios Kyrtzidis | bef35c9 | 2012-03-07 01:51:17 +0000 | [diff] [blame] | 3152 | Module *WritingModule, StringRef isysroot, |
| 3153 | bool hasErrors) { |
Douglas Gregor | 61c5e34 | 2011-09-17 00:05:03 +0000 | [diff] [blame] | 3154 | WritingAST = true; |
| 3155 | |
Argyrios Kyrtzidis | bef35c9 | 2012-03-07 01:51:17 +0000 | [diff] [blame] | 3156 | ASTHasCompilerErrors = hasErrors; |
| 3157 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3158 | // Emit the file header. |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 3159 | Stream.Emit((unsigned)'C', 8); |
| 3160 | Stream.Emit((unsigned)'P', 8); |
| 3161 | Stream.Emit((unsigned)'C', 8); |
| 3162 | Stream.Emit((unsigned)'H', 8); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3163 | |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 3164 | WriteBlockInfoBlock(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3165 | |
Douglas Gregor | 3b8043b | 2011-08-09 15:13:55 +0000 | [diff] [blame] | 3166 | Context = &SemaRef.Context; |
Douglas Gregor | 185dbd7 | 2011-12-01 02:07:58 +0000 | [diff] [blame] | 3167 | PP = &SemaRef.PP; |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 3168 | this->WritingModule = WritingModule; |
Douglas Gregor | a8cc6ce | 2011-11-30 04:39:39 +0000 | [diff] [blame] | 3169 | WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule); |
Douglas Gregor | 3b8043b | 2011-08-09 15:13:55 +0000 | [diff] [blame] | 3170 | Context = 0; |
Douglas Gregor | 185dbd7 | 2011-12-01 02:07:58 +0000 | [diff] [blame] | 3171 | PP = 0; |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 3172 | this->WritingModule = 0; |
Douglas Gregor | 61c5e34 | 2011-09-17 00:05:03 +0000 | [diff] [blame] | 3173 | |
| 3174 | WritingAST = false; |
Sebastian Redl | 1dc13a1 | 2010-07-12 22:02:52 +0000 | [diff] [blame] | 3175 | } |
| 3176 | |
Douglas Gregor | a2ee20a | 2011-07-27 21:45:57 +0000 | [diff] [blame] | 3177 | template<typename Vector> |
| 3178 | static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec, |
| 3179 | ASTWriter::RecordData &Record) { |
| 3180 | for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end(); |
| 3181 | I != E; ++I) { |
| 3182 | Writer.AddDeclRef(*I, Record); |
| 3183 | } |
| 3184 | } |
| 3185 | |
Sebastian Redl | a4232eb | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 3186 | void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls, |
Douglas Gregor | 832d620 | 2011-07-22 16:35:34 +0000 | [diff] [blame] | 3187 | StringRef isysroot, |
Douglas Gregor | a8cc6ce | 2011-11-30 04:39:39 +0000 | [diff] [blame] | 3188 | const std::string &OutputFile, |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 3189 | Module *WritingModule) { |
Sebastian Redl | 1dc13a1 | 2010-07-12 22:02:52 +0000 | [diff] [blame] | 3190 | using namespace llvm; |
| 3191 | |
Douglas Gregor | ecc2c09 | 2011-12-01 22:20:10 +0000 | [diff] [blame] | 3192 | // Make sure that the AST reader knows to finalize itself. |
| 3193 | if (Chain) |
| 3194 | Chain->finalizeForWriting(); |
| 3195 | |
Sebastian Redl | 1dc13a1 | 2010-07-12 22:02:52 +0000 | [diff] [blame] | 3196 | ASTContext &Context = SemaRef.Context; |
| 3197 | Preprocessor &PP = SemaRef.PP; |
| 3198 | |
Douglas Gregor | 6bf2b9f | 2011-08-12 00:15:20 +0000 | [diff] [blame] | 3199 | // Set up predefined declaration IDs. |
| 3200 | DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID; |
Douglas Gregor | 4dfd02a | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 3201 | if (Context.ObjCIdDecl) |
| 3202 | DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID; |
Douglas Gregor | 7a27ea5 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 3203 | if (Context.ObjCSelDecl) |
| 3204 | DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID; |
Douglas Gregor | 79d6726 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 3205 | if (Context.ObjCClassDecl) |
| 3206 | DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID; |
Douglas Gregor | a6ea10e | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 3207 | if (Context.ObjCProtocolClassDecl) |
| 3208 | DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID; |
Douglas Gregor | 772eeae | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 3209 | if (Context.Int128Decl) |
| 3210 | DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID; |
| 3211 | if (Context.UInt128Decl) |
| 3212 | DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID; |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 3213 | if (Context.ObjCInstanceTypeDecl) |
| 3214 | DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID; |
Douglas Gregor | 79d6726 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 3215 | |
Douglas Gregor | b7c324f | 2011-08-12 01:39:19 +0000 | [diff] [blame] | 3216 | if (!Chain) { |
| 3217 | // Make sure that we emit IdentifierInfos (and any attached |
| 3218 | // declarations) for builtins. We don't need to do this when we're |
| 3219 | // emitting chained PCH files, because all of the builtins will be |
| 3220 | // in the original PCH file. |
| 3221 | // FIXME: Modules won't like this at all. |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 3222 | IdentifierTable &Table = PP.getIdentifierTable(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3223 | SmallVector<const char *, 32> BuiltinNames; |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 3224 | Context.BuiltinInfo.GetBuiltinNames(BuiltinNames, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 3225 | Context.getLangOpts().NoBuiltin); |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 3226 | for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I) |
| 3227 | getIdentifierRef(&Table.get(BuiltinNames[I])); |
| 3228 | } |
| 3229 | |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 3230 | // If there are any out-of-date identifiers, bring them up to date. |
| 3231 | if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) { |
| 3232 | for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(), |
| 3233 | IDEnd = PP.getIdentifierTable().end(); |
| 3234 | ID != IDEnd; ++ID) |
| 3235 | if (ID->second->isOutOfDate()) |
| 3236 | ExtSource->updateOutOfDateIdentifier(*ID->second); |
| 3237 | } |
| 3238 | |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 3239 | // Build a record containing all of the tentative definitions in this file, in |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 3240 | // TentativeDefinitions order. Generally, this record will be empty for |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 3241 | // headers. |
Douglas Gregor | 4c0e86b | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 3242 | RecordData TentativeDefinitions; |
Douglas Gregor | a2ee20a | 2011-07-27 21:45:57 +0000 | [diff] [blame] | 3243 | AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions); |
Douglas Gregor | a862320 | 2011-07-27 20:58:46 +0000 | [diff] [blame] | 3244 | |
Argyrios Kyrtzidis | 49b96d1 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 3245 | // Build a record containing all of the file scoped decls in this file. |
| 3246 | RecordData UnusedFileScopedDecls; |
Douglas Gregor | a2ee20a | 2011-07-27 21:45:57 +0000 | [diff] [blame] | 3247 | AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls, |
| 3248 | UnusedFileScopedDecls); |
Sebastian Redl | 4056680 | 2010-08-05 18:21:25 +0000 | [diff] [blame] | 3249 | |
Douglas Gregor | b7c324f | 2011-08-12 01:39:19 +0000 | [diff] [blame] | 3250 | // Build a record containing all of the delegating constructors we still need |
| 3251 | // to resolve. |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 3252 | RecordData DelegatingCtorDecls; |
Douglas Gregor | 0129b56 | 2011-07-27 21:57:17 +0000 | [diff] [blame] | 3253 | AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls); |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 3254 | |
Douglas Gregor | b7c324f | 2011-08-12 01:39:19 +0000 | [diff] [blame] | 3255 | // Write the set of weak, undeclared identifiers. We always write the |
| 3256 | // entire table, since later PCH files in a PCH chain are only interested in |
| 3257 | // the results at the end of the chain. |
Argyrios Kyrtzidis | 72b9057 | 2010-08-05 09:48:08 +0000 | [diff] [blame] | 3258 | RecordData WeakUndeclaredIdentifiers; |
| 3259 | if (!SemaRef.WeakUndeclaredIdentifiers.empty()) { |
Douglas Gregor | 31e37b2 | 2011-07-28 18:09:57 +0000 | [diff] [blame] | 3260 | for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator |
Argyrios Kyrtzidis | 72b9057 | 2010-08-05 09:48:08 +0000 | [diff] [blame] | 3261 | I = SemaRef.WeakUndeclaredIdentifiers.begin(), |
| 3262 | E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) { |
| 3263 | AddIdentifierRef(I->first, WeakUndeclaredIdentifiers); |
| 3264 | AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers); |
| 3265 | AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers); |
| 3266 | WeakUndeclaredIdentifiers.push_back(I->second.getUsed()); |
| 3267 | } |
| 3268 | } |
Kovarththanan Rajaratnam | 11a18f1 | 2010-03-14 07:06:50 +0000 | [diff] [blame] | 3269 | |
Douglas Gregor | 14c22f2 | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 3270 | // Build a record containing all of the locally-scoped external |
| 3271 | // declarations in this header file. Generally, this record will be |
| 3272 | // empty. |
| 3273 | RecordData LocallyScopedExternalDecls; |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 3274 | // FIXME: This is filling in the AST file in densemap order which is |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 3275 | // nondeterminstic! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3276 | for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator |
Douglas Gregor | 14c22f2 | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 3277 | TD = SemaRef.LocallyScopedExternalDecls.begin(), |
| 3278 | TDEnd = SemaRef.LocallyScopedExternalDecls.end(); |
Douglas Gregor | ec12ce2 | 2011-07-28 14:20:37 +0000 | [diff] [blame] | 3279 | TD != TDEnd; ++TD) { |
Douglas Gregor | 919814d | 2011-09-09 23:01:35 +0000 | [diff] [blame] | 3280 | if (!TD->second->isFromASTFile()) |
Douglas Gregor | ec12ce2 | 2011-07-28 14:20:37 +0000 | [diff] [blame] | 3281 | AddDeclRef(TD->second, LocallyScopedExternalDecls); |
| 3282 | } |
| 3283 | |
Douglas Gregor | b81c170 | 2009-04-27 20:06:05 +0000 | [diff] [blame] | 3284 | // Build a record containing all of the ext_vector declarations. |
| 3285 | RecordData ExtVectorDecls; |
Douglas Gregor | d58a0a5 | 2011-07-28 00:39:29 +0000 | [diff] [blame] | 3286 | AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls); |
Douglas Gregor | b81c170 | 2009-04-27 20:06:05 +0000 | [diff] [blame] | 3287 | |
Argyrios Kyrtzidis | d455add | 2010-07-06 15:37:04 +0000 | [diff] [blame] | 3288 | // Build a record containing all of the VTable uses information. |
| 3289 | RecordData VTableUses; |
Argyrios Kyrtzidis | be4ebcd | 2010-08-03 17:29:52 +0000 | [diff] [blame] | 3290 | if (!SemaRef.VTableUses.empty()) { |
Argyrios Kyrtzidis | be4ebcd | 2010-08-03 17:29:52 +0000 | [diff] [blame] | 3291 | for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) { |
| 3292 | AddDeclRef(SemaRef.VTableUses[I].first, VTableUses); |
| 3293 | AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses); |
| 3294 | VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]); |
| 3295 | } |
Argyrios Kyrtzidis | d455add | 2010-07-06 15:37:04 +0000 | [diff] [blame] | 3296 | } |
| 3297 | |
| 3298 | // Build a record containing all of dynamic classes declarations. |
| 3299 | RecordData DynamicClasses; |
Douglas Gregor | a126f17 | 2011-07-28 00:53:40 +0000 | [diff] [blame] | 3300 | AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses); |
Argyrios Kyrtzidis | d455add | 2010-07-06 15:37:04 +0000 | [diff] [blame] | 3301 | |
Argyrios Kyrtzidis | 0e03638 | 2010-08-05 09:48:16 +0000 | [diff] [blame] | 3302 | // Build a record containing all of pending implicit instantiations. |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3303 | RecordData PendingInstantiations; |
Argyrios Kyrtzidis | 0e03638 | 2010-08-05 09:48:16 +0000 | [diff] [blame] | 3304 | for (std::deque<Sema::PendingImplicitInstantiation>::iterator |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3305 | I = SemaRef.PendingInstantiations.begin(), |
| 3306 | N = SemaRef.PendingInstantiations.end(); I != N; ++I) { |
| 3307 | AddDeclRef(I->first, PendingInstantiations); |
| 3308 | AddSourceLocation(I->second, PendingInstantiations); |
Argyrios Kyrtzidis | 0e03638 | 2010-08-05 09:48:16 +0000 | [diff] [blame] | 3309 | } |
| 3310 | assert(SemaRef.PendingLocalImplicitInstantiations.empty() && |
| 3311 | "There are local ones at end of translation unit!"); |
| 3312 | |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3313 | // Build a record containing some declaration references. |
| 3314 | RecordData SemaDeclRefs; |
| 3315 | if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) { |
| 3316 | AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs); |
| 3317 | AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs); |
| 3318 | } |
| 3319 | |
Peter Collingbourne | 14b6ba7 | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 3320 | RecordData CUDASpecialDeclRefs; |
| 3321 | if (Context.getcudaConfigureCallDecl()) { |
| 3322 | AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs); |
| 3323 | } |
| 3324 | |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 3325 | // Build a record containing all of the known namespaces. |
| 3326 | RecordData KnownNamespaces; |
| 3327 | for (llvm::DenseMap<NamespaceDecl*, bool>::iterator |
| 3328 | I = SemaRef.KnownNamespaces.begin(), |
| 3329 | IEnd = SemaRef.KnownNamespaces.end(); |
| 3330 | I != IEnd; ++I) { |
| 3331 | if (!I->second) |
| 3332 | AddDeclRef(I->first, KnownNamespaces); |
| 3333 | } |
| 3334 | |
Sebastian Redl | 3397c55 | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 3335 | // Write the remaining AST contents. |
Douglas Gregor | ad1de00 | 2009-04-18 05:55:16 +0000 | [diff] [blame] | 3336 | RecordData Record; |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3337 | Stream.EnterSubblock(AST_BLOCK_ID, 5); |
Argyrios Kyrtzidis | 8e3df4d | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 3338 | WriteMetadata(Context, isysroot, OutputFile); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 3339 | WriteLanguageOptions(Context.getLangOpts()); |
Douglas Gregor | 832d620 | 2011-07-22 16:35:34 +0000 | [diff] [blame] | 3340 | if (StatCalls && isysroot.empty()) |
Douglas Gregor | dd41ed5 | 2010-07-12 23:48:14 +0000 | [diff] [blame] | 3341 | WriteStatCache(*StatCalls); |
Douglas Gregor | b7c324f | 2011-08-12 01:39:19 +0000 | [diff] [blame] | 3342 | |
| 3343 | // Create a lexical update block containing all of the declarations in the |
| 3344 | // translation unit that do not come from other AST files. |
| 3345 | const TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); |
| 3346 | SmallVector<KindDeclIDPair, 64> NewGlobalDecls; |
| 3347 | for (DeclContext::decl_iterator I = TU->noload_decls_begin(), |
| 3348 | E = TU->noload_decls_end(); |
| 3349 | I != E; ++I) { |
Douglas Gregor | 919814d | 2011-09-09 23:01:35 +0000 | [diff] [blame] | 3350 | if (!(*I)->isFromASTFile()) |
Douglas Gregor | b7c324f | 2011-08-12 01:39:19 +0000 | [diff] [blame] | 3351 | NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I))); |
Douglas Gregor | b7c324f | 2011-08-12 01:39:19 +0000 | [diff] [blame] | 3352 | } |
| 3353 | |
| 3354 | llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev(); |
| 3355 | Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL)); |
| 3356 | Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob)); |
| 3357 | unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv); |
| 3358 | Record.clear(); |
| 3359 | Record.push_back(TU_UPDATE_LEXICAL); |
| 3360 | Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record, |
| 3361 | data(NewGlobalDecls)); |
| 3362 | |
| 3363 | // And a visible updates block for the translation unit. |
| 3364 | Abv = new llvm::BitCodeAbbrev(); |
| 3365 | Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE)); |
| 3366 | Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6)); |
| 3367 | Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32)); |
| 3368 | Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob)); |
| 3369 | UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv); |
| 3370 | WriteDeclContextVisibleUpdate(TU); |
| 3371 | |
| 3372 | // If the translation unit has an anonymous namespace, and we don't already |
| 3373 | // have an update block for it, write it as an update block. |
| 3374 | if (NamespaceDecl *NS = TU->getAnonymousNamespace()) { |
| 3375 | ASTWriter::UpdateRecord &Record = DeclUpdates[TU]; |
| 3376 | if (Record.empty()) { |
| 3377 | Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE); |
Douglas Gregor | 61c5e34 | 2011-09-17 00:05:03 +0000 | [diff] [blame] | 3378 | Record.push_back(reinterpret_cast<uint64_t>(NS)); |
Douglas Gregor | b7c324f | 2011-08-12 01:39:19 +0000 | [diff] [blame] | 3379 | } |
| 3380 | } |
| 3381 | |
Argyrios Kyrtzidis | 67bc4ba | 2011-11-14 04:52:24 +0000 | [diff] [blame] | 3382 | // Resolve any declaration pointers within the declaration updates block. |
Douglas Gregor | 61c5e34 | 2011-09-17 00:05:03 +0000 | [diff] [blame] | 3383 | ResolveDeclUpdatesBlocks(); |
Douglas Gregor | 61c5e34 | 2011-09-17 00:05:03 +0000 | [diff] [blame] | 3384 | |
Douglas Gregor | a119da0 | 2011-08-02 16:26:37 +0000 | [diff] [blame] | 3385 | // Form the record of special types. |
| 3386 | RecordData SpecialTypes; |
| 3387 | AddTypeRef(Context.getBuiltinVaListType(), SpecialTypes); |
Douglas Gregor | a119da0 | 2011-08-02 16:26:37 +0000 | [diff] [blame] | 3388 | AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes); |
Douglas Gregor | a119da0 | 2011-08-02 16:26:37 +0000 | [diff] [blame] | 3389 | AddTypeRef(Context.getFILEType(), SpecialTypes); |
| 3390 | AddTypeRef(Context.getjmp_bufType(), SpecialTypes); |
| 3391 | AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes); |
| 3392 | AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes); |
| 3393 | AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes); |
Douglas Gregor | a119da0 | 2011-08-02 16:26:37 +0000 | [diff] [blame] | 3394 | AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes); |
Rafael Espindola | e2d4f4e | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 3395 | AddTypeRef(Context.getucontext_tType(), SpecialTypes); |
Douglas Gregor | 185dbd7 | 2011-12-01 02:07:58 +0000 | [diff] [blame] | 3396 | |
Douglas Gregor | 366809a | 2009-04-26 03:49:13 +0000 | [diff] [blame] | 3397 | // Keep writing types and declarations until all types and |
| 3398 | // declarations have been written. |
Douglas Gregor | a72d8c4 | 2011-06-03 02:27:19 +0000 | [diff] [blame] | 3399 | Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE); |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 3400 | WriteDeclsBlockAbbrevs(); |
Douglas Gregor | b7c324f | 2011-08-12 01:39:19 +0000 | [diff] [blame] | 3401 | for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(), |
| 3402 | E = DeclsToRewrite.end(); |
| 3403 | I != E; ++I) |
| 3404 | DeclTypesToEmit.push(const_cast<Decl*>(*I)); |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 3405 | while (!DeclTypesToEmit.empty()) { |
| 3406 | DeclOrType DOT = DeclTypesToEmit.front(); |
| 3407 | DeclTypesToEmit.pop(); |
| 3408 | if (DOT.isType()) |
| 3409 | WriteType(DOT.getType()); |
| 3410 | else |
| 3411 | WriteDecl(Context, DOT.getDecl()); |
| 3412 | } |
| 3413 | Stream.ExitBlock(); |
Kovarththanan Rajaratnam | 11a18f1 | 2010-03-14 07:06:50 +0000 | [diff] [blame] | 3414 | |
Argyrios Kyrtzidis | 10f3df5 | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 3415 | WriteFileDeclIDsMap(); |
| 3416 | WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot); |
| 3417 | |
| 3418 | if (Chain) { |
| 3419 | // Write the mapping information describing our module dependencies and how |
| 3420 | // each of those modules were mapped into our own offset/ID space, so that |
| 3421 | // the reader can build the appropriate mapping to its own offset/ID space. |
| 3422 | // The map consists solely of a blob with the following format: |
| 3423 | // *(module-name-len:i16 module-name:len*i8 |
| 3424 | // source-location-offset:i32 |
| 3425 | // identifier-id:i32 |
| 3426 | // preprocessed-entity-id:i32 |
| 3427 | // macro-definition-id:i32 |
Douglas Gregor | 26ced12 | 2011-12-01 00:59:36 +0000 | [diff] [blame] | 3428 | // submodule-id:i32 |
Argyrios Kyrtzidis | 10f3df5 | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 3429 | // selector-id:i32 |
| 3430 | // declaration-id:i32 |
| 3431 | // c++-base-specifiers-id:i32 |
| 3432 | // type-id:i32) |
| 3433 | // |
| 3434 | llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 3435 | Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP)); |
| 3436 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 3437 | unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev); |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3438 | SmallString<2048> Buffer; |
Argyrios Kyrtzidis | 10f3df5 | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 3439 | { |
| 3440 | llvm::raw_svector_ostream Out(Buffer); |
| 3441 | for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(), |
Douglas Gregor | af13bfc | 2011-12-02 18:58:38 +0000 | [diff] [blame] | 3442 | MEnd = Chain->ModuleMgr.end(); |
Argyrios Kyrtzidis | 10f3df5 | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 3443 | M != MEnd; ++M) { |
| 3444 | StringRef FileName = (*M)->FileName; |
| 3445 | io::Emit16(Out, FileName.size()); |
| 3446 | Out.write(FileName.data(), FileName.size()); |
| 3447 | io::Emit32(Out, (*M)->SLocEntryBaseOffset); |
| 3448 | io::Emit32(Out, (*M)->BaseIdentifierID); |
| 3449 | io::Emit32(Out, (*M)->BasePreprocessedEntityID); |
Douglas Gregor | 26ced12 | 2011-12-01 00:59:36 +0000 | [diff] [blame] | 3450 | io::Emit32(Out, (*M)->BaseSubmoduleID); |
Argyrios Kyrtzidis | 10f3df5 | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 3451 | io::Emit32(Out, (*M)->BaseSelectorID); |
| 3452 | io::Emit32(Out, (*M)->BaseDeclID); |
| 3453 | io::Emit32(Out, (*M)->BaseTypeIndex); |
| 3454 | } |
| 3455 | } |
| 3456 | Record.clear(); |
| 3457 | Record.push_back(MODULE_OFFSET_MAP); |
| 3458 | Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record, |
| 3459 | Buffer.data(), Buffer.size()); |
| 3460 | } |
Douglas Gregor | a8cc6ce | 2011-11-30 04:39:39 +0000 | [diff] [blame] | 3461 | WritePreprocessor(PP, WritingModule != 0); |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 3462 | WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot); |
Sebastian Redl | 059612d | 2010-08-03 21:58:15 +0000 | [diff] [blame] | 3463 | WriteSelectors(SemaRef); |
Fariborz Jahanian | 3201983 | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 3464 | WriteReferencedSelectorsPool(SemaRef); |
Douglas Gregor | a8cc6ce | 2011-11-30 04:39:39 +0000 | [diff] [blame] | 3465 | WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0); |
Peter Collingbourne | 84bccea | 2011-02-15 19:46:30 +0000 | [diff] [blame] | 3466 | WriteFPPragmaOptions(SemaRef.getFPOptions()); |
| 3467 | WriteOpenCLExtensions(SemaRef); |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 3468 | |
Sebastian Redl | 1476ed4 | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 3469 | WriteTypeDeclOffsets(); |
Argyrios Kyrtzidis | 3efd52c | 2011-01-14 20:54:07 +0000 | [diff] [blame] | 3470 | WritePragmaDiagnosticMappings(Context.getDiagnostics()); |
Douglas Gregor | ad1de00 | 2009-04-18 05:55:16 +0000 | [diff] [blame] | 3471 | |
Anders Carlsson | c850578 | 2011-03-06 18:41:18 +0000 | [diff] [blame] | 3472 | WriteCXXBaseSpecifiersOffsets(); |
Douglas Gregor | 7c789c1 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 3473 | |
Douglas Gregor | e209e50 | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 3474 | // If we're emitting a module, write out the submodule information. |
| 3475 | if (WritingModule) |
| 3476 | WriteSubmodules(WritingModule); |
| 3477 | |
Douglas Gregor | a119da0 | 2011-08-02 16:26:37 +0000 | [diff] [blame] | 3478 | Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes); |
| 3479 | |
Douglas Gregor | 4c0e86b | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 3480 | // Write the record containing external, unnamed definitions. |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 3481 | if (!ExternalDefinitions.empty()) |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3482 | Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions); |
Douglas Gregor | 4c0e86b | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 3483 | |
| 3484 | // Write the record containing tentative definitions. |
| 3485 | if (!TentativeDefinitions.empty()) |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3486 | Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions); |
Douglas Gregor | 14c22f2 | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 3487 | |
Argyrios Kyrtzidis | 49b96d1 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 3488 | // Write the record containing unused file scoped decls. |
| 3489 | if (!UnusedFileScopedDecls.empty()) |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3490 | Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls); |
Kovarththanan Rajaratnam | 11a18f1 | 2010-03-14 07:06:50 +0000 | [diff] [blame] | 3491 | |
Argyrios Kyrtzidis | 72b9057 | 2010-08-05 09:48:08 +0000 | [diff] [blame] | 3492 | // Write the record containing weak undeclared identifiers. |
| 3493 | if (!WeakUndeclaredIdentifiers.empty()) |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3494 | Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS, |
Argyrios Kyrtzidis | 72b9057 | 2010-08-05 09:48:08 +0000 | [diff] [blame] | 3495 | WeakUndeclaredIdentifiers); |
| 3496 | |
Douglas Gregor | 14c22f2 | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 3497 | // Write the record containing locally-scoped external definitions. |
| 3498 | if (!LocallyScopedExternalDecls.empty()) |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3499 | Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS, |
Douglas Gregor | 14c22f2 | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 3500 | LocallyScopedExternalDecls); |
Douglas Gregor | b81c170 | 2009-04-27 20:06:05 +0000 | [diff] [blame] | 3501 | |
| 3502 | // Write the record containing ext_vector type names. |
| 3503 | if (!ExtVectorDecls.empty()) |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3504 | Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3505 | |
Argyrios Kyrtzidis | d455add | 2010-07-06 15:37:04 +0000 | [diff] [blame] | 3506 | // Write the record containing VTable uses information. |
| 3507 | if (!VTableUses.empty()) |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3508 | Stream.EmitRecord(VTABLE_USES, VTableUses); |
Argyrios Kyrtzidis | d455add | 2010-07-06 15:37:04 +0000 | [diff] [blame] | 3509 | |
| 3510 | // Write the record containing dynamic classes declarations. |
| 3511 | if (!DynamicClasses.empty()) |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3512 | Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses); |
Argyrios Kyrtzidis | d455add | 2010-07-06 15:37:04 +0000 | [diff] [blame] | 3513 | |
Argyrios Kyrtzidis | 0e03638 | 2010-08-05 09:48:16 +0000 | [diff] [blame] | 3514 | // Write the record containing pending implicit instantiations. |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3515 | if (!PendingInstantiations.empty()) |
| 3516 | Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations); |
Argyrios Kyrtzidis | 0e03638 | 2010-08-05 09:48:16 +0000 | [diff] [blame] | 3517 | |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3518 | // Write the record containing declaration references of Sema. |
| 3519 | if (!SemaDeclRefs.empty()) |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3520 | Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs); |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3521 | |
Peter Collingbourne | 14b6ba7 | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 3522 | // Write the record containing CUDA-specific declaration references. |
| 3523 | if (!CUDASpecialDeclRefs.empty()) |
| 3524 | Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs); |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 3525 | |
| 3526 | // Write the delegating constructors. |
| 3527 | if (!DelegatingCtorDecls.empty()) |
| 3528 | Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls); |
Peter Collingbourne | 14b6ba7 | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 3529 | |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 3530 | // Write the known namespaces. |
| 3531 | if (!KnownNamespaces.empty()) |
| 3532 | Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces); |
| 3533 | |
Douglas Gregor | b7c324f | 2011-08-12 01:39:19 +0000 | [diff] [blame] | 3534 | // Write the visible updates to DeclContexts. |
| 3535 | for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator |
| 3536 | I = UpdatedDeclContexts.begin(), |
| 3537 | E = UpdatedDeclContexts.end(); |
| 3538 | I != E; ++I) |
| 3539 | WriteDeclContextVisibleUpdate(*I); |
| 3540 | |
Douglas Gregor | c5e0f9b | 2011-12-03 01:15:29 +0000 | [diff] [blame] | 3541 | if (!WritingModule) { |
| 3542 | // Write the submodules that were imported, if any. |
| 3543 | RecordData ImportedModules; |
| 3544 | for (ASTContext::import_iterator I = Context.local_import_begin(), |
| 3545 | IEnd = Context.local_import_end(); |
| 3546 | I != IEnd; ++I) { |
| 3547 | assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end()); |
| 3548 | ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]); |
| 3549 | } |
| 3550 | if (!ImportedModules.empty()) { |
| 3551 | // Sort module IDs. |
| 3552 | llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end()); |
| 3553 | |
| 3554 | // Unique module IDs. |
| 3555 | ImportedModules.erase(std::unique(ImportedModules.begin(), |
| 3556 | ImportedModules.end()), |
| 3557 | ImportedModules.end()); |
| 3558 | |
| 3559 | Stream.EmitRecord(IMPORTED_MODULES, ImportedModules); |
| 3560 | } |
Douglas Gregor | f6137e4 | 2011-12-03 00:59:55 +0000 | [diff] [blame] | 3561 | } |
| 3562 | |
Douglas Gregor | 6bf2b9f | 2011-08-12 00:15:20 +0000 | [diff] [blame] | 3563 | WriteDeclUpdatesBlocks(); |
Douglas Gregor | b7c324f | 2011-08-12 01:39:19 +0000 | [diff] [blame] | 3564 | WriteDeclReplacementsBlock(); |
Douglas Gregor | c3cfd2a | 2011-12-22 21:40:42 +0000 | [diff] [blame] | 3565 | WriteMergedDecls(); |
Douglas Gregor | 2171bf1 | 2012-01-15 16:58:34 +0000 | [diff] [blame] | 3566 | WriteRedeclarations(); |
Douglas Gregor | cff9f26 | 2012-01-27 01:47:08 +0000 | [diff] [blame] | 3567 | WriteObjCCategories(); |
Douglas Gregor | a1be278 | 2011-12-17 23:38:30 +0000 | [diff] [blame] | 3568 | |
Douglas Gregor | 3e1af84 | 2009-04-17 22:13:46 +0000 | [diff] [blame] | 3569 | // Some simple statistics |
Douglas Gregor | ad1de00 | 2009-04-18 05:55:16 +0000 | [diff] [blame] | 3570 | Record.clear(); |
Douglas Gregor | 3e1af84 | 2009-04-17 22:13:46 +0000 | [diff] [blame] | 3571 | Record.push_back(NumStatements); |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 3572 | Record.push_back(NumMacros); |
Douglas Gregor | 2512308 | 2009-04-22 22:34:57 +0000 | [diff] [blame] | 3573 | Record.push_back(NumLexicalDeclContexts); |
| 3574 | Record.push_back(NumVisibleDeclContexts); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3575 | Stream.EmitRecord(STATISTICS, Record); |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 3576 | Stream.ExitBlock(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3577 | } |
| 3578 | |
Douglas Gregor | 61c5e34 | 2011-09-17 00:05:03 +0000 | [diff] [blame] | 3579 | /// \brief Go through the declaration update blocks and resolve declaration |
| 3580 | /// pointers into declaration IDs. |
| 3581 | void ASTWriter::ResolveDeclUpdatesBlocks() { |
| 3582 | for (DeclUpdateMap::iterator |
| 3583 | I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) { |
| 3584 | const Decl *D = I->first; |
| 3585 | UpdateRecord &URec = I->second; |
| 3586 | |
Argyrios Kyrtzidis | ad834d5 | 2011-11-12 21:07:46 +0000 | [diff] [blame] | 3587 | if (isRewritten(D)) |
Douglas Gregor | 61c5e34 | 2011-09-17 00:05:03 +0000 | [diff] [blame] | 3588 | continue; // The decl will be written completely |
| 3589 | |
| 3590 | unsigned Idx = 0, N = URec.size(); |
| 3591 | while (Idx < N) { |
| 3592 | switch ((DeclUpdateKind)URec[Idx++]) { |
Douglas Gregor | 61c5e34 | 2011-09-17 00:05:03 +0000 | [diff] [blame] | 3593 | case UPD_CXX_ADDED_IMPLICIT_MEMBER: |
| 3594 | case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION: |
| 3595 | case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: |
| 3596 | URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx])); |
| 3597 | ++Idx; |
| 3598 | break; |
| 3599 | |
| 3600 | case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER: |
| 3601 | ++Idx; |
| 3602 | break; |
| 3603 | } |
| 3604 | } |
| 3605 | } |
| 3606 | } |
| 3607 | |
Argyrios Kyrtzidis | aacdd02 | 2010-10-24 17:26:43 +0000 | [diff] [blame] | 3608 | void ASTWriter::WriteDeclUpdatesBlocks() { |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 3609 | if (DeclUpdates.empty()) |
| 3610 | return; |
| 3611 | |
| 3612 | RecordData OffsetsRecord; |
Douglas Gregor | a72d8c4 | 2011-06-03 02:27:19 +0000 | [diff] [blame] | 3613 | Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE); |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 3614 | for (DeclUpdateMap::iterator |
| 3615 | I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) { |
| 3616 | const Decl *D = I->first; |
| 3617 | UpdateRecord &URec = I->second; |
| 3618 | |
Argyrios Kyrtzidis | ad834d5 | 2011-11-12 21:07:46 +0000 | [diff] [blame] | 3619 | if (isRewritten(D)) |
Argyrios Kyrtzidis | ba901b5 | 2010-10-24 17:26:46 +0000 | [diff] [blame] | 3620 | continue; // The decl will be written completely,no need to store updates. |
| 3621 | |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 3622 | uint64_t Offset = Stream.GetCurrentBitNo(); |
| 3623 | Stream.EmitRecord(DECL_UPDATES, URec); |
| 3624 | |
| 3625 | OffsetsRecord.push_back(GetDeclRef(D)); |
| 3626 | OffsetsRecord.push_back(Offset); |
| 3627 | } |
| 3628 | Stream.ExitBlock(); |
| 3629 | Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord); |
| 3630 | } |
| 3631 | |
Argyrios Kyrtzidis | aacdd02 | 2010-10-24 17:26:43 +0000 | [diff] [blame] | 3632 | void ASTWriter::WriteDeclReplacementsBlock() { |
Sebastian Redl | 0b17c61 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 3633 | if (ReplacedDecls.empty()) |
| 3634 | return; |
| 3635 | |
| 3636 | RecordData Record; |
Argyrios Kyrtzidis | ef23b60 | 2011-10-31 07:20:15 +0000 | [diff] [blame] | 3637 | for (SmallVector<ReplacedDeclInfo, 16>::iterator |
Sebastian Redl | 0b17c61 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 3638 | I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) { |
Argyrios Kyrtzidis | ef23b60 | 2011-10-31 07:20:15 +0000 | [diff] [blame] | 3639 | Record.push_back(I->ID); |
| 3640 | Record.push_back(I->Offset); |
| 3641 | Record.push_back(I->Loc); |
Sebastian Redl | 0b17c61 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 3642 | } |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3643 | Stream.EmitRecord(DECL_REPLACEMENTS, Record); |
Sebastian Redl | 0b17c61 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 3644 | } |
| 3645 | |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3646 | void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3647 | Record.push_back(Loc.getRawEncoding()); |
| 3648 | } |
| 3649 | |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3650 | void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) { |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 3651 | AddSourceLocation(Range.getBegin(), Record); |
| 3652 | AddSourceLocation(Range.getEnd(), Record); |
| 3653 | } |
| 3654 | |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3655 | void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3656 | Record.push_back(Value.getBitWidth()); |
Benjamin Kramer | 4ea884b | 2010-09-06 23:43:28 +0000 | [diff] [blame] | 3657 | const uint64_t *Words = Value.getRawData(); |
| 3658 | Record.append(Words, Words + Value.getNumWords()); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3659 | } |
| 3660 | |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3661 | void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) { |
Douglas Gregor | 0a2b45e | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 3662 | Record.push_back(Value.isUnsigned()); |
| 3663 | AddAPInt(Value, Record); |
| 3664 | } |
| 3665 | |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3666 | void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) { |
Douglas Gregor | 17fc223 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 3667 | AddAPInt(Value.bitcastToAPInt(), Record); |
| 3668 | } |
| 3669 | |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3670 | void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) { |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 3671 | Record.push_back(getIdentifierRef(II)); |
| 3672 | } |
| 3673 | |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3674 | IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) { |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 3675 | if (II == 0) |
| 3676 | return 0; |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 3677 | |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3678 | IdentID &ID = IdentifierIDs[II]; |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 3679 | if (ID == 0) |
Sebastian Redl | f2f0f03 | 2010-07-23 23:49:55 +0000 | [diff] [blame] | 3680 | ID = NextIdentID++; |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 3681 | return ID; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3682 | } |
| 3683 | |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3684 | void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) { |
Sebastian Redl | 5d05007 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 3685 | Record.push_back(getSelectorRef(SelRef)); |
| 3686 | } |
| 3687 | |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3688 | SelectorID ASTWriter::getSelectorRef(Selector Sel) { |
Sebastian Redl | 5d05007 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 3689 | if (Sel.getAsOpaquePtr() == 0) { |
| 3690 | return 0; |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 3691 | } |
| 3692 | |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3693 | SelectorID &SID = SelectorIDs[Sel]; |
Sebastian Redl | e58aa89 | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 3694 | if (SID == 0 && Chain) { |
| 3695 | // This might trigger a ReadSelector callback, which will set the ID for |
| 3696 | // this selector. |
| 3697 | Chain->LoadSelector(Sel); |
| 3698 | } |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 3699 | if (SID == 0) { |
Sebastian Redl | e58aa89 | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 3700 | SID = NextSelectorID++; |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 3701 | } |
Sebastian Redl | 5d05007 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 3702 | return SID; |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 3703 | } |
| 3704 | |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3705 | void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) { |
Chris Lattner | d259836 | 2010-05-10 00:25:06 +0000 | [diff] [blame] | 3706 | AddDeclRef(Temp->getDestructor(), Record); |
| 3707 | } |
| 3708 | |
Douglas Gregor | 7c789c1 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 3709 | void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases, |
| 3710 | CXXBaseSpecifier const *BasesEnd, |
| 3711 | RecordDataImpl &Record) { |
| 3712 | assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded"); |
| 3713 | CXXBaseSpecifiersToWrite.push_back( |
| 3714 | QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID, |
| 3715 | Bases, BasesEnd)); |
| 3716 | Record.push_back(NextCXXBaseSpecifiersID++); |
| 3717 | } |
| 3718 | |
Sebastian Redl | a4232eb | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 3719 | void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind, |
Argyrios Kyrtzidis | 44f8c37 | 2010-06-22 09:54:59 +0000 | [diff] [blame] | 3720 | const TemplateArgumentLocInfo &Arg, |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3721 | RecordDataImpl &Record) { |
Argyrios Kyrtzidis | 44f8c37 | 2010-06-22 09:54:59 +0000 | [diff] [blame] | 3722 | switch (Kind) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3723 | case TemplateArgument::Expression: |
Argyrios Kyrtzidis | 44f8c37 | 2010-06-22 09:54:59 +0000 | [diff] [blame] | 3724 | AddStmt(Arg.getAsExpr()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3725 | break; |
| 3726 | case TemplateArgument::Type: |
Argyrios Kyrtzidis | 44f8c37 | 2010-06-22 09:54:59 +0000 | [diff] [blame] | 3727 | AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3728 | break; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3729 | case TemplateArgument::Template: |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3730 | AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record); |
Argyrios Kyrtzidis | 17cfded | 2010-06-28 09:31:42 +0000 | [diff] [blame] | 3731 | AddSourceLocation(Arg.getTemplateNameLoc(), Record); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3732 | break; |
| 3733 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3734 | AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3735 | AddSourceLocation(Arg.getTemplateNameLoc(), Record); |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 3736 | AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3737 | break; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3738 | case TemplateArgument::Null: |
| 3739 | case TemplateArgument::Integral: |
| 3740 | case TemplateArgument::Declaration: |
| 3741 | case TemplateArgument::Pack: |
| 3742 | break; |
| 3743 | } |
| 3744 | } |
| 3745 | |
Sebastian Redl | a4232eb | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 3746 | void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg, |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3747 | RecordDataImpl &Record) { |
Argyrios Kyrtzidis | 44f8c37 | 2010-06-22 09:54:59 +0000 | [diff] [blame] | 3748 | AddTemplateArgument(Arg.getArgument(), Record); |
Argyrios Kyrtzidis | 17cfded | 2010-06-28 09:31:42 +0000 | [diff] [blame] | 3749 | |
| 3750 | if (Arg.getArgument().getKind() == TemplateArgument::Expression) { |
| 3751 | bool InfoHasSameExpr |
| 3752 | = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr(); |
| 3753 | Record.push_back(InfoHasSameExpr); |
| 3754 | if (InfoHasSameExpr) |
| 3755 | return; // Avoid storing the same expr twice. |
| 3756 | } |
Argyrios Kyrtzidis | 44f8c37 | 2010-06-22 09:54:59 +0000 | [diff] [blame] | 3757 | AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(), |
| 3758 | Record); |
| 3759 | } |
| 3760 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 3761 | void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, |
| 3762 | RecordDataImpl &Record) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3763 | if (TInfo == 0) { |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 3764 | AddTypeRef(QualType(), Record); |
| 3765 | return; |
| 3766 | } |
| 3767 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 3768 | AddTypeLoc(TInfo->getTypeLoc(), Record); |
| 3769 | } |
| 3770 | |
| 3771 | void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) { |
| 3772 | AddTypeRef(TL.getType(), Record); |
| 3773 | |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 3774 | TypeLocWriter TLW(*this, Record); |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 3775 | for (; !TL.isNull(); TL = TL.getNextTypeLoc()) |
Kovarththanan Rajaratnam | 11a18f1 | 2010-03-14 07:06:50 +0000 | [diff] [blame] | 3776 | TLW.Visit(TL); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 3777 | } |
| 3778 | |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3779 | void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) { |
Argyrios Kyrtzidis | 7fb3518 | 2010-08-20 16:04:14 +0000 | [diff] [blame] | 3780 | Record.push_back(GetOrCreateTypeID(T)); |
| 3781 | } |
| 3782 | |
Douglas Gregor | 3b8043b | 2011-08-09 15:13:55 +0000 | [diff] [blame] | 3783 | TypeID ASTWriter::GetOrCreateTypeID( QualType T) { |
| 3784 | return MakeTypeID(*Context, T, |
Argyrios Kyrtzidis | eb3f04e | 2010-08-20 16:04:20 +0000 | [diff] [blame] | 3785 | std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this)); |
| 3786 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3787 | |
Argyrios Kyrtzidis | 5d26768 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3788 | TypeID ASTWriter::getTypeID(QualType T) const { |
Douglas Gregor | 3b8043b | 2011-08-09 15:13:55 +0000 | [diff] [blame] | 3789 | return MakeTypeID(*Context, T, |
Argyrios Kyrtzidis | eb3f04e | 2010-08-20 16:04:20 +0000 | [diff] [blame] | 3790 | std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this)); |
Argyrios Kyrtzidis | 26fca90 | 2010-08-20 16:04:09 +0000 | [diff] [blame] | 3791 | } |
| 3792 | |
| 3793 | TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) { |
| 3794 | if (T.isNull()) |
| 3795 | return TypeIdx(); |
| 3796 | assert(!T.getLocalFastQualifiers()); |
| 3797 | |
Argyrios Kyrtzidis | 01b81c4 | 2010-08-20 16:04:04 +0000 | [diff] [blame] | 3798 | TypeIdx &Idx = TypeIdxs[T]; |
Argyrios Kyrtzidis | c8e5d51 | 2010-08-20 16:03:59 +0000 | [diff] [blame] | 3799 | if (Idx.getIndex() == 0) { |
Douglas Gregor | 366809a | 2009-04-26 03:49:13 +0000 | [diff] [blame] | 3800 | // We haven't seen this type before. Assign it a new ID and put it |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3801 | // into the queue of types to emit. |
Argyrios Kyrtzidis | c8e5d51 | 2010-08-20 16:03:59 +0000 | [diff] [blame] | 3802 | Idx = TypeIdx(NextTypeID++); |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 3803 | DeclTypesToEmit.push(T); |
Douglas Gregor | 366809a | 2009-04-26 03:49:13 +0000 | [diff] [blame] | 3804 | } |
Argyrios Kyrtzidis | 26fca90 | 2010-08-20 16:04:09 +0000 | [diff] [blame] | 3805 | return Idx; |
| 3806 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3807 | |
Argyrios Kyrtzidis | 5d26768 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3808 | TypeIdx ASTWriter::getTypeIdx(QualType T) const { |
Argyrios Kyrtzidis | 26fca90 | 2010-08-20 16:04:09 +0000 | [diff] [blame] | 3809 | if (T.isNull()) |
| 3810 | return TypeIdx(); |
| 3811 | assert(!T.getLocalFastQualifiers()); |
| 3812 | |
Argyrios Kyrtzidis | 5d26768 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3813 | TypeIdxMap::const_iterator I = TypeIdxs.find(T); |
| 3814 | assert(I != TypeIdxs.end() && "Type not emitted!"); |
| 3815 | return I->second; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3816 | } |
| 3817 | |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 3818 | void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) { |
Sebastian Redl | 681d723 | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 3819 | Record.push_back(GetDeclRef(D)); |
| 3820 | } |
| 3821 | |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3822 | DeclID ASTWriter::GetDeclRef(const Decl *D) { |
Douglas Gregor | 61c5e34 | 2011-09-17 00:05:03 +0000 | [diff] [blame] | 3823 | assert(WritingAST && "Cannot request a declaration ID before AST writing"); |
| 3824 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3825 | if (D == 0) { |
Sebastian Redl | 681d723 | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 3826 | return 0; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3827 | } |
Douglas Gregor | 1c7946a | 2012-01-05 22:33:30 +0000 | [diff] [blame] | 3828 | |
| 3829 | // If D comes from an AST file, its declaration ID is already known and |
| 3830 | // fixed. |
| 3831 | if (D->isFromASTFile()) |
| 3832 | return D->getGlobalID(); |
| 3833 | |
Douglas Gregor | 9747583 | 2010-10-05 18:37:06 +0000 | [diff] [blame] | 3834 | assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer"); |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3835 | DeclID &ID = DeclIDs[D]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3836 | if (ID == 0) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3837 | // We haven't seen this declaration before. Give it a new ID and |
| 3838 | // enqueue it in the list of declarations to emit. |
Sebastian Redl | f2f0f03 | 2010-07-23 23:49:55 +0000 | [diff] [blame] | 3839 | ID = NextDeclID++; |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 3840 | DeclTypesToEmit.push(const_cast<Decl *>(D)); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3841 | } |
| 3842 | |
Sebastian Redl | 681d723 | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 3843 | return ID; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3844 | } |
| 3845 | |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3846 | DeclID ASTWriter::getDeclID(const Decl *D) { |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 3847 | if (D == 0) |
| 3848 | return 0; |
| 3849 | |
Douglas Gregor | 1c7946a | 2012-01-05 22:33:30 +0000 | [diff] [blame] | 3850 | // If D comes from an AST file, its declaration ID is already known and |
| 3851 | // fixed. |
| 3852 | if (D->isFromASTFile()) |
| 3853 | return D->getGlobalID(); |
| 3854 | |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 3855 | assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!"); |
| 3856 | return DeclIDs[D]; |
| 3857 | } |
| 3858 | |
Argyrios Kyrtzidis | 10f3df5 | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 3859 | static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L, |
| 3860 | std::pair<unsigned, serialization::DeclID> R) { |
| 3861 | return L.first < R.first; |
| 3862 | } |
| 3863 | |
Argyrios Kyrtzidis | 19645d2 | 2011-10-28 23:57:43 +0000 | [diff] [blame] | 3864 | void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) { |
Argyrios Kyrtzidis | 10f3df5 | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 3865 | assert(ID); |
Argyrios Kyrtzidis | 19645d2 | 2011-10-28 23:57:43 +0000 | [diff] [blame] | 3866 | assert(D); |
| 3867 | |
| 3868 | SourceLocation Loc = D->getLocation(); |
| 3869 | if (Loc.isInvalid()) |
| 3870 | return; |
Argyrios Kyrtzidis | 10f3df5 | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 3871 | |
| 3872 | // We only keep track of the file-level declarations of each file. |
| 3873 | if (!D->getLexicalDeclContext()->isFileContext()) |
| 3874 | return; |
Argyrios Kyrtzidis | 69015c2 | 2012-02-24 19:45:46 +0000 | [diff] [blame] | 3875 | // FIXME: ParmVarDecls that are part of a function type of a parameter of |
| 3876 | // a function/objc method, should not have TU as lexical context. |
Argyrios Kyrtzidis | 8cceefa | 2012-02-24 01:12:38 +0000 | [diff] [blame] | 3877 | if (isa<ParmVarDecl>(D)) |
| 3878 | return; |
Argyrios Kyrtzidis | 10f3df5 | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 3879 | |
| 3880 | SourceManager &SM = Context->getSourceManager(); |
Argyrios Kyrtzidis | 19645d2 | 2011-10-28 23:57:43 +0000 | [diff] [blame] | 3881 | SourceLocation FileLoc = SM.getFileLoc(Loc); |
Argyrios Kyrtzidis | 10f3df5 | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 3882 | assert(SM.isLocalSourceLocation(FileLoc)); |
Argyrios Kyrtzidis | fab8d5b | 2011-10-28 23:57:47 +0000 | [diff] [blame] | 3883 | FileID FID; |
| 3884 | unsigned Offset; |
| 3885 | llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc); |
Argyrios Kyrtzidis | 10f3df5 | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 3886 | if (FID.isInvalid()) |
| 3887 | return; |
| 3888 | const SrcMgr::SLocEntry *Entry = &SM.getSLocEntry(FID); |
| 3889 | assert(Entry->isFile()); |
| 3890 | |
| 3891 | DeclIDInFileInfo *&Info = FileDeclIDs[Entry]; |
| 3892 | if (!Info) |
| 3893 | Info = new DeclIDInFileInfo(); |
| 3894 | |
Argyrios Kyrtzidis | fab8d5b | 2011-10-28 23:57:47 +0000 | [diff] [blame] | 3895 | std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID); |
Argyrios Kyrtzidis | 10f3df5 | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 3896 | LocDeclIDsTy &Decls = Info->DeclIDs; |
| 3897 | |
Argyrios Kyrtzidis | fab8d5b | 2011-10-28 23:57:47 +0000 | [diff] [blame] | 3898 | if (Decls.empty() || Decls.back().first <= Offset) { |
Argyrios Kyrtzidis | 10f3df5 | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 3899 | Decls.push_back(LocDecl); |
| 3900 | return; |
| 3901 | } |
| 3902 | |
| 3903 | LocDeclIDsTy::iterator |
| 3904 | I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl); |
| 3905 | |
| 3906 | Decls.insert(I, LocDecl); |
| 3907 | } |
| 3908 | |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3909 | void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) { |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 3910 | // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3911 | Record.push_back(Name.getNameKind()); |
| 3912 | switch (Name.getNameKind()) { |
| 3913 | case DeclarationName::Identifier: |
| 3914 | AddIdentifierRef(Name.getAsIdentifierInfo(), Record); |
| 3915 | break; |
| 3916 | |
| 3917 | case DeclarationName::ObjCZeroArgSelector: |
| 3918 | case DeclarationName::ObjCOneArgSelector: |
| 3919 | case DeclarationName::ObjCMultiArgSelector: |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 3920 | AddSelectorRef(Name.getObjCSelector(), Record); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3921 | break; |
| 3922 | |
| 3923 | case DeclarationName::CXXConstructorName: |
| 3924 | case DeclarationName::CXXDestructorName: |
| 3925 | case DeclarationName::CXXConversionFunctionName: |
| 3926 | AddTypeRef(Name.getCXXNameType(), Record); |
| 3927 | break; |
| 3928 | |
| 3929 | case DeclarationName::CXXOperatorName: |
| 3930 | Record.push_back(Name.getCXXOverloadedOperator()); |
| 3931 | break; |
| 3932 | |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 3933 | case DeclarationName::CXXLiteralOperatorName: |
| 3934 | AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record); |
| 3935 | break; |
| 3936 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3937 | case DeclarationName::CXXUsingDirective: |
| 3938 | // No extra data to emit |
| 3939 | break; |
| 3940 | } |
| 3941 | } |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 3942 | |
Argyrios Kyrtzidis | 4045107 | 2010-10-15 18:21:24 +0000 | [diff] [blame] | 3943 | void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc, |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3944 | DeclarationName Name, RecordDataImpl &Record) { |
Argyrios Kyrtzidis | 4045107 | 2010-10-15 18:21:24 +0000 | [diff] [blame] | 3945 | switch (Name.getNameKind()) { |
| 3946 | case DeclarationName::CXXConstructorName: |
| 3947 | case DeclarationName::CXXDestructorName: |
| 3948 | case DeclarationName::CXXConversionFunctionName: |
| 3949 | AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record); |
| 3950 | break; |
| 3951 | |
| 3952 | case DeclarationName::CXXOperatorName: |
| 3953 | AddSourceLocation( |
| 3954 | SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc), |
| 3955 | Record); |
| 3956 | AddSourceLocation( |
| 3957 | SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc), |
| 3958 | Record); |
| 3959 | break; |
| 3960 | |
| 3961 | case DeclarationName::CXXLiteralOperatorName: |
| 3962 | AddSourceLocation( |
| 3963 | SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc), |
| 3964 | Record); |
| 3965 | break; |
| 3966 | |
| 3967 | case DeclarationName::Identifier: |
| 3968 | case DeclarationName::ObjCZeroArgSelector: |
| 3969 | case DeclarationName::ObjCOneArgSelector: |
| 3970 | case DeclarationName::ObjCMultiArgSelector: |
| 3971 | case DeclarationName::CXXUsingDirective: |
| 3972 | break; |
| 3973 | } |
| 3974 | } |
| 3975 | |
| 3976 | void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo, |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3977 | RecordDataImpl &Record) { |
Argyrios Kyrtzidis | 4045107 | 2010-10-15 18:21:24 +0000 | [diff] [blame] | 3978 | AddDeclarationName(NameInfo.getName(), Record); |
| 3979 | AddSourceLocation(NameInfo.getLoc(), Record); |
| 3980 | AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record); |
| 3981 | } |
| 3982 | |
| 3983 | void ASTWriter::AddQualifierInfo(const QualifierInfo &Info, |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3984 | RecordDataImpl &Record) { |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3985 | AddNestedNameSpecifierLoc(Info.QualifierLoc, Record); |
Argyrios Kyrtzidis | 4045107 | 2010-10-15 18:21:24 +0000 | [diff] [blame] | 3986 | Record.push_back(Info.NumTemplParamLists); |
| 3987 | for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i) |
| 3988 | AddTemplateParameterList(Info.TemplParamLists[i], Record); |
| 3989 | } |
| 3990 | |
Sebastian Redl | a4232eb | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 3991 | void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS, |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3992 | RecordDataImpl &Record) { |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 3993 | // Nested name specifiers usually aren't too long. I think that 8 would |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3994 | // typically accommodate the vast majority. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3995 | SmallVector<NestedNameSpecifier *, 8> NestedNames; |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 3996 | |
| 3997 | // Push each of the NNS's onto a stack for serialization in reverse order. |
| 3998 | while (NNS) { |
| 3999 | NestedNames.push_back(NNS); |
| 4000 | NNS = NNS->getPrefix(); |
| 4001 | } |
| 4002 | |
| 4003 | Record.push_back(NestedNames.size()); |
| 4004 | while(!NestedNames.empty()) { |
| 4005 | NNS = NestedNames.pop_back_val(); |
| 4006 | NestedNameSpecifier::SpecifierKind Kind = NNS->getKind(); |
| 4007 | Record.push_back(Kind); |
| 4008 | switch (Kind) { |
| 4009 | case NestedNameSpecifier::Identifier: |
| 4010 | AddIdentifierRef(NNS->getAsIdentifier(), Record); |
| 4011 | break; |
| 4012 | |
| 4013 | case NestedNameSpecifier::Namespace: |
| 4014 | AddDeclRef(NNS->getAsNamespace(), Record); |
| 4015 | break; |
| 4016 | |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 4017 | case NestedNameSpecifier::NamespaceAlias: |
| 4018 | AddDeclRef(NNS->getAsNamespaceAlias(), Record); |
| 4019 | break; |
| 4020 | |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 4021 | case NestedNameSpecifier::TypeSpec: |
| 4022 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 4023 | AddTypeRef(QualType(NNS->getAsType(), 0), Record); |
| 4024 | Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate); |
| 4025 | break; |
| 4026 | |
| 4027 | case NestedNameSpecifier::Global: |
| 4028 | // Don't need to write an associated value. |
| 4029 | break; |
| 4030 | } |
| 4031 | } |
| 4032 | } |
Argyrios Kyrtzidis | 90b715e | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 4033 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 4034 | void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS, |
| 4035 | RecordDataImpl &Record) { |
| 4036 | // Nested name specifiers usually aren't too long. I think that 8 would |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 4037 | // typically accommodate the vast majority. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4038 | SmallVector<NestedNameSpecifierLoc , 8> NestedNames; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 4039 | |
| 4040 | // Push each of the nested-name-specifiers's onto a stack for |
| 4041 | // serialization in reverse order. |
| 4042 | while (NNS) { |
| 4043 | NestedNames.push_back(NNS); |
| 4044 | NNS = NNS.getPrefix(); |
| 4045 | } |
| 4046 | |
| 4047 | Record.push_back(NestedNames.size()); |
| 4048 | while(!NestedNames.empty()) { |
| 4049 | NNS = NestedNames.pop_back_val(); |
| 4050 | NestedNameSpecifier::SpecifierKind Kind |
| 4051 | = NNS.getNestedNameSpecifier()->getKind(); |
| 4052 | Record.push_back(Kind); |
| 4053 | switch (Kind) { |
| 4054 | case NestedNameSpecifier::Identifier: |
| 4055 | AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record); |
| 4056 | AddSourceRange(NNS.getLocalSourceRange(), Record); |
| 4057 | break; |
| 4058 | |
| 4059 | case NestedNameSpecifier::Namespace: |
| 4060 | AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record); |
| 4061 | AddSourceRange(NNS.getLocalSourceRange(), Record); |
| 4062 | break; |
| 4063 | |
| 4064 | case NestedNameSpecifier::NamespaceAlias: |
| 4065 | AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record); |
| 4066 | AddSourceRange(NNS.getLocalSourceRange(), Record); |
| 4067 | break; |
| 4068 | |
| 4069 | case NestedNameSpecifier::TypeSpec: |
| 4070 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 4071 | Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate); |
| 4072 | AddTypeLoc(NNS.getTypeLoc(), Record); |
| 4073 | AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record); |
| 4074 | break; |
| 4075 | |
| 4076 | case NestedNameSpecifier::Global: |
| 4077 | AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record); |
| 4078 | break; |
| 4079 | } |
| 4080 | } |
| 4081 | } |
| 4082 | |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4083 | void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) { |
Michael J. Spencer | 20249a1 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 4084 | TemplateName::NameKind Kind = Name.getKind(); |
Argyrios Kyrtzidis | 90b715e | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 4085 | Record.push_back(Kind); |
| 4086 | switch (Kind) { |
| 4087 | case TemplateName::Template: |
| 4088 | AddDeclRef(Name.getAsTemplateDecl(), Record); |
| 4089 | break; |
| 4090 | |
| 4091 | case TemplateName::OverloadedTemplate: { |
| 4092 | OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate(); |
| 4093 | Record.push_back(OvT->size()); |
| 4094 | for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end(); |
| 4095 | I != E; ++I) |
| 4096 | AddDeclRef(*I, Record); |
| 4097 | break; |
| 4098 | } |
Michael J. Spencer | 20249a1 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 4099 | |
Argyrios Kyrtzidis | 90b715e | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 4100 | case TemplateName::QualifiedTemplate: { |
| 4101 | QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName(); |
| 4102 | AddNestedNameSpecifier(QualT->getQualifier(), Record); |
| 4103 | Record.push_back(QualT->hasTemplateKeyword()); |
| 4104 | AddDeclRef(QualT->getTemplateDecl(), Record); |
| 4105 | break; |
| 4106 | } |
Michael J. Spencer | 20249a1 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 4107 | |
Argyrios Kyrtzidis | 90b715e | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 4108 | case TemplateName::DependentTemplate: { |
| 4109 | DependentTemplateName *DepT = Name.getAsDependentTemplateName(); |
| 4110 | AddNestedNameSpecifier(DepT->getQualifier(), Record); |
| 4111 | Record.push_back(DepT->isIdentifier()); |
| 4112 | if (DepT->isIdentifier()) |
| 4113 | AddIdentifierRef(DepT->getIdentifier(), Record); |
| 4114 | else |
| 4115 | Record.push_back(DepT->getOperator()); |
| 4116 | break; |
| 4117 | } |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4118 | |
| 4119 | case TemplateName::SubstTemplateTemplateParm: { |
| 4120 | SubstTemplateTemplateParmStorage *subst |
| 4121 | = Name.getAsSubstTemplateTemplateParm(); |
| 4122 | AddDeclRef(subst->getParameter(), Record); |
| 4123 | AddTemplateName(subst->getReplacement(), Record); |
| 4124 | break; |
| 4125 | } |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 4126 | |
| 4127 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 4128 | SubstTemplateTemplateParmPackStorage *SubstPack |
| 4129 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 4130 | AddDeclRef(SubstPack->getParameterPack(), Record); |
| 4131 | AddTemplateArgument(SubstPack->getArgumentPack(), Record); |
| 4132 | break; |
| 4133 | } |
Argyrios Kyrtzidis | 90b715e | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 4134 | } |
| 4135 | } |
| 4136 | |
Michael J. Spencer | 20249a1 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 4137 | void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg, |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4138 | RecordDataImpl &Record) { |
Argyrios Kyrtzidis | 90b715e | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 4139 | Record.push_back(Arg.getKind()); |
| 4140 | switch (Arg.getKind()) { |
| 4141 | case TemplateArgument::Null: |
| 4142 | break; |
| 4143 | case TemplateArgument::Type: |
| 4144 | AddTypeRef(Arg.getAsType(), Record); |
| 4145 | break; |
| 4146 | case TemplateArgument::Declaration: |
| 4147 | AddDeclRef(Arg.getAsDecl(), Record); |
| 4148 | break; |
| 4149 | case TemplateArgument::Integral: |
| 4150 | AddAPSInt(*Arg.getAsIntegral(), Record); |
| 4151 | AddTypeRef(Arg.getIntegralType(), Record); |
| 4152 | break; |
| 4153 | case TemplateArgument::Template: |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 4154 | AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record); |
| 4155 | break; |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4156 | case TemplateArgument::TemplateExpansion: |
| 4157 | AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record); |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 4158 | if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions()) |
| 4159 | Record.push_back(*NumExpansions + 1); |
| 4160 | else |
| 4161 | Record.push_back(0); |
Argyrios Kyrtzidis | 90b715e | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 4162 | break; |
| 4163 | case TemplateArgument::Expression: |
| 4164 | AddStmt(Arg.getAsExpr()); |
| 4165 | break; |
| 4166 | case TemplateArgument::Pack: |
| 4167 | Record.push_back(Arg.pack_size()); |
| 4168 | for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end(); |
| 4169 | I != E; ++I) |
| 4170 | AddTemplateArgument(*I, Record); |
| 4171 | break; |
| 4172 | } |
| 4173 | } |
Argyrios Kyrtzidis | dd41c14 | 2010-06-23 13:48:30 +0000 | [diff] [blame] | 4174 | |
| 4175 | void |
Sebastian Redl | a4232eb | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 4176 | ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams, |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4177 | RecordDataImpl &Record) { |
Argyrios Kyrtzidis | dd41c14 | 2010-06-23 13:48:30 +0000 | [diff] [blame] | 4178 | assert(TemplateParams && "No TemplateParams!"); |
| 4179 | AddSourceLocation(TemplateParams->getTemplateLoc(), Record); |
| 4180 | AddSourceLocation(TemplateParams->getLAngleLoc(), Record); |
| 4181 | AddSourceLocation(TemplateParams->getRAngleLoc(), Record); |
| 4182 | Record.push_back(TemplateParams->size()); |
| 4183 | for (TemplateParameterList::const_iterator |
| 4184 | P = TemplateParams->begin(), PEnd = TemplateParams->end(); |
| 4185 | P != PEnd; ++P) |
| 4186 | AddDeclRef(*P, Record); |
| 4187 | } |
| 4188 | |
| 4189 | /// \brief Emit a template argument list. |
| 4190 | void |
Sebastian Redl | a4232eb | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 4191 | ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs, |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4192 | RecordDataImpl &Record) { |
Argyrios Kyrtzidis | dd41c14 | 2010-06-23 13:48:30 +0000 | [diff] [blame] | 4193 | assert(TemplateArgs && "No TemplateArgs!"); |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4194 | Record.push_back(TemplateArgs->size()); |
| 4195 | for (int i=0, e = TemplateArgs->size(); i != e; ++i) |
Argyrios Kyrtzidis | dd41c14 | 2010-06-23 13:48:30 +0000 | [diff] [blame] | 4196 | AddTemplateArgument(TemplateArgs->get(i), Record); |
| 4197 | } |
Argyrios Kyrtzidis | 37ffed3 | 2010-07-02 11:55:32 +0000 | [diff] [blame] | 4198 | |
| 4199 | |
| 4200 | void |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4201 | ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) { |
Argyrios Kyrtzidis | 37ffed3 | 2010-07-02 11:55:32 +0000 | [diff] [blame] | 4202 | Record.push_back(Set.size()); |
| 4203 | for (UnresolvedSetImpl::const_iterator |
| 4204 | I = Set.begin(), E = Set.end(); I != E; ++I) { |
| 4205 | AddDeclRef(I.getDecl(), Record); |
| 4206 | Record.push_back(I.getAccess()); |
| 4207 | } |
| 4208 | } |
Argyrios Kyrtzidis | 0745d0a | 2010-07-02 23:30:27 +0000 | [diff] [blame] | 4209 | |
Sebastian Redl | a4232eb | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 4210 | void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base, |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4211 | RecordDataImpl &Record) { |
Argyrios Kyrtzidis | 0745d0a | 2010-07-02 23:30:27 +0000 | [diff] [blame] | 4212 | Record.push_back(Base.isVirtual()); |
| 4213 | Record.push_back(Base.isBaseOfClass()); |
| 4214 | Record.push_back(Base.getAccessSpecifierAsWritten()); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 4215 | Record.push_back(Base.getInheritConstructors()); |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 4216 | AddTypeSourceInfo(Base.getTypeSourceInfo(), Record); |
Argyrios Kyrtzidis | 0745d0a | 2010-07-02 23:30:27 +0000 | [diff] [blame] | 4217 | AddSourceRange(Base.getSourceRange(), Record); |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 4218 | AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc() |
| 4219 | : SourceLocation(), |
| 4220 | Record); |
Argyrios Kyrtzidis | 0745d0a | 2010-07-02 23:30:27 +0000 | [diff] [blame] | 4221 | } |
Sebastian Redl | 30c514c | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 4222 | |
Douglas Gregor | 7c789c1 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 4223 | void ASTWriter::FlushCXXBaseSpecifiers() { |
| 4224 | RecordData Record; |
| 4225 | for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) { |
| 4226 | Record.clear(); |
| 4227 | |
| 4228 | // Record the offset of this base-specifier set. |
Douglas Gregor | e92b8a1 | 2011-08-04 00:01:48 +0000 | [diff] [blame] | 4229 | unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1; |
Douglas Gregor | 7c789c1 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 4230 | if (Index == CXXBaseSpecifiersOffsets.size()) |
| 4231 | CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo()); |
| 4232 | else { |
| 4233 | if (Index > CXXBaseSpecifiersOffsets.size()) |
| 4234 | CXXBaseSpecifiersOffsets.resize(Index + 1); |
| 4235 | CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo(); |
| 4236 | } |
| 4237 | |
| 4238 | const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases, |
| 4239 | *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd; |
| 4240 | Record.push_back(BEnd - B); |
| 4241 | for (; B != BEnd; ++B) |
| 4242 | AddCXXBaseSpecifier(*B, Record); |
| 4243 | Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record); |
Douglas Gregor | acec34b | 2010-10-30 04:28:16 +0000 | [diff] [blame] | 4244 | |
| 4245 | // Flush any expressions that were written as part of the base specifiers. |
| 4246 | FlushStmts(); |
Douglas Gregor | 7c789c1 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 4247 | } |
| 4248 | |
| 4249 | CXXBaseSpecifiersToWrite.clear(); |
| 4250 | } |
| 4251 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 4252 | void ASTWriter::AddCXXCtorInitializers( |
| 4253 | const CXXCtorInitializer * const *CtorInitializers, |
| 4254 | unsigned NumCtorInitializers, |
| 4255 | RecordDataImpl &Record) { |
| 4256 | Record.push_back(NumCtorInitializers); |
| 4257 | for (unsigned i=0; i != NumCtorInitializers; ++i) { |
| 4258 | const CXXCtorInitializer *Init = CtorInitializers[i]; |
Argyrios Kyrtzidis | 8e706f4 | 2010-08-09 10:54:12 +0000 | [diff] [blame] | 4259 | |
Argyrios Kyrtzidis | 8e706f4 | 2010-08-09 10:54:12 +0000 | [diff] [blame] | 4260 | if (Init->isBaseInitializer()) { |
Sean Hunt | 156b640 | 2011-05-04 01:19:08 +0000 | [diff] [blame] | 4261 | Record.push_back(CTOR_INITIALIZER_BASE); |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4262 | AddTypeSourceInfo(Init->getTypeSourceInfo(), Record); |
Argyrios Kyrtzidis | 8e706f4 | 2010-08-09 10:54:12 +0000 | [diff] [blame] | 4263 | Record.push_back(Init->isBaseVirtual()); |
Sean Hunt | 156b640 | 2011-05-04 01:19:08 +0000 | [diff] [blame] | 4264 | } else if (Init->isDelegatingInitializer()) { |
| 4265 | Record.push_back(CTOR_INITIALIZER_DELEGATING); |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4266 | AddTypeSourceInfo(Init->getTypeSourceInfo(), Record); |
Sean Hunt | 156b640 | 2011-05-04 01:19:08 +0000 | [diff] [blame] | 4267 | } else if (Init->isMemberInitializer()){ |
| 4268 | Record.push_back(CTOR_INITIALIZER_MEMBER); |
| 4269 | AddDeclRef(Init->getMember(), Record); |
Argyrios Kyrtzidis | 8e706f4 | 2010-08-09 10:54:12 +0000 | [diff] [blame] | 4270 | } else { |
Sean Hunt | 156b640 | 2011-05-04 01:19:08 +0000 | [diff] [blame] | 4271 | Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER); |
| 4272 | AddDeclRef(Init->getIndirectMember(), Record); |
Argyrios Kyrtzidis | 8e706f4 | 2010-08-09 10:54:12 +0000 | [diff] [blame] | 4273 | } |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4274 | |
Argyrios Kyrtzidis | 8e706f4 | 2010-08-09 10:54:12 +0000 | [diff] [blame] | 4275 | AddSourceLocation(Init->getMemberLocation(), Record); |
| 4276 | AddStmt(Init->getInit()); |
Argyrios Kyrtzidis | 8e706f4 | 2010-08-09 10:54:12 +0000 | [diff] [blame] | 4277 | AddSourceLocation(Init->getLParenLoc(), Record); |
| 4278 | AddSourceLocation(Init->getRParenLoc(), Record); |
| 4279 | Record.push_back(Init->isWritten()); |
| 4280 | if (Init->isWritten()) { |
| 4281 | Record.push_back(Init->getSourceOrder()); |
| 4282 | } else { |
| 4283 | Record.push_back(Init->getNumArrayIndices()); |
| 4284 | for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i) |
| 4285 | AddDeclRef(Init->getArrayIndex(i), Record); |
| 4286 | } |
| 4287 | } |
| 4288 | } |
| 4289 | |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4290 | void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) { |
| 4291 | assert(D->DefinitionData); |
| 4292 | struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData; |
Douglas Gregor | 9d36f5d | 2012-02-14 17:54:36 +0000 | [diff] [blame] | 4293 | Record.push_back(Data.IsLambda); |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4294 | Record.push_back(Data.UserDeclaredConstructor); |
| 4295 | Record.push_back(Data.UserDeclaredCopyConstructor); |
Douglas Gregor | 58e9797 | 2011-09-06 16:38:46 +0000 | [diff] [blame] | 4296 | Record.push_back(Data.UserDeclaredMoveConstructor); |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4297 | Record.push_back(Data.UserDeclaredCopyAssignment); |
Douglas Gregor | 58e9797 | 2011-09-06 16:38:46 +0000 | [diff] [blame] | 4298 | Record.push_back(Data.UserDeclaredMoveAssignment); |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4299 | Record.push_back(Data.UserDeclaredDestructor); |
| 4300 | Record.push_back(Data.Aggregate); |
| 4301 | Record.push_back(Data.PlainOldData); |
| 4302 | Record.push_back(Data.Empty); |
| 4303 | Record.push_back(Data.Polymorphic); |
| 4304 | Record.push_back(Data.Abstract); |
Chandler Carruth | ec997dc | 2011-04-30 10:07:30 +0000 | [diff] [blame] | 4305 | Record.push_back(Data.IsStandardLayout); |
Chandler Carruth | a822544 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 4306 | Record.push_back(Data.HasNoNonEmptyBases); |
| 4307 | Record.push_back(Data.HasPrivateFields); |
| 4308 | Record.push_back(Data.HasProtectedFields); |
| 4309 | Record.push_back(Data.HasPublicFields); |
Douglas Gregor | 2bb1101 | 2011-05-13 01:05:07 +0000 | [diff] [blame] | 4310 | Record.push_back(Data.HasMutableFields); |
Richard Smith | dfefb84 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 4311 | Record.push_back(Data.HasOnlyCMembers); |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 4312 | Record.push_back(Data.HasTrivialDefaultConstructor); |
Richard Smith | 6b8bc07 | 2011-08-10 18:11:37 +0000 | [diff] [blame] | 4313 | Record.push_back(Data.HasConstexprNonCopyMoveConstructor); |
Richard Smith | dfefb84 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 4314 | Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr); |
| 4315 | Record.push_back(Data.DefaultedCopyConstructorIsConstexpr); |
| 4316 | Record.push_back(Data.DefaultedMoveConstructorIsConstexpr); |
| 4317 | Record.push_back(Data.HasConstexprDefaultConstructor); |
| 4318 | Record.push_back(Data.HasConstexprCopyConstructor); |
| 4319 | Record.push_back(Data.HasConstexprMoveConstructor); |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4320 | Record.push_back(Data.HasTrivialCopyConstructor); |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 4321 | Record.push_back(Data.HasTrivialMoveConstructor); |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4322 | Record.push_back(Data.HasTrivialCopyAssignment); |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 4323 | Record.push_back(Data.HasTrivialMoveAssignment); |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4324 | Record.push_back(Data.HasTrivialDestructor); |
Richard Smith | dfefb84 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 4325 | Record.push_back(Data.HasIrrelevantDestructor); |
Chandler Carruth | 9b6347c | 2011-04-24 02:49:34 +0000 | [diff] [blame] | 4326 | Record.push_back(Data.HasNonLiteralTypeFieldsOrBases); |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4327 | Record.push_back(Data.ComputedVisibleConversions); |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4328 | Record.push_back(Data.UserProvidedDefaultConstructor); |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4329 | Record.push_back(Data.DeclaredDefaultConstructor); |
| 4330 | Record.push_back(Data.DeclaredCopyConstructor); |
Douglas Gregor | 58e9797 | 2011-09-06 16:38:46 +0000 | [diff] [blame] | 4331 | Record.push_back(Data.DeclaredMoveConstructor); |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4332 | Record.push_back(Data.DeclaredCopyAssignment); |
Douglas Gregor | 58e9797 | 2011-09-06 16:38:46 +0000 | [diff] [blame] | 4333 | Record.push_back(Data.DeclaredMoveAssignment); |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4334 | Record.push_back(Data.DeclaredDestructor); |
Sebastian Redl | 14c3633 | 2011-08-31 13:59:56 +0000 | [diff] [blame] | 4335 | Record.push_back(Data.FailedImplicitMoveConstructor); |
| 4336 | Record.push_back(Data.FailedImplicitMoveAssignment); |
Richard Smith | dfefb84 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 4337 | // IsLambda bit is already saved. |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4338 | |
| 4339 | Record.push_back(Data.NumBases); |
Douglas Gregor | 7c789c1 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 4340 | if (Data.NumBases > 0) |
| 4341 | AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases, |
| 4342 | Record); |
| 4343 | |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4344 | // FIXME: Make VBases lazily computed when needed to avoid storing them. |
| 4345 | Record.push_back(Data.NumVBases); |
Douglas Gregor | 7c789c1 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 4346 | if (Data.NumVBases > 0) |
| 4347 | AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases, |
| 4348 | Record); |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4349 | |
| 4350 | AddUnresolvedSet(Data.Conversions, Record); |
| 4351 | AddUnresolvedSet(Data.VisibleConversions, Record); |
| 4352 | // Data.Definition is the owning decl, no need to write it. |
| 4353 | AddDeclRef(Data.FirstFriend, Record); |
Douglas Gregor | 9d36f5d | 2012-02-14 17:54:36 +0000 | [diff] [blame] | 4354 | |
| 4355 | // Add lambda-specific data. |
| 4356 | if (Data.IsLambda) { |
| 4357 | CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData(); |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 4358 | Record.push_back(Lambda.Dependent); |
Douglas Gregor | 9d36f5d | 2012-02-14 17:54:36 +0000 | [diff] [blame] | 4359 | Record.push_back(Lambda.NumCaptures); |
| 4360 | Record.push_back(Lambda.NumExplicitCaptures); |
Douglas Gregor | 9e8c92a | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 4361 | Record.push_back(Lambda.ManglingNumber); |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 4362 | AddDeclRef(Lambda.ContextDecl, Record); |
Douglas Gregor | 9d36f5d | 2012-02-14 17:54:36 +0000 | [diff] [blame] | 4363 | for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) { |
| 4364 | LambdaExpr::Capture &Capture = Lambda.Captures[I]; |
| 4365 | AddSourceLocation(Capture.getLocation(), Record); |
| 4366 | Record.push_back(Capture.isImplicit()); |
| 4367 | Record.push_back(Capture.getCaptureKind()); // FIXME: stable! |
| 4368 | VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0; |
| 4369 | AddDeclRef(Var, Record); |
| 4370 | AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc() |
| 4371 | : SourceLocation(), |
| 4372 | Record); |
| 4373 | } |
| 4374 | } |
Argyrios Kyrtzidis | 89eaf3a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4375 | } |
| 4376 | |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 4377 | void ASTWriter::ReaderInitialized(ASTReader *Reader) { |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 4378 | assert(Reader && "Cannot remove chain"); |
Douglas Gregor | 10bc00f | 2011-08-18 04:12:04 +0000 | [diff] [blame] | 4379 | assert((!Chain || Chain == Reader) && "Cannot replace chain"); |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 4380 | assert(FirstDeclID == NextDeclID && |
| 4381 | FirstTypeID == NextTypeID && |
| 4382 | FirstIdentID == NextIdentID && |
Douglas Gregor | 26ced12 | 2011-12-01 00:59:36 +0000 | [diff] [blame] | 4383 | FirstSubmoduleID == NextSubmoduleID && |
Sebastian Redl | e58aa89 | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 4384 | FirstSelectorID == NextSelectorID && |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 4385 | "Setting chain after writing has started."); |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 4386 | |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 4387 | Chain = Reader; |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 4388 | |
Douglas Gregor | 10bc00f | 2011-08-18 04:12:04 +0000 | [diff] [blame] | 4389 | FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls(); |
| 4390 | FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes(); |
| 4391 | FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers(); |
Douglas Gregor | 26ced12 | 2011-12-01 00:59:36 +0000 | [diff] [blame] | 4392 | FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules(); |
Douglas Gregor | 10bc00f | 2011-08-18 04:12:04 +0000 | [diff] [blame] | 4393 | FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors(); |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 4394 | NextDeclID = FirstDeclID; |
| 4395 | NextTypeID = FirstTypeID; |
| 4396 | NextIdentID = FirstIdentID; |
| 4397 | NextSelectorID = FirstSelectorID; |
Douglas Gregor | 26ced12 | 2011-12-01 00:59:36 +0000 | [diff] [blame] | 4398 | NextSubmoduleID = FirstSubmoduleID; |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 4399 | } |
| 4400 | |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 4401 | void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) { |
Sebastian Redl | f2f0f03 | 2010-07-23 23:49:55 +0000 | [diff] [blame] | 4402 | IdentifierIDs[II] = ID; |
Douglas Gregor | 040a804 | 2011-02-11 00:26:14 +0000 | [diff] [blame] | 4403 | if (II->hasMacroDefinition()) |
| 4404 | DeserializedMacroNames.push_back(II); |
Sebastian Redl | f2f0f03 | 2010-07-23 23:49:55 +0000 | [diff] [blame] | 4405 | } |
| 4406 | |
Argyrios Kyrtzidis | c8e5d51 | 2010-08-20 16:03:59 +0000 | [diff] [blame] | 4407 | void ASTWriter::TypeRead(TypeIdx Idx, QualType T) { |
Douglas Gregor | 9747583 | 2010-10-05 18:37:06 +0000 | [diff] [blame] | 4408 | // Always take the highest-numbered type index. This copes with an interesting |
| 4409 | // case for chained AST writing where we schedule writing the type and then, |
Michael J. Spencer | 20249a1 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 4410 | // later, deserialize the type from another AST. In this case, we want to |
Douglas Gregor | 9747583 | 2010-10-05 18:37:06 +0000 | [diff] [blame] | 4411 | // keep the higher-numbered entry so that we can properly write it out to |
| 4412 | // the AST file. |
| 4413 | TypeIdx &StoredIdx = TypeIdxs[T]; |
| 4414 | if (Idx.getIndex() >= StoredIdx.getIndex()) |
| 4415 | StoredIdx = Idx; |
Sebastian Redl | 30c514c | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 4416 | } |
| 4417 | |
Sebastian Redl | 8538e8d | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 4418 | void ASTWriter::SelectorRead(SelectorID ID, Selector S) { |
Sebastian Redl | 5d05007 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 4419 | SelectorIDs[S] = ID; |
| 4420 | } |
Douglas Gregor | 77424bc | 2010-10-02 19:29:26 +0000 | [diff] [blame] | 4421 | |
Argyrios Kyrtzidis | e24692b | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 4422 | void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID, |
Douglas Gregor | 77424bc | 2010-10-02 19:29:26 +0000 | [diff] [blame] | 4423 | MacroDefinition *MD) { |
Argyrios Kyrtzidis | e24692b | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 4424 | assert(MacroDefinitions.find(MD) == MacroDefinitions.end()); |
Douglas Gregor | 77424bc | 2010-10-02 19:29:26 +0000 | [diff] [blame] | 4425 | MacroDefinitions[MD] = ID; |
| 4426 | } |
Argyrios Kyrtzidis | 565bf30 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 4427 | |
Douglas Gregor | 1d4c113 | 2011-12-20 22:06:13 +0000 | [diff] [blame] | 4428 | void ASTWriter::MacroVisible(IdentifierInfo *II) { |
| 4429 | DeserializedMacroNames.push_back(II); |
| 4430 | } |
| 4431 | |
Douglas Gregor | a015cab | 2011-12-02 17:30:13 +0000 | [diff] [blame] | 4432 | void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) { |
| 4433 | assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end()); |
| 4434 | SubmoduleIDs[Mod] = ID; |
| 4435 | } |
| 4436 | |
Argyrios Kyrtzidis | 565bf30 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 4437 | void ASTWriter::CompletedTagDefinition(const TagDecl *D) { |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 4438 | assert(D->isCompleteDefinition()); |
Douglas Gregor | 61c5e34 | 2011-09-17 00:05:03 +0000 | [diff] [blame] | 4439 | assert(!WritingAST && "Already writing the AST!"); |
Argyrios Kyrtzidis | 565bf30 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 4440 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
| 4441 | // We are interested when a PCH decl is modified. |
Douglas Gregor | 919814d | 2011-09-09 23:01:35 +0000 | [diff] [blame] | 4442 | if (RD->isFromASTFile()) { |
Argyrios Kyrtzidis | 565bf30 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 4443 | // A forward reference was mutated into a definition. Rewrite it. |
| 4444 | // FIXME: This happens during template instantiation, should we |
| 4445 | // have created a new definition decl instead ? |
Argyrios Kyrtzidis | d3d0755 | 2010-10-28 07:38:45 +0000 | [diff] [blame] | 4446 | RewriteDecl(RD); |
Argyrios Kyrtzidis | 565bf30 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 4447 | } |
Argyrios Kyrtzidis | 565bf30 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 4448 | } |
| 4449 | } |
Argyrios Kyrtzidis | 100050b | 2010-10-28 07:38:51 +0000 | [diff] [blame] | 4450 | void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) { |
Douglas Gregor | 61c5e34 | 2011-09-17 00:05:03 +0000 | [diff] [blame] | 4451 | assert(!WritingAST && "Already writing the AST!"); |
| 4452 | |
Argyrios Kyrtzidis | 100050b | 2010-10-28 07:38:51 +0000 | [diff] [blame] | 4453 | // TU and namespaces are handled elsewhere. |
| 4454 | if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC)) |
| 4455 | return; |
| 4456 | |
Douglas Gregor | 919814d | 2011-09-09 23:01:35 +0000 | [diff] [blame] | 4457 | if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile())) |
Argyrios Kyrtzidis | 100050b | 2010-10-28 07:38:51 +0000 | [diff] [blame] | 4458 | return; // Not a source decl added to a DeclContext from PCH. |
| 4459 | |
| 4460 | AddUpdatedDeclContext(DC); |
| 4461 | } |
Argyrios Kyrtzidis | b6cc0e1 | 2010-10-24 17:26:54 +0000 | [diff] [blame] | 4462 | |
| 4463 | void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) { |
Douglas Gregor | 61c5e34 | 2011-09-17 00:05:03 +0000 | [diff] [blame] | 4464 | assert(!WritingAST && "Already writing the AST!"); |
Argyrios Kyrtzidis | b6cc0e1 | 2010-10-24 17:26:54 +0000 | [diff] [blame] | 4465 | assert(D->isImplicit()); |
Douglas Gregor | 919814d | 2011-09-09 23:01:35 +0000 | [diff] [blame] | 4466 | if (!(!D->isFromASTFile() && RD->isFromASTFile())) |
Argyrios Kyrtzidis | b6cc0e1 | 2010-10-24 17:26:54 +0000 | [diff] [blame] | 4467 | return; // Not a source member added to a class from PCH. |
| 4468 | if (!isa<CXXMethodDecl>(D)) |
| 4469 | return; // We are interested in lazily declared implicit methods. |
| 4470 | |
| 4471 | // A decl coming from PCH was modified. |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 4472 | assert(RD->isCompleteDefinition()); |
Argyrios Kyrtzidis | b6cc0e1 | 2010-10-24 17:26:54 +0000 | [diff] [blame] | 4473 | UpdateRecord &Record = DeclUpdates[RD]; |
| 4474 | Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER); |
Douglas Gregor | 61c5e34 | 2011-09-17 00:05:03 +0000 | [diff] [blame] | 4475 | Record.push_back(reinterpret_cast<uint64_t>(D)); |
Argyrios Kyrtzidis | b6cc0e1 | 2010-10-24 17:26:54 +0000 | [diff] [blame] | 4476 | } |
Argyrios Kyrtzidis | bef1a7b | 2010-10-28 07:38:42 +0000 | [diff] [blame] | 4477 | |
| 4478 | void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD, |
| 4479 | const ClassTemplateSpecializationDecl *D) { |
Argyrios Kyrtzidis | 0f04f69 | 2010-10-28 07:38:47 +0000 | [diff] [blame] | 4480 | // The specializations set is kept in the canonical template. |
Douglas Gregor | 61c5e34 | 2011-09-17 00:05:03 +0000 | [diff] [blame] | 4481 | assert(!WritingAST && "Already writing the AST!"); |
Argyrios Kyrtzidis | 0f04f69 | 2010-10-28 07:38:47 +0000 | [diff] [blame] | 4482 | TD = TD->getCanonicalDecl(); |
Douglas Gregor | 919814d | 2011-09-09 23:01:35 +0000 | [diff] [blame] | 4483 | if (!(!D->isFromASTFile() && TD->isFromASTFile())) |
Argyrios Kyrtzidis | bef1a7b | 2010-10-28 07:38:42 +0000 | [diff] [blame] | 4484 | return; // Not a source specialization added to a template from PCH. |
| 4485 | |
| 4486 | UpdateRecord &Record = DeclUpdates[TD]; |
| 4487 | Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION); |
Douglas Gregor | 61c5e34 | 2011-09-17 00:05:03 +0000 | [diff] [blame] | 4488 | Record.push_back(reinterpret_cast<uint64_t>(D)); |
Argyrios Kyrtzidis | bef1a7b | 2010-10-28 07:38:42 +0000 | [diff] [blame] | 4489 | } |
Douglas Gregor | 89d9980 | 2010-11-30 06:16:57 +0000 | [diff] [blame] | 4490 | |
Sebastian Redl | 5bbcdbf | 2011-04-14 14:07:59 +0000 | [diff] [blame] | 4491 | void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD, |
| 4492 | const FunctionDecl *D) { |
| 4493 | // The specializations set is kept in the canonical template. |
Douglas Gregor | 61c5e34 | 2011-09-17 00:05:03 +0000 | [diff] [blame] | 4494 | assert(!WritingAST && "Already writing the AST!"); |
Sebastian Redl | 5bbcdbf | 2011-04-14 14:07:59 +0000 | [diff] [blame] | 4495 | TD = TD->getCanonicalDecl(); |
Douglas Gregor | 919814d | 2011-09-09 23:01:35 +0000 | [diff] [blame] | 4496 | if (!(!D->isFromASTFile() && TD->isFromASTFile())) |
Sebastian Redl | 5bbcdbf | 2011-04-14 14:07:59 +0000 | [diff] [blame] | 4497 | return; // Not a source specialization added to a template from PCH. |
| 4498 | |
| 4499 | UpdateRecord &Record = DeclUpdates[TD]; |
| 4500 | Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION); |
Douglas Gregor | 61c5e34 | 2011-09-17 00:05:03 +0000 | [diff] [blame] | 4501 | Record.push_back(reinterpret_cast<uint64_t>(D)); |
Sebastian Redl | 5bbcdbf | 2011-04-14 14:07:59 +0000 | [diff] [blame] | 4502 | } |
| 4503 | |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 4504 | void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) { |
Douglas Gregor | 61c5e34 | 2011-09-17 00:05:03 +0000 | [diff] [blame] | 4505 | assert(!WritingAST && "Already writing the AST!"); |
Douglas Gregor | 919814d | 2011-09-09 23:01:35 +0000 | [diff] [blame] | 4506 | if (!D->isFromASTFile()) |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 4507 | return; // Declaration not imported from PCH. |
| 4508 | |
| 4509 | // Implicit decl from a PCH was defined. |
| 4510 | // FIXME: Should implicit definition be a separate FunctionDecl? |
| 4511 | RewriteDecl(D); |
| 4512 | } |
| 4513 | |
Sebastian Redl | f79a719 | 2011-04-29 08:19:30 +0000 | [diff] [blame] | 4514 | void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) { |
Douglas Gregor | 61c5e34 | 2011-09-17 00:05:03 +0000 | [diff] [blame] | 4515 | assert(!WritingAST && "Already writing the AST!"); |
Douglas Gregor | 919814d | 2011-09-09 23:01:35 +0000 | [diff] [blame] | 4516 | if (!D->isFromASTFile()) |
Sebastian Redl | f79a719 | 2011-04-29 08:19:30 +0000 | [diff] [blame] | 4517 | return; |
| 4518 | |
| 4519 | // Since the actual instantiation is delayed, this really means that we need |
| 4520 | // to update the instantiation location. |
| 4521 | UpdateRecord &Record = DeclUpdates[D]; |
| 4522 | Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER); |
| 4523 | AddSourceLocation( |
| 4524 | D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record); |
| 4525 | } |
| 4526 | |
Argyrios Kyrtzidis | e6b8d68 | 2011-09-01 00:58:55 +0000 | [diff] [blame] | 4527 | void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD, |
| 4528 | const ObjCInterfaceDecl *IFD) { |
Douglas Gregor | 61c5e34 | 2011-09-17 00:05:03 +0000 | [diff] [blame] | 4529 | assert(!WritingAST && "Already writing the AST!"); |
Douglas Gregor | 919814d | 2011-09-09 23:01:35 +0000 | [diff] [blame] | 4530 | if (!IFD->isFromASTFile()) |
Argyrios Kyrtzidis | e6b8d68 | 2011-09-01 00:58:55 +0000 | [diff] [blame] | 4531 | return; // Declaration not imported from PCH. |
Douglas Gregor | cff9f26 | 2012-01-27 01:47:08 +0000 | [diff] [blame] | 4532 | |
| 4533 | assert(IFD->getDefinition() && "Category on a class without a definition?"); |
| 4534 | ObjCClassesWithCategories.insert( |
| 4535 | const_cast<ObjCInterfaceDecl *>(IFD->getDefinition())); |
Argyrios Kyrtzidis | e6b8d68 | 2011-09-01 00:58:55 +0000 | [diff] [blame] | 4536 | } |
Argyrios Kyrtzidis | ad834d5 | 2011-11-12 21:07:46 +0000 | [diff] [blame] | 4537 | |
Argyrios Kyrtzidis | 1a43415 | 2011-11-12 21:07:52 +0000 | [diff] [blame] | 4538 | |
Argyrios Kyrtzidis | c80553e | 2011-11-14 04:52:29 +0000 | [diff] [blame] | 4539 | void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop, |
| 4540 | const ObjCPropertyDecl *OrigProp, |
| 4541 | const ObjCCategoryDecl *ClassExt) { |
| 4542 | const ObjCInterfaceDecl *D = ClassExt->getClassInterface(); |
| 4543 | if (!D) |
| 4544 | return; |
| 4545 | |
| 4546 | assert(!WritingAST && "Already writing the AST!"); |
| 4547 | if (!D->isFromASTFile()) |
| 4548 | return; // Declaration not imported from PCH. |
| 4549 | |
| 4550 | RewriteDecl(D); |
| 4551 | } |