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