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