Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1 | //===--- PCHWriter.h - Precompiled Headers Writer ---------------*- C++ -*-===// |
| 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 | e778504 | 2009-04-20 15:53:59 +0000 | [diff] [blame] | 15 | #include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 16 | #include "../Sema/IdentifierResolver.h" // FIXME: move header |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
| 18 | #include "clang/AST/Decl.h" |
| 19 | #include "clang/AST/DeclContextInternals.h" |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 21 | #include "clang/AST/Type.h" |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 22 | #include "clang/Lex/MacroInfo.h" |
| 23 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 24 | #include "clang/Lex/HeaderSearch.h" |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 25 | #include "clang/Basic/FileManager.h" |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 26 | #include "clang/Basic/OnDiskHashTable.h" |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 27 | #include "clang/Basic/SourceManager.h" |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 28 | #include "clang/Basic/SourceManagerInternals.h" |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 29 | #include "clang/Basic/TargetInfo.h" |
Douglas Gregor | ab41e63 | 2009-04-27 22:23:34 +0000 | [diff] [blame] | 30 | #include "clang/Basic/Version.h" |
Douglas Gregor | 17fc223 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/APFloat.h" |
| 32 | #include "llvm/ADT/APInt.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 33 | #include "llvm/Bitcode/BitstreamWriter.h" |
| 34 | #include "llvm/Support/Compiler.h" |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 35 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | 3c304bd | 2009-04-11 18:40:46 +0000 | [diff] [blame] | 36 | #include <cstdio> |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 37 | using namespace clang; |
| 38 | |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | // Type serialization |
| 41 | //===----------------------------------------------------------------------===// |
Chris Lattner | 12b1c76 | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 42 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 43 | namespace { |
| 44 | class VISIBILITY_HIDDEN PCHTypeWriter { |
| 45 | PCHWriter &Writer; |
| 46 | PCHWriter::RecordData &Record; |
| 47 | |
| 48 | public: |
| 49 | /// \brief Type code that corresponds to the record generated. |
| 50 | pch::TypeCode Code; |
| 51 | |
| 52 | PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record) |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 53 | : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 54 | |
| 55 | void VisitArrayType(const ArrayType *T); |
| 56 | void VisitFunctionType(const FunctionType *T); |
| 57 | void VisitTagType(const TagType *T); |
| 58 | |
| 59 | #define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T); |
| 60 | #define ABSTRACT_TYPE(Class, Base) |
| 61 | #define DEPENDENT_TYPE(Class, Base) |
| 62 | #include "clang/AST/TypeNodes.def" |
| 63 | }; |
| 64 | } |
| 65 | |
| 66 | void PCHTypeWriter::VisitExtQualType(const ExtQualType *T) { |
| 67 | Writer.AddTypeRef(QualType(T->getBaseType(), 0), Record); |
| 68 | Record.push_back(T->getObjCGCAttr()); // FIXME: use stable values |
| 69 | Record.push_back(T->getAddressSpace()); |
| 70 | Code = pch::TYPE_EXT_QUAL; |
| 71 | } |
| 72 | |
| 73 | void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) { |
| 74 | assert(false && "Built-in types are never serialized"); |
| 75 | } |
| 76 | |
| 77 | void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) { |
| 78 | Record.push_back(T->getWidth()); |
| 79 | Record.push_back(T->isSigned()); |
| 80 | Code = pch::TYPE_FIXED_WIDTH_INT; |
| 81 | } |
| 82 | |
| 83 | void PCHTypeWriter::VisitComplexType(const ComplexType *T) { |
| 84 | Writer.AddTypeRef(T->getElementType(), Record); |
| 85 | Code = pch::TYPE_COMPLEX; |
| 86 | } |
| 87 | |
| 88 | void PCHTypeWriter::VisitPointerType(const PointerType *T) { |
| 89 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 90 | Code = pch::TYPE_POINTER; |
| 91 | } |
| 92 | |
| 93 | void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) { |
| 94 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 95 | Code = pch::TYPE_BLOCK_POINTER; |
| 96 | } |
| 97 | |
| 98 | void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) { |
| 99 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 100 | Code = pch::TYPE_LVALUE_REFERENCE; |
| 101 | } |
| 102 | |
| 103 | void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) { |
| 104 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 105 | Code = pch::TYPE_RVALUE_REFERENCE; |
| 106 | } |
| 107 | |
| 108 | void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) { |
| 109 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 110 | Writer.AddTypeRef(QualType(T->getClass(), 0), Record); |
| 111 | Code = pch::TYPE_MEMBER_POINTER; |
| 112 | } |
| 113 | |
| 114 | void PCHTypeWriter::VisitArrayType(const ArrayType *T) { |
| 115 | Writer.AddTypeRef(T->getElementType(), Record); |
| 116 | Record.push_back(T->getSizeModifier()); // FIXME: stable values |
| 117 | Record.push_back(T->getIndexTypeQualifier()); // FIXME: stable values |
| 118 | } |
| 119 | |
| 120 | void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) { |
| 121 | VisitArrayType(T); |
| 122 | Writer.AddAPInt(T->getSize(), Record); |
| 123 | Code = pch::TYPE_CONSTANT_ARRAY; |
| 124 | } |
| 125 | |
| 126 | void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) { |
| 127 | VisitArrayType(T); |
| 128 | Code = pch::TYPE_INCOMPLETE_ARRAY; |
| 129 | } |
| 130 | |
| 131 | void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) { |
| 132 | VisitArrayType(T); |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 133 | Writer.AddStmt(T->getSizeExpr()); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 134 | Code = pch::TYPE_VARIABLE_ARRAY; |
| 135 | } |
| 136 | |
| 137 | void PCHTypeWriter::VisitVectorType(const VectorType *T) { |
| 138 | Writer.AddTypeRef(T->getElementType(), Record); |
| 139 | Record.push_back(T->getNumElements()); |
| 140 | Code = pch::TYPE_VECTOR; |
| 141 | } |
| 142 | |
| 143 | void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) { |
| 144 | VisitVectorType(T); |
| 145 | Code = pch::TYPE_EXT_VECTOR; |
| 146 | } |
| 147 | |
| 148 | void PCHTypeWriter::VisitFunctionType(const FunctionType *T) { |
| 149 | Writer.AddTypeRef(T->getResultType(), Record); |
| 150 | } |
| 151 | |
| 152 | void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { |
| 153 | VisitFunctionType(T); |
| 154 | Code = pch::TYPE_FUNCTION_NO_PROTO; |
| 155 | } |
| 156 | |
| 157 | void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) { |
| 158 | VisitFunctionType(T); |
| 159 | Record.push_back(T->getNumArgs()); |
| 160 | for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I) |
| 161 | Writer.AddTypeRef(T->getArgType(I), Record); |
| 162 | Record.push_back(T->isVariadic()); |
| 163 | Record.push_back(T->getTypeQuals()); |
Sebastian Redl | 7b9a2ee | 2009-04-30 19:20:55 +0000 | [diff] [blame] | 164 | Record.push_back(T->hasExceptionSpec()); |
| 165 | Record.push_back(T->hasAnyExceptionSpec()); |
| 166 | Record.push_back(T->getNumExceptions()); |
| 167 | for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I) |
| 168 | Writer.AddTypeRef(T->getExceptionType(I), Record); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 169 | Code = pch::TYPE_FUNCTION_PROTO; |
| 170 | } |
| 171 | |
| 172 | void PCHTypeWriter::VisitTypedefType(const TypedefType *T) { |
| 173 | Writer.AddDeclRef(T->getDecl(), Record); |
| 174 | Code = pch::TYPE_TYPEDEF; |
| 175 | } |
| 176 | |
| 177 | void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) { |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 178 | Writer.AddStmt(T->getUnderlyingExpr()); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 179 | Code = pch::TYPE_TYPEOF_EXPR; |
| 180 | } |
| 181 | |
| 182 | void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) { |
| 183 | Writer.AddTypeRef(T->getUnderlyingType(), Record); |
| 184 | Code = pch::TYPE_TYPEOF; |
| 185 | } |
| 186 | |
| 187 | void PCHTypeWriter::VisitTagType(const TagType *T) { |
| 188 | Writer.AddDeclRef(T->getDecl(), Record); |
| 189 | assert(!T->isBeingDefined() && |
| 190 | "Cannot serialize in the middle of a type definition"); |
| 191 | } |
| 192 | |
| 193 | void PCHTypeWriter::VisitRecordType(const RecordType *T) { |
| 194 | VisitTagType(T); |
| 195 | Code = pch::TYPE_RECORD; |
| 196 | } |
| 197 | |
| 198 | void PCHTypeWriter::VisitEnumType(const EnumType *T) { |
| 199 | VisitTagType(T); |
| 200 | Code = pch::TYPE_ENUM; |
| 201 | } |
| 202 | |
| 203 | void |
| 204 | PCHTypeWriter::VisitTemplateSpecializationType( |
| 205 | const TemplateSpecializationType *T) { |
Douglas Gregor | 6a2bfb2 | 2009-04-15 18:43:11 +0000 | [diff] [blame] | 206 | // FIXME: Serialize this type (C++ only) |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 207 | assert(false && "Cannot serialize template specialization types"); |
| 208 | } |
| 209 | |
| 210 | void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) { |
Douglas Gregor | 6a2bfb2 | 2009-04-15 18:43:11 +0000 | [diff] [blame] | 211 | // FIXME: Serialize this type (C++ only) |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 212 | assert(false && "Cannot serialize qualified name types"); |
| 213 | } |
| 214 | |
| 215 | void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { |
| 216 | Writer.AddDeclRef(T->getDecl(), Record); |
| 217 | Code = pch::TYPE_OBJC_INTERFACE; |
| 218 | } |
| 219 | |
| 220 | void |
| 221 | PCHTypeWriter::VisitObjCQualifiedInterfaceType( |
| 222 | const ObjCQualifiedInterfaceType *T) { |
| 223 | VisitObjCInterfaceType(T); |
| 224 | Record.push_back(T->getNumProtocols()); |
| 225 | for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I) |
| 226 | Writer.AddDeclRef(T->getProtocol(I), Record); |
| 227 | Code = pch::TYPE_OBJC_QUALIFIED_INTERFACE; |
| 228 | } |
| 229 | |
| 230 | void PCHTypeWriter::VisitObjCQualifiedIdType(const ObjCQualifiedIdType *T) { |
| 231 | Record.push_back(T->getNumProtocols()); |
| 232 | for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I) |
| 233 | Writer.AddDeclRef(T->getProtocols(I), Record); |
| 234 | Code = pch::TYPE_OBJC_QUALIFIED_ID; |
| 235 | } |
| 236 | |
Chris Lattner | 4dcf151a | 2009-04-22 05:57:30 +0000 | [diff] [blame] | 237 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 238 | // PCHWriter Implementation |
| 239 | //===----------------------------------------------------------------------===// |
| 240 | |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 241 | static void EmitBlockID(unsigned ID, const char *Name, |
| 242 | llvm::BitstreamWriter &Stream, |
| 243 | PCHWriter::RecordData &Record) { |
| 244 | Record.clear(); |
| 245 | Record.push_back(ID); |
| 246 | Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record); |
| 247 | |
| 248 | // Emit the block name if present. |
| 249 | if (Name == 0 || Name[0] == 0) return; |
| 250 | Record.clear(); |
| 251 | while (*Name) |
| 252 | Record.push_back(*Name++); |
| 253 | Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record); |
| 254 | } |
| 255 | |
| 256 | static void EmitRecordID(unsigned ID, const char *Name, |
| 257 | llvm::BitstreamWriter &Stream, |
| 258 | PCHWriter::RecordData &Record) { |
| 259 | Record.clear(); |
| 260 | Record.push_back(ID); |
| 261 | while (*Name) |
| 262 | Record.push_back(*Name++); |
| 263 | Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record); |
Chris Lattner | 0558df2 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static void AddStmtsExprs(llvm::BitstreamWriter &Stream, |
| 267 | PCHWriter::RecordData &Record) { |
| 268 | #define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record) |
| 269 | RECORD(STMT_STOP); |
| 270 | RECORD(STMT_NULL_PTR); |
| 271 | RECORD(STMT_NULL); |
| 272 | RECORD(STMT_COMPOUND); |
| 273 | RECORD(STMT_CASE); |
| 274 | RECORD(STMT_DEFAULT); |
| 275 | RECORD(STMT_LABEL); |
| 276 | RECORD(STMT_IF); |
| 277 | RECORD(STMT_SWITCH); |
| 278 | RECORD(STMT_WHILE); |
| 279 | RECORD(STMT_DO); |
| 280 | RECORD(STMT_FOR); |
| 281 | RECORD(STMT_GOTO); |
| 282 | RECORD(STMT_INDIRECT_GOTO); |
| 283 | RECORD(STMT_CONTINUE); |
| 284 | RECORD(STMT_BREAK); |
| 285 | RECORD(STMT_RETURN); |
| 286 | RECORD(STMT_DECL); |
| 287 | RECORD(STMT_ASM); |
| 288 | RECORD(EXPR_PREDEFINED); |
| 289 | RECORD(EXPR_DECL_REF); |
| 290 | RECORD(EXPR_INTEGER_LITERAL); |
| 291 | RECORD(EXPR_FLOATING_LITERAL); |
| 292 | RECORD(EXPR_IMAGINARY_LITERAL); |
| 293 | RECORD(EXPR_STRING_LITERAL); |
| 294 | RECORD(EXPR_CHARACTER_LITERAL); |
| 295 | RECORD(EXPR_PAREN); |
| 296 | RECORD(EXPR_UNARY_OPERATOR); |
| 297 | RECORD(EXPR_SIZEOF_ALIGN_OF); |
| 298 | RECORD(EXPR_ARRAY_SUBSCRIPT); |
| 299 | RECORD(EXPR_CALL); |
| 300 | RECORD(EXPR_MEMBER); |
| 301 | RECORD(EXPR_BINARY_OPERATOR); |
| 302 | RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR); |
| 303 | RECORD(EXPR_CONDITIONAL_OPERATOR); |
| 304 | RECORD(EXPR_IMPLICIT_CAST); |
| 305 | RECORD(EXPR_CSTYLE_CAST); |
| 306 | RECORD(EXPR_COMPOUND_LITERAL); |
| 307 | RECORD(EXPR_EXT_VECTOR_ELEMENT); |
| 308 | RECORD(EXPR_INIT_LIST); |
| 309 | RECORD(EXPR_DESIGNATED_INIT); |
| 310 | RECORD(EXPR_IMPLICIT_VALUE_INIT); |
| 311 | RECORD(EXPR_VA_ARG); |
| 312 | RECORD(EXPR_ADDR_LABEL); |
| 313 | RECORD(EXPR_STMT); |
| 314 | RECORD(EXPR_TYPES_COMPATIBLE); |
| 315 | RECORD(EXPR_CHOOSE); |
| 316 | RECORD(EXPR_GNU_NULL); |
| 317 | RECORD(EXPR_SHUFFLE_VECTOR); |
| 318 | RECORD(EXPR_BLOCK); |
| 319 | RECORD(EXPR_BLOCK_DECL_REF); |
| 320 | RECORD(EXPR_OBJC_STRING_LITERAL); |
| 321 | RECORD(EXPR_OBJC_ENCODE); |
| 322 | RECORD(EXPR_OBJC_SELECTOR_EXPR); |
| 323 | RECORD(EXPR_OBJC_PROTOCOL_EXPR); |
| 324 | RECORD(EXPR_OBJC_IVAR_REF_EXPR); |
| 325 | RECORD(EXPR_OBJC_PROPERTY_REF_EXPR); |
| 326 | RECORD(EXPR_OBJC_KVC_REF_EXPR); |
| 327 | RECORD(EXPR_OBJC_MESSAGE_EXPR); |
| 328 | RECORD(EXPR_OBJC_SUPER_EXPR); |
| 329 | RECORD(STMT_OBJC_FOR_COLLECTION); |
| 330 | RECORD(STMT_OBJC_CATCH); |
| 331 | RECORD(STMT_OBJC_FINALLY); |
| 332 | RECORD(STMT_OBJC_AT_TRY); |
| 333 | RECORD(STMT_OBJC_AT_SYNCHRONIZED); |
| 334 | RECORD(STMT_OBJC_AT_THROW); |
| 335 | #undef RECORD |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | void PCHWriter::WriteBlockInfoBlock() { |
| 339 | RecordData Record; |
| 340 | Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3); |
| 341 | |
Chris Lattner | 2f4efd1 | 2009-04-27 00:40:25 +0000 | [diff] [blame] | 342 | #define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record) |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 343 | #define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record) |
| 344 | |
| 345 | // PCH Top-Level Block. |
Chris Lattner | 2f4efd1 | 2009-04-27 00:40:25 +0000 | [diff] [blame] | 346 | BLOCK(PCH_BLOCK); |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 347 | RECORD(TYPE_OFFSET); |
| 348 | RECORD(DECL_OFFSET); |
| 349 | RECORD(LANGUAGE_OPTIONS); |
Douglas Gregor | ab41e63 | 2009-04-27 22:23:34 +0000 | [diff] [blame] | 350 | RECORD(METADATA); |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 351 | RECORD(IDENTIFIER_OFFSET); |
| 352 | RECORD(IDENTIFIER_TABLE); |
| 353 | RECORD(EXTERNAL_DEFINITIONS); |
| 354 | RECORD(SPECIAL_TYPES); |
| 355 | RECORD(STATISTICS); |
| 356 | RECORD(TENTATIVE_DEFINITIONS); |
| 357 | RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS); |
| 358 | RECORD(SELECTOR_OFFSETS); |
| 359 | RECORD(METHOD_POOL); |
| 360 | RECORD(PP_COUNTER_VALUE); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 361 | RECORD(SOURCE_LOCATION_OFFSETS); |
| 362 | RECORD(SOURCE_LOCATION_PRELOADS); |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 363 | RECORD(STAT_CACHE); |
Douglas Gregor | b81c170 | 2009-04-27 20:06:05 +0000 | [diff] [blame] | 364 | RECORD(EXT_VECTOR_DECLS); |
| 365 | RECORD(OBJC_CATEGORY_IMPLEMENTATIONS); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 366 | |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 367 | // SourceManager Block. |
Chris Lattner | 2f4efd1 | 2009-04-27 00:40:25 +0000 | [diff] [blame] | 368 | BLOCK(SOURCE_MANAGER_BLOCK); |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 369 | RECORD(SM_SLOC_FILE_ENTRY); |
| 370 | RECORD(SM_SLOC_BUFFER_ENTRY); |
| 371 | RECORD(SM_SLOC_BUFFER_BLOB); |
| 372 | RECORD(SM_SLOC_INSTANTIATION_ENTRY); |
| 373 | RECORD(SM_LINE_TABLE); |
| 374 | RECORD(SM_HEADER_FILE_INFO); |
| 375 | |
| 376 | // Preprocessor Block. |
Chris Lattner | 2f4efd1 | 2009-04-27 00:40:25 +0000 | [diff] [blame] | 377 | BLOCK(PREPROCESSOR_BLOCK); |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 378 | RECORD(PP_MACRO_OBJECT_LIKE); |
| 379 | RECORD(PP_MACRO_FUNCTION_LIKE); |
| 380 | RECORD(PP_TOKEN); |
| 381 | |
| 382 | // Types block. |
Chris Lattner | 2f4efd1 | 2009-04-27 00:40:25 +0000 | [diff] [blame] | 383 | BLOCK(TYPES_BLOCK); |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 384 | RECORD(TYPE_EXT_QUAL); |
| 385 | RECORD(TYPE_FIXED_WIDTH_INT); |
| 386 | RECORD(TYPE_COMPLEX); |
| 387 | RECORD(TYPE_POINTER); |
| 388 | RECORD(TYPE_BLOCK_POINTER); |
| 389 | RECORD(TYPE_LVALUE_REFERENCE); |
| 390 | RECORD(TYPE_RVALUE_REFERENCE); |
| 391 | RECORD(TYPE_MEMBER_POINTER); |
| 392 | RECORD(TYPE_CONSTANT_ARRAY); |
| 393 | RECORD(TYPE_INCOMPLETE_ARRAY); |
| 394 | RECORD(TYPE_VARIABLE_ARRAY); |
| 395 | RECORD(TYPE_VECTOR); |
| 396 | RECORD(TYPE_EXT_VECTOR); |
| 397 | RECORD(TYPE_FUNCTION_PROTO); |
| 398 | RECORD(TYPE_FUNCTION_NO_PROTO); |
| 399 | RECORD(TYPE_TYPEDEF); |
| 400 | RECORD(TYPE_TYPEOF_EXPR); |
| 401 | RECORD(TYPE_TYPEOF); |
| 402 | RECORD(TYPE_RECORD); |
| 403 | RECORD(TYPE_ENUM); |
| 404 | RECORD(TYPE_OBJC_INTERFACE); |
| 405 | RECORD(TYPE_OBJC_QUALIFIED_INTERFACE); |
| 406 | RECORD(TYPE_OBJC_QUALIFIED_ID); |
Chris Lattner | 0558df2 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 407 | // Statements and Exprs can occur in the Types block. |
| 408 | AddStmtsExprs(Stream, Record); |
| 409 | |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 410 | // Decls block. |
Chris Lattner | 2f4efd1 | 2009-04-27 00:40:25 +0000 | [diff] [blame] | 411 | BLOCK(DECLS_BLOCK); |
Chris Lattner | 0ff8cda | 2009-04-26 22:32:16 +0000 | [diff] [blame] | 412 | RECORD(DECL_ATTR); |
| 413 | RECORD(DECL_TRANSLATION_UNIT); |
| 414 | RECORD(DECL_TYPEDEF); |
| 415 | RECORD(DECL_ENUM); |
| 416 | RECORD(DECL_RECORD); |
| 417 | RECORD(DECL_ENUM_CONSTANT); |
| 418 | RECORD(DECL_FUNCTION); |
| 419 | RECORD(DECL_OBJC_METHOD); |
| 420 | RECORD(DECL_OBJC_INTERFACE); |
| 421 | RECORD(DECL_OBJC_PROTOCOL); |
| 422 | RECORD(DECL_OBJC_IVAR); |
| 423 | RECORD(DECL_OBJC_AT_DEFS_FIELD); |
| 424 | RECORD(DECL_OBJC_CLASS); |
| 425 | RECORD(DECL_OBJC_FORWARD_PROTOCOL); |
| 426 | RECORD(DECL_OBJC_CATEGORY); |
| 427 | RECORD(DECL_OBJC_CATEGORY_IMPL); |
| 428 | RECORD(DECL_OBJC_IMPLEMENTATION); |
| 429 | RECORD(DECL_OBJC_COMPATIBLE_ALIAS); |
| 430 | RECORD(DECL_OBJC_PROPERTY); |
| 431 | RECORD(DECL_OBJC_PROPERTY_IMPL); |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 432 | RECORD(DECL_FIELD); |
| 433 | RECORD(DECL_VAR); |
Chris Lattner | 0ff8cda | 2009-04-26 22:32:16 +0000 | [diff] [blame] | 434 | RECORD(DECL_IMPLICIT_PARAM); |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 435 | RECORD(DECL_PARM_VAR); |
Chris Lattner | 0ff8cda | 2009-04-26 22:32:16 +0000 | [diff] [blame] | 436 | RECORD(DECL_ORIGINAL_PARM_VAR); |
| 437 | RECORD(DECL_FILE_SCOPE_ASM); |
| 438 | RECORD(DECL_BLOCK); |
| 439 | RECORD(DECL_CONTEXT_LEXICAL); |
| 440 | RECORD(DECL_CONTEXT_VISIBLE); |
Chris Lattner | 0558df2 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 441 | // Statements and Exprs can occur in the Decls block. |
| 442 | AddStmtsExprs(Stream, Record); |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 443 | #undef RECORD |
| 444 | #undef BLOCK |
| 445 | Stream.ExitBlock(); |
| 446 | } |
| 447 | |
| 448 | |
Douglas Gregor | ab41e63 | 2009-04-27 22:23:34 +0000 | [diff] [blame] | 449 | /// \brief Write the PCH metadata (e.g., i686-apple-darwin9). |
| 450 | void PCHWriter::WriteMetadata(const TargetInfo &Target) { |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 451 | using namespace llvm; |
| 452 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
Douglas Gregor | ab41e63 | 2009-04-27 22:23:34 +0000 | [diff] [blame] | 453 | Abbrev->Add(BitCodeAbbrevOp(pch::METADATA)); |
| 454 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major |
| 455 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor |
| 456 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major |
| 457 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor |
| 458 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple |
| 459 | unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 460 | |
| 461 | RecordData Record; |
Douglas Gregor | ab41e63 | 2009-04-27 22:23:34 +0000 | [diff] [blame] | 462 | Record.push_back(pch::METADATA); |
| 463 | Record.push_back(pch::VERSION_MAJOR); |
| 464 | Record.push_back(pch::VERSION_MINOR); |
| 465 | Record.push_back(CLANG_VERSION_MAJOR); |
| 466 | Record.push_back(CLANG_VERSION_MINOR); |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 467 | const char *Triple = Target.getTargetTriple(); |
Douglas Gregor | ab41e63 | 2009-04-27 22:23:34 +0000 | [diff] [blame] | 468 | Stream.EmitRecordWithBlob(AbbrevCode, Record, Triple, strlen(Triple)); |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /// \brief Write the LangOptions structure. |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 472 | void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) { |
| 473 | RecordData Record; |
| 474 | Record.push_back(LangOpts.Trigraphs); |
| 475 | Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments. |
| 476 | Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers. |
| 477 | Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode. |
| 478 | Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc) |
| 479 | Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'. |
| 480 | Record.push_back(LangOpts.Digraphs); // C94, C99 and C++ |
| 481 | Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants. |
| 482 | Record.push_back(LangOpts.C99); // C99 Support |
| 483 | Record.push_back(LangOpts.Microsoft); // Microsoft extensions. |
| 484 | Record.push_back(LangOpts.CPlusPlus); // C++ Support |
| 485 | Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 486 | Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords. |
| 487 | |
| 488 | Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled. |
| 489 | Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled. |
| 490 | Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled |
| 491 | |
| 492 | Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 493 | Record.push_back(LangOpts.WritableStrings); // Allow writable strings |
| 494 | Record.push_back(LangOpts.LaxVectorConversions); |
| 495 | Record.push_back(LangOpts.Exceptions); // Support exception handling. |
| 496 | |
| 497 | Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime. |
| 498 | Record.push_back(LangOpts.Freestanding); // Freestanding implementation |
| 499 | Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin) |
| 500 | |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 501 | // Whether static initializers are protected by locks. |
| 502 | Record.push_back(LangOpts.ThreadsafeStatics); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 503 | Record.push_back(LangOpts.Blocks); // block extension to C |
| 504 | Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if |
| 505 | // they are unused. |
| 506 | Record.push_back(LangOpts.MathErrno); // Math functions must respect errno |
| 507 | // (modulo the platform support). |
| 508 | |
| 509 | Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when |
| 510 | // signed integer arithmetic overflows. |
| 511 | |
| 512 | Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and |
| 513 | // may be ripped out at any time. |
| 514 | |
| 515 | Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined. |
| 516 | Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be |
| 517 | // defined. |
| 518 | Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as |
| 519 | // opposed to __DYNAMIC__). |
| 520 | Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero. |
| 521 | |
| 522 | Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be |
| 523 | // used (instead of C99 semantics). |
| 524 | Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined. |
| 525 | Record.push_back(LangOpts.getGCMode()); |
| 526 | Record.push_back(LangOpts.getVisibilityMode()); |
| 527 | Record.push_back(LangOpts.InstantiationDepth); |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 528 | Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 531 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 532 | // stat cache Serialization |
| 533 | //===----------------------------------------------------------------------===// |
| 534 | |
| 535 | namespace { |
| 536 | // Trait used for the on-disk hash table of stat cache results. |
| 537 | class VISIBILITY_HIDDEN PCHStatCacheTrait { |
| 538 | public: |
| 539 | typedef const char * key_type; |
| 540 | typedef key_type key_type_ref; |
| 541 | |
| 542 | typedef std::pair<int, struct stat> data_type; |
| 543 | typedef const data_type& data_type_ref; |
| 544 | |
| 545 | static unsigned ComputeHash(const char *path) { |
| 546 | return BernsteinHash(path); |
| 547 | } |
| 548 | |
| 549 | std::pair<unsigned,unsigned> |
| 550 | EmitKeyDataLength(llvm::raw_ostream& Out, const char *path, |
| 551 | data_type_ref Data) { |
| 552 | unsigned StrLen = strlen(path); |
| 553 | clang::io::Emit16(Out, StrLen); |
| 554 | unsigned DataLen = 1; // result value |
| 555 | if (Data.first == 0) |
| 556 | DataLen += 4 + 4 + 2 + 8 + 8; |
| 557 | clang::io::Emit8(Out, DataLen); |
| 558 | return std::make_pair(StrLen + 1, DataLen); |
| 559 | } |
| 560 | |
| 561 | void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) { |
| 562 | Out.write(path, KeyLen); |
| 563 | } |
| 564 | |
| 565 | void EmitData(llvm::raw_ostream& Out, key_type_ref, |
| 566 | data_type_ref Data, unsigned DataLen) { |
| 567 | using namespace clang::io; |
| 568 | uint64_t Start = Out.tell(); (void)Start; |
| 569 | |
| 570 | // Result of stat() |
| 571 | Emit8(Out, Data.first? 1 : 0); |
| 572 | |
| 573 | if (Data.first == 0) { |
| 574 | Emit32(Out, (uint32_t) Data.second.st_ino); |
| 575 | Emit32(Out, (uint32_t) Data.second.st_dev); |
| 576 | Emit16(Out, (uint16_t) Data.second.st_mode); |
| 577 | Emit64(Out, (uint64_t) Data.second.st_mtime); |
| 578 | Emit64(Out, (uint64_t) Data.second.st_size); |
| 579 | } |
| 580 | |
| 581 | assert(Out.tell() - Start == DataLen && "Wrong data length"); |
| 582 | } |
| 583 | }; |
| 584 | } // end anonymous namespace |
| 585 | |
| 586 | /// \brief Write the stat() system call cache to the PCH file. |
| 587 | void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls) { |
| 588 | // Build the on-disk hash table containing information about every |
| 589 | // stat() call. |
| 590 | OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator; |
| 591 | unsigned NumStatEntries = 0; |
| 592 | for (MemorizeStatCalls::iterator Stat = StatCalls.begin(), |
| 593 | StatEnd = StatCalls.end(); |
| 594 | Stat != StatEnd; ++Stat, ++NumStatEntries) |
| 595 | Generator.insert(Stat->first(), Stat->second); |
| 596 | |
| 597 | // Create the on-disk hash table in a buffer. |
| 598 | llvm::SmallVector<char, 4096> StatCacheData; |
| 599 | uint32_t BucketOffset; |
| 600 | { |
| 601 | llvm::raw_svector_ostream Out(StatCacheData); |
| 602 | // Make sure that no bucket is at offset 0 |
| 603 | clang::io::Emit32(Out, 0); |
| 604 | BucketOffset = Generator.Emit(Out); |
| 605 | } |
| 606 | |
| 607 | // Create a blob abbreviation |
| 608 | using namespace llvm; |
| 609 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 610 | Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE)); |
| 611 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
| 612 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
| 613 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 614 | unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev); |
| 615 | |
| 616 | // Write the stat cache |
| 617 | RecordData Record; |
| 618 | Record.push_back(pch::STAT_CACHE); |
| 619 | Record.push_back(BucketOffset); |
| 620 | Record.push_back(NumStatEntries); |
| 621 | Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, |
| 622 | &StatCacheData.front(), |
| 623 | StatCacheData.size()); |
| 624 | } |
| 625 | |
| 626 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 627 | // Source Manager Serialization |
| 628 | //===----------------------------------------------------------------------===// |
| 629 | |
| 630 | /// \brief Create an abbreviation for the SLocEntry that refers to a |
| 631 | /// file. |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 632 | static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) { |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 633 | using namespace llvm; |
| 634 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 635 | Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY)); |
| 636 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset |
| 637 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location |
| 638 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic |
| 639 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 640 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 641 | return Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | /// \brief Create an abbreviation for the SLocEntry that refers to a |
| 645 | /// buffer. |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 646 | static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) { |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 647 | using namespace llvm; |
| 648 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 649 | Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY)); |
| 650 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset |
| 651 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location |
| 652 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic |
| 653 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives |
| 654 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 655 | return Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | /// \brief Create an abbreviation for the SLocEntry that refers to a |
| 659 | /// buffer's blob. |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 660 | static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) { |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 661 | using namespace llvm; |
| 662 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 663 | Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB)); |
| 664 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 665 | return Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | /// \brief Create an abbreviation for the SLocEntry that refers to an |
| 669 | /// buffer. |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 670 | static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) { |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 671 | using namespace llvm; |
| 672 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 673 | Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY)); |
| 674 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset |
| 675 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location |
| 676 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location |
| 677 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location |
Douglas Gregor | f60e991 | 2009-04-15 18:05:10 +0000 | [diff] [blame] | 678 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 679 | return Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /// \brief Writes the block containing the serialized form of the |
| 683 | /// source manager. |
| 684 | /// |
| 685 | /// TODO: We should probably use an on-disk hash table (stored in a |
| 686 | /// blob), indexed based on the file name, so that we only create |
| 687 | /// entries for files that we actually need. In the common case (no |
| 688 | /// errors), we probably won't have to create file entries for any of |
| 689 | /// the files in the AST. |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 690 | void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr, |
| 691 | const Preprocessor &PP) { |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 692 | RecordData Record; |
| 693 | |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 694 | // Enter the source manager block. |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 695 | Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 696 | |
| 697 | // Abbreviations for the various kinds of source-location entries. |
Chris Lattner | 828e18c | 2009-04-27 19:03:22 +0000 | [diff] [blame] | 698 | unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream); |
| 699 | unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream); |
| 700 | unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream); |
| 701 | unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 702 | |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 703 | // Write the line table. |
| 704 | if (SourceMgr.hasLineTable()) { |
| 705 | LineTableInfo &LineTable = SourceMgr.getLineTable(); |
| 706 | |
| 707 | // Emit the file names |
| 708 | Record.push_back(LineTable.getNumFilenames()); |
| 709 | for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) { |
| 710 | // Emit the file name |
| 711 | const char *Filename = LineTable.getFilename(I); |
| 712 | unsigned FilenameLen = Filename? strlen(Filename) : 0; |
| 713 | Record.push_back(FilenameLen); |
| 714 | if (FilenameLen) |
| 715 | Record.insert(Record.end(), Filename, Filename + FilenameLen); |
| 716 | } |
| 717 | |
| 718 | // Emit the line entries |
| 719 | for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end(); |
| 720 | L != LEnd; ++L) { |
| 721 | // Emit the file ID |
| 722 | Record.push_back(L->first); |
| 723 | |
| 724 | // Emit the line entries |
| 725 | Record.push_back(L->second.size()); |
| 726 | for (std::vector<LineEntry>::iterator LE = L->second.begin(), |
| 727 | LEEnd = L->second.end(); |
| 728 | LE != LEEnd; ++LE) { |
| 729 | Record.push_back(LE->FileOffset); |
| 730 | Record.push_back(LE->LineNo); |
| 731 | Record.push_back(LE->FilenameID); |
| 732 | Record.push_back((unsigned)LE->FileKind); |
| 733 | Record.push_back(LE->IncludeOffset); |
| 734 | } |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 735 | Stream.EmitRecord(pch::SM_LINE_TABLE, Record); |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 736 | } |
| 737 | } |
| 738 | |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 739 | // Write out entries for all of the header files we know about. |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 740 | HeaderSearch &HS = PP.getHeaderSearchInfo(); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 741 | Record.clear(); |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 742 | for (HeaderSearch::header_file_iterator I = HS.header_file_begin(), |
| 743 | E = HS.header_file_end(); |
| 744 | I != E; ++I) { |
| 745 | Record.push_back(I->isImport); |
| 746 | Record.push_back(I->DirInfo); |
| 747 | Record.push_back(I->NumIncludes); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 748 | AddIdentifierRef(I->ControllingMacro, Record); |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 749 | Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record); |
| 750 | Record.clear(); |
| 751 | } |
| 752 | |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 753 | // Write out the source location entry table. We skip the first |
| 754 | // entry, which is always the same dummy entry. |
Chris Lattner | 090d9b5 | 2009-04-27 19:01:47 +0000 | [diff] [blame] | 755 | std::vector<uint32_t> SLocEntryOffsets; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 756 | RecordData PreloadSLocs; |
| 757 | SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1); |
| 758 | for (SourceManager::sloc_entry_iterator |
| 759 | SLoc = SourceMgr.sloc_entry_begin() + 1, |
| 760 | SLocEnd = SourceMgr.sloc_entry_end(); |
| 761 | SLoc != SLocEnd; ++SLoc) { |
| 762 | // Record the offset of this source-location entry. |
| 763 | SLocEntryOffsets.push_back(Stream.GetCurrentBitNo()); |
| 764 | |
| 765 | // Figure out which record code to use. |
| 766 | unsigned Code; |
| 767 | if (SLoc->isFile()) { |
| 768 | if (SLoc->getFile().getContentCache()->Entry) |
| 769 | Code = pch::SM_SLOC_FILE_ENTRY; |
| 770 | else |
| 771 | Code = pch::SM_SLOC_BUFFER_ENTRY; |
| 772 | } else |
| 773 | Code = pch::SM_SLOC_INSTANTIATION_ENTRY; |
| 774 | Record.clear(); |
| 775 | Record.push_back(Code); |
| 776 | |
| 777 | Record.push_back(SLoc->getOffset()); |
| 778 | if (SLoc->isFile()) { |
| 779 | const SrcMgr::FileInfo &File = SLoc->getFile(); |
| 780 | Record.push_back(File.getIncludeLoc().getRawEncoding()); |
| 781 | Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding |
| 782 | Record.push_back(File.hasLineDirectives()); |
| 783 | |
| 784 | const SrcMgr::ContentCache *Content = File.getContentCache(); |
| 785 | if (Content->Entry) { |
| 786 | // The source location entry is a file. The blob associated |
| 787 | // with this entry is the file name. |
| 788 | Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, |
| 789 | Content->Entry->getName(), |
| 790 | strlen(Content->Entry->getName())); |
| 791 | |
| 792 | // FIXME: For now, preload all file source locations, so that |
| 793 | // we get the appropriate File entries in the reader. This is |
| 794 | // a temporary measure. |
| 795 | PreloadSLocs.push_back(SLocEntryOffsets.size()); |
| 796 | } else { |
| 797 | // The source location entry is a buffer. The blob associated |
| 798 | // with this entry contains the contents of the buffer. |
| 799 | |
| 800 | // We add one to the size so that we capture the trailing NULL |
| 801 | // that is required by llvm::MemoryBuffer::getMemBuffer (on |
| 802 | // the reader side). |
| 803 | const llvm::MemoryBuffer *Buffer = Content->getBuffer(); |
| 804 | const char *Name = Buffer->getBufferIdentifier(); |
| 805 | Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1); |
| 806 | Record.clear(); |
| 807 | Record.push_back(pch::SM_SLOC_BUFFER_BLOB); |
| 808 | Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, |
| 809 | Buffer->getBufferStart(), |
| 810 | Buffer->getBufferSize() + 1); |
| 811 | |
| 812 | if (strcmp(Name, "<built-in>") == 0) |
| 813 | PreloadSLocs.push_back(SLocEntryOffsets.size()); |
| 814 | } |
| 815 | } else { |
| 816 | // The source location entry is an instantiation. |
| 817 | const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation(); |
| 818 | Record.push_back(Inst.getSpellingLoc().getRawEncoding()); |
| 819 | Record.push_back(Inst.getInstantiationLocStart().getRawEncoding()); |
| 820 | Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding()); |
| 821 | |
| 822 | // Compute the token length for this macro expansion. |
| 823 | unsigned NextOffset = SourceMgr.getNextOffset(); |
| 824 | SourceManager::sloc_entry_iterator NextSLoc = SLoc; |
| 825 | if (++NextSLoc != SLocEnd) |
| 826 | NextOffset = NextSLoc->getOffset(); |
| 827 | Record.push_back(NextOffset - SLoc->getOffset() - 1); |
| 828 | Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record); |
| 829 | } |
| 830 | } |
| 831 | |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 832 | Stream.ExitBlock(); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 833 | |
| 834 | if (SLocEntryOffsets.empty()) |
| 835 | return; |
| 836 | |
| 837 | // Write the source-location offsets table into the PCH block. This |
| 838 | // table is used for lazily loading source-location information. |
| 839 | using namespace llvm; |
| 840 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 841 | Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS)); |
| 842 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs |
| 843 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset |
| 844 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets |
| 845 | unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev); |
| 846 | |
| 847 | Record.clear(); |
| 848 | Record.push_back(pch::SOURCE_LOCATION_OFFSETS); |
| 849 | Record.push_back(SLocEntryOffsets.size()); |
| 850 | Record.push_back(SourceMgr.getNextOffset()); |
| 851 | Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, |
| 852 | (const char *)&SLocEntryOffsets.front(), |
Chris Lattner | 090d9b5 | 2009-04-27 19:01:47 +0000 | [diff] [blame] | 853 | SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0])); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 854 | |
| 855 | // Write the source location entry preloads array, telling the PCH |
| 856 | // reader which source locations entries it should load eagerly. |
| 857 | Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 860 | //===----------------------------------------------------------------------===// |
| 861 | // Preprocessor Serialization |
| 862 | //===----------------------------------------------------------------------===// |
| 863 | |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 864 | /// \brief Writes the block containing the serialized form of the |
| 865 | /// preprocessor. |
| 866 | /// |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 867 | void PCHWriter::WritePreprocessor(const Preprocessor &PP) { |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 868 | RecordData Record; |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 869 | |
Chris Lattner | c1f9d82 | 2009-04-13 01:29:17 +0000 | [diff] [blame] | 870 | // If the preprocessor __COUNTER__ value has been bumped, remember it. |
| 871 | if (PP.getCounterValue() != 0) { |
| 872 | Record.push_back(PP.getCounterValue()); |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 873 | Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record); |
Chris Lattner | c1f9d82 | 2009-04-13 01:29:17 +0000 | [diff] [blame] | 874 | Record.clear(); |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | // Enter the preprocessor block. |
| 878 | Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2); |
Chris Lattner | c1f9d82 | 2009-04-13 01:29:17 +0000 | [diff] [blame] | 879 | |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 880 | // If the PCH file contains __DATE__ or __TIME__ emit a warning about this. |
| 881 | // FIXME: use diagnostics subsystem for localization etc. |
| 882 | if (PP.SawDateOrTime()) |
| 883 | fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n"); |
| 884 | |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 885 | // Loop over all the macro definitions that are live at the end of the file, |
| 886 | // emitting each to the PP section. |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 887 | for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end(); |
| 888 | I != E; ++I) { |
Chris Lattner | 42d42b5 | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 889 | // FIXME: This emits macros in hash table order, we should do it in a stable |
| 890 | // order so that output is reproducible. |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 891 | MacroInfo *MI = I->second; |
| 892 | |
| 893 | // Don't emit builtin macros like __LINE__ to the PCH file unless they have |
| 894 | // been redefined by the header (in which case they are not isBuiltinMacro). |
| 895 | if (MI->isBuiltinMacro()) |
| 896 | continue; |
| 897 | |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 898 | // FIXME: Remove this identifier reference? |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 899 | AddIdentifierRef(I->first, Record); |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 900 | MacroOffsets[I->first] = Stream.GetCurrentBitNo(); |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 901 | Record.push_back(MI->getDefinitionLoc().getRawEncoding()); |
| 902 | Record.push_back(MI->isUsed()); |
| 903 | |
| 904 | unsigned Code; |
| 905 | if (MI->isObjectLike()) { |
| 906 | Code = pch::PP_MACRO_OBJECT_LIKE; |
| 907 | } else { |
| 908 | Code = pch::PP_MACRO_FUNCTION_LIKE; |
| 909 | |
| 910 | Record.push_back(MI->isC99Varargs()); |
| 911 | Record.push_back(MI->isGNUVarargs()); |
| 912 | Record.push_back(MI->getNumArgs()); |
| 913 | for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end(); |
| 914 | I != E; ++I) |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 915 | AddIdentifierRef(*I, Record); |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 916 | } |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 917 | Stream.EmitRecord(Code, Record); |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 918 | Record.clear(); |
| 919 | |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 920 | // Emit the tokens array. |
| 921 | for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) { |
| 922 | // Note that we know that the preprocessor does not have any annotation |
| 923 | // tokens in it because they are created by the parser, and thus can't be |
| 924 | // in a macro definition. |
| 925 | const Token &Tok = MI->getReplacementToken(TokNo); |
| 926 | |
| 927 | Record.push_back(Tok.getLocation().getRawEncoding()); |
| 928 | Record.push_back(Tok.getLength()); |
| 929 | |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 930 | // FIXME: When reading literal tokens, reconstruct the literal pointer if |
| 931 | // it is needed. |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 932 | AddIdentifierRef(Tok.getIdentifierInfo(), Record); |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 933 | |
| 934 | // FIXME: Should translate token kind to a stable encoding. |
| 935 | Record.push_back(Tok.getKind()); |
| 936 | // FIXME: Should translate token flags to a stable encoding. |
| 937 | Record.push_back(Tok.getFlags()); |
| 938 | |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 939 | Stream.EmitRecord(pch::PP_TOKEN, Record); |
Chris Lattner | df961c2 | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 940 | Record.clear(); |
| 941 | } |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 942 | ++NumMacros; |
Chris Lattner | 7c5d24e | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 943 | } |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 944 | Stream.ExitBlock(); |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 945 | } |
| 946 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 947 | //===----------------------------------------------------------------------===// |
| 948 | // Type Serialization |
| 949 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 950 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 951 | /// \brief Write the representation of a type to the PCH stream. |
| 952 | void PCHWriter::WriteType(const Type *T) { |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 953 | pch::TypeID &ID = TypeIDs[T]; |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 954 | if (ID == 0) // we haven't seen this type before. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 955 | ID = NextTypeID++; |
| 956 | |
| 957 | // Record the offset for this type. |
| 958 | if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS) |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 959 | TypeOffsets.push_back(Stream.GetCurrentBitNo()); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 960 | else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) { |
| 961 | TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS); |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 962 | TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | RecordData Record; |
| 966 | |
| 967 | // Emit the type's representation. |
| 968 | PCHTypeWriter W(*this, Record); |
| 969 | switch (T->getTypeClass()) { |
| 970 | // For all of the concrete, non-dependent types, call the |
| 971 | // appropriate visitor function. |
| 972 | #define TYPE(Class, Base) \ |
| 973 | case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break; |
| 974 | #define ABSTRACT_TYPE(Class, Base) |
| 975 | #define DEPENDENT_TYPE(Class, Base) |
| 976 | #include "clang/AST/TypeNodes.def" |
| 977 | |
| 978 | // For all of the dependent type nodes (which only occur in C++ |
| 979 | // templates), produce an error. |
| 980 | #define TYPE(Class, Base) |
| 981 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 982 | #include "clang/AST/TypeNodes.def" |
| 983 | assert(false && "Cannot serialize dependent type nodes"); |
| 984 | break; |
| 985 | } |
| 986 | |
| 987 | // Emit the serialized record. |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 988 | Stream.EmitRecord(W.Code, Record); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 989 | |
| 990 | // Flush any expressions that were written as part of this type. |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 991 | FlushStmts(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | /// \brief Write a block containing all of the types. |
| 995 | void PCHWriter::WriteTypesBlock(ASTContext &Context) { |
Chris Lattner | f04ad69 | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 996 | // Enter the types block. |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 997 | Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 998 | |
Douglas Gregor | 366809a | 2009-04-26 03:49:13 +0000 | [diff] [blame] | 999 | // Emit all of the types that need to be emitted (so far). |
| 1000 | while (!TypesToEmit.empty()) { |
| 1001 | const Type *T = TypesToEmit.front(); |
| 1002 | TypesToEmit.pop(); |
| 1003 | assert(!isa<BuiltinType>(T) && "Built-in types are not serialized"); |
| 1004 | WriteType(T); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | // Exit the types block |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1008 | Stream.ExitBlock(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1011 | //===----------------------------------------------------------------------===// |
| 1012 | // Declaration Serialization |
| 1013 | //===----------------------------------------------------------------------===// |
| 1014 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1015 | /// \brief Write the block containing all of the declaration IDs |
| 1016 | /// lexically declared within the given DeclContext. |
| 1017 | /// |
| 1018 | /// \returns the offset of the DECL_CONTEXT_LEXICAL block within the |
| 1019 | /// bistream, or 0 if no block was written. |
| 1020 | uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context, |
| 1021 | DeclContext *DC) { |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1022 | if (DC->decls_empty(Context)) |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1023 | return 0; |
| 1024 | |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1025 | uint64_t Offset = Stream.GetCurrentBitNo(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1026 | RecordData Record; |
| 1027 | for (DeclContext::decl_iterator D = DC->decls_begin(Context), |
| 1028 | DEnd = DC->decls_end(Context); |
| 1029 | D != DEnd; ++D) |
| 1030 | AddDeclRef(*D, Record); |
| 1031 | |
Douglas Gregor | 2512308 | 2009-04-22 22:34:57 +0000 | [diff] [blame] | 1032 | ++NumLexicalDeclContexts; |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1033 | Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1034 | return Offset; |
| 1035 | } |
| 1036 | |
| 1037 | /// \brief Write the block containing all of the declaration IDs |
| 1038 | /// visible from the given DeclContext. |
| 1039 | /// |
| 1040 | /// \returns the offset of the DECL_CONTEXT_VISIBLE block within the |
| 1041 | /// bistream, or 0 if no block was written. |
| 1042 | uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context, |
| 1043 | DeclContext *DC) { |
| 1044 | if (DC->getPrimaryContext() != DC) |
| 1045 | return 0; |
| 1046 | |
Douglas Gregor | aff22df | 2009-04-21 22:32:33 +0000 | [diff] [blame] | 1047 | // Since there is no name lookup into functions or methods, and we |
| 1048 | // perform name lookup for the translation unit via the |
| 1049 | // IdentifierInfo chains, don't bother to build a |
| 1050 | // visible-declarations table for these entities. |
| 1051 | if (DC->isFunctionOrMethod() || DC->isTranslationUnit()) |
Douglas Gregor | 58f0699 | 2009-04-18 15:49:20 +0000 | [diff] [blame] | 1052 | return 0; |
| 1053 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1054 | // Force the DeclContext to build a its name-lookup table. |
| 1055 | DC->lookup(Context, DeclarationName()); |
| 1056 | |
| 1057 | // Serialize the contents of the mapping used for lookup. Note that, |
| 1058 | // although we have two very different code paths, the serialized |
| 1059 | // representation is the same for both cases: a declaration name, |
| 1060 | // followed by a size, followed by references to the visible |
| 1061 | // declarations that have that name. |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1062 | uint64_t Offset = Stream.GetCurrentBitNo(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1063 | RecordData Record; |
| 1064 | StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr()); |
Douglas Gregor | 8c70006 | 2009-04-13 21:20:57 +0000 | [diff] [blame] | 1065 | if (!Map) |
| 1066 | return 0; |
| 1067 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1068 | for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end(); |
| 1069 | D != DEnd; ++D) { |
| 1070 | AddDeclarationName(D->first, Record); |
| 1071 | DeclContext::lookup_result Result = D->second.getLookupResult(Context); |
| 1072 | Record.push_back(Result.second - Result.first); |
| 1073 | for(; Result.first != Result.second; ++Result.first) |
| 1074 | AddDeclRef(*Result.first, Record); |
| 1075 | } |
| 1076 | |
| 1077 | if (Record.size() == 0) |
| 1078 | return 0; |
| 1079 | |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1080 | Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record); |
Douglas Gregor | 2512308 | 2009-04-22 22:34:57 +0000 | [diff] [blame] | 1081 | ++NumVisibleDeclContexts; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1082 | return Offset; |
| 1083 | } |
| 1084 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1085 | //===----------------------------------------------------------------------===// |
| 1086 | // Global Method Pool and Selector Serialization |
| 1087 | //===----------------------------------------------------------------------===// |
| 1088 | |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 1089 | namespace { |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1090 | // Trait used for the on-disk hash table used in the method pool. |
| 1091 | class VISIBILITY_HIDDEN PCHMethodPoolTrait { |
| 1092 | PCHWriter &Writer; |
| 1093 | |
| 1094 | public: |
| 1095 | typedef Selector key_type; |
| 1096 | typedef key_type key_type_ref; |
| 1097 | |
| 1098 | typedef std::pair<ObjCMethodList, ObjCMethodList> data_type; |
| 1099 | typedef const data_type& data_type_ref; |
| 1100 | |
| 1101 | explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { } |
| 1102 | |
| 1103 | static unsigned ComputeHash(Selector Sel) { |
| 1104 | unsigned N = Sel.getNumArgs(); |
| 1105 | if (N == 0) |
| 1106 | ++N; |
| 1107 | unsigned R = 5381; |
| 1108 | for (unsigned I = 0; I != N; ++I) |
| 1109 | if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I)) |
| 1110 | R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R); |
| 1111 | return R; |
| 1112 | } |
| 1113 | |
| 1114 | std::pair<unsigned,unsigned> |
| 1115 | EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel, |
| 1116 | data_type_ref Methods) { |
| 1117 | unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4); |
| 1118 | clang::io::Emit16(Out, KeyLen); |
| 1119 | unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts |
| 1120 | for (const ObjCMethodList *Method = &Methods.first; Method; |
| 1121 | Method = Method->Next) |
| 1122 | if (Method->Method) |
| 1123 | DataLen += 4; |
| 1124 | for (const ObjCMethodList *Method = &Methods.second; Method; |
| 1125 | Method = Method->Next) |
| 1126 | if (Method->Method) |
| 1127 | DataLen += 4; |
| 1128 | clang::io::Emit16(Out, DataLen); |
| 1129 | return std::make_pair(KeyLen, DataLen); |
| 1130 | } |
| 1131 | |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1132 | void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) { |
| 1133 | uint64_t Start = Out.tell(); |
| 1134 | assert((Start >> 32) == 0 && "Selector key offset too large"); |
| 1135 | Writer.SetSelectorOffset(Sel, Start); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1136 | unsigned N = Sel.getNumArgs(); |
| 1137 | clang::io::Emit16(Out, N); |
| 1138 | if (N == 0) |
| 1139 | N = 1; |
| 1140 | for (unsigned I = 0; I != N; ++I) |
| 1141 | clang::io::Emit32(Out, |
| 1142 | Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I))); |
| 1143 | } |
| 1144 | |
| 1145 | void EmitData(llvm::raw_ostream& Out, key_type_ref, |
Douglas Gregor | a67e58c | 2009-04-24 21:49:02 +0000 | [diff] [blame] | 1146 | data_type_ref Methods, unsigned DataLen) { |
| 1147 | uint64_t Start = Out.tell(); (void)Start; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1148 | unsigned NumInstanceMethods = 0; |
| 1149 | for (const ObjCMethodList *Method = &Methods.first; Method; |
| 1150 | Method = Method->Next) |
| 1151 | if (Method->Method) |
| 1152 | ++NumInstanceMethods; |
| 1153 | |
| 1154 | unsigned NumFactoryMethods = 0; |
| 1155 | for (const ObjCMethodList *Method = &Methods.second; Method; |
| 1156 | Method = Method->Next) |
| 1157 | if (Method->Method) |
| 1158 | ++NumFactoryMethods; |
| 1159 | |
| 1160 | clang::io::Emit16(Out, NumInstanceMethods); |
| 1161 | clang::io::Emit16(Out, NumFactoryMethods); |
| 1162 | for (const ObjCMethodList *Method = &Methods.first; Method; |
| 1163 | Method = Method->Next) |
| 1164 | if (Method->Method) |
| 1165 | clang::io::Emit32(Out, Writer.getDeclID(Method->Method)); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1166 | for (const ObjCMethodList *Method = &Methods.second; Method; |
| 1167 | Method = Method->Next) |
| 1168 | if (Method->Method) |
| 1169 | clang::io::Emit32(Out, Writer.getDeclID(Method->Method)); |
Douglas Gregor | a67e58c | 2009-04-24 21:49:02 +0000 | [diff] [blame] | 1170 | |
| 1171 | assert(Out.tell() - Start == DataLen && "Data length is wrong"); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1172 | } |
| 1173 | }; |
| 1174 | } // end anonymous namespace |
| 1175 | |
| 1176 | /// \brief Write the method pool into the PCH file. |
| 1177 | /// |
| 1178 | /// The method pool contains both instance and factory methods, stored |
| 1179 | /// in an on-disk hash table indexed by the selector. |
| 1180 | void PCHWriter::WriteMethodPool(Sema &SemaRef) { |
| 1181 | using namespace llvm; |
| 1182 | |
| 1183 | // Create and write out the blob that contains the instance and |
| 1184 | // factor method pools. |
| 1185 | bool Empty = true; |
| 1186 | { |
| 1187 | OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator; |
| 1188 | |
| 1189 | // Create the on-disk hash table representation. Start by |
| 1190 | // iterating through the instance method pool. |
| 1191 | PCHMethodPoolTrait::key_type Key; |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1192 | unsigned NumSelectorsInMethodPool = 0; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1193 | for (llvm::DenseMap<Selector, ObjCMethodList>::iterator |
| 1194 | Instance = SemaRef.InstanceMethodPool.begin(), |
| 1195 | InstanceEnd = SemaRef.InstanceMethodPool.end(); |
| 1196 | Instance != InstanceEnd; ++Instance) { |
| 1197 | // Check whether there is a factory method with the same |
| 1198 | // selector. |
| 1199 | llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory |
| 1200 | = SemaRef.FactoryMethodPool.find(Instance->first); |
| 1201 | |
| 1202 | if (Factory == SemaRef.FactoryMethodPool.end()) |
| 1203 | Generator.insert(Instance->first, |
| 1204 | std::make_pair(Instance->second, |
| 1205 | ObjCMethodList())); |
| 1206 | else |
| 1207 | Generator.insert(Instance->first, |
| 1208 | std::make_pair(Instance->second, Factory->second)); |
| 1209 | |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1210 | ++NumSelectorsInMethodPool; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1211 | Empty = false; |
| 1212 | } |
| 1213 | |
| 1214 | // Now iterate through the factory method pool, to pick up any |
| 1215 | // selectors that weren't already in the instance method pool. |
| 1216 | for (llvm::DenseMap<Selector, ObjCMethodList>::iterator |
| 1217 | Factory = SemaRef.FactoryMethodPool.begin(), |
| 1218 | FactoryEnd = SemaRef.FactoryMethodPool.end(); |
| 1219 | Factory != FactoryEnd; ++Factory) { |
| 1220 | // Check whether there is an instance method with the same |
| 1221 | // selector. If so, there is no work to do here. |
| 1222 | llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance |
| 1223 | = SemaRef.InstanceMethodPool.find(Factory->first); |
| 1224 | |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1225 | if (Instance == SemaRef.InstanceMethodPool.end()) { |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1226 | Generator.insert(Factory->first, |
| 1227 | std::make_pair(ObjCMethodList(), Factory->second)); |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1228 | ++NumSelectorsInMethodPool; |
| 1229 | } |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1230 | |
| 1231 | Empty = false; |
| 1232 | } |
| 1233 | |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1234 | if (Empty && SelectorOffsets.empty()) |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1235 | return; |
| 1236 | |
| 1237 | // Create the on-disk hash table in a buffer. |
| 1238 | llvm::SmallVector<char, 4096> MethodPool; |
| 1239 | uint32_t BucketOffset; |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1240 | SelectorOffsets.resize(SelVector.size()); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1241 | { |
| 1242 | PCHMethodPoolTrait Trait(*this); |
| 1243 | llvm::raw_svector_ostream Out(MethodPool); |
| 1244 | // Make sure that no bucket is at offset 0 |
Douglas Gregor | a67e58c | 2009-04-24 21:49:02 +0000 | [diff] [blame] | 1245 | clang::io::Emit32(Out, 0); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1246 | BucketOffset = Generator.Emit(Out, Trait); |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1247 | |
| 1248 | // For every selector that we have seen but which was not |
| 1249 | // written into the hash table, write the selector itself and |
| 1250 | // record it's offset. |
| 1251 | for (unsigned I = 0, N = SelVector.size(); I != N; ++I) |
| 1252 | if (SelectorOffsets[I] == 0) |
| 1253 | Trait.EmitKey(Out, SelVector[I], 0); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | // Create a blob abbreviation |
| 1257 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 1258 | Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL)); |
| 1259 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1260 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1261 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 1262 | unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev); |
| 1263 | |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1264 | // Write the method pool |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1265 | RecordData Record; |
| 1266 | Record.push_back(pch::METHOD_POOL); |
| 1267 | Record.push_back(BucketOffset); |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1268 | Record.push_back(NumSelectorsInMethodPool); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1269 | Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, |
| 1270 | &MethodPool.front(), |
| 1271 | MethodPool.size()); |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1272 | |
| 1273 | // Create a blob abbreviation for the selector table offsets. |
| 1274 | Abbrev = new BitCodeAbbrev(); |
| 1275 | Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS)); |
| 1276 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index |
| 1277 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 1278 | unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev); |
| 1279 | |
| 1280 | // Write the selector offsets table. |
| 1281 | Record.clear(); |
| 1282 | Record.push_back(pch::SELECTOR_OFFSETS); |
| 1283 | Record.push_back(SelectorOffsets.size()); |
| 1284 | Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record, |
| 1285 | (const char *)&SelectorOffsets.front(), |
| 1286 | SelectorOffsets.size() * 4); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1287 | } |
| 1288 | } |
| 1289 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1290 | //===----------------------------------------------------------------------===// |
| 1291 | // Identifier Table Serialization |
| 1292 | //===----------------------------------------------------------------------===// |
| 1293 | |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1294 | namespace { |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 1295 | class VISIBILITY_HIDDEN PCHIdentifierTableTrait { |
| 1296 | PCHWriter &Writer; |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1297 | Preprocessor &PP; |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 1298 | |
Douglas Gregor | a92193e | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 1299 | /// \brief Determines whether this is an "interesting" identifier |
| 1300 | /// that needs a full IdentifierInfo structure written into the hash |
| 1301 | /// table. |
| 1302 | static bool isInterestingIdentifier(const IdentifierInfo *II) { |
| 1303 | return II->isPoisoned() || |
| 1304 | II->isExtensionToken() || |
| 1305 | II->hasMacroDefinition() || |
| 1306 | II->getObjCOrBuiltinID() || |
| 1307 | II->getFETokenInfo<void>(); |
| 1308 | } |
| 1309 | |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 1310 | public: |
| 1311 | typedef const IdentifierInfo* key_type; |
| 1312 | typedef key_type key_type_ref; |
| 1313 | |
| 1314 | typedef pch::IdentID data_type; |
| 1315 | typedef data_type data_type_ref; |
| 1316 | |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1317 | PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP) |
| 1318 | : Writer(Writer), PP(PP) { } |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 1319 | |
| 1320 | static unsigned ComputeHash(const IdentifierInfo* II) { |
| 1321 | return clang::BernsteinHash(II->getName()); |
| 1322 | } |
| 1323 | |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1324 | std::pair<unsigned,unsigned> |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 1325 | EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II, |
| 1326 | pch::IdentID ID) { |
| 1327 | unsigned KeyLen = strlen(II->getName()) + 1; |
Douglas Gregor | a92193e | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 1328 | unsigned DataLen = 4; // 4 bytes for the persistent ID << 1 |
| 1329 | if (isInterestingIdentifier(II)) { |
Douglas Gregor | 5998da5 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 1330 | DataLen += 2; // 2 bytes for builtin ID, flags |
Douglas Gregor | a92193e | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 1331 | if (II->hasMacroDefinition() && |
| 1332 | !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro()) |
Douglas Gregor | 5998da5 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 1333 | DataLen += 4; |
Douglas Gregor | a92193e | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 1334 | for (IdentifierResolver::iterator D = IdentifierResolver::begin(II), |
| 1335 | DEnd = IdentifierResolver::end(); |
| 1336 | D != DEnd; ++D) |
| 1337 | DataLen += sizeof(pch::DeclID); |
| 1338 | } |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1339 | clang::io::Emit16(Out, DataLen); |
Douglas Gregor | 02fc751 | 2009-04-28 20:01:51 +0000 | [diff] [blame] | 1340 | // We emit the key length after the data length so that every |
| 1341 | // string is preceded by a 16-bit length. This matches the PTH |
| 1342 | // format for storing identifiers. |
Douglas Gregor | d6595a4 | 2009-04-25 21:04:17 +0000 | [diff] [blame] | 1343 | clang::io::Emit16(Out, KeyLen); |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 1344 | return std::make_pair(KeyLen, DataLen); |
| 1345 | } |
| 1346 | |
| 1347 | void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II, |
| 1348 | unsigned KeyLen) { |
| 1349 | // Record the location of the key data. This is used when generating |
| 1350 | // the mapping from persistent IDs to strings. |
| 1351 | Writer.SetIdentifierOffset(II, Out.tell()); |
| 1352 | Out.write(II->getName(), KeyLen); |
| 1353 | } |
| 1354 | |
| 1355 | void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II, |
| 1356 | pch::IdentID ID, unsigned) { |
Douglas Gregor | a92193e | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 1357 | if (!isInterestingIdentifier(II)) { |
| 1358 | clang::io::Emit32(Out, ID << 1); |
| 1359 | return; |
| 1360 | } |
Douglas Gregor | 5998da5 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 1361 | |
Douglas Gregor | a92193e | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 1362 | clang::io::Emit32(Out, (ID << 1) | 0x01); |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 1363 | uint32_t Bits = 0; |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1364 | bool hasMacroDefinition = |
| 1365 | II->hasMacroDefinition() && |
| 1366 | !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro(); |
Douglas Gregor | 5998da5 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 1367 | Bits = (uint32_t)II->getObjCOrBuiltinID(); |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 1368 | Bits = (Bits << 1) | hasMacroDefinition; |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 1369 | Bits = (Bits << 1) | II->isExtensionToken(); |
| 1370 | Bits = (Bits << 1) | II->isPoisoned(); |
| 1371 | Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword(); |
Douglas Gregor | 5998da5 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 1372 | clang::io::Emit16(Out, Bits); |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 1373 | |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1374 | if (hasMacroDefinition) |
Douglas Gregor | 5998da5 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 1375 | clang::io::Emit32(Out, Writer.getMacroOffset(II)); |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1376 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1377 | // Emit the declaration IDs in reverse order, because the |
| 1378 | // IdentifierResolver provides the declarations as they would be |
| 1379 | // visible (e.g., the function "stat" would come before the struct |
| 1380 | // "stat"), but IdentifierResolver::AddDeclToIdentifierChain() |
| 1381 | // adds declarations to the end of the list (so we need to see the |
| 1382 | // struct "status" before the function "status"). |
| 1383 | llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II), |
| 1384 | IdentifierResolver::end()); |
| 1385 | for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(), |
| 1386 | DEnd = Decls.rend(); |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 1387 | D != DEnd; ++D) |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1388 | clang::io::Emit32(Out, Writer.getDeclID(*D)); |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 1389 | } |
| 1390 | }; |
| 1391 | } // end anonymous namespace |
| 1392 | |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1393 | /// \brief Write the identifier table into the PCH file. |
| 1394 | /// |
| 1395 | /// The identifier table consists of a blob containing string data |
| 1396 | /// (the actual identifiers themselves) and a separate "offsets" index |
| 1397 | /// that maps identifier IDs to locations within the blob. |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1398 | void PCHWriter::WriteIdentifierTable(Preprocessor &PP) { |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1399 | using namespace llvm; |
| 1400 | |
| 1401 | // Create and write out the blob that contains the identifier |
| 1402 | // strings. |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1403 | { |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 1404 | OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator; |
| 1405 | |
Douglas Gregor | 92b059e | 2009-04-28 20:33:11 +0000 | [diff] [blame] | 1406 | // Look for any identifiers that were named while processing the |
| 1407 | // headers, but are otherwise not needed. We add these to the hash |
| 1408 | // table to enable checking of the predefines buffer in the case |
| 1409 | // where the user adds new macro definitions when building the PCH |
| 1410 | // file. |
| 1411 | for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(), |
| 1412 | IDEnd = PP.getIdentifierTable().end(); |
| 1413 | ID != IDEnd; ++ID) |
| 1414 | getIdentifierRef(ID->second); |
| 1415 | |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 1416 | // Create the on-disk hash table representation. |
Douglas Gregor | 92b059e | 2009-04-28 20:33:11 +0000 | [diff] [blame] | 1417 | IdentifierOffsets.resize(IdentifierIDs.size()); |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1418 | for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator |
| 1419 | ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end(); |
| 1420 | ID != IDEnd; ++ID) { |
| 1421 | assert(ID->first && "NULL identifier in identifier table"); |
Douglas Gregor | 02fc751 | 2009-04-28 20:01:51 +0000 | [diff] [blame] | 1422 | Generator.insert(ID->first, ID->second); |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 1423 | } |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1424 | |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 1425 | // Create the on-disk hash table in a buffer. |
| 1426 | llvm::SmallVector<char, 4096> IdentifierTable; |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1427 | uint32_t BucketOffset; |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 1428 | { |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1429 | PCHIdentifierTableTrait Trait(*this, PP); |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 1430 | llvm::raw_svector_ostream Out(IdentifierTable); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1431 | // Make sure that no bucket is at offset 0 |
Douglas Gregor | a67e58c | 2009-04-24 21:49:02 +0000 | [diff] [blame] | 1432 | clang::io::Emit32(Out, 0); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1433 | BucketOffset = Generator.Emit(Out, Trait); |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | // Create a blob abbreviation |
| 1437 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 1438 | Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE)); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1439 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 1440 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1441 | unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1442 | |
| 1443 | // Write the identifier table |
| 1444 | RecordData Record; |
| 1445 | Record.push_back(pch::IDENTIFIER_TABLE); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1446 | Record.push_back(BucketOffset); |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 1447 | Stream.EmitRecordWithBlob(IDTableAbbrev, Record, |
| 1448 | &IdentifierTable.front(), |
| 1449 | IdentifierTable.size()); |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | // Write the offsets table for identifier IDs. |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1453 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 1454 | Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET)); |
| 1455 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers |
| 1456 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 1457 | unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev); |
| 1458 | |
| 1459 | RecordData Record; |
| 1460 | Record.push_back(pch::IDENTIFIER_OFFSET); |
| 1461 | Record.push_back(IdentifierOffsets.size()); |
| 1462 | Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record, |
| 1463 | (const char *)&IdentifierOffsets.front(), |
| 1464 | IdentifierOffsets.size() * sizeof(uint32_t)); |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1467 | //===----------------------------------------------------------------------===// |
| 1468 | // General Serialization Routines |
| 1469 | //===----------------------------------------------------------------------===// |
| 1470 | |
Douglas Gregor | 68a2eb0 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 1471 | /// \brief Write a record containing the given attributes. |
| 1472 | void PCHWriter::WriteAttributeRecord(const Attr *Attr) { |
| 1473 | RecordData Record; |
| 1474 | for (; Attr; Attr = Attr->getNext()) { |
| 1475 | Record.push_back(Attr->getKind()); // FIXME: stable encoding |
| 1476 | Record.push_back(Attr->isInherited()); |
| 1477 | switch (Attr->getKind()) { |
| 1478 | case Attr::Alias: |
| 1479 | AddString(cast<AliasAttr>(Attr)->getAliasee(), Record); |
| 1480 | break; |
| 1481 | |
| 1482 | case Attr::Aligned: |
| 1483 | Record.push_back(cast<AlignedAttr>(Attr)->getAlignment()); |
| 1484 | break; |
| 1485 | |
| 1486 | case Attr::AlwaysInline: |
| 1487 | break; |
| 1488 | |
| 1489 | case Attr::AnalyzerNoReturn: |
| 1490 | break; |
| 1491 | |
| 1492 | case Attr::Annotate: |
| 1493 | AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record); |
| 1494 | break; |
| 1495 | |
| 1496 | case Attr::AsmLabel: |
| 1497 | AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record); |
| 1498 | break; |
| 1499 | |
| 1500 | case Attr::Blocks: |
| 1501 | Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable |
| 1502 | break; |
| 1503 | |
| 1504 | case Attr::Cleanup: |
| 1505 | AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record); |
| 1506 | break; |
| 1507 | |
| 1508 | case Attr::Const: |
| 1509 | break; |
| 1510 | |
| 1511 | case Attr::Constructor: |
| 1512 | Record.push_back(cast<ConstructorAttr>(Attr)->getPriority()); |
| 1513 | break; |
| 1514 | |
| 1515 | case Attr::DLLExport: |
| 1516 | case Attr::DLLImport: |
| 1517 | case Attr::Deprecated: |
| 1518 | break; |
| 1519 | |
| 1520 | case Attr::Destructor: |
| 1521 | Record.push_back(cast<DestructorAttr>(Attr)->getPriority()); |
| 1522 | break; |
| 1523 | |
| 1524 | case Attr::FastCall: |
| 1525 | break; |
| 1526 | |
| 1527 | case Attr::Format: { |
| 1528 | const FormatAttr *Format = cast<FormatAttr>(Attr); |
| 1529 | AddString(Format->getType(), Record); |
| 1530 | Record.push_back(Format->getFormatIdx()); |
| 1531 | Record.push_back(Format->getFirstArg()); |
| 1532 | break; |
| 1533 | } |
| 1534 | |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 1535 | case Attr::GNUInline: |
Douglas Gregor | 68a2eb0 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 1536 | case Attr::IBOutletKind: |
| 1537 | case Attr::NoReturn: |
| 1538 | case Attr::NoThrow: |
| 1539 | case Attr::Nodebug: |
| 1540 | case Attr::Noinline: |
| 1541 | break; |
| 1542 | |
| 1543 | case Attr::NonNull: { |
| 1544 | const NonNullAttr *NonNull = cast<NonNullAttr>(Attr); |
| 1545 | Record.push_back(NonNull->size()); |
| 1546 | Record.insert(Record.end(), NonNull->begin(), NonNull->end()); |
| 1547 | break; |
| 1548 | } |
| 1549 | |
| 1550 | case Attr::ObjCException: |
| 1551 | case Attr::ObjCNSObject: |
Ted Kremenek | 31c215e | 2009-05-04 17:29:57 +0000 | [diff] [blame] | 1552 | case Attr::CFOwnershipRelease: |
| 1553 | case Attr::CFOwnershipRetain: |
Ted Kremenek | e351aa1 | 2009-05-05 00:46:09 +0000 | [diff] [blame] | 1554 | case Attr::CFOwnershipReturns: |
Ted Kremenek | 69aa080 | 2009-05-05 18:44:20 +0000 | [diff] [blame^] | 1555 | case Attr::NSOwnershipAutorelease: |
Ted Kremenek | 75494ff | 2009-05-04 19:10:19 +0000 | [diff] [blame] | 1556 | case Attr::NSOwnershipRelease: |
| 1557 | case Attr::NSOwnershipRetain: |
| 1558 | case Attr::NSOwnershipReturns: |
Douglas Gregor | 68a2eb0 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 1559 | case Attr::Overloadable: |
| 1560 | break; |
| 1561 | |
| 1562 | case Attr::Packed: |
| 1563 | Record.push_back(cast<PackedAttr>(Attr)->getAlignment()); |
| 1564 | break; |
| 1565 | |
| 1566 | case Attr::Pure: |
| 1567 | break; |
| 1568 | |
| 1569 | case Attr::Regparm: |
| 1570 | Record.push_back(cast<RegparmAttr>(Attr)->getNumParams()); |
| 1571 | break; |
| 1572 | |
| 1573 | case Attr::Section: |
| 1574 | AddString(cast<SectionAttr>(Attr)->getName(), Record); |
| 1575 | break; |
| 1576 | |
| 1577 | case Attr::StdCall: |
| 1578 | case Attr::TransparentUnion: |
| 1579 | case Attr::Unavailable: |
| 1580 | case Attr::Unused: |
| 1581 | case Attr::Used: |
| 1582 | break; |
| 1583 | |
| 1584 | case Attr::Visibility: |
| 1585 | // FIXME: stable encoding |
| 1586 | Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility()); |
| 1587 | break; |
| 1588 | |
| 1589 | case Attr::WarnUnusedResult: |
| 1590 | case Attr::Weak: |
| 1591 | case Attr::WeakImport: |
| 1592 | break; |
| 1593 | } |
| 1594 | } |
| 1595 | |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1596 | Stream.EmitRecord(pch::DECL_ATTR, Record); |
Douglas Gregor | 68a2eb0 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | void PCHWriter::AddString(const std::string &Str, RecordData &Record) { |
| 1600 | Record.push_back(Str.size()); |
| 1601 | Record.insert(Record.end(), Str.begin(), Str.end()); |
| 1602 | } |
| 1603 | |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 1604 | /// \brief Note that the identifier II occurs at the given offset |
| 1605 | /// within the identifier table. |
| 1606 | void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) { |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1607 | IdentifierOffsets[IdentifierIDs[II] - 1] = Offset; |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1610 | /// \brief Note that the selector Sel occurs at the given offset |
| 1611 | /// within the method pool/selector table. |
| 1612 | void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) { |
| 1613 | unsigned ID = SelectorIDs[Sel]; |
| 1614 | assert(ID && "Unknown selector"); |
| 1615 | SelectorOffsets[ID - 1] = Offset; |
| 1616 | } |
| 1617 | |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1618 | PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream) |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1619 | : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS), |
Douglas Gregor | 2512308 | 2009-04-22 22:34:57 +0000 | [diff] [blame] | 1620 | NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0), |
| 1621 | NumVisibleDeclContexts(0) { } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1622 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1623 | void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls) { |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1624 | using namespace llvm; |
| 1625 | |
Douglas Gregor | e778504 | 2009-04-20 15:53:59 +0000 | [diff] [blame] | 1626 | ASTContext &Context = SemaRef.Context; |
| 1627 | Preprocessor &PP = SemaRef.PP; |
| 1628 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1629 | // Emit the file header. |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1630 | Stream.Emit((unsigned)'C', 8); |
| 1631 | Stream.Emit((unsigned)'P', 8); |
| 1632 | Stream.Emit((unsigned)'C', 8); |
| 1633 | Stream.Emit((unsigned)'H', 8); |
Chris Lattner | b145b1e | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 1634 | |
| 1635 | WriteBlockInfoBlock(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1636 | |
| 1637 | // The translation unit is the first declaration we'll emit. |
| 1638 | DeclIDs[Context.getTranslationUnitDecl()] = 1; |
| 1639 | DeclsToEmit.push(Context.getTranslationUnitDecl()); |
| 1640 | |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 1641 | // Make sure that we emit IdentifierInfos (and any attached |
| 1642 | // declarations) for builtins. |
| 1643 | { |
| 1644 | IdentifierTable &Table = PP.getIdentifierTable(); |
| 1645 | llvm::SmallVector<const char *, 32> BuiltinNames; |
| 1646 | Context.BuiltinInfo.GetBuiltinNames(BuiltinNames, |
| 1647 | Context.getLangOptions().NoBuiltin); |
| 1648 | for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I) |
| 1649 | getIdentifierRef(&Table.get(BuiltinNames[I])); |
| 1650 | } |
| 1651 | |
Douglas Gregor | 4c0e86b | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 1652 | // Build a record containing all of the tentative definitions in |
| 1653 | // this header file. Generally, this record will be empty. |
| 1654 | RecordData TentativeDefinitions; |
| 1655 | for (llvm::DenseMap<DeclarationName, VarDecl *>::iterator |
| 1656 | TD = SemaRef.TentativeDefinitions.begin(), |
| 1657 | TDEnd = SemaRef.TentativeDefinitions.end(); |
| 1658 | TD != TDEnd; ++TD) |
| 1659 | AddDeclRef(TD->second, TentativeDefinitions); |
| 1660 | |
Douglas Gregor | 14c22f2 | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 1661 | // Build a record containing all of the locally-scoped external |
| 1662 | // declarations in this header file. Generally, this record will be |
| 1663 | // empty. |
| 1664 | RecordData LocallyScopedExternalDecls; |
| 1665 | for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator |
| 1666 | TD = SemaRef.LocallyScopedExternalDecls.begin(), |
| 1667 | TDEnd = SemaRef.LocallyScopedExternalDecls.end(); |
| 1668 | TD != TDEnd; ++TD) |
| 1669 | AddDeclRef(TD->second, LocallyScopedExternalDecls); |
| 1670 | |
Douglas Gregor | b81c170 | 2009-04-27 20:06:05 +0000 | [diff] [blame] | 1671 | // Build a record containing all of the ext_vector declarations. |
| 1672 | RecordData ExtVectorDecls; |
| 1673 | for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) |
| 1674 | AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls); |
| 1675 | |
| 1676 | // Build a record containing all of the Objective-C category |
| 1677 | // implementations. |
| 1678 | RecordData ObjCCategoryImpls; |
| 1679 | for (unsigned I = 0, N = SemaRef.ObjCCategoryImpls.size(); I != N; ++I) |
| 1680 | AddDeclRef(SemaRef.ObjCCategoryImpls[I], ObjCCategoryImpls); |
| 1681 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1682 | // Write the remaining PCH contents. |
Douglas Gregor | ad1de00 | 2009-04-18 05:55:16 +0000 | [diff] [blame] | 1683 | RecordData Record; |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1684 | Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4); |
Douglas Gregor | ab41e63 | 2009-04-27 22:23:34 +0000 | [diff] [blame] | 1685 | WriteMetadata(Context.Target); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1686 | WriteLanguageOptions(Context.getLangOptions()); |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1687 | if (StatCalls) |
| 1688 | WriteStatCache(*StatCalls); |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 1689 | WriteSourceManagerBlock(Context.getSourceManager(), PP); |
Chris Lattner | 0b1fb98 | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 1690 | WritePreprocessor(PP); |
Douglas Gregor | 366809a | 2009-04-26 03:49:13 +0000 | [diff] [blame] | 1691 | |
| 1692 | // Keep writing types and declarations until all types and |
| 1693 | // declarations have been written. |
| 1694 | do { |
| 1695 | if (!DeclsToEmit.empty()) |
| 1696 | WriteDeclsBlock(Context); |
| 1697 | if (!TypesToEmit.empty()) |
| 1698 | WriteTypesBlock(Context); |
| 1699 | } while (!(DeclsToEmit.empty() && TypesToEmit.empty())); |
| 1700 | |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1701 | WriteMethodPool(SemaRef); |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1702 | WriteIdentifierTable(PP); |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1703 | |
| 1704 | // Write the type offsets array |
| 1705 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 1706 | Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET)); |
| 1707 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types |
| 1708 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block |
| 1709 | unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev); |
| 1710 | Record.clear(); |
| 1711 | Record.push_back(pch::TYPE_OFFSET); |
| 1712 | Record.push_back(TypeOffsets.size()); |
| 1713 | Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, |
| 1714 | (const char *)&TypeOffsets.front(), |
Chris Lattner | c732f5a | 2009-04-27 18:24:17 +0000 | [diff] [blame] | 1715 | TypeOffsets.size() * sizeof(TypeOffsets[0])); |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1716 | |
| 1717 | // Write the declaration offsets array |
| 1718 | Abbrev = new BitCodeAbbrev(); |
| 1719 | Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET)); |
| 1720 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations |
| 1721 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block |
| 1722 | unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev); |
| 1723 | Record.clear(); |
| 1724 | Record.push_back(pch::DECL_OFFSET); |
| 1725 | Record.push_back(DeclOffsets.size()); |
| 1726 | Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, |
| 1727 | (const char *)&DeclOffsets.front(), |
Chris Lattner | c732f5a | 2009-04-27 18:24:17 +0000 | [diff] [blame] | 1728 | DeclOffsets.size() * sizeof(DeclOffsets[0])); |
Douglas Gregor | ad1de00 | 2009-04-18 05:55:16 +0000 | [diff] [blame] | 1729 | |
| 1730 | // Write the record of special types. |
| 1731 | Record.clear(); |
| 1732 | AddTypeRef(Context.getBuiltinVaListType(), Record); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 1733 | AddTypeRef(Context.getObjCIdType(), Record); |
| 1734 | AddTypeRef(Context.getObjCSelType(), Record); |
| 1735 | AddTypeRef(Context.getObjCProtoType(), Record); |
| 1736 | AddTypeRef(Context.getObjCClassType(), Record); |
| 1737 | AddTypeRef(Context.getRawCFConstantStringType(), Record); |
| 1738 | AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record); |
Douglas Gregor | ad1de00 | 2009-04-18 05:55:16 +0000 | [diff] [blame] | 1739 | Stream.EmitRecord(pch::SPECIAL_TYPES, Record); |
| 1740 | |
Douglas Gregor | 4c0e86b | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 1741 | // Write the record containing external, unnamed definitions. |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1742 | if (!ExternalDefinitions.empty()) |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1743 | Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions); |
Douglas Gregor | 4c0e86b | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 1744 | |
| 1745 | // Write the record containing tentative definitions. |
| 1746 | if (!TentativeDefinitions.empty()) |
| 1747 | Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions); |
Douglas Gregor | 14c22f2 | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 1748 | |
| 1749 | // Write the record containing locally-scoped external definitions. |
| 1750 | if (!LocallyScopedExternalDecls.empty()) |
| 1751 | Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS, |
| 1752 | LocallyScopedExternalDecls); |
Douglas Gregor | b81c170 | 2009-04-27 20:06:05 +0000 | [diff] [blame] | 1753 | |
| 1754 | // Write the record containing ext_vector type names. |
| 1755 | if (!ExtVectorDecls.empty()) |
| 1756 | Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls); |
| 1757 | |
| 1758 | // Write the record containing Objective-C category implementations. |
| 1759 | if (!ObjCCategoryImpls.empty()) |
| 1760 | Stream.EmitRecord(pch::OBJC_CATEGORY_IMPLEMENTATIONS, ObjCCategoryImpls); |
Douglas Gregor | 3e1af84 | 2009-04-17 22:13:46 +0000 | [diff] [blame] | 1761 | |
| 1762 | // Some simple statistics |
Douglas Gregor | ad1de00 | 2009-04-18 05:55:16 +0000 | [diff] [blame] | 1763 | Record.clear(); |
Douglas Gregor | 3e1af84 | 2009-04-17 22:13:46 +0000 | [diff] [blame] | 1764 | Record.push_back(NumStatements); |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1765 | Record.push_back(NumMacros); |
Douglas Gregor | 2512308 | 2009-04-22 22:34:57 +0000 | [diff] [blame] | 1766 | Record.push_back(NumLexicalDeclContexts); |
| 1767 | Record.push_back(NumVisibleDeclContexts); |
Douglas Gregor | 3e1af84 | 2009-04-17 22:13:46 +0000 | [diff] [blame] | 1768 | Stream.EmitRecord(pch::STATISTICS, Record); |
Douglas Gregor | c9490c0 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1769 | Stream.ExitBlock(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1770 | } |
| 1771 | |
| 1772 | void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) { |
| 1773 | Record.push_back(Loc.getRawEncoding()); |
| 1774 | } |
| 1775 | |
| 1776 | void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) { |
| 1777 | Record.push_back(Value.getBitWidth()); |
| 1778 | unsigned N = Value.getNumWords(); |
| 1779 | const uint64_t* Words = Value.getRawData(); |
| 1780 | for (unsigned I = 0; I != N; ++I) |
| 1781 | Record.push_back(Words[I]); |
| 1782 | } |
| 1783 | |
Douglas Gregor | 0a2b45e | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 1784 | void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) { |
| 1785 | Record.push_back(Value.isUnsigned()); |
| 1786 | AddAPInt(Value, Record); |
| 1787 | } |
| 1788 | |
Douglas Gregor | 17fc223 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 1789 | void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) { |
| 1790 | AddAPInt(Value.bitcastToAPInt(), Record); |
| 1791 | } |
| 1792 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1793 | void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) { |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 1794 | Record.push_back(getIdentifierRef(II)); |
| 1795 | } |
| 1796 | |
| 1797 | pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) { |
| 1798 | if (II == 0) |
| 1799 | return 0; |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1800 | |
| 1801 | pch::IdentID &ID = IdentifierIDs[II]; |
| 1802 | if (ID == 0) |
| 1803 | ID = IdentifierIDs.size(); |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 1804 | return ID; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 1807 | void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) { |
| 1808 | if (SelRef.getAsOpaquePtr() == 0) { |
| 1809 | Record.push_back(0); |
| 1810 | return; |
| 1811 | } |
| 1812 | |
| 1813 | pch::SelectorID &SID = SelectorIDs[SelRef]; |
| 1814 | if (SID == 0) { |
| 1815 | SID = SelectorIDs.size(); |
| 1816 | SelVector.push_back(SelRef); |
| 1817 | } |
| 1818 | Record.push_back(SID); |
| 1819 | } |
| 1820 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1821 | void PCHWriter::AddTypeRef(QualType T, RecordData &Record) { |
| 1822 | if (T.isNull()) { |
| 1823 | Record.push_back(pch::PREDEF_TYPE_NULL_ID); |
| 1824 | return; |
| 1825 | } |
| 1826 | |
| 1827 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) { |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1828 | pch::TypeID ID = 0; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1829 | switch (BT->getKind()) { |
| 1830 | case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break; |
| 1831 | case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break; |
| 1832 | case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break; |
| 1833 | case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break; |
| 1834 | case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break; |
| 1835 | case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break; |
| 1836 | case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break; |
| 1837 | case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 1838 | case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1839 | case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break; |
| 1840 | case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break; |
| 1841 | case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break; |
| 1842 | case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break; |
| 1843 | case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break; |
| 1844 | case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break; |
| 1845 | case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 1846 | case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1847 | case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break; |
| 1848 | case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break; |
| 1849 | case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break; |
| 1850 | case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break; |
| 1851 | case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break; |
| 1852 | } |
| 1853 | |
| 1854 | Record.push_back((ID << 3) | T.getCVRQualifiers()); |
| 1855 | return; |
| 1856 | } |
| 1857 | |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1858 | pch::TypeID &ID = TypeIDs[T.getTypePtr()]; |
Douglas Gregor | 366809a | 2009-04-26 03:49:13 +0000 | [diff] [blame] | 1859 | if (ID == 0) { |
| 1860 | // We haven't seen this type before. Assign it a new ID and put it |
| 1861 | // into the queu of types to emit. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1862 | ID = NextTypeID++; |
Douglas Gregor | 366809a | 2009-04-26 03:49:13 +0000 | [diff] [blame] | 1863 | TypesToEmit.push(T.getTypePtr()); |
| 1864 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1865 | |
| 1866 | // Encode the type qualifiers in the type reference. |
| 1867 | Record.push_back((ID << 3) | T.getCVRQualifiers()); |
| 1868 | } |
| 1869 | |
| 1870 | void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) { |
| 1871 | if (D == 0) { |
| 1872 | Record.push_back(0); |
| 1873 | return; |
| 1874 | } |
| 1875 | |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1876 | pch::DeclID &ID = DeclIDs[D]; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1877 | if (ID == 0) { |
| 1878 | // We haven't seen this declaration before. Give it a new ID and |
| 1879 | // enqueue it in the list of declarations to emit. |
| 1880 | ID = DeclIDs.size(); |
| 1881 | DeclsToEmit.push(const_cast<Decl *>(D)); |
| 1882 | } |
| 1883 | |
| 1884 | Record.push_back(ID); |
| 1885 | } |
| 1886 | |
Douglas Gregor | 3251ceb | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 1887 | pch::DeclID PCHWriter::getDeclID(const Decl *D) { |
| 1888 | if (D == 0) |
| 1889 | return 0; |
| 1890 | |
| 1891 | assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!"); |
| 1892 | return DeclIDs[D]; |
| 1893 | } |
| 1894 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1895 | void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) { |
Chris Lattner | ea5ce47 | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 1896 | // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1897 | Record.push_back(Name.getNameKind()); |
| 1898 | switch (Name.getNameKind()) { |
| 1899 | case DeclarationName::Identifier: |
| 1900 | AddIdentifierRef(Name.getAsIdentifierInfo(), Record); |
| 1901 | break; |
| 1902 | |
| 1903 | case DeclarationName::ObjCZeroArgSelector: |
| 1904 | case DeclarationName::ObjCOneArgSelector: |
| 1905 | case DeclarationName::ObjCMultiArgSelector: |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 1906 | AddSelectorRef(Name.getObjCSelector(), Record); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1907 | break; |
| 1908 | |
| 1909 | case DeclarationName::CXXConstructorName: |
| 1910 | case DeclarationName::CXXDestructorName: |
| 1911 | case DeclarationName::CXXConversionFunctionName: |
| 1912 | AddTypeRef(Name.getCXXNameType(), Record); |
| 1913 | break; |
| 1914 | |
| 1915 | case DeclarationName::CXXOperatorName: |
| 1916 | Record.push_back(Name.getCXXOverloadedOperator()); |
| 1917 | break; |
| 1918 | |
| 1919 | case DeclarationName::CXXUsingDirective: |
| 1920 | // No extra data to emit |
| 1921 | break; |
| 1922 | } |
| 1923 | } |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1924 | |