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); |
Gabor Horvath | 0866c2f | 2016-11-23 15:24:23 +0000 | [diff] [blame] | 42 | QualType VisitAtomicType(const AtomicType *T); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 43 | QualType VisitBuiltinType(const BuiltinType *T); |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 44 | QualType VisitDecayedType(const DecayedType *T); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 45 | QualType VisitComplexType(const ComplexType *T); |
| 46 | QualType VisitPointerType(const PointerType *T); |
| 47 | QualType VisitBlockPointerType(const BlockPointerType *T); |
| 48 | QualType VisitLValueReferenceType(const LValueReferenceType *T); |
| 49 | QualType VisitRValueReferenceType(const RValueReferenceType *T); |
| 50 | QualType VisitMemberPointerType(const MemberPointerType *T); |
| 51 | QualType VisitConstantArrayType(const ConstantArrayType *T); |
| 52 | QualType VisitIncompleteArrayType(const IncompleteArrayType *T); |
| 53 | QualType VisitVariableArrayType(const VariableArrayType *T); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 54 | // FIXME: DependentSizedArrayType |
| 55 | // FIXME: DependentSizedExtVectorType |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 56 | QualType VisitVectorType(const VectorType *T); |
| 57 | QualType VisitExtVectorType(const ExtVectorType *T); |
| 58 | QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T); |
| 59 | QualType VisitFunctionProtoType(const FunctionProtoType *T); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 60 | // FIXME: UnresolvedUsingType |
Sean Callanan | da6df8a | 2011-08-11 16:56:07 +0000 | [diff] [blame] | 61 | QualType VisitParenType(const ParenType *T); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 62 | QualType VisitTypedefType(const TypedefType *T); |
| 63 | QualType VisitTypeOfExprType(const TypeOfExprType *T); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 64 | // FIXME: DependentTypeOfExprType |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 65 | QualType VisitTypeOfType(const TypeOfType *T); |
| 66 | QualType VisitDecltypeType(const DecltypeType *T); |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 67 | QualType VisitUnaryTransformType(const UnaryTransformType *T); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 68 | QualType VisitAutoType(const AutoType *T); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 69 | QualType VisitInjectedClassNameType(const InjectedClassNameType *T); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 70 | // FIXME: DependentDecltypeType |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 71 | QualType VisitRecordType(const RecordType *T); |
| 72 | QualType VisitEnumType(const EnumType *T); |
Sean Callanan | 72fe085 | 2015-04-02 23:50:08 +0000 | [diff] [blame] | 73 | QualType VisitAttributedType(const AttributedType *T); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 74 | QualType VisitTemplateTypeParmType(const TemplateTypeParmType *T); |
Aleksei Sidorin | 855086d | 2017-01-23 09:30:36 +0000 | [diff] [blame] | 75 | QualType VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 76 | QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T); |
| 77 | QualType VisitElaboratedType(const ElaboratedType *T); |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 78 | // FIXME: DependentNameType |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 79 | // FIXME: DependentTemplateSpecializationType |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 80 | QualType VisitObjCInterfaceType(const ObjCInterfaceType *T); |
| 81 | QualType VisitObjCObjectType(const ObjCObjectType *T); |
| 82 | QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 83 | |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 84 | // Importing declarations |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 85 | bool ImportDeclParts(NamedDecl *D, DeclContext *&DC, |
| 86 | DeclContext *&LexicalDC, DeclarationName &Name, |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 87 | NamedDecl *&ToD, SourceLocation &Loc); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 88 | void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 89 | void ImportDeclarationNameLoc(const DeclarationNameInfo &From, |
| 90 | DeclarationNameInfo& To); |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 91 | void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 92 | |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 93 | bool ImportCastPath(CastExpr *E, CXXCastPath &Path); |
| 94 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 95 | typedef DesignatedInitExpr::Designator Designator; |
| 96 | Designator ImportDesignator(const Designator &D); |
| 97 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 98 | |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 99 | /// \brief What we should import from the definition. |
| 100 | enum ImportDefinitionKind { |
| 101 | /// \brief Import the default subset of the definition, which might be |
| 102 | /// nothing (if minimal import is set) or might be everything (if minimal |
| 103 | /// import is not set). |
| 104 | IDK_Default, |
| 105 | /// \brief Import everything. |
| 106 | IDK_Everything, |
| 107 | /// \brief Import only the bare bones needed to establish a valid |
| 108 | /// DeclContext. |
| 109 | IDK_Basic |
| 110 | }; |
| 111 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 112 | bool shouldForceImportDeclContext(ImportDefinitionKind IDK) { |
| 113 | return IDK == IDK_Everything || |
| 114 | (IDK == IDK_Default && !Importer.isMinimalImport()); |
| 115 | } |
| 116 | |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 117 | bool ImportDefinition(RecordDecl *From, RecordDecl *To, |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 118 | ImportDefinitionKind Kind = IDK_Default); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 119 | bool ImportDefinition(VarDecl *From, VarDecl *To, |
| 120 | ImportDefinitionKind Kind = IDK_Default); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 121 | bool ImportDefinition(EnumDecl *From, EnumDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 122 | ImportDefinitionKind Kind = IDK_Default); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 123 | bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 124 | ImportDefinitionKind Kind = IDK_Default); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 125 | bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 126 | ImportDefinitionKind Kind = IDK_Default); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 127 | TemplateParameterList *ImportTemplateParameterList( |
| 128 | TemplateParameterList *Params); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 129 | TemplateArgument ImportTemplateArgument(const TemplateArgument &From); |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 130 | TemplateArgumentLoc ImportTemplateArgumentLoc( |
| 131 | const TemplateArgumentLoc &TALoc, bool &Error); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 132 | bool ImportTemplateArguments(const TemplateArgument *FromArgs, |
| 133 | unsigned NumFromArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 134 | SmallVectorImpl<TemplateArgument> &ToArgs); |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 135 | bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord, |
| 136 | bool Complain = true); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 137 | bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar, |
| 138 | bool Complain = true); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 139 | bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord); |
Douglas Gregor | 9115508 | 2012-11-14 22:29:20 +0000 | [diff] [blame] | 140 | bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 141 | bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 142 | bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To); |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 143 | Decl *VisitDecl(Decl *D); |
Argyrios Kyrtzidis | 544ea71 | 2016-02-18 23:08:36 +0000 | [diff] [blame] | 144 | Decl *VisitAccessSpecDecl(AccessSpecDecl *D); |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 145 | Decl *VisitStaticAssertDecl(StaticAssertDecl *D); |
Sean Callanan | 6519827 | 2011-11-17 23:20:56 +0000 | [diff] [blame] | 146 | Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D); |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 147 | Decl *VisitNamespaceDecl(NamespaceDecl *D); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 148 | Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias); |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 149 | Decl *VisitTypedefDecl(TypedefDecl *D); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 150 | Decl *VisitTypeAliasDecl(TypeAliasDecl *D); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 151 | Decl *VisitLabelDecl(LabelDecl *D); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 152 | Decl *VisitEnumDecl(EnumDecl *D); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 153 | Decl *VisitRecordDecl(RecordDecl *D); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 154 | Decl *VisitEnumConstantDecl(EnumConstantDecl *D); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 155 | Decl *VisitFunctionDecl(FunctionDecl *D); |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 156 | Decl *VisitCXXMethodDecl(CXXMethodDecl *D); |
| 157 | Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D); |
| 158 | Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D); |
| 159 | Decl *VisitCXXConversionDecl(CXXConversionDecl *D); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 160 | Decl *VisitFieldDecl(FieldDecl *D); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 161 | Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D); |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 162 | Decl *VisitFriendDecl(FriendDecl *D); |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 163 | Decl *VisitObjCIvarDecl(ObjCIvarDecl *D); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 164 | Decl *VisitVarDecl(VarDecl *D); |
Douglas Gregor | 8b228d7 | 2010-02-17 21:22:52 +0000 | [diff] [blame] | 165 | Decl *VisitImplicitParamDecl(ImplicitParamDecl *D); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 166 | Decl *VisitParmVarDecl(ParmVarDecl *D); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 167 | Decl *VisitObjCMethodDecl(ObjCMethodDecl *D); |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 168 | Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 169 | Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D); |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 170 | Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D); |
Sean Callanan | 0aae041 | 2014-12-10 00:00:37 +0000 | [diff] [blame] | 171 | Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D); |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 172 | |
| 173 | ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list); |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 174 | Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 175 | Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 176 | Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 177 | Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 178 | Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 179 | Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); |
| 180 | Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); |
| 181 | Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); |
| 182 | Decl *VisitClassTemplateDecl(ClassTemplateDecl *D); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 183 | Decl *VisitClassTemplateSpecializationDecl( |
| 184 | ClassTemplateSpecializationDecl *D); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 185 | Decl *VisitVarTemplateDecl(VarTemplateDecl *D); |
| 186 | Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D); |
| 187 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 188 | // Importing statements |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 189 | DeclGroupRef ImportDeclGroup(DeclGroupRef DG); |
| 190 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 191 | Stmt *VisitStmt(Stmt *S); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 192 | Stmt *VisitGCCAsmStmt(GCCAsmStmt *S); |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 193 | Stmt *VisitDeclStmt(DeclStmt *S); |
| 194 | Stmt *VisitNullStmt(NullStmt *S); |
| 195 | Stmt *VisitCompoundStmt(CompoundStmt *S); |
| 196 | Stmt *VisitCaseStmt(CaseStmt *S); |
| 197 | Stmt *VisitDefaultStmt(DefaultStmt *S); |
| 198 | Stmt *VisitLabelStmt(LabelStmt *S); |
| 199 | Stmt *VisitAttributedStmt(AttributedStmt *S); |
| 200 | Stmt *VisitIfStmt(IfStmt *S); |
| 201 | Stmt *VisitSwitchStmt(SwitchStmt *S); |
| 202 | Stmt *VisitWhileStmt(WhileStmt *S); |
| 203 | Stmt *VisitDoStmt(DoStmt *S); |
| 204 | Stmt *VisitForStmt(ForStmt *S); |
| 205 | Stmt *VisitGotoStmt(GotoStmt *S); |
| 206 | Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S); |
| 207 | Stmt *VisitContinueStmt(ContinueStmt *S); |
| 208 | Stmt *VisitBreakStmt(BreakStmt *S); |
| 209 | Stmt *VisitReturnStmt(ReturnStmt *S); |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 210 | // FIXME: MSAsmStmt |
| 211 | // FIXME: SEHExceptStmt |
| 212 | // FIXME: SEHFinallyStmt |
| 213 | // FIXME: SEHTryStmt |
| 214 | // FIXME: SEHLeaveStmt |
| 215 | // FIXME: CapturedStmt |
| 216 | Stmt *VisitCXXCatchStmt(CXXCatchStmt *S); |
| 217 | Stmt *VisitCXXTryStmt(CXXTryStmt *S); |
| 218 | Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S); |
| 219 | // FIXME: MSDependentExistsStmt |
| 220 | Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S); |
| 221 | Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S); |
| 222 | Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S); |
| 223 | Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S); |
| 224 | Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
| 225 | Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S); |
| 226 | Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S); |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 227 | |
| 228 | // Importing expressions |
| 229 | Expr *VisitExpr(Expr *E); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 230 | Expr *VisitVAArgExpr(VAArgExpr *E); |
| 231 | Expr *VisitGNUNullExpr(GNUNullExpr *E); |
| 232 | Expr *VisitPredefinedExpr(PredefinedExpr *E); |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 233 | Expr *VisitDeclRefExpr(DeclRefExpr *E); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 234 | Expr *VisitImplicitValueInitExpr(ImplicitValueInitExpr *ILE); |
| 235 | Expr *VisitDesignatedInitExpr(DesignatedInitExpr *E); |
| 236 | Expr *VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E); |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 237 | Expr *VisitIntegerLiteral(IntegerLiteral *E); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 238 | Expr *VisitFloatingLiteral(FloatingLiteral *E); |
Douglas Gregor | 623421d | 2010-02-18 02:21:22 +0000 | [diff] [blame] | 239 | Expr *VisitCharacterLiteral(CharacterLiteral *E); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 240 | Expr *VisitStringLiteral(StringLiteral *E); |
| 241 | Expr *VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
| 242 | Expr *VisitAtomicExpr(AtomicExpr *E); |
| 243 | Expr *VisitAddrLabelExpr(AddrLabelExpr *E); |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 244 | Expr *VisitParenExpr(ParenExpr *E); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 245 | Expr *VisitParenListExpr(ParenListExpr *E); |
| 246 | Expr *VisitStmtExpr(StmtExpr *E); |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 247 | Expr *VisitUnaryOperator(UnaryOperator *E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 248 | Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 249 | Expr *VisitBinaryOperator(BinaryOperator *E); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 250 | Expr *VisitConditionalOperator(ConditionalOperator *E); |
| 251 | Expr *VisitBinaryConditionalOperator(BinaryConditionalOperator *E); |
| 252 | Expr *VisitOpaqueValueExpr(OpaqueValueExpr *E); |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 253 | Expr *VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E); |
| 254 | Expr *VisitExpressionTraitExpr(ExpressionTraitExpr *E); |
| 255 | Expr *VisitArraySubscriptExpr(ArraySubscriptExpr *E); |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 256 | Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 257 | Expr *VisitImplicitCastExpr(ImplicitCastExpr *E); |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 258 | Expr *VisitExplicitCastExpr(ExplicitCastExpr *E); |
| 259 | Expr *VisitOffsetOfExpr(OffsetOfExpr *OE); |
| 260 | Expr *VisitCXXThrowExpr(CXXThrowExpr *E); |
| 261 | Expr *VisitCXXNoexceptExpr(CXXNoexceptExpr *E); |
| 262 | Expr *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E); |
| 263 | Expr *VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); |
| 264 | Expr *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E); |
| 265 | Expr *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE); |
| 266 | Expr *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E); |
| 267 | Expr *VisitCXXNewExpr(CXXNewExpr *CE); |
| 268 | Expr *VisitCXXDeleteExpr(CXXDeleteExpr *E); |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 269 | Expr *VisitCXXConstructExpr(CXXConstructExpr *E); |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 270 | Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E); |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 271 | Expr *VisitExprWithCleanups(ExprWithCleanups *EWC); |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 272 | Expr *VisitCXXThisExpr(CXXThisExpr *E); |
| 273 | Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E); |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 274 | Expr *VisitMemberExpr(MemberExpr *E); |
| 275 | Expr *VisitCallExpr(CallExpr *E); |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 276 | Expr *VisitInitListExpr(InitListExpr *E); |
Richard Smith | 30e304e | 2016-12-14 00:03:17 +0000 | [diff] [blame] | 277 | Expr *VisitArrayInitLoopExpr(ArrayInitLoopExpr *E); |
| 278 | Expr *VisitArrayInitIndexExpr(ArrayInitIndexExpr *E); |
Sean Callanan | dd2c174 | 2016-05-16 20:48:03 +0000 | [diff] [blame] | 279 | Expr *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E); |
| 280 | Expr *VisitCXXNamedCastExpr(CXXNamedCastExpr *E); |
Aleksei Sidorin | 855086d | 2017-01-23 09:30:36 +0000 | [diff] [blame] | 281 | Expr *VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E); |
| 282 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 283 | |
| 284 | template<typename IIter, typename OIter> |
| 285 | void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) { |
| 286 | typedef typename std::remove_reference<decltype(*Obegin)>::type ItemT; |
| 287 | ASTImporter &ImporterRef = Importer; |
| 288 | std::transform(Ibegin, Iend, Obegin, |
| 289 | [&ImporterRef](ItemT From) -> ItemT { |
| 290 | return ImporterRef.Import(From); |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 291 | }); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | template<typename IIter, typename OIter> |
| 295 | bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) { |
| 296 | typedef typename std::remove_reference<decltype(**Obegin)>::type ItemT; |
| 297 | ASTImporter &ImporterRef = Importer; |
| 298 | bool Failed = false; |
| 299 | std::transform(Ibegin, Iend, Obegin, |
| 300 | [&ImporterRef, &Failed](ItemT *From) -> ItemT * { |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 301 | ItemT *To = cast_or_null<ItemT>( |
| 302 | ImporterRef.Import(From)); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 303 | if (!To && From) |
| 304 | Failed = true; |
| 305 | return To; |
| 306 | }); |
| 307 | return Failed; |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 308 | } |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 309 | |
| 310 | template<typename InContainerTy, typename OutContainerTy> |
| 311 | bool ImportContainerChecked(const InContainerTy &InContainer, |
| 312 | OutContainerTy &OutContainer) { |
| 313 | return ImportArrayChecked(InContainer.begin(), InContainer.end(), |
| 314 | OutContainer.begin()); |
| 315 | } |
| 316 | |
| 317 | template<typename InContainerTy, typename OIter> |
| 318 | bool ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) { |
| 319 | return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin); |
| 320 | } |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 321 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 322 | } |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 323 | |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 324 | using namespace clang; |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 325 | |
| 326 | //---------------------------------------------------------------------------- |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 327 | // Structural Equivalence |
| 328 | //---------------------------------------------------------------------------- |
| 329 | |
| 330 | namespace { |
| 331 | struct StructuralEquivalenceContext { |
| 332 | /// \brief AST contexts for which we are checking structural equivalence. |
| 333 | ASTContext &C1, &C2; |
| 334 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 335 | /// \brief The set of "tentative" equivalences between two canonical |
| 336 | /// declarations, mapping from a declaration in the first context to the |
| 337 | /// declaration in the second context that we believe to be equivalent. |
| 338 | llvm::DenseMap<Decl *, Decl *> TentativeEquivalences; |
| 339 | |
| 340 | /// \brief Queue of declarations in the first context whose equivalence |
| 341 | /// with a declaration in the second context still needs to be verified. |
| 342 | std::deque<Decl *> DeclsToCheck; |
| 343 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 344 | /// \brief Declaration (from, to) pairs that are known not to be equivalent |
| 345 | /// (which we have already complained about). |
| 346 | llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls; |
| 347 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 348 | /// \brief Whether we're being strict about the spelling of types when |
| 349 | /// unifying two types. |
| 350 | bool StrictTypeSpelling; |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 351 | |
| 352 | /// \brief Whether to complain about failures. |
| 353 | bool Complain; |
| 354 | |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 355 | /// \brief \c true if the last diagnostic came from C2. |
| 356 | bool LastDiagFromC2; |
| 357 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 358 | StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2, |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 359 | llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls, |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 360 | bool StrictTypeSpelling = false, |
| 361 | bool Complain = true) |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 362 | : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls), |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 363 | StrictTypeSpelling(StrictTypeSpelling), Complain(Complain), |
| 364 | LastDiagFromC2(false) {} |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 365 | |
| 366 | /// \brief Determine whether the two declarations are structurally |
| 367 | /// equivalent. |
| 368 | bool IsStructurallyEquivalent(Decl *D1, Decl *D2); |
| 369 | |
| 370 | /// \brief Determine whether the two types are structurally equivalent. |
| 371 | bool IsStructurallyEquivalent(QualType T1, QualType T2); |
| 372 | |
| 373 | private: |
| 374 | /// \brief Finish checking all of the structural equivalences. |
| 375 | /// |
| 376 | /// \returns true if an error occurred, false otherwise. |
| 377 | bool Finish(); |
| 378 | |
| 379 | public: |
| 380 | DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 381 | assert(Complain && "Not allowed to complain"); |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 382 | if (LastDiagFromC2) |
| 383 | C1.getDiagnostics().notePriorDiagnosticFrom(C2.getDiagnostics()); |
| 384 | LastDiagFromC2 = false; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 385 | return C1.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 389 | assert(Complain && "Not allowed to complain"); |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 390 | if (!LastDiagFromC2) |
| 391 | C2.getDiagnostics().notePriorDiagnosticFrom(C1.getDiagnostics()); |
| 392 | LastDiagFromC2 = true; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 393 | return C2.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 394 | } |
| 395 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 396 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 397 | |
| 398 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 399 | QualType T1, QualType T2); |
| 400 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 401 | Decl *D1, Decl *D2); |
Aleksei Sidorin | 855086d | 2017-01-23 09:30:36 +0000 | [diff] [blame] | 402 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 403 | const TemplateArgument &Arg1, |
| 404 | const TemplateArgument &Arg2); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 405 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 406 | /// \brief Determine structural equivalence of two expressions. |
| 407 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 408 | Expr *E1, Expr *E2) { |
| 409 | if (!E1 || !E2) |
| 410 | return E1 == E2; |
| 411 | |
| 412 | // FIXME: Actually perform a structural comparison! |
| 413 | return true; |
| 414 | } |
| 415 | |
| 416 | /// \brief Determine whether two identifiers are equivalent. |
| 417 | static bool IsStructurallyEquivalent(const IdentifierInfo *Name1, |
| 418 | const IdentifierInfo *Name2) { |
| 419 | if (!Name1 || !Name2) |
| 420 | return Name1 == Name2; |
| 421 | |
| 422 | return Name1->getName() == Name2->getName(); |
| 423 | } |
| 424 | |
| 425 | /// \brief Determine whether two nested-name-specifiers are equivalent. |
| 426 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 427 | NestedNameSpecifier *NNS1, |
| 428 | NestedNameSpecifier *NNS2) { |
Aleksei Sidorin | 855086d | 2017-01-23 09:30:36 +0000 | [diff] [blame] | 429 | if (NNS1->getKind() != NNS2->getKind()) |
| 430 | return false; |
| 431 | |
| 432 | NestedNameSpecifier *Prefix1 = NNS1->getPrefix(), |
| 433 | *Prefix2 = NNS2->getPrefix(); |
| 434 | if ((bool)Prefix1 != (bool)Prefix2) |
| 435 | return false; |
| 436 | |
| 437 | if (Prefix1) |
| 438 | if (!IsStructurallyEquivalent(Context, Prefix1, Prefix2)) |
| 439 | return false; |
| 440 | |
| 441 | switch (NNS1->getKind()) { |
| 442 | case NestedNameSpecifier::Identifier: |
| 443 | return IsStructurallyEquivalent(NNS1->getAsIdentifier(), |
| 444 | NNS2->getAsIdentifier()); |
| 445 | case NestedNameSpecifier::Namespace: |
| 446 | return IsStructurallyEquivalent(Context, NNS1->getAsNamespace(), |
| 447 | NNS2->getAsNamespace()); |
| 448 | case NestedNameSpecifier::NamespaceAlias: |
| 449 | return IsStructurallyEquivalent(Context, NNS1->getAsNamespaceAlias(), |
| 450 | NNS2->getAsNamespaceAlias()); |
| 451 | case NestedNameSpecifier::TypeSpec: |
| 452 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 453 | return IsStructurallyEquivalent(Context, QualType(NNS1->getAsType(), 0), |
| 454 | QualType(NNS2->getAsType(), 0)); |
| 455 | case NestedNameSpecifier::Global: |
| 456 | return true; |
| 457 | case NestedNameSpecifier::Super: |
| 458 | return IsStructurallyEquivalent(Context, NNS1->getAsRecordDecl(), |
| 459 | NNS2->getAsRecordDecl()); |
| 460 | } |
| 461 | return false; |
| 462 | } |
| 463 | |
| 464 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 465 | const TemplateName &N1, |
| 466 | const TemplateName &N2) { |
| 467 | if (N1.getKind() != N2.getKind()) |
| 468 | return false; |
| 469 | switch (N1.getKind()) { |
| 470 | case TemplateName::Template: |
| 471 | return IsStructurallyEquivalent(Context, N1.getAsTemplateDecl(), |
| 472 | N2.getAsTemplateDecl()); |
| 473 | |
| 474 | case TemplateName::OverloadedTemplate: { |
| 475 | OverloadedTemplateStorage *OS1 = N1.getAsOverloadedTemplate(), |
| 476 | *OS2 = N2.getAsOverloadedTemplate(); |
| 477 | OverloadedTemplateStorage::iterator I1 = OS1->begin(), I2 = OS2->begin(), |
| 478 | E1 = OS1->end(), E2 = OS2->end(); |
| 479 | for (; I1 != E1 && I2 != E2; ++I1, ++I2) |
| 480 | if (!IsStructurallyEquivalent(Context, *I1, *I2)) |
| 481 | return false; |
| 482 | return I1 == E1 && I2 == E2; |
| 483 | } |
| 484 | |
| 485 | case TemplateName::QualifiedTemplate: { |
| 486 | QualifiedTemplateName *QN1 = N1.getAsQualifiedTemplateName(), |
| 487 | *QN2 = N2.getAsQualifiedTemplateName(); |
| 488 | return IsStructurallyEquivalent(Context, QN1->getDecl(), QN2->getDecl()) && |
| 489 | IsStructurallyEquivalent(Context, QN1->getQualifier(), |
| 490 | QN2->getQualifier()); |
| 491 | } |
| 492 | |
| 493 | case TemplateName::DependentTemplate: { |
| 494 | DependentTemplateName *DN1 = N1.getAsDependentTemplateName(), |
| 495 | *DN2 = N2.getAsDependentTemplateName(); |
| 496 | if (!IsStructurallyEquivalent(Context, DN1->getQualifier(), |
| 497 | DN2->getQualifier())) |
| 498 | return false; |
| 499 | if (DN1->isIdentifier() && DN2->isIdentifier()) |
| 500 | return IsStructurallyEquivalent(DN1->getIdentifier(), |
| 501 | DN2->getIdentifier()); |
| 502 | else if (DN1->isOverloadedOperator() && DN2->isOverloadedOperator()) |
| 503 | return DN1->getOperator() == DN2->getOperator(); |
| 504 | return false; |
| 505 | } |
| 506 | |
| 507 | case TemplateName::SubstTemplateTemplateParm: { |
| 508 | SubstTemplateTemplateParmStorage *TS1 = N1.getAsSubstTemplateTemplateParm(), |
| 509 | *TS2 = N2.getAsSubstTemplateTemplateParm(); |
| 510 | return IsStructurallyEquivalent(Context, TS1->getParameter(), |
| 511 | TS2->getParameter()) && |
| 512 | IsStructurallyEquivalent(Context, TS1->getReplacement(), |
| 513 | TS2->getReplacement()); |
| 514 | } |
| 515 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 516 | SubstTemplateTemplateParmPackStorage |
| 517 | *P1 = N1.getAsSubstTemplateTemplateParmPack(), |
| 518 | *P2 = N2.getAsSubstTemplateTemplateParmPack(); |
| 519 | return IsStructurallyEquivalent(Context, P1->getArgumentPack(), |
| 520 | P2->getArgumentPack()) && |
| 521 | IsStructurallyEquivalent(Context, P1->getParameterPack(), |
| 522 | P2->getParameterPack()); |
| 523 | } |
| 524 | } |
| 525 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | /// \brief Determine whether two template arguments are equivalent. |
| 529 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 530 | const TemplateArgument &Arg1, |
| 531 | const TemplateArgument &Arg2) { |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 532 | if (Arg1.getKind() != Arg2.getKind()) |
| 533 | return false; |
| 534 | |
| 535 | switch (Arg1.getKind()) { |
| 536 | case TemplateArgument::Null: |
| 537 | return true; |
| 538 | |
| 539 | case TemplateArgument::Type: |
| 540 | return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType()); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 541 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 542 | case TemplateArgument::Integral: |
| 543 | if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(), |
| 544 | Arg2.getIntegralType())) |
| 545 | return false; |
| 546 | |
Eric Christopher | 6dcc376 | 2012-07-15 00:23:57 +0000 | [diff] [blame] | 547 | return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral()); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 548 | |
| 549 | case TemplateArgument::Declaration: |
| 550 | return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl()); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 551 | |
| 552 | case TemplateArgument::NullPtr: |
| 553 | return true; // FIXME: Is this correct? |
| 554 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 555 | case TemplateArgument::Template: |
| 556 | return IsStructurallyEquivalent(Context, |
| 557 | Arg1.getAsTemplate(), |
| 558 | Arg2.getAsTemplate()); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 559 | |
| 560 | case TemplateArgument::TemplateExpansion: |
| 561 | return IsStructurallyEquivalent(Context, |
| 562 | Arg1.getAsTemplateOrTemplatePattern(), |
| 563 | Arg2.getAsTemplateOrTemplatePattern()); |
| 564 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 565 | case TemplateArgument::Expression: |
| 566 | return IsStructurallyEquivalent(Context, |
| 567 | Arg1.getAsExpr(), Arg2.getAsExpr()); |
| 568 | |
| 569 | case TemplateArgument::Pack: |
| 570 | if (Arg1.pack_size() != Arg2.pack_size()) |
| 571 | return false; |
| 572 | |
| 573 | for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I) |
| 574 | if (!IsStructurallyEquivalent(Context, |
| 575 | Arg1.pack_begin()[I], |
| 576 | Arg2.pack_begin()[I])) |
| 577 | return false; |
| 578 | |
| 579 | return true; |
| 580 | } |
| 581 | |
| 582 | llvm_unreachable("Invalid template argument kind"); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | /// \brief Determine structural equivalence for the common part of array |
| 586 | /// types. |
| 587 | static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 588 | const ArrayType *Array1, |
| 589 | const ArrayType *Array2) { |
| 590 | if (!IsStructurallyEquivalent(Context, |
| 591 | Array1->getElementType(), |
| 592 | Array2->getElementType())) |
| 593 | return false; |
| 594 | if (Array1->getSizeModifier() != Array2->getSizeModifier()) |
| 595 | return false; |
| 596 | if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers()) |
| 597 | return false; |
| 598 | |
| 599 | return true; |
| 600 | } |
| 601 | |
| 602 | /// \brief Determine structural equivalence of two types. |
| 603 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 604 | QualType T1, QualType T2) { |
| 605 | if (T1.isNull() || T2.isNull()) |
| 606 | return T1.isNull() && T2.isNull(); |
| 607 | |
| 608 | if (!Context.StrictTypeSpelling) { |
| 609 | // We aren't being strict about token-to-token equivalence of types, |
| 610 | // so map down to the canonical type. |
| 611 | T1 = Context.C1.getCanonicalType(T1); |
| 612 | T2 = Context.C2.getCanonicalType(T2); |
| 613 | } |
| 614 | |
| 615 | if (T1.getQualifiers() != T2.getQualifiers()) |
| 616 | return false; |
| 617 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 618 | Type::TypeClass TC = T1->getTypeClass(); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 619 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 620 | if (T1->getTypeClass() != T2->getTypeClass()) { |
| 621 | // Compare function types with prototypes vs. without prototypes as if |
| 622 | // both did not have prototypes. |
| 623 | if (T1->getTypeClass() == Type::FunctionProto && |
| 624 | T2->getTypeClass() == Type::FunctionNoProto) |
| 625 | TC = Type::FunctionNoProto; |
| 626 | else if (T1->getTypeClass() == Type::FunctionNoProto && |
| 627 | T2->getTypeClass() == Type::FunctionProto) |
| 628 | TC = Type::FunctionNoProto; |
| 629 | else |
| 630 | return false; |
| 631 | } |
| 632 | |
| 633 | switch (TC) { |
| 634 | case Type::Builtin: |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 635 | // FIXME: Deal with Char_S/Char_U. |
| 636 | if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind()) |
| 637 | return false; |
| 638 | break; |
| 639 | |
| 640 | case Type::Complex: |
| 641 | if (!IsStructurallyEquivalent(Context, |
| 642 | cast<ComplexType>(T1)->getElementType(), |
| 643 | cast<ComplexType>(T2)->getElementType())) |
| 644 | return false; |
| 645 | break; |
| 646 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 647 | case Type::Adjusted: |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 648 | case Type::Decayed: |
| 649 | if (!IsStructurallyEquivalent(Context, |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 650 | cast<AdjustedType>(T1)->getOriginalType(), |
| 651 | cast<AdjustedType>(T2)->getOriginalType())) |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 652 | return false; |
| 653 | break; |
| 654 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 655 | case Type::Pointer: |
| 656 | if (!IsStructurallyEquivalent(Context, |
| 657 | cast<PointerType>(T1)->getPointeeType(), |
| 658 | cast<PointerType>(T2)->getPointeeType())) |
| 659 | return false; |
| 660 | break; |
| 661 | |
| 662 | case Type::BlockPointer: |
| 663 | if (!IsStructurallyEquivalent(Context, |
| 664 | cast<BlockPointerType>(T1)->getPointeeType(), |
| 665 | cast<BlockPointerType>(T2)->getPointeeType())) |
| 666 | return false; |
| 667 | break; |
| 668 | |
| 669 | case Type::LValueReference: |
| 670 | case Type::RValueReference: { |
| 671 | const ReferenceType *Ref1 = cast<ReferenceType>(T1); |
| 672 | const ReferenceType *Ref2 = cast<ReferenceType>(T2); |
| 673 | if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue()) |
| 674 | return false; |
| 675 | if (Ref1->isInnerRef() != Ref2->isInnerRef()) |
| 676 | return false; |
| 677 | if (!IsStructurallyEquivalent(Context, |
| 678 | Ref1->getPointeeTypeAsWritten(), |
| 679 | Ref2->getPointeeTypeAsWritten())) |
| 680 | return false; |
| 681 | break; |
| 682 | } |
| 683 | |
| 684 | case Type::MemberPointer: { |
| 685 | const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1); |
| 686 | const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2); |
| 687 | if (!IsStructurallyEquivalent(Context, |
| 688 | MemPtr1->getPointeeType(), |
| 689 | MemPtr2->getPointeeType())) |
| 690 | return false; |
| 691 | if (!IsStructurallyEquivalent(Context, |
| 692 | QualType(MemPtr1->getClass(), 0), |
| 693 | QualType(MemPtr2->getClass(), 0))) |
| 694 | return false; |
| 695 | break; |
| 696 | } |
| 697 | |
| 698 | case Type::ConstantArray: { |
| 699 | const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1); |
| 700 | const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2); |
Eric Christopher | 6dcc376 | 2012-07-15 00:23:57 +0000 | [diff] [blame] | 701 | if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 702 | return false; |
| 703 | |
| 704 | if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) |
| 705 | return false; |
| 706 | break; |
| 707 | } |
| 708 | |
| 709 | case Type::IncompleteArray: |
| 710 | if (!IsArrayStructurallyEquivalent(Context, |
| 711 | cast<ArrayType>(T1), |
| 712 | cast<ArrayType>(T2))) |
| 713 | return false; |
| 714 | break; |
| 715 | |
| 716 | case Type::VariableArray: { |
| 717 | const VariableArrayType *Array1 = cast<VariableArrayType>(T1); |
| 718 | const VariableArrayType *Array2 = cast<VariableArrayType>(T2); |
| 719 | if (!IsStructurallyEquivalent(Context, |
| 720 | Array1->getSizeExpr(), Array2->getSizeExpr())) |
| 721 | return false; |
| 722 | |
| 723 | if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) |
| 724 | return false; |
| 725 | |
| 726 | break; |
| 727 | } |
| 728 | |
| 729 | case Type::DependentSizedArray: { |
| 730 | const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1); |
| 731 | const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2); |
| 732 | if (!IsStructurallyEquivalent(Context, |
| 733 | Array1->getSizeExpr(), Array2->getSizeExpr())) |
| 734 | return false; |
| 735 | |
| 736 | if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) |
| 737 | return false; |
| 738 | |
| 739 | break; |
| 740 | } |
| 741 | |
| 742 | case Type::DependentSizedExtVector: { |
| 743 | const DependentSizedExtVectorType *Vec1 |
| 744 | = cast<DependentSizedExtVectorType>(T1); |
| 745 | const DependentSizedExtVectorType *Vec2 |
| 746 | = cast<DependentSizedExtVectorType>(T2); |
| 747 | if (!IsStructurallyEquivalent(Context, |
| 748 | Vec1->getSizeExpr(), Vec2->getSizeExpr())) |
| 749 | return false; |
| 750 | if (!IsStructurallyEquivalent(Context, |
| 751 | Vec1->getElementType(), |
| 752 | Vec2->getElementType())) |
| 753 | return false; |
| 754 | break; |
| 755 | } |
| 756 | |
| 757 | case Type::Vector: |
| 758 | case Type::ExtVector: { |
| 759 | const VectorType *Vec1 = cast<VectorType>(T1); |
| 760 | const VectorType *Vec2 = cast<VectorType>(T2); |
| 761 | if (!IsStructurallyEquivalent(Context, |
| 762 | Vec1->getElementType(), |
| 763 | Vec2->getElementType())) |
| 764 | return false; |
| 765 | if (Vec1->getNumElements() != Vec2->getNumElements()) |
| 766 | return false; |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 767 | if (Vec1->getVectorKind() != Vec2->getVectorKind()) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 768 | return false; |
Douglas Gregor | 01cc437 | 2010-02-19 01:36:36 +0000 | [diff] [blame] | 769 | break; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | case Type::FunctionProto: { |
| 773 | const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1); |
| 774 | const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 775 | if (Proto1->getNumParams() != Proto2->getNumParams()) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 776 | return false; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 777 | for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) { |
| 778 | if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I), |
| 779 | Proto2->getParamType(I))) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 780 | return false; |
| 781 | } |
| 782 | if (Proto1->isVariadic() != Proto2->isVariadic()) |
| 783 | return false; |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 784 | if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType()) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 785 | return false; |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 786 | if (Proto1->getExceptionSpecType() == EST_Dynamic) { |
| 787 | if (Proto1->getNumExceptions() != Proto2->getNumExceptions()) |
| 788 | return false; |
| 789 | for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) { |
| 790 | if (!IsStructurallyEquivalent(Context, |
| 791 | Proto1->getExceptionType(I), |
| 792 | Proto2->getExceptionType(I))) |
| 793 | return false; |
| 794 | } |
| 795 | } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 796 | if (!IsStructurallyEquivalent(Context, |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 797 | Proto1->getNoexceptExpr(), |
| 798 | Proto2->getNoexceptExpr())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 799 | return false; |
| 800 | } |
| 801 | if (Proto1->getTypeQuals() != Proto2->getTypeQuals()) |
| 802 | return false; |
| 803 | |
| 804 | // Fall through to check the bits common with FunctionNoProtoType. |
| 805 | } |
| 806 | |
| 807 | case Type::FunctionNoProto: { |
| 808 | const FunctionType *Function1 = cast<FunctionType>(T1); |
| 809 | const FunctionType *Function2 = cast<FunctionType>(T2); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 810 | if (!IsStructurallyEquivalent(Context, Function1->getReturnType(), |
| 811 | Function2->getReturnType())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 812 | return false; |
Justin Bogner | 62c04de | 2016-03-20 16:58:03 +0000 | [diff] [blame] | 813 | if (Function1->getExtInfo() != Function2->getExtInfo()) |
| 814 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 815 | break; |
| 816 | } |
| 817 | |
| 818 | case Type::UnresolvedUsing: |
| 819 | if (!IsStructurallyEquivalent(Context, |
| 820 | cast<UnresolvedUsingType>(T1)->getDecl(), |
| 821 | cast<UnresolvedUsingType>(T2)->getDecl())) |
| 822 | return false; |
| 823 | |
| 824 | break; |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 825 | |
| 826 | case Type::Attributed: |
| 827 | if (!IsStructurallyEquivalent(Context, |
| 828 | cast<AttributedType>(T1)->getModifiedType(), |
| 829 | cast<AttributedType>(T2)->getModifiedType())) |
| 830 | return false; |
| 831 | if (!IsStructurallyEquivalent(Context, |
| 832 | cast<AttributedType>(T1)->getEquivalentType(), |
| 833 | cast<AttributedType>(T2)->getEquivalentType())) |
| 834 | return false; |
| 835 | break; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 836 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 837 | case Type::Paren: |
| 838 | if (!IsStructurallyEquivalent(Context, |
| 839 | cast<ParenType>(T1)->getInnerType(), |
| 840 | cast<ParenType>(T2)->getInnerType())) |
| 841 | return false; |
| 842 | break; |
| 843 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 844 | case Type::Typedef: |
| 845 | if (!IsStructurallyEquivalent(Context, |
| 846 | cast<TypedefType>(T1)->getDecl(), |
| 847 | cast<TypedefType>(T2)->getDecl())) |
| 848 | return false; |
| 849 | break; |
| 850 | |
| 851 | case Type::TypeOfExpr: |
| 852 | if (!IsStructurallyEquivalent(Context, |
| 853 | cast<TypeOfExprType>(T1)->getUnderlyingExpr(), |
| 854 | cast<TypeOfExprType>(T2)->getUnderlyingExpr())) |
| 855 | return false; |
| 856 | break; |
| 857 | |
| 858 | case Type::TypeOf: |
| 859 | if (!IsStructurallyEquivalent(Context, |
| 860 | cast<TypeOfType>(T1)->getUnderlyingType(), |
| 861 | cast<TypeOfType>(T2)->getUnderlyingType())) |
| 862 | return false; |
| 863 | break; |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 864 | |
| 865 | case Type::UnaryTransform: |
| 866 | if (!IsStructurallyEquivalent(Context, |
| 867 | cast<UnaryTransformType>(T1)->getUnderlyingType(), |
| 868 | cast<UnaryTransformType>(T1)->getUnderlyingType())) |
| 869 | return false; |
| 870 | break; |
| 871 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 872 | case Type::Decltype: |
| 873 | if (!IsStructurallyEquivalent(Context, |
| 874 | cast<DecltypeType>(T1)->getUnderlyingExpr(), |
| 875 | cast<DecltypeType>(T2)->getUnderlyingExpr())) |
| 876 | return false; |
| 877 | break; |
| 878 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 879 | case Type::Auto: |
| 880 | if (!IsStructurallyEquivalent(Context, |
| 881 | cast<AutoType>(T1)->getDeducedType(), |
| 882 | cast<AutoType>(T2)->getDeducedType())) |
| 883 | return false; |
| 884 | break; |
| 885 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 886 | case Type::Record: |
| 887 | case Type::Enum: |
| 888 | if (!IsStructurallyEquivalent(Context, |
| 889 | cast<TagType>(T1)->getDecl(), |
| 890 | cast<TagType>(T2)->getDecl())) |
| 891 | return false; |
| 892 | break; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 893 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 894 | case Type::TemplateTypeParm: { |
| 895 | const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1); |
| 896 | const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2); |
| 897 | if (Parm1->getDepth() != Parm2->getDepth()) |
| 898 | return false; |
| 899 | if (Parm1->getIndex() != Parm2->getIndex()) |
| 900 | return false; |
| 901 | if (Parm1->isParameterPack() != Parm2->isParameterPack()) |
| 902 | return false; |
| 903 | |
| 904 | // Names of template type parameters are never significant. |
| 905 | break; |
| 906 | } |
| 907 | |
| 908 | case Type::SubstTemplateTypeParm: { |
| 909 | const SubstTemplateTypeParmType *Subst1 |
| 910 | = cast<SubstTemplateTypeParmType>(T1); |
| 911 | const SubstTemplateTypeParmType *Subst2 |
| 912 | = cast<SubstTemplateTypeParmType>(T2); |
| 913 | if (!IsStructurallyEquivalent(Context, |
| 914 | QualType(Subst1->getReplacedParameter(), 0), |
| 915 | QualType(Subst2->getReplacedParameter(), 0))) |
| 916 | return false; |
| 917 | if (!IsStructurallyEquivalent(Context, |
| 918 | Subst1->getReplacementType(), |
| 919 | Subst2->getReplacementType())) |
| 920 | return false; |
| 921 | break; |
| 922 | } |
| 923 | |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 924 | case Type::SubstTemplateTypeParmPack: { |
| 925 | const SubstTemplateTypeParmPackType *Subst1 |
| 926 | = cast<SubstTemplateTypeParmPackType>(T1); |
| 927 | const SubstTemplateTypeParmPackType *Subst2 |
| 928 | = cast<SubstTemplateTypeParmPackType>(T2); |
| 929 | if (!IsStructurallyEquivalent(Context, |
| 930 | QualType(Subst1->getReplacedParameter(), 0), |
| 931 | QualType(Subst2->getReplacedParameter(), 0))) |
| 932 | return false; |
| 933 | if (!IsStructurallyEquivalent(Context, |
| 934 | Subst1->getArgumentPack(), |
| 935 | Subst2->getArgumentPack())) |
| 936 | return false; |
| 937 | break; |
| 938 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 939 | case Type::TemplateSpecialization: { |
| 940 | const TemplateSpecializationType *Spec1 |
| 941 | = cast<TemplateSpecializationType>(T1); |
| 942 | const TemplateSpecializationType *Spec2 |
| 943 | = cast<TemplateSpecializationType>(T2); |
| 944 | if (!IsStructurallyEquivalent(Context, |
| 945 | Spec1->getTemplateName(), |
| 946 | Spec2->getTemplateName())) |
| 947 | return false; |
| 948 | if (Spec1->getNumArgs() != Spec2->getNumArgs()) |
| 949 | return false; |
| 950 | for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) { |
| 951 | if (!IsStructurallyEquivalent(Context, |
| 952 | Spec1->getArg(I), Spec2->getArg(I))) |
| 953 | return false; |
| 954 | } |
| 955 | break; |
| 956 | } |
| 957 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 958 | case Type::Elaborated: { |
| 959 | const ElaboratedType *Elab1 = cast<ElaboratedType>(T1); |
| 960 | const ElaboratedType *Elab2 = cast<ElaboratedType>(T2); |
| 961 | // CHECKME: what if a keyword is ETK_None or ETK_typename ? |
| 962 | if (Elab1->getKeyword() != Elab2->getKeyword()) |
| 963 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 964 | if (!IsStructurallyEquivalent(Context, |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 965 | Elab1->getQualifier(), |
| 966 | Elab2->getQualifier())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 967 | return false; |
| 968 | if (!IsStructurallyEquivalent(Context, |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 969 | Elab1->getNamedType(), |
| 970 | Elab2->getNamedType())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 971 | return false; |
| 972 | break; |
| 973 | } |
| 974 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 975 | case Type::InjectedClassName: { |
| 976 | const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1); |
| 977 | const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2); |
| 978 | if (!IsStructurallyEquivalent(Context, |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 979 | Inj1->getInjectedSpecializationType(), |
| 980 | Inj2->getInjectedSpecializationType())) |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 981 | return false; |
| 982 | break; |
| 983 | } |
| 984 | |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 985 | case Type::DependentName: { |
| 986 | const DependentNameType *Typename1 = cast<DependentNameType>(T1); |
| 987 | const DependentNameType *Typename2 = cast<DependentNameType>(T2); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 988 | if (!IsStructurallyEquivalent(Context, |
| 989 | Typename1->getQualifier(), |
| 990 | Typename2->getQualifier())) |
| 991 | return false; |
| 992 | if (!IsStructurallyEquivalent(Typename1->getIdentifier(), |
| 993 | Typename2->getIdentifier())) |
| 994 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 995 | |
| 996 | break; |
| 997 | } |
| 998 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 999 | case Type::DependentTemplateSpecialization: { |
| 1000 | const DependentTemplateSpecializationType *Spec1 = |
| 1001 | cast<DependentTemplateSpecializationType>(T1); |
| 1002 | const DependentTemplateSpecializationType *Spec2 = |
| 1003 | cast<DependentTemplateSpecializationType>(T2); |
| 1004 | if (!IsStructurallyEquivalent(Context, |
| 1005 | Spec1->getQualifier(), |
| 1006 | Spec2->getQualifier())) |
| 1007 | return false; |
| 1008 | if (!IsStructurallyEquivalent(Spec1->getIdentifier(), |
| 1009 | Spec2->getIdentifier())) |
| 1010 | return false; |
| 1011 | if (Spec1->getNumArgs() != Spec2->getNumArgs()) |
| 1012 | return false; |
| 1013 | for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) { |
| 1014 | if (!IsStructurallyEquivalent(Context, |
| 1015 | Spec1->getArg(I), Spec2->getArg(I))) |
| 1016 | return false; |
| 1017 | } |
| 1018 | break; |
| 1019 | } |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 1020 | |
| 1021 | case Type::PackExpansion: |
| 1022 | if (!IsStructurallyEquivalent(Context, |
| 1023 | cast<PackExpansionType>(T1)->getPattern(), |
| 1024 | cast<PackExpansionType>(T2)->getPattern())) |
| 1025 | return false; |
| 1026 | break; |
| 1027 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1028 | case Type::ObjCInterface: { |
| 1029 | const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1); |
| 1030 | const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2); |
| 1031 | if (!IsStructurallyEquivalent(Context, |
| 1032 | Iface1->getDecl(), Iface2->getDecl())) |
| 1033 | return false; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1034 | break; |
| 1035 | } |
| 1036 | |
Manman Ren | e6be26c | 2016-09-13 17:25:08 +0000 | [diff] [blame] | 1037 | case Type::ObjCTypeParam: { |
| 1038 | const ObjCTypeParamType *Obj1 = cast<ObjCTypeParamType>(T1); |
| 1039 | const ObjCTypeParamType *Obj2 = cast<ObjCTypeParamType>(T2); |
| 1040 | if (!IsStructurallyEquivalent(Context, Obj1->getDecl(), |
| 1041 | Obj2->getDecl())) |
| 1042 | return false; |
| 1043 | |
| 1044 | if (Obj1->getNumProtocols() != Obj2->getNumProtocols()) |
| 1045 | return false; |
| 1046 | for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) { |
| 1047 | if (!IsStructurallyEquivalent(Context, |
| 1048 | Obj1->getProtocol(I), |
| 1049 | Obj2->getProtocol(I))) |
| 1050 | return false; |
| 1051 | } |
| 1052 | break; |
| 1053 | } |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1054 | case Type::ObjCObject: { |
| 1055 | const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1); |
| 1056 | const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2); |
| 1057 | if (!IsStructurallyEquivalent(Context, |
| 1058 | Obj1->getBaseType(), |
| 1059 | Obj2->getBaseType())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1060 | return false; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1061 | if (Obj1->getNumProtocols() != Obj2->getNumProtocols()) |
| 1062 | return false; |
| 1063 | for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1064 | if (!IsStructurallyEquivalent(Context, |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1065 | Obj1->getProtocol(I), |
| 1066 | Obj2->getProtocol(I))) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1067 | return false; |
| 1068 | } |
| 1069 | break; |
| 1070 | } |
| 1071 | |
| 1072 | case Type::ObjCObjectPointer: { |
| 1073 | const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1); |
| 1074 | const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2); |
| 1075 | if (!IsStructurallyEquivalent(Context, |
| 1076 | Ptr1->getPointeeType(), |
| 1077 | Ptr2->getPointeeType())) |
| 1078 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1079 | break; |
| 1080 | } |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1081 | |
| 1082 | case Type::Atomic: { |
| 1083 | if (!IsStructurallyEquivalent(Context, |
| 1084 | cast<AtomicType>(T1)->getValueType(), |
| 1085 | cast<AtomicType>(T2)->getValueType())) |
| 1086 | return false; |
| 1087 | break; |
| 1088 | } |
| 1089 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 1090 | case Type::Pipe: { |
| 1091 | if (!IsStructurallyEquivalent(Context, |
| 1092 | cast<PipeType>(T1)->getElementType(), |
| 1093 | cast<PipeType>(T2)->getElementType())) |
| 1094 | return false; |
| 1095 | break; |
| 1096 | } |
| 1097 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1098 | } // end switch |
| 1099 | |
| 1100 | return true; |
| 1101 | } |
| 1102 | |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 1103 | /// \brief Determine structural equivalence of two fields. |
| 1104 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1105 | FieldDecl *Field1, FieldDecl *Field2) { |
| 1106 | RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext()); |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 1107 | |
| 1108 | // For anonymous structs/unions, match up the anonymous struct/union type |
| 1109 | // declarations directly, so that we don't go off searching for anonymous |
| 1110 | // types |
| 1111 | if (Field1->isAnonymousStructOrUnion() && |
| 1112 | Field2->isAnonymousStructOrUnion()) { |
| 1113 | RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl(); |
| 1114 | RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl(); |
| 1115 | return IsStructurallyEquivalent(Context, D1, D2); |
| 1116 | } |
Sean Callanan | 969c5bd | 2013-04-26 22:49:25 +0000 | [diff] [blame] | 1117 | |
| 1118 | // Check for equivalent field names. |
| 1119 | IdentifierInfo *Name1 = Field1->getIdentifier(); |
| 1120 | IdentifierInfo *Name2 = Field2->getIdentifier(); |
| 1121 | if (!::IsStructurallyEquivalent(Name1, Name2)) |
| 1122 | return false; |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 1123 | |
| 1124 | if (!IsStructurallyEquivalent(Context, |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 1125 | Field1->getType(), Field2->getType())) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1126 | if (Context.Complain) { |
| 1127 | Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1128 | << Context.C2.getTypeDeclType(Owner2); |
| 1129 | Context.Diag2(Field2->getLocation(), diag::note_odr_field) |
| 1130 | << Field2->getDeclName() << Field2->getType(); |
| 1131 | Context.Diag1(Field1->getLocation(), diag::note_odr_field) |
| 1132 | << Field1->getDeclName() << Field1->getType(); |
| 1133 | } |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 1134 | return false; |
| 1135 | } |
| 1136 | |
| 1137 | if (Field1->isBitField() != Field2->isBitField()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1138 | if (Context.Complain) { |
| 1139 | Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1140 | << Context.C2.getTypeDeclType(Owner2); |
| 1141 | if (Field1->isBitField()) { |
| 1142 | Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field) |
| 1143 | << Field1->getDeclName() << Field1->getType() |
| 1144 | << Field1->getBitWidthValue(Context.C1); |
| 1145 | Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field) |
| 1146 | << Field2->getDeclName(); |
| 1147 | } else { |
| 1148 | Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field) |
| 1149 | << Field2->getDeclName() << Field2->getType() |
| 1150 | << Field2->getBitWidthValue(Context.C2); |
| 1151 | Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field) |
| 1152 | << Field1->getDeclName(); |
| 1153 | } |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 1154 | } |
| 1155 | return false; |
| 1156 | } |
| 1157 | |
| 1158 | if (Field1->isBitField()) { |
| 1159 | // Make sure that the bit-fields are the same length. |
| 1160 | unsigned Bits1 = Field1->getBitWidthValue(Context.C1); |
| 1161 | unsigned Bits2 = Field2->getBitWidthValue(Context.C2); |
| 1162 | |
| 1163 | if (Bits1 != Bits2) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1164 | if (Context.Complain) { |
| 1165 | Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1166 | << Context.C2.getTypeDeclType(Owner2); |
| 1167 | Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field) |
| 1168 | << Field2->getDeclName() << Field2->getType() << Bits2; |
| 1169 | Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field) |
| 1170 | << Field1->getDeclName() << Field1->getType() << Bits1; |
| 1171 | } |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 1172 | return false; |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | return true; |
| 1177 | } |
| 1178 | |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 1179 | /// \brief Find the index of the given anonymous struct/union within its |
| 1180 | /// context. |
| 1181 | /// |
| 1182 | /// \returns Returns the index of this anonymous struct/union in its context, |
| 1183 | /// including the next assigned index (if none of them match). Returns an |
| 1184 | /// empty option if the context is not a record, i.e.. if the anonymous |
| 1185 | /// struct/union is at namespace or block scope. |
Sean Callanan | 488f861 | 2016-07-14 19:53:44 +0000 | [diff] [blame] | 1186 | static Optional<unsigned> findUntaggedStructOrUnionIndex(RecordDecl *Anon) { |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 1187 | ASTContext &Context = Anon->getASTContext(); |
| 1188 | QualType AnonTy = Context.getRecordType(Anon); |
| 1189 | |
| 1190 | RecordDecl *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext()); |
| 1191 | if (!Owner) |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 1192 | return None; |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 1193 | |
| 1194 | unsigned Index = 0; |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 1195 | for (const auto *D : Owner->noload_decls()) { |
| 1196 | const auto *F = dyn_cast<FieldDecl>(D); |
Sean Callanan | 488f861 | 2016-07-14 19:53:44 +0000 | [diff] [blame] | 1197 | if (!F) |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 1198 | continue; |
| 1199 | |
Sean Callanan | 488f861 | 2016-07-14 19:53:44 +0000 | [diff] [blame] | 1200 | if (F->isAnonymousStructOrUnion()) { |
| 1201 | if (Context.hasSameType(F->getType(), AnonTy)) |
| 1202 | break; |
| 1203 | ++Index; |
| 1204 | continue; |
| 1205 | } |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 1206 | |
Sean Callanan | 488f861 | 2016-07-14 19:53:44 +0000 | [diff] [blame] | 1207 | // If the field looks like this: |
| 1208 | // struct { ... } A; |
| 1209 | QualType FieldType = F->getType(); |
| 1210 | if (const auto *RecType = dyn_cast<RecordType>(FieldType)) { |
| 1211 | const RecordDecl *RecDecl = RecType->getDecl(); |
| 1212 | if (RecDecl->getDeclContext() == Owner && |
| 1213 | !RecDecl->getIdentifier()) { |
| 1214 | if (Context.hasSameType(FieldType, AnonTy)) |
| 1215 | break; |
| 1216 | ++Index; |
| 1217 | continue; |
| 1218 | } |
| 1219 | } |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | return Index; |
| 1223 | } |
| 1224 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1225 | /// \brief Determine structural equivalence of two records. |
| 1226 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1227 | RecordDecl *D1, RecordDecl *D2) { |
| 1228 | if (D1->isUnion() != D2->isUnion()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1229 | if (Context.Complain) { |
| 1230 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1231 | << Context.C2.getTypeDeclType(D2); |
| 1232 | Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here) |
| 1233 | << D1->getDeclName() << (unsigned)D1->getTagKind(); |
| 1234 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1235 | return false; |
| 1236 | } |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 1237 | |
| 1238 | if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) { |
| 1239 | // If both anonymous structs/unions are in a record context, make sure |
| 1240 | // they occur in the same location in the context records. |
Sean Callanan | 488f861 | 2016-07-14 19:53:44 +0000 | [diff] [blame] | 1241 | if (Optional<unsigned> Index1 = findUntaggedStructOrUnionIndex(D1)) { |
| 1242 | if (Optional<unsigned> Index2 = findUntaggedStructOrUnionIndex(D2)) { |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 1243 | if (*Index1 != *Index2) |
| 1244 | return false; |
| 1245 | } |
| 1246 | } |
| 1247 | } |
| 1248 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1249 | // If both declarations are class template specializations, we know |
| 1250 | // the ODR applies, so check the template and template arguments. |
| 1251 | ClassTemplateSpecializationDecl *Spec1 |
| 1252 | = dyn_cast<ClassTemplateSpecializationDecl>(D1); |
| 1253 | ClassTemplateSpecializationDecl *Spec2 |
| 1254 | = dyn_cast<ClassTemplateSpecializationDecl>(D2); |
| 1255 | if (Spec1 && Spec2) { |
| 1256 | // Check that the specialized templates are the same. |
| 1257 | if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(), |
| 1258 | Spec2->getSpecializedTemplate())) |
| 1259 | return false; |
| 1260 | |
| 1261 | // Check that the template arguments are the same. |
| 1262 | if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size()) |
| 1263 | return false; |
| 1264 | |
| 1265 | for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I) |
| 1266 | if (!IsStructurallyEquivalent(Context, |
| 1267 | Spec1->getTemplateArgs().get(I), |
| 1268 | Spec2->getTemplateArgs().get(I))) |
| 1269 | return false; |
| 1270 | } |
| 1271 | // 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] | 1272 | // structures are different. |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1273 | else if (Spec1 || Spec2) |
| 1274 | return false; |
| 1275 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1276 | // Compare the definitions of these two records. If either or both are |
| 1277 | // incomplete, we assume that they are equivalent. |
| 1278 | D1 = D1->getDefinition(); |
| 1279 | D2 = D2->getDefinition(); |
| 1280 | if (!D1 || !D2) |
| 1281 | return true; |
| 1282 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1283 | if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) { |
| 1284 | if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) { |
| 1285 | if (D1CXX->getNumBases() != D2CXX->getNumBases()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1286 | if (Context.Complain) { |
| 1287 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1288 | << Context.C2.getTypeDeclType(D2); |
| 1289 | Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases) |
| 1290 | << D2CXX->getNumBases(); |
| 1291 | Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases) |
| 1292 | << D1CXX->getNumBases(); |
| 1293 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1294 | return false; |
| 1295 | } |
| 1296 | |
| 1297 | // Check the base classes. |
| 1298 | for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(), |
| 1299 | BaseEnd1 = D1CXX->bases_end(), |
| 1300 | Base2 = D2CXX->bases_begin(); |
| 1301 | Base1 != BaseEnd1; |
| 1302 | ++Base1, ++Base2) { |
| 1303 | if (!IsStructurallyEquivalent(Context, |
| 1304 | Base1->getType(), Base2->getType())) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1305 | if (Context.Complain) { |
| 1306 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1307 | << Context.C2.getTypeDeclType(D2); |
| 1308 | Context.Diag2(Base2->getLocStart(), diag::note_odr_base) |
| 1309 | << Base2->getType() |
| 1310 | << Base2->getSourceRange(); |
| 1311 | Context.Diag1(Base1->getLocStart(), diag::note_odr_base) |
| 1312 | << Base1->getType() |
| 1313 | << Base1->getSourceRange(); |
| 1314 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1315 | return false; |
| 1316 | } |
| 1317 | |
| 1318 | // Check virtual vs. non-virtual inheritance mismatch. |
| 1319 | if (Base1->isVirtual() != Base2->isVirtual()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1320 | if (Context.Complain) { |
| 1321 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1322 | << Context.C2.getTypeDeclType(D2); |
| 1323 | Context.Diag2(Base2->getLocStart(), |
| 1324 | diag::note_odr_virtual_base) |
| 1325 | << Base2->isVirtual() << Base2->getSourceRange(); |
| 1326 | Context.Diag1(Base1->getLocStart(), diag::note_odr_base) |
| 1327 | << Base1->isVirtual() |
| 1328 | << Base1->getSourceRange(); |
| 1329 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1330 | return false; |
| 1331 | } |
| 1332 | } |
| 1333 | } else if (D1CXX->getNumBases() > 0) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1334 | if (Context.Complain) { |
| 1335 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1336 | << Context.C2.getTypeDeclType(D2); |
| 1337 | const CXXBaseSpecifier *Base1 = D1CXX->bases_begin(); |
| 1338 | Context.Diag1(Base1->getLocStart(), diag::note_odr_base) |
| 1339 | << Base1->getType() |
| 1340 | << Base1->getSourceRange(); |
| 1341 | Context.Diag2(D2->getLocation(), diag::note_odr_missing_base); |
| 1342 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1343 | return false; |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | // Check the fields for consistency. |
Dmitri Gribenko | 898cff0 | 2012-05-19 17:17:26 +0000 | [diff] [blame] | 1348 | RecordDecl::field_iterator Field2 = D2->field_begin(), |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1349 | Field2End = D2->field_end(); |
Dmitri Gribenko | 898cff0 | 2012-05-19 17:17:26 +0000 | [diff] [blame] | 1350 | for (RecordDecl::field_iterator Field1 = D1->field_begin(), |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1351 | Field1End = D1->field_end(); |
| 1352 | Field1 != Field1End; |
| 1353 | ++Field1, ++Field2) { |
| 1354 | if (Field2 == Field2End) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1355 | if (Context.Complain) { |
| 1356 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1357 | << Context.C2.getTypeDeclType(D2); |
| 1358 | Context.Diag1(Field1->getLocation(), diag::note_odr_field) |
| 1359 | << Field1->getDeclName() << Field1->getType(); |
| 1360 | Context.Diag2(D2->getLocation(), diag::note_odr_missing_field); |
| 1361 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1362 | return false; |
| 1363 | } |
| 1364 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1365 | if (!IsStructurallyEquivalent(Context, *Field1, *Field2)) |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 1366 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | if (Field2 != Field2End) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1370 | if (Context.Complain) { |
| 1371 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1372 | << Context.C2.getTypeDeclType(D2); |
| 1373 | Context.Diag2(Field2->getLocation(), diag::note_odr_field) |
| 1374 | << Field2->getDeclName() << Field2->getType(); |
| 1375 | Context.Diag1(D1->getLocation(), diag::note_odr_missing_field); |
| 1376 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1377 | return false; |
| 1378 | } |
| 1379 | |
| 1380 | return true; |
| 1381 | } |
| 1382 | |
| 1383 | /// \brief Determine structural equivalence of two enums. |
| 1384 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1385 | EnumDecl *D1, EnumDecl *D2) { |
| 1386 | EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(), |
| 1387 | EC2End = D2->enumerator_end(); |
| 1388 | for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(), |
| 1389 | EC1End = D1->enumerator_end(); |
| 1390 | EC1 != EC1End; ++EC1, ++EC2) { |
| 1391 | if (EC2 == EC2End) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1392 | if (Context.Complain) { |
| 1393 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1394 | << Context.C2.getTypeDeclType(D2); |
| 1395 | Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator) |
| 1396 | << EC1->getDeclName() |
| 1397 | << EC1->getInitVal().toString(10); |
| 1398 | Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator); |
| 1399 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1400 | return false; |
| 1401 | } |
| 1402 | |
| 1403 | llvm::APSInt Val1 = EC1->getInitVal(); |
| 1404 | llvm::APSInt Val2 = EC2->getInitVal(); |
Eric Christopher | 6dcc376 | 2012-07-15 00:23:57 +0000 | [diff] [blame] | 1405 | if (!llvm::APSInt::isSameValue(Val1, Val2) || |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1406 | !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1407 | if (Context.Complain) { |
| 1408 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1409 | << Context.C2.getTypeDeclType(D2); |
| 1410 | Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator) |
| 1411 | << EC2->getDeclName() |
| 1412 | << EC2->getInitVal().toString(10); |
| 1413 | Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator) |
| 1414 | << EC1->getDeclName() |
| 1415 | << EC1->getInitVal().toString(10); |
| 1416 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1417 | return false; |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | if (EC2 != EC2End) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1422 | if (Context.Complain) { |
| 1423 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1424 | << Context.C2.getTypeDeclType(D2); |
| 1425 | Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator) |
| 1426 | << EC2->getDeclName() |
| 1427 | << EC2->getInitVal().toString(10); |
| 1428 | Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator); |
| 1429 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1430 | return false; |
| 1431 | } |
| 1432 | |
| 1433 | return true; |
| 1434 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1435 | |
| 1436 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1437 | TemplateParameterList *Params1, |
| 1438 | TemplateParameterList *Params2) { |
| 1439 | if (Params1->size() != Params2->size()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1440 | if (Context.Complain) { |
| 1441 | Context.Diag2(Params2->getTemplateLoc(), |
| 1442 | diag::err_odr_different_num_template_parameters) |
| 1443 | << Params1->size() << Params2->size(); |
| 1444 | Context.Diag1(Params1->getTemplateLoc(), |
| 1445 | diag::note_odr_template_parameter_list); |
| 1446 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1447 | return false; |
| 1448 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1449 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1450 | for (unsigned I = 0, N = Params1->size(); I != N; ++I) { |
| 1451 | if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1452 | if (Context.Complain) { |
| 1453 | Context.Diag2(Params2->getParam(I)->getLocation(), |
| 1454 | diag::err_odr_different_template_parameter_kind); |
| 1455 | Context.Diag1(Params1->getParam(I)->getLocation(), |
| 1456 | diag::note_odr_template_parameter_here); |
| 1457 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1458 | return false; |
| 1459 | } |
| 1460 | |
| 1461 | if (!Context.IsStructurallyEquivalent(Params1->getParam(I), |
| 1462 | Params2->getParam(I))) { |
| 1463 | |
| 1464 | return false; |
| 1465 | } |
| 1466 | } |
| 1467 | |
| 1468 | return true; |
| 1469 | } |
| 1470 | |
| 1471 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1472 | TemplateTypeParmDecl *D1, |
| 1473 | TemplateTypeParmDecl *D2) { |
| 1474 | if (D1->isParameterPack() != D2->isParameterPack()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1475 | if (Context.Complain) { |
| 1476 | Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack) |
| 1477 | << D2->isParameterPack(); |
| 1478 | Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) |
| 1479 | << D1->isParameterPack(); |
| 1480 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1481 | return false; |
| 1482 | } |
| 1483 | |
| 1484 | return true; |
| 1485 | } |
| 1486 | |
| 1487 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1488 | NonTypeTemplateParmDecl *D1, |
| 1489 | NonTypeTemplateParmDecl *D2) { |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1490 | if (D1->isParameterPack() != D2->isParameterPack()) { |
Douglas Gregor | 3c7380b | 2012-10-26 15:36:15 +0000 | [diff] [blame] | 1491 | if (Context.Complain) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1492 | Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack) |
| 1493 | << D2->isParameterPack(); |
| 1494 | Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) |
| 1495 | << D1->isParameterPack(); |
| 1496 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1497 | return false; |
| 1498 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1499 | |
| 1500 | // Check types. |
| 1501 | if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1502 | if (Context.Complain) { |
| 1503 | Context.Diag2(D2->getLocation(), |
| 1504 | diag::err_odr_non_type_parameter_type_inconsistent) |
| 1505 | << D2->getType() << D1->getType(); |
| 1506 | Context.Diag1(D1->getLocation(), diag::note_odr_value_here) |
| 1507 | << D1->getType(); |
| 1508 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1509 | return false; |
| 1510 | } |
| 1511 | |
| 1512 | return true; |
| 1513 | } |
| 1514 | |
| 1515 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1516 | TemplateTemplateParmDecl *D1, |
| 1517 | TemplateTemplateParmDecl *D2) { |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1518 | if (D1->isParameterPack() != D2->isParameterPack()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1519 | if (Context.Complain) { |
| 1520 | Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack) |
| 1521 | << D2->isParameterPack(); |
| 1522 | Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) |
| 1523 | << D1->isParameterPack(); |
| 1524 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1525 | return false; |
| 1526 | } |
Douglas Gregor | 3c7380b | 2012-10-26 15:36:15 +0000 | [diff] [blame] | 1527 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1528 | // Check template parameter lists. |
| 1529 | return IsStructurallyEquivalent(Context, D1->getTemplateParameters(), |
| 1530 | D2->getTemplateParameters()); |
| 1531 | } |
| 1532 | |
| 1533 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1534 | ClassTemplateDecl *D1, |
| 1535 | ClassTemplateDecl *D2) { |
| 1536 | // Check template parameters. |
| 1537 | if (!IsStructurallyEquivalent(Context, |
| 1538 | D1->getTemplateParameters(), |
| 1539 | D2->getTemplateParameters())) |
| 1540 | return false; |
| 1541 | |
| 1542 | // Check the templated declaration. |
| 1543 | return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(), |
| 1544 | D2->getTemplatedDecl()); |
| 1545 | } |
| 1546 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1547 | /// \brief Determine structural equivalence of two declarations. |
| 1548 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1549 | Decl *D1, Decl *D2) { |
| 1550 | // FIXME: Check for known structural equivalences via a callback of some sort. |
| 1551 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1552 | // Check whether we already know that these two declarations are not |
| 1553 | // structurally equivalent. |
| 1554 | if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(), |
| 1555 | D2->getCanonicalDecl()))) |
| 1556 | return false; |
| 1557 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1558 | // Determine whether we've already produced a tentative equivalence for D1. |
| 1559 | Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()]; |
| 1560 | if (EquivToD1) |
| 1561 | return EquivToD1 == D2->getCanonicalDecl(); |
| 1562 | |
| 1563 | // Produce a tentative equivalence D1 <-> D2, which will be checked later. |
| 1564 | EquivToD1 = D2->getCanonicalDecl(); |
| 1565 | Context.DeclsToCheck.push_back(D1->getCanonicalDecl()); |
| 1566 | return true; |
| 1567 | } |
| 1568 | |
| 1569 | bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1, |
| 1570 | Decl *D2) { |
| 1571 | if (!::IsStructurallyEquivalent(*this, D1, D2)) |
| 1572 | return false; |
| 1573 | |
| 1574 | return !Finish(); |
| 1575 | } |
| 1576 | |
| 1577 | bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1, |
| 1578 | QualType T2) { |
| 1579 | if (!::IsStructurallyEquivalent(*this, T1, T2)) |
| 1580 | return false; |
| 1581 | |
| 1582 | return !Finish(); |
| 1583 | } |
| 1584 | |
| 1585 | bool StructuralEquivalenceContext::Finish() { |
| 1586 | while (!DeclsToCheck.empty()) { |
| 1587 | // Check the next declaration. |
| 1588 | Decl *D1 = DeclsToCheck.front(); |
| 1589 | DeclsToCheck.pop_front(); |
| 1590 | |
| 1591 | Decl *D2 = TentativeEquivalences[D1]; |
| 1592 | assert(D2 && "Unrecorded tentative equivalence?"); |
| 1593 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1594 | bool Equivalent = true; |
| 1595 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1596 | // FIXME: Switch on all declaration kinds. For now, we're just going to |
| 1597 | // check the obvious ones. |
| 1598 | if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) { |
| 1599 | if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) { |
| 1600 | // Check for equivalent structure names. |
| 1601 | IdentifierInfo *Name1 = Record1->getIdentifier(); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1602 | if (!Name1 && Record1->getTypedefNameForAnonDecl()) |
| 1603 | Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier(); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1604 | IdentifierInfo *Name2 = Record2->getIdentifier(); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1605 | if (!Name2 && Record2->getTypedefNameForAnonDecl()) |
| 1606 | Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier(); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1607 | if (!::IsStructurallyEquivalent(Name1, Name2) || |
| 1608 | !::IsStructurallyEquivalent(*this, Record1, Record2)) |
| 1609 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1610 | } else { |
| 1611 | // Record/non-record mismatch. |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1612 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1613 | } |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1614 | } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1615 | if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) { |
| 1616 | // Check for equivalent enum names. |
| 1617 | IdentifierInfo *Name1 = Enum1->getIdentifier(); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1618 | if (!Name1 && Enum1->getTypedefNameForAnonDecl()) |
| 1619 | Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier(); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1620 | IdentifierInfo *Name2 = Enum2->getIdentifier(); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1621 | if (!Name2 && Enum2->getTypedefNameForAnonDecl()) |
| 1622 | Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier(); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1623 | if (!::IsStructurallyEquivalent(Name1, Name2) || |
| 1624 | !::IsStructurallyEquivalent(*this, Enum1, Enum2)) |
| 1625 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1626 | } else { |
| 1627 | // Enum/non-enum mismatch |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1628 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1629 | } |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1630 | } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) { |
| 1631 | if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1632 | if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(), |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1633 | Typedef2->getIdentifier()) || |
| 1634 | !::IsStructurallyEquivalent(*this, |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1635 | Typedef1->getUnderlyingType(), |
| 1636 | Typedef2->getUnderlyingType())) |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1637 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1638 | } else { |
| 1639 | // Typedef/non-typedef mismatch. |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1640 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1641 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1642 | } else if (ClassTemplateDecl *ClassTemplate1 |
| 1643 | = dyn_cast<ClassTemplateDecl>(D1)) { |
| 1644 | if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) { |
| 1645 | if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(), |
| 1646 | ClassTemplate2->getIdentifier()) || |
| 1647 | !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2)) |
| 1648 | Equivalent = false; |
| 1649 | } else { |
| 1650 | // Class template/non-class-template mismatch. |
| 1651 | Equivalent = false; |
| 1652 | } |
| 1653 | } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) { |
| 1654 | if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) { |
| 1655 | if (!::IsStructurallyEquivalent(*this, TTP1, TTP2)) |
| 1656 | Equivalent = false; |
| 1657 | } else { |
| 1658 | // Kind mismatch. |
| 1659 | Equivalent = false; |
| 1660 | } |
| 1661 | } else if (NonTypeTemplateParmDecl *NTTP1 |
| 1662 | = dyn_cast<NonTypeTemplateParmDecl>(D1)) { |
| 1663 | if (NonTypeTemplateParmDecl *NTTP2 |
| 1664 | = dyn_cast<NonTypeTemplateParmDecl>(D2)) { |
| 1665 | if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2)) |
| 1666 | Equivalent = false; |
| 1667 | } else { |
| 1668 | // Kind mismatch. |
| 1669 | Equivalent = false; |
| 1670 | } |
| 1671 | } else if (TemplateTemplateParmDecl *TTP1 |
| 1672 | = dyn_cast<TemplateTemplateParmDecl>(D1)) { |
| 1673 | if (TemplateTemplateParmDecl *TTP2 |
| 1674 | = dyn_cast<TemplateTemplateParmDecl>(D2)) { |
| 1675 | if (!::IsStructurallyEquivalent(*this, TTP1, TTP2)) |
| 1676 | Equivalent = false; |
| 1677 | } else { |
| 1678 | // Kind mismatch. |
| 1679 | Equivalent = false; |
| 1680 | } |
| 1681 | } |
| 1682 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1683 | if (!Equivalent) { |
| 1684 | // Note that these two declarations are not equivalent (and we already |
| 1685 | // know about it). |
| 1686 | NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(), |
| 1687 | D2->getCanonicalDecl())); |
| 1688 | return true; |
| 1689 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1690 | // FIXME: Check other declaration kinds! |
| 1691 | } |
| 1692 | |
| 1693 | return false; |
| 1694 | } |
| 1695 | |
| 1696 | //---------------------------------------------------------------------------- |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1697 | // Import Types |
| 1698 | //---------------------------------------------------------------------------- |
| 1699 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1700 | QualType ASTNodeImporter::VisitType(const Type *T) { |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 1701 | Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node) |
| 1702 | << T->getTypeClassName(); |
| 1703 | return QualType(); |
| 1704 | } |
| 1705 | |
Gabor Horvath | 0866c2f | 2016-11-23 15:24:23 +0000 | [diff] [blame] | 1706 | QualType ASTNodeImporter::VisitAtomicType(const AtomicType *T){ |
| 1707 | QualType UnderlyingType = Importer.Import(T->getValueType()); |
| 1708 | if(UnderlyingType.isNull()) |
| 1709 | return QualType(); |
| 1710 | |
| 1711 | return Importer.getToContext().getAtomicType(UnderlyingType); |
| 1712 | } |
| 1713 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1714 | QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1715 | switch (T->getKind()) { |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 1716 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 1717 | case BuiltinType::Id: \ |
| 1718 | return Importer.getToContext().SingletonId; |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 1719 | #include "clang/Basic/OpenCLImageTypes.def" |
John McCall | e314e27 | 2011-10-18 21:02:43 +0000 | [diff] [blame] | 1720 | #define SHARED_SINGLETON_TYPE(Expansion) |
| 1721 | #define BUILTIN_TYPE(Id, SingletonId) \ |
| 1722 | case BuiltinType::Id: return Importer.getToContext().SingletonId; |
| 1723 | #include "clang/AST/BuiltinTypes.def" |
| 1724 | |
| 1725 | // FIXME: for Char16, Char32, and NullPtr, make sure that the "to" |
| 1726 | // context supports C++. |
| 1727 | |
| 1728 | // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to" |
| 1729 | // context supports ObjC. |
| 1730 | |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1731 | case BuiltinType::Char_U: |
| 1732 | // The context we're importing from has an unsigned 'char'. If we're |
| 1733 | // importing into a context with a signed 'char', translate to |
| 1734 | // 'unsigned char' instead. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1735 | if (Importer.getToContext().getLangOpts().CharIsSigned) |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1736 | return Importer.getToContext().UnsignedCharTy; |
| 1737 | |
| 1738 | return Importer.getToContext().CharTy; |
| 1739 | |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1740 | case BuiltinType::Char_S: |
| 1741 | // The context we're importing from has an unsigned 'char'. If we're |
| 1742 | // importing into a context with a signed 'char', translate to |
| 1743 | // 'unsigned char' instead. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1744 | if (!Importer.getToContext().getLangOpts().CharIsSigned) |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1745 | return Importer.getToContext().SignedCharTy; |
| 1746 | |
| 1747 | return Importer.getToContext().CharTy; |
| 1748 | |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 1749 | case BuiltinType::WChar_S: |
| 1750 | case BuiltinType::WChar_U: |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1751 | // FIXME: If not in C++, shall we translate to the C equivalent of |
| 1752 | // wchar_t? |
| 1753 | return Importer.getToContext().WCharTy; |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1754 | } |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1755 | |
| 1756 | llvm_unreachable("Invalid BuiltinType Kind!"); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 1759 | QualType ASTNodeImporter::VisitDecayedType(const DecayedType *T) { |
| 1760 | QualType OrigT = Importer.Import(T->getOriginalType()); |
| 1761 | if (OrigT.isNull()) |
| 1762 | return QualType(); |
| 1763 | |
| 1764 | return Importer.getToContext().getDecayedType(OrigT); |
| 1765 | } |
| 1766 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1767 | QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1768 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1769 | if (ToElementType.isNull()) |
| 1770 | return QualType(); |
| 1771 | |
| 1772 | return Importer.getToContext().getComplexType(ToElementType); |
| 1773 | } |
| 1774 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1775 | QualType ASTNodeImporter::VisitPointerType(const PointerType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1776 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 1777 | if (ToPointeeType.isNull()) |
| 1778 | return QualType(); |
| 1779 | |
| 1780 | return Importer.getToContext().getPointerType(ToPointeeType); |
| 1781 | } |
| 1782 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1783 | QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1784 | // FIXME: Check for blocks support in "to" context. |
| 1785 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 1786 | if (ToPointeeType.isNull()) |
| 1787 | return QualType(); |
| 1788 | |
| 1789 | return Importer.getToContext().getBlockPointerType(ToPointeeType); |
| 1790 | } |
| 1791 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1792 | QualType |
| 1793 | ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1794 | // FIXME: Check for C++ support in "to" context. |
| 1795 | QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); |
| 1796 | if (ToPointeeType.isNull()) |
| 1797 | return QualType(); |
| 1798 | |
| 1799 | return Importer.getToContext().getLValueReferenceType(ToPointeeType); |
| 1800 | } |
| 1801 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1802 | QualType |
| 1803 | ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1804 | // FIXME: Check for C++0x support in "to" context. |
| 1805 | QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); |
| 1806 | if (ToPointeeType.isNull()) |
| 1807 | return QualType(); |
| 1808 | |
| 1809 | return Importer.getToContext().getRValueReferenceType(ToPointeeType); |
| 1810 | } |
| 1811 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1812 | QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1813 | // FIXME: Check for C++ support in "to" context. |
| 1814 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 1815 | if (ToPointeeType.isNull()) |
| 1816 | return QualType(); |
| 1817 | |
| 1818 | QualType ClassType = Importer.Import(QualType(T->getClass(), 0)); |
| 1819 | return Importer.getToContext().getMemberPointerType(ToPointeeType, |
| 1820 | ClassType.getTypePtr()); |
| 1821 | } |
| 1822 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1823 | QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1824 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1825 | if (ToElementType.isNull()) |
| 1826 | return QualType(); |
| 1827 | |
| 1828 | return Importer.getToContext().getConstantArrayType(ToElementType, |
| 1829 | T->getSize(), |
| 1830 | T->getSizeModifier(), |
| 1831 | T->getIndexTypeCVRQualifiers()); |
| 1832 | } |
| 1833 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1834 | QualType |
| 1835 | ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1836 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1837 | if (ToElementType.isNull()) |
| 1838 | return QualType(); |
| 1839 | |
| 1840 | return Importer.getToContext().getIncompleteArrayType(ToElementType, |
| 1841 | T->getSizeModifier(), |
| 1842 | T->getIndexTypeCVRQualifiers()); |
| 1843 | } |
| 1844 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1845 | QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1846 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1847 | if (ToElementType.isNull()) |
| 1848 | return QualType(); |
| 1849 | |
| 1850 | Expr *Size = Importer.Import(T->getSizeExpr()); |
| 1851 | if (!Size) |
| 1852 | return QualType(); |
| 1853 | |
| 1854 | SourceRange Brackets = Importer.Import(T->getBracketsRange()); |
| 1855 | return Importer.getToContext().getVariableArrayType(ToElementType, Size, |
| 1856 | T->getSizeModifier(), |
| 1857 | T->getIndexTypeCVRQualifiers(), |
| 1858 | Brackets); |
| 1859 | } |
| 1860 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1861 | QualType ASTNodeImporter::VisitVectorType(const VectorType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1862 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1863 | if (ToElementType.isNull()) |
| 1864 | return QualType(); |
| 1865 | |
| 1866 | return Importer.getToContext().getVectorType(ToElementType, |
| 1867 | T->getNumElements(), |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1868 | T->getVectorKind()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1869 | } |
| 1870 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1871 | QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1872 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1873 | if (ToElementType.isNull()) |
| 1874 | return QualType(); |
| 1875 | |
| 1876 | return Importer.getToContext().getExtVectorType(ToElementType, |
| 1877 | T->getNumElements()); |
| 1878 | } |
| 1879 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1880 | QualType |
| 1881 | ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1882 | // FIXME: What happens if we're importing a function without a prototype |
| 1883 | // into C++? Should we make it variadic? |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1884 | QualType ToResultType = Importer.Import(T->getReturnType()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1885 | if (ToResultType.isNull()) |
| 1886 | return QualType(); |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1887 | |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1888 | return Importer.getToContext().getFunctionNoProtoType(ToResultType, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1889 | T->getExtInfo()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 1892 | QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1893 | QualType ToResultType = Importer.Import(T->getReturnType()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1894 | if (ToResultType.isNull()) |
| 1895 | return QualType(); |
| 1896 | |
| 1897 | // Import argument types |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1898 | SmallVector<QualType, 4> ArgTypes; |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 1899 | for (const auto &A : T->param_types()) { |
| 1900 | QualType ArgType = Importer.Import(A); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1901 | if (ArgType.isNull()) |
| 1902 | return QualType(); |
| 1903 | ArgTypes.push_back(ArgType); |
| 1904 | } |
| 1905 | |
| 1906 | // Import exception types |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1907 | SmallVector<QualType, 4> ExceptionTypes; |
Aaron Ballman | b088fbe | 2014-03-17 15:38:09 +0000 | [diff] [blame] | 1908 | for (const auto &E : T->exceptions()) { |
| 1909 | QualType ExceptionType = Importer.Import(E); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1910 | if (ExceptionType.isNull()) |
| 1911 | return QualType(); |
| 1912 | ExceptionTypes.push_back(ExceptionType); |
| 1913 | } |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1914 | |
Argyrios Kyrtzidis | b41791d | 2012-09-22 01:58:06 +0000 | [diff] [blame] | 1915 | FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo(); |
| 1916 | FunctionProtoType::ExtProtoInfo ToEPI; |
| 1917 | |
| 1918 | ToEPI.ExtInfo = FromEPI.ExtInfo; |
| 1919 | ToEPI.Variadic = FromEPI.Variadic; |
| 1920 | ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn; |
| 1921 | ToEPI.TypeQuals = FromEPI.TypeQuals; |
| 1922 | ToEPI.RefQualifier = FromEPI.RefQualifier; |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 1923 | ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type; |
| 1924 | ToEPI.ExceptionSpec.Exceptions = ExceptionTypes; |
| 1925 | ToEPI.ExceptionSpec.NoexceptExpr = |
| 1926 | Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr); |
| 1927 | ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>( |
| 1928 | Importer.Import(FromEPI.ExceptionSpec.SourceDecl)); |
| 1929 | ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>( |
| 1930 | Importer.Import(FromEPI.ExceptionSpec.SourceTemplate)); |
Argyrios Kyrtzidis | b41791d | 2012-09-22 01:58:06 +0000 | [diff] [blame] | 1931 | |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 1932 | return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI); |
Argyrios Kyrtzidis | b41791d | 2012-09-22 01:58:06 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
Sean Callanan | da6df8a | 2011-08-11 16:56:07 +0000 | [diff] [blame] | 1935 | QualType ASTNodeImporter::VisitParenType(const ParenType *T) { |
| 1936 | QualType ToInnerType = Importer.Import(T->getInnerType()); |
| 1937 | if (ToInnerType.isNull()) |
| 1938 | return QualType(); |
| 1939 | |
| 1940 | return Importer.getToContext().getParenType(ToInnerType); |
| 1941 | } |
| 1942 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1943 | QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1944 | TypedefNameDecl *ToDecl |
| 1945 | = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl())); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1946 | if (!ToDecl) |
| 1947 | return QualType(); |
| 1948 | |
| 1949 | return Importer.getToContext().getTypeDeclType(ToDecl); |
| 1950 | } |
| 1951 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1952 | QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1953 | Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); |
| 1954 | if (!ToExpr) |
| 1955 | return QualType(); |
| 1956 | |
| 1957 | return Importer.getToContext().getTypeOfExprType(ToExpr); |
| 1958 | } |
| 1959 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1960 | QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1961 | QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); |
| 1962 | if (ToUnderlyingType.isNull()) |
| 1963 | return QualType(); |
| 1964 | |
| 1965 | return Importer.getToContext().getTypeOfType(ToUnderlyingType); |
| 1966 | } |
| 1967 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1968 | QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) { |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1969 | // FIXME: Make sure that the "to" context supports C++0x! |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1970 | Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); |
| 1971 | if (!ToExpr) |
| 1972 | return QualType(); |
| 1973 | |
Douglas Gregor | 81495f3 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 1974 | QualType UnderlyingType = Importer.Import(T->getUnderlyingType()); |
| 1975 | if (UnderlyingType.isNull()) |
| 1976 | return QualType(); |
| 1977 | |
| 1978 | return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1981 | QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) { |
| 1982 | QualType ToBaseType = Importer.Import(T->getBaseType()); |
| 1983 | QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); |
| 1984 | if (ToBaseType.isNull() || ToUnderlyingType.isNull()) |
| 1985 | return QualType(); |
| 1986 | |
| 1987 | return Importer.getToContext().getUnaryTransformType(ToBaseType, |
| 1988 | ToUnderlyingType, |
| 1989 | T->getUTTKind()); |
| 1990 | } |
| 1991 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1992 | QualType ASTNodeImporter::VisitAutoType(const AutoType *T) { |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 1993 | // FIXME: Make sure that the "to" context supports C++11! |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1994 | QualType FromDeduced = T->getDeducedType(); |
| 1995 | QualType ToDeduced; |
| 1996 | if (!FromDeduced.isNull()) { |
| 1997 | ToDeduced = Importer.Import(FromDeduced); |
| 1998 | if (ToDeduced.isNull()) |
| 1999 | return QualType(); |
| 2000 | } |
| 2001 | |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 2002 | return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(), |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 2003 | /*IsDependent*/false); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2004 | } |
| 2005 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 2006 | QualType ASTNodeImporter::VisitInjectedClassNameType( |
| 2007 | const InjectedClassNameType *T) { |
| 2008 | CXXRecordDecl *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl())); |
| 2009 | if (!D) |
| 2010 | return QualType(); |
| 2011 | |
| 2012 | QualType InjType = Importer.Import(T->getInjectedSpecializationType()); |
| 2013 | if (InjType.isNull()) |
| 2014 | return QualType(); |
| 2015 | |
| 2016 | // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading |
| 2017 | // See comments in InjectedClassNameType definition for details |
| 2018 | // return Importer.getToContext().getInjectedClassNameType(D, InjType); |
| 2019 | enum { |
| 2020 | TypeAlignmentInBits = 4, |
| 2021 | TypeAlignment = 1 << TypeAlignmentInBits |
| 2022 | }; |
| 2023 | |
| 2024 | return QualType(new (Importer.getToContext(), TypeAlignment) |
| 2025 | InjectedClassNameType(D, InjType), 0); |
| 2026 | } |
| 2027 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2028 | QualType ASTNodeImporter::VisitRecordType(const RecordType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 2029 | RecordDecl *ToDecl |
| 2030 | = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl())); |
| 2031 | if (!ToDecl) |
| 2032 | return QualType(); |
| 2033 | |
| 2034 | return Importer.getToContext().getTagDeclType(ToDecl); |
| 2035 | } |
| 2036 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2037 | QualType ASTNodeImporter::VisitEnumType(const EnumType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 2038 | EnumDecl *ToDecl |
| 2039 | = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl())); |
| 2040 | if (!ToDecl) |
| 2041 | return QualType(); |
| 2042 | |
| 2043 | return Importer.getToContext().getTagDeclType(ToDecl); |
| 2044 | } |
| 2045 | |
Sean Callanan | 72fe085 | 2015-04-02 23:50:08 +0000 | [diff] [blame] | 2046 | QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) { |
| 2047 | QualType FromModifiedType = T->getModifiedType(); |
| 2048 | QualType FromEquivalentType = T->getEquivalentType(); |
| 2049 | QualType ToModifiedType; |
| 2050 | QualType ToEquivalentType; |
| 2051 | |
| 2052 | if (!FromModifiedType.isNull()) { |
| 2053 | ToModifiedType = Importer.Import(FromModifiedType); |
| 2054 | if (ToModifiedType.isNull()) |
| 2055 | return QualType(); |
| 2056 | } |
| 2057 | if (!FromEquivalentType.isNull()) { |
| 2058 | ToEquivalentType = Importer.Import(FromEquivalentType); |
| 2059 | if (ToEquivalentType.isNull()) |
| 2060 | return QualType(); |
| 2061 | } |
| 2062 | |
| 2063 | return Importer.getToContext().getAttributedType(T->getAttrKind(), |
| 2064 | ToModifiedType, ToEquivalentType); |
| 2065 | } |
| 2066 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 2067 | |
| 2068 | QualType ASTNodeImporter::VisitTemplateTypeParmType( |
| 2069 | const TemplateTypeParmType *T) { |
| 2070 | TemplateTypeParmDecl *ParmDecl = |
| 2071 | cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl())); |
| 2072 | if (!ParmDecl && T->getDecl()) |
| 2073 | return QualType(); |
| 2074 | |
| 2075 | return Importer.getToContext().getTemplateTypeParmType( |
| 2076 | T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl); |
| 2077 | } |
| 2078 | |
Aleksei Sidorin | 855086d | 2017-01-23 09:30:36 +0000 | [diff] [blame] | 2079 | QualType ASTNodeImporter::VisitSubstTemplateTypeParmType( |
| 2080 | const SubstTemplateTypeParmType *T) { |
| 2081 | const TemplateTypeParmType *Replaced = |
| 2082 | cast_or_null<TemplateTypeParmType>(Importer.Import( |
| 2083 | QualType(T->getReplacedParameter(), 0)).getTypePtr()); |
| 2084 | if (!Replaced) |
| 2085 | return QualType(); |
| 2086 | |
| 2087 | QualType Replacement = Importer.Import(T->getReplacementType()); |
| 2088 | if (Replacement.isNull()) |
| 2089 | return QualType(); |
| 2090 | Replacement = Replacement.getCanonicalType(); |
| 2091 | |
| 2092 | return Importer.getToContext().getSubstTemplateTypeParmType( |
| 2093 | Replaced, Replacement); |
| 2094 | } |
| 2095 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2096 | QualType ASTNodeImporter::VisitTemplateSpecializationType( |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2097 | const TemplateSpecializationType *T) { |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2098 | TemplateName ToTemplate = Importer.Import(T->getTemplateName()); |
| 2099 | if (ToTemplate.isNull()) |
| 2100 | return QualType(); |
| 2101 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2102 | SmallVector<TemplateArgument, 2> ToTemplateArgs; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2103 | if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs)) |
| 2104 | return QualType(); |
| 2105 | |
| 2106 | QualType ToCanonType; |
| 2107 | if (!QualType(T, 0).isCanonical()) { |
| 2108 | QualType FromCanonType |
| 2109 | = Importer.getFromContext().getCanonicalType(QualType(T, 0)); |
| 2110 | ToCanonType =Importer.Import(FromCanonType); |
| 2111 | if (ToCanonType.isNull()) |
| 2112 | return QualType(); |
| 2113 | } |
| 2114 | return Importer.getToContext().getTemplateSpecializationType(ToTemplate, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 2115 | ToTemplateArgs, |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2116 | ToCanonType); |
| 2117 | } |
| 2118 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2119 | QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2120 | NestedNameSpecifier *ToQualifier = nullptr; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2121 | // Note: the qualifier in an ElaboratedType is optional. |
| 2122 | if (T->getQualifier()) { |
| 2123 | ToQualifier = Importer.Import(T->getQualifier()); |
| 2124 | if (!ToQualifier) |
| 2125 | return QualType(); |
| 2126 | } |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 2127 | |
| 2128 | QualType ToNamedType = Importer.Import(T->getNamedType()); |
| 2129 | if (ToNamedType.isNull()) |
| 2130 | return QualType(); |
| 2131 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2132 | return Importer.getToContext().getElaboratedType(T->getKeyword(), |
| 2133 | ToQualifier, ToNamedType); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 2134 | } |
| 2135 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2136 | QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 2137 | ObjCInterfaceDecl *Class |
| 2138 | = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl())); |
| 2139 | if (!Class) |
| 2140 | return QualType(); |
| 2141 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2142 | return Importer.getToContext().getObjCInterfaceType(Class); |
| 2143 | } |
| 2144 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2145 | QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2146 | QualType ToBaseType = Importer.Import(T->getBaseType()); |
| 2147 | if (ToBaseType.isNull()) |
| 2148 | return QualType(); |
| 2149 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 2150 | SmallVector<QualType, 4> TypeArgs; |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 2151 | for (auto TypeArg : T->getTypeArgsAsWritten()) { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 2152 | QualType ImportedTypeArg = Importer.Import(TypeArg); |
| 2153 | if (ImportedTypeArg.isNull()) |
| 2154 | return QualType(); |
| 2155 | |
| 2156 | TypeArgs.push_back(ImportedTypeArg); |
| 2157 | } |
| 2158 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2159 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
Aaron Ballman | 1683f7b | 2014-03-17 15:55:30 +0000 | [diff] [blame] | 2160 | for (auto *P : T->quals()) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 2161 | ObjCProtocolDecl *Protocol |
Aaron Ballman | 1683f7b | 2014-03-17 15:55:30 +0000 | [diff] [blame] | 2162 | = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P)); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 2163 | if (!Protocol) |
| 2164 | return QualType(); |
| 2165 | Protocols.push_back(Protocol); |
| 2166 | } |
| 2167 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 2168 | return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs, |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 2169 | Protocols, |
| 2170 | T->isKindOfTypeAsWritten()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 2171 | } |
| 2172 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2173 | QualType |
| 2174 | ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 2175 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 2176 | if (ToPointeeType.isNull()) |
| 2177 | return QualType(); |
| 2178 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2179 | return Importer.getToContext().getObjCObjectPointerType(ToPointeeType); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 2180 | } |
| 2181 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2182 | //---------------------------------------------------------------------------- |
| 2183 | // Import Declarations |
| 2184 | //---------------------------------------------------------------------------- |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2185 | bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC, |
| 2186 | DeclContext *&LexicalDC, |
| 2187 | DeclarationName &Name, |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2188 | NamedDecl *&ToD, |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2189 | SourceLocation &Loc) { |
| 2190 | // Import the context of this declaration. |
| 2191 | DC = Importer.ImportContext(D->getDeclContext()); |
| 2192 | if (!DC) |
| 2193 | return true; |
| 2194 | |
| 2195 | LexicalDC = DC; |
| 2196 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 2197 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 2198 | if (!LexicalDC) |
| 2199 | return true; |
| 2200 | } |
| 2201 | |
| 2202 | // Import the name of this declaration. |
| 2203 | Name = Importer.Import(D->getDeclName()); |
| 2204 | if (D->getDeclName() && !Name) |
| 2205 | return true; |
| 2206 | |
| 2207 | // Import the location of this declaration. |
| 2208 | Loc = Importer.Import(D->getLocation()); |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2209 | ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D)); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2210 | return false; |
| 2211 | } |
| 2212 | |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2213 | void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) { |
| 2214 | if (!FromD) |
| 2215 | return; |
| 2216 | |
| 2217 | if (!ToD) { |
| 2218 | ToD = Importer.Import(FromD); |
| 2219 | if (!ToD) |
| 2220 | return; |
| 2221 | } |
| 2222 | |
| 2223 | if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) { |
| 2224 | if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) { |
Sean Callanan | 19dfc93 | 2013-01-11 23:17:47 +0000 | [diff] [blame] | 2225 | if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) { |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2226 | ImportDefinition(FromRecord, ToRecord); |
| 2227 | } |
| 2228 | } |
| 2229 | return; |
| 2230 | } |
| 2231 | |
| 2232 | if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) { |
| 2233 | if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) { |
| 2234 | if (FromEnum->getDefinition() && !ToEnum->getDefinition()) { |
| 2235 | ImportDefinition(FromEnum, ToEnum); |
| 2236 | } |
| 2237 | } |
| 2238 | return; |
| 2239 | } |
| 2240 | } |
| 2241 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2242 | void |
| 2243 | ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From, |
| 2244 | DeclarationNameInfo& To) { |
| 2245 | // NOTE: To.Name and To.Loc are already imported. |
| 2246 | // We only have to import To.LocInfo. |
| 2247 | switch (To.getName().getNameKind()) { |
| 2248 | case DeclarationName::Identifier: |
| 2249 | case DeclarationName::ObjCZeroArgSelector: |
| 2250 | case DeclarationName::ObjCOneArgSelector: |
| 2251 | case DeclarationName::ObjCMultiArgSelector: |
| 2252 | case DeclarationName::CXXUsingDirective: |
| 2253 | return; |
| 2254 | |
| 2255 | case DeclarationName::CXXOperatorName: { |
| 2256 | SourceRange Range = From.getCXXOperatorNameRange(); |
| 2257 | To.setCXXOperatorNameRange(Importer.Import(Range)); |
| 2258 | return; |
| 2259 | } |
| 2260 | case DeclarationName::CXXLiteralOperatorName: { |
| 2261 | SourceLocation Loc = From.getCXXLiteralOperatorNameLoc(); |
| 2262 | To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc)); |
| 2263 | return; |
| 2264 | } |
| 2265 | case DeclarationName::CXXConstructorName: |
| 2266 | case DeclarationName::CXXDestructorName: |
| 2267 | case DeclarationName::CXXConversionFunctionName: { |
| 2268 | TypeSourceInfo *FromTInfo = From.getNamedTypeInfo(); |
| 2269 | To.setNamedTypeInfo(Importer.Import(FromTInfo)); |
| 2270 | return; |
| 2271 | } |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2272 | } |
Douglas Gregor | 07216d1 | 2011-11-02 20:52:01 +0000 | [diff] [blame] | 2273 | llvm_unreachable("Unknown name kind."); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2274 | } |
| 2275 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 2276 | void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) { |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 2277 | if (Importer.isMinimalImport() && !ForceImport) { |
Sean Callanan | 81d577c | 2011-07-22 23:46:03 +0000 | [diff] [blame] | 2278 | Importer.ImportContext(FromDC); |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 2279 | return; |
| 2280 | } |
| 2281 | |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 2282 | for (auto *From : FromDC->decls()) |
| 2283 | Importer.Import(From); |
Douglas Gregor | 968d633 | 2010-02-21 18:24:45 +0000 | [diff] [blame] | 2284 | } |
| 2285 | |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2286 | bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To, |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 2287 | ImportDefinitionKind Kind) { |
| 2288 | if (To->getDefinition() || To->isBeingDefined()) { |
| 2289 | if (Kind == IDK_Everything) |
| 2290 | ImportDeclContext(From, /*ForceImport=*/true); |
| 2291 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2292 | return false; |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 2293 | } |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2294 | |
| 2295 | To->startDefinition(); |
| 2296 | |
| 2297 | // Add base classes. |
| 2298 | if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) { |
| 2299 | CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From); |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 2300 | |
| 2301 | struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data(); |
| 2302 | struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data(); |
| 2303 | ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor; |
Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 2304 | ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers; |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 2305 | ToData.Aggregate = FromData.Aggregate; |
| 2306 | ToData.PlainOldData = FromData.PlainOldData; |
| 2307 | ToData.Empty = FromData.Empty; |
| 2308 | ToData.Polymorphic = FromData.Polymorphic; |
| 2309 | ToData.Abstract = FromData.Abstract; |
| 2310 | ToData.IsStandardLayout = FromData.IsStandardLayout; |
| 2311 | ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases; |
| 2312 | ToData.HasPrivateFields = FromData.HasPrivateFields; |
| 2313 | ToData.HasProtectedFields = FromData.HasProtectedFields; |
| 2314 | ToData.HasPublicFields = FromData.HasPublicFields; |
| 2315 | ToData.HasMutableFields = FromData.HasMutableFields; |
Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 2316 | ToData.HasVariantMembers = FromData.HasVariantMembers; |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 2317 | ToData.HasOnlyCMembers = FromData.HasOnlyCMembers; |
Richard Smith | e2648ba | 2012-05-07 01:07:30 +0000 | [diff] [blame] | 2318 | ToData.HasInClassInitializer = FromData.HasInClassInitializer; |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 2319 | ToData.HasUninitializedReferenceMember |
| 2320 | = FromData.HasUninitializedReferenceMember; |
Nico Weber | 6a6376b | 2016-02-19 01:52:46 +0000 | [diff] [blame] | 2321 | ToData.HasUninitializedFields = FromData.HasUninitializedFields; |
Richard Smith | 12e7931 | 2016-05-13 06:47:56 +0000 | [diff] [blame] | 2322 | ToData.HasInheritedConstructor = FromData.HasInheritedConstructor; |
| 2323 | ToData.HasInheritedAssignment = FromData.HasInheritedAssignment; |
Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 2324 | ToData.NeedOverloadResolutionForMoveConstructor |
| 2325 | = FromData.NeedOverloadResolutionForMoveConstructor; |
| 2326 | ToData.NeedOverloadResolutionForMoveAssignment |
| 2327 | = FromData.NeedOverloadResolutionForMoveAssignment; |
| 2328 | ToData.NeedOverloadResolutionForDestructor |
| 2329 | = FromData.NeedOverloadResolutionForDestructor; |
| 2330 | ToData.DefaultedMoveConstructorIsDeleted |
| 2331 | = FromData.DefaultedMoveConstructorIsDeleted; |
| 2332 | ToData.DefaultedMoveAssignmentIsDeleted |
| 2333 | = FromData.DefaultedMoveAssignmentIsDeleted; |
| 2334 | ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted; |
Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 2335 | ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers; |
| 2336 | ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor; |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 2337 | ToData.HasConstexprNonCopyMoveConstructor |
| 2338 | = FromData.HasConstexprNonCopyMoveConstructor; |
Nico Weber | 72c57f4 | 2016-02-24 20:58:14 +0000 | [diff] [blame] | 2339 | ToData.HasDefaultedDefaultConstructor |
| 2340 | = FromData.HasDefaultedDefaultConstructor; |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 2341 | ToData.DefaultedDefaultConstructorIsConstexpr |
| 2342 | = FromData.DefaultedDefaultConstructorIsConstexpr; |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 2343 | ToData.HasConstexprDefaultConstructor |
| 2344 | = FromData.HasConstexprDefaultConstructor; |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 2345 | ToData.HasNonLiteralTypeFieldsOrBases |
| 2346 | = FromData.HasNonLiteralTypeFieldsOrBases; |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 2347 | // ComputedVisibleConversions not imported. |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 2348 | ToData.UserProvidedDefaultConstructor |
| 2349 | = FromData.UserProvidedDefaultConstructor; |
Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 2350 | ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers; |
Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 2351 | ToData.ImplicitCopyConstructorHasConstParam |
| 2352 | = FromData.ImplicitCopyConstructorHasConstParam; |
| 2353 | ToData.ImplicitCopyAssignmentHasConstParam |
| 2354 | = FromData.ImplicitCopyAssignmentHasConstParam; |
| 2355 | ToData.HasDeclaredCopyConstructorWithConstParam |
| 2356 | = FromData.HasDeclaredCopyConstructorWithConstParam; |
| 2357 | ToData.HasDeclaredCopyAssignmentWithConstParam |
| 2358 | = FromData.HasDeclaredCopyAssignmentWithConstParam; |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 2359 | ToData.IsLambda = FromData.IsLambda; |
| 2360 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2361 | SmallVector<CXXBaseSpecifier *, 4> Bases; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2362 | for (const auto &Base1 : FromCXX->bases()) { |
| 2363 | QualType T = Importer.Import(Base1.getType()); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2364 | if (T.isNull()) |
Douglas Gregor | 96303ea | 2010-12-02 19:33:37 +0000 | [diff] [blame] | 2365 | return true; |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 2366 | |
| 2367 | SourceLocation EllipsisLoc; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2368 | if (Base1.isPackExpansion()) |
| 2369 | EllipsisLoc = Importer.Import(Base1.getEllipsisLoc()); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2370 | |
| 2371 | // Ensure that we have a definition for the base. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2372 | ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl()); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2373 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2374 | Bases.push_back( |
| 2375 | new (Importer.getToContext()) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2376 | CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()), |
| 2377 | Base1.isVirtual(), |
| 2378 | Base1.isBaseOfClass(), |
| 2379 | Base1.getAccessSpecifierAsWritten(), |
| 2380 | Importer.Import(Base1.getTypeSourceInfo()), |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 2381 | EllipsisLoc)); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2382 | } |
| 2383 | if (!Bases.empty()) |
Craig Topper | e6337e1 | 2015-12-25 00:36:02 +0000 | [diff] [blame] | 2384 | ToCXX->setBases(Bases.data(), Bases.size()); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2385 | } |
| 2386 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 2387 | if (shouldForceImportDeclContext(Kind)) |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 2388 | ImportDeclContext(From, /*ForceImport=*/true); |
| 2389 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2390 | To->completeDefinition(); |
Douglas Gregor | 96303ea | 2010-12-02 19:33:37 +0000 | [diff] [blame] | 2391 | return false; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2394 | bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To, |
| 2395 | ImportDefinitionKind Kind) { |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2396 | if (To->getAnyInitializer()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2397 | return false; |
| 2398 | |
| 2399 | // FIXME: Can we really import any initializer? Alternatively, we could force |
| 2400 | // ourselves to import every declaration of a variable and then only use |
| 2401 | // getInit() here. |
| 2402 | To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer()))); |
| 2403 | |
| 2404 | // FIXME: Other bits to merge? |
| 2405 | |
| 2406 | return false; |
| 2407 | } |
| 2408 | |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2409 | bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 2410 | ImportDefinitionKind Kind) { |
| 2411 | if (To->getDefinition() || To->isBeingDefined()) { |
| 2412 | if (Kind == IDK_Everything) |
| 2413 | ImportDeclContext(From, /*ForceImport=*/true); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2414 | return false; |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 2415 | } |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2416 | |
| 2417 | To->startDefinition(); |
| 2418 | |
| 2419 | QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From)); |
| 2420 | if (T.isNull()) |
| 2421 | return true; |
| 2422 | |
| 2423 | QualType ToPromotionType = Importer.Import(From->getPromotionType()); |
| 2424 | if (ToPromotionType.isNull()) |
| 2425 | return true; |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 2426 | |
| 2427 | if (shouldForceImportDeclContext(Kind)) |
| 2428 | ImportDeclContext(From, /*ForceImport=*/true); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2429 | |
| 2430 | // FIXME: we might need to merge the number of positive or negative bits |
| 2431 | // if the enumerator lists don't match. |
| 2432 | To->completeDefinition(T, ToPromotionType, |
| 2433 | From->getNumPositiveBits(), |
| 2434 | From->getNumNegativeBits()); |
| 2435 | return false; |
| 2436 | } |
| 2437 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 2438 | TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList( |
| 2439 | TemplateParameterList *Params) { |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 2440 | SmallVector<NamedDecl *, 4> ToParams(Params->size()); |
| 2441 | if (ImportContainerChecked(*Params, ToParams)) |
| 2442 | return nullptr; |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 2443 | |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 2444 | Expr *ToRequiresClause; |
| 2445 | if (Expr *const R = Params->getRequiresClause()) { |
| 2446 | ToRequiresClause = Importer.Import(R); |
| 2447 | if (!ToRequiresClause) |
| 2448 | return nullptr; |
| 2449 | } else { |
| 2450 | ToRequiresClause = nullptr; |
| 2451 | } |
| 2452 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 2453 | return TemplateParameterList::Create(Importer.getToContext(), |
| 2454 | Importer.Import(Params->getTemplateLoc()), |
| 2455 | Importer.Import(Params->getLAngleLoc()), |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 2456 | ToParams, |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 2457 | Importer.Import(Params->getRAngleLoc()), |
| 2458 | ToRequiresClause); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 2459 | } |
| 2460 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2461 | TemplateArgument |
| 2462 | ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) { |
| 2463 | switch (From.getKind()) { |
| 2464 | case TemplateArgument::Null: |
| 2465 | return TemplateArgument(); |
| 2466 | |
| 2467 | case TemplateArgument::Type: { |
| 2468 | QualType ToType = Importer.Import(From.getAsType()); |
| 2469 | if (ToType.isNull()) |
| 2470 | return TemplateArgument(); |
| 2471 | return TemplateArgument(ToType); |
| 2472 | } |
| 2473 | |
| 2474 | case TemplateArgument::Integral: { |
| 2475 | QualType ToType = Importer.Import(From.getIntegralType()); |
| 2476 | if (ToType.isNull()) |
| 2477 | return TemplateArgument(); |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 2478 | return TemplateArgument(From, ToType); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2481 | case TemplateArgument::Declaration: { |
David Blaikie | 3c7dd6b | 2014-10-22 19:54:16 +0000 | [diff] [blame] | 2482 | ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl())); |
| 2483 | QualType ToType = Importer.Import(From.getParamTypeForDecl()); |
| 2484 | if (!To || ToType.isNull()) |
| 2485 | return TemplateArgument(); |
| 2486 | return TemplateArgument(To, ToType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
| 2489 | case TemplateArgument::NullPtr: { |
| 2490 | QualType ToType = Importer.Import(From.getNullPtrType()); |
| 2491 | if (ToType.isNull()) |
| 2492 | return TemplateArgument(); |
| 2493 | return TemplateArgument(ToType, /*isNullPtr*/true); |
| 2494 | } |
| 2495 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2496 | case TemplateArgument::Template: { |
| 2497 | TemplateName ToTemplate = Importer.Import(From.getAsTemplate()); |
| 2498 | if (ToTemplate.isNull()) |
| 2499 | return TemplateArgument(); |
| 2500 | |
| 2501 | return TemplateArgument(ToTemplate); |
| 2502 | } |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2503 | |
| 2504 | case TemplateArgument::TemplateExpansion: { |
| 2505 | TemplateName ToTemplate |
| 2506 | = Importer.Import(From.getAsTemplateOrTemplatePattern()); |
| 2507 | if (ToTemplate.isNull()) |
| 2508 | return TemplateArgument(); |
| 2509 | |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 2510 | return TemplateArgument(ToTemplate, From.getNumTemplateExpansions()); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2511 | } |
| 2512 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2513 | case TemplateArgument::Expression: |
| 2514 | if (Expr *ToExpr = Importer.Import(From.getAsExpr())) |
| 2515 | return TemplateArgument(ToExpr); |
| 2516 | return TemplateArgument(); |
| 2517 | |
| 2518 | case TemplateArgument::Pack: { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2519 | SmallVector<TemplateArgument, 2> ToPack; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2520 | ToPack.reserve(From.pack_size()); |
| 2521 | if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack)) |
| 2522 | return TemplateArgument(); |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 2523 | |
| 2524 | return TemplateArgument( |
| 2525 | llvm::makeArrayRef(ToPack).copy(Importer.getToContext())); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2526 | } |
| 2527 | } |
| 2528 | |
| 2529 | llvm_unreachable("Invalid template argument kind"); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2530 | } |
| 2531 | |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 2532 | TemplateArgumentLoc ASTNodeImporter::ImportTemplateArgumentLoc( |
| 2533 | const TemplateArgumentLoc &TALoc, bool &Error) { |
| 2534 | Error = false; |
| 2535 | TemplateArgument Arg = ImportTemplateArgument(TALoc.getArgument()); |
| 2536 | TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo(); |
| 2537 | TemplateArgumentLocInfo ToInfo; |
| 2538 | if (Arg.getKind() == TemplateArgument::Expression) { |
| 2539 | Expr *E = Importer.Import(FromInfo.getAsExpr()); |
| 2540 | ToInfo = TemplateArgumentLocInfo(E); |
| 2541 | if (!E) |
| 2542 | Error = true; |
| 2543 | } else if (Arg.getKind() == TemplateArgument::Type) { |
| 2544 | if (TypeSourceInfo *TSI = Importer.Import(FromInfo.getAsTypeSourceInfo())) |
| 2545 | ToInfo = TemplateArgumentLocInfo(TSI); |
| 2546 | else |
| 2547 | Error = true; |
| 2548 | } else { |
| 2549 | ToInfo = TemplateArgumentLocInfo( |
| 2550 | Importer.Import(FromInfo.getTemplateQualifierLoc()), |
| 2551 | Importer.Import(FromInfo.getTemplateNameLoc()), |
| 2552 | Importer.Import(FromInfo.getTemplateEllipsisLoc())); |
| 2553 | } |
| 2554 | return TemplateArgumentLoc(Arg, ToInfo); |
| 2555 | } |
| 2556 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2557 | bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs, |
| 2558 | unsigned NumFromArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2559 | SmallVectorImpl<TemplateArgument> &ToArgs) { |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2560 | for (unsigned I = 0; I != NumFromArgs; ++I) { |
| 2561 | TemplateArgument To = ImportTemplateArgument(FromArgs[I]); |
| 2562 | if (To.isNull() && !FromArgs[I].isNull()) |
| 2563 | return true; |
| 2564 | |
| 2565 | ToArgs.push_back(To); |
| 2566 | } |
| 2567 | |
| 2568 | return false; |
| 2569 | } |
| 2570 | |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2571 | bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord, |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2572 | RecordDecl *ToRecord, bool Complain) { |
Sean Callanan | c665c9e | 2013-10-09 21:45:11 +0000 | [diff] [blame] | 2573 | // Eliminate a potential failure point where we attempt to re-import |
| 2574 | // something we're trying to import while completing ToRecord. |
| 2575 | Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord); |
| 2576 | if (ToOrigin) { |
| 2577 | RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin); |
| 2578 | if (ToOriginRecord) |
| 2579 | ToRecord = ToOriginRecord; |
| 2580 | } |
| 2581 | |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 2582 | StructuralEquivalenceContext Ctx(Importer.getFromContext(), |
Sean Callanan | c665c9e | 2013-10-09 21:45:11 +0000 | [diff] [blame] | 2583 | ToRecord->getASTContext(), |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2584 | Importer.getNonEquivalentDecls(), |
| 2585 | false, Complain); |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 2586 | return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2587 | } |
| 2588 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2589 | bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar, |
| 2590 | bool Complain) { |
| 2591 | StructuralEquivalenceContext Ctx( |
| 2592 | Importer.getFromContext(), Importer.getToContext(), |
| 2593 | Importer.getNonEquivalentDecls(), false, Complain); |
| 2594 | return Ctx.IsStructurallyEquivalent(FromVar, ToVar); |
| 2595 | } |
| 2596 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2597 | bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) { |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 2598 | StructuralEquivalenceContext Ctx(Importer.getFromContext(), |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2599 | Importer.getToContext(), |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2600 | Importer.getNonEquivalentDecls()); |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 2601 | return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2602 | } |
| 2603 | |
Douglas Gregor | 9115508 | 2012-11-14 22:29:20 +0000 | [diff] [blame] | 2604 | bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC, |
| 2605 | EnumConstantDecl *ToEC) |
| 2606 | { |
| 2607 | const llvm::APSInt &FromVal = FromEC->getInitVal(); |
| 2608 | const llvm::APSInt &ToVal = ToEC->getInitVal(); |
| 2609 | |
| 2610 | return FromVal.isSigned() == ToVal.isSigned() && |
| 2611 | FromVal.getBitWidth() == ToVal.getBitWidth() && |
| 2612 | FromVal == ToVal; |
| 2613 | } |
| 2614 | |
| 2615 | bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From, |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 2616 | ClassTemplateDecl *To) { |
| 2617 | StructuralEquivalenceContext Ctx(Importer.getFromContext(), |
| 2618 | Importer.getToContext(), |
| 2619 | Importer.getNonEquivalentDecls()); |
| 2620 | return Ctx.IsStructurallyEquivalent(From, To); |
| 2621 | } |
| 2622 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2623 | bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From, |
| 2624 | VarTemplateDecl *To) { |
| 2625 | StructuralEquivalenceContext Ctx(Importer.getFromContext(), |
| 2626 | Importer.getToContext(), |
| 2627 | Importer.getNonEquivalentDecls()); |
| 2628 | return Ctx.IsStructurallyEquivalent(From, To); |
| 2629 | } |
| 2630 | |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 2631 | Decl *ASTNodeImporter::VisitDecl(Decl *D) { |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 2632 | Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 2633 | << D->getDeclKindName(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2634 | return nullptr; |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 2635 | } |
| 2636 | |
Sean Callanan | 6519827 | 2011-11-17 23:20:56 +0000 | [diff] [blame] | 2637 | Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 2638 | TranslationUnitDecl *ToD = |
| 2639 | Importer.getToContext().getTranslationUnitDecl(); |
| 2640 | |
| 2641 | Importer.Imported(D, ToD); |
| 2642 | |
| 2643 | return ToD; |
| 2644 | } |
| 2645 | |
Argyrios Kyrtzidis | 544ea71 | 2016-02-18 23:08:36 +0000 | [diff] [blame] | 2646 | Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) { |
| 2647 | |
| 2648 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 2649 | SourceLocation ColonLoc = Importer.Import(D->getColonLoc()); |
| 2650 | |
| 2651 | // Import the context of this declaration. |
| 2652 | DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| 2653 | if (!DC) |
| 2654 | return nullptr; |
| 2655 | |
| 2656 | AccessSpecDecl *accessSpecDecl |
| 2657 | = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(), |
| 2658 | DC, Loc, ColonLoc); |
| 2659 | |
| 2660 | if (!accessSpecDecl) |
| 2661 | return nullptr; |
| 2662 | |
| 2663 | // Lexical DeclContext and Semantic DeclContext |
| 2664 | // is always the same for the accessSpec. |
| 2665 | accessSpecDecl->setLexicalDeclContext(DC); |
| 2666 | DC->addDeclInternal(accessSpecDecl); |
| 2667 | |
| 2668 | return accessSpecDecl; |
| 2669 | } |
| 2670 | |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 2671 | Decl *ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 2672 | DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| 2673 | if (!DC) |
| 2674 | return nullptr; |
| 2675 | |
| 2676 | DeclContext *LexicalDC = DC; |
| 2677 | |
| 2678 | // Import the location of this declaration. |
| 2679 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 2680 | |
| 2681 | Expr *AssertExpr = Importer.Import(D->getAssertExpr()); |
| 2682 | if (!AssertExpr) |
| 2683 | return nullptr; |
| 2684 | |
| 2685 | StringLiteral *FromMsg = D->getMessage(); |
| 2686 | StringLiteral *ToMsg = cast_or_null<StringLiteral>(Importer.Import(FromMsg)); |
| 2687 | if (!ToMsg && FromMsg) |
| 2688 | return nullptr; |
| 2689 | |
| 2690 | StaticAssertDecl *ToD = StaticAssertDecl::Create( |
| 2691 | Importer.getToContext(), DC, Loc, AssertExpr, ToMsg, |
| 2692 | Importer.Import(D->getRParenLoc()), D->isFailed()); |
| 2693 | |
| 2694 | ToD->setLexicalDeclContext(LexicalDC); |
| 2695 | LexicalDC->addDeclInternal(ToD); |
| 2696 | Importer.Imported(D, ToD); |
| 2697 | return ToD; |
| 2698 | } |
| 2699 | |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2700 | Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) { |
| 2701 | // Import the major distinguishing characteristics of this namespace. |
| 2702 | DeclContext *DC, *LexicalDC; |
| 2703 | DeclarationName Name; |
| 2704 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2705 | NamedDecl *ToD; |
| 2706 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2707 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2708 | if (ToD) |
| 2709 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2710 | |
| 2711 | NamespaceDecl *MergeWithNamespace = nullptr; |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2712 | if (!Name) { |
| 2713 | // This is an anonymous namespace. Adopt an existing anonymous |
| 2714 | // namespace if we can. |
| 2715 | // FIXME: Not testable. |
| 2716 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC)) |
| 2717 | MergeWithNamespace = TU->getAnonymousNamespace(); |
| 2718 | else |
| 2719 | MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace(); |
| 2720 | } else { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2721 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2722 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 2723 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2724 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2725 | if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace)) |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2726 | continue; |
| 2727 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2728 | if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) { |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2729 | MergeWithNamespace = FoundNS; |
| 2730 | ConflictingDecls.clear(); |
| 2731 | break; |
| 2732 | } |
| 2733 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2734 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2735 | } |
| 2736 | |
| 2737 | if (!ConflictingDecls.empty()) { |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 2738 | Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace, |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2739 | ConflictingDecls.data(), |
| 2740 | ConflictingDecls.size()); |
| 2741 | } |
| 2742 | } |
| 2743 | |
| 2744 | // Create the "to" namespace, if needed. |
| 2745 | NamespaceDecl *ToNamespace = MergeWithNamespace; |
| 2746 | if (!ToNamespace) { |
Abramo Bagnara | b5545be | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 2747 | ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 2748 | D->isInline(), |
Abramo Bagnara | b5545be | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 2749 | Importer.Import(D->getLocStart()), |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 2750 | Loc, Name.getAsIdentifierInfo(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2751 | /*PrevDecl=*/nullptr); |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2752 | ToNamespace->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2753 | LexicalDC->addDeclInternal(ToNamespace); |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2754 | |
| 2755 | // If this is an anonymous namespace, register it as the anonymous |
| 2756 | // namespace within its context. |
| 2757 | if (!Name) { |
| 2758 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC)) |
| 2759 | TU->setAnonymousNamespace(ToNamespace); |
| 2760 | else |
| 2761 | cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace); |
| 2762 | } |
| 2763 | } |
| 2764 | Importer.Imported(D, ToNamespace); |
| 2765 | |
| 2766 | ImportDeclContext(D); |
| 2767 | |
| 2768 | return ToNamespace; |
| 2769 | } |
| 2770 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2771 | Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) { |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2772 | // Import the major distinguishing characteristics of this typedef. |
| 2773 | DeclContext *DC, *LexicalDC; |
| 2774 | DeclarationName Name; |
| 2775 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2776 | NamedDecl *ToD; |
| 2777 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2778 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2779 | if (ToD) |
| 2780 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2781 | |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2782 | // If this typedef is not in block scope, determine whether we've |
| 2783 | // seen a typedef with the same name (that we can merge with) or any |
| 2784 | // other entity by that name (which name lookup could conflict with). |
| 2785 | if (!DC->isFunctionOrMethod()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2786 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2787 | unsigned IDNS = Decl::IDNS_Ordinary; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2788 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 2789 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2790 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2791 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2792 | continue; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2793 | if (TypedefNameDecl *FoundTypedef = |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2794 | dyn_cast<TypedefNameDecl>(FoundDecls[I])) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2795 | if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(), |
| 2796 | FoundTypedef->getUnderlyingType())) |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2797 | return Importer.Imported(D, FoundTypedef); |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2798 | } |
| 2799 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2800 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2801 | } |
| 2802 | |
| 2803 | if (!ConflictingDecls.empty()) { |
| 2804 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2805 | ConflictingDecls.data(), |
| 2806 | ConflictingDecls.size()); |
| 2807 | if (!Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2808 | return nullptr; |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2809 | } |
| 2810 | } |
| 2811 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2812 | // Import the underlying type of this typedef; |
| 2813 | QualType T = Importer.Import(D->getUnderlyingType()); |
| 2814 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2815 | return nullptr; |
| 2816 | |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2817 | // Create the new typedef node. |
| 2818 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 2819 | SourceLocation StartL = Importer.Import(D->getLocStart()); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2820 | TypedefNameDecl *ToTypedef; |
| 2821 | if (IsAlias) |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 2822 | ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC, |
| 2823 | StartL, Loc, |
| 2824 | Name.getAsIdentifierInfo(), |
| 2825 | TInfo); |
| 2826 | else |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2827 | ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC, |
| 2828 | StartL, Loc, |
| 2829 | Name.getAsIdentifierInfo(), |
| 2830 | TInfo); |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 2831 | |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2832 | ToTypedef->setAccess(D->getAccess()); |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2833 | ToTypedef->setLexicalDeclContext(LexicalDC); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2834 | Importer.Imported(D, ToTypedef); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2835 | LexicalDC->addDeclInternal(ToTypedef); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2836 | |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2837 | return ToTypedef; |
| 2838 | } |
| 2839 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2840 | Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) { |
| 2841 | return VisitTypedefNameDecl(D, /*IsAlias=*/false); |
| 2842 | } |
| 2843 | |
| 2844 | Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| 2845 | return VisitTypedefNameDecl(D, /*IsAlias=*/true); |
| 2846 | } |
| 2847 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 2848 | Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) { |
| 2849 | // Import the major distinguishing characteristics of this label. |
| 2850 | DeclContext *DC, *LexicalDC; |
| 2851 | DeclarationName Name; |
| 2852 | SourceLocation Loc; |
| 2853 | NamedDecl *ToD; |
| 2854 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 2855 | return nullptr; |
| 2856 | if (ToD) |
| 2857 | return ToD; |
| 2858 | |
| 2859 | assert(LexicalDC->isFunctionOrMethod()); |
| 2860 | |
| 2861 | LabelDecl *ToLabel = D->isGnuLocal() |
| 2862 | ? LabelDecl::Create(Importer.getToContext(), |
| 2863 | DC, Importer.Import(D->getLocation()), |
| 2864 | Name.getAsIdentifierInfo(), |
| 2865 | Importer.Import(D->getLocStart())) |
| 2866 | : LabelDecl::Create(Importer.getToContext(), |
| 2867 | DC, Importer.Import(D->getLocation()), |
| 2868 | Name.getAsIdentifierInfo()); |
| 2869 | Importer.Imported(D, ToLabel); |
| 2870 | |
| 2871 | LabelStmt *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt())); |
| 2872 | if (!Label) |
| 2873 | return nullptr; |
| 2874 | |
| 2875 | ToLabel->setStmt(Label); |
| 2876 | ToLabel->setLexicalDeclContext(LexicalDC); |
| 2877 | LexicalDC->addDeclInternal(ToLabel); |
| 2878 | return ToLabel; |
| 2879 | } |
| 2880 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2881 | Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) { |
| 2882 | // Import the major distinguishing characteristics of this enum. |
| 2883 | DeclContext *DC, *LexicalDC; |
| 2884 | DeclarationName Name; |
| 2885 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2886 | NamedDecl *ToD; |
| 2887 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2888 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2889 | if (ToD) |
| 2890 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2891 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2892 | // Figure out what enum name we're looking for. |
| 2893 | unsigned IDNS = Decl::IDNS_Tag; |
| 2894 | DeclarationName SearchName = Name; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2895 | if (!SearchName && D->getTypedefNameForAnonDecl()) { |
| 2896 | SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName()); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2897 | IDNS = Decl::IDNS_Ordinary; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2898 | } else if (Importer.getToContext().getLangOpts().CPlusPlus) |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2899 | IDNS |= Decl::IDNS_Ordinary; |
| 2900 | |
| 2901 | // We may already have an enum of the same name; try to find and match it. |
| 2902 | if (!DC->isFunctionOrMethod() && SearchName) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2903 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2904 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 2905 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2906 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2907 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2908 | continue; |
| 2909 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2910 | Decl *Found = FoundDecls[I]; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2911 | if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) { |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2912 | if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) |
| 2913 | Found = Tag->getDecl(); |
| 2914 | } |
| 2915 | |
| 2916 | if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) { |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2917 | if (IsStructuralMatch(D, FoundEnum)) |
| 2918 | return Importer.Imported(D, FoundEnum); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2919 | } |
| 2920 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2921 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2922 | } |
| 2923 | |
| 2924 | if (!ConflictingDecls.empty()) { |
| 2925 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2926 | ConflictingDecls.data(), |
| 2927 | ConflictingDecls.size()); |
| 2928 | } |
| 2929 | } |
| 2930 | |
| 2931 | // Create the enum declaration. |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2932 | EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, |
| 2933 | Importer.Import(D->getLocStart()), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2934 | Loc, Name.getAsIdentifierInfo(), nullptr, |
Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2935 | D->isScoped(), D->isScopedUsingClassTag(), |
| 2936 | D->isFixed()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2937 | // Import the qualifier, if any. |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2938 | D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2939 | D2->setAccess(D->getAccess()); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2940 | D2->setLexicalDeclContext(LexicalDC); |
| 2941 | Importer.Imported(D, D2); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2942 | LexicalDC->addDeclInternal(D2); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2943 | |
| 2944 | // Import the integer type. |
| 2945 | QualType ToIntegerType = Importer.Import(D->getIntegerType()); |
| 2946 | if (ToIntegerType.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2947 | return nullptr; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2948 | D2->setIntegerType(ToIntegerType); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2949 | |
| 2950 | // Import the definition |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2951 | if (D->isCompleteDefinition() && ImportDefinition(D, D2)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2952 | return nullptr; |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2953 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2954 | return D2; |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2955 | } |
| 2956 | |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2957 | Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) { |
| 2958 | // If this record has a definition in the translation unit we're coming from, |
| 2959 | // but this particular declaration is not that definition, import the |
| 2960 | // definition and map to that. |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 2961 | TagDecl *Definition = D->getDefinition(); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2962 | if (Definition && Definition != D) { |
| 2963 | Decl *ImportedDef = Importer.Import(Definition); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2964 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2965 | return nullptr; |
| 2966 | |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2967 | return Importer.Imported(D, ImportedDef); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2968 | } |
| 2969 | |
| 2970 | // Import the major distinguishing characteristics of this record. |
| 2971 | DeclContext *DC, *LexicalDC; |
| 2972 | DeclarationName Name; |
| 2973 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2974 | NamedDecl *ToD; |
| 2975 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2976 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2977 | if (ToD) |
| 2978 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2979 | |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2980 | // Figure out what structure name we're looking for. |
| 2981 | unsigned IDNS = Decl::IDNS_Tag; |
| 2982 | DeclarationName SearchName = Name; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2983 | if (!SearchName && D->getTypedefNameForAnonDecl()) { |
| 2984 | SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName()); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2985 | IDNS = Decl::IDNS_Ordinary; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2986 | } else if (Importer.getToContext().getLangOpts().CPlusPlus) |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2987 | IDNS |= Decl::IDNS_Ordinary; |
| 2988 | |
| 2989 | // 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] | 2990 | RecordDecl *AdoptDecl = nullptr; |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2991 | if (!DC->isFunctionOrMethod()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2992 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2993 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 2994 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2995 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2996 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2997 | continue; |
| 2998 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2999 | Decl *Found = FoundDecls[I]; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3000 | if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) { |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 3001 | if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) |
| 3002 | Found = Tag->getDecl(); |
| 3003 | } |
| 3004 | |
| 3005 | if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) { |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 3006 | if (D->isAnonymousStructOrUnion() && |
| 3007 | FoundRecord->isAnonymousStructOrUnion()) { |
| 3008 | // If both anonymous structs/unions are in a record context, make sure |
| 3009 | // they occur in the same location in the context records. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3010 | if (Optional<unsigned> Index1 |
Sean Callanan | 488f861 | 2016-07-14 19:53:44 +0000 | [diff] [blame] | 3011 | = findUntaggedStructOrUnionIndex(D)) { |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3012 | if (Optional<unsigned> Index2 = |
Sean Callanan | 488f861 | 2016-07-14 19:53:44 +0000 | [diff] [blame] | 3013 | findUntaggedStructOrUnionIndex(FoundRecord)) { |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 3014 | if (*Index1 != *Index2) |
| 3015 | continue; |
| 3016 | } |
| 3017 | } |
| 3018 | } |
| 3019 | |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 3020 | if (RecordDecl *FoundDef = FoundRecord->getDefinition()) { |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 3021 | if ((SearchName && !D->isCompleteDefinition()) |
| 3022 | || (D->isCompleteDefinition() && |
| 3023 | D->isAnonymousStructOrUnion() |
| 3024 | == FoundDef->isAnonymousStructOrUnion() && |
| 3025 | IsStructuralMatch(D, FoundDef))) { |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 3026 | // The record types structurally match, or the "from" translation |
| 3027 | // unit only had a forward declaration anyway; call it the same |
| 3028 | // function. |
| 3029 | // FIXME: For C++, we should also merge methods here. |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 3030 | return Importer.Imported(D, FoundDef); |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 3031 | } |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 3032 | } else if (!D->isCompleteDefinition()) { |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 3033 | // We have a forward declaration of this type, so adopt that forward |
| 3034 | // declaration rather than building a new one. |
Sean Callanan | c94711c | 2014-03-04 18:11:50 +0000 | [diff] [blame] | 3035 | |
| 3036 | // If one or both can be completed from external storage then try one |
| 3037 | // last time to complete and compare them before doing this. |
| 3038 | |
| 3039 | if (FoundRecord->hasExternalLexicalStorage() && |
| 3040 | !FoundRecord->isCompleteDefinition()) |
| 3041 | FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord); |
| 3042 | if (D->hasExternalLexicalStorage()) |
| 3043 | D->getASTContext().getExternalSource()->CompleteType(D); |
| 3044 | |
| 3045 | if (FoundRecord->isCompleteDefinition() && |
| 3046 | D->isCompleteDefinition() && |
| 3047 | !IsStructuralMatch(D, FoundRecord)) |
| 3048 | continue; |
| 3049 | |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 3050 | AdoptDecl = FoundRecord; |
| 3051 | continue; |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 3052 | } else if (!SearchName) { |
| 3053 | continue; |
| 3054 | } |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 3055 | } |
| 3056 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3057 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 3058 | } |
| 3059 | |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 3060 | if (!ConflictingDecls.empty() && SearchName) { |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 3061 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 3062 | ConflictingDecls.data(), |
| 3063 | ConflictingDecls.size()); |
| 3064 | } |
| 3065 | } |
| 3066 | |
| 3067 | // Create the record declaration. |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 3068 | RecordDecl *D2 = AdoptDecl; |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3069 | SourceLocation StartLoc = Importer.Import(D->getLocStart()); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 3070 | if (!D2) { |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 3071 | CXXRecordDecl *D2CXX = nullptr; |
| 3072 | if (CXXRecordDecl *DCXX = llvm::dyn_cast<CXXRecordDecl>(D)) { |
| 3073 | if (DCXX->isLambda()) { |
| 3074 | TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo()); |
| 3075 | D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(), |
| 3076 | DC, TInfo, Loc, |
| 3077 | DCXX->isDependentLambda(), |
| 3078 | DCXX->isGenericLambda(), |
| 3079 | DCXX->getLambdaCaptureDefault()); |
| 3080 | Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl()); |
| 3081 | if (DCXX->getLambdaContextDecl() && !CDecl) |
| 3082 | return nullptr; |
Sean Callanan | 041cceb | 2016-05-14 05:43:57 +0000 | [diff] [blame] | 3083 | D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl); |
| 3084 | } else if (DCXX->isInjectedClassName()) { |
| 3085 | // We have to be careful to do a similar dance to the one in |
| 3086 | // Sema::ActOnStartCXXMemberDeclarations |
| 3087 | CXXRecordDecl *const PrevDecl = nullptr; |
| 3088 | const bool DelayTypeCreation = true; |
| 3089 | D2CXX = CXXRecordDecl::Create( |
| 3090 | Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc, |
| 3091 | Name.getAsIdentifierInfo(), PrevDecl, DelayTypeCreation); |
| 3092 | Importer.getToContext().getTypeDeclType( |
| 3093 | D2CXX, llvm::dyn_cast<CXXRecordDecl>(DC)); |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 3094 | } else { |
| 3095 | D2CXX = CXXRecordDecl::Create(Importer.getToContext(), |
| 3096 | D->getTagKind(), |
| 3097 | DC, StartLoc, Loc, |
| 3098 | Name.getAsIdentifierInfo()); |
| 3099 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 3100 | D2 = D2CXX; |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 3101 | D2->setAccess(D->getAccess()); |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 3102 | } else { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 3103 | D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3104 | DC, StartLoc, Loc, Name.getAsIdentifierInfo()); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 3105 | } |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3106 | |
| 3107 | D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 3108 | D2->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3109 | LexicalDC->addDeclInternal(D2); |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 3110 | if (D->isAnonymousStructOrUnion()) |
| 3111 | D2->setAnonymousStructOrUnion(true); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 3112 | } |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 3113 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 3114 | Importer.Imported(D, D2); |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 3115 | |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 3116 | if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3117 | return nullptr; |
| 3118 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 3119 | return D2; |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 3120 | } |
| 3121 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 3122 | Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 3123 | // Import the major distinguishing characteristics of this enumerator. |
| 3124 | DeclContext *DC, *LexicalDC; |
| 3125 | DeclarationName Name; |
| 3126 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3127 | NamedDecl *ToD; |
| 3128 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3129 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3130 | if (ToD) |
| 3131 | return ToD; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3132 | |
| 3133 | QualType T = Importer.Import(D->getType()); |
| 3134 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3135 | return nullptr; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3136 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 3137 | // Determine whether there are any other declarations with the same name and |
| 3138 | // in the same context. |
| 3139 | if (!LexicalDC->isFunctionOrMethod()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3140 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 3141 | unsigned IDNS = Decl::IDNS_Ordinary; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3142 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 3143 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3144 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 3145 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 3146 | continue; |
Douglas Gregor | 9115508 | 2012-11-14 22:29:20 +0000 | [diff] [blame] | 3147 | |
| 3148 | if (EnumConstantDecl *FoundEnumConstant |
| 3149 | = dyn_cast<EnumConstantDecl>(FoundDecls[I])) { |
| 3150 | if (IsStructuralMatch(D, FoundEnumConstant)) |
| 3151 | return Importer.Imported(D, FoundEnumConstant); |
| 3152 | } |
| 3153 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3154 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 3155 | } |
| 3156 | |
| 3157 | if (!ConflictingDecls.empty()) { |
| 3158 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 3159 | ConflictingDecls.data(), |
| 3160 | ConflictingDecls.size()); |
| 3161 | if (!Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3162 | return nullptr; |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 3163 | } |
| 3164 | } |
| 3165 | |
| 3166 | Expr *Init = Importer.Import(D->getInitExpr()); |
| 3167 | if (D->getInitExpr() && !Init) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3168 | return nullptr; |
| 3169 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 3170 | EnumConstantDecl *ToEnumerator |
| 3171 | = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc, |
| 3172 | Name.getAsIdentifierInfo(), T, |
| 3173 | Init, D->getInitVal()); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 3174 | ToEnumerator->setAccess(D->getAccess()); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 3175 | ToEnumerator->setLexicalDeclContext(LexicalDC); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 3176 | Importer.Imported(D, ToEnumerator); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3177 | LexicalDC->addDeclInternal(ToEnumerator); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 3178 | return ToEnumerator; |
| 3179 | } |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 3180 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3181 | Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) { |
| 3182 | // Import the major distinguishing characteristics of this function. |
| 3183 | DeclContext *DC, *LexicalDC; |
| 3184 | DeclarationName Name; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3185 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3186 | NamedDecl *ToD; |
| 3187 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3188 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3189 | if (ToD) |
| 3190 | return ToD; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3191 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3192 | // Try to find a function in our own ("to") context with the same name, same |
| 3193 | // type, and in the same context as the function we're importing. |
| 3194 | if (!LexicalDC->isFunctionOrMethod()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3195 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3196 | unsigned IDNS = Decl::IDNS_Ordinary; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3197 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 3198 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3199 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 3200 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3201 | continue; |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3202 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3203 | if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) { |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 3204 | if (FoundFunction->hasExternalFormalLinkage() && |
| 3205 | D->hasExternalFormalLinkage()) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3206 | if (Importer.IsStructurallyEquivalent(D->getType(), |
| 3207 | FoundFunction->getType())) { |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3208 | // FIXME: Actually try to merge the body and other attributes. |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 3209 | return Importer.Imported(D, FoundFunction); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3210 | } |
| 3211 | |
| 3212 | // FIXME: Check for overloading more carefully, e.g., by boosting |
| 3213 | // Sema::IsOverload out to the AST library. |
| 3214 | |
| 3215 | // Function overloading is okay in C++. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3216 | if (Importer.getToContext().getLangOpts().CPlusPlus) |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3217 | continue; |
| 3218 | |
| 3219 | // Complain about inconsistent function types. |
| 3220 | Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent) |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3221 | << Name << D->getType() << FoundFunction->getType(); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3222 | Importer.ToDiag(FoundFunction->getLocation(), |
| 3223 | diag::note_odr_value_here) |
| 3224 | << FoundFunction->getType(); |
| 3225 | } |
| 3226 | } |
| 3227 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3228 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3229 | } |
| 3230 | |
| 3231 | if (!ConflictingDecls.empty()) { |
| 3232 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 3233 | ConflictingDecls.data(), |
| 3234 | ConflictingDecls.size()); |
| 3235 | if (!Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3236 | return nullptr; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3237 | } |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 3238 | } |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3239 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3240 | DeclarationNameInfo NameInfo(Name, Loc); |
| 3241 | // Import additional name location/type info. |
| 3242 | ImportDeclarationNameLoc(D->getNameInfo(), NameInfo); |
| 3243 | |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 3244 | QualType FromTy = D->getType(); |
| 3245 | bool usedDifferentExceptionSpec = false; |
| 3246 | |
| 3247 | if (const FunctionProtoType * |
| 3248 | FromFPT = D->getType()->getAs<FunctionProtoType>()) { |
| 3249 | FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo(); |
| 3250 | // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the |
| 3251 | // FunctionDecl that we are importing the FunctionProtoType for. |
| 3252 | // To avoid an infinite recursion when importing, create the FunctionDecl |
| 3253 | // with a simplified function type and update it afterwards. |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3254 | if (FromEPI.ExceptionSpec.SourceDecl || |
| 3255 | FromEPI.ExceptionSpec.SourceTemplate || |
| 3256 | FromEPI.ExceptionSpec.NoexceptExpr) { |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 3257 | FunctionProtoType::ExtProtoInfo DefaultEPI; |
| 3258 | FromTy = Importer.getFromContext().getFunctionType( |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3259 | FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI); |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 3260 | usedDifferentExceptionSpec = true; |
| 3261 | } |
| 3262 | } |
| 3263 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3264 | // Import the type. |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 3265 | QualType T = Importer.Import(FromTy); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3266 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3267 | return nullptr; |
| 3268 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3269 | // Import the function parameters. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3270 | SmallVector<ParmVarDecl *, 8> Parameters; |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3271 | for (auto P : D->parameters()) { |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 3272 | ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P)); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3273 | if (!ToP) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3274 | return nullptr; |
| 3275 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3276 | Parameters.push_back(ToP); |
| 3277 | } |
| 3278 | |
| 3279 | // Create the imported function. |
| 3280 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3281 | FunctionDecl *ToFunction = nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3282 | SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart()); |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 3283 | if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) { |
| 3284 | ToFunction = CXXConstructorDecl::Create(Importer.getToContext(), |
| 3285 | cast<CXXRecordDecl>(DC), |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3286 | InnerLocStart, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3287 | NameInfo, T, TInfo, |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 3288 | FromConstructor->isExplicit(), |
| 3289 | D->isInlineSpecified(), |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 3290 | D->isImplicit(), |
| 3291 | D->isConstexpr()); |
Sean Callanan | dd2c174 | 2016-05-16 20:48:03 +0000 | [diff] [blame] | 3292 | if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) { |
| 3293 | SmallVector<CXXCtorInitializer *, 4> CtorInitializers; |
| 3294 | for (CXXCtorInitializer *I : FromConstructor->inits()) { |
| 3295 | CXXCtorInitializer *ToI = |
| 3296 | cast_or_null<CXXCtorInitializer>(Importer.Import(I)); |
| 3297 | if (!ToI && I) |
| 3298 | return nullptr; |
| 3299 | CtorInitializers.push_back(ToI); |
| 3300 | } |
| 3301 | CXXCtorInitializer **Memory = |
| 3302 | new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers]; |
| 3303 | std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory); |
| 3304 | CXXConstructorDecl *ToCtor = llvm::cast<CXXConstructorDecl>(ToFunction); |
| 3305 | ToCtor->setCtorInitializers(Memory); |
| 3306 | ToCtor->setNumCtorInitializers(NumInitializers); |
| 3307 | } |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 3308 | } else if (isa<CXXDestructorDecl>(D)) { |
| 3309 | ToFunction = CXXDestructorDecl::Create(Importer.getToContext(), |
| 3310 | cast<CXXRecordDecl>(DC), |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3311 | InnerLocStart, |
Craig Silverstein | af8808d | 2010-10-21 00:44:50 +0000 | [diff] [blame] | 3312 | NameInfo, T, TInfo, |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 3313 | D->isInlineSpecified(), |
| 3314 | D->isImplicit()); |
| 3315 | } else if (CXXConversionDecl *FromConversion |
| 3316 | = dyn_cast<CXXConversionDecl>(D)) { |
| 3317 | ToFunction = CXXConversionDecl::Create(Importer.getToContext(), |
| 3318 | cast<CXXRecordDecl>(DC), |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3319 | InnerLocStart, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3320 | NameInfo, T, TInfo, |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 3321 | D->isInlineSpecified(), |
Douglas Gregor | f2f0806 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 3322 | FromConversion->isExplicit(), |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 3323 | D->isConstexpr(), |
Douglas Gregor | f2f0806 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 3324 | Importer.Import(D->getLocEnd())); |
Douglas Gregor | a50ad13 | 2010-11-29 16:04:58 +0000 | [diff] [blame] | 3325 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 3326 | ToFunction = CXXMethodDecl::Create(Importer.getToContext(), |
| 3327 | cast<CXXRecordDecl>(DC), |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3328 | InnerLocStart, |
Douglas Gregor | a50ad13 | 2010-11-29 16:04:58 +0000 | [diff] [blame] | 3329 | NameInfo, T, TInfo, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3330 | Method->getStorageClass(), |
Douglas Gregor | f2f0806 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 3331 | Method->isInlineSpecified(), |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 3332 | D->isConstexpr(), |
Douglas Gregor | f2f0806 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 3333 | Importer.Import(D->getLocEnd())); |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 3334 | } else { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3335 | ToFunction = FunctionDecl::Create(Importer.getToContext(), DC, |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3336 | InnerLocStart, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3337 | NameInfo, T, TInfo, D->getStorageClass(), |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 3338 | D->isInlineSpecified(), |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 3339 | D->hasWrittenPrototype(), |
| 3340 | D->isConstexpr()); |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 3341 | } |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3342 | |
| 3343 | // Import the qualifier, if any. |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3344 | ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 3345 | ToFunction->setAccess(D->getAccess()); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3346 | ToFunction->setLexicalDeclContext(LexicalDC); |
John McCall | 08432c8 | 2011-01-27 02:37:01 +0000 | [diff] [blame] | 3347 | ToFunction->setVirtualAsWritten(D->isVirtualAsWritten()); |
| 3348 | ToFunction->setTrivial(D->isTrivial()); |
| 3349 | ToFunction->setPure(D->isPure()); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3350 | Importer.Imported(D, ToFunction); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 3351 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3352 | // Set the parameters. |
| 3353 | for (unsigned I = 0, N = Parameters.size(); I != N; ++I) { |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3354 | Parameters[I]->setOwningFunction(ToFunction); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3355 | ToFunction->addDeclInternal(Parameters[I]); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3356 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 3357 | ToFunction->setParams(Parameters); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3358 | |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 3359 | if (usedDifferentExceptionSpec) { |
| 3360 | // Update FunctionProtoType::ExtProtoInfo. |
| 3361 | QualType T = Importer.Import(D->getType()); |
| 3362 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3363 | return nullptr; |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 3364 | ToFunction->setType(T); |
Argyrios Kyrtzidis | b41791d | 2012-09-22 01:58:06 +0000 | [diff] [blame] | 3365 | } |
| 3366 | |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3367 | // Import the body, if any. |
| 3368 | if (Stmt *FromBody = D->getBody()) { |
| 3369 | if (Stmt *ToBody = Importer.Import(FromBody)) { |
| 3370 | ToFunction->setBody(ToBody); |
| 3371 | } |
| 3372 | } |
| 3373 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3374 | // FIXME: Other bits to merge? |
Douglas Gregor | 0eaa2bf | 2010-10-01 23:55:07 +0000 | [diff] [blame] | 3375 | |
| 3376 | // Add this function to the lexical context. |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3377 | LexicalDC->addDeclInternal(ToFunction); |
Douglas Gregor | 0eaa2bf | 2010-10-01 23:55:07 +0000 | [diff] [blame] | 3378 | |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3379 | return ToFunction; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3380 | } |
| 3381 | |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 3382 | Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) { |
| 3383 | return VisitFunctionDecl(D); |
| 3384 | } |
| 3385 | |
| 3386 | Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
| 3387 | return VisitCXXMethodDecl(D); |
| 3388 | } |
| 3389 | |
| 3390 | Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
| 3391 | return VisitCXXMethodDecl(D); |
| 3392 | } |
| 3393 | |
| 3394 | Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) { |
| 3395 | return VisitCXXMethodDecl(D); |
| 3396 | } |
| 3397 | |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 3398 | static unsigned getFieldIndex(Decl *F) { |
| 3399 | RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext()); |
| 3400 | if (!Owner) |
| 3401 | return 0; |
| 3402 | |
| 3403 | unsigned Index = 1; |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 3404 | for (const auto *D : Owner->noload_decls()) { |
| 3405 | if (D == F) |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 3406 | return Index; |
| 3407 | |
| 3408 | if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D)) |
| 3409 | ++Index; |
| 3410 | } |
| 3411 | |
| 3412 | return Index; |
| 3413 | } |
| 3414 | |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 3415 | Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) { |
| 3416 | // Import the major distinguishing characteristics of a variable. |
| 3417 | DeclContext *DC, *LexicalDC; |
| 3418 | DeclarationName Name; |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 3419 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3420 | NamedDecl *ToD; |
| 3421 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3422 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3423 | if (ToD) |
| 3424 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3425 | |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 3426 | // Determine whether we've already imported this field. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3427 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 3428 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3429 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 3430 | if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) { |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 3431 | // For anonymous fields, match up by index. |
| 3432 | if (!Name && getFieldIndex(D) != getFieldIndex(FoundField)) |
| 3433 | continue; |
| 3434 | |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 3435 | if (Importer.IsStructurallyEquivalent(D->getType(), |
| 3436 | FoundField->getType())) { |
| 3437 | Importer.Imported(D, FoundField); |
| 3438 | return FoundField; |
| 3439 | } |
| 3440 | |
| 3441 | Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent) |
| 3442 | << Name << D->getType() << FoundField->getType(); |
| 3443 | Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) |
| 3444 | << FoundField->getType(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3445 | return nullptr; |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 3446 | } |
| 3447 | } |
| 3448 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3449 | // Import the type. |
| 3450 | QualType T = Importer.Import(D->getType()); |
| 3451 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3452 | return nullptr; |
| 3453 | |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 3454 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 3455 | Expr *BitWidth = Importer.Import(D->getBitWidth()); |
| 3456 | if (!BitWidth && D->getBitWidth()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3457 | return nullptr; |
| 3458 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3459 | FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC, |
| 3460 | Importer.Import(D->getInnerLocStart()), |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 3461 | Loc, Name.getAsIdentifierInfo(), |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3462 | T, TInfo, BitWidth, D->isMutable(), |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3463 | D->getInClassInitStyle()); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 3464 | ToField->setAccess(D->getAccess()); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 3465 | ToField->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 3a83ea7 | 2016-03-03 02:22:05 +0000 | [diff] [blame] | 3466 | if (Expr *FromInitializer = D->getInClassInitializer()) { |
Sean Callanan | bb33f58 | 2016-03-03 01:21:28 +0000 | [diff] [blame] | 3467 | Expr *ToInitializer = Importer.Import(FromInitializer); |
| 3468 | if (ToInitializer) |
| 3469 | ToField->setInClassInitializer(ToInitializer); |
| 3470 | else |
| 3471 | return nullptr; |
| 3472 | } |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 3473 | ToField->setImplicit(D->isImplicit()); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 3474 | Importer.Imported(D, ToField); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3475 | LexicalDC->addDeclInternal(ToField); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 3476 | return ToField; |
| 3477 | } |
| 3478 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3479 | Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) { |
| 3480 | // Import the major distinguishing characteristics of a variable. |
| 3481 | DeclContext *DC, *LexicalDC; |
| 3482 | DeclarationName Name; |
| 3483 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3484 | NamedDecl *ToD; |
| 3485 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3486 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3487 | if (ToD) |
| 3488 | return ToD; |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3489 | |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 3490 | // Determine whether we've already imported this field. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3491 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 3492 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3493 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 3494 | if (IndirectFieldDecl *FoundField |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3495 | = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) { |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 3496 | // For anonymous indirect fields, match up by index. |
| 3497 | if (!Name && getFieldIndex(D) != getFieldIndex(FoundField)) |
| 3498 | continue; |
| 3499 | |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 3500 | if (Importer.IsStructurallyEquivalent(D->getType(), |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 3501 | FoundField->getType(), |
David Blaikie | 7d17010 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 3502 | !Name.isEmpty())) { |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 3503 | Importer.Imported(D, FoundField); |
| 3504 | return FoundField; |
| 3505 | } |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 3506 | |
| 3507 | // If there are more anonymous fields to check, continue. |
| 3508 | if (!Name && I < N-1) |
| 3509 | continue; |
| 3510 | |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 3511 | Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent) |
| 3512 | << Name << D->getType() << FoundField->getType(); |
| 3513 | Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) |
| 3514 | << FoundField->getType(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3515 | return nullptr; |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 3516 | } |
| 3517 | } |
| 3518 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3519 | // Import the type. |
| 3520 | QualType T = Importer.Import(D->getType()); |
| 3521 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3522 | return nullptr; |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3523 | |
| 3524 | NamedDecl **NamedChain = |
| 3525 | new (Importer.getToContext())NamedDecl*[D->getChainingSize()]; |
| 3526 | |
| 3527 | unsigned i = 0; |
Aaron Ballman | 29c9460 | 2014-03-07 18:36:15 +0000 | [diff] [blame] | 3528 | for (auto *PI : D->chain()) { |
Aaron Ballman | 1391608 | 2014-03-07 18:11:58 +0000 | [diff] [blame] | 3529 | Decl *D = Importer.Import(PI); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3530 | if (!D) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3531 | return nullptr; |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3532 | NamedChain[i++] = cast<NamedDecl>(D); |
| 3533 | } |
| 3534 | |
| 3535 | IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create( |
Aaron Ballman | 260995b | 2014-10-15 16:58:18 +0000 | [diff] [blame] | 3536 | Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T, |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3537 | {NamedChain, D->getChainingSize()}); |
Aaron Ballman | 260995b | 2014-10-15 16:58:18 +0000 | [diff] [blame] | 3538 | |
| 3539 | for (const auto *Attr : D->attrs()) |
| 3540 | ToIndirectField->addAttr(Attr->clone(Importer.getToContext())); |
| 3541 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3542 | ToIndirectField->setAccess(D->getAccess()); |
| 3543 | ToIndirectField->setLexicalDeclContext(LexicalDC); |
| 3544 | Importer.Imported(D, ToIndirectField); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3545 | LexicalDC->addDeclInternal(ToIndirectField); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3546 | return ToIndirectField; |
| 3547 | } |
| 3548 | |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 3549 | Decl *ASTNodeImporter::VisitFriendDecl(FriendDecl *D) { |
| 3550 | // Import the major distinguishing characteristics of a declaration. |
| 3551 | DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| 3552 | DeclContext *LexicalDC = D->getDeclContext() == D->getLexicalDeclContext() |
| 3553 | ? DC : Importer.ImportContext(D->getLexicalDeclContext()); |
| 3554 | if (!DC || !LexicalDC) |
| 3555 | return nullptr; |
| 3556 | |
| 3557 | // Determine whether we've already imported this decl. |
| 3558 | // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup. |
| 3559 | auto *RD = cast<CXXRecordDecl>(DC); |
| 3560 | FriendDecl *ImportedFriend = RD->getFirstFriend(); |
| 3561 | StructuralEquivalenceContext Context( |
| 3562 | Importer.getFromContext(), Importer.getToContext(), |
| 3563 | Importer.getNonEquivalentDecls(), false, false); |
| 3564 | |
| 3565 | while (ImportedFriend) { |
| 3566 | if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) { |
| 3567 | if (Context.IsStructurallyEquivalent(D->getFriendDecl(), |
| 3568 | ImportedFriend->getFriendDecl())) |
| 3569 | return Importer.Imported(D, ImportedFriend); |
| 3570 | |
| 3571 | } else if (D->getFriendType() && ImportedFriend->getFriendType()) { |
| 3572 | if (Importer.IsStructurallyEquivalent( |
| 3573 | D->getFriendType()->getType(), |
| 3574 | ImportedFriend->getFriendType()->getType(), true)) |
| 3575 | return Importer.Imported(D, ImportedFriend); |
| 3576 | } |
| 3577 | ImportedFriend = ImportedFriend->getNextFriend(); |
| 3578 | } |
| 3579 | |
| 3580 | // Not found. Create it. |
| 3581 | FriendDecl::FriendUnion ToFU; |
| 3582 | if (NamedDecl *FriendD = D->getFriendDecl()) |
| 3583 | ToFU = cast_or_null<NamedDecl>(Importer.Import(FriendD)); |
| 3584 | else |
| 3585 | ToFU = Importer.Import(D->getFriendType()); |
| 3586 | if (!ToFU) |
| 3587 | return nullptr; |
| 3588 | |
| 3589 | SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists); |
| 3590 | TemplateParameterList **FromTPLists = |
| 3591 | D->getTrailingObjects<TemplateParameterList *>(); |
| 3592 | for (unsigned I = 0; I < D->NumTPLists; I++) { |
| 3593 | TemplateParameterList *List = ImportTemplateParameterList(FromTPLists[I]); |
| 3594 | if (!List) |
| 3595 | return nullptr; |
| 3596 | ToTPLists[I] = List; |
| 3597 | } |
| 3598 | |
| 3599 | FriendDecl *FrD = FriendDecl::Create(Importer.getToContext(), DC, |
| 3600 | Importer.Import(D->getLocation()), |
| 3601 | ToFU, Importer.Import(D->getFriendLoc()), |
| 3602 | ToTPLists); |
| 3603 | |
| 3604 | Importer.Imported(D, FrD); |
| 3605 | RD->pushFriendDecl(FrD); |
| 3606 | |
| 3607 | FrD->setAccess(D->getAccess()); |
| 3608 | FrD->setLexicalDeclContext(LexicalDC); |
| 3609 | LexicalDC->addDeclInternal(FrD); |
| 3610 | return FrD; |
| 3611 | } |
| 3612 | |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3613 | Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) { |
| 3614 | // Import the major distinguishing characteristics of an ivar. |
| 3615 | DeclContext *DC, *LexicalDC; |
| 3616 | DeclarationName Name; |
| 3617 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3618 | NamedDecl *ToD; |
| 3619 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3620 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3621 | if (ToD) |
| 3622 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3623 | |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3624 | // Determine whether we've already imported this ivar |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3625 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 3626 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3627 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 3628 | if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) { |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3629 | if (Importer.IsStructurallyEquivalent(D->getType(), |
| 3630 | FoundIvar->getType())) { |
| 3631 | Importer.Imported(D, FoundIvar); |
| 3632 | return FoundIvar; |
| 3633 | } |
| 3634 | |
| 3635 | Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent) |
| 3636 | << Name << D->getType() << FoundIvar->getType(); |
| 3637 | Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here) |
| 3638 | << FoundIvar->getType(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3639 | return nullptr; |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3640 | } |
| 3641 | } |
| 3642 | |
| 3643 | // Import the type. |
| 3644 | QualType T = Importer.Import(D->getType()); |
| 3645 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3646 | return nullptr; |
| 3647 | |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3648 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 3649 | Expr *BitWidth = Importer.Import(D->getBitWidth()); |
| 3650 | if (!BitWidth && D->getBitWidth()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3651 | return nullptr; |
| 3652 | |
Daniel Dunbar | fe3ead7 | 2010-04-02 20:10:03 +0000 | [diff] [blame] | 3653 | ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(), |
| 3654 | cast<ObjCContainerDecl>(DC), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3655 | Importer.Import(D->getInnerLocStart()), |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3656 | Loc, Name.getAsIdentifierInfo(), |
| 3657 | T, TInfo, D->getAccessControl(), |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 3658 | BitWidth, D->getSynthesize()); |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3659 | ToIvar->setLexicalDeclContext(LexicalDC); |
| 3660 | Importer.Imported(D, ToIvar); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3661 | LexicalDC->addDeclInternal(ToIvar); |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3662 | return ToIvar; |
| 3663 | |
| 3664 | } |
| 3665 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3666 | Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) { |
| 3667 | // Import the major distinguishing characteristics of a variable. |
| 3668 | DeclContext *DC, *LexicalDC; |
| 3669 | DeclarationName Name; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3670 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3671 | NamedDecl *ToD; |
| 3672 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3673 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3674 | if (ToD) |
| 3675 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3676 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3677 | // Try to find a variable in our own ("to") context with the same name and |
| 3678 | // in the same context as the variable we're importing. |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 3679 | if (D->isFileVarDecl()) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3680 | VarDecl *MergeWithVar = nullptr; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3681 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3682 | unsigned IDNS = Decl::IDNS_Ordinary; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3683 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 3684 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3685 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 3686 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3687 | continue; |
| 3688 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3689 | if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) { |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3690 | // 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] | 3691 | if (FoundVar->hasExternalFormalLinkage() && |
| 3692 | D->hasExternalFormalLinkage()) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3693 | if (Importer.IsStructurallyEquivalent(D->getType(), |
| 3694 | FoundVar->getType())) { |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3695 | MergeWithVar = FoundVar; |
| 3696 | break; |
| 3697 | } |
| 3698 | |
Douglas Gregor | 56521c5 | 2010-02-12 17:23:39 +0000 | [diff] [blame] | 3699 | const ArrayType *FoundArray |
| 3700 | = Importer.getToContext().getAsArrayType(FoundVar->getType()); |
| 3701 | const ArrayType *TArray |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3702 | = Importer.getToContext().getAsArrayType(D->getType()); |
Douglas Gregor | 56521c5 | 2010-02-12 17:23:39 +0000 | [diff] [blame] | 3703 | if (FoundArray && TArray) { |
| 3704 | if (isa<IncompleteArrayType>(FoundArray) && |
| 3705 | isa<ConstantArrayType>(TArray)) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3706 | // Import the type. |
| 3707 | QualType T = Importer.Import(D->getType()); |
| 3708 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3709 | return nullptr; |
| 3710 | |
Douglas Gregor | 56521c5 | 2010-02-12 17:23:39 +0000 | [diff] [blame] | 3711 | FoundVar->setType(T); |
| 3712 | MergeWithVar = FoundVar; |
| 3713 | break; |
| 3714 | } else if (isa<IncompleteArrayType>(TArray) && |
| 3715 | isa<ConstantArrayType>(FoundArray)) { |
| 3716 | MergeWithVar = FoundVar; |
| 3717 | break; |
Douglas Gregor | 2fbe558 | 2010-02-10 17:16:49 +0000 | [diff] [blame] | 3718 | } |
| 3719 | } |
| 3720 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3721 | Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent) |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3722 | << Name << D->getType() << FoundVar->getType(); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3723 | Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here) |
| 3724 | << FoundVar->getType(); |
| 3725 | } |
| 3726 | } |
| 3727 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3728 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3729 | } |
| 3730 | |
| 3731 | if (MergeWithVar) { |
| 3732 | // An equivalent variable with external linkage has been found. Link |
| 3733 | // the two declarations, then merge them. |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 3734 | Importer.Imported(D, MergeWithVar); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3735 | |
| 3736 | if (VarDecl *DDef = D->getDefinition()) { |
| 3737 | if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) { |
| 3738 | Importer.ToDiag(ExistingDef->getLocation(), |
| 3739 | diag::err_odr_variable_multiple_def) |
| 3740 | << Name; |
| 3741 | Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here); |
| 3742 | } else { |
| 3743 | Expr *Init = Importer.Import(DDef->getInit()); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3744 | MergeWithVar->setInit(Init); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 3745 | if (DDef->isInitKnownICE()) { |
| 3746 | EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt(); |
| 3747 | Eval->CheckedICE = true; |
| 3748 | Eval->IsICE = DDef->isInitICE(); |
| 3749 | } |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3750 | } |
| 3751 | } |
| 3752 | |
| 3753 | return MergeWithVar; |
| 3754 | } |
| 3755 | |
| 3756 | if (!ConflictingDecls.empty()) { |
| 3757 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 3758 | ConflictingDecls.data(), |
| 3759 | ConflictingDecls.size()); |
| 3760 | if (!Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3761 | return nullptr; |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3762 | } |
| 3763 | } |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 3764 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3765 | // Import the type. |
| 3766 | QualType T = Importer.Import(D->getType()); |
| 3767 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3768 | return nullptr; |
| 3769 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3770 | // Create the imported variable. |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 3771 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3772 | VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, |
| 3773 | Importer.Import(D->getInnerLocStart()), |
| 3774 | Loc, Name.getAsIdentifierInfo(), |
| 3775 | T, TInfo, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3776 | D->getStorageClass()); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3777 | ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 3778 | ToVar->setAccess(D->getAccess()); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 3779 | ToVar->setLexicalDeclContext(LexicalDC); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 3780 | Importer.Imported(D, ToVar); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3781 | LexicalDC->addDeclInternal(ToVar); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 3782 | |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3783 | if (!D->isFileVarDecl() && |
| 3784 | D->isUsed()) |
| 3785 | ToVar->setIsUsed(); |
| 3786 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3787 | // Merge the initializer. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3788 | if (ImportDefinition(D, ToVar)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3789 | return nullptr; |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3790 | |
Aleksei Sidorin | 855086d | 2017-01-23 09:30:36 +0000 | [diff] [blame] | 3791 | if (D->isConstexpr()) |
| 3792 | ToVar->setConstexpr(true); |
| 3793 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3794 | return ToVar; |
| 3795 | } |
| 3796 | |
Douglas Gregor | 8b228d7 | 2010-02-17 21:22:52 +0000 | [diff] [blame] | 3797 | Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) { |
| 3798 | // Parameters are created in the translation unit's context, then moved |
| 3799 | // into the function declaration's context afterward. |
| 3800 | DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); |
| 3801 | |
| 3802 | // Import the name of this declaration. |
| 3803 | DeclarationName Name = Importer.Import(D->getDeclName()); |
| 3804 | if (D->getDeclName() && !Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3805 | return nullptr; |
| 3806 | |
Douglas Gregor | 8b228d7 | 2010-02-17 21:22:52 +0000 | [diff] [blame] | 3807 | // Import the location of this declaration. |
| 3808 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 3809 | |
| 3810 | // Import the parameter's type. |
| 3811 | QualType T = Importer.Import(D->getType()); |
| 3812 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3813 | return nullptr; |
| 3814 | |
Douglas Gregor | 8b228d7 | 2010-02-17 21:22:52 +0000 | [diff] [blame] | 3815 | // Create the imported parameter. |
| 3816 | ImplicitParamDecl *ToParm |
| 3817 | = ImplicitParamDecl::Create(Importer.getToContext(), DC, |
| 3818 | Loc, Name.getAsIdentifierInfo(), |
| 3819 | T); |
| 3820 | return Importer.Imported(D, ToParm); |
| 3821 | } |
| 3822 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3823 | Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) { |
| 3824 | // Parameters are created in the translation unit's context, then moved |
| 3825 | // into the function declaration's context afterward. |
| 3826 | DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); |
| 3827 | |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 3828 | // Import the name of this declaration. |
| 3829 | DeclarationName Name = Importer.Import(D->getDeclName()); |
| 3830 | if (D->getDeclName() && !Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3831 | return nullptr; |
| 3832 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3833 | // Import the location of this declaration. |
| 3834 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 3835 | |
| 3836 | // Import the parameter's type. |
| 3837 | QualType T = Importer.Import(D->getType()); |
| 3838 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3839 | return nullptr; |
| 3840 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3841 | // Create the imported parameter. |
| 3842 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 3843 | ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3844 | Importer.Import(D->getInnerLocStart()), |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3845 | Loc, Name.getAsIdentifierInfo(), |
| 3846 | T, TInfo, D->getStorageClass(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3847 | /*FIXME: Default argument*/nullptr); |
John McCall | f3cd665 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 3848 | ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg()); |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3849 | |
| 3850 | if (D->isUsed()) |
| 3851 | ToParm->setIsUsed(); |
| 3852 | |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 3853 | return Importer.Imported(D, ToParm); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3854 | } |
| 3855 | |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3856 | Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) { |
| 3857 | // Import the major distinguishing characteristics of a method. |
| 3858 | DeclContext *DC, *LexicalDC; |
| 3859 | DeclarationName Name; |
| 3860 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3861 | NamedDecl *ToD; |
| 3862 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3863 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3864 | if (ToD) |
| 3865 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3866 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3867 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 3868 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3869 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 3870 | if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) { |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3871 | if (FoundMethod->isInstanceMethod() != D->isInstanceMethod()) |
| 3872 | continue; |
| 3873 | |
| 3874 | // Check return types. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3875 | if (!Importer.IsStructurallyEquivalent(D->getReturnType(), |
| 3876 | FoundMethod->getReturnType())) { |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3877 | Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3878 | << D->isInstanceMethod() << Name << D->getReturnType() |
| 3879 | << FoundMethod->getReturnType(); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3880 | Importer.ToDiag(FoundMethod->getLocation(), |
| 3881 | diag::note_odr_objc_method_here) |
| 3882 | << D->isInstanceMethod() << Name; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3883 | return nullptr; |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3884 | } |
| 3885 | |
| 3886 | // Check the number of parameters. |
| 3887 | if (D->param_size() != FoundMethod->param_size()) { |
| 3888 | Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent) |
| 3889 | << D->isInstanceMethod() << Name |
| 3890 | << D->param_size() << FoundMethod->param_size(); |
| 3891 | Importer.ToDiag(FoundMethod->getLocation(), |
| 3892 | diag::note_odr_objc_method_here) |
| 3893 | << D->isInstanceMethod() << Name; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3894 | return nullptr; |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3895 | } |
| 3896 | |
| 3897 | // Check parameter types. |
| 3898 | for (ObjCMethodDecl::param_iterator P = D->param_begin(), |
| 3899 | PEnd = D->param_end(), FoundP = FoundMethod->param_begin(); |
| 3900 | P != PEnd; ++P, ++FoundP) { |
| 3901 | if (!Importer.IsStructurallyEquivalent((*P)->getType(), |
| 3902 | (*FoundP)->getType())) { |
| 3903 | Importer.FromDiag((*P)->getLocation(), |
| 3904 | diag::err_odr_objc_method_param_type_inconsistent) |
| 3905 | << D->isInstanceMethod() << Name |
| 3906 | << (*P)->getType() << (*FoundP)->getType(); |
| 3907 | Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here) |
| 3908 | << (*FoundP)->getType(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3909 | return nullptr; |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3910 | } |
| 3911 | } |
| 3912 | |
| 3913 | // Check variadic/non-variadic. |
| 3914 | // Check the number of parameters. |
| 3915 | if (D->isVariadic() != FoundMethod->isVariadic()) { |
| 3916 | Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent) |
| 3917 | << D->isInstanceMethod() << Name; |
| 3918 | Importer.ToDiag(FoundMethod->getLocation(), |
| 3919 | diag::note_odr_objc_method_here) |
| 3920 | << D->isInstanceMethod() << Name; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3921 | return nullptr; |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3922 | } |
| 3923 | |
| 3924 | // FIXME: Any other bits we need to merge? |
| 3925 | return Importer.Imported(D, FoundMethod); |
| 3926 | } |
| 3927 | } |
| 3928 | |
| 3929 | // Import the result type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3930 | QualType ResultTy = Importer.Import(D->getReturnType()); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3931 | if (ResultTy.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3932 | return nullptr; |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3933 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3934 | TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo()); |
Douglas Gregor | 12852d9 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 3935 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3936 | ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create( |
| 3937 | Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()), |
| 3938 | Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(), |
| 3939 | D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(), |
| 3940 | D->getImplementationControl(), D->hasRelatedResultType()); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3941 | |
| 3942 | // FIXME: When we decide to merge method definitions, we'll need to |
| 3943 | // deal with implicit parameters. |
| 3944 | |
| 3945 | // Import the parameters |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3946 | SmallVector<ParmVarDecl *, 5> ToParams; |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3947 | for (auto *FromP : D->parameters()) { |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 3948 | ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP)); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3949 | if (!ToP) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3950 | return nullptr; |
| 3951 | |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3952 | ToParams.push_back(ToP); |
| 3953 | } |
| 3954 | |
| 3955 | // Set the parameters. |
| 3956 | for (unsigned I = 0, N = ToParams.size(); I != N; ++I) { |
| 3957 | ToParams[I]->setOwningFunction(ToMethod); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3958 | ToMethod->addDeclInternal(ToParams[I]); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3959 | } |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 3960 | SmallVector<SourceLocation, 12> SelLocs; |
| 3961 | D->getSelectorLocs(SelLocs); |
| 3962 | ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3963 | |
| 3964 | ToMethod->setLexicalDeclContext(LexicalDC); |
| 3965 | Importer.Imported(D, ToMethod); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3966 | LexicalDC->addDeclInternal(ToMethod); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3967 | return ToMethod; |
| 3968 | } |
| 3969 | |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 3970 | Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) { |
| 3971 | // Import the major distinguishing characteristics of a category. |
| 3972 | DeclContext *DC, *LexicalDC; |
| 3973 | DeclarationName Name; |
| 3974 | SourceLocation Loc; |
| 3975 | NamedDecl *ToD; |
| 3976 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 3977 | return nullptr; |
| 3978 | if (ToD) |
| 3979 | return ToD; |
| 3980 | |
| 3981 | TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo()); |
| 3982 | if (!BoundInfo) |
| 3983 | return nullptr; |
| 3984 | |
| 3985 | ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create( |
| 3986 | Importer.getToContext(), DC, |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 3987 | D->getVariance(), |
| 3988 | Importer.Import(D->getVarianceLoc()), |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 3989 | D->getIndex(), |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 3990 | Importer.Import(D->getLocation()), |
| 3991 | Name.getAsIdentifierInfo(), |
| 3992 | Importer.Import(D->getColonLoc()), |
| 3993 | BoundInfo); |
| 3994 | Importer.Imported(D, Result); |
| 3995 | Result->setLexicalDeclContext(LexicalDC); |
| 3996 | return Result; |
| 3997 | } |
| 3998 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3999 | Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) { |
| 4000 | // Import the major distinguishing characteristics of a category. |
| 4001 | DeclContext *DC, *LexicalDC; |
| 4002 | DeclarationName Name; |
| 4003 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4004 | NamedDecl *ToD; |
| 4005 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4006 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4007 | if (ToD) |
| 4008 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4009 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 4010 | ObjCInterfaceDecl *ToInterface |
| 4011 | = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface())); |
| 4012 | if (!ToInterface) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4013 | return nullptr; |
| 4014 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 4015 | // Determine if we've already encountered this category. |
| 4016 | ObjCCategoryDecl *MergeWithCategory |
| 4017 | = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo()); |
| 4018 | ObjCCategoryDecl *ToCategory = MergeWithCategory; |
| 4019 | if (!ToCategory) { |
| 4020 | ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC, |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 4021 | Importer.Import(D->getAtStartLoc()), |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 4022 | Loc, |
| 4023 | Importer.Import(D->getCategoryNameLoc()), |
Argyrios Kyrtzidis | 3a5094b | 2011-08-30 19:43:26 +0000 | [diff] [blame] | 4024 | Name.getAsIdentifierInfo(), |
Fariborz Jahanian | a7765fe | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 4025 | ToInterface, |
Douglas Gregor | ab7f0b3 | 2015-07-07 06:20:12 +0000 | [diff] [blame] | 4026 | /*TypeParamList=*/nullptr, |
Fariborz Jahanian | a7765fe | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 4027 | Importer.Import(D->getIvarLBraceLoc()), |
| 4028 | Importer.Import(D->getIvarRBraceLoc())); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 4029 | ToCategory->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 4030 | LexicalDC->addDeclInternal(ToCategory); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 4031 | Importer.Imported(D, ToCategory); |
Douglas Gregor | ab7f0b3 | 2015-07-07 06:20:12 +0000 | [diff] [blame] | 4032 | // Import the type parameter list after calling Imported, to avoid |
| 4033 | // loops when bringing in their DeclContext. |
| 4034 | ToCategory->setTypeParamList(ImportObjCTypeParamList( |
| 4035 | D->getTypeParamList())); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 4036 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 4037 | // Import protocols |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4038 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 4039 | SmallVector<SourceLocation, 4> ProtocolLocs; |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 4040 | ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc |
| 4041 | = D->protocol_loc_begin(); |
| 4042 | for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(), |
| 4043 | FromProtoEnd = D->protocol_end(); |
| 4044 | FromProto != FromProtoEnd; |
| 4045 | ++FromProto, ++FromProtoLoc) { |
| 4046 | ObjCProtocolDecl *ToProto |
| 4047 | = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); |
| 4048 | if (!ToProto) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4049 | return nullptr; |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 4050 | Protocols.push_back(ToProto); |
| 4051 | ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); |
| 4052 | } |
| 4053 | |
| 4054 | // FIXME: If we're merging, make sure that the protocol list is the same. |
| 4055 | ToCategory->setProtocolList(Protocols.data(), Protocols.size(), |
| 4056 | ProtocolLocs.data(), Importer.getToContext()); |
| 4057 | |
| 4058 | } else { |
| 4059 | Importer.Imported(D, ToCategory); |
| 4060 | } |
| 4061 | |
| 4062 | // Import all of the members of this category. |
Douglas Gregor | 968d633 | 2010-02-21 18:24:45 +0000 | [diff] [blame] | 4063 | ImportDeclContext(D); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 4064 | |
| 4065 | // If we have an implementation, import it as well. |
| 4066 | if (D->getImplementation()) { |
| 4067 | ObjCCategoryImplDecl *Impl |
Douglas Gregor | 35fd7bc | 2010-12-08 16:41:55 +0000 | [diff] [blame] | 4068 | = cast_or_null<ObjCCategoryImplDecl>( |
| 4069 | Importer.Import(D->getImplementation())); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 4070 | if (!Impl) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4071 | return nullptr; |
| 4072 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 4073 | ToCategory->setImplementation(Impl); |
| 4074 | } |
| 4075 | |
| 4076 | return ToCategory; |
| 4077 | } |
| 4078 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4079 | bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From, |
| 4080 | ObjCProtocolDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 4081 | ImportDefinitionKind Kind) { |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4082 | if (To->getDefinition()) { |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 4083 | if (shouldForceImportDeclContext(Kind)) |
| 4084 | ImportDeclContext(From); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4085 | return false; |
| 4086 | } |
| 4087 | |
| 4088 | // Start the protocol definition |
| 4089 | To->startDefinition(); |
| 4090 | |
| 4091 | // Import protocols |
| 4092 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 4093 | SmallVector<SourceLocation, 4> ProtocolLocs; |
| 4094 | ObjCProtocolDecl::protocol_loc_iterator |
| 4095 | FromProtoLoc = From->protocol_loc_begin(); |
| 4096 | for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(), |
| 4097 | FromProtoEnd = From->protocol_end(); |
| 4098 | FromProto != FromProtoEnd; |
| 4099 | ++FromProto, ++FromProtoLoc) { |
| 4100 | ObjCProtocolDecl *ToProto |
| 4101 | = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); |
| 4102 | if (!ToProto) |
| 4103 | return true; |
| 4104 | Protocols.push_back(ToProto); |
| 4105 | ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); |
| 4106 | } |
| 4107 | |
| 4108 | // FIXME: If we're merging, make sure that the protocol list is the same. |
| 4109 | To->setProtocolList(Protocols.data(), Protocols.size(), |
| 4110 | ProtocolLocs.data(), Importer.getToContext()); |
| 4111 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 4112 | if (shouldForceImportDeclContext(Kind)) { |
| 4113 | // Import all of the members of this protocol. |
| 4114 | ImportDeclContext(From, /*ForceImport=*/true); |
| 4115 | } |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4116 | return false; |
| 4117 | } |
| 4118 | |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 4119 | Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) { |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4120 | // If this protocol has a definition in the translation unit we're coming |
| 4121 | // from, but this particular declaration is not that definition, import the |
| 4122 | // definition and map to that. |
| 4123 | ObjCProtocolDecl *Definition = D->getDefinition(); |
| 4124 | if (Definition && Definition != D) { |
| 4125 | Decl *ImportedDef = Importer.Import(Definition); |
| 4126 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4127 | return nullptr; |
| 4128 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4129 | return Importer.Imported(D, ImportedDef); |
| 4130 | } |
| 4131 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 4132 | // Import the major distinguishing characteristics of a protocol. |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 4133 | DeclContext *DC, *LexicalDC; |
| 4134 | DeclarationName Name; |
| 4135 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4136 | NamedDecl *ToD; |
| 4137 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4138 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4139 | if (ToD) |
| 4140 | return ToD; |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 4141 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4142 | ObjCProtocolDecl *MergeWithProtocol = nullptr; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4143 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 4144 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 4145 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 4146 | if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol)) |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 4147 | continue; |
| 4148 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 4149 | if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I]))) |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 4150 | break; |
| 4151 | } |
| 4152 | |
| 4153 | ObjCProtocolDecl *ToProto = MergeWithProtocol; |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4154 | if (!ToProto) { |
| 4155 | ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, |
| 4156 | Name.getAsIdentifierInfo(), Loc, |
| 4157 | Importer.Import(D->getAtStartLoc()), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4158 | /*PrevDecl=*/nullptr); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4159 | ToProto->setLexicalDeclContext(LexicalDC); |
| 4160 | LexicalDC->addDeclInternal(ToProto); |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 4161 | } |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4162 | |
| 4163 | Importer.Imported(D, ToProto); |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 4164 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4165 | if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4166 | return nullptr; |
| 4167 | |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 4168 | return ToProto; |
| 4169 | } |
| 4170 | |
Sean Callanan | 0aae041 | 2014-12-10 00:00:37 +0000 | [diff] [blame] | 4171 | Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 4172 | DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| 4173 | DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 4174 | |
| 4175 | SourceLocation ExternLoc = Importer.Import(D->getExternLoc()); |
| 4176 | SourceLocation LangLoc = Importer.Import(D->getLocation()); |
| 4177 | |
| 4178 | bool HasBraces = D->hasBraces(); |
| 4179 | |
Sean Callanan | b12a855 | 2014-12-10 21:22:20 +0000 | [diff] [blame] | 4180 | LinkageSpecDecl *ToLinkageSpec = |
| 4181 | LinkageSpecDecl::Create(Importer.getToContext(), |
| 4182 | DC, |
| 4183 | ExternLoc, |
| 4184 | LangLoc, |
| 4185 | D->getLanguage(), |
| 4186 | HasBraces); |
Sean Callanan | 0aae041 | 2014-12-10 00:00:37 +0000 | [diff] [blame] | 4187 | |
| 4188 | if (HasBraces) { |
| 4189 | SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc()); |
| 4190 | ToLinkageSpec->setRBraceLoc(RBraceLoc); |
| 4191 | } |
| 4192 | |
| 4193 | ToLinkageSpec->setLexicalDeclContext(LexicalDC); |
| 4194 | LexicalDC->addDeclInternal(ToLinkageSpec); |
| 4195 | |
| 4196 | Importer.Imported(D, ToLinkageSpec); |
| 4197 | |
| 4198 | return ToLinkageSpec; |
| 4199 | } |
| 4200 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4201 | bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From, |
| 4202 | ObjCInterfaceDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 4203 | ImportDefinitionKind Kind) { |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4204 | if (To->getDefinition()) { |
| 4205 | // Check consistency of superclass. |
| 4206 | ObjCInterfaceDecl *FromSuper = From->getSuperClass(); |
| 4207 | if (FromSuper) { |
| 4208 | FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper)); |
| 4209 | if (!FromSuper) |
| 4210 | return true; |
| 4211 | } |
| 4212 | |
| 4213 | ObjCInterfaceDecl *ToSuper = To->getSuperClass(); |
| 4214 | if ((bool)FromSuper != (bool)ToSuper || |
| 4215 | (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) { |
| 4216 | Importer.ToDiag(To->getLocation(), |
| 4217 | diag::err_odr_objc_superclass_inconsistent) |
| 4218 | << To->getDeclName(); |
| 4219 | if (ToSuper) |
| 4220 | Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass) |
| 4221 | << To->getSuperClass()->getDeclName(); |
| 4222 | else |
| 4223 | Importer.ToDiag(To->getLocation(), |
| 4224 | diag::note_odr_objc_missing_superclass); |
| 4225 | if (From->getSuperClass()) |
| 4226 | Importer.FromDiag(From->getSuperClassLoc(), |
| 4227 | diag::note_odr_objc_superclass) |
| 4228 | << From->getSuperClass()->getDeclName(); |
| 4229 | else |
| 4230 | Importer.FromDiag(From->getLocation(), |
| 4231 | diag::note_odr_objc_missing_superclass); |
| 4232 | } |
| 4233 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 4234 | if (shouldForceImportDeclContext(Kind)) |
| 4235 | ImportDeclContext(From); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4236 | return false; |
| 4237 | } |
| 4238 | |
| 4239 | // Start the definition. |
| 4240 | To->startDefinition(); |
| 4241 | |
| 4242 | // If this class has a superclass, import it. |
| 4243 | if (From->getSuperClass()) { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4244 | TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo()); |
| 4245 | if (!SuperTInfo) |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4246 | return true; |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4247 | |
| 4248 | To->setSuperClass(SuperTInfo); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4249 | } |
| 4250 | |
| 4251 | // Import protocols |
| 4252 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 4253 | SmallVector<SourceLocation, 4> ProtocolLocs; |
| 4254 | ObjCInterfaceDecl::protocol_loc_iterator |
| 4255 | FromProtoLoc = From->protocol_loc_begin(); |
| 4256 | |
| 4257 | for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(), |
| 4258 | FromProtoEnd = From->protocol_end(); |
| 4259 | FromProto != FromProtoEnd; |
| 4260 | ++FromProto, ++FromProtoLoc) { |
| 4261 | ObjCProtocolDecl *ToProto |
| 4262 | = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); |
| 4263 | if (!ToProto) |
| 4264 | return true; |
| 4265 | Protocols.push_back(ToProto); |
| 4266 | ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); |
| 4267 | } |
| 4268 | |
| 4269 | // FIXME: If we're merging, make sure that the protocol list is the same. |
| 4270 | To->setProtocolList(Protocols.data(), Protocols.size(), |
| 4271 | ProtocolLocs.data(), Importer.getToContext()); |
| 4272 | |
| 4273 | // Import categories. When the categories themselves are imported, they'll |
| 4274 | // hook themselves into this interface. |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 4275 | for (auto *Cat : From->known_categories()) |
| 4276 | Importer.Import(Cat); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 4277 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4278 | // If we have an @implementation, import it as well. |
| 4279 | if (From->getImplementation()) { |
| 4280 | ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>( |
| 4281 | Importer.Import(From->getImplementation())); |
| 4282 | if (!Impl) |
| 4283 | return true; |
| 4284 | |
| 4285 | To->setImplementation(Impl); |
| 4286 | } |
| 4287 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 4288 | if (shouldForceImportDeclContext(Kind)) { |
| 4289 | // Import all of the members of this class. |
| 4290 | ImportDeclContext(From, /*ForceImport=*/true); |
| 4291 | } |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4292 | return false; |
| 4293 | } |
| 4294 | |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 4295 | ObjCTypeParamList * |
| 4296 | ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) { |
| 4297 | if (!list) |
| 4298 | return nullptr; |
| 4299 | |
| 4300 | SmallVector<ObjCTypeParamDecl *, 4> toTypeParams; |
| 4301 | for (auto fromTypeParam : *list) { |
| 4302 | auto toTypeParam = cast_or_null<ObjCTypeParamDecl>( |
| 4303 | Importer.Import(fromTypeParam)); |
| 4304 | if (!toTypeParam) |
| 4305 | return nullptr; |
| 4306 | |
| 4307 | toTypeParams.push_back(toTypeParam); |
| 4308 | } |
| 4309 | |
| 4310 | return ObjCTypeParamList::create(Importer.getToContext(), |
| 4311 | Importer.Import(list->getLAngleLoc()), |
| 4312 | toTypeParams, |
| 4313 | Importer.Import(list->getRAngleLoc())); |
| 4314 | } |
| 4315 | |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 4316 | Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4317 | // If this class has a definition in the translation unit we're coming from, |
| 4318 | // but this particular declaration is not that definition, import the |
| 4319 | // definition and map to that. |
| 4320 | ObjCInterfaceDecl *Definition = D->getDefinition(); |
| 4321 | if (Definition && Definition != D) { |
| 4322 | Decl *ImportedDef = Importer.Import(Definition); |
| 4323 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4324 | return nullptr; |
| 4325 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4326 | return Importer.Imported(D, ImportedDef); |
| 4327 | } |
| 4328 | |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 4329 | // Import the major distinguishing characteristics of an @interface. |
| 4330 | DeclContext *DC, *LexicalDC; |
| 4331 | DeclarationName Name; |
| 4332 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4333 | NamedDecl *ToD; |
| 4334 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4335 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4336 | if (ToD) |
| 4337 | return ToD; |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 4338 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4339 | // Look for an existing interface with the same name. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4340 | ObjCInterfaceDecl *MergeWithIface = nullptr; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4341 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 4342 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 4343 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 4344 | if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 4345 | continue; |
| 4346 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 4347 | if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I]))) |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 4348 | break; |
| 4349 | } |
| 4350 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4351 | // Create an interface declaration, if one does not already exist. |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 4352 | ObjCInterfaceDecl *ToIface = MergeWithIface; |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4353 | if (!ToIface) { |
| 4354 | ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC, |
| 4355 | Importer.Import(D->getAtStartLoc()), |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 4356 | Name.getAsIdentifierInfo(), |
Douglas Gregor | ab7f0b3 | 2015-07-07 06:20:12 +0000 | [diff] [blame] | 4357 | /*TypeParamList=*/nullptr, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4358 | /*PrevDecl=*/nullptr, Loc, |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4359 | D->isImplicitInterfaceDecl()); |
| 4360 | ToIface->setLexicalDeclContext(LexicalDC); |
| 4361 | LexicalDC->addDeclInternal(ToIface); |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 4362 | } |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4363 | Importer.Imported(D, ToIface); |
Douglas Gregor | ab7f0b3 | 2015-07-07 06:20:12 +0000 | [diff] [blame] | 4364 | // Import the type parameter list after calling Imported, to avoid |
| 4365 | // loops when bringing in their DeclContext. |
| 4366 | ToIface->setTypeParamList(ImportObjCTypeParamList( |
| 4367 | D->getTypeParamListAsWritten())); |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 4368 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4369 | if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4370 | return nullptr; |
| 4371 | |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 4372 | return ToIface; |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 4373 | } |
| 4374 | |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 4375 | Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 4376 | ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>( |
| 4377 | Importer.Import(D->getCategoryDecl())); |
| 4378 | if (!Category) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4379 | return nullptr; |
| 4380 | |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 4381 | ObjCCategoryImplDecl *ToImpl = Category->getImplementation(); |
| 4382 | if (!ToImpl) { |
| 4383 | DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| 4384 | if (!DC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4385 | return nullptr; |
| 4386 | |
Argyrios Kyrtzidis | 4996f5f | 2011-12-09 00:31:40 +0000 | [diff] [blame] | 4387 | SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc()); |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 4388 | ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC, |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 4389 | Importer.Import(D->getIdentifier()), |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 4390 | Category->getClassInterface(), |
| 4391 | Importer.Import(D->getLocation()), |
Argyrios Kyrtzidis | 4996f5f | 2011-12-09 00:31:40 +0000 | [diff] [blame] | 4392 | Importer.Import(D->getAtStartLoc()), |
| 4393 | CategoryNameLoc); |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 4394 | |
| 4395 | DeclContext *LexicalDC = DC; |
| 4396 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 4397 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 4398 | if (!LexicalDC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4399 | return nullptr; |
| 4400 | |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 4401 | ToImpl->setLexicalDeclContext(LexicalDC); |
| 4402 | } |
| 4403 | |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 4404 | LexicalDC->addDeclInternal(ToImpl); |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 4405 | Category->setImplementation(ToImpl); |
| 4406 | } |
| 4407 | |
| 4408 | Importer.Imported(D, ToImpl); |
Douglas Gregor | 35fd7bc | 2010-12-08 16:41:55 +0000 | [diff] [blame] | 4409 | ImportDeclContext(D); |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 4410 | return ToImpl; |
| 4411 | } |
| 4412 | |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 4413 | Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 4414 | // Find the corresponding interface. |
| 4415 | ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>( |
| 4416 | Importer.Import(D->getClassInterface())); |
| 4417 | if (!Iface) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4418 | return nullptr; |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 4419 | |
| 4420 | // Import the superclass, if any. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4421 | ObjCInterfaceDecl *Super = nullptr; |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 4422 | if (D->getSuperClass()) { |
| 4423 | Super = cast_or_null<ObjCInterfaceDecl>( |
| 4424 | Importer.Import(D->getSuperClass())); |
| 4425 | if (!Super) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4426 | return nullptr; |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 4427 | } |
| 4428 | |
| 4429 | ObjCImplementationDecl *Impl = Iface->getImplementation(); |
| 4430 | if (!Impl) { |
| 4431 | // We haven't imported an implementation yet. Create a new @implementation |
| 4432 | // now. |
| 4433 | Impl = ObjCImplementationDecl::Create(Importer.getToContext(), |
| 4434 | Importer.ImportContext(D->getDeclContext()), |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 4435 | Iface, Super, |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 4436 | Importer.Import(D->getLocation()), |
Fariborz Jahanian | a7765fe | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 4437 | Importer.Import(D->getAtStartLoc()), |
Argyrios Kyrtzidis | 5d2ce84 | 2013-05-03 22:31:26 +0000 | [diff] [blame] | 4438 | Importer.Import(D->getSuperClassLoc()), |
Fariborz Jahanian | a7765fe | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 4439 | Importer.Import(D->getIvarLBraceLoc()), |
| 4440 | Importer.Import(D->getIvarRBraceLoc())); |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 4441 | |
| 4442 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 4443 | DeclContext *LexicalDC |
| 4444 | = Importer.ImportContext(D->getLexicalDeclContext()); |
| 4445 | if (!LexicalDC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4446 | return nullptr; |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 4447 | Impl->setLexicalDeclContext(LexicalDC); |
| 4448 | } |
| 4449 | |
| 4450 | // Associate the implementation with the class it implements. |
| 4451 | Iface->setImplementation(Impl); |
| 4452 | Importer.Imported(D, Iface->getImplementation()); |
| 4453 | } else { |
| 4454 | Importer.Imported(D, Iface->getImplementation()); |
| 4455 | |
| 4456 | // Verify that the existing @implementation has the same superclass. |
| 4457 | if ((Super && !Impl->getSuperClass()) || |
| 4458 | (!Super && Impl->getSuperClass()) || |
Craig Topper | dcfc60f | 2014-05-07 06:57:44 +0000 | [diff] [blame] | 4459 | (Super && Impl->getSuperClass() && |
| 4460 | !declaresSameEntity(Super->getCanonicalDecl(), |
| 4461 | Impl->getSuperClass()))) { |
| 4462 | Importer.ToDiag(Impl->getLocation(), |
| 4463 | diag::err_odr_objc_superclass_inconsistent) |
| 4464 | << Iface->getDeclName(); |
| 4465 | // FIXME: It would be nice to have the location of the superclass |
| 4466 | // below. |
| 4467 | if (Impl->getSuperClass()) |
| 4468 | Importer.ToDiag(Impl->getLocation(), |
| 4469 | diag::note_odr_objc_superclass) |
| 4470 | << Impl->getSuperClass()->getDeclName(); |
| 4471 | else |
| 4472 | Importer.ToDiag(Impl->getLocation(), |
| 4473 | diag::note_odr_objc_missing_superclass); |
| 4474 | if (D->getSuperClass()) |
| 4475 | Importer.FromDiag(D->getLocation(), |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 4476 | diag::note_odr_objc_superclass) |
Craig Topper | dcfc60f | 2014-05-07 06:57:44 +0000 | [diff] [blame] | 4477 | << D->getSuperClass()->getDeclName(); |
| 4478 | else |
| 4479 | Importer.FromDiag(D->getLocation(), |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 4480 | diag::note_odr_objc_missing_superclass); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4481 | return nullptr; |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 4482 | } |
| 4483 | } |
| 4484 | |
| 4485 | // Import all of the members of this @implementation. |
| 4486 | ImportDeclContext(D); |
| 4487 | |
| 4488 | return Impl; |
| 4489 | } |
| 4490 | |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 4491 | Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { |
| 4492 | // Import the major distinguishing characteristics of an @property. |
| 4493 | DeclContext *DC, *LexicalDC; |
| 4494 | DeclarationName Name; |
| 4495 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4496 | NamedDecl *ToD; |
| 4497 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4498 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4499 | if (ToD) |
| 4500 | return ToD; |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 4501 | |
| 4502 | // Check whether we have already imported this property. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4503 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 4504 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 4505 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 4506 | if (ObjCPropertyDecl *FoundProp |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 4507 | = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) { |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 4508 | // Check property types. |
| 4509 | if (!Importer.IsStructurallyEquivalent(D->getType(), |
| 4510 | FoundProp->getType())) { |
| 4511 | Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent) |
| 4512 | << Name << D->getType() << FoundProp->getType(); |
| 4513 | Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here) |
| 4514 | << FoundProp->getType(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4515 | return nullptr; |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 4516 | } |
| 4517 | |
| 4518 | // FIXME: Check property attributes, getters, setters, etc.? |
| 4519 | |
| 4520 | // Consider these properties to be equivalent. |
| 4521 | Importer.Imported(D, FoundProp); |
| 4522 | return FoundProp; |
| 4523 | } |
| 4524 | } |
| 4525 | |
| 4526 | // Import the type. |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4527 | TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo()); |
| 4528 | if (!TSI) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4529 | return nullptr; |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 4530 | |
| 4531 | // Create the new property. |
| 4532 | ObjCPropertyDecl *ToProperty |
| 4533 | = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc, |
| 4534 | Name.getAsIdentifierInfo(), |
| 4535 | Importer.Import(D->getAtLoc()), |
Fariborz Jahanian | 86c2f5c | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 4536 | Importer.Import(D->getLParenLoc()), |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4537 | Importer.Import(D->getType()), |
| 4538 | TSI, |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 4539 | D->getPropertyImplementation()); |
| 4540 | Importer.Imported(D, ToProperty); |
| 4541 | ToProperty->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 4542 | LexicalDC->addDeclInternal(ToProperty); |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 4543 | |
| 4544 | ToProperty->setPropertyAttributes(D->getPropertyAttributes()); |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 4545 | ToProperty->setPropertyAttributesAsWritten( |
| 4546 | D->getPropertyAttributesAsWritten()); |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 4547 | ToProperty->setGetterName(Importer.Import(D->getGetterName())); |
| 4548 | ToProperty->setSetterName(Importer.Import(D->getSetterName())); |
| 4549 | ToProperty->setGetterMethodDecl( |
| 4550 | cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl()))); |
| 4551 | ToProperty->setSetterMethodDecl( |
| 4552 | cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl()))); |
| 4553 | ToProperty->setPropertyIvarDecl( |
| 4554 | cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl()))); |
| 4555 | return ToProperty; |
| 4556 | } |
| 4557 | |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4558 | Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { |
| 4559 | ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>( |
| 4560 | Importer.Import(D->getPropertyDecl())); |
| 4561 | if (!Property) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4562 | return nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4563 | |
| 4564 | DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| 4565 | if (!DC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4566 | return nullptr; |
| 4567 | |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4568 | // Import the lexical declaration context. |
| 4569 | DeclContext *LexicalDC = DC; |
| 4570 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 4571 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 4572 | if (!LexicalDC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4573 | return nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4574 | } |
| 4575 | |
| 4576 | ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC); |
| 4577 | if (!InImpl) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4578 | return nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4579 | |
| 4580 | // Import the ivar (for an @synthesize). |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4581 | ObjCIvarDecl *Ivar = nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4582 | if (D->getPropertyIvarDecl()) { |
| 4583 | Ivar = cast_or_null<ObjCIvarDecl>( |
| 4584 | Importer.Import(D->getPropertyIvarDecl())); |
| 4585 | if (!Ivar) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4586 | return nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4587 | } |
| 4588 | |
| 4589 | ObjCPropertyImplDecl *ToImpl |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 4590 | = InImpl->FindPropertyImplDecl(Property->getIdentifier(), |
| 4591 | Property->getQueryKind()); |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4592 | if (!ToImpl) { |
| 4593 | ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC, |
| 4594 | Importer.Import(D->getLocStart()), |
| 4595 | Importer.Import(D->getLocation()), |
| 4596 | Property, |
| 4597 | D->getPropertyImplementation(), |
| 4598 | Ivar, |
| 4599 | Importer.Import(D->getPropertyIvarDeclLoc())); |
| 4600 | ToImpl->setLexicalDeclContext(LexicalDC); |
| 4601 | Importer.Imported(D, ToImpl); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 4602 | LexicalDC->addDeclInternal(ToImpl); |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4603 | } else { |
| 4604 | // Check that we have the same kind of property implementation (@synthesize |
| 4605 | // vs. @dynamic). |
| 4606 | if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) { |
| 4607 | Importer.ToDiag(ToImpl->getLocation(), |
| 4608 | diag::err_odr_objc_property_impl_kind_inconsistent) |
| 4609 | << Property->getDeclName() |
| 4610 | << (ToImpl->getPropertyImplementation() |
| 4611 | == ObjCPropertyImplDecl::Dynamic); |
| 4612 | Importer.FromDiag(D->getLocation(), |
| 4613 | diag::note_odr_objc_property_impl_kind) |
| 4614 | << D->getPropertyDecl()->getDeclName() |
| 4615 | << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4616 | return nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4617 | } |
| 4618 | |
| 4619 | // For @synthesize, check that we have the same |
| 4620 | if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize && |
| 4621 | Ivar != ToImpl->getPropertyIvarDecl()) { |
| 4622 | Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(), |
| 4623 | diag::err_odr_objc_synthesize_ivar_inconsistent) |
| 4624 | << Property->getDeclName() |
| 4625 | << ToImpl->getPropertyIvarDecl()->getDeclName() |
| 4626 | << Ivar->getDeclName(); |
| 4627 | Importer.FromDiag(D->getPropertyIvarDeclLoc(), |
| 4628 | diag::note_odr_objc_synthesize_ivar_here) |
| 4629 | << D->getPropertyIvarDecl()->getDeclName(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4630 | return nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4631 | } |
| 4632 | |
| 4633 | // Merge the existing implementation with the new implementation. |
| 4634 | Importer.Imported(D, ToImpl); |
| 4635 | } |
| 4636 | |
| 4637 | return ToImpl; |
| 4638 | } |
| 4639 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4640 | Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
| 4641 | // For template arguments, we adopt the translation unit as our declaration |
| 4642 | // context. This context will be fixed when the actual template declaration |
| 4643 | // is created. |
| 4644 | |
| 4645 | // FIXME: Import default argument. |
| 4646 | return TemplateTypeParmDecl::Create(Importer.getToContext(), |
| 4647 | Importer.getToContext().getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 4648 | Importer.Import(D->getLocStart()), |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4649 | Importer.Import(D->getLocation()), |
| 4650 | D->getDepth(), |
| 4651 | D->getIndex(), |
| 4652 | Importer.Import(D->getIdentifier()), |
| 4653 | D->wasDeclaredWithTypename(), |
| 4654 | D->isParameterPack()); |
| 4655 | } |
| 4656 | |
| 4657 | Decl * |
| 4658 | ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
| 4659 | // Import the name of this declaration. |
| 4660 | DeclarationName Name = Importer.Import(D->getDeclName()); |
| 4661 | if (D->getDeclName() && !Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4662 | return nullptr; |
| 4663 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4664 | // Import the location of this declaration. |
| 4665 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 4666 | |
| 4667 | // Import the type of this declaration. |
| 4668 | QualType T = Importer.Import(D->getType()); |
| 4669 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4670 | return nullptr; |
| 4671 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4672 | // Import type-source information. |
| 4673 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 4674 | if (D->getTypeSourceInfo() && !TInfo) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4675 | return nullptr; |
| 4676 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4677 | // FIXME: Import default argument. |
| 4678 | |
| 4679 | return NonTypeTemplateParmDecl::Create(Importer.getToContext(), |
| 4680 | Importer.getToContext().getTranslationUnitDecl(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4681 | Importer.Import(D->getInnerLocStart()), |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4682 | Loc, D->getDepth(), D->getPosition(), |
| 4683 | Name.getAsIdentifierInfo(), |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 4684 | T, D->isParameterPack(), TInfo); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4685 | } |
| 4686 | |
| 4687 | Decl * |
| 4688 | ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
| 4689 | // Import the name of this declaration. |
| 4690 | DeclarationName Name = Importer.Import(D->getDeclName()); |
| 4691 | if (D->getDeclName() && !Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4692 | return nullptr; |
| 4693 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4694 | // Import the location of this declaration. |
| 4695 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 4696 | |
| 4697 | // Import template parameters. |
| 4698 | TemplateParameterList *TemplateParams |
| 4699 | = ImportTemplateParameterList(D->getTemplateParameters()); |
| 4700 | if (!TemplateParams) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4701 | return nullptr; |
| 4702 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4703 | // FIXME: Import default argument. |
| 4704 | |
| 4705 | return TemplateTemplateParmDecl::Create(Importer.getToContext(), |
| 4706 | Importer.getToContext().getTranslationUnitDecl(), |
| 4707 | Loc, D->getDepth(), D->getPosition(), |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 4708 | D->isParameterPack(), |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4709 | Name.getAsIdentifierInfo(), |
| 4710 | TemplateParams); |
| 4711 | } |
| 4712 | |
| 4713 | Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 4714 | // If this record has a definition in the translation unit we're coming from, |
| 4715 | // but this particular declaration is not that definition, import the |
| 4716 | // definition and map to that. |
| 4717 | CXXRecordDecl *Definition |
| 4718 | = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition()); |
| 4719 | if (Definition && Definition != D->getTemplatedDecl()) { |
| 4720 | Decl *ImportedDef |
| 4721 | = Importer.Import(Definition->getDescribedClassTemplate()); |
| 4722 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4723 | return nullptr; |
| 4724 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4725 | return Importer.Imported(D, ImportedDef); |
| 4726 | } |
| 4727 | |
| 4728 | // Import the major distinguishing characteristics of this class template. |
| 4729 | DeclContext *DC, *LexicalDC; |
| 4730 | DeclarationName Name; |
| 4731 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4732 | NamedDecl *ToD; |
| 4733 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4734 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4735 | if (ToD) |
| 4736 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4737 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4738 | // We may already have a template of the same name; try to find and match it. |
| 4739 | if (!DC->isFunctionOrMethod()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4740 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4741 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 4742 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 4743 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 4744 | if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4745 | continue; |
| 4746 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 4747 | Decl *Found = FoundDecls[I]; |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4748 | if (ClassTemplateDecl *FoundTemplate |
| 4749 | = dyn_cast<ClassTemplateDecl>(Found)) { |
| 4750 | if (IsStructuralMatch(D, FoundTemplate)) { |
| 4751 | // The class templates structurally match; call it the same template. |
| 4752 | // FIXME: We may be filling in a forward declaration here. Handle |
| 4753 | // this case! |
| 4754 | Importer.Imported(D->getTemplatedDecl(), |
| 4755 | FoundTemplate->getTemplatedDecl()); |
| 4756 | return Importer.Imported(D, FoundTemplate); |
| 4757 | } |
| 4758 | } |
| 4759 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 4760 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4761 | } |
| 4762 | |
| 4763 | if (!ConflictingDecls.empty()) { |
| 4764 | Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary, |
| 4765 | ConflictingDecls.data(), |
| 4766 | ConflictingDecls.size()); |
| 4767 | } |
| 4768 | |
| 4769 | if (!Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4770 | return nullptr; |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4771 | } |
| 4772 | |
| 4773 | CXXRecordDecl *DTemplated = D->getTemplatedDecl(); |
| 4774 | |
| 4775 | // Create the declaration that is being templated. |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 4776 | // Create the declaration that is being templated. |
| 4777 | CXXRecordDecl *D2Templated = cast_or_null<CXXRecordDecl>( |
| 4778 | Importer.Import(DTemplated)); |
| 4779 | if (!D2Templated) |
| 4780 | return nullptr; |
| 4781 | |
| 4782 | // Resolve possible cyclic import. |
| 4783 | if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D)) |
| 4784 | return AlreadyImported; |
| 4785 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4786 | // Create the class template declaration itself. |
| 4787 | TemplateParameterList *TemplateParams |
| 4788 | = ImportTemplateParameterList(D->getTemplateParameters()); |
| 4789 | if (!TemplateParams) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4790 | return nullptr; |
| 4791 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4792 | ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC, |
| 4793 | Loc, Name, TemplateParams, |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 4794 | D2Templated); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4795 | D2Templated->setDescribedClassTemplate(D2); |
| 4796 | |
| 4797 | D2->setAccess(D->getAccess()); |
| 4798 | D2->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 4799 | LexicalDC->addDeclInternal(D2); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4800 | |
| 4801 | // Note the relationship between the class templates. |
| 4802 | Importer.Imported(D, D2); |
| 4803 | Importer.Imported(DTemplated, D2Templated); |
| 4804 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 4805 | if (DTemplated->isCompleteDefinition() && |
| 4806 | !D2Templated->isCompleteDefinition()) { |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4807 | // FIXME: Import definition! |
| 4808 | } |
| 4809 | |
| 4810 | return D2; |
| 4811 | } |
| 4812 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4813 | Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl( |
| 4814 | ClassTemplateSpecializationDecl *D) { |
| 4815 | // If this record has a definition in the translation unit we're coming from, |
| 4816 | // but this particular declaration is not that definition, import the |
| 4817 | // definition and map to that. |
| 4818 | TagDecl *Definition = D->getDefinition(); |
| 4819 | if (Definition && Definition != D) { |
| 4820 | Decl *ImportedDef = Importer.Import(Definition); |
| 4821 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4822 | return nullptr; |
| 4823 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4824 | return Importer.Imported(D, ImportedDef); |
| 4825 | } |
| 4826 | |
| 4827 | ClassTemplateDecl *ClassTemplate |
| 4828 | = cast_or_null<ClassTemplateDecl>(Importer.Import( |
| 4829 | D->getSpecializedTemplate())); |
| 4830 | if (!ClassTemplate) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4831 | return nullptr; |
| 4832 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4833 | // Import the context of this declaration. |
| 4834 | DeclContext *DC = ClassTemplate->getDeclContext(); |
| 4835 | if (!DC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4836 | return nullptr; |
| 4837 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4838 | DeclContext *LexicalDC = DC; |
| 4839 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 4840 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 4841 | if (!LexicalDC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4842 | return nullptr; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4843 | } |
| 4844 | |
| 4845 | // Import the location of this declaration. |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4846 | SourceLocation StartLoc = Importer.Import(D->getLocStart()); |
| 4847 | SourceLocation IdLoc = Importer.Import(D->getLocation()); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4848 | |
| 4849 | // Import template arguments. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4850 | SmallVector<TemplateArgument, 2> TemplateArgs; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4851 | if (ImportTemplateArguments(D->getTemplateArgs().data(), |
| 4852 | D->getTemplateArgs().size(), |
| 4853 | TemplateArgs)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4854 | return nullptr; |
| 4855 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4856 | // Try to find an existing specialization with these template arguments. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4857 | void *InsertPos = nullptr; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4858 | ClassTemplateSpecializationDecl *D2 |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 4859 | = ClassTemplate->findSpecialization(TemplateArgs, InsertPos); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4860 | if (D2) { |
| 4861 | // We already have a class template specialization with these template |
| 4862 | // arguments. |
| 4863 | |
| 4864 | // FIXME: Check for specialization vs. instantiation errors. |
| 4865 | |
| 4866 | if (RecordDecl *FoundDef = D2->getDefinition()) { |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 4867 | if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) { |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4868 | // The record types structurally match, or the "from" translation |
| 4869 | // unit only had a forward declaration anyway; call it the same |
| 4870 | // function. |
| 4871 | return Importer.Imported(D, FoundDef); |
| 4872 | } |
| 4873 | } |
| 4874 | } else { |
| 4875 | // Create a new specialization. |
Aleksei Sidorin | 855086d | 2017-01-23 09:30:36 +0000 | [diff] [blame] | 4876 | if (ClassTemplatePartialSpecializationDecl *PartialSpec = |
| 4877 | dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) { |
| 4878 | |
| 4879 | // Import TemplateArgumentListInfo |
| 4880 | TemplateArgumentListInfo ToTAInfo; |
| 4881 | auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten(); |
| 4882 | for (unsigned I = 0, E = ASTTemplateArgs.NumTemplateArgs; I < E; ++I) { |
| 4883 | bool Error = false; |
| 4884 | auto ToLoc = ImportTemplateArgumentLoc(ASTTemplateArgs[I], Error); |
| 4885 | if (Error) |
| 4886 | return nullptr; |
| 4887 | ToTAInfo.addArgument(ToLoc); |
| 4888 | } |
| 4889 | |
| 4890 | QualType CanonInjType = Importer.Import( |
| 4891 | PartialSpec->getInjectedSpecializationType()); |
| 4892 | if (CanonInjType.isNull()) |
| 4893 | return nullptr; |
| 4894 | CanonInjType = CanonInjType.getCanonicalType(); |
| 4895 | |
| 4896 | TemplateParameterList *ToTPList = ImportTemplateParameterList( |
| 4897 | PartialSpec->getTemplateParameters()); |
| 4898 | if (!ToTPList && PartialSpec->getTemplateParameters()) |
| 4899 | return nullptr; |
| 4900 | |
| 4901 | D2 = ClassTemplatePartialSpecializationDecl::Create( |
| 4902 | Importer.getToContext(), D->getTagKind(), DC, StartLoc, IdLoc, |
| 4903 | ToTPList, ClassTemplate, |
| 4904 | llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()), |
| 4905 | ToTAInfo, CanonInjType, nullptr); |
| 4906 | |
| 4907 | } else { |
| 4908 | D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(), |
| 4909 | D->getTagKind(), DC, |
| 4910 | StartLoc, IdLoc, |
| 4911 | ClassTemplate, |
| 4912 | TemplateArgs, |
| 4913 | /*PrevDecl=*/nullptr); |
| 4914 | } |
| 4915 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4916 | D2->setSpecializationKind(D->getSpecializationKind()); |
| 4917 | |
| 4918 | // Add this specialization to the class template. |
| 4919 | ClassTemplate->AddSpecialization(D2, InsertPos); |
| 4920 | |
| 4921 | // Import the qualifier, if any. |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4922 | D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Aleksei Sidorin | 855086d | 2017-01-23 09:30:36 +0000 | [diff] [blame] | 4923 | |
| 4924 | Importer.Imported(D, D2); |
| 4925 | |
| 4926 | if (auto *TSI = D->getTypeAsWritten()) { |
| 4927 | TypeSourceInfo *TInfo = Importer.Import(TSI); |
| 4928 | if (!TInfo) |
| 4929 | return nullptr; |
| 4930 | D2->setTypeAsWritten(TInfo); |
| 4931 | D2->setTemplateKeywordLoc(Importer.Import(D->getTemplateKeywordLoc())); |
| 4932 | D2->setExternLoc(Importer.Import(D->getExternLoc())); |
| 4933 | } |
| 4934 | |
| 4935 | SourceLocation POI = Importer.Import(D->getPointOfInstantiation()); |
| 4936 | if (POI.isValid()) |
| 4937 | D2->setPointOfInstantiation(POI); |
| 4938 | else if (D->getPointOfInstantiation().isValid()) |
| 4939 | return nullptr; |
| 4940 | |
| 4941 | D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind()); |
| 4942 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4943 | // Add the specialization to this context. |
| 4944 | D2->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 4945 | LexicalDC->addDeclInternal(D2); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4946 | } |
| 4947 | Importer.Imported(D, D2); |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 4948 | if (D->isCompleteDefinition() && ImportDefinition(D, D2)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4949 | return nullptr; |
| 4950 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4951 | return D2; |
| 4952 | } |
| 4953 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4954 | Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) { |
| 4955 | // If this variable has a definition in the translation unit we're coming |
| 4956 | // from, |
| 4957 | // but this particular declaration is not that definition, import the |
| 4958 | // definition and map to that. |
| 4959 | VarDecl *Definition = |
| 4960 | cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition()); |
| 4961 | if (Definition && Definition != D->getTemplatedDecl()) { |
| 4962 | Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate()); |
| 4963 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4964 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4965 | |
| 4966 | return Importer.Imported(D, ImportedDef); |
| 4967 | } |
| 4968 | |
| 4969 | // Import the major distinguishing characteristics of this variable template. |
| 4970 | DeclContext *DC, *LexicalDC; |
| 4971 | DeclarationName Name; |
| 4972 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4973 | NamedDecl *ToD; |
| 4974 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4975 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4976 | if (ToD) |
| 4977 | return ToD; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4978 | |
| 4979 | // We may already have a template of the same name; try to find and match it. |
| 4980 | assert(!DC->isFunctionOrMethod() && |
| 4981 | "Variable templates cannot be declared at function scope"); |
| 4982 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 4983 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 4984 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4985 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 4986 | if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
| 4987 | continue; |
| 4988 | |
| 4989 | Decl *Found = FoundDecls[I]; |
| 4990 | if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) { |
| 4991 | if (IsStructuralMatch(D, FoundTemplate)) { |
| 4992 | // The variable templates structurally match; call it the same template. |
| 4993 | Importer.Imported(D->getTemplatedDecl(), |
| 4994 | FoundTemplate->getTemplatedDecl()); |
| 4995 | return Importer.Imported(D, FoundTemplate); |
| 4996 | } |
| 4997 | } |
| 4998 | |
| 4999 | ConflictingDecls.push_back(FoundDecls[I]); |
| 5000 | } |
| 5001 | |
| 5002 | if (!ConflictingDecls.empty()) { |
| 5003 | Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary, |
| 5004 | ConflictingDecls.data(), |
| 5005 | ConflictingDecls.size()); |
| 5006 | } |
| 5007 | |
| 5008 | if (!Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5009 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5010 | |
| 5011 | VarDecl *DTemplated = D->getTemplatedDecl(); |
| 5012 | |
| 5013 | // Import the type. |
| 5014 | QualType T = Importer.Import(DTemplated->getType()); |
| 5015 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5016 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5017 | |
| 5018 | // Create the declaration that is being templated. |
| 5019 | SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart()); |
| 5020 | SourceLocation IdLoc = Importer.Import(DTemplated->getLocation()); |
| 5021 | TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo()); |
| 5022 | VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc, |
| 5023 | IdLoc, Name.getAsIdentifierInfo(), T, |
| 5024 | TInfo, DTemplated->getStorageClass()); |
| 5025 | D2Templated->setAccess(DTemplated->getAccess()); |
| 5026 | D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc())); |
| 5027 | D2Templated->setLexicalDeclContext(LexicalDC); |
| 5028 | |
| 5029 | // Importer.Imported(DTemplated, D2Templated); |
| 5030 | // LexicalDC->addDeclInternal(D2Templated); |
| 5031 | |
| 5032 | // Merge the initializer. |
| 5033 | if (ImportDefinition(DTemplated, D2Templated)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5034 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5035 | |
| 5036 | // Create the variable template declaration itself. |
| 5037 | TemplateParameterList *TemplateParams = |
| 5038 | ImportTemplateParameterList(D->getTemplateParameters()); |
| 5039 | if (!TemplateParams) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5040 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5041 | |
| 5042 | VarTemplateDecl *D2 = VarTemplateDecl::Create( |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 5043 | Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5044 | D2Templated->setDescribedVarTemplate(D2); |
| 5045 | |
| 5046 | D2->setAccess(D->getAccess()); |
| 5047 | D2->setLexicalDeclContext(LexicalDC); |
| 5048 | LexicalDC->addDeclInternal(D2); |
| 5049 | |
| 5050 | // Note the relationship between the variable templates. |
| 5051 | Importer.Imported(D, D2); |
| 5052 | Importer.Imported(DTemplated, D2Templated); |
| 5053 | |
| 5054 | if (DTemplated->isThisDeclarationADefinition() && |
| 5055 | !D2Templated->isThisDeclarationADefinition()) { |
| 5056 | // FIXME: Import definition! |
| 5057 | } |
| 5058 | |
| 5059 | return D2; |
| 5060 | } |
| 5061 | |
| 5062 | Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl( |
| 5063 | VarTemplateSpecializationDecl *D) { |
| 5064 | // If this record has a definition in the translation unit we're coming from, |
| 5065 | // but this particular declaration is not that definition, import the |
| 5066 | // definition and map to that. |
| 5067 | VarDecl *Definition = D->getDefinition(); |
| 5068 | if (Definition && Definition != D) { |
| 5069 | Decl *ImportedDef = Importer.Import(Definition); |
| 5070 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5071 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5072 | |
| 5073 | return Importer.Imported(D, ImportedDef); |
| 5074 | } |
| 5075 | |
| 5076 | VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>( |
| 5077 | Importer.Import(D->getSpecializedTemplate())); |
| 5078 | if (!VarTemplate) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5079 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5080 | |
| 5081 | // Import the context of this declaration. |
| 5082 | DeclContext *DC = VarTemplate->getDeclContext(); |
| 5083 | if (!DC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5084 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5085 | |
| 5086 | DeclContext *LexicalDC = DC; |
| 5087 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 5088 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 5089 | if (!LexicalDC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5090 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5091 | } |
| 5092 | |
| 5093 | // Import the location of this declaration. |
| 5094 | SourceLocation StartLoc = Importer.Import(D->getLocStart()); |
| 5095 | SourceLocation IdLoc = Importer.Import(D->getLocation()); |
| 5096 | |
| 5097 | // Import template arguments. |
| 5098 | SmallVector<TemplateArgument, 2> TemplateArgs; |
| 5099 | if (ImportTemplateArguments(D->getTemplateArgs().data(), |
| 5100 | D->getTemplateArgs().size(), TemplateArgs)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5101 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5102 | |
| 5103 | // Try to find an existing specialization with these template arguments. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5104 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5105 | VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization( |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 5106 | TemplateArgs, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5107 | if (D2) { |
| 5108 | // We already have a variable template specialization with these template |
| 5109 | // arguments. |
| 5110 | |
| 5111 | // FIXME: Check for specialization vs. instantiation errors. |
| 5112 | |
| 5113 | if (VarDecl *FoundDef = D2->getDefinition()) { |
| 5114 | if (!D->isThisDeclarationADefinition() || |
| 5115 | IsStructuralMatch(D, FoundDef)) { |
| 5116 | // The record types structurally match, or the "from" translation |
| 5117 | // unit only had a forward declaration anyway; call it the same |
| 5118 | // variable. |
| 5119 | return Importer.Imported(D, FoundDef); |
| 5120 | } |
| 5121 | } |
| 5122 | } else { |
| 5123 | |
| 5124 | // Import the type. |
| 5125 | QualType T = Importer.Import(D->getType()); |
| 5126 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5127 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5128 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 5129 | |
| 5130 | // Create a new specialization. |
| 5131 | D2 = VarTemplateSpecializationDecl::Create( |
| 5132 | Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 5133 | D->getStorageClass(), TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5134 | D2->setSpecializationKind(D->getSpecializationKind()); |
| 5135 | D2->setTemplateArgsInfo(D->getTemplateArgsInfo()); |
| 5136 | |
| 5137 | // Add this specialization to the class template. |
| 5138 | VarTemplate->AddSpecialization(D2, InsertPos); |
| 5139 | |
| 5140 | // Import the qualifier, if any. |
| 5141 | D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
| 5142 | |
| 5143 | // Add the specialization to this context. |
| 5144 | D2->setLexicalDeclContext(LexicalDC); |
| 5145 | LexicalDC->addDeclInternal(D2); |
| 5146 | } |
| 5147 | Importer.Imported(D, D2); |
| 5148 | |
| 5149 | if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5150 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5151 | |
| 5152 | return D2; |
| 5153 | } |
| 5154 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 5155 | //---------------------------------------------------------------------------- |
| 5156 | // Import Statements |
| 5157 | //---------------------------------------------------------------------------- |
| 5158 | |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 5159 | DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) { |
| 5160 | if (DG.isNull()) |
| 5161 | return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0); |
| 5162 | size_t NumDecls = DG.end() - DG.begin(); |
| 5163 | SmallVector<Decl *, 1> ToDecls(NumDecls); |
| 5164 | auto &_Importer = this->Importer; |
| 5165 | std::transform(DG.begin(), DG.end(), ToDecls.begin(), |
| 5166 | [&_Importer](Decl *D) -> Decl * { |
| 5167 | return _Importer.Import(D); |
| 5168 | }); |
| 5169 | return DeclGroupRef::Create(Importer.getToContext(), |
| 5170 | ToDecls.begin(), |
| 5171 | NumDecls); |
| 5172 | } |
| 5173 | |
| 5174 | Stmt *ASTNodeImporter::VisitStmt(Stmt *S) { |
| 5175 | Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node) |
| 5176 | << S->getStmtClassName(); |
| 5177 | return nullptr; |
| 5178 | } |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5179 | |
| 5180 | |
| 5181 | Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) { |
| 5182 | SmallVector<IdentifierInfo *, 4> Names; |
| 5183 | for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) { |
| 5184 | IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I)); |
| 5185 | if (!ToII) |
| 5186 | return nullptr; |
| 5187 | Names.push_back(ToII); |
| 5188 | } |
| 5189 | for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) { |
| 5190 | IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I)); |
| 5191 | if (!ToII) |
| 5192 | return nullptr; |
| 5193 | Names.push_back(ToII); |
| 5194 | } |
| 5195 | |
| 5196 | SmallVector<StringLiteral *, 4> Clobbers; |
| 5197 | for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) { |
| 5198 | StringLiteral *Clobber = cast_or_null<StringLiteral>( |
| 5199 | Importer.Import(S->getClobberStringLiteral(I))); |
| 5200 | if (!Clobber) |
| 5201 | return nullptr; |
| 5202 | Clobbers.push_back(Clobber); |
| 5203 | } |
| 5204 | |
| 5205 | SmallVector<StringLiteral *, 4> Constraints; |
| 5206 | for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) { |
| 5207 | StringLiteral *Output = cast_or_null<StringLiteral>( |
| 5208 | Importer.Import(S->getOutputConstraintLiteral(I))); |
| 5209 | if (!Output) |
| 5210 | return nullptr; |
| 5211 | Constraints.push_back(Output); |
| 5212 | } |
| 5213 | |
| 5214 | for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) { |
| 5215 | StringLiteral *Input = cast_or_null<StringLiteral>( |
| 5216 | Importer.Import(S->getInputConstraintLiteral(I))); |
| 5217 | if (!Input) |
| 5218 | return nullptr; |
| 5219 | Constraints.push_back(Input); |
| 5220 | } |
| 5221 | |
| 5222 | SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs()); |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 5223 | if (ImportContainerChecked(S->outputs(), Exprs)) |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5224 | return nullptr; |
| 5225 | |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 5226 | if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs())) |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5227 | return nullptr; |
| 5228 | |
| 5229 | StringLiteral *AsmStr = cast_or_null<StringLiteral>( |
| 5230 | Importer.Import(S->getAsmString())); |
| 5231 | if (!AsmStr) |
| 5232 | return nullptr; |
| 5233 | |
| 5234 | return new (Importer.getToContext()) GCCAsmStmt( |
| 5235 | Importer.getToContext(), |
| 5236 | Importer.Import(S->getAsmLoc()), |
| 5237 | S->isSimple(), |
| 5238 | S->isVolatile(), |
| 5239 | S->getNumOutputs(), |
| 5240 | S->getNumInputs(), |
| 5241 | Names.data(), |
| 5242 | Constraints.data(), |
| 5243 | Exprs.data(), |
| 5244 | AsmStr, |
| 5245 | S->getNumClobbers(), |
| 5246 | Clobbers.data(), |
| 5247 | Importer.Import(S->getRParenLoc())); |
| 5248 | } |
| 5249 | |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 5250 | Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) { |
| 5251 | DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup()); |
| 5252 | for (Decl *ToD : ToDG) { |
| 5253 | if (!ToD) |
| 5254 | return nullptr; |
| 5255 | } |
| 5256 | SourceLocation ToStartLoc = Importer.Import(S->getStartLoc()); |
| 5257 | SourceLocation ToEndLoc = Importer.Import(S->getEndLoc()); |
| 5258 | return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc); |
| 5259 | } |
| 5260 | |
| 5261 | Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) { |
| 5262 | SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc()); |
| 5263 | return new (Importer.getToContext()) NullStmt(ToSemiLoc, |
| 5264 | S->hasLeadingEmptyMacro()); |
| 5265 | } |
| 5266 | |
| 5267 | Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) { |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5268 | llvm::SmallVector<Stmt *, 8> ToStmts(S->size()); |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 5269 | |
| 5270 | if (ImportContainerChecked(S->body(), ToStmts)) |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 5271 | return nullptr; |
| 5272 | |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 5273 | SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc()); |
| 5274 | SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc()); |
| 5275 | return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(), |
| 5276 | ToStmts, |
| 5277 | ToLBraceLoc, ToRBraceLoc); |
| 5278 | } |
| 5279 | |
| 5280 | Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) { |
| 5281 | Expr *ToLHS = Importer.Import(S->getLHS()); |
| 5282 | if (!ToLHS) |
| 5283 | return nullptr; |
| 5284 | Expr *ToRHS = Importer.Import(S->getRHS()); |
| 5285 | if (!ToRHS && S->getRHS()) |
| 5286 | return nullptr; |
| 5287 | SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc()); |
| 5288 | SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc()); |
| 5289 | SourceLocation ToColonLoc = Importer.Import(S->getColonLoc()); |
| 5290 | return new (Importer.getToContext()) CaseStmt(ToLHS, ToRHS, |
| 5291 | ToCaseLoc, ToEllipsisLoc, |
| 5292 | ToColonLoc); |
| 5293 | } |
| 5294 | |
| 5295 | Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) { |
| 5296 | SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc()); |
| 5297 | SourceLocation ToColonLoc = Importer.Import(S->getColonLoc()); |
| 5298 | Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); |
| 5299 | if (!ToSubStmt && S->getSubStmt()) |
| 5300 | return nullptr; |
| 5301 | return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc, |
| 5302 | ToSubStmt); |
| 5303 | } |
| 5304 | |
| 5305 | Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) { |
| 5306 | SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc()); |
| 5307 | LabelDecl *ToLabelDecl = |
| 5308 | cast_or_null<LabelDecl>(Importer.Import(S->getDecl())); |
| 5309 | if (!ToLabelDecl && S->getDecl()) |
| 5310 | return nullptr; |
| 5311 | Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); |
| 5312 | if (!ToSubStmt && S->getSubStmt()) |
| 5313 | return nullptr; |
| 5314 | return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl, |
| 5315 | ToSubStmt); |
| 5316 | } |
| 5317 | |
| 5318 | Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) { |
| 5319 | SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc()); |
| 5320 | ArrayRef<const Attr*> FromAttrs(S->getAttrs()); |
| 5321 | SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size()); |
| 5322 | ASTContext &_ToContext = Importer.getToContext(); |
| 5323 | std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(), |
| 5324 | [&_ToContext](const Attr *A) -> const Attr * { |
| 5325 | return A->clone(_ToContext); |
| 5326 | }); |
| 5327 | for (const Attr *ToA : ToAttrs) { |
| 5328 | if (!ToA) |
| 5329 | return nullptr; |
| 5330 | } |
| 5331 | Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); |
| 5332 | if (!ToSubStmt && S->getSubStmt()) |
| 5333 | return nullptr; |
| 5334 | return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc, |
| 5335 | ToAttrs, ToSubStmt); |
| 5336 | } |
| 5337 | |
| 5338 | Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) { |
| 5339 | SourceLocation ToIfLoc = Importer.Import(S->getIfLoc()); |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 5340 | Stmt *ToInit = Importer.Import(S->getInit()); |
| 5341 | if (!ToInit && S->getInit()) |
| 5342 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 5343 | VarDecl *ToConditionVariable = nullptr; |
| 5344 | if (VarDecl *FromConditionVariable = S->getConditionVariable()) { |
| 5345 | ToConditionVariable = |
| 5346 | dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); |
| 5347 | if (!ToConditionVariable) |
| 5348 | return nullptr; |
| 5349 | } |
| 5350 | Expr *ToCondition = Importer.Import(S->getCond()); |
| 5351 | if (!ToCondition && S->getCond()) |
| 5352 | return nullptr; |
| 5353 | Stmt *ToThenStmt = Importer.Import(S->getThen()); |
| 5354 | if (!ToThenStmt && S->getThen()) |
| 5355 | return nullptr; |
| 5356 | SourceLocation ToElseLoc = Importer.Import(S->getElseLoc()); |
| 5357 | Stmt *ToElseStmt = Importer.Import(S->getElse()); |
| 5358 | if (!ToElseStmt && S->getElse()) |
| 5359 | return nullptr; |
| 5360 | return new (Importer.getToContext()) IfStmt(Importer.getToContext(), |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 5361 | ToIfLoc, S->isConstexpr(), |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 5362 | ToInit, |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 5363 | ToConditionVariable, |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 5364 | ToCondition, ToThenStmt, |
| 5365 | ToElseLoc, ToElseStmt); |
| 5366 | } |
| 5367 | |
| 5368 | Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) { |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 5369 | Stmt *ToInit = Importer.Import(S->getInit()); |
| 5370 | if (!ToInit && S->getInit()) |
| 5371 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 5372 | VarDecl *ToConditionVariable = nullptr; |
| 5373 | if (VarDecl *FromConditionVariable = S->getConditionVariable()) { |
| 5374 | ToConditionVariable = |
| 5375 | dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); |
| 5376 | if (!ToConditionVariable) |
| 5377 | return nullptr; |
| 5378 | } |
| 5379 | Expr *ToCondition = Importer.Import(S->getCond()); |
| 5380 | if (!ToCondition && S->getCond()) |
| 5381 | return nullptr; |
| 5382 | SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt( |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 5383 | Importer.getToContext(), ToInit, |
| 5384 | ToConditionVariable, ToCondition); |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 5385 | Stmt *ToBody = Importer.Import(S->getBody()); |
| 5386 | if (!ToBody && S->getBody()) |
| 5387 | return nullptr; |
| 5388 | ToStmt->setBody(ToBody); |
| 5389 | ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc())); |
| 5390 | // Now we have to re-chain the cases. |
| 5391 | SwitchCase *LastChainedSwitchCase = nullptr; |
| 5392 | for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr; |
| 5393 | SC = SC->getNextSwitchCase()) { |
| 5394 | SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC)); |
| 5395 | if (!ToSC) |
| 5396 | return nullptr; |
| 5397 | if (LastChainedSwitchCase) |
| 5398 | LastChainedSwitchCase->setNextSwitchCase(ToSC); |
| 5399 | else |
| 5400 | ToStmt->setSwitchCaseList(ToSC); |
| 5401 | LastChainedSwitchCase = ToSC; |
| 5402 | } |
| 5403 | return ToStmt; |
| 5404 | } |
| 5405 | |
| 5406 | Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) { |
| 5407 | VarDecl *ToConditionVariable = nullptr; |
| 5408 | if (VarDecl *FromConditionVariable = S->getConditionVariable()) { |
| 5409 | ToConditionVariable = |
| 5410 | dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); |
| 5411 | if (!ToConditionVariable) |
| 5412 | return nullptr; |
| 5413 | } |
| 5414 | Expr *ToCondition = Importer.Import(S->getCond()); |
| 5415 | if (!ToCondition && S->getCond()) |
| 5416 | return nullptr; |
| 5417 | Stmt *ToBody = Importer.Import(S->getBody()); |
| 5418 | if (!ToBody && S->getBody()) |
| 5419 | return nullptr; |
| 5420 | SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc()); |
| 5421 | return new (Importer.getToContext()) WhileStmt(Importer.getToContext(), |
| 5422 | ToConditionVariable, |
| 5423 | ToCondition, ToBody, |
| 5424 | ToWhileLoc); |
| 5425 | } |
| 5426 | |
| 5427 | Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) { |
| 5428 | Stmt *ToBody = Importer.Import(S->getBody()); |
| 5429 | if (!ToBody && S->getBody()) |
| 5430 | return nullptr; |
| 5431 | Expr *ToCondition = Importer.Import(S->getCond()); |
| 5432 | if (!ToCondition && S->getCond()) |
| 5433 | return nullptr; |
| 5434 | SourceLocation ToDoLoc = Importer.Import(S->getDoLoc()); |
| 5435 | SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc()); |
| 5436 | SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); |
| 5437 | return new (Importer.getToContext()) DoStmt(ToBody, ToCondition, |
| 5438 | ToDoLoc, ToWhileLoc, |
| 5439 | ToRParenLoc); |
| 5440 | } |
| 5441 | |
| 5442 | Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) { |
| 5443 | Stmt *ToInit = Importer.Import(S->getInit()); |
| 5444 | if (!ToInit && S->getInit()) |
| 5445 | return nullptr; |
| 5446 | Expr *ToCondition = Importer.Import(S->getCond()); |
| 5447 | if (!ToCondition && S->getCond()) |
| 5448 | return nullptr; |
| 5449 | VarDecl *ToConditionVariable = nullptr; |
| 5450 | if (VarDecl *FromConditionVariable = S->getConditionVariable()) { |
| 5451 | ToConditionVariable = |
| 5452 | dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); |
| 5453 | if (!ToConditionVariable) |
| 5454 | return nullptr; |
| 5455 | } |
| 5456 | Expr *ToInc = Importer.Import(S->getInc()); |
| 5457 | if (!ToInc && S->getInc()) |
| 5458 | return nullptr; |
| 5459 | Stmt *ToBody = Importer.Import(S->getBody()); |
| 5460 | if (!ToBody && S->getBody()) |
| 5461 | return nullptr; |
| 5462 | SourceLocation ToForLoc = Importer.Import(S->getForLoc()); |
| 5463 | SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc()); |
| 5464 | SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); |
| 5465 | return new (Importer.getToContext()) ForStmt(Importer.getToContext(), |
| 5466 | ToInit, ToCondition, |
| 5467 | ToConditionVariable, |
| 5468 | ToInc, ToBody, |
| 5469 | ToForLoc, ToLParenLoc, |
| 5470 | ToRParenLoc); |
| 5471 | } |
| 5472 | |
| 5473 | Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) { |
| 5474 | LabelDecl *ToLabel = nullptr; |
| 5475 | if (LabelDecl *FromLabel = S->getLabel()) { |
| 5476 | ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel)); |
| 5477 | if (!ToLabel) |
| 5478 | return nullptr; |
| 5479 | } |
| 5480 | SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc()); |
| 5481 | SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc()); |
| 5482 | return new (Importer.getToContext()) GotoStmt(ToLabel, |
| 5483 | ToGotoLoc, ToLabelLoc); |
| 5484 | } |
| 5485 | |
| 5486 | Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) { |
| 5487 | SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc()); |
| 5488 | SourceLocation ToStarLoc = Importer.Import(S->getStarLoc()); |
| 5489 | Expr *ToTarget = Importer.Import(S->getTarget()); |
| 5490 | if (!ToTarget && S->getTarget()) |
| 5491 | return nullptr; |
| 5492 | return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc, |
| 5493 | ToTarget); |
| 5494 | } |
| 5495 | |
| 5496 | Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) { |
| 5497 | SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc()); |
| 5498 | return new (Importer.getToContext()) ContinueStmt(ToContinueLoc); |
| 5499 | } |
| 5500 | |
| 5501 | Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) { |
| 5502 | SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc()); |
| 5503 | return new (Importer.getToContext()) BreakStmt(ToBreakLoc); |
| 5504 | } |
| 5505 | |
| 5506 | Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) { |
| 5507 | SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc()); |
| 5508 | Expr *ToRetExpr = Importer.Import(S->getRetValue()); |
| 5509 | if (!ToRetExpr && S->getRetValue()) |
| 5510 | return nullptr; |
| 5511 | VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate()); |
| 5512 | VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate)); |
| 5513 | if (!ToNRVOCandidate && NRVOCandidate) |
| 5514 | return nullptr; |
| 5515 | return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr, |
| 5516 | ToNRVOCandidate); |
| 5517 | } |
| 5518 | |
| 5519 | Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) { |
| 5520 | SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc()); |
| 5521 | VarDecl *ToExceptionDecl = nullptr; |
| 5522 | if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) { |
| 5523 | ToExceptionDecl = |
| 5524 | dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl)); |
| 5525 | if (!ToExceptionDecl) |
| 5526 | return nullptr; |
| 5527 | } |
| 5528 | Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock()); |
| 5529 | if (!ToHandlerBlock && S->getHandlerBlock()) |
| 5530 | return nullptr; |
| 5531 | return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc, |
| 5532 | ToExceptionDecl, |
| 5533 | ToHandlerBlock); |
| 5534 | } |
| 5535 | |
| 5536 | Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) { |
| 5537 | SourceLocation ToTryLoc = Importer.Import(S->getTryLoc()); |
| 5538 | Stmt *ToTryBlock = Importer.Import(S->getTryBlock()); |
| 5539 | if (!ToTryBlock && S->getTryBlock()) |
| 5540 | return nullptr; |
| 5541 | SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers()); |
| 5542 | for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) { |
| 5543 | CXXCatchStmt *FromHandler = S->getHandler(HI); |
| 5544 | if (Stmt *ToHandler = Importer.Import(FromHandler)) |
| 5545 | ToHandlers[HI] = ToHandler; |
| 5546 | else |
| 5547 | return nullptr; |
| 5548 | } |
| 5549 | return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock, |
| 5550 | ToHandlers); |
| 5551 | } |
| 5552 | |
| 5553 | Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) { |
| 5554 | DeclStmt *ToRange = |
| 5555 | dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt())); |
| 5556 | if (!ToRange && S->getRangeStmt()) |
| 5557 | return nullptr; |
Richard Smith | 01694c3 | 2016-03-20 10:33:40 +0000 | [diff] [blame] | 5558 | DeclStmt *ToBegin = |
| 5559 | dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt())); |
| 5560 | if (!ToBegin && S->getBeginStmt()) |
| 5561 | return nullptr; |
| 5562 | DeclStmt *ToEnd = |
| 5563 | dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt())); |
| 5564 | if (!ToEnd && S->getEndStmt()) |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 5565 | return nullptr; |
| 5566 | Expr *ToCond = Importer.Import(S->getCond()); |
| 5567 | if (!ToCond && S->getCond()) |
| 5568 | return nullptr; |
| 5569 | Expr *ToInc = Importer.Import(S->getInc()); |
| 5570 | if (!ToInc && S->getInc()) |
| 5571 | return nullptr; |
| 5572 | DeclStmt *ToLoopVar = |
| 5573 | dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt())); |
| 5574 | if (!ToLoopVar && S->getLoopVarStmt()) |
| 5575 | return nullptr; |
| 5576 | Stmt *ToBody = Importer.Import(S->getBody()); |
| 5577 | if (!ToBody && S->getBody()) |
| 5578 | return nullptr; |
| 5579 | SourceLocation ToForLoc = Importer.Import(S->getForLoc()); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 5580 | SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc()); |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 5581 | SourceLocation ToColonLoc = Importer.Import(S->getColonLoc()); |
| 5582 | SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); |
Richard Smith | 01694c3 | 2016-03-20 10:33:40 +0000 | [diff] [blame] | 5583 | return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd, |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 5584 | ToCond, ToInc, |
| 5585 | ToLoopVar, ToBody, |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 5586 | ToForLoc, ToCoawaitLoc, |
| 5587 | ToColonLoc, ToRParenLoc); |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 5588 | } |
| 5589 | |
| 5590 | Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) { |
| 5591 | Stmt *ToElem = Importer.Import(S->getElement()); |
| 5592 | if (!ToElem && S->getElement()) |
| 5593 | return nullptr; |
| 5594 | Expr *ToCollect = Importer.Import(S->getCollection()); |
| 5595 | if (!ToCollect && S->getCollection()) |
| 5596 | return nullptr; |
| 5597 | Stmt *ToBody = Importer.Import(S->getBody()); |
| 5598 | if (!ToBody && S->getBody()) |
| 5599 | return nullptr; |
| 5600 | SourceLocation ToForLoc = Importer.Import(S->getForLoc()); |
| 5601 | SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); |
| 5602 | return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem, |
| 5603 | ToCollect, |
| 5604 | ToBody, ToForLoc, |
| 5605 | ToRParenLoc); |
| 5606 | } |
| 5607 | |
| 5608 | Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) { |
| 5609 | SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc()); |
| 5610 | SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); |
| 5611 | VarDecl *ToExceptionDecl = nullptr; |
| 5612 | if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) { |
| 5613 | ToExceptionDecl = |
| 5614 | dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl)); |
| 5615 | if (!ToExceptionDecl) |
| 5616 | return nullptr; |
| 5617 | } |
| 5618 | Stmt *ToBody = Importer.Import(S->getCatchBody()); |
| 5619 | if (!ToBody && S->getCatchBody()) |
| 5620 | return nullptr; |
| 5621 | return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc, |
| 5622 | ToRParenLoc, |
| 5623 | ToExceptionDecl, |
| 5624 | ToBody); |
| 5625 | } |
| 5626 | |
| 5627 | Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) { |
| 5628 | SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc()); |
| 5629 | Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody()); |
| 5630 | if (!ToAtFinallyStmt && S->getFinallyBody()) |
| 5631 | return nullptr; |
| 5632 | return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc, |
| 5633 | ToAtFinallyStmt); |
| 5634 | } |
| 5635 | |
| 5636 | Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) { |
| 5637 | SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc()); |
| 5638 | Stmt *ToAtTryStmt = Importer.Import(S->getTryBody()); |
| 5639 | if (!ToAtTryStmt && S->getTryBody()) |
| 5640 | return nullptr; |
| 5641 | SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts()); |
| 5642 | for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) { |
| 5643 | ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI); |
| 5644 | if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt)) |
| 5645 | ToCatchStmts[CI] = ToCatchStmt; |
| 5646 | else |
| 5647 | return nullptr; |
| 5648 | } |
| 5649 | Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt()); |
| 5650 | if (!ToAtFinallyStmt && S->getFinallyStmt()) |
| 5651 | return nullptr; |
| 5652 | return ObjCAtTryStmt::Create(Importer.getToContext(), |
| 5653 | ToAtTryLoc, ToAtTryStmt, |
| 5654 | ToCatchStmts.begin(), ToCatchStmts.size(), |
| 5655 | ToAtFinallyStmt); |
| 5656 | } |
| 5657 | |
| 5658 | Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt |
| 5659 | (ObjCAtSynchronizedStmt *S) { |
| 5660 | SourceLocation ToAtSynchronizedLoc = |
| 5661 | Importer.Import(S->getAtSynchronizedLoc()); |
| 5662 | Expr *ToSynchExpr = Importer.Import(S->getSynchExpr()); |
| 5663 | if (!ToSynchExpr && S->getSynchExpr()) |
| 5664 | return nullptr; |
| 5665 | Stmt *ToSynchBody = Importer.Import(S->getSynchBody()); |
| 5666 | if (!ToSynchBody && S->getSynchBody()) |
| 5667 | return nullptr; |
| 5668 | return new (Importer.getToContext()) ObjCAtSynchronizedStmt( |
| 5669 | ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody); |
| 5670 | } |
| 5671 | |
| 5672 | Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) { |
| 5673 | SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc()); |
| 5674 | Expr *ToThrow = Importer.Import(S->getThrowExpr()); |
| 5675 | if (!ToThrow && S->getThrowExpr()) |
| 5676 | return nullptr; |
| 5677 | return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow); |
| 5678 | } |
| 5679 | |
| 5680 | Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt |
| 5681 | (ObjCAutoreleasePoolStmt *S) { |
| 5682 | SourceLocation ToAtLoc = Importer.Import(S->getAtLoc()); |
| 5683 | Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); |
| 5684 | if (!ToSubStmt && S->getSubStmt()) |
| 5685 | return nullptr; |
| 5686 | return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc, |
| 5687 | ToSubStmt); |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 5688 | } |
| 5689 | |
| 5690 | //---------------------------------------------------------------------------- |
| 5691 | // Import Expressions |
| 5692 | //---------------------------------------------------------------------------- |
| 5693 | Expr *ASTNodeImporter::VisitExpr(Expr *E) { |
| 5694 | Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node) |
| 5695 | << E->getStmtClassName(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5696 | return nullptr; |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 5697 | } |
| 5698 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5699 | Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) { |
| 5700 | QualType T = Importer.Import(E->getType()); |
| 5701 | if (T.isNull()) |
| 5702 | return nullptr; |
| 5703 | |
| 5704 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 5705 | if (!SubExpr && E->getSubExpr()) |
| 5706 | return nullptr; |
| 5707 | |
| 5708 | TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo()); |
| 5709 | if (!TInfo) |
| 5710 | return nullptr; |
| 5711 | |
| 5712 | return new (Importer.getToContext()) VAArgExpr( |
| 5713 | Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo, |
| 5714 | Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI()); |
| 5715 | } |
| 5716 | |
| 5717 | |
| 5718 | Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) { |
| 5719 | QualType T = Importer.Import(E->getType()); |
| 5720 | if (T.isNull()) |
| 5721 | return nullptr; |
| 5722 | |
| 5723 | return new (Importer.getToContext()) GNUNullExpr( |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 5724 | T, Importer.Import(E->getLocStart())); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5725 | } |
| 5726 | |
| 5727 | Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) { |
| 5728 | QualType T = Importer.Import(E->getType()); |
| 5729 | if (T.isNull()) |
| 5730 | return nullptr; |
| 5731 | |
| 5732 | StringLiteral *SL = cast_or_null<StringLiteral>( |
| 5733 | Importer.Import(E->getFunctionName())); |
| 5734 | if (!SL && E->getFunctionName()) |
| 5735 | return nullptr; |
| 5736 | |
| 5737 | return new (Importer.getToContext()) PredefinedExpr( |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 5738 | Importer.Import(E->getLocStart()), T, E->getIdentType(), SL); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5739 | } |
| 5740 | |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 5741 | Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) { |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 5742 | ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl())); |
| 5743 | if (!ToD) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5744 | return nullptr; |
Chandler Carruth | 8d26bb0 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 5745 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5746 | NamedDecl *FoundD = nullptr; |
Chandler Carruth | 8d26bb0 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 5747 | if (E->getDecl() != E->getFoundDecl()) { |
| 5748 | FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl())); |
| 5749 | if (!FoundD) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5750 | return nullptr; |
Chandler Carruth | 8d26bb0 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 5751 | } |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 5752 | |
| 5753 | QualType T = Importer.Import(E->getType()); |
| 5754 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5755 | return nullptr; |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5756 | |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 5757 | |
| 5758 | TemplateArgumentListInfo ToTAInfo; |
| 5759 | TemplateArgumentListInfo *ResInfo = nullptr; |
| 5760 | if (E->hasExplicitTemplateArgs()) { |
| 5761 | for (const auto &FromLoc : E->template_arguments()) { |
| 5762 | bool Error = false; |
| 5763 | TemplateArgumentLoc ToTALoc = ImportTemplateArgumentLoc(FromLoc, Error); |
| 5764 | if (Error) |
| 5765 | return nullptr; |
| 5766 | ToTAInfo.addArgument(ToTALoc); |
| 5767 | } |
| 5768 | ResInfo = &ToTAInfo; |
| 5769 | } |
| 5770 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5771 | DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(), |
| 5772 | Importer.Import(E->getQualifierLoc()), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 5773 | Importer.Import(E->getTemplateKeywordLoc()), |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5774 | ToD, |
Alexey Bataev | 19acc3d | 2015-01-12 10:17:46 +0000 | [diff] [blame] | 5775 | E->refersToEnclosingVariableOrCapture(), |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5776 | Importer.Import(E->getLocation()), |
| 5777 | T, E->getValueKind(), |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 5778 | FoundD, ResInfo); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5779 | if (E->hadMultipleCandidates()) |
| 5780 | DRE->setHadMultipleCandidates(true); |
| 5781 | return DRE; |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 5782 | } |
| 5783 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5784 | Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { |
| 5785 | QualType T = Importer.Import(E->getType()); |
| 5786 | if (T.isNull()) |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 5787 | return nullptr; |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5788 | |
| 5789 | return new (Importer.getToContext()) ImplicitValueInitExpr(T); |
| 5790 | } |
| 5791 | |
| 5792 | ASTNodeImporter::Designator |
| 5793 | ASTNodeImporter::ImportDesignator(const Designator &D) { |
| 5794 | if (D.isFieldDesignator()) { |
| 5795 | IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName()); |
| 5796 | // Caller checks for import error |
| 5797 | return Designator(ToFieldName, Importer.Import(D.getDotLoc()), |
| 5798 | Importer.Import(D.getFieldLoc())); |
| 5799 | } |
| 5800 | if (D.isArrayDesignator()) |
| 5801 | return Designator(D.getFirstExprIndex(), |
| 5802 | Importer.Import(D.getLBracketLoc()), |
| 5803 | Importer.Import(D.getRBracketLoc())); |
| 5804 | |
| 5805 | assert(D.isArrayRangeDesignator()); |
| 5806 | return Designator(D.getFirstExprIndex(), |
| 5807 | Importer.Import(D.getLBracketLoc()), |
| 5808 | Importer.Import(D.getEllipsisLoc()), |
| 5809 | Importer.Import(D.getRBracketLoc())); |
| 5810 | } |
| 5811 | |
| 5812 | |
| 5813 | Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) { |
| 5814 | Expr *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit())); |
| 5815 | if (!Init) |
| 5816 | return nullptr; |
| 5817 | |
| 5818 | SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1); |
| 5819 | // List elements from the second, the first is Init itself |
| 5820 | for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) { |
| 5821 | if (Expr *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I)))) |
| 5822 | IndexExprs[I - 1] = Arg; |
| 5823 | else |
| 5824 | return nullptr; |
| 5825 | } |
| 5826 | |
| 5827 | SmallVector<Designator, 4> Designators(DIE->size()); |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 5828 | llvm::transform(DIE->designators(), Designators.begin(), |
| 5829 | [this](const Designator &D) -> Designator { |
| 5830 | return ImportDesignator(D); |
| 5831 | }); |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5832 | |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 5833 | for (const Designator &D : DIE->designators()) |
| 5834 | if (D.isFieldDesignator() && !D.getFieldName()) |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5835 | return nullptr; |
| 5836 | |
| 5837 | return DesignatedInitExpr::Create( |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 5838 | Importer.getToContext(), Designators, |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5839 | IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()), |
| 5840 | DIE->usesGNUSyntax(), Init); |
| 5841 | } |
| 5842 | |
| 5843 | Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) { |
| 5844 | QualType T = Importer.Import(E->getType()); |
| 5845 | if (T.isNull()) |
| 5846 | return nullptr; |
| 5847 | |
| 5848 | return new (Importer.getToContext()) |
| 5849 | CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation())); |
| 5850 | } |
| 5851 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 5852 | Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) { |
| 5853 | QualType T = Importer.Import(E->getType()); |
| 5854 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5855 | return nullptr; |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 5856 | |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 5857 | return IntegerLiteral::Create(Importer.getToContext(), |
| 5858 | E->getValue(), T, |
| 5859 | Importer.Import(E->getLocation())); |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 5860 | } |
| 5861 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5862 | Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) { |
| 5863 | QualType T = Importer.Import(E->getType()); |
| 5864 | if (T.isNull()) |
| 5865 | return nullptr; |
| 5866 | |
| 5867 | return FloatingLiteral::Create(Importer.getToContext(), |
| 5868 | E->getValue(), E->isExact(), T, |
| 5869 | Importer.Import(E->getLocation())); |
| 5870 | } |
| 5871 | |
Douglas Gregor | 623421d | 2010-02-18 02:21:22 +0000 | [diff] [blame] | 5872 | Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) { |
| 5873 | QualType T = Importer.Import(E->getType()); |
| 5874 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5875 | return nullptr; |
| 5876 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5877 | return new (Importer.getToContext()) CharacterLiteral(E->getValue(), |
| 5878 | E->getKind(), T, |
Douglas Gregor | 623421d | 2010-02-18 02:21:22 +0000 | [diff] [blame] | 5879 | Importer.Import(E->getLocation())); |
| 5880 | } |
| 5881 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5882 | Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) { |
| 5883 | QualType T = Importer.Import(E->getType()); |
| 5884 | if (T.isNull()) |
| 5885 | return nullptr; |
| 5886 | |
| 5887 | SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated()); |
| 5888 | ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin()); |
| 5889 | |
| 5890 | return StringLiteral::Create(Importer.getToContext(), E->getBytes(), |
| 5891 | E->getKind(), E->isPascal(), T, |
| 5892 | Locations.data(), Locations.size()); |
| 5893 | } |
| 5894 | |
| 5895 | Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 5896 | QualType T = Importer.Import(E->getType()); |
| 5897 | if (T.isNull()) |
| 5898 | return nullptr; |
| 5899 | |
| 5900 | TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo()); |
| 5901 | if (!TInfo) |
| 5902 | return nullptr; |
| 5903 | |
| 5904 | Expr *Init = Importer.Import(E->getInitializer()); |
| 5905 | if (!Init) |
| 5906 | return nullptr; |
| 5907 | |
| 5908 | return new (Importer.getToContext()) CompoundLiteralExpr( |
| 5909 | Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(), |
| 5910 | Init, E->isFileScope()); |
| 5911 | } |
| 5912 | |
| 5913 | Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) { |
| 5914 | QualType T = Importer.Import(E->getType()); |
| 5915 | if (T.isNull()) |
| 5916 | return nullptr; |
| 5917 | |
| 5918 | SmallVector<Expr *, 6> Exprs(E->getNumSubExprs()); |
| 5919 | if (ImportArrayChecked( |
| 5920 | E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(), |
| 5921 | Exprs.begin())) |
| 5922 | return nullptr; |
| 5923 | |
| 5924 | return new (Importer.getToContext()) AtomicExpr( |
| 5925 | Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(), |
| 5926 | Importer.Import(E->getRParenLoc())); |
| 5927 | } |
| 5928 | |
| 5929 | Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) { |
| 5930 | QualType T = Importer.Import(E->getType()); |
| 5931 | if (T.isNull()) |
| 5932 | return nullptr; |
| 5933 | |
| 5934 | LabelDecl *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel())); |
| 5935 | if (!ToLabel) |
| 5936 | return nullptr; |
| 5937 | |
| 5938 | return new (Importer.getToContext()) AddrLabelExpr( |
| 5939 | Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()), |
| 5940 | ToLabel, T); |
| 5941 | } |
| 5942 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5943 | Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) { |
| 5944 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 5945 | if (!SubExpr) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5946 | return nullptr; |
| 5947 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5948 | return new (Importer.getToContext()) |
| 5949 | ParenExpr(Importer.Import(E->getLParen()), |
| 5950 | Importer.Import(E->getRParen()), |
| 5951 | SubExpr); |
| 5952 | } |
| 5953 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5954 | Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) { |
| 5955 | SmallVector<Expr *, 4> Exprs(E->getNumExprs()); |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 5956 | if (ImportContainerChecked(E->exprs(), Exprs)) |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 5957 | return nullptr; |
| 5958 | |
| 5959 | return new (Importer.getToContext()) ParenListExpr( |
| 5960 | Importer.getToContext(), Importer.Import(E->getLParenLoc()), |
| 5961 | Exprs, Importer.Import(E->getLParenLoc())); |
| 5962 | } |
| 5963 | |
| 5964 | Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) { |
| 5965 | QualType T = Importer.Import(E->getType()); |
| 5966 | if (T.isNull()) |
| 5967 | return nullptr; |
| 5968 | |
| 5969 | CompoundStmt *ToSubStmt = cast_or_null<CompoundStmt>( |
| 5970 | Importer.Import(E->getSubStmt())); |
| 5971 | if (!ToSubStmt && E->getSubStmt()) |
| 5972 | return nullptr; |
| 5973 | |
| 5974 | return new (Importer.getToContext()) StmtExpr(ToSubStmt, T, |
| 5975 | Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc())); |
| 5976 | } |
| 5977 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5978 | Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) { |
| 5979 | QualType T = Importer.Import(E->getType()); |
| 5980 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5981 | return nullptr; |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5982 | |
| 5983 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 5984 | if (!SubExpr) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5985 | return nullptr; |
| 5986 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5987 | return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5988 | T, E->getValueKind(), |
| 5989 | E->getObjectKind(), |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5990 | Importer.Import(E->getOperatorLoc())); |
| 5991 | } |
| 5992 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5993 | Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr( |
| 5994 | UnaryExprOrTypeTraitExpr *E) { |
Douglas Gregor | d8552cd | 2010-02-19 01:24:23 +0000 | [diff] [blame] | 5995 | QualType ResultType = Importer.Import(E->getType()); |
| 5996 | |
| 5997 | if (E->isArgumentType()) { |
| 5998 | TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo()); |
| 5999 | if (!TInfo) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6000 | return nullptr; |
| 6001 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 6002 | return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(), |
| 6003 | TInfo, ResultType, |
Douglas Gregor | d8552cd | 2010-02-19 01:24:23 +0000 | [diff] [blame] | 6004 | Importer.Import(E->getOperatorLoc()), |
| 6005 | Importer.Import(E->getRParenLoc())); |
| 6006 | } |
| 6007 | |
| 6008 | Expr *SubExpr = Importer.Import(E->getArgumentExpr()); |
| 6009 | if (!SubExpr) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6010 | return nullptr; |
| 6011 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 6012 | return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(), |
| 6013 | SubExpr, ResultType, |
Douglas Gregor | d8552cd | 2010-02-19 01:24:23 +0000 | [diff] [blame] | 6014 | Importer.Import(E->getOperatorLoc()), |
| 6015 | Importer.Import(E->getRParenLoc())); |
| 6016 | } |
| 6017 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 6018 | Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) { |
| 6019 | QualType T = Importer.Import(E->getType()); |
| 6020 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6021 | return nullptr; |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 6022 | |
| 6023 | Expr *LHS = Importer.Import(E->getLHS()); |
| 6024 | if (!LHS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6025 | return nullptr; |
| 6026 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 6027 | Expr *RHS = Importer.Import(E->getRHS()); |
| 6028 | if (!RHS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6029 | return nullptr; |
| 6030 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 6031 | return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6032 | T, E->getValueKind(), |
| 6033 | E->getObjectKind(), |
Lang Hames | 5de91cc | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 6034 | Importer.Import(E->getOperatorLoc()), |
| 6035 | E->isFPContractable()); |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 6036 | } |
| 6037 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 6038 | Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) { |
| 6039 | QualType T = Importer.Import(E->getType()); |
| 6040 | if (T.isNull()) |
| 6041 | return nullptr; |
| 6042 | |
| 6043 | Expr *ToLHS = Importer.Import(E->getLHS()); |
| 6044 | if (!ToLHS) |
| 6045 | return nullptr; |
| 6046 | |
| 6047 | Expr *ToRHS = Importer.Import(E->getRHS()); |
| 6048 | if (!ToRHS) |
| 6049 | return nullptr; |
| 6050 | |
| 6051 | Expr *ToCond = Importer.Import(E->getCond()); |
| 6052 | if (!ToCond) |
| 6053 | return nullptr; |
| 6054 | |
| 6055 | return new (Importer.getToContext()) ConditionalOperator( |
| 6056 | ToCond, Importer.Import(E->getQuestionLoc()), |
| 6057 | ToLHS, Importer.Import(E->getColonLoc()), |
| 6058 | ToRHS, T, E->getValueKind(), E->getObjectKind()); |
| 6059 | } |
| 6060 | |
| 6061 | Expr *ASTNodeImporter::VisitBinaryConditionalOperator( |
| 6062 | BinaryConditionalOperator *E) { |
| 6063 | QualType T = Importer.Import(E->getType()); |
| 6064 | if (T.isNull()) |
| 6065 | return nullptr; |
| 6066 | |
| 6067 | Expr *Common = Importer.Import(E->getCommon()); |
| 6068 | if (!Common) |
| 6069 | return nullptr; |
| 6070 | |
| 6071 | Expr *Cond = Importer.Import(E->getCond()); |
| 6072 | if (!Cond) |
| 6073 | return nullptr; |
| 6074 | |
| 6075 | OpaqueValueExpr *OpaqueValue = cast_or_null<OpaqueValueExpr>( |
| 6076 | Importer.Import(E->getOpaqueValue())); |
| 6077 | if (!OpaqueValue) |
| 6078 | return nullptr; |
| 6079 | |
| 6080 | Expr *TrueExpr = Importer.Import(E->getTrueExpr()); |
| 6081 | if (!TrueExpr) |
| 6082 | return nullptr; |
| 6083 | |
| 6084 | Expr *FalseExpr = Importer.Import(E->getFalseExpr()); |
| 6085 | if (!FalseExpr) |
| 6086 | return nullptr; |
| 6087 | |
| 6088 | return new (Importer.getToContext()) BinaryConditionalOperator( |
| 6089 | Common, OpaqueValue, Cond, TrueExpr, FalseExpr, |
| 6090 | Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()), |
| 6091 | T, E->getValueKind(), E->getObjectKind()); |
| 6092 | } |
| 6093 | |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 6094 | Expr *ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { |
| 6095 | QualType T = Importer.Import(E->getType()); |
| 6096 | if (T.isNull()) |
| 6097 | return nullptr; |
| 6098 | |
| 6099 | TypeSourceInfo *ToQueried = Importer.Import(E->getQueriedTypeSourceInfo()); |
| 6100 | if (!ToQueried) |
| 6101 | return nullptr; |
| 6102 | |
| 6103 | Expr *Dim = Importer.Import(E->getDimensionExpression()); |
| 6104 | if (!Dim && E->getDimensionExpression()) |
| 6105 | return nullptr; |
| 6106 | |
| 6107 | return new (Importer.getToContext()) ArrayTypeTraitExpr( |
| 6108 | Importer.Import(E->getLocStart()), E->getTrait(), ToQueried, |
| 6109 | E->getValue(), Dim, Importer.Import(E->getLocEnd()), T); |
| 6110 | } |
| 6111 | |
| 6112 | Expr *ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { |
| 6113 | QualType T = Importer.Import(E->getType()); |
| 6114 | if (T.isNull()) |
| 6115 | return nullptr; |
| 6116 | |
| 6117 | Expr *ToQueried = Importer.Import(E->getQueriedExpression()); |
| 6118 | if (!ToQueried) |
| 6119 | return nullptr; |
| 6120 | |
| 6121 | return new (Importer.getToContext()) ExpressionTraitExpr( |
| 6122 | Importer.Import(E->getLocStart()), E->getTrait(), ToQueried, |
| 6123 | E->getValue(), Importer.Import(E->getLocEnd()), T); |
| 6124 | } |
| 6125 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 6126 | Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) { |
| 6127 | QualType T = Importer.Import(E->getType()); |
| 6128 | if (T.isNull()) |
| 6129 | return nullptr; |
| 6130 | |
| 6131 | Expr *SourceExpr = Importer.Import(E->getSourceExpr()); |
| 6132 | if (!SourceExpr && E->getSourceExpr()) |
| 6133 | return nullptr; |
| 6134 | |
| 6135 | return new (Importer.getToContext()) OpaqueValueExpr( |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 6136 | Importer.Import(E->getLocation()), T, E->getValueKind(), |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 6137 | E->getObjectKind(), SourceExpr); |
| 6138 | } |
| 6139 | |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 6140 | Expr *ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { |
| 6141 | QualType T = Importer.Import(E->getType()); |
| 6142 | if (T.isNull()) |
| 6143 | return nullptr; |
| 6144 | |
| 6145 | Expr *ToLHS = Importer.Import(E->getLHS()); |
| 6146 | if (!ToLHS) |
| 6147 | return nullptr; |
| 6148 | |
| 6149 | Expr *ToRHS = Importer.Import(E->getRHS()); |
| 6150 | if (!ToRHS) |
| 6151 | return nullptr; |
| 6152 | |
| 6153 | return new (Importer.getToContext()) ArraySubscriptExpr( |
| 6154 | ToLHS, ToRHS, T, E->getValueKind(), E->getObjectKind(), |
| 6155 | Importer.Import(E->getRBracketLoc())); |
| 6156 | } |
| 6157 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 6158 | Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) { |
| 6159 | QualType T = Importer.Import(E->getType()); |
| 6160 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6161 | return nullptr; |
| 6162 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 6163 | QualType CompLHSType = Importer.Import(E->getComputationLHSType()); |
| 6164 | if (CompLHSType.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6165 | return nullptr; |
| 6166 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 6167 | QualType CompResultType = Importer.Import(E->getComputationResultType()); |
| 6168 | if (CompResultType.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6169 | return nullptr; |
| 6170 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 6171 | Expr *LHS = Importer.Import(E->getLHS()); |
| 6172 | if (!LHS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6173 | return nullptr; |
| 6174 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 6175 | Expr *RHS = Importer.Import(E->getRHS()); |
| 6176 | if (!RHS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6177 | return nullptr; |
| 6178 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 6179 | return new (Importer.getToContext()) |
| 6180 | CompoundAssignOperator(LHS, RHS, E->getOpcode(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6181 | T, E->getValueKind(), |
| 6182 | E->getObjectKind(), |
| 6183 | CompLHSType, CompResultType, |
Lang Hames | 5de91cc | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 6184 | Importer.Import(E->getOperatorLoc()), |
| 6185 | E->isFPContractable()); |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 6186 | } |
| 6187 | |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 6188 | bool ASTNodeImporter::ImportCastPath(CastExpr *CE, CXXCastPath &Path) { |
| 6189 | for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) { |
| 6190 | if (CXXBaseSpecifier *Spec = Importer.Import(*I)) |
| 6191 | Path.push_back(Spec); |
| 6192 | else |
| 6193 | return true; |
| 6194 | } |
| 6195 | return false; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 6196 | } |
| 6197 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 6198 | Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 6199 | QualType T = Importer.Import(E->getType()); |
| 6200 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6201 | return nullptr; |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 6202 | |
| 6203 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 6204 | if (!SubExpr) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6205 | return nullptr; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 6206 | |
| 6207 | CXXCastPath BasePath; |
| 6208 | if (ImportCastPath(E, BasePath)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6209 | return nullptr; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 6210 | |
| 6211 | return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 6212 | SubExpr, &BasePath, E->getValueKind()); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 6213 | } |
| 6214 | |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 6215 | Expr *ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) { |
Douglas Gregor | 5481d32 | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 6216 | QualType T = Importer.Import(E->getType()); |
| 6217 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6218 | return nullptr; |
| 6219 | |
Douglas Gregor | 5481d32 | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 6220 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 6221 | if (!SubExpr) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6222 | return nullptr; |
Douglas Gregor | 5481d32 | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 6223 | |
| 6224 | TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten()); |
| 6225 | if (!TInfo && E->getTypeInfoAsWritten()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6226 | return nullptr; |
| 6227 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 6228 | CXXCastPath BasePath; |
| 6229 | if (ImportCastPath(E, BasePath)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6230 | return nullptr; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 6231 | |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 6232 | switch (E->getStmtClass()) { |
| 6233 | case Stmt::CStyleCastExprClass: { |
| 6234 | CStyleCastExpr *CCE = cast<CStyleCastExpr>(E); |
| 6235 | return CStyleCastExpr::Create(Importer.getToContext(), T, |
| 6236 | E->getValueKind(), E->getCastKind(), |
| 6237 | SubExpr, &BasePath, TInfo, |
| 6238 | Importer.Import(CCE->getLParenLoc()), |
| 6239 | Importer.Import(CCE->getRParenLoc())); |
| 6240 | } |
| 6241 | |
| 6242 | case Stmt::CXXFunctionalCastExprClass: { |
| 6243 | CXXFunctionalCastExpr *FCE = cast<CXXFunctionalCastExpr>(E); |
| 6244 | return CXXFunctionalCastExpr::Create(Importer.getToContext(), T, |
| 6245 | E->getValueKind(), TInfo, |
| 6246 | E->getCastKind(), SubExpr, &BasePath, |
| 6247 | Importer.Import(FCE->getLParenLoc()), |
| 6248 | Importer.Import(FCE->getRParenLoc())); |
| 6249 | } |
| 6250 | |
| 6251 | case Stmt::ObjCBridgedCastExprClass: { |
| 6252 | ObjCBridgedCastExpr *OCE = cast<ObjCBridgedCastExpr>(E); |
| 6253 | return new (Importer.getToContext()) ObjCBridgedCastExpr( |
| 6254 | Importer.Import(OCE->getLParenLoc()), OCE->getBridgeKind(), |
| 6255 | E->getCastKind(), Importer.Import(OCE->getBridgeKeywordLoc()), |
| 6256 | TInfo, SubExpr); |
| 6257 | } |
| 6258 | default: |
| 6259 | break; // just fall through |
| 6260 | } |
| 6261 | |
| 6262 | CXXNamedCastExpr *Named = cast<CXXNamedCastExpr>(E); |
| 6263 | SourceLocation ExprLoc = Importer.Import(Named->getOperatorLoc()), |
| 6264 | RParenLoc = Importer.Import(Named->getRParenLoc()); |
| 6265 | SourceRange Brackets = Importer.Import(Named->getAngleBrackets()); |
| 6266 | |
| 6267 | switch (E->getStmtClass()) { |
| 6268 | case Stmt::CXXStaticCastExprClass: |
| 6269 | return CXXStaticCastExpr::Create(Importer.getToContext(), T, |
| 6270 | E->getValueKind(), E->getCastKind(), |
| 6271 | SubExpr, &BasePath, TInfo, |
| 6272 | ExprLoc, RParenLoc, Brackets); |
| 6273 | |
| 6274 | case Stmt::CXXDynamicCastExprClass: |
| 6275 | return CXXDynamicCastExpr::Create(Importer.getToContext(), T, |
| 6276 | E->getValueKind(), E->getCastKind(), |
| 6277 | SubExpr, &BasePath, TInfo, |
| 6278 | ExprLoc, RParenLoc, Brackets); |
| 6279 | |
| 6280 | case Stmt::CXXReinterpretCastExprClass: |
| 6281 | return CXXReinterpretCastExpr::Create(Importer.getToContext(), T, |
| 6282 | E->getValueKind(), E->getCastKind(), |
| 6283 | SubExpr, &BasePath, TInfo, |
| 6284 | ExprLoc, RParenLoc, Brackets); |
| 6285 | |
| 6286 | case Stmt::CXXConstCastExprClass: |
| 6287 | return CXXConstCastExpr::Create(Importer.getToContext(), T, |
| 6288 | E->getValueKind(), SubExpr, TInfo, ExprLoc, |
| 6289 | RParenLoc, Brackets); |
| 6290 | default: |
| 6291 | llvm_unreachable("Cast expression of unsupported type!"); |
| 6292 | return nullptr; |
| 6293 | } |
| 6294 | } |
| 6295 | |
| 6296 | Expr *ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *OE) { |
| 6297 | QualType T = Importer.Import(OE->getType()); |
| 6298 | if (T.isNull()) |
| 6299 | return nullptr; |
| 6300 | |
| 6301 | SmallVector<OffsetOfNode, 4> Nodes; |
| 6302 | for (int I = 0, E = OE->getNumComponents(); I < E; ++I) { |
| 6303 | const OffsetOfNode &Node = OE->getComponent(I); |
| 6304 | |
| 6305 | switch (Node.getKind()) { |
| 6306 | case OffsetOfNode::Array: |
| 6307 | Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), |
| 6308 | Node.getArrayExprIndex(), |
| 6309 | Importer.Import(Node.getLocEnd()))); |
| 6310 | break; |
| 6311 | |
| 6312 | case OffsetOfNode::Base: { |
| 6313 | CXXBaseSpecifier *BS = Importer.Import(Node.getBase()); |
| 6314 | if (!BS && Node.getBase()) |
| 6315 | return nullptr; |
| 6316 | Nodes.push_back(OffsetOfNode(BS)); |
| 6317 | break; |
| 6318 | } |
| 6319 | case OffsetOfNode::Field: { |
| 6320 | FieldDecl *FD = cast_or_null<FieldDecl>(Importer.Import(Node.getField())); |
| 6321 | if (!FD) |
| 6322 | return nullptr; |
| 6323 | Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), FD, |
| 6324 | Importer.Import(Node.getLocEnd()))); |
| 6325 | break; |
| 6326 | } |
| 6327 | case OffsetOfNode::Identifier: { |
| 6328 | IdentifierInfo *ToII = Importer.Import(Node.getFieldName()); |
| 6329 | if (!ToII) |
| 6330 | return nullptr; |
| 6331 | Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), ToII, |
| 6332 | Importer.Import(Node.getLocEnd()))); |
| 6333 | break; |
| 6334 | } |
| 6335 | } |
| 6336 | } |
| 6337 | |
| 6338 | SmallVector<Expr *, 4> Exprs(OE->getNumExpressions()); |
| 6339 | for (int I = 0, E = OE->getNumExpressions(); I < E; ++I) { |
| 6340 | Expr *ToIndexExpr = Importer.Import(OE->getIndexExpr(I)); |
| 6341 | if (!ToIndexExpr) |
| 6342 | return nullptr; |
| 6343 | Exprs[I] = ToIndexExpr; |
| 6344 | } |
| 6345 | |
| 6346 | TypeSourceInfo *TInfo = Importer.Import(OE->getTypeSourceInfo()); |
| 6347 | if (!TInfo && OE->getTypeSourceInfo()) |
| 6348 | return nullptr; |
| 6349 | |
| 6350 | return OffsetOfExpr::Create(Importer.getToContext(), T, |
| 6351 | Importer.Import(OE->getOperatorLoc()), |
| 6352 | TInfo, Nodes, Exprs, |
| 6353 | Importer.Import(OE->getRParenLoc())); |
| 6354 | } |
| 6355 | |
| 6356 | Expr *ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { |
| 6357 | QualType T = Importer.Import(E->getType()); |
| 6358 | if (T.isNull()) |
| 6359 | return nullptr; |
| 6360 | |
| 6361 | Expr *Operand = Importer.Import(E->getOperand()); |
| 6362 | if (!Operand) |
| 6363 | return nullptr; |
| 6364 | |
| 6365 | CanThrowResult CanThrow; |
| 6366 | if (E->isValueDependent()) |
| 6367 | CanThrow = CT_Dependent; |
| 6368 | else |
| 6369 | CanThrow = E->getValue() ? CT_Can : CT_Cannot; |
| 6370 | |
| 6371 | return new (Importer.getToContext()) CXXNoexceptExpr( |
| 6372 | T, Operand, CanThrow, |
| 6373 | Importer.Import(E->getLocStart()), Importer.Import(E->getLocEnd())); |
| 6374 | } |
| 6375 | |
| 6376 | Expr *ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) { |
| 6377 | QualType T = Importer.Import(E->getType()); |
| 6378 | if (T.isNull()) |
| 6379 | return nullptr; |
| 6380 | |
| 6381 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 6382 | if (!SubExpr && E->getSubExpr()) |
| 6383 | return nullptr; |
| 6384 | |
| 6385 | return new (Importer.getToContext()) CXXThrowExpr( |
| 6386 | SubExpr, T, Importer.Import(E->getThrowLoc()), |
| 6387 | E->isThrownVariableInScope()); |
| 6388 | } |
| 6389 | |
| 6390 | Expr *ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { |
| 6391 | ParmVarDecl *Param = cast_or_null<ParmVarDecl>( |
| 6392 | Importer.Import(E->getParam())); |
| 6393 | if (!Param) |
| 6394 | return nullptr; |
| 6395 | |
| 6396 | return CXXDefaultArgExpr::Create( |
| 6397 | Importer.getToContext(), Importer.Import(E->getUsedLocation()), Param); |
| 6398 | } |
| 6399 | |
| 6400 | Expr *ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { |
| 6401 | QualType T = Importer.Import(E->getType()); |
| 6402 | if (T.isNull()) |
| 6403 | return nullptr; |
| 6404 | |
| 6405 | TypeSourceInfo *TypeInfo = Importer.Import(E->getTypeSourceInfo()); |
| 6406 | if (!TypeInfo) |
| 6407 | return nullptr; |
| 6408 | |
| 6409 | return new (Importer.getToContext()) CXXScalarValueInitExpr( |
| 6410 | T, TypeInfo, Importer.Import(E->getRParenLoc())); |
| 6411 | } |
| 6412 | |
| 6413 | Expr *ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { |
| 6414 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 6415 | if (!SubExpr) |
| 6416 | return nullptr; |
| 6417 | |
| 6418 | auto *Dtor = cast_or_null<CXXDestructorDecl>( |
| 6419 | Importer.Import(const_cast<CXXDestructorDecl *>( |
| 6420 | E->getTemporary()->getDestructor()))); |
| 6421 | if (!Dtor) |
| 6422 | return nullptr; |
| 6423 | |
| 6424 | ASTContext &ToCtx = Importer.getToContext(); |
| 6425 | CXXTemporary *Temp = CXXTemporary::Create(ToCtx, Dtor); |
| 6426 | return CXXBindTemporaryExpr::Create(ToCtx, Temp, SubExpr); |
| 6427 | } |
| 6428 | |
| 6429 | Expr *ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE) { |
| 6430 | QualType T = Importer.Import(CE->getType()); |
| 6431 | if (T.isNull()) |
| 6432 | return nullptr; |
| 6433 | |
| 6434 | SmallVector<Expr *, 8> Args(CE->getNumArgs()); |
| 6435 | if (ImportContainerChecked(CE->arguments(), Args)) |
| 6436 | return nullptr; |
| 6437 | |
| 6438 | auto *Ctor = cast_or_null<CXXConstructorDecl>( |
| 6439 | Importer.Import(CE->getConstructor())); |
| 6440 | if (!Ctor) |
| 6441 | return nullptr; |
| 6442 | |
| 6443 | return CXXTemporaryObjectExpr::Create( |
| 6444 | Importer.getToContext(), T, |
| 6445 | Importer.Import(CE->getLocStart()), |
| 6446 | Ctor, |
| 6447 | CE->isElidable(), |
| 6448 | Args, |
| 6449 | CE->hadMultipleCandidates(), |
| 6450 | CE->isListInitialization(), |
| 6451 | CE->isStdInitListInitialization(), |
| 6452 | CE->requiresZeroInitialization(), |
| 6453 | CE->getConstructionKind(), |
| 6454 | Importer.Import(CE->getParenOrBraceRange())); |
| 6455 | } |
| 6456 | |
| 6457 | Expr * |
| 6458 | ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) { |
| 6459 | QualType T = Importer.Import(E->getType()); |
| 6460 | if (T.isNull()) |
| 6461 | return nullptr; |
| 6462 | |
| 6463 | Expr *TempE = Importer.Import(E->GetTemporaryExpr()); |
| 6464 | if (!TempE) |
| 6465 | return nullptr; |
| 6466 | |
| 6467 | ValueDecl *ExtendedBy = cast_or_null<ValueDecl>( |
| 6468 | Importer.Import(const_cast<ValueDecl *>(E->getExtendingDecl()))); |
| 6469 | if (!ExtendedBy && E->getExtendingDecl()) |
| 6470 | return nullptr; |
| 6471 | |
| 6472 | auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr( |
| 6473 | T, TempE, E->isBoundToLvalueReference()); |
| 6474 | |
| 6475 | // FIXME: Should ManglingNumber get numbers associated with 'to' context? |
| 6476 | ToMTE->setExtendingDecl(ExtendedBy, E->getManglingNumber()); |
| 6477 | return ToMTE; |
| 6478 | } |
| 6479 | |
| 6480 | Expr *ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *CE) { |
| 6481 | QualType T = Importer.Import(CE->getType()); |
| 6482 | if (T.isNull()) |
| 6483 | return nullptr; |
| 6484 | |
| 6485 | SmallVector<Expr *, 4> PlacementArgs(CE->getNumPlacementArgs()); |
| 6486 | if (ImportContainerChecked(CE->placement_arguments(), PlacementArgs)) |
| 6487 | return nullptr; |
| 6488 | |
| 6489 | FunctionDecl *OperatorNewDecl = cast_or_null<FunctionDecl>( |
| 6490 | Importer.Import(CE->getOperatorNew())); |
| 6491 | if (!OperatorNewDecl && CE->getOperatorNew()) |
| 6492 | return nullptr; |
| 6493 | |
| 6494 | FunctionDecl *OperatorDeleteDecl = cast_or_null<FunctionDecl>( |
| 6495 | Importer.Import(CE->getOperatorDelete())); |
| 6496 | if (!OperatorDeleteDecl && CE->getOperatorDelete()) |
| 6497 | return nullptr; |
| 6498 | |
| 6499 | Expr *ToInit = Importer.Import(CE->getInitializer()); |
| 6500 | if (!ToInit && CE->getInitializer()) |
| 6501 | return nullptr; |
| 6502 | |
| 6503 | TypeSourceInfo *TInfo = Importer.Import(CE->getAllocatedTypeSourceInfo()); |
| 6504 | if (!TInfo) |
| 6505 | return nullptr; |
| 6506 | |
| 6507 | Expr *ToArrSize = Importer.Import(CE->getArraySize()); |
| 6508 | if (!ToArrSize && CE->getArraySize()) |
| 6509 | return nullptr; |
| 6510 | |
| 6511 | return new (Importer.getToContext()) CXXNewExpr( |
| 6512 | Importer.getToContext(), |
| 6513 | CE->isGlobalNew(), |
| 6514 | OperatorNewDecl, OperatorDeleteDecl, |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 6515 | CE->passAlignment(), |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 6516 | CE->doesUsualArrayDeleteWantSize(), |
| 6517 | PlacementArgs, |
| 6518 | Importer.Import(CE->getTypeIdParens()), |
| 6519 | ToArrSize, CE->getInitializationStyle(), ToInit, T, TInfo, |
| 6520 | Importer.Import(CE->getSourceRange()), |
| 6521 | Importer.Import(CE->getDirectInitRange())); |
| 6522 | } |
| 6523 | |
| 6524 | Expr *ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 6525 | QualType T = Importer.Import(E->getType()); |
| 6526 | if (T.isNull()) |
| 6527 | return nullptr; |
| 6528 | |
| 6529 | FunctionDecl *OperatorDeleteDecl = cast_or_null<FunctionDecl>( |
| 6530 | Importer.Import(E->getOperatorDelete())); |
| 6531 | if (!OperatorDeleteDecl && E->getOperatorDelete()) |
| 6532 | return nullptr; |
| 6533 | |
| 6534 | Expr *ToArg = Importer.Import(E->getArgument()); |
| 6535 | if (!ToArg && E->getArgument()) |
| 6536 | return nullptr; |
| 6537 | |
| 6538 | return new (Importer.getToContext()) CXXDeleteExpr( |
| 6539 | T, E->isGlobalDelete(), |
| 6540 | E->isArrayForm(), |
| 6541 | E->isArrayFormAsWritten(), |
| 6542 | E->doesUsualArrayDeleteWantSize(), |
| 6543 | OperatorDeleteDecl, |
| 6544 | ToArg, |
| 6545 | Importer.Import(E->getLocStart())); |
Douglas Gregor | 5481d32 | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 6546 | } |
| 6547 | |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 6548 | Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 6549 | QualType T = Importer.Import(E->getType()); |
| 6550 | if (T.isNull()) |
| 6551 | return nullptr; |
| 6552 | |
| 6553 | CXXConstructorDecl *ToCCD = |
Sean Callanan | dd2c174 | 2016-05-16 20:48:03 +0000 | [diff] [blame] | 6554 | dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor())); |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6555 | if (!ToCCD) |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 6556 | return nullptr; |
| 6557 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 6558 | SmallVector<Expr *, 6> ToArgs(E->getNumArgs()); |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 6559 | if (ImportContainerChecked(E->arguments(), ToArgs)) |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 6560 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 6561 | |
| 6562 | return CXXConstructExpr::Create(Importer.getToContext(), T, |
| 6563 | Importer.Import(E->getLocation()), |
Richard Smith | c83bf82 | 2016-06-10 00:58:19 +0000 | [diff] [blame] | 6564 | ToCCD, E->isElidable(), |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 6565 | ToArgs, E->hadMultipleCandidates(), |
| 6566 | E->isListInitialization(), |
| 6567 | E->isStdInitListInitialization(), |
| 6568 | E->requiresZeroInitialization(), |
| 6569 | E->getConstructionKind(), |
| 6570 | Importer.Import(E->getParenOrBraceRange())); |
| 6571 | } |
| 6572 | |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 6573 | Expr *ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *EWC) { |
| 6574 | Expr *SubExpr = Importer.Import(EWC->getSubExpr()); |
| 6575 | if (!SubExpr && EWC->getSubExpr()) |
| 6576 | return nullptr; |
| 6577 | |
| 6578 | SmallVector<ExprWithCleanups::CleanupObject, 8> Objs(EWC->getNumObjects()); |
| 6579 | for (unsigned I = 0, E = EWC->getNumObjects(); I < E; I++) |
| 6580 | if (ExprWithCleanups::CleanupObject Obj = |
| 6581 | cast_or_null<BlockDecl>(Importer.Import(EWC->getObject(I)))) |
| 6582 | Objs[I] = Obj; |
| 6583 | else |
| 6584 | return nullptr; |
| 6585 | |
| 6586 | return ExprWithCleanups::Create(Importer.getToContext(), |
| 6587 | SubExpr, EWC->cleanupsHaveSideEffects(), |
| 6588 | Objs); |
| 6589 | } |
| 6590 | |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 6591 | Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) { |
| 6592 | QualType T = Importer.Import(E->getType()); |
| 6593 | if (T.isNull()) |
| 6594 | return nullptr; |
| 6595 | |
| 6596 | Expr *ToFn = Importer.Import(E->getCallee()); |
| 6597 | if (!ToFn) |
| 6598 | return nullptr; |
| 6599 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 6600 | SmallVector<Expr *, 4> ToArgs(E->getNumArgs()); |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 6601 | if (ImportContainerChecked(E->arguments(), ToArgs)) |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 6602 | return nullptr; |
| 6603 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 6604 | return new (Importer.getToContext()) CXXMemberCallExpr( |
| 6605 | Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(), |
| 6606 | Importer.Import(E->getRParenLoc())); |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 6607 | } |
| 6608 | |
| 6609 | Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) { |
| 6610 | QualType T = Importer.Import(E->getType()); |
| 6611 | if (T.isNull()) |
| 6612 | return nullptr; |
| 6613 | |
| 6614 | return new (Importer.getToContext()) |
| 6615 | CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit()); |
| 6616 | } |
| 6617 | |
| 6618 | Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) { |
| 6619 | QualType T = Importer.Import(E->getType()); |
| 6620 | if (T.isNull()) |
| 6621 | return nullptr; |
| 6622 | |
| 6623 | return new (Importer.getToContext()) |
| 6624 | CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation())); |
| 6625 | } |
| 6626 | |
| 6627 | |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 6628 | Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) { |
| 6629 | QualType T = Importer.Import(E->getType()); |
| 6630 | if (T.isNull()) |
| 6631 | return nullptr; |
| 6632 | |
| 6633 | Expr *ToBase = Importer.Import(E->getBase()); |
| 6634 | if (!ToBase && E->getBase()) |
| 6635 | return nullptr; |
| 6636 | |
| 6637 | ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl())); |
| 6638 | if (!ToMember && E->getMemberDecl()) |
| 6639 | return nullptr; |
| 6640 | |
| 6641 | DeclAccessPair ToFoundDecl = DeclAccessPair::make( |
| 6642 | dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())), |
| 6643 | E->getFoundDecl().getAccess()); |
| 6644 | |
| 6645 | DeclarationNameInfo ToMemberNameInfo( |
| 6646 | Importer.Import(E->getMemberNameInfo().getName()), |
| 6647 | Importer.Import(E->getMemberNameInfo().getLoc())); |
| 6648 | |
| 6649 | if (E->hasExplicitTemplateArgs()) { |
| 6650 | return nullptr; // FIXME: handle template arguments |
| 6651 | } |
| 6652 | |
| 6653 | return MemberExpr::Create(Importer.getToContext(), ToBase, |
| 6654 | E->isArrow(), |
| 6655 | Importer.Import(E->getOperatorLoc()), |
| 6656 | Importer.Import(E->getQualifierLoc()), |
| 6657 | Importer.Import(E->getTemplateKeywordLoc()), |
| 6658 | ToMember, ToFoundDecl, ToMemberNameInfo, |
| 6659 | nullptr, T, E->getValueKind(), |
| 6660 | E->getObjectKind()); |
| 6661 | } |
| 6662 | |
| 6663 | Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) { |
| 6664 | QualType T = Importer.Import(E->getType()); |
| 6665 | if (T.isNull()) |
| 6666 | return nullptr; |
| 6667 | |
| 6668 | Expr *ToCallee = Importer.Import(E->getCallee()); |
| 6669 | if (!ToCallee && E->getCallee()) |
| 6670 | return nullptr; |
| 6671 | |
| 6672 | unsigned NumArgs = E->getNumArgs(); |
| 6673 | |
| 6674 | llvm::SmallVector<Expr *, 2> ToArgs(NumArgs); |
| 6675 | |
| 6676 | for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) { |
| 6677 | Expr *FromArg = E->getArg(ai); |
| 6678 | Expr *ToArg = Importer.Import(FromArg); |
| 6679 | if (!ToArg) |
| 6680 | return nullptr; |
| 6681 | ToArgs[ai] = ToArg; |
| 6682 | } |
| 6683 | |
| 6684 | Expr **ToArgs_Copied = new (Importer.getToContext()) |
| 6685 | Expr*[NumArgs]; |
| 6686 | |
| 6687 | for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) |
| 6688 | ToArgs_Copied[ai] = ToArgs[ai]; |
| 6689 | |
| 6690 | return new (Importer.getToContext()) |
| 6691 | CallExpr(Importer.getToContext(), ToCallee, |
Craig Topper | c005cc0 | 2015-09-27 03:44:08 +0000 | [diff] [blame] | 6692 | llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(), |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 6693 | Importer.Import(E->getRParenLoc())); |
| 6694 | } |
| 6695 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 6696 | Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) { |
| 6697 | QualType T = Importer.Import(ILE->getType()); |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 6698 | if (T.isNull()) |
| 6699 | return nullptr; |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 6700 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 6701 | llvm::SmallVector<Expr *, 4> Exprs(ILE->getNumInits()); |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 6702 | if (ImportContainerChecked(ILE->inits(), Exprs)) |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 6703 | return nullptr; |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 6704 | |
Artem Dergachev | 4e7c6fd | 2016-04-14 11:51:27 +0000 | [diff] [blame] | 6705 | ASTContext &ToCtx = Importer.getToContext(); |
| 6706 | InitListExpr *To = new (ToCtx) InitListExpr( |
| 6707 | ToCtx, Importer.Import(ILE->getLBraceLoc()), |
| 6708 | Exprs, Importer.Import(ILE->getLBraceLoc())); |
| 6709 | To->setType(T); |
| 6710 | |
| 6711 | if (ILE->hasArrayFiller()) { |
| 6712 | Expr *Filler = Importer.Import(ILE->getArrayFiller()); |
| 6713 | if (!Filler) |
| 6714 | return nullptr; |
| 6715 | To->setArrayFiller(Filler); |
| 6716 | } |
| 6717 | |
| 6718 | if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) { |
| 6719 | FieldDecl *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD)); |
| 6720 | if (!ToFD) |
| 6721 | return nullptr; |
| 6722 | To->setInitializedFieldInUnion(ToFD); |
| 6723 | } |
| 6724 | |
| 6725 | if (InitListExpr *SyntForm = ILE->getSyntacticForm()) { |
| 6726 | InitListExpr *ToSyntForm = cast_or_null<InitListExpr>( |
| 6727 | Importer.Import(SyntForm)); |
| 6728 | if (!ToSyntForm) |
| 6729 | return nullptr; |
| 6730 | To->setSyntacticForm(ToSyntForm); |
| 6731 | } |
| 6732 | |
| 6733 | To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator()); |
| 6734 | To->setValueDependent(ILE->isValueDependent()); |
| 6735 | To->setInstantiationDependent(ILE->isInstantiationDependent()); |
| 6736 | |
| 6737 | return To; |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 6738 | } |
| 6739 | |
Richard Smith | 30e304e | 2016-12-14 00:03:17 +0000 | [diff] [blame] | 6740 | Expr *ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) { |
| 6741 | QualType ToType = Importer.Import(E->getType()); |
| 6742 | if (ToType.isNull()) |
| 6743 | return nullptr; |
| 6744 | |
| 6745 | Expr *ToCommon = Importer.Import(E->getCommonExpr()); |
| 6746 | if (!ToCommon && E->getCommonExpr()) |
| 6747 | return nullptr; |
| 6748 | |
| 6749 | Expr *ToSubExpr = Importer.Import(E->getSubExpr()); |
| 6750 | if (!ToSubExpr && E->getSubExpr()) |
| 6751 | return nullptr; |
| 6752 | |
| 6753 | return new (Importer.getToContext()) |
| 6754 | ArrayInitLoopExpr(ToType, ToCommon, ToSubExpr); |
| 6755 | } |
| 6756 | |
| 6757 | Expr *ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) { |
| 6758 | QualType ToType = Importer.Import(E->getType()); |
| 6759 | if (ToType.isNull()) |
| 6760 | return nullptr; |
| 6761 | return new (Importer.getToContext()) ArrayInitIndexExpr(ToType); |
| 6762 | } |
| 6763 | |
Sean Callanan | dd2c174 | 2016-05-16 20:48:03 +0000 | [diff] [blame] | 6764 | Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) { |
| 6765 | FieldDecl *ToField = llvm::dyn_cast_or_null<FieldDecl>( |
| 6766 | Importer.Import(DIE->getField())); |
| 6767 | if (!ToField && DIE->getField()) |
| 6768 | return nullptr; |
| 6769 | |
| 6770 | return CXXDefaultInitExpr::Create( |
| 6771 | Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField); |
| 6772 | } |
| 6773 | |
| 6774 | Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) { |
| 6775 | QualType ToType = Importer.Import(E->getType()); |
| 6776 | if (ToType.isNull() && !E->getType().isNull()) |
| 6777 | return nullptr; |
| 6778 | ExprValueKind VK = E->getValueKind(); |
| 6779 | CastKind CK = E->getCastKind(); |
| 6780 | Expr *ToOp = Importer.Import(E->getSubExpr()); |
| 6781 | if (!ToOp && E->getSubExpr()) |
| 6782 | return nullptr; |
| 6783 | CXXCastPath BasePath; |
| 6784 | if (ImportCastPath(E, BasePath)) |
| 6785 | return nullptr; |
| 6786 | TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten()); |
| 6787 | SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc()); |
| 6788 | SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc()); |
| 6789 | SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets()); |
| 6790 | |
| 6791 | if (isa<CXXStaticCastExpr>(E)) { |
| 6792 | return CXXStaticCastExpr::Create( |
| 6793 | Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath, |
| 6794 | ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets); |
| 6795 | } else if (isa<CXXDynamicCastExpr>(E)) { |
| 6796 | return CXXDynamicCastExpr::Create( |
| 6797 | Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath, |
| 6798 | ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets); |
| 6799 | } else if (isa<CXXReinterpretCastExpr>(E)) { |
| 6800 | return CXXReinterpretCastExpr::Create( |
| 6801 | Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath, |
| 6802 | ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets); |
| 6803 | } else { |
| 6804 | return nullptr; |
| 6805 | } |
| 6806 | } |
| 6807 | |
Aleksei Sidorin | 855086d | 2017-01-23 09:30:36 +0000 | [diff] [blame] | 6808 | |
| 6809 | Expr *ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr( |
| 6810 | SubstNonTypeTemplateParmExpr *E) { |
| 6811 | QualType T = Importer.Import(E->getType()); |
| 6812 | if (T.isNull()) |
| 6813 | return nullptr; |
| 6814 | |
| 6815 | NonTypeTemplateParmDecl *Param = cast_or_null<NonTypeTemplateParmDecl>( |
| 6816 | Importer.Import(E->getParameter())); |
| 6817 | if (!Param) |
| 6818 | return nullptr; |
| 6819 | |
| 6820 | Expr *Replacement = Importer.Import(E->getReplacement()); |
| 6821 | if (!Replacement) |
| 6822 | return nullptr; |
| 6823 | |
| 6824 | return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr( |
| 6825 | T, E->getValueKind(), Importer.Import(E->getExprLoc()), Param, |
| 6826 | Replacement); |
| 6827 | } |
| 6828 | |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 6829 | ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager, |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 6830 | ASTContext &FromContext, FileManager &FromFileManager, |
| 6831 | bool MinimalImport) |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 6832 | : ToContext(ToContext), FromContext(FromContext), |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 6833 | ToFileManager(ToFileManager), FromFileManager(FromFileManager), |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 6834 | Minimal(MinimalImport), LastDiagFromFrom(false) |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 6835 | { |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6836 | ImportedDecls[FromContext.getTranslationUnitDecl()] |
| 6837 | = ToContext.getTranslationUnitDecl(); |
| 6838 | } |
| 6839 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 6840 | ASTImporter::~ASTImporter() { } |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 6841 | |
| 6842 | QualType ASTImporter::Import(QualType FromT) { |
| 6843 | if (FromT.isNull()) |
| 6844 | return QualType(); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 6845 | |
| 6846 | const Type *fromTy = FromT.getTypePtr(); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 6847 | |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 6848 | // Check whether we've already imported this type. |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 6849 | llvm::DenseMap<const Type *, const Type *>::iterator Pos |
| 6850 | = ImportedTypes.find(fromTy); |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 6851 | if (Pos != ImportedTypes.end()) |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 6852 | return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 6853 | |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 6854 | // Import the type |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 6855 | ASTNodeImporter Importer(*this); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 6856 | QualType ToT = Importer.Visit(fromTy); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 6857 | if (ToT.isNull()) |
| 6858 | return ToT; |
| 6859 | |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 6860 | // Record the imported type. |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 6861 | ImportedTypes[fromTy] = ToT.getTypePtr(); |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 6862 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 6863 | return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 6864 | } |
| 6865 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6866 | TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) { |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 6867 | if (!FromTSI) |
| 6868 | return FromTSI; |
| 6869 | |
| 6870 | // FIXME: For now we just create a "trivial" type source info based |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 6871 | // 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] | 6872 | QualType T = Import(FromTSI->getType()); |
| 6873 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6874 | return nullptr; |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 6875 | |
| 6876 | return ToContext.getTrivialTypeSourceInfo(T, |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 6877 | Import(FromTSI->getTypeLoc().getLocStart())); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6878 | } |
| 6879 | |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 6880 | Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) { |
| 6881 | llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD); |
| 6882 | if (Pos != ImportedDecls.end()) { |
| 6883 | Decl *ToD = Pos->second; |
| 6884 | ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD); |
| 6885 | return ToD; |
| 6886 | } else { |
| 6887 | return nullptr; |
| 6888 | } |
| 6889 | } |
| 6890 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6891 | Decl *ASTImporter::Import(Decl *FromD) { |
| 6892 | if (!FromD) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6893 | return nullptr; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6894 | |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 6895 | ASTNodeImporter Importer(*this); |
| 6896 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6897 | // Check whether we've already imported this declaration. |
| 6898 | llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 6899 | if (Pos != ImportedDecls.end()) { |
| 6900 | Decl *ToD = Pos->second; |
| 6901 | Importer.ImportDefinitionIfNeeded(FromD, ToD); |
| 6902 | return ToD; |
| 6903 | } |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6904 | |
| 6905 | // Import the type |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6906 | Decl *ToD = Importer.Visit(FromD); |
| 6907 | if (!ToD) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6908 | return nullptr; |
| 6909 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6910 | // Record the imported declaration. |
| 6911 | ImportedDecls[FromD] = ToD; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 6912 | |
| 6913 | if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) { |
| 6914 | // Keep track of anonymous tags that have an associated typedef. |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6915 | if (FromTag->getTypedefNameForAnonDecl()) |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 6916 | AnonTagsWithPendingTypedefs.push_back(FromTag); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6917 | } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 6918 | // When we've finished transforming a typedef, see whether it was the |
| 6919 | // typedef for an anonymous tag. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 6920 | for (SmallVectorImpl<TagDecl *>::iterator |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 6921 | FromTag = AnonTagsWithPendingTypedefs.begin(), |
| 6922 | FromTagEnd = AnonTagsWithPendingTypedefs.end(); |
| 6923 | FromTag != FromTagEnd; ++FromTag) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6924 | if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 6925 | if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) { |
| 6926 | // We found the typedef for an anonymous tag; link them. |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6927 | ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD)); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 6928 | AnonTagsWithPendingTypedefs.erase(FromTag); |
| 6929 | break; |
| 6930 | } |
| 6931 | } |
| 6932 | } |
| 6933 | } |
| 6934 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6935 | return ToD; |
| 6936 | } |
| 6937 | |
| 6938 | DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) { |
| 6939 | if (!FromDC) |
| 6940 | return FromDC; |
| 6941 | |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 6942 | DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC))); |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 6943 | if (!ToDC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6944 | return nullptr; |
| 6945 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 6946 | // When we're using a record/enum/Objective-C class/protocol as a context, we |
| 6947 | // need it to have a definition. |
| 6948 | if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) { |
Douglas Gregor | 63db971 | 2012-01-25 01:13:20 +0000 | [diff] [blame] | 6949 | RecordDecl *FromRecord = cast<RecordDecl>(FromDC); |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 6950 | if (ToRecord->isCompleteDefinition()) { |
| 6951 | // Do nothing. |
| 6952 | } else if (FromRecord->isCompleteDefinition()) { |
| 6953 | ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord, |
| 6954 | ASTNodeImporter::IDK_Basic); |
| 6955 | } else { |
| 6956 | CompleteDecl(ToRecord); |
| 6957 | } |
| 6958 | } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) { |
| 6959 | EnumDecl *FromEnum = cast<EnumDecl>(FromDC); |
| 6960 | if (ToEnum->isCompleteDefinition()) { |
| 6961 | // Do nothing. |
| 6962 | } else if (FromEnum->isCompleteDefinition()) { |
| 6963 | ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum, |
| 6964 | ASTNodeImporter::IDK_Basic); |
| 6965 | } else { |
| 6966 | CompleteDecl(ToEnum); |
| 6967 | } |
| 6968 | } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) { |
| 6969 | ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC); |
| 6970 | if (ToClass->getDefinition()) { |
| 6971 | // Do nothing. |
| 6972 | } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) { |
| 6973 | ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass, |
| 6974 | ASTNodeImporter::IDK_Basic); |
| 6975 | } else { |
| 6976 | CompleteDecl(ToClass); |
| 6977 | } |
| 6978 | } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) { |
| 6979 | ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC); |
| 6980 | if (ToProto->getDefinition()) { |
| 6981 | // Do nothing. |
| 6982 | } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) { |
| 6983 | ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto, |
| 6984 | ASTNodeImporter::IDK_Basic); |
| 6985 | } else { |
| 6986 | CompleteDecl(ToProto); |
| 6987 | } |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 6988 | } |
| 6989 | |
| 6990 | return ToDC; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6991 | } |
| 6992 | |
| 6993 | Expr *ASTImporter::Import(Expr *FromE) { |
| 6994 | if (!FromE) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6995 | return nullptr; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6996 | |
| 6997 | return cast_or_null<Expr>(Import(cast<Stmt>(FromE))); |
| 6998 | } |
| 6999 | |
| 7000 | Stmt *ASTImporter::Import(Stmt *FromS) { |
| 7001 | if (!FromS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7002 | return nullptr; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 7003 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 7004 | // Check whether we've already imported this declaration. |
| 7005 | llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS); |
| 7006 | if (Pos != ImportedStmts.end()) |
| 7007 | return Pos->second; |
| 7008 | |
| 7009 | // Import the type |
| 7010 | ASTNodeImporter Importer(*this); |
| 7011 | Stmt *ToS = Importer.Visit(FromS); |
| 7012 | if (!ToS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7013 | return nullptr; |
| 7014 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 7015 | // Record the imported declaration. |
| 7016 | ImportedStmts[FromS] = ToS; |
| 7017 | return ToS; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 7018 | } |
| 7019 | |
| 7020 | NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) { |
| 7021 | if (!FromNNS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7022 | return nullptr; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 7023 | |
Douglas Gregor | 90ebf25 | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 7024 | NestedNameSpecifier *prefix = Import(FromNNS->getPrefix()); |
| 7025 | |
| 7026 | switch (FromNNS->getKind()) { |
| 7027 | case NestedNameSpecifier::Identifier: |
| 7028 | if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) { |
| 7029 | return NestedNameSpecifier::Create(ToContext, prefix, II); |
| 7030 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7031 | return nullptr; |
Douglas Gregor | 90ebf25 | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 7032 | |
| 7033 | case NestedNameSpecifier::Namespace: |
| 7034 | if (NamespaceDecl *NS = |
Aleksei Sidorin | 855086d | 2017-01-23 09:30:36 +0000 | [diff] [blame] | 7035 | cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) { |
Douglas Gregor | 90ebf25 | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 7036 | return NestedNameSpecifier::Create(ToContext, prefix, NS); |
| 7037 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7038 | return nullptr; |
Douglas Gregor | 90ebf25 | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 7039 | |
| 7040 | case NestedNameSpecifier::NamespaceAlias: |
| 7041 | if (NamespaceAliasDecl *NSAD = |
Aleksei Sidorin | 855086d | 2017-01-23 09:30:36 +0000 | [diff] [blame] | 7042 | cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) { |
Douglas Gregor | 90ebf25 | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 7043 | return NestedNameSpecifier::Create(ToContext, prefix, NSAD); |
| 7044 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7045 | return nullptr; |
Douglas Gregor | 90ebf25 | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 7046 | |
| 7047 | case NestedNameSpecifier::Global: |
| 7048 | return NestedNameSpecifier::GlobalSpecifier(ToContext); |
| 7049 | |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 7050 | case NestedNameSpecifier::Super: |
| 7051 | if (CXXRecordDecl *RD = |
Aleksei Sidorin | 855086d | 2017-01-23 09:30:36 +0000 | [diff] [blame] | 7052 | cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) { |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 7053 | return NestedNameSpecifier::SuperSpecifier(ToContext, RD); |
| 7054 | } |
| 7055 | return nullptr; |
| 7056 | |
Douglas Gregor | 90ebf25 | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 7057 | case NestedNameSpecifier::TypeSpec: |
| 7058 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 7059 | QualType T = Import(QualType(FromNNS->getAsType(), 0u)); |
| 7060 | if (!T.isNull()) { |
| 7061 | bool bTemplate = FromNNS->getKind() == |
| 7062 | NestedNameSpecifier::TypeSpecWithTemplate; |
| 7063 | return NestedNameSpecifier::Create(ToContext, prefix, |
| 7064 | bTemplate, T.getTypePtr()); |
| 7065 | } |
| 7066 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7067 | return nullptr; |
Douglas Gregor | 90ebf25 | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 7068 | } |
| 7069 | |
| 7070 | llvm_unreachable("Invalid nested name specifier kind"); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 7071 | } |
| 7072 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 7073 | NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) { |
Aleksei Sidorin | 855086d | 2017-01-23 09:30:36 +0000 | [diff] [blame] | 7074 | // Copied from NestedNameSpecifier mostly. |
| 7075 | SmallVector<NestedNameSpecifierLoc , 8> NestedNames; |
| 7076 | NestedNameSpecifierLoc NNS = FromNNS; |
| 7077 | |
| 7078 | // Push each of the nested-name-specifiers's onto a stack for |
| 7079 | // serialization in reverse order. |
| 7080 | while (NNS) { |
| 7081 | NestedNames.push_back(NNS); |
| 7082 | NNS = NNS.getPrefix(); |
| 7083 | } |
| 7084 | |
| 7085 | NestedNameSpecifierLocBuilder Builder; |
| 7086 | |
| 7087 | while (!NestedNames.empty()) { |
| 7088 | NNS = NestedNames.pop_back_val(); |
| 7089 | NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier()); |
| 7090 | if (!Spec) |
| 7091 | return NestedNameSpecifierLoc(); |
| 7092 | |
| 7093 | NestedNameSpecifier::SpecifierKind Kind = Spec->getKind(); |
| 7094 | switch (Kind) { |
| 7095 | case NestedNameSpecifier::Identifier: |
| 7096 | Builder.Extend(getToContext(), |
| 7097 | Spec->getAsIdentifier(), |
| 7098 | Import(NNS.getLocalBeginLoc()), |
| 7099 | Import(NNS.getLocalEndLoc())); |
| 7100 | break; |
| 7101 | |
| 7102 | case NestedNameSpecifier::Namespace: |
| 7103 | Builder.Extend(getToContext(), |
| 7104 | Spec->getAsNamespace(), |
| 7105 | Import(NNS.getLocalBeginLoc()), |
| 7106 | Import(NNS.getLocalEndLoc())); |
| 7107 | break; |
| 7108 | |
| 7109 | case NestedNameSpecifier::NamespaceAlias: |
| 7110 | Builder.Extend(getToContext(), |
| 7111 | Spec->getAsNamespaceAlias(), |
| 7112 | Import(NNS.getLocalBeginLoc()), |
| 7113 | Import(NNS.getLocalEndLoc())); |
| 7114 | break; |
| 7115 | |
| 7116 | case NestedNameSpecifier::TypeSpec: |
| 7117 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 7118 | TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo( |
| 7119 | QualType(Spec->getAsType(), 0)); |
| 7120 | Builder.Extend(getToContext(), |
| 7121 | Import(NNS.getLocalBeginLoc()), |
| 7122 | TSI->getTypeLoc(), |
| 7123 | Import(NNS.getLocalEndLoc())); |
| 7124 | break; |
| 7125 | } |
| 7126 | |
| 7127 | case NestedNameSpecifier::Global: |
| 7128 | Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc())); |
| 7129 | break; |
| 7130 | |
| 7131 | case NestedNameSpecifier::Super: { |
| 7132 | SourceRange ToRange = Import(NNS.getSourceRange()); |
| 7133 | Builder.MakeSuper(getToContext(), |
| 7134 | Spec->getAsRecordDecl(), |
| 7135 | ToRange.getBegin(), |
| 7136 | ToRange.getEnd()); |
| 7137 | } |
| 7138 | } |
| 7139 | } |
| 7140 | |
| 7141 | return Builder.getWithLocInContext(getToContext()); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 7142 | } |
| 7143 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 7144 | TemplateName ASTImporter::Import(TemplateName From) { |
| 7145 | switch (From.getKind()) { |
| 7146 | case TemplateName::Template: |
| 7147 | if (TemplateDecl *ToTemplate |
| 7148 | = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl()))) |
| 7149 | return TemplateName(ToTemplate); |
| 7150 | |
| 7151 | return TemplateName(); |
| 7152 | |
| 7153 | case TemplateName::OverloadedTemplate: { |
| 7154 | OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate(); |
| 7155 | UnresolvedSet<2> ToTemplates; |
| 7156 | for (OverloadedTemplateStorage::iterator I = FromStorage->begin(), |
| 7157 | E = FromStorage->end(); |
| 7158 | I != E; ++I) { |
| 7159 | if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I))) |
| 7160 | ToTemplates.addDecl(To); |
| 7161 | else |
| 7162 | return TemplateName(); |
| 7163 | } |
| 7164 | return ToContext.getOverloadedTemplateName(ToTemplates.begin(), |
| 7165 | ToTemplates.end()); |
| 7166 | } |
| 7167 | |
| 7168 | case TemplateName::QualifiedTemplate: { |
| 7169 | QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName(); |
| 7170 | NestedNameSpecifier *Qualifier = Import(QTN->getQualifier()); |
| 7171 | if (!Qualifier) |
| 7172 | return TemplateName(); |
| 7173 | |
| 7174 | if (TemplateDecl *ToTemplate |
| 7175 | = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl()))) |
| 7176 | return ToContext.getQualifiedTemplateName(Qualifier, |
| 7177 | QTN->hasTemplateKeyword(), |
| 7178 | ToTemplate); |
| 7179 | |
| 7180 | return TemplateName(); |
| 7181 | } |
| 7182 | |
| 7183 | case TemplateName::DependentTemplate: { |
| 7184 | DependentTemplateName *DTN = From.getAsDependentTemplateName(); |
| 7185 | NestedNameSpecifier *Qualifier = Import(DTN->getQualifier()); |
| 7186 | if (!Qualifier) |
| 7187 | return TemplateName(); |
| 7188 | |
| 7189 | if (DTN->isIdentifier()) { |
| 7190 | return ToContext.getDependentTemplateName(Qualifier, |
| 7191 | Import(DTN->getIdentifier())); |
| 7192 | } |
| 7193 | |
| 7194 | return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator()); |
| 7195 | } |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 7196 | |
| 7197 | case TemplateName::SubstTemplateTemplateParm: { |
| 7198 | SubstTemplateTemplateParmStorage *subst |
| 7199 | = From.getAsSubstTemplateTemplateParm(); |
| 7200 | TemplateTemplateParmDecl *param |
| 7201 | = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter())); |
| 7202 | if (!param) |
| 7203 | return TemplateName(); |
| 7204 | |
| 7205 | TemplateName replacement = Import(subst->getReplacement()); |
| 7206 | if (replacement.isNull()) return TemplateName(); |
| 7207 | |
| 7208 | return ToContext.getSubstTemplateTemplateParm(param, replacement); |
| 7209 | } |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 7210 | |
| 7211 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 7212 | SubstTemplateTemplateParmPackStorage *SubstPack |
| 7213 | = From.getAsSubstTemplateTemplateParmPack(); |
| 7214 | TemplateTemplateParmDecl *Param |
| 7215 | = cast_or_null<TemplateTemplateParmDecl>( |
| 7216 | Import(SubstPack->getParameterPack())); |
| 7217 | if (!Param) |
| 7218 | return TemplateName(); |
| 7219 | |
| 7220 | ASTNodeImporter Importer(*this); |
| 7221 | TemplateArgument ArgPack |
| 7222 | = Importer.ImportTemplateArgument(SubstPack->getArgumentPack()); |
| 7223 | if (ArgPack.isNull()) |
| 7224 | return TemplateName(); |
| 7225 | |
| 7226 | return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack); |
| 7227 | } |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 7228 | } |
| 7229 | |
| 7230 | llvm_unreachable("Invalid template name kind"); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 7231 | } |
| 7232 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 7233 | SourceLocation ASTImporter::Import(SourceLocation FromLoc) { |
| 7234 | if (FromLoc.isInvalid()) |
| 7235 | return SourceLocation(); |
| 7236 | |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 7237 | SourceManager &FromSM = FromContext.getSourceManager(); |
| 7238 | |
Sean Callanan | 24c5fe6 | 2016-11-07 20:42:25 +0000 | [diff] [blame] | 7239 | // For now, map everything down to its file location, so that we |
Chandler Carruth | 2536641 | 2011-07-15 00:04:35 +0000 | [diff] [blame] | 7240 | // don't have to import macro expansions. |
| 7241 | // FIXME: Import macro expansions! |
Sean Callanan | 24c5fe6 | 2016-11-07 20:42:25 +0000 | [diff] [blame] | 7242 | FromLoc = FromSM.getFileLoc(FromLoc); |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 7243 | std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc); |
| 7244 | SourceManager &ToSM = ToContext.getSourceManager(); |
Sean Callanan | 238d897 | 2014-12-10 01:26:39 +0000 | [diff] [blame] | 7245 | FileID ToFileID = Import(Decomposed.first); |
| 7246 | if (ToFileID.isInvalid()) |
| 7247 | return SourceLocation(); |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 7248 | SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID) |
| 7249 | .getLocWithOffset(Decomposed.second); |
| 7250 | return ret; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 7251 | } |
| 7252 | |
| 7253 | SourceRange ASTImporter::Import(SourceRange FromRange) { |
| 7254 | return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd())); |
| 7255 | } |
| 7256 | |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 7257 | FileID ASTImporter::Import(FileID FromID) { |
Sebastian Redl | 99219f1 | 2010-09-30 01:03:06 +0000 | [diff] [blame] | 7258 | llvm::DenseMap<FileID, FileID>::iterator Pos |
| 7259 | = ImportedFileIDs.find(FromID); |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 7260 | if (Pos != ImportedFileIDs.end()) |
| 7261 | return Pos->second; |
| 7262 | |
| 7263 | SourceManager &FromSM = FromContext.getSourceManager(); |
| 7264 | SourceManager &ToSM = ToContext.getSourceManager(); |
| 7265 | const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID); |
Chandler Carruth | 2536641 | 2011-07-15 00:04:35 +0000 | [diff] [blame] | 7266 | assert(FromSLoc.isFile() && "Cannot handle macro expansions yet"); |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 7267 | |
| 7268 | // Include location of this file. |
| 7269 | SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc()); |
| 7270 | |
| 7271 | // Map the FileID for to the "to" source manager. |
| 7272 | FileID ToID; |
| 7273 | const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache(); |
Sean Callanan | 25d34af | 2015-04-30 00:44:21 +0000 | [diff] [blame] | 7274 | if (Cache->OrigEntry && Cache->OrigEntry->getDir()) { |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 7275 | // FIXME: We probably want to use getVirtualFile(), so we don't hit the |
| 7276 | // disk again |
| 7277 | // FIXME: We definitely want to re-use the existing MemoryBuffer, rather |
| 7278 | // than mmap the files several times. |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 7279 | const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName()); |
Sean Callanan | 238d897 | 2014-12-10 01:26:39 +0000 | [diff] [blame] | 7280 | if (!Entry) |
| 7281 | return FileID(); |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 7282 | ToID = ToSM.createFileID(Entry, ToIncludeLoc, |
| 7283 | FromSLoc.getFile().getFileCharacteristic()); |
| 7284 | } else { |
| 7285 | // FIXME: We want to re-use the existing MemoryBuffer! |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 7286 | const llvm::MemoryBuffer * |
| 7287 | FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM); |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 7288 | std::unique_ptr<llvm::MemoryBuffer> ToBuf |
Chris Lattner | 58c7934 | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 7289 | = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(), |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 7290 | FromBuf->getBufferIdentifier()); |
David Blaikie | 50a5f97 | 2014-08-29 07:59:55 +0000 | [diff] [blame] | 7291 | ToID = ToSM.createFileID(std::move(ToBuf), |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 7292 | FromSLoc.getFile().getFileCharacteristic()); |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 7293 | } |
| 7294 | |
| 7295 | |
Sebastian Redl | 99219f1 | 2010-09-30 01:03:06 +0000 | [diff] [blame] | 7296 | ImportedFileIDs[FromID] = ToID; |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 7297 | return ToID; |
| 7298 | } |
| 7299 | |
Sean Callanan | dd2c174 | 2016-05-16 20:48:03 +0000 | [diff] [blame] | 7300 | CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) { |
| 7301 | Expr *ToExpr = Import(From->getInit()); |
| 7302 | if (!ToExpr && From->getInit()) |
| 7303 | return nullptr; |
| 7304 | |
| 7305 | if (From->isBaseInitializer()) { |
| 7306 | TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo()); |
| 7307 | if (!ToTInfo && From->getTypeSourceInfo()) |
| 7308 | return nullptr; |
| 7309 | |
| 7310 | return new (ToContext) CXXCtorInitializer( |
| 7311 | ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()), |
| 7312 | ToExpr, Import(From->getRParenLoc()), |
| 7313 | From->isPackExpansion() ? Import(From->getEllipsisLoc()) |
| 7314 | : SourceLocation()); |
| 7315 | } else if (From->isMemberInitializer()) { |
| 7316 | FieldDecl *ToField = |
| 7317 | llvm::cast_or_null<FieldDecl>(Import(From->getMember())); |
| 7318 | if (!ToField && From->getMember()) |
| 7319 | return nullptr; |
| 7320 | |
| 7321 | return new (ToContext) CXXCtorInitializer( |
| 7322 | ToContext, ToField, Import(From->getMemberLocation()), |
| 7323 | Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc())); |
| 7324 | } else if (From->isIndirectMemberInitializer()) { |
| 7325 | IndirectFieldDecl *ToIField = llvm::cast_or_null<IndirectFieldDecl>( |
| 7326 | Import(From->getIndirectMember())); |
| 7327 | if (!ToIField && From->getIndirectMember()) |
| 7328 | return nullptr; |
| 7329 | |
| 7330 | return new (ToContext) CXXCtorInitializer( |
| 7331 | ToContext, ToIField, Import(From->getMemberLocation()), |
| 7332 | Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc())); |
| 7333 | } else if (From->isDelegatingInitializer()) { |
| 7334 | TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo()); |
| 7335 | if (!ToTInfo && From->getTypeSourceInfo()) |
| 7336 | return nullptr; |
| 7337 | |
| 7338 | return new (ToContext) |
| 7339 | CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()), |
| 7340 | ToExpr, Import(From->getRParenLoc())); |
Sean Callanan | dd2c174 | 2016-05-16 20:48:03 +0000 | [diff] [blame] | 7341 | } else { |
| 7342 | return nullptr; |
| 7343 | } |
| 7344 | } |
| 7345 | |
| 7346 | |
Aleksei Sidorin | a693b37 | 2016-09-28 10:16:56 +0000 | [diff] [blame] | 7347 | CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) { |
| 7348 | auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec); |
| 7349 | if (Pos != ImportedCXXBaseSpecifiers.end()) |
| 7350 | return Pos->second; |
| 7351 | |
| 7352 | CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier( |
| 7353 | Import(BaseSpec->getSourceRange()), |
| 7354 | BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(), |
| 7355 | BaseSpec->getAccessSpecifierAsWritten(), |
| 7356 | Import(BaseSpec->getTypeSourceInfo()), |
| 7357 | Import(BaseSpec->getEllipsisLoc())); |
| 7358 | ImportedCXXBaseSpecifiers[BaseSpec] = Imported; |
| 7359 | return Imported; |
| 7360 | } |
| 7361 | |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 7362 | void ASTImporter::ImportDefinition(Decl *From) { |
| 7363 | Decl *To = Import(From); |
| 7364 | if (!To) |
| 7365 | return; |
| 7366 | |
| 7367 | if (DeclContext *FromDC = cast<DeclContext>(From)) { |
| 7368 | ASTNodeImporter Importer(*this); |
Sean Callanan | 53a6bff | 2011-07-19 22:38:25 +0000 | [diff] [blame] | 7369 | |
| 7370 | if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) { |
| 7371 | if (!ToRecord->getDefinition()) { |
| 7372 | Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord, |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 7373 | ASTNodeImporter::IDK_Everything); |
Sean Callanan | 53a6bff | 2011-07-19 22:38:25 +0000 | [diff] [blame] | 7374 | return; |
| 7375 | } |
| 7376 | } |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 7377 | |
| 7378 | if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) { |
| 7379 | if (!ToEnum->getDefinition()) { |
| 7380 | Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 7381 | ASTNodeImporter::IDK_Everything); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 7382 | return; |
| 7383 | } |
| 7384 | } |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 7385 | |
| 7386 | if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) { |
| 7387 | if (!ToIFace->getDefinition()) { |
| 7388 | Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 7389 | ASTNodeImporter::IDK_Everything); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 7390 | return; |
| 7391 | } |
| 7392 | } |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 7393 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 7394 | if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) { |
| 7395 | if (!ToProto->getDefinition()) { |
| 7396 | Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 7397 | ASTNodeImporter::IDK_Everything); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 7398 | return; |
| 7399 | } |
| 7400 | } |
| 7401 | |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 7402 | Importer.ImportDeclContext(FromDC, true); |
| 7403 | } |
| 7404 | } |
| 7405 | |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 7406 | DeclarationName ASTImporter::Import(DeclarationName FromName) { |
| 7407 | if (!FromName) |
| 7408 | return DeclarationName(); |
| 7409 | |
| 7410 | switch (FromName.getNameKind()) { |
| 7411 | case DeclarationName::Identifier: |
| 7412 | return Import(FromName.getAsIdentifierInfo()); |
| 7413 | |
| 7414 | case DeclarationName::ObjCZeroArgSelector: |
| 7415 | case DeclarationName::ObjCOneArgSelector: |
| 7416 | case DeclarationName::ObjCMultiArgSelector: |
| 7417 | return Import(FromName.getObjCSelector()); |
| 7418 | |
| 7419 | case DeclarationName::CXXConstructorName: { |
| 7420 | QualType T = Import(FromName.getCXXNameType()); |
| 7421 | if (T.isNull()) |
| 7422 | return DeclarationName(); |
| 7423 | |
| 7424 | return ToContext.DeclarationNames.getCXXConstructorName( |
| 7425 | ToContext.getCanonicalType(T)); |
| 7426 | } |
| 7427 | |
| 7428 | case DeclarationName::CXXDestructorName: { |
| 7429 | QualType T = Import(FromName.getCXXNameType()); |
| 7430 | if (T.isNull()) |
| 7431 | return DeclarationName(); |
| 7432 | |
| 7433 | return ToContext.DeclarationNames.getCXXDestructorName( |
| 7434 | ToContext.getCanonicalType(T)); |
| 7435 | } |
| 7436 | |
| 7437 | case DeclarationName::CXXConversionFunctionName: { |
| 7438 | QualType T = Import(FromName.getCXXNameType()); |
| 7439 | if (T.isNull()) |
| 7440 | return DeclarationName(); |
| 7441 | |
| 7442 | return ToContext.DeclarationNames.getCXXConversionFunctionName( |
| 7443 | ToContext.getCanonicalType(T)); |
| 7444 | } |
| 7445 | |
| 7446 | case DeclarationName::CXXOperatorName: |
| 7447 | return ToContext.DeclarationNames.getCXXOperatorName( |
| 7448 | FromName.getCXXOverloadedOperator()); |
| 7449 | |
| 7450 | case DeclarationName::CXXLiteralOperatorName: |
| 7451 | return ToContext.DeclarationNames.getCXXLiteralOperatorName( |
| 7452 | Import(FromName.getCXXLiteralIdentifier())); |
| 7453 | |
| 7454 | case DeclarationName::CXXUsingDirective: |
| 7455 | // FIXME: STATICS! |
| 7456 | return DeclarationName::getUsingDirectiveName(); |
| 7457 | } |
| 7458 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 7459 | llvm_unreachable("Invalid DeclarationName Kind!"); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 7460 | } |
| 7461 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 7462 | IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 7463 | if (!FromId) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7464 | return nullptr; |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 7465 | |
Sean Callanan | f94ef1d | 2016-05-14 06:11:19 +0000 | [diff] [blame] | 7466 | IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName()); |
| 7467 | |
| 7468 | if (!ToId->getBuiltinID() && FromId->getBuiltinID()) |
| 7469 | ToId->setBuiltinID(FromId->getBuiltinID()); |
| 7470 | |
| 7471 | return ToId; |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 7472 | } |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 7473 | |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 7474 | Selector ASTImporter::Import(Selector FromSel) { |
| 7475 | if (FromSel.isNull()) |
| 7476 | return Selector(); |
| 7477 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7478 | SmallVector<IdentifierInfo *, 4> Idents; |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 7479 | Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0))); |
| 7480 | for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I) |
| 7481 | Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I))); |
| 7482 | return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data()); |
| 7483 | } |
| 7484 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 7485 | DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name, |
| 7486 | DeclContext *DC, |
| 7487 | unsigned IDNS, |
| 7488 | NamedDecl **Decls, |
| 7489 | unsigned NumDecls) { |
| 7490 | return Name; |
| 7491 | } |
| 7492 | |
| 7493 | DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) { |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 7494 | if (LastDiagFromFrom) |
| 7495 | ToContext.getDiagnostics().notePriorDiagnosticFrom( |
| 7496 | FromContext.getDiagnostics()); |
| 7497 | LastDiagFromFrom = false; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 7498 | return ToContext.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 7499 | } |
| 7500 | |
| 7501 | DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) { |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 7502 | if (!LastDiagFromFrom) |
| 7503 | FromContext.getDiagnostics().notePriorDiagnosticFrom( |
| 7504 | ToContext.getDiagnostics()); |
| 7505 | LastDiagFromFrom = true; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 7506 | return FromContext.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 7507 | } |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 7508 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 7509 | void ASTImporter::CompleteDecl (Decl *D) { |
| 7510 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 7511 | if (!ID->getDefinition()) |
| 7512 | ID->startDefinition(); |
| 7513 | } |
| 7514 | else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
| 7515 | if (!PD->getDefinition()) |
| 7516 | PD->startDefinition(); |
| 7517 | } |
| 7518 | else if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 7519 | if (!TD->getDefinition() && !TD->isBeingDefined()) { |
| 7520 | TD->startDefinition(); |
| 7521 | TD->setCompleteDefinition(true); |
| 7522 | } |
| 7523 | } |
| 7524 | else { |
| 7525 | assert (0 && "CompleteDecl called on a Decl that can't be completed"); |
| 7526 | } |
| 7527 | } |
| 7528 | |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 7529 | Decl *ASTImporter::Imported(Decl *From, Decl *To) { |
Sean Callanan | 8bca996 | 2016-03-28 21:43:01 +0000 | [diff] [blame] | 7530 | if (From->hasAttrs()) { |
| 7531 | for (Attr *FromAttr : From->getAttrs()) |
| 7532 | To->addAttr(FromAttr->clone(To->getASTContext())); |
| 7533 | } |
| 7534 | if (From->isUsed()) { |
| 7535 | To->setIsUsed(); |
| 7536 | } |
Sean Callanan | dd2c174 | 2016-05-16 20:48:03 +0000 | [diff] [blame] | 7537 | if (From->isImplicit()) { |
| 7538 | To->setImplicit(); |
| 7539 | } |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 7540 | ImportedDecls[From] = To; |
| 7541 | return To; |
Daniel Dunbar | 9ced542 | 2010-02-13 20:24:39 +0000 | [diff] [blame] | 7542 | } |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 7543 | |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 7544 | bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To, |
| 7545 | bool Complain) { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 7546 | llvm::DenseMap<const Type *, const Type *>::iterator Pos |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 7547 | = ImportedTypes.find(From.getTypePtr()); |
| 7548 | if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To)) |
| 7549 | return true; |
| 7550 | |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 7551 | StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls, |
| 7552 | false, Complain); |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 7553 | return Ctx.IsStructurallyEquivalent(From, To); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 7554 | } |