Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1 | //===--- ASTImporter.cpp - Importing ASTs from other Contexts ---*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the ASTImporter class which imports AST nodes from one |
| 11 | // context into another context. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #include "clang/AST/ASTImporter.h" |
| 15 | |
| 16 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 8852373 | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTDiagnostic.h" |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclVisitor.h" |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 20 | #include "clang/AST/TypeVisitor.h" |
Douglas Gregor | 8852373 | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 21 | #include "clang/Basic/FileManager.h" |
| 22 | #include "clang/Basic/SourceManager.h" |
| 23 | #include "llvm/Support/MemoryBuffer.h" |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace clang; |
| 26 | |
| 27 | namespace { |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 28 | class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>, |
| 29 | public DeclVisitor<ASTNodeImporter, Decl *> { |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 30 | ASTImporter &Importer; |
| 31 | |
| 32 | public: |
| 33 | explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { } |
| 34 | |
| 35 | using TypeVisitor<ASTNodeImporter, QualType>::Visit; |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 36 | using DeclVisitor<ASTNodeImporter, Decl *>::Visit; |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 37 | |
| 38 | // Importing types |
Douglas Gregor | 89cc9d6 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 39 | QualType VisitType(Type *T); |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 40 | QualType VisitBuiltinType(BuiltinType *T); |
| 41 | QualType VisitComplexType(ComplexType *T); |
| 42 | QualType VisitPointerType(PointerType *T); |
| 43 | QualType VisitBlockPointerType(BlockPointerType *T); |
| 44 | QualType VisitLValueReferenceType(LValueReferenceType *T); |
| 45 | QualType VisitRValueReferenceType(RValueReferenceType *T); |
| 46 | QualType VisitMemberPointerType(MemberPointerType *T); |
| 47 | QualType VisitConstantArrayType(ConstantArrayType *T); |
| 48 | QualType VisitIncompleteArrayType(IncompleteArrayType *T); |
| 49 | QualType VisitVariableArrayType(VariableArrayType *T); |
| 50 | // FIXME: DependentSizedArrayType |
| 51 | // FIXME: DependentSizedExtVectorType |
| 52 | QualType VisitVectorType(VectorType *T); |
| 53 | QualType VisitExtVectorType(ExtVectorType *T); |
| 54 | QualType VisitFunctionNoProtoType(FunctionNoProtoType *T); |
| 55 | QualType VisitFunctionProtoType(FunctionProtoType *T); |
| 56 | // FIXME: UnresolvedUsingType |
| 57 | QualType VisitTypedefType(TypedefType *T); |
| 58 | QualType VisitTypeOfExprType(TypeOfExprType *T); |
| 59 | // FIXME: DependentTypeOfExprType |
| 60 | QualType VisitTypeOfType(TypeOfType *T); |
| 61 | QualType VisitDecltypeType(DecltypeType *T); |
| 62 | // FIXME: DependentDecltypeType |
| 63 | QualType VisitRecordType(RecordType *T); |
| 64 | QualType VisitEnumType(EnumType *T); |
| 65 | QualType VisitElaboratedType(ElaboratedType *T); |
| 66 | // FIXME: TemplateTypeParmType |
| 67 | // FIXME: SubstTemplateTypeParmType |
| 68 | // FIXME: TemplateSpecializationType |
| 69 | QualType VisitQualifiedNameType(QualifiedNameType *T); |
| 70 | // FIXME: TypenameType |
| 71 | QualType VisitObjCInterfaceType(ObjCInterfaceType *T); |
| 72 | QualType VisitObjCObjectPointerType(ObjCObjectPointerType *T); |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 73 | |
| 74 | // Importing declarations |
Douglas Gregor | 89cc9d6 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 75 | Decl *VisitDecl(Decl *D); |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 76 | Decl *VisitVarDecl(VarDecl *D); |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 77 | }; |
| 78 | } |
| 79 | |
| 80 | //---------------------------------------------------------------------------- |
| 81 | // Import Types |
| 82 | //---------------------------------------------------------------------------- |
| 83 | |
Douglas Gregor | 89cc9d6 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 84 | QualType ASTNodeImporter::VisitType(Type *T) { |
| 85 | Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node) |
| 86 | << T->getTypeClassName(); |
| 87 | return QualType(); |
| 88 | } |
| 89 | |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 90 | QualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) { |
| 91 | switch (T->getKind()) { |
| 92 | case BuiltinType::Void: return Importer.getToContext().VoidTy; |
| 93 | case BuiltinType::Bool: return Importer.getToContext().BoolTy; |
| 94 | |
| 95 | case BuiltinType::Char_U: |
| 96 | // The context we're importing from has an unsigned 'char'. If we're |
| 97 | // importing into a context with a signed 'char', translate to |
| 98 | // 'unsigned char' instead. |
| 99 | if (Importer.getToContext().getLangOptions().CharIsSigned) |
| 100 | return Importer.getToContext().UnsignedCharTy; |
| 101 | |
| 102 | return Importer.getToContext().CharTy; |
| 103 | |
| 104 | case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy; |
| 105 | |
| 106 | case BuiltinType::Char16: |
| 107 | // FIXME: Make sure that the "to" context supports C++! |
| 108 | return Importer.getToContext().Char16Ty; |
| 109 | |
| 110 | case BuiltinType::Char32: |
| 111 | // FIXME: Make sure that the "to" context supports C++! |
| 112 | return Importer.getToContext().Char32Ty; |
| 113 | |
| 114 | case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy; |
| 115 | case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy; |
| 116 | case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy; |
| 117 | case BuiltinType::ULongLong: |
| 118 | return Importer.getToContext().UnsignedLongLongTy; |
| 119 | case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty; |
| 120 | |
| 121 | case BuiltinType::Char_S: |
| 122 | // The context we're importing from has an unsigned 'char'. If we're |
| 123 | // importing into a context with a signed 'char', translate to |
| 124 | // 'unsigned char' instead. |
| 125 | if (!Importer.getToContext().getLangOptions().CharIsSigned) |
| 126 | return Importer.getToContext().SignedCharTy; |
| 127 | |
| 128 | return Importer.getToContext().CharTy; |
| 129 | |
| 130 | case BuiltinType::SChar: return Importer.getToContext().SignedCharTy; |
| 131 | case BuiltinType::WChar: |
| 132 | // FIXME: If not in C++, shall we translate to the C equivalent of |
| 133 | // wchar_t? |
| 134 | return Importer.getToContext().WCharTy; |
| 135 | |
| 136 | case BuiltinType::Short : return Importer.getToContext().ShortTy; |
| 137 | case BuiltinType::Int : return Importer.getToContext().IntTy; |
| 138 | case BuiltinType::Long : return Importer.getToContext().LongTy; |
| 139 | case BuiltinType::LongLong : return Importer.getToContext().LongLongTy; |
| 140 | case BuiltinType::Int128 : return Importer.getToContext().Int128Ty; |
| 141 | case BuiltinType::Float: return Importer.getToContext().FloatTy; |
| 142 | case BuiltinType::Double: return Importer.getToContext().DoubleTy; |
| 143 | case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy; |
| 144 | |
| 145 | case BuiltinType::NullPtr: |
| 146 | // FIXME: Make sure that the "to" context supports C++0x! |
| 147 | return Importer.getToContext().NullPtrTy; |
| 148 | |
| 149 | case BuiltinType::Overload: return Importer.getToContext().OverloadTy; |
| 150 | case BuiltinType::Dependent: return Importer.getToContext().DependentTy; |
| 151 | case BuiltinType::UndeducedAuto: |
| 152 | // FIXME: Make sure that the "to" context supports C++0x! |
| 153 | return Importer.getToContext().UndeducedAutoTy; |
| 154 | |
| 155 | case BuiltinType::ObjCId: |
| 156 | // FIXME: Make sure that the "to" context supports Objective-C! |
| 157 | return Importer.getToContext().ObjCBuiltinIdTy; |
| 158 | |
| 159 | case BuiltinType::ObjCClass: |
| 160 | return Importer.getToContext().ObjCBuiltinClassTy; |
| 161 | |
| 162 | case BuiltinType::ObjCSel: |
| 163 | return Importer.getToContext().ObjCBuiltinSelTy; |
| 164 | } |
| 165 | |
| 166 | return QualType(); |
| 167 | } |
| 168 | |
| 169 | QualType ASTNodeImporter::VisitComplexType(ComplexType *T) { |
| 170 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 171 | if (ToElementType.isNull()) |
| 172 | return QualType(); |
| 173 | |
| 174 | return Importer.getToContext().getComplexType(ToElementType); |
| 175 | } |
| 176 | |
| 177 | QualType ASTNodeImporter::VisitPointerType(PointerType *T) { |
| 178 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 179 | if (ToPointeeType.isNull()) |
| 180 | return QualType(); |
| 181 | |
| 182 | return Importer.getToContext().getPointerType(ToPointeeType); |
| 183 | } |
| 184 | |
| 185 | QualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) { |
| 186 | // FIXME: Check for blocks support in "to" context. |
| 187 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 188 | if (ToPointeeType.isNull()) |
| 189 | return QualType(); |
| 190 | |
| 191 | return Importer.getToContext().getBlockPointerType(ToPointeeType); |
| 192 | } |
| 193 | |
| 194 | QualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) { |
| 195 | // FIXME: Check for C++ support in "to" context. |
| 196 | QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); |
| 197 | if (ToPointeeType.isNull()) |
| 198 | return QualType(); |
| 199 | |
| 200 | return Importer.getToContext().getLValueReferenceType(ToPointeeType); |
| 201 | } |
| 202 | |
| 203 | QualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) { |
| 204 | // FIXME: Check for C++0x support in "to" context. |
| 205 | QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); |
| 206 | if (ToPointeeType.isNull()) |
| 207 | return QualType(); |
| 208 | |
| 209 | return Importer.getToContext().getRValueReferenceType(ToPointeeType); |
| 210 | } |
| 211 | |
| 212 | QualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) { |
| 213 | // FIXME: Check for C++ support in "to" context. |
| 214 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 215 | if (ToPointeeType.isNull()) |
| 216 | return QualType(); |
| 217 | |
| 218 | QualType ClassType = Importer.Import(QualType(T->getClass(), 0)); |
| 219 | return Importer.getToContext().getMemberPointerType(ToPointeeType, |
| 220 | ClassType.getTypePtr()); |
| 221 | } |
| 222 | |
| 223 | QualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) { |
| 224 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 225 | if (ToElementType.isNull()) |
| 226 | return QualType(); |
| 227 | |
| 228 | return Importer.getToContext().getConstantArrayType(ToElementType, |
| 229 | T->getSize(), |
| 230 | T->getSizeModifier(), |
| 231 | T->getIndexTypeCVRQualifiers()); |
| 232 | } |
| 233 | |
| 234 | QualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) { |
| 235 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 236 | if (ToElementType.isNull()) |
| 237 | return QualType(); |
| 238 | |
| 239 | return Importer.getToContext().getIncompleteArrayType(ToElementType, |
| 240 | T->getSizeModifier(), |
| 241 | T->getIndexTypeCVRQualifiers()); |
| 242 | } |
| 243 | |
| 244 | QualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) { |
| 245 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 246 | if (ToElementType.isNull()) |
| 247 | return QualType(); |
| 248 | |
| 249 | Expr *Size = Importer.Import(T->getSizeExpr()); |
| 250 | if (!Size) |
| 251 | return QualType(); |
| 252 | |
| 253 | SourceRange Brackets = Importer.Import(T->getBracketsRange()); |
| 254 | return Importer.getToContext().getVariableArrayType(ToElementType, Size, |
| 255 | T->getSizeModifier(), |
| 256 | T->getIndexTypeCVRQualifiers(), |
| 257 | Brackets); |
| 258 | } |
| 259 | |
| 260 | QualType ASTNodeImporter::VisitVectorType(VectorType *T) { |
| 261 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 262 | if (ToElementType.isNull()) |
| 263 | return QualType(); |
| 264 | |
| 265 | return Importer.getToContext().getVectorType(ToElementType, |
| 266 | T->getNumElements(), |
| 267 | T->isAltiVec(), |
| 268 | T->isPixel()); |
| 269 | } |
| 270 | |
| 271 | QualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) { |
| 272 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 273 | if (ToElementType.isNull()) |
| 274 | return QualType(); |
| 275 | |
| 276 | return Importer.getToContext().getExtVectorType(ToElementType, |
| 277 | T->getNumElements()); |
| 278 | } |
| 279 | |
| 280 | QualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) { |
| 281 | // FIXME: What happens if we're importing a function without a prototype |
| 282 | // into C++? Should we make it variadic? |
| 283 | QualType ToResultType = Importer.Import(T->getResultType()); |
| 284 | if (ToResultType.isNull()) |
| 285 | return QualType(); |
| 286 | |
| 287 | return Importer.getToContext().getFunctionNoProtoType(ToResultType, |
| 288 | T->getNoReturnAttr(), |
| 289 | T->getCallConv()); |
| 290 | } |
| 291 | |
| 292 | QualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) { |
| 293 | QualType ToResultType = Importer.Import(T->getResultType()); |
| 294 | if (ToResultType.isNull()) |
| 295 | return QualType(); |
| 296 | |
| 297 | // Import argument types |
| 298 | llvm::SmallVector<QualType, 4> ArgTypes; |
| 299 | for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(), |
| 300 | AEnd = T->arg_type_end(); |
| 301 | A != AEnd; ++A) { |
| 302 | QualType ArgType = Importer.Import(*A); |
| 303 | if (ArgType.isNull()) |
| 304 | return QualType(); |
| 305 | ArgTypes.push_back(ArgType); |
| 306 | } |
| 307 | |
| 308 | // Import exception types |
| 309 | llvm::SmallVector<QualType, 4> ExceptionTypes; |
| 310 | for (FunctionProtoType::exception_iterator E = T->exception_begin(), |
| 311 | EEnd = T->exception_end(); |
| 312 | E != EEnd; ++E) { |
| 313 | QualType ExceptionType = Importer.Import(*E); |
| 314 | if (ExceptionType.isNull()) |
| 315 | return QualType(); |
| 316 | ExceptionTypes.push_back(ExceptionType); |
| 317 | } |
| 318 | |
| 319 | return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(), |
| 320 | ArgTypes.size(), |
| 321 | T->isVariadic(), |
| 322 | T->getTypeQuals(), |
| 323 | T->hasExceptionSpec(), |
| 324 | T->hasAnyExceptionSpec(), |
| 325 | ExceptionTypes.size(), |
| 326 | ExceptionTypes.data(), |
| 327 | T->getNoReturnAttr(), |
| 328 | T->getCallConv()); |
| 329 | } |
| 330 | |
| 331 | QualType ASTNodeImporter::VisitTypedefType(TypedefType *T) { |
| 332 | TypedefDecl *ToDecl |
| 333 | = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl())); |
| 334 | if (!ToDecl) |
| 335 | return QualType(); |
| 336 | |
| 337 | return Importer.getToContext().getTypeDeclType(ToDecl); |
| 338 | } |
| 339 | |
| 340 | QualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) { |
| 341 | Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); |
| 342 | if (!ToExpr) |
| 343 | return QualType(); |
| 344 | |
| 345 | return Importer.getToContext().getTypeOfExprType(ToExpr); |
| 346 | } |
| 347 | |
| 348 | QualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) { |
| 349 | QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); |
| 350 | if (ToUnderlyingType.isNull()) |
| 351 | return QualType(); |
| 352 | |
| 353 | return Importer.getToContext().getTypeOfType(ToUnderlyingType); |
| 354 | } |
| 355 | |
| 356 | QualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) { |
| 357 | Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); |
| 358 | if (!ToExpr) |
| 359 | return QualType(); |
| 360 | |
| 361 | return Importer.getToContext().getDecltypeType(ToExpr); |
| 362 | } |
| 363 | |
| 364 | QualType ASTNodeImporter::VisitRecordType(RecordType *T) { |
| 365 | RecordDecl *ToDecl |
| 366 | = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl())); |
| 367 | if (!ToDecl) |
| 368 | return QualType(); |
| 369 | |
| 370 | return Importer.getToContext().getTagDeclType(ToDecl); |
| 371 | } |
| 372 | |
| 373 | QualType ASTNodeImporter::VisitEnumType(EnumType *T) { |
| 374 | EnumDecl *ToDecl |
| 375 | = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl())); |
| 376 | if (!ToDecl) |
| 377 | return QualType(); |
| 378 | |
| 379 | return Importer.getToContext().getTagDeclType(ToDecl); |
| 380 | } |
| 381 | |
| 382 | QualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) { |
| 383 | QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); |
| 384 | if (ToUnderlyingType.isNull()) |
| 385 | return QualType(); |
| 386 | |
| 387 | return Importer.getToContext().getElaboratedType(ToUnderlyingType, |
| 388 | T->getTagKind()); |
| 389 | } |
| 390 | |
| 391 | QualType ASTNodeImporter::VisitQualifiedNameType(QualifiedNameType *T) { |
| 392 | NestedNameSpecifier *ToQualifier = Importer.Import(T->getQualifier()); |
| 393 | if (!ToQualifier) |
| 394 | return QualType(); |
| 395 | |
| 396 | QualType ToNamedType = Importer.Import(T->getNamedType()); |
| 397 | if (ToNamedType.isNull()) |
| 398 | return QualType(); |
| 399 | |
| 400 | return Importer.getToContext().getQualifiedNameType(ToQualifier, ToNamedType); |
| 401 | } |
| 402 | |
| 403 | QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) { |
| 404 | ObjCInterfaceDecl *Class |
| 405 | = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl())); |
| 406 | if (!Class) |
| 407 | return QualType(); |
| 408 | |
| 409 | llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 410 | for (ObjCInterfaceType::qual_iterator P = T->qual_begin(), |
| 411 | PEnd = T->qual_end(); |
| 412 | P != PEnd; ++P) { |
| 413 | ObjCProtocolDecl *Protocol |
| 414 | = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P)); |
| 415 | if (!Protocol) |
| 416 | return QualType(); |
| 417 | Protocols.push_back(Protocol); |
| 418 | } |
| 419 | |
| 420 | return Importer.getToContext().getObjCInterfaceType(Class, |
| 421 | Protocols.data(), |
| 422 | Protocols.size()); |
| 423 | } |
| 424 | |
| 425 | QualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) { |
| 426 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 427 | if (ToPointeeType.isNull()) |
| 428 | return QualType(); |
| 429 | |
| 430 | llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 431 | for (ObjCObjectPointerType::qual_iterator P = T->qual_begin(), |
| 432 | PEnd = T->qual_end(); |
| 433 | P != PEnd; ++P) { |
| 434 | ObjCProtocolDecl *Protocol |
| 435 | = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P)); |
| 436 | if (!Protocol) |
| 437 | return QualType(); |
| 438 | Protocols.push_back(Protocol); |
| 439 | } |
| 440 | |
| 441 | return Importer.getToContext().getObjCObjectPointerType(ToPointeeType, |
| 442 | Protocols.data(), |
| 443 | Protocols.size()); |
| 444 | } |
| 445 | |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 446 | //---------------------------------------------------------------------------- |
| 447 | // Import Declarations |
| 448 | //---------------------------------------------------------------------------- |
Douglas Gregor | 89cc9d6 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 449 | Decl *ASTNodeImporter::VisitDecl(Decl *D) { |
Douglas Gregor | 8852373 | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 450 | Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) |
Douglas Gregor | 89cc9d6 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 451 | << D->getDeclKindName(); |
| 452 | return 0; |
| 453 | } |
| 454 | |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 455 | Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) { |
| 456 | // Import the context of this declaration. |
| 457 | DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| 458 | if (!DC) |
| 459 | return 0; |
| 460 | |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 461 | DeclContext *LexicalDC = DC; |
| 462 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 463 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 464 | if (!LexicalDC) |
| 465 | return 0; |
| 466 | } |
| 467 | |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 468 | // Import the name of this declaration. |
| 469 | DeclarationName Name = Importer.Import(D->getDeclName()); |
| 470 | if (D->getDeclName() && !Name) |
| 471 | return 0; |
| 472 | |
| 473 | // Import the type of this declaration. |
| 474 | QualType T = Importer.Import(D->getType()); |
| 475 | if (T.isNull()) |
| 476 | return 0; |
| 477 | |
| 478 | // Import the location of this declaration. |
| 479 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 480 | |
| 481 | // Try to find a variable in our own ("to") context with the same name and |
| 482 | // in the same context as the variable we're importing. |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 483 | if (D->isFileVarDecl()) { |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 484 | VarDecl *MergeWithVar = 0; |
| 485 | llvm::SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 486 | unsigned IDNS = Decl::IDNS_Ordinary; |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 487 | for (DeclContext::lookup_result Lookup = DC->lookup(Name); |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 488 | Lookup.first != Lookup.second; |
| 489 | ++Lookup.first) { |
| 490 | if (!(*Lookup.first)->isInIdentifierNamespace(IDNS)) |
| 491 | continue; |
| 492 | |
| 493 | if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) { |
| 494 | // We have found a variable that we may need to merge with. Check it. |
| 495 | if (isExternalLinkage(FoundVar->getLinkage()) && |
| 496 | isExternalLinkage(D->getLinkage())) { |
| 497 | if (Importer.getToContext().typesAreCompatible(T, |
| 498 | FoundVar->getType())) { |
| 499 | MergeWithVar = FoundVar; |
| 500 | break; |
| 501 | } |
| 502 | |
Douglas Gregor | 0f962a8 | 2010-02-10 17:16:49 +0000 | [diff] [blame^] | 503 | if (const IncompleteArrayType *FoundArray |
| 504 | = Importer.getToContext().getAsIncompleteArrayType( |
| 505 | FoundVar->getType())) { |
| 506 | if (const ConstantArrayType *TArray |
| 507 | = Importer.getToContext().getAsConstantArrayType(T)) { |
| 508 | if (Importer.getToContext().typesAreCompatible( |
| 509 | TArray->getElementType(), |
| 510 | FoundArray->getElementType())) { |
| 511 | FoundVar->setType(T); |
| 512 | MergeWithVar = FoundVar; |
| 513 | break; |
| 514 | } |
| 515 | } |
| 516 | } else if (const IncompleteArrayType *TArray |
| 517 | = Importer.getToContext().getAsIncompleteArrayType(T)) { |
| 518 | if (const ConstantArrayType *FoundArray |
| 519 | = Importer.getToContext().getAsConstantArrayType( |
| 520 | FoundVar->getType())) { |
| 521 | if (Importer.getToContext().typesAreCompatible( |
| 522 | TArray->getElementType(), |
| 523 | FoundArray->getElementType())) { |
| 524 | MergeWithVar = FoundVar; |
| 525 | break; |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 530 | Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent) |
| 531 | << Name << T << FoundVar->getType(); |
| 532 | Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here) |
| 533 | << FoundVar->getType(); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | ConflictingDecls.push_back(*Lookup.first); |
| 538 | } |
| 539 | |
| 540 | if (MergeWithVar) { |
| 541 | // An equivalent variable with external linkage has been found. Link |
| 542 | // the two declarations, then merge them. |
| 543 | Importer.getImportedDecls()[D] = MergeWithVar; |
| 544 | |
| 545 | if (VarDecl *DDef = D->getDefinition()) { |
| 546 | if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) { |
| 547 | Importer.ToDiag(ExistingDef->getLocation(), |
| 548 | diag::err_odr_variable_multiple_def) |
| 549 | << Name; |
| 550 | Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here); |
| 551 | } else { |
| 552 | Expr *Init = Importer.Import(DDef->getInit()); |
| 553 | MergeWithVar->setInit(Importer.getToContext(), Init); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | return MergeWithVar; |
| 558 | } |
| 559 | |
| 560 | if (!ConflictingDecls.empty()) { |
| 561 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 562 | ConflictingDecls.data(), |
| 563 | ConflictingDecls.size()); |
| 564 | if (!Name) |
| 565 | return 0; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | TypeSourceInfo *TInfo = 0; |
| 570 | if (TypeSourceInfo *FromTInfo = D->getTypeSourceInfo()) { |
| 571 | TInfo = Importer.Import(FromTInfo); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 572 | #if 0 |
| 573 | // FIXME: Tolerate failures in translation type source |
| 574 | // information, at least until it is implemented. |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 575 | if (!TInfo) |
| 576 | return 0; |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 577 | #endif |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | // Create the imported variable. |
| 581 | VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc, |
| 582 | Name.getAsIdentifierInfo(), T, TInfo, |
| 583 | D->getStorageClass()); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 584 | ToVar->setLexicalDeclContext(LexicalDC); |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 585 | Importer.getImportedDecls()[D] = ToVar; |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 586 | LexicalDC->addDecl(ToVar); |
| 587 | |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 588 | // Merge the initializer. |
| 589 | // FIXME: Can we really import any initializer? Alternatively, we could force |
| 590 | // ourselves to import every declaration of a variable and then only use |
| 591 | // getInit() here. |
| 592 | ToVar->setInit(Importer.getToContext(), |
| 593 | Importer.Import(const_cast<Expr *>(D->getAnyInitializer()))); |
| 594 | |
| 595 | // FIXME: Other bits to merge? |
| 596 | |
| 597 | return ToVar; |
| 598 | } |
| 599 | |
Douglas Gregor | 8852373 | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 600 | ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager, |
| 601 | Diagnostic &ToDiags, |
| 602 | ASTContext &FromContext, FileManager &FromFileManager, |
| 603 | Diagnostic &FromDiags) |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 604 | : ToContext(ToContext), FromContext(FromContext), |
Douglas Gregor | 8852373 | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 605 | ToFileManager(ToFileManager), FromFileManager(FromFileManager), |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 606 | ToDiags(ToDiags), FromDiags(FromDiags) { |
| 607 | ImportedDecls[FromContext.getTranslationUnitDecl()] |
| 608 | = ToContext.getTranslationUnitDecl(); |
| 609 | } |
| 610 | |
| 611 | ASTImporter::~ASTImporter() { } |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 612 | |
| 613 | QualType ASTImporter::Import(QualType FromT) { |
| 614 | if (FromT.isNull()) |
| 615 | return QualType(); |
| 616 | |
Douglas Gregor | 169fba5 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 617 | // Check whether we've already imported this type. |
| 618 | llvm::DenseMap<Type *, Type *>::iterator Pos |
| 619 | = ImportedTypes.find(FromT.getTypePtr()); |
| 620 | if (Pos != ImportedTypes.end()) |
| 621 | return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers()); |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 622 | |
Douglas Gregor | 169fba5 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 623 | // Import the type |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 624 | ASTNodeImporter Importer(*this); |
| 625 | QualType ToT = Importer.Visit(FromT.getTypePtr()); |
| 626 | if (ToT.isNull()) |
| 627 | return ToT; |
| 628 | |
Douglas Gregor | 169fba5 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 629 | // Record the imported type. |
| 630 | ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr(); |
| 631 | |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 632 | return ToContext.getQualifiedType(ToT, FromT.getQualifiers()); |
| 633 | } |
| 634 | |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 635 | TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) { |
| 636 | // FIXME: Implement! |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | Decl *ASTImporter::Import(Decl *FromD) { |
| 641 | if (!FromD) |
| 642 | return 0; |
| 643 | |
| 644 | // Check whether we've already imported this declaration. |
| 645 | llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD); |
| 646 | if (Pos != ImportedDecls.end()) |
| 647 | return Pos->second; |
| 648 | |
| 649 | // Import the type |
| 650 | ASTNodeImporter Importer(*this); |
| 651 | Decl *ToD = Importer.Visit(FromD); |
| 652 | if (!ToD) |
| 653 | return 0; |
| 654 | |
| 655 | // Record the imported declaration. |
| 656 | ImportedDecls[FromD] = ToD; |
| 657 | return ToD; |
| 658 | } |
| 659 | |
| 660 | DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) { |
| 661 | if (!FromDC) |
| 662 | return FromDC; |
| 663 | |
| 664 | return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC))); |
| 665 | } |
| 666 | |
| 667 | Expr *ASTImporter::Import(Expr *FromE) { |
| 668 | if (!FromE) |
| 669 | return 0; |
| 670 | |
| 671 | return cast_or_null<Expr>(Import(cast<Stmt>(FromE))); |
| 672 | } |
| 673 | |
| 674 | Stmt *ASTImporter::Import(Stmt *FromS) { |
| 675 | if (!FromS) |
| 676 | return 0; |
| 677 | |
| 678 | // FIXME: Implement! |
| 679 | return 0; |
| 680 | } |
| 681 | |
| 682 | NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) { |
| 683 | if (!FromNNS) |
| 684 | return 0; |
| 685 | |
| 686 | // FIXME: Implement! |
| 687 | return 0; |
| 688 | } |
| 689 | |
| 690 | SourceLocation ASTImporter::Import(SourceLocation FromLoc) { |
| 691 | if (FromLoc.isInvalid()) |
| 692 | return SourceLocation(); |
| 693 | |
Douglas Gregor | 8852373 | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 694 | SourceManager &FromSM = FromContext.getSourceManager(); |
| 695 | |
| 696 | // For now, map everything down to its spelling location, so that we |
| 697 | // don't have to import macro instantiations. |
| 698 | // FIXME: Import macro instantiations! |
| 699 | FromLoc = FromSM.getSpellingLoc(FromLoc); |
| 700 | std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc); |
| 701 | SourceManager &ToSM = ToContext.getSourceManager(); |
| 702 | return ToSM.getLocForStartOfFile(Import(Decomposed.first)) |
| 703 | .getFileLocWithOffset(Decomposed.second); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | SourceRange ASTImporter::Import(SourceRange FromRange) { |
| 707 | return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd())); |
| 708 | } |
| 709 | |
Douglas Gregor | 8852373 | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 710 | FileID ASTImporter::Import(FileID FromID) { |
| 711 | llvm::DenseMap<unsigned, FileID>::iterator Pos |
| 712 | = ImportedFileIDs.find(FromID.getHashValue()); |
| 713 | if (Pos != ImportedFileIDs.end()) |
| 714 | return Pos->second; |
| 715 | |
| 716 | SourceManager &FromSM = FromContext.getSourceManager(); |
| 717 | SourceManager &ToSM = ToContext.getSourceManager(); |
| 718 | const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID); |
| 719 | assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet"); |
| 720 | |
| 721 | // Include location of this file. |
| 722 | SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc()); |
| 723 | |
| 724 | // Map the FileID for to the "to" source manager. |
| 725 | FileID ToID; |
| 726 | const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache(); |
| 727 | if (Cache->Entry) { |
| 728 | // FIXME: We probably want to use getVirtualFile(), so we don't hit the |
| 729 | // disk again |
| 730 | // FIXME: We definitely want to re-use the existing MemoryBuffer, rather |
| 731 | // than mmap the files several times. |
| 732 | const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName()); |
| 733 | ToID = ToSM.createFileID(Entry, ToIncludeLoc, |
| 734 | FromSLoc.getFile().getFileCharacteristic()); |
| 735 | } else { |
| 736 | // FIXME: We want to re-use the existing MemoryBuffer! |
| 737 | const llvm::MemoryBuffer *FromBuf = Cache->getBuffer(); |
| 738 | llvm::MemoryBuffer *ToBuf |
| 739 | = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBufferStart(), |
| 740 | FromBuf->getBufferEnd(), |
| 741 | FromBuf->getBufferIdentifier()); |
| 742 | ToID = ToSM.createFileIDForMemBuffer(ToBuf); |
| 743 | } |
| 744 | |
| 745 | |
| 746 | ImportedFileIDs[FromID.getHashValue()] = ToID; |
| 747 | return ToID; |
| 748 | } |
| 749 | |
Douglas Gregor | 1b2949d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 750 | DeclarationName ASTImporter::Import(DeclarationName FromName) { |
| 751 | if (!FromName) |
| 752 | return DeclarationName(); |
| 753 | |
| 754 | switch (FromName.getNameKind()) { |
| 755 | case DeclarationName::Identifier: |
| 756 | return Import(FromName.getAsIdentifierInfo()); |
| 757 | |
| 758 | case DeclarationName::ObjCZeroArgSelector: |
| 759 | case DeclarationName::ObjCOneArgSelector: |
| 760 | case DeclarationName::ObjCMultiArgSelector: |
| 761 | return Import(FromName.getObjCSelector()); |
| 762 | |
| 763 | case DeclarationName::CXXConstructorName: { |
| 764 | QualType T = Import(FromName.getCXXNameType()); |
| 765 | if (T.isNull()) |
| 766 | return DeclarationName(); |
| 767 | |
| 768 | return ToContext.DeclarationNames.getCXXConstructorName( |
| 769 | ToContext.getCanonicalType(T)); |
| 770 | } |
| 771 | |
| 772 | case DeclarationName::CXXDestructorName: { |
| 773 | QualType T = Import(FromName.getCXXNameType()); |
| 774 | if (T.isNull()) |
| 775 | return DeclarationName(); |
| 776 | |
| 777 | return ToContext.DeclarationNames.getCXXDestructorName( |
| 778 | ToContext.getCanonicalType(T)); |
| 779 | } |
| 780 | |
| 781 | case DeclarationName::CXXConversionFunctionName: { |
| 782 | QualType T = Import(FromName.getCXXNameType()); |
| 783 | if (T.isNull()) |
| 784 | return DeclarationName(); |
| 785 | |
| 786 | return ToContext.DeclarationNames.getCXXConversionFunctionName( |
| 787 | ToContext.getCanonicalType(T)); |
| 788 | } |
| 789 | |
| 790 | case DeclarationName::CXXOperatorName: |
| 791 | return ToContext.DeclarationNames.getCXXOperatorName( |
| 792 | FromName.getCXXOverloadedOperator()); |
| 793 | |
| 794 | case DeclarationName::CXXLiteralOperatorName: |
| 795 | return ToContext.DeclarationNames.getCXXLiteralOperatorName( |
| 796 | Import(FromName.getCXXLiteralIdentifier())); |
| 797 | |
| 798 | case DeclarationName::CXXUsingDirective: |
| 799 | // FIXME: STATICS! |
| 800 | return DeclarationName::getUsingDirectiveName(); |
| 801 | } |
| 802 | |
| 803 | // Silence bogus GCC warning |
| 804 | return DeclarationName(); |
| 805 | } |
| 806 | |
| 807 | IdentifierInfo *ASTImporter::Import(IdentifierInfo *FromId) { |
| 808 | if (!FromId) |
| 809 | return 0; |
| 810 | |
| 811 | return &ToContext.Idents.get(FromId->getName()); |
| 812 | } |
Douglas Gregor | 089459a | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 813 | |
| 814 | DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name, |
| 815 | DeclContext *DC, |
| 816 | unsigned IDNS, |
| 817 | NamedDecl **Decls, |
| 818 | unsigned NumDecls) { |
| 819 | return Name; |
| 820 | } |
| 821 | |
| 822 | DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) { |
| 823 | return ToDiags.Report(FullSourceLoc(Loc, ToContext.getSourceManager()), |
| 824 | DiagID); |
| 825 | } |
| 826 | |
| 827 | DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) { |
| 828 | return FromDiags.Report(FullSourceLoc(Loc, FromContext.getSourceManager()), |
| 829 | DiagID); |
| 830 | } |