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