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