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