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