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" |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTDiagnostic.h" |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclVisitor.h" |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 20 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 21 | #include "clang/AST/TypeVisitor.h" |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 22 | #include "clang/Basic/FileManager.h" |
| 23 | #include "clang/Basic/SourceManager.h" |
| 24 | #include "llvm/Support/MemoryBuffer.h" |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 25 | #include <deque> |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 26 | |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 27 | namespace clang { |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 28 | class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>, |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 29 | public DeclVisitor<ASTNodeImporter, Decl *>, |
| 30 | public StmtVisitor<ASTNodeImporter, Stmt *> { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 31 | ASTImporter &Importer; |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 32 | |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 33 | public: |
| 34 | explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { } |
| 35 | |
| 36 | using TypeVisitor<ASTNodeImporter, QualType>::Visit; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 37 | using DeclVisitor<ASTNodeImporter, Decl *>::Visit; |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 38 | using StmtVisitor<ASTNodeImporter, Stmt *>::Visit; |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 39 | |
| 40 | // Importing types |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 41 | QualType VisitType(const Type *T); |
| 42 | QualType VisitBuiltinType(const BuiltinType *T); |
| 43 | QualType VisitComplexType(const ComplexType *T); |
| 44 | QualType VisitPointerType(const PointerType *T); |
| 45 | QualType VisitBlockPointerType(const BlockPointerType *T); |
| 46 | QualType VisitLValueReferenceType(const LValueReferenceType *T); |
| 47 | QualType VisitRValueReferenceType(const RValueReferenceType *T); |
| 48 | QualType VisitMemberPointerType(const MemberPointerType *T); |
| 49 | QualType VisitConstantArrayType(const ConstantArrayType *T); |
| 50 | QualType VisitIncompleteArrayType(const IncompleteArrayType *T); |
| 51 | QualType VisitVariableArrayType(const VariableArrayType *T); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 52 | // FIXME: DependentSizedArrayType |
| 53 | // FIXME: DependentSizedExtVectorType |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 54 | QualType VisitVectorType(const VectorType *T); |
| 55 | QualType VisitExtVectorType(const ExtVectorType *T); |
| 56 | QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T); |
| 57 | QualType VisitFunctionProtoType(const FunctionProtoType *T); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 58 | // FIXME: UnresolvedUsingType |
Sean Callanan | da6df8a | 2011-08-11 16:56:07 +0000 | [diff] [blame] | 59 | QualType VisitParenType(const ParenType *T); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 60 | QualType VisitTypedefType(const TypedefType *T); |
| 61 | QualType VisitTypeOfExprType(const TypeOfExprType *T); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 62 | // FIXME: DependentTypeOfExprType |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 63 | QualType VisitTypeOfType(const TypeOfType *T); |
| 64 | QualType VisitDecltypeType(const DecltypeType *T); |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 65 | QualType VisitUnaryTransformType(const UnaryTransformType *T); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 66 | QualType VisitAutoType(const AutoType *T); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 67 | QualType VisitInjectedClassNameType(const InjectedClassNameType *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); |
Sean Callanan | 72fe085 | 2015-04-02 23:50:08 +0000 | [diff] [blame] | 71 | QualType VisitAttributedType(const AttributedType *T); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 72 | QualType VisitTemplateTypeParmType(const TemplateTypeParmType *T); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 73 | // FIXME: SubstTemplateTypeParmType |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 74 | QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T); |
| 75 | QualType VisitElaboratedType(const ElaboratedType *T); |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 76 | // FIXME: DependentNameType |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 77 | // FIXME: DependentTemplateSpecializationType |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 78 | QualType VisitObjCInterfaceType(const ObjCInterfaceType *T); |
| 79 | QualType VisitObjCObjectType(const ObjCObjectType *T); |
| 80 | QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 81 | |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 82 | // Importing declarations |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 83 | bool ImportDeclParts(NamedDecl *D, DeclContext *&DC, |
| 84 | DeclContext *&LexicalDC, DeclarationName &Name, |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 85 | NamedDecl *&ToD, SourceLocation &Loc); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 86 | void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 87 | void ImportDeclarationNameLoc(const DeclarationNameInfo &From, |
| 88 | DeclarationNameInfo& To); |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 89 | void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 90 | |
| 91 | typedef DesignatedInitExpr::Designator Designator; |
| 92 | Designator ImportDesignator(const Designator &D); |
| 93 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 94 | |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 95 | /// \brief What we should import from the definition. |
| 96 | enum ImportDefinitionKind { |
| 97 | /// \brief Import the default subset of the definition, which might be |
| 98 | /// nothing (if minimal import is set) or might be everything (if minimal |
| 99 | /// import is not set). |
| 100 | IDK_Default, |
| 101 | /// \brief Import everything. |
| 102 | IDK_Everything, |
| 103 | /// \brief Import only the bare bones needed to establish a valid |
| 104 | /// DeclContext. |
| 105 | IDK_Basic |
| 106 | }; |
| 107 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 108 | bool shouldForceImportDeclContext(ImportDefinitionKind IDK) { |
| 109 | return IDK == IDK_Everything || |
| 110 | (IDK == IDK_Default && !Importer.isMinimalImport()); |
| 111 | } |
| 112 | |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 113 | bool ImportDefinition(RecordDecl *From, RecordDecl *To, |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 114 | ImportDefinitionKind Kind = IDK_Default); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 115 | bool ImportDefinition(VarDecl *From, VarDecl *To, |
| 116 | ImportDefinitionKind Kind = IDK_Default); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 117 | bool ImportDefinition(EnumDecl *From, EnumDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 118 | ImportDefinitionKind Kind = IDK_Default); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 119 | bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 120 | ImportDefinitionKind Kind = IDK_Default); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 121 | bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 122 | ImportDefinitionKind Kind = IDK_Default); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 123 | TemplateParameterList *ImportTemplateParameterList( |
| 124 | TemplateParameterList *Params); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 125 | TemplateArgument ImportTemplateArgument(const TemplateArgument &From); |
| 126 | bool ImportTemplateArguments(const TemplateArgument *FromArgs, |
| 127 | unsigned NumFromArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 128 | SmallVectorImpl<TemplateArgument> &ToArgs); |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 129 | bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord, |
| 130 | bool Complain = true); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 131 | bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar, |
| 132 | bool Complain = true); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 133 | bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord); |
Douglas Gregor | 9115508 | 2012-11-14 22:29:20 +0000 | [diff] [blame] | 134 | bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 135 | bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 136 | bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To); |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 137 | Decl *VisitDecl(Decl *D); |
Argyrios Kyrtzidis | 544ea71 | 2016-02-18 23:08:36 +0000 | [diff] [blame] | 138 | Decl *VisitAccessSpecDecl(AccessSpecDecl *D); |
Sean Callanan | 6519827 | 2011-11-17 23:20:56 +0000 | [diff] [blame] | 139 | Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D); |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 140 | Decl *VisitNamespaceDecl(NamespaceDecl *D); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 141 | Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias); |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 142 | Decl *VisitTypedefDecl(TypedefDecl *D); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 143 | Decl *VisitTypeAliasDecl(TypeAliasDecl *D); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 144 | Decl *VisitLabelDecl(LabelDecl *D); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 145 | Decl *VisitEnumDecl(EnumDecl *D); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 146 | Decl *VisitRecordDecl(RecordDecl *D); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 147 | Decl *VisitEnumConstantDecl(EnumConstantDecl *D); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 148 | Decl *VisitFunctionDecl(FunctionDecl *D); |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 149 | Decl *VisitCXXMethodDecl(CXXMethodDecl *D); |
| 150 | Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D); |
| 151 | Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D); |
| 152 | Decl *VisitCXXConversionDecl(CXXConversionDecl *D); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 153 | Decl *VisitFieldDecl(FieldDecl *D); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 154 | Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D); |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 155 | Decl *VisitObjCIvarDecl(ObjCIvarDecl *D); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 156 | Decl *VisitVarDecl(VarDecl *D); |
Douglas Gregor | 8b228d7 | 2010-02-17 21:22:52 +0000 | [diff] [blame] | 157 | Decl *VisitImplicitParamDecl(ImplicitParamDecl *D); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 158 | Decl *VisitParmVarDecl(ParmVarDecl *D); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 159 | Decl *VisitObjCMethodDecl(ObjCMethodDecl *D); |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 160 | Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 161 | Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D); |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 162 | Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D); |
Sean Callanan | 0aae041 | 2014-12-10 00:00:37 +0000 | [diff] [blame] | 163 | Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D); |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 164 | |
| 165 | ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list); |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 166 | Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 167 | Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 168 | Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 169 | Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 170 | Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 171 | Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); |
| 172 | Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); |
| 173 | Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); |
| 174 | Decl *VisitClassTemplateDecl(ClassTemplateDecl *D); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 175 | Decl *VisitClassTemplateSpecializationDecl( |
| 176 | ClassTemplateSpecializationDecl *D); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 177 | Decl *VisitVarTemplateDecl(VarTemplateDecl *D); |
| 178 | Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D); |
| 179 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 180 | // Importing statements |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 181 | DeclGroupRef ImportDeclGroup(DeclGroupRef DG); |
| 182 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 183 | Stmt *VisitStmt(Stmt *S); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 184 | Stmt *VisitGCCAsmStmt(GCCAsmStmt *S); |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 185 | Stmt *VisitDeclStmt(DeclStmt *S); |
| 186 | Stmt *VisitNullStmt(NullStmt *S); |
| 187 | Stmt *VisitCompoundStmt(CompoundStmt *S); |
| 188 | Stmt *VisitCaseStmt(CaseStmt *S); |
| 189 | Stmt *VisitDefaultStmt(DefaultStmt *S); |
| 190 | Stmt *VisitLabelStmt(LabelStmt *S); |
| 191 | Stmt *VisitAttributedStmt(AttributedStmt *S); |
| 192 | Stmt *VisitIfStmt(IfStmt *S); |
| 193 | Stmt *VisitSwitchStmt(SwitchStmt *S); |
| 194 | Stmt *VisitWhileStmt(WhileStmt *S); |
| 195 | Stmt *VisitDoStmt(DoStmt *S); |
| 196 | Stmt *VisitForStmt(ForStmt *S); |
| 197 | Stmt *VisitGotoStmt(GotoStmt *S); |
| 198 | Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S); |
| 199 | Stmt *VisitContinueStmt(ContinueStmt *S); |
| 200 | Stmt *VisitBreakStmt(BreakStmt *S); |
| 201 | Stmt *VisitReturnStmt(ReturnStmt *S); |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 202 | // FIXME: MSAsmStmt |
| 203 | // FIXME: SEHExceptStmt |
| 204 | // FIXME: SEHFinallyStmt |
| 205 | // FIXME: SEHTryStmt |
| 206 | // FIXME: SEHLeaveStmt |
| 207 | // FIXME: CapturedStmt |
| 208 | Stmt *VisitCXXCatchStmt(CXXCatchStmt *S); |
| 209 | Stmt *VisitCXXTryStmt(CXXTryStmt *S); |
| 210 | Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S); |
| 211 | // FIXME: MSDependentExistsStmt |
| 212 | Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S); |
| 213 | Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S); |
| 214 | Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S); |
| 215 | Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S); |
| 216 | Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
| 217 | Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S); |
| 218 | Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S); |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 219 | |
| 220 | // Importing expressions |
| 221 | Expr *VisitExpr(Expr *E); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 222 | Expr *VisitVAArgExpr(VAArgExpr *E); |
| 223 | Expr *VisitGNUNullExpr(GNUNullExpr *E); |
| 224 | Expr *VisitPredefinedExpr(PredefinedExpr *E); |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 225 | Expr *VisitDeclRefExpr(DeclRefExpr *E); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 226 | Expr *VisitImplicitValueInitExpr(ImplicitValueInitExpr *ILE); |
| 227 | Expr *VisitDesignatedInitExpr(DesignatedInitExpr *E); |
| 228 | Expr *VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E); |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 229 | Expr *VisitIntegerLiteral(IntegerLiteral *E); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 230 | Expr *VisitFloatingLiteral(FloatingLiteral *E); |
Douglas Gregor | 623421d | 2010-02-18 02:21:22 +0000 | [diff] [blame] | 231 | Expr *VisitCharacterLiteral(CharacterLiteral *E); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 232 | Expr *VisitStringLiteral(StringLiteral *E); |
| 233 | Expr *VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
| 234 | Expr *VisitAtomicExpr(AtomicExpr *E); |
| 235 | Expr *VisitAddrLabelExpr(AddrLabelExpr *E); |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 236 | Expr *VisitParenExpr(ParenExpr *E); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 237 | Expr *VisitParenListExpr(ParenListExpr *E); |
| 238 | Expr *VisitStmtExpr(StmtExpr *E); |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 239 | Expr *VisitUnaryOperator(UnaryOperator *E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 240 | Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 241 | Expr *VisitBinaryOperator(BinaryOperator *E); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 242 | Expr *VisitConditionalOperator(ConditionalOperator *E); |
| 243 | Expr *VisitBinaryConditionalOperator(BinaryConditionalOperator *E); |
| 244 | Expr *VisitOpaqueValueExpr(OpaqueValueExpr *E); |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 245 | Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 246 | Expr *VisitImplicitCastExpr(ImplicitCastExpr *E); |
Douglas Gregor | 5481d32 | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 247 | Expr *VisitCStyleCastExpr(CStyleCastExpr *E); |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 248 | Expr *VisitCXXConstructExpr(CXXConstructExpr *E); |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 249 | Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E); |
| 250 | Expr *VisitCXXThisExpr(CXXThisExpr *E); |
| 251 | Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E); |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 252 | Expr *VisitMemberExpr(MemberExpr *E); |
| 253 | Expr *VisitCallExpr(CallExpr *E); |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 254 | Expr *VisitInitListExpr(InitListExpr *E); |
Sean Callanan | dd2c174 | 2016-05-16 20:48:03 +0000 | [diff] [blame] | 255 | Expr *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E); |
| 256 | Expr *VisitCXXNamedCastExpr(CXXNamedCastExpr *E); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 257 | |
| 258 | template<typename IIter, typename OIter> |
| 259 | void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) { |
| 260 | typedef typename std::remove_reference<decltype(*Obegin)>::type ItemT; |
| 261 | ASTImporter &ImporterRef = Importer; |
| 262 | std::transform(Ibegin, Iend, Obegin, |
| 263 | [&ImporterRef](ItemT From) -> ItemT { |
| 264 | return ImporterRef.Import(From); |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 265 | }); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | template<typename IIter, typename OIter> |
| 269 | bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) { |
| 270 | typedef typename std::remove_reference<decltype(**Obegin)>::type ItemT; |
| 271 | ASTImporter &ImporterRef = Importer; |
| 272 | bool Failed = false; |
| 273 | std::transform(Ibegin, Iend, Obegin, |
| 274 | [&ImporterRef, &Failed](ItemT *From) -> ItemT * { |
| 275 | ItemT *To = ImporterRef.Import(From); |
| 276 | if (!To && From) |
| 277 | Failed = true; |
| 278 | return To; |
| 279 | }); |
| 280 | return Failed; |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 281 | } |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 282 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 283 | } |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 284 | |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 285 | using namespace clang; |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 286 | |
| 287 | //---------------------------------------------------------------------------- |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 288 | // Structural Equivalence |
| 289 | //---------------------------------------------------------------------------- |
| 290 | |
| 291 | namespace { |
| 292 | struct StructuralEquivalenceContext { |
| 293 | /// \brief AST contexts for which we are checking structural equivalence. |
| 294 | ASTContext &C1, &C2; |
| 295 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 296 | /// \brief The set of "tentative" equivalences between two canonical |
| 297 | /// declarations, mapping from a declaration in the first context to the |
| 298 | /// declaration in the second context that we believe to be equivalent. |
| 299 | llvm::DenseMap<Decl *, Decl *> TentativeEquivalences; |
| 300 | |
| 301 | /// \brief Queue of declarations in the first context whose equivalence |
| 302 | /// with a declaration in the second context still needs to be verified. |
| 303 | std::deque<Decl *> DeclsToCheck; |
| 304 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 305 | /// \brief Declaration (from, to) pairs that are known not to be equivalent |
| 306 | /// (which we have already complained about). |
| 307 | llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls; |
| 308 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 309 | /// \brief Whether we're being strict about the spelling of types when |
| 310 | /// unifying two types. |
| 311 | bool StrictTypeSpelling; |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 312 | |
| 313 | /// \brief Whether to complain about failures. |
| 314 | bool Complain; |
| 315 | |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 316 | /// \brief \c true if the last diagnostic came from C2. |
| 317 | bool LastDiagFromC2; |
| 318 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 319 | StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2, |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 320 | llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls, |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 321 | bool StrictTypeSpelling = false, |
| 322 | bool Complain = true) |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 323 | : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls), |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 324 | StrictTypeSpelling(StrictTypeSpelling), Complain(Complain), |
| 325 | LastDiagFromC2(false) {} |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 326 | |
| 327 | /// \brief Determine whether the two declarations are structurally |
| 328 | /// equivalent. |
| 329 | bool IsStructurallyEquivalent(Decl *D1, Decl *D2); |
| 330 | |
| 331 | /// \brief Determine whether the two types are structurally equivalent. |
| 332 | bool IsStructurallyEquivalent(QualType T1, QualType T2); |
| 333 | |
| 334 | private: |
| 335 | /// \brief Finish checking all of the structural equivalences. |
| 336 | /// |
| 337 | /// \returns true if an error occurred, false otherwise. |
| 338 | bool Finish(); |
| 339 | |
| 340 | public: |
| 341 | DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 342 | assert(Complain && "Not allowed to complain"); |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 343 | if (LastDiagFromC2) |
| 344 | C1.getDiagnostics().notePriorDiagnosticFrom(C2.getDiagnostics()); |
| 345 | LastDiagFromC2 = false; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 346 | return C1.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 350 | assert(Complain && "Not allowed to complain"); |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 351 | if (!LastDiagFromC2) |
| 352 | C2.getDiagnostics().notePriorDiagnosticFrom(C1.getDiagnostics()); |
| 353 | LastDiagFromC2 = true; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 354 | return C2.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 355 | } |
| 356 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 357 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 358 | |
| 359 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 360 | QualType T1, QualType T2); |
| 361 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 362 | Decl *D1, Decl *D2); |
| 363 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 364 | /// \brief Determine structural equivalence of two expressions. |
| 365 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 366 | Expr *E1, Expr *E2) { |
| 367 | if (!E1 || !E2) |
| 368 | return E1 == E2; |
| 369 | |
| 370 | // FIXME: Actually perform a structural comparison! |
| 371 | return true; |
| 372 | } |
| 373 | |
| 374 | /// \brief Determine whether two identifiers are equivalent. |
| 375 | static bool IsStructurallyEquivalent(const IdentifierInfo *Name1, |
| 376 | const IdentifierInfo *Name2) { |
| 377 | if (!Name1 || !Name2) |
| 378 | return Name1 == Name2; |
| 379 | |
| 380 | return Name1->getName() == Name2->getName(); |
| 381 | } |
| 382 | |
| 383 | /// \brief Determine whether two nested-name-specifiers are equivalent. |
| 384 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 385 | NestedNameSpecifier *NNS1, |
| 386 | NestedNameSpecifier *NNS2) { |
| 387 | // FIXME: Implement! |
| 388 | return true; |
| 389 | } |
| 390 | |
| 391 | /// \brief Determine whether two template arguments are equivalent. |
| 392 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 393 | const TemplateArgument &Arg1, |
| 394 | const TemplateArgument &Arg2) { |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 395 | if (Arg1.getKind() != Arg2.getKind()) |
| 396 | return false; |
| 397 | |
| 398 | switch (Arg1.getKind()) { |
| 399 | case TemplateArgument::Null: |
| 400 | return true; |
| 401 | |
| 402 | case TemplateArgument::Type: |
| 403 | return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType()); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 404 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 405 | case TemplateArgument::Integral: |
| 406 | if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(), |
| 407 | Arg2.getIntegralType())) |
| 408 | return false; |
| 409 | |
Eric Christopher | 6dcc376 | 2012-07-15 00:23:57 +0000 | [diff] [blame] | 410 | return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral()); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 411 | |
| 412 | case TemplateArgument::Declaration: |
| 413 | return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl()); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 414 | |
| 415 | case TemplateArgument::NullPtr: |
| 416 | return true; // FIXME: Is this correct? |
| 417 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 418 | case TemplateArgument::Template: |
| 419 | return IsStructurallyEquivalent(Context, |
| 420 | Arg1.getAsTemplate(), |
| 421 | Arg2.getAsTemplate()); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 422 | |
| 423 | case TemplateArgument::TemplateExpansion: |
| 424 | return IsStructurallyEquivalent(Context, |
| 425 | Arg1.getAsTemplateOrTemplatePattern(), |
| 426 | Arg2.getAsTemplateOrTemplatePattern()); |
| 427 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 428 | case TemplateArgument::Expression: |
| 429 | return IsStructurallyEquivalent(Context, |
| 430 | Arg1.getAsExpr(), Arg2.getAsExpr()); |
| 431 | |
| 432 | case TemplateArgument::Pack: |
| 433 | if (Arg1.pack_size() != Arg2.pack_size()) |
| 434 | return false; |
| 435 | |
| 436 | for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I) |
| 437 | if (!IsStructurallyEquivalent(Context, |
| 438 | Arg1.pack_begin()[I], |
| 439 | Arg2.pack_begin()[I])) |
| 440 | return false; |
| 441 | |
| 442 | return true; |
| 443 | } |
| 444 | |
| 445 | llvm_unreachable("Invalid template argument kind"); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /// \brief Determine structural equivalence for the common part of array |
| 449 | /// types. |
| 450 | static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 451 | const ArrayType *Array1, |
| 452 | const ArrayType *Array2) { |
| 453 | if (!IsStructurallyEquivalent(Context, |
| 454 | Array1->getElementType(), |
| 455 | Array2->getElementType())) |
| 456 | return false; |
| 457 | if (Array1->getSizeModifier() != Array2->getSizeModifier()) |
| 458 | return false; |
| 459 | if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers()) |
| 460 | return false; |
| 461 | |
| 462 | return true; |
| 463 | } |
| 464 | |
| 465 | /// \brief Determine structural equivalence of two types. |
| 466 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 467 | QualType T1, QualType T2) { |
| 468 | if (T1.isNull() || T2.isNull()) |
| 469 | return T1.isNull() && T2.isNull(); |
| 470 | |
| 471 | if (!Context.StrictTypeSpelling) { |
| 472 | // We aren't being strict about token-to-token equivalence of types, |
| 473 | // so map down to the canonical type. |
| 474 | T1 = Context.C1.getCanonicalType(T1); |
| 475 | T2 = Context.C2.getCanonicalType(T2); |
| 476 | } |
| 477 | |
| 478 | if (T1.getQualifiers() != T2.getQualifiers()) |
| 479 | return false; |
| 480 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 481 | Type::TypeClass TC = T1->getTypeClass(); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 482 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 483 | if (T1->getTypeClass() != T2->getTypeClass()) { |
| 484 | // Compare function types with prototypes vs. without prototypes as if |
| 485 | // both did not have prototypes. |
| 486 | if (T1->getTypeClass() == Type::FunctionProto && |
| 487 | T2->getTypeClass() == Type::FunctionNoProto) |
| 488 | TC = Type::FunctionNoProto; |
| 489 | else if (T1->getTypeClass() == Type::FunctionNoProto && |
| 490 | T2->getTypeClass() == Type::FunctionProto) |
| 491 | TC = Type::FunctionNoProto; |
| 492 | else |
| 493 | return false; |
| 494 | } |
| 495 | |
| 496 | switch (TC) { |
| 497 | case Type::Builtin: |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 498 | // FIXME: Deal with Char_S/Char_U. |
| 499 | if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind()) |
| 500 | return false; |
| 501 | break; |
| 502 | |
| 503 | case Type::Complex: |
| 504 | if (!IsStructurallyEquivalent(Context, |
| 505 | cast<ComplexType>(T1)->getElementType(), |
| 506 | cast<ComplexType>(T2)->getElementType())) |
| 507 | return false; |
| 508 | break; |
| 509 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 510 | case Type::Adjusted: |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 511 | case Type::Decayed: |
| 512 | if (!IsStructurallyEquivalent(Context, |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 513 | cast<AdjustedType>(T1)->getOriginalType(), |
| 514 | cast<AdjustedType>(T2)->getOriginalType())) |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 515 | return false; |
| 516 | break; |
| 517 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 518 | case Type::Pointer: |
| 519 | if (!IsStructurallyEquivalent(Context, |
| 520 | cast<PointerType>(T1)->getPointeeType(), |
| 521 | cast<PointerType>(T2)->getPointeeType())) |
| 522 | return false; |
| 523 | break; |
| 524 | |
| 525 | case Type::BlockPointer: |
| 526 | if (!IsStructurallyEquivalent(Context, |
| 527 | cast<BlockPointerType>(T1)->getPointeeType(), |
| 528 | cast<BlockPointerType>(T2)->getPointeeType())) |
| 529 | return false; |
| 530 | break; |
| 531 | |
| 532 | case Type::LValueReference: |
| 533 | case Type::RValueReference: { |
| 534 | const ReferenceType *Ref1 = cast<ReferenceType>(T1); |
| 535 | const ReferenceType *Ref2 = cast<ReferenceType>(T2); |
| 536 | if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue()) |
| 537 | return false; |
| 538 | if (Ref1->isInnerRef() != Ref2->isInnerRef()) |
| 539 | return false; |
| 540 | if (!IsStructurallyEquivalent(Context, |
| 541 | Ref1->getPointeeTypeAsWritten(), |
| 542 | Ref2->getPointeeTypeAsWritten())) |
| 543 | return false; |
| 544 | break; |
| 545 | } |
| 546 | |
| 547 | case Type::MemberPointer: { |
| 548 | const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1); |
| 549 | const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2); |
| 550 | if (!IsStructurallyEquivalent(Context, |
| 551 | MemPtr1->getPointeeType(), |
| 552 | MemPtr2->getPointeeType())) |
| 553 | return false; |
| 554 | if (!IsStructurallyEquivalent(Context, |
| 555 | QualType(MemPtr1->getClass(), 0), |
| 556 | QualType(MemPtr2->getClass(), 0))) |
| 557 | return false; |
| 558 | break; |
| 559 | } |
| 560 | |
| 561 | case Type::ConstantArray: { |
| 562 | const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1); |
| 563 | const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2); |
Eric Christopher | 6dcc376 | 2012-07-15 00:23:57 +0000 | [diff] [blame] | 564 | if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 565 | return false; |
| 566 | |
| 567 | if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) |
| 568 | return false; |
| 569 | break; |
| 570 | } |
| 571 | |
| 572 | case Type::IncompleteArray: |
| 573 | if (!IsArrayStructurallyEquivalent(Context, |
| 574 | cast<ArrayType>(T1), |
| 575 | cast<ArrayType>(T2))) |
| 576 | return false; |
| 577 | break; |
| 578 | |
| 579 | case Type::VariableArray: { |
| 580 | const VariableArrayType *Array1 = cast<VariableArrayType>(T1); |
| 581 | const VariableArrayType *Array2 = cast<VariableArrayType>(T2); |
| 582 | if (!IsStructurallyEquivalent(Context, |
| 583 | Array1->getSizeExpr(), Array2->getSizeExpr())) |
| 584 | return false; |
| 585 | |
| 586 | if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) |
| 587 | return false; |
| 588 | |
| 589 | break; |
| 590 | } |
| 591 | |
| 592 | case Type::DependentSizedArray: { |
| 593 | const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1); |
| 594 | const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2); |
| 595 | if (!IsStructurallyEquivalent(Context, |
| 596 | Array1->getSizeExpr(), Array2->getSizeExpr())) |
| 597 | return false; |
| 598 | |
| 599 | if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) |
| 600 | return false; |
| 601 | |
| 602 | break; |
| 603 | } |
| 604 | |
| 605 | case Type::DependentSizedExtVector: { |
| 606 | const DependentSizedExtVectorType *Vec1 |
| 607 | = cast<DependentSizedExtVectorType>(T1); |
| 608 | const DependentSizedExtVectorType *Vec2 |
| 609 | = cast<DependentSizedExtVectorType>(T2); |
| 610 | if (!IsStructurallyEquivalent(Context, |
| 611 | Vec1->getSizeExpr(), Vec2->getSizeExpr())) |
| 612 | return false; |
| 613 | if (!IsStructurallyEquivalent(Context, |
| 614 | Vec1->getElementType(), |
| 615 | Vec2->getElementType())) |
| 616 | return false; |
| 617 | break; |
| 618 | } |
| 619 | |
| 620 | case Type::Vector: |
| 621 | case Type::ExtVector: { |
| 622 | const VectorType *Vec1 = cast<VectorType>(T1); |
| 623 | const VectorType *Vec2 = cast<VectorType>(T2); |
| 624 | if (!IsStructurallyEquivalent(Context, |
| 625 | Vec1->getElementType(), |
| 626 | Vec2->getElementType())) |
| 627 | return false; |
| 628 | if (Vec1->getNumElements() != Vec2->getNumElements()) |
| 629 | return false; |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 630 | if (Vec1->getVectorKind() != Vec2->getVectorKind()) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 631 | return false; |
Douglas Gregor | 01cc437 | 2010-02-19 01:36:36 +0000 | [diff] [blame] | 632 | break; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | case Type::FunctionProto: { |
| 636 | const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1); |
| 637 | const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 638 | if (Proto1->getNumParams() != Proto2->getNumParams()) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 639 | return false; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 640 | for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) { |
| 641 | if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I), |
| 642 | Proto2->getParamType(I))) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 643 | return false; |
| 644 | } |
| 645 | if (Proto1->isVariadic() != Proto2->isVariadic()) |
| 646 | return false; |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 647 | if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType()) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 648 | return false; |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 649 | if (Proto1->getExceptionSpecType() == EST_Dynamic) { |
| 650 | if (Proto1->getNumExceptions() != Proto2->getNumExceptions()) |
| 651 | return false; |
| 652 | for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) { |
| 653 | if (!IsStructurallyEquivalent(Context, |
| 654 | Proto1->getExceptionType(I), |
| 655 | Proto2->getExceptionType(I))) |
| 656 | return false; |
| 657 | } |
| 658 | } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 659 | if (!IsStructurallyEquivalent(Context, |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 660 | Proto1->getNoexceptExpr(), |
| 661 | Proto2->getNoexceptExpr())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 662 | return false; |
| 663 | } |
| 664 | if (Proto1->getTypeQuals() != Proto2->getTypeQuals()) |
| 665 | return false; |
| 666 | |
| 667 | // Fall through to check the bits common with FunctionNoProtoType. |
| 668 | } |
| 669 | |
| 670 | case Type::FunctionNoProto: { |
| 671 | const FunctionType *Function1 = cast<FunctionType>(T1); |
| 672 | const FunctionType *Function2 = cast<FunctionType>(T2); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 673 | if (!IsStructurallyEquivalent(Context, Function1->getReturnType(), |
| 674 | Function2->getReturnType())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 675 | return false; |
Justin Bogner | 62c04de | 2016-03-20 16:58:03 +0000 | [diff] [blame] | 676 | if (Function1->getExtInfo() != Function2->getExtInfo()) |
| 677 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 678 | break; |
| 679 | } |
| 680 | |
| 681 | case Type::UnresolvedUsing: |
| 682 | if (!IsStructurallyEquivalent(Context, |
| 683 | cast<UnresolvedUsingType>(T1)->getDecl(), |
| 684 | cast<UnresolvedUsingType>(T2)->getDecl())) |
| 685 | return false; |
| 686 | |
| 687 | break; |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 688 | |
| 689 | case Type::Attributed: |
| 690 | if (!IsStructurallyEquivalent(Context, |
| 691 | cast<AttributedType>(T1)->getModifiedType(), |
| 692 | cast<AttributedType>(T2)->getModifiedType())) |
| 693 | return false; |
| 694 | if (!IsStructurallyEquivalent(Context, |
| 695 | cast<AttributedType>(T1)->getEquivalentType(), |
| 696 | cast<AttributedType>(T2)->getEquivalentType())) |
| 697 | return false; |
| 698 | break; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 699 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 700 | case Type::Paren: |
| 701 | if (!IsStructurallyEquivalent(Context, |
| 702 | cast<ParenType>(T1)->getInnerType(), |
| 703 | cast<ParenType>(T2)->getInnerType())) |
| 704 | return false; |
| 705 | break; |
| 706 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 707 | case Type::Typedef: |
| 708 | if (!IsStructurallyEquivalent(Context, |
| 709 | cast<TypedefType>(T1)->getDecl(), |
| 710 | cast<TypedefType>(T2)->getDecl())) |
| 711 | return false; |
| 712 | break; |
| 713 | |
| 714 | case Type::TypeOfExpr: |
| 715 | if (!IsStructurallyEquivalent(Context, |
| 716 | cast<TypeOfExprType>(T1)->getUnderlyingExpr(), |
| 717 | cast<TypeOfExprType>(T2)->getUnderlyingExpr())) |
| 718 | return false; |
| 719 | break; |
| 720 | |
| 721 | case Type::TypeOf: |
| 722 | if (!IsStructurallyEquivalent(Context, |
| 723 | cast<TypeOfType>(T1)->getUnderlyingType(), |
| 724 | cast<TypeOfType>(T2)->getUnderlyingType())) |
| 725 | return false; |
| 726 | break; |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 727 | |
| 728 | case Type::UnaryTransform: |
| 729 | if (!IsStructurallyEquivalent(Context, |
| 730 | cast<UnaryTransformType>(T1)->getUnderlyingType(), |
| 731 | cast<UnaryTransformType>(T1)->getUnderlyingType())) |
| 732 | return false; |
| 733 | break; |
| 734 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 735 | case Type::Decltype: |
| 736 | if (!IsStructurallyEquivalent(Context, |
| 737 | cast<DecltypeType>(T1)->getUnderlyingExpr(), |
| 738 | cast<DecltypeType>(T2)->getUnderlyingExpr())) |
| 739 | return false; |
| 740 | break; |
| 741 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 742 | case Type::Auto: |
| 743 | if (!IsStructurallyEquivalent(Context, |
| 744 | cast<AutoType>(T1)->getDeducedType(), |
| 745 | cast<AutoType>(T2)->getDeducedType())) |
| 746 | return false; |
| 747 | break; |
| 748 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 749 | case Type::Record: |
| 750 | case Type::Enum: |
| 751 | if (!IsStructurallyEquivalent(Context, |
| 752 | cast<TagType>(T1)->getDecl(), |
| 753 | cast<TagType>(T2)->getDecl())) |
| 754 | return false; |
| 755 | break; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 756 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 757 | case Type::TemplateTypeParm: { |
| 758 | const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1); |
| 759 | const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2); |
| 760 | if (Parm1->getDepth() != Parm2->getDepth()) |
| 761 | return false; |
| 762 | if (Parm1->getIndex() != Parm2->getIndex()) |
| 763 | return false; |
| 764 | if (Parm1->isParameterPack() != Parm2->isParameterPack()) |
| 765 | return false; |
| 766 | |
| 767 | // Names of template type parameters are never significant. |
| 768 | break; |
| 769 | } |
| 770 | |
| 771 | case Type::SubstTemplateTypeParm: { |
| 772 | const SubstTemplateTypeParmType *Subst1 |
| 773 | = cast<SubstTemplateTypeParmType>(T1); |
| 774 | const SubstTemplateTypeParmType *Subst2 |
| 775 | = cast<SubstTemplateTypeParmType>(T2); |
| 776 | if (!IsStructurallyEquivalent(Context, |
| 777 | QualType(Subst1->getReplacedParameter(), 0), |
| 778 | QualType(Subst2->getReplacedParameter(), 0))) |
| 779 | return false; |
| 780 | if (!IsStructurallyEquivalent(Context, |
| 781 | Subst1->getReplacementType(), |
| 782 | Subst2->getReplacementType())) |
| 783 | return false; |
| 784 | break; |
| 785 | } |
| 786 | |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 787 | case Type::SubstTemplateTypeParmPack: { |
| 788 | const SubstTemplateTypeParmPackType *Subst1 |
| 789 | = cast<SubstTemplateTypeParmPackType>(T1); |
| 790 | const SubstTemplateTypeParmPackType *Subst2 |
| 791 | = cast<SubstTemplateTypeParmPackType>(T2); |
| 792 | if (!IsStructurallyEquivalent(Context, |
| 793 | QualType(Subst1->getReplacedParameter(), 0), |
| 794 | QualType(Subst2->getReplacedParameter(), 0))) |
| 795 | return false; |
| 796 | if (!IsStructurallyEquivalent(Context, |
| 797 | Subst1->getArgumentPack(), |
| 798 | Subst2->getArgumentPack())) |
| 799 | return false; |
| 800 | break; |
| 801 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 802 | case Type::TemplateSpecialization: { |
| 803 | const TemplateSpecializationType *Spec1 |
| 804 | = cast<TemplateSpecializationType>(T1); |
| 805 | const TemplateSpecializationType *Spec2 |
| 806 | = cast<TemplateSpecializationType>(T2); |
| 807 | if (!IsStructurallyEquivalent(Context, |
| 808 | Spec1->getTemplateName(), |
| 809 | Spec2->getTemplateName())) |
| 810 | return false; |
| 811 | if (Spec1->getNumArgs() != Spec2->getNumArgs()) |
| 812 | return false; |
| 813 | for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) { |
| 814 | if (!IsStructurallyEquivalent(Context, |
| 815 | Spec1->getArg(I), Spec2->getArg(I))) |
| 816 | return false; |
| 817 | } |
| 818 | break; |
| 819 | } |
| 820 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 821 | case Type::Elaborated: { |
| 822 | const ElaboratedType *Elab1 = cast<ElaboratedType>(T1); |
| 823 | const ElaboratedType *Elab2 = cast<ElaboratedType>(T2); |
| 824 | // CHECKME: what if a keyword is ETK_None or ETK_typename ? |
| 825 | if (Elab1->getKeyword() != Elab2->getKeyword()) |
| 826 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 827 | if (!IsStructurallyEquivalent(Context, |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 828 | Elab1->getQualifier(), |
| 829 | Elab2->getQualifier())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 830 | return false; |
| 831 | if (!IsStructurallyEquivalent(Context, |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 832 | Elab1->getNamedType(), |
| 833 | Elab2->getNamedType())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 834 | return false; |
| 835 | break; |
| 836 | } |
| 837 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 838 | case Type::InjectedClassName: { |
| 839 | const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1); |
| 840 | const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2); |
| 841 | if (!IsStructurallyEquivalent(Context, |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 842 | Inj1->getInjectedSpecializationType(), |
| 843 | Inj2->getInjectedSpecializationType())) |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 844 | return false; |
| 845 | break; |
| 846 | } |
| 847 | |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 848 | case Type::DependentName: { |
| 849 | const DependentNameType *Typename1 = cast<DependentNameType>(T1); |
| 850 | const DependentNameType *Typename2 = cast<DependentNameType>(T2); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 851 | if (!IsStructurallyEquivalent(Context, |
| 852 | Typename1->getQualifier(), |
| 853 | Typename2->getQualifier())) |
| 854 | return false; |
| 855 | if (!IsStructurallyEquivalent(Typename1->getIdentifier(), |
| 856 | Typename2->getIdentifier())) |
| 857 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 858 | |
| 859 | break; |
| 860 | } |
| 861 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 862 | case Type::DependentTemplateSpecialization: { |
| 863 | const DependentTemplateSpecializationType *Spec1 = |
| 864 | cast<DependentTemplateSpecializationType>(T1); |
| 865 | const DependentTemplateSpecializationType *Spec2 = |
| 866 | cast<DependentTemplateSpecializationType>(T2); |
| 867 | if (!IsStructurallyEquivalent(Context, |
| 868 | Spec1->getQualifier(), |
| 869 | Spec2->getQualifier())) |
| 870 | return false; |
| 871 | if (!IsStructurallyEquivalent(Spec1->getIdentifier(), |
| 872 | Spec2->getIdentifier())) |
| 873 | return false; |
| 874 | if (Spec1->getNumArgs() != Spec2->getNumArgs()) |
| 875 | return false; |
| 876 | for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) { |
| 877 | if (!IsStructurallyEquivalent(Context, |
| 878 | Spec1->getArg(I), Spec2->getArg(I))) |
| 879 | return false; |
| 880 | } |
| 881 | break; |
| 882 | } |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 883 | |
| 884 | case Type::PackExpansion: |
| 885 | if (!IsStructurallyEquivalent(Context, |
| 886 | cast<PackExpansionType>(T1)->getPattern(), |
| 887 | cast<PackExpansionType>(T2)->getPattern())) |
| 888 | return false; |
| 889 | break; |
| 890 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 891 | case Type::ObjCInterface: { |
| 892 | const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1); |
| 893 | const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2); |
| 894 | if (!IsStructurallyEquivalent(Context, |
| 895 | Iface1->getDecl(), Iface2->getDecl())) |
| 896 | return false; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 897 | break; |
| 898 | } |
| 899 | |
| 900 | case Type::ObjCObject: { |
| 901 | const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1); |
| 902 | const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2); |
| 903 | if (!IsStructurallyEquivalent(Context, |
| 904 | Obj1->getBaseType(), |
| 905 | Obj2->getBaseType())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 906 | return false; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 907 | if (Obj1->getNumProtocols() != Obj2->getNumProtocols()) |
| 908 | return false; |
| 909 | for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 910 | if (!IsStructurallyEquivalent(Context, |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 911 | Obj1->getProtocol(I), |
| 912 | Obj2->getProtocol(I))) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 913 | return false; |
| 914 | } |
| 915 | break; |
| 916 | } |
| 917 | |
| 918 | case Type::ObjCObjectPointer: { |
| 919 | const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1); |
| 920 | const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2); |
| 921 | if (!IsStructurallyEquivalent(Context, |
| 922 | Ptr1->getPointeeType(), |
| 923 | Ptr2->getPointeeType())) |
| 924 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 925 | break; |
| 926 | } |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 927 | |
| 928 | case Type::Atomic: { |
| 929 | if (!IsStructurallyEquivalent(Context, |
| 930 | cast<AtomicType>(T1)->getValueType(), |
| 931 | cast<AtomicType>(T2)->getValueType())) |
| 932 | return false; |
| 933 | break; |
| 934 | } |
| 935 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 936 | case Type::Pipe: { |
| 937 | if (!IsStructurallyEquivalent(Context, |
| 938 | cast<PipeType>(T1)->getElementType(), |
| 939 | cast<PipeType>(T2)->getElementType())) |
| 940 | return false; |
| 941 | break; |
| 942 | } |
| 943 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 944 | } // end switch |
| 945 | |
| 946 | return true; |
| 947 | } |
| 948 | |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 949 | /// \brief Determine structural equivalence of two fields. |
| 950 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 951 | FieldDecl *Field1, FieldDecl *Field2) { |
| 952 | RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext()); |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 953 | |
| 954 | // For anonymous structs/unions, match up the anonymous struct/union type |
| 955 | // declarations directly, so that we don't go off searching for anonymous |
| 956 | // types |
| 957 | if (Field1->isAnonymousStructOrUnion() && |
| 958 | Field2->isAnonymousStructOrUnion()) { |
| 959 | RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl(); |
| 960 | RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl(); |
| 961 | return IsStructurallyEquivalent(Context, D1, D2); |
| 962 | } |
Sean Callanan | 969c5bd | 2013-04-26 22:49:25 +0000 | [diff] [blame] | 963 | |
| 964 | // Check for equivalent field names. |
| 965 | IdentifierInfo *Name1 = Field1->getIdentifier(); |
| 966 | IdentifierInfo *Name2 = Field2->getIdentifier(); |
| 967 | if (!::IsStructurallyEquivalent(Name1, Name2)) |
| 968 | return false; |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 969 | |
| 970 | if (!IsStructurallyEquivalent(Context, |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 971 | Field1->getType(), Field2->getType())) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 972 | if (Context.Complain) { |
| 973 | Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 974 | << Context.C2.getTypeDeclType(Owner2); |
| 975 | Context.Diag2(Field2->getLocation(), diag::note_odr_field) |
| 976 | << Field2->getDeclName() << Field2->getType(); |
| 977 | Context.Diag1(Field1->getLocation(), diag::note_odr_field) |
| 978 | << Field1->getDeclName() << Field1->getType(); |
| 979 | } |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 980 | return false; |
| 981 | } |
| 982 | |
| 983 | if (Field1->isBitField() != Field2->isBitField()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 984 | if (Context.Complain) { |
| 985 | Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 986 | << Context.C2.getTypeDeclType(Owner2); |
| 987 | if (Field1->isBitField()) { |
| 988 | Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field) |
| 989 | << Field1->getDeclName() << Field1->getType() |
| 990 | << Field1->getBitWidthValue(Context.C1); |
| 991 | Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field) |
| 992 | << Field2->getDeclName(); |
| 993 | } else { |
| 994 | Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field) |
| 995 | << Field2->getDeclName() << Field2->getType() |
| 996 | << Field2->getBitWidthValue(Context.C2); |
| 997 | Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field) |
| 998 | << Field1->getDeclName(); |
| 999 | } |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 1000 | } |
| 1001 | return false; |
| 1002 | } |
| 1003 | |
| 1004 | if (Field1->isBitField()) { |
| 1005 | // Make sure that the bit-fields are the same length. |
| 1006 | unsigned Bits1 = Field1->getBitWidthValue(Context.C1); |
| 1007 | unsigned Bits2 = Field2->getBitWidthValue(Context.C2); |
| 1008 | |
| 1009 | if (Bits1 != Bits2) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1010 | if (Context.Complain) { |
| 1011 | Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1012 | << Context.C2.getTypeDeclType(Owner2); |
| 1013 | Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field) |
| 1014 | << Field2->getDeclName() << Field2->getType() << Bits2; |
| 1015 | Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field) |
| 1016 | << Field1->getDeclName() << Field1->getType() << Bits1; |
| 1017 | } |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 1018 | return false; |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | return true; |
| 1023 | } |
| 1024 | |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 1025 | /// \brief Find the index of the given anonymous struct/union within its |
| 1026 | /// context. |
| 1027 | /// |
| 1028 | /// \returns Returns the index of this anonymous struct/union in its context, |
| 1029 | /// including the next assigned index (if none of them match). Returns an |
| 1030 | /// empty option if the context is not a record, i.e.. if the anonymous |
| 1031 | /// struct/union is at namespace or block scope. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1032 | static Optional<unsigned> findAnonymousStructOrUnionIndex(RecordDecl *Anon) { |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 1033 | ASTContext &Context = Anon->getASTContext(); |
| 1034 | QualType AnonTy = Context.getRecordType(Anon); |
| 1035 | |
| 1036 | RecordDecl *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext()); |
| 1037 | if (!Owner) |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 1038 | return None; |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 1039 | |
| 1040 | unsigned Index = 0; |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 1041 | for (const auto *D : Owner->noload_decls()) { |
| 1042 | const auto *F = dyn_cast<FieldDecl>(D); |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 1043 | if (!F || !F->isAnonymousStructOrUnion()) |
| 1044 | continue; |
| 1045 | |
| 1046 | if (Context.hasSameType(F->getType(), AnonTy)) |
| 1047 | break; |
| 1048 | |
| 1049 | ++Index; |
| 1050 | } |
| 1051 | |
| 1052 | return Index; |
| 1053 | } |
| 1054 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1055 | /// \brief Determine structural equivalence of two records. |
| 1056 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1057 | RecordDecl *D1, RecordDecl *D2) { |
| 1058 | if (D1->isUnion() != D2->isUnion()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1059 | if (Context.Complain) { |
| 1060 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1061 | << Context.C2.getTypeDeclType(D2); |
| 1062 | Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here) |
| 1063 | << D1->getDeclName() << (unsigned)D1->getTagKind(); |
| 1064 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1065 | return false; |
| 1066 | } |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 1067 | |
| 1068 | if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) { |
| 1069 | // If both anonymous structs/unions are in a record context, make sure |
| 1070 | // they occur in the same location in the context records. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1071 | if (Optional<unsigned> Index1 = findAnonymousStructOrUnionIndex(D1)) { |
| 1072 | if (Optional<unsigned> Index2 = findAnonymousStructOrUnionIndex(D2)) { |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 1073 | if (*Index1 != *Index2) |
| 1074 | return false; |
| 1075 | } |
| 1076 | } |
| 1077 | } |
| 1078 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1079 | // If both declarations are class template specializations, we know |
| 1080 | // the ODR applies, so check the template and template arguments. |
| 1081 | ClassTemplateSpecializationDecl *Spec1 |
| 1082 | = dyn_cast<ClassTemplateSpecializationDecl>(D1); |
| 1083 | ClassTemplateSpecializationDecl *Spec2 |
| 1084 | = dyn_cast<ClassTemplateSpecializationDecl>(D2); |
| 1085 | if (Spec1 && Spec2) { |
| 1086 | // Check that the specialized templates are the same. |
| 1087 | if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(), |
| 1088 | Spec2->getSpecializedTemplate())) |
| 1089 | return false; |
| 1090 | |
| 1091 | // Check that the template arguments are the same. |
| 1092 | if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size()) |
| 1093 | return false; |
| 1094 | |
| 1095 | for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I) |
| 1096 | if (!IsStructurallyEquivalent(Context, |
| 1097 | Spec1->getTemplateArgs().get(I), |
| 1098 | Spec2->getTemplateArgs().get(I))) |
| 1099 | return false; |
| 1100 | } |
| 1101 | // 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] | 1102 | // structures are different. |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1103 | else if (Spec1 || Spec2) |
| 1104 | return false; |
| 1105 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1106 | // Compare the definitions of these two records. If either or both are |
| 1107 | // incomplete, we assume that they are equivalent. |
| 1108 | D1 = D1->getDefinition(); |
| 1109 | D2 = D2->getDefinition(); |
| 1110 | if (!D1 || !D2) |
| 1111 | return true; |
| 1112 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1113 | if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) { |
| 1114 | if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) { |
| 1115 | if (D1CXX->getNumBases() != D2CXX->getNumBases()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1116 | if (Context.Complain) { |
| 1117 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1118 | << Context.C2.getTypeDeclType(D2); |
| 1119 | Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases) |
| 1120 | << D2CXX->getNumBases(); |
| 1121 | Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases) |
| 1122 | << D1CXX->getNumBases(); |
| 1123 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1124 | return false; |
| 1125 | } |
| 1126 | |
| 1127 | // Check the base classes. |
| 1128 | for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(), |
| 1129 | BaseEnd1 = D1CXX->bases_end(), |
| 1130 | Base2 = D2CXX->bases_begin(); |
| 1131 | Base1 != BaseEnd1; |
| 1132 | ++Base1, ++Base2) { |
| 1133 | if (!IsStructurallyEquivalent(Context, |
| 1134 | Base1->getType(), Base2->getType())) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1135 | if (Context.Complain) { |
| 1136 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1137 | << Context.C2.getTypeDeclType(D2); |
| 1138 | Context.Diag2(Base2->getLocStart(), diag::note_odr_base) |
| 1139 | << Base2->getType() |
| 1140 | << Base2->getSourceRange(); |
| 1141 | Context.Diag1(Base1->getLocStart(), diag::note_odr_base) |
| 1142 | << Base1->getType() |
| 1143 | << Base1->getSourceRange(); |
| 1144 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1145 | return false; |
| 1146 | } |
| 1147 | |
| 1148 | // Check virtual vs. non-virtual inheritance mismatch. |
| 1149 | if (Base1->isVirtual() != Base2->isVirtual()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1150 | if (Context.Complain) { |
| 1151 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1152 | << Context.C2.getTypeDeclType(D2); |
| 1153 | Context.Diag2(Base2->getLocStart(), |
| 1154 | diag::note_odr_virtual_base) |
| 1155 | << Base2->isVirtual() << Base2->getSourceRange(); |
| 1156 | Context.Diag1(Base1->getLocStart(), diag::note_odr_base) |
| 1157 | << Base1->isVirtual() |
| 1158 | << Base1->getSourceRange(); |
| 1159 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1160 | return false; |
| 1161 | } |
| 1162 | } |
| 1163 | } else if (D1CXX->getNumBases() > 0) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1164 | if (Context.Complain) { |
| 1165 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1166 | << Context.C2.getTypeDeclType(D2); |
| 1167 | const CXXBaseSpecifier *Base1 = D1CXX->bases_begin(); |
| 1168 | Context.Diag1(Base1->getLocStart(), diag::note_odr_base) |
| 1169 | << Base1->getType() |
| 1170 | << Base1->getSourceRange(); |
| 1171 | Context.Diag2(D2->getLocation(), diag::note_odr_missing_base); |
| 1172 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1173 | return false; |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | // Check the fields for consistency. |
Dmitri Gribenko | 898cff0 | 2012-05-19 17:17:26 +0000 | [diff] [blame] | 1178 | RecordDecl::field_iterator Field2 = D2->field_begin(), |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1179 | Field2End = D2->field_end(); |
Dmitri Gribenko | 898cff0 | 2012-05-19 17:17:26 +0000 | [diff] [blame] | 1180 | for (RecordDecl::field_iterator Field1 = D1->field_begin(), |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1181 | Field1End = D1->field_end(); |
| 1182 | Field1 != Field1End; |
| 1183 | ++Field1, ++Field2) { |
| 1184 | if (Field2 == Field2End) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1185 | if (Context.Complain) { |
| 1186 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1187 | << Context.C2.getTypeDeclType(D2); |
| 1188 | Context.Diag1(Field1->getLocation(), diag::note_odr_field) |
| 1189 | << Field1->getDeclName() << Field1->getType(); |
| 1190 | Context.Diag2(D2->getLocation(), diag::note_odr_missing_field); |
| 1191 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1192 | return false; |
| 1193 | } |
| 1194 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1195 | if (!IsStructurallyEquivalent(Context, *Field1, *Field2)) |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 1196 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | if (Field2 != Field2End) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1200 | if (Context.Complain) { |
| 1201 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1202 | << Context.C2.getTypeDeclType(D2); |
| 1203 | Context.Diag2(Field2->getLocation(), diag::note_odr_field) |
| 1204 | << Field2->getDeclName() << Field2->getType(); |
| 1205 | Context.Diag1(D1->getLocation(), diag::note_odr_missing_field); |
| 1206 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1207 | return false; |
| 1208 | } |
| 1209 | |
| 1210 | return true; |
| 1211 | } |
| 1212 | |
| 1213 | /// \brief Determine structural equivalence of two enums. |
| 1214 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1215 | EnumDecl *D1, EnumDecl *D2) { |
| 1216 | EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(), |
| 1217 | EC2End = D2->enumerator_end(); |
| 1218 | for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(), |
| 1219 | EC1End = D1->enumerator_end(); |
| 1220 | EC1 != EC1End; ++EC1, ++EC2) { |
| 1221 | if (EC2 == EC2End) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1222 | if (Context.Complain) { |
| 1223 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1224 | << Context.C2.getTypeDeclType(D2); |
| 1225 | Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator) |
| 1226 | << EC1->getDeclName() |
| 1227 | << EC1->getInitVal().toString(10); |
| 1228 | Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator); |
| 1229 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1230 | return false; |
| 1231 | } |
| 1232 | |
| 1233 | llvm::APSInt Val1 = EC1->getInitVal(); |
| 1234 | llvm::APSInt Val2 = EC2->getInitVal(); |
Eric Christopher | 6dcc376 | 2012-07-15 00:23:57 +0000 | [diff] [blame] | 1235 | if (!llvm::APSInt::isSameValue(Val1, Val2) || |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1236 | !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1237 | if (Context.Complain) { |
| 1238 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1239 | << Context.C2.getTypeDeclType(D2); |
| 1240 | Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator) |
| 1241 | << EC2->getDeclName() |
| 1242 | << EC2->getInitVal().toString(10); |
| 1243 | Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator) |
| 1244 | << EC1->getDeclName() |
| 1245 | << EC1->getInitVal().toString(10); |
| 1246 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1247 | return false; |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | if (EC2 != EC2End) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1252 | if (Context.Complain) { |
| 1253 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1254 | << Context.C2.getTypeDeclType(D2); |
| 1255 | Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator) |
| 1256 | << EC2->getDeclName() |
| 1257 | << EC2->getInitVal().toString(10); |
| 1258 | Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator); |
| 1259 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1260 | return false; |
| 1261 | } |
| 1262 | |
| 1263 | return true; |
| 1264 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1265 | |
| 1266 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1267 | TemplateParameterList *Params1, |
| 1268 | TemplateParameterList *Params2) { |
| 1269 | if (Params1->size() != Params2->size()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1270 | if (Context.Complain) { |
| 1271 | Context.Diag2(Params2->getTemplateLoc(), |
| 1272 | diag::err_odr_different_num_template_parameters) |
| 1273 | << Params1->size() << Params2->size(); |
| 1274 | Context.Diag1(Params1->getTemplateLoc(), |
| 1275 | diag::note_odr_template_parameter_list); |
| 1276 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1277 | return false; |
| 1278 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1279 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1280 | for (unsigned I = 0, N = Params1->size(); I != N; ++I) { |
| 1281 | if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1282 | if (Context.Complain) { |
| 1283 | Context.Diag2(Params2->getParam(I)->getLocation(), |
| 1284 | diag::err_odr_different_template_parameter_kind); |
| 1285 | Context.Diag1(Params1->getParam(I)->getLocation(), |
| 1286 | diag::note_odr_template_parameter_here); |
| 1287 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1288 | return false; |
| 1289 | } |
| 1290 | |
| 1291 | if (!Context.IsStructurallyEquivalent(Params1->getParam(I), |
| 1292 | Params2->getParam(I))) { |
| 1293 | |
| 1294 | return false; |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | return true; |
| 1299 | } |
| 1300 | |
| 1301 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1302 | TemplateTypeParmDecl *D1, |
| 1303 | TemplateTypeParmDecl *D2) { |
| 1304 | if (D1->isParameterPack() != D2->isParameterPack()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1305 | if (Context.Complain) { |
| 1306 | Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack) |
| 1307 | << D2->isParameterPack(); |
| 1308 | Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) |
| 1309 | << D1->isParameterPack(); |
| 1310 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1311 | return false; |
| 1312 | } |
| 1313 | |
| 1314 | return true; |
| 1315 | } |
| 1316 | |
| 1317 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1318 | NonTypeTemplateParmDecl *D1, |
| 1319 | NonTypeTemplateParmDecl *D2) { |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1320 | if (D1->isParameterPack() != D2->isParameterPack()) { |
Douglas Gregor | 3c7380b | 2012-10-26 15:36:15 +0000 | [diff] [blame] | 1321 | if (Context.Complain) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1322 | Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack) |
| 1323 | << D2->isParameterPack(); |
| 1324 | Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) |
| 1325 | << D1->isParameterPack(); |
| 1326 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1327 | return false; |
| 1328 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1329 | |
| 1330 | // Check types. |
| 1331 | if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1332 | if (Context.Complain) { |
| 1333 | Context.Diag2(D2->getLocation(), |
| 1334 | diag::err_odr_non_type_parameter_type_inconsistent) |
| 1335 | << D2->getType() << D1->getType(); |
| 1336 | Context.Diag1(D1->getLocation(), diag::note_odr_value_here) |
| 1337 | << D1->getType(); |
| 1338 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1339 | return false; |
| 1340 | } |
| 1341 | |
| 1342 | return true; |
| 1343 | } |
| 1344 | |
| 1345 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1346 | TemplateTemplateParmDecl *D1, |
| 1347 | TemplateTemplateParmDecl *D2) { |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1348 | if (D1->isParameterPack() != D2->isParameterPack()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1349 | if (Context.Complain) { |
| 1350 | Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack) |
| 1351 | << D2->isParameterPack(); |
| 1352 | Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) |
| 1353 | << D1->isParameterPack(); |
| 1354 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1355 | return false; |
| 1356 | } |
Douglas Gregor | 3c7380b | 2012-10-26 15:36:15 +0000 | [diff] [blame] | 1357 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1358 | // Check template parameter lists. |
| 1359 | return IsStructurallyEquivalent(Context, D1->getTemplateParameters(), |
| 1360 | D2->getTemplateParameters()); |
| 1361 | } |
| 1362 | |
| 1363 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1364 | ClassTemplateDecl *D1, |
| 1365 | ClassTemplateDecl *D2) { |
| 1366 | // Check template parameters. |
| 1367 | if (!IsStructurallyEquivalent(Context, |
| 1368 | D1->getTemplateParameters(), |
| 1369 | D2->getTemplateParameters())) |
| 1370 | return false; |
| 1371 | |
| 1372 | // Check the templated declaration. |
| 1373 | return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(), |
| 1374 | D2->getTemplatedDecl()); |
| 1375 | } |
| 1376 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1377 | /// \brief Determine structural equivalence of two declarations. |
| 1378 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1379 | Decl *D1, Decl *D2) { |
| 1380 | // FIXME: Check for known structural equivalences via a callback of some sort. |
| 1381 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1382 | // Check whether we already know that these two declarations are not |
| 1383 | // structurally equivalent. |
| 1384 | if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(), |
| 1385 | D2->getCanonicalDecl()))) |
| 1386 | return false; |
| 1387 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1388 | // Determine whether we've already produced a tentative equivalence for D1. |
| 1389 | Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()]; |
| 1390 | if (EquivToD1) |
| 1391 | return EquivToD1 == D2->getCanonicalDecl(); |
| 1392 | |
| 1393 | // Produce a tentative equivalence D1 <-> D2, which will be checked later. |
| 1394 | EquivToD1 = D2->getCanonicalDecl(); |
| 1395 | Context.DeclsToCheck.push_back(D1->getCanonicalDecl()); |
| 1396 | return true; |
| 1397 | } |
| 1398 | |
| 1399 | bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1, |
| 1400 | Decl *D2) { |
| 1401 | if (!::IsStructurallyEquivalent(*this, D1, D2)) |
| 1402 | return false; |
| 1403 | |
| 1404 | return !Finish(); |
| 1405 | } |
| 1406 | |
| 1407 | bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1, |
| 1408 | QualType T2) { |
| 1409 | if (!::IsStructurallyEquivalent(*this, T1, T2)) |
| 1410 | return false; |
| 1411 | |
| 1412 | return !Finish(); |
| 1413 | } |
| 1414 | |
| 1415 | bool StructuralEquivalenceContext::Finish() { |
| 1416 | while (!DeclsToCheck.empty()) { |
| 1417 | // Check the next declaration. |
| 1418 | Decl *D1 = DeclsToCheck.front(); |
| 1419 | DeclsToCheck.pop_front(); |
| 1420 | |
| 1421 | Decl *D2 = TentativeEquivalences[D1]; |
| 1422 | assert(D2 && "Unrecorded tentative equivalence?"); |
| 1423 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1424 | bool Equivalent = true; |
| 1425 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1426 | // FIXME: Switch on all declaration kinds. For now, we're just going to |
| 1427 | // check the obvious ones. |
| 1428 | if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) { |
| 1429 | if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) { |
| 1430 | // Check for equivalent structure names. |
| 1431 | IdentifierInfo *Name1 = Record1->getIdentifier(); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1432 | if (!Name1 && Record1->getTypedefNameForAnonDecl()) |
| 1433 | Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier(); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1434 | IdentifierInfo *Name2 = Record2->getIdentifier(); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1435 | if (!Name2 && Record2->getTypedefNameForAnonDecl()) |
| 1436 | Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier(); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1437 | if (!::IsStructurallyEquivalent(Name1, Name2) || |
| 1438 | !::IsStructurallyEquivalent(*this, Record1, Record2)) |
| 1439 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1440 | } else { |
| 1441 | // Record/non-record mismatch. |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1442 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1443 | } |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1444 | } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1445 | if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) { |
| 1446 | // Check for equivalent enum names. |
| 1447 | IdentifierInfo *Name1 = Enum1->getIdentifier(); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1448 | if (!Name1 && Enum1->getTypedefNameForAnonDecl()) |
| 1449 | Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier(); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1450 | IdentifierInfo *Name2 = Enum2->getIdentifier(); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1451 | if (!Name2 && Enum2->getTypedefNameForAnonDecl()) |
| 1452 | Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier(); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1453 | if (!::IsStructurallyEquivalent(Name1, Name2) || |
| 1454 | !::IsStructurallyEquivalent(*this, Enum1, Enum2)) |
| 1455 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1456 | } else { |
| 1457 | // Enum/non-enum mismatch |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1458 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1459 | } |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1460 | } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) { |
| 1461 | if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1462 | if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(), |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1463 | Typedef2->getIdentifier()) || |
| 1464 | !::IsStructurallyEquivalent(*this, |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1465 | Typedef1->getUnderlyingType(), |
| 1466 | Typedef2->getUnderlyingType())) |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1467 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1468 | } else { |
| 1469 | // Typedef/non-typedef mismatch. |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1470 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1471 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1472 | } else if (ClassTemplateDecl *ClassTemplate1 |
| 1473 | = dyn_cast<ClassTemplateDecl>(D1)) { |
| 1474 | if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) { |
| 1475 | if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(), |
| 1476 | ClassTemplate2->getIdentifier()) || |
| 1477 | !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2)) |
| 1478 | Equivalent = false; |
| 1479 | } else { |
| 1480 | // Class template/non-class-template mismatch. |
| 1481 | Equivalent = false; |
| 1482 | } |
| 1483 | } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) { |
| 1484 | if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) { |
| 1485 | if (!::IsStructurallyEquivalent(*this, TTP1, TTP2)) |
| 1486 | Equivalent = false; |
| 1487 | } else { |
| 1488 | // Kind mismatch. |
| 1489 | Equivalent = false; |
| 1490 | } |
| 1491 | } else if (NonTypeTemplateParmDecl *NTTP1 |
| 1492 | = dyn_cast<NonTypeTemplateParmDecl>(D1)) { |
| 1493 | if (NonTypeTemplateParmDecl *NTTP2 |
| 1494 | = dyn_cast<NonTypeTemplateParmDecl>(D2)) { |
| 1495 | if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2)) |
| 1496 | Equivalent = false; |
| 1497 | } else { |
| 1498 | // Kind mismatch. |
| 1499 | Equivalent = false; |
| 1500 | } |
| 1501 | } else if (TemplateTemplateParmDecl *TTP1 |
| 1502 | = dyn_cast<TemplateTemplateParmDecl>(D1)) { |
| 1503 | if (TemplateTemplateParmDecl *TTP2 |
| 1504 | = dyn_cast<TemplateTemplateParmDecl>(D2)) { |
| 1505 | if (!::IsStructurallyEquivalent(*this, TTP1, TTP2)) |
| 1506 | Equivalent = false; |
| 1507 | } else { |
| 1508 | // Kind mismatch. |
| 1509 | Equivalent = false; |
| 1510 | } |
| 1511 | } |
| 1512 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1513 | if (!Equivalent) { |
| 1514 | // Note that these two declarations are not equivalent (and we already |
| 1515 | // know about it). |
| 1516 | NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(), |
| 1517 | D2->getCanonicalDecl())); |
| 1518 | return true; |
| 1519 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1520 | // FIXME: Check other declaration kinds! |
| 1521 | } |
| 1522 | |
| 1523 | return false; |
| 1524 | } |
| 1525 | |
| 1526 | //---------------------------------------------------------------------------- |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1527 | // Import Types |
| 1528 | //---------------------------------------------------------------------------- |
| 1529 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1530 | QualType ASTNodeImporter::VisitType(const Type *T) { |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 1531 | Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node) |
| 1532 | << T->getTypeClassName(); |
| 1533 | return QualType(); |
| 1534 | } |
| 1535 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1536 | QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1537 | switch (T->getKind()) { |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 1538 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 1539 | case BuiltinType::Id: \ |
| 1540 | return Importer.getToContext().SingletonId; |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 1541 | #include "clang/Basic/OpenCLImageTypes.def" |
John McCall | e314e27 | 2011-10-18 21:02:43 +0000 | [diff] [blame] | 1542 | #define SHARED_SINGLETON_TYPE(Expansion) |
| 1543 | #define BUILTIN_TYPE(Id, SingletonId) \ |
| 1544 | case BuiltinType::Id: return Importer.getToContext().SingletonId; |
| 1545 | #include "clang/AST/BuiltinTypes.def" |
| 1546 | |
| 1547 | // FIXME: for Char16, Char32, and NullPtr, make sure that the "to" |
| 1548 | // context supports C++. |
| 1549 | |
| 1550 | // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to" |
| 1551 | // context supports ObjC. |
| 1552 | |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1553 | case BuiltinType::Char_U: |
| 1554 | // The context we're importing from has an unsigned 'char'. If we're |
| 1555 | // importing into a context with a signed 'char', translate to |
| 1556 | // 'unsigned char' instead. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1557 | if (Importer.getToContext().getLangOpts().CharIsSigned) |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1558 | return Importer.getToContext().UnsignedCharTy; |
| 1559 | |
| 1560 | return Importer.getToContext().CharTy; |
| 1561 | |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1562 | case BuiltinType::Char_S: |
| 1563 | // The context we're importing from has an unsigned 'char'. If we're |
| 1564 | // importing into a context with a signed 'char', translate to |
| 1565 | // 'unsigned char' instead. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1566 | if (!Importer.getToContext().getLangOpts().CharIsSigned) |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1567 | return Importer.getToContext().SignedCharTy; |
| 1568 | |
| 1569 | return Importer.getToContext().CharTy; |
| 1570 | |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 1571 | case BuiltinType::WChar_S: |
| 1572 | case BuiltinType::WChar_U: |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1573 | // FIXME: If not in C++, shall we translate to the C equivalent of |
| 1574 | // wchar_t? |
| 1575 | return Importer.getToContext().WCharTy; |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1576 | } |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1577 | |
| 1578 | llvm_unreachable("Invalid BuiltinType Kind!"); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1581 | QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1582 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1583 | if (ToElementType.isNull()) |
| 1584 | return QualType(); |
| 1585 | |
| 1586 | return Importer.getToContext().getComplexType(ToElementType); |
| 1587 | } |
| 1588 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1589 | QualType ASTNodeImporter::VisitPointerType(const PointerType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1590 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 1591 | if (ToPointeeType.isNull()) |
| 1592 | return QualType(); |
| 1593 | |
| 1594 | return Importer.getToContext().getPointerType(ToPointeeType); |
| 1595 | } |
| 1596 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1597 | QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1598 | // FIXME: Check for blocks support in "to" context. |
| 1599 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 1600 | if (ToPointeeType.isNull()) |
| 1601 | return QualType(); |
| 1602 | |
| 1603 | return Importer.getToContext().getBlockPointerType(ToPointeeType); |
| 1604 | } |
| 1605 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1606 | QualType |
| 1607 | ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1608 | // FIXME: Check for C++ support in "to" context. |
| 1609 | QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); |
| 1610 | if (ToPointeeType.isNull()) |
| 1611 | return QualType(); |
| 1612 | |
| 1613 | return Importer.getToContext().getLValueReferenceType(ToPointeeType); |
| 1614 | } |
| 1615 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1616 | QualType |
| 1617 | ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1618 | // FIXME: Check for C++0x support in "to" context. |
| 1619 | QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); |
| 1620 | if (ToPointeeType.isNull()) |
| 1621 | return QualType(); |
| 1622 | |
| 1623 | return Importer.getToContext().getRValueReferenceType(ToPointeeType); |
| 1624 | } |
| 1625 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1626 | QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1627 | // FIXME: Check for C++ support in "to" context. |
| 1628 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 1629 | if (ToPointeeType.isNull()) |
| 1630 | return QualType(); |
| 1631 | |
| 1632 | QualType ClassType = Importer.Import(QualType(T->getClass(), 0)); |
| 1633 | return Importer.getToContext().getMemberPointerType(ToPointeeType, |
| 1634 | ClassType.getTypePtr()); |
| 1635 | } |
| 1636 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1637 | QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1638 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1639 | if (ToElementType.isNull()) |
| 1640 | return QualType(); |
| 1641 | |
| 1642 | return Importer.getToContext().getConstantArrayType(ToElementType, |
| 1643 | T->getSize(), |
| 1644 | T->getSizeModifier(), |
| 1645 | T->getIndexTypeCVRQualifiers()); |
| 1646 | } |
| 1647 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1648 | QualType |
| 1649 | ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1650 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1651 | if (ToElementType.isNull()) |
| 1652 | return QualType(); |
| 1653 | |
| 1654 | return Importer.getToContext().getIncompleteArrayType(ToElementType, |
| 1655 | T->getSizeModifier(), |
| 1656 | T->getIndexTypeCVRQualifiers()); |
| 1657 | } |
| 1658 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1659 | QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1660 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1661 | if (ToElementType.isNull()) |
| 1662 | return QualType(); |
| 1663 | |
| 1664 | Expr *Size = Importer.Import(T->getSizeExpr()); |
| 1665 | if (!Size) |
| 1666 | return QualType(); |
| 1667 | |
| 1668 | SourceRange Brackets = Importer.Import(T->getBracketsRange()); |
| 1669 | return Importer.getToContext().getVariableArrayType(ToElementType, Size, |
| 1670 | T->getSizeModifier(), |
| 1671 | T->getIndexTypeCVRQualifiers(), |
| 1672 | Brackets); |
| 1673 | } |
| 1674 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1675 | QualType ASTNodeImporter::VisitVectorType(const VectorType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1676 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1677 | if (ToElementType.isNull()) |
| 1678 | return QualType(); |
| 1679 | |
| 1680 | return Importer.getToContext().getVectorType(ToElementType, |
| 1681 | T->getNumElements(), |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1682 | T->getVectorKind()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1685 | QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1686 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1687 | if (ToElementType.isNull()) |
| 1688 | return QualType(); |
| 1689 | |
| 1690 | return Importer.getToContext().getExtVectorType(ToElementType, |
| 1691 | T->getNumElements()); |
| 1692 | } |
| 1693 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1694 | QualType |
| 1695 | ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1696 | // FIXME: What happens if we're importing a function without a prototype |
| 1697 | // into C++? Should we make it variadic? |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1698 | QualType ToResultType = Importer.Import(T->getReturnType()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1699 | if (ToResultType.isNull()) |
| 1700 | return QualType(); |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1701 | |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1702 | return Importer.getToContext().getFunctionNoProtoType(ToResultType, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1703 | T->getExtInfo()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 1706 | QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1707 | QualType ToResultType = Importer.Import(T->getReturnType()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1708 | if (ToResultType.isNull()) |
| 1709 | return QualType(); |
| 1710 | |
| 1711 | // Import argument types |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1712 | SmallVector<QualType, 4> ArgTypes; |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 1713 | for (const auto &A : T->param_types()) { |
| 1714 | QualType ArgType = Importer.Import(A); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1715 | if (ArgType.isNull()) |
| 1716 | return QualType(); |
| 1717 | ArgTypes.push_back(ArgType); |
| 1718 | } |
| 1719 | |
| 1720 | // Import exception types |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1721 | SmallVector<QualType, 4> ExceptionTypes; |
Aaron Ballman | b088fbe | 2014-03-17 15:38:09 +0000 | [diff] [blame] | 1722 | for (const auto &E : T->exceptions()) { |
| 1723 | QualType ExceptionType = Importer.Import(E); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1724 | if (ExceptionType.isNull()) |
| 1725 | return QualType(); |
| 1726 | ExceptionTypes.push_back(ExceptionType); |
| 1727 | } |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1728 | |
Argyrios Kyrtzidis | b41791d | 2012-09-22 01:58:06 +0000 | [diff] [blame] | 1729 | FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo(); |
| 1730 | FunctionProtoType::ExtProtoInfo ToEPI; |
| 1731 | |
| 1732 | ToEPI.ExtInfo = FromEPI.ExtInfo; |
| 1733 | ToEPI.Variadic = FromEPI.Variadic; |
| 1734 | ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn; |
| 1735 | ToEPI.TypeQuals = FromEPI.TypeQuals; |
| 1736 | ToEPI.RefQualifier = FromEPI.RefQualifier; |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 1737 | ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type; |
| 1738 | ToEPI.ExceptionSpec.Exceptions = ExceptionTypes; |
| 1739 | ToEPI.ExceptionSpec.NoexceptExpr = |
| 1740 | Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr); |
| 1741 | ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>( |
| 1742 | Importer.Import(FromEPI.ExceptionSpec.SourceDecl)); |
| 1743 | ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>( |
| 1744 | Importer.Import(FromEPI.ExceptionSpec.SourceTemplate)); |
Argyrios Kyrtzidis | b41791d | 2012-09-22 01:58:06 +0000 | [diff] [blame] | 1745 | |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 1746 | return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI); |
Argyrios Kyrtzidis | b41791d | 2012-09-22 01:58:06 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
Sean Callanan | da6df8a | 2011-08-11 16:56:07 +0000 | [diff] [blame] | 1749 | QualType ASTNodeImporter::VisitParenType(const ParenType *T) { |
| 1750 | QualType ToInnerType = Importer.Import(T->getInnerType()); |
| 1751 | if (ToInnerType.isNull()) |
| 1752 | return QualType(); |
| 1753 | |
| 1754 | return Importer.getToContext().getParenType(ToInnerType); |
| 1755 | } |
| 1756 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1757 | QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1758 | TypedefNameDecl *ToDecl |
| 1759 | = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl())); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1760 | if (!ToDecl) |
| 1761 | return QualType(); |
| 1762 | |
| 1763 | return Importer.getToContext().getTypeDeclType(ToDecl); |
| 1764 | } |
| 1765 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1766 | QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1767 | Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); |
| 1768 | if (!ToExpr) |
| 1769 | return QualType(); |
| 1770 | |
| 1771 | return Importer.getToContext().getTypeOfExprType(ToExpr); |
| 1772 | } |
| 1773 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1774 | QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1775 | QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); |
| 1776 | if (ToUnderlyingType.isNull()) |
| 1777 | return QualType(); |
| 1778 | |
| 1779 | return Importer.getToContext().getTypeOfType(ToUnderlyingType); |
| 1780 | } |
| 1781 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1782 | QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) { |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1783 | // FIXME: Make sure that the "to" context supports C++0x! |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1784 | Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); |
| 1785 | if (!ToExpr) |
| 1786 | return QualType(); |
| 1787 | |
Douglas Gregor | 81495f3 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 1788 | QualType UnderlyingType = Importer.Import(T->getUnderlyingType()); |
| 1789 | if (UnderlyingType.isNull()) |
| 1790 | return QualType(); |
| 1791 | |
| 1792 | return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1795 | QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) { |
| 1796 | QualType ToBaseType = Importer.Import(T->getBaseType()); |
| 1797 | QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); |
| 1798 | if (ToBaseType.isNull() || ToUnderlyingType.isNull()) |
| 1799 | return QualType(); |
| 1800 | |
| 1801 | return Importer.getToContext().getUnaryTransformType(ToBaseType, |
| 1802 | ToUnderlyingType, |
| 1803 | T->getUTTKind()); |
| 1804 | } |
| 1805 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1806 | QualType ASTNodeImporter::VisitAutoType(const AutoType *T) { |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 1807 | // FIXME: Make sure that the "to" context supports C++11! |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1808 | QualType FromDeduced = T->getDeducedType(); |
| 1809 | QualType ToDeduced; |
| 1810 | if (!FromDeduced.isNull()) { |
| 1811 | ToDeduced = Importer.Import(FromDeduced); |
| 1812 | if (ToDeduced.isNull()) |
| 1813 | return QualType(); |
| 1814 | } |
| 1815 | |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 1816 | return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(), |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 1817 | /*IsDependent*/false); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1818 | } |
| 1819 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 1820 | QualType ASTNodeImporter::VisitInjectedClassNameType( |
| 1821 | const InjectedClassNameType *T) { |
| 1822 | CXXRecordDecl *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl())); |
| 1823 | if (!D) |
| 1824 | return QualType(); |
| 1825 | |
| 1826 | QualType InjType = Importer.Import(T->getInjectedSpecializationType()); |
| 1827 | if (InjType.isNull()) |
| 1828 | return QualType(); |
| 1829 | |
| 1830 | // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading |
| 1831 | // See comments in InjectedClassNameType definition for details |
| 1832 | // return Importer.getToContext().getInjectedClassNameType(D, InjType); |
| 1833 | enum { |
| 1834 | TypeAlignmentInBits = 4, |
| 1835 | TypeAlignment = 1 << TypeAlignmentInBits |
| 1836 | }; |
| 1837 | |
| 1838 | return QualType(new (Importer.getToContext(), TypeAlignment) |
| 1839 | InjectedClassNameType(D, InjType), 0); |
| 1840 | } |
| 1841 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1842 | QualType ASTNodeImporter::VisitRecordType(const RecordType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1843 | RecordDecl *ToDecl |
| 1844 | = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl())); |
| 1845 | if (!ToDecl) |
| 1846 | return QualType(); |
| 1847 | |
| 1848 | return Importer.getToContext().getTagDeclType(ToDecl); |
| 1849 | } |
| 1850 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1851 | QualType ASTNodeImporter::VisitEnumType(const EnumType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1852 | EnumDecl *ToDecl |
| 1853 | = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl())); |
| 1854 | if (!ToDecl) |
| 1855 | return QualType(); |
| 1856 | |
| 1857 | return Importer.getToContext().getTagDeclType(ToDecl); |
| 1858 | } |
| 1859 | |
Sean Callanan | 72fe085 | 2015-04-02 23:50:08 +0000 | [diff] [blame] | 1860 | QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) { |
| 1861 | QualType FromModifiedType = T->getModifiedType(); |
| 1862 | QualType FromEquivalentType = T->getEquivalentType(); |
| 1863 | QualType ToModifiedType; |
| 1864 | QualType ToEquivalentType; |
| 1865 | |
| 1866 | if (!FromModifiedType.isNull()) { |
| 1867 | ToModifiedType = Importer.Import(FromModifiedType); |
| 1868 | if (ToModifiedType.isNull()) |
| 1869 | return QualType(); |
| 1870 | } |
| 1871 | if (!FromEquivalentType.isNull()) { |
| 1872 | ToEquivalentType = Importer.Import(FromEquivalentType); |
| 1873 | if (ToEquivalentType.isNull()) |
| 1874 | return QualType(); |
| 1875 | } |
| 1876 | |
| 1877 | return Importer.getToContext().getAttributedType(T->getAttrKind(), |
| 1878 | ToModifiedType, ToEquivalentType); |
| 1879 | } |
| 1880 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 1881 | |
| 1882 | QualType ASTNodeImporter::VisitTemplateTypeParmType( |
| 1883 | const TemplateTypeParmType *T) { |
| 1884 | TemplateTypeParmDecl *ParmDecl = |
| 1885 | cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl())); |
| 1886 | if (!ParmDecl && T->getDecl()) |
| 1887 | return QualType(); |
| 1888 | |
| 1889 | return Importer.getToContext().getTemplateTypeParmType( |
| 1890 | T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl); |
| 1891 | } |
| 1892 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1893 | QualType ASTNodeImporter::VisitTemplateSpecializationType( |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1894 | const TemplateSpecializationType *T) { |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1895 | TemplateName ToTemplate = Importer.Import(T->getTemplateName()); |
| 1896 | if (ToTemplate.isNull()) |
| 1897 | return QualType(); |
| 1898 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1899 | SmallVector<TemplateArgument, 2> ToTemplateArgs; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1900 | if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs)) |
| 1901 | return QualType(); |
| 1902 | |
| 1903 | QualType ToCanonType; |
| 1904 | if (!QualType(T, 0).isCanonical()) { |
| 1905 | QualType FromCanonType |
| 1906 | = Importer.getFromContext().getCanonicalType(QualType(T, 0)); |
| 1907 | ToCanonType =Importer.Import(FromCanonType); |
| 1908 | if (ToCanonType.isNull()) |
| 1909 | return QualType(); |
| 1910 | } |
| 1911 | return Importer.getToContext().getTemplateSpecializationType(ToTemplate, |
| 1912 | ToTemplateArgs.data(), |
| 1913 | ToTemplateArgs.size(), |
| 1914 | ToCanonType); |
| 1915 | } |
| 1916 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1917 | QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1918 | NestedNameSpecifier *ToQualifier = nullptr; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1919 | // Note: the qualifier in an ElaboratedType is optional. |
| 1920 | if (T->getQualifier()) { |
| 1921 | ToQualifier = Importer.Import(T->getQualifier()); |
| 1922 | if (!ToQualifier) |
| 1923 | return QualType(); |
| 1924 | } |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1925 | |
| 1926 | QualType ToNamedType = Importer.Import(T->getNamedType()); |
| 1927 | if (ToNamedType.isNull()) |
| 1928 | return QualType(); |
| 1929 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1930 | return Importer.getToContext().getElaboratedType(T->getKeyword(), |
| 1931 | ToQualifier, ToNamedType); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1934 | QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1935 | ObjCInterfaceDecl *Class |
| 1936 | = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl())); |
| 1937 | if (!Class) |
| 1938 | return QualType(); |
| 1939 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1940 | return Importer.getToContext().getObjCInterfaceType(Class); |
| 1941 | } |
| 1942 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1943 | QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1944 | QualType ToBaseType = Importer.Import(T->getBaseType()); |
| 1945 | if (ToBaseType.isNull()) |
| 1946 | return QualType(); |
| 1947 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1948 | SmallVector<QualType, 4> TypeArgs; |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 1949 | for (auto TypeArg : T->getTypeArgsAsWritten()) { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1950 | QualType ImportedTypeArg = Importer.Import(TypeArg); |
| 1951 | if (ImportedTypeArg.isNull()) |
| 1952 | return QualType(); |
| 1953 | |
| 1954 | TypeArgs.push_back(ImportedTypeArg); |
| 1955 | } |
| 1956 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1957 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
Aaron Ballman | 1683f7b | 2014-03-17 15:55:30 +0000 | [diff] [blame] | 1958 | for (auto *P : T->quals()) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1959 | ObjCProtocolDecl *Protocol |
Aaron Ballman | 1683f7b | 2014-03-17 15:55:30 +0000 | [diff] [blame] | 1960 | = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P)); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1961 | if (!Protocol) |
| 1962 | return QualType(); |
| 1963 | Protocols.push_back(Protocol); |
| 1964 | } |
| 1965 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1966 | return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs, |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 1967 | Protocols, |
| 1968 | T->isKindOfTypeAsWritten()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1969 | } |
| 1970 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1971 | QualType |
| 1972 | ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1973 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 1974 | if (ToPointeeType.isNull()) |
| 1975 | return QualType(); |
| 1976 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1977 | return Importer.getToContext().getObjCObjectPointerType(ToPointeeType); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 1980 | //---------------------------------------------------------------------------- |
| 1981 | // Import Declarations |
| 1982 | //---------------------------------------------------------------------------- |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 1983 | bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC, |
| 1984 | DeclContext *&LexicalDC, |
| 1985 | DeclarationName &Name, |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 1986 | NamedDecl *&ToD, |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 1987 | SourceLocation &Loc) { |
| 1988 | // Import the context of this declaration. |
| 1989 | DC = Importer.ImportContext(D->getDeclContext()); |
| 1990 | if (!DC) |
| 1991 | return true; |
| 1992 | |
| 1993 | LexicalDC = DC; |
| 1994 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 1995 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 1996 | if (!LexicalDC) |
| 1997 | return true; |
| 1998 | } |
| 1999 | |
| 2000 | // Import the name of this declaration. |
| 2001 | Name = Importer.Import(D->getDeclName()); |
| 2002 | if (D->getDeclName() && !Name) |
| 2003 | return true; |
| 2004 | |
| 2005 | // Import the location of this declaration. |
| 2006 | Loc = Importer.Import(D->getLocation()); |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2007 | ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D)); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2008 | return false; |
| 2009 | } |
| 2010 | |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2011 | void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) { |
| 2012 | if (!FromD) |
| 2013 | return; |
| 2014 | |
| 2015 | if (!ToD) { |
| 2016 | ToD = Importer.Import(FromD); |
| 2017 | if (!ToD) |
| 2018 | return; |
| 2019 | } |
| 2020 | |
| 2021 | if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) { |
| 2022 | if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) { |
Sean Callanan | 19dfc93 | 2013-01-11 23:17:47 +0000 | [diff] [blame] | 2023 | if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) { |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2024 | ImportDefinition(FromRecord, ToRecord); |
| 2025 | } |
| 2026 | } |
| 2027 | return; |
| 2028 | } |
| 2029 | |
| 2030 | if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) { |
| 2031 | if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) { |
| 2032 | if (FromEnum->getDefinition() && !ToEnum->getDefinition()) { |
| 2033 | ImportDefinition(FromEnum, ToEnum); |
| 2034 | } |
| 2035 | } |
| 2036 | return; |
| 2037 | } |
| 2038 | } |
| 2039 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2040 | void |
| 2041 | ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From, |
| 2042 | DeclarationNameInfo& To) { |
| 2043 | // NOTE: To.Name and To.Loc are already imported. |
| 2044 | // We only have to import To.LocInfo. |
| 2045 | switch (To.getName().getNameKind()) { |
| 2046 | case DeclarationName::Identifier: |
| 2047 | case DeclarationName::ObjCZeroArgSelector: |
| 2048 | case DeclarationName::ObjCOneArgSelector: |
| 2049 | case DeclarationName::ObjCMultiArgSelector: |
| 2050 | case DeclarationName::CXXUsingDirective: |
| 2051 | return; |
| 2052 | |
| 2053 | case DeclarationName::CXXOperatorName: { |
| 2054 | SourceRange Range = From.getCXXOperatorNameRange(); |
| 2055 | To.setCXXOperatorNameRange(Importer.Import(Range)); |
| 2056 | return; |
| 2057 | } |
| 2058 | case DeclarationName::CXXLiteralOperatorName: { |
| 2059 | SourceLocation Loc = From.getCXXLiteralOperatorNameLoc(); |
| 2060 | To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc)); |
| 2061 | return; |
| 2062 | } |
| 2063 | case DeclarationName::CXXConstructorName: |
| 2064 | case DeclarationName::CXXDestructorName: |
| 2065 | case DeclarationName::CXXConversionFunctionName: { |
| 2066 | TypeSourceInfo *FromTInfo = From.getNamedTypeInfo(); |
| 2067 | To.setNamedTypeInfo(Importer.Import(FromTInfo)); |
| 2068 | return; |
| 2069 | } |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2070 | } |
Douglas Gregor | 07216d1 | 2011-11-02 20:52:01 +0000 | [diff] [blame] | 2071 | llvm_unreachable("Unknown name kind."); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 2074 | void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) { |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 2075 | if (Importer.isMinimalImport() && !ForceImport) { |
Sean Callanan | 81d577c | 2011-07-22 23:46:03 +0000 | [diff] [blame] | 2076 | Importer.ImportContext(FromDC); |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 2077 | return; |
| 2078 | } |
| 2079 | |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 2080 | for (auto *From : FromDC->decls()) |
| 2081 | Importer.Import(From); |
Douglas Gregor | 968d633 | 2010-02-21 18:24:45 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2084 | bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To, |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 2085 | ImportDefinitionKind Kind) { |
| 2086 | if (To->getDefinition() || To->isBeingDefined()) { |
| 2087 | if (Kind == IDK_Everything) |
| 2088 | ImportDeclContext(From, /*ForceImport=*/true); |
| 2089 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2090 | return false; |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 2091 | } |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2092 | |
| 2093 | To->startDefinition(); |
| 2094 | |
| 2095 | // Add base classes. |
| 2096 | if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) { |
| 2097 | CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From); |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 2098 | |
| 2099 | struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data(); |
| 2100 | struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data(); |
| 2101 | ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor; |
Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 2102 | ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers; |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 2103 | ToData.Aggregate = FromData.Aggregate; |
| 2104 | ToData.PlainOldData = FromData.PlainOldData; |
| 2105 | ToData.Empty = FromData.Empty; |
| 2106 | ToData.Polymorphic = FromData.Polymorphic; |
| 2107 | ToData.Abstract = FromData.Abstract; |
| 2108 | ToData.IsStandardLayout = FromData.IsStandardLayout; |
| 2109 | ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases; |
| 2110 | ToData.HasPrivateFields = FromData.HasPrivateFields; |
| 2111 | ToData.HasProtectedFields = FromData.HasProtectedFields; |
| 2112 | ToData.HasPublicFields = FromData.HasPublicFields; |
| 2113 | ToData.HasMutableFields = FromData.HasMutableFields; |
Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 2114 | ToData.HasVariantMembers = FromData.HasVariantMembers; |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 2115 | ToData.HasOnlyCMembers = FromData.HasOnlyCMembers; |
Richard Smith | e2648ba | 2012-05-07 01:07:30 +0000 | [diff] [blame] | 2116 | ToData.HasInClassInitializer = FromData.HasInClassInitializer; |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 2117 | ToData.HasUninitializedReferenceMember |
| 2118 | = FromData.HasUninitializedReferenceMember; |
Nico Weber | 6a6376b | 2016-02-19 01:52:46 +0000 | [diff] [blame] | 2119 | ToData.HasUninitializedFields = FromData.HasUninitializedFields; |
Richard Smith | 12e7931 | 2016-05-13 06:47:56 +0000 | [diff] [blame] | 2120 | ToData.HasInheritedConstructor = FromData.HasInheritedConstructor; |
| 2121 | ToData.HasInheritedAssignment = FromData.HasInheritedAssignment; |
Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 2122 | ToData.NeedOverloadResolutionForMoveConstructor |
| 2123 | = FromData.NeedOverloadResolutionForMoveConstructor; |
| 2124 | ToData.NeedOverloadResolutionForMoveAssignment |
| 2125 | = FromData.NeedOverloadResolutionForMoveAssignment; |
| 2126 | ToData.NeedOverloadResolutionForDestructor |
| 2127 | = FromData.NeedOverloadResolutionForDestructor; |
| 2128 | ToData.DefaultedMoveConstructorIsDeleted |
| 2129 | = FromData.DefaultedMoveConstructorIsDeleted; |
| 2130 | ToData.DefaultedMoveAssignmentIsDeleted |
| 2131 | = FromData.DefaultedMoveAssignmentIsDeleted; |
| 2132 | ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted; |
Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 2133 | ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers; |
| 2134 | ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor; |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 2135 | ToData.HasConstexprNonCopyMoveConstructor |
| 2136 | = FromData.HasConstexprNonCopyMoveConstructor; |
Nico Weber | 72c57f4 | 2016-02-24 20:58:14 +0000 | [diff] [blame] | 2137 | ToData.HasDefaultedDefaultConstructor |
| 2138 | = FromData.HasDefaultedDefaultConstructor; |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 2139 | ToData.DefaultedDefaultConstructorIsConstexpr |
| 2140 | = FromData.DefaultedDefaultConstructorIsConstexpr; |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 2141 | ToData.HasConstexprDefaultConstructor |
| 2142 | = FromData.HasConstexprDefaultConstructor; |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 2143 | ToData.HasNonLiteralTypeFieldsOrBases |
| 2144 | = FromData.HasNonLiteralTypeFieldsOrBases; |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 2145 | // ComputedVisibleConversions not imported. |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 2146 | ToData.UserProvidedDefaultConstructor |
| 2147 | = FromData.UserProvidedDefaultConstructor; |
Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 2148 | ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers; |
Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 2149 | ToData.ImplicitCopyConstructorHasConstParam |
| 2150 | = FromData.ImplicitCopyConstructorHasConstParam; |
| 2151 | ToData.ImplicitCopyAssignmentHasConstParam |
| 2152 | = FromData.ImplicitCopyAssignmentHasConstParam; |
| 2153 | ToData.HasDeclaredCopyConstructorWithConstParam |
| 2154 | = FromData.HasDeclaredCopyConstructorWithConstParam; |
| 2155 | ToData.HasDeclaredCopyAssignmentWithConstParam |
| 2156 | = FromData.HasDeclaredCopyAssignmentWithConstParam; |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 2157 | ToData.IsLambda = FromData.IsLambda; |
| 2158 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2159 | SmallVector<CXXBaseSpecifier *, 4> Bases; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2160 | for (const auto &Base1 : FromCXX->bases()) { |
| 2161 | QualType T = Importer.Import(Base1.getType()); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2162 | if (T.isNull()) |
Douglas Gregor | 96303ea | 2010-12-02 19:33:37 +0000 | [diff] [blame] | 2163 | return true; |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 2164 | |
| 2165 | SourceLocation EllipsisLoc; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2166 | if (Base1.isPackExpansion()) |
| 2167 | EllipsisLoc = Importer.Import(Base1.getEllipsisLoc()); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2168 | |
| 2169 | // Ensure that we have a definition for the base. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2170 | ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl()); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2171 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2172 | Bases.push_back( |
| 2173 | new (Importer.getToContext()) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2174 | CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()), |
| 2175 | Base1.isVirtual(), |
| 2176 | Base1.isBaseOfClass(), |
| 2177 | Base1.getAccessSpecifierAsWritten(), |
| 2178 | Importer.Import(Base1.getTypeSourceInfo()), |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 2179 | EllipsisLoc)); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2180 | } |
| 2181 | if (!Bases.empty()) |
Craig Topper | e6337e1 | 2015-12-25 00:36:02 +0000 | [diff] [blame] | 2182 | ToCXX->setBases(Bases.data(), Bases.size()); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 2185 | if (shouldForceImportDeclContext(Kind)) |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 2186 | ImportDeclContext(From, /*ForceImport=*/true); |
| 2187 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2188 | To->completeDefinition(); |
Douglas Gregor | 96303ea | 2010-12-02 19:33:37 +0000 | [diff] [blame] | 2189 | return false; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2190 | } |
| 2191 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2192 | bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To, |
| 2193 | ImportDefinitionKind Kind) { |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2194 | if (To->getAnyInitializer()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2195 | return false; |
| 2196 | |
| 2197 | // FIXME: Can we really import any initializer? Alternatively, we could force |
| 2198 | // ourselves to import every declaration of a variable and then only use |
| 2199 | // getInit() here. |
| 2200 | To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer()))); |
| 2201 | |
| 2202 | // FIXME: Other bits to merge? |
| 2203 | |
| 2204 | return false; |
| 2205 | } |
| 2206 | |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2207 | bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 2208 | ImportDefinitionKind Kind) { |
| 2209 | if (To->getDefinition() || To->isBeingDefined()) { |
| 2210 | if (Kind == IDK_Everything) |
| 2211 | ImportDeclContext(From, /*ForceImport=*/true); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2212 | return false; |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 2213 | } |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2214 | |
| 2215 | To->startDefinition(); |
| 2216 | |
| 2217 | QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From)); |
| 2218 | if (T.isNull()) |
| 2219 | return true; |
| 2220 | |
| 2221 | QualType ToPromotionType = Importer.Import(From->getPromotionType()); |
| 2222 | if (ToPromotionType.isNull()) |
| 2223 | return true; |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 2224 | |
| 2225 | if (shouldForceImportDeclContext(Kind)) |
| 2226 | ImportDeclContext(From, /*ForceImport=*/true); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2227 | |
| 2228 | // FIXME: we might need to merge the number of positive or negative bits |
| 2229 | // if the enumerator lists don't match. |
| 2230 | To->completeDefinition(T, ToPromotionType, |
| 2231 | From->getNumPositiveBits(), |
| 2232 | From->getNumNegativeBits()); |
| 2233 | return false; |
| 2234 | } |
| 2235 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 2236 | TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList( |
| 2237 | TemplateParameterList *Params) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2238 | SmallVector<NamedDecl *, 4> ToParams; |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 2239 | ToParams.reserve(Params->size()); |
| 2240 | for (TemplateParameterList::iterator P = Params->begin(), |
| 2241 | PEnd = Params->end(); |
| 2242 | P != PEnd; ++P) { |
| 2243 | Decl *To = Importer.Import(*P); |
| 2244 | if (!To) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2245 | return nullptr; |
| 2246 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 2247 | ToParams.push_back(cast<NamedDecl>(To)); |
| 2248 | } |
| 2249 | |
| 2250 | return TemplateParameterList::Create(Importer.getToContext(), |
| 2251 | Importer.Import(Params->getTemplateLoc()), |
| 2252 | Importer.Import(Params->getLAngleLoc()), |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 2253 | ToParams, |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 2254 | Importer.Import(Params->getRAngleLoc())); |
| 2255 | } |
| 2256 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2257 | TemplateArgument |
| 2258 | ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) { |
| 2259 | switch (From.getKind()) { |
| 2260 | case TemplateArgument::Null: |
| 2261 | return TemplateArgument(); |
| 2262 | |
| 2263 | case TemplateArgument::Type: { |
| 2264 | QualType ToType = Importer.Import(From.getAsType()); |
| 2265 | if (ToType.isNull()) |
| 2266 | return TemplateArgument(); |
| 2267 | return TemplateArgument(ToType); |
| 2268 | } |
| 2269 | |
| 2270 | case TemplateArgument::Integral: { |
| 2271 | QualType ToType = Importer.Import(From.getIntegralType()); |
| 2272 | if (ToType.isNull()) |
| 2273 | return TemplateArgument(); |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 2274 | return TemplateArgument(From, ToType); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2275 | } |
| 2276 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2277 | case TemplateArgument::Declaration: { |
David Blaikie | 3c7dd6b | 2014-10-22 19:54:16 +0000 | [diff] [blame] | 2278 | ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl())); |
| 2279 | QualType ToType = Importer.Import(From.getParamTypeForDecl()); |
| 2280 | if (!To || ToType.isNull()) |
| 2281 | return TemplateArgument(); |
| 2282 | return TemplateArgument(To, ToType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2283 | } |
| 2284 | |
| 2285 | case TemplateArgument::NullPtr: { |
| 2286 | QualType ToType = Importer.Import(From.getNullPtrType()); |
| 2287 | if (ToType.isNull()) |
| 2288 | return TemplateArgument(); |
| 2289 | return TemplateArgument(ToType, /*isNullPtr*/true); |
| 2290 | } |
| 2291 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2292 | case TemplateArgument::Template: { |
| 2293 | TemplateName ToTemplate = Importer.Import(From.getAsTemplate()); |
| 2294 | if (ToTemplate.isNull()) |
| 2295 | return TemplateArgument(); |
| 2296 | |
| 2297 | return TemplateArgument(ToTemplate); |
| 2298 | } |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2299 | |
| 2300 | case TemplateArgument::TemplateExpansion: { |
| 2301 | TemplateName ToTemplate |
| 2302 | = Importer.Import(From.getAsTemplateOrTemplatePattern()); |
| 2303 | if (ToTemplate.isNull()) |
| 2304 | return TemplateArgument(); |
| 2305 | |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 2306 | return TemplateArgument(ToTemplate, From.getNumTemplateExpansions()); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2309 | case TemplateArgument::Expression: |
| 2310 | if (Expr *ToExpr = Importer.Import(From.getAsExpr())) |
| 2311 | return TemplateArgument(ToExpr); |
| 2312 | return TemplateArgument(); |
| 2313 | |
| 2314 | case TemplateArgument::Pack: { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2315 | SmallVector<TemplateArgument, 2> ToPack; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2316 | ToPack.reserve(From.pack_size()); |
| 2317 | if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack)) |
| 2318 | return TemplateArgument(); |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 2319 | |
| 2320 | return TemplateArgument( |
| 2321 | llvm::makeArrayRef(ToPack).copy(Importer.getToContext())); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2322 | } |
| 2323 | } |
| 2324 | |
| 2325 | llvm_unreachable("Invalid template argument kind"); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2326 | } |
| 2327 | |
| 2328 | bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs, |
| 2329 | unsigned NumFromArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2330 | SmallVectorImpl<TemplateArgument> &ToArgs) { |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2331 | for (unsigned I = 0; I != NumFromArgs; ++I) { |
| 2332 | TemplateArgument To = ImportTemplateArgument(FromArgs[I]); |
| 2333 | if (To.isNull() && !FromArgs[I].isNull()) |
| 2334 | return true; |
| 2335 | |
| 2336 | ToArgs.push_back(To); |
| 2337 | } |
| 2338 | |
| 2339 | return false; |
| 2340 | } |
| 2341 | |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2342 | bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord, |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2343 | RecordDecl *ToRecord, bool Complain) { |
Sean Callanan | c665c9e | 2013-10-09 21:45:11 +0000 | [diff] [blame] | 2344 | // Eliminate a potential failure point where we attempt to re-import |
| 2345 | // something we're trying to import while completing ToRecord. |
| 2346 | Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord); |
| 2347 | if (ToOrigin) { |
| 2348 | RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin); |
| 2349 | if (ToOriginRecord) |
| 2350 | ToRecord = ToOriginRecord; |
| 2351 | } |
| 2352 | |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 2353 | StructuralEquivalenceContext Ctx(Importer.getFromContext(), |
Sean Callanan | c665c9e | 2013-10-09 21:45:11 +0000 | [diff] [blame] | 2354 | ToRecord->getASTContext(), |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2355 | Importer.getNonEquivalentDecls(), |
| 2356 | false, Complain); |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 2357 | return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2358 | } |
| 2359 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2360 | bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar, |
| 2361 | bool Complain) { |
| 2362 | StructuralEquivalenceContext Ctx( |
| 2363 | Importer.getFromContext(), Importer.getToContext(), |
| 2364 | Importer.getNonEquivalentDecls(), false, Complain); |
| 2365 | return Ctx.IsStructurallyEquivalent(FromVar, ToVar); |
| 2366 | } |
| 2367 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2368 | bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) { |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 2369 | StructuralEquivalenceContext Ctx(Importer.getFromContext(), |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2370 | Importer.getToContext(), |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2371 | Importer.getNonEquivalentDecls()); |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 2372 | return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
Douglas Gregor | 9115508 | 2012-11-14 22:29:20 +0000 | [diff] [blame] | 2375 | bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC, |
| 2376 | EnumConstantDecl *ToEC) |
| 2377 | { |
| 2378 | const llvm::APSInt &FromVal = FromEC->getInitVal(); |
| 2379 | const llvm::APSInt &ToVal = ToEC->getInitVal(); |
| 2380 | |
| 2381 | return FromVal.isSigned() == ToVal.isSigned() && |
| 2382 | FromVal.getBitWidth() == ToVal.getBitWidth() && |
| 2383 | FromVal == ToVal; |
| 2384 | } |
| 2385 | |
| 2386 | bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From, |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 2387 | ClassTemplateDecl *To) { |
| 2388 | StructuralEquivalenceContext Ctx(Importer.getFromContext(), |
| 2389 | Importer.getToContext(), |
| 2390 | Importer.getNonEquivalentDecls()); |
| 2391 | return Ctx.IsStructurallyEquivalent(From, To); |
| 2392 | } |
| 2393 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2394 | bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From, |
| 2395 | VarTemplateDecl *To) { |
| 2396 | StructuralEquivalenceContext Ctx(Importer.getFromContext(), |
| 2397 | Importer.getToContext(), |
| 2398 | Importer.getNonEquivalentDecls()); |
| 2399 | return Ctx.IsStructurallyEquivalent(From, To); |
| 2400 | } |
| 2401 | |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 2402 | Decl *ASTNodeImporter::VisitDecl(Decl *D) { |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 2403 | Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 2404 | << D->getDeclKindName(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2405 | return nullptr; |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 2406 | } |
| 2407 | |
Sean Callanan | 6519827 | 2011-11-17 23:20:56 +0000 | [diff] [blame] | 2408 | Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 2409 | TranslationUnitDecl *ToD = |
| 2410 | Importer.getToContext().getTranslationUnitDecl(); |
| 2411 | |
| 2412 | Importer.Imported(D, ToD); |
| 2413 | |
| 2414 | return ToD; |
| 2415 | } |
| 2416 | |
Argyrios Kyrtzidis | 544ea71 | 2016-02-18 23:08:36 +0000 | [diff] [blame] | 2417 | Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) { |
| 2418 | |
| 2419 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 2420 | SourceLocation ColonLoc = Importer.Import(D->getColonLoc()); |
| 2421 | |
| 2422 | // Import the context of this declaration. |
| 2423 | DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| 2424 | if (!DC) |
| 2425 | return nullptr; |
| 2426 | |
| 2427 | AccessSpecDecl *accessSpecDecl |
| 2428 | = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(), |
| 2429 | DC, Loc, ColonLoc); |
| 2430 | |
| 2431 | if (!accessSpecDecl) |
| 2432 | return nullptr; |
| 2433 | |
| 2434 | // Lexical DeclContext and Semantic DeclContext |
| 2435 | // is always the same for the accessSpec. |
| 2436 | accessSpecDecl->setLexicalDeclContext(DC); |
| 2437 | DC->addDeclInternal(accessSpecDecl); |
| 2438 | |
| 2439 | return accessSpecDecl; |
| 2440 | } |
| 2441 | |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2442 | Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) { |
| 2443 | // Import the major distinguishing characteristics of this namespace. |
| 2444 | DeclContext *DC, *LexicalDC; |
| 2445 | DeclarationName Name; |
| 2446 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2447 | NamedDecl *ToD; |
| 2448 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2449 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2450 | if (ToD) |
| 2451 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2452 | |
| 2453 | NamespaceDecl *MergeWithNamespace = nullptr; |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2454 | if (!Name) { |
| 2455 | // This is an anonymous namespace. Adopt an existing anonymous |
| 2456 | // namespace if we can. |
| 2457 | // FIXME: Not testable. |
| 2458 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC)) |
| 2459 | MergeWithNamespace = TU->getAnonymousNamespace(); |
| 2460 | else |
| 2461 | MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace(); |
| 2462 | } else { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2463 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2464 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 2465 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2466 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2467 | if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace)) |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2468 | continue; |
| 2469 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2470 | if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) { |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2471 | MergeWithNamespace = FoundNS; |
| 2472 | ConflictingDecls.clear(); |
| 2473 | break; |
| 2474 | } |
| 2475 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2476 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2477 | } |
| 2478 | |
| 2479 | if (!ConflictingDecls.empty()) { |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 2480 | Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace, |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2481 | ConflictingDecls.data(), |
| 2482 | ConflictingDecls.size()); |
| 2483 | } |
| 2484 | } |
| 2485 | |
| 2486 | // Create the "to" namespace, if needed. |
| 2487 | NamespaceDecl *ToNamespace = MergeWithNamespace; |
| 2488 | if (!ToNamespace) { |
Abramo Bagnara | b5545be | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 2489 | ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 2490 | D->isInline(), |
Abramo Bagnara | b5545be | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 2491 | Importer.Import(D->getLocStart()), |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 2492 | Loc, Name.getAsIdentifierInfo(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2493 | /*PrevDecl=*/nullptr); |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2494 | ToNamespace->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2495 | LexicalDC->addDeclInternal(ToNamespace); |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2496 | |
| 2497 | // If this is an anonymous namespace, register it as the anonymous |
| 2498 | // namespace within its context. |
| 2499 | if (!Name) { |
| 2500 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC)) |
| 2501 | TU->setAnonymousNamespace(ToNamespace); |
| 2502 | else |
| 2503 | cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace); |
| 2504 | } |
| 2505 | } |
| 2506 | Importer.Imported(D, ToNamespace); |
| 2507 | |
| 2508 | ImportDeclContext(D); |
| 2509 | |
| 2510 | return ToNamespace; |
| 2511 | } |
| 2512 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2513 | Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) { |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2514 | // Import the major distinguishing characteristics of this typedef. |
| 2515 | DeclContext *DC, *LexicalDC; |
| 2516 | DeclarationName Name; |
| 2517 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2518 | NamedDecl *ToD; |
| 2519 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2520 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2521 | if (ToD) |
| 2522 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2523 | |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2524 | // If this typedef is not in block scope, determine whether we've |
| 2525 | // seen a typedef with the same name (that we can merge with) or any |
| 2526 | // other entity by that name (which name lookup could conflict with). |
| 2527 | if (!DC->isFunctionOrMethod()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2528 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2529 | unsigned IDNS = Decl::IDNS_Ordinary; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2530 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 2531 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2532 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2533 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2534 | continue; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2535 | if (TypedefNameDecl *FoundTypedef = |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2536 | dyn_cast<TypedefNameDecl>(FoundDecls[I])) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2537 | if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(), |
| 2538 | FoundTypedef->getUnderlyingType())) |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2539 | return Importer.Imported(D, FoundTypedef); |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2540 | } |
| 2541 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2542 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2543 | } |
| 2544 | |
| 2545 | if (!ConflictingDecls.empty()) { |
| 2546 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2547 | ConflictingDecls.data(), |
| 2548 | ConflictingDecls.size()); |
| 2549 | if (!Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2550 | return nullptr; |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2551 | } |
| 2552 | } |
| 2553 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2554 | // Import the underlying type of this typedef; |
| 2555 | QualType T = Importer.Import(D->getUnderlyingType()); |
| 2556 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2557 | return nullptr; |
| 2558 | |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2559 | // Create the new typedef node. |
| 2560 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 2561 | SourceLocation StartL = Importer.Import(D->getLocStart()); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2562 | TypedefNameDecl *ToTypedef; |
| 2563 | if (IsAlias) |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 2564 | ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC, |
| 2565 | StartL, Loc, |
| 2566 | Name.getAsIdentifierInfo(), |
| 2567 | TInfo); |
| 2568 | else |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2569 | ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC, |
| 2570 | StartL, Loc, |
| 2571 | Name.getAsIdentifierInfo(), |
| 2572 | TInfo); |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 2573 | |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2574 | ToTypedef->setAccess(D->getAccess()); |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2575 | ToTypedef->setLexicalDeclContext(LexicalDC); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2576 | Importer.Imported(D, ToTypedef); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2577 | LexicalDC->addDeclInternal(ToTypedef); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2578 | |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2579 | return ToTypedef; |
| 2580 | } |
| 2581 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2582 | Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) { |
| 2583 | return VisitTypedefNameDecl(D, /*IsAlias=*/false); |
| 2584 | } |
| 2585 | |
| 2586 | Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| 2587 | return VisitTypedefNameDecl(D, /*IsAlias=*/true); |
| 2588 | } |
| 2589 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 2590 | Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) { |
| 2591 | // Import the major distinguishing characteristics of this label. |
| 2592 | DeclContext *DC, *LexicalDC; |
| 2593 | DeclarationName Name; |
| 2594 | SourceLocation Loc; |
| 2595 | NamedDecl *ToD; |
| 2596 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 2597 | return nullptr; |
| 2598 | if (ToD) |
| 2599 | return ToD; |
| 2600 | |
| 2601 | assert(LexicalDC->isFunctionOrMethod()); |
| 2602 | |
| 2603 | LabelDecl *ToLabel = D->isGnuLocal() |
| 2604 | ? LabelDecl::Create(Importer.getToContext(), |
| 2605 | DC, Importer.Import(D->getLocation()), |
| 2606 | Name.getAsIdentifierInfo(), |
| 2607 | Importer.Import(D->getLocStart())) |
| 2608 | : LabelDecl::Create(Importer.getToContext(), |
| 2609 | DC, Importer.Import(D->getLocation()), |
| 2610 | Name.getAsIdentifierInfo()); |
| 2611 | Importer.Imported(D, ToLabel); |
| 2612 | |
| 2613 | LabelStmt *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt())); |
| 2614 | if (!Label) |
| 2615 | return nullptr; |
| 2616 | |
| 2617 | ToLabel->setStmt(Label); |
| 2618 | ToLabel->setLexicalDeclContext(LexicalDC); |
| 2619 | LexicalDC->addDeclInternal(ToLabel); |
| 2620 | return ToLabel; |
| 2621 | } |
| 2622 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2623 | Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) { |
| 2624 | // Import the major distinguishing characteristics of this enum. |
| 2625 | DeclContext *DC, *LexicalDC; |
| 2626 | DeclarationName Name; |
| 2627 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2628 | NamedDecl *ToD; |
| 2629 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2630 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2631 | if (ToD) |
| 2632 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2633 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2634 | // Figure out what enum name we're looking for. |
| 2635 | unsigned IDNS = Decl::IDNS_Tag; |
| 2636 | DeclarationName SearchName = Name; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2637 | if (!SearchName && D->getTypedefNameForAnonDecl()) { |
| 2638 | SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName()); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2639 | IDNS = Decl::IDNS_Ordinary; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2640 | } else if (Importer.getToContext().getLangOpts().CPlusPlus) |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2641 | IDNS |= Decl::IDNS_Ordinary; |
| 2642 | |
| 2643 | // We may already have an enum of the same name; try to find and match it. |
| 2644 | if (!DC->isFunctionOrMethod() && SearchName) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2645 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2646 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 2647 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2648 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2649 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2650 | continue; |
| 2651 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2652 | Decl *Found = FoundDecls[I]; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2653 | if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) { |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2654 | if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) |
| 2655 | Found = Tag->getDecl(); |
| 2656 | } |
| 2657 | |
| 2658 | if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) { |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2659 | if (IsStructuralMatch(D, FoundEnum)) |
| 2660 | return Importer.Imported(D, FoundEnum); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2661 | } |
| 2662 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2663 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2664 | } |
| 2665 | |
| 2666 | if (!ConflictingDecls.empty()) { |
| 2667 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2668 | ConflictingDecls.data(), |
| 2669 | ConflictingDecls.size()); |
| 2670 | } |
| 2671 | } |
| 2672 | |
| 2673 | // Create the enum declaration. |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2674 | EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, |
| 2675 | Importer.Import(D->getLocStart()), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2676 | Loc, Name.getAsIdentifierInfo(), nullptr, |
Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2677 | D->isScoped(), D->isScopedUsingClassTag(), |
| 2678 | D->isFixed()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2679 | // Import the qualifier, if any. |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2680 | D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2681 | D2->setAccess(D->getAccess()); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2682 | D2->setLexicalDeclContext(LexicalDC); |
| 2683 | Importer.Imported(D, D2); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2684 | LexicalDC->addDeclInternal(D2); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2685 | |
| 2686 | // Import the integer type. |
| 2687 | QualType ToIntegerType = Importer.Import(D->getIntegerType()); |
| 2688 | if (ToIntegerType.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2689 | return nullptr; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2690 | D2->setIntegerType(ToIntegerType); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2691 | |
| 2692 | // Import the definition |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2693 | if (D->isCompleteDefinition() && ImportDefinition(D, D2)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2694 | return nullptr; |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2695 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2696 | return D2; |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2697 | } |
| 2698 | |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2699 | Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) { |
| 2700 | // If this record has a definition in the translation unit we're coming from, |
| 2701 | // but this particular declaration is not that definition, import the |
| 2702 | // definition and map to that. |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 2703 | TagDecl *Definition = D->getDefinition(); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2704 | if (Definition && Definition != D) { |
| 2705 | Decl *ImportedDef = Importer.Import(Definition); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2706 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2707 | return nullptr; |
| 2708 | |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2709 | return Importer.Imported(D, ImportedDef); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
| 2712 | // Import the major distinguishing characteristics of this record. |
| 2713 | DeclContext *DC, *LexicalDC; |
| 2714 | DeclarationName Name; |
| 2715 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2716 | NamedDecl *ToD; |
| 2717 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2718 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2719 | if (ToD) |
| 2720 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2721 | |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2722 | // Figure out what structure name we're looking for. |
| 2723 | unsigned IDNS = Decl::IDNS_Tag; |
| 2724 | DeclarationName SearchName = Name; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2725 | if (!SearchName && D->getTypedefNameForAnonDecl()) { |
| 2726 | SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName()); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2727 | IDNS = Decl::IDNS_Ordinary; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2728 | } else if (Importer.getToContext().getLangOpts().CPlusPlus) |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2729 | IDNS |= Decl::IDNS_Ordinary; |
| 2730 | |
| 2731 | // We may already have a record of the same name; try to find and match it. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2732 | RecordDecl *AdoptDecl = nullptr; |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2733 | if (!DC->isFunctionOrMethod()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2734 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2735 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 2736 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2737 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2738 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2739 | continue; |
| 2740 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2741 | Decl *Found = FoundDecls[I]; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2742 | if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) { |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2743 | if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) |
| 2744 | Found = Tag->getDecl(); |
| 2745 | } |
| 2746 | |
| 2747 | if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) { |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 2748 | if (D->isAnonymousStructOrUnion() && |
| 2749 | FoundRecord->isAnonymousStructOrUnion()) { |
| 2750 | // If both anonymous structs/unions are in a record context, make sure |
| 2751 | // they occur in the same location in the context records. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 2752 | if (Optional<unsigned> Index1 |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 2753 | = findAnonymousStructOrUnionIndex(D)) { |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 2754 | if (Optional<unsigned> Index2 = |
| 2755 | findAnonymousStructOrUnionIndex(FoundRecord)) { |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 2756 | if (*Index1 != *Index2) |
| 2757 | continue; |
| 2758 | } |
| 2759 | } |
| 2760 | } |
| 2761 | |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2762 | if (RecordDecl *FoundDef = FoundRecord->getDefinition()) { |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2763 | if ((SearchName && !D->isCompleteDefinition()) |
| 2764 | || (D->isCompleteDefinition() && |
| 2765 | D->isAnonymousStructOrUnion() |
| 2766 | == FoundDef->isAnonymousStructOrUnion() && |
| 2767 | IsStructuralMatch(D, FoundDef))) { |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2768 | // The record types structurally match, or the "from" translation |
| 2769 | // unit only had a forward declaration anyway; call it the same |
| 2770 | // function. |
| 2771 | // FIXME: For C++, we should also merge methods here. |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2772 | return Importer.Imported(D, FoundDef); |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2773 | } |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2774 | } else if (!D->isCompleteDefinition()) { |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2775 | // We have a forward declaration of this type, so adopt that forward |
| 2776 | // declaration rather than building a new one. |
Sean Callanan | c94711c | 2014-03-04 18:11:50 +0000 | [diff] [blame] | 2777 | |
| 2778 | // If one or both can be completed from external storage then try one |
| 2779 | // last time to complete and compare them before doing this. |
| 2780 | |
| 2781 | if (FoundRecord->hasExternalLexicalStorage() && |
| 2782 | !FoundRecord->isCompleteDefinition()) |
| 2783 | FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord); |
| 2784 | if (D->hasExternalLexicalStorage()) |
| 2785 | D->getASTContext().getExternalSource()->CompleteType(D); |
| 2786 | |
| 2787 | if (FoundRecord->isCompleteDefinition() && |
| 2788 | D->isCompleteDefinition() && |
| 2789 | !IsStructuralMatch(D, FoundRecord)) |
| 2790 | continue; |
| 2791 | |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2792 | AdoptDecl = FoundRecord; |
| 2793 | continue; |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2794 | } else if (!SearchName) { |
| 2795 | continue; |
| 2796 | } |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2797 | } |
| 2798 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2799 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2800 | } |
| 2801 | |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2802 | if (!ConflictingDecls.empty() && SearchName) { |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2803 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2804 | ConflictingDecls.data(), |
| 2805 | ConflictingDecls.size()); |
| 2806 | } |
| 2807 | } |
| 2808 | |
| 2809 | // Create the record declaration. |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2810 | RecordDecl *D2 = AdoptDecl; |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2811 | SourceLocation StartLoc = Importer.Import(D->getLocStart()); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2812 | if (!D2) { |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 2813 | CXXRecordDecl *D2CXX = nullptr; |
| 2814 | if (CXXRecordDecl *DCXX = llvm::dyn_cast<CXXRecordDecl>(D)) { |
| 2815 | if (DCXX->isLambda()) { |
| 2816 | TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo()); |
| 2817 | D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(), |
| 2818 | DC, TInfo, Loc, |
| 2819 | DCXX->isDependentLambda(), |
| 2820 | DCXX->isGenericLambda(), |
| 2821 | DCXX->getLambdaCaptureDefault()); |
| 2822 | Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl()); |
| 2823 | if (DCXX->getLambdaContextDecl() && !CDecl) |
| 2824 | return nullptr; |
Sean Callanan | 041cceb | 2016-05-14 05:43:57 +0000 | [diff] [blame] | 2825 | D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl); |
| 2826 | } else if (DCXX->isInjectedClassName()) { |
| 2827 | // We have to be careful to do a similar dance to the one in |
| 2828 | // Sema::ActOnStartCXXMemberDeclarations |
| 2829 | CXXRecordDecl *const PrevDecl = nullptr; |
| 2830 | const bool DelayTypeCreation = true; |
| 2831 | D2CXX = CXXRecordDecl::Create( |
| 2832 | Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc, |
| 2833 | Name.getAsIdentifierInfo(), PrevDecl, DelayTypeCreation); |
| 2834 | Importer.getToContext().getTypeDeclType( |
| 2835 | D2CXX, llvm::dyn_cast<CXXRecordDecl>(DC)); |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 2836 | } else { |
| 2837 | D2CXX = CXXRecordDecl::Create(Importer.getToContext(), |
| 2838 | D->getTagKind(), |
| 2839 | DC, StartLoc, Loc, |
| 2840 | Name.getAsIdentifierInfo()); |
| 2841 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2842 | D2 = D2CXX; |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2843 | D2->setAccess(D->getAccess()); |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2844 | } else { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2845 | D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2846 | DC, StartLoc, Loc, Name.getAsIdentifierInfo()); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2847 | } |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2848 | |
| 2849 | D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2850 | D2->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2851 | LexicalDC->addDeclInternal(D2); |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2852 | if (D->isAnonymousStructOrUnion()) |
| 2853 | D2->setAnonymousStructOrUnion(true); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2854 | } |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2855 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2856 | Importer.Imported(D, D2); |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2857 | |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 2858 | if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2859 | return nullptr; |
| 2860 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2861 | return D2; |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2862 | } |
| 2863 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2864 | Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 2865 | // Import the major distinguishing characteristics of this enumerator. |
| 2866 | DeclContext *DC, *LexicalDC; |
| 2867 | DeclarationName Name; |
| 2868 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2869 | NamedDecl *ToD; |
| 2870 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2871 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2872 | if (ToD) |
| 2873 | return ToD; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2874 | |
| 2875 | QualType T = Importer.Import(D->getType()); |
| 2876 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2877 | return nullptr; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2878 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2879 | // Determine whether there are any other declarations with the same name and |
| 2880 | // in the same context. |
| 2881 | if (!LexicalDC->isFunctionOrMethod()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2882 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2883 | unsigned IDNS = Decl::IDNS_Ordinary; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2884 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 2885 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2886 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2887 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2888 | continue; |
Douglas Gregor | 9115508 | 2012-11-14 22:29:20 +0000 | [diff] [blame] | 2889 | |
| 2890 | if (EnumConstantDecl *FoundEnumConstant |
| 2891 | = dyn_cast<EnumConstantDecl>(FoundDecls[I])) { |
| 2892 | if (IsStructuralMatch(D, FoundEnumConstant)) |
| 2893 | return Importer.Imported(D, FoundEnumConstant); |
| 2894 | } |
| 2895 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2896 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2897 | } |
| 2898 | |
| 2899 | if (!ConflictingDecls.empty()) { |
| 2900 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2901 | ConflictingDecls.data(), |
| 2902 | ConflictingDecls.size()); |
| 2903 | if (!Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2904 | return nullptr; |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2905 | } |
| 2906 | } |
| 2907 | |
| 2908 | Expr *Init = Importer.Import(D->getInitExpr()); |
| 2909 | if (D->getInitExpr() && !Init) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2910 | return nullptr; |
| 2911 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2912 | EnumConstantDecl *ToEnumerator |
| 2913 | = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc, |
| 2914 | Name.getAsIdentifierInfo(), T, |
| 2915 | Init, D->getInitVal()); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2916 | ToEnumerator->setAccess(D->getAccess()); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2917 | ToEnumerator->setLexicalDeclContext(LexicalDC); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2918 | Importer.Imported(D, ToEnumerator); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2919 | LexicalDC->addDeclInternal(ToEnumerator); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2920 | return ToEnumerator; |
| 2921 | } |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2922 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2923 | Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) { |
| 2924 | // Import the major distinguishing characteristics of this function. |
| 2925 | DeclContext *DC, *LexicalDC; |
| 2926 | DeclarationName Name; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2927 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2928 | NamedDecl *ToD; |
| 2929 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2930 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2931 | if (ToD) |
| 2932 | return ToD; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2933 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2934 | // Try to find a function in our own ("to") context with the same name, same |
| 2935 | // type, and in the same context as the function we're importing. |
| 2936 | if (!LexicalDC->isFunctionOrMethod()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2937 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2938 | unsigned IDNS = Decl::IDNS_Ordinary; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2939 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 2940 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2941 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2942 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2943 | continue; |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2944 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2945 | if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) { |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 2946 | if (FoundFunction->hasExternalFormalLinkage() && |
| 2947 | D->hasExternalFormalLinkage()) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2948 | if (Importer.IsStructurallyEquivalent(D->getType(), |
| 2949 | FoundFunction->getType())) { |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2950 | // FIXME: Actually try to merge the body and other attributes. |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2951 | return Importer.Imported(D, FoundFunction); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2952 | } |
| 2953 | |
| 2954 | // FIXME: Check for overloading more carefully, e.g., by boosting |
| 2955 | // Sema::IsOverload out to the AST library. |
| 2956 | |
| 2957 | // Function overloading is okay in C++. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2958 | if (Importer.getToContext().getLangOpts().CPlusPlus) |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2959 | continue; |
| 2960 | |
| 2961 | // Complain about inconsistent function types. |
| 2962 | Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent) |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2963 | << Name << D->getType() << FoundFunction->getType(); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2964 | Importer.ToDiag(FoundFunction->getLocation(), |
| 2965 | diag::note_odr_value_here) |
| 2966 | << FoundFunction->getType(); |
| 2967 | } |
| 2968 | } |
| 2969 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2970 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
| 2973 | if (!ConflictingDecls.empty()) { |
| 2974 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2975 | ConflictingDecls.data(), |
| 2976 | ConflictingDecls.size()); |
| 2977 | if (!Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2978 | return nullptr; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2979 | } |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 2980 | } |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2981 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2982 | DeclarationNameInfo NameInfo(Name, Loc); |
| 2983 | // Import additional name location/type info. |
| 2984 | ImportDeclarationNameLoc(D->getNameInfo(), NameInfo); |
| 2985 | |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 2986 | QualType FromTy = D->getType(); |
| 2987 | bool usedDifferentExceptionSpec = false; |
| 2988 | |
| 2989 | if (const FunctionProtoType * |
| 2990 | FromFPT = D->getType()->getAs<FunctionProtoType>()) { |
| 2991 | FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo(); |
| 2992 | // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the |
| 2993 | // FunctionDecl that we are importing the FunctionProtoType for. |
| 2994 | // To avoid an infinite recursion when importing, create the FunctionDecl |
| 2995 | // with a simplified function type and update it afterwards. |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 2996 | if (FromEPI.ExceptionSpec.SourceDecl || |
| 2997 | FromEPI.ExceptionSpec.SourceTemplate || |
| 2998 | FromEPI.ExceptionSpec.NoexceptExpr) { |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 2999 | FunctionProtoType::ExtProtoInfo DefaultEPI; |
| 3000 | FromTy = Importer.getFromContext().getFunctionType( |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3001 | FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI); |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 3002 | usedDifferentExceptionSpec = true; |
| 3003 | } |
| 3004 | } |
| 3005 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3006 | // Import the type. |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 3007 | QualType T = Importer.Import(FromTy); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3008 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3009 | return nullptr; |
| 3010 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3011 | // Import the function parameters. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3012 | SmallVector<ParmVarDecl *, 8> Parameters; |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 3013 | for (auto P : D->params()) { |
| 3014 | ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P)); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3015 | if (!ToP) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3016 | return nullptr; |
| 3017 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3018 | Parameters.push_back(ToP); |
| 3019 | } |
| 3020 | |
| 3021 | // Create the imported function. |
| 3022 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3023 | FunctionDecl *ToFunction = nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3024 | SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart()); |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 3025 | if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) { |
| 3026 | ToFunction = CXXConstructorDecl::Create(Importer.getToContext(), |
| 3027 | cast<CXXRecordDecl>(DC), |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3028 | InnerLocStart, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3029 | NameInfo, T, TInfo, |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 3030 | FromConstructor->isExplicit(), |
| 3031 | D->isInlineSpecified(), |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 3032 | D->isImplicit(), |
| 3033 | D->isConstexpr()); |
Sean Callanan | dd2c174 | 2016-05-16 20:48:03 +0000 | [diff] [blame] | 3034 | if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) { |
| 3035 | SmallVector<CXXCtorInitializer *, 4> CtorInitializers; |
| 3036 | for (CXXCtorInitializer *I : FromConstructor->inits()) { |
| 3037 | CXXCtorInitializer *ToI = |
| 3038 | cast_or_null<CXXCtorInitializer>(Importer.Import(I)); |
| 3039 | if (!ToI && I) |
| 3040 | return nullptr; |
| 3041 | CtorInitializers.push_back(ToI); |
| 3042 | } |
| 3043 | CXXCtorInitializer **Memory = |
| 3044 | new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers]; |
| 3045 | std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory); |
| 3046 | CXXConstructorDecl *ToCtor = llvm::cast<CXXConstructorDecl>(ToFunction); |
| 3047 | ToCtor->setCtorInitializers(Memory); |
| 3048 | ToCtor->setNumCtorInitializers(NumInitializers); |
| 3049 | } |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 3050 | } else if (isa<CXXDestructorDecl>(D)) { |
| 3051 | ToFunction = CXXDestructorDecl::Create(Importer.getToContext(), |
| 3052 | cast<CXXRecordDecl>(DC), |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3053 | InnerLocStart, |
Craig Silverstein | af8808d | 2010-10-21 00:44:50 +0000 | [diff] [blame] | 3054 | NameInfo, T, TInfo, |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 3055 | D->isInlineSpecified(), |
| 3056 | D->isImplicit()); |
| 3057 | } else if (CXXConversionDecl *FromConversion |
| 3058 | = dyn_cast<CXXConversionDecl>(D)) { |
| 3059 | ToFunction = CXXConversionDecl::Create(Importer.getToContext(), |
| 3060 | cast<CXXRecordDecl>(DC), |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3061 | InnerLocStart, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3062 | NameInfo, T, TInfo, |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 3063 | D->isInlineSpecified(), |
Douglas Gregor | f2f0806 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 3064 | FromConversion->isExplicit(), |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 3065 | D->isConstexpr(), |
Douglas Gregor | f2f0806 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 3066 | Importer.Import(D->getLocEnd())); |
Douglas Gregor | a50ad13 | 2010-11-29 16:04:58 +0000 | [diff] [blame] | 3067 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 3068 | ToFunction = CXXMethodDecl::Create(Importer.getToContext(), |
| 3069 | cast<CXXRecordDecl>(DC), |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3070 | InnerLocStart, |
Douglas Gregor | a50ad13 | 2010-11-29 16:04:58 +0000 | [diff] [blame] | 3071 | NameInfo, T, TInfo, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3072 | Method->getStorageClass(), |
Douglas Gregor | f2f0806 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 3073 | Method->isInlineSpecified(), |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 3074 | D->isConstexpr(), |
Douglas Gregor | f2f0806 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 3075 | Importer.Import(D->getLocEnd())); |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 3076 | } else { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3077 | ToFunction = FunctionDecl::Create(Importer.getToContext(), DC, |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3078 | InnerLocStart, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3079 | NameInfo, T, TInfo, D->getStorageClass(), |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 3080 | D->isInlineSpecified(), |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 3081 | D->hasWrittenPrototype(), |
| 3082 | D->isConstexpr()); |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 3083 | } |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3084 | |
| 3085 | // Import the qualifier, if any. |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3086 | ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 3087 | ToFunction->setAccess(D->getAccess()); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3088 | ToFunction->setLexicalDeclContext(LexicalDC); |
John McCall | 08432c8 | 2011-01-27 02:37:01 +0000 | [diff] [blame] | 3089 | ToFunction->setVirtualAsWritten(D->isVirtualAsWritten()); |
| 3090 | ToFunction->setTrivial(D->isTrivial()); |
| 3091 | ToFunction->setPure(D->isPure()); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3092 | Importer.Imported(D, ToFunction); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 3093 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3094 | // Set the parameters. |
| 3095 | for (unsigned I = 0, N = Parameters.size(); I != N; ++I) { |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3096 | Parameters[I]->setOwningFunction(ToFunction); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3097 | ToFunction->addDeclInternal(Parameters[I]); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3098 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 3099 | ToFunction->setParams(Parameters); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3100 | |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 3101 | if (usedDifferentExceptionSpec) { |
| 3102 | // Update FunctionProtoType::ExtProtoInfo. |
| 3103 | QualType T = Importer.Import(D->getType()); |
| 3104 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3105 | return nullptr; |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 3106 | ToFunction->setType(T); |
Argyrios Kyrtzidis | b41791d | 2012-09-22 01:58:06 +0000 | [diff] [blame] | 3107 | } |
| 3108 | |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3109 | // Import the body, if any. |
| 3110 | if (Stmt *FromBody = D->getBody()) { |
| 3111 | if (Stmt *ToBody = Importer.Import(FromBody)) { |
| 3112 | ToFunction->setBody(ToBody); |
| 3113 | } |
| 3114 | } |
| 3115 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3116 | // FIXME: Other bits to merge? |
Douglas Gregor | 0eaa2bf | 2010-10-01 23:55:07 +0000 | [diff] [blame] | 3117 | |
| 3118 | // Add this function to the lexical context. |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3119 | LexicalDC->addDeclInternal(ToFunction); |
Douglas Gregor | 0eaa2bf | 2010-10-01 23:55:07 +0000 | [diff] [blame] | 3120 | |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3121 | return ToFunction; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3122 | } |
| 3123 | |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 3124 | Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) { |
| 3125 | return VisitFunctionDecl(D); |
| 3126 | } |
| 3127 | |
| 3128 | Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
| 3129 | return VisitCXXMethodDecl(D); |
| 3130 | } |
| 3131 | |
| 3132 | Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
| 3133 | return VisitCXXMethodDecl(D); |
| 3134 | } |
| 3135 | |
| 3136 | Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) { |
| 3137 | return VisitCXXMethodDecl(D); |
| 3138 | } |
| 3139 | |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 3140 | static unsigned getFieldIndex(Decl *F) { |
| 3141 | RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext()); |
| 3142 | if (!Owner) |
| 3143 | return 0; |
| 3144 | |
| 3145 | unsigned Index = 1; |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 3146 | for (const auto *D : Owner->noload_decls()) { |
| 3147 | if (D == F) |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 3148 | return Index; |
| 3149 | |
| 3150 | if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D)) |
| 3151 | ++Index; |
| 3152 | } |
| 3153 | |
| 3154 | return Index; |
| 3155 | } |
| 3156 | |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 3157 | Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) { |
| 3158 | // Import the major distinguishing characteristics of a variable. |
| 3159 | DeclContext *DC, *LexicalDC; |
| 3160 | DeclarationName Name; |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 3161 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3162 | NamedDecl *ToD; |
| 3163 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3164 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3165 | if (ToD) |
| 3166 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3167 | |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 3168 | // Determine whether we've already imported this field. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3169 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 3170 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3171 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 3172 | if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) { |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 3173 | // For anonymous fields, match up by index. |
| 3174 | if (!Name && getFieldIndex(D) != getFieldIndex(FoundField)) |
| 3175 | continue; |
| 3176 | |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 3177 | if (Importer.IsStructurallyEquivalent(D->getType(), |
| 3178 | FoundField->getType())) { |
| 3179 | Importer.Imported(D, FoundField); |
| 3180 | return FoundField; |
| 3181 | } |
| 3182 | |
| 3183 | Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent) |
| 3184 | << Name << D->getType() << FoundField->getType(); |
| 3185 | Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) |
| 3186 | << FoundField->getType(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3187 | return nullptr; |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 3188 | } |
| 3189 | } |
| 3190 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3191 | // Import the type. |
| 3192 | QualType T = Importer.Import(D->getType()); |
| 3193 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3194 | return nullptr; |
| 3195 | |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 3196 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 3197 | Expr *BitWidth = Importer.Import(D->getBitWidth()); |
| 3198 | if (!BitWidth && D->getBitWidth()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3199 | return nullptr; |
| 3200 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3201 | FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC, |
| 3202 | Importer.Import(D->getInnerLocStart()), |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 3203 | Loc, Name.getAsIdentifierInfo(), |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3204 | T, TInfo, BitWidth, D->isMutable(), |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3205 | D->getInClassInitStyle()); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 3206 | ToField->setAccess(D->getAccess()); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 3207 | ToField->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 3a83ea7 | 2016-03-03 02:22:05 +0000 | [diff] [blame] | 3208 | if (Expr *FromInitializer = D->getInClassInitializer()) { |
Sean Callanan | bb33f58 | 2016-03-03 01:21:28 +0000 | [diff] [blame] | 3209 | Expr *ToInitializer = Importer.Import(FromInitializer); |
| 3210 | if (ToInitializer) |
| 3211 | ToField->setInClassInitializer(ToInitializer); |
| 3212 | else |
| 3213 | return nullptr; |
| 3214 | } |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 3215 | ToField->setImplicit(D->isImplicit()); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 3216 | Importer.Imported(D, ToField); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3217 | LexicalDC->addDeclInternal(ToField); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 3218 | return ToField; |
| 3219 | } |
| 3220 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3221 | Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) { |
| 3222 | // Import the major distinguishing characteristics of a variable. |
| 3223 | DeclContext *DC, *LexicalDC; |
| 3224 | DeclarationName Name; |
| 3225 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3226 | NamedDecl *ToD; |
| 3227 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3228 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3229 | if (ToD) |
| 3230 | return ToD; |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3231 | |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 3232 | // Determine whether we've already imported this field. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3233 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 3234 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3235 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 3236 | if (IndirectFieldDecl *FoundField |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3237 | = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) { |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 3238 | // For anonymous indirect fields, match up by index. |
| 3239 | if (!Name && getFieldIndex(D) != getFieldIndex(FoundField)) |
| 3240 | continue; |
| 3241 | |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 3242 | if (Importer.IsStructurallyEquivalent(D->getType(), |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 3243 | FoundField->getType(), |
David Blaikie | 7d17010 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 3244 | !Name.isEmpty())) { |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 3245 | Importer.Imported(D, FoundField); |
| 3246 | return FoundField; |
| 3247 | } |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 3248 | |
| 3249 | // If there are more anonymous fields to check, continue. |
| 3250 | if (!Name && I < N-1) |
| 3251 | continue; |
| 3252 | |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 3253 | Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent) |
| 3254 | << Name << D->getType() << FoundField->getType(); |
| 3255 | Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) |
| 3256 | << FoundField->getType(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3257 | return nullptr; |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 3258 | } |
| 3259 | } |
| 3260 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3261 | // Import the type. |
| 3262 | QualType T = Importer.Import(D->getType()); |
| 3263 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3264 | return nullptr; |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3265 | |
| 3266 | NamedDecl **NamedChain = |
| 3267 | new (Importer.getToContext())NamedDecl*[D->getChainingSize()]; |
| 3268 | |
| 3269 | unsigned i = 0; |
Aaron Ballman | 29c9460 | 2014-03-07 18:36:15 +0000 | [diff] [blame] | 3270 | for (auto *PI : D->chain()) { |
Aaron Ballman | 1391608 | 2014-03-07 18:11:58 +0000 | [diff] [blame] | 3271 | Decl *D = Importer.Import(PI); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3272 | if (!D) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3273 | return nullptr; |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3274 | NamedChain[i++] = cast<NamedDecl>(D); |
| 3275 | } |
| 3276 | |
| 3277 | IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create( |
Aaron Ballman | 260995b | 2014-10-15 16:58:18 +0000 | [diff] [blame] | 3278 | Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T, |
| 3279 | NamedChain, D->getChainingSize()); |
| 3280 | |
| 3281 | for (const auto *Attr : D->attrs()) |
| 3282 | ToIndirectField->addAttr(Attr->clone(Importer.getToContext())); |
| 3283 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3284 | ToIndirectField->setAccess(D->getAccess()); |
| 3285 | ToIndirectField->setLexicalDeclContext(LexicalDC); |
| 3286 | Importer.Imported(D, ToIndirectField); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3287 | LexicalDC->addDeclInternal(ToIndirectField); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3288 | return ToIndirectField; |
| 3289 | } |
| 3290 | |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3291 | Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) { |
| 3292 | // Import the major distinguishing characteristics of an ivar. |
| 3293 | DeclContext *DC, *LexicalDC; |
| 3294 | DeclarationName Name; |
| 3295 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3296 | NamedDecl *ToD; |
| 3297 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3298 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3299 | if (ToD) |
| 3300 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3301 | |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3302 | // Determine whether we've already imported this ivar |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3303 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 3304 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3305 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 3306 | if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) { |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3307 | if (Importer.IsStructurallyEquivalent(D->getType(), |
| 3308 | FoundIvar->getType())) { |
| 3309 | Importer.Imported(D, FoundIvar); |
| 3310 | return FoundIvar; |
| 3311 | } |
| 3312 | |
| 3313 | Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent) |
| 3314 | << Name << D->getType() << FoundIvar->getType(); |
| 3315 | Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here) |
| 3316 | << FoundIvar->getType(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3317 | return nullptr; |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3318 | } |
| 3319 | } |
| 3320 | |
| 3321 | // Import the type. |
| 3322 | QualType T = Importer.Import(D->getType()); |
| 3323 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3324 | return nullptr; |
| 3325 | |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3326 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 3327 | Expr *BitWidth = Importer.Import(D->getBitWidth()); |
| 3328 | if (!BitWidth && D->getBitWidth()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3329 | return nullptr; |
| 3330 | |
Daniel Dunbar | fe3ead7 | 2010-04-02 20:10:03 +0000 | [diff] [blame] | 3331 | ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(), |
| 3332 | cast<ObjCContainerDecl>(DC), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3333 | Importer.Import(D->getInnerLocStart()), |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3334 | Loc, Name.getAsIdentifierInfo(), |
| 3335 | T, TInfo, D->getAccessControl(), |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 3336 | BitWidth, D->getSynthesize()); |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3337 | ToIvar->setLexicalDeclContext(LexicalDC); |
| 3338 | Importer.Imported(D, ToIvar); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3339 | LexicalDC->addDeclInternal(ToIvar); |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3340 | return ToIvar; |
| 3341 | |
| 3342 | } |
| 3343 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3344 | Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) { |
| 3345 | // Import the major distinguishing characteristics of a variable. |
| 3346 | DeclContext *DC, *LexicalDC; |
| 3347 | DeclarationName Name; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3348 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3349 | NamedDecl *ToD; |
| 3350 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3351 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3352 | if (ToD) |
| 3353 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3354 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3355 | // Try to find a variable in our own ("to") context with the same name and |
| 3356 | // in the same context as the variable we're importing. |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 3357 | if (D->isFileVarDecl()) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3358 | VarDecl *MergeWithVar = nullptr; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3359 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3360 | unsigned IDNS = Decl::IDNS_Ordinary; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3361 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 3362 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3363 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 3364 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3365 | continue; |
| 3366 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3367 | if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) { |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3368 | // We have found a variable that we may need to merge with. Check it. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 3369 | if (FoundVar->hasExternalFormalLinkage() && |
| 3370 | D->hasExternalFormalLinkage()) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3371 | if (Importer.IsStructurallyEquivalent(D->getType(), |
| 3372 | FoundVar->getType())) { |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3373 | MergeWithVar = FoundVar; |
| 3374 | break; |
| 3375 | } |
| 3376 | |
Douglas Gregor | 56521c5 | 2010-02-12 17:23:39 +0000 | [diff] [blame] | 3377 | const ArrayType *FoundArray |
| 3378 | = Importer.getToContext().getAsArrayType(FoundVar->getType()); |
| 3379 | const ArrayType *TArray |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3380 | = Importer.getToContext().getAsArrayType(D->getType()); |
Douglas Gregor | 56521c5 | 2010-02-12 17:23:39 +0000 | [diff] [blame] | 3381 | if (FoundArray && TArray) { |
| 3382 | if (isa<IncompleteArrayType>(FoundArray) && |
| 3383 | isa<ConstantArrayType>(TArray)) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3384 | // Import the type. |
| 3385 | QualType T = Importer.Import(D->getType()); |
| 3386 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3387 | return nullptr; |
| 3388 | |
Douglas Gregor | 56521c5 | 2010-02-12 17:23:39 +0000 | [diff] [blame] | 3389 | FoundVar->setType(T); |
| 3390 | MergeWithVar = FoundVar; |
| 3391 | break; |
| 3392 | } else if (isa<IncompleteArrayType>(TArray) && |
| 3393 | isa<ConstantArrayType>(FoundArray)) { |
| 3394 | MergeWithVar = FoundVar; |
| 3395 | break; |
Douglas Gregor | 2fbe558 | 2010-02-10 17:16:49 +0000 | [diff] [blame] | 3396 | } |
| 3397 | } |
| 3398 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3399 | Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent) |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3400 | << Name << D->getType() << FoundVar->getType(); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3401 | Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here) |
| 3402 | << FoundVar->getType(); |
| 3403 | } |
| 3404 | } |
| 3405 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3406 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
| 3409 | if (MergeWithVar) { |
| 3410 | // An equivalent variable with external linkage has been found. Link |
| 3411 | // the two declarations, then merge them. |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 3412 | Importer.Imported(D, MergeWithVar); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3413 | |
| 3414 | if (VarDecl *DDef = D->getDefinition()) { |
| 3415 | if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) { |
| 3416 | Importer.ToDiag(ExistingDef->getLocation(), |
| 3417 | diag::err_odr_variable_multiple_def) |
| 3418 | << Name; |
| 3419 | Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here); |
| 3420 | } else { |
| 3421 | Expr *Init = Importer.Import(DDef->getInit()); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3422 | MergeWithVar->setInit(Init); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 3423 | if (DDef->isInitKnownICE()) { |
| 3424 | EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt(); |
| 3425 | Eval->CheckedICE = true; |
| 3426 | Eval->IsICE = DDef->isInitICE(); |
| 3427 | } |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3428 | } |
| 3429 | } |
| 3430 | |
| 3431 | return MergeWithVar; |
| 3432 | } |
| 3433 | |
| 3434 | if (!ConflictingDecls.empty()) { |
| 3435 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 3436 | ConflictingDecls.data(), |
| 3437 | ConflictingDecls.size()); |
| 3438 | if (!Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3439 | return nullptr; |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3440 | } |
| 3441 | } |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 3442 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3443 | // Import the type. |
| 3444 | QualType T = Importer.Import(D->getType()); |
| 3445 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3446 | return nullptr; |
| 3447 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3448 | // Create the imported variable. |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 3449 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3450 | VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, |
| 3451 | Importer.Import(D->getInnerLocStart()), |
| 3452 | Loc, Name.getAsIdentifierInfo(), |
| 3453 | T, TInfo, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3454 | D->getStorageClass()); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3455 | ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 3456 | ToVar->setAccess(D->getAccess()); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 3457 | ToVar->setLexicalDeclContext(LexicalDC); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 3458 | Importer.Imported(D, ToVar); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3459 | LexicalDC->addDeclInternal(ToVar); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 3460 | |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3461 | if (!D->isFileVarDecl() && |
| 3462 | D->isUsed()) |
| 3463 | ToVar->setIsUsed(); |
| 3464 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3465 | // Merge the initializer. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3466 | if (ImportDefinition(D, ToVar)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3467 | return nullptr; |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3468 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3469 | return ToVar; |
| 3470 | } |
| 3471 | |
Douglas Gregor | 8b228d7 | 2010-02-17 21:22:52 +0000 | [diff] [blame] | 3472 | Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) { |
| 3473 | // Parameters are created in the translation unit's context, then moved |
| 3474 | // into the function declaration's context afterward. |
| 3475 | DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); |
| 3476 | |
| 3477 | // Import the name of this declaration. |
| 3478 | DeclarationName Name = Importer.Import(D->getDeclName()); |
| 3479 | if (D->getDeclName() && !Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3480 | return nullptr; |
| 3481 | |
Douglas Gregor | 8b228d7 | 2010-02-17 21:22:52 +0000 | [diff] [blame] | 3482 | // Import the location of this declaration. |
| 3483 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 3484 | |
| 3485 | // Import the parameter's type. |
| 3486 | QualType T = Importer.Import(D->getType()); |
| 3487 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3488 | return nullptr; |
| 3489 | |
Douglas Gregor | 8b228d7 | 2010-02-17 21:22:52 +0000 | [diff] [blame] | 3490 | // Create the imported parameter. |
| 3491 | ImplicitParamDecl *ToParm |
| 3492 | = ImplicitParamDecl::Create(Importer.getToContext(), DC, |
| 3493 | Loc, Name.getAsIdentifierInfo(), |
| 3494 | T); |
| 3495 | return Importer.Imported(D, ToParm); |
| 3496 | } |
| 3497 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3498 | Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) { |
| 3499 | // Parameters are created in the translation unit's context, then moved |
| 3500 | // into the function declaration's context afterward. |
| 3501 | DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); |
| 3502 | |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 3503 | // Import the name of this declaration. |
| 3504 | DeclarationName Name = Importer.Import(D->getDeclName()); |
| 3505 | if (D->getDeclName() && !Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3506 | return nullptr; |
| 3507 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3508 | // Import the location of this declaration. |
| 3509 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 3510 | |
| 3511 | // Import the parameter's type. |
| 3512 | QualType T = Importer.Import(D->getType()); |
| 3513 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3514 | return nullptr; |
| 3515 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3516 | // Create the imported parameter. |
| 3517 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 3518 | ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3519 | Importer.Import(D->getInnerLocStart()), |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3520 | Loc, Name.getAsIdentifierInfo(), |
| 3521 | T, TInfo, D->getStorageClass(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3522 | /*FIXME: Default argument*/nullptr); |
John McCall | f3cd665 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 3523 | ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg()); |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3524 | |
| 3525 | if (D->isUsed()) |
| 3526 | ToParm->setIsUsed(); |
| 3527 | |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 3528 | return Importer.Imported(D, ToParm); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3529 | } |
| 3530 | |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3531 | Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) { |
| 3532 | // Import the major distinguishing characteristics of a method. |
| 3533 | DeclContext *DC, *LexicalDC; |
| 3534 | DeclarationName Name; |
| 3535 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3536 | NamedDecl *ToD; |
| 3537 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3538 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3539 | if (ToD) |
| 3540 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3541 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3542 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 3543 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3544 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 3545 | if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) { |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3546 | if (FoundMethod->isInstanceMethod() != D->isInstanceMethod()) |
| 3547 | continue; |
| 3548 | |
| 3549 | // Check return types. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3550 | if (!Importer.IsStructurallyEquivalent(D->getReturnType(), |
| 3551 | FoundMethod->getReturnType())) { |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3552 | Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3553 | << D->isInstanceMethod() << Name << D->getReturnType() |
| 3554 | << FoundMethod->getReturnType(); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3555 | Importer.ToDiag(FoundMethod->getLocation(), |
| 3556 | diag::note_odr_objc_method_here) |
| 3557 | << D->isInstanceMethod() << Name; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3558 | return nullptr; |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3559 | } |
| 3560 | |
| 3561 | // Check the number of parameters. |
| 3562 | if (D->param_size() != FoundMethod->param_size()) { |
| 3563 | Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent) |
| 3564 | << D->isInstanceMethod() << Name |
| 3565 | << D->param_size() << FoundMethod->param_size(); |
| 3566 | Importer.ToDiag(FoundMethod->getLocation(), |
| 3567 | diag::note_odr_objc_method_here) |
| 3568 | << D->isInstanceMethod() << Name; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3569 | return nullptr; |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3570 | } |
| 3571 | |
| 3572 | // Check parameter types. |
| 3573 | for (ObjCMethodDecl::param_iterator P = D->param_begin(), |
| 3574 | PEnd = D->param_end(), FoundP = FoundMethod->param_begin(); |
| 3575 | P != PEnd; ++P, ++FoundP) { |
| 3576 | if (!Importer.IsStructurallyEquivalent((*P)->getType(), |
| 3577 | (*FoundP)->getType())) { |
| 3578 | Importer.FromDiag((*P)->getLocation(), |
| 3579 | diag::err_odr_objc_method_param_type_inconsistent) |
| 3580 | << D->isInstanceMethod() << Name |
| 3581 | << (*P)->getType() << (*FoundP)->getType(); |
| 3582 | Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here) |
| 3583 | << (*FoundP)->getType(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3584 | return nullptr; |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3585 | } |
| 3586 | } |
| 3587 | |
| 3588 | // Check variadic/non-variadic. |
| 3589 | // Check the number of parameters. |
| 3590 | if (D->isVariadic() != FoundMethod->isVariadic()) { |
| 3591 | Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent) |
| 3592 | << D->isInstanceMethod() << Name; |
| 3593 | Importer.ToDiag(FoundMethod->getLocation(), |
| 3594 | diag::note_odr_objc_method_here) |
| 3595 | << D->isInstanceMethod() << Name; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3596 | return nullptr; |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3597 | } |
| 3598 | |
| 3599 | // FIXME: Any other bits we need to merge? |
| 3600 | return Importer.Imported(D, FoundMethod); |
| 3601 | } |
| 3602 | } |
| 3603 | |
| 3604 | // Import the result type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3605 | QualType ResultTy = Importer.Import(D->getReturnType()); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3606 | if (ResultTy.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3607 | return nullptr; |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3608 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3609 | TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo()); |
Douglas Gregor | 12852d9 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 3610 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3611 | ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create( |
| 3612 | Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()), |
| 3613 | Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(), |
| 3614 | D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(), |
| 3615 | D->getImplementationControl(), D->hasRelatedResultType()); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3616 | |
| 3617 | // FIXME: When we decide to merge method definitions, we'll need to |
| 3618 | // deal with implicit parameters. |
| 3619 | |
| 3620 | // Import the parameters |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3621 | SmallVector<ParmVarDecl *, 5> ToParams; |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 3622 | for (auto *FromP : D->params()) { |
| 3623 | ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP)); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3624 | if (!ToP) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3625 | return nullptr; |
| 3626 | |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3627 | ToParams.push_back(ToP); |
| 3628 | } |
| 3629 | |
| 3630 | // Set the parameters. |
| 3631 | for (unsigned I = 0, N = ToParams.size(); I != N; ++I) { |
| 3632 | ToParams[I]->setOwningFunction(ToMethod); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3633 | ToMethod->addDeclInternal(ToParams[I]); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3634 | } |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 3635 | SmallVector<SourceLocation, 12> SelLocs; |
| 3636 | D->getSelectorLocs(SelLocs); |
| 3637 | ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3638 | |
| 3639 | ToMethod->setLexicalDeclContext(LexicalDC); |
| 3640 | Importer.Imported(D, ToMethod); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3641 | LexicalDC->addDeclInternal(ToMethod); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3642 | return ToMethod; |
| 3643 | } |
| 3644 | |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 3645 | Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) { |
| 3646 | // Import the major distinguishing characteristics of a category. |
| 3647 | DeclContext *DC, *LexicalDC; |
| 3648 | DeclarationName Name; |
| 3649 | SourceLocation Loc; |
| 3650 | NamedDecl *ToD; |
| 3651 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 3652 | return nullptr; |
| 3653 | if (ToD) |
| 3654 | return ToD; |
| 3655 | |
| 3656 | TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo()); |
| 3657 | if (!BoundInfo) |
| 3658 | return nullptr; |
| 3659 | |
| 3660 | ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create( |
| 3661 | Importer.getToContext(), DC, |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 3662 | D->getVariance(), |
| 3663 | Importer.Import(D->getVarianceLoc()), |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 3664 | D->getIndex(), |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 3665 | Importer.Import(D->getLocation()), |
| 3666 | Name.getAsIdentifierInfo(), |
| 3667 | Importer.Import(D->getColonLoc()), |
| 3668 | BoundInfo); |
| 3669 | Importer.Imported(D, Result); |
| 3670 | Result->setLexicalDeclContext(LexicalDC); |
| 3671 | return Result; |
| 3672 | } |
| 3673 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3674 | Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) { |
| 3675 | // Import the major distinguishing characteristics of a category. |
| 3676 | DeclContext *DC, *LexicalDC; |
| 3677 | DeclarationName Name; |
| 3678 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3679 | NamedDecl *ToD; |
| 3680 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3681 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3682 | if (ToD) |
| 3683 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3684 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3685 | ObjCInterfaceDecl *ToInterface |
| 3686 | = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface())); |
| 3687 | if (!ToInterface) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3688 | return nullptr; |
| 3689 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3690 | // Determine if we've already encountered this category. |
| 3691 | ObjCCategoryDecl *MergeWithCategory |
| 3692 | = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo()); |
| 3693 | ObjCCategoryDecl *ToCategory = MergeWithCategory; |
| 3694 | if (!ToCategory) { |
| 3695 | ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC, |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 3696 | Importer.Import(D->getAtStartLoc()), |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3697 | Loc, |
| 3698 | Importer.Import(D->getCategoryNameLoc()), |
Argyrios Kyrtzidis | 3a5094b | 2011-08-30 19:43:26 +0000 | [diff] [blame] | 3699 | Name.getAsIdentifierInfo(), |
Fariborz Jahanian | a7765fe | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 3700 | ToInterface, |
Douglas Gregor | ab7f0b3 | 2015-07-07 06:20:12 +0000 | [diff] [blame] | 3701 | /*TypeParamList=*/nullptr, |
Fariborz Jahanian | a7765fe | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 3702 | Importer.Import(D->getIvarLBraceLoc()), |
| 3703 | Importer.Import(D->getIvarRBraceLoc())); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3704 | ToCategory->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3705 | LexicalDC->addDeclInternal(ToCategory); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3706 | Importer.Imported(D, ToCategory); |
Douglas Gregor | ab7f0b3 | 2015-07-07 06:20:12 +0000 | [diff] [blame] | 3707 | // Import the type parameter list after calling Imported, to avoid |
| 3708 | // loops when bringing in their DeclContext. |
| 3709 | ToCategory->setTypeParamList(ImportObjCTypeParamList( |
| 3710 | D->getTypeParamList())); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3711 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3712 | // Import protocols |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3713 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 3714 | SmallVector<SourceLocation, 4> ProtocolLocs; |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3715 | ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc |
| 3716 | = D->protocol_loc_begin(); |
| 3717 | for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(), |
| 3718 | FromProtoEnd = D->protocol_end(); |
| 3719 | FromProto != FromProtoEnd; |
| 3720 | ++FromProto, ++FromProtoLoc) { |
| 3721 | ObjCProtocolDecl *ToProto |
| 3722 | = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); |
| 3723 | if (!ToProto) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3724 | return nullptr; |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3725 | Protocols.push_back(ToProto); |
| 3726 | ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); |
| 3727 | } |
| 3728 | |
| 3729 | // FIXME: If we're merging, make sure that the protocol list is the same. |
| 3730 | ToCategory->setProtocolList(Protocols.data(), Protocols.size(), |
| 3731 | ProtocolLocs.data(), Importer.getToContext()); |
| 3732 | |
| 3733 | } else { |
| 3734 | Importer.Imported(D, ToCategory); |
| 3735 | } |
| 3736 | |
| 3737 | // Import all of the members of this category. |
Douglas Gregor | 968d633 | 2010-02-21 18:24:45 +0000 | [diff] [blame] | 3738 | ImportDeclContext(D); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3739 | |
| 3740 | // If we have an implementation, import it as well. |
| 3741 | if (D->getImplementation()) { |
| 3742 | ObjCCategoryImplDecl *Impl |
Douglas Gregor | 35fd7bc | 2010-12-08 16:41:55 +0000 | [diff] [blame] | 3743 | = cast_or_null<ObjCCategoryImplDecl>( |
| 3744 | Importer.Import(D->getImplementation())); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3745 | if (!Impl) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3746 | return nullptr; |
| 3747 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3748 | ToCategory->setImplementation(Impl); |
| 3749 | } |
| 3750 | |
| 3751 | return ToCategory; |
| 3752 | } |
| 3753 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3754 | bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From, |
| 3755 | ObjCProtocolDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 3756 | ImportDefinitionKind Kind) { |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3757 | if (To->getDefinition()) { |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 3758 | if (shouldForceImportDeclContext(Kind)) |
| 3759 | ImportDeclContext(From); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3760 | return false; |
| 3761 | } |
| 3762 | |
| 3763 | // Start the protocol definition |
| 3764 | To->startDefinition(); |
| 3765 | |
| 3766 | // Import protocols |
| 3767 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 3768 | SmallVector<SourceLocation, 4> ProtocolLocs; |
| 3769 | ObjCProtocolDecl::protocol_loc_iterator |
| 3770 | FromProtoLoc = From->protocol_loc_begin(); |
| 3771 | for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(), |
| 3772 | FromProtoEnd = From->protocol_end(); |
| 3773 | FromProto != FromProtoEnd; |
| 3774 | ++FromProto, ++FromProtoLoc) { |
| 3775 | ObjCProtocolDecl *ToProto |
| 3776 | = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); |
| 3777 | if (!ToProto) |
| 3778 | return true; |
| 3779 | Protocols.push_back(ToProto); |
| 3780 | ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); |
| 3781 | } |
| 3782 | |
| 3783 | // FIXME: If we're merging, make sure that the protocol list is the same. |
| 3784 | To->setProtocolList(Protocols.data(), Protocols.size(), |
| 3785 | ProtocolLocs.data(), Importer.getToContext()); |
| 3786 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 3787 | if (shouldForceImportDeclContext(Kind)) { |
| 3788 | // Import all of the members of this protocol. |
| 3789 | ImportDeclContext(From, /*ForceImport=*/true); |
| 3790 | } |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3791 | return false; |
| 3792 | } |
| 3793 | |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3794 | Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) { |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3795 | // If this protocol has a definition in the translation unit we're coming |
| 3796 | // from, but this particular declaration is not that definition, import the |
| 3797 | // definition and map to that. |
| 3798 | ObjCProtocolDecl *Definition = D->getDefinition(); |
| 3799 | if (Definition && Definition != D) { |
| 3800 | Decl *ImportedDef = Importer.Import(Definition); |
| 3801 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3802 | return nullptr; |
| 3803 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3804 | return Importer.Imported(D, ImportedDef); |
| 3805 | } |
| 3806 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3807 | // Import the major distinguishing characteristics of a protocol. |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3808 | DeclContext *DC, *LexicalDC; |
| 3809 | DeclarationName Name; |
| 3810 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3811 | NamedDecl *ToD; |
| 3812 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3813 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3814 | if (ToD) |
| 3815 | return ToD; |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3816 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3817 | ObjCProtocolDecl *MergeWithProtocol = nullptr; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3818 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 3819 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3820 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 3821 | if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol)) |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3822 | continue; |
| 3823 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3824 | if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I]))) |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3825 | break; |
| 3826 | } |
| 3827 | |
| 3828 | ObjCProtocolDecl *ToProto = MergeWithProtocol; |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3829 | if (!ToProto) { |
| 3830 | ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, |
| 3831 | Name.getAsIdentifierInfo(), Loc, |
| 3832 | Importer.Import(D->getAtStartLoc()), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3833 | /*PrevDecl=*/nullptr); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3834 | ToProto->setLexicalDeclContext(LexicalDC); |
| 3835 | LexicalDC->addDeclInternal(ToProto); |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3836 | } |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3837 | |
| 3838 | Importer.Imported(D, ToProto); |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3839 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3840 | if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3841 | return nullptr; |
| 3842 | |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3843 | return ToProto; |
| 3844 | } |
| 3845 | |
Sean Callanan | 0aae041 | 2014-12-10 00:00:37 +0000 | [diff] [blame] | 3846 | Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 3847 | DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| 3848 | DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 3849 | |
| 3850 | SourceLocation ExternLoc = Importer.Import(D->getExternLoc()); |
| 3851 | SourceLocation LangLoc = Importer.Import(D->getLocation()); |
| 3852 | |
| 3853 | bool HasBraces = D->hasBraces(); |
| 3854 | |
Sean Callanan | b12a855 | 2014-12-10 21:22:20 +0000 | [diff] [blame] | 3855 | LinkageSpecDecl *ToLinkageSpec = |
| 3856 | LinkageSpecDecl::Create(Importer.getToContext(), |
| 3857 | DC, |
| 3858 | ExternLoc, |
| 3859 | LangLoc, |
| 3860 | D->getLanguage(), |
| 3861 | HasBraces); |
Sean Callanan | 0aae041 | 2014-12-10 00:00:37 +0000 | [diff] [blame] | 3862 | |
| 3863 | if (HasBraces) { |
| 3864 | SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc()); |
| 3865 | ToLinkageSpec->setRBraceLoc(RBraceLoc); |
| 3866 | } |
| 3867 | |
| 3868 | ToLinkageSpec->setLexicalDeclContext(LexicalDC); |
| 3869 | LexicalDC->addDeclInternal(ToLinkageSpec); |
| 3870 | |
| 3871 | Importer.Imported(D, ToLinkageSpec); |
| 3872 | |
| 3873 | return ToLinkageSpec; |
| 3874 | } |
| 3875 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3876 | bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From, |
| 3877 | ObjCInterfaceDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 3878 | ImportDefinitionKind Kind) { |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3879 | if (To->getDefinition()) { |
| 3880 | // Check consistency of superclass. |
| 3881 | ObjCInterfaceDecl *FromSuper = From->getSuperClass(); |
| 3882 | if (FromSuper) { |
| 3883 | FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper)); |
| 3884 | if (!FromSuper) |
| 3885 | return true; |
| 3886 | } |
| 3887 | |
| 3888 | ObjCInterfaceDecl *ToSuper = To->getSuperClass(); |
| 3889 | if ((bool)FromSuper != (bool)ToSuper || |
| 3890 | (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) { |
| 3891 | Importer.ToDiag(To->getLocation(), |
| 3892 | diag::err_odr_objc_superclass_inconsistent) |
| 3893 | << To->getDeclName(); |
| 3894 | if (ToSuper) |
| 3895 | Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass) |
| 3896 | << To->getSuperClass()->getDeclName(); |
| 3897 | else |
| 3898 | Importer.ToDiag(To->getLocation(), |
| 3899 | diag::note_odr_objc_missing_superclass); |
| 3900 | if (From->getSuperClass()) |
| 3901 | Importer.FromDiag(From->getSuperClassLoc(), |
| 3902 | diag::note_odr_objc_superclass) |
| 3903 | << From->getSuperClass()->getDeclName(); |
| 3904 | else |
| 3905 | Importer.FromDiag(From->getLocation(), |
| 3906 | diag::note_odr_objc_missing_superclass); |
| 3907 | } |
| 3908 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 3909 | if (shouldForceImportDeclContext(Kind)) |
| 3910 | ImportDeclContext(From); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3911 | return false; |
| 3912 | } |
| 3913 | |
| 3914 | // Start the definition. |
| 3915 | To->startDefinition(); |
| 3916 | |
| 3917 | // If this class has a superclass, import it. |
| 3918 | if (From->getSuperClass()) { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3919 | TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo()); |
| 3920 | if (!SuperTInfo) |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3921 | return true; |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3922 | |
| 3923 | To->setSuperClass(SuperTInfo); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3924 | } |
| 3925 | |
| 3926 | // Import protocols |
| 3927 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 3928 | SmallVector<SourceLocation, 4> ProtocolLocs; |
| 3929 | ObjCInterfaceDecl::protocol_loc_iterator |
| 3930 | FromProtoLoc = From->protocol_loc_begin(); |
| 3931 | |
| 3932 | for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(), |
| 3933 | FromProtoEnd = From->protocol_end(); |
| 3934 | FromProto != FromProtoEnd; |
| 3935 | ++FromProto, ++FromProtoLoc) { |
| 3936 | ObjCProtocolDecl *ToProto |
| 3937 | = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); |
| 3938 | if (!ToProto) |
| 3939 | return true; |
| 3940 | Protocols.push_back(ToProto); |
| 3941 | ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); |
| 3942 | } |
| 3943 | |
| 3944 | // FIXME: If we're merging, make sure that the protocol list is the same. |
| 3945 | To->setProtocolList(Protocols.data(), Protocols.size(), |
| 3946 | ProtocolLocs.data(), Importer.getToContext()); |
| 3947 | |
| 3948 | // Import categories. When the categories themselves are imported, they'll |
| 3949 | // hook themselves into this interface. |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 3950 | for (auto *Cat : From->known_categories()) |
| 3951 | Importer.Import(Cat); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 3952 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3953 | // If we have an @implementation, import it as well. |
| 3954 | if (From->getImplementation()) { |
| 3955 | ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>( |
| 3956 | Importer.Import(From->getImplementation())); |
| 3957 | if (!Impl) |
| 3958 | return true; |
| 3959 | |
| 3960 | To->setImplementation(Impl); |
| 3961 | } |
| 3962 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 3963 | if (shouldForceImportDeclContext(Kind)) { |
| 3964 | // Import all of the members of this class. |
| 3965 | ImportDeclContext(From, /*ForceImport=*/true); |
| 3966 | } |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3967 | return false; |
| 3968 | } |
| 3969 | |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 3970 | ObjCTypeParamList * |
| 3971 | ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) { |
| 3972 | if (!list) |
| 3973 | return nullptr; |
| 3974 | |
| 3975 | SmallVector<ObjCTypeParamDecl *, 4> toTypeParams; |
| 3976 | for (auto fromTypeParam : *list) { |
| 3977 | auto toTypeParam = cast_or_null<ObjCTypeParamDecl>( |
| 3978 | Importer.Import(fromTypeParam)); |
| 3979 | if (!toTypeParam) |
| 3980 | return nullptr; |
| 3981 | |
| 3982 | toTypeParams.push_back(toTypeParam); |
| 3983 | } |
| 3984 | |
| 3985 | return ObjCTypeParamList::create(Importer.getToContext(), |
| 3986 | Importer.Import(list->getLAngleLoc()), |
| 3987 | toTypeParams, |
| 3988 | Importer.Import(list->getRAngleLoc())); |
| 3989 | } |
| 3990 | |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3991 | Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3992 | // If this class has a definition in the translation unit we're coming from, |
| 3993 | // but this particular declaration is not that definition, import the |
| 3994 | // definition and map to that. |
| 3995 | ObjCInterfaceDecl *Definition = D->getDefinition(); |
| 3996 | if (Definition && Definition != D) { |
| 3997 | Decl *ImportedDef = Importer.Import(Definition); |
| 3998 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3999 | return nullptr; |
| 4000 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4001 | return Importer.Imported(D, ImportedDef); |
| 4002 | } |
| 4003 | |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 4004 | // Import the major distinguishing characteristics of an @interface. |
| 4005 | DeclContext *DC, *LexicalDC; |
| 4006 | DeclarationName Name; |
| 4007 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4008 | NamedDecl *ToD; |
| 4009 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4010 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4011 | if (ToD) |
| 4012 | return ToD; |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 4013 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4014 | // Look for an existing interface with the same name. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4015 | ObjCInterfaceDecl *MergeWithIface = nullptr; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4016 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 4017 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 4018 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 4019 | if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 4020 | continue; |
| 4021 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 4022 | if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I]))) |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 4023 | break; |
| 4024 | } |
| 4025 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4026 | // Create an interface declaration, if one does not already exist. |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 4027 | ObjCInterfaceDecl *ToIface = MergeWithIface; |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4028 | if (!ToIface) { |
| 4029 | ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC, |
| 4030 | Importer.Import(D->getAtStartLoc()), |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 4031 | Name.getAsIdentifierInfo(), |
Douglas Gregor | ab7f0b3 | 2015-07-07 06:20:12 +0000 | [diff] [blame] | 4032 | /*TypeParamList=*/nullptr, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4033 | /*PrevDecl=*/nullptr, Loc, |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4034 | D->isImplicitInterfaceDecl()); |
| 4035 | ToIface->setLexicalDeclContext(LexicalDC); |
| 4036 | LexicalDC->addDeclInternal(ToIface); |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 4037 | } |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4038 | Importer.Imported(D, ToIface); |
Douglas Gregor | ab7f0b3 | 2015-07-07 06:20:12 +0000 | [diff] [blame] | 4039 | // Import the type parameter list after calling Imported, to avoid |
| 4040 | // loops when bringing in their DeclContext. |
| 4041 | ToIface->setTypeParamList(ImportObjCTypeParamList( |
| 4042 | D->getTypeParamListAsWritten())); |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 4043 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4044 | if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4045 | return nullptr; |
| 4046 | |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 4047 | return ToIface; |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 4048 | } |
| 4049 | |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 4050 | Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 4051 | ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>( |
| 4052 | Importer.Import(D->getCategoryDecl())); |
| 4053 | if (!Category) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4054 | return nullptr; |
| 4055 | |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 4056 | ObjCCategoryImplDecl *ToImpl = Category->getImplementation(); |
| 4057 | if (!ToImpl) { |
| 4058 | DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| 4059 | if (!DC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4060 | return nullptr; |
| 4061 | |
Argyrios Kyrtzidis | 4996f5f | 2011-12-09 00:31:40 +0000 | [diff] [blame] | 4062 | SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc()); |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 4063 | ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC, |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 4064 | Importer.Import(D->getIdentifier()), |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 4065 | Category->getClassInterface(), |
| 4066 | Importer.Import(D->getLocation()), |
Argyrios Kyrtzidis | 4996f5f | 2011-12-09 00:31:40 +0000 | [diff] [blame] | 4067 | Importer.Import(D->getAtStartLoc()), |
| 4068 | CategoryNameLoc); |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 4069 | |
| 4070 | DeclContext *LexicalDC = DC; |
| 4071 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 4072 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 4073 | if (!LexicalDC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4074 | return nullptr; |
| 4075 | |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 4076 | ToImpl->setLexicalDeclContext(LexicalDC); |
| 4077 | } |
| 4078 | |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 4079 | LexicalDC->addDeclInternal(ToImpl); |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 4080 | Category->setImplementation(ToImpl); |
| 4081 | } |
| 4082 | |
| 4083 | Importer.Imported(D, ToImpl); |
Douglas Gregor | 35fd7bc | 2010-12-08 16:41:55 +0000 | [diff] [blame] | 4084 | ImportDeclContext(D); |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 4085 | return ToImpl; |
| 4086 | } |
| 4087 | |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 4088 | Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 4089 | // Find the corresponding interface. |
| 4090 | ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>( |
| 4091 | Importer.Import(D->getClassInterface())); |
| 4092 | if (!Iface) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4093 | return nullptr; |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 4094 | |
| 4095 | // Import the superclass, if any. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4096 | ObjCInterfaceDecl *Super = nullptr; |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 4097 | if (D->getSuperClass()) { |
| 4098 | Super = cast_or_null<ObjCInterfaceDecl>( |
| 4099 | Importer.Import(D->getSuperClass())); |
| 4100 | if (!Super) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4101 | return nullptr; |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 4102 | } |
| 4103 | |
| 4104 | ObjCImplementationDecl *Impl = Iface->getImplementation(); |
| 4105 | if (!Impl) { |
| 4106 | // We haven't imported an implementation yet. Create a new @implementation |
| 4107 | // now. |
| 4108 | Impl = ObjCImplementationDecl::Create(Importer.getToContext(), |
| 4109 | Importer.ImportContext(D->getDeclContext()), |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 4110 | Iface, Super, |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 4111 | Importer.Import(D->getLocation()), |
Fariborz Jahanian | a7765fe | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 4112 | Importer.Import(D->getAtStartLoc()), |
Argyrios Kyrtzidis | 5d2ce84 | 2013-05-03 22:31:26 +0000 | [diff] [blame] | 4113 | Importer.Import(D->getSuperClassLoc()), |
Fariborz Jahanian | a7765fe | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 4114 | Importer.Import(D->getIvarLBraceLoc()), |
| 4115 | Importer.Import(D->getIvarRBraceLoc())); |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 4116 | |
| 4117 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 4118 | DeclContext *LexicalDC |
| 4119 | = Importer.ImportContext(D->getLexicalDeclContext()); |
| 4120 | if (!LexicalDC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4121 | return nullptr; |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 4122 | Impl->setLexicalDeclContext(LexicalDC); |
| 4123 | } |
| 4124 | |
| 4125 | // Associate the implementation with the class it implements. |
| 4126 | Iface->setImplementation(Impl); |
| 4127 | Importer.Imported(D, Iface->getImplementation()); |
| 4128 | } else { |
| 4129 | Importer.Imported(D, Iface->getImplementation()); |
| 4130 | |
| 4131 | // Verify that the existing @implementation has the same superclass. |
| 4132 | if ((Super && !Impl->getSuperClass()) || |
| 4133 | (!Super && Impl->getSuperClass()) || |
Craig Topper | dcfc60f | 2014-05-07 06:57:44 +0000 | [diff] [blame] | 4134 | (Super && Impl->getSuperClass() && |
| 4135 | !declaresSameEntity(Super->getCanonicalDecl(), |
| 4136 | Impl->getSuperClass()))) { |
| 4137 | Importer.ToDiag(Impl->getLocation(), |
| 4138 | diag::err_odr_objc_superclass_inconsistent) |
| 4139 | << Iface->getDeclName(); |
| 4140 | // FIXME: It would be nice to have the location of the superclass |
| 4141 | // below. |
| 4142 | if (Impl->getSuperClass()) |
| 4143 | Importer.ToDiag(Impl->getLocation(), |
| 4144 | diag::note_odr_objc_superclass) |
| 4145 | << Impl->getSuperClass()->getDeclName(); |
| 4146 | else |
| 4147 | Importer.ToDiag(Impl->getLocation(), |
| 4148 | diag::note_odr_objc_missing_superclass); |
| 4149 | if (D->getSuperClass()) |
| 4150 | Importer.FromDiag(D->getLocation(), |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 4151 | diag::note_odr_objc_superclass) |
Craig Topper | dcfc60f | 2014-05-07 06:57:44 +0000 | [diff] [blame] | 4152 | << D->getSuperClass()->getDeclName(); |
| 4153 | else |
| 4154 | Importer.FromDiag(D->getLocation(), |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 4155 | diag::note_odr_objc_missing_superclass); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4156 | return nullptr; |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 4157 | } |
| 4158 | } |
| 4159 | |
| 4160 | // Import all of the members of this @implementation. |
| 4161 | ImportDeclContext(D); |
| 4162 | |
| 4163 | return Impl; |
| 4164 | } |
| 4165 | |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 4166 | Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { |
| 4167 | // Import the major distinguishing characteristics of an @property. |
| 4168 | DeclContext *DC, *LexicalDC; |
| 4169 | DeclarationName Name; |
| 4170 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4171 | NamedDecl *ToD; |
| 4172 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4173 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4174 | if (ToD) |
| 4175 | return ToD; |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 4176 | |
| 4177 | // Check whether we have already imported this property. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4178 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 4179 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 4180 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 4181 | if (ObjCPropertyDecl *FoundProp |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 4182 | = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) { |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 4183 | // Check property types. |
| 4184 | if (!Importer.IsStructurallyEquivalent(D->getType(), |
| 4185 | FoundProp->getType())) { |
| 4186 | Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent) |
| 4187 | << Name << D->getType() << FoundProp->getType(); |
| 4188 | Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here) |
| 4189 | << FoundProp->getType(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4190 | return nullptr; |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 4191 | } |
| 4192 | |
| 4193 | // FIXME: Check property attributes, getters, setters, etc.? |
| 4194 | |
| 4195 | // Consider these properties to be equivalent. |
| 4196 | Importer.Imported(D, FoundProp); |
| 4197 | return FoundProp; |
| 4198 | } |
| 4199 | } |
| 4200 | |
| 4201 | // Import the type. |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4202 | TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo()); |
| 4203 | if (!TSI) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4204 | return nullptr; |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 4205 | |
| 4206 | // Create the new property. |
| 4207 | ObjCPropertyDecl *ToProperty |
| 4208 | = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc, |
| 4209 | Name.getAsIdentifierInfo(), |
| 4210 | Importer.Import(D->getAtLoc()), |
Fariborz Jahanian | 86c2f5c | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 4211 | Importer.Import(D->getLParenLoc()), |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4212 | Importer.Import(D->getType()), |
| 4213 | TSI, |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 4214 | D->getPropertyImplementation()); |
| 4215 | Importer.Imported(D, ToProperty); |
| 4216 | ToProperty->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 4217 | LexicalDC->addDeclInternal(ToProperty); |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 4218 | |
| 4219 | ToProperty->setPropertyAttributes(D->getPropertyAttributes()); |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 4220 | ToProperty->setPropertyAttributesAsWritten( |
| 4221 | D->getPropertyAttributesAsWritten()); |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 4222 | ToProperty->setGetterName(Importer.Import(D->getGetterName())); |
| 4223 | ToProperty->setSetterName(Importer.Import(D->getSetterName())); |
| 4224 | ToProperty->setGetterMethodDecl( |
| 4225 | cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl()))); |
| 4226 | ToProperty->setSetterMethodDecl( |
| 4227 | cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl()))); |
| 4228 | ToProperty->setPropertyIvarDecl( |
| 4229 | cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl()))); |
| 4230 | return ToProperty; |
| 4231 | } |
| 4232 | |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4233 | Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { |
| 4234 | ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>( |
| 4235 | Importer.Import(D->getPropertyDecl())); |
| 4236 | if (!Property) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4237 | return nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4238 | |
| 4239 | DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| 4240 | if (!DC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4241 | return nullptr; |
| 4242 | |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4243 | // Import the lexical declaration context. |
| 4244 | DeclContext *LexicalDC = DC; |
| 4245 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 4246 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 4247 | if (!LexicalDC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4248 | return nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4249 | } |
| 4250 | |
| 4251 | ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC); |
| 4252 | if (!InImpl) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4253 | return nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4254 | |
| 4255 | // Import the ivar (for an @synthesize). |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4256 | ObjCIvarDecl *Ivar = nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4257 | if (D->getPropertyIvarDecl()) { |
| 4258 | Ivar = cast_or_null<ObjCIvarDecl>( |
| 4259 | Importer.Import(D->getPropertyIvarDecl())); |
| 4260 | if (!Ivar) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4261 | return nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4262 | } |
| 4263 | |
| 4264 | ObjCPropertyImplDecl *ToImpl |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 4265 | = InImpl->FindPropertyImplDecl(Property->getIdentifier(), |
| 4266 | Property->getQueryKind()); |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4267 | if (!ToImpl) { |
| 4268 | ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC, |
| 4269 | Importer.Import(D->getLocStart()), |
| 4270 | Importer.Import(D->getLocation()), |
| 4271 | Property, |
| 4272 | D->getPropertyImplementation(), |
| 4273 | Ivar, |
| 4274 | Importer.Import(D->getPropertyIvarDeclLoc())); |
| 4275 | ToImpl->setLexicalDeclContext(LexicalDC); |
| 4276 | Importer.Imported(D, ToImpl); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 4277 | LexicalDC->addDeclInternal(ToImpl); |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4278 | } else { |
| 4279 | // Check that we have the same kind of property implementation (@synthesize |
| 4280 | // vs. @dynamic). |
| 4281 | if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) { |
| 4282 | Importer.ToDiag(ToImpl->getLocation(), |
| 4283 | diag::err_odr_objc_property_impl_kind_inconsistent) |
| 4284 | << Property->getDeclName() |
| 4285 | << (ToImpl->getPropertyImplementation() |
| 4286 | == ObjCPropertyImplDecl::Dynamic); |
| 4287 | Importer.FromDiag(D->getLocation(), |
| 4288 | diag::note_odr_objc_property_impl_kind) |
| 4289 | << D->getPropertyDecl()->getDeclName() |
| 4290 | << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4291 | return nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4292 | } |
| 4293 | |
| 4294 | // For @synthesize, check that we have the same |
| 4295 | if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize && |
| 4296 | Ivar != ToImpl->getPropertyIvarDecl()) { |
| 4297 | Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(), |
| 4298 | diag::err_odr_objc_synthesize_ivar_inconsistent) |
| 4299 | << Property->getDeclName() |
| 4300 | << ToImpl->getPropertyIvarDecl()->getDeclName() |
| 4301 | << Ivar->getDeclName(); |
| 4302 | Importer.FromDiag(D->getPropertyIvarDeclLoc(), |
| 4303 | diag::note_odr_objc_synthesize_ivar_here) |
| 4304 | << D->getPropertyIvarDecl()->getDeclName(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4305 | return nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4306 | } |
| 4307 | |
| 4308 | // Merge the existing implementation with the new implementation. |
| 4309 | Importer.Imported(D, ToImpl); |
| 4310 | } |
| 4311 | |
| 4312 | return ToImpl; |
| 4313 | } |
| 4314 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4315 | Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
| 4316 | // For template arguments, we adopt the translation unit as our declaration |
| 4317 | // context. This context will be fixed when the actual template declaration |
| 4318 | // is created. |
| 4319 | |
| 4320 | // FIXME: Import default argument. |
| 4321 | return TemplateTypeParmDecl::Create(Importer.getToContext(), |
| 4322 | Importer.getToContext().getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 4323 | Importer.Import(D->getLocStart()), |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4324 | Importer.Import(D->getLocation()), |
| 4325 | D->getDepth(), |
| 4326 | D->getIndex(), |
| 4327 | Importer.Import(D->getIdentifier()), |
| 4328 | D->wasDeclaredWithTypename(), |
| 4329 | D->isParameterPack()); |
| 4330 | } |
| 4331 | |
| 4332 | Decl * |
| 4333 | ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
| 4334 | // Import the name of this declaration. |
| 4335 | DeclarationName Name = Importer.Import(D->getDeclName()); |
| 4336 | if (D->getDeclName() && !Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4337 | return nullptr; |
| 4338 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4339 | // Import the location of this declaration. |
| 4340 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 4341 | |
| 4342 | // Import the type of this declaration. |
| 4343 | QualType T = Importer.Import(D->getType()); |
| 4344 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4345 | return nullptr; |
| 4346 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4347 | // Import type-source information. |
| 4348 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 4349 | if (D->getTypeSourceInfo() && !TInfo) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4350 | return nullptr; |
| 4351 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4352 | // FIXME: Import default argument. |
| 4353 | |
| 4354 | return NonTypeTemplateParmDecl::Create(Importer.getToContext(), |
| 4355 | Importer.getToContext().getTranslationUnitDecl(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4356 | Importer.Import(D->getInnerLocStart()), |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4357 | Loc, D->getDepth(), D->getPosition(), |
| 4358 | Name.getAsIdentifierInfo(), |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 4359 | T, D->isParameterPack(), TInfo); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4360 | } |
| 4361 | |
| 4362 | Decl * |
| 4363 | ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
| 4364 | // Import the name of this declaration. |
| 4365 | DeclarationName Name = Importer.Import(D->getDeclName()); |
| 4366 | if (D->getDeclName() && !Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4367 | return nullptr; |
| 4368 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4369 | // Import the location of this declaration. |
| 4370 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 4371 | |
| 4372 | // Import template parameters. |
| 4373 | TemplateParameterList *TemplateParams |
| 4374 | = ImportTemplateParameterList(D->getTemplateParameters()); |
| 4375 | if (!TemplateParams) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4376 | return nullptr; |
| 4377 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4378 | // FIXME: Import default argument. |
| 4379 | |
| 4380 | return TemplateTemplateParmDecl::Create(Importer.getToContext(), |
| 4381 | Importer.getToContext().getTranslationUnitDecl(), |
| 4382 | Loc, D->getDepth(), D->getPosition(), |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 4383 | D->isParameterPack(), |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4384 | Name.getAsIdentifierInfo(), |
| 4385 | TemplateParams); |
| 4386 | } |
| 4387 | |
| 4388 | Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 4389 | // If this record has a definition in the translation unit we're coming from, |
| 4390 | // but this particular declaration is not that definition, import the |
| 4391 | // definition and map to that. |
| 4392 | CXXRecordDecl *Definition |
| 4393 | = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition()); |
| 4394 | if (Definition && Definition != D->getTemplatedDecl()) { |
| 4395 | Decl *ImportedDef |
| 4396 | = Importer.Import(Definition->getDescribedClassTemplate()); |
| 4397 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4398 | return nullptr; |
| 4399 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4400 | return Importer.Imported(D, ImportedDef); |
| 4401 | } |
| 4402 | |
| 4403 | // Import the major distinguishing characteristics of this class template. |
| 4404 | DeclContext *DC, *LexicalDC; |
| 4405 | DeclarationName Name; |
| 4406 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4407 | NamedDecl *ToD; |
| 4408 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4409 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4410 | if (ToD) |
| 4411 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4412 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4413 | // We may already have a template of the same name; try to find and match it. |
| 4414 | if (!DC->isFunctionOrMethod()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4415 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4416 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 4417 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 4418 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 4419 | if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4420 | continue; |
| 4421 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 4422 | Decl *Found = FoundDecls[I]; |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4423 | if (ClassTemplateDecl *FoundTemplate |
| 4424 | = dyn_cast<ClassTemplateDecl>(Found)) { |
| 4425 | if (IsStructuralMatch(D, FoundTemplate)) { |
| 4426 | // The class templates structurally match; call it the same template. |
| 4427 | // FIXME: We may be filling in a forward declaration here. Handle |
| 4428 | // this case! |
| 4429 | Importer.Imported(D->getTemplatedDecl(), |
| 4430 | FoundTemplate->getTemplatedDecl()); |
| 4431 | return Importer.Imported(D, FoundTemplate); |
| 4432 | } |
| 4433 | } |
| 4434 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 4435 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4436 | } |
| 4437 | |
| 4438 | if (!ConflictingDecls.empty()) { |
| 4439 | Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary, |
| 4440 | ConflictingDecls.data(), |
| 4441 | ConflictingDecls.size()); |
| 4442 | } |
| 4443 | |
| 4444 | if (!Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4445 | return nullptr; |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4446 | } |
| 4447 | |
| 4448 | CXXRecordDecl *DTemplated = D->getTemplatedDecl(); |
| 4449 | |
| 4450 | // Create the declaration that is being templated. |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 4451 | // Create the declaration that is being templated. |
| 4452 | CXXRecordDecl *D2Templated = cast_or_null<CXXRecordDecl>( |
| 4453 | Importer.Import(DTemplated)); |
| 4454 | if (!D2Templated) |
| 4455 | return nullptr; |
| 4456 | |
| 4457 | // Resolve possible cyclic import. |
| 4458 | if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D)) |
| 4459 | return AlreadyImported; |
| 4460 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4461 | // Create the class template declaration itself. |
| 4462 | TemplateParameterList *TemplateParams |
| 4463 | = ImportTemplateParameterList(D->getTemplateParameters()); |
| 4464 | if (!TemplateParams) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4465 | return nullptr; |
| 4466 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4467 | ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC, |
| 4468 | Loc, Name, TemplateParams, |
| 4469 | D2Templated, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4470 | /*PrevDecl=*/nullptr); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4471 | D2Templated->setDescribedClassTemplate(D2); |
| 4472 | |
| 4473 | D2->setAccess(D->getAccess()); |
| 4474 | D2->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 4475 | LexicalDC->addDeclInternal(D2); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4476 | |
| 4477 | // Note the relationship between the class templates. |
| 4478 | Importer.Imported(D, D2); |
| 4479 | Importer.Imported(DTemplated, D2Templated); |
| 4480 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 4481 | if (DTemplated->isCompleteDefinition() && |
| 4482 | !D2Templated->isCompleteDefinition()) { |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4483 | // FIXME: Import definition! |
| 4484 | } |
| 4485 | |
| 4486 | return D2; |
| 4487 | } |
| 4488 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4489 | Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl( |
| 4490 | ClassTemplateSpecializationDecl *D) { |
| 4491 | // If this record has a definition in the translation unit we're coming from, |
| 4492 | // but this particular declaration is not that definition, import the |
| 4493 | // definition and map to that. |
| 4494 | TagDecl *Definition = D->getDefinition(); |
| 4495 | if (Definition && Definition != D) { |
| 4496 | Decl *ImportedDef = Importer.Import(Definition); |
| 4497 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4498 | return nullptr; |
| 4499 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4500 | return Importer.Imported(D, ImportedDef); |
| 4501 | } |
| 4502 | |
| 4503 | ClassTemplateDecl *ClassTemplate |
| 4504 | = cast_or_null<ClassTemplateDecl>(Importer.Import( |
| 4505 | D->getSpecializedTemplate())); |
| 4506 | if (!ClassTemplate) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4507 | return nullptr; |
| 4508 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4509 | // Import the context of this declaration. |
| 4510 | DeclContext *DC = ClassTemplate->getDeclContext(); |
| 4511 | if (!DC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4512 | return nullptr; |
| 4513 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4514 | DeclContext *LexicalDC = DC; |
| 4515 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 4516 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 4517 | if (!LexicalDC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4518 | return nullptr; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4519 | } |
| 4520 | |
| 4521 | // Import the location of this declaration. |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4522 | SourceLocation StartLoc = Importer.Import(D->getLocStart()); |
| 4523 | SourceLocation IdLoc = Importer.Import(D->getLocation()); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4524 | |
| 4525 | // Import template arguments. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4526 | SmallVector<TemplateArgument, 2> TemplateArgs; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4527 | if (ImportTemplateArguments(D->getTemplateArgs().data(), |
| 4528 | D->getTemplateArgs().size(), |
| 4529 | TemplateArgs)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4530 | return nullptr; |
| 4531 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4532 | // Try to find an existing specialization with these template arguments. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4533 | void *InsertPos = nullptr; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4534 | ClassTemplateSpecializationDecl *D2 |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 4535 | = ClassTemplate->findSpecialization(TemplateArgs, InsertPos); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4536 | if (D2) { |
| 4537 | // We already have a class template specialization with these template |
| 4538 | // arguments. |
| 4539 | |
| 4540 | // FIXME: Check for specialization vs. instantiation errors. |
| 4541 | |
| 4542 | if (RecordDecl *FoundDef = D2->getDefinition()) { |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 4543 | if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) { |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4544 | // The record types structurally match, or the "from" translation |
| 4545 | // unit only had a forward declaration anyway; call it the same |
| 4546 | // function. |
| 4547 | return Importer.Imported(D, FoundDef); |
| 4548 | } |
| 4549 | } |
| 4550 | } else { |
| 4551 | // Create a new specialization. |
| 4552 | D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(), |
| 4553 | D->getTagKind(), DC, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4554 | StartLoc, IdLoc, |
| 4555 | ClassTemplate, |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4556 | TemplateArgs.data(), |
| 4557 | TemplateArgs.size(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4558 | /*PrevDecl=*/nullptr); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4559 | D2->setSpecializationKind(D->getSpecializationKind()); |
| 4560 | |
| 4561 | // Add this specialization to the class template. |
| 4562 | ClassTemplate->AddSpecialization(D2, InsertPos); |
| 4563 | |
| 4564 | // Import the qualifier, if any. |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4565 | D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4566 | |
| 4567 | // Add the specialization to this context. |
| 4568 | D2->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 4569 | LexicalDC->addDeclInternal(D2); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4570 | } |
| 4571 | Importer.Imported(D, D2); |
| 4572 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 4573 | if (D->isCompleteDefinition() && ImportDefinition(D, D2)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4574 | return nullptr; |
| 4575 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4576 | return D2; |
| 4577 | } |
| 4578 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4579 | Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) { |
| 4580 | // If this variable has a definition in the translation unit we're coming |
| 4581 | // from, |
| 4582 | // but this particular declaration is not that definition, import the |
| 4583 | // definition and map to that. |
| 4584 | VarDecl *Definition = |
| 4585 | cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition()); |
| 4586 | if (Definition && Definition != D->getTemplatedDecl()) { |
| 4587 | Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate()); |
| 4588 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4589 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4590 | |
| 4591 | return Importer.Imported(D, ImportedDef); |
| 4592 | } |
| 4593 | |
| 4594 | // Import the major distinguishing characteristics of this variable template. |
| 4595 | DeclContext *DC, *LexicalDC; |
| 4596 | DeclarationName Name; |
| 4597 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4598 | NamedDecl *ToD; |
| 4599 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4600 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4601 | if (ToD) |
| 4602 | return ToD; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4603 | |
| 4604 | // We may already have a template of the same name; try to find and match it. |
| 4605 | assert(!DC->isFunctionOrMethod() && |
| 4606 | "Variable templates cannot be declared at function scope"); |
| 4607 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 4608 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 4609 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4610 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 4611 | if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
| 4612 | continue; |
| 4613 | |
| 4614 | Decl *Found = FoundDecls[I]; |
| 4615 | if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) { |
| 4616 | if (IsStructuralMatch(D, FoundTemplate)) { |
| 4617 | // The variable templates structurally match; call it the same template. |
| 4618 | Importer.Imported(D->getTemplatedDecl(), |
| 4619 | FoundTemplate->getTemplatedDecl()); |
| 4620 | return Importer.Imported(D, FoundTemplate); |
| 4621 | } |
| 4622 | } |
| 4623 | |
| 4624 | ConflictingDecls.push_back(FoundDecls[I]); |
| 4625 | } |
| 4626 | |
| 4627 | if (!ConflictingDecls.empty()) { |
| 4628 | Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary, |
| 4629 | ConflictingDecls.data(), |
| 4630 | ConflictingDecls.size()); |
| 4631 | } |
| 4632 | |
| 4633 | if (!Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4634 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4635 | |
| 4636 | VarDecl *DTemplated = D->getTemplatedDecl(); |
| 4637 | |
| 4638 | // Import the type. |
| 4639 | QualType T = Importer.Import(DTemplated->getType()); |
| 4640 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4641 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4642 | |
| 4643 | // Create the declaration that is being templated. |
| 4644 | SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart()); |
| 4645 | SourceLocation IdLoc = Importer.Import(DTemplated->getLocation()); |
| 4646 | TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo()); |
| 4647 | VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc, |
| 4648 | IdLoc, Name.getAsIdentifierInfo(), T, |
| 4649 | TInfo, DTemplated->getStorageClass()); |
| 4650 | D2Templated->setAccess(DTemplated->getAccess()); |
| 4651 | D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc())); |
| 4652 | D2Templated->setLexicalDeclContext(LexicalDC); |
| 4653 | |
| 4654 | // Importer.Imported(DTemplated, D2Templated); |
| 4655 | // LexicalDC->addDeclInternal(D2Templated); |
| 4656 | |
| 4657 | // Merge the initializer. |
| 4658 | if (ImportDefinition(DTemplated, D2Templated)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4659 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4660 | |
| 4661 | // Create the variable template declaration itself. |
| 4662 | TemplateParameterList *TemplateParams = |
| 4663 | ImportTemplateParameterList(D->getTemplateParameters()); |
| 4664 | if (!TemplateParams) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4665 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4666 | |
| 4667 | VarTemplateDecl *D2 = VarTemplateDecl::Create( |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 4668 | Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4669 | D2Templated->setDescribedVarTemplate(D2); |
| 4670 | |
| 4671 | D2->setAccess(D->getAccess()); |
| 4672 | D2->setLexicalDeclContext(LexicalDC); |
| 4673 | LexicalDC->addDeclInternal(D2); |
| 4674 | |
| 4675 | // Note the relationship between the variable templates. |
| 4676 | Importer.Imported(D, D2); |
| 4677 | Importer.Imported(DTemplated, D2Templated); |
| 4678 | |
| 4679 | if (DTemplated->isThisDeclarationADefinition() && |
| 4680 | !D2Templated->isThisDeclarationADefinition()) { |
| 4681 | // FIXME: Import definition! |
| 4682 | } |
| 4683 | |
| 4684 | return D2; |
| 4685 | } |
| 4686 | |
| 4687 | Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl( |
| 4688 | VarTemplateSpecializationDecl *D) { |
| 4689 | // If this record has a definition in the translation unit we're coming from, |
| 4690 | // but this particular declaration is not that definition, import the |
| 4691 | // definition and map to that. |
| 4692 | VarDecl *Definition = D->getDefinition(); |
| 4693 | if (Definition && Definition != D) { |
| 4694 | Decl *ImportedDef = Importer.Import(Definition); |
| 4695 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4696 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4697 | |
| 4698 | return Importer.Imported(D, ImportedDef); |
| 4699 | } |
| 4700 | |
| 4701 | VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>( |
| 4702 | Importer.Import(D->getSpecializedTemplate())); |
| 4703 | if (!VarTemplate) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4704 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4705 | |
| 4706 | // Import the context of this declaration. |
| 4707 | DeclContext *DC = VarTemplate->getDeclContext(); |
| 4708 | if (!DC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4709 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4710 | |
| 4711 | DeclContext *LexicalDC = DC; |
| 4712 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 4713 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 4714 | if (!LexicalDC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4715 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4716 | } |
| 4717 | |
| 4718 | // Import the location of this declaration. |
| 4719 | SourceLocation StartLoc = Importer.Import(D->getLocStart()); |
| 4720 | SourceLocation IdLoc = Importer.Import(D->getLocation()); |
| 4721 | |
| 4722 | // Import template arguments. |
| 4723 | SmallVector<TemplateArgument, 2> TemplateArgs; |
| 4724 | if (ImportTemplateArguments(D->getTemplateArgs().data(), |
| 4725 | D->getTemplateArgs().size(), TemplateArgs)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4726 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4727 | |
| 4728 | // Try to find an existing specialization with these template arguments. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4729 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4730 | VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization( |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 4731 | TemplateArgs, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4732 | if (D2) { |
| 4733 | // We already have a variable template specialization with these template |
| 4734 | // arguments. |
| 4735 | |
| 4736 | // FIXME: Check for specialization vs. instantiation errors. |
| 4737 | |
| 4738 | if (VarDecl *FoundDef = D2->getDefinition()) { |
| 4739 | if (!D->isThisDeclarationADefinition() || |
| 4740 | IsStructuralMatch(D, FoundDef)) { |
| 4741 | // The record types structurally match, or the "from" translation |
| 4742 | // unit only had a forward declaration anyway; call it the same |
| 4743 | // variable. |
| 4744 | return Importer.Imported(D, FoundDef); |
| 4745 | } |
| 4746 | } |
| 4747 | } else { |
| 4748 | |
| 4749 | // Import the type. |
| 4750 | QualType T = Importer.Import(D->getType()); |
| 4751 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4752 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4753 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 4754 | |
| 4755 | // Create a new specialization. |
| 4756 | D2 = VarTemplateSpecializationDecl::Create( |
| 4757 | Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo, |
| 4758 | D->getStorageClass(), TemplateArgs.data(), TemplateArgs.size()); |
| 4759 | D2->setSpecializationKind(D->getSpecializationKind()); |
| 4760 | D2->setTemplateArgsInfo(D->getTemplateArgsInfo()); |
| 4761 | |
| 4762 | // Add this specialization to the class template. |
| 4763 | VarTemplate->AddSpecialization(D2, InsertPos); |
| 4764 | |
| 4765 | // Import the qualifier, if any. |
| 4766 | D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
| 4767 | |
| 4768 | // Add the specialization to this context. |
| 4769 | D2->setLexicalDeclContext(LexicalDC); |
| 4770 | LexicalDC->addDeclInternal(D2); |
| 4771 | } |
| 4772 | Importer.Imported(D, D2); |
| 4773 | |
| 4774 | if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4775 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4776 | |
| 4777 | return D2; |
| 4778 | } |
| 4779 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 4780 | //---------------------------------------------------------------------------- |
| 4781 | // Import Statements |
| 4782 | //---------------------------------------------------------------------------- |
| 4783 | |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4784 | DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) { |
| 4785 | if (DG.isNull()) |
| 4786 | return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0); |
| 4787 | size_t NumDecls = DG.end() - DG.begin(); |
| 4788 | SmallVector<Decl *, 1> ToDecls(NumDecls); |
| 4789 | auto &_Importer = this->Importer; |
| 4790 | std::transform(DG.begin(), DG.end(), ToDecls.begin(), |
| 4791 | [&_Importer](Decl *D) -> Decl * { |
| 4792 | return _Importer.Import(D); |
| 4793 | }); |
| 4794 | return DeclGroupRef::Create(Importer.getToContext(), |
| 4795 | ToDecls.begin(), |
| 4796 | NumDecls); |
| 4797 | } |
| 4798 | |
| 4799 | Stmt *ASTNodeImporter::VisitStmt(Stmt *S) { |
| 4800 | Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node) |
| 4801 | << S->getStmtClassName(); |
| 4802 | return nullptr; |
| 4803 | } |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 4804 | |
| 4805 | |
| 4806 | Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) { |
| 4807 | SmallVector<IdentifierInfo *, 4> Names; |
| 4808 | for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) { |
| 4809 | IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I)); |
| 4810 | if (!ToII) |
| 4811 | return nullptr; |
| 4812 | Names.push_back(ToII); |
| 4813 | } |
| 4814 | for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) { |
| 4815 | IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I)); |
| 4816 | if (!ToII) |
| 4817 | return nullptr; |
| 4818 | Names.push_back(ToII); |
| 4819 | } |
| 4820 | |
| 4821 | SmallVector<StringLiteral *, 4> Clobbers; |
| 4822 | for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) { |
| 4823 | StringLiteral *Clobber = cast_or_null<StringLiteral>( |
| 4824 | Importer.Import(S->getClobberStringLiteral(I))); |
| 4825 | if (!Clobber) |
| 4826 | return nullptr; |
| 4827 | Clobbers.push_back(Clobber); |
| 4828 | } |
| 4829 | |
| 4830 | SmallVector<StringLiteral *, 4> Constraints; |
| 4831 | for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) { |
| 4832 | StringLiteral *Output = cast_or_null<StringLiteral>( |
| 4833 | Importer.Import(S->getOutputConstraintLiteral(I))); |
| 4834 | if (!Output) |
| 4835 | return nullptr; |
| 4836 | Constraints.push_back(Output); |
| 4837 | } |
| 4838 | |
| 4839 | for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) { |
| 4840 | StringLiteral *Input = cast_or_null<StringLiteral>( |
| 4841 | Importer.Import(S->getInputConstraintLiteral(I))); |
| 4842 | if (!Input) |
| 4843 | return nullptr; |
| 4844 | Constraints.push_back(Input); |
| 4845 | } |
| 4846 | |
| 4847 | SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs()); |
| 4848 | if (ImportArrayChecked(S->begin_outputs(), S->end_outputs(), Exprs.begin())) |
| 4849 | return nullptr; |
| 4850 | |
| 4851 | if (ImportArrayChecked(S->begin_inputs(), S->end_inputs(), |
| 4852 | Exprs.begin() + S->getNumOutputs())) |
| 4853 | return nullptr; |
| 4854 | |
| 4855 | StringLiteral *AsmStr = cast_or_null<StringLiteral>( |
| 4856 | Importer.Import(S->getAsmString())); |
| 4857 | if (!AsmStr) |
| 4858 | return nullptr; |
| 4859 | |
| 4860 | return new (Importer.getToContext()) GCCAsmStmt( |
| 4861 | Importer.getToContext(), |
| 4862 | Importer.Import(S->getAsmLoc()), |
| 4863 | S->isSimple(), |
| 4864 | S->isVolatile(), |
| 4865 | S->getNumOutputs(), |
| 4866 | S->getNumInputs(), |
| 4867 | Names.data(), |
| 4868 | Constraints.data(), |
| 4869 | Exprs.data(), |
| 4870 | AsmStr, |
| 4871 | S->getNumClobbers(), |
| 4872 | Clobbers.data(), |
| 4873 | Importer.Import(S->getRParenLoc())); |
| 4874 | } |
| 4875 | |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4876 | Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) { |
| 4877 | DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup()); |
| 4878 | for (Decl *ToD : ToDG) { |
| 4879 | if (!ToD) |
| 4880 | return nullptr; |
| 4881 | } |
| 4882 | SourceLocation ToStartLoc = Importer.Import(S->getStartLoc()); |
| 4883 | SourceLocation ToEndLoc = Importer.Import(S->getEndLoc()); |
| 4884 | return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc); |
| 4885 | } |
| 4886 | |
| 4887 | Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) { |
| 4888 | SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc()); |
| 4889 | return new (Importer.getToContext()) NullStmt(ToSemiLoc, |
| 4890 | S->hasLeadingEmptyMacro()); |
| 4891 | } |
| 4892 | |
| 4893 | Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) { |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 4894 | llvm::SmallVector<Stmt *, 8> ToStmts(S->size()); |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 4895 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 4896 | if (ImportArrayChecked(S->body_begin(), S->body_end(), ToStmts.begin())) |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 4897 | return nullptr; |
| 4898 | |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4899 | SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc()); |
| 4900 | SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc()); |
| 4901 | return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(), |
| 4902 | ToStmts, |
| 4903 | ToLBraceLoc, ToRBraceLoc); |
| 4904 | } |
| 4905 | |
| 4906 | Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) { |
| 4907 | Expr *ToLHS = Importer.Import(S->getLHS()); |
| 4908 | if (!ToLHS) |
| 4909 | return nullptr; |
| 4910 | Expr *ToRHS = Importer.Import(S->getRHS()); |
| 4911 | if (!ToRHS && S->getRHS()) |
| 4912 | return nullptr; |
| 4913 | SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc()); |
| 4914 | SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc()); |
| 4915 | SourceLocation ToColonLoc = Importer.Import(S->getColonLoc()); |
| 4916 | return new (Importer.getToContext()) CaseStmt(ToLHS, ToRHS, |
| 4917 | ToCaseLoc, ToEllipsisLoc, |
| 4918 | ToColonLoc); |
| 4919 | } |
| 4920 | |
| 4921 | Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) { |
| 4922 | SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc()); |
| 4923 | SourceLocation ToColonLoc = Importer.Import(S->getColonLoc()); |
| 4924 | Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); |
| 4925 | if (!ToSubStmt && S->getSubStmt()) |
| 4926 | return nullptr; |
| 4927 | return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc, |
| 4928 | ToSubStmt); |
| 4929 | } |
| 4930 | |
| 4931 | Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) { |
| 4932 | SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc()); |
| 4933 | LabelDecl *ToLabelDecl = |
| 4934 | cast_or_null<LabelDecl>(Importer.Import(S->getDecl())); |
| 4935 | if (!ToLabelDecl && S->getDecl()) |
| 4936 | return nullptr; |
| 4937 | Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); |
| 4938 | if (!ToSubStmt && S->getSubStmt()) |
| 4939 | return nullptr; |
| 4940 | return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl, |
| 4941 | ToSubStmt); |
| 4942 | } |
| 4943 | |
| 4944 | Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) { |
| 4945 | SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc()); |
| 4946 | ArrayRef<const Attr*> FromAttrs(S->getAttrs()); |
| 4947 | SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size()); |
| 4948 | ASTContext &_ToContext = Importer.getToContext(); |
| 4949 | std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(), |
| 4950 | [&_ToContext](const Attr *A) -> const Attr * { |
| 4951 | return A->clone(_ToContext); |
| 4952 | }); |
| 4953 | for (const Attr *ToA : ToAttrs) { |
| 4954 | if (!ToA) |
| 4955 | return nullptr; |
| 4956 | } |
| 4957 | Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); |
| 4958 | if (!ToSubStmt && S->getSubStmt()) |
| 4959 | return nullptr; |
| 4960 | return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc, |
| 4961 | ToAttrs, ToSubStmt); |
| 4962 | } |
| 4963 | |
| 4964 | Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) { |
| 4965 | SourceLocation ToIfLoc = Importer.Import(S->getIfLoc()); |
| 4966 | VarDecl *ToConditionVariable = nullptr; |
| 4967 | if (VarDecl *FromConditionVariable = S->getConditionVariable()) { |
| 4968 | ToConditionVariable = |
| 4969 | dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); |
| 4970 | if (!ToConditionVariable) |
| 4971 | return nullptr; |
| 4972 | } |
| 4973 | Expr *ToCondition = Importer.Import(S->getCond()); |
| 4974 | if (!ToCondition && S->getCond()) |
| 4975 | return nullptr; |
| 4976 | Stmt *ToThenStmt = Importer.Import(S->getThen()); |
| 4977 | if (!ToThenStmt && S->getThen()) |
| 4978 | return nullptr; |
| 4979 | SourceLocation ToElseLoc = Importer.Import(S->getElseLoc()); |
| 4980 | Stmt *ToElseStmt = Importer.Import(S->getElse()); |
| 4981 | if (!ToElseStmt && S->getElse()) |
| 4982 | return nullptr; |
| 4983 | return new (Importer.getToContext()) IfStmt(Importer.getToContext(), |
| 4984 | ToIfLoc, ToConditionVariable, |
| 4985 | ToCondition, ToThenStmt, |
| 4986 | ToElseLoc, ToElseStmt); |
| 4987 | } |
| 4988 | |
| 4989 | Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) { |
| 4990 | VarDecl *ToConditionVariable = nullptr; |
| 4991 | if (VarDecl *FromConditionVariable = S->getConditionVariable()) { |
| 4992 | ToConditionVariable = |
| 4993 | dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); |
| 4994 | if (!ToConditionVariable) |
| 4995 | return nullptr; |
| 4996 | } |
| 4997 | Expr *ToCondition = Importer.Import(S->getCond()); |
| 4998 | if (!ToCondition && S->getCond()) |
| 4999 | return nullptr; |
| 5000 | SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt( |
| 5001 | Importer.getToContext(), ToConditionVariable, |
| 5002 | ToCondition); |
| 5003 | Stmt *ToBody = Importer.Import(S->getBody()); |
| 5004 | if (!ToBody && S->getBody()) |
| 5005 | return nullptr; |
| 5006 | ToStmt->setBody(ToBody); |
| 5007 | ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc())); |
| 5008 | // Now we have to re-chain the cases. |
| 5009 | SwitchCase *LastChainedSwitchCase = nullptr; |
| 5010 | for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr; |
| 5011 | SC = SC->getNextSwitchCase()) { |
| 5012 | SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC)); |
| 5013 | if (!ToSC) |
| 5014 | return nullptr; |
| 5015 | if (LastChainedSwitchCase) |
| 5016 | LastChainedSwitchCase->setNextSwitchCase(ToSC); |
| 5017 | else |
| 5018 | ToStmt->setSwitchCaseList(ToSC); |
| 5019 | LastChainedSwitchCase = ToSC; |
| 5020 | } |
| 5021 | return ToStmt; |
| 5022 | } |
| 5023 | |
| 5024 | Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) { |
| 5025 | VarDecl *ToConditionVariable = nullptr; |
| 5026 | if (VarDecl *FromConditionVariable = S->getConditionVariable()) { |
| 5027 | ToConditionVariable = |
| 5028 | dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); |
| 5029 | if (!ToConditionVariable) |
| 5030 | return nullptr; |
| 5031 | } |
| 5032 | Expr *ToCondition = Importer.Import(S->getCond()); |
| 5033 | if (!ToCondition && S->getCond()) |
| 5034 | return nullptr; |
| 5035 | Stmt *ToBody = Importer.Import(S->getBody()); |
| 5036 | if (!ToBody && S->getBody()) |
| 5037 | return nullptr; |
| 5038 | SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc()); |
| 5039 | return new (Importer.getToContext()) WhileStmt(Importer.getToContext(), |
| 5040 | ToConditionVariable, |
| 5041 | ToCondition, ToBody, |
| 5042 | ToWhileLoc); |
| 5043 | } |
| 5044 | |
| 5045 | Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) { |
| 5046 | Stmt *ToBody = Importer.Import(S->getBody()); |
| 5047 | if (!ToBody && S->getBody()) |
| 5048 | return nullptr; |
| 5049 | Expr *ToCondition = Importer.Import(S->getCond()); |
| 5050 | if (!ToCondition && S->getCond()) |
| 5051 | return nullptr; |
| 5052 | SourceLocation ToDoLoc = Importer.Import(S->getDoLoc()); |
| 5053 | SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc()); |
| 5054 | SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); |
| 5055 | return new (Importer.getToContext()) DoStmt(ToBody, ToCondition, |
| 5056 | ToDoLoc, ToWhileLoc, |
| 5057 | ToRParenLoc); |
| 5058 | } |
| 5059 | |
| 5060 | Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) { |
| 5061 | Stmt *ToInit = Importer.Import(S->getInit()); |
| 5062 | if (!ToInit && S->getInit()) |
| 5063 | return nullptr; |
| 5064 | Expr *ToCondition = Importer.Import(S->getCond()); |
| 5065 | if (!ToCondition && S->getCond()) |
| 5066 | return nullptr; |
| 5067 | VarDecl *ToConditionVariable = nullptr; |
| 5068 | if (VarDecl *FromConditionVariable = S->getConditionVariable()) { |
| 5069 | ToConditionVariable = |
| 5070 | dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); |
| 5071 | if (!ToConditionVariable) |
| 5072 | return nullptr; |
| 5073 | } |
| 5074 | Expr *ToInc = Importer.Import(S->getInc()); |
| 5075 | if (!ToInc && S->getInc()) |
| 5076 | return nullptr; |
| 5077 | Stmt *ToBody = Importer.Import(S->getBody()); |
| 5078 | if (!ToBody && S->getBody()) |
| 5079 | return nullptr; |
| 5080 | SourceLocation ToForLoc = Importer.Import(S->getForLoc()); |
| 5081 | SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc()); |
| 5082 | SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); |
| 5083 | return new (Importer.getToContext()) ForStmt(Importer.getToContext(), |
| 5084 | ToInit, ToCondition, |
| 5085 | ToConditionVariable, |
| 5086 | ToInc, ToBody, |
| 5087 | ToForLoc, ToLParenLoc, |
| 5088 | ToRParenLoc); |
| 5089 | } |
| 5090 | |
| 5091 | Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) { |
| 5092 | LabelDecl *ToLabel = nullptr; |
| 5093 | if (LabelDecl *FromLabel = S->getLabel()) { |
| 5094 | ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel)); |
| 5095 | if (!ToLabel) |
| 5096 | return nullptr; |
| 5097 | } |
| 5098 | SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc()); |
| 5099 | SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc()); |
| 5100 | return new (Importer.getToContext()) GotoStmt(ToLabel, |
| 5101 | ToGotoLoc, ToLabelLoc); |
| 5102 | } |
| 5103 | |
| 5104 | Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) { |
| 5105 | SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc()); |
| 5106 | SourceLocation ToStarLoc = Importer.Import(S->getStarLoc()); |
| 5107 | Expr *ToTarget = Importer.Import(S->getTarget()); |
| 5108 | if (!ToTarget && S->getTarget()) |
| 5109 | return nullptr; |
| 5110 | return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc, |
| 5111 | ToTarget); |
| 5112 | } |
| 5113 | |
| 5114 | Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) { |
| 5115 | SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc()); |
| 5116 | return new (Importer.getToContext()) ContinueStmt(ToContinueLoc); |
| 5117 | } |
| 5118 | |
| 5119 | Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) { |
| 5120 | SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc()); |
| 5121 | return new (Importer.getToContext()) BreakStmt(ToBreakLoc); |
| 5122 | } |
| 5123 | |
| 5124 | Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) { |
| 5125 | SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc()); |
| 5126 | Expr *ToRetExpr = Importer.Import(S->getRetValue()); |
| 5127 | if (!ToRetExpr && S->getRetValue()) |
| 5128 | return nullptr; |
| 5129 | VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate()); |
| 5130 | VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate)); |
| 5131 | if (!ToNRVOCandidate && NRVOCandidate) |
| 5132 | return nullptr; |
| 5133 | return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr, |
| 5134 | ToNRVOCandidate); |
| 5135 | } |
| 5136 | |
| 5137 | Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) { |
| 5138 | SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc()); |
| 5139 | VarDecl *ToExceptionDecl = nullptr; |
| 5140 | if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) { |
| 5141 | ToExceptionDecl = |
| 5142 | dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl)); |
| 5143 | if (!ToExceptionDecl) |
| 5144 | return nullptr; |
| 5145 | } |
| 5146 | Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock()); |
| 5147 | if (!ToHandlerBlock && S->getHandlerBlock()) |
| 5148 | return nullptr; |
| 5149 | return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc, |
| 5150 | ToExceptionDecl, |
| 5151 | ToHandlerBlock); |
| 5152 | } |
| 5153 | |
| 5154 | Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) { |
| 5155 | SourceLocation ToTryLoc = Importer.Import(S->getTryLoc()); |
| 5156 | Stmt *ToTryBlock = Importer.Import(S->getTryBlock()); |
| 5157 | if (!ToTryBlock && S->getTryBlock()) |
| 5158 | return nullptr; |
| 5159 | SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers()); |
| 5160 | for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) { |
| 5161 | CXXCatchStmt *FromHandler = S->getHandler(HI); |
| 5162 | if (Stmt *ToHandler = Importer.Import(FromHandler)) |
| 5163 | ToHandlers[HI] = ToHandler; |
| 5164 | else |
| 5165 | return nullptr; |
| 5166 | } |
| 5167 | return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock, |
| 5168 | ToHandlers); |
| 5169 | } |
| 5170 | |
| 5171 | Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) { |
| 5172 | DeclStmt *ToRange = |
| 5173 | dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt())); |
| 5174 | if (!ToRange && S->getRangeStmt()) |
| 5175 | return nullptr; |
Richard Smith | 01694c3 | 2016-03-20 10:33:40 +0000 | [diff] [blame] | 5176 | DeclStmt *ToBegin = |
| 5177 | dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt())); |
| 5178 | if (!ToBegin && S->getBeginStmt()) |
| 5179 | return nullptr; |
| 5180 | DeclStmt *ToEnd = |
| 5181 | dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt())); |
| 5182 | if (!ToEnd && S->getEndStmt()) |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 5183 | return nullptr; |
| 5184 | Expr *ToCond = Importer.Import(S->getCond()); |
| 5185 | if (!ToCond && S->getCond()) |
| 5186 | return nullptr; |
| 5187 | Expr *ToInc = Importer.Import(S->getInc()); |
| 5188 | if (!ToInc && S->getInc()) |
| 5189 | return nullptr; |
| 5190 | DeclStmt *ToLoopVar = |
| 5191 | dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt())); |
| 5192 | if (!ToLoopVar && S->getLoopVarStmt()) |
| 5193 | return nullptr; |
| 5194 | Stmt *ToBody = Importer.Import(S->getBody()); |
| 5195 | if (!ToBody && S->getBody()) |
| 5196 | return nullptr; |
| 5197 | SourceLocation ToForLoc = Importer.Import(S->getForLoc()); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 5198 | SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc()); |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 5199 | SourceLocation ToColonLoc = Importer.Import(S->getColonLoc()); |
| 5200 | SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); |
Richard Smith | 01694c3 | 2016-03-20 10:33:40 +0000 | [diff] [blame] | 5201 | return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd, |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 5202 | ToCond, ToInc, |
| 5203 | ToLoopVar, ToBody, |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 5204 | ToForLoc, ToCoawaitLoc, |
| 5205 | ToColonLoc, ToRParenLoc); |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 5206 | } |
| 5207 | |
| 5208 | Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) { |
| 5209 | Stmt *ToElem = Importer.Import(S->getElement()); |
| 5210 | if (!ToElem && S->getElement()) |
| 5211 | return nullptr; |
| 5212 | Expr *ToCollect = Importer.Import(S->getCollection()); |
| 5213 | if (!ToCollect && S->getCollection()) |
| 5214 | return nullptr; |
| 5215 | Stmt *ToBody = Importer.Import(S->getBody()); |
| 5216 | if (!ToBody && S->getBody()) |
| 5217 | return nullptr; |
| 5218 | SourceLocation ToForLoc = Importer.Import(S->getForLoc()); |
| 5219 | SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); |
| 5220 | return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem, |
| 5221 | ToCollect, |
| 5222 | ToBody, ToForLoc, |
| 5223 | ToRParenLoc); |
| 5224 | } |
| 5225 | |
| 5226 | Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) { |
| 5227 | SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc()); |
| 5228 | SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); |
| 5229 | VarDecl *ToExceptionDecl = nullptr; |
| 5230 | if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) { |
| 5231 | ToExceptionDecl = |
| 5232 | dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl)); |
| 5233 | if (!ToExceptionDecl) |
| 5234 | return nullptr; |
| 5235 | } |
| 5236 | Stmt *ToBody = Importer.Import(S->getCatchBody()); |
| 5237 | if (!ToBody && S->getCatchBody()) |
| 5238 | return nullptr; |
| 5239 | return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc, |
| 5240 | ToRParenLoc, |
| 5241 | ToExceptionDecl, |
| 5242 | ToBody); |
| 5243 | } |
| 5244 | |
| 5245 | Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) { |
| 5246 | SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc()); |
| 5247 | Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody()); |
| 5248 | if (!ToAtFinallyStmt && S->getFinallyBody()) |
| 5249 | return nullptr; |
| 5250 | return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc, |
| 5251 | ToAtFinallyStmt); |
| 5252 | } |
| 5253 | |
| 5254 | Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) { |
| 5255 | SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc()); |
| 5256 | Stmt *ToAtTryStmt = Importer.Import(S->getTryBody()); |
| 5257 | if (!ToAtTryStmt && S->getTryBody()) |
| 5258 | return nullptr; |
| 5259 | SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts()); |
| 5260 | for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) { |
| 5261 | ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI); |
| 5262 | if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt)) |
| 5263 | ToCatchStmts[CI] = ToCatchStmt; |
| 5264 | else |
| 5265 | return nullptr; |
| 5266 | } |
| 5267 | Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt()); |
| 5268 | if (!ToAtFinallyStmt && S->getFinallyStmt()) |
| 5269 | return nullptr; |
| 5270 | return ObjCAtTryStmt::Create(Importer.getToContext(), |
| 5271 | ToAtTryLoc, ToAtTryStmt, |
| 5272 | ToCatchStmts.begin(), ToCatchStmts.size(), |
| 5273 | ToAtFinallyStmt); |
| 5274 | } |
| 5275 | |
| 5276 | Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt |
| 5277 | (ObjCAtSynchronizedStmt *S) { |
| 5278 | SourceLocation ToAtSynchronizedLoc = |
| 5279 | Importer.Import(S->getAtSynchronizedLoc()); |
| 5280 | Expr *ToSynchExpr = Importer.Import(S->getSynchExpr()); |
| 5281 | if (!ToSynchExpr && S->getSynchExpr()) |
| 5282 | return nullptr; |
| 5283 | Stmt *ToSynchBody = Importer.Import(S->getSynchBody()); |
| 5284 | if (!ToSynchBody && S->getSynchBody()) |
| 5285 | return nullptr; |
| 5286 | return new (Importer.getToContext()) ObjCAtSynchronizedStmt( |
| 5287 | ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody); |
| 5288 | } |
| 5289 | |
| 5290 | Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) { |
| 5291 | SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc()); |
| 5292 | Expr *ToThrow = Importer.Import(S->getThrowExpr()); |
| 5293 | if (!ToThrow && S->getThrowExpr()) |
| 5294 | return nullptr; |
| 5295 | return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow); |
| 5296 | } |
| 5297 | |
| 5298 | Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt |
| 5299 | (ObjCAutoreleasePoolStmt *S) { |
| 5300 | SourceLocation ToAtLoc = Importer.Import(S->getAtLoc()); |
| 5301 | Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); |
| 5302 | if (!ToSubStmt && S->getSubStmt()) |
| 5303 | return nullptr; |
| 5304 | return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc, |
| 5305 | ToSubStmt); |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 5306 | } |
| 5307 | |
| 5308 | //---------------------------------------------------------------------------- |
| 5309 | // Import Expressions |
| 5310 | //---------------------------------------------------------------------------- |
| 5311 | Expr *ASTNodeImporter::VisitExpr(Expr *E) { |
| 5312 | Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node) |
| 5313 | << E->getStmtClassName(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5314 | return nullptr; |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 5315 | } |
| 5316 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5317 | Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) { |
| 5318 | QualType T = Importer.Import(E->getType()); |
| 5319 | if (T.isNull()) |
| 5320 | return nullptr; |
| 5321 | |
| 5322 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 5323 | if (!SubExpr && E->getSubExpr()) |
| 5324 | return nullptr; |
| 5325 | |
| 5326 | TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo()); |
| 5327 | if (!TInfo) |
| 5328 | return nullptr; |
| 5329 | |
| 5330 | return new (Importer.getToContext()) VAArgExpr( |
| 5331 | Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo, |
| 5332 | Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI()); |
| 5333 | } |
| 5334 | |
| 5335 | |
| 5336 | Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) { |
| 5337 | QualType T = Importer.Import(E->getType()); |
| 5338 | if (T.isNull()) |
| 5339 | return nullptr; |
| 5340 | |
| 5341 | return new (Importer.getToContext()) GNUNullExpr( |
| 5342 | T, Importer.Import(E->getExprLoc())); |
| 5343 | } |
| 5344 | |
| 5345 | Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) { |
| 5346 | QualType T = Importer.Import(E->getType()); |
| 5347 | if (T.isNull()) |
| 5348 | return nullptr; |
| 5349 | |
| 5350 | StringLiteral *SL = cast_or_null<StringLiteral>( |
| 5351 | Importer.Import(E->getFunctionName())); |
| 5352 | if (!SL && E->getFunctionName()) |
| 5353 | return nullptr; |
| 5354 | |
| 5355 | return new (Importer.getToContext()) PredefinedExpr( |
| 5356 | Importer.Import(E->getExprLoc()), T, E->getIdentType(), SL); |
| 5357 | } |
| 5358 | |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 5359 | Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) { |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 5360 | ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl())); |
| 5361 | if (!ToD) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5362 | return nullptr; |
Chandler Carruth | 8d26bb0 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 5363 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5364 | NamedDecl *FoundD = nullptr; |
Chandler Carruth | 8d26bb0 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 5365 | if (E->getDecl() != E->getFoundDecl()) { |
| 5366 | FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl())); |
| 5367 | if (!FoundD) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5368 | return nullptr; |
Chandler Carruth | 8d26bb0 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 5369 | } |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 5370 | |
| 5371 | QualType T = Importer.Import(E->getType()); |
| 5372 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5373 | return nullptr; |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5374 | |
| 5375 | DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(), |
| 5376 | Importer.Import(E->getQualifierLoc()), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 5377 | Importer.Import(E->getTemplateKeywordLoc()), |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5378 | ToD, |
Alexey Bataev | 19acc3d | 2015-01-12 10:17:46 +0000 | [diff] [blame] | 5379 | E->refersToEnclosingVariableOrCapture(), |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5380 | Importer.Import(E->getLocation()), |
| 5381 | T, E->getValueKind(), |
| 5382 | FoundD, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5383 | /*FIXME:TemplateArgs=*/nullptr); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5384 | if (E->hadMultipleCandidates()) |
| 5385 | DRE->setHadMultipleCandidates(true); |
| 5386 | return DRE; |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 5387 | } |
| 5388 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5389 | Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { |
| 5390 | QualType T = Importer.Import(E->getType()); |
| 5391 | if (T.isNull()) |
| 5392 | return NULL; |
| 5393 | |
| 5394 | return new (Importer.getToContext()) ImplicitValueInitExpr(T); |
| 5395 | } |
| 5396 | |
| 5397 | ASTNodeImporter::Designator |
| 5398 | ASTNodeImporter::ImportDesignator(const Designator &D) { |
| 5399 | if (D.isFieldDesignator()) { |
| 5400 | IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName()); |
| 5401 | // Caller checks for import error |
| 5402 | return Designator(ToFieldName, Importer.Import(D.getDotLoc()), |
| 5403 | Importer.Import(D.getFieldLoc())); |
| 5404 | } |
| 5405 | if (D.isArrayDesignator()) |
| 5406 | return Designator(D.getFirstExprIndex(), |
| 5407 | Importer.Import(D.getLBracketLoc()), |
| 5408 | Importer.Import(D.getRBracketLoc())); |
| 5409 | |
| 5410 | assert(D.isArrayRangeDesignator()); |
| 5411 | return Designator(D.getFirstExprIndex(), |
| 5412 | Importer.Import(D.getLBracketLoc()), |
| 5413 | Importer.Import(D.getEllipsisLoc()), |
| 5414 | Importer.Import(D.getRBracketLoc())); |
| 5415 | } |
| 5416 | |
| 5417 | |
| 5418 | Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) { |
| 5419 | Expr *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit())); |
| 5420 | if (!Init) |
| 5421 | return nullptr; |
| 5422 | |
| 5423 | SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1); |
| 5424 | // List elements from the second, the first is Init itself |
| 5425 | for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) { |
| 5426 | if (Expr *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I)))) |
| 5427 | IndexExprs[I - 1] = Arg; |
| 5428 | else |
| 5429 | return nullptr; |
| 5430 | } |
| 5431 | |
| 5432 | SmallVector<Designator, 4> Designators(DIE->size()); |
| 5433 | std::transform(DIE->designators_begin(), DIE->designators_end(), |
| 5434 | Designators.begin(), |
| 5435 | [this](const Designator &D) -> Designator { |
| 5436 | return ImportDesignator(D); |
| 5437 | }); |
| 5438 | |
| 5439 | for (auto I = DIE->designators_begin(), E = DIE->designators_end(); I != E; |
| 5440 | ++I) |
| 5441 | if (I->isFieldDesignator() && !I->getFieldName()) |
| 5442 | return nullptr; |
| 5443 | |
| 5444 | return DesignatedInitExpr::Create( |
| 5445 | Importer.getToContext(), Designators.data(), Designators.size(), |
| 5446 | IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()), |
| 5447 | DIE->usesGNUSyntax(), Init); |
| 5448 | } |
| 5449 | |
| 5450 | Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) { |
| 5451 | QualType T = Importer.Import(E->getType()); |
| 5452 | if (T.isNull()) |
| 5453 | return nullptr; |
| 5454 | |
| 5455 | return new (Importer.getToContext()) |
| 5456 | CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation())); |
| 5457 | } |
| 5458 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 5459 | Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) { |
| 5460 | QualType T = Importer.Import(E->getType()); |
| 5461 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5462 | return nullptr; |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 5463 | |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 5464 | return IntegerLiteral::Create(Importer.getToContext(), |
| 5465 | E->getValue(), T, |
| 5466 | Importer.Import(E->getLocation())); |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 5467 | } |
| 5468 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5469 | Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) { |
| 5470 | QualType T = Importer.Import(E->getType()); |
| 5471 | if (T.isNull()) |
| 5472 | return nullptr; |
| 5473 | |
| 5474 | return FloatingLiteral::Create(Importer.getToContext(), |
| 5475 | E->getValue(), E->isExact(), T, |
| 5476 | Importer.Import(E->getLocation())); |
| 5477 | } |
| 5478 | |
Douglas Gregor | 623421d | 2010-02-18 02:21:22 +0000 | [diff] [blame] | 5479 | Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) { |
| 5480 | QualType T = Importer.Import(E->getType()); |
| 5481 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5482 | return nullptr; |
| 5483 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5484 | return new (Importer.getToContext()) CharacterLiteral(E->getValue(), |
| 5485 | E->getKind(), T, |
Douglas Gregor | 623421d | 2010-02-18 02:21:22 +0000 | [diff] [blame] | 5486 | Importer.Import(E->getLocation())); |
| 5487 | } |
| 5488 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5489 | Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) { |
| 5490 | QualType T = Importer.Import(E->getType()); |
| 5491 | if (T.isNull()) |
| 5492 | return nullptr; |
| 5493 | |
| 5494 | SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated()); |
| 5495 | ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin()); |
| 5496 | |
| 5497 | return StringLiteral::Create(Importer.getToContext(), E->getBytes(), |
| 5498 | E->getKind(), E->isPascal(), T, |
| 5499 | Locations.data(), Locations.size()); |
| 5500 | } |
| 5501 | |
| 5502 | Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 5503 | QualType T = Importer.Import(E->getType()); |
| 5504 | if (T.isNull()) |
| 5505 | return nullptr; |
| 5506 | |
| 5507 | TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo()); |
| 5508 | if (!TInfo) |
| 5509 | return nullptr; |
| 5510 | |
| 5511 | Expr *Init = Importer.Import(E->getInitializer()); |
| 5512 | if (!Init) |
| 5513 | return nullptr; |
| 5514 | |
| 5515 | return new (Importer.getToContext()) CompoundLiteralExpr( |
| 5516 | Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(), |
| 5517 | Init, E->isFileScope()); |
| 5518 | } |
| 5519 | |
| 5520 | Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) { |
| 5521 | QualType T = Importer.Import(E->getType()); |
| 5522 | if (T.isNull()) |
| 5523 | return nullptr; |
| 5524 | |
| 5525 | SmallVector<Expr *, 6> Exprs(E->getNumSubExprs()); |
| 5526 | if (ImportArrayChecked( |
| 5527 | E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(), |
| 5528 | Exprs.begin())) |
| 5529 | return nullptr; |
| 5530 | |
| 5531 | return new (Importer.getToContext()) AtomicExpr( |
| 5532 | Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(), |
| 5533 | Importer.Import(E->getRParenLoc())); |
| 5534 | } |
| 5535 | |
| 5536 | Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) { |
| 5537 | QualType T = Importer.Import(E->getType()); |
| 5538 | if (T.isNull()) |
| 5539 | return nullptr; |
| 5540 | |
| 5541 | LabelDecl *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel())); |
| 5542 | if (!ToLabel) |
| 5543 | return nullptr; |
| 5544 | |
| 5545 | return new (Importer.getToContext()) AddrLabelExpr( |
| 5546 | Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()), |
| 5547 | ToLabel, T); |
| 5548 | } |
| 5549 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5550 | Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) { |
| 5551 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 5552 | if (!SubExpr) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5553 | return nullptr; |
| 5554 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5555 | return new (Importer.getToContext()) |
| 5556 | ParenExpr(Importer.Import(E->getLParen()), |
| 5557 | Importer.Import(E->getRParen()), |
| 5558 | SubExpr); |
| 5559 | } |
| 5560 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5561 | Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) { |
| 5562 | SmallVector<Expr *, 4> Exprs(E->getNumExprs()); |
| 5563 | if (ImportArrayChecked( |
| 5564 | E->getExprs(), E->getExprs() + E->getNumExprs(), Exprs.begin())) |
| 5565 | return nullptr; |
| 5566 | |
| 5567 | return new (Importer.getToContext()) ParenListExpr( |
| 5568 | Importer.getToContext(), Importer.Import(E->getLParenLoc()), |
| 5569 | Exprs, Importer.Import(E->getLParenLoc())); |
| 5570 | } |
| 5571 | |
| 5572 | Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) { |
| 5573 | QualType T = Importer.Import(E->getType()); |
| 5574 | if (T.isNull()) |
| 5575 | return nullptr; |
| 5576 | |
| 5577 | CompoundStmt *ToSubStmt = cast_or_null<CompoundStmt>( |
| 5578 | Importer.Import(E->getSubStmt())); |
| 5579 | if (!ToSubStmt && E->getSubStmt()) |
| 5580 | return nullptr; |
| 5581 | |
| 5582 | return new (Importer.getToContext()) StmtExpr(ToSubStmt, T, |
| 5583 | Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc())); |
| 5584 | } |
| 5585 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5586 | Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) { |
| 5587 | QualType T = Importer.Import(E->getType()); |
| 5588 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5589 | return nullptr; |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5590 | |
| 5591 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 5592 | if (!SubExpr) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5593 | return nullptr; |
| 5594 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5595 | return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5596 | T, E->getValueKind(), |
| 5597 | E->getObjectKind(), |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5598 | Importer.Import(E->getOperatorLoc())); |
| 5599 | } |
| 5600 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5601 | Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr( |
| 5602 | UnaryExprOrTypeTraitExpr *E) { |
Douglas Gregor | d8552cd | 2010-02-19 01:24:23 +0000 | [diff] [blame] | 5603 | QualType ResultType = Importer.Import(E->getType()); |
| 5604 | |
| 5605 | if (E->isArgumentType()) { |
| 5606 | TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo()); |
| 5607 | if (!TInfo) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5608 | return nullptr; |
| 5609 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5610 | return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(), |
| 5611 | TInfo, ResultType, |
Douglas Gregor | d8552cd | 2010-02-19 01:24:23 +0000 | [diff] [blame] | 5612 | Importer.Import(E->getOperatorLoc()), |
| 5613 | Importer.Import(E->getRParenLoc())); |
| 5614 | } |
| 5615 | |
| 5616 | Expr *SubExpr = Importer.Import(E->getArgumentExpr()); |
| 5617 | if (!SubExpr) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5618 | return nullptr; |
| 5619 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5620 | return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(), |
| 5621 | SubExpr, ResultType, |
Douglas Gregor | d8552cd | 2010-02-19 01:24:23 +0000 | [diff] [blame] | 5622 | Importer.Import(E->getOperatorLoc()), |
| 5623 | Importer.Import(E->getRParenLoc())); |
| 5624 | } |
| 5625 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5626 | Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) { |
| 5627 | QualType T = Importer.Import(E->getType()); |
| 5628 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5629 | return nullptr; |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5630 | |
| 5631 | Expr *LHS = Importer.Import(E->getLHS()); |
| 5632 | if (!LHS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5633 | return nullptr; |
| 5634 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5635 | Expr *RHS = Importer.Import(E->getRHS()); |
| 5636 | if (!RHS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5637 | return nullptr; |
| 5638 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5639 | return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5640 | T, E->getValueKind(), |
| 5641 | E->getObjectKind(), |
Lang Hames | 5de91cc | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 5642 | Importer.Import(E->getOperatorLoc()), |
| 5643 | E->isFPContractable()); |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5644 | } |
| 5645 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5646 | Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) { |
| 5647 | QualType T = Importer.Import(E->getType()); |
| 5648 | if (T.isNull()) |
| 5649 | return nullptr; |
| 5650 | |
| 5651 | Expr *ToLHS = Importer.Import(E->getLHS()); |
| 5652 | if (!ToLHS) |
| 5653 | return nullptr; |
| 5654 | |
| 5655 | Expr *ToRHS = Importer.Import(E->getRHS()); |
| 5656 | if (!ToRHS) |
| 5657 | return nullptr; |
| 5658 | |
| 5659 | Expr *ToCond = Importer.Import(E->getCond()); |
| 5660 | if (!ToCond) |
| 5661 | return nullptr; |
| 5662 | |
| 5663 | return new (Importer.getToContext()) ConditionalOperator( |
| 5664 | ToCond, Importer.Import(E->getQuestionLoc()), |
| 5665 | ToLHS, Importer.Import(E->getColonLoc()), |
| 5666 | ToRHS, T, E->getValueKind(), E->getObjectKind()); |
| 5667 | } |
| 5668 | |
| 5669 | Expr *ASTNodeImporter::VisitBinaryConditionalOperator( |
| 5670 | BinaryConditionalOperator *E) { |
| 5671 | QualType T = Importer.Import(E->getType()); |
| 5672 | if (T.isNull()) |
| 5673 | return nullptr; |
| 5674 | |
| 5675 | Expr *Common = Importer.Import(E->getCommon()); |
| 5676 | if (!Common) |
| 5677 | return nullptr; |
| 5678 | |
| 5679 | Expr *Cond = Importer.Import(E->getCond()); |
| 5680 | if (!Cond) |
| 5681 | return nullptr; |
| 5682 | |
| 5683 | OpaqueValueExpr *OpaqueValue = cast_or_null<OpaqueValueExpr>( |
| 5684 | Importer.Import(E->getOpaqueValue())); |
| 5685 | if (!OpaqueValue) |
| 5686 | return nullptr; |
| 5687 | |
| 5688 | Expr *TrueExpr = Importer.Import(E->getTrueExpr()); |
| 5689 | if (!TrueExpr) |
| 5690 | return nullptr; |
| 5691 | |
| 5692 | Expr *FalseExpr = Importer.Import(E->getFalseExpr()); |
| 5693 | if (!FalseExpr) |
| 5694 | return nullptr; |
| 5695 | |
| 5696 | return new (Importer.getToContext()) BinaryConditionalOperator( |
| 5697 | Common, OpaqueValue, Cond, TrueExpr, FalseExpr, |
| 5698 | Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()), |
| 5699 | T, E->getValueKind(), E->getObjectKind()); |
| 5700 | } |
| 5701 | |
| 5702 | Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) { |
| 5703 | QualType T = Importer.Import(E->getType()); |
| 5704 | if (T.isNull()) |
| 5705 | return nullptr; |
| 5706 | |
| 5707 | Expr *SourceExpr = Importer.Import(E->getSourceExpr()); |
| 5708 | if (!SourceExpr && E->getSourceExpr()) |
| 5709 | return nullptr; |
| 5710 | |
| 5711 | return new (Importer.getToContext()) OpaqueValueExpr( |
| 5712 | Importer.Import(E->getExprLoc()), T, E->getValueKind(), |
| 5713 | E->getObjectKind(), SourceExpr); |
| 5714 | } |
| 5715 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5716 | Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) { |
| 5717 | QualType T = Importer.Import(E->getType()); |
| 5718 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5719 | return nullptr; |
| 5720 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5721 | QualType CompLHSType = Importer.Import(E->getComputationLHSType()); |
| 5722 | if (CompLHSType.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5723 | return nullptr; |
| 5724 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5725 | QualType CompResultType = Importer.Import(E->getComputationResultType()); |
| 5726 | if (CompResultType.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5727 | return nullptr; |
| 5728 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5729 | Expr *LHS = Importer.Import(E->getLHS()); |
| 5730 | if (!LHS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5731 | return nullptr; |
| 5732 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5733 | Expr *RHS = Importer.Import(E->getRHS()); |
| 5734 | if (!RHS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5735 | return nullptr; |
| 5736 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5737 | return new (Importer.getToContext()) |
| 5738 | CompoundAssignOperator(LHS, RHS, E->getOpcode(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5739 | T, E->getValueKind(), |
| 5740 | E->getObjectKind(), |
| 5741 | CompLHSType, CompResultType, |
Lang Hames | 5de91cc | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 5742 | Importer.Import(E->getOperatorLoc()), |
| 5743 | E->isFPContractable()); |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5744 | } |
| 5745 | |
Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 5746 | static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) { |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5747 | if (E->path_empty()) return false; |
| 5748 | |
| 5749 | // TODO: import cast paths |
| 5750 | return true; |
| 5751 | } |
| 5752 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 5753 | Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 5754 | QualType T = Importer.Import(E->getType()); |
| 5755 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5756 | return nullptr; |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 5757 | |
| 5758 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 5759 | if (!SubExpr) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5760 | return nullptr; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5761 | |
| 5762 | CXXCastPath BasePath; |
| 5763 | if (ImportCastPath(E, BasePath)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5764 | return nullptr; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5765 | |
| 5766 | return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5767 | SubExpr, &BasePath, E->getValueKind()); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 5768 | } |
| 5769 | |
Douglas Gregor | 5481d32 | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 5770 | Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) { |
| 5771 | QualType T = Importer.Import(E->getType()); |
| 5772 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5773 | return nullptr; |
| 5774 | |
Douglas Gregor | 5481d32 | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 5775 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 5776 | if (!SubExpr) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5777 | return nullptr; |
Douglas Gregor | 5481d32 | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 5778 | |
| 5779 | TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten()); |
| 5780 | if (!TInfo && E->getTypeInfoAsWritten()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5781 | return nullptr; |
| 5782 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5783 | CXXCastPath BasePath; |
| 5784 | if (ImportCastPath(E, BasePath)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5785 | return nullptr; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5786 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5787 | return CStyleCastExpr::Create(Importer.getToContext(), T, |
| 5788 | E->getValueKind(), E->getCastKind(), |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5789 | SubExpr, &BasePath, TInfo, |
| 5790 | Importer.Import(E->getLParenLoc()), |
| 5791 | Importer.Import(E->getRParenLoc())); |
Douglas Gregor | 5481d32 | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 5792 | } |
| 5793 | |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 5794 | Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 5795 | QualType T = Importer.Import(E->getType()); |
| 5796 | if (T.isNull()) |
| 5797 | return nullptr; |
| 5798 | |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 5799 | NamedDecl *ToFound = |
Sean Callanan | dd2c174 | 2016-05-16 20:48:03 +0000 | [diff] [blame] | 5800 | dyn_cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl())); |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 5801 | if (!ToFound) |
| 5802 | return nullptr; |
| 5803 | |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 5804 | CXXConstructorDecl *ToCCD = |
Sean Callanan | dd2c174 | 2016-05-16 20:48:03 +0000 | [diff] [blame] | 5805 | dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor())); |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 5806 | if (!ToCCD) |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 5807 | return nullptr; |
| 5808 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5809 | SmallVector<Expr *, 6> ToArgs(E->getNumArgs()); |
| 5810 | if (ImportArrayChecked(E->getArgs(), E->getArgs() + E->getNumArgs(), |
| 5811 | ToArgs.begin())) |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 5812 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 5813 | |
| 5814 | return CXXConstructExpr::Create(Importer.getToContext(), T, |
| 5815 | Importer.Import(E->getLocation()), |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 5816 | ToFound, ToCCD, E->isElidable(), |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 5817 | ToArgs, E->hadMultipleCandidates(), |
| 5818 | E->isListInitialization(), |
| 5819 | E->isStdInitListInitialization(), |
| 5820 | E->requiresZeroInitialization(), |
| 5821 | E->getConstructionKind(), |
| 5822 | Importer.Import(E->getParenOrBraceRange())); |
| 5823 | } |
| 5824 | |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 5825 | Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) { |
| 5826 | QualType T = Importer.Import(E->getType()); |
| 5827 | if (T.isNull()) |
| 5828 | return nullptr; |
| 5829 | |
| 5830 | Expr *ToFn = Importer.Import(E->getCallee()); |
| 5831 | if (!ToFn) |
| 5832 | return nullptr; |
| 5833 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5834 | SmallVector<Expr *, 4> ToArgs(E->getNumArgs()); |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 5835 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5836 | if (ImportArrayChecked(E->arg_begin(), E->arg_end(), ToArgs.begin())) |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 5837 | return nullptr; |
| 5838 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5839 | return new (Importer.getToContext()) CXXMemberCallExpr( |
| 5840 | Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(), |
| 5841 | Importer.Import(E->getRParenLoc())); |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 5842 | } |
| 5843 | |
| 5844 | Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) { |
| 5845 | QualType T = Importer.Import(E->getType()); |
| 5846 | if (T.isNull()) |
| 5847 | return nullptr; |
| 5848 | |
| 5849 | return new (Importer.getToContext()) |
| 5850 | CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit()); |
| 5851 | } |
| 5852 | |
| 5853 | Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) { |
| 5854 | QualType T = Importer.Import(E->getType()); |
| 5855 | if (T.isNull()) |
| 5856 | return nullptr; |
| 5857 | |
| 5858 | return new (Importer.getToContext()) |
| 5859 | CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation())); |
| 5860 | } |
| 5861 | |
| 5862 | |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 5863 | Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) { |
| 5864 | QualType T = Importer.Import(E->getType()); |
| 5865 | if (T.isNull()) |
| 5866 | return nullptr; |
| 5867 | |
| 5868 | Expr *ToBase = Importer.Import(E->getBase()); |
| 5869 | if (!ToBase && E->getBase()) |
| 5870 | return nullptr; |
| 5871 | |
| 5872 | ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl())); |
| 5873 | if (!ToMember && E->getMemberDecl()) |
| 5874 | return nullptr; |
| 5875 | |
| 5876 | DeclAccessPair ToFoundDecl = DeclAccessPair::make( |
| 5877 | dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())), |
| 5878 | E->getFoundDecl().getAccess()); |
| 5879 | |
| 5880 | DeclarationNameInfo ToMemberNameInfo( |
| 5881 | Importer.Import(E->getMemberNameInfo().getName()), |
| 5882 | Importer.Import(E->getMemberNameInfo().getLoc())); |
| 5883 | |
| 5884 | if (E->hasExplicitTemplateArgs()) { |
| 5885 | return nullptr; // FIXME: handle template arguments |
| 5886 | } |
| 5887 | |
| 5888 | return MemberExpr::Create(Importer.getToContext(), ToBase, |
| 5889 | E->isArrow(), |
| 5890 | Importer.Import(E->getOperatorLoc()), |
| 5891 | Importer.Import(E->getQualifierLoc()), |
| 5892 | Importer.Import(E->getTemplateKeywordLoc()), |
| 5893 | ToMember, ToFoundDecl, ToMemberNameInfo, |
| 5894 | nullptr, T, E->getValueKind(), |
| 5895 | E->getObjectKind()); |
| 5896 | } |
| 5897 | |
| 5898 | Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) { |
| 5899 | QualType T = Importer.Import(E->getType()); |
| 5900 | if (T.isNull()) |
| 5901 | return nullptr; |
| 5902 | |
| 5903 | Expr *ToCallee = Importer.Import(E->getCallee()); |
| 5904 | if (!ToCallee && E->getCallee()) |
| 5905 | return nullptr; |
| 5906 | |
| 5907 | unsigned NumArgs = E->getNumArgs(); |
| 5908 | |
| 5909 | llvm::SmallVector<Expr *, 2> ToArgs(NumArgs); |
| 5910 | |
| 5911 | for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) { |
| 5912 | Expr *FromArg = E->getArg(ai); |
| 5913 | Expr *ToArg = Importer.Import(FromArg); |
| 5914 | if (!ToArg) |
| 5915 | return nullptr; |
| 5916 | ToArgs[ai] = ToArg; |
| 5917 | } |
| 5918 | |
| 5919 | Expr **ToArgs_Copied = new (Importer.getToContext()) |
| 5920 | Expr*[NumArgs]; |
| 5921 | |
| 5922 | for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) |
| 5923 | ToArgs_Copied[ai] = ToArgs[ai]; |
| 5924 | |
| 5925 | return new (Importer.getToContext()) |
| 5926 | CallExpr(Importer.getToContext(), ToCallee, |
Craig Topper | c005cc0 | 2015-09-27 03:44:08 +0000 | [diff] [blame] | 5927 | llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(), |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 5928 | Importer.Import(E->getRParenLoc())); |
| 5929 | } |
| 5930 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5931 | Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) { |
| 5932 | QualType T = Importer.Import(ILE->getType()); |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 5933 | if (T.isNull()) |
| 5934 | return nullptr; |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 5935 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5936 | llvm::SmallVector<Expr *, 4> Exprs(ILE->getNumInits()); |
| 5937 | if (ImportArrayChecked( |
| 5938 | ILE->getInits(), ILE->getInits() + ILE->getNumInits(), Exprs.begin())) |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 5939 | return nullptr; |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 5940 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5941 | ASTContext &ToCtx = Importer.getToContext(); |
| 5942 | InitListExpr *To = new (ToCtx) InitListExpr( |
| 5943 | ToCtx, Importer.Import(ILE->getLBraceLoc()), |
| 5944 | Exprs, Importer.Import(ILE->getLBraceLoc())); |
| 5945 | To->setType(T); |
| 5946 | |
| 5947 | if (ILE->hasArrayFiller()) { |
| 5948 | Expr *Filler = Importer.Import(ILE->getArrayFiller()); |
| 5949 | if (!Filler) |
| 5950 | return nullptr; |
| 5951 | To->setArrayFiller(Filler); |
| 5952 | } |
| 5953 | |
| 5954 | if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) { |
| 5955 | FieldDecl *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD)); |
| 5956 | if (!ToFD) |
| 5957 | return nullptr; |
| 5958 | To->setInitializedFieldInUnion(ToFD); |
| 5959 | } |
| 5960 | |
| 5961 | if (InitListExpr *SyntForm = ILE->getSyntacticForm()) { |
| 5962 | InitListExpr *ToSyntForm = cast_or_null<InitListExpr>( |
| 5963 | Importer.Import(SyntForm)); |
| 5964 | if (!ToSyntForm) |
| 5965 | return nullptr; |
| 5966 | To->setSyntacticForm(ToSyntForm); |
| 5967 | } |
| 5968 | |
| 5969 | To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator()); |
| 5970 | To->setValueDependent(ILE->isValueDependent()); |
| 5971 | To->setInstantiationDependent(ILE->isInstantiationDependent()); |
| 5972 | |
| 5973 | return To; |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 5974 | } |
| 5975 | |
Sean Callanan | dd2c174 | 2016-05-16 20:48:03 +0000 | [diff] [blame] | 5976 | Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) { |
| 5977 | FieldDecl *ToField = llvm::dyn_cast_or_null<FieldDecl>( |
| 5978 | Importer.Import(DIE->getField())); |
| 5979 | if (!ToField && DIE->getField()) |
| 5980 | return nullptr; |
| 5981 | |
| 5982 | return CXXDefaultInitExpr::Create( |
| 5983 | Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField); |
| 5984 | } |
| 5985 | |
| 5986 | Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) { |
| 5987 | QualType ToType = Importer.Import(E->getType()); |
| 5988 | if (ToType.isNull() && !E->getType().isNull()) |
| 5989 | return nullptr; |
| 5990 | ExprValueKind VK = E->getValueKind(); |
| 5991 | CastKind CK = E->getCastKind(); |
| 5992 | Expr *ToOp = Importer.Import(E->getSubExpr()); |
| 5993 | if (!ToOp && E->getSubExpr()) |
| 5994 | return nullptr; |
| 5995 | CXXCastPath BasePath; |
| 5996 | if (ImportCastPath(E, BasePath)) |
| 5997 | return nullptr; |
| 5998 | TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten()); |
| 5999 | SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc()); |
| 6000 | SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc()); |
| 6001 | SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets()); |
| 6002 | |
| 6003 | if (isa<CXXStaticCastExpr>(E)) { |
| 6004 | return CXXStaticCastExpr::Create( |
| 6005 | Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath, |
| 6006 | ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets); |
| 6007 | } else if (isa<CXXDynamicCastExpr>(E)) { |
| 6008 | return CXXDynamicCastExpr::Create( |
| 6009 | Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath, |
| 6010 | ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets); |
| 6011 | } else if (isa<CXXReinterpretCastExpr>(E)) { |
| 6012 | return CXXReinterpretCastExpr::Create( |
| 6013 | Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath, |
| 6014 | ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets); |
| 6015 | } else { |
| 6016 | return nullptr; |
| 6017 | } |
| 6018 | } |
| 6019 | |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 6020 | ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager, |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 6021 | ASTContext &FromContext, FileManager &FromFileManager, |
| 6022 | bool MinimalImport) |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 6023 | : ToContext(ToContext), FromContext(FromContext), |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 6024 | ToFileManager(ToFileManager), FromFileManager(FromFileManager), |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 6025 | Minimal(MinimalImport), LastDiagFromFrom(false) |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 6026 | { |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6027 | ImportedDecls[FromContext.getTranslationUnitDecl()] |
| 6028 | = ToContext.getTranslationUnitDecl(); |
| 6029 | } |
| 6030 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 6031 | ASTImporter::~ASTImporter() { } |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 6032 | |
| 6033 | QualType ASTImporter::Import(QualType FromT) { |
| 6034 | if (FromT.isNull()) |
| 6035 | return QualType(); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 6036 | |
| 6037 | const Type *fromTy = FromT.getTypePtr(); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 6038 | |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 6039 | // Check whether we've already imported this type. |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 6040 | llvm::DenseMap<const Type *, const Type *>::iterator Pos |
| 6041 | = ImportedTypes.find(fromTy); |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 6042 | if (Pos != ImportedTypes.end()) |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 6043 | return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 6044 | |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 6045 | // Import the type |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 6046 | ASTNodeImporter Importer(*this); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 6047 | QualType ToT = Importer.Visit(fromTy); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 6048 | if (ToT.isNull()) |
| 6049 | return ToT; |
| 6050 | |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 6051 | // Record the imported type. |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 6052 | ImportedTypes[fromTy] = ToT.getTypePtr(); |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 6053 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 6054 | return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 6055 | } |
| 6056 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6057 | TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) { |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 6058 | if (!FromTSI) |
| 6059 | return FromTSI; |
| 6060 | |
| 6061 | // FIXME: For now we just create a "trivial" type source info based |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 6062 | // 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] | 6063 | QualType T = Import(FromTSI->getType()); |
| 6064 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6065 | return nullptr; |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 6066 | |
| 6067 | return ToContext.getTrivialTypeSourceInfo(T, |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 6068 | Import(FromTSI->getTypeLoc().getLocStart())); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6069 | } |
| 6070 | |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 6071 | Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) { |
| 6072 | llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD); |
| 6073 | if (Pos != ImportedDecls.end()) { |
| 6074 | Decl *ToD = Pos->second; |
| 6075 | ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD); |
| 6076 | return ToD; |
| 6077 | } else { |
| 6078 | return nullptr; |
| 6079 | } |
| 6080 | } |
| 6081 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6082 | Decl *ASTImporter::Import(Decl *FromD) { |
| 6083 | if (!FromD) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6084 | return nullptr; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6085 | |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 6086 | ASTNodeImporter Importer(*this); |
| 6087 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6088 | // Check whether we've already imported this declaration. |
| 6089 | llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 6090 | if (Pos != ImportedDecls.end()) { |
| 6091 | Decl *ToD = Pos->second; |
| 6092 | Importer.ImportDefinitionIfNeeded(FromD, ToD); |
| 6093 | return ToD; |
| 6094 | } |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6095 | |
| 6096 | // Import the type |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6097 | Decl *ToD = Importer.Visit(FromD); |
| 6098 | if (!ToD) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6099 | return nullptr; |
| 6100 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6101 | // Record the imported declaration. |
| 6102 | ImportedDecls[FromD] = ToD; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 6103 | |
| 6104 | if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) { |
| 6105 | // Keep track of anonymous tags that have an associated typedef. |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6106 | if (FromTag->getTypedefNameForAnonDecl()) |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 6107 | AnonTagsWithPendingTypedefs.push_back(FromTag); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6108 | } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 6109 | // When we've finished transforming a typedef, see whether it was the |
| 6110 | // typedef for an anonymous tag. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 6111 | for (SmallVectorImpl<TagDecl *>::iterator |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 6112 | FromTag = AnonTagsWithPendingTypedefs.begin(), |
| 6113 | FromTagEnd = AnonTagsWithPendingTypedefs.end(); |
| 6114 | FromTag != FromTagEnd; ++FromTag) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6115 | if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 6116 | if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) { |
| 6117 | // We found the typedef for an anonymous tag; link them. |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6118 | ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD)); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 6119 | AnonTagsWithPendingTypedefs.erase(FromTag); |
| 6120 | break; |
| 6121 | } |
| 6122 | } |
| 6123 | } |
| 6124 | } |
| 6125 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6126 | return ToD; |
| 6127 | } |
| 6128 | |
| 6129 | DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) { |
| 6130 | if (!FromDC) |
| 6131 | return FromDC; |
| 6132 | |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 6133 | DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC))); |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 6134 | if (!ToDC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6135 | return nullptr; |
| 6136 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 6137 | // When we're using a record/enum/Objective-C class/protocol as a context, we |
| 6138 | // need it to have a definition. |
| 6139 | if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) { |
Douglas Gregor | 63db971 | 2012-01-25 01:13:20 +0000 | [diff] [blame] | 6140 | RecordDecl *FromRecord = cast<RecordDecl>(FromDC); |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 6141 | if (ToRecord->isCompleteDefinition()) { |
| 6142 | // Do nothing. |
| 6143 | } else if (FromRecord->isCompleteDefinition()) { |
| 6144 | ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord, |
| 6145 | ASTNodeImporter::IDK_Basic); |
| 6146 | } else { |
| 6147 | CompleteDecl(ToRecord); |
| 6148 | } |
| 6149 | } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) { |
| 6150 | EnumDecl *FromEnum = cast<EnumDecl>(FromDC); |
| 6151 | if (ToEnum->isCompleteDefinition()) { |
| 6152 | // Do nothing. |
| 6153 | } else if (FromEnum->isCompleteDefinition()) { |
| 6154 | ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum, |
| 6155 | ASTNodeImporter::IDK_Basic); |
| 6156 | } else { |
| 6157 | CompleteDecl(ToEnum); |
| 6158 | } |
| 6159 | } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) { |
| 6160 | ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC); |
| 6161 | if (ToClass->getDefinition()) { |
| 6162 | // Do nothing. |
| 6163 | } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) { |
| 6164 | ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass, |
| 6165 | ASTNodeImporter::IDK_Basic); |
| 6166 | } else { |
| 6167 | CompleteDecl(ToClass); |
| 6168 | } |
| 6169 | } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) { |
| 6170 | ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC); |
| 6171 | if (ToProto->getDefinition()) { |
| 6172 | // Do nothing. |
| 6173 | } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) { |
| 6174 | ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto, |
| 6175 | ASTNodeImporter::IDK_Basic); |
| 6176 | } else { |
| 6177 | CompleteDecl(ToProto); |
| 6178 | } |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 6179 | } |
| 6180 | |
| 6181 | return ToDC; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6182 | } |
| 6183 | |
| 6184 | Expr *ASTImporter::Import(Expr *FromE) { |
| 6185 | if (!FromE) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6186 | return nullptr; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6187 | |
| 6188 | return cast_or_null<Expr>(Import(cast<Stmt>(FromE))); |
| 6189 | } |
| 6190 | |
| 6191 | Stmt *ASTImporter::Import(Stmt *FromS) { |
| 6192 | if (!FromS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6193 | return nullptr; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6194 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 6195 | // Check whether we've already imported this declaration. |
| 6196 | llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS); |
| 6197 | if (Pos != ImportedStmts.end()) |
| 6198 | return Pos->second; |
| 6199 | |
| 6200 | // Import the type |
| 6201 | ASTNodeImporter Importer(*this); |
| 6202 | Stmt *ToS = Importer.Visit(FromS); |
| 6203 | if (!ToS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6204 | return nullptr; |
| 6205 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 6206 | // Record the imported declaration. |
| 6207 | ImportedStmts[FromS] = ToS; |
| 6208 | return ToS; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6209 | } |
| 6210 | |
| 6211 | NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) { |
| 6212 | if (!FromNNS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6213 | return nullptr; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6214 | |
Douglas Gregor | 90ebf25 | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 6215 | NestedNameSpecifier *prefix = Import(FromNNS->getPrefix()); |
| 6216 | |
| 6217 | switch (FromNNS->getKind()) { |
| 6218 | case NestedNameSpecifier::Identifier: |
| 6219 | if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) { |
| 6220 | return NestedNameSpecifier::Create(ToContext, prefix, II); |
| 6221 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6222 | return nullptr; |
Douglas Gregor | 90ebf25 | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 6223 | |
| 6224 | case NestedNameSpecifier::Namespace: |
| 6225 | if (NamespaceDecl *NS = |
| 6226 | cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) { |
| 6227 | return NestedNameSpecifier::Create(ToContext, prefix, NS); |
| 6228 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6229 | return nullptr; |
Douglas Gregor | 90ebf25 | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 6230 | |
| 6231 | case NestedNameSpecifier::NamespaceAlias: |
| 6232 | if (NamespaceAliasDecl *NSAD = |
| 6233 | cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) { |
| 6234 | return NestedNameSpecifier::Create(ToContext, prefix, NSAD); |
| 6235 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6236 | return nullptr; |
Douglas Gregor | 90ebf25 | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 6237 | |
| 6238 | case NestedNameSpecifier::Global: |
| 6239 | return NestedNameSpecifier::GlobalSpecifier(ToContext); |
| 6240 | |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 6241 | case NestedNameSpecifier::Super: |
| 6242 | if (CXXRecordDecl *RD = |
| 6243 | cast<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) { |
| 6244 | return NestedNameSpecifier::SuperSpecifier(ToContext, RD); |
| 6245 | } |
| 6246 | return nullptr; |
| 6247 | |
Douglas Gregor | 90ebf25 | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 6248 | case NestedNameSpecifier::TypeSpec: |
| 6249 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 6250 | QualType T = Import(QualType(FromNNS->getAsType(), 0u)); |
| 6251 | if (!T.isNull()) { |
| 6252 | bool bTemplate = FromNNS->getKind() == |
| 6253 | NestedNameSpecifier::TypeSpecWithTemplate; |
| 6254 | return NestedNameSpecifier::Create(ToContext, prefix, |
| 6255 | bTemplate, T.getTypePtr()); |
| 6256 | } |
| 6257 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6258 | return nullptr; |
Douglas Gregor | 90ebf25 | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 6259 | } |
| 6260 | |
| 6261 | llvm_unreachable("Invalid nested name specifier kind"); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6262 | } |
| 6263 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 6264 | NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) { |
| 6265 | // FIXME: Implement! |
| 6266 | return NestedNameSpecifierLoc(); |
| 6267 | } |
| 6268 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 6269 | TemplateName ASTImporter::Import(TemplateName From) { |
| 6270 | switch (From.getKind()) { |
| 6271 | case TemplateName::Template: |
| 6272 | if (TemplateDecl *ToTemplate |
| 6273 | = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl()))) |
| 6274 | return TemplateName(ToTemplate); |
| 6275 | |
| 6276 | return TemplateName(); |
| 6277 | |
| 6278 | case TemplateName::OverloadedTemplate: { |
| 6279 | OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate(); |
| 6280 | UnresolvedSet<2> ToTemplates; |
| 6281 | for (OverloadedTemplateStorage::iterator I = FromStorage->begin(), |
| 6282 | E = FromStorage->end(); |
| 6283 | I != E; ++I) { |
| 6284 | if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I))) |
| 6285 | ToTemplates.addDecl(To); |
| 6286 | else |
| 6287 | return TemplateName(); |
| 6288 | } |
| 6289 | return ToContext.getOverloadedTemplateName(ToTemplates.begin(), |
| 6290 | ToTemplates.end()); |
| 6291 | } |
| 6292 | |
| 6293 | case TemplateName::QualifiedTemplate: { |
| 6294 | QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName(); |
| 6295 | NestedNameSpecifier *Qualifier = Import(QTN->getQualifier()); |
| 6296 | if (!Qualifier) |
| 6297 | return TemplateName(); |
| 6298 | |
| 6299 | if (TemplateDecl *ToTemplate |
| 6300 | = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl()))) |
| 6301 | return ToContext.getQualifiedTemplateName(Qualifier, |
| 6302 | QTN->hasTemplateKeyword(), |
| 6303 | ToTemplate); |
| 6304 | |
| 6305 | return TemplateName(); |
| 6306 | } |
| 6307 | |
| 6308 | case TemplateName::DependentTemplate: { |
| 6309 | DependentTemplateName *DTN = From.getAsDependentTemplateName(); |
| 6310 | NestedNameSpecifier *Qualifier = Import(DTN->getQualifier()); |
| 6311 | if (!Qualifier) |
| 6312 | return TemplateName(); |
| 6313 | |
| 6314 | if (DTN->isIdentifier()) { |
| 6315 | return ToContext.getDependentTemplateName(Qualifier, |
| 6316 | Import(DTN->getIdentifier())); |
| 6317 | } |
| 6318 | |
| 6319 | return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator()); |
| 6320 | } |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 6321 | |
| 6322 | case TemplateName::SubstTemplateTemplateParm: { |
| 6323 | SubstTemplateTemplateParmStorage *subst |
| 6324 | = From.getAsSubstTemplateTemplateParm(); |
| 6325 | TemplateTemplateParmDecl *param |
| 6326 | = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter())); |
| 6327 | if (!param) |
| 6328 | return TemplateName(); |
| 6329 | |
| 6330 | TemplateName replacement = Import(subst->getReplacement()); |
| 6331 | if (replacement.isNull()) return TemplateName(); |
| 6332 | |
| 6333 | return ToContext.getSubstTemplateTemplateParm(param, replacement); |
| 6334 | } |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 6335 | |
| 6336 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 6337 | SubstTemplateTemplateParmPackStorage *SubstPack |
| 6338 | = From.getAsSubstTemplateTemplateParmPack(); |
| 6339 | TemplateTemplateParmDecl *Param |
| 6340 | = cast_or_null<TemplateTemplateParmDecl>( |
| 6341 | Import(SubstPack->getParameterPack())); |
| 6342 | if (!Param) |
| 6343 | return TemplateName(); |
| 6344 | |
| 6345 | ASTNodeImporter Importer(*this); |
| 6346 | TemplateArgument ArgPack |
| 6347 | = Importer.ImportTemplateArgument(SubstPack->getArgumentPack()); |
| 6348 | if (ArgPack.isNull()) |
| 6349 | return TemplateName(); |
| 6350 | |
| 6351 | return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack); |
| 6352 | } |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 6353 | } |
| 6354 | |
| 6355 | llvm_unreachable("Invalid template name kind"); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 6356 | } |
| 6357 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6358 | SourceLocation ASTImporter::Import(SourceLocation FromLoc) { |
| 6359 | if (FromLoc.isInvalid()) |
| 6360 | return SourceLocation(); |
| 6361 | |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 6362 | SourceManager &FromSM = FromContext.getSourceManager(); |
| 6363 | |
| 6364 | // For now, map everything down to its spelling location, so that we |
Chandler Carruth | 2536641 | 2011-07-15 00:04:35 +0000 | [diff] [blame] | 6365 | // don't have to import macro expansions. |
| 6366 | // FIXME: Import macro expansions! |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 6367 | FromLoc = FromSM.getSpellingLoc(FromLoc); |
| 6368 | std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc); |
| 6369 | SourceManager &ToSM = ToContext.getSourceManager(); |
Sean Callanan | 238d897 | 2014-12-10 01:26:39 +0000 | [diff] [blame] | 6370 | FileID ToFileID = Import(Decomposed.first); |
| 6371 | if (ToFileID.isInvalid()) |
| 6372 | return SourceLocation(); |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 6373 | SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID) |
| 6374 | .getLocWithOffset(Decomposed.second); |
| 6375 | return ret; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6376 | } |
| 6377 | |
| 6378 | SourceRange ASTImporter::Import(SourceRange FromRange) { |
| 6379 | return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd())); |
| 6380 | } |
| 6381 | |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 6382 | FileID ASTImporter::Import(FileID FromID) { |
Sebastian Redl | 99219f1 | 2010-09-30 01:03:06 +0000 | [diff] [blame] | 6383 | llvm::DenseMap<FileID, FileID>::iterator Pos |
| 6384 | = ImportedFileIDs.find(FromID); |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 6385 | if (Pos != ImportedFileIDs.end()) |
| 6386 | return Pos->second; |
| 6387 | |
| 6388 | SourceManager &FromSM = FromContext.getSourceManager(); |
| 6389 | SourceManager &ToSM = ToContext.getSourceManager(); |
| 6390 | const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID); |
Chandler Carruth | 2536641 | 2011-07-15 00:04:35 +0000 | [diff] [blame] | 6391 | assert(FromSLoc.isFile() && "Cannot handle macro expansions yet"); |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 6392 | |
| 6393 | // Include location of this file. |
| 6394 | SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc()); |
| 6395 | |
| 6396 | // Map the FileID for to the "to" source manager. |
| 6397 | FileID ToID; |
| 6398 | const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache(); |
Sean Callanan | 25d34af | 2015-04-30 00:44:21 +0000 | [diff] [blame] | 6399 | if (Cache->OrigEntry && Cache->OrigEntry->getDir()) { |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 6400 | // FIXME: We probably want to use getVirtualFile(), so we don't hit the |
| 6401 | // disk again |
| 6402 | // FIXME: We definitely want to re-use the existing MemoryBuffer, rather |
| 6403 | // than mmap the files several times. |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 6404 | const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName()); |
Sean Callanan | 238d897 | 2014-12-10 01:26:39 +0000 | [diff] [blame] | 6405 | if (!Entry) |
| 6406 | return FileID(); |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 6407 | ToID = ToSM.createFileID(Entry, ToIncludeLoc, |
| 6408 | FromSLoc.getFile().getFileCharacteristic()); |
| 6409 | } else { |
| 6410 | // FIXME: We want to re-use the existing MemoryBuffer! |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 6411 | const llvm::MemoryBuffer * |
| 6412 | FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM); |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 6413 | std::unique_ptr<llvm::MemoryBuffer> ToBuf |
Chris Lattner | 58c7934 | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 6414 | = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(), |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 6415 | FromBuf->getBufferIdentifier()); |
David Blaikie | 50a5f97 | 2014-08-29 07:59:55 +0000 | [diff] [blame] | 6416 | ToID = ToSM.createFileID(std::move(ToBuf), |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 6417 | FromSLoc.getFile().getFileCharacteristic()); |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 6418 | } |
| 6419 | |
| 6420 | |
Sebastian Redl | 99219f1 | 2010-09-30 01:03:06 +0000 | [diff] [blame] | 6421 | ImportedFileIDs[FromID] = ToID; |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 6422 | return ToID; |
| 6423 | } |
| 6424 | |
Sean Callanan | dd2c174 | 2016-05-16 20:48:03 +0000 | [diff] [blame] | 6425 | CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) { |
| 6426 | Expr *ToExpr = Import(From->getInit()); |
| 6427 | if (!ToExpr && From->getInit()) |
| 6428 | return nullptr; |
| 6429 | |
| 6430 | if (From->isBaseInitializer()) { |
| 6431 | TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo()); |
| 6432 | if (!ToTInfo && From->getTypeSourceInfo()) |
| 6433 | return nullptr; |
| 6434 | |
| 6435 | return new (ToContext) CXXCtorInitializer( |
| 6436 | ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()), |
| 6437 | ToExpr, Import(From->getRParenLoc()), |
| 6438 | From->isPackExpansion() ? Import(From->getEllipsisLoc()) |
| 6439 | : SourceLocation()); |
| 6440 | } else if (From->isMemberInitializer()) { |
| 6441 | FieldDecl *ToField = |
| 6442 | llvm::cast_or_null<FieldDecl>(Import(From->getMember())); |
| 6443 | if (!ToField && From->getMember()) |
| 6444 | return nullptr; |
| 6445 | |
| 6446 | return new (ToContext) CXXCtorInitializer( |
| 6447 | ToContext, ToField, Import(From->getMemberLocation()), |
| 6448 | Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc())); |
| 6449 | } else if (From->isIndirectMemberInitializer()) { |
| 6450 | IndirectFieldDecl *ToIField = llvm::cast_or_null<IndirectFieldDecl>( |
| 6451 | Import(From->getIndirectMember())); |
| 6452 | if (!ToIField && From->getIndirectMember()) |
| 6453 | return nullptr; |
| 6454 | |
| 6455 | return new (ToContext) CXXCtorInitializer( |
| 6456 | ToContext, ToIField, Import(From->getMemberLocation()), |
| 6457 | Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc())); |
| 6458 | } else if (From->isDelegatingInitializer()) { |
| 6459 | TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo()); |
| 6460 | if (!ToTInfo && From->getTypeSourceInfo()) |
| 6461 | return nullptr; |
| 6462 | |
| 6463 | return new (ToContext) |
| 6464 | CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()), |
| 6465 | ToExpr, Import(From->getRParenLoc())); |
| 6466 | } else if (unsigned NumArrayIndices = From->getNumArrayIndices()) { |
| 6467 | FieldDecl *ToField = |
| 6468 | llvm::cast_or_null<FieldDecl>(Import(From->getMember())); |
| 6469 | if (!ToField && From->getMember()) |
| 6470 | return nullptr; |
| 6471 | |
| 6472 | SmallVector<VarDecl *, 4> ToAIs(NumArrayIndices); |
| 6473 | |
| 6474 | for (unsigned AII = 0; AII < NumArrayIndices; ++AII) { |
| 6475 | VarDecl *ToArrayIndex = |
| 6476 | dyn_cast_or_null<VarDecl>(Import(From->getArrayIndex(AII))); |
| 6477 | if (!ToArrayIndex && From->getArrayIndex(AII)) |
| 6478 | return nullptr; |
| 6479 | } |
| 6480 | |
| 6481 | return CXXCtorInitializer::Create( |
| 6482 | ToContext, ToField, Import(From->getMemberLocation()), |
| 6483 | Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()), |
| 6484 | ToAIs.data(), NumArrayIndices); |
| 6485 | } else { |
| 6486 | return nullptr; |
| 6487 | } |
| 6488 | } |
| 6489 | |
| 6490 | |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 6491 | void ASTImporter::ImportDefinition(Decl *From) { |
| 6492 | Decl *To = Import(From); |
| 6493 | if (!To) |
| 6494 | return; |
| 6495 | |
| 6496 | if (DeclContext *FromDC = cast<DeclContext>(From)) { |
| 6497 | ASTNodeImporter Importer(*this); |
Sean Callanan | 53a6bff | 2011-07-19 22:38:25 +0000 | [diff] [blame] | 6498 | |
| 6499 | if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) { |
| 6500 | if (!ToRecord->getDefinition()) { |
| 6501 | Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord, |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 6502 | ASTNodeImporter::IDK_Everything); |
Sean Callanan | 53a6bff | 2011-07-19 22:38:25 +0000 | [diff] [blame] | 6503 | return; |
| 6504 | } |
| 6505 | } |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 6506 | |
| 6507 | if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) { |
| 6508 | if (!ToEnum->getDefinition()) { |
| 6509 | Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 6510 | ASTNodeImporter::IDK_Everything); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 6511 | return; |
| 6512 | } |
| 6513 | } |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 6514 | |
| 6515 | if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) { |
| 6516 | if (!ToIFace->getDefinition()) { |
| 6517 | Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 6518 | ASTNodeImporter::IDK_Everything); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 6519 | return; |
| 6520 | } |
| 6521 | } |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 6522 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 6523 | if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) { |
| 6524 | if (!ToProto->getDefinition()) { |
| 6525 | Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 6526 | ASTNodeImporter::IDK_Everything); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 6527 | return; |
| 6528 | } |
| 6529 | } |
| 6530 | |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 6531 | Importer.ImportDeclContext(FromDC, true); |
| 6532 | } |
| 6533 | } |
| 6534 | |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 6535 | DeclarationName ASTImporter::Import(DeclarationName FromName) { |
| 6536 | if (!FromName) |
| 6537 | return DeclarationName(); |
| 6538 | |
| 6539 | switch (FromName.getNameKind()) { |
| 6540 | case DeclarationName::Identifier: |
| 6541 | return Import(FromName.getAsIdentifierInfo()); |
| 6542 | |
| 6543 | case DeclarationName::ObjCZeroArgSelector: |
| 6544 | case DeclarationName::ObjCOneArgSelector: |
| 6545 | case DeclarationName::ObjCMultiArgSelector: |
| 6546 | return Import(FromName.getObjCSelector()); |
| 6547 | |
| 6548 | case DeclarationName::CXXConstructorName: { |
| 6549 | QualType T = Import(FromName.getCXXNameType()); |
| 6550 | if (T.isNull()) |
| 6551 | return DeclarationName(); |
| 6552 | |
| 6553 | return ToContext.DeclarationNames.getCXXConstructorName( |
| 6554 | ToContext.getCanonicalType(T)); |
| 6555 | } |
| 6556 | |
| 6557 | case DeclarationName::CXXDestructorName: { |
| 6558 | QualType T = Import(FromName.getCXXNameType()); |
| 6559 | if (T.isNull()) |
| 6560 | return DeclarationName(); |
| 6561 | |
| 6562 | return ToContext.DeclarationNames.getCXXDestructorName( |
| 6563 | ToContext.getCanonicalType(T)); |
| 6564 | } |
| 6565 | |
| 6566 | case DeclarationName::CXXConversionFunctionName: { |
| 6567 | QualType T = Import(FromName.getCXXNameType()); |
| 6568 | if (T.isNull()) |
| 6569 | return DeclarationName(); |
| 6570 | |
| 6571 | return ToContext.DeclarationNames.getCXXConversionFunctionName( |
| 6572 | ToContext.getCanonicalType(T)); |
| 6573 | } |
| 6574 | |
| 6575 | case DeclarationName::CXXOperatorName: |
| 6576 | return ToContext.DeclarationNames.getCXXOperatorName( |
| 6577 | FromName.getCXXOverloadedOperator()); |
| 6578 | |
| 6579 | case DeclarationName::CXXLiteralOperatorName: |
| 6580 | return ToContext.DeclarationNames.getCXXLiteralOperatorName( |
| 6581 | Import(FromName.getCXXLiteralIdentifier())); |
| 6582 | |
| 6583 | case DeclarationName::CXXUsingDirective: |
| 6584 | // FIXME: STATICS! |
| 6585 | return DeclarationName::getUsingDirectiveName(); |
| 6586 | } |
| 6587 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 6588 | llvm_unreachable("Invalid DeclarationName Kind!"); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 6589 | } |
| 6590 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 6591 | IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 6592 | if (!FromId) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6593 | return nullptr; |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 6594 | |
Sean Callanan | f94ef1d | 2016-05-14 06:11:19 +0000 | [diff] [blame] | 6595 | IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName()); |
| 6596 | |
| 6597 | if (!ToId->getBuiltinID() && FromId->getBuiltinID()) |
| 6598 | ToId->setBuiltinID(FromId->getBuiltinID()); |
| 6599 | |
| 6600 | return ToId; |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 6601 | } |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 6602 | |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 6603 | Selector ASTImporter::Import(Selector FromSel) { |
| 6604 | if (FromSel.isNull()) |
| 6605 | return Selector(); |
| 6606 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6607 | SmallVector<IdentifierInfo *, 4> Idents; |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 6608 | Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0))); |
| 6609 | for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I) |
| 6610 | Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I))); |
| 6611 | return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data()); |
| 6612 | } |
| 6613 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 6614 | DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name, |
| 6615 | DeclContext *DC, |
| 6616 | unsigned IDNS, |
| 6617 | NamedDecl **Decls, |
| 6618 | unsigned NumDecls) { |
| 6619 | return Name; |
| 6620 | } |
| 6621 | |
| 6622 | DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) { |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 6623 | if (LastDiagFromFrom) |
| 6624 | ToContext.getDiagnostics().notePriorDiagnosticFrom( |
| 6625 | FromContext.getDiagnostics()); |
| 6626 | LastDiagFromFrom = false; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 6627 | return ToContext.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 6628 | } |
| 6629 | |
| 6630 | DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) { |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 6631 | if (!LastDiagFromFrom) |
| 6632 | FromContext.getDiagnostics().notePriorDiagnosticFrom( |
| 6633 | ToContext.getDiagnostics()); |
| 6634 | LastDiagFromFrom = true; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 6635 | return FromContext.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 6636 | } |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 6637 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 6638 | void ASTImporter::CompleteDecl (Decl *D) { |
| 6639 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 6640 | if (!ID->getDefinition()) |
| 6641 | ID->startDefinition(); |
| 6642 | } |
| 6643 | else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
| 6644 | if (!PD->getDefinition()) |
| 6645 | PD->startDefinition(); |
| 6646 | } |
| 6647 | else if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 6648 | if (!TD->getDefinition() && !TD->isBeingDefined()) { |
| 6649 | TD->startDefinition(); |
| 6650 | TD->setCompleteDefinition(true); |
| 6651 | } |
| 6652 | } |
| 6653 | else { |
| 6654 | assert (0 && "CompleteDecl called on a Decl that can't be completed"); |
| 6655 | } |
| 6656 | } |
| 6657 | |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 6658 | Decl *ASTImporter::Imported(Decl *From, Decl *To) { |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 6659 | if (From->hasAttrs()) { |
| 6660 | for (Attr *FromAttr : From->getAttrs()) |
| 6661 | To->addAttr(FromAttr->clone(To->getASTContext())); |
| 6662 | } |
| 6663 | if (From->isUsed()) { |
| 6664 | To->setIsUsed(); |
| 6665 | } |
Sean Callanan | dd2c174 | 2016-05-16 20:48:03 +0000 | [diff] [blame] | 6666 | if (From->isImplicit()) { |
| 6667 | To->setImplicit(); |
| 6668 | } |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 6669 | ImportedDecls[From] = To; |
| 6670 | return To; |
Daniel Dunbar | 9ced542 | 2010-02-13 20:24:39 +0000 | [diff] [blame] | 6671 | } |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 6672 | |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 6673 | bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To, |
| 6674 | bool Complain) { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 6675 | llvm::DenseMap<const Type *, const Type *>::iterator Pos |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 6676 | = ImportedTypes.find(From.getTypePtr()); |
| 6677 | if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To)) |
| 6678 | return true; |
| 6679 | |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 6680 | StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls, |
| 6681 | false, Complain); |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 6682 | return Ctx.IsStructurallyEquivalent(From, To); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 6683 | } |