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