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