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