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