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