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