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