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