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