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