Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 1 | //===--- ASTWriter.cpp - AST File Writer ------------------------*- C++ -*-===// |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 10 | // This file defines the ASTWriter class, which writes AST files. |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Sebastian Redl | 1914c6f | 2010-08-18 23:56:37 +0000 | [diff] [blame] | 14 | #include "clang/Serialization/ASTWriter.h" |
Douglas Gregor | 6623e1f | 2015-11-03 18:33:07 +0000 | [diff] [blame] | 15 | #include "clang/Serialization/ModuleFileExtension.h" |
Argyrios Kyrtzidis | 4bd9710 | 2010-08-20 16:03:52 +0000 | [diff] [blame] | 16 | #include "ASTCommon.h" |
Richard Smith | d88a7f1 | 2015-09-01 20:35:42 +0000 | [diff] [blame] | 17 | #include "ASTReaderInternals.h" |
| 18 | #include "MultiOnDiskHashTable.h" |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
| 20 | #include "clang/AST/Decl.h" |
| 21 | #include "clang/AST/DeclContextInternals.h" |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclFriend.h" |
Richard Smith | 961eae5 | 2014-03-25 01:14:22 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclLookups.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | feb84b0 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 25 | #include "clang/AST/Expr.h" |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 26 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 27 | #include "clang/AST/Type.h" |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 28 | #include "clang/AST/TypeLocVisitor.h" |
Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 29 | #include "clang/Basic/DiagnosticOptions.h" |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 30 | #include "clang/Basic/FileManager.h" |
Chris Lattner | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 31 | #include "clang/Basic/FileSystemStatCache.h" |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 32 | #include "clang/Basic/SourceManager.h" |
Douglas Gregor | 4c7626e | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 33 | #include "clang/Basic/SourceManagerInternals.h" |
Douglas Gregor | bfbde53 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 34 | #include "clang/Basic/TargetInfo.h" |
Douglas Gregor | cb177f1 | 2012-10-16 23:40:58 +0000 | [diff] [blame] | 35 | #include "clang/Basic/TargetOptions.h" |
Douglas Gregor | 7b71e63 | 2009-04-27 22:23:34 +0000 | [diff] [blame] | 36 | #include "clang/Basic/Version.h" |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 37 | #include "clang/Basic/VersionTuple.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 38 | #include "clang/Lex/HeaderSearch.h" |
| 39 | #include "clang/Lex/HeaderSearchOptions.h" |
| 40 | #include "clang/Lex/MacroInfo.h" |
| 41 | #include "clang/Lex/PreprocessingRecord.h" |
| 42 | #include "clang/Lex/Preprocessor.h" |
| 43 | #include "clang/Lex/PreprocessorOptions.h" |
| 44 | #include "clang/Sema/IdentifierResolver.h" |
| 45 | #include "clang/Sema/Sema.h" |
| 46 | #include "clang/Serialization/ASTReader.h" |
Richard Smith | b5aaf5a | 2015-09-01 02:35:58 +0000 | [diff] [blame] | 47 | #include "clang/Serialization/SerializationDiagnostic.h" |
Douglas Gregor | e0a3a51 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 48 | #include "llvm/ADT/APFloat.h" |
| 49 | #include "llvm/ADT/APInt.h" |
Argyrios Kyrtzidis | 5c2a345 | 2013-03-06 18:12:47 +0000 | [diff] [blame] | 50 | #include "llvm/ADT/Hashing.h" |
Daniel Dunbar | f8502d5 | 2009-10-17 23:52:28 +0000 | [diff] [blame] | 51 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 52 | #include "llvm/Bitcode/BitstreamWriter.h" |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 53 | #include "llvm/Support/EndianStream.h" |
Michael J. Spencer | 740857f | 2010-12-21 16:45:57 +0000 | [diff] [blame] | 54 | #include "llvm/Support/FileSystem.h" |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 55 | #include "llvm/Support/MemoryBuffer.h" |
Justin Bogner | bb094f0 | 2014-04-18 19:57:06 +0000 | [diff] [blame] | 56 | #include "llvm/Support/OnDiskHashTable.h" |
Michael J. Spencer | 8aaf499 | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 57 | #include "llvm/Support/Path.h" |
Ben Langmuir | 487ea14 | 2014-10-23 18:05:36 +0000 | [diff] [blame] | 58 | #include "llvm/Support/Process.h" |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 59 | #include <algorithm> |
Chris Lattner | 225dd6c | 2009-04-11 18:40:46 +0000 | [diff] [blame] | 60 | #include <cstdio> |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 61 | #include <string.h> |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 62 | #include <utility> |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 63 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 64 | using namespace clang; |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 65 | using namespace clang::serialization; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 66 | |
Sebastian Redl | 3df5a08 | 2010-07-30 17:03:48 +0000 | [diff] [blame] | 67 | template <typename T, typename Allocator> |
Reid Kleckner | 012665e | 2015-05-21 00:13:09 +0000 | [diff] [blame] | 68 | static StringRef bytes(const std::vector<T, Allocator> &v) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 69 | if (v.empty()) return StringRef(); |
| 70 | return StringRef(reinterpret_cast<const char*>(&v[0]), |
Benjamin Kramer | d47a12a | 2011-04-24 17:44:50 +0000 | [diff] [blame] | 71 | sizeof(T) * v.size()); |
Sebastian Redl | 3df5a08 | 2010-07-30 17:03:48 +0000 | [diff] [blame] | 72 | } |
Benjamin Kramer | d47a12a | 2011-04-24 17:44:50 +0000 | [diff] [blame] | 73 | |
| 74 | template <typename T> |
Reid Kleckner | 012665e | 2015-05-21 00:13:09 +0000 | [diff] [blame] | 75 | static StringRef bytes(const SmallVectorImpl<T> &v) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 76 | return StringRef(reinterpret_cast<const char*>(v.data()), |
Benjamin Kramer | d47a12a | 2011-04-24 17:44:50 +0000 | [diff] [blame] | 77 | sizeof(T) * v.size()); |
Sebastian Redl | 3df5a08 | 2010-07-30 17:03:48 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 80 | //===----------------------------------------------------------------------===// |
| 81 | // Type serialization |
| 82 | //===----------------------------------------------------------------------===// |
Chris Lattner | 7099dbc | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 83 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 84 | namespace { |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 85 | class ASTTypeWriter { |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 86 | ASTWriter &Writer; |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 87 | ASTWriter::RecordDataImpl &Record; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 88 | |
| 89 | public: |
| 90 | /// \brief Type code that corresponds to the record generated. |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 91 | TypeCode Code; |
Richard Smith | 01b2cb4 | 2014-07-26 06:37:51 +0000 | [diff] [blame] | 92 | /// \brief Abbreviation to use for the record, if any. |
| 93 | unsigned AbbrevToUse; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 94 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 95 | ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 96 | : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 97 | |
| 98 | void VisitArrayType(const ArrayType *T); |
| 99 | void VisitFunctionType(const FunctionType *T); |
| 100 | void VisitTagType(const TagType *T); |
| 101 | |
| 102 | #define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T); |
| 103 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 104 | #include "clang/AST/TypeNodes.def" |
| 105 | }; |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 106 | } // end anonymous namespace |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 107 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 108 | void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 109 | llvm_unreachable("Built-in types are never serialized"); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 112 | void ASTTypeWriter::VisitComplexType(const ComplexType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 113 | Writer.AddTypeRef(T->getElementType(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 114 | Code = TYPE_COMPLEX; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 117 | void ASTTypeWriter::VisitPointerType(const PointerType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 118 | Writer.AddTypeRef(T->getPointeeType(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 119 | Code = TYPE_POINTER; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 122 | void ASTTypeWriter::VisitDecayedType(const DecayedType *T) { |
| 123 | Writer.AddTypeRef(T->getOriginalType(), Record); |
| 124 | Code = TYPE_DECAYED; |
| 125 | } |
| 126 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 127 | void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) { |
| 128 | Writer.AddTypeRef(T->getOriginalType(), Record); |
| 129 | Writer.AddTypeRef(T->getAdjustedType(), Record); |
| 130 | Code = TYPE_ADJUSTED; |
| 131 | } |
| 132 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 133 | void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | Writer.AddTypeRef(T->getPointeeType(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 135 | Code = TYPE_BLOCK_POINTER; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 138 | void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) { |
Richard Smith | 0f53846 | 2011-04-12 10:38:03 +0000 | [diff] [blame] | 139 | Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record); |
| 140 | Record.push_back(T->isSpelledAsLValue()); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 141 | Code = TYPE_LVALUE_REFERENCE; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 144 | void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) { |
Richard Smith | 0f53846 | 2011-04-12 10:38:03 +0000 | [diff] [blame] | 145 | Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 146 | Code = TYPE_RVALUE_REFERENCE; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 149 | void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 151 | Writer.AddTypeRef(QualType(T->getClass(), 0), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 152 | Code = TYPE_MEMBER_POINTER; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 155 | void ASTTypeWriter::VisitArrayType(const ArrayType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 156 | Writer.AddTypeRef(T->getElementType(), Record); |
| 157 | Record.push_back(T->getSizeModifier()); // FIXME: stable values |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 158 | Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 161 | void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 162 | VisitArrayType(T); |
| 163 | Writer.AddAPInt(T->getSize(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 164 | Code = TYPE_CONSTANT_ARRAY; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 167 | void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 168 | VisitArrayType(T); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 169 | Code = TYPE_INCOMPLETE_ARRAY; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 172 | void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 173 | VisitArrayType(T); |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 174 | Writer.AddSourceLocation(T->getLBracketLoc(), Record); |
| 175 | Writer.AddSourceLocation(T->getRBracketLoc(), Record); |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 176 | Writer.AddStmt(T->getSizeExpr()); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 177 | Code = TYPE_VARIABLE_ARRAY; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 180 | void ASTTypeWriter::VisitVectorType(const VectorType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 181 | Writer.AddTypeRef(T->getElementType(), Record); |
| 182 | Record.push_back(T->getNumElements()); |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 183 | Record.push_back(T->getVectorKind()); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 184 | Code = TYPE_VECTOR; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 187 | void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 188 | VisitVectorType(T); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 189 | Code = TYPE_EXT_VECTOR; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 192 | void ASTTypeWriter::VisitFunctionType(const FunctionType *T) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 193 | Writer.AddTypeRef(T->getReturnType(), Record); |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 194 | FunctionType::ExtInfo C = T->getExtInfo(); |
| 195 | Record.push_back(C.getNoReturn()); |
Eli Friedman | c5b20b5 | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 196 | Record.push_back(C.getHasRegParm()); |
Rafael Espindola | 49b85ab | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 197 | Record.push_back(C.getRegParm()); |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 198 | // FIXME: need to stabilize encoding of calling convention... |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 199 | Record.push_back(C.getCC()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 200 | Record.push_back(C.getProducesResult()); |
Richard Smith | 01b2cb4 | 2014-07-26 06:37:51 +0000 | [diff] [blame] | 201 | |
| 202 | if (C.getHasRegParm() || C.getRegParm() || C.getProducesResult()) |
| 203 | AbbrevToUse = 0; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 206 | void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 207 | VisitFunctionType(T); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 208 | Code = TYPE_FUNCTION_NO_PROTO; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Richard Smith | 564417a | 2014-03-20 21:47:22 +0000 | [diff] [blame] | 211 | static void addExceptionSpec(ASTWriter &Writer, const FunctionProtoType *T, |
| 212 | ASTWriter::RecordDataImpl &Record) { |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 213 | Record.push_back(T->getExceptionSpecType()); |
| 214 | if (T->getExceptionSpecType() == EST_Dynamic) { |
| 215 | Record.push_back(T->getNumExceptions()); |
| 216 | for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I) |
| 217 | Writer.AddTypeRef(T->getExceptionType(I), Record); |
| 218 | } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) { |
| 219 | Writer.AddStmt(T->getNoexceptExpr()); |
Richard Smith | 8b987a9 | 2012-04-21 17:47:47 +0000 | [diff] [blame] | 220 | } else if (T->getExceptionSpecType() == EST_Uninstantiated) { |
| 221 | Writer.AddDeclRef(T->getExceptionSpecDecl(), Record); |
| 222 | Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record); |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 223 | } else if (T->getExceptionSpecType() == EST_Unevaluated) { |
| 224 | Writer.AddDeclRef(T->getExceptionSpecDecl(), Record); |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 225 | } |
Richard Smith | 564417a | 2014-03-20 21:47:22 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) { |
| 229 | VisitFunctionType(T); |
Richard Smith | 01b2cb4 | 2014-07-26 06:37:51 +0000 | [diff] [blame] | 230 | |
Richard Smith | 564417a | 2014-03-20 21:47:22 +0000 | [diff] [blame] | 231 | Record.push_back(T->isVariadic()); |
| 232 | Record.push_back(T->hasTrailingReturn()); |
| 233 | Record.push_back(T->getTypeQuals()); |
| 234 | Record.push_back(static_cast<unsigned>(T->getRefQualifier())); |
| 235 | addExceptionSpec(Writer, T, Record); |
Richard Smith | 01b2cb4 | 2014-07-26 06:37:51 +0000 | [diff] [blame] | 236 | |
| 237 | Record.push_back(T->getNumParams()); |
| 238 | for (unsigned I = 0, N = T->getNumParams(); I != N; ++I) |
| 239 | Writer.AddTypeRef(T->getParamType(I), Record); |
| 240 | |
| 241 | if (T->isVariadic() || T->hasTrailingReturn() || T->getTypeQuals() || |
| 242 | T->getRefQualifier() || T->getExceptionSpecType() != EST_None) |
| 243 | AbbrevToUse = 0; |
| 244 | |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 245 | Code = TYPE_FUNCTION_PROTO; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 248 | void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) { |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 249 | Writer.AddDeclRef(T->getDecl(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 250 | Code = TYPE_UNRESOLVED_USING; |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 251 | } |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 252 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 253 | void ASTTypeWriter::VisitTypedefType(const TypedefType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 254 | Writer.AddDeclRef(T->getDecl(), Record); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 255 | assert(!T->isCanonicalUnqualified() && "Invalid typedef ?"); |
| 256 | Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 257 | Code = TYPE_TYPEDEF; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 260 | void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) { |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 261 | Writer.AddStmt(T->getUnderlyingExpr()); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 262 | Code = TYPE_TYPEOF_EXPR; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 265 | void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 266 | Writer.AddTypeRef(T->getUnderlyingType(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 267 | Code = TYPE_TYPEOF; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 270 | void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) { |
Douglas Gregor | 81495f3 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 271 | Writer.AddTypeRef(T->getUnderlyingType(), Record); |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 272 | Writer.AddStmt(T->getUnderlyingExpr()); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 273 | Code = TYPE_DECLTYPE; |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 276 | void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) { |
| 277 | Writer.AddTypeRef(T->getBaseType(), Record); |
| 278 | Writer.AddTypeRef(T->getUnderlyingType(), Record); |
| 279 | Record.push_back(T->getUTTKind()); |
| 280 | Code = TYPE_UNARY_TRANSFORM; |
| 281 | } |
| 282 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 283 | void ASTTypeWriter::VisitAutoType(const AutoType *T) { |
| 284 | Writer.AddTypeRef(T->getDeducedType(), Record); |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 285 | Record.push_back((unsigned)T->getKeyword()); |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 286 | if (T->getDeducedType().isNull()) |
| 287 | Record.push_back(T->isDependentType()); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 288 | Code = TYPE_AUTO; |
| 289 | } |
| 290 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 291 | void ASTTypeWriter::VisitTagType(const TagType *T) { |
Argyrios Kyrtzidis | a4ed181 | 2010-07-08 13:09:53 +0000 | [diff] [blame] | 292 | Record.push_back(T->isDependentType()); |
Douglas Gregor | f3bccd7 | 2012-01-17 19:21:53 +0000 | [diff] [blame] | 293 | Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | assert(!T->isBeingDefined() && |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 295 | "Cannot serialize in the middle of a type definition"); |
| 296 | } |
| 297 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 298 | void ASTTypeWriter::VisitRecordType(const RecordType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 299 | VisitTagType(T); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 300 | Code = TYPE_RECORD; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 303 | void ASTTypeWriter::VisitEnumType(const EnumType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 304 | VisitTagType(T); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 305 | Code = TYPE_ENUM; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 306 | } |
| 307 | |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 308 | void ASTTypeWriter::VisitAttributedType(const AttributedType *T) { |
| 309 | Writer.AddTypeRef(T->getModifiedType(), Record); |
| 310 | Writer.AddTypeRef(T->getEquivalentType(), Record); |
| 311 | Record.push_back(T->getAttrKind()); |
| 312 | Code = TYPE_ATTRIBUTED; |
| 313 | } |
| 314 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 315 | void |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 316 | ASTTypeWriter::VisitSubstTemplateTypeParmType( |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 317 | const SubstTemplateTypeParmType *T) { |
| 318 | Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record); |
| 319 | Writer.AddTypeRef(T->getReplacementType(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 320 | Code = TYPE_SUBST_TEMPLATE_TYPE_PARM; |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 324 | ASTTypeWriter::VisitSubstTemplateTypeParmPackType( |
| 325 | const SubstTemplateTypeParmPackType *T) { |
| 326 | Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record); |
| 327 | Writer.AddTemplateArgument(T->getArgumentPack(), Record); |
| 328 | Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK; |
| 329 | } |
| 330 | |
| 331 | void |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 332 | ASTTypeWriter::VisitTemplateSpecializationType( |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 333 | const TemplateSpecializationType *T) { |
Argyrios Kyrtzidis | a4ed181 | 2010-07-08 13:09:53 +0000 | [diff] [blame] | 334 | Record.push_back(T->isDependentType()); |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 335 | Writer.AddTemplateName(T->getTemplateName(), Record); |
| 336 | Record.push_back(T->getNumArgs()); |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 337 | for (const auto &ArgI : *T) |
| 338 | Writer.AddTemplateArgument(ArgI, Record); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 339 | Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() : |
| 340 | T->isCanonicalUnqualified() ? QualType() |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 341 | : T->getCanonicalTypeInternal(), |
| 342 | Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 343 | Code = TYPE_TEMPLATE_SPECIALIZATION; |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | void |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 347 | ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) { |
Argyrios Kyrtzidis | 4a57bd0 | 2010-06-30 08:49:25 +0000 | [diff] [blame] | 348 | VisitArrayType(T); |
| 349 | Writer.AddStmt(T->getSizeExpr()); |
| 350 | Writer.AddSourceRange(T->getBracketsRange(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 351 | Code = TYPE_DEPENDENT_SIZED_ARRAY; |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | void |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 355 | ASTTypeWriter::VisitDependentSizedExtVectorType( |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 356 | const DependentSizedExtVectorType *T) { |
| 357 | // FIXME: Serialize this type (C++ only) |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 358 | llvm_unreachable("Cannot serialize dependent sized extended vector types"); |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | void |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 362 | ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 363 | Record.push_back(T->getDepth()); |
| 364 | Record.push_back(T->getIndex()); |
| 365 | Record.push_back(T->isParameterPack()); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 366 | Writer.AddDeclRef(T->getDecl(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 367 | Code = TYPE_TEMPLATE_TYPE_PARM; |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | void |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 371 | ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) { |
Argyrios Kyrtzidis | bfcacee | 2010-06-24 08:57:31 +0000 | [diff] [blame] | 372 | Record.push_back(T->getKeyword()); |
| 373 | Writer.AddNestedNameSpecifier(T->getQualifier(), Record); |
| 374 | Writer.AddIdentifierRef(T->getIdentifier(), Record); |
Argyrios Kyrtzidis | e929095 | 2010-07-02 11:55:24 +0000 | [diff] [blame] | 375 | Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType() |
| 376 | : T->getCanonicalTypeInternal(), |
| 377 | Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 378 | Code = TYPE_DEPENDENT_NAME; |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | void |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 382 | ASTTypeWriter::VisitDependentTemplateSpecializationType( |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 383 | const DependentTemplateSpecializationType *T) { |
Argyrios Kyrtzidis | f0f7a79 | 2010-06-25 16:24:58 +0000 | [diff] [blame] | 384 | Record.push_back(T->getKeyword()); |
| 385 | Writer.AddNestedNameSpecifier(T->getQualifier(), Record); |
| 386 | Writer.AddIdentifierRef(T->getIdentifier(), Record); |
| 387 | Record.push_back(T->getNumArgs()); |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 388 | for (const auto &I : *T) |
| 389 | Writer.AddTemplateArgument(I, Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 390 | Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 393 | void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) { |
| 394 | Writer.AddTypeRef(T->getPattern(), Record); |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 395 | if (Optional<unsigned> NumExpansions = T->getNumExpansions()) |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 396 | Record.push_back(*NumExpansions + 1); |
| 397 | else |
| 398 | Record.push_back(0); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 399 | Code = TYPE_PACK_EXPANSION; |
| 400 | } |
| 401 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 402 | void ASTTypeWriter::VisitParenType(const ParenType *T) { |
| 403 | Writer.AddTypeRef(T->getInnerType(), Record); |
| 404 | Code = TYPE_PAREN; |
| 405 | } |
| 406 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 407 | void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 408 | Record.push_back(T->getKeyword()); |
Argyrios Kyrtzidis | f0f7a79 | 2010-06-25 16:24:58 +0000 | [diff] [blame] | 409 | Writer.AddNestedNameSpecifier(T->getQualifier(), Record); |
| 410 | Writer.AddTypeRef(T->getNamedType(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 411 | Code = TYPE_ELABORATED; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 414 | void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) { |
Douglas Gregor | 9f21889 | 2012-03-26 15:52:37 +0000 | [diff] [blame] | 415 | Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 416 | Writer.AddTypeRef(T->getInjectedSpecializationType(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 417 | Code = TYPE_INJECTED_CLASS_NAME; |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 420 | void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { |
Douglas Gregor | f3bccd7 | 2012-01-17 19:21:53 +0000 | [diff] [blame] | 421 | Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 422 | Code = TYPE_OBJC_INTERFACE; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 425 | void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 426 | Writer.AddTypeRef(T->getBaseType(), Record); |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 427 | Record.push_back(T->getTypeArgsAsWritten().size()); |
| 428 | for (auto TypeArg : T->getTypeArgsAsWritten()) |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 429 | Writer.AddTypeRef(TypeArg, Record); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 430 | Record.push_back(T->getNumProtocols()); |
Aaron Ballman | 1683f7b | 2014-03-17 15:55:30 +0000 | [diff] [blame] | 431 | for (const auto *I : T->quals()) |
| 432 | Writer.AddDeclRef(I, Record); |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 433 | Record.push_back(T->isKindOfTypeAsWritten()); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 434 | Code = TYPE_OBJC_OBJECT; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 437 | void |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 438 | ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 439 | Writer.AddTypeRef(T->getPointeeType(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 440 | Code = TYPE_OBJC_OBJECT_POINTER; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 443 | void |
| 444 | ASTTypeWriter::VisitAtomicType(const AtomicType *T) { |
| 445 | Writer.AddTypeRef(T->getValueType(), Record); |
| 446 | Code = TYPE_ATOMIC; |
| 447 | } |
| 448 | |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 449 | namespace { |
| 450 | |
| 451 | class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> { |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 452 | ASTWriter &Writer; |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 453 | ASTWriter::RecordDataImpl &Record; |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 454 | |
| 455 | public: |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 456 | TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record) |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 457 | : Writer(Writer), Record(Record) { } |
| 458 | |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 459 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 460 | #define TYPELOC(CLASS, PARENT) \ |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 461 | void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 462 | #include "clang/AST/TypeLocNodes.def" |
| 463 | |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 464 | void VisitArrayTypeLoc(ArrayTypeLoc TyLoc); |
| 465 | void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 466 | }; |
| 467 | |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 468 | } // end anonymous namespace |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 469 | |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 470 | void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
| 471 | // nothing to do |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 472 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 473 | void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
Douglas Gregor | c9b7a59 | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 474 | Writer.AddSourceLocation(TL.getBuiltinLoc(), Record); |
| 475 | if (TL.needsExtraLocalData()) { |
| 476 | Record.push_back(TL.getWrittenTypeSpec()); |
| 477 | Record.push_back(TL.getWrittenSignSpec()); |
| 478 | Record.push_back(TL.getWrittenWidthSpec()); |
| 479 | Record.push_back(TL.hasModeAttr()); |
| 480 | } |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 481 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 482 | void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) { |
| 483 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 484 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 485 | void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 486 | Writer.AddSourceLocation(TL.getStarLoc(), Record); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 487 | } |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 488 | void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) { |
| 489 | // nothing to do |
| 490 | } |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 491 | void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { |
| 492 | // nothing to do |
| 493 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 494 | void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 495 | Writer.AddSourceLocation(TL.getCaretLoc(), Record); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 496 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 497 | void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
| 498 | Writer.AddSourceLocation(TL.getAmpLoc(), Record); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 499 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 500 | void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
| 501 | Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 502 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 503 | void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 504 | Writer.AddSourceLocation(TL.getStarLoc(), Record); |
Abramo Bagnara | 50935784 | 2011-03-05 14:42:21 +0000 | [diff] [blame] | 505 | Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 506 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 507 | void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 508 | Writer.AddSourceLocation(TL.getLBracketLoc(), Record); |
| 509 | Writer.AddSourceLocation(TL.getRBracketLoc(), Record); |
| 510 | Record.push_back(TL.getSizeExpr() ? 1 : 0); |
| 511 | if (TL.getSizeExpr()) |
| 512 | Writer.AddStmt(TL.getSizeExpr()); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 513 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 514 | void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { |
| 515 | VisitArrayTypeLoc(TL); |
| 516 | } |
| 517 | void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { |
| 518 | VisitArrayTypeLoc(TL); |
| 519 | } |
| 520 | void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { |
| 521 | VisitArrayTypeLoc(TL); |
| 522 | } |
| 523 | void TypeLocWriter::VisitDependentSizedArrayTypeLoc( |
| 524 | DependentSizedArrayTypeLoc TL) { |
| 525 | VisitArrayTypeLoc(TL); |
| 526 | } |
| 527 | void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc( |
| 528 | DependentSizedExtVectorTypeLoc TL) { |
| 529 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 530 | } |
| 531 | void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) { |
| 532 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 533 | } |
| 534 | void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { |
| 535 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 536 | } |
| 537 | void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) { |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 538 | Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record); |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 539 | Writer.AddSourceLocation(TL.getLParenLoc(), Record); |
| 540 | Writer.AddSourceLocation(TL.getRParenLoc(), Record); |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 541 | Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record); |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 542 | for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) |
| 543 | Writer.AddDeclRef(TL.getParam(i), Record); |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 544 | } |
| 545 | void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { |
| 546 | VisitFunctionTypeLoc(TL); |
| 547 | } |
| 548 | void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { |
| 549 | VisitFunctionTypeLoc(TL); |
| 550 | } |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 551 | void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { |
| 552 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 553 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 554 | void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
| 555 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 556 | } |
| 557 | void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
John McCall | e859503 | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 558 | Writer.AddSourceLocation(TL.getTypeofLoc(), Record); |
| 559 | Writer.AddSourceLocation(TL.getLParenLoc(), Record); |
| 560 | Writer.AddSourceLocation(TL.getRParenLoc(), Record); |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 561 | } |
| 562 | void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
John McCall | e859503 | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 563 | Writer.AddSourceLocation(TL.getTypeofLoc(), Record); |
| 564 | Writer.AddSourceLocation(TL.getLParenLoc(), Record); |
| 565 | Writer.AddSourceLocation(TL.getRParenLoc(), Record); |
| 566 | Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record); |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 567 | } |
| 568 | void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { |
| 569 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 570 | } |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 571 | void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { |
| 572 | Writer.AddSourceLocation(TL.getKWLoc(), Record); |
| 573 | Writer.AddSourceLocation(TL.getLParenLoc(), Record); |
| 574 | Writer.AddSourceLocation(TL.getRParenLoc(), Record); |
| 575 | Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record); |
| 576 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 577 | void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) { |
| 578 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 579 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 580 | void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) { |
| 581 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 582 | } |
| 583 | void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) { |
| 584 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 585 | } |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 586 | void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) { |
| 587 | Writer.AddSourceLocation(TL.getAttrNameLoc(), Record); |
| 588 | if (TL.hasAttrOperand()) { |
| 589 | SourceRange range = TL.getAttrOperandParensRange(); |
| 590 | Writer.AddSourceLocation(range.getBegin(), Record); |
| 591 | Writer.AddSourceLocation(range.getEnd(), Record); |
| 592 | } |
| 593 | if (TL.hasAttrExprOperand()) { |
| 594 | Expr *operand = TL.getAttrExprOperand(); |
| 595 | Record.push_back(operand ? 1 : 0); |
| 596 | if (operand) Writer.AddStmt(operand); |
| 597 | } else if (TL.hasAttrEnumOperand()) { |
| 598 | Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record); |
| 599 | } |
| 600 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 601 | void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 602 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 603 | } |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 604 | void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc( |
| 605 | SubstTemplateTypeParmTypeLoc TL) { |
| 606 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 607 | } |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 608 | void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc( |
| 609 | SubstTemplateTypeParmPackTypeLoc TL) { |
| 610 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 611 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 612 | void TypeLocWriter::VisitTemplateSpecializationTypeLoc( |
| 613 | TemplateSpecializationTypeLoc TL) { |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 614 | Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 615 | Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record); |
| 616 | Writer.AddSourceLocation(TL.getLAngleLoc(), Record); |
| 617 | Writer.AddSourceLocation(TL.getRAngleLoc(), Record); |
| 618 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
Argyrios Kyrtzidis | ae85e24 | 2010-06-22 09:54:59 +0000 | [diff] [blame] | 619 | Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(), |
| 620 | TL.getArgLoc(i).getLocInfo(), Record); |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 621 | } |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 622 | void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) { |
| 623 | Writer.AddSourceLocation(TL.getLParenLoc(), Record); |
| 624 | Writer.AddSourceLocation(TL.getRParenLoc(), Record); |
| 625 | } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 626 | void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 627 | Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 628 | Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record); |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 629 | } |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 630 | void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { |
| 631 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 632 | } |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 633 | void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 634 | Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record); |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 635 | Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record); |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 636 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 637 | } |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 638 | void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc( |
| 639 | DependentTemplateSpecializationTypeLoc TL) { |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 640 | Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record); |
Douglas Gregor | a7a795b | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 641 | Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 642 | Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 643 | Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 644 | Writer.AddSourceLocation(TL.getLAngleLoc(), Record); |
| 645 | Writer.AddSourceLocation(TL.getRAngleLoc(), Record); |
| 646 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) |
Argyrios Kyrtzidis | ae85e24 | 2010-06-22 09:54:59 +0000 | [diff] [blame] | 647 | Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(), |
| 648 | TL.getArgLoc(I).getLocInfo(), Record); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 649 | } |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 650 | void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { |
| 651 | Writer.AddSourceLocation(TL.getEllipsisLoc(), Record); |
| 652 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 653 | void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
| 654 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 655 | } |
| 656 | void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { |
| 657 | Record.push_back(TL.hasBaseTypeAsWritten()); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 658 | Writer.AddSourceLocation(TL.getTypeArgsLAngleLoc(), Record); |
| 659 | Writer.AddSourceLocation(TL.getTypeArgsRAngleLoc(), Record); |
| 660 | for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i) |
| 661 | Writer.AddTypeSourceInfo(TL.getTypeArgTInfo(i), Record); |
| 662 | Writer.AddSourceLocation(TL.getProtocolLAngleLoc(), Record); |
| 663 | Writer.AddSourceLocation(TL.getProtocolRAngleLoc(), Record); |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 664 | for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) |
| 665 | Writer.AddSourceLocation(TL.getProtocolLoc(i), Record); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 666 | } |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 667 | void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
| 668 | Writer.AddSourceLocation(TL.getStarLoc(), Record); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 669 | } |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 670 | void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) { |
| 671 | Writer.AddSourceLocation(TL.getKWLoc(), Record); |
| 672 | Writer.AddSourceLocation(TL.getLParenLoc(), Record); |
| 673 | Writer.AddSourceLocation(TL.getRParenLoc(), Record); |
| 674 | } |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 675 | |
Richard Smith | 01b2cb4 | 2014-07-26 06:37:51 +0000 | [diff] [blame] | 676 | void ASTWriter::WriteTypeAbbrevs() { |
| 677 | using namespace llvm; |
| 678 | |
| 679 | BitCodeAbbrev *Abv; |
| 680 | |
| 681 | // Abbreviation for TYPE_EXT_QUAL |
| 682 | Abv = new BitCodeAbbrev(); |
| 683 | Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL)); |
| 684 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type |
| 685 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals |
| 686 | TypeExtQualAbbrev = Stream.EmitAbbrev(Abv); |
| 687 | |
| 688 | // Abbreviation for TYPE_FUNCTION_PROTO |
| 689 | Abv = new BitCodeAbbrev(); |
| 690 | Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO)); |
| 691 | // FunctionType |
| 692 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType |
| 693 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn |
| 694 | Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm |
| 695 | Abv->Add(BitCodeAbbrevOp(0)); // RegParm |
| 696 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC |
| 697 | Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult |
| 698 | // FunctionProtoType |
| 699 | Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic |
| 700 | Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn |
| 701 | Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals |
| 702 | Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier |
| 703 | Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec |
| 704 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams |
| 705 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
| 706 | Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params |
| 707 | TypeFunctionProtoAbbrev = Stream.EmitAbbrev(Abv); |
| 708 | } |
| 709 | |
Chris Lattner | 19cea4e | 2009-04-22 05:57:30 +0000 | [diff] [blame] | 710 | //===----------------------------------------------------------------------===// |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 711 | // ASTWriter Implementation |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 712 | //===----------------------------------------------------------------------===// |
| 713 | |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 714 | static void EmitBlockID(unsigned ID, const char *Name, |
| 715 | llvm::BitstreamWriter &Stream, |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 716 | ASTWriter::RecordDataImpl &Record) { |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 717 | Record.clear(); |
| 718 | Record.push_back(ID); |
| 719 | Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record); |
| 720 | |
| 721 | // Emit the block name if present. |
Craig Topper | a13603a | 2014-05-22 05:54:18 +0000 | [diff] [blame] | 722 | if (!Name || Name[0] == 0) |
| 723 | return; |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 724 | Record.clear(); |
| 725 | while (*Name) |
| 726 | Record.push_back(*Name++); |
| 727 | Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record); |
| 728 | } |
| 729 | |
| 730 | static void EmitRecordID(unsigned ID, const char *Name, |
| 731 | llvm::BitstreamWriter &Stream, |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 732 | ASTWriter::RecordDataImpl &Record) { |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 733 | Record.clear(); |
| 734 | Record.push_back(ID); |
| 735 | while (*Name) |
| 736 | Record.push_back(*Name++); |
| 737 | Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record); |
Chris Lattner | ccac3a6 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | static void AddStmtsExprs(llvm::BitstreamWriter &Stream, |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 741 | ASTWriter::RecordDataImpl &Record) { |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 742 | #define RECORD(X) EmitRecordID(X, #X, Stream, Record) |
Chris Lattner | ccac3a6 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 743 | RECORD(STMT_STOP); |
| 744 | RECORD(STMT_NULL_PTR); |
Richard Smith | 01b2cb4 | 2014-07-26 06:37:51 +0000 | [diff] [blame] | 745 | RECORD(STMT_REF_PTR); |
Chris Lattner | ccac3a6 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 746 | RECORD(STMT_NULL); |
| 747 | RECORD(STMT_COMPOUND); |
| 748 | RECORD(STMT_CASE); |
| 749 | RECORD(STMT_DEFAULT); |
| 750 | RECORD(STMT_LABEL); |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 751 | RECORD(STMT_ATTRIBUTED); |
Chris Lattner | ccac3a6 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 752 | RECORD(STMT_IF); |
| 753 | RECORD(STMT_SWITCH); |
| 754 | RECORD(STMT_WHILE); |
| 755 | RECORD(STMT_DO); |
| 756 | RECORD(STMT_FOR); |
| 757 | RECORD(STMT_GOTO); |
| 758 | RECORD(STMT_INDIRECT_GOTO); |
| 759 | RECORD(STMT_CONTINUE); |
| 760 | RECORD(STMT_BREAK); |
| 761 | RECORD(STMT_RETURN); |
| 762 | RECORD(STMT_DECL); |
Chad Rosier | de70e0e | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 763 | RECORD(STMT_GCCASM); |
Chad Rosier | e30d499 | 2012-08-24 23:51:02 +0000 | [diff] [blame] | 764 | RECORD(STMT_MSASM); |
Chris Lattner | ccac3a6 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 765 | RECORD(EXPR_PREDEFINED); |
| 766 | RECORD(EXPR_DECL_REF); |
| 767 | RECORD(EXPR_INTEGER_LITERAL); |
| 768 | RECORD(EXPR_FLOATING_LITERAL); |
| 769 | RECORD(EXPR_IMAGINARY_LITERAL); |
| 770 | RECORD(EXPR_STRING_LITERAL); |
| 771 | RECORD(EXPR_CHARACTER_LITERAL); |
| 772 | RECORD(EXPR_PAREN); |
Richard Smith | f1b4b8b | 2014-07-27 04:29:04 +0000 | [diff] [blame] | 773 | RECORD(EXPR_PAREN_LIST); |
Chris Lattner | ccac3a6 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 774 | RECORD(EXPR_UNARY_OPERATOR); |
| 775 | RECORD(EXPR_SIZEOF_ALIGN_OF); |
| 776 | RECORD(EXPR_ARRAY_SUBSCRIPT); |
| 777 | RECORD(EXPR_CALL); |
| 778 | RECORD(EXPR_MEMBER); |
| 779 | RECORD(EXPR_BINARY_OPERATOR); |
| 780 | RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR); |
| 781 | RECORD(EXPR_CONDITIONAL_OPERATOR); |
| 782 | RECORD(EXPR_IMPLICIT_CAST); |
| 783 | RECORD(EXPR_CSTYLE_CAST); |
| 784 | RECORD(EXPR_COMPOUND_LITERAL); |
| 785 | RECORD(EXPR_EXT_VECTOR_ELEMENT); |
| 786 | RECORD(EXPR_INIT_LIST); |
| 787 | RECORD(EXPR_DESIGNATED_INIT); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 788 | RECORD(EXPR_DESIGNATED_INIT_UPDATE); |
Chris Lattner | ccac3a6 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 789 | RECORD(EXPR_IMPLICIT_VALUE_INIT); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 790 | RECORD(EXPR_NO_INIT); |
Chris Lattner | ccac3a6 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 791 | RECORD(EXPR_VA_ARG); |
| 792 | RECORD(EXPR_ADDR_LABEL); |
| 793 | RECORD(EXPR_STMT); |
Chris Lattner | ccac3a6 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 794 | RECORD(EXPR_CHOOSE); |
| 795 | RECORD(EXPR_GNU_NULL); |
| 796 | RECORD(EXPR_SHUFFLE_VECTOR); |
| 797 | RECORD(EXPR_BLOCK); |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 798 | RECORD(EXPR_GENERIC_SELECTION); |
Chris Lattner | ccac3a6 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 799 | RECORD(EXPR_OBJC_STRING_LITERAL); |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 800 | RECORD(EXPR_OBJC_BOXED_EXPRESSION); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 801 | RECORD(EXPR_OBJC_ARRAY_LITERAL); |
| 802 | RECORD(EXPR_OBJC_DICTIONARY_LITERAL); |
Chris Lattner | ccac3a6 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 803 | RECORD(EXPR_OBJC_ENCODE); |
| 804 | RECORD(EXPR_OBJC_SELECTOR_EXPR); |
| 805 | RECORD(EXPR_OBJC_PROTOCOL_EXPR); |
| 806 | RECORD(EXPR_OBJC_IVAR_REF_EXPR); |
| 807 | RECORD(EXPR_OBJC_PROPERTY_REF_EXPR); |
| 808 | RECORD(EXPR_OBJC_KVC_REF_EXPR); |
| 809 | RECORD(EXPR_OBJC_MESSAGE_EXPR); |
Chris Lattner | ccac3a6 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 810 | RECORD(STMT_OBJC_FOR_COLLECTION); |
| 811 | RECORD(STMT_OBJC_CATCH); |
| 812 | RECORD(STMT_OBJC_FINALLY); |
| 813 | RECORD(STMT_OBJC_AT_TRY); |
| 814 | RECORD(STMT_OBJC_AT_SYNCHRONIZED); |
| 815 | RECORD(STMT_OBJC_AT_THROW); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 816 | RECORD(EXPR_OBJC_BOOL_LITERAL); |
Richard Smith | 01b2cb4 | 2014-07-26 06:37:51 +0000 | [diff] [blame] | 817 | RECORD(STMT_CXX_CATCH); |
| 818 | RECORD(STMT_CXX_TRY); |
| 819 | RECORD(STMT_CXX_FOR_RANGE); |
Sam Weinig | e83b3ac | 2010-02-07 06:32:43 +0000 | [diff] [blame] | 820 | RECORD(EXPR_CXX_OPERATOR_CALL); |
Richard Smith | f1b4b8b | 2014-07-27 04:29:04 +0000 | [diff] [blame] | 821 | RECORD(EXPR_CXX_MEMBER_CALL); |
Sam Weinig | e83b3ac | 2010-02-07 06:32:43 +0000 | [diff] [blame] | 822 | RECORD(EXPR_CXX_CONSTRUCT); |
Richard Smith | f1b4b8b | 2014-07-27 04:29:04 +0000 | [diff] [blame] | 823 | RECORD(EXPR_CXX_TEMPORARY_OBJECT); |
Sam Weinig | e83b3ac | 2010-02-07 06:32:43 +0000 | [diff] [blame] | 824 | RECORD(EXPR_CXX_STATIC_CAST); |
| 825 | RECORD(EXPR_CXX_DYNAMIC_CAST); |
| 826 | RECORD(EXPR_CXX_REINTERPRET_CAST); |
| 827 | RECORD(EXPR_CXX_CONST_CAST); |
| 828 | RECORD(EXPR_CXX_FUNCTIONAL_CAST); |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 829 | RECORD(EXPR_USER_DEFINED_LITERAL); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 830 | RECORD(EXPR_CXX_STD_INITIALIZER_LIST); |
Sam Weinig | e83b3ac | 2010-02-07 06:32:43 +0000 | [diff] [blame] | 831 | RECORD(EXPR_CXX_BOOL_LITERAL); |
| 832 | RECORD(EXPR_CXX_NULL_PTR_LITERAL); |
Douglas Gregor | 0beaec0 | 2011-02-08 16:34:17 +0000 | [diff] [blame] | 833 | RECORD(EXPR_CXX_TYPEID_EXPR); |
| 834 | RECORD(EXPR_CXX_TYPEID_TYPE); |
Douglas Gregor | 0beaec0 | 2011-02-08 16:34:17 +0000 | [diff] [blame] | 835 | RECORD(EXPR_CXX_THIS); |
| 836 | RECORD(EXPR_CXX_THROW); |
| 837 | RECORD(EXPR_CXX_DEFAULT_ARG); |
Richard Smith | 01b2cb4 | 2014-07-26 06:37:51 +0000 | [diff] [blame] | 838 | RECORD(EXPR_CXX_DEFAULT_INIT); |
Douglas Gregor | 0beaec0 | 2011-02-08 16:34:17 +0000 | [diff] [blame] | 839 | RECORD(EXPR_CXX_BIND_TEMPORARY); |
| 840 | RECORD(EXPR_CXX_SCALAR_VALUE_INIT); |
| 841 | RECORD(EXPR_CXX_NEW); |
| 842 | RECORD(EXPR_CXX_DELETE); |
| 843 | RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR); |
| 844 | RECORD(EXPR_EXPR_WITH_CLEANUPS); |
| 845 | RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER); |
| 846 | RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF); |
| 847 | RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT); |
| 848 | RECORD(EXPR_CXX_UNRESOLVED_MEMBER); |
| 849 | RECORD(EXPR_CXX_UNRESOLVED_LOOKUP); |
Richard Smith | 01b2cb4 | 2014-07-26 06:37:51 +0000 | [diff] [blame] | 850 | RECORD(EXPR_CXX_EXPRESSION_TRAIT); |
Douglas Gregor | 0beaec0 | 2011-02-08 16:34:17 +0000 | [diff] [blame] | 851 | RECORD(EXPR_CXX_NOEXCEPT); |
| 852 | RECORD(EXPR_OPAQUE_VALUE); |
Richard Smith | 01b2cb4 | 2014-07-26 06:37:51 +0000 | [diff] [blame] | 853 | RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR); |
| 854 | RECORD(EXPR_TYPE_TRAIT); |
| 855 | RECORD(EXPR_ARRAY_TYPE_TRAIT); |
Douglas Gregor | 0beaec0 | 2011-02-08 16:34:17 +0000 | [diff] [blame] | 856 | RECORD(EXPR_PACK_EXPANSION); |
| 857 | RECORD(EXPR_SIZEOF_PACK); |
Richard Smith | 01b2cb4 | 2014-07-26 06:37:51 +0000 | [diff] [blame] | 858 | RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM); |
Douglas Gregor | 0beaec0 | 2011-02-08 16:34:17 +0000 | [diff] [blame] | 859 | RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK); |
Richard Smith | 01b2cb4 | 2014-07-26 06:37:51 +0000 | [diff] [blame] | 860 | RECORD(EXPR_FUNCTION_PARM_PACK); |
| 861 | RECORD(EXPR_MATERIALIZE_TEMPORARY); |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 862 | RECORD(EXPR_CUDA_KERNEL_CALL); |
Richard Smith | 01b2cb4 | 2014-07-26 06:37:51 +0000 | [diff] [blame] | 863 | RECORD(EXPR_CXX_UUIDOF_EXPR); |
| 864 | RECORD(EXPR_CXX_UUIDOF_TYPE); |
| 865 | RECORD(EXPR_LAMBDA); |
Chris Lattner | ccac3a6 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 866 | #undef RECORD |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 867 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 868 | |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 869 | void ASTWriter::WriteBlockInfoBlock() { |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 870 | RecordData Record; |
| 871 | Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 872 | |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 873 | #define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record) |
| 874 | #define RECORD(X) EmitRecordID(X, #X, Stream, Record) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 875 | |
Douglas Gregor | 0aa21c9 | 2012-10-18 18:27:37 +0000 | [diff] [blame] | 876 | // Control Block. |
| 877 | BLOCK(CONTROL_BLOCK); |
| 878 | RECORD(METADATA); |
Ben Langmuir | 487ea14 | 2014-10-23 18:05:36 +0000 | [diff] [blame] | 879 | RECORD(SIGNATURE); |
Ben Langmuir | beee15e | 2014-04-14 18:00:01 +0000 | [diff] [blame] | 880 | RECORD(MODULE_NAME); |
Richard Smith | fa71565 | 2015-08-31 22:43:10 +0000 | [diff] [blame] | 881 | RECORD(MODULE_DIRECTORY); |
Ben Langmuir | beee15e | 2014-04-14 18:00:01 +0000 | [diff] [blame] | 882 | RECORD(MODULE_MAP_FILE); |
Douglas Gregor | 0aa21c9 | 2012-10-18 18:27:37 +0000 | [diff] [blame] | 883 | RECORD(IMPORTS); |
Douglas Gregor | fad10d8 | 2012-10-18 18:36:53 +0000 | [diff] [blame] | 884 | RECORD(ORIGINAL_FILE); |
Douglas Gregor | 0aa21c9 | 2012-10-18 18:27:37 +0000 | [diff] [blame] | 885 | RECORD(ORIGINAL_PCH_DIR); |
Argyrios Kyrtzidis | 5259524 | 2012-11-15 18:57:27 +0000 | [diff] [blame] | 886 | RECORD(ORIGINAL_FILE_ID); |
Douglas Gregor | 3120d2c | 2012-10-22 18:42:04 +0000 | [diff] [blame] | 887 | RECORD(INPUT_FILE_OFFSETS); |
Richard Smith | 0516b18 | 2015-09-08 19:40:14 +0000 | [diff] [blame] | 888 | |
| 889 | BLOCK(OPTIONS_BLOCK); |
| 890 | RECORD(LANGUAGE_OPTIONS); |
| 891 | RECORD(TARGET_OPTIONS); |
Douglas Gregor | 8263ffb | 2012-10-24 15:17:15 +0000 | [diff] [blame] | 892 | RECORD(DIAGNOSTIC_OPTIONS); |
Douglas Gregor | c6317db | 2012-10-24 15:49:58 +0000 | [diff] [blame] | 893 | RECORD(FILE_SYSTEM_OPTIONS); |
Douglas Gregor | 2d30236 | 2012-10-24 16:50:34 +0000 | [diff] [blame] | 894 | RECORD(HEADER_SEARCH_OPTIONS); |
Douglas Gregor | b6af6c2 | 2012-10-24 20:05:57 +0000 | [diff] [blame] | 895 | RECORD(PREPROCESSOR_OPTIONS); |
| 896 | |
Douglas Gregor | 108cb22 | 2012-10-19 00:45:00 +0000 | [diff] [blame] | 897 | BLOCK(INPUT_FILES_BLOCK); |
| 898 | RECORD(INPUT_FILE); |
| 899 | |
Douglas Gregor | 0aa21c9 | 2012-10-18 18:27:37 +0000 | [diff] [blame] | 900 | // AST Top-Level Block. |
| 901 | BLOCK(AST_BLOCK); |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 902 | RECORD(TYPE_OFFSET); |
| 903 | RECORD(DECL_OFFSET); |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 904 | RECORD(IDENTIFIER_OFFSET); |
| 905 | RECORD(IDENTIFIER_TABLE); |
Ben Langmuir | 332aafe | 2014-01-31 01:06:56 +0000 | [diff] [blame] | 906 | RECORD(EAGERLY_DESERIALIZED_DECLS); |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 907 | RECORD(SPECIAL_TYPES); |
| 908 | RECORD(STATISTICS); |
| 909 | RECORD(TENTATIVE_DEFINITIONS); |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 910 | RECORD(SELECTOR_OFFSETS); |
| 911 | RECORD(METHOD_POOL); |
| 912 | RECORD(PP_COUNTER_VALUE); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 913 | RECORD(SOURCE_LOCATION_OFFSETS); |
| 914 | RECORD(SOURCE_LOCATION_PRELOADS); |
Douglas Gregor | 61cac2b | 2009-04-27 20:06:05 +0000 | [diff] [blame] | 915 | RECORD(EXT_VECTOR_DECLS); |
Richard Smith | fa71565 | 2015-08-31 22:43:10 +0000 | [diff] [blame] | 916 | RECORD(UNUSED_FILESCOPED_DECLS); |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 917 | RECORD(PPD_ENTITIES_OFFSETS); |
Richard Smith | fa71565 | 2015-08-31 22:43:10 +0000 | [diff] [blame] | 918 | RECORD(VTABLE_USES); |
Fariborz Jahanian | c51609a | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 919 | RECORD(REFERENCED_SELECTOR_POOL); |
Douglas Gregor | 0beaec0 | 2011-02-08 16:34:17 +0000 | [diff] [blame] | 920 | RECORD(TU_UPDATE_LEXICAL); |
Douglas Gregor | 0beaec0 | 2011-02-08 16:34:17 +0000 | [diff] [blame] | 921 | RECORD(SEMA_DECL_REFS); |
| 922 | RECORD(WEAK_UNDECLARED_IDENTIFIERS); |
| 923 | RECORD(PENDING_IMPLICIT_INSTANTIATIONS); |
| 924 | RECORD(DECL_REPLACEMENTS); |
| 925 | RECORD(UPDATE_VISIBLE); |
| 926 | RECORD(DECL_UPDATE_OFFSETS); |
| 927 | RECORD(DECL_UPDATES); |
| 928 | RECORD(CXX_BASE_SPECIFIER_OFFSETS); |
| 929 | RECORD(DIAG_PRAGMA_MAPPINGS); |
Peter Collingbourne | 5df20e0 | 2011-02-15 19:46:30 +0000 | [diff] [blame] | 930 | RECORD(CUDA_SPECIAL_DECL_REFS); |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 931 | RECORD(HEADER_SEARCH_TABLE); |
Peter Collingbourne | 5df20e0 | 2011-02-15 19:46:30 +0000 | [diff] [blame] | 932 | RECORD(FP_PRAGMA_OPTIONS); |
| 933 | RECORD(OPENCL_EXTENSIONS); |
Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 934 | RECORD(DELEGATING_CTORS); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 935 | RECORD(KNOWN_NAMESPACES); |
Douglas Gregor | 78d0b57 | 2011-08-04 16:39:39 +0000 | [diff] [blame] | 936 | RECORD(MODULE_OFFSET_MAP); |
| 937 | RECORD(SOURCE_MANAGER_LINE_TABLE); |
Douglas Gregor | 404cdde | 2012-01-27 01:47:08 +0000 | [diff] [blame] | 938 | RECORD(OBJC_CATEGORIES_MAP); |
Douglas Gregor | 66e4add | 2011-12-19 21:09:25 +0000 | [diff] [blame] | 939 | RECORD(FILE_SORTED_DECLS); |
| 940 | RECORD(IMPORTED_MODULES); |
Douglas Gregor | 404cdde | 2012-01-27 01:47:08 +0000 | [diff] [blame] | 941 | RECORD(OBJC_CATEGORIES); |
Douglas Gregor | cb28f9d | 2012-10-09 23:05:51 +0000 | [diff] [blame] | 942 | RECORD(MACRO_OFFSET); |
Richard Smith | fa71565 | 2015-08-31 22:43:10 +0000 | [diff] [blame] | 943 | RECORD(INTERESTING_IDENTIFIERS); |
| 944 | RECORD(UNDEFINED_BUT_USED); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 945 | RECORD(LATE_PARSED_TEMPLATE); |
Dario Domizioli | 13a0a38 | 2014-05-23 12:13:25 +0000 | [diff] [blame] | 946 | RECORD(OPTIMIZE_PRAGMA_OPTIONS); |
Richard Smith | fa71565 | 2015-08-31 22:43:10 +0000 | [diff] [blame] | 947 | RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES); |
| 948 | RECORD(CXX_CTOR_INITIALIZERS_OFFSETS); |
| 949 | RECORD(DELETE_EXPRS_TO_ANALYZE); |
Douglas Gregor | 358cd44 | 2012-01-15 16:58:34 +0000 | [diff] [blame] | 950 | |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 951 | // SourceManager Block. |
Chris Lattner | 6403198 | 2009-04-27 00:40:25 +0000 | [diff] [blame] | 952 | BLOCK(SOURCE_MANAGER_BLOCK); |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 953 | RECORD(SM_SLOC_FILE_ENTRY); |
| 954 | RECORD(SM_SLOC_BUFFER_ENTRY); |
| 955 | RECORD(SM_SLOC_BUFFER_BLOB); |
Chandler Carruth | f92ac9e | 2011-07-15 07:25:21 +0000 | [diff] [blame] | 956 | RECORD(SM_SLOC_EXPANSION_ENTRY); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 957 | |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 958 | // Preprocessor Block. |
Chris Lattner | 6403198 | 2009-04-27 00:40:25 +0000 | [diff] [blame] | 959 | BLOCK(PREPROCESSOR_BLOCK); |
Richard Smith | ec21650 | 2015-02-13 19:48:37 +0000 | [diff] [blame] | 960 | RECORD(PP_MACRO_DIRECTIVE_HISTORY); |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 961 | RECORD(PP_MACRO_FUNCTION_LIKE); |
Richard Smith | d732939 | 2015-04-21 21:46:32 +0000 | [diff] [blame] | 962 | RECORD(PP_MACRO_OBJECT_LIKE); |
| 963 | RECORD(PP_MODULE_MACRO); |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 964 | RECORD(PP_TOKEN); |
Richard Smith | ec21650 | 2015-02-13 19:48:37 +0000 | [diff] [blame] | 965 | |
Richard Smith | fa71565 | 2015-08-31 22:43:10 +0000 | [diff] [blame] | 966 | // Submodule Block. |
| 967 | BLOCK(SUBMODULE_BLOCK); |
| 968 | RECORD(SUBMODULE_METADATA); |
| 969 | RECORD(SUBMODULE_DEFINITION); |
| 970 | RECORD(SUBMODULE_UMBRELLA_HEADER); |
| 971 | RECORD(SUBMODULE_HEADER); |
| 972 | RECORD(SUBMODULE_TOPHEADER); |
| 973 | RECORD(SUBMODULE_UMBRELLA_DIR); |
| 974 | RECORD(SUBMODULE_IMPORTS); |
| 975 | RECORD(SUBMODULE_EXPORTS); |
| 976 | RECORD(SUBMODULE_REQUIRES); |
| 977 | RECORD(SUBMODULE_EXCLUDED_HEADER); |
| 978 | RECORD(SUBMODULE_LINK_LIBRARY); |
| 979 | RECORD(SUBMODULE_CONFIG_MACRO); |
| 980 | RECORD(SUBMODULE_CONFLICT); |
| 981 | RECORD(SUBMODULE_PRIVATE_HEADER); |
| 982 | RECORD(SUBMODULE_TEXTUAL_HEADER); |
| 983 | RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER); |
| 984 | |
| 985 | // Comments Block. |
| 986 | BLOCK(COMMENTS_BLOCK); |
| 987 | RECORD(COMMENTS_RAW_COMMENT); |
| 988 | |
Douglas Gregor | 12bfa38 | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 989 | // Decls and Types block. |
| 990 | BLOCK(DECLTYPES_BLOCK); |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 991 | RECORD(TYPE_EXT_QUAL); |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 992 | RECORD(TYPE_COMPLEX); |
| 993 | RECORD(TYPE_POINTER); |
| 994 | RECORD(TYPE_BLOCK_POINTER); |
| 995 | RECORD(TYPE_LVALUE_REFERENCE); |
| 996 | RECORD(TYPE_RVALUE_REFERENCE); |
| 997 | RECORD(TYPE_MEMBER_POINTER); |
| 998 | RECORD(TYPE_CONSTANT_ARRAY); |
| 999 | RECORD(TYPE_INCOMPLETE_ARRAY); |
| 1000 | RECORD(TYPE_VARIABLE_ARRAY); |
| 1001 | RECORD(TYPE_VECTOR); |
| 1002 | RECORD(TYPE_EXT_VECTOR); |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 1003 | RECORD(TYPE_FUNCTION_NO_PROTO); |
Richard Smith | f1b4b8b | 2014-07-27 04:29:04 +0000 | [diff] [blame] | 1004 | RECORD(TYPE_FUNCTION_PROTO); |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 1005 | RECORD(TYPE_TYPEDEF); |
| 1006 | RECORD(TYPE_TYPEOF_EXPR); |
| 1007 | RECORD(TYPE_TYPEOF); |
| 1008 | RECORD(TYPE_RECORD); |
| 1009 | RECORD(TYPE_ENUM); |
| 1010 | RECORD(TYPE_OBJC_INTERFACE); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1011 | RECORD(TYPE_OBJC_OBJECT_POINTER); |
Douglas Gregor | 0beaec0 | 2011-02-08 16:34:17 +0000 | [diff] [blame] | 1012 | RECORD(TYPE_DECLTYPE); |
| 1013 | RECORD(TYPE_ELABORATED); |
| 1014 | RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM); |
| 1015 | RECORD(TYPE_UNRESOLVED_USING); |
| 1016 | RECORD(TYPE_INJECTED_CLASS_NAME); |
| 1017 | RECORD(TYPE_OBJC_OBJECT); |
| 1018 | RECORD(TYPE_TEMPLATE_TYPE_PARM); |
| 1019 | RECORD(TYPE_TEMPLATE_SPECIALIZATION); |
| 1020 | RECORD(TYPE_DEPENDENT_NAME); |
| 1021 | RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION); |
| 1022 | RECORD(TYPE_DEPENDENT_SIZED_ARRAY); |
| 1023 | RECORD(TYPE_PAREN); |
| 1024 | RECORD(TYPE_PACK_EXPANSION); |
| 1025 | RECORD(TYPE_ATTRIBUTED); |
| 1026 | RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK); |
Richard Smith | f1b4b8b | 2014-07-27 04:29:04 +0000 | [diff] [blame] | 1027 | RECORD(TYPE_AUTO); |
| 1028 | RECORD(TYPE_UNARY_TRANSFORM); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1029 | RECORD(TYPE_ATOMIC); |
Richard Smith | f1b4b8b | 2014-07-27 04:29:04 +0000 | [diff] [blame] | 1030 | RECORD(TYPE_DECAYED); |
| 1031 | RECORD(TYPE_ADJUSTED); |
Richard Smith | d61d4ac | 2015-08-22 20:13:39 +0000 | [diff] [blame] | 1032 | RECORD(LOCAL_REDECLARATIONS); |
Chris Lattner | db397b6 | 2009-04-26 22:32:16 +0000 | [diff] [blame] | 1033 | RECORD(DECL_TYPEDEF); |
Richard Smith | 01b2cb4 | 2014-07-26 06:37:51 +0000 | [diff] [blame] | 1034 | RECORD(DECL_TYPEALIAS); |
Chris Lattner | db397b6 | 2009-04-26 22:32:16 +0000 | [diff] [blame] | 1035 | RECORD(DECL_ENUM); |
| 1036 | RECORD(DECL_RECORD); |
| 1037 | RECORD(DECL_ENUM_CONSTANT); |
| 1038 | RECORD(DECL_FUNCTION); |
| 1039 | RECORD(DECL_OBJC_METHOD); |
| 1040 | RECORD(DECL_OBJC_INTERFACE); |
| 1041 | RECORD(DECL_OBJC_PROTOCOL); |
| 1042 | RECORD(DECL_OBJC_IVAR); |
| 1043 | RECORD(DECL_OBJC_AT_DEFS_FIELD); |
Chris Lattner | db397b6 | 2009-04-26 22:32:16 +0000 | [diff] [blame] | 1044 | RECORD(DECL_OBJC_CATEGORY); |
| 1045 | RECORD(DECL_OBJC_CATEGORY_IMPL); |
| 1046 | RECORD(DECL_OBJC_IMPLEMENTATION); |
| 1047 | RECORD(DECL_OBJC_COMPATIBLE_ALIAS); |
| 1048 | RECORD(DECL_OBJC_PROPERTY); |
| 1049 | RECORD(DECL_OBJC_PROPERTY_IMPL); |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 1050 | RECORD(DECL_FIELD); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 1051 | RECORD(DECL_MS_PROPERTY); |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 1052 | RECORD(DECL_VAR); |
Chris Lattner | db397b6 | 2009-04-26 22:32:16 +0000 | [diff] [blame] | 1053 | RECORD(DECL_IMPLICIT_PARAM); |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 1054 | RECORD(DECL_PARM_VAR); |
Chris Lattner | db397b6 | 2009-04-26 22:32:16 +0000 | [diff] [blame] | 1055 | RECORD(DECL_FILE_SCOPE_ASM); |
| 1056 | RECORD(DECL_BLOCK); |
| 1057 | RECORD(DECL_CONTEXT_LEXICAL); |
| 1058 | RECORD(DECL_CONTEXT_VISIBLE); |
Douglas Gregor | 0beaec0 | 2011-02-08 16:34:17 +0000 | [diff] [blame] | 1059 | RECORD(DECL_NAMESPACE); |
| 1060 | RECORD(DECL_NAMESPACE_ALIAS); |
| 1061 | RECORD(DECL_USING); |
| 1062 | RECORD(DECL_USING_SHADOW); |
| 1063 | RECORD(DECL_USING_DIRECTIVE); |
| 1064 | RECORD(DECL_UNRESOLVED_USING_VALUE); |
| 1065 | RECORD(DECL_UNRESOLVED_USING_TYPENAME); |
| 1066 | RECORD(DECL_LINKAGE_SPEC); |
| 1067 | RECORD(DECL_CXX_RECORD); |
| 1068 | RECORD(DECL_CXX_METHOD); |
| 1069 | RECORD(DECL_CXX_CONSTRUCTOR); |
| 1070 | RECORD(DECL_CXX_DESTRUCTOR); |
| 1071 | RECORD(DECL_CXX_CONVERSION); |
| 1072 | RECORD(DECL_ACCESS_SPEC); |
| 1073 | RECORD(DECL_FRIEND); |
| 1074 | RECORD(DECL_FRIEND_TEMPLATE); |
| 1075 | RECORD(DECL_CLASS_TEMPLATE); |
| 1076 | RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION); |
| 1077 | RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1078 | RECORD(DECL_VAR_TEMPLATE); |
| 1079 | RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION); |
| 1080 | RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION); |
Douglas Gregor | 0beaec0 | 2011-02-08 16:34:17 +0000 | [diff] [blame] | 1081 | RECORD(DECL_FUNCTION_TEMPLATE); |
| 1082 | RECORD(DECL_TEMPLATE_TYPE_PARM); |
| 1083 | RECORD(DECL_NON_TYPE_TEMPLATE_PARM); |
| 1084 | RECORD(DECL_TEMPLATE_TEMPLATE_PARM); |
| 1085 | RECORD(DECL_STATIC_ASSERT); |
| 1086 | RECORD(DECL_CXX_BASE_SPECIFIERS); |
| 1087 | RECORD(DECL_INDIRECTFIELD); |
| 1088 | RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK); |
| 1089 | |
Douglas Gregor | 03412ba | 2011-06-03 02:27:19 +0000 | [diff] [blame] | 1090 | // Statements and Exprs can occur in the Decls and Types block. |
| 1091 | AddStmtsExprs(Stream, Record); |
| 1092 | |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 1093 | BLOCK(PREPROCESSOR_DETAIL_BLOCK); |
Chandler Carruth | f92ac9e | 2011-07-15 07:25:21 +0000 | [diff] [blame] | 1094 | RECORD(PPD_MACRO_EXPANSION); |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 1095 | RECORD(PPD_MACRO_DEFINITION); |
| 1096 | RECORD(PPD_INCLUSION_DIRECTIVE); |
Douglas Gregor | 6623e1f | 2015-11-03 18:33:07 +0000 | [diff] [blame] | 1097 | |
| 1098 | // Decls and Types block. |
| 1099 | BLOCK(EXTENSION_BLOCK); |
| 1100 | RECORD(EXTENSION_METADATA); |
| 1101 | |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 1102 | #undef RECORD |
| 1103 | #undef BLOCK |
| 1104 | Stream.ExitBlock(); |
| 1105 | } |
| 1106 | |
Richard Smith | 54cc3c2 | 2014-12-11 20:50:24 +0000 | [diff] [blame] | 1107 | /// \brief Prepares a path for being written to an AST file by converting it |
| 1108 | /// to an absolute path and removing nested './'s. |
| 1109 | /// |
| 1110 | /// \return \c true if the path was changed. |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 1111 | static bool cleanPathForOutput(FileManager &FileMgr, |
| 1112 | SmallVectorImpl<char> &Path) { |
Argyrios Kyrtzidis | 403cbcb | 2015-07-31 01:39:23 +0000 | [diff] [blame] | 1113 | bool Changed = FileMgr.makeAbsolutePath(Path); |
Mike Aizatsky | aeb9dd9 | 2015-11-09 19:12:18 +0000 | [diff] [blame] | 1114 | return Changed | llvm::sys::path::remove_dots(Path); |
Richard Smith | 54cc3c2 | 2014-12-11 20:50:24 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1117 | /// \brief Adjusts the given filename to only write out the portion of the |
| 1118 | /// filename that is not part of the system root directory. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1119 | /// |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1120 | /// \param Filename the file name to adjust. |
| 1121 | /// |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 1122 | /// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and |
| 1123 | /// the returned filename will be adjusted by this root directory. |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1124 | /// |
| 1125 | /// \returns either the original filename (if it needs no adjustment) or the |
| 1126 | /// adjusted filename (which points into the @p Filename parameter). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1127 | static const char * |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 1128 | adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) { |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1129 | assert(Filename && "No file name to adjust?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1130 | |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 1131 | if (BaseDir.empty()) |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1132 | return Filename; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1133 | |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1134 | // Verify that the filename and the system root have the same prefix. |
| 1135 | unsigned Pos = 0; |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 1136 | for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos) |
| 1137 | if (Filename[Pos] != BaseDir[Pos]) |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1138 | return Filename; // Prefixes don't match. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1139 | |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1140 | // We hit the end of the filename before we hit the end of the system root. |
| 1141 | if (!Filename[Pos]) |
| 1142 | return Filename; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1143 | |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 1144 | // If there's not a path separator at the end of the base directory nor |
| 1145 | // immediately after it, then this isn't within the base directory. |
| 1146 | if (!llvm::sys::path::is_separator(Filename[Pos])) { |
| 1147 | if (!llvm::sys::path::is_separator(BaseDir.back())) |
| 1148 | return Filename; |
| 1149 | } else { |
| 1150 | // If the file name has a '/' at the current position, skip over the '/'. |
| 1151 | // We distinguish relative paths from absolute paths by the |
| 1152 | // absence of '/' at the beginning of relative paths. |
| 1153 | // |
| 1154 | // FIXME: This is wrong. We distinguish them by asking if the path is |
| 1155 | // absolute, which isn't the same thing. And there might be multiple '/'s |
| 1156 | // in a row. Use a better mechanism to indicate whether we have emitted an |
| 1157 | // absolute or relative path. |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1158 | ++Pos; |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 1159 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1160 | |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1161 | return Filename + Pos; |
| 1162 | } |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 1163 | |
Ben Langmuir | 487ea14 | 2014-10-23 18:05:36 +0000 | [diff] [blame] | 1164 | static ASTFileSignature getSignature() { |
| 1165 | while (1) { |
| 1166 | if (ASTFileSignature S = llvm::sys::Process::GetRandomNumber()) |
| 1167 | return S; |
| 1168 | // Rely on GetRandomNumber to eventually return non-zero... |
| 1169 | } |
| 1170 | } |
| 1171 | |
Douglas Gregor | 112b907 | 2012-10-18 05:31:06 +0000 | [diff] [blame] | 1172 | /// \brief Write the control block. |
Adrian Prantl | adbd2b1 | 2015-09-22 23:26:31 +0000 | [diff] [blame] | 1173 | uint64_t ASTWriter::WriteControlBlock(Preprocessor &PP, |
| 1174 | ASTContext &Context, |
| 1175 | StringRef isysroot, |
| 1176 | const std::string &OutputFile) { |
| 1177 | ASTFileSignature Signature = 0; |
| 1178 | |
Douglas Gregor | bfbde53 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 1179 | using namespace llvm; |
Douglas Gregor | 0aa21c9 | 2012-10-18 18:27:37 +0000 | [diff] [blame] | 1180 | Stream.EnterSubblock(CONTROL_BLOCK_ID, 5); |
Mehdi Amini | 5ae4a85 | 2015-09-09 20:35:37 +0000 | [diff] [blame] | 1181 | RecordData Record; |
| 1182 | |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1183 | // Metadata |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 1184 | auto *MetadataAbbrev = new BitCodeAbbrev(); |
Douglas Gregor | 0aa21c9 | 2012-10-18 18:27:37 +0000 | [diff] [blame] | 1185 | MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA)); |
| 1186 | MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major |
| 1187 | MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor |
| 1188 | MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj. |
| 1189 | MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min. |
| 1190 | MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable |
Richard Smith | e75ee0f | 2015-08-17 07:13:32 +0000 | [diff] [blame] | 1191 | MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps |
Douglas Gregor | 0aa21c9 | 2012-10-18 18:27:37 +0000 | [diff] [blame] | 1192 | MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors |
| 1193 | MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag |
| 1194 | unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev); |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 1195 | assert((!WritingModule || isysroot.empty()) && |
| 1196 | "writing module as a relocatable PCH?"); |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 1197 | { |
| 1198 | RecordData::value_type Record[] = {METADATA, VERSION_MAJOR, VERSION_MINOR, |
| 1199 | CLANG_VERSION_MAJOR, CLANG_VERSION_MINOR, |
| 1200 | !isysroot.empty(), IncludeTimestamps, |
| 1201 | ASTHasCompilerErrors}; |
| 1202 | Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record, |
| 1203 | getClangFullRepositoryVersion()); |
| 1204 | } |
Ben Langmuir | beee15e | 2014-04-14 18:00:01 +0000 | [diff] [blame] | 1205 | if (WritingModule) { |
Chandler Carruth | 885e78c | 2015-03-24 21:18:10 +0000 | [diff] [blame] | 1206 | // For implicit modules we output a signature that we can use to ensure |
| 1207 | // duplicate module builds don't collide in the cache as their output order |
| 1208 | // is non-deterministic. |
| 1209 | // FIXME: Remove this when output is deterministic. |
| 1210 | if (Context.getLangOpts().ImplicitModules) { |
Adrian Prantl | adbd2b1 | 2015-09-22 23:26:31 +0000 | [diff] [blame] | 1211 | Signature = getSignature(); |
| 1212 | RecordData::value_type Record[] = {Signature}; |
Chandler Carruth | 885e78c | 2015-03-24 21:18:10 +0000 | [diff] [blame] | 1213 | Stream.EmitRecord(SIGNATURE, Record); |
| 1214 | } |
| 1215 | |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 1216 | // Module name |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 1217 | auto *Abbrev = new BitCodeAbbrev(); |
Ben Langmuir | beee15e | 2014-04-14 18:00:01 +0000 | [diff] [blame] | 1218 | Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME)); |
| 1219 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name |
| 1220 | unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev); |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 1221 | RecordData::value_type Record[] = {MODULE_NAME}; |
Ben Langmuir | beee15e | 2014-04-14 18:00:01 +0000 | [diff] [blame] | 1222 | Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name); |
| 1223 | } |
| 1224 | |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 1225 | if (WritingModule && WritingModule->Directory) { |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 1226 | SmallString<128> BaseDir(WritingModule->Directory->getName()); |
Richard Smith | 54cc3c2 | 2014-12-11 20:50:24 +0000 | [diff] [blame] | 1227 | cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir); |
Richard Smith | f7b4137 | 2015-08-13 23:47:44 +0000 | [diff] [blame] | 1228 | |
| 1229 | // If the home of the module is the current working directory, then we |
| 1230 | // want to pick up the cwd of the build process loading the module, not |
| 1231 | // our cwd, when we load this module. |
| 1232 | if (!PP.getHeaderSearchInfo() |
| 1233 | .getHeaderSearchOpts() |
| 1234 | .ModuleMapFileHomeIsCwd || |
| 1235 | WritingModule->Directory->getName() != StringRef(".")) { |
| 1236 | // Module directory. |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 1237 | auto *Abbrev = new BitCodeAbbrev(); |
Richard Smith | f7b4137 | 2015-08-13 23:47:44 +0000 | [diff] [blame] | 1238 | Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY)); |
| 1239 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory |
| 1240 | unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev); |
| 1241 | |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 1242 | RecordData::value_type Record[] = {MODULE_DIRECTORY}; |
Richard Smith | f7b4137 | 2015-08-13 23:47:44 +0000 | [diff] [blame] | 1243 | Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir); |
| 1244 | } |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 1245 | |
| 1246 | // Write out all other paths relative to the base directory if possible. |
| 1247 | BaseDirectory.assign(BaseDir.begin(), BaseDir.end()); |
| 1248 | } else if (!isysroot.empty()) { |
| 1249 | // Write out paths relative to the sysroot if possible. |
| 1250 | BaseDirectory = isysroot; |
| 1251 | } |
| 1252 | |
Ben Langmuir | beee15e | 2014-04-14 18:00:01 +0000 | [diff] [blame] | 1253 | // Module map file |
| 1254 | if (WritingModule) { |
Mehdi Amini | 5ae4a85 | 2015-09-09 20:35:37 +0000 | [diff] [blame] | 1255 | Record.clear(); |
Ben Langmuir | beee15e | 2014-04-14 18:00:01 +0000 | [diff] [blame] | 1256 | |
Ben Langmuir | 4b8a9e9 | 2014-08-12 16:42:33 +0000 | [diff] [blame] | 1257 | auto &Map = PP.getHeaderSearchInfo().getModuleMap(); |
| 1258 | |
| 1259 | // Primary module map file. |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 1260 | AddPath(Map.getModuleMapFileForUniquing(WritingModule)->getName(), Record); |
Ben Langmuir | 4b8a9e9 | 2014-08-12 16:42:33 +0000 | [diff] [blame] | 1261 | |
| 1262 | // Additional module map files. |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 1263 | if (auto *AdditionalModMaps = |
| 1264 | Map.getAdditionalModuleMapFiles(WritingModule)) { |
Ben Langmuir | 4b8a9e9 | 2014-08-12 16:42:33 +0000 | [diff] [blame] | 1265 | Record.push_back(AdditionalModMaps->size()); |
| 1266 | for (const FileEntry *F : *AdditionalModMaps) |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 1267 | AddPath(F->getName(), Record); |
Ben Langmuir | 4b8a9e9 | 2014-08-12 16:42:33 +0000 | [diff] [blame] | 1268 | } else { |
| 1269 | Record.push_back(0); |
| 1270 | } |
| 1271 | |
| 1272 | Stream.EmitRecord(MODULE_MAP_FILE, Record); |
Ben Langmuir | beee15e | 2014-04-14 18:00:01 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
Douglas Gregor | 112b907 | 2012-10-18 05:31:06 +0000 | [diff] [blame] | 1275 | // Imports |
Douglas Gregor | 29cc642 | 2011-08-17 21:07:30 +0000 | [diff] [blame] | 1276 | if (Chain) { |
Douglas Gregor | 29cc642 | 2011-08-17 21:07:30 +0000 | [diff] [blame] | 1277 | serialization::ModuleManager &Mgr = Chain->getModuleManager(); |
Mehdi Amini | 5ae4a85 | 2015-09-09 20:35:37 +0000 | [diff] [blame] | 1278 | Record.clear(); |
Douglas Gregor | df0c151 | 2011-08-18 04:12:04 +0000 | [diff] [blame] | 1279 | |
Richard Smith | 7f330cd | 2015-03-18 01:42:29 +0000 | [diff] [blame] | 1280 | for (auto *M : Mgr) { |
Douglas Gregor | df0c151 | 2011-08-18 04:12:04 +0000 | [diff] [blame] | 1281 | // Skip modules that weren't directly imported. |
Richard Smith | 7f330cd | 2015-03-18 01:42:29 +0000 | [diff] [blame] | 1282 | if (!M->isDirectlyImported()) |
Douglas Gregor | df0c151 | 2011-08-18 04:12:04 +0000 | [diff] [blame] | 1283 | continue; |
| 1284 | |
Richard Smith | 7f330cd | 2015-03-18 01:42:29 +0000 | [diff] [blame] | 1285 | Record.push_back((unsigned)M->Kind); // FIXME: Stable encoding |
| 1286 | AddSourceLocation(M->ImportLoc, Record); |
| 1287 | Record.push_back(M->File->getSize()); |
Richard Smith | e75ee0f | 2015-08-17 07:13:32 +0000 | [diff] [blame] | 1288 | Record.push_back(getTimestampForOutput(M->File)); |
Richard Smith | 7f330cd | 2015-03-18 01:42:29 +0000 | [diff] [blame] | 1289 | Record.push_back(M->Signature); |
| 1290 | AddPath(M->FileName, Record); |
Douglas Gregor | df0c151 | 2011-08-18 04:12:04 +0000 | [diff] [blame] | 1291 | } |
Douglas Gregor | 29cc642 | 2011-08-17 21:07:30 +0000 | [diff] [blame] | 1292 | Stream.EmitRecord(IMPORTS, Record); |
| 1293 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1294 | |
Richard Smith | 0516b18 | 2015-09-08 19:40:14 +0000 | [diff] [blame] | 1295 | // Write the options block. |
| 1296 | Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4); |
| 1297 | |
Douglas Gregor | 112b907 | 2012-10-18 05:31:06 +0000 | [diff] [blame] | 1298 | // Language options. |
Mehdi Amini | 5ae4a85 | 2015-09-09 20:35:37 +0000 | [diff] [blame] | 1299 | Record.clear(); |
Douglas Gregor | 112b907 | 2012-10-18 05:31:06 +0000 | [diff] [blame] | 1300 | const LangOptions &LangOpts = Context.getLangOpts(); |
| 1301 | #define LANGOPT(Name, Bits, Default, Description) \ |
| 1302 | Record.push_back(LangOpts.Name); |
| 1303 | #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ |
| 1304 | Record.push_back(static_cast<unsigned>(LangOpts.get##Name())); |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 1305 | #include "clang/Basic/LangOptions.def" |
| 1306 | #define SANITIZER(NAME, ID) \ |
| 1307 | Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID)); |
Will Dietz | f54319c | 2013-01-18 11:30:38 +0000 | [diff] [blame] | 1308 | #include "clang/Basic/Sanitizers.def" |
Douglas Gregor | 112b907 | 2012-10-18 05:31:06 +0000 | [diff] [blame] | 1309 | |
Ben Langmuir | cd98cb7 | 2015-06-23 18:20:18 +0000 | [diff] [blame] | 1310 | Record.push_back(LangOpts.ModuleFeatures.size()); |
| 1311 | for (StringRef Feature : LangOpts.ModuleFeatures) |
| 1312 | AddString(Feature, Record); |
| 1313 | |
Douglas Gregor | 112b907 | 2012-10-18 05:31:06 +0000 | [diff] [blame] | 1314 | Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind()); |
| 1315 | AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record); |
Ben Langmuir | d4a667a | 2015-06-23 18:20:23 +0000 | [diff] [blame] | 1316 | |
| 1317 | AddString(LangOpts.CurrentModule, Record); |
Dmitri Gribenko | acf2e78 | 2013-02-22 14:21:27 +0000 | [diff] [blame] | 1318 | |
| 1319 | // Comment options. |
| 1320 | Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size()); |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 1321 | for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) { |
| 1322 | AddString(I, Record); |
Dmitri Gribenko | acf2e78 | 2013-02-22 14:21:27 +0000 | [diff] [blame] | 1323 | } |
Dmitri Gribenko | a7d16ce | 2013-04-10 15:35:17 +0000 | [diff] [blame] | 1324 | Record.push_back(LangOpts.CommentOpts.ParseAllComments); |
Dmitri Gribenko | acf2e78 | 2013-02-22 14:21:27 +0000 | [diff] [blame] | 1325 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame^] | 1326 | // OpenMP offloading options. |
| 1327 | Record.push_back(LangOpts.OMPTargetTriples.size()); |
| 1328 | for (auto &T : LangOpts.OMPTargetTriples) |
| 1329 | AddString(T.getTriple(), Record); |
| 1330 | |
| 1331 | AddString(LangOpts.OMPHostIRFile, Record); |
| 1332 | |
Douglas Gregor | 112b907 | 2012-10-18 05:31:06 +0000 | [diff] [blame] | 1333 | Stream.EmitRecord(LANGUAGE_OPTIONS, Record); |
| 1334 | |
Douglas Gregor | 4d3611c | 2012-10-18 17:58:09 +0000 | [diff] [blame] | 1335 | // Target options. |
| 1336 | Record.clear(); |
Douglas Gregor | 0aa21c9 | 2012-10-18 18:27:37 +0000 | [diff] [blame] | 1337 | const TargetInfo &Target = Context.getTargetInfo(); |
| 1338 | const TargetOptions &TargetOpts = Target.getTargetOpts(); |
Douglas Gregor | 4d3611c | 2012-10-18 17:58:09 +0000 | [diff] [blame] | 1339 | AddString(TargetOpts.Triple, Record); |
| 1340 | AddString(TargetOpts.CPU, Record); |
| 1341 | AddString(TargetOpts.ABI, Record); |
Douglas Gregor | 4d3611c | 2012-10-18 17:58:09 +0000 | [diff] [blame] | 1342 | Record.push_back(TargetOpts.FeaturesAsWritten.size()); |
| 1343 | for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) { |
| 1344 | AddString(TargetOpts.FeaturesAsWritten[I], Record); |
| 1345 | } |
| 1346 | Record.push_back(TargetOpts.Features.size()); |
| 1347 | for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) { |
| 1348 | AddString(TargetOpts.Features[I], Record); |
| 1349 | } |
| 1350 | Stream.EmitRecord(TARGET_OPTIONS, Record); |
| 1351 | |
Douglas Gregor | 8263ffb | 2012-10-24 15:17:15 +0000 | [diff] [blame] | 1352 | // Diagnostic options. |
| 1353 | Record.clear(); |
| 1354 | const DiagnosticOptions &DiagOpts |
| 1355 | = Context.getDiagnostics().getDiagnosticOptions(); |
| 1356 | #define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name); |
| 1357 | #define ENUM_DIAGOPT(Name, Type, Bits, Default) \ |
| 1358 | Record.push_back(static_cast<unsigned>(DiagOpts.get##Name())); |
| 1359 | #include "clang/Basic/DiagnosticOptions.def" |
| 1360 | Record.push_back(DiagOpts.Warnings.size()); |
| 1361 | for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I) |
| 1362 | AddString(DiagOpts.Warnings[I], Record); |
Richard Smith | 3be1cb2 | 2014-08-07 00:24:21 +0000 | [diff] [blame] | 1363 | Record.push_back(DiagOpts.Remarks.size()); |
| 1364 | for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I) |
| 1365 | AddString(DiagOpts.Remarks[I], Record); |
Douglas Gregor | 8263ffb | 2012-10-24 15:17:15 +0000 | [diff] [blame] | 1366 | // Note: we don't serialize the log or serialization file names, because they |
| 1367 | // are generally transient files and will almost always be overridden. |
| 1368 | Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record); |
| 1369 | |
Douglas Gregor | c6317db | 2012-10-24 15:49:58 +0000 | [diff] [blame] | 1370 | // File system options. |
| 1371 | Record.clear(); |
Yaron Keren | 8dec46c | 2015-08-26 08:10:22 +0000 | [diff] [blame] | 1372 | const FileSystemOptions &FSOpts = |
| 1373 | Context.getSourceManager().getFileManager().getFileSystemOpts(); |
Douglas Gregor | c6317db | 2012-10-24 15:49:58 +0000 | [diff] [blame] | 1374 | AddString(FSOpts.WorkingDir, Record); |
| 1375 | Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record); |
| 1376 | |
Douglas Gregor | 2d30236 | 2012-10-24 16:50:34 +0000 | [diff] [blame] | 1377 | // Header search options. |
| 1378 | Record.clear(); |
| 1379 | const HeaderSearchOptions &HSOpts |
| 1380 | = PP.getHeaderSearchInfo().getHeaderSearchOpts(); |
| 1381 | AddString(HSOpts.Sysroot, Record); |
| 1382 | |
| 1383 | // Include entries. |
| 1384 | Record.push_back(HSOpts.UserEntries.size()); |
| 1385 | for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) { |
| 1386 | const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I]; |
| 1387 | AddString(Entry.Path, Record); |
| 1388 | Record.push_back(static_cast<unsigned>(Entry.Group)); |
Douglas Gregor | 2d30236 | 2012-10-24 16:50:34 +0000 | [diff] [blame] | 1389 | Record.push_back(Entry.IsFramework); |
| 1390 | Record.push_back(Entry.IgnoreSysRoot); |
Douglas Gregor | 2d30236 | 2012-10-24 16:50:34 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | // System header prefixes. |
| 1394 | Record.push_back(HSOpts.SystemHeaderPrefixes.size()); |
| 1395 | for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) { |
| 1396 | AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record); |
| 1397 | Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader); |
| 1398 | } |
| 1399 | |
| 1400 | AddString(HSOpts.ResourceDir, Record); |
| 1401 | AddString(HSOpts.ModuleCachePath, Record); |
Argyrios Kyrtzidis | 1594c15 | 2014-03-03 08:12:05 +0000 | [diff] [blame] | 1402 | AddString(HSOpts.ModuleUserBuildPath, Record); |
Douglas Gregor | 2d30236 | 2012-10-24 16:50:34 +0000 | [diff] [blame] | 1403 | Record.push_back(HSOpts.DisableModuleHash); |
| 1404 | Record.push_back(HSOpts.UseBuiltinIncludes); |
| 1405 | Record.push_back(HSOpts.UseStandardSystemIncludes); |
| 1406 | Record.push_back(HSOpts.UseStandardCXXIncludes); |
| 1407 | Record.push_back(HSOpts.UseLibcxx); |
Argyrios Kyrtzidis | bd0b651 | 2015-02-19 20:12:20 +0000 | [diff] [blame] | 1408 | // Write out the specific module cache path that contains the module files. |
| 1409 | AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record); |
Douglas Gregor | 2d30236 | 2012-10-24 16:50:34 +0000 | [diff] [blame] | 1410 | Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record); |
| 1411 | |
Douglas Gregor | b6af6c2 | 2012-10-24 20:05:57 +0000 | [diff] [blame] | 1412 | // Preprocessor options. |
| 1413 | Record.clear(); |
| 1414 | const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts(); |
| 1415 | |
| 1416 | // Macro definitions. |
| 1417 | Record.push_back(PPOpts.Macros.size()); |
| 1418 | for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) { |
| 1419 | AddString(PPOpts.Macros[I].first, Record); |
| 1420 | Record.push_back(PPOpts.Macros[I].second); |
| 1421 | } |
| 1422 | |
| 1423 | // Includes |
| 1424 | Record.push_back(PPOpts.Includes.size()); |
| 1425 | for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I) |
| 1426 | AddString(PPOpts.Includes[I], Record); |
| 1427 | |
| 1428 | // Macro includes |
| 1429 | Record.push_back(PPOpts.MacroIncludes.size()); |
| 1430 | for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I) |
| 1431 | AddString(PPOpts.MacroIncludes[I], Record); |
| 1432 | |
Douglas Gregor | b636875 | 2012-10-24 23:41:50 +0000 | [diff] [blame] | 1433 | Record.push_back(PPOpts.UsePredefines); |
Argyrios Kyrtzidis | d3afa0c | 2013-04-26 21:33:40 +0000 | [diff] [blame] | 1434 | // Detailed record is important since it is used for the module cache hash. |
| 1435 | Record.push_back(PPOpts.DetailedRecord); |
Douglas Gregor | b6af6c2 | 2012-10-24 20:05:57 +0000 | [diff] [blame] | 1436 | AddString(PPOpts.ImplicitPCHInclude, Record); |
| 1437 | AddString(PPOpts.ImplicitPTHInclude, Record); |
| 1438 | Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary)); |
| 1439 | Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record); |
| 1440 | |
Richard Smith | 0516b18 | 2015-09-08 19:40:14 +0000 | [diff] [blame] | 1441 | // Leave the options block. |
| 1442 | Stream.ExitBlock(); |
| 1443 | |
Douglas Gregor | a3b2026 | 2011-05-06 21:43:30 +0000 | [diff] [blame] | 1444 | // Original file name and file ID |
Douglas Gregor | 45fe036 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1445 | SourceManager &SM = Context.getSourceManager(); |
| 1446 | if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) { |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 1447 | auto *FileAbbrev = new BitCodeAbbrev(); |
Douglas Gregor | fad10d8 | 2012-10-18 18:36:53 +0000 | [diff] [blame] | 1448 | FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE)); |
| 1449 | FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID |
Douglas Gregor | 45fe036 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1450 | FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name |
| 1451 | unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev); |
| 1452 | |
Douglas Gregor | b6af6c2 | 2012-10-24 20:05:57 +0000 | [diff] [blame] | 1453 | Record.clear(); |
Douglas Gregor | fad10d8 | 2012-10-18 18:36:53 +0000 | [diff] [blame] | 1454 | Record.push_back(ORIGINAL_FILE); |
Douglas Gregor | a3b2026 | 2011-05-06 21:43:30 +0000 | [diff] [blame] | 1455 | Record.push_back(SM.getMainFileID().getOpaqueValue()); |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 1456 | EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName()); |
Douglas Gregor | 45fe036 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1457 | } |
Kovarththanan Rajaratnam | a9c81a8 | 2010-03-14 07:06:50 +0000 | [diff] [blame] | 1458 | |
Argyrios Kyrtzidis | 5259524 | 2012-11-15 18:57:27 +0000 | [diff] [blame] | 1459 | Record.clear(); |
| 1460 | Record.push_back(SM.getMainFileID().getOpaqueValue()); |
| 1461 | Stream.EmitRecord(ORIGINAL_FILE_ID, Record); |
| 1462 | |
Argyrios Kyrtzidis | 10b2368 | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 1463 | // Original PCH directory |
| 1464 | if (!OutputFile.empty() && OutputFile != "-") { |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 1465 | auto *Abbrev = new BitCodeAbbrev(); |
Argyrios Kyrtzidis | 10b2368 | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 1466 | Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR)); |
| 1467 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name |
| 1468 | unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev); |
| 1469 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1470 | SmallString<128> OutputPath(OutputFile); |
Argyrios Kyrtzidis | 10b2368 | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 1471 | |
Argyrios Kyrtzidis | c56419e | 2015-07-31 00:58:32 +0000 | [diff] [blame] | 1472 | SM.getFileManager().makeAbsolutePath(OutputPath); |
Argyrios Kyrtzidis | 10b2368 | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 1473 | StringRef origDir = llvm::sys::path::parent_path(OutputPath); |
| 1474 | |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 1475 | RecordData::value_type Record[] = {ORIGINAL_PCH_DIR}; |
Argyrios Kyrtzidis | 10b2368 | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 1476 | Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir); |
| 1477 | } |
| 1478 | |
Douglas Gregor | 49491f7 | 2013-03-15 22:15:07 +0000 | [diff] [blame] | 1479 | WriteInputFiles(Context.SourceMgr, |
| 1480 | PP.getHeaderSearchInfo().getHeaderSearchOpts(), |
Douglas Gregor | a3dd900 | 2013-07-22 20:48:33 +0000 | [diff] [blame] | 1481 | PP.getLangOpts().Modules); |
Douglas Gregor | 72be390 | 2012-10-19 00:38:02 +0000 | [diff] [blame] | 1482 | Stream.ExitBlock(); |
Adrian Prantl | adbd2b1 | 2015-09-22 23:26:31 +0000 | [diff] [blame] | 1483 | return Signature; |
Douglas Gregor | 72be390 | 2012-10-19 00:38:02 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Douglas Gregor | 49491f7 | 2013-03-15 22:15:07 +0000 | [diff] [blame] | 1486 | namespace { |
| 1487 | /// \brief An input file. |
| 1488 | struct InputFileEntry { |
| 1489 | const FileEntry *File; |
| 1490 | bool IsSystemFile; |
Richard Smith | a8cfffa | 2015-11-26 02:04:16 +0000 | [diff] [blame] | 1491 | bool IsTransient; |
Douglas Gregor | 49491f7 | 2013-03-15 22:15:07 +0000 | [diff] [blame] | 1492 | bool BufferOverridden; |
| 1493 | }; |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 1494 | } // end anonymous namespace |
Douglas Gregor | 49491f7 | 2013-03-15 22:15:07 +0000 | [diff] [blame] | 1495 | |
| 1496 | void ASTWriter::WriteInputFiles(SourceManager &SourceMgr, |
| 1497 | HeaderSearchOptions &HSOpts, |
Douglas Gregor | a3dd900 | 2013-07-22 20:48:33 +0000 | [diff] [blame] | 1498 | bool Modules) { |
Douglas Gregor | 72be390 | 2012-10-19 00:38:02 +0000 | [diff] [blame] | 1499 | using namespace llvm; |
| 1500 | Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4); |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 1501 | |
Douglas Gregor | 72be390 | 2012-10-19 00:38:02 +0000 | [diff] [blame] | 1502 | // Create input-file abbreviation. |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 1503 | auto *IFAbbrev = new BitCodeAbbrev(); |
Douglas Gregor | 72be390 | 2012-10-19 00:38:02 +0000 | [diff] [blame] | 1504 | IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE)); |
Douglas Gregor | 3120d2c | 2012-10-22 18:42:04 +0000 | [diff] [blame] | 1505 | IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID |
Douglas Gregor | 72be390 | 2012-10-19 00:38:02 +0000 | [diff] [blame] | 1506 | IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size |
| 1507 | IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time |
Douglas Gregor | 3120d2c | 2012-10-22 18:42:04 +0000 | [diff] [blame] | 1508 | IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden |
Richard Smith | a8cfffa | 2015-11-26 02:04:16 +0000 | [diff] [blame] | 1509 | IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient |
Douglas Gregor | 72be390 | 2012-10-19 00:38:02 +0000 | [diff] [blame] | 1510 | IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name |
| 1511 | unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev); |
| 1512 | |
Argyrios Kyrtzidis | 7d23857 | 2013-03-06 18:12:50 +0000 | [diff] [blame] | 1513 | // Get all ContentCache objects for files, sorted by whether the file is a |
| 1514 | // system one or not. System files go at the back, users files at the front. |
Douglas Gregor | 49491f7 | 2013-03-15 22:15:07 +0000 | [diff] [blame] | 1515 | std::deque<InputFileEntry> SortedFiles; |
Douglas Gregor | 72be390 | 2012-10-19 00:38:02 +0000 | [diff] [blame] | 1516 | for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) { |
| 1517 | // Get this source location entry. |
| 1518 | const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I); |
NAKAMURA Takumi | deca50f | 2012-10-19 01:53:57 +0000 | [diff] [blame] | 1519 | assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc); |
Douglas Gregor | 72be390 | 2012-10-19 00:38:02 +0000 | [diff] [blame] | 1520 | |
| 1521 | // We only care about file entries that were not overridden. |
| 1522 | if (!SLoc->isFile()) |
| 1523 | continue; |
| 1524 | const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache(); |
Douglas Gregor | 3120d2c | 2012-10-22 18:42:04 +0000 | [diff] [blame] | 1525 | if (!Cache->OrigEntry) |
Douglas Gregor | 72be390 | 2012-10-19 00:38:02 +0000 | [diff] [blame] | 1526 | continue; |
| 1527 | |
Douglas Gregor | 49491f7 | 2013-03-15 22:15:07 +0000 | [diff] [blame] | 1528 | InputFileEntry Entry; |
| 1529 | Entry.File = Cache->OrigEntry; |
| 1530 | Entry.IsSystemFile = Cache->IsSystemFile; |
Richard Smith | a8cfffa | 2015-11-26 02:04:16 +0000 | [diff] [blame] | 1531 | Entry.IsTransient = Cache->IsTransient; |
Douglas Gregor | 49491f7 | 2013-03-15 22:15:07 +0000 | [diff] [blame] | 1532 | Entry.BufferOverridden = Cache->BufferOverridden; |
Argyrios Kyrtzidis | 7d23857 | 2013-03-06 18:12:50 +0000 | [diff] [blame] | 1533 | if (Cache->IsSystemFile) |
Douglas Gregor | 49491f7 | 2013-03-15 22:15:07 +0000 | [diff] [blame] | 1534 | SortedFiles.push_back(Entry); |
Argyrios Kyrtzidis | 7d23857 | 2013-03-06 18:12:50 +0000 | [diff] [blame] | 1535 | else |
Douglas Gregor | 49491f7 | 2013-03-15 22:15:07 +0000 | [diff] [blame] | 1536 | SortedFiles.push_front(Entry); |
| 1537 | } |
| 1538 | |
Argyrios Kyrtzidis | 7d23857 | 2013-03-06 18:12:50 +0000 | [diff] [blame] | 1539 | unsigned UserFilesNum = 0; |
| 1540 | // Write out all of the input files. |
Richard Smith | ec21650 | 2015-02-13 19:48:37 +0000 | [diff] [blame] | 1541 | std::vector<uint64_t> InputFileOffsets; |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 1542 | for (const auto &Entry : SortedFiles) { |
Douglas Gregor | 49491f7 | 2013-03-15 22:15:07 +0000 | [diff] [blame] | 1543 | uint32_t &InputFileID = InputFileIDs[Entry.File]; |
Argyrios Kyrtzidis | e65856f | 2012-12-11 07:48:08 +0000 | [diff] [blame] | 1544 | if (InputFileID != 0) |
| 1545 | continue; // already recorded this file. |
| 1546 | |
Douglas Gregor | 3120d2c | 2012-10-22 18:42:04 +0000 | [diff] [blame] | 1547 | // Record this entry's offset. |
| 1548 | InputFileOffsets.push_back(Stream.GetCurrentBitNo()); |
Argyrios Kyrtzidis | e65856f | 2012-12-11 07:48:08 +0000 | [diff] [blame] | 1549 | |
| 1550 | InputFileID = InputFileOffsets.size(); |
Douglas Gregor | 3120d2c | 2012-10-22 18:42:04 +0000 | [diff] [blame] | 1551 | |
Douglas Gregor | 49491f7 | 2013-03-15 22:15:07 +0000 | [diff] [blame] | 1552 | if (!Entry.IsSystemFile) |
Argyrios Kyrtzidis | 7d23857 | 2013-03-06 18:12:50 +0000 | [diff] [blame] | 1553 | ++UserFilesNum; |
| 1554 | |
Douglas Gregor | 72be390 | 2012-10-19 00:38:02 +0000 | [diff] [blame] | 1555 | // Emit size/modification time for this file. |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 1556 | // And whether this file was overridden. |
| 1557 | RecordData::value_type Record[] = { |
Richard Smith | a8cfffa | 2015-11-26 02:04:16 +0000 | [diff] [blame] | 1558 | INPUT_FILE, |
| 1559 | InputFileOffsets.size(), |
| 1560 | (uint64_t)Entry.File->getSize(), |
| 1561 | (uint64_t)getTimestampForOutput(Entry.File), |
| 1562 | Entry.BufferOverridden, |
| 1563 | Entry.IsTransient}; |
Douglas Gregor | 3120d2c | 2012-10-22 18:42:04 +0000 | [diff] [blame] | 1564 | |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 1565 | EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName()); |
| 1566 | } |
Douglas Gregor | 49491f7 | 2013-03-15 22:15:07 +0000 | [diff] [blame] | 1567 | |
Douglas Gregor | 112b907 | 2012-10-18 05:31:06 +0000 | [diff] [blame] | 1568 | Stream.ExitBlock(); |
Douglas Gregor | 3120d2c | 2012-10-22 18:42:04 +0000 | [diff] [blame] | 1569 | |
| 1570 | // Create input file offsets abbreviation. |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 1571 | auto *OffsetsAbbrev = new BitCodeAbbrev(); |
Douglas Gregor | 3120d2c | 2012-10-22 18:42:04 +0000 | [diff] [blame] | 1572 | OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS)); |
| 1573 | OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files |
Argyrios Kyrtzidis | 7d23857 | 2013-03-06 18:12:50 +0000 | [diff] [blame] | 1574 | OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system |
| 1575 | // input files |
Douglas Gregor | 3120d2c | 2012-10-22 18:42:04 +0000 | [diff] [blame] | 1576 | OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array |
| 1577 | unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev); |
| 1578 | |
| 1579 | // Write input file offsets. |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 1580 | RecordData::value_type Record[] = {INPUT_FILE_OFFSETS, |
| 1581 | InputFileOffsets.size(), UserFilesNum}; |
Reid Kleckner | 012665e | 2015-05-21 00:13:09 +0000 | [diff] [blame] | 1582 | Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets)); |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1585 | //===----------------------------------------------------------------------===// |
| 1586 | // Source Manager Serialization |
| 1587 | //===----------------------------------------------------------------------===// |
| 1588 | |
| 1589 | /// \brief Create an abbreviation for the SLocEntry that refers to a |
| 1590 | /// file. |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1591 | static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) { |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1592 | using namespace llvm; |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 1593 | |
| 1594 | auto *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1595 | Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY)); |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1596 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset |
| 1597 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location |
| 1598 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic |
| 1599 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives |
Douglas Gregor | b41ca8f | 2010-03-21 22:49:54 +0000 | [diff] [blame] | 1600 | // FileEntry fields. |
Douglas Gregor | 3120d2c | 2012-10-22 18:42:04 +0000 | [diff] [blame] | 1601 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID |
Argyrios Kyrtzidis | 61ef3db | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 1602 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 1603 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex |
| 1604 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1605 | return Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | /// \brief Create an abbreviation for the SLocEntry that refers to a |
| 1609 | /// buffer. |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1610 | static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) { |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1611 | using namespace llvm; |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 1612 | |
| 1613 | auto *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1614 | Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY)); |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1615 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset |
| 1616 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location |
| 1617 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic |
| 1618 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives |
| 1619 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1620 | return Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | /// \brief Create an abbreviation for the SLocEntry that refers to a |
| 1624 | /// buffer's blob. |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1625 | static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) { |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1626 | using namespace llvm; |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 1627 | |
| 1628 | auto *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1629 | Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB)); |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1630 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1631 | return Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
Chandler Carruth | f92ac9e | 2011-07-15 07:25:21 +0000 | [diff] [blame] | 1634 | /// \brief Create an abbreviation for the SLocEntry that refers to a macro |
| 1635 | /// expansion. |
| 1636 | static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) { |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1637 | using namespace llvm; |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 1638 | |
| 1639 | auto *Abbrev = new BitCodeAbbrev(); |
Chandler Carruth | f92ac9e | 2011-07-15 07:25:21 +0000 | [diff] [blame] | 1640 | Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY)); |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1641 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset |
| 1642 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location |
| 1643 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location |
| 1644 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location |
Douglas Gregor | 8324327 | 2009-04-15 18:05:10 +0000 | [diff] [blame] | 1645 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1646 | return Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1649 | namespace { |
| 1650 | // Trait used for the on-disk hash table of header search information. |
| 1651 | class HeaderFileInfoTrait { |
| 1652 | ASTWriter &Writer; |
Argyrios Kyrtzidis | b146baa | 2013-03-13 21:13:51 +0000 | [diff] [blame] | 1653 | const HeaderSearch &HS; |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1654 | |
Douglas Gregor | 4b123cb | 2011-07-28 04:50:02 +0000 | [diff] [blame] | 1655 | // Keep track of the framework names we've used during serialization. |
| 1656 | SmallVector<char, 128> FrameworkStringData; |
| 1657 | llvm::StringMap<unsigned> FrameworkNameOffset; |
| 1658 | |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1659 | public: |
Argyrios Kyrtzidis | b146baa | 2013-03-13 21:13:51 +0000 | [diff] [blame] | 1660 | HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS) |
| 1661 | : Writer(Writer), HS(HS) { } |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1662 | |
Argyrios Kyrtzidis | 5c2a345 | 2013-03-06 18:12:47 +0000 | [diff] [blame] | 1663 | struct key_type { |
| 1664 | const FileEntry *FE; |
| 1665 | const char *Filename; |
| 1666 | }; |
| 1667 | typedef const key_type &key_type_ref; |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1668 | |
| 1669 | typedef HeaderFileInfo data_type; |
| 1670 | typedef const data_type &data_type_ref; |
Justin Bogner | 25463f1 | 2014-04-18 20:27:24 +0000 | [diff] [blame] | 1671 | typedef unsigned hash_value_type; |
| 1672 | typedef unsigned offset_type; |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1673 | |
Richard Smith | e75ee0f | 2015-08-17 07:13:32 +0000 | [diff] [blame] | 1674 | hash_value_type ComputeHash(key_type_ref key) { |
Argyrios Kyrtzidis | 5c2a345 | 2013-03-06 18:12:47 +0000 | [diff] [blame] | 1675 | // The hash is based only on size/time of the file, so that the reader can |
| 1676 | // match even when symlinking or excess path elements ("foo/../", "../") |
| 1677 | // change the form of the name. However, complete path is still the key. |
| 1678 | return llvm::hash_combine(key.FE->getSize(), |
Richard Smith | e75ee0f | 2015-08-17 07:13:32 +0000 | [diff] [blame] | 1679 | Writer.getTimestampForOutput(key.FE)); |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
| 1682 | std::pair<unsigned,unsigned> |
Argyrios Kyrtzidis | 5c2a345 | 2013-03-06 18:12:47 +0000 | [diff] [blame] | 1683 | EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) { |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 1684 | using namespace llvm::support; |
Richard Smith | 386bb07 | 2015-08-18 23:42:23 +0000 | [diff] [blame] | 1685 | endian::Writer<little> LE(Out); |
Argyrios Kyrtzidis | 5c2a345 | 2013-03-06 18:12:47 +0000 | [diff] [blame] | 1686 | unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8; |
Richard Smith | 386bb07 | 2015-08-18 23:42:23 +0000 | [diff] [blame] | 1687 | LE.write<uint16_t>(KeyLen); |
Douglas Gregor | 4b123cb | 2011-07-28 04:50:02 +0000 | [diff] [blame] | 1688 | unsigned DataLen = 1 + 2 + 4 + 4; |
Richard Smith | 386bb07 | 2015-08-18 23:42:23 +0000 | [diff] [blame] | 1689 | for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE)) |
| 1690 | if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule())) |
| 1691 | DataLen += 4; |
| 1692 | LE.write<uint8_t>(DataLen); |
Argyrios Kyrtzidis | 5c2a345 | 2013-03-06 18:12:47 +0000 | [diff] [blame] | 1693 | return std::make_pair(KeyLen, DataLen); |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
Argyrios Kyrtzidis | 5c2a345 | 2013-03-06 18:12:47 +0000 | [diff] [blame] | 1696 | void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) { |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 1697 | using namespace llvm::support; |
| 1698 | endian::Writer<little> LE(Out); |
| 1699 | LE.write<uint64_t>(key.FE->getSize()); |
Argyrios Kyrtzidis | 5c2a345 | 2013-03-06 18:12:47 +0000 | [diff] [blame] | 1700 | KeyLen -= 8; |
Richard Smith | e75ee0f | 2015-08-17 07:13:32 +0000 | [diff] [blame] | 1701 | LE.write<uint64_t>(Writer.getTimestampForOutput(key.FE)); |
Argyrios Kyrtzidis | 5c2a345 | 2013-03-06 18:12:47 +0000 | [diff] [blame] | 1702 | KeyLen -= 8; |
| 1703 | Out.write(key.Filename, KeyLen); |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
Argyrios Kyrtzidis | b146baa | 2013-03-13 21:13:51 +0000 | [diff] [blame] | 1706 | void EmitData(raw_ostream &Out, key_type_ref key, |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1707 | data_type_ref Data, unsigned DataLen) { |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 1708 | using namespace llvm::support; |
| 1709 | endian::Writer<little> LE(Out); |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1710 | uint64_t Start = Out.tell(); (void)Start; |
| 1711 | |
Richard Smith | 386bb07 | 2015-08-18 23:42:23 +0000 | [diff] [blame] | 1712 | unsigned char Flags = (Data.isImport << 4) |
| 1713 | | (Data.isPragmaOnce << 3) |
| 1714 | | (Data.DirInfo << 1) |
Douglas Gregor | 4b123cb | 2011-07-28 04:50:02 +0000 | [diff] [blame] | 1715 | | Data.IndexHeaderMapHeader; |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 1716 | LE.write<uint8_t>(Flags); |
| 1717 | LE.write<uint16_t>(Data.NumIncludes); |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1718 | |
| 1719 | if (!Data.ControllingMacro) |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 1720 | LE.write<uint32_t>(Data.ControllingMacroID); |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1721 | else |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 1722 | LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro)); |
Douglas Gregor | 4b123cb | 2011-07-28 04:50:02 +0000 | [diff] [blame] | 1723 | |
| 1724 | unsigned Offset = 0; |
| 1725 | if (!Data.Framework.empty()) { |
| 1726 | // If this header refers into a framework, save the framework name. |
| 1727 | llvm::StringMap<unsigned>::iterator Pos |
| 1728 | = FrameworkNameOffset.find(Data.Framework); |
| 1729 | if (Pos == FrameworkNameOffset.end()) { |
| 1730 | Offset = FrameworkStringData.size() + 1; |
| 1731 | FrameworkStringData.append(Data.Framework.begin(), |
| 1732 | Data.Framework.end()); |
| 1733 | FrameworkStringData.push_back(0); |
| 1734 | |
| 1735 | FrameworkNameOffset[Data.Framework] = Offset; |
| 1736 | } else |
| 1737 | Offset = Pos->second; |
| 1738 | } |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 1739 | LE.write<uint32_t>(Offset); |
Argyrios Kyrtzidis | b146baa | 2013-03-13 21:13:51 +0000 | [diff] [blame] | 1740 | |
Richard Smith | 386bb07 | 2015-08-18 23:42:23 +0000 | [diff] [blame] | 1741 | // FIXME: If the header is excluded, we should write out some |
| 1742 | // record of that fact. |
| 1743 | for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE)) { |
| 1744 | if (uint32_t ModID = |
| 1745 | Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule())) { |
| 1746 | uint32_t Value = (ModID << 2) | (unsigned)ModInfo.getRole(); |
| 1747 | assert((Value >> 2) == ModID && "overflow in header module info"); |
| 1748 | LE.write<uint32_t>(Value); |
| 1749 | } |
Argyrios Kyrtzidis | b146baa | 2013-03-13 21:13:51 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1752 | assert(Out.tell() - Start == DataLen && "Wrong data length"); |
| 1753 | } |
Douglas Gregor | 4b123cb | 2011-07-28 04:50:02 +0000 | [diff] [blame] | 1754 | |
| 1755 | const char *strings_begin() const { return FrameworkStringData.begin(); } |
| 1756 | const char *strings_end() const { return FrameworkStringData.end(); } |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1757 | }; |
| 1758 | } // end anonymous namespace |
| 1759 | |
| 1760 | /// \brief Write the header search block for the list of files that |
| 1761 | /// |
| 1762 | /// \param HS The header search structure to save. |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 1763 | void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1764 | SmallVector<const FileEntry *, 16> FilesByUID; |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1765 | HS.getFileMgr().GetUniqueIDMapping(FilesByUID); |
| 1766 | |
| 1767 | if (FilesByUID.size() > HS.header_file_size()) |
| 1768 | FilesByUID.resize(HS.header_file_size()); |
| 1769 | |
Argyrios Kyrtzidis | b146baa | 2013-03-13 21:13:51 +0000 | [diff] [blame] | 1770 | HeaderFileInfoTrait GeneratorTrait(*this, HS); |
Justin Bogner | bb094f0 | 2014-04-18 19:57:06 +0000 | [diff] [blame] | 1771 | llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1772 | SmallVector<const char *, 4> SavedStrings; |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1773 | unsigned NumHeaderSearchEntries = 0; |
| 1774 | for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) { |
| 1775 | const FileEntry *File = FilesByUID[UID]; |
| 1776 | if (!File) |
| 1777 | continue; |
| 1778 | |
Richard Smith | 386bb07 | 2015-08-18 23:42:23 +0000 | [diff] [blame] | 1779 | // Get the file info. This will load info from the external source if |
| 1780 | // necessary. Skip emitting this file if we have no information on it |
| 1781 | // as a header file (in which case HFI will be null) or if it hasn't |
| 1782 | // changed since it was loaded. Also skip it if it's for a modular header |
| 1783 | // from a different module; in that case, we rely on the module(s) |
| 1784 | // containing the header to provide this information. |
Richard Smith | d8879c8 | 2015-08-24 21:59:32 +0000 | [diff] [blame] | 1785 | const HeaderFileInfo *HFI = |
| 1786 | HS.getExistingFileInfo(File, /*WantExternal*/!Chain); |
| 1787 | if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader)) |
Argyrios Kyrtzidis | 6f722b4 | 2013-05-08 23:46:46 +0000 | [diff] [blame] | 1788 | continue; |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1789 | |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 1790 | // Massage the file path into an appropriate form. |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1791 | const char *Filename = File->getName(); |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 1792 | SmallString<128> FilenameTmp(Filename); |
| 1793 | if (PreparePathForOutput(FilenameTmp)) { |
| 1794 | // If we performed any translation on the file name at all, we need to |
| 1795 | // save this string, since the generator will refer to it later. |
| 1796 | Filename = strdup(FilenameTmp.c_str()); |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1797 | SavedStrings.push_back(Filename); |
| 1798 | } |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 1799 | |
Argyrios Kyrtzidis | 5c2a345 | 2013-03-06 18:12:47 +0000 | [diff] [blame] | 1800 | HeaderFileInfoTrait::key_type key = { File, Filename }; |
Richard Smith | 386bb07 | 2015-08-18 23:42:23 +0000 | [diff] [blame] | 1801 | Generator.insert(key, *HFI, GeneratorTrait); |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1802 | ++NumHeaderSearchEntries; |
| 1803 | } |
| 1804 | |
| 1805 | // Create the on-disk hash table in a buffer. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1806 | SmallString<4096> TableData; |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1807 | uint32_t BucketOffset; |
| 1808 | { |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 1809 | using namespace llvm::support; |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1810 | llvm::raw_svector_ostream Out(TableData); |
| 1811 | // Make sure that no bucket is at offset 0 |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 1812 | endian::Writer<little>(Out).write<uint32_t>(0); |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1813 | BucketOffset = Generator.Emit(Out, GeneratorTrait); |
| 1814 | } |
| 1815 | |
| 1816 | // Create a blob abbreviation |
| 1817 | using namespace llvm; |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 1818 | |
| 1819 | auto *Abbrev = new BitCodeAbbrev(); |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1820 | Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE)); |
| 1821 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
| 1822 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
Douglas Gregor | 4b123cb | 2011-07-28 04:50:02 +0000 | [diff] [blame] | 1823 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1824 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 1825 | unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev); |
| 1826 | |
Douglas Gregor | 4b123cb | 2011-07-28 04:50:02 +0000 | [diff] [blame] | 1827 | // Write the header search table |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 1828 | RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset, |
| 1829 | NumHeaderSearchEntries, TableData.size()}; |
Douglas Gregor | 4b123cb | 2011-07-28 04:50:02 +0000 | [diff] [blame] | 1830 | TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end()); |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 1831 | Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData); |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1832 | |
| 1833 | // Free all of the strings we had to duplicate. |
| 1834 | for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I) |
David Greene | bae0e35 | 2013-01-15 22:09:43 +0000 | [diff] [blame] | 1835 | free(const_cast<char *>(SavedStrings[I])); |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1838 | /// \brief Writes the block containing the serialized form of the |
| 1839 | /// source manager. |
| 1840 | /// |
| 1841 | /// TODO: We should probably use an on-disk hash table (stored in a |
| 1842 | /// blob), indexed based on the file name, so that we only create |
| 1843 | /// entries for files that we actually need. In the common case (no |
| 1844 | /// errors), we probably won't have to create file entries for any of |
| 1845 | /// the files in the AST. |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 1846 | void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr, |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 1847 | const Preprocessor &PP) { |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1848 | RecordData Record; |
| 1849 | |
Chris Lattner | 0910e3b | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 1850 | // Enter the source manager block. |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1851 | Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3); |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1852 | |
| 1853 | // Abbreviations for the various kinds of source-location entries. |
Chris Lattner | c4976c73 | 2009-04-27 19:03:22 +0000 | [diff] [blame] | 1854 | unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream); |
| 1855 | unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream); |
| 1856 | unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream); |
Chandler Carruth | f92ac9e | 2011-07-15 07:25:21 +0000 | [diff] [blame] | 1857 | unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream); |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1858 | |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1859 | // Write out the source location entry table. We skip the first |
| 1860 | // entry, which is always the same dummy entry. |
Chris Lattner | 12d61d3 | 2009-04-27 19:01:47 +0000 | [diff] [blame] | 1861 | std::vector<uint32_t> SLocEntryOffsets; |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1862 | RecordData PreloadSLocs; |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1863 | SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1); |
| 1864 | for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); |
Sebastian Redl | 5c415f3 | 2010-07-22 17:01:13 +0000 | [diff] [blame] | 1865 | I != N; ++I) { |
Douglas Gregor | 8655e88 | 2009-10-16 22:46:09 +0000 | [diff] [blame] | 1866 | // Get this source location entry. |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1867 | const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I); |
Argyrios Kyrtzidis | 4db774a | 2012-10-02 21:09:17 +0000 | [diff] [blame] | 1868 | FileID FID = FileID::get(I); |
| 1869 | assert(&SourceMgr.getSLocEntry(FID) == SLoc); |
Kovarththanan Rajaratnam | a9c81a8 | 2010-03-14 07:06:50 +0000 | [diff] [blame] | 1870 | |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1871 | // Record the offset of this source-location entry. |
| 1872 | SLocEntryOffsets.push_back(Stream.GetCurrentBitNo()); |
| 1873 | |
| 1874 | // Figure out which record code to use. |
| 1875 | unsigned Code; |
| 1876 | if (SLoc->isFile()) { |
Douglas Gregor | 9dc3212 | 2011-11-16 20:05:18 +0000 | [diff] [blame] | 1877 | const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache(); |
| 1878 | if (Cache->OrigEntry) { |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1879 | Code = SM_SLOC_FILE_ENTRY; |
Argyrios Kyrtzidis | 92dd466 | 2011-06-02 20:01:46 +0000 | [diff] [blame] | 1880 | } else |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1881 | Code = SM_SLOC_BUFFER_ENTRY; |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1882 | } else |
Chandler Carruth | f92ac9e | 2011-07-15 07:25:21 +0000 | [diff] [blame] | 1883 | Code = SM_SLOC_EXPANSION_ENTRY; |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1884 | Record.clear(); |
| 1885 | Record.push_back(Code); |
| 1886 | |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1887 | // Starting offset of this entry within this module, so skip the dummy. |
| 1888 | Record.push_back(SLoc->getOffset() - 2); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1889 | if (SLoc->isFile()) { |
| 1890 | const SrcMgr::FileInfo &File = SLoc->getFile(); |
| 1891 | Record.push_back(File.getIncludeLoc().getRawEncoding()); |
| 1892 | Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding |
| 1893 | Record.push_back(File.hasLineDirectives()); |
| 1894 | |
| 1895 | const SrcMgr::ContentCache *Content = File.getContentCache(); |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 1896 | if (Content->OrigEntry) { |
| 1897 | assert(Content->OrigEntry == Content->ContentsEntry && |
Douglas Gregor | 9dc3212 | 2011-11-16 20:05:18 +0000 | [diff] [blame] | 1898 | "Writing to AST an overridden file is not supported"); |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 1899 | |
Douglas Gregor | 3120d2c | 2012-10-22 18:42:04 +0000 | [diff] [blame] | 1900 | // The source location entry is a file. Emit input file ID. |
| 1901 | assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry"); |
| 1902 | Record.push_back(InputFileIDs[Content->OrigEntry]); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1903 | |
Argyrios Kyrtzidis | 61ef3db | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 1904 | Record.push_back(File.NumCreatedFIDs); |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 1905 | |
Argyrios Kyrtzidis | 4db774a | 2012-10-02 21:09:17 +0000 | [diff] [blame] | 1906 | FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID); |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 1907 | if (FDI != FileDeclIDs.end()) { |
| 1908 | Record.push_back(FDI->second->FirstDeclIndex); |
| 1909 | Record.push_back(FDI->second->DeclIDs.size()); |
| 1910 | } else { |
| 1911 | Record.push_back(0); |
| 1912 | Record.push_back(0); |
| 1913 | } |
Mehdi Amini | 5ae4a85 | 2015-09-09 20:35:37 +0000 | [diff] [blame] | 1914 | |
| 1915 | Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record); |
| 1916 | |
Richard Smith | a8cfffa | 2015-11-26 02:04:16 +0000 | [diff] [blame] | 1917 | if (Content->BufferOverridden || Content->IsTransient) { |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 1918 | RecordData::value_type Record[] = {SM_SLOC_BUFFER_BLOB}; |
Douglas Gregor | 9dc3212 | 2011-11-16 20:05:18 +0000 | [diff] [blame] | 1919 | const llvm::MemoryBuffer *Buffer |
| 1920 | = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager()); |
| 1921 | Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, |
| 1922 | StringRef(Buffer->getBufferStart(), |
| 1923 | Buffer->getBufferSize() + 1)); |
| 1924 | } |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1925 | } else { |
| 1926 | // The source location entry is a buffer. The blob associated |
| 1927 | // with this entry contains the contents of the buffer. |
| 1928 | |
| 1929 | // We add one to the size so that we capture the trailing NULL |
| 1930 | // that is required by llvm::MemoryBuffer::getMemBuffer (on |
| 1931 | // the reader side). |
Douglas Gregor | 874cc62 | 2010-03-16 00:35:39 +0000 | [diff] [blame] | 1932 | const llvm::MemoryBuffer *Buffer |
Chris Lattner | fb24a3a | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1933 | = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager()); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1934 | const char *Name = Buffer->getBufferIdentifier(); |
Mehdi Amini | 5ae4a85 | 2015-09-09 20:35:37 +0000 | [diff] [blame] | 1935 | Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1936 | StringRef(Name, strlen(Name) + 1)); |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 1937 | RecordData::value_type Record[] = {SM_SLOC_BUFFER_BLOB}; |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1938 | Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1939 | StringRef(Buffer->getBufferStart(), |
Daniel Dunbar | 8100d01 | 2009-08-24 09:31:37 +0000 | [diff] [blame] | 1940 | Buffer->getBufferSize() + 1)); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1941 | |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1942 | if (strcmp(Name, "<built-in>") == 0) { |
| 1943 | PreloadSLocs.push_back(SLocEntryOffsets.size()); |
| 1944 | } |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1945 | } |
| 1946 | } else { |
Chandler Carruth | f92ac9e | 2011-07-15 07:25:21 +0000 | [diff] [blame] | 1947 | // The source location entry is a macro expansion. |
Chandler Carruth | ee4c1d1 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 1948 | const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion(); |
Chandler Carruth | 73ee5d7 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 1949 | Record.push_back(Expansion.getSpellingLoc().getRawEncoding()); |
| 1950 | Record.push_back(Expansion.getExpansionLocStart().getRawEncoding()); |
Argyrios Kyrtzidis | a1d943a | 2011-08-17 00:31:14 +0000 | [diff] [blame] | 1951 | Record.push_back(Expansion.isMacroArgExpansion() ? 0 |
| 1952 | : Expansion.getExpansionLocEnd().getRawEncoding()); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1953 | |
| 1954 | // Compute the token length for this macro expansion. |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1955 | unsigned NextOffset = SourceMgr.getNextLocalOffset(); |
Douglas Gregor | 8655e88 | 2009-10-16 22:46:09 +0000 | [diff] [blame] | 1956 | if (I + 1 != N) |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1957 | NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset(); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1958 | Record.push_back(NextOffset - SLoc->getOffset() - 1); |
Mehdi Amini | 5ae4a85 | 2015-09-09 20:35:37 +0000 | [diff] [blame] | 1959 | Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1960 | } |
| 1961 | } |
| 1962 | |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1963 | Stream.ExitBlock(); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1964 | |
| 1965 | if (SLocEntryOffsets.empty()) |
| 1966 | return; |
| 1967 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 1968 | // Write the source-location offsets table into the AST block. This |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1969 | // table is used for lazily loading source-location information. |
| 1970 | using namespace llvm; |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 1971 | |
| 1972 | auto *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1973 | Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS)); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1974 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1975 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1976 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets |
| 1977 | unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev); |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 1978 | { |
| 1979 | RecordData::value_type Record[] = { |
| 1980 | SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(), |
| 1981 | SourceMgr.getNextLocalOffset() - 1 /* skip dummy */}; |
| 1982 | Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, |
| 1983 | bytes(SLocEntryOffsets)); |
| 1984 | } |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 1985 | // Write the source location entry preloads array, telling the AST |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1986 | // reader which source locations entries it should load eagerly. |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1987 | Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs); |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1988 | |
| 1989 | // Write the line table. It depends on remapping working, so it must come |
| 1990 | // after the source location offsets. |
| 1991 | if (SourceMgr.hasLineTable()) { |
| 1992 | LineTableInfo &LineTable = SourceMgr.getLineTable(); |
| 1993 | |
| 1994 | Record.clear(); |
Richard Smith | 6307849 | 2015-09-01 07:41:55 +0000 | [diff] [blame] | 1995 | |
| 1996 | // Emit the needed file names. |
| 1997 | llvm::DenseMap<int, int> FilenameMap; |
| 1998 | for (const auto &L : LineTable) { |
| 1999 | if (L.first.ID < 0) |
| 2000 | continue; |
| 2001 | for (auto &LE : L.second) { |
| 2002 | if (FilenameMap.insert(std::make_pair(LE.FilenameID, |
| 2003 | FilenameMap.size())).second) |
| 2004 | AddPath(LineTable.getFilename(LE.FilenameID), Record); |
| 2005 | } |
| 2006 | } |
| 2007 | Record.push_back(0); |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2008 | |
| 2009 | // Emit the line entries |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 2010 | for (const auto &L : LineTable) { |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2011 | // Only emit entries for local files. |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 2012 | if (L.first.ID < 0) |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2013 | continue; |
| 2014 | |
| 2015 | // Emit the file ID |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 2016 | Record.push_back(L.first.ID); |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2017 | |
| 2018 | // Emit the line entries |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 2019 | Record.push_back(L.second.size()); |
| 2020 | for (const auto &LE : L.second) { |
| 2021 | Record.push_back(LE.FileOffset); |
| 2022 | Record.push_back(LE.LineNo); |
| 2023 | Record.push_back(FilenameMap[LE.FilenameID]); |
| 2024 | Record.push_back((unsigned)LE.FileKind); |
| 2025 | Record.push_back(LE.IncludeOffset); |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2026 | } |
| 2027 | } |
Richard Smith | 6307849 | 2015-09-01 07:41:55 +0000 | [diff] [blame] | 2028 | |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2029 | Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record); |
| 2030 | } |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 2033 | //===----------------------------------------------------------------------===// |
| 2034 | // Preprocessor Serialization |
| 2035 | //===----------------------------------------------------------------------===// |
| 2036 | |
Argyrios Kyrtzidis | 0aef0f0 | 2013-03-15 22:43:10 +0000 | [diff] [blame] | 2037 | static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule, |
| 2038 | const Preprocessor &PP) { |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 2039 | if (MacroInfo *MI = MD->getMacroInfo()) |
| 2040 | if (MI->isBuiltinMacro()) |
| 2041 | return true; |
Argyrios Kyrtzidis | 0aef0f0 | 2013-03-15 22:43:10 +0000 | [diff] [blame] | 2042 | |
| 2043 | if (IsModule) { |
| 2044 | SourceLocation Loc = MD->getLocation(); |
| 2045 | if (Loc.isInvalid()) |
| 2046 | return true; |
| 2047 | if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID()) |
| 2048 | return true; |
| 2049 | } |
| 2050 | |
| 2051 | return false; |
| 2052 | } |
| 2053 | |
Chris Lattner | eeffaef | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 2054 | /// \brief Writes the block containing the serialized form of the |
| 2055 | /// preprocessor. |
| 2056 | /// |
Douglas Gregor | 4a69c2e | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 2057 | void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) { |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 2058 | PreprocessingRecord *PPRec = PP.getPreprocessingRecord(); |
| 2059 | if (PPRec) |
| 2060 | WritePreprocessorDetail(*PPRec); |
| 2061 | |
Chris Lattner | baa52f4 | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 2062 | RecordData Record; |
Richard Smith | d732939 | 2015-04-21 21:46:32 +0000 | [diff] [blame] | 2063 | RecordData ModuleMacroRecord; |
Chris Lattner | 0910e3b | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 2064 | |
Chris Lattner | 0af3ba1 | 2009-04-13 01:29:17 +0000 | [diff] [blame] | 2065 | // If the preprocessor __COUNTER__ value has been bumped, remember it. |
| 2066 | if (PP.getCounterValue() != 0) { |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 2067 | RecordData::value_type Record[] = {PP.getCounterValue()}; |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2068 | Stream.EmitRecord(PP_COUNTER_VALUE, Record); |
Douglas Gregor | eda6a89 | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
| 2071 | // Enter the preprocessor block. |
Douglas Gregor | 796d76a | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 2072 | Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2073 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2074 | // If the AST file contains __DATE__ or __TIME__ emit a warning about this. |
Richard Smith | b5aaf5a | 2015-09-01 02:35:58 +0000 | [diff] [blame] | 2075 | // FIXME: Include a location for the use, and say which one was used. |
Douglas Gregor | eda6a89 | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 2076 | if (PP.SawDateOrTime()) |
Richard Smith | b5aaf5a | 2015-09-01 02:35:58 +0000 | [diff] [blame] | 2077 | PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule; |
Douglas Gregor | 796d76a | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 2078 | |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 2079 | // Loop over all the macro directives that are live at the end of the file, |
Chris Lattner | baa52f4 | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 2080 | // emitting each to the PP section. |
Michael J. Spencer | 4c0ffa8 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 2081 | |
Richard Smith | ee97793 | 2015-05-01 21:22:17 +0000 | [diff] [blame] | 2082 | // Construct the list of identifiers with macro directives that need to be |
| 2083 | // serialized. |
| 2084 | SmallVector<const IdentifierInfo *, 128> MacroIdentifiers; |
| 2085 | for (auto &Id : PP.getIdentifierTable()) |
| 2086 | if (Id.second->hadMacroDefinition() && |
| 2087 | (!Id.second->isFromAST() || |
| 2088 | Id.second->hasChangedSinceDeserialization())) |
| 2089 | MacroIdentifiers.push_back(Id.second); |
Douglas Gregor | 2e5571d | 2011-02-10 18:20:09 +0000 | [diff] [blame] | 2090 | // Sort the set of macro definitions that need to be serialized by the |
| 2091 | // name of the macro, to provide a stable ordering. |
Richard Smith | ee97793 | 2015-05-01 21:22:17 +0000 | [diff] [blame] | 2092 | std::sort(MacroIdentifiers.begin(), MacroIdentifiers.end(), |
| 2093 | llvm::less_ptr<IdentifierInfo>()); |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 2094 | |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 2095 | // Emit the macro directives as a list and associate the offset with the |
| 2096 | // identifier they belong to. |
Richard Smith | ee97793 | 2015-05-01 21:22:17 +0000 | [diff] [blame] | 2097 | for (const IdentifierInfo *Name : MacroIdentifiers) { |
| 2098 | MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name); |
Richard Smith | d732939 | 2015-04-21 21:46:32 +0000 | [diff] [blame] | 2099 | auto StartOffset = Stream.GetCurrentBitNo(); |
| 2100 | |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 2101 | // Emit the macro directives in reverse source order. |
| 2102 | for (; MD; MD = MD->getPrevious()) { |
Richard Smith | d732939 | 2015-04-21 21:46:32 +0000 | [diff] [blame] | 2103 | // Once we hit an ignored macro, we're done: the rest of the chain |
| 2104 | // will all be ignored macros. |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 2105 | if (shouldIgnoreMacro(MD, IsModule, PP)) |
Richard Smith | d732939 | 2015-04-21 21:46:32 +0000 | [diff] [blame] | 2106 | break; |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 2107 | |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 2108 | AddSourceLocation(MD->getLocation(), Record); |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 2109 | Record.push_back(MD->getKind()); |
Richard Smith | daa69e0 | 2014-07-25 04:40:03 +0000 | [diff] [blame] | 2110 | if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) { |
Richard Smith | 3981b17 | 2015-04-30 02:16:23 +0000 | [diff] [blame] | 2111 | Record.push_back(getMacroRef(DefMD->getInfo(), Name)); |
Richard Smith | 713369b | 2015-04-23 20:40:50 +0000 | [diff] [blame] | 2112 | } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) { |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 2113 | Record.push_back(VisMD->isPublic()); |
| 2114 | } |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 2115 | } |
Richard Smith | d732939 | 2015-04-21 21:46:32 +0000 | [diff] [blame] | 2116 | |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 2117 | // Write out any exported module macros. |
Richard Smith | ee97793 | 2015-05-01 21:22:17 +0000 | [diff] [blame] | 2118 | bool EmittedModuleMacros = false; |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 2119 | if (IsModule) { |
| 2120 | auto Leafs = PP.getLeafModuleMacros(Name); |
| 2121 | SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end()); |
| 2122 | llvm::DenseMap<ModuleMacro*, unsigned> Visits; |
| 2123 | while (!Worklist.empty()) { |
| 2124 | auto *Macro = Worklist.pop_back_val(); |
Richard Smith | d732939 | 2015-04-21 21:46:32 +0000 | [diff] [blame] | 2125 | |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 2126 | // Emit a record indicating this submodule exports this macro. |
| 2127 | ModuleMacroRecord.push_back( |
| 2128 | getSubmoduleID(Macro->getOwningModule())); |
Richard Smith | ee97793 | 2015-05-01 21:22:17 +0000 | [diff] [blame] | 2129 | ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name)); |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 2130 | for (auto *M : Macro->overrides()) |
| 2131 | ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule())); |
Richard Smith | d732939 | 2015-04-21 21:46:32 +0000 | [diff] [blame] | 2132 | |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 2133 | Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord); |
| 2134 | ModuleMacroRecord.clear(); |
Richard Smith | d732939 | 2015-04-21 21:46:32 +0000 | [diff] [blame] | 2135 | |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 2136 | // Enqueue overridden macros once we've visited all their ancestors. |
| 2137 | for (auto *M : Macro->overrides()) |
| 2138 | if (++Visits[M] == M->getNumOverridingMacros()) |
| 2139 | Worklist.push_back(M); |
Richard Smith | ee97793 | 2015-05-01 21:22:17 +0000 | [diff] [blame] | 2140 | |
| 2141 | EmittedModuleMacros = true; |
Richard Smith | d732939 | 2015-04-21 21:46:32 +0000 | [diff] [blame] | 2142 | } |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 2143 | } |
Richard Smith | d732939 | 2015-04-21 21:46:32 +0000 | [diff] [blame] | 2144 | |
Richard Smith | ee97793 | 2015-05-01 21:22:17 +0000 | [diff] [blame] | 2145 | if (Record.empty() && !EmittedModuleMacros) |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 2146 | continue; |
| 2147 | |
Richard Smith | d732939 | 2015-04-21 21:46:32 +0000 | [diff] [blame] | 2148 | IdentMacroDirectivesOffsetMap[Name] = StartOffset; |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 2149 | Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record); |
| 2150 | Record.clear(); |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 2151 | } |
Alexander Kornienko | 1d26c02 | 2012-09-25 17:18:14 +0000 | [diff] [blame] | 2152 | |
Douglas Gregor | cb28f9d | 2012-10-09 23:05:51 +0000 | [diff] [blame] | 2153 | /// \brief Offsets of each of the macros into the bitstream, indexed by |
| 2154 | /// the local macro ID |
| 2155 | /// |
| 2156 | /// For each identifier that is associated with a macro, this map |
| 2157 | /// provides the offset into the bitstream where that macro is |
| 2158 | /// defined. |
| 2159 | std::vector<uint32_t> MacroOffsets; |
| 2160 | |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 2161 | for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) { |
| 2162 | const IdentifierInfo *Name = MacroInfosToEmit[I].Name; |
| 2163 | MacroInfo *MI = MacroInfosToEmit[I].MI; |
| 2164 | MacroID ID = MacroInfosToEmit[I].ID; |
Douglas Gregor | eb114da | 2010-10-01 01:03:07 +0000 | [diff] [blame] | 2165 | |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 2166 | if (ID < FirstMacroID) { |
| 2167 | assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?"); |
| 2168 | continue; |
Chris Lattner | 2199f5b | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 2169 | } |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 2170 | |
| 2171 | // Record the local offset of this macro. |
| 2172 | unsigned Index = ID - FirstMacroID; |
| 2173 | if (Index == MacroOffsets.size()) |
| 2174 | MacroOffsets.push_back(Stream.GetCurrentBitNo()); |
| 2175 | else { |
| 2176 | if (Index > MacroOffsets.size()) |
| 2177 | MacroOffsets.resize(Index + 1); |
| 2178 | |
| 2179 | MacroOffsets[Index] = Stream.GetCurrentBitNo(); |
| 2180 | } |
| 2181 | |
| 2182 | AddIdentifierRef(Name, Record); |
| 2183 | Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc())); |
| 2184 | AddSourceLocation(MI->getDefinitionLoc(), Record); |
| 2185 | AddSourceLocation(MI->getDefinitionEndLoc(), Record); |
| 2186 | Record.push_back(MI->isUsed()); |
Argyrios Kyrtzidis | 9ef53ce | 2014-04-09 18:21:23 +0000 | [diff] [blame] | 2187 | Record.push_back(MI->isUsedForHeaderGuard()); |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 2188 | unsigned Code; |
| 2189 | if (MI->isObjectLike()) { |
| 2190 | Code = PP_MACRO_OBJECT_LIKE; |
| 2191 | } else { |
| 2192 | Code = PP_MACRO_FUNCTION_LIKE; |
| 2193 | |
| 2194 | Record.push_back(MI->isC99Varargs()); |
| 2195 | Record.push_back(MI->isGNUVarargs()); |
| 2196 | Record.push_back(MI->hasCommaPasting()); |
| 2197 | Record.push_back(MI->getNumArgs()); |
Daniel Marjamaki | e4770da | 2015-05-29 09:15:24 +0000 | [diff] [blame] | 2198 | for (const IdentifierInfo *Arg : MI->args()) |
| 2199 | AddIdentifierRef(Arg, Record); |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
| 2202 | // If we have a detailed preprocessing record, record the macro definition |
| 2203 | // ID that corresponds to this macro. |
| 2204 | if (PPRec) |
| 2205 | Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]); |
| 2206 | |
| 2207 | Stream.EmitRecord(Code, Record); |
| 2208 | Record.clear(); |
| 2209 | |
| 2210 | // Emit the tokens array. |
| 2211 | for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) { |
| 2212 | // Note that we know that the preprocessor does not have any annotation |
| 2213 | // tokens in it because they are created by the parser, and thus can't |
| 2214 | // be in a macro definition. |
| 2215 | const Token &Tok = MI->getReplacementToken(TokNo); |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 2216 | AddToken(Tok, Record); |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 2217 | Stream.EmitRecord(PP_TOKEN, Record); |
| 2218 | Record.clear(); |
| 2219 | } |
| 2220 | ++NumMacros; |
Chris Lattner | baa52f4 | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 2221 | } |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 2222 | |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 2223 | Stream.ExitBlock(); |
Douglas Gregor | cb28f9d | 2012-10-09 23:05:51 +0000 | [diff] [blame] | 2224 | |
| 2225 | // Write the offsets table for macro IDs. |
| 2226 | using namespace llvm; |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 2227 | |
Richard Smith | 13090a2 | 2015-04-10 02:02:24 +0000 | [diff] [blame] | 2228 | auto *Abbrev = new BitCodeAbbrev(); |
Douglas Gregor | cb28f9d | 2012-10-09 23:05:51 +0000 | [diff] [blame] | 2229 | Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET)); |
| 2230 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros |
| 2231 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID |
| 2232 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 2233 | |
| 2234 | unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev); |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 2235 | { |
| 2236 | RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(), |
| 2237 | FirstMacroID - NUM_PREDEF_MACRO_IDS}; |
| 2238 | Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets)); |
| 2239 | } |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 2240 | } |
| 2241 | |
| 2242 | void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) { |
Argyrios Kyrtzidis | 7f44836 | 2011-09-19 20:40:42 +0000 | [diff] [blame] | 2243 | if (PPRec.local_begin() == PPRec.local_end()) |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 2244 | return; |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 2245 | |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 2246 | SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets; |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 2247 | |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 2248 | // Enter the preprocessor block. |
| 2249 | Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3); |
Michael J. Spencer | 4c0ffa8 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 2250 | |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 2251 | // If the preprocessor has a preprocessing record, emit it. |
| 2252 | unsigned NumPreprocessingRecords = 0; |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 2253 | using namespace llvm; |
| 2254 | |
| 2255 | // Set up the abbreviation for |
| 2256 | unsigned InclusionAbbrev = 0; |
| 2257 | { |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 2258 | auto *Abbrev = new BitCodeAbbrev(); |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 2259 | Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE)); |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 2260 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length |
| 2261 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes |
| 2262 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind |
Argyrios Kyrtzidis | f590e09 | 2012-10-02 16:10:46 +0000 | [diff] [blame] | 2263 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 2264 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 2265 | InclusionAbbrev = Stream.EmitAbbrev(Abbrev); |
| 2266 | } |
| 2267 | |
Douglas Gregor | 2f555fc | 2011-08-04 18:56:47 +0000 | [diff] [blame] | 2268 | unsigned FirstPreprocessorEntityID |
| 2269 | = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0) |
| 2270 | + NUM_PREDEF_PP_ENTITY_IDS; |
| 2271 | unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID; |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 2272 | RecordData Record; |
Argyrios Kyrtzidis | 7f44836 | 2011-09-19 20:40:42 +0000 | [diff] [blame] | 2273 | for (PreprocessingRecord::iterator E = PPRec.local_begin(), |
| 2274 | EEnd = PPRec.local_end(); |
Douglas Gregor | 0d4b431 | 2011-08-04 17:06:18 +0000 | [diff] [blame] | 2275 | E != EEnd; |
| 2276 | (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) { |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 2277 | Record.clear(); |
Michael J. Spencer | 4c0ffa8 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 2278 | |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 2279 | PreprocessedEntityOffsets.push_back( |
| 2280 | PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo())); |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 2281 | |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 2282 | if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) { |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 2283 | // Record this macro definition's ID. |
| 2284 | MacroDefinitions[MD] = NextPreprocessorEntityID; |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 2285 | |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 2286 | AddIdentifierRef(MD->getName(), Record); |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 2287 | Stream.EmitRecord(PPD_MACRO_DEFINITION, Record); |
| 2288 | continue; |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 2289 | } |
Michael J. Spencer | 4c0ffa8 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 2290 | |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 2291 | if (auto *ME = dyn_cast<MacroExpansion>(*E)) { |
Argyrios Kyrtzidis | 80f78b9 | 2011-09-08 17:18:41 +0000 | [diff] [blame] | 2292 | Record.push_back(ME->isBuiltinMacro()); |
| 2293 | if (ME->isBuiltinMacro()) |
| 2294 | AddIdentifierRef(ME->getName(), Record); |
| 2295 | else |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 2296 | Record.push_back(MacroDefinitions[ME->getDefinition()]); |
Chandler Carruth | f92ac9e | 2011-07-15 07:25:21 +0000 | [diff] [blame] | 2297 | Stream.EmitRecord(PPD_MACRO_EXPANSION, Record); |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 2298 | continue; |
| 2299 | } |
| 2300 | |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 2301 | if (auto *ID = dyn_cast<InclusionDirective>(*E)) { |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 2302 | Record.push_back(PPD_INCLUSION_DIRECTIVE); |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 2303 | Record.push_back(ID->getFileName().size()); |
| 2304 | Record.push_back(ID->wasInQuotes()); |
| 2305 | Record.push_back(static_cast<unsigned>(ID->getKind())); |
Argyrios Kyrtzidis | f590e09 | 2012-10-02 16:10:46 +0000 | [diff] [blame] | 2306 | Record.push_back(ID->importedModule()); |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2307 | SmallString<64> Buffer; |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 2308 | Buffer += ID->getFileName(); |
Argyrios Kyrtzidis | 8dbcfc3 | 2012-03-08 01:08:28 +0000 | [diff] [blame] | 2309 | // Check that the FileEntry is not null because it was not resolved and |
| 2310 | // we create a PCH even with compiler errors. |
| 2311 | if (ID->getFile()) |
| 2312 | Buffer += ID->getFile()->getName(); |
Mehdi Amini | 5ae4a85 | 2015-09-09 20:35:37 +0000 | [diff] [blame] | 2313 | Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer); |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 2314 | continue; |
| 2315 | } |
| 2316 | |
| 2317 | llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter"); |
| 2318 | } |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 2319 | Stream.ExitBlock(); |
Michael J. Spencer | 4c0ffa8 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 2320 | |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 2321 | // Write the offsets table for the preprocessing record. |
| 2322 | if (NumPreprocessingRecords > 0) { |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 2323 | assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords); |
| 2324 | |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 2325 | // Write the offsets table for identifier IDs. |
| 2326 | using namespace llvm; |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 2327 | |
| 2328 | auto *Abbrev = new BitCodeAbbrev(); |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 2329 | Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS)); |
Douglas Gregor | 2f555fc | 2011-08-04 18:56:47 +0000 | [diff] [blame] | 2330 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 2331 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 2332 | unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev); |
Michael J. Spencer | 4c0ffa8 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 2333 | |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 2334 | RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS, |
| 2335 | FirstPreprocessorEntityID - |
| 2336 | NUM_PREDEF_PP_ENTITY_IDS}; |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 2337 | Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record, |
Reid Kleckner | 012665e | 2015-05-21 00:13:09 +0000 | [diff] [blame] | 2338 | bytes(PreprocessedEntityOffsets)); |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 2339 | } |
Chris Lattner | eeffaef | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
Richard Smith | 386bb07 | 2015-08-18 23:42:23 +0000 | [diff] [blame] | 2342 | unsigned ASTWriter::getLocalOrImportedSubmoduleID(Module *Mod) { |
Richard Smith | 080056b | 2015-08-18 21:53:42 +0000 | [diff] [blame] | 2343 | if (!Mod) |
| 2344 | return 0; |
| 2345 | |
Douglas Gregor | a89c5ac | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 2346 | llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod); |
| 2347 | if (Known != SubmoduleIDs.end()) |
| 2348 | return Known->second; |
Richard Smith | 386bb07 | 2015-08-18 23:42:23 +0000 | [diff] [blame] | 2349 | |
| 2350 | if (Mod->getTopLevelModule() != WritingModule) |
| 2351 | return 0; |
| 2352 | |
Douglas Gregor | a89c5ac | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 2353 | return SubmoduleIDs[Mod] = NextSubmoduleID++; |
| 2354 | } |
| 2355 | |
Richard Smith | 386bb07 | 2015-08-18 23:42:23 +0000 | [diff] [blame] | 2356 | unsigned ASTWriter::getSubmoduleID(Module *Mod) { |
| 2357 | // FIXME: This can easily happen, if we have a reference to a submodule that |
| 2358 | // did not result in us loading a module file for that submodule. For |
| 2359 | // instance, a cross-top-level-module 'conflict' declaration will hit this. |
| 2360 | unsigned ID = getLocalOrImportedSubmoduleID(Mod); |
| 2361 | assert((ID || !Mod) && |
| 2362 | "asked for module ID for non-local, non-imported module"); |
| 2363 | return ID; |
Argyrios Kyrtzidis | b146baa | 2013-03-13 21:13:51 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
Douglas Gregor | 253eefe | 2011-12-01 00:59:36 +0000 | [diff] [blame] | 2366 | /// \brief Compute the number of modules within the given tree (including the |
| 2367 | /// given module). |
| 2368 | static unsigned getNumberOfModules(Module *Mod) { |
| 2369 | unsigned ChildModules = 0; |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 2370 | for (auto Sub = Mod->submodule_begin(), SubEnd = Mod->submodule_end(); |
Douglas Gregor | 253eefe | 2011-12-01 00:59:36 +0000 | [diff] [blame] | 2371 | Sub != SubEnd; ++Sub) |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 2372 | ChildModules += getNumberOfModules(*Sub); |
Douglas Gregor | 253eefe | 2011-12-01 00:59:36 +0000 | [diff] [blame] | 2373 | |
| 2374 | return ChildModules + 1; |
| 2375 | } |
| 2376 | |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 2377 | void ASTWriter::WriteSubmodules(Module *WritingModule) { |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 2378 | // Enter the submodule description block. |
Richard Smith | 202210b | 2014-10-24 20:23:01 +0000 | [diff] [blame] | 2379 | Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5); |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 2380 | |
| 2381 | // Write the abbreviations needed for the submodules block. |
| 2382 | using namespace llvm; |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 2383 | |
| 2384 | auto *Abbrev = new BitCodeAbbrev(); |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 2385 | Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION)); |
Douglas Gregor | a89c5ac | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 2386 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 2387 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent |
| 2388 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework |
| 2389 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit |
Richard Smith | 9bca298 | 2014-03-08 00:03:56 +0000 | [diff] [blame] | 2390 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem |
| 2391 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC |
Douglas Gregor | a686e1b | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 2392 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules... |
Douglas Gregor | 7344109 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 2393 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit... |
Douglas Gregor | 7344109 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 2394 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild... |
Douglas Gregor | 35b13ec | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 2395 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh... |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 2396 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name |
| 2397 | unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev); |
| 2398 | |
| 2399 | Abbrev = new BitCodeAbbrev(); |
Douglas Gregor | 524e33e | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 2400 | Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER)); |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 2401 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name |
| 2402 | unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev); |
| 2403 | |
| 2404 | Abbrev = new BitCodeAbbrev(); |
| 2405 | Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER)); |
| 2406 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name |
| 2407 | unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | 524e33e | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 2408 | |
| 2409 | Abbrev = new BitCodeAbbrev(); |
Argyrios Kyrtzidis | c597c8c | 2012-10-05 00:22:33 +0000 | [diff] [blame] | 2410 | Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER)); |
| 2411 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name |
| 2412 | unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev); |
| 2413 | |
| 2414 | Abbrev = new BitCodeAbbrev(); |
Douglas Gregor | 524e33e | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 2415 | Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR)); |
| 2416 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name |
| 2417 | unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev); |
| 2418 | |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 2419 | Abbrev = new BitCodeAbbrev(); |
| 2420 | Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES)); |
Richard Smith | a3feee2 | 2013-10-28 22:18:19 +0000 | [diff] [blame] | 2421 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State |
| 2422 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 2423 | unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev); |
| 2424 | |
Douglas Gregor | 5952766 | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 2425 | Abbrev = new BitCodeAbbrev(); |
| 2426 | Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER)); |
| 2427 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name |
| 2428 | unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev); |
| 2429 | |
Douglas Gregor | 6ddfca9 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 2430 | Abbrev = new BitCodeAbbrev(); |
Richard Smith | 306d892 | 2014-10-22 23:50:56 +0000 | [diff] [blame] | 2431 | Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER)); |
| 2432 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name |
| 2433 | unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev); |
| 2434 | |
| 2435 | Abbrev = new BitCodeAbbrev(); |
Lawrence Crowl | b53e548 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 2436 | Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER)); |
| 2437 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name |
| 2438 | unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev); |
| 2439 | |
| 2440 | Abbrev = new BitCodeAbbrev(); |
Richard Smith | 202210b | 2014-10-24 20:23:01 +0000 | [diff] [blame] | 2441 | Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER)); |
| 2442 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name |
| 2443 | unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev); |
| 2444 | |
| 2445 | Abbrev = new BitCodeAbbrev(); |
Douglas Gregor | 6ddfca9 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 2446 | Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY)); |
| 2447 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework |
| 2448 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name |
| 2449 | unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev); |
| 2450 | |
Douglas Gregor | 35b13ec | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 2451 | Abbrev = new BitCodeAbbrev(); |
| 2452 | Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO)); |
| 2453 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name |
| 2454 | unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev); |
| 2455 | |
Douglas Gregor | fb91265 | 2013-03-20 21:10:35 +0000 | [diff] [blame] | 2456 | Abbrev = new BitCodeAbbrev(); |
| 2457 | Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT)); |
| 2458 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module |
| 2459 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message |
| 2460 | unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev); |
| 2461 | |
Douglas Gregor | 253eefe | 2011-12-01 00:59:36 +0000 | [diff] [blame] | 2462 | // Write the submodule metadata block. |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 2463 | RecordData::value_type Record[] = {getNumberOfModules(WritingModule), |
| 2464 | FirstSubmoduleID - |
| 2465 | NUM_PREDEF_SUBMODULE_IDS}; |
Douglas Gregor | 253eefe | 2011-12-01 00:59:36 +0000 | [diff] [blame] | 2466 | Stream.EmitRecord(SUBMODULE_METADATA, Record); |
| 2467 | |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 2468 | // Write all of the submodules. |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 2469 | std::queue<Module *> Q; |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 2470 | Q.push(WritingModule); |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 2471 | while (!Q.empty()) { |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 2472 | Module *Mod = Q.front(); |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 2473 | Q.pop(); |
Douglas Gregor | a89c5ac | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 2474 | unsigned ID = getSubmoduleID(Mod); |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 2475 | |
| 2476 | uint64_t ParentID = 0; |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 2477 | if (Mod->Parent) { |
| 2478 | assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?"); |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 2479 | ParentID = SubmoduleIDs[Mod->Parent]; |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 2480 | } |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 2481 | |
| 2482 | // Emit the definition of the block. |
| 2483 | { |
| 2484 | RecordData::value_type Record[] = { |
| 2485 | SUBMODULE_DEFINITION, ID, ParentID, Mod->IsFramework, Mod->IsExplicit, |
| 2486 | Mod->IsSystem, Mod->IsExternC, Mod->InferSubmodules, |
| 2487 | Mod->InferExplicitSubmodules, Mod->InferExportWildcard, |
| 2488 | Mod->ConfigMacrosExhaustive}; |
| 2489 | Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name); |
| 2490 | } |
| 2491 | |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 2492 | // Emit the requirements. |
Richard Smith | 080056b | 2015-08-18 21:53:42 +0000 | [diff] [blame] | 2493 | for (const auto &R : Mod->Requirements) { |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 2494 | RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second}; |
Richard Smith | 080056b | 2015-08-18 21:53:42 +0000 | [diff] [blame] | 2495 | Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first); |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 2496 | } |
| 2497 | |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 2498 | // Emit the umbrella header, if there is one. |
Richard Smith | 2b63d15 | 2015-05-16 02:28:53 +0000 | [diff] [blame] | 2499 | if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) { |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 2500 | RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER}; |
Richard Smith | 2b63d15 | 2015-05-16 02:28:53 +0000 | [diff] [blame] | 2501 | Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record, |
| 2502 | UmbrellaHeader.NameAsWritten); |
| 2503 | } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) { |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 2504 | RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR}; |
| 2505 | Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record, |
Richard Smith | 2b63d15 | 2015-05-16 02:28:53 +0000 | [diff] [blame] | 2506 | UmbrellaDir.NameAsWritten); |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 2507 | } |
Richard Smith | 306d892 | 2014-10-22 23:50:56 +0000 | [diff] [blame] | 2508 | |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 2509 | // Emit the headers. |
Richard Smith | 202210b | 2014-10-24 20:23:01 +0000 | [diff] [blame] | 2510 | struct { |
Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 2511 | unsigned RecordKind; |
Richard Smith | 202210b | 2014-10-24 20:23:01 +0000 | [diff] [blame] | 2512 | unsigned Abbrev; |
Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 2513 | Module::HeaderKind HeaderKind; |
Richard Smith | 202210b | 2014-10-24 20:23:01 +0000 | [diff] [blame] | 2514 | } HeaderLists[] = { |
Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 2515 | {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal}, |
| 2516 | {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual}, |
| 2517 | {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private}, |
Richard Smith | 202210b | 2014-10-24 20:23:01 +0000 | [diff] [blame] | 2518 | {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev, |
Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 2519 | Module::HK_PrivateTextual}, |
| 2520 | {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded} |
Richard Smith | 202210b | 2014-10-24 20:23:01 +0000 | [diff] [blame] | 2521 | }; |
| 2522 | for (auto &HL : HeaderLists) { |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 2523 | RecordData::value_type Record[] = {HL.RecordKind}; |
Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 2524 | for (auto &H : Mod->Headers[HL.HeaderKind]) |
| 2525 | Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten); |
| 2526 | } |
| 2527 | |
| 2528 | // Emit the top headers. |
| 2529 | { |
| 2530 | auto TopHeaders = Mod->getTopHeaders(PP->getFileManager()); |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 2531 | RecordData::value_type Record[] = {SUBMODULE_TOPHEADER}; |
Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 2532 | for (auto *H : TopHeaders) |
| 2533 | Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName()); |
Argyrios Kyrtzidis | c597c8c | 2012-10-05 00:22:33 +0000 | [diff] [blame] | 2534 | } |
Douglas Gregor | 0093b3c | 2011-12-05 16:33:54 +0000 | [diff] [blame] | 2535 | |
| 2536 | // Emit the imports. |
| 2537 | if (!Mod->Imports.empty()) { |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 2538 | RecordData Record; |
Richard Smith | 080056b | 2015-08-18 21:53:42 +0000 | [diff] [blame] | 2539 | for (auto *I : Mod->Imports) |
| 2540 | Record.push_back(getSubmoduleID(I)); |
Douglas Gregor | 0093b3c | 2011-12-05 16:33:54 +0000 | [diff] [blame] | 2541 | Stream.EmitRecord(SUBMODULE_IMPORTS, Record); |
| 2542 | } |
| 2543 | |
Douglas Gregor | 24bb923 | 2011-12-02 18:58:38 +0000 | [diff] [blame] | 2544 | // Emit the exports. |
| 2545 | if (!Mod->Exports.empty()) { |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 2546 | RecordData Record; |
Richard Smith | 080056b | 2015-08-18 21:53:42 +0000 | [diff] [blame] | 2547 | for (const auto &E : Mod->Exports) { |
| 2548 | // FIXME: This may fail; we don't require that all exported modules |
| 2549 | // are local or imported. |
| 2550 | Record.push_back(getSubmoduleID(E.getPointer())); |
| 2551 | Record.push_back(E.getInt()); |
Douglas Gregor | 24bb923 | 2011-12-02 18:58:38 +0000 | [diff] [blame] | 2552 | } |
| 2553 | Stream.EmitRecord(SUBMODULE_EXPORTS, Record); |
| 2554 | } |
Douglas Gregor | 6ddfca9 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 2555 | |
Daniel Jasper | ba7f2f7 | 2013-09-24 09:14:14 +0000 | [diff] [blame] | 2556 | //FIXME: How do we emit the 'use'd modules? They may not be submodules. |
| 2557 | // Might be unnecessary as use declarations are only used to build the |
| 2558 | // module itself. |
| 2559 | |
Douglas Gregor | 6ddfca9 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 2560 | // Emit the link libraries. |
Richard Smith | 080056b | 2015-08-18 21:53:42 +0000 | [diff] [blame] | 2561 | for (const auto &LL : Mod->LinkLibraries) { |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 2562 | RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY, |
| 2563 | LL.IsFramework}; |
Richard Smith | 080056b | 2015-08-18 21:53:42 +0000 | [diff] [blame] | 2564 | Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library); |
Douglas Gregor | 6ddfca9 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 2565 | } |
| 2566 | |
Douglas Gregor | fb91265 | 2013-03-20 21:10:35 +0000 | [diff] [blame] | 2567 | // Emit the conflicts. |
Richard Smith | 080056b | 2015-08-18 21:53:42 +0000 | [diff] [blame] | 2568 | for (const auto &C : Mod->Conflicts) { |
Richard Smith | 080056b | 2015-08-18 21:53:42 +0000 | [diff] [blame] | 2569 | // FIXME: This may fail; we don't require that all conflicting modules |
| 2570 | // are local or imported. |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 2571 | RecordData::value_type Record[] = {SUBMODULE_CONFLICT, |
| 2572 | getSubmoduleID(C.Other)}; |
Richard Smith | 080056b | 2015-08-18 21:53:42 +0000 | [diff] [blame] | 2573 | Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message); |
Douglas Gregor | fb91265 | 2013-03-20 21:10:35 +0000 | [diff] [blame] | 2574 | } |
| 2575 | |
Douglas Gregor | 35b13ec | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 2576 | // Emit the configuration macros. |
Richard Smith | 080056b | 2015-08-18 21:53:42 +0000 | [diff] [blame] | 2577 | for (const auto &CM : Mod->ConfigMacros) { |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 2578 | RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO}; |
Richard Smith | 080056b | 2015-08-18 21:53:42 +0000 | [diff] [blame] | 2579 | Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM); |
Douglas Gregor | 35b13ec | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 2580 | } |
| 2581 | |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 2582 | // Queue up the submodules of this module. |
Richard Smith | 080056b | 2015-08-18 21:53:42 +0000 | [diff] [blame] | 2583 | for (auto *M : Mod->submodules()) |
| 2584 | Q.push(M); |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | Stream.ExitBlock(); |
Richard Smith | 23d8d03 | 2015-05-14 00:45:20 +0000 | [diff] [blame] | 2588 | |
Richard Smith | 23d8d03 | 2015-05-14 00:45:20 +0000 | [diff] [blame] | 2589 | assert((NextSubmoduleID - FirstSubmoduleID == |
| 2590 | getNumberOfModules(WritingModule)) && |
| 2591 | "Wrong # of submodules; found a reference to a non-local, " |
| 2592 | "non-imported submodule?"); |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 2593 | } |
| 2594 | |
Douglas Gregor | a28bcdd | 2011-12-01 02:07:58 +0000 | [diff] [blame] | 2595 | serialization::SubmoduleID |
| 2596 | ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) { |
Douglas Gregor | a89c5ac | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 2597 | if (Loc.isInvalid() || !WritingModule) |
Douglas Gregor | a28bcdd | 2011-12-01 02:07:58 +0000 | [diff] [blame] | 2598 | return 0; // No submodule |
Douglas Gregor | 0093b3c | 2011-12-05 16:33:54 +0000 | [diff] [blame] | 2599 | |
| 2600 | // Find the module that owns this location. |
Douglas Gregor | a28bcdd | 2011-12-01 02:07:58 +0000 | [diff] [blame] | 2601 | ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap(); |
Douglas Gregor | 0093b3c | 2011-12-05 16:33:54 +0000 | [diff] [blame] | 2602 | Module *OwningMod |
| 2603 | = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager())); |
Douglas Gregor | a28bcdd | 2011-12-01 02:07:58 +0000 | [diff] [blame] | 2604 | if (!OwningMod) |
| 2605 | return 0; |
| 2606 | |
Douglas Gregor | a89c5ac | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 2607 | // Check whether this submodule is part of our own module. |
| 2608 | if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule)) |
Douglas Gregor | a28bcdd | 2011-12-01 02:07:58 +0000 | [diff] [blame] | 2609 | return 0; |
| 2610 | |
Douglas Gregor | a89c5ac | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 2611 | return getSubmoduleID(OwningMod); |
Douglas Gregor | a28bcdd | 2011-12-01 02:07:58 +0000 | [diff] [blame] | 2612 | } |
| 2613 | |
Argyrios Kyrtzidis | 0f06b98 | 2013-03-27 17:17:23 +0000 | [diff] [blame] | 2614 | void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag, |
| 2615 | bool isModule) { |
| 2616 | // Make sure set diagnostic pragmas don't affect the translation unit that |
| 2617 | // imports the module. |
| 2618 | // FIXME: Make diagnostic pragma sections work properly with modules. |
| 2619 | if (isModule) |
| 2620 | return; |
| 2621 | |
Argyrios Kyrtzidis | efaa54a | 2012-10-30 00:27:21 +0000 | [diff] [blame] | 2622 | llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64> |
| 2623 | DiagStateIDMap; |
| 2624 | unsigned CurrID = 0; |
| 2625 | DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one. |
Argyrios Kyrtzidis | 452707c | 2010-11-05 22:10:18 +0000 | [diff] [blame] | 2626 | RecordData Record; |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 2627 | for (DiagnosticsEngine::DiagStatePointsTy::const_iterator |
Argyrios Kyrtzidis | 243aedb | 2011-01-14 20:54:07 +0000 | [diff] [blame] | 2628 | I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end(); |
| 2629 | I != E; ++I) { |
Argyrios Kyrtzidis | efaa54a | 2012-10-30 00:27:21 +0000 | [diff] [blame] | 2630 | const DiagnosticsEngine::DiagStatePoint &point = *I; |
Argyrios Kyrtzidis | 243aedb | 2011-01-14 20:54:07 +0000 | [diff] [blame] | 2631 | if (point.Loc.isInvalid()) |
| 2632 | continue; |
| 2633 | |
| 2634 | Record.push_back(point.Loc.getRawEncoding()); |
Argyrios Kyrtzidis | efaa54a | 2012-10-30 00:27:21 +0000 | [diff] [blame] | 2635 | unsigned &DiagStateID = DiagStateIDMap[point.State]; |
| 2636 | Record.push_back(DiagStateID); |
| 2637 | |
| 2638 | if (DiagStateID == 0) { |
| 2639 | DiagStateID = ++CurrID; |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 2640 | for (const auto &I : *(point.State)) { |
| 2641 | if (I.second.isPragma()) { |
| 2642 | Record.push_back(I.first); |
| 2643 | Record.push_back((unsigned)I.second.getSeverity()); |
Argyrios Kyrtzidis | efaa54a | 2012-10-30 00:27:21 +0000 | [diff] [blame] | 2644 | } |
Argyrios Kyrtzidis | 243aedb | 2011-01-14 20:54:07 +0000 | [diff] [blame] | 2645 | } |
Argyrios Kyrtzidis | efaa54a | 2012-10-30 00:27:21 +0000 | [diff] [blame] | 2646 | Record.push_back(-1); // mark the end of the diag/map pairs for this |
| 2647 | // location. |
Argyrios Kyrtzidis | 452707c | 2010-11-05 22:10:18 +0000 | [diff] [blame] | 2648 | } |
| 2649 | } |
| 2650 | |
Argyrios Kyrtzidis | b0ca9eb | 2010-11-05 22:20:49 +0000 | [diff] [blame] | 2651 | if (!Record.empty()) |
Argyrios Kyrtzidis | 243aedb | 2011-01-14 20:54:07 +0000 | [diff] [blame] | 2652 | Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record); |
Argyrios Kyrtzidis | 452707c | 2010-11-05 22:10:18 +0000 | [diff] [blame] | 2653 | } |
| 2654 | |
Richard Smith | c2bb818 | 2015-03-24 06:36:48 +0000 | [diff] [blame] | 2655 | void ASTWriter::WriteCXXCtorInitializersOffsets() { |
| 2656 | if (CXXCtorInitializersOffsets.empty()) |
| 2657 | return; |
| 2658 | |
Richard Smith | c2bb818 | 2015-03-24 06:36:48 +0000 | [diff] [blame] | 2659 | // Create a blob abbreviation for the C++ ctor initializer offsets. |
| 2660 | using namespace llvm; |
| 2661 | |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 2662 | auto *Abbrev = new BitCodeAbbrev(); |
Richard Smith | c2bb818 | 2015-03-24 06:36:48 +0000 | [diff] [blame] | 2663 | Abbrev->Add(BitCodeAbbrevOp(CXX_CTOR_INITIALIZERS_OFFSETS)); |
| 2664 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size |
| 2665 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 2666 | unsigned CtorInitializersOffsetAbbrev = Stream.EmitAbbrev(Abbrev); |
| 2667 | |
| 2668 | // Write the base specifier offsets table. |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 2669 | RecordData::value_type Record[] = {CXX_CTOR_INITIALIZERS_OFFSETS, |
| 2670 | CXXCtorInitializersOffsets.size()}; |
Richard Smith | c2bb818 | 2015-03-24 06:36:48 +0000 | [diff] [blame] | 2671 | Stream.EmitRecordWithBlob(CtorInitializersOffsetAbbrev, Record, |
Reid Kleckner | 012665e | 2015-05-21 00:13:09 +0000 | [diff] [blame] | 2672 | bytes(CXXCtorInitializersOffsets)); |
Richard Smith | c2bb818 | 2015-03-24 06:36:48 +0000 | [diff] [blame] | 2673 | } |
| 2674 | |
Anders Carlsson | 9bb83e8 | 2011-03-06 18:41:18 +0000 | [diff] [blame] | 2675 | void ASTWriter::WriteCXXBaseSpecifiersOffsets() { |
| 2676 | if (CXXBaseSpecifiersOffsets.empty()) |
| 2677 | return; |
| 2678 | |
Anders Carlsson | 9bb83e8 | 2011-03-06 18:41:18 +0000 | [diff] [blame] | 2679 | // Create a blob abbreviation for the C++ base specifiers offsets. |
| 2680 | using namespace llvm; |
| 2681 | |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 2682 | auto *Abbrev = new BitCodeAbbrev(); |
Anders Carlsson | 9bb83e8 | 2011-03-06 18:41:18 +0000 | [diff] [blame] | 2683 | Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS)); |
| 2684 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size |
| 2685 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 2686 | unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev); |
| 2687 | |
Douglas Gregor | c27b287 | 2011-08-04 00:01:48 +0000 | [diff] [blame] | 2688 | // Write the base specifier offsets table. |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 2689 | RecordData::value_type Record[] = {CXX_BASE_SPECIFIER_OFFSETS, |
| 2690 | CXXBaseSpecifiersOffsets.size()}; |
Anders Carlsson | 9bb83e8 | 2011-03-06 18:41:18 +0000 | [diff] [blame] | 2691 | Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record, |
Reid Kleckner | 012665e | 2015-05-21 00:13:09 +0000 | [diff] [blame] | 2692 | bytes(CXXBaseSpecifiersOffsets)); |
Anders Carlsson | 9bb83e8 | 2011-03-06 18:41:18 +0000 | [diff] [blame] | 2693 | } |
| 2694 | |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 2695 | //===----------------------------------------------------------------------===// |
| 2696 | // Type Serialization |
| 2697 | //===----------------------------------------------------------------------===// |
Chris Lattner | eeffaef | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 2698 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2699 | /// \brief Write the representation of a type to the AST stream. |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 2700 | void ASTWriter::WriteType(QualType T) { |
Argyrios Kyrtzidis | a7fbbb0 | 2010-08-20 16:04:04 +0000 | [diff] [blame] | 2701 | TypeIdx &Idx = TypeIdxs[T]; |
Argyrios Kyrtzidis | bb5c7eae | 2010-08-20 16:03:59 +0000 | [diff] [blame] | 2702 | if (Idx.getIndex() == 0) // we haven't seen this type before. |
| 2703 | Idx = TypeIdx(NextTypeID++); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2704 | |
Douglas Gregor | 9b3932c | 2010-10-05 18:37:06 +0000 | [diff] [blame] | 2705 | assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST"); |
Douglas Gregor | dc72caa | 2010-10-04 18:21:45 +0000 | [diff] [blame] | 2706 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2707 | // Record the offset for this type. |
Argyrios Kyrtzidis | bb5c7eae | 2010-08-20 16:03:59 +0000 | [diff] [blame] | 2708 | unsigned Index = Idx.getIndex() - FirstTypeID; |
Sebastian Redl | 66c5eef | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 2709 | if (TypeOffsets.size() == Index) |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 2710 | TypeOffsets.push_back(Stream.GetCurrentBitNo()); |
Sebastian Redl | 66c5eef | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 2711 | else if (TypeOffsets.size() < Index) { |
| 2712 | TypeOffsets.resize(Index + 1); |
| 2713 | TypeOffsets[Index] = Stream.GetCurrentBitNo(); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2714 | } |
| 2715 | |
| 2716 | RecordData Record; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2717 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2718 | // Emit the type's representation. |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2719 | ASTTypeWriter W(*this, Record); |
Richard Smith | 01b2cb4 | 2014-07-26 06:37:51 +0000 | [diff] [blame] | 2720 | W.AbbrevToUse = 0; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2721 | |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2722 | if (T.hasLocalNonFastQualifiers()) { |
| 2723 | Qualifiers Qs = T.getLocalQualifiers(); |
| 2724 | AddTypeRef(T.getLocalUnqualifiedType(), Record); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2725 | Record.push_back(Qs.getAsOpaqueValue()); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2726 | W.Code = TYPE_EXT_QUAL; |
Richard Smith | 01b2cb4 | 2014-07-26 06:37:51 +0000 | [diff] [blame] | 2727 | W.AbbrevToUse = TypeExtQualAbbrev; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2728 | } else { |
| 2729 | switch (T->getTypeClass()) { |
| 2730 | // For all of the concrete, non-dependent types, call the |
| 2731 | // appropriate visitor function. |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2732 | #define TYPE(Class, Base) \ |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 2733 | case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2734 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2735 | #include "clang/AST/TypeNodes.def" |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2736 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2737 | } |
| 2738 | |
| 2739 | // Emit the serialized record. |
Richard Smith | 01b2cb4 | 2014-07-26 06:37:51 +0000 | [diff] [blame] | 2740 | Stream.EmitRecord(W.Code, Record, W.AbbrevToUse); |
Douglas Gregor | feb84b0 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 2741 | |
| 2742 | // Flush any expressions that were written as part of this type. |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 2743 | FlushStmts(); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2744 | } |
| 2745 | |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 2746 | //===----------------------------------------------------------------------===// |
| 2747 | // Declaration Serialization |
| 2748 | //===----------------------------------------------------------------------===// |
| 2749 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2750 | /// \brief Write the block containing all of the declaration IDs |
| 2751 | /// lexically declared within the given DeclContext. |
| 2752 | /// |
| 2753 | /// \returns the offset of the DECL_CONTEXT_LEXICAL block within the |
| 2754 | /// bistream, or 0 if no block was written. |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 2755 | uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context, |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2756 | DeclContext *DC) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2757 | if (DC->decls_empty()) |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2758 | return 0; |
| 2759 | |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 2760 | uint64_t Offset = Stream.GetCurrentBitNo(); |
Richard Smith | 82f8fcd | 2015-08-06 22:07:25 +0000 | [diff] [blame] | 2761 | SmallVector<uint32_t, 128> KindDeclPairs; |
| 2762 | for (const auto *D : DC->decls()) { |
| 2763 | KindDeclPairs.push_back(D->getKind()); |
| 2764 | KindDeclPairs.push_back(GetDeclRef(D)); |
| 2765 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2766 | |
Douglas Gregor | a57c3ab | 2009-04-22 22:34:57 +0000 | [diff] [blame] | 2767 | ++NumLexicalDeclContexts; |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 2768 | RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL}; |
Richard Smith | 82f8fcd | 2015-08-06 22:07:25 +0000 | [diff] [blame] | 2769 | Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, |
| 2770 | bytes(KindDeclPairs)); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2771 | return Offset; |
| 2772 | } |
| 2773 | |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 2774 | void ASTWriter::WriteTypeDeclOffsets() { |
Sebastian Redl | 1ea025b | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 2775 | using namespace llvm; |
Sebastian Redl | 1ea025b | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 2776 | |
| 2777 | // Write the type offsets array |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 2778 | auto *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2779 | Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET)); |
Sebastian Redl | 1ea025b | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 2780 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types |
Douglas Gregor | 5204bde | 2011-08-02 16:26:37 +0000 | [diff] [blame] | 2781 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index |
Sebastian Redl | 1ea025b | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 2782 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block |
| 2783 | unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev); |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 2784 | { |
| 2785 | RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(), |
| 2786 | FirstTypeID - NUM_PREDEF_TYPE_IDS}; |
| 2787 | Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets)); |
| 2788 | } |
Sebastian Redl | 1ea025b | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 2789 | |
| 2790 | // Write the declaration offsets array |
| 2791 | Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2792 | Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET)); |
Sebastian Redl | 1ea025b | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 2793 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations |
Douglas Gregor | f718062 | 2011-08-03 15:48:04 +0000 | [diff] [blame] | 2794 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID |
Sebastian Redl | 1ea025b | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 2795 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block |
| 2796 | unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev); |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 2797 | { |
| 2798 | RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(), |
| 2799 | FirstDeclID - NUM_PREDEF_DECL_IDS}; |
| 2800 | Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets)); |
| 2801 | } |
Sebastian Redl | 1ea025b | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 2802 | } |
| 2803 | |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 2804 | void ASTWriter::WriteFileDeclIDsMap() { |
| 2805 | using namespace llvm; |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 2806 | |
Chandler Carruth | b306d73 | 2015-03-27 00:31:20 +0000 | [diff] [blame] | 2807 | SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs( |
| 2808 | FileDeclIDs.begin(), FileDeclIDs.end()); |
| 2809 | std::sort(SortedFileDeclIDs.begin(), SortedFileDeclIDs.end(), |
| 2810 | llvm::less_first()); |
| 2811 | |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 2812 | // Join the vectors of DeclIDs from all files. |
Chandler Carruth | b306d73 | 2015-03-27 00:31:20 +0000 | [diff] [blame] | 2813 | SmallVector<DeclID, 256> FileGroupedDeclIDs; |
| 2814 | for (auto &FileDeclEntry : SortedFileDeclIDs) { |
| 2815 | DeclIDInFileInfo &Info = *FileDeclEntry.second; |
| 2816 | Info.FirstDeclIndex = FileGroupedDeclIDs.size(); |
| 2817 | for (auto &LocDeclEntry : Info.DeclIDs) |
| 2818 | FileGroupedDeclIDs.push_back(LocDeclEntry.second); |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 2819 | } |
| 2820 | |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 2821 | auto *Abbrev = new BitCodeAbbrev(); |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 2822 | Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS)); |
Argyrios Kyrtzidis | 10e7846 | 2012-10-02 21:09:13 +0000 | [diff] [blame] | 2823 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 2824 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 2825 | unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev); |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 2826 | RecordData::value_type Record[] = {FILE_SORTED_DECLS, |
| 2827 | FileGroupedDeclIDs.size()}; |
Reid Kleckner | 012665e | 2015-05-21 00:13:09 +0000 | [diff] [blame] | 2828 | Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs)); |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 2829 | } |
| 2830 | |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 2831 | void ASTWriter::WriteComments() { |
| 2832 | Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3); |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 2833 | ArrayRef<RawComment *> RawComments = Context->Comments.getComments(); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 2834 | RecordData Record; |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 2835 | for (const auto *I : RawComments) { |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 2836 | Record.clear(); |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 2837 | AddSourceRange(I->getSourceRange(), Record); |
| 2838 | Record.push_back(I->getKind()); |
| 2839 | Record.push_back(I->isTrailingComment()); |
| 2840 | Record.push_back(I->isAlmostTrailingComment()); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 2841 | Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record); |
| 2842 | } |
| 2843 | Stream.ExitBlock(); |
| 2844 | } |
| 2845 | |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 2846 | //===----------------------------------------------------------------------===// |
| 2847 | // Global Method Pool and Selector Serialization |
| 2848 | //===----------------------------------------------------------------------===// |
| 2849 | |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2850 | namespace { |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2851 | // Trait used for the on-disk hash table used in the method pool. |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2852 | class ASTMethodPoolTrait { |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 2853 | ASTWriter &Writer; |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2854 | |
| 2855 | public: |
| 2856 | typedef Selector key_type; |
| 2857 | typedef key_type key_type_ref; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2858 | |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 2859 | struct data_type { |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2860 | SelectorID ID; |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 2861 | ObjCMethodList Instance, Factory; |
| 2862 | }; |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2863 | typedef const data_type& data_type_ref; |
| 2864 | |
Justin Bogner | 25463f1 | 2014-04-18 20:27:24 +0000 | [diff] [blame] | 2865 | typedef unsigned hash_value_type; |
| 2866 | typedef unsigned offset_type; |
| 2867 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2868 | explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2869 | |
Justin Bogner | 25463f1 | 2014-04-18 20:27:24 +0000 | [diff] [blame] | 2870 | static hash_value_type ComputeHash(Selector Sel) { |
Argyrios Kyrtzidis | 4bd9710 | 2010-08-20 16:03:52 +0000 | [diff] [blame] | 2871 | return serialization::ComputeHash(Sel); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2872 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2873 | |
| 2874 | std::pair<unsigned,unsigned> |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2875 | EmitKeyDataLength(raw_ostream& Out, Selector Sel, |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2876 | data_type_ref Methods) { |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 2877 | using namespace llvm::support; |
| 2878 | endian::Writer<little> LE(Out); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2879 | unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4); |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 2880 | LE.write<uint16_t>(KeyLen); |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 2881 | unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts |
| 2882 | for (const ObjCMethodList *Method = &Methods.Instance; Method; |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 2883 | Method = Method->getNext()) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 2884 | if (Method->getMethod()) |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2885 | DataLen += 4; |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 2886 | for (const ObjCMethodList *Method = &Methods.Factory; Method; |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 2887 | Method = Method->getNext()) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 2888 | if (Method->getMethod()) |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2889 | DataLen += 4; |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 2890 | LE.write<uint16_t>(DataLen); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2891 | return std::make_pair(KeyLen, DataLen); |
| 2892 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2893 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2894 | void EmitKey(raw_ostream& Out, Selector Sel, unsigned) { |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 2895 | using namespace llvm::support; |
| 2896 | endian::Writer<little> LE(Out); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2897 | uint64_t Start = Out.tell(); |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2898 | assert((Start >> 32) == 0 && "Selector key offset too large"); |
| 2899 | Writer.SetSelectorOffset(Sel, Start); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2900 | unsigned N = Sel.getNumArgs(); |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 2901 | LE.write<uint16_t>(N); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2902 | if (N == 0) |
| 2903 | N = 1; |
| 2904 | for (unsigned I = 0; I != N; ++I) |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 2905 | LE.write<uint32_t>( |
| 2906 | Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I))); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2907 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2908 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2909 | void EmitData(raw_ostream& Out, key_type_ref, |
Douglas Gregor | 4647cfa | 2009-04-24 21:49:02 +0000 | [diff] [blame] | 2910 | data_type_ref Methods, unsigned DataLen) { |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 2911 | using namespace llvm::support; |
| 2912 | endian::Writer<little> LE(Out); |
Douglas Gregor | 4647cfa | 2009-04-24 21:49:02 +0000 | [diff] [blame] | 2913 | uint64_t Start = Out.tell(); (void)Start; |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 2914 | LE.write<uint32_t>(Methods.ID); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2915 | unsigned NumInstanceMethods = 0; |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 2916 | for (const ObjCMethodList *Method = &Methods.Instance; Method; |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 2917 | Method = Method->getNext()) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 2918 | if (Method->getMethod()) |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2919 | ++NumInstanceMethods; |
| 2920 | |
| 2921 | unsigned NumFactoryMethods = 0; |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 2922 | for (const ObjCMethodList *Method = &Methods.Factory; Method; |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 2923 | Method = Method->getNext()) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 2924 | if (Method->getMethod()) |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2925 | ++NumFactoryMethods; |
| 2926 | |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 2927 | unsigned InstanceBits = Methods.Instance.getBits(); |
| 2928 | assert(InstanceBits < 4); |
Nico Weber | ff4b35e | 2014-12-27 22:14:15 +0000 | [diff] [blame] | 2929 | unsigned InstanceHasMoreThanOneDeclBit = |
| 2930 | Methods.Instance.hasMoreThanOneDecl(); |
| 2931 | unsigned FullInstanceBits = (NumInstanceMethods << 3) | |
| 2932 | (InstanceHasMoreThanOneDeclBit << 2) | |
| 2933 | InstanceBits; |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 2934 | unsigned FactoryBits = Methods.Factory.getBits(); |
| 2935 | assert(FactoryBits < 4); |
Nico Weber | ff4b35e | 2014-12-27 22:14:15 +0000 | [diff] [blame] | 2936 | unsigned FactoryHasMoreThanOneDeclBit = |
| 2937 | Methods.Factory.hasMoreThanOneDecl(); |
| 2938 | unsigned FullFactoryBits = (NumFactoryMethods << 3) | |
| 2939 | (FactoryHasMoreThanOneDeclBit << 2) | |
| 2940 | FactoryBits; |
| 2941 | LE.write<uint16_t>(FullInstanceBits); |
| 2942 | LE.write<uint16_t>(FullFactoryBits); |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 2943 | for (const ObjCMethodList *Method = &Methods.Instance; Method; |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 2944 | Method = Method->getNext()) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 2945 | if (Method->getMethod()) |
| 2946 | LE.write<uint32_t>(Writer.getDeclID(Method->getMethod())); |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 2947 | for (const ObjCMethodList *Method = &Methods.Factory; Method; |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 2948 | Method = Method->getNext()) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 2949 | if (Method->getMethod()) |
| 2950 | LE.write<uint32_t>(Writer.getDeclID(Method->getMethod())); |
Douglas Gregor | 4647cfa | 2009-04-24 21:49:02 +0000 | [diff] [blame] | 2951 | |
| 2952 | assert(Out.tell() - Start == DataLen && "Data length is wrong"); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2953 | } |
| 2954 | }; |
| 2955 | } // end anonymous namespace |
| 2956 | |
Sebastian Redl | a19a67f | 2010-08-03 21:58:15 +0000 | [diff] [blame] | 2957 | /// \brief Write ObjC data: selectors and the method pool. |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2958 | /// |
| 2959 | /// The method pool contains both instance and factory methods, stored |
Sebastian Redl | a19a67f | 2010-08-03 21:58:15 +0000 | [diff] [blame] | 2960 | /// in an on-disk hash table indexed by the selector. The hash table also |
| 2961 | /// contains an empty entry for every other selector known to Sema. |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 2962 | void ASTWriter::WriteSelectors(Sema &SemaRef) { |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2963 | using namespace llvm; |
| 2964 | |
Sebastian Redl | a19a67f | 2010-08-03 21:58:15 +0000 | [diff] [blame] | 2965 | // Do we have to do anything at all? |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 2966 | if (SemaRef.MethodPool.empty() && SelectorIDs.empty()) |
Sebastian Redl | a19a67f | 2010-08-03 21:58:15 +0000 | [diff] [blame] | 2967 | return; |
Sebastian Redl | d95a56e | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 2968 | unsigned NumTableEntries = 0; |
Sebastian Redl | a19a67f | 2010-08-03 21:58:15 +0000 | [diff] [blame] | 2969 | // Create and write out the blob that contains selectors and the method pool. |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2970 | { |
Justin Bogner | bb094f0 | 2014-04-18 19:57:06 +0000 | [diff] [blame] | 2971 | llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator; |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 2972 | ASTMethodPoolTrait Trait(*this); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2973 | |
Sebastian Redl | a19a67f | 2010-08-03 21:58:15 +0000 | [diff] [blame] | 2974 | // Create the on-disk hash table representation. We walk through every |
| 2975 | // selector we've seen and look it up in the method pool. |
Sebastian Redl | d95a56e | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 2976 | SelectorOffsets.resize(NextSelectorID - FirstSelectorID); |
Chandler Carruth | acbbeb9 | 2015-03-27 00:47:43 +0000 | [diff] [blame] | 2977 | for (auto &SelectorAndID : SelectorIDs) { |
| 2978 | Selector S = SelectorAndID.first; |
| 2979 | SelectorID ID = SelectorAndID.second; |
Sebastian Redl | a19a67f | 2010-08-03 21:58:15 +0000 | [diff] [blame] | 2980 | Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S); |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2981 | ASTMethodPoolTrait::data_type Data = { |
Chandler Carruth | acbbeb9 | 2015-03-27 00:47:43 +0000 | [diff] [blame] | 2982 | ID, |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 2983 | ObjCMethodList(), |
| 2984 | ObjCMethodList() |
| 2985 | }; |
| 2986 | if (F != SemaRef.MethodPool.end()) { |
| 2987 | Data.Instance = F->second.first; |
| 2988 | Data.Factory = F->second.second; |
| 2989 | } |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2990 | // Only write this selector if it's not in an existing AST or something |
Sebastian Redl | d95a56e | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 2991 | // changed. |
Chandler Carruth | acbbeb9 | 2015-03-27 00:47:43 +0000 | [diff] [blame] | 2992 | if (Chain && ID < FirstSelectorID) { |
Sebastian Redl | d95a56e | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 2993 | // Selector already exists. Did it change? |
| 2994 | bool changed = false; |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 2995 | for (ObjCMethodList *M = &Data.Instance; |
| 2996 | !changed && M && M->getMethod(); M = M->getNext()) { |
| 2997 | if (!M->getMethod()->isFromASTFile()) |
Sebastian Redl | d95a56e | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 2998 | changed = true; |
| 2999 | } |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3000 | for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod(); |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 3001 | M = M->getNext()) { |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3002 | if (!M->getMethod()->isFromASTFile()) |
Sebastian Redl | d95a56e | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 3003 | changed = true; |
| 3004 | } |
| 3005 | if (!changed) |
| 3006 | continue; |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3007 | } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) { |
Sebastian Redl | 6e1a2a0 | 2010-08-04 21:22:45 +0000 | [diff] [blame] | 3008 | // A new method pool entry. |
| 3009 | ++NumTableEntries; |
Sebastian Redl | d95a56e | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 3010 | } |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3011 | Generator.insert(S, Data, Trait); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3012 | } |
| 3013 | |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3014 | // Create the on-disk hash table in a buffer. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3015 | SmallString<4096> MethodPool; |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3016 | uint32_t BucketOffset; |
| 3017 | { |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 3018 | using namespace llvm::support; |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 3019 | ASTMethodPoolTrait Trait(*this); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3020 | llvm::raw_svector_ostream Out(MethodPool); |
| 3021 | // Make sure that no bucket is at offset 0 |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 3022 | endian::Writer<little>(Out).write<uint32_t>(0); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3023 | BucketOffset = Generator.Emit(Out, Trait); |
| 3024 | } |
| 3025 | |
| 3026 | // Create a blob abbreviation |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 3027 | auto *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3028 | Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL)); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3029 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3030 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3031 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 3032 | unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev); |
| 3033 | |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3034 | // Write the method pool |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 3035 | { |
| 3036 | RecordData::value_type Record[] = {METHOD_POOL, BucketOffset, |
| 3037 | NumTableEntries}; |
| 3038 | Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool); |
| 3039 | } |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3040 | |
| 3041 | // Create a blob abbreviation for the selector table offsets. |
| 3042 | Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3043 | Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS)); |
Douglas Gregor | d4c5ed0 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 3044 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size |
Douglas Gregor | 8f364fb | 2011-08-03 23:28:44 +0000 | [diff] [blame] | 3045 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3046 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 3047 | unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev); |
| 3048 | |
| 3049 | // Write the selector offsets table. |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 3050 | { |
| 3051 | RecordData::value_type Record[] = { |
| 3052 | SELECTOR_OFFSETS, SelectorOffsets.size(), |
| 3053 | FirstSelectorID - NUM_PREDEF_SELECTOR_IDS}; |
| 3054 | Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record, |
| 3055 | bytes(SelectorOffsets)); |
| 3056 | } |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3057 | } |
| 3058 | } |
| 3059 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 3060 | /// \brief Write the selectors referenced in @selector expression into AST file. |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 3061 | void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) { |
Fariborz Jahanian | c51609a | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 3062 | using namespace llvm; |
| 3063 | if (SemaRef.ReferencedSelectors.empty()) |
| 3064 | return; |
Sebastian Redl | ada023c | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 3065 | |
Fariborz Jahanian | c51609a | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 3066 | RecordData Record; |
Sebastian Redl | ada023c | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 3067 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 3068 | // Note: this writes out all references even for a dependent AST. But it is |
Sebastian Redl | 51c79d8 | 2010-08-04 22:21:29 +0000 | [diff] [blame] | 3069 | // very tricky to fix, and given that @selector shouldn't really appear in |
| 3070 | // headers, probably not worth it. It's not a correctness issue. |
Chandler Carruth | 12c8f65 | 2015-03-27 00:55:05 +0000 | [diff] [blame] | 3071 | for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) { |
| 3072 | Selector Sel = SelectorAndLocation.first; |
| 3073 | SourceLocation Loc = SelectorAndLocation.second; |
Fariborz Jahanian | c51609a | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 3074 | AddSelectorRef(Sel, Record); |
| 3075 | AddSourceLocation(Loc, Record); |
| 3076 | } |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3077 | Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record); |
Fariborz Jahanian | c51609a | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 3078 | } |
| 3079 | |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 3080 | //===----------------------------------------------------------------------===// |
| 3081 | // Identifier Table Serialization |
| 3082 | //===----------------------------------------------------------------------===// |
| 3083 | |
Richard Smith | b376191 | 2015-02-05 23:08:52 +0000 | [diff] [blame] | 3084 | /// Determine the declaration that should be put into the name lookup table to |
| 3085 | /// represent the given declaration in this module. This is usually D itself, |
| 3086 | /// but if D was imported and merged into a local declaration, we want the most |
| 3087 | /// recent local declaration instead. The chosen declaration will be the most |
| 3088 | /// recent declaration in any module that imports this one. |
| 3089 | static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts, |
| 3090 | NamedDecl *D) { |
| 3091 | if (!LangOpts.Modules || !D->isFromASTFile()) |
| 3092 | return D; |
| 3093 | |
| 3094 | if (Decl *Redecl = D->getPreviousDecl()) { |
| 3095 | // For Redeclarable decls, a prior declaration might be local. |
| 3096 | for (; Redecl; Redecl = Redecl->getPreviousDecl()) { |
| 3097 | if (!Redecl->isFromASTFile()) |
| 3098 | return cast<NamedDecl>(Redecl); |
Richard Smith | fe620d2 | 2015-03-05 23:24:12 +0000 | [diff] [blame] | 3099 | // If we find a decl from a (chained-)PCH stop since we won't find a |
Richard Smith | b376191 | 2015-02-05 23:08:52 +0000 | [diff] [blame] | 3100 | // local one. |
| 3101 | if (D->getOwningModuleID() == 0) |
| 3102 | break; |
| 3103 | } |
| 3104 | } else if (Decl *First = D->getCanonicalDecl()) { |
| 3105 | // For Mergeable decls, the first decl might be local. |
| 3106 | if (!First->isFromASTFile()) |
| 3107 | return cast<NamedDecl>(First); |
| 3108 | } |
| 3109 | |
| 3110 | // All declarations are imported. Our most recent declaration will also be |
| 3111 | // the most recent one in anyone who imports us. |
| 3112 | return D; |
| 3113 | } |
| 3114 | |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3115 | namespace { |
Richard Smith | cf97cf6 | 2015-04-19 01:34:23 +0000 | [diff] [blame] | 3116 | class ASTIdentifierTableTrait { |
| 3117 | ASTWriter &Writer; |
| 3118 | Preprocessor &PP; |
| 3119 | IdentifierResolver &IdResolver; |
Richard Smith | 9c25418 | 2015-07-19 21:41:12 +0000 | [diff] [blame] | 3120 | bool IsModule; |
Richard Smith | a534a31 | 2015-07-21 23:54:07 +0000 | [diff] [blame] | 3121 | bool NeedDecls; |
Richard Smith | 33e0f7e | 2015-07-22 02:08:40 +0000 | [diff] [blame] | 3122 | ASTWriter::RecordData *InterestingIdentifierOffsets; |
Richard Smith | cf97cf6 | 2015-04-19 01:34:23 +0000 | [diff] [blame] | 3123 | |
| 3124 | /// \brief Determines whether this is an "interesting" identifier that needs a |
| 3125 | /// full IdentifierInfo structure written into the hash table. Notably, this |
| 3126 | /// doesn't check whether the name has macros defined; use PublicMacroIterator |
| 3127 | /// to check that. |
Richard Smith | 9c25418 | 2015-07-19 21:41:12 +0000 | [diff] [blame] | 3128 | bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) { |
Richard Smith | d732939 | 2015-04-21 21:46:32 +0000 | [diff] [blame] | 3129 | if (MacroOffset || |
| 3130 | II->isPoisoned() || |
Richard Smith | 9c25418 | 2015-07-19 21:41:12 +0000 | [diff] [blame] | 3131 | (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) || |
Richard Smith | cf97cf6 | 2015-04-19 01:34:23 +0000 | [diff] [blame] | 3132 | II->hasRevertedTokenIDToIdentifier() || |
Richard Smith | a534a31 | 2015-07-21 23:54:07 +0000 | [diff] [blame] | 3133 | (NeedDecls && II->getFETokenInfo<void>())) |
Richard Smith | cf97cf6 | 2015-04-19 01:34:23 +0000 | [diff] [blame] | 3134 | return true; |
| 3135 | |
| 3136 | return false; |
| 3137 | } |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 3138 | |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 3139 | public: |
Douglas Gregor | 4a69c2e | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 3140 | typedef IdentifierInfo* key_type; |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 3141 | typedef key_type key_type_ref; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3142 | |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3143 | typedef IdentID data_type; |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 3144 | typedef data_type data_type_ref; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3145 | |
Justin Bogner | 25463f1 | 2014-04-18 20:27:24 +0000 | [diff] [blame] | 3146 | typedef unsigned hash_value_type; |
| 3147 | typedef unsigned offset_type; |
| 3148 | |
Richard Smith | d732939 | 2015-04-21 21:46:32 +0000 | [diff] [blame] | 3149 | ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP, |
Richard Smith | 33e0f7e | 2015-07-22 02:08:40 +0000 | [diff] [blame] | 3150 | IdentifierResolver &IdResolver, bool IsModule, |
| 3151 | ASTWriter::RecordData *InterestingIdentifierOffsets) |
Richard Smith | a534a31 | 2015-07-21 23:54:07 +0000 | [diff] [blame] | 3152 | : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule), |
Richard Smith | 33e0f7e | 2015-07-22 02:08:40 +0000 | [diff] [blame] | 3153 | NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus), |
| 3154 | InterestingIdentifierOffsets(InterestingIdentifierOffsets) {} |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 3155 | |
Justin Bogner | 25463f1 | 2014-04-18 20:27:24 +0000 | [diff] [blame] | 3156 | static hash_value_type ComputeHash(const IdentifierInfo* II) { |
Daniel Dunbar | f8502d5 | 2009-10-17 23:52:28 +0000 | [diff] [blame] | 3157 | return llvm::HashString(II->getName()); |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 3158 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3159 | |
Richard Smith | 33e0f7e | 2015-07-22 02:08:40 +0000 | [diff] [blame] | 3160 | bool isInterestingIdentifier(const IdentifierInfo *II) { |
| 3161 | auto MacroOffset = Writer.getMacroDirectivesOffset(II); |
| 3162 | return isInterestingIdentifier(II, MacroOffset); |
| 3163 | } |
Richard Smith | 9c25418 | 2015-07-19 21:41:12 +0000 | [diff] [blame] | 3164 | bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) { |
| 3165 | return isInterestingIdentifier(II, 0); |
| 3166 | } |
| 3167 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3168 | std::pair<unsigned,unsigned> |
Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 3169 | EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) { |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 3170 | unsigned KeyLen = II->getLength() + 1; |
Douglas Gregor | 1d583f2 | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 3171 | unsigned DataLen = 4; // 4 bytes for the persistent ID << 1 |
Richard Smith | d732939 | 2015-04-21 21:46:32 +0000 | [diff] [blame] | 3172 | auto MacroOffset = Writer.getMacroDirectivesOffset(II); |
| 3173 | if (isInterestingIdentifier(II, MacroOffset)) { |
Alexander Kornienko | 1d26c02 | 2012-09-25 17:18:14 +0000 | [diff] [blame] | 3174 | DataLen += 2; // 2 bytes for builtin ID |
| 3175 | DataLen += 2; // 2 bytes for flags |
Richard Smith | d732939 | 2015-04-21 21:46:32 +0000 | [diff] [blame] | 3176 | if (MacroOffset) |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 3177 | DataLen += 4; // MacroDirectives offset. |
Alexander Kornienko | 1d26c02 | 2012-09-25 17:18:14 +0000 | [diff] [blame] | 3178 | |
Richard Smith | a534a31 | 2015-07-21 23:54:07 +0000 | [diff] [blame] | 3179 | if (NeedDecls) { |
| 3180 | for (IdentifierResolver::iterator D = IdResolver.begin(II), |
| 3181 | DEnd = IdResolver.end(); |
| 3182 | D != DEnd; ++D) |
| 3183 | DataLen += 4; |
| 3184 | } |
Douglas Gregor | 1d583f2 | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 3185 | } |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 3186 | using namespace llvm::support; |
| 3187 | endian::Writer<little> LE(Out); |
| 3188 | |
Richard Smith | df8a831 | 2015-03-13 04:05:01 +0000 | [diff] [blame] | 3189 | assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen); |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 3190 | LE.write<uint16_t>(DataLen); |
Douglas Gregor | ab4df58 | 2009-04-28 20:01:51 +0000 | [diff] [blame] | 3191 | // We emit the key length after the data length so that every |
| 3192 | // string is preceded by a 16-bit length. This matches the PTH |
| 3193 | // format for storing identifiers. |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 3194 | LE.write<uint16_t>(KeyLen); |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 3195 | return std::make_pair(KeyLen, DataLen); |
| 3196 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3197 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3198 | void EmitKey(raw_ostream& Out, const IdentifierInfo* II, |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 3199 | unsigned KeyLen) { |
| 3200 | // Record the location of the key data. This is used when generating |
| 3201 | // the mapping from persistent IDs to strings. |
| 3202 | Writer.SetIdentifierOffset(II, Out.tell()); |
Richard Smith | 33e0f7e | 2015-07-22 02:08:40 +0000 | [diff] [blame] | 3203 | |
| 3204 | // Emit the offset of the key/data length information to the interesting |
| 3205 | // identifiers table if necessary. |
| 3206 | if (InterestingIdentifierOffsets && isInterestingIdentifier(II)) |
| 3207 | InterestingIdentifierOffsets->push_back(Out.tell() - 4); |
| 3208 | |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 3209 | Out.write(II->getNameStart(), KeyLen); |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 3210 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3211 | |
Douglas Gregor | 4a69c2e | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 3212 | void EmitData(raw_ostream& Out, IdentifierInfo* II, |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3213 | IdentID ID, unsigned) { |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 3214 | using namespace llvm::support; |
| 3215 | endian::Writer<little> LE(Out); |
Richard Smith | cf97cf6 | 2015-04-19 01:34:23 +0000 | [diff] [blame] | 3216 | |
Richard Smith | d732939 | 2015-04-21 21:46:32 +0000 | [diff] [blame] | 3217 | auto MacroOffset = Writer.getMacroDirectivesOffset(II); |
| 3218 | if (!isInterestingIdentifier(II, MacroOffset)) { |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 3219 | LE.write<uint32_t>(ID << 1); |
Douglas Gregor | 1d583f2 | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 3220 | return; |
| 3221 | } |
Douglas Gregor | b925652 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 3222 | |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 3223 | LE.write<uint32_t>((ID << 1) | 0x01); |
Alexander Kornienko | 1d26c02 | 2012-09-25 17:18:14 +0000 | [diff] [blame] | 3224 | uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID(); |
| 3225 | assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader."); |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 3226 | LE.write<uint16_t>(Bits); |
Alexander Kornienko | 1d26c02 | 2012-09-25 17:18:14 +0000 | [diff] [blame] | 3227 | Bits = 0; |
Richard Smith | d732939 | 2015-04-21 21:46:32 +0000 | [diff] [blame] | 3228 | bool HadMacroDefinition = MacroOffset != 0; |
Alexander Kornienko | 1d26c02 | 2012-09-25 17:18:14 +0000 | [diff] [blame] | 3229 | Bits = (Bits << 1) | unsigned(HadMacroDefinition); |
Daniel Dunbar | 91b640a | 2009-12-18 20:58:47 +0000 | [diff] [blame] | 3230 | Bits = (Bits << 1) | unsigned(II->isExtensionToken()); |
| 3231 | Bits = (Bits << 1) | unsigned(II->isPoisoned()); |
Richard Smith | 9c25418 | 2015-07-19 21:41:12 +0000 | [diff] [blame] | 3232 | Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin()); |
Argyrios Kyrtzidis | 3084a61 | 2010-08-11 22:55:12 +0000 | [diff] [blame] | 3233 | Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier()); |
Daniel Dunbar | 91b640a | 2009-12-18 20:58:47 +0000 | [diff] [blame] | 3234 | Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword()); |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 3235 | LE.write<uint16_t>(Bits); |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 3236 | |
Richard Smith | d732939 | 2015-04-21 21:46:32 +0000 | [diff] [blame] | 3237 | if (HadMacroDefinition) |
| 3238 | LE.write<uint32_t>(MacroOffset); |
Alexander Kornienko | 1d26c02 | 2012-09-25 17:18:14 +0000 | [diff] [blame] | 3239 | |
Richard Smith | a534a31 | 2015-07-21 23:54:07 +0000 | [diff] [blame] | 3240 | if (NeedDecls) { |
| 3241 | // Emit the declaration IDs in reverse order, because the |
| 3242 | // IdentifierResolver provides the declarations as they would be |
| 3243 | // visible (e.g., the function "stat" would come before the struct |
| 3244 | // "stat"), but the ASTReader adds declarations to the end of the list |
| 3245 | // (so we need to see the struct "stat" before the function "stat"). |
| 3246 | // Only emit declarations that aren't from a chained PCH, though. |
| 3247 | SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II), |
| 3248 | IdResolver.end()); |
| 3249 | for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(), |
| 3250 | DEnd = Decls.rend(); |
| 3251 | D != DEnd; ++D) |
| 3252 | LE.write<uint32_t>( |
| 3253 | Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D))); |
| 3254 | } |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 3255 | } |
| 3256 | }; |
| 3257 | } // end anonymous namespace |
| 3258 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 3259 | /// \brief Write the identifier table into the AST file. |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 3260 | /// |
| 3261 | /// The identifier table consists of a blob containing string data |
| 3262 | /// (the actual identifiers themselves) and a separate "offsets" index |
| 3263 | /// that maps identifier IDs to locations within the blob. |
Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 3264 | void ASTWriter::WriteIdentifierTable(Preprocessor &PP, |
| 3265 | IdentifierResolver &IdResolver, |
| 3266 | bool IsModule) { |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 3267 | using namespace llvm; |
| 3268 | |
Richard Smith | 33e0f7e | 2015-07-22 02:08:40 +0000 | [diff] [blame] | 3269 | RecordData InterestingIdents; |
| 3270 | |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 3271 | // Create and write out the blob that contains the identifier |
| 3272 | // strings. |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 3273 | { |
Justin Bogner | bb094f0 | 2014-04-18 19:57:06 +0000 | [diff] [blame] | 3274 | llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator; |
Richard Smith | 33e0f7e | 2015-07-22 02:08:40 +0000 | [diff] [blame] | 3275 | ASTIdentifierTableTrait Trait( |
| 3276 | *this, PP, IdResolver, IsModule, |
| 3277 | (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3278 | |
Douglas Gregor | e6648fb | 2009-04-28 20:33:11 +0000 | [diff] [blame] | 3279 | // Look for any identifiers that were named while processing the |
| 3280 | // headers, but are otherwise not needed. We add these to the hash |
| 3281 | // table to enable checking of the predefines buffer in the case |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 3282 | // where the user adds new macro definitions when building the AST |
Douglas Gregor | e6648fb | 2009-04-28 20:33:11 +0000 | [diff] [blame] | 3283 | // file. |
Chandler Carruth | 8440c98 | 2015-03-26 23:54:15 +0000 | [diff] [blame] | 3284 | SmallVector<const IdentifierInfo *, 128> IIs; |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 3285 | for (const auto &ID : PP.getIdentifierTable()) |
| 3286 | IIs.push_back(ID.second); |
Chandler Carruth | 8440c98 | 2015-03-26 23:54:15 +0000 | [diff] [blame] | 3287 | // Sort the identifiers lexicographically before getting them references so |
| 3288 | // that their order is stable. |
| 3289 | std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>()); |
| 3290 | for (const IdentifierInfo *II : IIs) |
Richard Smith | 9c25418 | 2015-07-19 21:41:12 +0000 | [diff] [blame] | 3291 | if (Trait.isInterestingNonMacroIdentifier(II)) |
| 3292 | getIdentifierRef(II); |
Douglas Gregor | e6648fb | 2009-04-28 20:33:11 +0000 | [diff] [blame] | 3293 | |
Sebastian Redl | ff4a295 | 2010-07-23 23:49:55 +0000 | [diff] [blame] | 3294 | // Create the on-disk hash table representation. We only store offsets |
| 3295 | // for identifiers that appear here for the first time. |
| 3296 | IdentifierOffsets.resize(NextIdentID - FirstIdentID); |
Chandler Carruth | 885e78c | 2015-03-24 21:18:10 +0000 | [diff] [blame] | 3297 | for (auto IdentIDPair : IdentifierIDs) { |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 3298 | auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first); |
Chandler Carruth | 885e78c | 2015-03-24 21:18:10 +0000 | [diff] [blame] | 3299 | IdentID ID = IdentIDPair.second; |
| 3300 | assert(II && "NULL identifier in identifier table"); |
| 3301 | if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization()) |
| 3302 | Generator.insert(II, ID, Trait); |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 3303 | } |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 3304 | |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 3305 | // Create the on-disk hash table in a buffer. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3306 | SmallString<4096> IdentifierTable; |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 3307 | uint32_t BucketOffset; |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 3308 | { |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 3309 | using namespace llvm::support; |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 3310 | llvm::raw_svector_ostream Out(IdentifierTable); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3311 | // Make sure that no bucket is at offset 0 |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 3312 | endian::Writer<little>(Out).write<uint32_t>(0); |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 3313 | BucketOffset = Generator.Emit(Out, Trait); |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 3314 | } |
| 3315 | |
| 3316 | // Create a blob abbreviation |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 3317 | auto *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3318 | Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE)); |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 3319 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 3320 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 3321 | unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 3322 | |
| 3323 | // Write the identifier table |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 3324 | RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset}; |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 3325 | Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable); |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 3326 | } |
| 3327 | |
| 3328 | // Write the offsets table for identifier IDs. |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 3329 | auto *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3330 | Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET)); |
Douglas Gregor | 0e14997 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 3331 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers |
Douglas Gregor | 1ab036c | 2011-08-03 21:49:18 +0000 | [diff] [blame] | 3332 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID |
Douglas Gregor | 0e14997 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 3333 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 3334 | unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev); |
| 3335 | |
Douglas Gregor | 8d7edce | 2013-02-08 21:30:59 +0000 | [diff] [blame] | 3336 | #ifndef NDEBUG |
| 3337 | for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I) |
| 3338 | assert(IdentifierOffsets[I] && "Missing identifier offset?"); |
| 3339 | #endif |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 3340 | |
| 3341 | RecordData::value_type Record[] = {IDENTIFIER_OFFSET, |
| 3342 | IdentifierOffsets.size(), |
| 3343 | FirstIdentID - NUM_PREDEF_IDENT_IDS}; |
Douglas Gregor | 0e14997 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 3344 | Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record, |
Reid Kleckner | 012665e | 2015-05-21 00:13:09 +0000 | [diff] [blame] | 3345 | bytes(IdentifierOffsets)); |
Richard Smith | 33e0f7e | 2015-07-22 02:08:40 +0000 | [diff] [blame] | 3346 | |
| 3347 | // In C++, write the list of interesting identifiers (those that are |
| 3348 | // defined as macros, poisoned, or similar unusual things). |
| 3349 | if (!InterestingIdents.empty()) |
| 3350 | Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents); |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 3351 | } |
| 3352 | |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 3353 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3354 | // DeclContext's Name Lookup Table Serialization |
| 3355 | //===----------------------------------------------------------------------===// |
| 3356 | |
| 3357 | namespace { |
| 3358 | // Trait used for the on-disk hash table used in the method pool. |
| 3359 | class ASTDeclContextNameLookupTrait { |
| 3360 | ASTWriter &Writer; |
Richard Smith | d88a7f1 | 2015-09-01 20:35:42 +0000 | [diff] [blame] | 3361 | llvm::SmallVector<DeclID, 64> DeclIDs; |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3362 | |
| 3363 | public: |
Richard Smith | a06c7e6 | 2015-08-26 23:55:49 +0000 | [diff] [blame] | 3364 | typedef DeclarationNameKey key_type; |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3365 | typedef key_type key_type_ref; |
| 3366 | |
Richard Smith | d88a7f1 | 2015-09-01 20:35:42 +0000 | [diff] [blame] | 3367 | /// A start and end index into DeclIDs, representing a sequence of decls. |
| 3368 | typedef std::pair<unsigned, unsigned> data_type; |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3369 | typedef const data_type& data_type_ref; |
| 3370 | |
Justin Bogner | 25463f1 | 2014-04-18 20:27:24 +0000 | [diff] [blame] | 3371 | typedef unsigned hash_value_type; |
| 3372 | typedef unsigned offset_type; |
| 3373 | |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3374 | explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { } |
| 3375 | |
Richard Smith | d88a7f1 | 2015-09-01 20:35:42 +0000 | [diff] [blame] | 3376 | template<typename Coll> |
| 3377 | data_type getData(const Coll &Decls) { |
| 3378 | unsigned Start = DeclIDs.size(); |
| 3379 | for (NamedDecl *D : Decls) { |
| 3380 | DeclIDs.push_back( |
| 3381 | Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D))); |
| 3382 | } |
| 3383 | return std::make_pair(Start, DeclIDs.size()); |
| 3384 | } |
| 3385 | |
| 3386 | data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) { |
| 3387 | unsigned Start = DeclIDs.size(); |
| 3388 | for (auto ID : FromReader) |
| 3389 | DeclIDs.push_back(ID); |
| 3390 | return std::make_pair(Start, DeclIDs.size()); |
| 3391 | } |
| 3392 | |
| 3393 | static bool EqualKey(key_type_ref a, key_type_ref b) { |
| 3394 | return a == b; |
| 3395 | } |
| 3396 | |
Richard Smith | a06c7e6 | 2015-08-26 23:55:49 +0000 | [diff] [blame] | 3397 | hash_value_type ComputeHash(DeclarationNameKey Name) { |
| 3398 | return Name.getHash(); |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3399 | } |
| 3400 | |
Richard Smith | d88a7f1 | 2015-09-01 20:35:42 +0000 | [diff] [blame] | 3401 | void EmitFileRef(raw_ostream &Out, ModuleFile *F) const { |
| 3402 | assert(Writer.hasChain() && |
| 3403 | "have reference to loaded module file but no chain?"); |
| 3404 | |
| 3405 | using namespace llvm::support; |
| 3406 | endian::Writer<little>(Out) |
| 3407 | .write<uint32_t>(Writer.getChain()->getModuleFileID(F)); |
| 3408 | } |
| 3409 | |
Richard Smith | a06c7e6 | 2015-08-26 23:55:49 +0000 | [diff] [blame] | 3410 | std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out, |
| 3411 | DeclarationNameKey Name, |
| 3412 | data_type_ref Lookup) { |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 3413 | using namespace llvm::support; |
| 3414 | endian::Writer<little> LE(Out); |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3415 | unsigned KeyLen = 1; |
Richard Smith | a06c7e6 | 2015-08-26 23:55:49 +0000 | [diff] [blame] | 3416 | switch (Name.getKind()) { |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3417 | case DeclarationName::Identifier: |
| 3418 | case DeclarationName::ObjCZeroArgSelector: |
| 3419 | case DeclarationName::ObjCOneArgSelector: |
| 3420 | case DeclarationName::ObjCMultiArgSelector: |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3421 | case DeclarationName::CXXLiteralOperatorName: |
| 3422 | KeyLen += 4; |
| 3423 | break; |
| 3424 | case DeclarationName::CXXOperatorName: |
| 3425 | KeyLen += 1; |
| 3426 | break; |
Douglas Gregor | 3b65ed0 | 2011-08-02 18:32:54 +0000 | [diff] [blame] | 3427 | case DeclarationName::CXXConstructorName: |
| 3428 | case DeclarationName::CXXDestructorName: |
| 3429 | case DeclarationName::CXXConversionFunctionName: |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3430 | case DeclarationName::CXXUsingDirective: |
| 3431 | break; |
| 3432 | } |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 3433 | LE.write<uint16_t>(KeyLen); |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3434 | |
Richard Smith | f02662d | 2015-07-30 03:17:16 +0000 | [diff] [blame] | 3435 | // 4 bytes for each DeclID. |
Richard Smith | d88a7f1 | 2015-09-01 20:35:42 +0000 | [diff] [blame] | 3436 | unsigned DataLen = 4 * (Lookup.second - Lookup.first); |
| 3437 | assert(uint16_t(DataLen) == DataLen && |
| 3438 | "too many decls for serialized lookup result"); |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 3439 | LE.write<uint16_t>(DataLen); |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3440 | |
| 3441 | return std::make_pair(KeyLen, DataLen); |
| 3442 | } |
| 3443 | |
Richard Smith | a06c7e6 | 2015-08-26 23:55:49 +0000 | [diff] [blame] | 3444 | void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) { |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 3445 | using namespace llvm::support; |
| 3446 | endian::Writer<little> LE(Out); |
Richard Smith | a06c7e6 | 2015-08-26 23:55:49 +0000 | [diff] [blame] | 3447 | LE.write<uint8_t>(Name.getKind()); |
| 3448 | switch (Name.getKind()) { |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3449 | case DeclarationName::Identifier: |
Richard Smith | a06c7e6 | 2015-08-26 23:55:49 +0000 | [diff] [blame] | 3450 | case DeclarationName::CXXLiteralOperatorName: |
| 3451 | LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier())); |
Benjamin Kramer | 53750b1 | 2012-09-19 13:40:40 +0000 | [diff] [blame] | 3452 | return; |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3453 | case DeclarationName::ObjCZeroArgSelector: |
| 3454 | case DeclarationName::ObjCOneArgSelector: |
| 3455 | case DeclarationName::ObjCMultiArgSelector: |
Richard Smith | a06c7e6 | 2015-08-26 23:55:49 +0000 | [diff] [blame] | 3456 | LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector())); |
Benjamin Kramer | 53750b1 | 2012-09-19 13:40:40 +0000 | [diff] [blame] | 3457 | return; |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3458 | case DeclarationName::CXXOperatorName: |
Richard Smith | a06c7e6 | 2015-08-26 23:55:49 +0000 | [diff] [blame] | 3459 | assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS && |
Benjamin Kramer | 53750b1 | 2012-09-19 13:40:40 +0000 | [diff] [blame] | 3460 | "Invalid operator?"); |
Richard Smith | a06c7e6 | 2015-08-26 23:55:49 +0000 | [diff] [blame] | 3461 | LE.write<uint8_t>(Name.getOperatorKind()); |
Benjamin Kramer | 53750b1 | 2012-09-19 13:40:40 +0000 | [diff] [blame] | 3462 | return; |
Douglas Gregor | 3b65ed0 | 2011-08-02 18:32:54 +0000 | [diff] [blame] | 3463 | case DeclarationName::CXXConstructorName: |
| 3464 | case DeclarationName::CXXDestructorName: |
| 3465 | case DeclarationName::CXXConversionFunctionName: |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3466 | case DeclarationName::CXXUsingDirective: |
Benjamin Kramer | 53750b1 | 2012-09-19 13:40:40 +0000 | [diff] [blame] | 3467 | return; |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3468 | } |
Benjamin Kramer | 53750b1 | 2012-09-19 13:40:40 +0000 | [diff] [blame] | 3469 | |
| 3470 | llvm_unreachable("Invalid name kind?"); |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3471 | } |
| 3472 | |
Richard Smith | a06c7e6 | 2015-08-26 23:55:49 +0000 | [diff] [blame] | 3473 | void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup, |
| 3474 | unsigned DataLen) { |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 3475 | using namespace llvm::support; |
| 3476 | endian::Writer<little> LE(Out); |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3477 | uint64_t Start = Out.tell(); (void)Start; |
Richard Smith | d88a7f1 | 2015-09-01 20:35:42 +0000 | [diff] [blame] | 3478 | for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I) |
| 3479 | LE.write<uint32_t>(DeclIDs[I]); |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3480 | assert(Out.tell() - Start == DataLen && "Data length is wrong"); |
| 3481 | } |
| 3482 | }; |
| 3483 | } // end anonymous namespace |
| 3484 | |
Chandler Carruth | e972c36 | 2015-03-26 03:11:40 +0000 | [diff] [blame] | 3485 | bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result, |
| 3486 | DeclContext *DC) { |
| 3487 | return Result.hasExternalDecls() && DC->NeedToReconcileExternalVisibleStorage; |
| 3488 | } |
| 3489 | |
| 3490 | bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result, |
| 3491 | DeclContext *DC) { |
| 3492 | for (auto *D : Result.getLookupResult()) |
| 3493 | if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile()) |
| 3494 | return false; |
| 3495 | |
| 3496 | return true; |
| 3497 | } |
| 3498 | |
Richard Smith | d88a7f1 | 2015-09-01 20:35:42 +0000 | [diff] [blame] | 3499 | void |
Chandler Carruth | e972c36 | 2015-03-26 03:11:40 +0000 | [diff] [blame] | 3500 | ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC, |
Richard Smith | cd45dbc | 2014-04-19 03:48:30 +0000 | [diff] [blame] | 3501 | llvm::SmallVectorImpl<char> &LookupTable) { |
Chandler Carruth | e972c36 | 2015-03-26 03:11:40 +0000 | [diff] [blame] | 3502 | assert(!ConstDC->HasLazyLocalLexicalLookups && |
| 3503 | !ConstDC->HasLazyExternalLexicalLookups && |
Richard Smith | 9e2341d | 2015-03-23 03:25:59 +0000 | [diff] [blame] | 3504 | "must call buildLookups first"); |
Richard Smith | cd45dbc | 2014-04-19 03:48:30 +0000 | [diff] [blame] | 3505 | |
Chandler Carruth | e972c36 | 2015-03-26 03:11:40 +0000 | [diff] [blame] | 3506 | // FIXME: We need to build the lookups table, which is logically const. |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 3507 | auto *DC = const_cast<DeclContext*>(ConstDC); |
Chandler Carruth | e972c36 | 2015-03-26 03:11:40 +0000 | [diff] [blame] | 3508 | assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table"); |
| 3509 | |
| 3510 | // Create the on-disk hash table representation. |
Richard Smith | d88a7f1 | 2015-09-01 20:35:42 +0000 | [diff] [blame] | 3511 | MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait, |
| 3512 | ASTDeclContextNameLookupTrait> Generator; |
Richard Smith | cd45dbc | 2014-04-19 03:48:30 +0000 | [diff] [blame] | 3513 | ASTDeclContextNameLookupTrait Trait(*this); |
| 3514 | |
Chandler Carruth | e972c36 | 2015-03-26 03:11:40 +0000 | [diff] [blame] | 3515 | // The first step is to collect the declaration names which we need to |
| 3516 | // serialize into the name lookup table, and to collect them in a stable |
| 3517 | // order. |
| 3518 | SmallVector<DeclarationName, 16> Names; |
Richard Smith | cd45dbc | 2014-04-19 03:48:30 +0000 | [diff] [blame] | 3519 | |
Chandler Carruth | e972c36 | 2015-03-26 03:11:40 +0000 | [diff] [blame] | 3520 | // We also build up small sets of the constructor and conversion function |
| 3521 | // names which are visible. |
| 3522 | llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet; |
Richard Smith | cd45dbc | 2014-04-19 03:48:30 +0000 | [diff] [blame] | 3523 | |
Chandler Carruth | e972c36 | 2015-03-26 03:11:40 +0000 | [diff] [blame] | 3524 | for (auto &Lookup : *DC->buildLookup()) { |
| 3525 | auto &Name = Lookup.first; |
| 3526 | auto &Result = Lookup.second; |
| 3527 | |
Douglas Gregor | 7dd37e5 | 2015-11-03 01:20:54 +0000 | [diff] [blame] | 3528 | // If there are no local declarations in our lookup result, we |
| 3529 | // don't need to write an entry for the name at all. If we can't |
| 3530 | // write out a lookup set without performing more deserialization, |
| 3531 | // just skip this entry. |
| 3532 | if (isLookupResultExternal(Result, DC) && |
Chandler Carruth | e972c36 | 2015-03-26 03:11:40 +0000 | [diff] [blame] | 3533 | isLookupResultEntirelyExternal(Result, DC)) |
| 3534 | continue; |
| 3535 | |
| 3536 | // We also skip empty results. If any of the results could be external and |
| 3537 | // the currently available results are empty, then all of the results are |
| 3538 | // external and we skip it above. So the only way we get here with an empty |
| 3539 | // results is when no results could have been external *and* we have |
| 3540 | // external results. |
| 3541 | // |
| 3542 | // FIXME: While we might want to start emitting on-disk entries for negative |
| 3543 | // lookups into a decl context as an optimization, today we *have* to skip |
| 3544 | // them because there are names with empty lookup results in decl contexts |
| 3545 | // which we can't emit in any stable ordering: we lookup constructors and |
| 3546 | // conversion functions in the enclosing namespace scope creating empty |
| 3547 | // results for them. This in almost certainly a bug in Clang's name lookup, |
| 3548 | // but that is likely to be hard or impossible to fix and so we tolerate it |
| 3549 | // here by omitting lookups with empty results. |
| 3550 | if (Lookup.second.getLookupResult().empty()) |
| 3551 | continue; |
| 3552 | |
| 3553 | switch (Lookup.first.getNameKind()) { |
| 3554 | default: |
| 3555 | Names.push_back(Lookup.first); |
| 3556 | break; |
| 3557 | |
Richard Smith | cd45dbc | 2014-04-19 03:48:30 +0000 | [diff] [blame] | 3558 | case DeclarationName::CXXConstructorName: |
Chandler Carruth | e972c36 | 2015-03-26 03:11:40 +0000 | [diff] [blame] | 3559 | assert(isa<CXXRecordDecl>(DC) && |
| 3560 | "Cannot have a constructor name outside of a class!"); |
| 3561 | ConstructorNameSet.insert(Name); |
| 3562 | break; |
Richard Smith | cd45dbc | 2014-04-19 03:48:30 +0000 | [diff] [blame] | 3563 | |
| 3564 | case DeclarationName::CXXConversionFunctionName: |
Chandler Carruth | e972c36 | 2015-03-26 03:11:40 +0000 | [diff] [blame] | 3565 | assert(isa<CXXRecordDecl>(DC) && |
| 3566 | "Cannot have a conversion function name outside of a class!"); |
| 3567 | ConversionNameSet.insert(Name); |
Rafael Espindola | c606b3e | 2015-03-25 04:43:15 +0000 | [diff] [blame] | 3568 | break; |
Richard Smith | cd45dbc | 2014-04-19 03:48:30 +0000 | [diff] [blame] | 3569 | } |
Chandler Carruth | 75c9f13 | 2015-03-25 00:34:51 +0000 | [diff] [blame] | 3570 | } |
Chandler Carruth | 75c9f13 | 2015-03-25 00:34:51 +0000 | [diff] [blame] | 3571 | |
Chandler Carruth | e972c36 | 2015-03-26 03:11:40 +0000 | [diff] [blame] | 3572 | // Sort the names into a stable order. |
| 3573 | std::sort(Names.begin(), Names.end()); |
| 3574 | |
Chandler Carruth | ffbf705 | 2015-03-26 22:27:09 +0000 | [diff] [blame] | 3575 | if (auto *D = dyn_cast<CXXRecordDecl>(DC)) { |
Chandler Carruth | e972c36 | 2015-03-26 03:11:40 +0000 | [diff] [blame] | 3576 | // We need to establish an ordering of constructor and conversion function |
Chandler Carruth | ffbf705 | 2015-03-26 22:27:09 +0000 | [diff] [blame] | 3577 | // names, and they don't have an intrinsic ordering. |
Chandler Carruth | e972c36 | 2015-03-26 03:11:40 +0000 | [diff] [blame] | 3578 | |
Chandler Carruth | ffbf705 | 2015-03-26 22:27:09 +0000 | [diff] [blame] | 3579 | // First we try the easy case by forming the current context's constructor |
| 3580 | // name and adding that name first. This is a very useful optimization to |
| 3581 | // avoid walking the lexical declarations in many cases, and it also |
| 3582 | // handles the only case where a constructor name can come from some other |
| 3583 | // lexical context -- when that name is an implicit constructor merged from |
| 3584 | // another declaration in the redecl chain. Any non-implicit constructor or |
| 3585 | // conversion function which doesn't occur in all the lexical contexts |
| 3586 | // would be an ODR violation. |
| 3587 | auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName( |
| 3588 | Context->getCanonicalType(Context->getRecordType(D))); |
| 3589 | if (ConstructorNameSet.erase(ImplicitCtorName)) |
| 3590 | Names.push_back(ImplicitCtorName); |
Chandler Carruth | e972c36 | 2015-03-26 03:11:40 +0000 | [diff] [blame] | 3591 | |
Chandler Carruth | ffbf705 | 2015-03-26 22:27:09 +0000 | [diff] [blame] | 3592 | // If we still have constructors or conversion functions, we walk all the |
| 3593 | // names in the decl and add the constructors and conversion functions |
| 3594 | // which are visible in the order they lexically occur within the context. |
| 3595 | if (!ConstructorNameSet.empty() || !ConversionNameSet.empty()) |
| 3596 | for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls()) |
| 3597 | if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) { |
| 3598 | auto Name = ChildND->getDeclName(); |
| 3599 | switch (Name.getNameKind()) { |
| 3600 | default: |
| 3601 | continue; |
| 3602 | |
| 3603 | case DeclarationName::CXXConstructorName: |
| 3604 | if (ConstructorNameSet.erase(Name)) |
| 3605 | Names.push_back(Name); |
| 3606 | break; |
| 3607 | |
| 3608 | case DeclarationName::CXXConversionFunctionName: |
| 3609 | if (ConversionNameSet.erase(Name)) |
| 3610 | Names.push_back(Name); |
| 3611 | break; |
| 3612 | } |
| 3613 | |
| 3614 | if (ConstructorNameSet.empty() && ConversionNameSet.empty()) |
| 3615 | break; |
Chandler Carruth | e972c36 | 2015-03-26 03:11:40 +0000 | [diff] [blame] | 3616 | } |
| 3617 | |
Chandler Carruth | e972c36 | 2015-03-26 03:11:40 +0000 | [diff] [blame] | 3618 | assert(ConstructorNameSet.empty() && "Failed to find all of the visible " |
| 3619 | "constructors by walking all the " |
| 3620 | "lexical members of the context."); |
| 3621 | assert(ConversionNameSet.empty() && "Failed to find all of the visible " |
| 3622 | "conversion functions by walking all " |
| 3623 | "the lexical members of the context."); |
Richard Smith | 961eae5 | 2014-03-25 01:14:22 +0000 | [diff] [blame] | 3624 | } |
| 3625 | |
Chandler Carruth | e972c36 | 2015-03-26 03:11:40 +0000 | [diff] [blame] | 3626 | // Next we need to do a lookup with each name into this decl context to fully |
| 3627 | // populate any results from external sources. We don't actually use the |
| 3628 | // results of these lookups because we only want to use the results after all |
| 3629 | // results have been loaded and the pointers into them will be stable. |
| 3630 | for (auto &Name : Names) |
| 3631 | DC->lookup(Name); |
| 3632 | |
| 3633 | // Now we need to insert the results for each name into the hash table. For |
| 3634 | // constructor names and conversion function names, we actually need to merge |
| 3635 | // all of the results for them into one list of results each and insert |
| 3636 | // those. |
| 3637 | SmallVector<NamedDecl *, 8> ConstructorDecls; |
| 3638 | SmallVector<NamedDecl *, 8> ConversionDecls; |
| 3639 | |
| 3640 | // Now loop over the names, either inserting them or appending for the two |
| 3641 | // special cases. |
| 3642 | for (auto &Name : Names) { |
| 3643 | DeclContext::lookup_result Result = DC->noload_lookup(Name); |
| 3644 | |
| 3645 | switch (Name.getNameKind()) { |
| 3646 | default: |
Richard Smith | d88a7f1 | 2015-09-01 20:35:42 +0000 | [diff] [blame] | 3647 | Generator.insert(Name, Trait.getData(Result), Trait); |
Chandler Carruth | e972c36 | 2015-03-26 03:11:40 +0000 | [diff] [blame] | 3648 | break; |
| 3649 | |
| 3650 | case DeclarationName::CXXConstructorName: |
| 3651 | ConstructorDecls.append(Result.begin(), Result.end()); |
| 3652 | break; |
| 3653 | |
| 3654 | case DeclarationName::CXXConversionFunctionName: |
| 3655 | ConversionDecls.append(Result.begin(), Result.end()); |
| 3656 | break; |
| 3657 | } |
| 3658 | } |
| 3659 | |
| 3660 | // Handle our two special cases if we ended up having any. We arbitrarily use |
| 3661 | // the first declaration's name here because the name itself isn't part of |
| 3662 | // the key, only the kind of name is used. |
| 3663 | if (!ConstructorDecls.empty()) |
| 3664 | Generator.insert(ConstructorDecls.front()->getDeclName(), |
Richard Smith | d88a7f1 | 2015-09-01 20:35:42 +0000 | [diff] [blame] | 3665 | Trait.getData(ConstructorDecls), Trait); |
Chandler Carruth | e972c36 | 2015-03-26 03:11:40 +0000 | [diff] [blame] | 3666 | if (!ConversionDecls.empty()) |
| 3667 | Generator.insert(ConversionDecls.front()->getDeclName(), |
Richard Smith | d88a7f1 | 2015-09-01 20:35:42 +0000 | [diff] [blame] | 3668 | Trait.getData(ConversionDecls), Trait); |
Chandler Carruth | e972c36 | 2015-03-26 03:11:40 +0000 | [diff] [blame] | 3669 | |
Richard Smith | d88a7f1 | 2015-09-01 20:35:42 +0000 | [diff] [blame] | 3670 | // Create the on-disk hash table. Also emit the existing imported and |
| 3671 | // merged table if there is one. |
| 3672 | auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr; |
| 3673 | Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr); |
Richard Smith | 961eae5 | 2014-03-25 01:14:22 +0000 | [diff] [blame] | 3674 | } |
| 3675 | |
Argyrios Kyrtzidis | ba88bfa | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 3676 | /// \brief Write the block containing all of the declaration IDs |
| 3677 | /// visible from the given DeclContext. |
| 3678 | /// |
| 3679 | /// \returns the offset of the DECL_CONTEXT_VISIBLE block within the |
Sebastian Redl | a4071b4 | 2010-08-24 00:50:09 +0000 | [diff] [blame] | 3680 | /// bitstream, or 0 if no block was written. |
Argyrios Kyrtzidis | ba88bfa | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 3681 | uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context, |
| 3682 | DeclContext *DC) { |
Richard Smith | 5fc18a9 | 2015-07-12 23:43:21 +0000 | [diff] [blame] | 3683 | // If we imported a key declaration of this namespace, write the visible |
| 3684 | // lookup results as an update record for it rather than including them |
| 3685 | // on this declaration. We will only look at key declarations on reload. |
| 3686 | if (isa<NamespaceDecl>(DC) && Chain && |
| 3687 | Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) { |
| 3688 | // Only do this once, for the first local declaration of the namespace. |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 3689 | for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev; |
Richard Smith | 5fc18a9 | 2015-07-12 23:43:21 +0000 | [diff] [blame] | 3690 | Prev = Prev->getPreviousDecl()) |
| 3691 | if (!Prev->isFromASTFile()) |
| 3692 | return 0; |
| 3693 | |
| 3694 | // Note that we need to emit an update record for the primary context. |
| 3695 | UpdatedDeclContexts.insert(DC->getPrimaryContext()); |
| 3696 | |
| 3697 | // Make sure all visible decls are written. They will be recorded later. We |
| 3698 | // do this using a side data structure so we can sort the names into |
| 3699 | // a deterministic order. |
| 3700 | StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup(); |
| 3701 | SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16> |
| 3702 | LookupResults; |
| 3703 | if (Map) { |
| 3704 | LookupResults.reserve(Map->size()); |
| 3705 | for (auto &Entry : *Map) |
| 3706 | LookupResults.push_back( |
| 3707 | std::make_pair(Entry.first, Entry.second.getLookupResult())); |
| 3708 | } |
| 3709 | |
| 3710 | std::sort(LookupResults.begin(), LookupResults.end(), llvm::less_first()); |
| 3711 | for (auto &NameAndResult : LookupResults) { |
| 3712 | DeclarationName Name = NameAndResult.first; |
| 3713 | DeclContext::lookup_result Result = NameAndResult.second; |
| 3714 | if (Name.getNameKind() == DeclarationName::CXXConstructorName || |
| 3715 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
| 3716 | // We have to work around a name lookup bug here where negative lookup |
| 3717 | // results for these names get cached in namespace lookup tables (these |
| 3718 | // names should never be looked up in a namespace). |
| 3719 | assert(Result.empty() && "Cannot have a constructor or conversion " |
| 3720 | "function name in a namespace!"); |
| 3721 | continue; |
| 3722 | } |
| 3723 | |
| 3724 | for (NamedDecl *ND : Result) |
| 3725 | if (!ND->isFromASTFile()) |
| 3726 | GetDeclRef(ND); |
| 3727 | } |
| 3728 | |
| 3729 | return 0; |
| 3730 | } |
| 3731 | |
Argyrios Kyrtzidis | ba88bfa | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 3732 | if (DC->getPrimaryContext() != DC) |
| 3733 | return 0; |
| 3734 | |
Chandler Carruth | e972c36 | 2015-03-26 03:11:40 +0000 | [diff] [blame] | 3735 | // Skip contexts which don't support name lookup. |
| 3736 | if (!DC->isLookupContext()) |
Argyrios Kyrtzidis | ba88bfa | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 3737 | return 0; |
| 3738 | |
| 3739 | // If not in C++, we perform name lookup for the translation unit via the |
| 3740 | // IdentifierInfo chains, don't bother to build a visible-declarations table. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3741 | if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus) |
Argyrios Kyrtzidis | ba88bfa | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 3742 | return 0; |
| 3743 | |
Argyrios Kyrtzidis | ba88bfa | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 3744 | // Serialize the contents of the mapping used for lookup. Note that, |
| 3745 | // although we have two very different code paths, the serialized |
| 3746 | // representation is the same for both cases: a declaration name, |
| 3747 | // followed by a size, followed by references to the visible |
| 3748 | // declarations that have that name. |
| 3749 | uint64_t Offset = Stream.GetCurrentBitNo(); |
Richard Smith | f634c90 | 2012-03-16 06:12:59 +0000 | [diff] [blame] | 3750 | StoredDeclsMap *Map = DC->buildLookup(); |
Argyrios Kyrtzidis | ba88bfa | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 3751 | if (!Map || Map->empty()) |
| 3752 | return 0; |
| 3753 | |
Argyrios Kyrtzidis | ba88bfa | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 3754 | // Create the on-disk hash table in a buffer. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3755 | SmallString<4096> LookupTable; |
Richard Smith | d88a7f1 | 2015-09-01 20:35:42 +0000 | [diff] [blame] | 3756 | GenerateNameLookupTable(DC, LookupTable); |
Argyrios Kyrtzidis | ba88bfa | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 3757 | |
| 3758 | // Write the lookup table |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 3759 | RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE}; |
Argyrios Kyrtzidis | ba88bfa | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 3760 | Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record, |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 3761 | LookupTable); |
Argyrios Kyrtzidis | ba88bfa | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 3762 | ++NumVisibleDeclContexts; |
| 3763 | return Offset; |
| 3764 | } |
| 3765 | |
Sebastian Redl | a4071b4 | 2010-08-24 00:50:09 +0000 | [diff] [blame] | 3766 | /// \brief Write an UPDATE_VISIBLE block for the given context. |
| 3767 | /// |
| 3768 | /// UPDATE_VISIBLE blocks contain the declarations that are added to an existing |
| 3769 | /// DeclContext in a dependent AST file. As such, they only exist for the TU |
Richard Smith | f634c90 | 2012-03-16 06:12:59 +0000 | [diff] [blame] | 3770 | /// (in C++), for namespaces, and for classes with forward-declared unscoped |
| 3771 | /// enumeration members (in C++11). |
Sebastian Redl | a4071b4 | 2010-08-24 00:50:09 +0000 | [diff] [blame] | 3772 | void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) { |
Richard Smith | 961eae5 | 2014-03-25 01:14:22 +0000 | [diff] [blame] | 3773 | StoredDeclsMap *Map = DC->getLookupPtr(); |
Sebastian Redl | a4071b4 | 2010-08-24 00:50:09 +0000 | [diff] [blame] | 3774 | if (!Map || Map->empty()) |
| 3775 | return; |
| 3776 | |
Sebastian Redl | a4071b4 | 2010-08-24 00:50:09 +0000 | [diff] [blame] | 3777 | // Create the on-disk hash table in a buffer. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3778 | SmallString<4096> LookupTable; |
Richard Smith | d88a7f1 | 2015-09-01 20:35:42 +0000 | [diff] [blame] | 3779 | GenerateNameLookupTable(DC, LookupTable); |
Sebastian Redl | a4071b4 | 2010-08-24 00:50:09 +0000 | [diff] [blame] | 3780 | |
Richard Smith | 5fc18a9 | 2015-07-12 23:43:21 +0000 | [diff] [blame] | 3781 | // If we're updating a namespace, select a key declaration as the key for the |
| 3782 | // update record; those are the only ones that will be checked on reload. |
| 3783 | if (isa<NamespaceDecl>(DC)) |
| 3784 | DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC))); |
| 3785 | |
Sebastian Redl | a4071b4 | 2010-08-24 00:50:09 +0000 | [diff] [blame] | 3786 | // Write the lookup table |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 3787 | RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))}; |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 3788 | Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable); |
Sebastian Redl | a4071b4 | 2010-08-24 00:50:09 +0000 | [diff] [blame] | 3789 | } |
| 3790 | |
Peter Collingbourne | 5df20e0 | 2011-02-15 19:46:30 +0000 | [diff] [blame] | 3791 | /// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions. |
| 3792 | void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) { |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 3793 | RecordData::value_type Record[] = {Opts.fp_contract}; |
Peter Collingbourne | 5df20e0 | 2011-02-15 19:46:30 +0000 | [diff] [blame] | 3794 | Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record); |
| 3795 | } |
| 3796 | |
| 3797 | /// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions. |
| 3798 | void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3799 | if (!SemaRef.Context.getLangOpts().OpenCL) |
Peter Collingbourne | 5df20e0 | 2011-02-15 19:46:30 +0000 | [diff] [blame] | 3800 | return; |
| 3801 | |
| 3802 | const OpenCLOptions &Opts = SemaRef.getOpenCLOptions(); |
| 3803 | RecordData Record; |
| 3804 | #define OPENCLEXT(nm) Record.push_back(Opts.nm); |
| 3805 | #include "clang/Basic/OpenCLExtensions.def" |
| 3806 | Stream.EmitRecord(OPENCL_EXTENSIONS, Record); |
| 3807 | } |
| 3808 | |
Douglas Gregor | 404cdde | 2012-01-27 01:47:08 +0000 | [diff] [blame] | 3809 | void ASTWriter::WriteObjCCategories() { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3810 | SmallVector<ObjCCategoriesInfo, 2> CategoriesMap; |
Douglas Gregor | 404cdde | 2012-01-27 01:47:08 +0000 | [diff] [blame] | 3811 | RecordData Categories; |
| 3812 | |
| 3813 | for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) { |
| 3814 | unsigned Size = 0; |
| 3815 | unsigned StartIndex = Categories.size(); |
| 3816 | |
| 3817 | ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I]; |
| 3818 | |
| 3819 | // Allocate space for the size. |
| 3820 | Categories.push_back(0); |
| 3821 | |
| 3822 | // Add the categories. |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 3823 | for (ObjCInterfaceDecl::known_categories_iterator |
| 3824 | Cat = Class->known_categories_begin(), |
| 3825 | CatEnd = Class->known_categories_end(); |
| 3826 | Cat != CatEnd; ++Cat, ++Size) { |
| 3827 | assert(getDeclID(*Cat) != 0 && "Bogus category"); |
| 3828 | AddDeclRef(*Cat, Categories); |
Douglas Gregor | 404cdde | 2012-01-27 01:47:08 +0000 | [diff] [blame] | 3829 | } |
| 3830 | |
| 3831 | // Update the size. |
| 3832 | Categories[StartIndex] = Size; |
| 3833 | |
| 3834 | // Record this interface -> category map. |
| 3835 | ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex }; |
| 3836 | CategoriesMap.push_back(CatInfo); |
| 3837 | } |
| 3838 | |
| 3839 | // Sort the categories map by the definition ID, since the reader will be |
| 3840 | // performing binary searches on this information. |
| 3841 | llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end()); |
| 3842 | |
| 3843 | // Emit the categories map. |
| 3844 | using namespace llvm; |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 3845 | |
| 3846 | auto *Abbrev = new BitCodeAbbrev(); |
Douglas Gregor | 404cdde | 2012-01-27 01:47:08 +0000 | [diff] [blame] | 3847 | Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP)); |
| 3848 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries |
| 3849 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 3850 | unsigned AbbrevID = Stream.EmitAbbrev(Abbrev); |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 3851 | |
| 3852 | RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()}; |
| 3853 | Stream.EmitRecordWithBlob(AbbrevID, Record, |
| 3854 | reinterpret_cast<char *>(CategoriesMap.data()), |
Douglas Gregor | 404cdde | 2012-01-27 01:47:08 +0000 | [diff] [blame] | 3855 | CategoriesMap.size() * sizeof(ObjCCategoriesInfo)); |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 3856 | |
Douglas Gregor | 404cdde | 2012-01-27 01:47:08 +0000 | [diff] [blame] | 3857 | // Emit the category lists. |
| 3858 | Stream.EmitRecord(OBJC_CATEGORIES, Categories); |
| 3859 | } |
| 3860 | |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 3861 | void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) { |
| 3862 | Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap; |
| 3863 | |
| 3864 | if (LPTMap.empty()) |
| 3865 | return; |
| 3866 | |
| 3867 | RecordData Record; |
Chandler Carruth | 52cee4d | 2015-03-26 09:08:15 +0000 | [diff] [blame] | 3868 | for (auto LPTMapEntry : LPTMap) { |
| 3869 | const FunctionDecl *FD = LPTMapEntry.first; |
| 3870 | LateParsedTemplate *LPT = LPTMapEntry.second; |
| 3871 | AddDeclRef(FD, Record); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 3872 | AddDeclRef(LPT->D, Record); |
| 3873 | Record.push_back(LPT->Toks.size()); |
| 3874 | |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 3875 | for (const auto &Tok : LPT->Toks) { |
| 3876 | AddToken(Tok, Record); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 3877 | } |
| 3878 | } |
| 3879 | Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record); |
| 3880 | } |
| 3881 | |
Dario Domizioli | 13a0a38 | 2014-05-23 12:13:25 +0000 | [diff] [blame] | 3882 | /// \brief Write the state of 'pragma clang optimize' at the end of the module. |
| 3883 | void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) { |
| 3884 | RecordData Record; |
| 3885 | SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation(); |
| 3886 | AddSourceLocation(PragmaLoc, Record); |
| 3887 | Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record); |
| 3888 | } |
| 3889 | |
Douglas Gregor | 8f64ca1 | 2015-12-08 22:43:32 +0000 | [diff] [blame] | 3890 | void ASTWriter::WriteModuleFileExtension(Sema &SemaRef, |
| 3891 | ModuleFileExtensionWriter &Writer) { |
Douglas Gregor | 6623e1f | 2015-11-03 18:33:07 +0000 | [diff] [blame] | 3892 | // Enter the extension block. |
| 3893 | Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4); |
| 3894 | |
| 3895 | // Emit the metadata record abbreviation. |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 3896 | auto *Abv = new llvm::BitCodeAbbrev(); |
Douglas Gregor | 6623e1f | 2015-11-03 18:33:07 +0000 | [diff] [blame] | 3897 | Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA)); |
| 3898 | Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6)); |
| 3899 | Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6)); |
| 3900 | Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6)); |
| 3901 | Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6)); |
| 3902 | Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob)); |
| 3903 | unsigned Abbrev = Stream.EmitAbbrev(Abv); |
| 3904 | |
| 3905 | // Emit the metadata record. |
| 3906 | RecordData Record; |
| 3907 | auto Metadata = Writer.getExtension()->getExtensionMetadata(); |
| 3908 | Record.push_back(EXTENSION_METADATA); |
| 3909 | Record.push_back(Metadata.MajorVersion); |
| 3910 | Record.push_back(Metadata.MinorVersion); |
| 3911 | Record.push_back(Metadata.BlockName.size()); |
| 3912 | Record.push_back(Metadata.UserInfo.size()); |
| 3913 | SmallString<64> Buffer; |
| 3914 | Buffer += Metadata.BlockName; |
| 3915 | Buffer += Metadata.UserInfo; |
| 3916 | Stream.EmitRecordWithBlob(Abbrev, Record, Buffer); |
| 3917 | |
| 3918 | // Emit the contents of the extension block. |
Douglas Gregor | 8f64ca1 | 2015-12-08 22:43:32 +0000 | [diff] [blame] | 3919 | Writer.writeExtensionContents(SemaRef, Stream); |
Douglas Gregor | 6623e1f | 2015-11-03 18:33:07 +0000 | [diff] [blame] | 3920 | |
| 3921 | // Exit the extension block. |
| 3922 | Stream.ExitBlock(); |
| 3923 | } |
| 3924 | |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3925 | //===----------------------------------------------------------------------===// |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 3926 | // General Serialization Routines |
| 3927 | //===----------------------------------------------------------------------===// |
| 3928 | |
Douglas Gregor | bc8a78d5 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 3929 | /// \brief Write a record containing the given attributes. |
Alexander Kornienko | 20f6fc6 | 2012-07-09 10:04:07 +0000 | [diff] [blame] | 3930 | void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs, |
| 3931 | RecordDataImpl &Record) { |
Argyrios Kyrtzidis | 9beef8e | 2010-10-18 19:20:11 +0000 | [diff] [blame] | 3932 | Record.push_back(Attrs.size()); |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 3933 | for (const auto *A : Attrs) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3934 | Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3935 | AddSourceRange(A->getRange(), Record); |
Douglas Gregor | bc8a78d5 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 3936 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3937 | #include "clang/Serialization/AttrPCHWrite.inc" |
Daniel Dunbar | fc6507e | 2010-05-27 02:25:39 +0000 | [diff] [blame] | 3938 | |
Douglas Gregor | bc8a78d5 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 3939 | } |
Douglas Gregor | bc8a78d5 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 3940 | } |
| 3941 | |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 3942 | void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) { |
| 3943 | AddSourceLocation(Tok.getLocation(), Record); |
| 3944 | Record.push_back(Tok.getLength()); |
| 3945 | |
| 3946 | // FIXME: When reading literal tokens, reconstruct the literal pointer |
| 3947 | // if it is needed. |
| 3948 | AddIdentifierRef(Tok.getIdentifierInfo(), Record); |
| 3949 | // FIXME: Should translate token kind to a stable encoding. |
| 3950 | Record.push_back(Tok.getKind()); |
| 3951 | // FIXME: Should translate token flags to a stable encoding. |
| 3952 | Record.push_back(Tok.getFlags()); |
| 3953 | } |
| 3954 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3955 | void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) { |
Douglas Gregor | bc8a78d5 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 3956 | Record.push_back(Str.size()); |
| 3957 | Record.insert(Record.end(), Str.begin(), Str.end()); |
| 3958 | } |
| 3959 | |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 3960 | bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) { |
Richard Smith | 54cc3c2 | 2014-12-11 20:50:24 +0000 | [diff] [blame] | 3961 | assert(Context && "should have context when outputting path"); |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 3962 | |
Richard Smith | 54cc3c2 | 2014-12-11 20:50:24 +0000 | [diff] [blame] | 3963 | bool Changed = |
| 3964 | cleanPathForOutput(Context->getSourceManager().getFileManager(), Path); |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 3965 | |
| 3966 | // Remove a prefix to make the path relative, if relevant. |
| 3967 | const char *PathBegin = Path.data(); |
| 3968 | const char *PathPtr = |
| 3969 | adjustFilenameForRelocatableAST(PathBegin, BaseDirectory); |
| 3970 | if (PathPtr != PathBegin) { |
| 3971 | Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin)); |
| 3972 | Changed = true; |
| 3973 | } |
| 3974 | |
| 3975 | return Changed; |
| 3976 | } |
| 3977 | |
| 3978 | void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) { |
| 3979 | SmallString<128> FilePath(Path); |
| 3980 | PreparePathForOutput(FilePath); |
| 3981 | AddString(FilePath, Record); |
| 3982 | } |
| 3983 | |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 3984 | void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record, |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 3985 | StringRef Path) { |
| 3986 | SmallString<128> FilePath(Path); |
| 3987 | PreparePathForOutput(FilePath); |
| 3988 | Stream.EmitRecordWithBlob(Abbrev, Record, FilePath); |
| 3989 | } |
| 3990 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 3991 | void ASTWriter::AddVersionTuple(const VersionTuple &Version, |
| 3992 | RecordDataImpl &Record) { |
| 3993 | Record.push_back(Version.getMajor()); |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3994 | if (Optional<unsigned> Minor = Version.getMinor()) |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 3995 | Record.push_back(*Minor + 1); |
| 3996 | else |
| 3997 | Record.push_back(0); |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3998 | if (Optional<unsigned> Subminor = Version.getSubminor()) |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 3999 | Record.push_back(*Subminor + 1); |
| 4000 | else |
| 4001 | Record.push_back(0); |
| 4002 | } |
| 4003 | |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 4004 | /// \brief Note that the identifier II occurs at the given offset |
| 4005 | /// within the identifier table. |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 4006 | void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) { |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 4007 | IdentID ID = IdentifierIDs[II]; |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 4008 | // Only store offsets new to this AST file. Other identifier names are looked |
Sebastian Redl | ff4a295 | 2010-07-23 23:49:55 +0000 | [diff] [blame] | 4009 | // up earlier in the chain and thus don't need an offset. |
| 4010 | if (ID >= FirstIdentID) |
| 4011 | IdentifierOffsets[ID - FirstIdentID] = Offset; |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 4012 | } |
| 4013 | |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 4014 | /// \brief Note that the selector Sel occurs at the given offset |
| 4015 | /// within the method pool/selector table. |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 4016 | void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) { |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 4017 | unsigned ID = SelectorIDs[Sel]; |
| 4018 | assert(ID && "Unknown selector"); |
Sebastian Redl | d95a56e | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 4019 | // Don't record offsets for selectors that are also available in a different |
| 4020 | // file. |
| 4021 | if (ID < FirstSelectorID) |
| 4022 | return; |
| 4023 | SelectorOffsets[ID - FirstSelectorID] = Offset; |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 4024 | } |
| 4025 | |
Douglas Gregor | 6623e1f | 2015-11-03 18:33:07 +0000 | [diff] [blame] | 4026 | ASTWriter::ASTWriter( |
| 4027 | llvm::BitstreamWriter &Stream, |
| 4028 | ArrayRef<llvm::IntrusiveRefCntPtr<ModuleFileExtension>> Extensions, |
| 4029 | bool IncludeTimestamps) |
Richard Smith | 01b2cb4 | 2014-07-26 06:37:51 +0000 | [diff] [blame] | 4030 | : Stream(Stream), Context(nullptr), PP(nullptr), Chain(nullptr), |
Richard Smith | e75ee0f | 2015-08-17 07:13:32 +0000 | [diff] [blame] | 4031 | WritingModule(nullptr), IncludeTimestamps(IncludeTimestamps), |
| 4032 | WritingAST(false), DoneWritingDeclsAndTypes(false), |
| 4033 | ASTHasCompilerErrors(false), FirstDeclID(NUM_PREDEF_DECL_IDS), |
| 4034 | NextDeclID(FirstDeclID), FirstTypeID(NUM_PREDEF_TYPE_IDS), |
| 4035 | NextTypeID(FirstTypeID), FirstIdentID(NUM_PREDEF_IDENT_IDS), |
| 4036 | NextIdentID(FirstIdentID), FirstMacroID(NUM_PREDEF_MACRO_IDS), |
| 4037 | NextMacroID(FirstMacroID), FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS), |
Richard Smith | 01b2cb4 | 2014-07-26 06:37:51 +0000 | [diff] [blame] | 4038 | NextSubmoduleID(FirstSubmoduleID), |
| 4039 | FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID), |
| 4040 | CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0), |
| 4041 | NumLexicalDeclContexts(0), NumVisibleDeclContexts(0), |
Richard Smith | c2bb818 | 2015-03-24 06:36:48 +0000 | [diff] [blame] | 4042 | NextCXXBaseSpecifiersID(1), NextCXXCtorInitializersID(1), |
Richard Smith | e75ee0f | 2015-08-17 07:13:32 +0000 | [diff] [blame] | 4043 | TypeExtQualAbbrev(0), TypeFunctionProtoAbbrev(0), DeclParmVarAbbrev(0), |
Richard Smith | 01b2cb4 | 2014-07-26 06:37:51 +0000 | [diff] [blame] | 4044 | DeclContextLexicalAbbrev(0), DeclContextVisibleLookupAbbrev(0), |
Richard Smith | a27c26e | 2014-07-27 04:19:32 +0000 | [diff] [blame] | 4045 | UpdateVisibleAbbrev(0), DeclRecordAbbrev(0), DeclTypedefAbbrev(0), |
Richard Smith | 01b2cb4 | 2014-07-26 06:37:51 +0000 | [diff] [blame] | 4046 | DeclVarAbbrev(0), DeclFieldAbbrev(0), DeclEnumAbbrev(0), |
Richard Smith | a27c26e | 2014-07-27 04:19:32 +0000 | [diff] [blame] | 4047 | DeclObjCIvarAbbrev(0), DeclCXXMethodAbbrev(0), DeclRefExprAbbrev(0), |
| 4048 | CharacterLiteralAbbrev(0), IntegerLiteralAbbrev(0), |
Douglas Gregor | 6623e1f | 2015-11-03 18:33:07 +0000 | [diff] [blame] | 4049 | ExprImplicitCastAbbrev(0) { |
| 4050 | for (const auto &Ext : Extensions) { |
| 4051 | if (auto Writer = Ext->createExtensionWriter(*this)) |
| 4052 | ModuleFileExtensionWriters.push_back(std::move(Writer)); |
| 4053 | } |
| 4054 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 4055 | |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 4056 | ASTWriter::~ASTWriter() { |
Reid Kleckner | 588c937 | 2014-02-19 23:44:52 +0000 | [diff] [blame] | 4057 | llvm::DeleteContainerSeconds(FileDeclIDs); |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 4058 | } |
| 4059 | |
Richard Smith | b376191 | 2015-02-05 23:08:52 +0000 | [diff] [blame] | 4060 | const LangOptions &ASTWriter::getLangOpts() const { |
| 4061 | assert(WritingAST && "can't determine lang opts when not writing AST"); |
| 4062 | return Context->getLangOpts(); |
| 4063 | } |
| 4064 | |
Richard Smith | e75ee0f | 2015-08-17 07:13:32 +0000 | [diff] [blame] | 4065 | time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const { |
| 4066 | return IncludeTimestamps ? E->getModificationTime() : 0; |
| 4067 | } |
| 4068 | |
Adrian Prantl | adbd2b1 | 2015-09-22 23:26:31 +0000 | [diff] [blame] | 4069 | uint64_t ASTWriter::WriteAST(Sema &SemaRef, const std::string &OutputFile, |
| 4070 | Module *WritingModule, StringRef isysroot, |
| 4071 | bool hasErrors) { |
Douglas Gregor | 2fd3d40 | 2011-09-17 00:05:03 +0000 | [diff] [blame] | 4072 | WritingAST = true; |
Adrian Prantl | adbd2b1 | 2015-09-22 23:26:31 +0000 | [diff] [blame] | 4073 | |
Argyrios Kyrtzidis | 4a280ff | 2012-03-07 01:51:17 +0000 | [diff] [blame] | 4074 | ASTHasCompilerErrors = hasErrors; |
| 4075 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 4076 | // Emit the file header. |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 4077 | Stream.Emit((unsigned)'C', 8); |
| 4078 | Stream.Emit((unsigned)'P', 8); |
| 4079 | Stream.Emit((unsigned)'C', 8); |
| 4080 | Stream.Emit((unsigned)'H', 8); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4081 | |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 4082 | WriteBlockInfoBlock(); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 4083 | |
Douglas Gregor | eda8e12 | 2011-08-09 15:13:55 +0000 | [diff] [blame] | 4084 | Context = &SemaRef.Context; |
Douglas Gregor | a28bcdd | 2011-12-01 02:07:58 +0000 | [diff] [blame] | 4085 | PP = &SemaRef.PP; |
Douglas Gregor | a89c5ac | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 4086 | this->WritingModule = WritingModule; |
Adrian Prantl | adbd2b1 | 2015-09-22 23:26:31 +0000 | [diff] [blame] | 4087 | ASTFileSignature Signature = |
| 4088 | WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule); |
Craig Topper | a13603a | 2014-05-22 05:54:18 +0000 | [diff] [blame] | 4089 | Context = nullptr; |
| 4090 | PP = nullptr; |
| 4091 | this->WritingModule = nullptr; |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 4092 | this->BaseDirectory.clear(); |
Craig Topper | a13603a | 2014-05-22 05:54:18 +0000 | [diff] [blame] | 4093 | |
Douglas Gregor | 2fd3d40 | 2011-09-17 00:05:03 +0000 | [diff] [blame] | 4094 | WritingAST = false; |
Adrian Prantl | adbd2b1 | 2015-09-22 23:26:31 +0000 | [diff] [blame] | 4095 | return Signature; |
Sebastian Redl | 143413f | 2010-07-12 22:02:52 +0000 | [diff] [blame] | 4096 | } |
| 4097 | |
Douglas Gregor | a94a154 | 2011-07-27 21:45:57 +0000 | [diff] [blame] | 4098 | template<typename Vector> |
| 4099 | static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec, |
| 4100 | ASTWriter::RecordData &Record) { |
Craig Topper | a13603a | 2014-05-22 05:54:18 +0000 | [diff] [blame] | 4101 | for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end(); |
| 4102 | I != E; ++I) { |
Douglas Gregor | a94a154 | 2011-07-27 21:45:57 +0000 | [diff] [blame] | 4103 | Writer.AddDeclRef(*I, Record); |
| 4104 | } |
| 4105 | } |
| 4106 | |
Adrian Prantl | adbd2b1 | 2015-09-22 23:26:31 +0000 | [diff] [blame] | 4107 | uint64_t ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot, |
| 4108 | const std::string &OutputFile, |
| 4109 | Module *WritingModule) { |
Sebastian Redl | 143413f | 2010-07-12 22:02:52 +0000 | [diff] [blame] | 4110 | using namespace llvm; |
| 4111 | |
Craig Topper | a13603a | 2014-05-22 05:54:18 +0000 | [diff] [blame] | 4112 | bool isModule = WritingModule != nullptr; |
Argyrios Kyrtzidis | ffb3558 | 2013-03-14 04:44:56 +0000 | [diff] [blame] | 4113 | |
Douglas Gregor | cf68c58 | 2011-12-01 22:20:10 +0000 | [diff] [blame] | 4114 | // Make sure that the AST reader knows to finalize itself. |
| 4115 | if (Chain) |
| 4116 | Chain->finalizeForWriting(); |
| 4117 | |
Sebastian Redl | 143413f | 2010-07-12 22:02:52 +0000 | [diff] [blame] | 4118 | ASTContext &Context = SemaRef.Context; |
| 4119 | Preprocessor &PP = SemaRef.PP; |
| 4120 | |
Douglas Gregor | dab4243 | 2011-08-12 00:15:20 +0000 | [diff] [blame] | 4121 | // Set up predefined declaration IDs. |
Richard Smith | 5fc18a9 | 2015-07-12 23:43:21 +0000 | [diff] [blame] | 4122 | auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) { |
| 4123 | if (D) { |
| 4124 | assert(D->isCanonicalDecl() && "predefined decl is not canonical"); |
| 4125 | DeclIDs[D] = ID; |
Richard Smith | 5fc18a9 | 2015-07-12 23:43:21 +0000 | [diff] [blame] | 4126 | } |
| 4127 | }; |
| 4128 | RegisterPredefDecl(Context.getTranslationUnitDecl(), |
| 4129 | PREDEF_DECL_TRANSLATION_UNIT_ID); |
| 4130 | RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID); |
| 4131 | RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID); |
| 4132 | RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID); |
| 4133 | RegisterPredefDecl(Context.ObjCProtocolClassDecl, |
| 4134 | PREDEF_DECL_OBJC_PROTOCOL_ID); |
| 4135 | RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID); |
| 4136 | RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID); |
| 4137 | RegisterPredefDecl(Context.ObjCInstanceTypeDecl, |
| 4138 | PREDEF_DECL_OBJC_INSTANCETYPE_ID); |
| 4139 | RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 4140 | RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG); |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 4141 | RegisterPredefDecl(Context.BuiltinMSVaListDecl, |
| 4142 | PREDEF_DECL_BUILTIN_MS_VA_LIST_ID); |
Richard Smith | 5fc18a9 | 2015-07-12 23:43:21 +0000 | [diff] [blame] | 4143 | RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 4144 | RegisterPredefDecl(Context.MakeIntegerSeqDecl, |
| 4145 | PREDEF_DECL_MAKE_INTEGER_SEQ_ID); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 4146 | |
Chris Lattner | 0c79736 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 4147 | // Build a record containing all of the tentative definitions in this file, in |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 4148 | // TentativeDefinitions order. Generally, this record will be empty for |
Chris Lattner | 0c79736 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 4149 | // headers. |
Douglas Gregor | d4df865 | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 4150 | RecordData TentativeDefinitions; |
Douglas Gregor | a94a154 | 2011-07-27 21:45:57 +0000 | [diff] [blame] | 4151 | AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions); |
Douglas Gregor | eb08bd4 | 2011-07-27 20:58:46 +0000 | [diff] [blame] | 4152 | |
Argyrios Kyrtzidis | 35672e7 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 4153 | // Build a record containing all of the file scoped decls in this file. |
| 4154 | RecordData UnusedFileScopedDecls; |
Argyrios Kyrtzidis | 5985236 | 2013-03-14 04:45:00 +0000 | [diff] [blame] | 4155 | if (!isModule) |
| 4156 | AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls, |
| 4157 | UnusedFileScopedDecls); |
Sebastian Redl | 08aca9025 | 2010-08-05 18:21:25 +0000 | [diff] [blame] | 4158 | |
Douglas Gregor | 851443c | 2011-08-12 01:39:19 +0000 | [diff] [blame] | 4159 | // Build a record containing all of the delegating constructors we still need |
| 4160 | // to resolve. |
Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 4161 | RecordData DelegatingCtorDecls; |
Argyrios Kyrtzidis | ffb3558 | 2013-03-14 04:44:56 +0000 | [diff] [blame] | 4162 | if (!isModule) |
| 4163 | AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls); |
Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 4164 | |
Douglas Gregor | 851443c | 2011-08-12 01:39:19 +0000 | [diff] [blame] | 4165 | // Write the set of weak, undeclared identifiers. We always write the |
| 4166 | // entire table, since later PCH files in a PCH chain are only interested in |
| 4167 | // the results at the end of the chain. |
Argyrios Kyrtzidis | ee1afa3 | 2010-08-05 09:48:08 +0000 | [diff] [blame] | 4168 | RecordData WeakUndeclaredIdentifiers; |
Chandler Carruth | f85d982 | 2015-03-26 08:32:49 +0000 | [diff] [blame] | 4169 | for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) { |
| 4170 | IdentifierInfo *II = WeakUndeclaredIdentifier.first; |
| 4171 | WeakInfo &WI = WeakUndeclaredIdentifier.second; |
| 4172 | AddIdentifierRef(II, WeakUndeclaredIdentifiers); |
| 4173 | AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers); |
| 4174 | AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers); |
| 4175 | WeakUndeclaredIdentifiers.push_back(WI.getUsed()); |
Argyrios Kyrtzidis | ee1afa3 | 2010-08-05 09:48:08 +0000 | [diff] [blame] | 4176 | } |
Kovarththanan Rajaratnam | a9c81a8 | 2010-03-14 07:06:50 +0000 | [diff] [blame] | 4177 | |
Douglas Gregor | 61cac2b | 2009-04-27 20:06:05 +0000 | [diff] [blame] | 4178 | // Build a record containing all of the ext_vector declarations. |
| 4179 | RecordData ExtVectorDecls; |
Douglas Gregor | b7098a3 | 2011-07-28 00:39:29 +0000 | [diff] [blame] | 4180 | AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls); |
Douglas Gregor | 61cac2b | 2009-04-27 20:06:05 +0000 | [diff] [blame] | 4181 | |
Argyrios Kyrtzidis | af2eac2 | 2010-07-06 15:37:04 +0000 | [diff] [blame] | 4182 | // Build a record containing all of the VTable uses information. |
| 4183 | RecordData VTableUses; |
Argyrios Kyrtzidis | edee67f | 2010-08-03 17:29:52 +0000 | [diff] [blame] | 4184 | if (!SemaRef.VTableUses.empty()) { |
Argyrios Kyrtzidis | edee67f | 2010-08-03 17:29:52 +0000 | [diff] [blame] | 4185 | for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) { |
| 4186 | AddDeclRef(SemaRef.VTableUses[I].first, VTableUses); |
| 4187 | AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses); |
| 4188 | VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]); |
| 4189 | } |
Argyrios Kyrtzidis | af2eac2 | 2010-07-06 15:37:04 +0000 | [diff] [blame] | 4190 | } |
| 4191 | |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 4192 | // Build a record containing all of the UnusedLocalTypedefNameCandidates. |
| 4193 | RecordData UnusedLocalTypedefNameCandidates; |
| 4194 | for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates) |
| 4195 | AddDeclRef(TD, UnusedLocalTypedefNameCandidates); |
| 4196 | |
Argyrios Kyrtzidis | 7f76d11 | 2010-08-05 09:48:16 +0000 | [diff] [blame] | 4197 | // Build a record containing all of pending implicit instantiations. |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 4198 | RecordData PendingInstantiations; |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 4199 | for (const auto &I : SemaRef.PendingInstantiations) { |
| 4200 | AddDeclRef(I.first, PendingInstantiations); |
| 4201 | AddSourceLocation(I.second, PendingInstantiations); |
Argyrios Kyrtzidis | 7f76d11 | 2010-08-05 09:48:16 +0000 | [diff] [blame] | 4202 | } |
| 4203 | assert(SemaRef.PendingLocalImplicitInstantiations.empty() && |
| 4204 | "There are local ones at end of translation unit!"); |
| 4205 | |
Argyrios Kyrtzidis | 2d68810 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 4206 | // Build a record containing some declaration references. |
| 4207 | RecordData SemaDeclRefs; |
| 4208 | if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) { |
| 4209 | AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs); |
| 4210 | AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs); |
| 4211 | } |
| 4212 | |
Peter Collingbourne | 9e2c81f | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 4213 | RecordData CUDASpecialDeclRefs; |
| 4214 | if (Context.getcudaConfigureCallDecl()) { |
| 4215 | AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs); |
| 4216 | } |
| 4217 | |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4218 | // Build a record containing all of the known namespaces. |
| 4219 | RecordData KnownNamespaces; |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 4220 | for (const auto &I : SemaRef.KnownNamespaces) { |
| 4221 | if (!I.second) |
| 4222 | AddDeclRef(I.first, KnownNamespaces); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4223 | } |
Douglas Gregor | 112b907 | 2012-10-18 05:31:06 +0000 | [diff] [blame] | 4224 | |
Nick Lewycky | 9c7eb1d | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 4225 | // Build a record of all used, undefined objects that require definitions. |
| 4226 | RecordData UndefinedButUsed; |
Nick Lewycky | f0f5616 | 2013-01-31 03:23:57 +0000 | [diff] [blame] | 4227 | |
| 4228 | SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined; |
Nick Lewycky | 9c7eb1d | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 4229 | SemaRef.getUndefinedButUsed(Undefined); |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 4230 | for (const auto &I : Undefined) { |
| 4231 | AddDeclRef(I.first, UndefinedButUsed); |
| 4232 | AddSourceLocation(I.second, UndefinedButUsed); |
Nick Lewycky | 8334af8 | 2013-01-26 00:35:08 +0000 | [diff] [blame] | 4233 | } |
| 4234 | |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 4235 | // Build a record containing all delete-expressions that we would like to |
| 4236 | // analyze later in AST. |
| 4237 | RecordData DeleteExprsToAnalyze; |
| 4238 | |
| 4239 | for (const auto &DeleteExprsInfo : |
| 4240 | SemaRef.getMismatchingDeleteExpressions()) { |
| 4241 | AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze); |
| 4242 | DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size()); |
| 4243 | for (const auto &DeleteLoc : DeleteExprsInfo.second) { |
| 4244 | AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze); |
| 4245 | DeleteExprsToAnalyze.push_back(DeleteLoc.second); |
| 4246 | } |
| 4247 | } |
| 4248 | |
Douglas Gregor | 112b907 | 2012-10-18 05:31:06 +0000 | [diff] [blame] | 4249 | // Write the control block |
Adrian Prantl | adbd2b1 | 2015-09-22 23:26:31 +0000 | [diff] [blame] | 4250 | uint64_t Signature = WriteControlBlock(PP, Context, isysroot, OutputFile); |
Douglas Gregor | 112b907 | 2012-10-18 05:31:06 +0000 | [diff] [blame] | 4251 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 4252 | // Write the remaining AST contents. |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 4253 | Stream.EnterSubblock(AST_BLOCK_ID, 5); |
Douglas Gregor | 851443c | 2011-08-12 01:39:19 +0000 | [diff] [blame] | 4254 | |
Argyrios Kyrtzidis | 3960540 | 2012-12-13 21:38:23 +0000 | [diff] [blame] | 4255 | // This is so that older clang versions, before the introduction |
| 4256 | // of the control block, can read and reject the newer PCH format. |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 4257 | { |
| 4258 | RecordData Record = {VERSION_MAJOR}; |
| 4259 | Stream.EmitRecord(METADATA_OLD_FORMAT, Record); |
| 4260 | } |
Argyrios Kyrtzidis | 3960540 | 2012-12-13 21:38:23 +0000 | [diff] [blame] | 4261 | |
Douglas Gregor | 851443c | 2011-08-12 01:39:19 +0000 | [diff] [blame] | 4262 | // Create a lexical update block containing all of the declarations in the |
| 4263 | // translation unit that do not come from other AST files. |
| 4264 | const TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); |
Richard Smith | 82f8fcd | 2015-08-06 22:07:25 +0000 | [diff] [blame] | 4265 | SmallVector<uint32_t, 128> NewGlobalKindDeclPairs; |
| 4266 | for (const auto *D : TU->noload_decls()) { |
| 4267 | if (!D->isFromASTFile()) { |
| 4268 | NewGlobalKindDeclPairs.push_back(D->getKind()); |
| 4269 | NewGlobalKindDeclPairs.push_back(GetDeclRef(D)); |
| 4270 | } |
Douglas Gregor | 851443c | 2011-08-12 01:39:19 +0000 | [diff] [blame] | 4271 | } |
| 4272 | |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 4273 | auto *Abv = new llvm::BitCodeAbbrev(); |
Douglas Gregor | 851443c | 2011-08-12 01:39:19 +0000 | [diff] [blame] | 4274 | Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL)); |
| 4275 | Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob)); |
| 4276 | unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv); |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 4277 | { |
| 4278 | RecordData::value_type Record[] = {TU_UPDATE_LEXICAL}; |
| 4279 | Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record, |
| 4280 | bytes(NewGlobalKindDeclPairs)); |
| 4281 | } |
| 4282 | |
Douglas Gregor | 851443c | 2011-08-12 01:39:19 +0000 | [diff] [blame] | 4283 | // And a visible updates block for the translation unit. |
| 4284 | Abv = new llvm::BitCodeAbbrev(); |
| 4285 | Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE)); |
| 4286 | Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6)); |
Douglas Gregor | 851443c | 2011-08-12 01:39:19 +0000 | [diff] [blame] | 4287 | Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob)); |
| 4288 | UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv); |
| 4289 | WriteDeclContextVisibleUpdate(TU); |
Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 4290 | |
| 4291 | // If we have any extern "C" names, write out a visible update for them. |
| 4292 | if (Context.ExternCContext) |
| 4293 | WriteDeclContextVisibleUpdate(Context.ExternCContext); |
Douglas Gregor | 851443c | 2011-08-12 01:39:19 +0000 | [diff] [blame] | 4294 | |
| 4295 | // If the translation unit has an anonymous namespace, and we don't already |
| 4296 | // have an update block for it, write it as an update block. |
Richard Smith | 6ef4293 | 2014-03-20 21:02:00 +0000 | [diff] [blame] | 4297 | // FIXME: Why do we not do this if there's already an update block? |
Douglas Gregor | 851443c | 2011-08-12 01:39:19 +0000 | [diff] [blame] | 4298 | if (NamespaceDecl *NS = TU->getAnonymousNamespace()) { |
| 4299 | ASTWriter::UpdateRecord &Record = DeclUpdates[TU]; |
Richard Smith | 6ef4293 | 2014-03-20 21:02:00 +0000 | [diff] [blame] | 4300 | if (Record.empty()) |
Aaron Ballman | 4f45b71 | 2014-03-21 15:22:56 +0000 | [diff] [blame] | 4301 | Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS)); |
Douglas Gregor | 851443c | 2011-08-12 01:39:19 +0000 | [diff] [blame] | 4302 | } |
Argyrios Kyrtzidis | 442dd80 | 2012-07-02 19:19:01 +0000 | [diff] [blame] | 4303 | |
Richard Smith | 5652c0f | 2014-03-21 01:48:23 +0000 | [diff] [blame] | 4304 | // Add update records for all mangling numbers and static local numbers. |
| 4305 | // These aren't really update records, but this is a convenient way of |
| 4306 | // tagging this rare extra data onto the declarations. |
| 4307 | for (const auto &Number : Context.MangleNumbers) |
| 4308 | if (!Number.first->isFromASTFile()) |
Aaron Ballman | 4f45b71 | 2014-03-21 15:22:56 +0000 | [diff] [blame] | 4309 | DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER, |
| 4310 | Number.second)); |
Richard Smith | 5652c0f | 2014-03-21 01:48:23 +0000 | [diff] [blame] | 4311 | for (const auto &Number : Context.StaticLocalNumbers) |
| 4312 | if (!Number.first->isFromASTFile()) |
Aaron Ballman | 4f45b71 | 2014-03-21 15:22:56 +0000 | [diff] [blame] | 4313 | DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER, |
| 4314 | Number.second)); |
Richard Smith | 5652c0f | 2014-03-21 01:48:23 +0000 | [diff] [blame] | 4315 | |
Argyrios Kyrtzidis | 442dd80 | 2012-07-02 19:19:01 +0000 | [diff] [blame] | 4316 | // Make sure visible decls, added to DeclContexts previously loaded from |
| 4317 | // an AST file, are registered for serialization. |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 4318 | for (const auto *I : UpdatingVisibleDecls) { |
| 4319 | GetDeclRef(I); |
Argyrios Kyrtzidis | 442dd80 | 2012-07-02 19:19:01 +0000 | [diff] [blame] | 4320 | } |
| 4321 | |
Argyrios Kyrtzidis | acfbbd7 | 2013-08-07 21:17:33 +0000 | [diff] [blame] | 4322 | // Make sure all decls associated with an identifier are registered for |
Richard Smith | 79bf920 | 2015-08-24 03:33:22 +0000 | [diff] [blame] | 4323 | // serialization, if we're storing decls with identifiers. |
| 4324 | if (!WritingModule || !getLangOpts().CPlusPlus) { |
| 4325 | llvm::SmallVector<const IdentifierInfo*, 256> IIs; |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 4326 | for (const auto &ID : PP.getIdentifierTable()) { |
| 4327 | const IdentifierInfo *II = ID.second; |
Richard Smith | 79bf920 | 2015-08-24 03:33:22 +0000 | [diff] [blame] | 4328 | if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization()) |
| 4329 | IIs.push_back(II); |
| 4330 | } |
| 4331 | // Sort the identifiers to visit based on their name. |
| 4332 | std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>()); |
| 4333 | for (const IdentifierInfo *II : IIs) { |
| 4334 | for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II), |
| 4335 | DEnd = SemaRef.IdResolver.end(); |
| 4336 | D != DEnd; ++D) { |
| 4337 | GetDeclRef(*D); |
| 4338 | } |
Argyrios Kyrtzidis | acfbbd7 | 2013-08-07 21:17:33 +0000 | [diff] [blame] | 4339 | } |
| 4340 | } |
| 4341 | |
Douglas Gregor | 5204bde | 2011-08-02 16:26:37 +0000 | [diff] [blame] | 4342 | // Form the record of special types. |
| 4343 | RecordData SpecialTypes; |
Douglas Gregor | 5204bde | 2011-08-02 16:26:37 +0000 | [diff] [blame] | 4344 | AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes); |
Douglas Gregor | 5204bde | 2011-08-02 16:26:37 +0000 | [diff] [blame] | 4345 | AddTypeRef(Context.getFILEType(), SpecialTypes); |
| 4346 | AddTypeRef(Context.getjmp_bufType(), SpecialTypes); |
| 4347 | AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes); |
| 4348 | AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes); |
| 4349 | AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes); |
Douglas Gregor | 5204bde | 2011-08-02 16:26:37 +0000 | [diff] [blame] | 4350 | AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes); |
Rafael Espindola | 6cfa82b | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 4351 | AddTypeRef(Context.getucontext_tType(), SpecialTypes); |
Douglas Gregor | a28bcdd | 2011-12-01 02:07:58 +0000 | [diff] [blame] | 4352 | |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 4353 | if (Chain) { |
| 4354 | // Write the mapping information describing our module dependencies and how |
| 4355 | // each of those modules were mapped into our own offset/ID space, so that |
| 4356 | // the reader can build the appropriate mapping to its own offset/ID space. |
| 4357 | // The map consists solely of a blob with the following format: |
| 4358 | // *(module-name-len:i16 module-name:len*i8 |
| 4359 | // source-location-offset:i32 |
| 4360 | // identifier-id:i32 |
| 4361 | // preprocessed-entity-id:i32 |
| 4362 | // macro-definition-id:i32 |
Douglas Gregor | 253eefe | 2011-12-01 00:59:36 +0000 | [diff] [blame] | 4363 | // submodule-id:i32 |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 4364 | // selector-id:i32 |
| 4365 | // declaration-id:i32 |
| 4366 | // c++-base-specifiers-id:i32 |
| 4367 | // type-id:i32) |
| 4368 | // |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 4369 | auto *Abbrev = new BitCodeAbbrev(); |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 4370 | Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP)); |
| 4371 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 4372 | unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev); |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 4373 | SmallString<2048> Buffer; |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 4374 | { |
| 4375 | llvm::raw_svector_ostream Out(Buffer); |
Ben Langmuir | 785180e | 2014-10-20 16:27:30 +0000 | [diff] [blame] | 4376 | for (ModuleFile *M : Chain->ModuleMgr) { |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 4377 | using namespace llvm::support; |
| 4378 | endian::Writer<little> LE(Out); |
Ben Langmuir | 785180e | 2014-10-20 16:27:30 +0000 | [diff] [blame] | 4379 | StringRef FileName = M->FileName; |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 4380 | LE.write<uint16_t>(FileName.size()); |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 4381 | Out.write(FileName.data(), FileName.size()); |
Ben Langmuir | fe971d9 | 2014-08-16 04:54:18 +0000 | [diff] [blame] | 4382 | |
Ben Langmuir | 785180e | 2014-10-20 16:27:30 +0000 | [diff] [blame] | 4383 | // Note: if a base ID was uint max, it would not be possible to load |
| 4384 | // another module after it or have more than one entity inside it. |
| 4385 | uint32_t None = std::numeric_limits<uint32_t>::max(); |
| 4386 | |
| 4387 | auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) { |
| 4388 | assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high"); |
| 4389 | if (ShouldWrite) |
| 4390 | LE.write<uint32_t>(BaseID); |
| 4391 | else |
| 4392 | LE.write<uint32_t>(None); |
| 4393 | }; |
| 4394 | |
Ben Langmuir | fe971d9 | 2014-08-16 04:54:18 +0000 | [diff] [blame] | 4395 | // These values should be unique within a chain, since they will be read |
| 4396 | // as keys into ContinuousRangeMaps. |
Ben Langmuir | 785180e | 2014-10-20 16:27:30 +0000 | [diff] [blame] | 4397 | writeBaseIDOrNone(M->SLocEntryBaseOffset, M->LocalNumSLocEntries); |
| 4398 | writeBaseIDOrNone(M->BaseIdentifierID, M->LocalNumIdentifiers); |
| 4399 | writeBaseIDOrNone(M->BaseMacroID, M->LocalNumMacros); |
| 4400 | writeBaseIDOrNone(M->BasePreprocessedEntityID, |
| 4401 | M->NumPreprocessedEntities); |
| 4402 | writeBaseIDOrNone(M->BaseSubmoduleID, M->LocalNumSubmodules); |
| 4403 | writeBaseIDOrNone(M->BaseSelectorID, M->LocalNumSelectors); |
| 4404 | writeBaseIDOrNone(M->BaseDeclID, M->LocalNumDecls); |
| 4405 | writeBaseIDOrNone(M->BaseTypeIndex, M->LocalNumTypes); |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 4406 | } |
| 4407 | } |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 4408 | RecordData::value_type Record[] = {MODULE_OFFSET_MAP}; |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 4409 | Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record, |
| 4410 | Buffer.data(), Buffer.size()); |
| 4411 | } |
Richard Smith | b9eab6d | 2014-03-20 19:44:17 +0000 | [diff] [blame] | 4412 | |
Richard Smith | b9eab6d | 2014-03-20 19:44:17 +0000 | [diff] [blame] | 4413 | RecordData DeclUpdatesOffsetsRecord; |
| 4414 | |
Richard Smith | 59442b4 | 2014-03-20 20:07:19 +0000 | [diff] [blame] | 4415 | // Keep writing types, declarations, and declaration update records |
| 4416 | // until we've emitted all of them. |
Richard Smith | 01b2cb4 | 2014-07-26 06:37:51 +0000 | [diff] [blame] | 4417 | Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5); |
| 4418 | WriteTypeAbbrevs(); |
| 4419 | WriteDeclAbbrevs(); |
Richard Smith | 59442b4 | 2014-03-20 20:07:19 +0000 | [diff] [blame] | 4420 | do { |
| 4421 | WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord); |
| 4422 | while (!DeclTypesToEmit.empty()) { |
| 4423 | DeclOrType DOT = DeclTypesToEmit.front(); |
| 4424 | DeclTypesToEmit.pop(); |
| 4425 | if (DOT.isType()) |
| 4426 | WriteType(DOT.getType()); |
| 4427 | else |
| 4428 | WriteDecl(Context, DOT.getDecl()); |
| 4429 | } |
| 4430 | } while (!DeclUpdates.empty()); |
Richard Smith | b9eab6d | 2014-03-20 19:44:17 +0000 | [diff] [blame] | 4431 | Stream.ExitBlock(); |
| 4432 | |
Richard Smith | b9eab6d | 2014-03-20 19:44:17 +0000 | [diff] [blame] | 4433 | DoneWritingDeclsAndTypes = true; |
| 4434 | |
| 4435 | // These things can only be done once we've written out decls and types. |
| 4436 | WriteTypeDeclOffsets(); |
Richard Smith | 5652c0f | 2014-03-21 01:48:23 +0000 | [diff] [blame] | 4437 | if (!DeclUpdatesOffsetsRecord.empty()) |
| 4438 | Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord); |
Richard Smith | b9eab6d | 2014-03-20 19:44:17 +0000 | [diff] [blame] | 4439 | WriteCXXBaseSpecifiersOffsets(); |
Richard Smith | c2bb818 | 2015-03-24 06:36:48 +0000 | [diff] [blame] | 4440 | WriteCXXCtorInitializersOffsets(); |
Richard Smith | b9eab6d | 2014-03-20 19:44:17 +0000 | [diff] [blame] | 4441 | WriteFileDeclIDsMap(); |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 4442 | WriteSourceManagerBlock(Context.getSourceManager(), PP); |
Richard Smith | b9eab6d | 2014-03-20 19:44:17 +0000 | [diff] [blame] | 4443 | WriteComments(); |
Argyrios Kyrtzidis | ffb3558 | 2013-03-14 04:44:56 +0000 | [diff] [blame] | 4444 | WritePreprocessor(PP, isModule); |
Richard Smith | 7ed1bc9 | 2014-12-05 22:42:13 +0000 | [diff] [blame] | 4445 | WriteHeaderSearch(PP.getHeaderSearchInfo()); |
Sebastian Redl | a19a67f | 2010-08-03 21:58:15 +0000 | [diff] [blame] | 4446 | WriteSelectors(SemaRef); |
Fariborz Jahanian | c51609a | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 4447 | WriteReferencedSelectorsPool(SemaRef); |
Richard Smith | 9c25418 | 2015-07-19 21:41:12 +0000 | [diff] [blame] | 4448 | WriteLateParsedTemplates(SemaRef); |
Argyrios Kyrtzidis | ffb3558 | 2013-03-14 04:44:56 +0000 | [diff] [blame] | 4449 | WriteIdentifierTable(PP, SemaRef.IdResolver, isModule); |
Peter Collingbourne | 5df20e0 | 2011-02-15 19:46:30 +0000 | [diff] [blame] | 4450 | WriteFPPragmaOptions(SemaRef.getFPOptions()); |
| 4451 | WriteOpenCLExtensions(SemaRef); |
Argyrios Kyrtzidis | 0f06b98 | 2013-03-27 17:17:23 +0000 | [diff] [blame] | 4452 | WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule); |
Douglas Gregor | 652d82a | 2009-04-18 05:55:16 +0000 | [diff] [blame] | 4453 | |
Douglas Gregor | a89c5ac | 2011-12-06 01:10:29 +0000 | [diff] [blame] | 4454 | // If we're emitting a module, write out the submodule information. |
| 4455 | if (WritingModule) |
| 4456 | WriteSubmodules(WritingModule); |
| 4457 | |
Douglas Gregor | 5204bde | 2011-08-02 16:26:37 +0000 | [diff] [blame] | 4458 | Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes); |
| 4459 | |
Douglas Gregor | d4df865 | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 4460 | // Write the record containing external, unnamed definitions. |
Ben Langmuir | 332aafe | 2014-01-31 01:06:56 +0000 | [diff] [blame] | 4461 | if (!EagerlyDeserializedDecls.empty()) |
| 4462 | Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls); |
Douglas Gregor | d4df865 | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 4463 | |
| 4464 | // Write the record containing tentative definitions. |
| 4465 | if (!TentativeDefinitions.empty()) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 4466 | Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions); |
Douglas Gregor | acfc76c | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 4467 | |
Argyrios Kyrtzidis | 35672e7 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 4468 | // Write the record containing unused file scoped decls. |
| 4469 | if (!UnusedFileScopedDecls.empty()) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 4470 | Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls); |
Kovarththanan Rajaratnam | a9c81a8 | 2010-03-14 07:06:50 +0000 | [diff] [blame] | 4471 | |
Argyrios Kyrtzidis | ee1afa3 | 2010-08-05 09:48:08 +0000 | [diff] [blame] | 4472 | // Write the record containing weak undeclared identifiers. |
| 4473 | if (!WeakUndeclaredIdentifiers.empty()) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 4474 | Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS, |
Argyrios Kyrtzidis | ee1afa3 | 2010-08-05 09:48:08 +0000 | [diff] [blame] | 4475 | WeakUndeclaredIdentifiers); |
| 4476 | |
Douglas Gregor | 61cac2b | 2009-04-27 20:06:05 +0000 | [diff] [blame] | 4477 | // Write the record containing ext_vector type names. |
| 4478 | if (!ExtVectorDecls.empty()) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 4479 | Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4480 | |
Argyrios Kyrtzidis | af2eac2 | 2010-07-06 15:37:04 +0000 | [diff] [blame] | 4481 | // Write the record containing VTable uses information. |
| 4482 | if (!VTableUses.empty()) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 4483 | Stream.EmitRecord(VTABLE_USES, VTableUses); |
Argyrios Kyrtzidis | af2eac2 | 2010-07-06 15:37:04 +0000 | [diff] [blame] | 4484 | |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 4485 | // Write the record containing potentially unused local typedefs. |
| 4486 | if (!UnusedLocalTypedefNameCandidates.empty()) |
| 4487 | Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES, |
| 4488 | UnusedLocalTypedefNameCandidates); |
| 4489 | |
Argyrios Kyrtzidis | 7f76d11 | 2010-08-05 09:48:16 +0000 | [diff] [blame] | 4490 | // Write the record containing pending implicit instantiations. |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 4491 | if (!PendingInstantiations.empty()) |
| 4492 | Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations); |
Argyrios Kyrtzidis | 7f76d11 | 2010-08-05 09:48:16 +0000 | [diff] [blame] | 4493 | |
Argyrios Kyrtzidis | 2d68810 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 4494 | // Write the record containing declaration references of Sema. |
| 4495 | if (!SemaDeclRefs.empty()) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 4496 | Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs); |
Argyrios Kyrtzidis | 2d68810 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 4497 | |
Peter Collingbourne | 9e2c81f | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 4498 | // Write the record containing CUDA-specific declaration references. |
| 4499 | if (!CUDASpecialDeclRefs.empty()) |
| 4500 | Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs); |
Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 4501 | |
| 4502 | // Write the delegating constructors. |
| 4503 | if (!DelegatingCtorDecls.empty()) |
| 4504 | Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls); |
Peter Collingbourne | 9e2c81f | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 4505 | |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4506 | // Write the known namespaces. |
| 4507 | if (!KnownNamespaces.empty()) |
| 4508 | Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces); |
Nick Lewycky | 8334af8 | 2013-01-26 00:35:08 +0000 | [diff] [blame] | 4509 | |
Nick Lewycky | 9c7eb1d | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 4510 | // Write the undefined internal functions and variables, and inline functions. |
| 4511 | if (!UndefinedButUsed.empty()) |
| 4512 | Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed); |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 4513 | |
| 4514 | if (!DeleteExprsToAnalyze.empty()) |
| 4515 | Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze); |
| 4516 | |
Douglas Gregor | 851443c | 2011-08-12 01:39:19 +0000 | [diff] [blame] | 4517 | // Write the visible updates to DeclContexts. |
Richard Smith | cd45dbc | 2014-04-19 03:48:30 +0000 | [diff] [blame] | 4518 | for (auto *DC : UpdatedDeclContexts) |
| 4519 | WriteDeclContextVisibleUpdate(DC); |
Douglas Gregor | 851443c | 2011-08-12 01:39:19 +0000 | [diff] [blame] | 4520 | |
Douglas Gregor | 959bb06 | 2011-12-03 01:15:29 +0000 | [diff] [blame] | 4521 | if (!WritingModule) { |
| 4522 | // Write the submodules that were imported, if any. |
Aaron Ballman | 4f45b71 | 2014-03-21 15:22:56 +0000 | [diff] [blame] | 4523 | struct ModuleInfo { |
| 4524 | uint64_t ID; |
| 4525 | Module *M; |
| 4526 | ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {} |
| 4527 | }; |
Richard Smith | 56be754 | 2014-03-21 00:33:59 +0000 | [diff] [blame] | 4528 | llvm::SmallVector<ModuleInfo, 64> Imports; |
Aaron Ballman | bbc3121 | 2014-03-14 20:59:21 +0000 | [diff] [blame] | 4529 | for (const auto *I : Context.local_imports()) { |
Douglas Gregor | 959bb06 | 2011-12-03 01:15:29 +0000 | [diff] [blame] | 4530 | assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end()); |
Aaron Ballman | 4f45b71 | 2014-03-21 15:22:56 +0000 | [diff] [blame] | 4531 | Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()], |
| 4532 | I->getImportedModule())); |
Douglas Gregor | 959bb06 | 2011-12-03 01:15:29 +0000 | [diff] [blame] | 4533 | } |
Richard Smith | 56be754 | 2014-03-21 00:33:59 +0000 | [diff] [blame] | 4534 | |
| 4535 | if (!Imports.empty()) { |
| 4536 | auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) { |
| 4537 | return A.ID < B.ID; |
| 4538 | }; |
Ben Langmuir | 5f95c8f | 2014-09-08 20:36:26 +0000 | [diff] [blame] | 4539 | auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) { |
| 4540 | return A.ID == B.ID; |
| 4541 | }; |
Richard Smith | 56be754 | 2014-03-21 00:33:59 +0000 | [diff] [blame] | 4542 | |
| 4543 | // Sort and deduplicate module IDs. |
| 4544 | std::sort(Imports.begin(), Imports.end(), Cmp); |
Ben Langmuir | 5f95c8f | 2014-09-08 20:36:26 +0000 | [diff] [blame] | 4545 | Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq), |
Richard Smith | 56be754 | 2014-03-21 00:33:59 +0000 | [diff] [blame] | 4546 | Imports.end()); |
| 4547 | |
| 4548 | RecordData ImportedModules; |
| 4549 | for (const auto &Import : Imports) { |
| 4550 | ImportedModules.push_back(Import.ID); |
| 4551 | // FIXME: If the module has macros imported then later has declarations |
| 4552 | // imported, this location won't be the right one as a location for the |
| 4553 | // declaration imports. |
Richard Smith | 10434f3 | 2015-05-02 02:08:26 +0000 | [diff] [blame] | 4554 | AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules); |
Richard Smith | 56be754 | 2014-03-21 00:33:59 +0000 | [diff] [blame] | 4555 | } |
| 4556 | |
Douglas Gregor | 959bb06 | 2011-12-03 01:15:29 +0000 | [diff] [blame] | 4557 | Stream.EmitRecord(IMPORTED_MODULES, ImportedModules); |
| 4558 | } |
Douglas Gregor | 0a83913 | 2011-12-03 00:59:55 +0000 | [diff] [blame] | 4559 | } |
Douglas Gregor | cb28f9d | 2012-10-09 23:05:51 +0000 | [diff] [blame] | 4560 | |
Douglas Gregor | 851443c | 2011-08-12 01:39:19 +0000 | [diff] [blame] | 4561 | WriteDeclReplacementsBlock(); |
Douglas Gregor | 404cdde | 2012-01-27 01:47:08 +0000 | [diff] [blame] | 4562 | WriteObjCCategories(); |
Dario Domizioli | 13a0a38 | 2014-05-23 12:13:25 +0000 | [diff] [blame] | 4563 | if(!WritingModule) |
| 4564 | WriteOptimizePragmaOptions(SemaRef); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 4565 | |
Douglas Gregor | 08f0129 | 2009-04-17 22:13:46 +0000 | [diff] [blame] | 4566 | // Some simple statistics |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 4567 | RecordData::value_type Record[] = { |
| 4568 | NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts}; |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 4569 | Stream.EmitRecord(STATISTICS, Record); |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 4570 | Stream.ExitBlock(); |
Adrian Prantl | adbd2b1 | 2015-09-22 23:26:31 +0000 | [diff] [blame] | 4571 | |
Douglas Gregor | 6623e1f | 2015-11-03 18:33:07 +0000 | [diff] [blame] | 4572 | // Write the module file extension blocks. |
| 4573 | for (const auto &ExtWriter : ModuleFileExtensionWriters) |
Douglas Gregor | 8f64ca1 | 2015-12-08 22:43:32 +0000 | [diff] [blame] | 4574 | WriteModuleFileExtension(SemaRef, *ExtWriter); |
Douglas Gregor | 6623e1f | 2015-11-03 18:33:07 +0000 | [diff] [blame] | 4575 | |
Adrian Prantl | adbd2b1 | 2015-09-22 23:26:31 +0000 | [diff] [blame] | 4576 | return Signature; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 4577 | } |
| 4578 | |
Richard Smith | b9eab6d | 2014-03-20 19:44:17 +0000 | [diff] [blame] | 4579 | void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) { |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 4580 | if (DeclUpdates.empty()) |
| 4581 | return; |
| 4582 | |
Richard Smith | 59442b4 | 2014-03-20 20:07:19 +0000 | [diff] [blame] | 4583 | DeclUpdateMap LocalUpdates; |
| 4584 | LocalUpdates.swap(DeclUpdates); |
| 4585 | |
Richard Smith | 6ef4293 | 2014-03-20 21:02:00 +0000 | [diff] [blame] | 4586 | for (auto &DeclUpdate : LocalUpdates) { |
| 4587 | const Decl *D = DeclUpdate.first; |
Argyrios Kyrtzidis | 3ba70b8 | 2010-10-24 17:26:46 +0000 | [diff] [blame] | 4588 | |
Richard Smith | d28ac5b | 2014-03-22 23:33:22 +0000 | [diff] [blame] | 4589 | bool HasUpdatedBody = false; |
Richard Smith | 6ef4293 | 2014-03-20 21:02:00 +0000 | [diff] [blame] | 4590 | RecordData Record; |
| 4591 | for (auto &Update : DeclUpdate.second) { |
| 4592 | DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind(); |
| 4593 | |
| 4594 | Record.push_back(Kind); |
| 4595 | switch (Kind) { |
| 4596 | case UPD_CXX_ADDED_IMPLICIT_MEMBER: |
| 4597 | case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION: |
| 4598 | case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: |
Richard Smith | cd45dbc | 2014-04-19 03:48:30 +0000 | [diff] [blame] | 4599 | assert(Update.getDecl() && "no decl to add?"); |
Richard Smith | 6ef4293 | 2014-03-20 21:02:00 +0000 | [diff] [blame] | 4600 | Record.push_back(GetDeclRef(Update.getDecl())); |
| 4601 | break; |
| 4602 | |
Richard Smith | 4d23579 | 2014-08-07 18:53:08 +0000 | [diff] [blame] | 4603 | case UPD_CXX_ADDED_FUNCTION_DEFINITION: |
Richard Smith | d28ac5b | 2014-03-22 23:33:22 +0000 | [diff] [blame] | 4604 | // An updated body is emitted last, so that the reader doesn't need |
| 4605 | // to skip over the lazy body to reach statements for other records. |
| 4606 | Record.pop_back(); |
| 4607 | HasUpdatedBody = true; |
| 4608 | break; |
| 4609 | |
Richard Smith | 4d23579 | 2014-08-07 18:53:08 +0000 | [diff] [blame] | 4610 | case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER: |
| 4611 | AddSourceLocation(Update.getLoc(), Record); |
| 4612 | break; |
| 4613 | |
Richard Smith | cd45dbc | 2014-04-19 03:48:30 +0000 | [diff] [blame] | 4614 | case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: { |
| 4615 | auto *RD = cast<CXXRecordDecl>(D); |
Chandler Carruth | 8a3d24d | 2015-03-26 04:27:10 +0000 | [diff] [blame] | 4616 | UpdatedDeclContexts.insert(RD->getPrimaryContext()); |
Richard Smith | cd45dbc | 2014-04-19 03:48:30 +0000 | [diff] [blame] | 4617 | AddCXXDefinitionData(RD, Record); |
| 4618 | Record.push_back(WriteDeclContextLexicalBlock( |
| 4619 | *Context, const_cast<CXXRecordDecl *>(RD))); |
| 4620 | |
| 4621 | // This state is sometimes updated by template instantiation, when we |
| 4622 | // switch from the specialization referring to the template declaration |
| 4623 | // to it referring to the template definition. |
| 4624 | if (auto *MSInfo = RD->getMemberSpecializationInfo()) { |
| 4625 | Record.push_back(MSInfo->getTemplateSpecializationKind()); |
| 4626 | AddSourceLocation(MSInfo->getPointOfInstantiation(), Record); |
| 4627 | } else { |
| 4628 | auto *Spec = cast<ClassTemplateSpecializationDecl>(RD); |
| 4629 | Record.push_back(Spec->getTemplateSpecializationKind()); |
| 4630 | AddSourceLocation(Spec->getPointOfInstantiation(), Record); |
Richard Smith | df35205 | 2014-05-22 20:59:29 +0000 | [diff] [blame] | 4631 | |
| 4632 | // The instantiation might have been resolved to a partial |
| 4633 | // specialization. If so, record which one. |
| 4634 | auto From = Spec->getInstantiatedFrom(); |
| 4635 | if (auto PartialSpec = |
| 4636 | From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) { |
| 4637 | Record.push_back(true); |
| 4638 | AddDeclRef(PartialSpec, Record); |
| 4639 | AddTemplateArgumentList(&Spec->getTemplateInstantiationArgs(), |
| 4640 | Record); |
| 4641 | } else { |
| 4642 | Record.push_back(false); |
| 4643 | } |
Richard Smith | cd45dbc | 2014-04-19 03:48:30 +0000 | [diff] [blame] | 4644 | } |
| 4645 | Record.push_back(RD->getTagKind()); |
| 4646 | AddSourceLocation(RD->getLocation(), Record); |
| 4647 | AddSourceLocation(RD->getLocStart(), Record); |
| 4648 | AddSourceLocation(RD->getRBraceLoc(), Record); |
| 4649 | |
| 4650 | // Instantiation may change attributes; write them all out afresh. |
| 4651 | Record.push_back(D->hasAttrs()); |
| 4652 | if (Record.back()) |
Craig Topper | 8c2a2a0 | 2014-08-30 16:55:39 +0000 | [diff] [blame] | 4653 | WriteAttributes(llvm::makeArrayRef(D->getAttrs().begin(), |
| 4654 | D->getAttrs().size()), Record); |
Richard Smith | cd45dbc | 2014-04-19 03:48:30 +0000 | [diff] [blame] | 4655 | |
| 4656 | // FIXME: Ensure we don't get here for explicit instantiations. |
| 4657 | break; |
| 4658 | } |
| 4659 | |
Richard Smith | f813400 | 2015-03-10 01:41:22 +0000 | [diff] [blame] | 4660 | case UPD_CXX_RESOLVED_DTOR_DELETE: |
| 4661 | AddDeclRef(Update.getDecl(), Record); |
| 4662 | break; |
| 4663 | |
Richard Smith | 564417a | 2014-03-20 21:47:22 +0000 | [diff] [blame] | 4664 | case UPD_CXX_RESOLVED_EXCEPTION_SPEC: |
| 4665 | addExceptionSpec( |
| 4666 | *this, |
| 4667 | cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(), |
| 4668 | Record); |
| 4669 | break; |
| 4670 | |
Richard Smith | 6ef4293 | 2014-03-20 21:02:00 +0000 | [diff] [blame] | 4671 | case UPD_CXX_DEDUCED_RETURN_TYPE: |
| 4672 | Record.push_back(GetOrCreateTypeID(Update.getType())); |
| 4673 | break; |
| 4674 | |
| 4675 | case UPD_DECL_MARKED_USED: |
| 4676 | break; |
Richard Smith | 5652c0f | 2014-03-21 01:48:23 +0000 | [diff] [blame] | 4677 | |
| 4678 | case UPD_MANGLING_NUMBER: |
| 4679 | case UPD_STATIC_LOCAL_NUMBER: |
| 4680 | Record.push_back(Update.getNumber()); |
| 4681 | break; |
Richard Smith | 65ebb4a | 2015-03-26 04:09:53 +0000 | [diff] [blame] | 4682 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 4683 | case UPD_DECL_MARKED_OPENMP_THREADPRIVATE: |
| 4684 | AddSourceRange(D->getAttr<OMPThreadPrivateDeclAttr>()->getRange(), |
| 4685 | Record); |
| 4686 | break; |
Richard Smith | 65ebb4a | 2015-03-26 04:09:53 +0000 | [diff] [blame] | 4687 | |
| 4688 | case UPD_DECL_EXPORTED: |
Richard Smith | 4caa449 | 2015-05-15 02:34:32 +0000 | [diff] [blame] | 4689 | Record.push_back(getSubmoduleID(Update.getModule())); |
Richard Smith | 65ebb4a | 2015-03-26 04:09:53 +0000 | [diff] [blame] | 4690 | break; |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 4691 | |
| 4692 | case UPD_ADDED_ATTR_TO_RECORD: |
| 4693 | WriteAttributes(llvm::makeArrayRef(Update.getAttr()), Record); |
| 4694 | break; |
Richard Smith | 6ef4293 | 2014-03-20 21:02:00 +0000 | [diff] [blame] | 4695 | } |
| 4696 | } |
| 4697 | |
Richard Smith | d28ac5b | 2014-03-22 23:33:22 +0000 | [diff] [blame] | 4698 | if (HasUpdatedBody) { |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 4699 | const auto *Def = cast<FunctionDecl>(D); |
Richard Smith | 4d23579 | 2014-08-07 18:53:08 +0000 | [diff] [blame] | 4700 | Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION); |
Richard Smith | d28ac5b | 2014-03-22 23:33:22 +0000 | [diff] [blame] | 4701 | Record.push_back(Def->isInlined()); |
| 4702 | AddSourceLocation(Def->getInnerLocStart(), Record); |
| 4703 | AddFunctionDefinition(Def, Record); |
| 4704 | } |
| 4705 | |
Richard Smith | cd45dbc | 2014-04-19 03:48:30 +0000 | [diff] [blame] | 4706 | OffsetsRecord.push_back(GetDeclRef(D)); |
| 4707 | OffsetsRecord.push_back(Stream.GetCurrentBitNo()); |
| 4708 | |
Richard Smith | 6ef4293 | 2014-03-20 21:02:00 +0000 | [diff] [blame] | 4709 | Stream.EmitRecord(DECL_UPDATES, Record); |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 4710 | |
Richard Smith | c2bb818 | 2015-03-24 06:36:48 +0000 | [diff] [blame] | 4711 | FlushPendingAfterDecl(); |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 4712 | } |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 4713 | } |
| 4714 | |
Argyrios Kyrtzidis | 97bfda9 | 2010-10-24 17:26:43 +0000 | [diff] [blame] | 4715 | void ASTWriter::WriteDeclReplacementsBlock() { |
Sebastian Redl | e7c1fe6 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 4716 | if (ReplacedDecls.empty()) |
| 4717 | return; |
| 4718 | |
| 4719 | RecordData Record; |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 4720 | for (const auto &I : ReplacedDecls) { |
| 4721 | Record.push_back(I.ID); |
| 4722 | Record.push_back(I.Offset); |
| 4723 | Record.push_back(I.Loc); |
Sebastian Redl | e7c1fe6 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 4724 | } |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 4725 | Stream.EmitRecord(DECL_REPLACEMENTS, Record); |
Sebastian Redl | e7c1fe6 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 4726 | } |
| 4727 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4728 | void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 4729 | Record.push_back(Loc.getRawEncoding()); |
| 4730 | } |
| 4731 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4732 | void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) { |
Chris Lattner | ca025db | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 4733 | AddSourceLocation(Range.getBegin(), Record); |
| 4734 | AddSourceLocation(Range.getEnd(), Record); |
| 4735 | } |
| 4736 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4737 | void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 4738 | Record.push_back(Value.getBitWidth()); |
Benjamin Kramer | 25f9ea6 | 2010-09-06 23:43:28 +0000 | [diff] [blame] | 4739 | const uint64_t *Words = Value.getRawData(); |
| 4740 | Record.append(Words, Words + Value.getNumWords()); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 4741 | } |
| 4742 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4743 | void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) { |
Douglas Gregor | 1daeb69 | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 4744 | Record.push_back(Value.isUnsigned()); |
| 4745 | AddAPInt(Value, Record); |
| 4746 | } |
| 4747 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4748 | void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) { |
Douglas Gregor | e0a3a51 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 4749 | AddAPInt(Value.bitcastToAPInt(), Record); |
| 4750 | } |
| 4751 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4752 | void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) { |
Douglas Gregor | 4621c6a | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 4753 | Record.push_back(getIdentifierRef(II)); |
| 4754 | } |
| 4755 | |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 4756 | IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) { |
Craig Topper | a13603a | 2014-05-22 05:54:18 +0000 | [diff] [blame] | 4757 | if (!II) |
Douglas Gregor | 4621c6a | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 4758 | return 0; |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 4759 | |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 4760 | IdentID &ID = IdentifierIDs[II]; |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 4761 | if (ID == 0) |
Sebastian Redl | ff4a295 | 2010-07-23 23:49:55 +0000 | [diff] [blame] | 4762 | ID = NextIdentID++; |
Douglas Gregor | 4621c6a | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 4763 | return ID; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 4764 | } |
| 4765 | |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 4766 | MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) { |
Douglas Gregor | cb28f9d | 2012-10-09 23:05:51 +0000 | [diff] [blame] | 4767 | // Don't emit builtin macros like __LINE__ to the AST file unless they |
| 4768 | // have been redefined by the header (in which case they are not |
| 4769 | // isBuiltinMacro). |
Craig Topper | a13603a | 2014-05-22 05:54:18 +0000 | [diff] [blame] | 4770 | if (!MI || MI->isBuiltinMacro()) |
Douglas Gregor | cb28f9d | 2012-10-09 23:05:51 +0000 | [diff] [blame] | 4771 | return 0; |
| 4772 | |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 4773 | MacroID &ID = MacroIDs[MI]; |
| 4774 | if (ID == 0) { |
Douglas Gregor | cb28f9d | 2012-10-09 23:05:51 +0000 | [diff] [blame] | 4775 | ID = NextMacroID++; |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 4776 | MacroInfoToEmitData Info = { Name, MI, ID }; |
| 4777 | MacroInfosToEmit.push_back(Info); |
| 4778 | } |
Douglas Gregor | cb28f9d | 2012-10-09 23:05:51 +0000 | [diff] [blame] | 4779 | return ID; |
| 4780 | } |
| 4781 | |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 4782 | MacroID ASTWriter::getMacroID(MacroInfo *MI) { |
Craig Topper | a13603a | 2014-05-22 05:54:18 +0000 | [diff] [blame] | 4783 | if (!MI || MI->isBuiltinMacro()) |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 4784 | return 0; |
| 4785 | |
| 4786 | assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!"); |
| 4787 | return MacroIDs[MI]; |
| 4788 | } |
| 4789 | |
| 4790 | uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) { |
Richard Smith | d732939 | 2015-04-21 21:46:32 +0000 | [diff] [blame] | 4791 | return IdentMacroDirectivesOffsetMap.lookup(Name); |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 4792 | } |
| 4793 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4794 | void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) { |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 4795 | Record.push_back(getSelectorRef(SelRef)); |
| 4796 | } |
| 4797 | |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 4798 | SelectorID ASTWriter::getSelectorRef(Selector Sel) { |
Craig Topper | a13603a | 2014-05-22 05:54:18 +0000 | [diff] [blame] | 4799 | if (Sel.getAsOpaquePtr() == nullptr) { |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 4800 | return 0; |
Steve Naroff | 2ddea05 | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 4801 | } |
| 4802 | |
Douglas Gregor | 8d7edce | 2013-02-08 21:30:59 +0000 | [diff] [blame] | 4803 | SelectorID SID = SelectorIDs[Sel]; |
Sebastian Redl | d95a56e | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 4804 | if (SID == 0 && Chain) { |
| 4805 | // This might trigger a ReadSelector callback, which will set the ID for |
| 4806 | // this selector. |
| 4807 | Chain->LoadSelector(Sel); |
Douglas Gregor | 8d7edce | 2013-02-08 21:30:59 +0000 | [diff] [blame] | 4808 | SID = SelectorIDs[Sel]; |
Sebastian Redl | d95a56e | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 4809 | } |
Steve Naroff | 2ddea05 | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 4810 | if (SID == 0) { |
Sebastian Redl | d95a56e | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 4811 | SID = NextSelectorID++; |
Douglas Gregor | 8d7edce | 2013-02-08 21:30:59 +0000 | [diff] [blame] | 4812 | SelectorIDs[Sel] = SID; |
Steve Naroff | 2ddea05 | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 4813 | } |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 4814 | return SID; |
Steve Naroff | 2ddea05 | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 4815 | } |
| 4816 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4817 | void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) { |
Chris Lattner | cba8614 | 2010-05-10 00:25:06 +0000 | [diff] [blame] | 4818 | AddDeclRef(Temp->getDestructor(), Record); |
| 4819 | } |
| 4820 | |
Richard Smith | c2bb818 | 2015-03-24 06:36:48 +0000 | [diff] [blame] | 4821 | void ASTWriter::AddCXXCtorInitializersRef(ArrayRef<CXXCtorInitializer *> Inits, |
| 4822 | RecordDataImpl &Record) { |
| 4823 | assert(!Inits.empty() && "Empty ctor initializer sets are not recorded"); |
| 4824 | CXXCtorInitializersToWrite.push_back( |
| 4825 | QueuedCXXCtorInitializers(NextCXXCtorInitializersID, Inits)); |
| 4826 | Record.push_back(NextCXXCtorInitializersID++); |
| 4827 | } |
| 4828 | |
Douglas Gregor | d4c5ed0 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 4829 | void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases, |
Richard Smith | c2bb818 | 2015-03-24 06:36:48 +0000 | [diff] [blame] | 4830 | CXXBaseSpecifier const *BasesEnd, |
Douglas Gregor | d4c5ed0 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 4831 | RecordDataImpl &Record) { |
| 4832 | assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded"); |
| 4833 | CXXBaseSpecifiersToWrite.push_back( |
| 4834 | QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID, |
| 4835 | Bases, BasesEnd)); |
| 4836 | Record.push_back(NextCXXBaseSpecifiersID++); |
| 4837 | } |
| 4838 | |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 4839 | void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind, |
Argyrios Kyrtzidis | ae85e24 | 2010-06-22 09:54:59 +0000 | [diff] [blame] | 4840 | const TemplateArgumentLocInfo &Arg, |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4841 | RecordDataImpl &Record) { |
Argyrios Kyrtzidis | ae85e24 | 2010-06-22 09:54:59 +0000 | [diff] [blame] | 4842 | switch (Kind) { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4843 | case TemplateArgument::Expression: |
Argyrios Kyrtzidis | ae85e24 | 2010-06-22 09:54:59 +0000 | [diff] [blame] | 4844 | AddStmt(Arg.getAsExpr()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4845 | break; |
| 4846 | case TemplateArgument::Type: |
Argyrios Kyrtzidis | ae85e24 | 2010-06-22 09:54:59 +0000 | [diff] [blame] | 4847 | AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4848 | break; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4849 | case TemplateArgument::Template: |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4850 | AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record); |
Argyrios Kyrtzidis | ddf5f21 | 2010-06-28 09:31:42 +0000 | [diff] [blame] | 4851 | AddSourceLocation(Arg.getTemplateNameLoc(), Record); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4852 | break; |
| 4853 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4854 | AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4855 | AddSourceLocation(Arg.getTemplateNameLoc(), Record); |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 4856 | AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4857 | break; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4858 | case TemplateArgument::Null: |
| 4859 | case TemplateArgument::Integral: |
| 4860 | case TemplateArgument::Declaration: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4861 | case TemplateArgument::NullPtr: |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4862 | case TemplateArgument::Pack: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4863 | // FIXME: Is this right? |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4864 | break; |
| 4865 | } |
| 4866 | } |
| 4867 | |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 4868 | void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg, |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4869 | RecordDataImpl &Record) { |
Argyrios Kyrtzidis | ae85e24 | 2010-06-22 09:54:59 +0000 | [diff] [blame] | 4870 | AddTemplateArgument(Arg.getArgument(), Record); |
Argyrios Kyrtzidis | ddf5f21 | 2010-06-28 09:31:42 +0000 | [diff] [blame] | 4871 | |
| 4872 | if (Arg.getArgument().getKind() == TemplateArgument::Expression) { |
| 4873 | bool InfoHasSameExpr |
| 4874 | = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr(); |
| 4875 | Record.push_back(InfoHasSameExpr); |
| 4876 | if (InfoHasSameExpr) |
| 4877 | return; // Avoid storing the same expr twice. |
| 4878 | } |
Argyrios Kyrtzidis | ae85e24 | 2010-06-22 09:54:59 +0000 | [diff] [blame] | 4879 | AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(), |
| 4880 | Record); |
| 4881 | } |
| 4882 | |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 4883 | void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, |
| 4884 | RecordDataImpl &Record) { |
Craig Topper | a13603a | 2014-05-22 05:54:18 +0000 | [diff] [blame] | 4885 | if (!TInfo) { |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 4886 | AddTypeRef(QualType(), Record); |
| 4887 | return; |
| 4888 | } |
| 4889 | |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 4890 | AddTypeLoc(TInfo->getTypeLoc(), Record); |
| 4891 | } |
| 4892 | |
| 4893 | void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) { |
| 4894 | AddTypeRef(TL.getType(), Record); |
| 4895 | |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 4896 | TypeLocWriter TLW(*this, Record); |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 4897 | for (; !TL.isNull(); TL = TL.getNextTypeLoc()) |
Kovarththanan Rajaratnam | a9c81a8 | 2010-03-14 07:06:50 +0000 | [diff] [blame] | 4898 | TLW.Visit(TL); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 4899 | } |
| 4900 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 4901 | void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) { |
Argyrios Kyrtzidis | 9ab44ea | 2010-08-20 16:04:14 +0000 | [diff] [blame] | 4902 | Record.push_back(GetOrCreateTypeID(T)); |
| 4903 | } |
| 4904 | |
Richard Smith | 2095ffe | 2015-03-16 20:11:03 +0000 | [diff] [blame] | 4905 | TypeID ASTWriter::GetOrCreateTypeID(QualType T) { |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 4906 | assert(Context); |
Richard Smith | 2095ffe | 2015-03-16 20:11:03 +0000 | [diff] [blame] | 4907 | return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx { |
| 4908 | if (T.isNull()) |
| 4909 | return TypeIdx(); |
| 4910 | assert(!T.getLocalFastQualifiers()); |
| 4911 | |
| 4912 | TypeIdx &Idx = TypeIdxs[T]; |
| 4913 | if (Idx.getIndex() == 0) { |
| 4914 | if (DoneWritingDeclsAndTypes) { |
| 4915 | assert(0 && "New type seen after serializing all the types to emit!"); |
| 4916 | return TypeIdx(); |
| 4917 | } |
| 4918 | |
| 4919 | // We haven't seen this type before. Assign it a new ID and put it |
| 4920 | // into the queue of types to emit. |
| 4921 | Idx = TypeIdx(NextTypeID++); |
| 4922 | DeclTypesToEmit.push(T); |
| 4923 | } |
| 4924 | return Idx; |
| 4925 | }); |
Argyrios Kyrtzidis | 082e461 | 2010-08-20 16:04:20 +0000 | [diff] [blame] | 4926 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 4927 | |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 4928 | TypeID ASTWriter::getTypeID(QualType T) const { |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 4929 | assert(Context); |
Richard Smith | 2095ffe | 2015-03-16 20:11:03 +0000 | [diff] [blame] | 4930 | return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx { |
| 4931 | if (T.isNull()) |
Argyrios Kyrtzidis | 442dd80 | 2012-07-02 19:19:01 +0000 | [diff] [blame] | 4932 | return TypeIdx(); |
Richard Smith | 2095ffe | 2015-03-16 20:11:03 +0000 | [diff] [blame] | 4933 | assert(!T.getLocalFastQualifiers()); |
Argyrios Kyrtzidis | 442dd80 | 2012-07-02 19:19:01 +0000 | [diff] [blame] | 4934 | |
Richard Smith | 2095ffe | 2015-03-16 20:11:03 +0000 | [diff] [blame] | 4935 | TypeIdxMap::const_iterator I = TypeIdxs.find(T); |
| 4936 | assert(I != TypeIdxs.end() && "Type not emitted!"); |
| 4937 | return I->second; |
| 4938 | }); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 4939 | } |
| 4940 | |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 4941 | void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) { |
Sebastian Redl | 66c5eef | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 4942 | Record.push_back(GetDeclRef(D)); |
| 4943 | } |
| 4944 | |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 4945 | DeclID ASTWriter::GetDeclRef(const Decl *D) { |
Douglas Gregor | 2fd3d40 | 2011-09-17 00:05:03 +0000 | [diff] [blame] | 4946 | assert(WritingAST && "Cannot request a declaration ID before AST writing"); |
Craig Topper | a13603a | 2014-05-22 05:54:18 +0000 | [diff] [blame] | 4947 | |
| 4948 | if (!D) { |
Sebastian Redl | 66c5eef | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 4949 | return 0; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 4950 | } |
Douglas Gregor | b3163e5 | 2012-01-05 22:33:30 +0000 | [diff] [blame] | 4951 | |
| 4952 | // If D comes from an AST file, its declaration ID is already known and |
| 4953 | // fixed. |
| 4954 | if (D->isFromASTFile()) |
| 4955 | return D->getGlobalID(); |
| 4956 | |
Douglas Gregor | 9b3932c | 2010-10-05 18:37:06 +0000 | [diff] [blame] | 4957 | assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer"); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 4958 | DeclID &ID = DeclIDs[D]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4959 | if (ID == 0) { |
Argyrios Kyrtzidis | 442dd80 | 2012-07-02 19:19:01 +0000 | [diff] [blame] | 4960 | if (DoneWritingDeclsAndTypes) { |
| 4961 | assert(0 && "New decl seen after serializing all the decls to emit!"); |
| 4962 | return 0; |
| 4963 | } |
| 4964 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 4965 | // We haven't seen this declaration before. Give it a new ID and |
| 4966 | // enqueue it in the list of declarations to emit. |
Sebastian Redl | ff4a295 | 2010-07-23 23:49:55 +0000 | [diff] [blame] | 4967 | ID = NextDeclID++; |
Douglas Gregor | 12bfa38 | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 4968 | DeclTypesToEmit.push(const_cast<Decl *>(D)); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 4969 | } |
| 4970 | |
Sebastian Redl | 66c5eef | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 4971 | return ID; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 4972 | } |
| 4973 | |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 4974 | DeclID ASTWriter::getDeclID(const Decl *D) { |
Craig Topper | a13603a | 2014-05-22 05:54:18 +0000 | [diff] [blame] | 4975 | if (!D) |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 4976 | return 0; |
| 4977 | |
Douglas Gregor | b3163e5 | 2012-01-05 22:33:30 +0000 | [diff] [blame] | 4978 | // If D comes from an AST file, its declaration ID is already known and |
| 4979 | // fixed. |
| 4980 | if (D->isFromASTFile()) |
| 4981 | return D->getGlobalID(); |
| 4982 | |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 4983 | assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!"); |
| 4984 | return DeclIDs[D]; |
| 4985 | } |
| 4986 | |
Argyrios Kyrtzidis | df53da8 | 2011-10-28 23:57:43 +0000 | [diff] [blame] | 4987 | void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) { |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 4988 | assert(ID); |
Argyrios Kyrtzidis | df53da8 | 2011-10-28 23:57:43 +0000 | [diff] [blame] | 4989 | assert(D); |
| 4990 | |
| 4991 | SourceLocation Loc = D->getLocation(); |
| 4992 | if (Loc.isInvalid()) |
| 4993 | return; |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 4994 | |
| 4995 | // We only keep track of the file-level declarations of each file. |
| 4996 | if (!D->getLexicalDeclContext()->isFileContext()) |
| 4997 | return; |
Argyrios Kyrtzidis | e1bc99e | 2012-02-24 19:45:46 +0000 | [diff] [blame] | 4998 | // FIXME: ParmVarDecls that are part of a function type of a parameter of |
| 4999 | // a function/objc method, should not have TU as lexical context. |
Argyrios Kyrtzidis | ffe055a8 | 2012-02-24 01:12:38 +0000 | [diff] [blame] | 5000 | if (isa<ParmVarDecl>(D)) |
| 5001 | return; |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 5002 | |
| 5003 | SourceManager &SM = Context->getSourceManager(); |
Argyrios Kyrtzidis | df53da8 | 2011-10-28 23:57:43 +0000 | [diff] [blame] | 5004 | SourceLocation FileLoc = SM.getFileLoc(Loc); |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 5005 | assert(SM.isLocalSourceLocation(FileLoc)); |
Argyrios Kyrtzidis | 7362e9b | 2011-10-28 23:57:47 +0000 | [diff] [blame] | 5006 | FileID FID; |
| 5007 | unsigned Offset; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 5008 | std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc); |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 5009 | if (FID.isInvalid()) |
| 5010 | return; |
Argyrios Kyrtzidis | 4db774a | 2012-10-02 21:09:17 +0000 | [diff] [blame] | 5011 | assert(SM.getSLocEntry(FID).isFile()); |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 5012 | |
Argyrios Kyrtzidis | 4db774a | 2012-10-02 21:09:17 +0000 | [diff] [blame] | 5013 | DeclIDInFileInfo *&Info = FileDeclIDs[FID]; |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 5014 | if (!Info) |
| 5015 | Info = new DeclIDInFileInfo(); |
| 5016 | |
Argyrios Kyrtzidis | 7362e9b | 2011-10-28 23:57:47 +0000 | [diff] [blame] | 5017 | std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID); |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 5018 | LocDeclIDsTy &Decls = Info->DeclIDs; |
| 5019 | |
Argyrios Kyrtzidis | 7362e9b | 2011-10-28 23:57:47 +0000 | [diff] [blame] | 5020 | if (Decls.empty() || Decls.back().first <= Offset) { |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 5021 | Decls.push_back(LocDecl); |
| 5022 | return; |
| 5023 | } |
| 5024 | |
Benjamin Kramer | 45025c0 | 2013-08-24 13:22:59 +0000 | [diff] [blame] | 5025 | LocDeclIDsTy::iterator I = |
| 5026 | std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first()); |
Argyrios Kyrtzidis | 5fc727a | 2011-10-28 22:54:21 +0000 | [diff] [blame] | 5027 | |
| 5028 | Decls.insert(I, LocDecl); |
| 5029 | } |
| 5030 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 5031 | void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) { |
Chris Lattner | 258172e | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 5032 | // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc. |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 5033 | Record.push_back(Name.getNameKind()); |
| 5034 | switch (Name.getNameKind()) { |
| 5035 | case DeclarationName::Identifier: |
| 5036 | AddIdentifierRef(Name.getAsIdentifierInfo(), Record); |
| 5037 | break; |
| 5038 | |
| 5039 | case DeclarationName::ObjCZeroArgSelector: |
| 5040 | case DeclarationName::ObjCOneArgSelector: |
| 5041 | case DeclarationName::ObjCMultiArgSelector: |
Steve Naroff | 2ddea05 | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 5042 | AddSelectorRef(Name.getObjCSelector(), Record); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 5043 | break; |
| 5044 | |
| 5045 | case DeclarationName::CXXConstructorName: |
| 5046 | case DeclarationName::CXXDestructorName: |
| 5047 | case DeclarationName::CXXConversionFunctionName: |
| 5048 | AddTypeRef(Name.getCXXNameType(), Record); |
| 5049 | break; |
| 5050 | |
| 5051 | case DeclarationName::CXXOperatorName: |
| 5052 | Record.push_back(Name.getCXXOverloadedOperator()); |
| 5053 | break; |
| 5054 | |
Alexis Hunt | 3d221f2 | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 5055 | case DeclarationName::CXXLiteralOperatorName: |
| 5056 | AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record); |
| 5057 | break; |
| 5058 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 5059 | case DeclarationName::CXXUsingDirective: |
| 5060 | // No extra data to emit |
| 5061 | break; |
| 5062 | } |
| 5063 | } |
Chris Lattner | ca025db | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 5064 | |
Richard Smith | d08aeb6 | 2014-08-28 01:33:39 +0000 | [diff] [blame] | 5065 | unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) { |
| 5066 | assert(needsAnonymousDeclarationNumber(D) && |
| 5067 | "expected an anonymous declaration"); |
| 5068 | |
| 5069 | // Number the anonymous declarations within this context, if we've not |
| 5070 | // already done so. |
| 5071 | auto It = AnonymousDeclarationNumbers.find(D); |
| 5072 | if (It == AnonymousDeclarationNumbers.end()) { |
Richard Smith | 2b56057 | 2015-02-07 03:11:11 +0000 | [diff] [blame] | 5073 | auto *DC = D->getLexicalDeclContext(); |
| 5074 | numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) { |
| 5075 | AnonymousDeclarationNumbers[ND] = Number; |
| 5076 | }); |
Richard Smith | d08aeb6 | 2014-08-28 01:33:39 +0000 | [diff] [blame] | 5077 | |
| 5078 | It = AnonymousDeclarationNumbers.find(D); |
| 5079 | assert(It != AnonymousDeclarationNumbers.end() && |
| 5080 | "declaration not found within its lexical context"); |
| 5081 | } |
| 5082 | |
| 5083 | return It->second; |
| 5084 | } |
| 5085 | |
Argyrios Kyrtzidis | 434383d | 2010-10-15 18:21:24 +0000 | [diff] [blame] | 5086 | void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc, |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 5087 | DeclarationName Name, RecordDataImpl &Record) { |
Argyrios Kyrtzidis | 434383d | 2010-10-15 18:21:24 +0000 | [diff] [blame] | 5088 | switch (Name.getNameKind()) { |
| 5089 | case DeclarationName::CXXConstructorName: |
| 5090 | case DeclarationName::CXXDestructorName: |
| 5091 | case DeclarationName::CXXConversionFunctionName: |
| 5092 | AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record); |
| 5093 | break; |
| 5094 | |
| 5095 | case DeclarationName::CXXOperatorName: |
| 5096 | AddSourceLocation( |
| 5097 | SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc), |
| 5098 | Record); |
| 5099 | AddSourceLocation( |
| 5100 | SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc), |
| 5101 | Record); |
| 5102 | break; |
| 5103 | |
| 5104 | case DeclarationName::CXXLiteralOperatorName: |
| 5105 | AddSourceLocation( |
| 5106 | SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc), |
| 5107 | Record); |
| 5108 | break; |
| 5109 | |
| 5110 | case DeclarationName::Identifier: |
| 5111 | case DeclarationName::ObjCZeroArgSelector: |
| 5112 | case DeclarationName::ObjCOneArgSelector: |
| 5113 | case DeclarationName::ObjCMultiArgSelector: |
| 5114 | case DeclarationName::CXXUsingDirective: |
| 5115 | break; |
| 5116 | } |
| 5117 | } |
| 5118 | |
| 5119 | void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo, |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 5120 | RecordDataImpl &Record) { |
Argyrios Kyrtzidis | 434383d | 2010-10-15 18:21:24 +0000 | [diff] [blame] | 5121 | AddDeclarationName(NameInfo.getName(), Record); |
| 5122 | AddSourceLocation(NameInfo.getLoc(), Record); |
| 5123 | AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record); |
| 5124 | } |
| 5125 | |
| 5126 | void ASTWriter::AddQualifierInfo(const QualifierInfo &Info, |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 5127 | RecordDataImpl &Record) { |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5128 | AddNestedNameSpecifierLoc(Info.QualifierLoc, Record); |
Argyrios Kyrtzidis | 434383d | 2010-10-15 18:21:24 +0000 | [diff] [blame] | 5129 | Record.push_back(Info.NumTemplParamLists); |
| 5130 | for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i) |
| 5131 | AddTemplateParameterList(Info.TemplParamLists[i], Record); |
| 5132 | } |
| 5133 | |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 5134 | void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS, |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 5135 | RecordDataImpl &Record) { |
Chris Lattner | ca025db | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 5136 | // Nested name specifiers usually aren't too long. I think that 8 would |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 5137 | // typically accommodate the vast majority. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5138 | SmallVector<NestedNameSpecifier *, 8> NestedNames; |
Chris Lattner | ca025db | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 5139 | |
| 5140 | // Push each of the NNS's onto a stack for serialization in reverse order. |
| 5141 | while (NNS) { |
| 5142 | NestedNames.push_back(NNS); |
| 5143 | NNS = NNS->getPrefix(); |
| 5144 | } |
| 5145 | |
| 5146 | Record.push_back(NestedNames.size()); |
| 5147 | while(!NestedNames.empty()) { |
| 5148 | NNS = NestedNames.pop_back_val(); |
| 5149 | NestedNameSpecifier::SpecifierKind Kind = NNS->getKind(); |
| 5150 | Record.push_back(Kind); |
| 5151 | switch (Kind) { |
| 5152 | case NestedNameSpecifier::Identifier: |
| 5153 | AddIdentifierRef(NNS->getAsIdentifier(), Record); |
| 5154 | break; |
| 5155 | |
| 5156 | case NestedNameSpecifier::Namespace: |
| 5157 | AddDeclRef(NNS->getAsNamespace(), Record); |
| 5158 | break; |
| 5159 | |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 5160 | case NestedNameSpecifier::NamespaceAlias: |
| 5161 | AddDeclRef(NNS->getAsNamespaceAlias(), Record); |
| 5162 | break; |
| 5163 | |
Chris Lattner | ca025db | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 5164 | case NestedNameSpecifier::TypeSpec: |
| 5165 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 5166 | AddTypeRef(QualType(NNS->getAsType(), 0), Record); |
| 5167 | Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate); |
| 5168 | break; |
| 5169 | |
| 5170 | case NestedNameSpecifier::Global: |
| 5171 | // Don't need to write an associated value. |
| 5172 | break; |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 5173 | |
| 5174 | case NestedNameSpecifier::Super: |
| 5175 | AddDeclRef(NNS->getAsRecordDecl(), Record); |
| 5176 | break; |
Chris Lattner | ca025db | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 5177 | } |
| 5178 | } |
| 5179 | } |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 5180 | |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5181 | void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS, |
| 5182 | RecordDataImpl &Record) { |
| 5183 | // Nested name specifiers usually aren't too long. I think that 8 would |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 5184 | // typically accommodate the vast majority. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5185 | SmallVector<NestedNameSpecifierLoc , 8> NestedNames; |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5186 | |
| 5187 | // Push each of the nested-name-specifiers's onto a stack for |
| 5188 | // serialization in reverse order. |
| 5189 | while (NNS) { |
| 5190 | NestedNames.push_back(NNS); |
| 5191 | NNS = NNS.getPrefix(); |
| 5192 | } |
| 5193 | |
| 5194 | Record.push_back(NestedNames.size()); |
| 5195 | while(!NestedNames.empty()) { |
| 5196 | NNS = NestedNames.pop_back_val(); |
| 5197 | NestedNameSpecifier::SpecifierKind Kind |
| 5198 | = NNS.getNestedNameSpecifier()->getKind(); |
| 5199 | Record.push_back(Kind); |
| 5200 | switch (Kind) { |
| 5201 | case NestedNameSpecifier::Identifier: |
| 5202 | AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record); |
| 5203 | AddSourceRange(NNS.getLocalSourceRange(), Record); |
| 5204 | break; |
| 5205 | |
| 5206 | case NestedNameSpecifier::Namespace: |
| 5207 | AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record); |
| 5208 | AddSourceRange(NNS.getLocalSourceRange(), Record); |
| 5209 | break; |
| 5210 | |
| 5211 | case NestedNameSpecifier::NamespaceAlias: |
| 5212 | AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record); |
| 5213 | AddSourceRange(NNS.getLocalSourceRange(), Record); |
| 5214 | break; |
| 5215 | |
| 5216 | case NestedNameSpecifier::TypeSpec: |
| 5217 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 5218 | Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate); |
| 5219 | AddTypeLoc(NNS.getTypeLoc(), Record); |
| 5220 | AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record); |
| 5221 | break; |
| 5222 | |
| 5223 | case NestedNameSpecifier::Global: |
| 5224 | AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record); |
| 5225 | break; |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 5226 | |
| 5227 | case NestedNameSpecifier::Super: |
| 5228 | AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl(), Record); |
| 5229 | AddSourceRange(NNS.getLocalSourceRange(), Record); |
| 5230 | break; |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5231 | } |
| 5232 | } |
| 5233 | } |
| 5234 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 5235 | void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) { |
Michael J. Spencer | 4c0ffa8 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 5236 | TemplateName::NameKind Kind = Name.getKind(); |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 5237 | Record.push_back(Kind); |
| 5238 | switch (Kind) { |
| 5239 | case TemplateName::Template: |
| 5240 | AddDeclRef(Name.getAsTemplateDecl(), Record); |
| 5241 | break; |
| 5242 | |
| 5243 | case TemplateName::OverloadedTemplate: { |
| 5244 | OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate(); |
| 5245 | Record.push_back(OvT->size()); |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 5246 | for (const auto &I : *OvT) |
| 5247 | AddDeclRef(I, Record); |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 5248 | break; |
| 5249 | } |
Michael J. Spencer | 4c0ffa8 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 5250 | |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 5251 | case TemplateName::QualifiedTemplate: { |
| 5252 | QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName(); |
| 5253 | AddNestedNameSpecifier(QualT->getQualifier(), Record); |
| 5254 | Record.push_back(QualT->hasTemplateKeyword()); |
| 5255 | AddDeclRef(QualT->getTemplateDecl(), Record); |
| 5256 | break; |
| 5257 | } |
Michael J. Spencer | 4c0ffa8 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 5258 | |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 5259 | case TemplateName::DependentTemplate: { |
| 5260 | DependentTemplateName *DepT = Name.getAsDependentTemplateName(); |
| 5261 | AddNestedNameSpecifier(DepT->getQualifier(), Record); |
| 5262 | Record.push_back(DepT->isIdentifier()); |
| 5263 | if (DepT->isIdentifier()) |
| 5264 | AddIdentifierRef(DepT->getIdentifier(), Record); |
| 5265 | else |
| 5266 | Record.push_back(DepT->getOperator()); |
| 5267 | break; |
| 5268 | } |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5269 | |
| 5270 | case TemplateName::SubstTemplateTemplateParm: { |
| 5271 | SubstTemplateTemplateParmStorage *subst |
| 5272 | = Name.getAsSubstTemplateTemplateParm(); |
| 5273 | AddDeclRef(subst->getParameter(), Record); |
| 5274 | AddTemplateName(subst->getReplacement(), Record); |
| 5275 | break; |
| 5276 | } |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 5277 | |
| 5278 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 5279 | SubstTemplateTemplateParmPackStorage *SubstPack |
| 5280 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 5281 | AddDeclRef(SubstPack->getParameterPack(), Record); |
| 5282 | AddTemplateArgument(SubstPack->getArgumentPack(), Record); |
| 5283 | break; |
| 5284 | } |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 5285 | } |
| 5286 | } |
| 5287 | |
Michael J. Spencer | 4c0ffa8 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 5288 | void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg, |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 5289 | RecordDataImpl &Record) { |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 5290 | Record.push_back(Arg.getKind()); |
| 5291 | switch (Arg.getKind()) { |
| 5292 | case TemplateArgument::Null: |
| 5293 | break; |
| 5294 | case TemplateArgument::Type: |
| 5295 | AddTypeRef(Arg.getAsType(), Record); |
| 5296 | break; |
| 5297 | case TemplateArgument::Declaration: |
| 5298 | AddDeclRef(Arg.getAsDecl(), Record); |
David Blaikie | 952a9b1 | 2014-10-17 18:00:12 +0000 | [diff] [blame] | 5299 | AddTypeRef(Arg.getParamTypeForDecl(), Record); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5300 | break; |
| 5301 | case TemplateArgument::NullPtr: |
| 5302 | AddTypeRef(Arg.getNullPtrType(), Record); |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 5303 | break; |
| 5304 | case TemplateArgument::Integral: |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 5305 | AddAPSInt(Arg.getAsIntegral(), Record); |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 5306 | AddTypeRef(Arg.getIntegralType(), Record); |
| 5307 | break; |
| 5308 | case TemplateArgument::Template: |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 5309 | AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record); |
| 5310 | break; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 5311 | case TemplateArgument::TemplateExpansion: |
| 5312 | AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record); |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 5313 | if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions()) |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 5314 | Record.push_back(*NumExpansions + 1); |
| 5315 | else |
| 5316 | Record.push_back(0); |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 5317 | break; |
| 5318 | case TemplateArgument::Expression: |
| 5319 | AddStmt(Arg.getAsExpr()); |
| 5320 | break; |
| 5321 | case TemplateArgument::Pack: |
| 5322 | Record.push_back(Arg.pack_size()); |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 5323 | for (const auto &P : Arg.pack_elements()) |
| 5324 | AddTemplateArgument(P, Record); |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 5325 | break; |
| 5326 | } |
| 5327 | } |
Argyrios Kyrtzidis | 818c5db | 2010-06-23 13:48:30 +0000 | [diff] [blame] | 5328 | |
| 5329 | void |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 5330 | ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams, |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 5331 | RecordDataImpl &Record) { |
Argyrios Kyrtzidis | 818c5db | 2010-06-23 13:48:30 +0000 | [diff] [blame] | 5332 | assert(TemplateParams && "No TemplateParams!"); |
| 5333 | AddSourceLocation(TemplateParams->getTemplateLoc(), Record); |
| 5334 | AddSourceLocation(TemplateParams->getLAngleLoc(), Record); |
| 5335 | AddSourceLocation(TemplateParams->getRAngleLoc(), Record); |
| 5336 | Record.push_back(TemplateParams->size()); |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 5337 | for (const auto &P : *TemplateParams) |
| 5338 | AddDeclRef(P, Record); |
Argyrios Kyrtzidis | 818c5db | 2010-06-23 13:48:30 +0000 | [diff] [blame] | 5339 | } |
| 5340 | |
| 5341 | /// \brief Emit a template argument list. |
| 5342 | void |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 5343 | ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs, |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 5344 | RecordDataImpl &Record) { |
Argyrios Kyrtzidis | 818c5db | 2010-06-23 13:48:30 +0000 | [diff] [blame] | 5345 | assert(TemplateArgs && "No TemplateArgs!"); |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5346 | Record.push_back(TemplateArgs->size()); |
| 5347 | for (int i=0, e = TemplateArgs->size(); i != e; ++i) |
Argyrios Kyrtzidis | 818c5db | 2010-06-23 13:48:30 +0000 | [diff] [blame] | 5348 | AddTemplateArgument(TemplateArgs->get(i), Record); |
| 5349 | } |
Argyrios Kyrtzidis | 2c2167a | 2010-07-02 11:55:32 +0000 | [diff] [blame] | 5350 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 5351 | void |
| 5352 | ASTWriter::AddASTTemplateArgumentListInfo |
| 5353 | (const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) { |
| 5354 | assert(ASTTemplArgList && "No ASTTemplArgList!"); |
| 5355 | AddSourceLocation(ASTTemplArgList->LAngleLoc, Record); |
| 5356 | AddSourceLocation(ASTTemplArgList->RAngleLoc, Record); |
| 5357 | Record.push_back(ASTTemplArgList->NumTemplateArgs); |
| 5358 | const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs(); |
| 5359 | for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i) |
| 5360 | AddTemplateArgumentLoc(TemplArgs[i], Record); |
| 5361 | } |
Argyrios Kyrtzidis | 2c2167a | 2010-07-02 11:55:32 +0000 | [diff] [blame] | 5362 | |
| 5363 | void |
Argyrios Kyrtzidis | 0f05fb9 | 2012-11-28 03:56:16 +0000 | [diff] [blame] | 5364 | ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) { |
Argyrios Kyrtzidis | 2c2167a | 2010-07-02 11:55:32 +0000 | [diff] [blame] | 5365 | Record.push_back(Set.size()); |
Argyrios Kyrtzidis | 0f05fb9 | 2012-11-28 03:56:16 +0000 | [diff] [blame] | 5366 | for (ASTUnresolvedSet::const_iterator |
Argyrios Kyrtzidis | 2c2167a | 2010-07-02 11:55:32 +0000 | [diff] [blame] | 5367 | I = Set.begin(), E = Set.end(); I != E; ++I) { |
| 5368 | AddDeclRef(I.getDecl(), Record); |
| 5369 | Record.push_back(I.getAccess()); |
| 5370 | } |
| 5371 | } |
Argyrios Kyrtzidis | 3701fcd | 2010-07-02 23:30:27 +0000 | [diff] [blame] | 5372 | |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 5373 | void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base, |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 5374 | RecordDataImpl &Record) { |
Argyrios Kyrtzidis | 3701fcd | 2010-07-02 23:30:27 +0000 | [diff] [blame] | 5375 | Record.push_back(Base.isVirtual()); |
| 5376 | Record.push_back(Base.isBaseOfClass()); |
| 5377 | Record.push_back(Base.getAccessSpecifierAsWritten()); |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 5378 | Record.push_back(Base.getInheritConstructors()); |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 5379 | AddTypeSourceInfo(Base.getTypeSourceInfo(), Record); |
Argyrios Kyrtzidis | 3701fcd | 2010-07-02 23:30:27 +0000 | [diff] [blame] | 5380 | AddSourceRange(Base.getSourceRange(), Record); |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 5381 | AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc() |
| 5382 | : SourceLocation(), |
| 5383 | Record); |
Argyrios Kyrtzidis | 3701fcd | 2010-07-02 23:30:27 +0000 | [diff] [blame] | 5384 | } |
Sebastian Redl | 85b2a6a | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 5385 | |
Douglas Gregor | d4c5ed0 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 5386 | void ASTWriter::FlushCXXBaseSpecifiers() { |
| 5387 | RecordData Record; |
Richard Smith | c2bb818 | 2015-03-24 06:36:48 +0000 | [diff] [blame] | 5388 | unsigned N = CXXBaseSpecifiersToWrite.size(); |
| 5389 | for (unsigned I = 0; I != N; ++I) { |
Douglas Gregor | d4c5ed0 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 5390 | Record.clear(); |
| 5391 | |
| 5392 | // Record the offset of this base-specifier set. |
Douglas Gregor | c27b287 | 2011-08-04 00:01:48 +0000 | [diff] [blame] | 5393 | unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1; |
Douglas Gregor | d4c5ed0 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 5394 | if (Index == CXXBaseSpecifiersOffsets.size()) |
| 5395 | CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo()); |
| 5396 | else { |
| 5397 | if (Index > CXXBaseSpecifiersOffsets.size()) |
| 5398 | CXXBaseSpecifiersOffsets.resize(Index + 1); |
| 5399 | CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo(); |
| 5400 | } |
| 5401 | |
| 5402 | const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases, |
| 5403 | *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd; |
| 5404 | Record.push_back(BEnd - B); |
| 5405 | for (; B != BEnd; ++B) |
| 5406 | AddCXXBaseSpecifier(*B, Record); |
| 5407 | Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record); |
Douglas Gregor | d585304 | 2010-10-30 04:28:16 +0000 | [diff] [blame] | 5408 | |
| 5409 | // Flush any expressions that were written as part of the base specifiers. |
| 5410 | FlushStmts(); |
Douglas Gregor | d4c5ed0 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 5411 | } |
| 5412 | |
Richard Smith | c2bb818 | 2015-03-24 06:36:48 +0000 | [diff] [blame] | 5413 | assert(N == CXXBaseSpecifiersToWrite.size() && |
| 5414 | "added more base specifiers while writing base specifiers"); |
Douglas Gregor | d4c5ed0 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 5415 | CXXBaseSpecifiersToWrite.clear(); |
| 5416 | } |
| 5417 | |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 5418 | void ASTWriter::AddCXXCtorInitializers( |
| 5419 | const CXXCtorInitializer * const *CtorInitializers, |
| 5420 | unsigned NumCtorInitializers, |
| 5421 | RecordDataImpl &Record) { |
| 5422 | Record.push_back(NumCtorInitializers); |
| 5423 | for (unsigned i=0; i != NumCtorInitializers; ++i) { |
| 5424 | const CXXCtorInitializer *Init = CtorInitializers[i]; |
Argyrios Kyrtzidis | 5b6a03f | 2010-08-09 10:54:12 +0000 | [diff] [blame] | 5425 | |
Argyrios Kyrtzidis | 5b6a03f | 2010-08-09 10:54:12 +0000 | [diff] [blame] | 5426 | if (Init->isBaseInitializer()) { |
Alexis Hunt | 37a477f | 2011-05-04 01:19:08 +0000 | [diff] [blame] | 5427 | Record.push_back(CTOR_INITIALIZER_BASE); |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 5428 | AddTypeSourceInfo(Init->getTypeSourceInfo(), Record); |
Argyrios Kyrtzidis | 5b6a03f | 2010-08-09 10:54:12 +0000 | [diff] [blame] | 5429 | Record.push_back(Init->isBaseVirtual()); |
Alexis Hunt | 37a477f | 2011-05-04 01:19:08 +0000 | [diff] [blame] | 5430 | } else if (Init->isDelegatingInitializer()) { |
| 5431 | Record.push_back(CTOR_INITIALIZER_DELEGATING); |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 5432 | AddTypeSourceInfo(Init->getTypeSourceInfo(), Record); |
Alexis Hunt | 37a477f | 2011-05-04 01:19:08 +0000 | [diff] [blame] | 5433 | } else if (Init->isMemberInitializer()){ |
| 5434 | Record.push_back(CTOR_INITIALIZER_MEMBER); |
| 5435 | AddDeclRef(Init->getMember(), Record); |
Argyrios Kyrtzidis | 5b6a03f | 2010-08-09 10:54:12 +0000 | [diff] [blame] | 5436 | } else { |
Alexis Hunt | 37a477f | 2011-05-04 01:19:08 +0000 | [diff] [blame] | 5437 | Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER); |
| 5438 | AddDeclRef(Init->getIndirectMember(), Record); |
Argyrios Kyrtzidis | 5b6a03f | 2010-08-09 10:54:12 +0000 | [diff] [blame] | 5439 | } |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 5440 | |
Argyrios Kyrtzidis | 5b6a03f | 2010-08-09 10:54:12 +0000 | [diff] [blame] | 5441 | AddSourceLocation(Init->getMemberLocation(), Record); |
| 5442 | AddStmt(Init->getInit()); |
Argyrios Kyrtzidis | 5b6a03f | 2010-08-09 10:54:12 +0000 | [diff] [blame] | 5443 | AddSourceLocation(Init->getLParenLoc(), Record); |
| 5444 | AddSourceLocation(Init->getRParenLoc(), Record); |
| 5445 | Record.push_back(Init->isWritten()); |
| 5446 | if (Init->isWritten()) { |
| 5447 | Record.push_back(Init->getSourceOrder()); |
| 5448 | } else { |
| 5449 | Record.push_back(Init->getNumArrayIndices()); |
| 5450 | for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i) |
| 5451 | AddDeclRef(Init->getArrayIndex(i), Record); |
| 5452 | } |
| 5453 | } |
| 5454 | } |
| 5455 | |
Richard Smith | c2bb818 | 2015-03-24 06:36:48 +0000 | [diff] [blame] | 5456 | void ASTWriter::FlushCXXCtorInitializers() { |
| 5457 | RecordData Record; |
| 5458 | |
| 5459 | unsigned N = CXXCtorInitializersToWrite.size(); |
Daniel Jasper | c77b0a1 | 2015-03-24 08:06:38 +0000 | [diff] [blame] | 5460 | (void)N; // Silence unused warning in non-assert builds. |
Richard Smith | c2bb818 | 2015-03-24 06:36:48 +0000 | [diff] [blame] | 5461 | for (auto &Init : CXXCtorInitializersToWrite) { |
| 5462 | Record.clear(); |
| 5463 | |
| 5464 | // Record the offset of this mem-initializer list. |
| 5465 | unsigned Index = Init.ID - 1; |
| 5466 | if (Index == CXXCtorInitializersOffsets.size()) |
| 5467 | CXXCtorInitializersOffsets.push_back(Stream.GetCurrentBitNo()); |
| 5468 | else { |
| 5469 | if (Index > CXXCtorInitializersOffsets.size()) |
| 5470 | CXXCtorInitializersOffsets.resize(Index + 1); |
| 5471 | CXXCtorInitializersOffsets[Index] = Stream.GetCurrentBitNo(); |
| 5472 | } |
| 5473 | |
| 5474 | AddCXXCtorInitializers(Init.Inits.data(), Init.Inits.size(), Record); |
| 5475 | Stream.EmitRecord(serialization::DECL_CXX_CTOR_INITIALIZERS, Record); |
| 5476 | |
| 5477 | // Flush any expressions that were written as part of the initializers. |
| 5478 | FlushStmts(); |
| 5479 | } |
| 5480 | |
| 5481 | assert(N == CXXCtorInitializersToWrite.size() && |
| 5482 | "added more ctor initializers while writing ctor initializers"); |
| 5483 | CXXCtorInitializersToWrite.clear(); |
| 5484 | } |
| 5485 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 5486 | void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 5487 | auto &Data = D->data(); |
Douglas Gregor | 99ae806 | 2012-02-14 17:54:36 +0000 | [diff] [blame] | 5488 | Record.push_back(Data.IsLambda); |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 5489 | Record.push_back(Data.UserDeclaredConstructor); |
Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 5490 | Record.push_back(Data.UserDeclaredSpecialMembers); |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 5491 | Record.push_back(Data.Aggregate); |
| 5492 | Record.push_back(Data.PlainOldData); |
| 5493 | Record.push_back(Data.Empty); |
| 5494 | Record.push_back(Data.Polymorphic); |
| 5495 | Record.push_back(Data.Abstract); |
Chandler Carruth | 583edf8 | 2011-04-30 10:07:30 +0000 | [diff] [blame] | 5496 | Record.push_back(Data.IsStandardLayout); |
Chandler Carruth | b196374 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 5497 | Record.push_back(Data.HasNoNonEmptyBases); |
| 5498 | Record.push_back(Data.HasPrivateFields); |
| 5499 | Record.push_back(Data.HasProtectedFields); |
| 5500 | Record.push_back(Data.HasPublicFields); |
Douglas Gregor | 61226d3 | 2011-05-13 01:05:07 +0000 | [diff] [blame] | 5501 | Record.push_back(Data.HasMutableFields); |
Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 5502 | Record.push_back(Data.HasVariantMembers); |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 5503 | Record.push_back(Data.HasOnlyCMembers); |
Richard Smith | e2648ba | 2012-05-07 01:07:30 +0000 | [diff] [blame] | 5504 | Record.push_back(Data.HasInClassInitializer); |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 5505 | Record.push_back(Data.HasUninitializedReferenceMember); |
Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5506 | Record.push_back(Data.NeedOverloadResolutionForMoveConstructor); |
| 5507 | Record.push_back(Data.NeedOverloadResolutionForMoveAssignment); |
| 5508 | Record.push_back(Data.NeedOverloadResolutionForDestructor); |
| 5509 | Record.push_back(Data.DefaultedMoveConstructorIsDeleted); |
| 5510 | Record.push_back(Data.DefaultedMoveAssignmentIsDeleted); |
| 5511 | Record.push_back(Data.DefaultedDestructorIsDeleted); |
Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 5512 | Record.push_back(Data.HasTrivialSpecialMembers); |
Richard Smith | b45a6f7 | 2014-04-17 20:33:01 +0000 | [diff] [blame] | 5513 | Record.push_back(Data.DeclaredNonTrivialSpecialMembers); |
Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 5514 | Record.push_back(Data.HasIrrelevantDestructor); |
Richard Smith | 111af8d | 2011-08-10 18:11:37 +0000 | [diff] [blame] | 5515 | Record.push_back(Data.HasConstexprNonCopyMoveConstructor); |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 5516 | Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr); |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 5517 | Record.push_back(Data.HasConstexprDefaultConstructor); |
Chandler Carruth | e71d062 | 2011-04-24 02:49:34 +0000 | [diff] [blame] | 5518 | Record.push_back(Data.HasNonLiteralTypeFieldsOrBases); |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 5519 | Record.push_back(Data.ComputedVisibleConversions); |
Alexis Hunt | ea6f032 | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5520 | Record.push_back(Data.UserProvidedDefaultConstructor); |
Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 5521 | Record.push_back(Data.DeclaredSpecialMembers); |
Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 5522 | Record.push_back(Data.ImplicitCopyConstructorHasConstParam); |
| 5523 | Record.push_back(Data.ImplicitCopyAssignmentHasConstParam); |
| 5524 | Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam); |
| 5525 | Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam); |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 5526 | // IsLambda bit is already saved. |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 5527 | |
| 5528 | Record.push_back(Data.NumBases); |
Douglas Gregor | d4c5ed0 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 5529 | if (Data.NumBases > 0) |
| 5530 | AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases, |
| 5531 | Record); |
| 5532 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 5533 | // FIXME: Make VBases lazily computed when needed to avoid storing them. |
| 5534 | Record.push_back(Data.NumVBases); |
Douglas Gregor | d4c5ed0 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 5535 | if (Data.NumVBases > 0) |
| 5536 | AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases, |
| 5537 | Record); |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 5538 | |
Richard Smith | a4ba74c | 2013-08-30 04:46:40 +0000 | [diff] [blame] | 5539 | AddUnresolvedSet(Data.Conversions.get(*Context), Record); |
| 5540 | AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record); |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 5541 | // Data.Definition is the owning decl, no need to write it. |
Richard Smith | 68ad0e7 | 2013-06-26 02:41:25 +0000 | [diff] [blame] | 5542 | AddDeclRef(D->getFirstFriend(), Record); |
Douglas Gregor | 99ae806 | 2012-02-14 17:54:36 +0000 | [diff] [blame] | 5543 | |
| 5544 | // Add lambda-specific data. |
| 5545 | if (Data.IsLambda) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 5546 | auto &Lambda = D->getLambdaData(); |
Douglas Gregor | 680e9e0 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 5547 | Record.push_back(Lambda.Dependent); |
Faisal Vali | c1a6dc4 | 2013-10-23 16:10:50 +0000 | [diff] [blame] | 5548 | Record.push_back(Lambda.IsGenericLambda); |
| 5549 | Record.push_back(Lambda.CaptureDefault); |
Douglas Gregor | 99ae806 | 2012-02-14 17:54:36 +0000 | [diff] [blame] | 5550 | Record.push_back(Lambda.NumCaptures); |
| 5551 | Record.push_back(Lambda.NumExplicitCaptures); |
Douglas Gregor | 6379854 | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 5552 | Record.push_back(Lambda.ManglingNumber); |
Douglas Gregor | 7fcbd90 | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 5553 | AddDeclRef(Lambda.ContextDecl, Record); |
Eli Friedman | d564afb | 2012-09-19 01:18:11 +0000 | [diff] [blame] | 5554 | AddTypeSourceInfo(Lambda.MethodTyInfo, Record); |
Douglas Gregor | 99ae806 | 2012-02-14 17:54:36 +0000 | [diff] [blame] | 5555 | for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) { |
Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 5556 | const LambdaCapture &Capture = Lambda.Captures[I]; |
Douglas Gregor | 99ae806 | 2012-02-14 17:54:36 +0000 | [diff] [blame] | 5557 | AddSourceLocation(Capture.getLocation(), Record); |
| 5558 | Record.push_back(Capture.isImplicit()); |
Richard Smith | ba71c08 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 5559 | Record.push_back(Capture.getCaptureKind()); |
| 5560 | switch (Capture.getCaptureKind()) { |
| 5561 | case LCK_This: |
Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 5562 | case LCK_VLAType: |
Richard Smith | ba71c08 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 5563 | break; |
| 5564 | case LCK_ByCopy: |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 5565 | case LCK_ByRef: |
Richard Smith | ba71c08 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 5566 | VarDecl *Var = |
Craig Topper | a13603a | 2014-05-22 05:54:18 +0000 | [diff] [blame] | 5567 | Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr; |
Richard Smith | ba71c08 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 5568 | AddDeclRef(Var, Record); |
| 5569 | AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc() |
| 5570 | : SourceLocation(), |
| 5571 | Record); |
| 5572 | break; |
| 5573 | } |
Douglas Gregor | 99ae806 | 2012-02-14 17:54:36 +0000 | [diff] [blame] | 5574 | } |
| 5575 | } |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 5576 | } |
| 5577 | |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 5578 | void ASTWriter::ReaderInitialized(ASTReader *Reader) { |
Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 5579 | assert(Reader && "Cannot remove chain"); |
Douglas Gregor | df0c151 | 2011-08-18 04:12:04 +0000 | [diff] [blame] | 5580 | assert((!Chain || Chain == Reader) && "Cannot replace chain"); |
Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 5581 | assert(FirstDeclID == NextDeclID && |
| 5582 | FirstTypeID == NextTypeID && |
| 5583 | FirstIdentID == NextIdentID && |
Douglas Gregor | cb28f9d | 2012-10-09 23:05:51 +0000 | [diff] [blame] | 5584 | FirstMacroID == NextMacroID && |
Douglas Gregor | 253eefe | 2011-12-01 00:59:36 +0000 | [diff] [blame] | 5585 | FirstSubmoduleID == NextSubmoduleID && |
Sebastian Redl | d95a56e | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 5586 | FirstSelectorID == NextSelectorID && |
Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 5587 | "Setting chain after writing has started."); |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 5588 | |
Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 5589 | Chain = Reader; |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 5590 | |
Richard Smith | 7f330cd | 2015-03-18 01:42:29 +0000 | [diff] [blame] | 5591 | // Note, this will get called multiple times, once one the reader starts up |
| 5592 | // and again each time it's done reading a PCH or module. |
Douglas Gregor | df0c151 | 2011-08-18 04:12:04 +0000 | [diff] [blame] | 5593 | FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls(); |
| 5594 | FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes(); |
| 5595 | FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers(); |
Douglas Gregor | cb28f9d | 2012-10-09 23:05:51 +0000 | [diff] [blame] | 5596 | FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros(); |
Douglas Gregor | 253eefe | 2011-12-01 00:59:36 +0000 | [diff] [blame] | 5597 | FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules(); |
Douglas Gregor | df0c151 | 2011-08-18 04:12:04 +0000 | [diff] [blame] | 5598 | FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors(); |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 5599 | NextDeclID = FirstDeclID; |
| 5600 | NextTypeID = FirstTypeID; |
| 5601 | NextIdentID = FirstIdentID; |
Douglas Gregor | cb28f9d | 2012-10-09 23:05:51 +0000 | [diff] [blame] | 5602 | NextMacroID = FirstMacroID; |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 5603 | NextSelectorID = FirstSelectorID; |
Douglas Gregor | 253eefe | 2011-12-01 00:59:36 +0000 | [diff] [blame] | 5604 | NextSubmoduleID = FirstSubmoduleID; |
Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 5605 | } |
| 5606 | |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 5607 | void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) { |
Douglas Gregor | 8d7edce | 2013-02-08 21:30:59 +0000 | [diff] [blame] | 5608 | // Always keep the highest ID. See \p TypeRead() for more information. |
| 5609 | IdentID &StoredID = IdentifierIDs[II]; |
| 5610 | if (ID > StoredID) |
| 5611 | StoredID = ID; |
Sebastian Redl | ff4a295 | 2010-07-23 23:49:55 +0000 | [diff] [blame] | 5612 | } |
| 5613 | |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 5614 | void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) { |
Douglas Gregor | 8d7edce | 2013-02-08 21:30:59 +0000 | [diff] [blame] | 5615 | // Always keep the highest ID. See \p TypeRead() for more information. |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 5616 | MacroID &StoredID = MacroIDs[MI]; |
Douglas Gregor | 8d7edce | 2013-02-08 21:30:59 +0000 | [diff] [blame] | 5617 | if (ID > StoredID) |
| 5618 | StoredID = ID; |
Douglas Gregor | cb28f9d | 2012-10-09 23:05:51 +0000 | [diff] [blame] | 5619 | } |
| 5620 | |
Argyrios Kyrtzidis | bb5c7eae | 2010-08-20 16:03:59 +0000 | [diff] [blame] | 5621 | void ASTWriter::TypeRead(TypeIdx Idx, QualType T) { |
Douglas Gregor | 9b3932c | 2010-10-05 18:37:06 +0000 | [diff] [blame] | 5622 | // Always take the highest-numbered type index. This copes with an interesting |
| 5623 | // case for chained AST writing where we schedule writing the type and then, |
Michael J. Spencer | 4c0ffa8 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 5624 | // later, deserialize the type from another AST. In this case, we want to |
Douglas Gregor | 9b3932c | 2010-10-05 18:37:06 +0000 | [diff] [blame] | 5625 | // keep the higher-numbered entry so that we can properly write it out to |
| 5626 | // the AST file. |
| 5627 | TypeIdx &StoredIdx = TypeIdxs[T]; |
| 5628 | if (Idx.getIndex() >= StoredIdx.getIndex()) |
| 5629 | StoredIdx = Idx; |
Sebastian Redl | 85b2a6a | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 5630 | } |
| 5631 | |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 5632 | void ASTWriter::SelectorRead(SelectorID ID, Selector S) { |
Douglas Gregor | 8d7edce | 2013-02-08 21:30:59 +0000 | [diff] [blame] | 5633 | // Always keep the highest ID. See \p TypeRead() for more information. |
| 5634 | SelectorID &StoredID = SelectorIDs[S]; |
| 5635 | if (ID > StoredID) |
| 5636 | StoredID = ID; |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 5637 | } |
Douglas Gregor | 9109629 | 2010-10-02 19:29:26 +0000 | [diff] [blame] | 5638 | |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 5639 | void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID, |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 5640 | MacroDefinitionRecord *MD) { |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 5641 | assert(MacroDefinitions.find(MD) == MacroDefinitions.end()); |
Douglas Gregor | 9109629 | 2010-10-02 19:29:26 +0000 | [diff] [blame] | 5642 | MacroDefinitions[MD] = ID; |
| 5643 | } |
Argyrios Kyrtzidis | d170d84 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 5644 | |
Douglas Gregor | e37a85a | 2011-12-02 17:30:13 +0000 | [diff] [blame] | 5645 | void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) { |
| 5646 | assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end()); |
| 5647 | SubmoduleIDs[Mod] = ID; |
| 5648 | } |
| 5649 | |
Argyrios Kyrtzidis | d170d84 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 5650 | void ASTWriter::CompletedTagDefinition(const TagDecl *D) { |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 5651 | assert(D->isCompleteDefinition()); |
Douglas Gregor | 2fd3d40 | 2011-09-17 00:05:03 +0000 | [diff] [blame] | 5652 | assert(!WritingAST && "Already writing the AST!"); |
Eugene Zelenko | ddaa4b4 | 2015-12-08 18:00:11 +0000 | [diff] [blame] | 5653 | if (auto *RD = dyn_cast<CXXRecordDecl>(D)) { |
Argyrios Kyrtzidis | d170d84 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 5654 | // We are interested when a PCH decl is modified. |
Douglas Gregor | b3722e2 | 2011-09-09 23:01:35 +0000 | [diff] [blame] | 5655 | if (RD->isFromASTFile()) { |
Argyrios Kyrtzidis | d170d84 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 5656 | // A forward reference was mutated into a definition. Rewrite it. |
| 5657 | // FIXME: This happens during template instantiation, should we |
| 5658 | // have created a new definition decl instead ? |
Richard Smith | cd45dbc | 2014-04-19 03:48:30 +0000 | [diff] [blame] | 5659 | assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) && |
| 5660 | "completed a tag from another module but not by instantiation?"); |
| 5661 | DeclUpdates[RD].push_back( |
| 5662 | DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION)); |
Argyrios Kyrtzidis | d170d84 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 5663 | } |
Argyrios Kyrtzidis | d170d84 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 5664 | } |
| 5665 | } |
Douglas Gregor | cb28f9d | 2012-10-09 23:05:51 +0000 | [diff] [blame] | 5666 | |
Richard Smith | f983f7f | 2015-10-13 00:23:25 +0000 | [diff] [blame] | 5667 | static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) { |
| 5668 | if (D->isFromASTFile()) |
| 5669 | return true; |
| 5670 | |
| 5671 | // If we've not loaded any modules, this can't be imported. |
| 5672 | if (!Chain || !Chain->getModuleManager().size()) |
| 5673 | return false; |
| 5674 | |
| 5675 | // The predefined __va_list_tag struct is imported if we imported any decls. |
| 5676 | // FIXME: This is a gross hack. |
| 5677 | return D == D->getASTContext().getVaListTagDecl(); |
| 5678 | } |
| 5679 | |
Argyrios Kyrtzidis | 01c2df4 | 2010-10-28 07:38:51 +0000 | [diff] [blame] | 5680 | void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) { |
| 5681 | // TU and namespaces are handled elsewhere. |
| 5682 | if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC)) |
| 5683 | return; |
| 5684 | |
Richard Smith | f983f7f | 2015-10-13 00:23:25 +0000 | [diff] [blame] | 5685 | // We're only interested in cases where a local declaration is added to an |
| 5686 | // imported context. |
| 5687 | if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC))) |
| 5688 | return; |
Argyrios Kyrtzidis | 01c2df4 | 2010-10-28 07:38:51 +0000 | [diff] [blame] | 5689 | |
Richard Smith | 0f4e2c4 | 2015-08-06 04:23:48 +0000 | [diff] [blame] | 5690 | assert(DC == DC->getPrimaryContext() && "added to non-primary context"); |
Douglas Gregor | 9f78289 | 2013-01-21 15:25:38 +0000 | [diff] [blame] | 5691 | assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!"); |
Richard Smith | e9a8bc3 | 2014-09-30 00:45:29 +0000 | [diff] [blame] | 5692 | assert(!WritingAST && "Already writing the AST!"); |
Richard Smith | f983f7f | 2015-10-13 00:23:25 +0000 | [diff] [blame] | 5693 | if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) { |
| 5694 | // We're adding a visible declaration to a predefined decl context. Ensure |
| 5695 | // that we write out all of its lookup results so we don't get a nasty |
| 5696 | // surprise when we try to emit its lookup table. |
| 5697 | for (auto *Child : DC->decls()) |
| 5698 | UpdatingVisibleDecls.push_back(Child); |
| 5699 | } |
Argyrios Kyrtzidis | 442dd80 | 2012-07-02 19:19:01 +0000 | [diff] [blame] | 5700 | UpdatingVisibleDecls.push_back(D); |
Argyrios Kyrtzidis | 01c2df4 | 2010-10-28 07:38:51 +0000 | [diff] [blame] | 5701 | } |
Argyrios Kyrtzidis | e16a530 | 2010-10-24 17:26:54 +0000 | [diff] [blame] | 5702 | |
| 5703 | void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) { |
| 5704 | assert(D->isImplicit()); |
Richard Smith | f983f7f | 2015-10-13 00:23:25 +0000 | [diff] [blame] | 5705 | |
| 5706 | // We're only interested in cases where a local declaration is added to an |
| 5707 | // imported context. |
| 5708 | if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD)) |
| 5709 | return; |
| 5710 | |
Argyrios Kyrtzidis | e16a530 | 2010-10-24 17:26:54 +0000 | [diff] [blame] | 5711 | if (!isa<CXXMethodDecl>(D)) |
Richard Smith | f983f7f | 2015-10-13 00:23:25 +0000 | [diff] [blame] | 5712 | return; |
Argyrios Kyrtzidis | e16a530 | 2010-10-24 17:26:54 +0000 | [diff] [blame] | 5713 | |
| 5714 | // A decl coming from PCH was modified. |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 5715 | assert(RD->isCompleteDefinition()); |
Richard Smith | e9a8bc3 | 2014-09-30 00:45:29 +0000 | [diff] [blame] | 5716 | assert(!WritingAST && "Already writing the AST!"); |
Aaron Ballman | 4f45b71 | 2014-03-21 15:22:56 +0000 | [diff] [blame] | 5717 | DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D)); |
Argyrios Kyrtzidis | e16a530 | 2010-10-24 17:26:54 +0000 | [diff] [blame] | 5718 | } |
Argyrios Kyrtzidis | 402dbbb | 2010-10-28 07:38:42 +0000 | [diff] [blame] | 5719 | |
Richard Smith | 564417a | 2014-03-20 21:47:22 +0000 | [diff] [blame] | 5720 | void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) { |
Richard Smith | 9e2341d | 2015-03-23 03:25:59 +0000 | [diff] [blame] | 5721 | assert(!DoneWritingDeclsAndTypes && "Already done writing updates!"); |
| 5722 | if (!Chain) return; |
Richard Smith | 5fc18a9 | 2015-07-12 23:43:21 +0000 | [diff] [blame] | 5723 | Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) { |
Richard Smith | 9e2341d | 2015-03-23 03:25:59 +0000 | [diff] [blame] | 5724 | // If we don't already know the exception specification for this redecl |
| 5725 | // chain, add an update record for it. |
| 5726 | if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D) |
| 5727 | ->getType() |
| 5728 | ->castAs<FunctionProtoType>() |
| 5729 | ->getExceptionSpecType())) |
| 5730 | DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC); |
| 5731 | }); |
Richard Smith | 564417a | 2014-03-20 21:47:22 +0000 | [diff] [blame] | 5732 | } |
| 5733 | |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 5734 | void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) { |
| 5735 | assert(!WritingAST && "Already writing the AST!"); |
Richard Smith | 9e2341d | 2015-03-23 03:25:59 +0000 | [diff] [blame] | 5736 | if (!Chain) return; |
Richard Smith | 5fc18a9 | 2015-07-12 23:43:21 +0000 | [diff] [blame] | 5737 | Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) { |
Richard Smith | 9e2341d | 2015-03-23 03:25:59 +0000 | [diff] [blame] | 5738 | DeclUpdates[D].push_back( |
| 5739 | DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType)); |
| 5740 | }); |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 5741 | } |
| 5742 | |
Richard Smith | f813400 | 2015-03-10 01:41:22 +0000 | [diff] [blame] | 5743 | void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD, |
| 5744 | const FunctionDecl *Delete) { |
| 5745 | assert(!WritingAST && "Already writing the AST!"); |
| 5746 | assert(Delete && "Not given an operator delete"); |
Richard Smith | 9e2341d | 2015-03-23 03:25:59 +0000 | [diff] [blame] | 5747 | if (!Chain) return; |
Richard Smith | 5fc18a9 | 2015-07-12 23:43:21 +0000 | [diff] [blame] | 5748 | Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) { |
Richard Smith | 9e2341d | 2015-03-23 03:25:59 +0000 | [diff] [blame] | 5749 | DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete)); |
| 5750 | }); |
Richard Smith | f813400 | 2015-03-10 01:41:22 +0000 | [diff] [blame] | 5751 | } |
| 5752 | |
Sebastian Redl | ab238a7 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 5753 | void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) { |
Douglas Gregor | 2fd3d40 | 2011-09-17 00:05:03 +0000 | [diff] [blame] | 5754 | assert(!WritingAST && "Already writing the AST!"); |
Douglas Gregor | b3722e2 | 2011-09-09 23:01:35 +0000 | [diff] [blame] | 5755 | if (!D->isFromASTFile()) |
Sebastian Redl | ab238a7 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 5756 | return; // Declaration not imported from PCH. |
| 5757 | |
Richard Smith | 4d23579 | 2014-08-07 18:53:08 +0000 | [diff] [blame] | 5758 | // Implicit function decl from a PCH was defined. |
| 5759 | DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION)); |
Sebastian Redl | ab238a7 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 5760 | } |
| 5761 | |
Richard Smith | d28ac5b | 2014-03-22 23:33:22 +0000 | [diff] [blame] | 5762 | void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) { |
| 5763 | assert(!WritingAST && "Already writing the AST!"); |
| 5764 | if (!D->isFromASTFile()) |
| 5765 | return; |
| 5766 | |
Richard Smith | 9e2341d | 2015-03-23 03:25:59 +0000 | [diff] [blame] | 5767 | DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION)); |
Richard Smith | d28ac5b | 2014-03-22 23:33:22 +0000 | [diff] [blame] | 5768 | } |
| 5769 | |
Sebastian Redl | 2ac2c72 | 2011-04-29 08:19:30 +0000 | [diff] [blame] | 5770 | void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) { |
Douglas Gregor | 2fd3d40 | 2011-09-17 00:05:03 +0000 | [diff] [blame] | 5771 | assert(!WritingAST && "Already writing the AST!"); |
Douglas Gregor | b3722e2 | 2011-09-09 23:01:35 +0000 | [diff] [blame] | 5772 | if (!D->isFromASTFile()) |
Sebastian Redl | 2ac2c72 | 2011-04-29 08:19:30 +0000 | [diff] [blame] | 5773 | return; |
| 5774 | |
| 5775 | // Since the actual instantiation is delayed, this really means that we need |
| 5776 | // to update the instantiation location. |
Richard Smith | 6ef4293 | 2014-03-20 21:02:00 +0000 | [diff] [blame] | 5777 | DeclUpdates[D].push_back( |
Aaron Ballman | 4f45b71 | 2014-03-21 15:22:56 +0000 | [diff] [blame] | 5778 | DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER, |
| 5779 | D->getMemberSpecializationInfo()->getPointOfInstantiation())); |
Sebastian Redl | 2ac2c72 | 2011-04-29 08:19:30 +0000 | [diff] [blame] | 5780 | } |
| 5781 | |
Argyrios Kyrtzidis | 7d847c9 | 2011-09-01 00:58:55 +0000 | [diff] [blame] | 5782 | void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD, |
| 5783 | const ObjCInterfaceDecl *IFD) { |
Douglas Gregor | 2fd3d40 | 2011-09-17 00:05:03 +0000 | [diff] [blame] | 5784 | assert(!WritingAST && "Already writing the AST!"); |
Douglas Gregor | b3722e2 | 2011-09-09 23:01:35 +0000 | [diff] [blame] | 5785 | if (!IFD->isFromASTFile()) |
Argyrios Kyrtzidis | 7d847c9 | 2011-09-01 00:58:55 +0000 | [diff] [blame] | 5786 | return; // Declaration not imported from PCH. |
Douglas Gregor | 404cdde | 2012-01-27 01:47:08 +0000 | [diff] [blame] | 5787 | |
| 5788 | assert(IFD->getDefinition() && "Category on a class without a definition?"); |
| 5789 | ObjCClassesWithCategories.insert( |
| 5790 | const_cast<ObjCInterfaceDecl *>(IFD->getDefinition())); |
Argyrios Kyrtzidis | 7d847c9 | 2011-09-01 00:58:55 +0000 | [diff] [blame] | 5791 | } |
Argyrios Kyrtzidis | b97a402 | 2011-11-12 21:07:46 +0000 | [diff] [blame] | 5792 | |
Eli Friedman | 276dd18 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 5793 | void ASTWriter::DeclarationMarkedUsed(const Decl *D) { |
| 5794 | assert(!WritingAST && "Already writing the AST!"); |
| 5795 | if (!D->isFromASTFile()) |
| 5796 | return; |
| 5797 | |
Aaron Ballman | 4f45b71 | 2014-03-21 15:22:56 +0000 | [diff] [blame] | 5798 | DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED)); |
Eli Friedman | 276dd18 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 5799 | } |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 5800 | |
| 5801 | void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) { |
| 5802 | assert(!WritingAST && "Already writing the AST!"); |
| 5803 | if (!D->isFromASTFile()) |
| 5804 | return; |
| 5805 | |
| 5806 | DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE)); |
| 5807 | } |
Richard Smith | 65ebb4a | 2015-03-26 04:09:53 +0000 | [diff] [blame] | 5808 | |
Richard Smith | 4caa449 | 2015-05-15 02:34:32 +0000 | [diff] [blame] | 5809 | void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) { |
Richard Smith | 65ebb4a | 2015-03-26 04:09:53 +0000 | [diff] [blame] | 5810 | assert(!WritingAST && "Already writing the AST!"); |
| 5811 | assert(D->isHidden() && "expected a hidden declaration"); |
Richard Smith | 4caa449 | 2015-05-15 02:34:32 +0000 | [diff] [blame] | 5812 | DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M)); |
Richard Smith | 65ebb4a | 2015-03-26 04:09:53 +0000 | [diff] [blame] | 5813 | } |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 5814 | |
| 5815 | void ASTWriter::AddedAttributeToRecord(const Attr *Attr, |
| 5816 | const RecordDecl *Record) { |
| 5817 | assert(!WritingAST && "Already writing the AST!"); |
| 5818 | if (!Record->isFromASTFile()) |
| 5819 | return; |
| 5820 | DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr)); |
| 5821 | } |