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