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