Douglas Gregor | 96e578d | 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 | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTDiagnostic.h" |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclVisitor.h" |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 21 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 22 | #include "clang/AST/TypeVisitor.h" |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 23 | #include "clang/Basic/FileManager.h" |
| 24 | #include "clang/Basic/SourceManager.h" |
| 25 | #include "llvm/Support/MemoryBuffer.h" |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 26 | #include <deque> |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 27 | |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 28 | namespace clang { |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 29 | class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>, |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 30 | public DeclVisitor<ASTNodeImporter, Decl *>, |
| 31 | public StmtVisitor<ASTNodeImporter, Stmt *> { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 32 | ASTImporter &Importer; |
| 33 | |
| 34 | public: |
| 35 | explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { } |
| 36 | |
| 37 | using TypeVisitor<ASTNodeImporter, QualType>::Visit; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 38 | using DeclVisitor<ASTNodeImporter, Decl *>::Visit; |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 39 | using StmtVisitor<ASTNodeImporter, Stmt *>::Visit; |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 40 | |
| 41 | // Importing types |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 42 | QualType VisitType(const Type *T); |
| 43 | QualType VisitBuiltinType(const BuiltinType *T); |
| 44 | QualType VisitComplexType(const ComplexType *T); |
| 45 | QualType VisitPointerType(const PointerType *T); |
| 46 | QualType VisitBlockPointerType(const BlockPointerType *T); |
| 47 | QualType VisitLValueReferenceType(const LValueReferenceType *T); |
| 48 | QualType VisitRValueReferenceType(const RValueReferenceType *T); |
| 49 | QualType VisitMemberPointerType(const MemberPointerType *T); |
| 50 | QualType VisitConstantArrayType(const ConstantArrayType *T); |
| 51 | QualType VisitIncompleteArrayType(const IncompleteArrayType *T); |
| 52 | QualType VisitVariableArrayType(const VariableArrayType *T); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 53 | // FIXME: DependentSizedArrayType |
| 54 | // FIXME: DependentSizedExtVectorType |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 55 | QualType VisitVectorType(const VectorType *T); |
| 56 | QualType VisitExtVectorType(const ExtVectorType *T); |
| 57 | QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T); |
| 58 | QualType VisitFunctionProtoType(const FunctionProtoType *T); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 59 | // FIXME: UnresolvedUsingType |
Sean Callanan | da6df8a | 2011-08-11 16:56:07 +0000 | [diff] [blame] | 60 | QualType VisitParenType(const ParenType *T); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 61 | QualType VisitTypedefType(const TypedefType *T); |
| 62 | QualType VisitTypeOfExprType(const TypeOfExprType *T); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 63 | // FIXME: DependentTypeOfExprType |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 64 | QualType VisitTypeOfType(const TypeOfType *T); |
| 65 | QualType VisitDecltypeType(const DecltypeType *T); |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 66 | QualType VisitUnaryTransformType(const UnaryTransformType *T); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 67 | QualType VisitAutoType(const AutoType *T); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 68 | // FIXME: DependentDecltypeType |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 69 | QualType VisitRecordType(const RecordType *T); |
| 70 | QualType VisitEnumType(const EnumType *T); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 71 | // FIXME: TemplateTypeParmType |
| 72 | // FIXME: SubstTemplateTypeParmType |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 73 | QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T); |
| 74 | QualType VisitElaboratedType(const ElaboratedType *T); |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 75 | // FIXME: DependentNameType |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 76 | // FIXME: DependentTemplateSpecializationType |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 77 | QualType VisitObjCInterfaceType(const ObjCInterfaceType *T); |
| 78 | QualType VisitObjCObjectType(const ObjCObjectType *T); |
| 79 | QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 80 | |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 81 | // Importing declarations |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 82 | bool ImportDeclParts(NamedDecl *D, DeclContext *&DC, |
| 83 | DeclContext *&LexicalDC, DeclarationName &Name, |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 84 | SourceLocation &Loc); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 85 | void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = 0); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 86 | void ImportDeclarationNameLoc(const DeclarationNameInfo &From, |
| 87 | DeclarationNameInfo& To); |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 88 | void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false); |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 89 | |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 90 | /// \brief What we should import from the definition. |
| 91 | enum ImportDefinitionKind { |
| 92 | /// \brief Import the default subset of the definition, which might be |
| 93 | /// nothing (if minimal import is set) or might be everything (if minimal |
| 94 | /// import is not set). |
| 95 | IDK_Default, |
| 96 | /// \brief Import everything. |
| 97 | IDK_Everything, |
| 98 | /// \brief Import only the bare bones needed to establish a valid |
| 99 | /// DeclContext. |
| 100 | IDK_Basic |
| 101 | }; |
| 102 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 103 | bool shouldForceImportDeclContext(ImportDefinitionKind IDK) { |
| 104 | return IDK == IDK_Everything || |
| 105 | (IDK == IDK_Default && !Importer.isMinimalImport()); |
| 106 | } |
| 107 | |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 108 | bool ImportDefinition(RecordDecl *From, RecordDecl *To, |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 109 | ImportDefinitionKind Kind = IDK_Default); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 110 | bool ImportDefinition(EnumDecl *From, EnumDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 111 | ImportDefinitionKind Kind = IDK_Default); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 112 | bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 113 | ImportDefinitionKind Kind = IDK_Default); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 114 | bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 115 | ImportDefinitionKind Kind = IDK_Default); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 116 | TemplateParameterList *ImportTemplateParameterList( |
| 117 | TemplateParameterList *Params); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 118 | TemplateArgument ImportTemplateArgument(const TemplateArgument &From); |
| 119 | bool ImportTemplateArguments(const TemplateArgument *FromArgs, |
| 120 | unsigned NumFromArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 121 | SmallVectorImpl<TemplateArgument> &ToArgs); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 122 | bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 123 | bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 124 | bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To); |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 125 | Decl *VisitDecl(Decl *D); |
Sean Callanan | 6519827 | 2011-11-17 23:20:56 +0000 | [diff] [blame] | 126 | Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D); |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 127 | Decl *VisitNamespaceDecl(NamespaceDecl *D); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 128 | Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias); |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 129 | Decl *VisitTypedefDecl(TypedefDecl *D); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 130 | Decl *VisitTypeAliasDecl(TypeAliasDecl *D); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 131 | Decl *VisitEnumDecl(EnumDecl *D); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 132 | Decl *VisitRecordDecl(RecordDecl *D); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 133 | Decl *VisitEnumConstantDecl(EnumConstantDecl *D); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 134 | Decl *VisitFunctionDecl(FunctionDecl *D); |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 135 | Decl *VisitCXXMethodDecl(CXXMethodDecl *D); |
| 136 | Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D); |
| 137 | Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D); |
| 138 | Decl *VisitCXXConversionDecl(CXXConversionDecl *D); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 139 | Decl *VisitFieldDecl(FieldDecl *D); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 140 | Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D); |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 141 | Decl *VisitObjCIvarDecl(ObjCIvarDecl *D); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 142 | Decl *VisitVarDecl(VarDecl *D); |
Douglas Gregor | 8b228d7 | 2010-02-17 21:22:52 +0000 | [diff] [blame] | 143 | Decl *VisitImplicitParamDecl(ImplicitParamDecl *D); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 144 | Decl *VisitParmVarDecl(ParmVarDecl *D); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 145 | Decl *VisitObjCMethodDecl(ObjCMethodDecl *D); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 146 | Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D); |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 147 | Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D); |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 148 | Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 149 | Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 150 | Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 151 | Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 152 | Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 153 | Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); |
| 154 | Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); |
| 155 | Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); |
| 156 | Decl *VisitClassTemplateDecl(ClassTemplateDecl *D); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 157 | Decl *VisitClassTemplateSpecializationDecl( |
| 158 | ClassTemplateSpecializationDecl *D); |
Douglas Gregor | 06537af | 2010-02-18 02:04:09 +0000 | [diff] [blame] | 159 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 160 | // Importing statements |
| 161 | Stmt *VisitStmt(Stmt *S); |
| 162 | |
| 163 | // Importing expressions |
| 164 | Expr *VisitExpr(Expr *E); |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 165 | Expr *VisitDeclRefExpr(DeclRefExpr *E); |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 166 | Expr *VisitIntegerLiteral(IntegerLiteral *E); |
Douglas Gregor | 623421d | 2010-02-18 02:21:22 +0000 | [diff] [blame] | 167 | Expr *VisitCharacterLiteral(CharacterLiteral *E); |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 168 | Expr *VisitParenExpr(ParenExpr *E); |
| 169 | Expr *VisitUnaryOperator(UnaryOperator *E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 170 | Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 171 | Expr *VisitBinaryOperator(BinaryOperator *E); |
| 172 | Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 173 | Expr *VisitImplicitCastExpr(ImplicitCastExpr *E); |
Douglas Gregor | 5481d32 | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 174 | Expr *VisitCStyleCastExpr(CStyleCastExpr *E); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 175 | }; |
| 176 | } |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 177 | using namespace clang; |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 178 | |
| 179 | //---------------------------------------------------------------------------- |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 180 | // Structural Equivalence |
| 181 | //---------------------------------------------------------------------------- |
| 182 | |
| 183 | namespace { |
| 184 | struct StructuralEquivalenceContext { |
| 185 | /// \brief AST contexts for which we are checking structural equivalence. |
| 186 | ASTContext &C1, &C2; |
| 187 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 188 | /// \brief The set of "tentative" equivalences between two canonical |
| 189 | /// declarations, mapping from a declaration in the first context to the |
| 190 | /// declaration in the second context that we believe to be equivalent. |
| 191 | llvm::DenseMap<Decl *, Decl *> TentativeEquivalences; |
| 192 | |
| 193 | /// \brief Queue of declarations in the first context whose equivalence |
| 194 | /// with a declaration in the second context still needs to be verified. |
| 195 | std::deque<Decl *> DeclsToCheck; |
| 196 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 197 | /// \brief Declaration (from, to) pairs that are known not to be equivalent |
| 198 | /// (which we have already complained about). |
| 199 | llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls; |
| 200 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 201 | /// \brief Whether we're being strict about the spelling of types when |
| 202 | /// unifying two types. |
| 203 | bool StrictTypeSpelling; |
| 204 | |
| 205 | StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2, |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 206 | llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls, |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 207 | bool StrictTypeSpelling = false) |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 208 | : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls), |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 209 | StrictTypeSpelling(StrictTypeSpelling) { } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 210 | |
| 211 | /// \brief Determine whether the two declarations are structurally |
| 212 | /// equivalent. |
| 213 | bool IsStructurallyEquivalent(Decl *D1, Decl *D2); |
| 214 | |
| 215 | /// \brief Determine whether the two types are structurally equivalent. |
| 216 | bool IsStructurallyEquivalent(QualType T1, QualType T2); |
| 217 | |
| 218 | private: |
| 219 | /// \brief Finish checking all of the structural equivalences. |
| 220 | /// |
| 221 | /// \returns true if an error occurred, false otherwise. |
| 222 | bool Finish(); |
| 223 | |
| 224 | public: |
| 225 | DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 226 | return C1.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 230 | return C2.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 231 | } |
| 232 | }; |
| 233 | } |
| 234 | |
| 235 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 236 | QualType T1, QualType T2); |
| 237 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 238 | Decl *D1, Decl *D2); |
| 239 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 240 | /// \brief Determine structural equivalence of two expressions. |
| 241 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 242 | Expr *E1, Expr *E2) { |
| 243 | if (!E1 || !E2) |
| 244 | return E1 == E2; |
| 245 | |
| 246 | // FIXME: Actually perform a structural comparison! |
| 247 | return true; |
| 248 | } |
| 249 | |
| 250 | /// \brief Determine whether two identifiers are equivalent. |
| 251 | static bool IsStructurallyEquivalent(const IdentifierInfo *Name1, |
| 252 | const IdentifierInfo *Name2) { |
| 253 | if (!Name1 || !Name2) |
| 254 | return Name1 == Name2; |
| 255 | |
| 256 | return Name1->getName() == Name2->getName(); |
| 257 | } |
| 258 | |
| 259 | /// \brief Determine whether two nested-name-specifiers are equivalent. |
| 260 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 261 | NestedNameSpecifier *NNS1, |
| 262 | NestedNameSpecifier *NNS2) { |
| 263 | // FIXME: Implement! |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | /// \brief Determine whether two template arguments are equivalent. |
| 268 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 269 | const TemplateArgument &Arg1, |
| 270 | const TemplateArgument &Arg2) { |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 271 | if (Arg1.getKind() != Arg2.getKind()) |
| 272 | return false; |
| 273 | |
| 274 | switch (Arg1.getKind()) { |
| 275 | case TemplateArgument::Null: |
| 276 | return true; |
| 277 | |
| 278 | case TemplateArgument::Type: |
| 279 | return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType()); |
| 280 | |
| 281 | case TemplateArgument::Integral: |
| 282 | if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(), |
| 283 | Arg2.getIntegralType())) |
| 284 | return false; |
| 285 | |
Eric Christopher | 6dcc376 | 2012-07-15 00:23:57 +0000 | [diff] [blame] | 286 | return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral()); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 287 | |
| 288 | case TemplateArgument::Declaration: |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 289 | if (!Arg1.getAsDecl() || !Arg2.getAsDecl()) |
| 290 | return !Arg1.getAsDecl() && !Arg2.getAsDecl(); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 291 | return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl()); |
| 292 | |
| 293 | case TemplateArgument::Template: |
| 294 | return IsStructurallyEquivalent(Context, |
| 295 | Arg1.getAsTemplate(), |
| 296 | Arg2.getAsTemplate()); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 297 | |
| 298 | case TemplateArgument::TemplateExpansion: |
| 299 | return IsStructurallyEquivalent(Context, |
| 300 | Arg1.getAsTemplateOrTemplatePattern(), |
| 301 | Arg2.getAsTemplateOrTemplatePattern()); |
| 302 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 303 | case TemplateArgument::Expression: |
| 304 | return IsStructurallyEquivalent(Context, |
| 305 | Arg1.getAsExpr(), Arg2.getAsExpr()); |
| 306 | |
| 307 | case TemplateArgument::Pack: |
| 308 | if (Arg1.pack_size() != Arg2.pack_size()) |
| 309 | return false; |
| 310 | |
| 311 | for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I) |
| 312 | if (!IsStructurallyEquivalent(Context, |
| 313 | Arg1.pack_begin()[I], |
| 314 | Arg2.pack_begin()[I])) |
| 315 | return false; |
| 316 | |
| 317 | return true; |
| 318 | } |
| 319 | |
| 320 | llvm_unreachable("Invalid template argument kind"); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /// \brief Determine structural equivalence for the common part of array |
| 324 | /// types. |
| 325 | static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 326 | const ArrayType *Array1, |
| 327 | const ArrayType *Array2) { |
| 328 | if (!IsStructurallyEquivalent(Context, |
| 329 | Array1->getElementType(), |
| 330 | Array2->getElementType())) |
| 331 | return false; |
| 332 | if (Array1->getSizeModifier() != Array2->getSizeModifier()) |
| 333 | return false; |
| 334 | if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers()) |
| 335 | return false; |
| 336 | |
| 337 | return true; |
| 338 | } |
| 339 | |
| 340 | /// \brief Determine structural equivalence of two types. |
| 341 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 342 | QualType T1, QualType T2) { |
| 343 | if (T1.isNull() || T2.isNull()) |
| 344 | return T1.isNull() && T2.isNull(); |
| 345 | |
| 346 | if (!Context.StrictTypeSpelling) { |
| 347 | // We aren't being strict about token-to-token equivalence of types, |
| 348 | // so map down to the canonical type. |
| 349 | T1 = Context.C1.getCanonicalType(T1); |
| 350 | T2 = Context.C2.getCanonicalType(T2); |
| 351 | } |
| 352 | |
| 353 | if (T1.getQualifiers() != T2.getQualifiers()) |
| 354 | return false; |
| 355 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 356 | Type::TypeClass TC = T1->getTypeClass(); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 357 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 358 | if (T1->getTypeClass() != T2->getTypeClass()) { |
| 359 | // Compare function types with prototypes vs. without prototypes as if |
| 360 | // both did not have prototypes. |
| 361 | if (T1->getTypeClass() == Type::FunctionProto && |
| 362 | T2->getTypeClass() == Type::FunctionNoProto) |
| 363 | TC = Type::FunctionNoProto; |
| 364 | else if (T1->getTypeClass() == Type::FunctionNoProto && |
| 365 | T2->getTypeClass() == Type::FunctionProto) |
| 366 | TC = Type::FunctionNoProto; |
| 367 | else |
| 368 | return false; |
| 369 | } |
| 370 | |
| 371 | switch (TC) { |
| 372 | case Type::Builtin: |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 373 | // FIXME: Deal with Char_S/Char_U. |
| 374 | if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind()) |
| 375 | return false; |
| 376 | break; |
| 377 | |
| 378 | case Type::Complex: |
| 379 | if (!IsStructurallyEquivalent(Context, |
| 380 | cast<ComplexType>(T1)->getElementType(), |
| 381 | cast<ComplexType>(T2)->getElementType())) |
| 382 | return false; |
| 383 | break; |
| 384 | |
| 385 | case Type::Pointer: |
| 386 | if (!IsStructurallyEquivalent(Context, |
| 387 | cast<PointerType>(T1)->getPointeeType(), |
| 388 | cast<PointerType>(T2)->getPointeeType())) |
| 389 | return false; |
| 390 | break; |
| 391 | |
| 392 | case Type::BlockPointer: |
| 393 | if (!IsStructurallyEquivalent(Context, |
| 394 | cast<BlockPointerType>(T1)->getPointeeType(), |
| 395 | cast<BlockPointerType>(T2)->getPointeeType())) |
| 396 | return false; |
| 397 | break; |
| 398 | |
| 399 | case Type::LValueReference: |
| 400 | case Type::RValueReference: { |
| 401 | const ReferenceType *Ref1 = cast<ReferenceType>(T1); |
| 402 | const ReferenceType *Ref2 = cast<ReferenceType>(T2); |
| 403 | if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue()) |
| 404 | return false; |
| 405 | if (Ref1->isInnerRef() != Ref2->isInnerRef()) |
| 406 | return false; |
| 407 | if (!IsStructurallyEquivalent(Context, |
| 408 | Ref1->getPointeeTypeAsWritten(), |
| 409 | Ref2->getPointeeTypeAsWritten())) |
| 410 | return false; |
| 411 | break; |
| 412 | } |
| 413 | |
| 414 | case Type::MemberPointer: { |
| 415 | const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1); |
| 416 | const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2); |
| 417 | if (!IsStructurallyEquivalent(Context, |
| 418 | MemPtr1->getPointeeType(), |
| 419 | MemPtr2->getPointeeType())) |
| 420 | return false; |
| 421 | if (!IsStructurallyEquivalent(Context, |
| 422 | QualType(MemPtr1->getClass(), 0), |
| 423 | QualType(MemPtr2->getClass(), 0))) |
| 424 | return false; |
| 425 | break; |
| 426 | } |
| 427 | |
| 428 | case Type::ConstantArray: { |
| 429 | const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1); |
| 430 | const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2); |
Eric Christopher | 6dcc376 | 2012-07-15 00:23:57 +0000 | [diff] [blame] | 431 | if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 432 | return false; |
| 433 | |
| 434 | if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) |
| 435 | return false; |
| 436 | break; |
| 437 | } |
| 438 | |
| 439 | case Type::IncompleteArray: |
| 440 | if (!IsArrayStructurallyEquivalent(Context, |
| 441 | cast<ArrayType>(T1), |
| 442 | cast<ArrayType>(T2))) |
| 443 | return false; |
| 444 | break; |
| 445 | |
| 446 | case Type::VariableArray: { |
| 447 | const VariableArrayType *Array1 = cast<VariableArrayType>(T1); |
| 448 | const VariableArrayType *Array2 = cast<VariableArrayType>(T2); |
| 449 | if (!IsStructurallyEquivalent(Context, |
| 450 | Array1->getSizeExpr(), Array2->getSizeExpr())) |
| 451 | return false; |
| 452 | |
| 453 | if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) |
| 454 | return false; |
| 455 | |
| 456 | break; |
| 457 | } |
| 458 | |
| 459 | case Type::DependentSizedArray: { |
| 460 | const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1); |
| 461 | const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2); |
| 462 | if (!IsStructurallyEquivalent(Context, |
| 463 | Array1->getSizeExpr(), Array2->getSizeExpr())) |
| 464 | return false; |
| 465 | |
| 466 | if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) |
| 467 | return false; |
| 468 | |
| 469 | break; |
| 470 | } |
| 471 | |
| 472 | case Type::DependentSizedExtVector: { |
| 473 | const DependentSizedExtVectorType *Vec1 |
| 474 | = cast<DependentSizedExtVectorType>(T1); |
| 475 | const DependentSizedExtVectorType *Vec2 |
| 476 | = cast<DependentSizedExtVectorType>(T2); |
| 477 | if (!IsStructurallyEquivalent(Context, |
| 478 | Vec1->getSizeExpr(), Vec2->getSizeExpr())) |
| 479 | return false; |
| 480 | if (!IsStructurallyEquivalent(Context, |
| 481 | Vec1->getElementType(), |
| 482 | Vec2->getElementType())) |
| 483 | return false; |
| 484 | break; |
| 485 | } |
| 486 | |
| 487 | case Type::Vector: |
| 488 | case Type::ExtVector: { |
| 489 | const VectorType *Vec1 = cast<VectorType>(T1); |
| 490 | const VectorType *Vec2 = cast<VectorType>(T2); |
| 491 | if (!IsStructurallyEquivalent(Context, |
| 492 | Vec1->getElementType(), |
| 493 | Vec2->getElementType())) |
| 494 | return false; |
| 495 | if (Vec1->getNumElements() != Vec2->getNumElements()) |
| 496 | return false; |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 497 | if (Vec1->getVectorKind() != Vec2->getVectorKind()) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 498 | return false; |
Douglas Gregor | 01cc437 | 2010-02-19 01:36:36 +0000 | [diff] [blame] | 499 | break; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | case Type::FunctionProto: { |
| 503 | const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1); |
| 504 | const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2); |
| 505 | if (Proto1->getNumArgs() != Proto2->getNumArgs()) |
| 506 | return false; |
| 507 | for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) { |
| 508 | if (!IsStructurallyEquivalent(Context, |
| 509 | Proto1->getArgType(I), |
| 510 | Proto2->getArgType(I))) |
| 511 | return false; |
| 512 | } |
| 513 | if (Proto1->isVariadic() != Proto2->isVariadic()) |
| 514 | return false; |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 515 | if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType()) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 516 | return false; |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 517 | if (Proto1->getExceptionSpecType() == EST_Dynamic) { |
| 518 | if (Proto1->getNumExceptions() != Proto2->getNumExceptions()) |
| 519 | return false; |
| 520 | for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) { |
| 521 | if (!IsStructurallyEquivalent(Context, |
| 522 | Proto1->getExceptionType(I), |
| 523 | Proto2->getExceptionType(I))) |
| 524 | return false; |
| 525 | } |
| 526 | } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 527 | if (!IsStructurallyEquivalent(Context, |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 528 | Proto1->getNoexceptExpr(), |
| 529 | Proto2->getNoexceptExpr())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 530 | return false; |
| 531 | } |
| 532 | if (Proto1->getTypeQuals() != Proto2->getTypeQuals()) |
| 533 | return false; |
| 534 | |
| 535 | // Fall through to check the bits common with FunctionNoProtoType. |
| 536 | } |
| 537 | |
| 538 | case Type::FunctionNoProto: { |
| 539 | const FunctionType *Function1 = cast<FunctionType>(T1); |
| 540 | const FunctionType *Function2 = cast<FunctionType>(T2); |
| 541 | if (!IsStructurallyEquivalent(Context, |
| 542 | Function1->getResultType(), |
| 543 | Function2->getResultType())) |
| 544 | return false; |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 545 | if (Function1->getExtInfo() != Function2->getExtInfo()) |
| 546 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 547 | break; |
| 548 | } |
| 549 | |
| 550 | case Type::UnresolvedUsing: |
| 551 | if (!IsStructurallyEquivalent(Context, |
| 552 | cast<UnresolvedUsingType>(T1)->getDecl(), |
| 553 | cast<UnresolvedUsingType>(T2)->getDecl())) |
| 554 | return false; |
| 555 | |
| 556 | break; |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 557 | |
| 558 | case Type::Attributed: |
| 559 | if (!IsStructurallyEquivalent(Context, |
| 560 | cast<AttributedType>(T1)->getModifiedType(), |
| 561 | cast<AttributedType>(T2)->getModifiedType())) |
| 562 | return false; |
| 563 | if (!IsStructurallyEquivalent(Context, |
| 564 | cast<AttributedType>(T1)->getEquivalentType(), |
| 565 | cast<AttributedType>(T2)->getEquivalentType())) |
| 566 | return false; |
| 567 | break; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 568 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 569 | case Type::Paren: |
| 570 | if (!IsStructurallyEquivalent(Context, |
| 571 | cast<ParenType>(T1)->getInnerType(), |
| 572 | cast<ParenType>(T2)->getInnerType())) |
| 573 | return false; |
| 574 | break; |
| 575 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 576 | case Type::Typedef: |
| 577 | if (!IsStructurallyEquivalent(Context, |
| 578 | cast<TypedefType>(T1)->getDecl(), |
| 579 | cast<TypedefType>(T2)->getDecl())) |
| 580 | return false; |
| 581 | break; |
| 582 | |
| 583 | case Type::TypeOfExpr: |
| 584 | if (!IsStructurallyEquivalent(Context, |
| 585 | cast<TypeOfExprType>(T1)->getUnderlyingExpr(), |
| 586 | cast<TypeOfExprType>(T2)->getUnderlyingExpr())) |
| 587 | return false; |
| 588 | break; |
| 589 | |
| 590 | case Type::TypeOf: |
| 591 | if (!IsStructurallyEquivalent(Context, |
| 592 | cast<TypeOfType>(T1)->getUnderlyingType(), |
| 593 | cast<TypeOfType>(T2)->getUnderlyingType())) |
| 594 | return false; |
| 595 | break; |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 596 | |
| 597 | case Type::UnaryTransform: |
| 598 | if (!IsStructurallyEquivalent(Context, |
| 599 | cast<UnaryTransformType>(T1)->getUnderlyingType(), |
| 600 | cast<UnaryTransformType>(T1)->getUnderlyingType())) |
| 601 | return false; |
| 602 | break; |
| 603 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 604 | case Type::Decltype: |
| 605 | if (!IsStructurallyEquivalent(Context, |
| 606 | cast<DecltypeType>(T1)->getUnderlyingExpr(), |
| 607 | cast<DecltypeType>(T2)->getUnderlyingExpr())) |
| 608 | return false; |
| 609 | break; |
| 610 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 611 | case Type::Auto: |
| 612 | if (!IsStructurallyEquivalent(Context, |
| 613 | cast<AutoType>(T1)->getDeducedType(), |
| 614 | cast<AutoType>(T2)->getDeducedType())) |
| 615 | return false; |
| 616 | break; |
| 617 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 618 | case Type::Record: |
| 619 | case Type::Enum: |
| 620 | if (!IsStructurallyEquivalent(Context, |
| 621 | cast<TagType>(T1)->getDecl(), |
| 622 | cast<TagType>(T2)->getDecl())) |
| 623 | return false; |
| 624 | break; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 625 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 626 | case Type::TemplateTypeParm: { |
| 627 | const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1); |
| 628 | const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2); |
| 629 | if (Parm1->getDepth() != Parm2->getDepth()) |
| 630 | return false; |
| 631 | if (Parm1->getIndex() != Parm2->getIndex()) |
| 632 | return false; |
| 633 | if (Parm1->isParameterPack() != Parm2->isParameterPack()) |
| 634 | return false; |
| 635 | |
| 636 | // Names of template type parameters are never significant. |
| 637 | break; |
| 638 | } |
| 639 | |
| 640 | case Type::SubstTemplateTypeParm: { |
| 641 | const SubstTemplateTypeParmType *Subst1 |
| 642 | = cast<SubstTemplateTypeParmType>(T1); |
| 643 | const SubstTemplateTypeParmType *Subst2 |
| 644 | = cast<SubstTemplateTypeParmType>(T2); |
| 645 | if (!IsStructurallyEquivalent(Context, |
| 646 | QualType(Subst1->getReplacedParameter(), 0), |
| 647 | QualType(Subst2->getReplacedParameter(), 0))) |
| 648 | return false; |
| 649 | if (!IsStructurallyEquivalent(Context, |
| 650 | Subst1->getReplacementType(), |
| 651 | Subst2->getReplacementType())) |
| 652 | return false; |
| 653 | break; |
| 654 | } |
| 655 | |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 656 | case Type::SubstTemplateTypeParmPack: { |
| 657 | const SubstTemplateTypeParmPackType *Subst1 |
| 658 | = cast<SubstTemplateTypeParmPackType>(T1); |
| 659 | const SubstTemplateTypeParmPackType *Subst2 |
| 660 | = cast<SubstTemplateTypeParmPackType>(T2); |
| 661 | if (!IsStructurallyEquivalent(Context, |
| 662 | QualType(Subst1->getReplacedParameter(), 0), |
| 663 | QualType(Subst2->getReplacedParameter(), 0))) |
| 664 | return false; |
| 665 | if (!IsStructurallyEquivalent(Context, |
| 666 | Subst1->getArgumentPack(), |
| 667 | Subst2->getArgumentPack())) |
| 668 | return false; |
| 669 | break; |
| 670 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 671 | case Type::TemplateSpecialization: { |
| 672 | const TemplateSpecializationType *Spec1 |
| 673 | = cast<TemplateSpecializationType>(T1); |
| 674 | const TemplateSpecializationType *Spec2 |
| 675 | = cast<TemplateSpecializationType>(T2); |
| 676 | if (!IsStructurallyEquivalent(Context, |
| 677 | Spec1->getTemplateName(), |
| 678 | Spec2->getTemplateName())) |
| 679 | return false; |
| 680 | if (Spec1->getNumArgs() != Spec2->getNumArgs()) |
| 681 | return false; |
| 682 | for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) { |
| 683 | if (!IsStructurallyEquivalent(Context, |
| 684 | Spec1->getArg(I), Spec2->getArg(I))) |
| 685 | return false; |
| 686 | } |
| 687 | break; |
| 688 | } |
| 689 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 690 | case Type::Elaborated: { |
| 691 | const ElaboratedType *Elab1 = cast<ElaboratedType>(T1); |
| 692 | const ElaboratedType *Elab2 = cast<ElaboratedType>(T2); |
| 693 | // CHECKME: what if a keyword is ETK_None or ETK_typename ? |
| 694 | if (Elab1->getKeyword() != Elab2->getKeyword()) |
| 695 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 696 | if (!IsStructurallyEquivalent(Context, |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 697 | Elab1->getQualifier(), |
| 698 | Elab2->getQualifier())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 699 | return false; |
| 700 | if (!IsStructurallyEquivalent(Context, |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 701 | Elab1->getNamedType(), |
| 702 | Elab2->getNamedType())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 703 | return false; |
| 704 | break; |
| 705 | } |
| 706 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 707 | case Type::InjectedClassName: { |
| 708 | const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1); |
| 709 | const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2); |
| 710 | if (!IsStructurallyEquivalent(Context, |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 711 | Inj1->getInjectedSpecializationType(), |
| 712 | Inj2->getInjectedSpecializationType())) |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 713 | return false; |
| 714 | break; |
| 715 | } |
| 716 | |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 717 | case Type::DependentName: { |
| 718 | const DependentNameType *Typename1 = cast<DependentNameType>(T1); |
| 719 | const DependentNameType *Typename2 = cast<DependentNameType>(T2); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 720 | if (!IsStructurallyEquivalent(Context, |
| 721 | Typename1->getQualifier(), |
| 722 | Typename2->getQualifier())) |
| 723 | return false; |
| 724 | if (!IsStructurallyEquivalent(Typename1->getIdentifier(), |
| 725 | Typename2->getIdentifier())) |
| 726 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 727 | |
| 728 | break; |
| 729 | } |
| 730 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 731 | case Type::DependentTemplateSpecialization: { |
| 732 | const DependentTemplateSpecializationType *Spec1 = |
| 733 | cast<DependentTemplateSpecializationType>(T1); |
| 734 | const DependentTemplateSpecializationType *Spec2 = |
| 735 | cast<DependentTemplateSpecializationType>(T2); |
| 736 | if (!IsStructurallyEquivalent(Context, |
| 737 | Spec1->getQualifier(), |
| 738 | Spec2->getQualifier())) |
| 739 | return false; |
| 740 | if (!IsStructurallyEquivalent(Spec1->getIdentifier(), |
| 741 | Spec2->getIdentifier())) |
| 742 | return false; |
| 743 | if (Spec1->getNumArgs() != Spec2->getNumArgs()) |
| 744 | return false; |
| 745 | for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) { |
| 746 | if (!IsStructurallyEquivalent(Context, |
| 747 | Spec1->getArg(I), Spec2->getArg(I))) |
| 748 | return false; |
| 749 | } |
| 750 | break; |
| 751 | } |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 752 | |
| 753 | case Type::PackExpansion: |
| 754 | if (!IsStructurallyEquivalent(Context, |
| 755 | cast<PackExpansionType>(T1)->getPattern(), |
| 756 | cast<PackExpansionType>(T2)->getPattern())) |
| 757 | return false; |
| 758 | break; |
| 759 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 760 | case Type::ObjCInterface: { |
| 761 | const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1); |
| 762 | const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2); |
| 763 | if (!IsStructurallyEquivalent(Context, |
| 764 | Iface1->getDecl(), Iface2->getDecl())) |
| 765 | return false; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 766 | break; |
| 767 | } |
| 768 | |
| 769 | case Type::ObjCObject: { |
| 770 | const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1); |
| 771 | const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2); |
| 772 | if (!IsStructurallyEquivalent(Context, |
| 773 | Obj1->getBaseType(), |
| 774 | Obj2->getBaseType())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 775 | return false; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 776 | if (Obj1->getNumProtocols() != Obj2->getNumProtocols()) |
| 777 | return false; |
| 778 | for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 779 | if (!IsStructurallyEquivalent(Context, |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 780 | Obj1->getProtocol(I), |
| 781 | Obj2->getProtocol(I))) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 782 | return false; |
| 783 | } |
| 784 | break; |
| 785 | } |
| 786 | |
| 787 | case Type::ObjCObjectPointer: { |
| 788 | const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1); |
| 789 | const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2); |
| 790 | if (!IsStructurallyEquivalent(Context, |
| 791 | Ptr1->getPointeeType(), |
| 792 | Ptr2->getPointeeType())) |
| 793 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 794 | break; |
| 795 | } |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 796 | |
| 797 | case Type::Atomic: { |
| 798 | if (!IsStructurallyEquivalent(Context, |
| 799 | cast<AtomicType>(T1)->getValueType(), |
| 800 | cast<AtomicType>(T2)->getValueType())) |
| 801 | return false; |
| 802 | break; |
| 803 | } |
| 804 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 805 | } // end switch |
| 806 | |
| 807 | return true; |
| 808 | } |
| 809 | |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 810 | /// \brief Determine structural equivalence of two fields. |
| 811 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 812 | FieldDecl *Field1, FieldDecl *Field2) { |
| 813 | RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext()); |
| 814 | |
| 815 | if (!IsStructurallyEquivalent(Context, |
| 816 | Field1->getType(), Field2->getType())) { |
| 817 | Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 818 | << Context.C2.getTypeDeclType(Owner2); |
| 819 | Context.Diag2(Field2->getLocation(), diag::note_odr_field) |
| 820 | << Field2->getDeclName() << Field2->getType(); |
| 821 | Context.Diag1(Field1->getLocation(), diag::note_odr_field) |
| 822 | << Field1->getDeclName() << Field1->getType(); |
| 823 | return false; |
| 824 | } |
| 825 | |
| 826 | if (Field1->isBitField() != Field2->isBitField()) { |
| 827 | Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 828 | << Context.C2.getTypeDeclType(Owner2); |
| 829 | if (Field1->isBitField()) { |
| 830 | Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field) |
| 831 | << Field1->getDeclName() << Field1->getType() |
| 832 | << Field1->getBitWidthValue(Context.C1); |
| 833 | Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field) |
| 834 | << Field2->getDeclName(); |
| 835 | } else { |
| 836 | Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field) |
| 837 | << Field2->getDeclName() << Field2->getType() |
| 838 | << Field2->getBitWidthValue(Context.C2); |
| 839 | Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field) |
| 840 | << Field1->getDeclName(); |
| 841 | } |
| 842 | return false; |
| 843 | } |
| 844 | |
| 845 | if (Field1->isBitField()) { |
| 846 | // Make sure that the bit-fields are the same length. |
| 847 | unsigned Bits1 = Field1->getBitWidthValue(Context.C1); |
| 848 | unsigned Bits2 = Field2->getBitWidthValue(Context.C2); |
| 849 | |
| 850 | if (Bits1 != Bits2) { |
| 851 | Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 852 | << Context.C2.getTypeDeclType(Owner2); |
| 853 | Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field) |
| 854 | << Field2->getDeclName() << Field2->getType() << Bits2; |
| 855 | Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field) |
| 856 | << Field1->getDeclName() << Field1->getType() << Bits1; |
| 857 | return false; |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | return true; |
| 862 | } |
| 863 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 864 | /// \brief Determine structural equivalence of two records. |
| 865 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 866 | RecordDecl *D1, RecordDecl *D2) { |
| 867 | if (D1->isUnion() != D2->isUnion()) { |
| 868 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 869 | << Context.C2.getTypeDeclType(D2); |
| 870 | Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here) |
| 871 | << D1->getDeclName() << (unsigned)D1->getTagKind(); |
| 872 | return false; |
| 873 | } |
| 874 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 875 | // If both declarations are class template specializations, we know |
| 876 | // the ODR applies, so check the template and template arguments. |
| 877 | ClassTemplateSpecializationDecl *Spec1 |
| 878 | = dyn_cast<ClassTemplateSpecializationDecl>(D1); |
| 879 | ClassTemplateSpecializationDecl *Spec2 |
| 880 | = dyn_cast<ClassTemplateSpecializationDecl>(D2); |
| 881 | if (Spec1 && Spec2) { |
| 882 | // Check that the specialized templates are the same. |
| 883 | if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(), |
| 884 | Spec2->getSpecializedTemplate())) |
| 885 | return false; |
| 886 | |
| 887 | // Check that the template arguments are the same. |
| 888 | if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size()) |
| 889 | return false; |
| 890 | |
| 891 | for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I) |
| 892 | if (!IsStructurallyEquivalent(Context, |
| 893 | Spec1->getTemplateArgs().get(I), |
| 894 | Spec2->getTemplateArgs().get(I))) |
| 895 | return false; |
| 896 | } |
| 897 | // If one is a class template specialization and the other is not, these |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 898 | // structures are different. |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 899 | else if (Spec1 || Spec2) |
| 900 | return false; |
| 901 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 902 | // Compare the definitions of these two records. If either or both are |
| 903 | // incomplete, we assume that they are equivalent. |
| 904 | D1 = D1->getDefinition(); |
| 905 | D2 = D2->getDefinition(); |
| 906 | if (!D1 || !D2) |
| 907 | return true; |
| 908 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 909 | if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) { |
| 910 | if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) { |
| 911 | if (D1CXX->getNumBases() != D2CXX->getNumBases()) { |
| 912 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 913 | << Context.C2.getTypeDeclType(D2); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 914 | Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases) |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 915 | << D2CXX->getNumBases(); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 916 | Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases) |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 917 | << D1CXX->getNumBases(); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 918 | return false; |
| 919 | } |
| 920 | |
| 921 | // Check the base classes. |
| 922 | for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(), |
| 923 | BaseEnd1 = D1CXX->bases_end(), |
| 924 | Base2 = D2CXX->bases_begin(); |
| 925 | Base1 != BaseEnd1; |
| 926 | ++Base1, ++Base2) { |
| 927 | if (!IsStructurallyEquivalent(Context, |
| 928 | Base1->getType(), Base2->getType())) { |
| 929 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 930 | << Context.C2.getTypeDeclType(D2); |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 931 | Context.Diag2(Base2->getLocStart(), diag::note_odr_base) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 932 | << Base2->getType() |
| 933 | << Base2->getSourceRange(); |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 934 | Context.Diag1(Base1->getLocStart(), diag::note_odr_base) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 935 | << Base1->getType() |
| 936 | << Base1->getSourceRange(); |
| 937 | return false; |
| 938 | } |
| 939 | |
| 940 | // Check virtual vs. non-virtual inheritance mismatch. |
| 941 | if (Base1->isVirtual() != Base2->isVirtual()) { |
| 942 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 943 | << Context.C2.getTypeDeclType(D2); |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 944 | Context.Diag2(Base2->getLocStart(), |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 945 | diag::note_odr_virtual_base) |
| 946 | << Base2->isVirtual() << Base2->getSourceRange(); |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 947 | Context.Diag1(Base1->getLocStart(), diag::note_odr_base) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 948 | << Base1->isVirtual() |
| 949 | << Base1->getSourceRange(); |
| 950 | return false; |
| 951 | } |
| 952 | } |
| 953 | } else if (D1CXX->getNumBases() > 0) { |
| 954 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 955 | << Context.C2.getTypeDeclType(D2); |
| 956 | const CXXBaseSpecifier *Base1 = D1CXX->bases_begin(); |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 957 | Context.Diag1(Base1->getLocStart(), diag::note_odr_base) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 958 | << Base1->getType() |
| 959 | << Base1->getSourceRange(); |
| 960 | Context.Diag2(D2->getLocation(), diag::note_odr_missing_base); |
| 961 | return false; |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | // Check the fields for consistency. |
Dmitri Gribenko | 898cff0 | 2012-05-19 17:17:26 +0000 | [diff] [blame] | 966 | RecordDecl::field_iterator Field2 = D2->field_begin(), |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 967 | Field2End = D2->field_end(); |
Dmitri Gribenko | 898cff0 | 2012-05-19 17:17:26 +0000 | [diff] [blame] | 968 | for (RecordDecl::field_iterator Field1 = D1->field_begin(), |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 969 | Field1End = D1->field_end(); |
| 970 | Field1 != Field1End; |
| 971 | ++Field1, ++Field2) { |
| 972 | if (Field2 == Field2End) { |
| 973 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 974 | << Context.C2.getTypeDeclType(D2); |
| 975 | Context.Diag1(Field1->getLocation(), diag::note_odr_field) |
| 976 | << Field1->getDeclName() << Field1->getType(); |
| 977 | Context.Diag2(D2->getLocation(), diag::note_odr_missing_field); |
| 978 | return false; |
| 979 | } |
| 980 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 981 | if (!IsStructurallyEquivalent(Context, *Field1, *Field2)) |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 982 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | if (Field2 != Field2End) { |
| 986 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 987 | << Context.C2.getTypeDeclType(D2); |
| 988 | Context.Diag2(Field2->getLocation(), diag::note_odr_field) |
| 989 | << Field2->getDeclName() << Field2->getType(); |
| 990 | Context.Diag1(D1->getLocation(), diag::note_odr_missing_field); |
| 991 | return false; |
| 992 | } |
| 993 | |
| 994 | return true; |
| 995 | } |
| 996 | |
| 997 | /// \brief Determine structural equivalence of two enums. |
| 998 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 999 | EnumDecl *D1, EnumDecl *D2) { |
| 1000 | EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(), |
| 1001 | EC2End = D2->enumerator_end(); |
| 1002 | for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(), |
| 1003 | EC1End = D1->enumerator_end(); |
| 1004 | EC1 != EC1End; ++EC1, ++EC2) { |
| 1005 | if (EC2 == EC2End) { |
| 1006 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1007 | << Context.C2.getTypeDeclType(D2); |
| 1008 | Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator) |
| 1009 | << EC1->getDeclName() |
| 1010 | << EC1->getInitVal().toString(10); |
| 1011 | Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator); |
| 1012 | return false; |
| 1013 | } |
| 1014 | |
| 1015 | llvm::APSInt Val1 = EC1->getInitVal(); |
| 1016 | llvm::APSInt Val2 = EC2->getInitVal(); |
Eric Christopher | 6dcc376 | 2012-07-15 00:23:57 +0000 | [diff] [blame] | 1017 | if (!llvm::APSInt::isSameValue(Val1, Val2) || |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1018 | !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) { |
| 1019 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1020 | << Context.C2.getTypeDeclType(D2); |
| 1021 | Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator) |
| 1022 | << EC2->getDeclName() |
| 1023 | << EC2->getInitVal().toString(10); |
| 1024 | Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator) |
| 1025 | << EC1->getDeclName() |
| 1026 | << EC1->getInitVal().toString(10); |
| 1027 | return false; |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | if (EC2 != EC2End) { |
| 1032 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1033 | << Context.C2.getTypeDeclType(D2); |
| 1034 | Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator) |
| 1035 | << EC2->getDeclName() |
| 1036 | << EC2->getInitVal().toString(10); |
| 1037 | Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator); |
| 1038 | return false; |
| 1039 | } |
| 1040 | |
| 1041 | return true; |
| 1042 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1043 | |
| 1044 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1045 | TemplateParameterList *Params1, |
| 1046 | TemplateParameterList *Params2) { |
| 1047 | if (Params1->size() != Params2->size()) { |
| 1048 | Context.Diag2(Params2->getTemplateLoc(), |
| 1049 | diag::err_odr_different_num_template_parameters) |
| 1050 | << Params1->size() << Params2->size(); |
| 1051 | Context.Diag1(Params1->getTemplateLoc(), |
| 1052 | diag::note_odr_template_parameter_list); |
| 1053 | return false; |
| 1054 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1055 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1056 | for (unsigned I = 0, N = Params1->size(); I != N; ++I) { |
| 1057 | if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) { |
| 1058 | Context.Diag2(Params2->getParam(I)->getLocation(), |
| 1059 | diag::err_odr_different_template_parameter_kind); |
| 1060 | Context.Diag1(Params1->getParam(I)->getLocation(), |
| 1061 | diag::note_odr_template_parameter_here); |
| 1062 | return false; |
| 1063 | } |
| 1064 | |
| 1065 | if (!Context.IsStructurallyEquivalent(Params1->getParam(I), |
| 1066 | Params2->getParam(I))) { |
| 1067 | |
| 1068 | return false; |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | return true; |
| 1073 | } |
| 1074 | |
| 1075 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1076 | TemplateTypeParmDecl *D1, |
| 1077 | TemplateTypeParmDecl *D2) { |
| 1078 | if (D1->isParameterPack() != D2->isParameterPack()) { |
| 1079 | Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack) |
| 1080 | << D2->isParameterPack(); |
| 1081 | Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) |
| 1082 | << D1->isParameterPack(); |
| 1083 | return false; |
| 1084 | } |
| 1085 | |
| 1086 | return true; |
| 1087 | } |
| 1088 | |
| 1089 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1090 | NonTypeTemplateParmDecl *D1, |
| 1091 | NonTypeTemplateParmDecl *D2) { |
| 1092 | // FIXME: Enable once we have variadic templates. |
| 1093 | #if 0 |
| 1094 | if (D1->isParameterPack() != D2->isParameterPack()) { |
| 1095 | Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack) |
| 1096 | << D2->isParameterPack(); |
| 1097 | Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) |
| 1098 | << D1->isParameterPack(); |
| 1099 | return false; |
| 1100 | } |
| 1101 | #endif |
| 1102 | |
| 1103 | // Check types. |
| 1104 | if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) { |
| 1105 | Context.Diag2(D2->getLocation(), |
| 1106 | diag::err_odr_non_type_parameter_type_inconsistent) |
| 1107 | << D2->getType() << D1->getType(); |
| 1108 | Context.Diag1(D1->getLocation(), diag::note_odr_value_here) |
| 1109 | << D1->getType(); |
| 1110 | return false; |
| 1111 | } |
| 1112 | |
| 1113 | return true; |
| 1114 | } |
| 1115 | |
| 1116 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1117 | TemplateTemplateParmDecl *D1, |
| 1118 | TemplateTemplateParmDecl *D2) { |
| 1119 | // FIXME: Enable once we have variadic templates. |
| 1120 | #if 0 |
| 1121 | if (D1->isParameterPack() != D2->isParameterPack()) { |
| 1122 | Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack) |
| 1123 | << D2->isParameterPack(); |
| 1124 | Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) |
| 1125 | << D1->isParameterPack(); |
| 1126 | return false; |
| 1127 | } |
| 1128 | #endif |
| 1129 | |
| 1130 | // Check template parameter lists. |
| 1131 | return IsStructurallyEquivalent(Context, D1->getTemplateParameters(), |
| 1132 | D2->getTemplateParameters()); |
| 1133 | } |
| 1134 | |
| 1135 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1136 | ClassTemplateDecl *D1, |
| 1137 | ClassTemplateDecl *D2) { |
| 1138 | // Check template parameters. |
| 1139 | if (!IsStructurallyEquivalent(Context, |
| 1140 | D1->getTemplateParameters(), |
| 1141 | D2->getTemplateParameters())) |
| 1142 | return false; |
| 1143 | |
| 1144 | // Check the templated declaration. |
| 1145 | return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(), |
| 1146 | D2->getTemplatedDecl()); |
| 1147 | } |
| 1148 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1149 | /// \brief Determine structural equivalence of two declarations. |
| 1150 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1151 | Decl *D1, Decl *D2) { |
| 1152 | // FIXME: Check for known structural equivalences via a callback of some sort. |
| 1153 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1154 | // Check whether we already know that these two declarations are not |
| 1155 | // structurally equivalent. |
| 1156 | if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(), |
| 1157 | D2->getCanonicalDecl()))) |
| 1158 | return false; |
| 1159 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1160 | // Determine whether we've already produced a tentative equivalence for D1. |
| 1161 | Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()]; |
| 1162 | if (EquivToD1) |
| 1163 | return EquivToD1 == D2->getCanonicalDecl(); |
| 1164 | |
| 1165 | // Produce a tentative equivalence D1 <-> D2, which will be checked later. |
| 1166 | EquivToD1 = D2->getCanonicalDecl(); |
| 1167 | Context.DeclsToCheck.push_back(D1->getCanonicalDecl()); |
| 1168 | return true; |
| 1169 | } |
| 1170 | |
| 1171 | bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1, |
| 1172 | Decl *D2) { |
| 1173 | if (!::IsStructurallyEquivalent(*this, D1, D2)) |
| 1174 | return false; |
| 1175 | |
| 1176 | return !Finish(); |
| 1177 | } |
| 1178 | |
| 1179 | bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1, |
| 1180 | QualType T2) { |
| 1181 | if (!::IsStructurallyEquivalent(*this, T1, T2)) |
| 1182 | return false; |
| 1183 | |
| 1184 | return !Finish(); |
| 1185 | } |
| 1186 | |
| 1187 | bool StructuralEquivalenceContext::Finish() { |
| 1188 | while (!DeclsToCheck.empty()) { |
| 1189 | // Check the next declaration. |
| 1190 | Decl *D1 = DeclsToCheck.front(); |
| 1191 | DeclsToCheck.pop_front(); |
| 1192 | |
| 1193 | Decl *D2 = TentativeEquivalences[D1]; |
| 1194 | assert(D2 && "Unrecorded tentative equivalence?"); |
| 1195 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1196 | bool Equivalent = true; |
| 1197 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1198 | // FIXME: Switch on all declaration kinds. For now, we're just going to |
| 1199 | // check the obvious ones. |
| 1200 | if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) { |
| 1201 | if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) { |
| 1202 | // Check for equivalent structure names. |
| 1203 | IdentifierInfo *Name1 = Record1->getIdentifier(); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1204 | if (!Name1 && Record1->getTypedefNameForAnonDecl()) |
| 1205 | Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier(); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1206 | IdentifierInfo *Name2 = Record2->getIdentifier(); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1207 | if (!Name2 && Record2->getTypedefNameForAnonDecl()) |
| 1208 | Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier(); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1209 | if (!::IsStructurallyEquivalent(Name1, Name2) || |
| 1210 | !::IsStructurallyEquivalent(*this, Record1, Record2)) |
| 1211 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1212 | } else { |
| 1213 | // Record/non-record mismatch. |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1214 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1215 | } |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1216 | } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1217 | if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) { |
| 1218 | // Check for equivalent enum names. |
| 1219 | IdentifierInfo *Name1 = Enum1->getIdentifier(); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1220 | if (!Name1 && Enum1->getTypedefNameForAnonDecl()) |
| 1221 | Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier(); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1222 | IdentifierInfo *Name2 = Enum2->getIdentifier(); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1223 | if (!Name2 && Enum2->getTypedefNameForAnonDecl()) |
| 1224 | Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier(); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1225 | if (!::IsStructurallyEquivalent(Name1, Name2) || |
| 1226 | !::IsStructurallyEquivalent(*this, Enum1, Enum2)) |
| 1227 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1228 | } else { |
| 1229 | // Enum/non-enum mismatch |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1230 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1231 | } |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1232 | } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) { |
| 1233 | if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1234 | if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(), |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1235 | Typedef2->getIdentifier()) || |
| 1236 | !::IsStructurallyEquivalent(*this, |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1237 | Typedef1->getUnderlyingType(), |
| 1238 | Typedef2->getUnderlyingType())) |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1239 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1240 | } else { |
| 1241 | // Typedef/non-typedef mismatch. |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1242 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1243 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1244 | } else if (ClassTemplateDecl *ClassTemplate1 |
| 1245 | = dyn_cast<ClassTemplateDecl>(D1)) { |
| 1246 | if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) { |
| 1247 | if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(), |
| 1248 | ClassTemplate2->getIdentifier()) || |
| 1249 | !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2)) |
| 1250 | Equivalent = false; |
| 1251 | } else { |
| 1252 | // Class template/non-class-template mismatch. |
| 1253 | Equivalent = false; |
| 1254 | } |
| 1255 | } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) { |
| 1256 | if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) { |
| 1257 | if (!::IsStructurallyEquivalent(*this, TTP1, TTP2)) |
| 1258 | Equivalent = false; |
| 1259 | } else { |
| 1260 | // Kind mismatch. |
| 1261 | Equivalent = false; |
| 1262 | } |
| 1263 | } else if (NonTypeTemplateParmDecl *NTTP1 |
| 1264 | = dyn_cast<NonTypeTemplateParmDecl>(D1)) { |
| 1265 | if (NonTypeTemplateParmDecl *NTTP2 |
| 1266 | = dyn_cast<NonTypeTemplateParmDecl>(D2)) { |
| 1267 | if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2)) |
| 1268 | Equivalent = false; |
| 1269 | } else { |
| 1270 | // Kind mismatch. |
| 1271 | Equivalent = false; |
| 1272 | } |
| 1273 | } else if (TemplateTemplateParmDecl *TTP1 |
| 1274 | = dyn_cast<TemplateTemplateParmDecl>(D1)) { |
| 1275 | if (TemplateTemplateParmDecl *TTP2 |
| 1276 | = dyn_cast<TemplateTemplateParmDecl>(D2)) { |
| 1277 | if (!::IsStructurallyEquivalent(*this, TTP1, TTP2)) |
| 1278 | Equivalent = false; |
| 1279 | } else { |
| 1280 | // Kind mismatch. |
| 1281 | Equivalent = false; |
| 1282 | } |
| 1283 | } |
| 1284 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1285 | if (!Equivalent) { |
| 1286 | // Note that these two declarations are not equivalent (and we already |
| 1287 | // know about it). |
| 1288 | NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(), |
| 1289 | D2->getCanonicalDecl())); |
| 1290 | return true; |
| 1291 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1292 | // FIXME: Check other declaration kinds! |
| 1293 | } |
| 1294 | |
| 1295 | return false; |
| 1296 | } |
| 1297 | |
| 1298 | //---------------------------------------------------------------------------- |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1299 | // Import Types |
| 1300 | //---------------------------------------------------------------------------- |
| 1301 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1302 | QualType ASTNodeImporter::VisitType(const Type *T) { |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 1303 | Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node) |
| 1304 | << T->getTypeClassName(); |
| 1305 | return QualType(); |
| 1306 | } |
| 1307 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1308 | QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1309 | switch (T->getKind()) { |
John McCall | e314e27 | 2011-10-18 21:02:43 +0000 | [diff] [blame] | 1310 | #define SHARED_SINGLETON_TYPE(Expansion) |
| 1311 | #define BUILTIN_TYPE(Id, SingletonId) \ |
| 1312 | case BuiltinType::Id: return Importer.getToContext().SingletonId; |
| 1313 | #include "clang/AST/BuiltinTypes.def" |
| 1314 | |
| 1315 | // FIXME: for Char16, Char32, and NullPtr, make sure that the "to" |
| 1316 | // context supports C++. |
| 1317 | |
| 1318 | // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to" |
| 1319 | // context supports ObjC. |
| 1320 | |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1321 | case BuiltinType::Char_U: |
| 1322 | // The context we're importing from has an unsigned 'char'. If we're |
| 1323 | // importing into a context with a signed 'char', translate to |
| 1324 | // 'unsigned char' instead. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1325 | if (Importer.getToContext().getLangOpts().CharIsSigned) |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1326 | return Importer.getToContext().UnsignedCharTy; |
| 1327 | |
| 1328 | return Importer.getToContext().CharTy; |
| 1329 | |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1330 | case BuiltinType::Char_S: |
| 1331 | // The context we're importing from has an unsigned 'char'. If we're |
| 1332 | // importing into a context with a signed 'char', translate to |
| 1333 | // 'unsigned char' instead. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1334 | if (!Importer.getToContext().getLangOpts().CharIsSigned) |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1335 | return Importer.getToContext().SignedCharTy; |
| 1336 | |
| 1337 | return Importer.getToContext().CharTy; |
| 1338 | |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 1339 | case BuiltinType::WChar_S: |
| 1340 | case BuiltinType::WChar_U: |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1341 | // FIXME: If not in C++, shall we translate to the C equivalent of |
| 1342 | // wchar_t? |
| 1343 | return Importer.getToContext().WCharTy; |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1344 | } |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1345 | |
| 1346 | llvm_unreachable("Invalid BuiltinType Kind!"); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1349 | QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1350 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1351 | if (ToElementType.isNull()) |
| 1352 | return QualType(); |
| 1353 | |
| 1354 | return Importer.getToContext().getComplexType(ToElementType); |
| 1355 | } |
| 1356 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1357 | QualType ASTNodeImporter::VisitPointerType(const PointerType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1358 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 1359 | if (ToPointeeType.isNull()) |
| 1360 | return QualType(); |
| 1361 | |
| 1362 | return Importer.getToContext().getPointerType(ToPointeeType); |
| 1363 | } |
| 1364 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1365 | QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1366 | // FIXME: Check for blocks support in "to" context. |
| 1367 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 1368 | if (ToPointeeType.isNull()) |
| 1369 | return QualType(); |
| 1370 | |
| 1371 | return Importer.getToContext().getBlockPointerType(ToPointeeType); |
| 1372 | } |
| 1373 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1374 | QualType |
| 1375 | ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1376 | // FIXME: Check for C++ support in "to" context. |
| 1377 | QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); |
| 1378 | if (ToPointeeType.isNull()) |
| 1379 | return QualType(); |
| 1380 | |
| 1381 | return Importer.getToContext().getLValueReferenceType(ToPointeeType); |
| 1382 | } |
| 1383 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1384 | QualType |
| 1385 | ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1386 | // FIXME: Check for C++0x support in "to" context. |
| 1387 | QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); |
| 1388 | if (ToPointeeType.isNull()) |
| 1389 | return QualType(); |
| 1390 | |
| 1391 | return Importer.getToContext().getRValueReferenceType(ToPointeeType); |
| 1392 | } |
| 1393 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1394 | QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1395 | // FIXME: Check for C++ support in "to" context. |
| 1396 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 1397 | if (ToPointeeType.isNull()) |
| 1398 | return QualType(); |
| 1399 | |
| 1400 | QualType ClassType = Importer.Import(QualType(T->getClass(), 0)); |
| 1401 | return Importer.getToContext().getMemberPointerType(ToPointeeType, |
| 1402 | ClassType.getTypePtr()); |
| 1403 | } |
| 1404 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1405 | QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1406 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1407 | if (ToElementType.isNull()) |
| 1408 | return QualType(); |
| 1409 | |
| 1410 | return Importer.getToContext().getConstantArrayType(ToElementType, |
| 1411 | T->getSize(), |
| 1412 | T->getSizeModifier(), |
| 1413 | T->getIndexTypeCVRQualifiers()); |
| 1414 | } |
| 1415 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1416 | QualType |
| 1417 | ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1418 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1419 | if (ToElementType.isNull()) |
| 1420 | return QualType(); |
| 1421 | |
| 1422 | return Importer.getToContext().getIncompleteArrayType(ToElementType, |
| 1423 | T->getSizeModifier(), |
| 1424 | T->getIndexTypeCVRQualifiers()); |
| 1425 | } |
| 1426 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1427 | QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1428 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1429 | if (ToElementType.isNull()) |
| 1430 | return QualType(); |
| 1431 | |
| 1432 | Expr *Size = Importer.Import(T->getSizeExpr()); |
| 1433 | if (!Size) |
| 1434 | return QualType(); |
| 1435 | |
| 1436 | SourceRange Brackets = Importer.Import(T->getBracketsRange()); |
| 1437 | return Importer.getToContext().getVariableArrayType(ToElementType, Size, |
| 1438 | T->getSizeModifier(), |
| 1439 | T->getIndexTypeCVRQualifiers(), |
| 1440 | Brackets); |
| 1441 | } |
| 1442 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1443 | QualType ASTNodeImporter::VisitVectorType(const VectorType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1444 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1445 | if (ToElementType.isNull()) |
| 1446 | return QualType(); |
| 1447 | |
| 1448 | return Importer.getToContext().getVectorType(ToElementType, |
| 1449 | T->getNumElements(), |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1450 | T->getVectorKind()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1453 | QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1454 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1455 | if (ToElementType.isNull()) |
| 1456 | return QualType(); |
| 1457 | |
| 1458 | return Importer.getToContext().getExtVectorType(ToElementType, |
| 1459 | T->getNumElements()); |
| 1460 | } |
| 1461 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1462 | QualType |
| 1463 | ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1464 | // FIXME: What happens if we're importing a function without a prototype |
| 1465 | // into C++? Should we make it variadic? |
| 1466 | QualType ToResultType = Importer.Import(T->getResultType()); |
| 1467 | if (ToResultType.isNull()) |
| 1468 | return QualType(); |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1469 | |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1470 | return Importer.getToContext().getFunctionNoProtoType(ToResultType, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1471 | T->getExtInfo()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1474 | QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1475 | QualType ToResultType = Importer.Import(T->getResultType()); |
| 1476 | if (ToResultType.isNull()) |
| 1477 | return QualType(); |
| 1478 | |
| 1479 | // Import argument types |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1480 | SmallVector<QualType, 4> ArgTypes; |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1481 | for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(), |
| 1482 | AEnd = T->arg_type_end(); |
| 1483 | A != AEnd; ++A) { |
| 1484 | QualType ArgType = Importer.Import(*A); |
| 1485 | if (ArgType.isNull()) |
| 1486 | return QualType(); |
| 1487 | ArgTypes.push_back(ArgType); |
| 1488 | } |
| 1489 | |
| 1490 | // Import exception types |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1491 | SmallVector<QualType, 4> ExceptionTypes; |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1492 | for (FunctionProtoType::exception_iterator E = T->exception_begin(), |
| 1493 | EEnd = T->exception_end(); |
| 1494 | E != EEnd; ++E) { |
| 1495 | QualType ExceptionType = Importer.Import(*E); |
| 1496 | if (ExceptionType.isNull()) |
| 1497 | return QualType(); |
| 1498 | ExceptionTypes.push_back(ExceptionType); |
| 1499 | } |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1500 | |
| 1501 | FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo(); |
| 1502 | EPI.Exceptions = ExceptionTypes.data(); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1503 | |
| 1504 | return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(), |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1505 | ArgTypes.size(), EPI); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
Sean Callanan | da6df8a | 2011-08-11 16:56:07 +0000 | [diff] [blame] | 1508 | QualType ASTNodeImporter::VisitParenType(const ParenType *T) { |
| 1509 | QualType ToInnerType = Importer.Import(T->getInnerType()); |
| 1510 | if (ToInnerType.isNull()) |
| 1511 | return QualType(); |
| 1512 | |
| 1513 | return Importer.getToContext().getParenType(ToInnerType); |
| 1514 | } |
| 1515 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1516 | QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1517 | TypedefNameDecl *ToDecl |
| 1518 | = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl())); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1519 | if (!ToDecl) |
| 1520 | return QualType(); |
| 1521 | |
| 1522 | return Importer.getToContext().getTypeDeclType(ToDecl); |
| 1523 | } |
| 1524 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1525 | QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1526 | Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); |
| 1527 | if (!ToExpr) |
| 1528 | return QualType(); |
| 1529 | |
| 1530 | return Importer.getToContext().getTypeOfExprType(ToExpr); |
| 1531 | } |
| 1532 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1533 | QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1534 | QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); |
| 1535 | if (ToUnderlyingType.isNull()) |
| 1536 | return QualType(); |
| 1537 | |
| 1538 | return Importer.getToContext().getTypeOfType(ToUnderlyingType); |
| 1539 | } |
| 1540 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1541 | QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) { |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1542 | // FIXME: Make sure that the "to" context supports C++0x! |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1543 | Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); |
| 1544 | if (!ToExpr) |
| 1545 | return QualType(); |
| 1546 | |
Douglas Gregor | 81495f3 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 1547 | QualType UnderlyingType = Importer.Import(T->getUnderlyingType()); |
| 1548 | if (UnderlyingType.isNull()) |
| 1549 | return QualType(); |
| 1550 | |
| 1551 | return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1554 | QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) { |
| 1555 | QualType ToBaseType = Importer.Import(T->getBaseType()); |
| 1556 | QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); |
| 1557 | if (ToBaseType.isNull() || ToUnderlyingType.isNull()) |
| 1558 | return QualType(); |
| 1559 | |
| 1560 | return Importer.getToContext().getUnaryTransformType(ToBaseType, |
| 1561 | ToUnderlyingType, |
| 1562 | T->getUTTKind()); |
| 1563 | } |
| 1564 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1565 | QualType ASTNodeImporter::VisitAutoType(const AutoType *T) { |
| 1566 | // FIXME: Make sure that the "to" context supports C++0x! |
| 1567 | QualType FromDeduced = T->getDeducedType(); |
| 1568 | QualType ToDeduced; |
| 1569 | if (!FromDeduced.isNull()) { |
| 1570 | ToDeduced = Importer.Import(FromDeduced); |
| 1571 | if (ToDeduced.isNull()) |
| 1572 | return QualType(); |
| 1573 | } |
| 1574 | |
| 1575 | return Importer.getToContext().getAutoType(ToDeduced); |
| 1576 | } |
| 1577 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1578 | QualType ASTNodeImporter::VisitRecordType(const RecordType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1579 | RecordDecl *ToDecl |
| 1580 | = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl())); |
| 1581 | if (!ToDecl) |
| 1582 | return QualType(); |
| 1583 | |
| 1584 | return Importer.getToContext().getTagDeclType(ToDecl); |
| 1585 | } |
| 1586 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1587 | QualType ASTNodeImporter::VisitEnumType(const EnumType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1588 | EnumDecl *ToDecl |
| 1589 | = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl())); |
| 1590 | if (!ToDecl) |
| 1591 | return QualType(); |
| 1592 | |
| 1593 | return Importer.getToContext().getTagDeclType(ToDecl); |
| 1594 | } |
| 1595 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1596 | QualType ASTNodeImporter::VisitTemplateSpecializationType( |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1597 | const TemplateSpecializationType *T) { |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1598 | TemplateName ToTemplate = Importer.Import(T->getTemplateName()); |
| 1599 | if (ToTemplate.isNull()) |
| 1600 | return QualType(); |
| 1601 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1602 | SmallVector<TemplateArgument, 2> ToTemplateArgs; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1603 | if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs)) |
| 1604 | return QualType(); |
| 1605 | |
| 1606 | QualType ToCanonType; |
| 1607 | if (!QualType(T, 0).isCanonical()) { |
| 1608 | QualType FromCanonType |
| 1609 | = Importer.getFromContext().getCanonicalType(QualType(T, 0)); |
| 1610 | ToCanonType =Importer.Import(FromCanonType); |
| 1611 | if (ToCanonType.isNull()) |
| 1612 | return QualType(); |
| 1613 | } |
| 1614 | return Importer.getToContext().getTemplateSpecializationType(ToTemplate, |
| 1615 | ToTemplateArgs.data(), |
| 1616 | ToTemplateArgs.size(), |
| 1617 | ToCanonType); |
| 1618 | } |
| 1619 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1620 | QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1621 | NestedNameSpecifier *ToQualifier = 0; |
| 1622 | // Note: the qualifier in an ElaboratedType is optional. |
| 1623 | if (T->getQualifier()) { |
| 1624 | ToQualifier = Importer.Import(T->getQualifier()); |
| 1625 | if (!ToQualifier) |
| 1626 | return QualType(); |
| 1627 | } |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1628 | |
| 1629 | QualType ToNamedType = Importer.Import(T->getNamedType()); |
| 1630 | if (ToNamedType.isNull()) |
| 1631 | return QualType(); |
| 1632 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1633 | return Importer.getToContext().getElaboratedType(T->getKeyword(), |
| 1634 | ToQualifier, ToNamedType); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1637 | QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1638 | ObjCInterfaceDecl *Class |
| 1639 | = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl())); |
| 1640 | if (!Class) |
| 1641 | return QualType(); |
| 1642 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1643 | return Importer.getToContext().getObjCInterfaceType(Class); |
| 1644 | } |
| 1645 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1646 | QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1647 | QualType ToBaseType = Importer.Import(T->getBaseType()); |
| 1648 | if (ToBaseType.isNull()) |
| 1649 | return QualType(); |
| 1650 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1651 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1652 | for (ObjCObjectType::qual_iterator P = T->qual_begin(), |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1653 | PEnd = T->qual_end(); |
| 1654 | P != PEnd; ++P) { |
| 1655 | ObjCProtocolDecl *Protocol |
| 1656 | = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P)); |
| 1657 | if (!Protocol) |
| 1658 | return QualType(); |
| 1659 | Protocols.push_back(Protocol); |
| 1660 | } |
| 1661 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1662 | return Importer.getToContext().getObjCObjectType(ToBaseType, |
| 1663 | Protocols.data(), |
| 1664 | Protocols.size()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1665 | } |
| 1666 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1667 | QualType |
| 1668 | ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1669 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 1670 | if (ToPointeeType.isNull()) |
| 1671 | return QualType(); |
| 1672 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1673 | return Importer.getToContext().getObjCObjectPointerType(ToPointeeType); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 1676 | //---------------------------------------------------------------------------- |
| 1677 | // Import Declarations |
| 1678 | //---------------------------------------------------------------------------- |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 1679 | bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC, |
| 1680 | DeclContext *&LexicalDC, |
| 1681 | DeclarationName &Name, |
| 1682 | SourceLocation &Loc) { |
| 1683 | // Import the context of this declaration. |
| 1684 | DC = Importer.ImportContext(D->getDeclContext()); |
| 1685 | if (!DC) |
| 1686 | return true; |
| 1687 | |
| 1688 | LexicalDC = DC; |
| 1689 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 1690 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 1691 | if (!LexicalDC) |
| 1692 | return true; |
| 1693 | } |
| 1694 | |
| 1695 | // Import the name of this declaration. |
| 1696 | Name = Importer.Import(D->getDeclName()); |
| 1697 | if (D->getDeclName() && !Name) |
| 1698 | return true; |
| 1699 | |
| 1700 | // Import the location of this declaration. |
| 1701 | Loc = Importer.Import(D->getLocation()); |
| 1702 | return false; |
| 1703 | } |
| 1704 | |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 1705 | void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) { |
| 1706 | if (!FromD) |
| 1707 | return; |
| 1708 | |
| 1709 | if (!ToD) { |
| 1710 | ToD = Importer.Import(FromD); |
| 1711 | if (!ToD) |
| 1712 | return; |
| 1713 | } |
| 1714 | |
| 1715 | if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) { |
| 1716 | if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) { |
| 1717 | if (FromRecord->getDefinition() && !ToRecord->getDefinition()) { |
| 1718 | ImportDefinition(FromRecord, ToRecord); |
| 1719 | } |
| 1720 | } |
| 1721 | return; |
| 1722 | } |
| 1723 | |
| 1724 | if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) { |
| 1725 | if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) { |
| 1726 | if (FromEnum->getDefinition() && !ToEnum->getDefinition()) { |
| 1727 | ImportDefinition(FromEnum, ToEnum); |
| 1728 | } |
| 1729 | } |
| 1730 | return; |
| 1731 | } |
| 1732 | } |
| 1733 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1734 | void |
| 1735 | ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From, |
| 1736 | DeclarationNameInfo& To) { |
| 1737 | // NOTE: To.Name and To.Loc are already imported. |
| 1738 | // We only have to import To.LocInfo. |
| 1739 | switch (To.getName().getNameKind()) { |
| 1740 | case DeclarationName::Identifier: |
| 1741 | case DeclarationName::ObjCZeroArgSelector: |
| 1742 | case DeclarationName::ObjCOneArgSelector: |
| 1743 | case DeclarationName::ObjCMultiArgSelector: |
| 1744 | case DeclarationName::CXXUsingDirective: |
| 1745 | return; |
| 1746 | |
| 1747 | case DeclarationName::CXXOperatorName: { |
| 1748 | SourceRange Range = From.getCXXOperatorNameRange(); |
| 1749 | To.setCXXOperatorNameRange(Importer.Import(Range)); |
| 1750 | return; |
| 1751 | } |
| 1752 | case DeclarationName::CXXLiteralOperatorName: { |
| 1753 | SourceLocation Loc = From.getCXXLiteralOperatorNameLoc(); |
| 1754 | To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc)); |
| 1755 | return; |
| 1756 | } |
| 1757 | case DeclarationName::CXXConstructorName: |
| 1758 | case DeclarationName::CXXDestructorName: |
| 1759 | case DeclarationName::CXXConversionFunctionName: { |
| 1760 | TypeSourceInfo *FromTInfo = From.getNamedTypeInfo(); |
| 1761 | To.setNamedTypeInfo(Importer.Import(FromTInfo)); |
| 1762 | return; |
| 1763 | } |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1764 | } |
Douglas Gregor | 07216d1 | 2011-11-02 20:52:01 +0000 | [diff] [blame] | 1765 | llvm_unreachable("Unknown name kind."); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 1768 | void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) { |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 1769 | if (Importer.isMinimalImport() && !ForceImport) { |
Sean Callanan | 81d577c | 2011-07-22 23:46:03 +0000 | [diff] [blame] | 1770 | Importer.ImportContext(FromDC); |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 1771 | return; |
| 1772 | } |
| 1773 | |
Douglas Gregor | 968d633 | 2010-02-21 18:24:45 +0000 | [diff] [blame] | 1774 | for (DeclContext::decl_iterator From = FromDC->decls_begin(), |
| 1775 | FromEnd = FromDC->decls_end(); |
| 1776 | From != FromEnd; |
| 1777 | ++From) |
| 1778 | Importer.Import(*From); |
| 1779 | } |
| 1780 | |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 1781 | bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To, |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 1782 | ImportDefinitionKind Kind) { |
| 1783 | if (To->getDefinition() || To->isBeingDefined()) { |
| 1784 | if (Kind == IDK_Everything) |
| 1785 | ImportDeclContext(From, /*ForceImport=*/true); |
| 1786 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1787 | return false; |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 1788 | } |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1789 | |
| 1790 | To->startDefinition(); |
| 1791 | |
| 1792 | // Add base classes. |
| 1793 | if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) { |
| 1794 | CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From); |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 1795 | |
| 1796 | struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data(); |
| 1797 | struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data(); |
| 1798 | ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor; |
| 1799 | ToData.UserDeclaredCopyConstructor = FromData.UserDeclaredCopyConstructor; |
| 1800 | ToData.UserDeclaredMoveConstructor = FromData.UserDeclaredMoveConstructor; |
| 1801 | ToData.UserDeclaredCopyAssignment = FromData.UserDeclaredCopyAssignment; |
| 1802 | ToData.UserDeclaredMoveAssignment = FromData.UserDeclaredMoveAssignment; |
| 1803 | ToData.UserDeclaredDestructor = FromData.UserDeclaredDestructor; |
| 1804 | ToData.Aggregate = FromData.Aggregate; |
| 1805 | ToData.PlainOldData = FromData.PlainOldData; |
| 1806 | ToData.Empty = FromData.Empty; |
| 1807 | ToData.Polymorphic = FromData.Polymorphic; |
| 1808 | ToData.Abstract = FromData.Abstract; |
| 1809 | ToData.IsStandardLayout = FromData.IsStandardLayout; |
| 1810 | ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases; |
| 1811 | ToData.HasPrivateFields = FromData.HasPrivateFields; |
| 1812 | ToData.HasProtectedFields = FromData.HasProtectedFields; |
| 1813 | ToData.HasPublicFields = FromData.HasPublicFields; |
| 1814 | ToData.HasMutableFields = FromData.HasMutableFields; |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 1815 | ToData.HasOnlyCMembers = FromData.HasOnlyCMembers; |
Richard Smith | e2648ba | 2012-05-07 01:07:30 +0000 | [diff] [blame] | 1816 | ToData.HasInClassInitializer = FromData.HasInClassInitializer; |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 1817 | ToData.HasTrivialDefaultConstructor = FromData.HasTrivialDefaultConstructor; |
| 1818 | ToData.HasConstexprNonCopyMoveConstructor |
| 1819 | = FromData.HasConstexprNonCopyMoveConstructor; |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 1820 | ToData.DefaultedDefaultConstructorIsConstexpr |
| 1821 | = FromData.DefaultedDefaultConstructorIsConstexpr; |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 1822 | ToData.HasConstexprDefaultConstructor |
| 1823 | = FromData.HasConstexprDefaultConstructor; |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 1824 | ToData.HasTrivialCopyConstructor = FromData.HasTrivialCopyConstructor; |
| 1825 | ToData.HasTrivialMoveConstructor = FromData.HasTrivialMoveConstructor; |
| 1826 | ToData.HasTrivialCopyAssignment = FromData.HasTrivialCopyAssignment; |
| 1827 | ToData.HasTrivialMoveAssignment = FromData.HasTrivialMoveAssignment; |
| 1828 | ToData.HasTrivialDestructor = FromData.HasTrivialDestructor; |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 1829 | ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor; |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 1830 | ToData.HasNonLiteralTypeFieldsOrBases |
| 1831 | = FromData.HasNonLiteralTypeFieldsOrBases; |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 1832 | // ComputedVisibleConversions not imported. |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 1833 | ToData.UserProvidedDefaultConstructor |
| 1834 | = FromData.UserProvidedDefaultConstructor; |
| 1835 | ToData.DeclaredDefaultConstructor = FromData.DeclaredDefaultConstructor; |
| 1836 | ToData.DeclaredCopyConstructor = FromData.DeclaredCopyConstructor; |
| 1837 | ToData.DeclaredMoveConstructor = FromData.DeclaredMoveConstructor; |
| 1838 | ToData.DeclaredCopyAssignment = FromData.DeclaredCopyAssignment; |
| 1839 | ToData.DeclaredMoveAssignment = FromData.DeclaredMoveAssignment; |
| 1840 | ToData.DeclaredDestructor = FromData.DeclaredDestructor; |
| 1841 | ToData.FailedImplicitMoveConstructor |
| 1842 | = FromData.FailedImplicitMoveConstructor; |
| 1843 | ToData.FailedImplicitMoveAssignment = FromData.FailedImplicitMoveAssignment; |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 1844 | ToData.IsLambda = FromData.IsLambda; |
| 1845 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1846 | SmallVector<CXXBaseSpecifier *, 4> Bases; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1847 | for (CXXRecordDecl::base_class_iterator |
| 1848 | Base1 = FromCXX->bases_begin(), |
| 1849 | FromBaseEnd = FromCXX->bases_end(); |
| 1850 | Base1 != FromBaseEnd; |
| 1851 | ++Base1) { |
| 1852 | QualType T = Importer.Import(Base1->getType()); |
| 1853 | if (T.isNull()) |
Douglas Gregor | 96303ea | 2010-12-02 19:33:37 +0000 | [diff] [blame] | 1854 | return true; |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1855 | |
| 1856 | SourceLocation EllipsisLoc; |
| 1857 | if (Base1->isPackExpansion()) |
| 1858 | EllipsisLoc = Importer.Import(Base1->getEllipsisLoc()); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 1859 | |
| 1860 | // Ensure that we have a definition for the base. |
| 1861 | ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl()); |
| 1862 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1863 | Bases.push_back( |
| 1864 | new (Importer.getToContext()) |
| 1865 | CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()), |
| 1866 | Base1->isVirtual(), |
| 1867 | Base1->isBaseOfClass(), |
| 1868 | Base1->getAccessSpecifierAsWritten(), |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1869 | Importer.Import(Base1->getTypeSourceInfo()), |
| 1870 | EllipsisLoc)); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1871 | } |
| 1872 | if (!Bases.empty()) |
| 1873 | ToCXX->setBases(Bases.data(), Bases.size()); |
| 1874 | } |
| 1875 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 1876 | if (shouldForceImportDeclContext(Kind)) |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 1877 | ImportDeclContext(From, /*ForceImport=*/true); |
| 1878 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1879 | To->completeDefinition(); |
Douglas Gregor | 96303ea | 2010-12-02 19:33:37 +0000 | [diff] [blame] | 1880 | return false; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 1883 | bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 1884 | ImportDefinitionKind Kind) { |
| 1885 | if (To->getDefinition() || To->isBeingDefined()) { |
| 1886 | if (Kind == IDK_Everything) |
| 1887 | ImportDeclContext(From, /*ForceImport=*/true); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 1888 | return false; |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 1889 | } |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 1890 | |
| 1891 | To->startDefinition(); |
| 1892 | |
| 1893 | QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From)); |
| 1894 | if (T.isNull()) |
| 1895 | return true; |
| 1896 | |
| 1897 | QualType ToPromotionType = Importer.Import(From->getPromotionType()); |
| 1898 | if (ToPromotionType.isNull()) |
| 1899 | return true; |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 1900 | |
| 1901 | if (shouldForceImportDeclContext(Kind)) |
| 1902 | ImportDeclContext(From, /*ForceImport=*/true); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 1903 | |
| 1904 | // FIXME: we might need to merge the number of positive or negative bits |
| 1905 | // if the enumerator lists don't match. |
| 1906 | To->completeDefinition(T, ToPromotionType, |
| 1907 | From->getNumPositiveBits(), |
| 1908 | From->getNumNegativeBits()); |
| 1909 | return false; |
| 1910 | } |
| 1911 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1912 | TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList( |
| 1913 | TemplateParameterList *Params) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1914 | SmallVector<NamedDecl *, 4> ToParams; |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1915 | ToParams.reserve(Params->size()); |
| 1916 | for (TemplateParameterList::iterator P = Params->begin(), |
| 1917 | PEnd = Params->end(); |
| 1918 | P != PEnd; ++P) { |
| 1919 | Decl *To = Importer.Import(*P); |
| 1920 | if (!To) |
| 1921 | return 0; |
| 1922 | |
| 1923 | ToParams.push_back(cast<NamedDecl>(To)); |
| 1924 | } |
| 1925 | |
| 1926 | return TemplateParameterList::Create(Importer.getToContext(), |
| 1927 | Importer.Import(Params->getTemplateLoc()), |
| 1928 | Importer.Import(Params->getLAngleLoc()), |
| 1929 | ToParams.data(), ToParams.size(), |
| 1930 | Importer.Import(Params->getRAngleLoc())); |
| 1931 | } |
| 1932 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1933 | TemplateArgument |
| 1934 | ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) { |
| 1935 | switch (From.getKind()) { |
| 1936 | case TemplateArgument::Null: |
| 1937 | return TemplateArgument(); |
| 1938 | |
| 1939 | case TemplateArgument::Type: { |
| 1940 | QualType ToType = Importer.Import(From.getAsType()); |
| 1941 | if (ToType.isNull()) |
| 1942 | return TemplateArgument(); |
| 1943 | return TemplateArgument(ToType); |
| 1944 | } |
| 1945 | |
| 1946 | case TemplateArgument::Integral: { |
| 1947 | QualType ToType = Importer.Import(From.getIntegralType()); |
| 1948 | if (ToType.isNull()) |
| 1949 | return TemplateArgument(); |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 1950 | return TemplateArgument(From, ToType); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
| 1953 | case TemplateArgument::Declaration: |
| 1954 | if (Decl *To = Importer.Import(From.getAsDecl())) |
| 1955 | return TemplateArgument(To); |
| 1956 | return TemplateArgument(); |
| 1957 | |
| 1958 | case TemplateArgument::Template: { |
| 1959 | TemplateName ToTemplate = Importer.Import(From.getAsTemplate()); |
| 1960 | if (ToTemplate.isNull()) |
| 1961 | return TemplateArgument(); |
| 1962 | |
| 1963 | return TemplateArgument(ToTemplate); |
| 1964 | } |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1965 | |
| 1966 | case TemplateArgument::TemplateExpansion: { |
| 1967 | TemplateName ToTemplate |
| 1968 | = Importer.Import(From.getAsTemplateOrTemplatePattern()); |
| 1969 | if (ToTemplate.isNull()) |
| 1970 | return TemplateArgument(); |
| 1971 | |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 1972 | return TemplateArgument(ToTemplate, From.getNumTemplateExpansions()); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1975 | case TemplateArgument::Expression: |
| 1976 | if (Expr *ToExpr = Importer.Import(From.getAsExpr())) |
| 1977 | return TemplateArgument(ToExpr); |
| 1978 | return TemplateArgument(); |
| 1979 | |
| 1980 | case TemplateArgument::Pack: { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1981 | SmallVector<TemplateArgument, 2> ToPack; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1982 | ToPack.reserve(From.pack_size()); |
| 1983 | if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack)) |
| 1984 | return TemplateArgument(); |
| 1985 | |
| 1986 | TemplateArgument *ToArgs |
| 1987 | = new (Importer.getToContext()) TemplateArgument[ToPack.size()]; |
| 1988 | std::copy(ToPack.begin(), ToPack.end(), ToArgs); |
| 1989 | return TemplateArgument(ToArgs, ToPack.size()); |
| 1990 | } |
| 1991 | } |
| 1992 | |
| 1993 | llvm_unreachable("Invalid template argument kind"); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1994 | } |
| 1995 | |
| 1996 | bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs, |
| 1997 | unsigned NumFromArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1998 | SmallVectorImpl<TemplateArgument> &ToArgs) { |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1999 | for (unsigned I = 0; I != NumFromArgs; ++I) { |
| 2000 | TemplateArgument To = ImportTemplateArgument(FromArgs[I]); |
| 2001 | if (To.isNull() && !FromArgs[I].isNull()) |
| 2002 | return true; |
| 2003 | |
| 2004 | ToArgs.push_back(To); |
| 2005 | } |
| 2006 | |
| 2007 | return false; |
| 2008 | } |
| 2009 | |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2010 | bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord, |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2011 | RecordDecl *ToRecord) { |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 2012 | StructuralEquivalenceContext Ctx(Importer.getFromContext(), |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2013 | Importer.getToContext(), |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2014 | Importer.getNonEquivalentDecls()); |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 2015 | return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2016 | } |
| 2017 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2018 | bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) { |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 2019 | StructuralEquivalenceContext Ctx(Importer.getFromContext(), |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2020 | Importer.getToContext(), |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2021 | Importer.getNonEquivalentDecls()); |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 2022 | return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 2025 | bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From, |
| 2026 | ClassTemplateDecl *To) { |
| 2027 | StructuralEquivalenceContext Ctx(Importer.getFromContext(), |
| 2028 | Importer.getToContext(), |
| 2029 | Importer.getNonEquivalentDecls()); |
| 2030 | return Ctx.IsStructurallyEquivalent(From, To); |
| 2031 | } |
| 2032 | |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 2033 | Decl *ASTNodeImporter::VisitDecl(Decl *D) { |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 2034 | Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 2035 | << D->getDeclKindName(); |
| 2036 | return 0; |
| 2037 | } |
| 2038 | |
Sean Callanan | 6519827 | 2011-11-17 23:20:56 +0000 | [diff] [blame] | 2039 | Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 2040 | TranslationUnitDecl *ToD = |
| 2041 | Importer.getToContext().getTranslationUnitDecl(); |
| 2042 | |
| 2043 | Importer.Imported(D, ToD); |
| 2044 | |
| 2045 | return ToD; |
| 2046 | } |
| 2047 | |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2048 | Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) { |
| 2049 | // Import the major distinguishing characteristics of this namespace. |
| 2050 | DeclContext *DC, *LexicalDC; |
| 2051 | DeclarationName Name; |
| 2052 | SourceLocation Loc; |
| 2053 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 2054 | return 0; |
| 2055 | |
| 2056 | NamespaceDecl *MergeWithNamespace = 0; |
| 2057 | if (!Name) { |
| 2058 | // This is an anonymous namespace. Adopt an existing anonymous |
| 2059 | // namespace if we can. |
| 2060 | // FIXME: Not testable. |
| 2061 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC)) |
| 2062 | MergeWithNamespace = TU->getAnonymousNamespace(); |
| 2063 | else |
| 2064 | MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace(); |
| 2065 | } else { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2066 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2067 | llvm::SmallVector<NamedDecl *, 2> FoundDecls; |
| 2068 | DC->localUncachedLookup(Name, FoundDecls); |
| 2069 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2070 | if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace)) |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2071 | continue; |
| 2072 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2073 | if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) { |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2074 | MergeWithNamespace = FoundNS; |
| 2075 | ConflictingDecls.clear(); |
| 2076 | break; |
| 2077 | } |
| 2078 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2079 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
| 2082 | if (!ConflictingDecls.empty()) { |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 2083 | Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace, |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2084 | ConflictingDecls.data(), |
| 2085 | ConflictingDecls.size()); |
| 2086 | } |
| 2087 | } |
| 2088 | |
| 2089 | // Create the "to" namespace, if needed. |
| 2090 | NamespaceDecl *ToNamespace = MergeWithNamespace; |
| 2091 | if (!ToNamespace) { |
Abramo Bagnara | b5545be | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 2092 | ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 2093 | D->isInline(), |
Abramo Bagnara | b5545be | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 2094 | Importer.Import(D->getLocStart()), |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 2095 | Loc, Name.getAsIdentifierInfo(), |
| 2096 | /*PrevDecl=*/0); |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2097 | ToNamespace->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2098 | LexicalDC->addDeclInternal(ToNamespace); |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2099 | |
| 2100 | // If this is an anonymous namespace, register it as the anonymous |
| 2101 | // namespace within its context. |
| 2102 | if (!Name) { |
| 2103 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC)) |
| 2104 | TU->setAnonymousNamespace(ToNamespace); |
| 2105 | else |
| 2106 | cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace); |
| 2107 | } |
| 2108 | } |
| 2109 | Importer.Imported(D, ToNamespace); |
| 2110 | |
| 2111 | ImportDeclContext(D); |
| 2112 | |
| 2113 | return ToNamespace; |
| 2114 | } |
| 2115 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2116 | Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) { |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2117 | // Import the major distinguishing characteristics of this typedef. |
| 2118 | DeclContext *DC, *LexicalDC; |
| 2119 | DeclarationName Name; |
| 2120 | SourceLocation Loc; |
| 2121 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 2122 | return 0; |
| 2123 | |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2124 | // If this typedef is not in block scope, determine whether we've |
| 2125 | // seen a typedef with the same name (that we can merge with) or any |
| 2126 | // other entity by that name (which name lookup could conflict with). |
| 2127 | if (!DC->isFunctionOrMethod()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2128 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2129 | unsigned IDNS = Decl::IDNS_Ordinary; |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2130 | llvm::SmallVector<NamedDecl *, 2> FoundDecls; |
| 2131 | DC->localUncachedLookup(Name, FoundDecls); |
| 2132 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2133 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2134 | continue; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2135 | if (TypedefNameDecl *FoundTypedef = |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2136 | dyn_cast<TypedefNameDecl>(FoundDecls[I])) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2137 | if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(), |
| 2138 | FoundTypedef->getUnderlyingType())) |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2139 | return Importer.Imported(D, FoundTypedef); |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2142 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | if (!ConflictingDecls.empty()) { |
| 2146 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2147 | ConflictingDecls.data(), |
| 2148 | ConflictingDecls.size()); |
| 2149 | if (!Name) |
| 2150 | return 0; |
| 2151 | } |
| 2152 | } |
| 2153 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2154 | // Import the underlying type of this typedef; |
| 2155 | QualType T = Importer.Import(D->getUnderlyingType()); |
| 2156 | if (T.isNull()) |
| 2157 | return 0; |
| 2158 | |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2159 | // Create the new typedef node. |
| 2160 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 2161 | SourceLocation StartL = Importer.Import(D->getLocStart()); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2162 | TypedefNameDecl *ToTypedef; |
| 2163 | if (IsAlias) |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 2164 | ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC, |
| 2165 | StartL, Loc, |
| 2166 | Name.getAsIdentifierInfo(), |
| 2167 | TInfo); |
| 2168 | else |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2169 | ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC, |
| 2170 | StartL, Loc, |
| 2171 | Name.getAsIdentifierInfo(), |
| 2172 | TInfo); |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 2173 | |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2174 | ToTypedef->setAccess(D->getAccess()); |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2175 | ToTypedef->setLexicalDeclContext(LexicalDC); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2176 | Importer.Imported(D, ToTypedef); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2177 | LexicalDC->addDeclInternal(ToTypedef); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2178 | |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2179 | return ToTypedef; |
| 2180 | } |
| 2181 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2182 | Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) { |
| 2183 | return VisitTypedefNameDecl(D, /*IsAlias=*/false); |
| 2184 | } |
| 2185 | |
| 2186 | Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| 2187 | return VisitTypedefNameDecl(D, /*IsAlias=*/true); |
| 2188 | } |
| 2189 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2190 | Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) { |
| 2191 | // Import the major distinguishing characteristics of this enum. |
| 2192 | DeclContext *DC, *LexicalDC; |
| 2193 | DeclarationName Name; |
| 2194 | SourceLocation Loc; |
| 2195 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 2196 | return 0; |
| 2197 | |
| 2198 | // Figure out what enum name we're looking for. |
| 2199 | unsigned IDNS = Decl::IDNS_Tag; |
| 2200 | DeclarationName SearchName = Name; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2201 | if (!SearchName && D->getTypedefNameForAnonDecl()) { |
| 2202 | SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName()); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2203 | IDNS = Decl::IDNS_Ordinary; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2204 | } else if (Importer.getToContext().getLangOpts().CPlusPlus) |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2205 | IDNS |= Decl::IDNS_Ordinary; |
| 2206 | |
| 2207 | // We may already have an enum of the same name; try to find and match it. |
| 2208 | if (!DC->isFunctionOrMethod() && SearchName) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2209 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2210 | llvm::SmallVector<NamedDecl *, 2> FoundDecls; |
| 2211 | DC->localUncachedLookup(SearchName, FoundDecls); |
| 2212 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2213 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2214 | continue; |
| 2215 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2216 | Decl *Found = FoundDecls[I]; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2217 | if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) { |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2218 | if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) |
| 2219 | Found = Tag->getDecl(); |
| 2220 | } |
| 2221 | |
| 2222 | if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) { |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2223 | if (IsStructuralMatch(D, FoundEnum)) |
| 2224 | return Importer.Imported(D, FoundEnum); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2225 | } |
| 2226 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2227 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
| 2230 | if (!ConflictingDecls.empty()) { |
| 2231 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2232 | ConflictingDecls.data(), |
| 2233 | ConflictingDecls.size()); |
| 2234 | } |
| 2235 | } |
| 2236 | |
| 2237 | // Create the enum declaration. |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2238 | EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, |
| 2239 | Importer.Import(D->getLocStart()), |
| 2240 | Loc, Name.getAsIdentifierInfo(), 0, |
Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2241 | D->isScoped(), D->isScopedUsingClassTag(), |
| 2242 | D->isFixed()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2243 | // Import the qualifier, if any. |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2244 | D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2245 | D2->setAccess(D->getAccess()); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2246 | D2->setLexicalDeclContext(LexicalDC); |
| 2247 | Importer.Imported(D, D2); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2248 | LexicalDC->addDeclInternal(D2); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2249 | |
| 2250 | // Import the integer type. |
| 2251 | QualType ToIntegerType = Importer.Import(D->getIntegerType()); |
| 2252 | if (ToIntegerType.isNull()) |
| 2253 | return 0; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2254 | D2->setIntegerType(ToIntegerType); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2255 | |
| 2256 | // Import the definition |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2257 | if (D->isCompleteDefinition() && ImportDefinition(D, D2)) |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2258 | return 0; |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2259 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2260 | return D2; |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2261 | } |
| 2262 | |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2263 | Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) { |
| 2264 | // If this record has a definition in the translation unit we're coming from, |
| 2265 | // but this particular declaration is not that definition, import the |
| 2266 | // definition and map to that. |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 2267 | TagDecl *Definition = D->getDefinition(); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2268 | if (Definition && Definition != D) { |
| 2269 | Decl *ImportedDef = Importer.Import(Definition); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2270 | if (!ImportedDef) |
| 2271 | return 0; |
| 2272 | |
| 2273 | return Importer.Imported(D, ImportedDef); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2274 | } |
| 2275 | |
| 2276 | // Import the major distinguishing characteristics of this record. |
| 2277 | DeclContext *DC, *LexicalDC; |
| 2278 | DeclarationName Name; |
| 2279 | SourceLocation Loc; |
| 2280 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 2281 | return 0; |
| 2282 | |
| 2283 | // Figure out what structure name we're looking for. |
| 2284 | unsigned IDNS = Decl::IDNS_Tag; |
| 2285 | DeclarationName SearchName = Name; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2286 | if (!SearchName && D->getTypedefNameForAnonDecl()) { |
| 2287 | SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName()); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2288 | IDNS = Decl::IDNS_Ordinary; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2289 | } else if (Importer.getToContext().getLangOpts().CPlusPlus) |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2290 | IDNS |= Decl::IDNS_Ordinary; |
| 2291 | |
| 2292 | // We may already have a record of the same name; try to find and match it. |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2293 | RecordDecl *AdoptDecl = 0; |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2294 | if (!DC->isFunctionOrMethod() && SearchName) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2295 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2296 | llvm::SmallVector<NamedDecl *, 2> FoundDecls; |
| 2297 | DC->localUncachedLookup(SearchName, FoundDecls); |
| 2298 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2299 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2300 | continue; |
| 2301 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2302 | Decl *Found = FoundDecls[I]; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2303 | if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) { |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2304 | if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) |
| 2305 | Found = Tag->getDecl(); |
| 2306 | } |
| 2307 | |
| 2308 | if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) { |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2309 | if (RecordDecl *FoundDef = FoundRecord->getDefinition()) { |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2310 | if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) { |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2311 | // The record types structurally match, or the "from" translation |
| 2312 | // unit only had a forward declaration anyway; call it the same |
| 2313 | // function. |
| 2314 | // FIXME: For C++, we should also merge methods here. |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2315 | return Importer.Imported(D, FoundDef); |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2316 | } |
| 2317 | } else { |
| 2318 | // We have a forward declaration of this type, so adopt that forward |
| 2319 | // declaration rather than building a new one. |
| 2320 | AdoptDecl = FoundRecord; |
| 2321 | continue; |
| 2322 | } |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2325 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2326 | } |
| 2327 | |
| 2328 | if (!ConflictingDecls.empty()) { |
| 2329 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2330 | ConflictingDecls.data(), |
| 2331 | ConflictingDecls.size()); |
| 2332 | } |
| 2333 | } |
| 2334 | |
| 2335 | // Create the record declaration. |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2336 | RecordDecl *D2 = AdoptDecl; |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2337 | SourceLocation StartLoc = Importer.Import(D->getLocStart()); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2338 | if (!D2) { |
John McCall | 1c70e99 | 2010-06-03 19:28:45 +0000 | [diff] [blame] | 2339 | if (isa<CXXRecordDecl>(D)) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2340 | CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(), |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2341 | D->getTagKind(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2342 | DC, StartLoc, Loc, |
| 2343 | Name.getAsIdentifierInfo()); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2344 | D2 = D2CXX; |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2345 | D2->setAccess(D->getAccess()); |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2346 | } else { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2347 | D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2348 | DC, StartLoc, Loc, Name.getAsIdentifierInfo()); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2349 | } |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2350 | |
| 2351 | D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2352 | D2->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2353 | LexicalDC->addDeclInternal(D2); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2354 | } |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2355 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2356 | Importer.Imported(D, D2); |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2357 | |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 2358 | if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default)) |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2359 | return 0; |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2360 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2361 | return D2; |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2362 | } |
| 2363 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2364 | Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 2365 | // Import the major distinguishing characteristics of this enumerator. |
| 2366 | DeclContext *DC, *LexicalDC; |
| 2367 | DeclarationName Name; |
| 2368 | SourceLocation Loc; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2369 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2370 | return 0; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2371 | |
| 2372 | QualType T = Importer.Import(D->getType()); |
| 2373 | if (T.isNull()) |
| 2374 | return 0; |
| 2375 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2376 | // Determine whether there are any other declarations with the same name and |
| 2377 | // in the same context. |
| 2378 | if (!LexicalDC->isFunctionOrMethod()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2379 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2380 | unsigned IDNS = Decl::IDNS_Ordinary; |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2381 | llvm::SmallVector<NamedDecl *, 2> FoundDecls; |
| 2382 | DC->localUncachedLookup(Name, FoundDecls); |
| 2383 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2384 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2385 | continue; |
| 2386 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2387 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
| 2390 | if (!ConflictingDecls.empty()) { |
| 2391 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2392 | ConflictingDecls.data(), |
| 2393 | ConflictingDecls.size()); |
| 2394 | if (!Name) |
| 2395 | return 0; |
| 2396 | } |
| 2397 | } |
| 2398 | |
| 2399 | Expr *Init = Importer.Import(D->getInitExpr()); |
| 2400 | if (D->getInitExpr() && !Init) |
| 2401 | return 0; |
| 2402 | |
| 2403 | EnumConstantDecl *ToEnumerator |
| 2404 | = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc, |
| 2405 | Name.getAsIdentifierInfo(), T, |
| 2406 | Init, D->getInitVal()); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2407 | ToEnumerator->setAccess(D->getAccess()); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2408 | ToEnumerator->setLexicalDeclContext(LexicalDC); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2409 | Importer.Imported(D, ToEnumerator); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2410 | LexicalDC->addDeclInternal(ToEnumerator); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2411 | return ToEnumerator; |
| 2412 | } |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2413 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2414 | Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) { |
| 2415 | // Import the major distinguishing characteristics of this function. |
| 2416 | DeclContext *DC, *LexicalDC; |
| 2417 | DeclarationName Name; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2418 | SourceLocation Loc; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2419 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2420 | return 0; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2421 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2422 | // Try to find a function in our own ("to") context with the same name, same |
| 2423 | // type, and in the same context as the function we're importing. |
| 2424 | if (!LexicalDC->isFunctionOrMethod()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2425 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2426 | unsigned IDNS = Decl::IDNS_Ordinary; |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2427 | llvm::SmallVector<NamedDecl *, 2> FoundDecls; |
| 2428 | DC->localUncachedLookup(Name, FoundDecls); |
| 2429 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2430 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2431 | continue; |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2432 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2433 | if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) { |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2434 | if (isExternalLinkage(FoundFunction->getLinkage()) && |
| 2435 | isExternalLinkage(D->getLinkage())) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2436 | if (Importer.IsStructurallyEquivalent(D->getType(), |
| 2437 | FoundFunction->getType())) { |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2438 | // FIXME: Actually try to merge the body and other attributes. |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2439 | return Importer.Imported(D, FoundFunction); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
| 2442 | // FIXME: Check for overloading more carefully, e.g., by boosting |
| 2443 | // Sema::IsOverload out to the AST library. |
| 2444 | |
| 2445 | // Function overloading is okay in C++. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2446 | if (Importer.getToContext().getLangOpts().CPlusPlus) |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2447 | continue; |
| 2448 | |
| 2449 | // Complain about inconsistent function types. |
| 2450 | Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent) |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2451 | << Name << D->getType() << FoundFunction->getType(); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2452 | Importer.ToDiag(FoundFunction->getLocation(), |
| 2453 | diag::note_odr_value_here) |
| 2454 | << FoundFunction->getType(); |
| 2455 | } |
| 2456 | } |
| 2457 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2458 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2459 | } |
| 2460 | |
| 2461 | if (!ConflictingDecls.empty()) { |
| 2462 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2463 | ConflictingDecls.data(), |
| 2464 | ConflictingDecls.size()); |
| 2465 | if (!Name) |
| 2466 | return 0; |
| 2467 | } |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 2468 | } |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2469 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2470 | DeclarationNameInfo NameInfo(Name, Loc); |
| 2471 | // Import additional name location/type info. |
| 2472 | ImportDeclarationNameLoc(D->getNameInfo(), NameInfo); |
| 2473 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2474 | // Import the type. |
| 2475 | QualType T = Importer.Import(D->getType()); |
| 2476 | if (T.isNull()) |
| 2477 | return 0; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2478 | |
| 2479 | // Import the function parameters. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2480 | SmallVector<ParmVarDecl *, 8> Parameters; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2481 | for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end(); |
| 2482 | P != PEnd; ++P) { |
| 2483 | ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P)); |
| 2484 | if (!ToP) |
| 2485 | return 0; |
| 2486 | |
| 2487 | Parameters.push_back(ToP); |
| 2488 | } |
| 2489 | |
| 2490 | // Create the imported function. |
| 2491 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2492 | FunctionDecl *ToFunction = 0; |
| 2493 | if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) { |
| 2494 | ToFunction = CXXConstructorDecl::Create(Importer.getToContext(), |
| 2495 | cast<CXXRecordDecl>(DC), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2496 | D->getInnerLocStart(), |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2497 | NameInfo, T, TInfo, |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2498 | FromConstructor->isExplicit(), |
| 2499 | D->isInlineSpecified(), |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 2500 | D->isImplicit(), |
| 2501 | D->isConstexpr()); |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2502 | } else if (isa<CXXDestructorDecl>(D)) { |
| 2503 | ToFunction = CXXDestructorDecl::Create(Importer.getToContext(), |
| 2504 | cast<CXXRecordDecl>(DC), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2505 | D->getInnerLocStart(), |
Craig Silverstein | af8808d | 2010-10-21 00:44:50 +0000 | [diff] [blame] | 2506 | NameInfo, T, TInfo, |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2507 | D->isInlineSpecified(), |
| 2508 | D->isImplicit()); |
| 2509 | } else if (CXXConversionDecl *FromConversion |
| 2510 | = dyn_cast<CXXConversionDecl>(D)) { |
| 2511 | ToFunction = CXXConversionDecl::Create(Importer.getToContext(), |
| 2512 | cast<CXXRecordDecl>(DC), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2513 | D->getInnerLocStart(), |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2514 | NameInfo, T, TInfo, |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2515 | D->isInlineSpecified(), |
Douglas Gregor | f2f0806 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 2516 | FromConversion->isExplicit(), |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 2517 | D->isConstexpr(), |
Douglas Gregor | f2f0806 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 2518 | Importer.Import(D->getLocEnd())); |
Douglas Gregor | a50ad13 | 2010-11-29 16:04:58 +0000 | [diff] [blame] | 2519 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 2520 | ToFunction = CXXMethodDecl::Create(Importer.getToContext(), |
| 2521 | cast<CXXRecordDecl>(DC), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2522 | D->getInnerLocStart(), |
Douglas Gregor | a50ad13 | 2010-11-29 16:04:58 +0000 | [diff] [blame] | 2523 | NameInfo, T, TInfo, |
| 2524 | Method->isStatic(), |
| 2525 | Method->getStorageClassAsWritten(), |
Douglas Gregor | f2f0806 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 2526 | Method->isInlineSpecified(), |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 2527 | D->isConstexpr(), |
Douglas Gregor | f2f0806 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 2528 | Importer.Import(D->getLocEnd())); |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2529 | } else { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2530 | ToFunction = FunctionDecl::Create(Importer.getToContext(), DC, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2531 | D->getInnerLocStart(), |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2532 | NameInfo, T, TInfo, D->getStorageClass(), |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2533 | D->getStorageClassAsWritten(), |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2534 | D->isInlineSpecified(), |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 2535 | D->hasWrittenPrototype(), |
| 2536 | D->isConstexpr()); |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2537 | } |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2538 | |
| 2539 | // Import the qualifier, if any. |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2540 | ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2541 | ToFunction->setAccess(D->getAccess()); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2542 | ToFunction->setLexicalDeclContext(LexicalDC); |
John McCall | 08432c8 | 2011-01-27 02:37:01 +0000 | [diff] [blame] | 2543 | ToFunction->setVirtualAsWritten(D->isVirtualAsWritten()); |
| 2544 | ToFunction->setTrivial(D->isTrivial()); |
| 2545 | ToFunction->setPure(D->isPure()); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2546 | Importer.Imported(D, ToFunction); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 2547 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2548 | // Set the parameters. |
| 2549 | for (unsigned I = 0, N = Parameters.size(); I != N; ++I) { |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2550 | Parameters[I]->setOwningFunction(ToFunction); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2551 | ToFunction->addDeclInternal(Parameters[I]); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2552 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 2553 | ToFunction->setParams(Parameters); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2554 | |
| 2555 | // FIXME: Other bits to merge? |
Douglas Gregor | 0eaa2bf | 2010-10-01 23:55:07 +0000 | [diff] [blame] | 2556 | |
| 2557 | // Add this function to the lexical context. |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2558 | LexicalDC->addDeclInternal(ToFunction); |
Douglas Gregor | 0eaa2bf | 2010-10-01 23:55:07 +0000 | [diff] [blame] | 2559 | |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2560 | return ToFunction; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2561 | } |
| 2562 | |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2563 | Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) { |
| 2564 | return VisitFunctionDecl(D); |
| 2565 | } |
| 2566 | |
| 2567 | Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
| 2568 | return VisitCXXMethodDecl(D); |
| 2569 | } |
| 2570 | |
| 2571 | Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
| 2572 | return VisitCXXMethodDecl(D); |
| 2573 | } |
| 2574 | |
| 2575 | Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) { |
| 2576 | return VisitCXXMethodDecl(D); |
| 2577 | } |
| 2578 | |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2579 | Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) { |
| 2580 | // Import the major distinguishing characteristics of a variable. |
| 2581 | DeclContext *DC, *LexicalDC; |
| 2582 | DeclarationName Name; |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2583 | SourceLocation Loc; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2584 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 2585 | return 0; |
| 2586 | |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 2587 | // Determine whether we've already imported this field. |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2588 | llvm::SmallVector<NamedDecl *, 2> FoundDecls; |
| 2589 | DC->localUncachedLookup(Name, FoundDecls); |
| 2590 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2591 | if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) { |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 2592 | if (Importer.IsStructurallyEquivalent(D->getType(), |
| 2593 | FoundField->getType())) { |
| 2594 | Importer.Imported(D, FoundField); |
| 2595 | return FoundField; |
| 2596 | } |
| 2597 | |
| 2598 | Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent) |
| 2599 | << Name << D->getType() << FoundField->getType(); |
| 2600 | Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) |
| 2601 | << FoundField->getType(); |
| 2602 | return 0; |
| 2603 | } |
| 2604 | } |
| 2605 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2606 | // Import the type. |
| 2607 | QualType T = Importer.Import(D->getType()); |
| 2608 | if (T.isNull()) |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2609 | return 0; |
| 2610 | |
| 2611 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 2612 | Expr *BitWidth = Importer.Import(D->getBitWidth()); |
| 2613 | if (!BitWidth && D->getBitWidth()) |
| 2614 | return 0; |
| 2615 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2616 | FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC, |
| 2617 | Importer.Import(D->getInnerLocStart()), |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2618 | Loc, Name.getAsIdentifierInfo(), |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2619 | T, TInfo, BitWidth, D->isMutable(), |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2620 | D->getInClassInitStyle()); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2621 | ToField->setAccess(D->getAccess()); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2622 | ToField->setLexicalDeclContext(LexicalDC); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2623 | if (ToField->hasInClassInitializer()) |
| 2624 | ToField->setInClassInitializer(D->getInClassInitializer()); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2625 | Importer.Imported(D, ToField); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2626 | LexicalDC->addDeclInternal(ToField); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2627 | return ToField; |
| 2628 | } |
| 2629 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2630 | Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) { |
| 2631 | // Import the major distinguishing characteristics of a variable. |
| 2632 | DeclContext *DC, *LexicalDC; |
| 2633 | DeclarationName Name; |
| 2634 | SourceLocation Loc; |
| 2635 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 2636 | return 0; |
| 2637 | |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 2638 | // Determine whether we've already imported this field. |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2639 | llvm::SmallVector<NamedDecl *, 2> FoundDecls; |
| 2640 | DC->localUncachedLookup(Name, FoundDecls); |
| 2641 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 2642 | if (IndirectFieldDecl *FoundField |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2643 | = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) { |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 2644 | if (Importer.IsStructurallyEquivalent(D->getType(), |
| 2645 | FoundField->getType())) { |
| 2646 | Importer.Imported(D, FoundField); |
| 2647 | return FoundField; |
| 2648 | } |
| 2649 | |
| 2650 | Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent) |
| 2651 | << Name << D->getType() << FoundField->getType(); |
| 2652 | Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) |
| 2653 | << FoundField->getType(); |
| 2654 | return 0; |
| 2655 | } |
| 2656 | } |
| 2657 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2658 | // Import the type. |
| 2659 | QualType T = Importer.Import(D->getType()); |
| 2660 | if (T.isNull()) |
| 2661 | return 0; |
| 2662 | |
| 2663 | NamedDecl **NamedChain = |
| 2664 | new (Importer.getToContext())NamedDecl*[D->getChainingSize()]; |
| 2665 | |
| 2666 | unsigned i = 0; |
| 2667 | for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(), |
| 2668 | PE = D->chain_end(); PI != PE; ++PI) { |
| 2669 | Decl* D = Importer.Import(*PI); |
| 2670 | if (!D) |
| 2671 | return 0; |
| 2672 | NamedChain[i++] = cast<NamedDecl>(D); |
| 2673 | } |
| 2674 | |
| 2675 | IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create( |
| 2676 | Importer.getToContext(), DC, |
| 2677 | Loc, Name.getAsIdentifierInfo(), T, |
| 2678 | NamedChain, D->getChainingSize()); |
| 2679 | ToIndirectField->setAccess(D->getAccess()); |
| 2680 | ToIndirectField->setLexicalDeclContext(LexicalDC); |
| 2681 | Importer.Imported(D, ToIndirectField); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2682 | LexicalDC->addDeclInternal(ToIndirectField); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2683 | return ToIndirectField; |
| 2684 | } |
| 2685 | |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 2686 | Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) { |
| 2687 | // Import the major distinguishing characteristics of an ivar. |
| 2688 | DeclContext *DC, *LexicalDC; |
| 2689 | DeclarationName Name; |
| 2690 | SourceLocation Loc; |
| 2691 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 2692 | return 0; |
| 2693 | |
| 2694 | // Determine whether we've already imported this ivar |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2695 | llvm::SmallVector<NamedDecl *, 2> FoundDecls; |
| 2696 | DC->localUncachedLookup(Name, FoundDecls); |
| 2697 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2698 | if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) { |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 2699 | if (Importer.IsStructurallyEquivalent(D->getType(), |
| 2700 | FoundIvar->getType())) { |
| 2701 | Importer.Imported(D, FoundIvar); |
| 2702 | return FoundIvar; |
| 2703 | } |
| 2704 | |
| 2705 | Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent) |
| 2706 | << Name << D->getType() << FoundIvar->getType(); |
| 2707 | Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here) |
| 2708 | << FoundIvar->getType(); |
| 2709 | return 0; |
| 2710 | } |
| 2711 | } |
| 2712 | |
| 2713 | // Import the type. |
| 2714 | QualType T = Importer.Import(D->getType()); |
| 2715 | if (T.isNull()) |
| 2716 | return 0; |
| 2717 | |
| 2718 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 2719 | Expr *BitWidth = Importer.Import(D->getBitWidth()); |
| 2720 | if (!BitWidth && D->getBitWidth()) |
| 2721 | return 0; |
| 2722 | |
Daniel Dunbar | fe3ead7 | 2010-04-02 20:10:03 +0000 | [diff] [blame] | 2723 | ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(), |
| 2724 | cast<ObjCContainerDecl>(DC), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2725 | Importer.Import(D->getInnerLocStart()), |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 2726 | Loc, Name.getAsIdentifierInfo(), |
| 2727 | T, TInfo, D->getAccessControl(), |
Fariborz Jahanian | aea8e1e | 2010-07-17 18:35:47 +0000 | [diff] [blame] | 2728 | BitWidth, D->getSynthesize()); |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 2729 | ToIvar->setLexicalDeclContext(LexicalDC); |
| 2730 | Importer.Imported(D, ToIvar); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2731 | LexicalDC->addDeclInternal(ToIvar); |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 2732 | return ToIvar; |
| 2733 | |
| 2734 | } |
| 2735 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2736 | Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) { |
| 2737 | // Import the major distinguishing characteristics of a variable. |
| 2738 | DeclContext *DC, *LexicalDC; |
| 2739 | DeclarationName Name; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2740 | SourceLocation Loc; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2741 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2742 | return 0; |
| 2743 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2744 | // Try to find a variable in our own ("to") context with the same name and |
| 2745 | // in the same context as the variable we're importing. |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 2746 | if (D->isFileVarDecl()) { |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2747 | VarDecl *MergeWithVar = 0; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2748 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2749 | unsigned IDNS = Decl::IDNS_Ordinary; |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2750 | llvm::SmallVector<NamedDecl *, 2> FoundDecls; |
| 2751 | DC->localUncachedLookup(Name, FoundDecls); |
| 2752 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2753 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2754 | continue; |
| 2755 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2756 | if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) { |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2757 | // We have found a variable that we may need to merge with. Check it. |
| 2758 | if (isExternalLinkage(FoundVar->getLinkage()) && |
| 2759 | isExternalLinkage(D->getLinkage())) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2760 | if (Importer.IsStructurallyEquivalent(D->getType(), |
| 2761 | FoundVar->getType())) { |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2762 | MergeWithVar = FoundVar; |
| 2763 | break; |
| 2764 | } |
| 2765 | |
Douglas Gregor | 56521c5 | 2010-02-12 17:23:39 +0000 | [diff] [blame] | 2766 | const ArrayType *FoundArray |
| 2767 | = Importer.getToContext().getAsArrayType(FoundVar->getType()); |
| 2768 | const ArrayType *TArray |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2769 | = Importer.getToContext().getAsArrayType(D->getType()); |
Douglas Gregor | 56521c5 | 2010-02-12 17:23:39 +0000 | [diff] [blame] | 2770 | if (FoundArray && TArray) { |
| 2771 | if (isa<IncompleteArrayType>(FoundArray) && |
| 2772 | isa<ConstantArrayType>(TArray)) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2773 | // Import the type. |
| 2774 | QualType T = Importer.Import(D->getType()); |
| 2775 | if (T.isNull()) |
| 2776 | return 0; |
| 2777 | |
Douglas Gregor | 56521c5 | 2010-02-12 17:23:39 +0000 | [diff] [blame] | 2778 | FoundVar->setType(T); |
| 2779 | MergeWithVar = FoundVar; |
| 2780 | break; |
| 2781 | } else if (isa<IncompleteArrayType>(TArray) && |
| 2782 | isa<ConstantArrayType>(FoundArray)) { |
| 2783 | MergeWithVar = FoundVar; |
| 2784 | break; |
Douglas Gregor | 2fbe558 | 2010-02-10 17:16:49 +0000 | [diff] [blame] | 2785 | } |
| 2786 | } |
| 2787 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2788 | Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent) |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2789 | << Name << D->getType() << FoundVar->getType(); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2790 | Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here) |
| 2791 | << FoundVar->getType(); |
| 2792 | } |
| 2793 | } |
| 2794 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2795 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2796 | } |
| 2797 | |
| 2798 | if (MergeWithVar) { |
| 2799 | // An equivalent variable with external linkage has been found. Link |
| 2800 | // the two declarations, then merge them. |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2801 | Importer.Imported(D, MergeWithVar); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2802 | |
| 2803 | if (VarDecl *DDef = D->getDefinition()) { |
| 2804 | if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) { |
| 2805 | Importer.ToDiag(ExistingDef->getLocation(), |
| 2806 | diag::err_odr_variable_multiple_def) |
| 2807 | << Name; |
| 2808 | Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here); |
| 2809 | } else { |
| 2810 | Expr *Init = Importer.Import(DDef->getInit()); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2811 | MergeWithVar->setInit(Init); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2812 | if (DDef->isInitKnownICE()) { |
| 2813 | EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt(); |
| 2814 | Eval->CheckedICE = true; |
| 2815 | Eval->IsICE = DDef->isInitICE(); |
| 2816 | } |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2817 | } |
| 2818 | } |
| 2819 | |
| 2820 | return MergeWithVar; |
| 2821 | } |
| 2822 | |
| 2823 | if (!ConflictingDecls.empty()) { |
| 2824 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2825 | ConflictingDecls.data(), |
| 2826 | ConflictingDecls.size()); |
| 2827 | if (!Name) |
| 2828 | return 0; |
| 2829 | } |
| 2830 | } |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 2831 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2832 | // Import the type. |
| 2833 | QualType T = Importer.Import(D->getType()); |
| 2834 | if (T.isNull()) |
| 2835 | return 0; |
| 2836 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2837 | // Create the imported variable. |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 2838 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2839 | VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, |
| 2840 | Importer.Import(D->getInnerLocStart()), |
| 2841 | Loc, Name.getAsIdentifierInfo(), |
| 2842 | T, TInfo, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2843 | D->getStorageClass(), |
| 2844 | D->getStorageClassAsWritten()); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2845 | ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2846 | ToVar->setAccess(D->getAccess()); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 2847 | ToVar->setLexicalDeclContext(LexicalDC); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2848 | Importer.Imported(D, ToVar); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2849 | LexicalDC->addDeclInternal(ToVar); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 2850 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2851 | // Merge the initializer. |
| 2852 | // FIXME: Can we really import any initializer? Alternatively, we could force |
| 2853 | // ourselves to import every declaration of a variable and then only use |
| 2854 | // getInit() here. |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2855 | ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer()))); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2856 | |
| 2857 | // FIXME: Other bits to merge? |
| 2858 | |
| 2859 | return ToVar; |
| 2860 | } |
| 2861 | |
Douglas Gregor | 8b228d7 | 2010-02-17 21:22:52 +0000 | [diff] [blame] | 2862 | Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) { |
| 2863 | // Parameters are created in the translation unit's context, then moved |
| 2864 | // into the function declaration's context afterward. |
| 2865 | DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); |
| 2866 | |
| 2867 | // Import the name of this declaration. |
| 2868 | DeclarationName Name = Importer.Import(D->getDeclName()); |
| 2869 | if (D->getDeclName() && !Name) |
| 2870 | return 0; |
| 2871 | |
| 2872 | // Import the location of this declaration. |
| 2873 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 2874 | |
| 2875 | // Import the parameter's type. |
| 2876 | QualType T = Importer.Import(D->getType()); |
| 2877 | if (T.isNull()) |
| 2878 | return 0; |
| 2879 | |
| 2880 | // Create the imported parameter. |
| 2881 | ImplicitParamDecl *ToParm |
| 2882 | = ImplicitParamDecl::Create(Importer.getToContext(), DC, |
| 2883 | Loc, Name.getAsIdentifierInfo(), |
| 2884 | T); |
| 2885 | return Importer.Imported(D, ToParm); |
| 2886 | } |
| 2887 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2888 | Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) { |
| 2889 | // Parameters are created in the translation unit's context, then moved |
| 2890 | // into the function declaration's context afterward. |
| 2891 | DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); |
| 2892 | |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 2893 | // Import the name of this declaration. |
| 2894 | DeclarationName Name = Importer.Import(D->getDeclName()); |
| 2895 | if (D->getDeclName() && !Name) |
| 2896 | return 0; |
| 2897 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2898 | // Import the location of this declaration. |
| 2899 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 2900 | |
| 2901 | // Import the parameter's type. |
| 2902 | QualType T = Importer.Import(D->getType()); |
| 2903 | if (T.isNull()) |
| 2904 | return 0; |
| 2905 | |
| 2906 | // Create the imported parameter. |
| 2907 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 2908 | ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2909 | Importer.Import(D->getInnerLocStart()), |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2910 | Loc, Name.getAsIdentifierInfo(), |
| 2911 | T, TInfo, D->getStorageClass(), |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2912 | D->getStorageClassAsWritten(), |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2913 | /*FIXME: Default argument*/ 0); |
John McCall | f3cd665 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 2914 | ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg()); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2915 | return Importer.Imported(D, ToParm); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2916 | } |
| 2917 | |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2918 | Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) { |
| 2919 | // Import the major distinguishing characteristics of a method. |
| 2920 | DeclContext *DC, *LexicalDC; |
| 2921 | DeclarationName Name; |
| 2922 | SourceLocation Loc; |
| 2923 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 2924 | return 0; |
| 2925 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2926 | llvm::SmallVector<NamedDecl *, 2> FoundDecls; |
| 2927 | DC->localUncachedLookup(Name, FoundDecls); |
| 2928 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2929 | if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) { |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2930 | if (FoundMethod->isInstanceMethod() != D->isInstanceMethod()) |
| 2931 | continue; |
| 2932 | |
| 2933 | // Check return types. |
| 2934 | if (!Importer.IsStructurallyEquivalent(D->getResultType(), |
| 2935 | FoundMethod->getResultType())) { |
| 2936 | Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent) |
| 2937 | << D->isInstanceMethod() << Name |
| 2938 | << D->getResultType() << FoundMethod->getResultType(); |
| 2939 | Importer.ToDiag(FoundMethod->getLocation(), |
| 2940 | diag::note_odr_objc_method_here) |
| 2941 | << D->isInstanceMethod() << Name; |
| 2942 | return 0; |
| 2943 | } |
| 2944 | |
| 2945 | // Check the number of parameters. |
| 2946 | if (D->param_size() != FoundMethod->param_size()) { |
| 2947 | Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent) |
| 2948 | << D->isInstanceMethod() << Name |
| 2949 | << D->param_size() << FoundMethod->param_size(); |
| 2950 | Importer.ToDiag(FoundMethod->getLocation(), |
| 2951 | diag::note_odr_objc_method_here) |
| 2952 | << D->isInstanceMethod() << Name; |
| 2953 | return 0; |
| 2954 | } |
| 2955 | |
| 2956 | // Check parameter types. |
| 2957 | for (ObjCMethodDecl::param_iterator P = D->param_begin(), |
| 2958 | PEnd = D->param_end(), FoundP = FoundMethod->param_begin(); |
| 2959 | P != PEnd; ++P, ++FoundP) { |
| 2960 | if (!Importer.IsStructurallyEquivalent((*P)->getType(), |
| 2961 | (*FoundP)->getType())) { |
| 2962 | Importer.FromDiag((*P)->getLocation(), |
| 2963 | diag::err_odr_objc_method_param_type_inconsistent) |
| 2964 | << D->isInstanceMethod() << Name |
| 2965 | << (*P)->getType() << (*FoundP)->getType(); |
| 2966 | Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here) |
| 2967 | << (*FoundP)->getType(); |
| 2968 | return 0; |
| 2969 | } |
| 2970 | } |
| 2971 | |
| 2972 | // Check variadic/non-variadic. |
| 2973 | // Check the number of parameters. |
| 2974 | if (D->isVariadic() != FoundMethod->isVariadic()) { |
| 2975 | Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent) |
| 2976 | << D->isInstanceMethod() << Name; |
| 2977 | Importer.ToDiag(FoundMethod->getLocation(), |
| 2978 | diag::note_odr_objc_method_here) |
| 2979 | << D->isInstanceMethod() << Name; |
| 2980 | return 0; |
| 2981 | } |
| 2982 | |
| 2983 | // FIXME: Any other bits we need to merge? |
| 2984 | return Importer.Imported(D, FoundMethod); |
| 2985 | } |
| 2986 | } |
| 2987 | |
| 2988 | // Import the result type. |
| 2989 | QualType ResultTy = Importer.Import(D->getResultType()); |
| 2990 | if (ResultTy.isNull()) |
| 2991 | return 0; |
| 2992 | |
Douglas Gregor | 12852d9 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 2993 | TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo()); |
| 2994 | |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2995 | ObjCMethodDecl *ToMethod |
| 2996 | = ObjCMethodDecl::Create(Importer.getToContext(), |
| 2997 | Loc, |
| 2998 | Importer.Import(D->getLocEnd()), |
| 2999 | Name.getObjCSelector(), |
Douglas Gregor | 12852d9 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 3000 | ResultTy, ResultTInfo, DC, |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3001 | D->isInstanceMethod(), |
| 3002 | D->isVariadic(), |
| 3003 | D->isSynthesized(), |
Argyrios Kyrtzidis | 004df6e | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 3004 | D->isImplicit(), |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 3005 | D->isDefined(), |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3006 | D->getImplementationControl(), |
| 3007 | D->hasRelatedResultType()); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3008 | |
| 3009 | // FIXME: When we decide to merge method definitions, we'll need to |
| 3010 | // deal with implicit parameters. |
| 3011 | |
| 3012 | // Import the parameters |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3013 | SmallVector<ParmVarDecl *, 5> ToParams; |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3014 | for (ObjCMethodDecl::param_iterator FromP = D->param_begin(), |
| 3015 | FromPEnd = D->param_end(); |
| 3016 | FromP != FromPEnd; |
| 3017 | ++FromP) { |
| 3018 | ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP)); |
| 3019 | if (!ToP) |
| 3020 | return 0; |
| 3021 | |
| 3022 | ToParams.push_back(ToP); |
| 3023 | } |
| 3024 | |
| 3025 | // Set the parameters. |
| 3026 | for (unsigned I = 0, N = ToParams.size(); I != N; ++I) { |
| 3027 | ToParams[I]->setOwningFunction(ToMethod); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3028 | ToMethod->addDeclInternal(ToParams[I]); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3029 | } |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 3030 | SmallVector<SourceLocation, 12> SelLocs; |
| 3031 | D->getSelectorLocs(SelLocs); |
| 3032 | ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3033 | |
| 3034 | ToMethod->setLexicalDeclContext(LexicalDC); |
| 3035 | Importer.Imported(D, ToMethod); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3036 | LexicalDC->addDeclInternal(ToMethod); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3037 | return ToMethod; |
| 3038 | } |
| 3039 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3040 | Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) { |
| 3041 | // Import the major distinguishing characteristics of a category. |
| 3042 | DeclContext *DC, *LexicalDC; |
| 3043 | DeclarationName Name; |
| 3044 | SourceLocation Loc; |
| 3045 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 3046 | return 0; |
| 3047 | |
| 3048 | ObjCInterfaceDecl *ToInterface |
| 3049 | = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface())); |
| 3050 | if (!ToInterface) |
| 3051 | return 0; |
| 3052 | |
| 3053 | // Determine if we've already encountered this category. |
| 3054 | ObjCCategoryDecl *MergeWithCategory |
| 3055 | = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo()); |
| 3056 | ObjCCategoryDecl *ToCategory = MergeWithCategory; |
| 3057 | if (!ToCategory) { |
| 3058 | ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC, |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 3059 | Importer.Import(D->getAtStartLoc()), |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3060 | Loc, |
| 3061 | Importer.Import(D->getCategoryNameLoc()), |
Argyrios Kyrtzidis | 3a5094b | 2011-08-30 19:43:26 +0000 | [diff] [blame] | 3062 | Name.getAsIdentifierInfo(), |
Fariborz Jahanian | a7765fe | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 3063 | ToInterface, |
| 3064 | Importer.Import(D->getIvarLBraceLoc()), |
| 3065 | Importer.Import(D->getIvarRBraceLoc())); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3066 | ToCategory->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3067 | LexicalDC->addDeclInternal(ToCategory); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3068 | Importer.Imported(D, ToCategory); |
| 3069 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3070 | // Import protocols |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3071 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 3072 | SmallVector<SourceLocation, 4> ProtocolLocs; |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3073 | ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc |
| 3074 | = D->protocol_loc_begin(); |
| 3075 | for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(), |
| 3076 | FromProtoEnd = D->protocol_end(); |
| 3077 | FromProto != FromProtoEnd; |
| 3078 | ++FromProto, ++FromProtoLoc) { |
| 3079 | ObjCProtocolDecl *ToProto |
| 3080 | = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); |
| 3081 | if (!ToProto) |
| 3082 | return 0; |
| 3083 | Protocols.push_back(ToProto); |
| 3084 | ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); |
| 3085 | } |
| 3086 | |
| 3087 | // FIXME: If we're merging, make sure that the protocol list is the same. |
| 3088 | ToCategory->setProtocolList(Protocols.data(), Protocols.size(), |
| 3089 | ProtocolLocs.data(), Importer.getToContext()); |
| 3090 | |
| 3091 | } else { |
| 3092 | Importer.Imported(D, ToCategory); |
| 3093 | } |
| 3094 | |
| 3095 | // Import all of the members of this category. |
Douglas Gregor | 968d633 | 2010-02-21 18:24:45 +0000 | [diff] [blame] | 3096 | ImportDeclContext(D); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3097 | |
| 3098 | // If we have an implementation, import it as well. |
| 3099 | if (D->getImplementation()) { |
| 3100 | ObjCCategoryImplDecl *Impl |
Douglas Gregor | 35fd7bc | 2010-12-08 16:41:55 +0000 | [diff] [blame] | 3101 | = cast_or_null<ObjCCategoryImplDecl>( |
| 3102 | Importer.Import(D->getImplementation())); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3103 | if (!Impl) |
| 3104 | return 0; |
| 3105 | |
| 3106 | ToCategory->setImplementation(Impl); |
| 3107 | } |
| 3108 | |
| 3109 | return ToCategory; |
| 3110 | } |
| 3111 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3112 | bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From, |
| 3113 | ObjCProtocolDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 3114 | ImportDefinitionKind Kind) { |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3115 | if (To->getDefinition()) { |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 3116 | if (shouldForceImportDeclContext(Kind)) |
| 3117 | ImportDeclContext(From); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3118 | return false; |
| 3119 | } |
| 3120 | |
| 3121 | // Start the protocol definition |
| 3122 | To->startDefinition(); |
| 3123 | |
| 3124 | // Import protocols |
| 3125 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 3126 | SmallVector<SourceLocation, 4> ProtocolLocs; |
| 3127 | ObjCProtocolDecl::protocol_loc_iterator |
| 3128 | FromProtoLoc = From->protocol_loc_begin(); |
| 3129 | for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(), |
| 3130 | FromProtoEnd = From->protocol_end(); |
| 3131 | FromProto != FromProtoEnd; |
| 3132 | ++FromProto, ++FromProtoLoc) { |
| 3133 | ObjCProtocolDecl *ToProto |
| 3134 | = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); |
| 3135 | if (!ToProto) |
| 3136 | return true; |
| 3137 | Protocols.push_back(ToProto); |
| 3138 | ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); |
| 3139 | } |
| 3140 | |
| 3141 | // FIXME: If we're merging, make sure that the protocol list is the same. |
| 3142 | To->setProtocolList(Protocols.data(), Protocols.size(), |
| 3143 | ProtocolLocs.data(), Importer.getToContext()); |
| 3144 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 3145 | if (shouldForceImportDeclContext(Kind)) { |
| 3146 | // Import all of the members of this protocol. |
| 3147 | ImportDeclContext(From, /*ForceImport=*/true); |
| 3148 | } |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3149 | return false; |
| 3150 | } |
| 3151 | |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3152 | Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) { |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3153 | // If this protocol has a definition in the translation unit we're coming |
| 3154 | // from, but this particular declaration is not that definition, import the |
| 3155 | // definition and map to that. |
| 3156 | ObjCProtocolDecl *Definition = D->getDefinition(); |
| 3157 | if (Definition && Definition != D) { |
| 3158 | Decl *ImportedDef = Importer.Import(Definition); |
| 3159 | if (!ImportedDef) |
| 3160 | return 0; |
| 3161 | |
| 3162 | return Importer.Imported(D, ImportedDef); |
| 3163 | } |
| 3164 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3165 | // Import the major distinguishing characteristics of a protocol. |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3166 | DeclContext *DC, *LexicalDC; |
| 3167 | DeclarationName Name; |
| 3168 | SourceLocation Loc; |
| 3169 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 3170 | return 0; |
| 3171 | |
| 3172 | ObjCProtocolDecl *MergeWithProtocol = 0; |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3173 | llvm::SmallVector<NamedDecl *, 2> FoundDecls; |
| 3174 | DC->localUncachedLookup(Name, FoundDecls); |
| 3175 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 3176 | if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol)) |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3177 | continue; |
| 3178 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3179 | if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I]))) |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3180 | break; |
| 3181 | } |
| 3182 | |
| 3183 | ObjCProtocolDecl *ToProto = MergeWithProtocol; |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3184 | if (!ToProto) { |
| 3185 | ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, |
| 3186 | Name.getAsIdentifierInfo(), Loc, |
| 3187 | Importer.Import(D->getAtStartLoc()), |
| 3188 | /*PrevDecl=*/0); |
| 3189 | ToProto->setLexicalDeclContext(LexicalDC); |
| 3190 | LexicalDC->addDeclInternal(ToProto); |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3191 | } |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3192 | |
| 3193 | Importer.Imported(D, ToProto); |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3194 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3195 | if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto)) |
| 3196 | return 0; |
| 3197 | |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3198 | return ToProto; |
| 3199 | } |
| 3200 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3201 | bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From, |
| 3202 | ObjCInterfaceDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 3203 | ImportDefinitionKind Kind) { |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3204 | if (To->getDefinition()) { |
| 3205 | // Check consistency of superclass. |
| 3206 | ObjCInterfaceDecl *FromSuper = From->getSuperClass(); |
| 3207 | if (FromSuper) { |
| 3208 | FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper)); |
| 3209 | if (!FromSuper) |
| 3210 | return true; |
| 3211 | } |
| 3212 | |
| 3213 | ObjCInterfaceDecl *ToSuper = To->getSuperClass(); |
| 3214 | if ((bool)FromSuper != (bool)ToSuper || |
| 3215 | (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) { |
| 3216 | Importer.ToDiag(To->getLocation(), |
| 3217 | diag::err_odr_objc_superclass_inconsistent) |
| 3218 | << To->getDeclName(); |
| 3219 | if (ToSuper) |
| 3220 | Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass) |
| 3221 | << To->getSuperClass()->getDeclName(); |
| 3222 | else |
| 3223 | Importer.ToDiag(To->getLocation(), |
| 3224 | diag::note_odr_objc_missing_superclass); |
| 3225 | if (From->getSuperClass()) |
| 3226 | Importer.FromDiag(From->getSuperClassLoc(), |
| 3227 | diag::note_odr_objc_superclass) |
| 3228 | << From->getSuperClass()->getDeclName(); |
| 3229 | else |
| 3230 | Importer.FromDiag(From->getLocation(), |
| 3231 | diag::note_odr_objc_missing_superclass); |
| 3232 | } |
| 3233 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 3234 | if (shouldForceImportDeclContext(Kind)) |
| 3235 | ImportDeclContext(From); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3236 | return false; |
| 3237 | } |
| 3238 | |
| 3239 | // Start the definition. |
| 3240 | To->startDefinition(); |
| 3241 | |
| 3242 | // If this class has a superclass, import it. |
| 3243 | if (From->getSuperClass()) { |
| 3244 | ObjCInterfaceDecl *Super = cast_or_null<ObjCInterfaceDecl>( |
| 3245 | Importer.Import(From->getSuperClass())); |
| 3246 | if (!Super) |
| 3247 | return true; |
| 3248 | |
| 3249 | To->setSuperClass(Super); |
| 3250 | To->setSuperClassLoc(Importer.Import(From->getSuperClassLoc())); |
| 3251 | } |
| 3252 | |
| 3253 | // Import protocols |
| 3254 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 3255 | SmallVector<SourceLocation, 4> ProtocolLocs; |
| 3256 | ObjCInterfaceDecl::protocol_loc_iterator |
| 3257 | FromProtoLoc = From->protocol_loc_begin(); |
| 3258 | |
| 3259 | for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(), |
| 3260 | FromProtoEnd = From->protocol_end(); |
| 3261 | FromProto != FromProtoEnd; |
| 3262 | ++FromProto, ++FromProtoLoc) { |
| 3263 | ObjCProtocolDecl *ToProto |
| 3264 | = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); |
| 3265 | if (!ToProto) |
| 3266 | return true; |
| 3267 | Protocols.push_back(ToProto); |
| 3268 | ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); |
| 3269 | } |
| 3270 | |
| 3271 | // FIXME: If we're merging, make sure that the protocol list is the same. |
| 3272 | To->setProtocolList(Protocols.data(), Protocols.size(), |
| 3273 | ProtocolLocs.data(), Importer.getToContext()); |
| 3274 | |
| 3275 | // Import categories. When the categories themselves are imported, they'll |
| 3276 | // hook themselves into this interface. |
| 3277 | for (ObjCCategoryDecl *FromCat = From->getCategoryList(); FromCat; |
| 3278 | FromCat = FromCat->getNextClassCategory()) |
| 3279 | Importer.Import(FromCat); |
| 3280 | |
| 3281 | // If we have an @implementation, import it as well. |
| 3282 | if (From->getImplementation()) { |
| 3283 | ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>( |
| 3284 | Importer.Import(From->getImplementation())); |
| 3285 | if (!Impl) |
| 3286 | return true; |
| 3287 | |
| 3288 | To->setImplementation(Impl); |
| 3289 | } |
| 3290 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 3291 | if (shouldForceImportDeclContext(Kind)) { |
| 3292 | // Import all of the members of this class. |
| 3293 | ImportDeclContext(From, /*ForceImport=*/true); |
| 3294 | } |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3295 | return false; |
| 3296 | } |
| 3297 | |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3298 | Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3299 | // If this class has a definition in the translation unit we're coming from, |
| 3300 | // but this particular declaration is not that definition, import the |
| 3301 | // definition and map to that. |
| 3302 | ObjCInterfaceDecl *Definition = D->getDefinition(); |
| 3303 | if (Definition && Definition != D) { |
| 3304 | Decl *ImportedDef = Importer.Import(Definition); |
| 3305 | if (!ImportedDef) |
| 3306 | return 0; |
| 3307 | |
| 3308 | return Importer.Imported(D, ImportedDef); |
| 3309 | } |
| 3310 | |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3311 | // Import the major distinguishing characteristics of an @interface. |
| 3312 | DeclContext *DC, *LexicalDC; |
| 3313 | DeclarationName Name; |
| 3314 | SourceLocation Loc; |
| 3315 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 3316 | return 0; |
| 3317 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3318 | // Look for an existing interface with the same name. |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3319 | ObjCInterfaceDecl *MergeWithIface = 0; |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3320 | llvm::SmallVector<NamedDecl *, 2> FoundDecls; |
| 3321 | DC->localUncachedLookup(Name, FoundDecls); |
| 3322 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 3323 | if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3324 | continue; |
| 3325 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3326 | if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I]))) |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3327 | break; |
| 3328 | } |
| 3329 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3330 | // Create an interface declaration, if one does not already exist. |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3331 | ObjCInterfaceDecl *ToIface = MergeWithIface; |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3332 | if (!ToIface) { |
| 3333 | ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC, |
| 3334 | Importer.Import(D->getAtStartLoc()), |
| 3335 | Name.getAsIdentifierInfo(), |
| 3336 | /*PrevDecl=*/0,Loc, |
| 3337 | D->isImplicitInterfaceDecl()); |
| 3338 | ToIface->setLexicalDeclContext(LexicalDC); |
| 3339 | LexicalDC->addDeclInternal(ToIface); |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3340 | } |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3341 | Importer.Imported(D, ToIface); |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3342 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3343 | if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface)) |
| 3344 | return 0; |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3345 | |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3346 | return ToIface; |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3347 | } |
| 3348 | |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 3349 | Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 3350 | ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>( |
| 3351 | Importer.Import(D->getCategoryDecl())); |
| 3352 | if (!Category) |
| 3353 | return 0; |
| 3354 | |
| 3355 | ObjCCategoryImplDecl *ToImpl = Category->getImplementation(); |
| 3356 | if (!ToImpl) { |
| 3357 | DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| 3358 | if (!DC) |
| 3359 | return 0; |
| 3360 | |
Argyrios Kyrtzidis | 4996f5f | 2011-12-09 00:31:40 +0000 | [diff] [blame] | 3361 | SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc()); |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 3362 | ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC, |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 3363 | Importer.Import(D->getIdentifier()), |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 3364 | Category->getClassInterface(), |
| 3365 | Importer.Import(D->getLocation()), |
Argyrios Kyrtzidis | 4996f5f | 2011-12-09 00:31:40 +0000 | [diff] [blame] | 3366 | Importer.Import(D->getAtStartLoc()), |
| 3367 | CategoryNameLoc); |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 3368 | |
| 3369 | DeclContext *LexicalDC = DC; |
| 3370 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 3371 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 3372 | if (!LexicalDC) |
| 3373 | return 0; |
| 3374 | |
| 3375 | ToImpl->setLexicalDeclContext(LexicalDC); |
| 3376 | } |
| 3377 | |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3378 | LexicalDC->addDeclInternal(ToImpl); |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 3379 | Category->setImplementation(ToImpl); |
| 3380 | } |
| 3381 | |
| 3382 | Importer.Imported(D, ToImpl); |
Douglas Gregor | 35fd7bc | 2010-12-08 16:41:55 +0000 | [diff] [blame] | 3383 | ImportDeclContext(D); |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 3384 | return ToImpl; |
| 3385 | } |
| 3386 | |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 3387 | Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 3388 | // Find the corresponding interface. |
| 3389 | ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>( |
| 3390 | Importer.Import(D->getClassInterface())); |
| 3391 | if (!Iface) |
| 3392 | return 0; |
| 3393 | |
| 3394 | // Import the superclass, if any. |
| 3395 | ObjCInterfaceDecl *Super = 0; |
| 3396 | if (D->getSuperClass()) { |
| 3397 | Super = cast_or_null<ObjCInterfaceDecl>( |
| 3398 | Importer.Import(D->getSuperClass())); |
| 3399 | if (!Super) |
| 3400 | return 0; |
| 3401 | } |
| 3402 | |
| 3403 | ObjCImplementationDecl *Impl = Iface->getImplementation(); |
| 3404 | if (!Impl) { |
| 3405 | // We haven't imported an implementation yet. Create a new @implementation |
| 3406 | // now. |
| 3407 | Impl = ObjCImplementationDecl::Create(Importer.getToContext(), |
| 3408 | Importer.ImportContext(D->getDeclContext()), |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 3409 | Iface, Super, |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 3410 | Importer.Import(D->getLocation()), |
Fariborz Jahanian | a7765fe | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 3411 | Importer.Import(D->getAtStartLoc()), |
| 3412 | Importer.Import(D->getIvarLBraceLoc()), |
| 3413 | Importer.Import(D->getIvarRBraceLoc())); |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 3414 | |
| 3415 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 3416 | DeclContext *LexicalDC |
| 3417 | = Importer.ImportContext(D->getLexicalDeclContext()); |
| 3418 | if (!LexicalDC) |
| 3419 | return 0; |
| 3420 | Impl->setLexicalDeclContext(LexicalDC); |
| 3421 | } |
| 3422 | |
| 3423 | // Associate the implementation with the class it implements. |
| 3424 | Iface->setImplementation(Impl); |
| 3425 | Importer.Imported(D, Iface->getImplementation()); |
| 3426 | } else { |
| 3427 | Importer.Imported(D, Iface->getImplementation()); |
| 3428 | |
| 3429 | // Verify that the existing @implementation has the same superclass. |
| 3430 | if ((Super && !Impl->getSuperClass()) || |
| 3431 | (!Super && Impl->getSuperClass()) || |
| 3432 | (Super && Impl->getSuperClass() && |
Douglas Gregor | 0b144e1 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 3433 | !declaresSameEntity(Super->getCanonicalDecl(), Impl->getSuperClass()))) { |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 3434 | Importer.ToDiag(Impl->getLocation(), |
| 3435 | diag::err_odr_objc_superclass_inconsistent) |
| 3436 | << Iface->getDeclName(); |
| 3437 | // FIXME: It would be nice to have the location of the superclass |
| 3438 | // below. |
| 3439 | if (Impl->getSuperClass()) |
| 3440 | Importer.ToDiag(Impl->getLocation(), |
| 3441 | diag::note_odr_objc_superclass) |
| 3442 | << Impl->getSuperClass()->getDeclName(); |
| 3443 | else |
| 3444 | Importer.ToDiag(Impl->getLocation(), |
| 3445 | diag::note_odr_objc_missing_superclass); |
| 3446 | if (D->getSuperClass()) |
| 3447 | Importer.FromDiag(D->getLocation(), |
| 3448 | diag::note_odr_objc_superclass) |
| 3449 | << D->getSuperClass()->getDeclName(); |
| 3450 | else |
| 3451 | Importer.FromDiag(D->getLocation(), |
| 3452 | diag::note_odr_objc_missing_superclass); |
| 3453 | return 0; |
| 3454 | } |
| 3455 | } |
| 3456 | |
| 3457 | // Import all of the members of this @implementation. |
| 3458 | ImportDeclContext(D); |
| 3459 | |
| 3460 | return Impl; |
| 3461 | } |
| 3462 | |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 3463 | Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { |
| 3464 | // Import the major distinguishing characteristics of an @property. |
| 3465 | DeclContext *DC, *LexicalDC; |
| 3466 | DeclarationName Name; |
| 3467 | SourceLocation Loc; |
| 3468 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 3469 | return 0; |
| 3470 | |
| 3471 | // Check whether we have already imported this property. |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3472 | llvm::SmallVector<NamedDecl *, 2> FoundDecls; |
| 3473 | DC->localUncachedLookup(Name, FoundDecls); |
| 3474 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 3475 | if (ObjCPropertyDecl *FoundProp |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3476 | = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) { |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 3477 | // Check property types. |
| 3478 | if (!Importer.IsStructurallyEquivalent(D->getType(), |
| 3479 | FoundProp->getType())) { |
| 3480 | Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent) |
| 3481 | << Name << D->getType() << FoundProp->getType(); |
| 3482 | Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here) |
| 3483 | << FoundProp->getType(); |
| 3484 | return 0; |
| 3485 | } |
| 3486 | |
| 3487 | // FIXME: Check property attributes, getters, setters, etc.? |
| 3488 | |
| 3489 | // Consider these properties to be equivalent. |
| 3490 | Importer.Imported(D, FoundProp); |
| 3491 | return FoundProp; |
| 3492 | } |
| 3493 | } |
| 3494 | |
| 3495 | // Import the type. |
John McCall | 339bb66 | 2010-06-04 20:50:08 +0000 | [diff] [blame] | 3496 | TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo()); |
| 3497 | if (!T) |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 3498 | return 0; |
| 3499 | |
| 3500 | // Create the new property. |
| 3501 | ObjCPropertyDecl *ToProperty |
| 3502 | = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc, |
| 3503 | Name.getAsIdentifierInfo(), |
| 3504 | Importer.Import(D->getAtLoc()), |
Fariborz Jahanian | 86c2f5c | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 3505 | Importer.Import(D->getLParenLoc()), |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 3506 | T, |
| 3507 | D->getPropertyImplementation()); |
| 3508 | Importer.Imported(D, ToProperty); |
| 3509 | ToProperty->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3510 | LexicalDC->addDeclInternal(ToProperty); |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 3511 | |
| 3512 | ToProperty->setPropertyAttributes(D->getPropertyAttributes()); |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 3513 | ToProperty->setPropertyAttributesAsWritten( |
| 3514 | D->getPropertyAttributesAsWritten()); |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 3515 | ToProperty->setGetterName(Importer.Import(D->getGetterName())); |
| 3516 | ToProperty->setSetterName(Importer.Import(D->getSetterName())); |
| 3517 | ToProperty->setGetterMethodDecl( |
| 3518 | cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl()))); |
| 3519 | ToProperty->setSetterMethodDecl( |
| 3520 | cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl()))); |
| 3521 | ToProperty->setPropertyIvarDecl( |
| 3522 | cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl()))); |
| 3523 | return ToProperty; |
| 3524 | } |
| 3525 | |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 3526 | Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { |
| 3527 | ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>( |
| 3528 | Importer.Import(D->getPropertyDecl())); |
| 3529 | if (!Property) |
| 3530 | return 0; |
| 3531 | |
| 3532 | DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| 3533 | if (!DC) |
| 3534 | return 0; |
| 3535 | |
| 3536 | // Import the lexical declaration context. |
| 3537 | DeclContext *LexicalDC = DC; |
| 3538 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 3539 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 3540 | if (!LexicalDC) |
| 3541 | return 0; |
| 3542 | } |
| 3543 | |
| 3544 | ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC); |
| 3545 | if (!InImpl) |
| 3546 | return 0; |
| 3547 | |
| 3548 | // Import the ivar (for an @synthesize). |
| 3549 | ObjCIvarDecl *Ivar = 0; |
| 3550 | if (D->getPropertyIvarDecl()) { |
| 3551 | Ivar = cast_or_null<ObjCIvarDecl>( |
| 3552 | Importer.Import(D->getPropertyIvarDecl())); |
| 3553 | if (!Ivar) |
| 3554 | return 0; |
| 3555 | } |
| 3556 | |
| 3557 | ObjCPropertyImplDecl *ToImpl |
| 3558 | = InImpl->FindPropertyImplDecl(Property->getIdentifier()); |
| 3559 | if (!ToImpl) { |
| 3560 | ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC, |
| 3561 | Importer.Import(D->getLocStart()), |
| 3562 | Importer.Import(D->getLocation()), |
| 3563 | Property, |
| 3564 | D->getPropertyImplementation(), |
| 3565 | Ivar, |
| 3566 | Importer.Import(D->getPropertyIvarDeclLoc())); |
| 3567 | ToImpl->setLexicalDeclContext(LexicalDC); |
| 3568 | Importer.Imported(D, ToImpl); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3569 | LexicalDC->addDeclInternal(ToImpl); |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 3570 | } else { |
| 3571 | // Check that we have the same kind of property implementation (@synthesize |
| 3572 | // vs. @dynamic). |
| 3573 | if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) { |
| 3574 | Importer.ToDiag(ToImpl->getLocation(), |
| 3575 | diag::err_odr_objc_property_impl_kind_inconsistent) |
| 3576 | << Property->getDeclName() |
| 3577 | << (ToImpl->getPropertyImplementation() |
| 3578 | == ObjCPropertyImplDecl::Dynamic); |
| 3579 | Importer.FromDiag(D->getLocation(), |
| 3580 | diag::note_odr_objc_property_impl_kind) |
| 3581 | << D->getPropertyDecl()->getDeclName() |
| 3582 | << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); |
| 3583 | return 0; |
| 3584 | } |
| 3585 | |
| 3586 | // For @synthesize, check that we have the same |
| 3587 | if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize && |
| 3588 | Ivar != ToImpl->getPropertyIvarDecl()) { |
| 3589 | Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(), |
| 3590 | diag::err_odr_objc_synthesize_ivar_inconsistent) |
| 3591 | << Property->getDeclName() |
| 3592 | << ToImpl->getPropertyIvarDecl()->getDeclName() |
| 3593 | << Ivar->getDeclName(); |
| 3594 | Importer.FromDiag(D->getPropertyIvarDeclLoc(), |
| 3595 | diag::note_odr_objc_synthesize_ivar_here) |
| 3596 | << D->getPropertyIvarDecl()->getDeclName(); |
| 3597 | return 0; |
| 3598 | } |
| 3599 | |
| 3600 | // Merge the existing implementation with the new implementation. |
| 3601 | Importer.Imported(D, ToImpl); |
| 3602 | } |
| 3603 | |
| 3604 | return ToImpl; |
| 3605 | } |
| 3606 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3607 | Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
| 3608 | // For template arguments, we adopt the translation unit as our declaration |
| 3609 | // context. This context will be fixed when the actual template declaration |
| 3610 | // is created. |
| 3611 | |
| 3612 | // FIXME: Import default argument. |
| 3613 | return TemplateTypeParmDecl::Create(Importer.getToContext(), |
| 3614 | Importer.getToContext().getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 3615 | Importer.Import(D->getLocStart()), |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3616 | Importer.Import(D->getLocation()), |
| 3617 | D->getDepth(), |
| 3618 | D->getIndex(), |
| 3619 | Importer.Import(D->getIdentifier()), |
| 3620 | D->wasDeclaredWithTypename(), |
| 3621 | D->isParameterPack()); |
| 3622 | } |
| 3623 | |
| 3624 | Decl * |
| 3625 | ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
| 3626 | // Import the name of this declaration. |
| 3627 | DeclarationName Name = Importer.Import(D->getDeclName()); |
| 3628 | if (D->getDeclName() && !Name) |
| 3629 | return 0; |
| 3630 | |
| 3631 | // Import the location of this declaration. |
| 3632 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 3633 | |
| 3634 | // Import the type of this declaration. |
| 3635 | QualType T = Importer.Import(D->getType()); |
| 3636 | if (T.isNull()) |
| 3637 | return 0; |
| 3638 | |
| 3639 | // Import type-source information. |
| 3640 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 3641 | if (D->getTypeSourceInfo() && !TInfo) |
| 3642 | return 0; |
| 3643 | |
| 3644 | // FIXME: Import default argument. |
| 3645 | |
| 3646 | return NonTypeTemplateParmDecl::Create(Importer.getToContext(), |
| 3647 | Importer.getToContext().getTranslationUnitDecl(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3648 | Importer.Import(D->getInnerLocStart()), |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3649 | Loc, D->getDepth(), D->getPosition(), |
| 3650 | Name.getAsIdentifierInfo(), |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 3651 | T, D->isParameterPack(), TInfo); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3652 | } |
| 3653 | |
| 3654 | Decl * |
| 3655 | ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
| 3656 | // Import the name of this declaration. |
| 3657 | DeclarationName Name = Importer.Import(D->getDeclName()); |
| 3658 | if (D->getDeclName() && !Name) |
| 3659 | return 0; |
| 3660 | |
| 3661 | // Import the location of this declaration. |
| 3662 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 3663 | |
| 3664 | // Import template parameters. |
| 3665 | TemplateParameterList *TemplateParams |
| 3666 | = ImportTemplateParameterList(D->getTemplateParameters()); |
| 3667 | if (!TemplateParams) |
| 3668 | return 0; |
| 3669 | |
| 3670 | // FIXME: Import default argument. |
| 3671 | |
| 3672 | return TemplateTemplateParmDecl::Create(Importer.getToContext(), |
| 3673 | Importer.getToContext().getTranslationUnitDecl(), |
| 3674 | Loc, D->getDepth(), D->getPosition(), |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 3675 | D->isParameterPack(), |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3676 | Name.getAsIdentifierInfo(), |
| 3677 | TemplateParams); |
| 3678 | } |
| 3679 | |
| 3680 | Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 3681 | // If this record has a definition in the translation unit we're coming from, |
| 3682 | // but this particular declaration is not that definition, import the |
| 3683 | // definition and map to that. |
| 3684 | CXXRecordDecl *Definition |
| 3685 | = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition()); |
| 3686 | if (Definition && Definition != D->getTemplatedDecl()) { |
| 3687 | Decl *ImportedDef |
| 3688 | = Importer.Import(Definition->getDescribedClassTemplate()); |
| 3689 | if (!ImportedDef) |
| 3690 | return 0; |
| 3691 | |
| 3692 | return Importer.Imported(D, ImportedDef); |
| 3693 | } |
| 3694 | |
| 3695 | // Import the major distinguishing characteristics of this class template. |
| 3696 | DeclContext *DC, *LexicalDC; |
| 3697 | DeclarationName Name; |
| 3698 | SourceLocation Loc; |
| 3699 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
| 3700 | return 0; |
| 3701 | |
| 3702 | // We may already have a template of the same name; try to find and match it. |
| 3703 | if (!DC->isFunctionOrMethod()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3704 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3705 | llvm::SmallVector<NamedDecl *, 2> FoundDecls; |
| 3706 | DC->localUncachedLookup(Name, FoundDecls); |
| 3707 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 3708 | if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3709 | continue; |
| 3710 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3711 | Decl *Found = FoundDecls[I]; |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3712 | if (ClassTemplateDecl *FoundTemplate |
| 3713 | = dyn_cast<ClassTemplateDecl>(Found)) { |
| 3714 | if (IsStructuralMatch(D, FoundTemplate)) { |
| 3715 | // The class templates structurally match; call it the same template. |
| 3716 | // FIXME: We may be filling in a forward declaration here. Handle |
| 3717 | // this case! |
| 3718 | Importer.Imported(D->getTemplatedDecl(), |
| 3719 | FoundTemplate->getTemplatedDecl()); |
| 3720 | return Importer.Imported(D, FoundTemplate); |
| 3721 | } |
| 3722 | } |
| 3723 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3724 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3725 | } |
| 3726 | |
| 3727 | if (!ConflictingDecls.empty()) { |
| 3728 | Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary, |
| 3729 | ConflictingDecls.data(), |
| 3730 | ConflictingDecls.size()); |
| 3731 | } |
| 3732 | |
| 3733 | if (!Name) |
| 3734 | return 0; |
| 3735 | } |
| 3736 | |
| 3737 | CXXRecordDecl *DTemplated = D->getTemplatedDecl(); |
| 3738 | |
| 3739 | // Create the declaration that is being templated. |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3740 | SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart()); |
| 3741 | SourceLocation IdLoc = Importer.Import(DTemplated->getLocation()); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3742 | CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(), |
| 3743 | DTemplated->getTagKind(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3744 | DC, StartLoc, IdLoc, |
| 3745 | Name.getAsIdentifierInfo()); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3746 | D2Templated->setAccess(DTemplated->getAccess()); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3747 | D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc())); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3748 | D2Templated->setLexicalDeclContext(LexicalDC); |
| 3749 | |
| 3750 | // Create the class template declaration itself. |
| 3751 | TemplateParameterList *TemplateParams |
| 3752 | = ImportTemplateParameterList(D->getTemplateParameters()); |
| 3753 | if (!TemplateParams) |
| 3754 | return 0; |
| 3755 | |
| 3756 | ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC, |
| 3757 | Loc, Name, TemplateParams, |
| 3758 | D2Templated, |
| 3759 | /*PrevDecl=*/0); |
| 3760 | D2Templated->setDescribedClassTemplate(D2); |
| 3761 | |
| 3762 | D2->setAccess(D->getAccess()); |
| 3763 | D2->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3764 | LexicalDC->addDeclInternal(D2); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3765 | |
| 3766 | // Note the relationship between the class templates. |
| 3767 | Importer.Imported(D, D2); |
| 3768 | Importer.Imported(DTemplated, D2Templated); |
| 3769 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3770 | if (DTemplated->isCompleteDefinition() && |
| 3771 | !D2Templated->isCompleteDefinition()) { |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3772 | // FIXME: Import definition! |
| 3773 | } |
| 3774 | |
| 3775 | return D2; |
| 3776 | } |
| 3777 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 3778 | Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl( |
| 3779 | ClassTemplateSpecializationDecl *D) { |
| 3780 | // If this record has a definition in the translation unit we're coming from, |
| 3781 | // but this particular declaration is not that definition, import the |
| 3782 | // definition and map to that. |
| 3783 | TagDecl *Definition = D->getDefinition(); |
| 3784 | if (Definition && Definition != D) { |
| 3785 | Decl *ImportedDef = Importer.Import(Definition); |
| 3786 | if (!ImportedDef) |
| 3787 | return 0; |
| 3788 | |
| 3789 | return Importer.Imported(D, ImportedDef); |
| 3790 | } |
| 3791 | |
| 3792 | ClassTemplateDecl *ClassTemplate |
| 3793 | = cast_or_null<ClassTemplateDecl>(Importer.Import( |
| 3794 | D->getSpecializedTemplate())); |
| 3795 | if (!ClassTemplate) |
| 3796 | return 0; |
| 3797 | |
| 3798 | // Import the context of this declaration. |
| 3799 | DeclContext *DC = ClassTemplate->getDeclContext(); |
| 3800 | if (!DC) |
| 3801 | return 0; |
| 3802 | |
| 3803 | DeclContext *LexicalDC = DC; |
| 3804 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 3805 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 3806 | if (!LexicalDC) |
| 3807 | return 0; |
| 3808 | } |
| 3809 | |
| 3810 | // Import the location of this declaration. |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3811 | SourceLocation StartLoc = Importer.Import(D->getLocStart()); |
| 3812 | SourceLocation IdLoc = Importer.Import(D->getLocation()); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 3813 | |
| 3814 | // Import template arguments. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3815 | SmallVector<TemplateArgument, 2> TemplateArgs; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 3816 | if (ImportTemplateArguments(D->getTemplateArgs().data(), |
| 3817 | D->getTemplateArgs().size(), |
| 3818 | TemplateArgs)) |
| 3819 | return 0; |
| 3820 | |
| 3821 | // Try to find an existing specialization with these template arguments. |
| 3822 | void *InsertPos = 0; |
| 3823 | ClassTemplateSpecializationDecl *D2 |
| 3824 | = ClassTemplate->findSpecialization(TemplateArgs.data(), |
| 3825 | TemplateArgs.size(), InsertPos); |
| 3826 | if (D2) { |
| 3827 | // We already have a class template specialization with these template |
| 3828 | // arguments. |
| 3829 | |
| 3830 | // FIXME: Check for specialization vs. instantiation errors. |
| 3831 | |
| 3832 | if (RecordDecl *FoundDef = D2->getDefinition()) { |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3833 | if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) { |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 3834 | // The record types structurally match, or the "from" translation |
| 3835 | // unit only had a forward declaration anyway; call it the same |
| 3836 | // function. |
| 3837 | return Importer.Imported(D, FoundDef); |
| 3838 | } |
| 3839 | } |
| 3840 | } else { |
| 3841 | // Create a new specialization. |
| 3842 | D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(), |
| 3843 | D->getTagKind(), DC, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3844 | StartLoc, IdLoc, |
| 3845 | ClassTemplate, |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 3846 | TemplateArgs.data(), |
| 3847 | TemplateArgs.size(), |
| 3848 | /*PrevDecl=*/0); |
| 3849 | D2->setSpecializationKind(D->getSpecializationKind()); |
| 3850 | |
| 3851 | // Add this specialization to the class template. |
| 3852 | ClassTemplate->AddSpecialization(D2, InsertPos); |
| 3853 | |
| 3854 | // Import the qualifier, if any. |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3855 | D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 3856 | |
| 3857 | // Add the specialization to this context. |
| 3858 | D2->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3859 | LexicalDC->addDeclInternal(D2); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 3860 | } |
| 3861 | Importer.Imported(D, D2); |
| 3862 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3863 | if (D->isCompleteDefinition() && ImportDefinition(D, D2)) |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 3864 | return 0; |
| 3865 | |
| 3866 | return D2; |
| 3867 | } |
| 3868 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 3869 | //---------------------------------------------------------------------------- |
| 3870 | // Import Statements |
| 3871 | //---------------------------------------------------------------------------- |
| 3872 | |
| 3873 | Stmt *ASTNodeImporter::VisitStmt(Stmt *S) { |
| 3874 | Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node) |
| 3875 | << S->getStmtClassName(); |
| 3876 | return 0; |
| 3877 | } |
| 3878 | |
| 3879 | //---------------------------------------------------------------------------- |
| 3880 | // Import Expressions |
| 3881 | //---------------------------------------------------------------------------- |
| 3882 | Expr *ASTNodeImporter::VisitExpr(Expr *E) { |
| 3883 | Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node) |
| 3884 | << E->getStmtClassName(); |
| 3885 | return 0; |
| 3886 | } |
| 3887 | |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 3888 | Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) { |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 3889 | ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl())); |
| 3890 | if (!ToD) |
| 3891 | return 0; |
Chandler Carruth | 8d26bb0 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 3892 | |
| 3893 | NamedDecl *FoundD = 0; |
| 3894 | if (E->getDecl() != E->getFoundDecl()) { |
| 3895 | FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl())); |
| 3896 | if (!FoundD) |
| 3897 | return 0; |
| 3898 | } |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 3899 | |
| 3900 | QualType T = Importer.Import(E->getType()); |
| 3901 | if (T.isNull()) |
| 3902 | return 0; |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3903 | |
| 3904 | DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(), |
| 3905 | Importer.Import(E->getQualifierLoc()), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3906 | Importer.Import(E->getTemplateKeywordLoc()), |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3907 | ToD, |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3908 | E->refersToEnclosingLocal(), |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3909 | Importer.Import(E->getLocation()), |
| 3910 | T, E->getValueKind(), |
| 3911 | FoundD, |
| 3912 | /*FIXME:TemplateArgs=*/0); |
| 3913 | if (E->hadMultipleCandidates()) |
| 3914 | DRE->setHadMultipleCandidates(true); |
| 3915 | return DRE; |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 3916 | } |
| 3917 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 3918 | Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) { |
| 3919 | QualType T = Importer.Import(E->getType()); |
| 3920 | if (T.isNull()) |
| 3921 | return 0; |
| 3922 | |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 3923 | return IntegerLiteral::Create(Importer.getToContext(), |
| 3924 | E->getValue(), T, |
| 3925 | Importer.Import(E->getLocation())); |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 3926 | } |
| 3927 | |
Douglas Gregor | 623421d | 2010-02-18 02:21:22 +0000 | [diff] [blame] | 3928 | Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) { |
| 3929 | QualType T = Importer.Import(E->getType()); |
| 3930 | if (T.isNull()) |
| 3931 | return 0; |
| 3932 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 3933 | return new (Importer.getToContext()) CharacterLiteral(E->getValue(), |
| 3934 | E->getKind(), T, |
Douglas Gregor | 623421d | 2010-02-18 02:21:22 +0000 | [diff] [blame] | 3935 | Importer.Import(E->getLocation())); |
| 3936 | } |
| 3937 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 3938 | Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) { |
| 3939 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 3940 | if (!SubExpr) |
| 3941 | return 0; |
| 3942 | |
| 3943 | return new (Importer.getToContext()) |
| 3944 | ParenExpr(Importer.Import(E->getLParen()), |
| 3945 | Importer.Import(E->getRParen()), |
| 3946 | SubExpr); |
| 3947 | } |
| 3948 | |
| 3949 | Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) { |
| 3950 | QualType T = Importer.Import(E->getType()); |
| 3951 | if (T.isNull()) |
| 3952 | return 0; |
| 3953 | |
| 3954 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 3955 | if (!SubExpr) |
| 3956 | return 0; |
| 3957 | |
| 3958 | return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3959 | T, E->getValueKind(), |
| 3960 | E->getObjectKind(), |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 3961 | Importer.Import(E->getOperatorLoc())); |
| 3962 | } |
| 3963 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3964 | Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr( |
| 3965 | UnaryExprOrTypeTraitExpr *E) { |
Douglas Gregor | d8552cd | 2010-02-19 01:24:23 +0000 | [diff] [blame] | 3966 | QualType ResultType = Importer.Import(E->getType()); |
| 3967 | |
| 3968 | if (E->isArgumentType()) { |
| 3969 | TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo()); |
| 3970 | if (!TInfo) |
| 3971 | return 0; |
| 3972 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3973 | return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(), |
| 3974 | TInfo, ResultType, |
Douglas Gregor | d8552cd | 2010-02-19 01:24:23 +0000 | [diff] [blame] | 3975 | Importer.Import(E->getOperatorLoc()), |
| 3976 | Importer.Import(E->getRParenLoc())); |
| 3977 | } |
| 3978 | |
| 3979 | Expr *SubExpr = Importer.Import(E->getArgumentExpr()); |
| 3980 | if (!SubExpr) |
| 3981 | return 0; |
| 3982 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3983 | return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(), |
| 3984 | SubExpr, ResultType, |
Douglas Gregor | d8552cd | 2010-02-19 01:24:23 +0000 | [diff] [blame] | 3985 | Importer.Import(E->getOperatorLoc()), |
| 3986 | Importer.Import(E->getRParenLoc())); |
| 3987 | } |
| 3988 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 3989 | Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) { |
| 3990 | QualType T = Importer.Import(E->getType()); |
| 3991 | if (T.isNull()) |
| 3992 | return 0; |
| 3993 | |
| 3994 | Expr *LHS = Importer.Import(E->getLHS()); |
| 3995 | if (!LHS) |
| 3996 | return 0; |
| 3997 | |
| 3998 | Expr *RHS = Importer.Import(E->getRHS()); |
| 3999 | if (!RHS) |
| 4000 | return 0; |
| 4001 | |
| 4002 | return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4003 | T, E->getValueKind(), |
| 4004 | E->getObjectKind(), |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 4005 | Importer.Import(E->getOperatorLoc())); |
| 4006 | } |
| 4007 | |
| 4008 | Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) { |
| 4009 | QualType T = Importer.Import(E->getType()); |
| 4010 | if (T.isNull()) |
| 4011 | return 0; |
| 4012 | |
| 4013 | QualType CompLHSType = Importer.Import(E->getComputationLHSType()); |
| 4014 | if (CompLHSType.isNull()) |
| 4015 | return 0; |
| 4016 | |
| 4017 | QualType CompResultType = Importer.Import(E->getComputationResultType()); |
| 4018 | if (CompResultType.isNull()) |
| 4019 | return 0; |
| 4020 | |
| 4021 | Expr *LHS = Importer.Import(E->getLHS()); |
| 4022 | if (!LHS) |
| 4023 | return 0; |
| 4024 | |
| 4025 | Expr *RHS = Importer.Import(E->getRHS()); |
| 4026 | if (!RHS) |
| 4027 | return 0; |
| 4028 | |
| 4029 | return new (Importer.getToContext()) |
| 4030 | CompoundAssignOperator(LHS, RHS, E->getOpcode(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4031 | T, E->getValueKind(), |
| 4032 | E->getObjectKind(), |
| 4033 | CompLHSType, CompResultType, |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 4034 | Importer.Import(E->getOperatorLoc())); |
| 4035 | } |
| 4036 | |
Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 4037 | static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) { |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4038 | if (E->path_empty()) return false; |
| 4039 | |
| 4040 | // TODO: import cast paths |
| 4041 | return true; |
| 4042 | } |
| 4043 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 4044 | Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 4045 | QualType T = Importer.Import(E->getType()); |
| 4046 | if (T.isNull()) |
| 4047 | return 0; |
| 4048 | |
| 4049 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 4050 | if (!SubExpr) |
| 4051 | return 0; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4052 | |
| 4053 | CXXCastPath BasePath; |
| 4054 | if (ImportCastPath(E, BasePath)) |
| 4055 | return 0; |
| 4056 | |
| 4057 | return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4058 | SubExpr, &BasePath, E->getValueKind()); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 4059 | } |
| 4060 | |
Douglas Gregor | 5481d32 | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 4061 | Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) { |
| 4062 | QualType T = Importer.Import(E->getType()); |
| 4063 | if (T.isNull()) |
| 4064 | return 0; |
| 4065 | |
| 4066 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 4067 | if (!SubExpr) |
| 4068 | return 0; |
| 4069 | |
| 4070 | TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten()); |
| 4071 | if (!TInfo && E->getTypeInfoAsWritten()) |
| 4072 | return 0; |
| 4073 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4074 | CXXCastPath BasePath; |
| 4075 | if (ImportCastPath(E, BasePath)) |
| 4076 | return 0; |
| 4077 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4078 | return CStyleCastExpr::Create(Importer.getToContext(), T, |
| 4079 | E->getValueKind(), E->getCastKind(), |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4080 | SubExpr, &BasePath, TInfo, |
| 4081 | Importer.Import(E->getLParenLoc()), |
| 4082 | Importer.Import(E->getRParenLoc())); |
Douglas Gregor | 5481d32 | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 4083 | } |
| 4084 | |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 4085 | ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager, |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 4086 | ASTContext &FromContext, FileManager &FromFileManager, |
| 4087 | bool MinimalImport) |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 4088 | : ToContext(ToContext), FromContext(FromContext), |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 4089 | ToFileManager(ToFileManager), FromFileManager(FromFileManager), |
| 4090 | Minimal(MinimalImport) |
| 4091 | { |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4092 | ImportedDecls[FromContext.getTranslationUnitDecl()] |
| 4093 | = ToContext.getTranslationUnitDecl(); |
| 4094 | } |
| 4095 | |
| 4096 | ASTImporter::~ASTImporter() { } |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 4097 | |
| 4098 | QualType ASTImporter::Import(QualType FromT) { |
| 4099 | if (FromT.isNull()) |
| 4100 | return QualType(); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4101 | |
| 4102 | const Type *fromTy = FromT.getTypePtr(); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 4103 | |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 4104 | // Check whether we've already imported this type. |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4105 | llvm::DenseMap<const Type *, const Type *>::iterator Pos |
| 4106 | = ImportedTypes.find(fromTy); |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 4107 | if (Pos != ImportedTypes.end()) |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4108 | return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 4109 | |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 4110 | // Import the type |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 4111 | ASTNodeImporter Importer(*this); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4112 | QualType ToT = Importer.Visit(fromTy); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 4113 | if (ToT.isNull()) |
| 4114 | return ToT; |
| 4115 | |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 4116 | // Record the imported type. |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4117 | ImportedTypes[fromTy] = ToT.getTypePtr(); |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 4118 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4119 | return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 4120 | } |
| 4121 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4122 | TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) { |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 4123 | if (!FromTSI) |
| 4124 | return FromTSI; |
| 4125 | |
| 4126 | // FIXME: For now we just create a "trivial" type source info based |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 4127 | // on the type and a single location. Implement a real version of this. |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 4128 | QualType T = Import(FromTSI->getType()); |
| 4129 | if (T.isNull()) |
| 4130 | return 0; |
| 4131 | |
| 4132 | return ToContext.getTrivialTypeSourceInfo(T, |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4133 | FromTSI->getTypeLoc().getLocStart()); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4134 | } |
| 4135 | |
| 4136 | Decl *ASTImporter::Import(Decl *FromD) { |
| 4137 | if (!FromD) |
| 4138 | return 0; |
| 4139 | |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 4140 | ASTNodeImporter Importer(*this); |
| 4141 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4142 | // Check whether we've already imported this declaration. |
| 4143 | llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 4144 | if (Pos != ImportedDecls.end()) { |
| 4145 | Decl *ToD = Pos->second; |
| 4146 | Importer.ImportDefinitionIfNeeded(FromD, ToD); |
| 4147 | return ToD; |
| 4148 | } |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4149 | |
| 4150 | // Import the type |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4151 | Decl *ToD = Importer.Visit(FromD); |
| 4152 | if (!ToD) |
| 4153 | return 0; |
| 4154 | |
| 4155 | // Record the imported declaration. |
| 4156 | ImportedDecls[FromD] = ToD; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 4157 | |
| 4158 | if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) { |
| 4159 | // Keep track of anonymous tags that have an associated typedef. |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4160 | if (FromTag->getTypedefNameForAnonDecl()) |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 4161 | AnonTagsWithPendingTypedefs.push_back(FromTag); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4162 | } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 4163 | // When we've finished transforming a typedef, see whether it was the |
| 4164 | // typedef for an anonymous tag. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4165 | for (SmallVector<TagDecl *, 4>::iterator |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 4166 | FromTag = AnonTagsWithPendingTypedefs.begin(), |
| 4167 | FromTagEnd = AnonTagsWithPendingTypedefs.end(); |
| 4168 | FromTag != FromTagEnd; ++FromTag) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4169 | if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 4170 | if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) { |
| 4171 | // We found the typedef for an anonymous tag; link them. |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4172 | ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD)); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 4173 | AnonTagsWithPendingTypedefs.erase(FromTag); |
| 4174 | break; |
| 4175 | } |
| 4176 | } |
| 4177 | } |
| 4178 | } |
| 4179 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4180 | return ToD; |
| 4181 | } |
| 4182 | |
| 4183 | DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) { |
| 4184 | if (!FromDC) |
| 4185 | return FromDC; |
| 4186 | |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 4187 | DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC))); |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 4188 | if (!ToDC) |
| 4189 | return 0; |
| 4190 | |
| 4191 | // When we're using a record/enum/Objective-C class/protocol as a context, we |
| 4192 | // need it to have a definition. |
| 4193 | if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) { |
Douglas Gregor | 63db971 | 2012-01-25 01:13:20 +0000 | [diff] [blame] | 4194 | RecordDecl *FromRecord = cast<RecordDecl>(FromDC); |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 4195 | if (ToRecord->isCompleteDefinition()) { |
| 4196 | // Do nothing. |
| 4197 | } else if (FromRecord->isCompleteDefinition()) { |
| 4198 | ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord, |
| 4199 | ASTNodeImporter::IDK_Basic); |
| 4200 | } else { |
| 4201 | CompleteDecl(ToRecord); |
| 4202 | } |
| 4203 | } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) { |
| 4204 | EnumDecl *FromEnum = cast<EnumDecl>(FromDC); |
| 4205 | if (ToEnum->isCompleteDefinition()) { |
| 4206 | // Do nothing. |
| 4207 | } else if (FromEnum->isCompleteDefinition()) { |
| 4208 | ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum, |
| 4209 | ASTNodeImporter::IDK_Basic); |
| 4210 | } else { |
| 4211 | CompleteDecl(ToEnum); |
| 4212 | } |
| 4213 | } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) { |
| 4214 | ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC); |
| 4215 | if (ToClass->getDefinition()) { |
| 4216 | // Do nothing. |
| 4217 | } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) { |
| 4218 | ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass, |
| 4219 | ASTNodeImporter::IDK_Basic); |
| 4220 | } else { |
| 4221 | CompleteDecl(ToClass); |
| 4222 | } |
| 4223 | } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) { |
| 4224 | ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC); |
| 4225 | if (ToProto->getDefinition()) { |
| 4226 | // Do nothing. |
| 4227 | } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) { |
| 4228 | ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto, |
| 4229 | ASTNodeImporter::IDK_Basic); |
| 4230 | } else { |
| 4231 | CompleteDecl(ToProto); |
| 4232 | } |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 4233 | } |
| 4234 | |
| 4235 | return ToDC; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4236 | } |
| 4237 | |
| 4238 | Expr *ASTImporter::Import(Expr *FromE) { |
| 4239 | if (!FromE) |
| 4240 | return 0; |
| 4241 | |
| 4242 | return cast_or_null<Expr>(Import(cast<Stmt>(FromE))); |
| 4243 | } |
| 4244 | |
| 4245 | Stmt *ASTImporter::Import(Stmt *FromS) { |
| 4246 | if (!FromS) |
| 4247 | return 0; |
| 4248 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 4249 | // Check whether we've already imported this declaration. |
| 4250 | llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS); |
| 4251 | if (Pos != ImportedStmts.end()) |
| 4252 | return Pos->second; |
| 4253 | |
| 4254 | // Import the type |
| 4255 | ASTNodeImporter Importer(*this); |
| 4256 | Stmt *ToS = Importer.Visit(FromS); |
| 4257 | if (!ToS) |
| 4258 | return 0; |
| 4259 | |
| 4260 | // Record the imported declaration. |
| 4261 | ImportedStmts[FromS] = ToS; |
| 4262 | return ToS; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4263 | } |
| 4264 | |
| 4265 | NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) { |
| 4266 | if (!FromNNS) |
| 4267 | return 0; |
| 4268 | |
Douglas Gregor | 90ebf25 | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 4269 | NestedNameSpecifier *prefix = Import(FromNNS->getPrefix()); |
| 4270 | |
| 4271 | switch (FromNNS->getKind()) { |
| 4272 | case NestedNameSpecifier::Identifier: |
| 4273 | if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) { |
| 4274 | return NestedNameSpecifier::Create(ToContext, prefix, II); |
| 4275 | } |
| 4276 | return 0; |
| 4277 | |
| 4278 | case NestedNameSpecifier::Namespace: |
| 4279 | if (NamespaceDecl *NS = |
| 4280 | cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) { |
| 4281 | return NestedNameSpecifier::Create(ToContext, prefix, NS); |
| 4282 | } |
| 4283 | return 0; |
| 4284 | |
| 4285 | case NestedNameSpecifier::NamespaceAlias: |
| 4286 | if (NamespaceAliasDecl *NSAD = |
| 4287 | cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) { |
| 4288 | return NestedNameSpecifier::Create(ToContext, prefix, NSAD); |
| 4289 | } |
| 4290 | return 0; |
| 4291 | |
| 4292 | case NestedNameSpecifier::Global: |
| 4293 | return NestedNameSpecifier::GlobalSpecifier(ToContext); |
| 4294 | |
| 4295 | case NestedNameSpecifier::TypeSpec: |
| 4296 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 4297 | QualType T = Import(QualType(FromNNS->getAsType(), 0u)); |
| 4298 | if (!T.isNull()) { |
| 4299 | bool bTemplate = FromNNS->getKind() == |
| 4300 | NestedNameSpecifier::TypeSpecWithTemplate; |
| 4301 | return NestedNameSpecifier::Create(ToContext, prefix, |
| 4302 | bTemplate, T.getTypePtr()); |
| 4303 | } |
| 4304 | } |
| 4305 | return 0; |
| 4306 | } |
| 4307 | |
| 4308 | llvm_unreachable("Invalid nested name specifier kind"); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4309 | } |
| 4310 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4311 | NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) { |
| 4312 | // FIXME: Implement! |
| 4313 | return NestedNameSpecifierLoc(); |
| 4314 | } |
| 4315 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4316 | TemplateName ASTImporter::Import(TemplateName From) { |
| 4317 | switch (From.getKind()) { |
| 4318 | case TemplateName::Template: |
| 4319 | if (TemplateDecl *ToTemplate |
| 4320 | = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl()))) |
| 4321 | return TemplateName(ToTemplate); |
| 4322 | |
| 4323 | return TemplateName(); |
| 4324 | |
| 4325 | case TemplateName::OverloadedTemplate: { |
| 4326 | OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate(); |
| 4327 | UnresolvedSet<2> ToTemplates; |
| 4328 | for (OverloadedTemplateStorage::iterator I = FromStorage->begin(), |
| 4329 | E = FromStorage->end(); |
| 4330 | I != E; ++I) { |
| 4331 | if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I))) |
| 4332 | ToTemplates.addDecl(To); |
| 4333 | else |
| 4334 | return TemplateName(); |
| 4335 | } |
| 4336 | return ToContext.getOverloadedTemplateName(ToTemplates.begin(), |
| 4337 | ToTemplates.end()); |
| 4338 | } |
| 4339 | |
| 4340 | case TemplateName::QualifiedTemplate: { |
| 4341 | QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName(); |
| 4342 | NestedNameSpecifier *Qualifier = Import(QTN->getQualifier()); |
| 4343 | if (!Qualifier) |
| 4344 | return TemplateName(); |
| 4345 | |
| 4346 | if (TemplateDecl *ToTemplate |
| 4347 | = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl()))) |
| 4348 | return ToContext.getQualifiedTemplateName(Qualifier, |
| 4349 | QTN->hasTemplateKeyword(), |
| 4350 | ToTemplate); |
| 4351 | |
| 4352 | return TemplateName(); |
| 4353 | } |
| 4354 | |
| 4355 | case TemplateName::DependentTemplate: { |
| 4356 | DependentTemplateName *DTN = From.getAsDependentTemplateName(); |
| 4357 | NestedNameSpecifier *Qualifier = Import(DTN->getQualifier()); |
| 4358 | if (!Qualifier) |
| 4359 | return TemplateName(); |
| 4360 | |
| 4361 | if (DTN->isIdentifier()) { |
| 4362 | return ToContext.getDependentTemplateName(Qualifier, |
| 4363 | Import(DTN->getIdentifier())); |
| 4364 | } |
| 4365 | |
| 4366 | return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator()); |
| 4367 | } |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4368 | |
| 4369 | case TemplateName::SubstTemplateTemplateParm: { |
| 4370 | SubstTemplateTemplateParmStorage *subst |
| 4371 | = From.getAsSubstTemplateTemplateParm(); |
| 4372 | TemplateTemplateParmDecl *param |
| 4373 | = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter())); |
| 4374 | if (!param) |
| 4375 | return TemplateName(); |
| 4376 | |
| 4377 | TemplateName replacement = Import(subst->getReplacement()); |
| 4378 | if (replacement.isNull()) return TemplateName(); |
| 4379 | |
| 4380 | return ToContext.getSubstTemplateTemplateParm(param, replacement); |
| 4381 | } |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 4382 | |
| 4383 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 4384 | SubstTemplateTemplateParmPackStorage *SubstPack |
| 4385 | = From.getAsSubstTemplateTemplateParmPack(); |
| 4386 | TemplateTemplateParmDecl *Param |
| 4387 | = cast_or_null<TemplateTemplateParmDecl>( |
| 4388 | Import(SubstPack->getParameterPack())); |
| 4389 | if (!Param) |
| 4390 | return TemplateName(); |
| 4391 | |
| 4392 | ASTNodeImporter Importer(*this); |
| 4393 | TemplateArgument ArgPack |
| 4394 | = Importer.ImportTemplateArgument(SubstPack->getArgumentPack()); |
| 4395 | if (ArgPack.isNull()) |
| 4396 | return TemplateName(); |
| 4397 | |
| 4398 | return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack); |
| 4399 | } |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4400 | } |
| 4401 | |
| 4402 | llvm_unreachable("Invalid template name kind"); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4403 | } |
| 4404 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4405 | SourceLocation ASTImporter::Import(SourceLocation FromLoc) { |
| 4406 | if (FromLoc.isInvalid()) |
| 4407 | return SourceLocation(); |
| 4408 | |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4409 | SourceManager &FromSM = FromContext.getSourceManager(); |
| 4410 | |
| 4411 | // For now, map everything down to its spelling location, so that we |
Chandler Carruth | 2536641 | 2011-07-15 00:04:35 +0000 | [diff] [blame] | 4412 | // don't have to import macro expansions. |
| 4413 | // FIXME: Import macro expansions! |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4414 | FromLoc = FromSM.getSpellingLoc(FromLoc); |
| 4415 | std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc); |
| 4416 | SourceManager &ToSM = ToContext.getSourceManager(); |
| 4417 | return ToSM.getLocForStartOfFile(Import(Decomposed.first)) |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4418 | .getLocWithOffset(Decomposed.second); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4419 | } |
| 4420 | |
| 4421 | SourceRange ASTImporter::Import(SourceRange FromRange) { |
| 4422 | return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd())); |
| 4423 | } |
| 4424 | |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4425 | FileID ASTImporter::Import(FileID FromID) { |
Sebastian Redl | 99219f1 | 2010-09-30 01:03:06 +0000 | [diff] [blame] | 4426 | llvm::DenseMap<FileID, FileID>::iterator Pos |
| 4427 | = ImportedFileIDs.find(FromID); |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4428 | if (Pos != ImportedFileIDs.end()) |
| 4429 | return Pos->second; |
| 4430 | |
| 4431 | SourceManager &FromSM = FromContext.getSourceManager(); |
| 4432 | SourceManager &ToSM = ToContext.getSourceManager(); |
| 4433 | const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID); |
Chandler Carruth | 2536641 | 2011-07-15 00:04:35 +0000 | [diff] [blame] | 4434 | assert(FromSLoc.isFile() && "Cannot handle macro expansions yet"); |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4435 | |
| 4436 | // Include location of this file. |
| 4437 | SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc()); |
| 4438 | |
| 4439 | // Map the FileID for to the "to" source manager. |
| 4440 | FileID ToID; |
| 4441 | const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache(); |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 4442 | if (Cache->OrigEntry) { |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4443 | // FIXME: We probably want to use getVirtualFile(), so we don't hit the |
| 4444 | // disk again |
| 4445 | // FIXME: We definitely want to re-use the existing MemoryBuffer, rather |
| 4446 | // than mmap the files several times. |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 4447 | const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName()); |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4448 | ToID = ToSM.createFileID(Entry, ToIncludeLoc, |
| 4449 | FromSLoc.getFile().getFileCharacteristic()); |
| 4450 | } else { |
| 4451 | // FIXME: We want to re-use the existing MemoryBuffer! |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 4452 | const llvm::MemoryBuffer * |
| 4453 | FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM); |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4454 | llvm::MemoryBuffer *ToBuf |
Chris Lattner | 58c7934 | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 4455 | = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(), |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4456 | FromBuf->getBufferIdentifier()); |
| 4457 | ToID = ToSM.createFileIDForMemBuffer(ToBuf); |
| 4458 | } |
| 4459 | |
| 4460 | |
Sebastian Redl | 99219f1 | 2010-09-30 01:03:06 +0000 | [diff] [blame] | 4461 | ImportedFileIDs[FromID] = ToID; |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4462 | return ToID; |
| 4463 | } |
| 4464 | |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 4465 | void ASTImporter::ImportDefinition(Decl *From) { |
| 4466 | Decl *To = Import(From); |
| 4467 | if (!To) |
| 4468 | return; |
| 4469 | |
| 4470 | if (DeclContext *FromDC = cast<DeclContext>(From)) { |
| 4471 | ASTNodeImporter Importer(*this); |
Sean Callanan | 53a6bff | 2011-07-19 22:38:25 +0000 | [diff] [blame] | 4472 | |
| 4473 | if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) { |
| 4474 | if (!ToRecord->getDefinition()) { |
| 4475 | Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord, |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 4476 | ASTNodeImporter::IDK_Everything); |
Sean Callanan | 53a6bff | 2011-07-19 22:38:25 +0000 | [diff] [blame] | 4477 | return; |
| 4478 | } |
| 4479 | } |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 4480 | |
| 4481 | if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) { |
| 4482 | if (!ToEnum->getDefinition()) { |
| 4483 | Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 4484 | ASTNodeImporter::IDK_Everything); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 4485 | return; |
| 4486 | } |
| 4487 | } |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4488 | |
| 4489 | if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) { |
| 4490 | if (!ToIFace->getDefinition()) { |
| 4491 | Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 4492 | ASTNodeImporter::IDK_Everything); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4493 | return; |
| 4494 | } |
| 4495 | } |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 4496 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4497 | if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) { |
| 4498 | if (!ToProto->getDefinition()) { |
| 4499 | Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 4500 | ASTNodeImporter::IDK_Everything); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4501 | return; |
| 4502 | } |
| 4503 | } |
| 4504 | |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 4505 | Importer.ImportDeclContext(FromDC, true); |
| 4506 | } |
| 4507 | } |
| 4508 | |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 4509 | DeclarationName ASTImporter::Import(DeclarationName FromName) { |
| 4510 | if (!FromName) |
| 4511 | return DeclarationName(); |
| 4512 | |
| 4513 | switch (FromName.getNameKind()) { |
| 4514 | case DeclarationName::Identifier: |
| 4515 | return Import(FromName.getAsIdentifierInfo()); |
| 4516 | |
| 4517 | case DeclarationName::ObjCZeroArgSelector: |
| 4518 | case DeclarationName::ObjCOneArgSelector: |
| 4519 | case DeclarationName::ObjCMultiArgSelector: |
| 4520 | return Import(FromName.getObjCSelector()); |
| 4521 | |
| 4522 | case DeclarationName::CXXConstructorName: { |
| 4523 | QualType T = Import(FromName.getCXXNameType()); |
| 4524 | if (T.isNull()) |
| 4525 | return DeclarationName(); |
| 4526 | |
| 4527 | return ToContext.DeclarationNames.getCXXConstructorName( |
| 4528 | ToContext.getCanonicalType(T)); |
| 4529 | } |
| 4530 | |
| 4531 | case DeclarationName::CXXDestructorName: { |
| 4532 | QualType T = Import(FromName.getCXXNameType()); |
| 4533 | if (T.isNull()) |
| 4534 | return DeclarationName(); |
| 4535 | |
| 4536 | return ToContext.DeclarationNames.getCXXDestructorName( |
| 4537 | ToContext.getCanonicalType(T)); |
| 4538 | } |
| 4539 | |
| 4540 | case DeclarationName::CXXConversionFunctionName: { |
| 4541 | QualType T = Import(FromName.getCXXNameType()); |
| 4542 | if (T.isNull()) |
| 4543 | return DeclarationName(); |
| 4544 | |
| 4545 | return ToContext.DeclarationNames.getCXXConversionFunctionName( |
| 4546 | ToContext.getCanonicalType(T)); |
| 4547 | } |
| 4548 | |
| 4549 | case DeclarationName::CXXOperatorName: |
| 4550 | return ToContext.DeclarationNames.getCXXOperatorName( |
| 4551 | FromName.getCXXOverloadedOperator()); |
| 4552 | |
| 4553 | case DeclarationName::CXXLiteralOperatorName: |
| 4554 | return ToContext.DeclarationNames.getCXXLiteralOperatorName( |
| 4555 | Import(FromName.getCXXLiteralIdentifier())); |
| 4556 | |
| 4557 | case DeclarationName::CXXUsingDirective: |
| 4558 | // FIXME: STATICS! |
| 4559 | return DeclarationName::getUsingDirectiveName(); |
| 4560 | } |
| 4561 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 4562 | llvm_unreachable("Invalid DeclarationName Kind!"); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 4563 | } |
| 4564 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4565 | IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 4566 | if (!FromId) |
| 4567 | return 0; |
| 4568 | |
| 4569 | return &ToContext.Idents.get(FromId->getName()); |
| 4570 | } |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 4571 | |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 4572 | Selector ASTImporter::Import(Selector FromSel) { |
| 4573 | if (FromSel.isNull()) |
| 4574 | return Selector(); |
| 4575 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4576 | SmallVector<IdentifierInfo *, 4> Idents; |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 4577 | Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0))); |
| 4578 | for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I) |
| 4579 | Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I))); |
| 4580 | return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data()); |
| 4581 | } |
| 4582 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 4583 | DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name, |
| 4584 | DeclContext *DC, |
| 4585 | unsigned IDNS, |
| 4586 | NamedDecl **Decls, |
| 4587 | unsigned NumDecls) { |
| 4588 | return Name; |
| 4589 | } |
| 4590 | |
| 4591 | DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 4592 | return ToContext.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 4593 | } |
| 4594 | |
| 4595 | DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 4596 | return FromContext.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 4597 | } |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 4598 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 4599 | void ASTImporter::CompleteDecl (Decl *D) { |
| 4600 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 4601 | if (!ID->getDefinition()) |
| 4602 | ID->startDefinition(); |
| 4603 | } |
| 4604 | else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
| 4605 | if (!PD->getDefinition()) |
| 4606 | PD->startDefinition(); |
| 4607 | } |
| 4608 | else if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 4609 | if (!TD->getDefinition() && !TD->isBeingDefined()) { |
| 4610 | TD->startDefinition(); |
| 4611 | TD->setCompleteDefinition(true); |
| 4612 | } |
| 4613 | } |
| 4614 | else { |
| 4615 | assert (0 && "CompleteDecl called on a Decl that can't be completed"); |
| 4616 | } |
| 4617 | } |
| 4618 | |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 4619 | Decl *ASTImporter::Imported(Decl *From, Decl *To) { |
| 4620 | ImportedDecls[From] = To; |
| 4621 | return To; |
Daniel Dunbar | 9ced542 | 2010-02-13 20:24:39 +0000 | [diff] [blame] | 4622 | } |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 4623 | |
| 4624 | bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4625 | llvm::DenseMap<const Type *, const Type *>::iterator Pos |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 4626 | = ImportedTypes.find(From.getTypePtr()); |
| 4627 | if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To)) |
| 4628 | return true; |
| 4629 | |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 4630 | StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls); |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 4631 | return Ctx.IsStructurallyEquivalent(From, To); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 4632 | } |