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; |
| 32 | |
| 33 | public: |
| 34 | explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { } |
| 35 | |
| 36 | using TypeVisitor<ASTNodeImporter, QualType>::Visit; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 37 | using DeclVisitor<ASTNodeImporter, Decl *>::Visit; |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 38 | using StmtVisitor<ASTNodeImporter, Stmt *>::Visit; |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 39 | |
| 40 | // Importing types |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 41 | QualType VisitType(const Type *T); |
| 42 | QualType VisitBuiltinType(const BuiltinType *T); |
| 43 | QualType VisitComplexType(const ComplexType *T); |
| 44 | QualType VisitPointerType(const PointerType *T); |
| 45 | QualType VisitBlockPointerType(const BlockPointerType *T); |
| 46 | QualType VisitLValueReferenceType(const LValueReferenceType *T); |
| 47 | QualType VisitRValueReferenceType(const RValueReferenceType *T); |
| 48 | QualType VisitMemberPointerType(const MemberPointerType *T); |
| 49 | QualType VisitConstantArrayType(const ConstantArrayType *T); |
| 50 | QualType VisitIncompleteArrayType(const IncompleteArrayType *T); |
| 51 | QualType VisitVariableArrayType(const VariableArrayType *T); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 52 | // FIXME: DependentSizedArrayType |
| 53 | // FIXME: DependentSizedExtVectorType |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 54 | QualType VisitVectorType(const VectorType *T); |
| 55 | QualType VisitExtVectorType(const ExtVectorType *T); |
| 56 | QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T); |
| 57 | QualType VisitFunctionProtoType(const FunctionProtoType *T); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 58 | // FIXME: UnresolvedUsingType |
Sean Callanan | da6df8a | 2011-08-11 16:56:07 +0000 | [diff] [blame] | 59 | QualType VisitParenType(const ParenType *T); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 60 | QualType VisitTypedefType(const TypedefType *T); |
| 61 | QualType VisitTypeOfExprType(const TypeOfExprType *T); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 62 | // FIXME: DependentTypeOfExprType |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 63 | QualType VisitTypeOfType(const TypeOfType *T); |
| 64 | QualType VisitDecltypeType(const DecltypeType *T); |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 65 | QualType VisitUnaryTransformType(const UnaryTransformType *T); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 66 | QualType VisitAutoType(const AutoType *T); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 67 | // FIXME: DependentDecltypeType |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 68 | QualType VisitRecordType(const RecordType *T); |
| 69 | QualType VisitEnumType(const EnumType *T); |
Sean Callanan | 72fe085 | 2015-04-02 23:50:08 +0000 | [diff] [blame] | 70 | QualType VisitAttributedType(const AttributedType *T); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 71 | // FIXME: TemplateTypeParmType |
| 72 | // FIXME: SubstTemplateTypeParmType |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 73 | QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T); |
| 74 | QualType VisitElaboratedType(const ElaboratedType *T); |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 75 | // FIXME: DependentNameType |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 76 | // FIXME: DependentTemplateSpecializationType |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 77 | QualType VisitObjCInterfaceType(const ObjCInterfaceType *T); |
| 78 | QualType VisitObjCObjectType(const ObjCObjectType *T); |
| 79 | QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 80 | |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 81 | // Importing declarations |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 82 | bool ImportDeclParts(NamedDecl *D, DeclContext *&DC, |
| 83 | DeclContext *&LexicalDC, DeclarationName &Name, |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 84 | NamedDecl *&ToD, SourceLocation &Loc); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 85 | void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 86 | void ImportDeclarationNameLoc(const DeclarationNameInfo &From, |
| 87 | DeclarationNameInfo& To); |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 88 | void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false); |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 89 | |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 90 | /// \brief What we should import from the definition. |
| 91 | enum ImportDefinitionKind { |
| 92 | /// \brief Import the default subset of the definition, which might be |
| 93 | /// nothing (if minimal import is set) or might be everything (if minimal |
| 94 | /// import is not set). |
| 95 | IDK_Default, |
| 96 | /// \brief Import everything. |
| 97 | IDK_Everything, |
| 98 | /// \brief Import only the bare bones needed to establish a valid |
| 99 | /// DeclContext. |
| 100 | IDK_Basic |
| 101 | }; |
| 102 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 103 | bool shouldForceImportDeclContext(ImportDefinitionKind IDK) { |
| 104 | return IDK == IDK_Everything || |
| 105 | (IDK == IDK_Default && !Importer.isMinimalImport()); |
| 106 | } |
| 107 | |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 108 | bool ImportDefinition(RecordDecl *From, RecordDecl *To, |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 109 | ImportDefinitionKind Kind = IDK_Default); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 110 | bool ImportDefinition(VarDecl *From, VarDecl *To, |
| 111 | ImportDefinitionKind Kind = IDK_Default); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 112 | bool ImportDefinition(EnumDecl *From, EnumDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 113 | ImportDefinitionKind Kind = IDK_Default); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 114 | bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 115 | ImportDefinitionKind Kind = IDK_Default); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 116 | bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 117 | ImportDefinitionKind Kind = IDK_Default); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 118 | TemplateParameterList *ImportTemplateParameterList( |
| 119 | TemplateParameterList *Params); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 120 | TemplateArgument ImportTemplateArgument(const TemplateArgument &From); |
| 121 | bool ImportTemplateArguments(const TemplateArgument *FromArgs, |
| 122 | unsigned NumFromArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 123 | SmallVectorImpl<TemplateArgument> &ToArgs); |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 124 | bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord, |
| 125 | bool Complain = true); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 126 | bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar, |
| 127 | bool Complain = true); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 128 | bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord); |
Douglas Gregor | 9115508 | 2012-11-14 22:29:20 +0000 | [diff] [blame] | 129 | bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 130 | bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 131 | bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To); |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 132 | Decl *VisitDecl(Decl *D); |
Argyrios Kyrtzidis | 544ea71 | 2016-02-18 23:08:36 +0000 | [diff] [blame] | 133 | Decl *VisitAccessSpecDecl(AccessSpecDecl *D); |
Sean Callanan | 6519827 | 2011-11-17 23:20:56 +0000 | [diff] [blame] | 134 | Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D); |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 135 | Decl *VisitNamespaceDecl(NamespaceDecl *D); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 136 | Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias); |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 137 | Decl *VisitTypedefDecl(TypedefDecl *D); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 138 | Decl *VisitTypeAliasDecl(TypeAliasDecl *D); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 139 | Decl *VisitEnumDecl(EnumDecl *D); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 140 | Decl *VisitRecordDecl(RecordDecl *D); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 141 | Decl *VisitEnumConstantDecl(EnumConstantDecl *D); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 142 | Decl *VisitFunctionDecl(FunctionDecl *D); |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 143 | Decl *VisitCXXMethodDecl(CXXMethodDecl *D); |
| 144 | Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D); |
| 145 | Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D); |
| 146 | Decl *VisitCXXConversionDecl(CXXConversionDecl *D); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 147 | Decl *VisitFieldDecl(FieldDecl *D); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 148 | Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D); |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 149 | Decl *VisitObjCIvarDecl(ObjCIvarDecl *D); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 150 | Decl *VisitVarDecl(VarDecl *D); |
Douglas Gregor | 8b228d7 | 2010-02-17 21:22:52 +0000 | [diff] [blame] | 151 | Decl *VisitImplicitParamDecl(ImplicitParamDecl *D); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 152 | Decl *VisitParmVarDecl(ParmVarDecl *D); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 153 | Decl *VisitObjCMethodDecl(ObjCMethodDecl *D); |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 154 | Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 155 | Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D); |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 156 | Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D); |
Sean Callanan | 0aae041 | 2014-12-10 00:00:37 +0000 | [diff] [blame] | 157 | Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D); |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 158 | |
| 159 | ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list); |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 160 | Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 161 | Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 162 | Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 163 | Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 164 | Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 165 | Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); |
| 166 | Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); |
| 167 | Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); |
| 168 | Decl *VisitClassTemplateDecl(ClassTemplateDecl *D); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 169 | Decl *VisitClassTemplateSpecializationDecl( |
| 170 | ClassTemplateSpecializationDecl *D); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 171 | Decl *VisitVarTemplateDecl(VarTemplateDecl *D); |
| 172 | Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D); |
| 173 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 174 | // Importing statements |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 175 | DeclGroupRef ImportDeclGroup(DeclGroupRef DG); |
| 176 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 177 | Stmt *VisitStmt(Stmt *S); |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 178 | Stmt *VisitDeclStmt(DeclStmt *S); |
| 179 | Stmt *VisitNullStmt(NullStmt *S); |
| 180 | Stmt *VisitCompoundStmt(CompoundStmt *S); |
| 181 | Stmt *VisitCaseStmt(CaseStmt *S); |
| 182 | Stmt *VisitDefaultStmt(DefaultStmt *S); |
| 183 | Stmt *VisitLabelStmt(LabelStmt *S); |
| 184 | Stmt *VisitAttributedStmt(AttributedStmt *S); |
| 185 | Stmt *VisitIfStmt(IfStmt *S); |
| 186 | Stmt *VisitSwitchStmt(SwitchStmt *S); |
| 187 | Stmt *VisitWhileStmt(WhileStmt *S); |
| 188 | Stmt *VisitDoStmt(DoStmt *S); |
| 189 | Stmt *VisitForStmt(ForStmt *S); |
| 190 | Stmt *VisitGotoStmt(GotoStmt *S); |
| 191 | Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S); |
| 192 | Stmt *VisitContinueStmt(ContinueStmt *S); |
| 193 | Stmt *VisitBreakStmt(BreakStmt *S); |
| 194 | Stmt *VisitReturnStmt(ReturnStmt *S); |
| 195 | // FIXME: GCCAsmStmt |
| 196 | // FIXME: MSAsmStmt |
| 197 | // FIXME: SEHExceptStmt |
| 198 | // FIXME: SEHFinallyStmt |
| 199 | // FIXME: SEHTryStmt |
| 200 | // FIXME: SEHLeaveStmt |
| 201 | // FIXME: CapturedStmt |
| 202 | Stmt *VisitCXXCatchStmt(CXXCatchStmt *S); |
| 203 | Stmt *VisitCXXTryStmt(CXXTryStmt *S); |
| 204 | Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S); |
| 205 | // FIXME: MSDependentExistsStmt |
| 206 | Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S); |
| 207 | Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S); |
| 208 | Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S); |
| 209 | Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S); |
| 210 | Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
| 211 | Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S); |
| 212 | Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S); |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 213 | |
| 214 | // Importing expressions |
| 215 | Expr *VisitExpr(Expr *E); |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 216 | Expr *VisitDeclRefExpr(DeclRefExpr *E); |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 217 | Expr *VisitIntegerLiteral(IntegerLiteral *E); |
Douglas Gregor | 623421d | 2010-02-18 02:21:22 +0000 | [diff] [blame] | 218 | Expr *VisitCharacterLiteral(CharacterLiteral *E); |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 219 | Expr *VisitParenExpr(ParenExpr *E); |
| 220 | Expr *VisitUnaryOperator(UnaryOperator *E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 221 | Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 222 | Expr *VisitBinaryOperator(BinaryOperator *E); |
| 223 | Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 224 | Expr *VisitImplicitCastExpr(ImplicitCastExpr *E); |
Douglas Gregor | 5481d32 | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 225 | Expr *VisitCStyleCastExpr(CStyleCastExpr *E); |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 226 | Expr *VisitCXXConstructExpr(CXXConstructExpr *E); |
| 227 | Expr *VisitMemberExpr(MemberExpr *E); |
| 228 | Expr *VisitCallExpr(CallExpr *E); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 229 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 230 | } |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 231 | using namespace clang; |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 232 | |
| 233 | //---------------------------------------------------------------------------- |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 234 | // Structural Equivalence |
| 235 | //---------------------------------------------------------------------------- |
| 236 | |
| 237 | namespace { |
| 238 | struct StructuralEquivalenceContext { |
| 239 | /// \brief AST contexts for which we are checking structural equivalence. |
| 240 | ASTContext &C1, &C2; |
| 241 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 242 | /// \brief The set of "tentative" equivalences between two canonical |
| 243 | /// declarations, mapping from a declaration in the first context to the |
| 244 | /// declaration in the second context that we believe to be equivalent. |
| 245 | llvm::DenseMap<Decl *, Decl *> TentativeEquivalences; |
| 246 | |
| 247 | /// \brief Queue of declarations in the first context whose equivalence |
| 248 | /// with a declaration in the second context still needs to be verified. |
| 249 | std::deque<Decl *> DeclsToCheck; |
| 250 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 251 | /// \brief Declaration (from, to) pairs that are known not to be equivalent |
| 252 | /// (which we have already complained about). |
| 253 | llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls; |
| 254 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 255 | /// \brief Whether we're being strict about the spelling of types when |
| 256 | /// unifying two types. |
| 257 | bool StrictTypeSpelling; |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 258 | |
| 259 | /// \brief Whether to complain about failures. |
| 260 | bool Complain; |
| 261 | |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 262 | /// \brief \c true if the last diagnostic came from C2. |
| 263 | bool LastDiagFromC2; |
| 264 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 265 | StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2, |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 266 | llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls, |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 267 | bool StrictTypeSpelling = false, |
| 268 | bool Complain = true) |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 269 | : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls), |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 270 | StrictTypeSpelling(StrictTypeSpelling), Complain(Complain), |
| 271 | LastDiagFromC2(false) {} |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 272 | |
| 273 | /// \brief Determine whether the two declarations are structurally |
| 274 | /// equivalent. |
| 275 | bool IsStructurallyEquivalent(Decl *D1, Decl *D2); |
| 276 | |
| 277 | /// \brief Determine whether the two types are structurally equivalent. |
| 278 | bool IsStructurallyEquivalent(QualType T1, QualType T2); |
| 279 | |
| 280 | private: |
| 281 | /// \brief Finish checking all of the structural equivalences. |
| 282 | /// |
| 283 | /// \returns true if an error occurred, false otherwise. |
| 284 | bool Finish(); |
| 285 | |
| 286 | public: |
| 287 | DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 288 | assert(Complain && "Not allowed to complain"); |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 289 | if (LastDiagFromC2) |
| 290 | C1.getDiagnostics().notePriorDiagnosticFrom(C2.getDiagnostics()); |
| 291 | LastDiagFromC2 = false; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 292 | return C1.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 296 | assert(Complain && "Not allowed to complain"); |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 297 | if (!LastDiagFromC2) |
| 298 | C2.getDiagnostics().notePriorDiagnosticFrom(C1.getDiagnostics()); |
| 299 | LastDiagFromC2 = true; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 300 | return C2.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 301 | } |
| 302 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 303 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 304 | |
| 305 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 306 | QualType T1, QualType T2); |
| 307 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 308 | Decl *D1, Decl *D2); |
| 309 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 310 | /// \brief Determine structural equivalence of two expressions. |
| 311 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 312 | Expr *E1, Expr *E2) { |
| 313 | if (!E1 || !E2) |
| 314 | return E1 == E2; |
| 315 | |
| 316 | // FIXME: Actually perform a structural comparison! |
| 317 | return true; |
| 318 | } |
| 319 | |
| 320 | /// \brief Determine whether two identifiers are equivalent. |
| 321 | static bool IsStructurallyEquivalent(const IdentifierInfo *Name1, |
| 322 | const IdentifierInfo *Name2) { |
| 323 | if (!Name1 || !Name2) |
| 324 | return Name1 == Name2; |
| 325 | |
| 326 | return Name1->getName() == Name2->getName(); |
| 327 | } |
| 328 | |
| 329 | /// \brief Determine whether two nested-name-specifiers are equivalent. |
| 330 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 331 | NestedNameSpecifier *NNS1, |
| 332 | NestedNameSpecifier *NNS2) { |
| 333 | // FIXME: Implement! |
| 334 | return true; |
| 335 | } |
| 336 | |
| 337 | /// \brief Determine whether two template arguments are equivalent. |
| 338 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 339 | const TemplateArgument &Arg1, |
| 340 | const TemplateArgument &Arg2) { |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 341 | if (Arg1.getKind() != Arg2.getKind()) |
| 342 | return false; |
| 343 | |
| 344 | switch (Arg1.getKind()) { |
| 345 | case TemplateArgument::Null: |
| 346 | return true; |
| 347 | |
| 348 | case TemplateArgument::Type: |
| 349 | return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType()); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 350 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 351 | case TemplateArgument::Integral: |
| 352 | if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(), |
| 353 | Arg2.getIntegralType())) |
| 354 | return false; |
| 355 | |
Eric Christopher | 6dcc376 | 2012-07-15 00:23:57 +0000 | [diff] [blame] | 356 | return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral()); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 357 | |
| 358 | case TemplateArgument::Declaration: |
| 359 | return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl()); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 360 | |
| 361 | case TemplateArgument::NullPtr: |
| 362 | return true; // FIXME: Is this correct? |
| 363 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 364 | case TemplateArgument::Template: |
| 365 | return IsStructurallyEquivalent(Context, |
| 366 | Arg1.getAsTemplate(), |
| 367 | Arg2.getAsTemplate()); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 368 | |
| 369 | case TemplateArgument::TemplateExpansion: |
| 370 | return IsStructurallyEquivalent(Context, |
| 371 | Arg1.getAsTemplateOrTemplatePattern(), |
| 372 | Arg2.getAsTemplateOrTemplatePattern()); |
| 373 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 374 | case TemplateArgument::Expression: |
| 375 | return IsStructurallyEquivalent(Context, |
| 376 | Arg1.getAsExpr(), Arg2.getAsExpr()); |
| 377 | |
| 378 | case TemplateArgument::Pack: |
| 379 | if (Arg1.pack_size() != Arg2.pack_size()) |
| 380 | return false; |
| 381 | |
| 382 | for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I) |
| 383 | if (!IsStructurallyEquivalent(Context, |
| 384 | Arg1.pack_begin()[I], |
| 385 | Arg2.pack_begin()[I])) |
| 386 | return false; |
| 387 | |
| 388 | return true; |
| 389 | } |
| 390 | |
| 391 | llvm_unreachable("Invalid template argument kind"); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /// \brief Determine structural equivalence for the common part of array |
| 395 | /// types. |
| 396 | static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 397 | const ArrayType *Array1, |
| 398 | const ArrayType *Array2) { |
| 399 | if (!IsStructurallyEquivalent(Context, |
| 400 | Array1->getElementType(), |
| 401 | Array2->getElementType())) |
| 402 | return false; |
| 403 | if (Array1->getSizeModifier() != Array2->getSizeModifier()) |
| 404 | return false; |
| 405 | if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers()) |
| 406 | return false; |
| 407 | |
| 408 | return true; |
| 409 | } |
| 410 | |
| 411 | /// \brief Determine structural equivalence of two types. |
| 412 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 413 | QualType T1, QualType T2) { |
| 414 | if (T1.isNull() || T2.isNull()) |
| 415 | return T1.isNull() && T2.isNull(); |
| 416 | |
| 417 | if (!Context.StrictTypeSpelling) { |
| 418 | // We aren't being strict about token-to-token equivalence of types, |
| 419 | // so map down to the canonical type. |
| 420 | T1 = Context.C1.getCanonicalType(T1); |
| 421 | T2 = Context.C2.getCanonicalType(T2); |
| 422 | } |
| 423 | |
| 424 | if (T1.getQualifiers() != T2.getQualifiers()) |
| 425 | return false; |
| 426 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 427 | Type::TypeClass TC = T1->getTypeClass(); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 428 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 429 | if (T1->getTypeClass() != T2->getTypeClass()) { |
| 430 | // Compare function types with prototypes vs. without prototypes as if |
| 431 | // both did not have prototypes. |
| 432 | if (T1->getTypeClass() == Type::FunctionProto && |
| 433 | T2->getTypeClass() == Type::FunctionNoProto) |
| 434 | TC = Type::FunctionNoProto; |
| 435 | else if (T1->getTypeClass() == Type::FunctionNoProto && |
| 436 | T2->getTypeClass() == Type::FunctionProto) |
| 437 | TC = Type::FunctionNoProto; |
| 438 | else |
| 439 | return false; |
| 440 | } |
| 441 | |
| 442 | switch (TC) { |
| 443 | case Type::Builtin: |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 444 | // FIXME: Deal with Char_S/Char_U. |
| 445 | if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind()) |
| 446 | return false; |
| 447 | break; |
| 448 | |
| 449 | case Type::Complex: |
| 450 | if (!IsStructurallyEquivalent(Context, |
| 451 | cast<ComplexType>(T1)->getElementType(), |
| 452 | cast<ComplexType>(T2)->getElementType())) |
| 453 | return false; |
| 454 | break; |
| 455 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 456 | case Type::Adjusted: |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 457 | case Type::Decayed: |
| 458 | if (!IsStructurallyEquivalent(Context, |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 459 | cast<AdjustedType>(T1)->getOriginalType(), |
| 460 | cast<AdjustedType>(T2)->getOriginalType())) |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 461 | return false; |
| 462 | break; |
| 463 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 464 | case Type::Pointer: |
| 465 | if (!IsStructurallyEquivalent(Context, |
| 466 | cast<PointerType>(T1)->getPointeeType(), |
| 467 | cast<PointerType>(T2)->getPointeeType())) |
| 468 | return false; |
| 469 | break; |
| 470 | |
| 471 | case Type::BlockPointer: |
| 472 | if (!IsStructurallyEquivalent(Context, |
| 473 | cast<BlockPointerType>(T1)->getPointeeType(), |
| 474 | cast<BlockPointerType>(T2)->getPointeeType())) |
| 475 | return false; |
| 476 | break; |
| 477 | |
| 478 | case Type::LValueReference: |
| 479 | case Type::RValueReference: { |
| 480 | const ReferenceType *Ref1 = cast<ReferenceType>(T1); |
| 481 | const ReferenceType *Ref2 = cast<ReferenceType>(T2); |
| 482 | if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue()) |
| 483 | return false; |
| 484 | if (Ref1->isInnerRef() != Ref2->isInnerRef()) |
| 485 | return false; |
| 486 | if (!IsStructurallyEquivalent(Context, |
| 487 | Ref1->getPointeeTypeAsWritten(), |
| 488 | Ref2->getPointeeTypeAsWritten())) |
| 489 | return false; |
| 490 | break; |
| 491 | } |
| 492 | |
| 493 | case Type::MemberPointer: { |
| 494 | const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1); |
| 495 | const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2); |
| 496 | if (!IsStructurallyEquivalent(Context, |
| 497 | MemPtr1->getPointeeType(), |
| 498 | MemPtr2->getPointeeType())) |
| 499 | return false; |
| 500 | if (!IsStructurallyEquivalent(Context, |
| 501 | QualType(MemPtr1->getClass(), 0), |
| 502 | QualType(MemPtr2->getClass(), 0))) |
| 503 | return false; |
| 504 | break; |
| 505 | } |
| 506 | |
| 507 | case Type::ConstantArray: { |
| 508 | const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1); |
| 509 | const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2); |
Eric Christopher | 6dcc376 | 2012-07-15 00:23:57 +0000 | [diff] [blame] | 510 | if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 511 | return false; |
| 512 | |
| 513 | if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) |
| 514 | return false; |
| 515 | break; |
| 516 | } |
| 517 | |
| 518 | case Type::IncompleteArray: |
| 519 | if (!IsArrayStructurallyEquivalent(Context, |
| 520 | cast<ArrayType>(T1), |
| 521 | cast<ArrayType>(T2))) |
| 522 | return false; |
| 523 | break; |
| 524 | |
| 525 | case Type::VariableArray: { |
| 526 | const VariableArrayType *Array1 = cast<VariableArrayType>(T1); |
| 527 | const VariableArrayType *Array2 = cast<VariableArrayType>(T2); |
| 528 | if (!IsStructurallyEquivalent(Context, |
| 529 | Array1->getSizeExpr(), Array2->getSizeExpr())) |
| 530 | return false; |
| 531 | |
| 532 | if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) |
| 533 | return false; |
| 534 | |
| 535 | break; |
| 536 | } |
| 537 | |
| 538 | case Type::DependentSizedArray: { |
| 539 | const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1); |
| 540 | const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2); |
| 541 | if (!IsStructurallyEquivalent(Context, |
| 542 | Array1->getSizeExpr(), Array2->getSizeExpr())) |
| 543 | return false; |
| 544 | |
| 545 | if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) |
| 546 | return false; |
| 547 | |
| 548 | break; |
| 549 | } |
| 550 | |
| 551 | case Type::DependentSizedExtVector: { |
| 552 | const DependentSizedExtVectorType *Vec1 |
| 553 | = cast<DependentSizedExtVectorType>(T1); |
| 554 | const DependentSizedExtVectorType *Vec2 |
| 555 | = cast<DependentSizedExtVectorType>(T2); |
| 556 | if (!IsStructurallyEquivalent(Context, |
| 557 | Vec1->getSizeExpr(), Vec2->getSizeExpr())) |
| 558 | return false; |
| 559 | if (!IsStructurallyEquivalent(Context, |
| 560 | Vec1->getElementType(), |
| 561 | Vec2->getElementType())) |
| 562 | return false; |
| 563 | break; |
| 564 | } |
| 565 | |
| 566 | case Type::Vector: |
| 567 | case Type::ExtVector: { |
| 568 | const VectorType *Vec1 = cast<VectorType>(T1); |
| 569 | const VectorType *Vec2 = cast<VectorType>(T2); |
| 570 | if (!IsStructurallyEquivalent(Context, |
| 571 | Vec1->getElementType(), |
| 572 | Vec2->getElementType())) |
| 573 | return false; |
| 574 | if (Vec1->getNumElements() != Vec2->getNumElements()) |
| 575 | return false; |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 576 | if (Vec1->getVectorKind() != Vec2->getVectorKind()) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 577 | return false; |
Douglas Gregor | 01cc437 | 2010-02-19 01:36:36 +0000 | [diff] [blame] | 578 | break; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | case Type::FunctionProto: { |
| 582 | const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1); |
| 583 | const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 584 | if (Proto1->getNumParams() != Proto2->getNumParams()) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 585 | return false; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 586 | for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) { |
| 587 | if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I), |
| 588 | Proto2->getParamType(I))) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 589 | return false; |
| 590 | } |
| 591 | if (Proto1->isVariadic() != Proto2->isVariadic()) |
| 592 | return false; |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 593 | if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType()) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 594 | return false; |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 595 | if (Proto1->getExceptionSpecType() == EST_Dynamic) { |
| 596 | if (Proto1->getNumExceptions() != Proto2->getNumExceptions()) |
| 597 | return false; |
| 598 | for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) { |
| 599 | if (!IsStructurallyEquivalent(Context, |
| 600 | Proto1->getExceptionType(I), |
| 601 | Proto2->getExceptionType(I))) |
| 602 | return false; |
| 603 | } |
| 604 | } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 605 | if (!IsStructurallyEquivalent(Context, |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 606 | Proto1->getNoexceptExpr(), |
| 607 | Proto2->getNoexceptExpr())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 608 | return false; |
| 609 | } |
| 610 | if (Proto1->getTypeQuals() != Proto2->getTypeQuals()) |
| 611 | return false; |
| 612 | |
| 613 | // Fall through to check the bits common with FunctionNoProtoType. |
| 614 | } |
| 615 | |
| 616 | case Type::FunctionNoProto: { |
| 617 | const FunctionType *Function1 = cast<FunctionType>(T1); |
| 618 | const FunctionType *Function2 = cast<FunctionType>(T2); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 619 | if (!IsStructurallyEquivalent(Context, Function1->getReturnType(), |
| 620 | Function2->getReturnType())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 621 | return false; |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 622 | if (Function1->getExtInfo() != Function2->getExtInfo()) |
| 623 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 624 | break; |
| 625 | } |
| 626 | |
| 627 | case Type::UnresolvedUsing: |
| 628 | if (!IsStructurallyEquivalent(Context, |
| 629 | cast<UnresolvedUsingType>(T1)->getDecl(), |
| 630 | cast<UnresolvedUsingType>(T2)->getDecl())) |
| 631 | return false; |
| 632 | |
| 633 | break; |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 634 | |
| 635 | case Type::Attributed: |
| 636 | if (!IsStructurallyEquivalent(Context, |
| 637 | cast<AttributedType>(T1)->getModifiedType(), |
| 638 | cast<AttributedType>(T2)->getModifiedType())) |
| 639 | return false; |
| 640 | if (!IsStructurallyEquivalent(Context, |
| 641 | cast<AttributedType>(T1)->getEquivalentType(), |
| 642 | cast<AttributedType>(T2)->getEquivalentType())) |
| 643 | return false; |
| 644 | break; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 645 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 646 | case Type::Paren: |
| 647 | if (!IsStructurallyEquivalent(Context, |
| 648 | cast<ParenType>(T1)->getInnerType(), |
| 649 | cast<ParenType>(T2)->getInnerType())) |
| 650 | return false; |
| 651 | break; |
| 652 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 653 | case Type::Typedef: |
| 654 | if (!IsStructurallyEquivalent(Context, |
| 655 | cast<TypedefType>(T1)->getDecl(), |
| 656 | cast<TypedefType>(T2)->getDecl())) |
| 657 | return false; |
| 658 | break; |
| 659 | |
| 660 | case Type::TypeOfExpr: |
| 661 | if (!IsStructurallyEquivalent(Context, |
| 662 | cast<TypeOfExprType>(T1)->getUnderlyingExpr(), |
| 663 | cast<TypeOfExprType>(T2)->getUnderlyingExpr())) |
| 664 | return false; |
| 665 | break; |
| 666 | |
| 667 | case Type::TypeOf: |
| 668 | if (!IsStructurallyEquivalent(Context, |
| 669 | cast<TypeOfType>(T1)->getUnderlyingType(), |
| 670 | cast<TypeOfType>(T2)->getUnderlyingType())) |
| 671 | return false; |
| 672 | break; |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 673 | |
| 674 | case Type::UnaryTransform: |
| 675 | if (!IsStructurallyEquivalent(Context, |
| 676 | cast<UnaryTransformType>(T1)->getUnderlyingType(), |
| 677 | cast<UnaryTransformType>(T1)->getUnderlyingType())) |
| 678 | return false; |
| 679 | break; |
| 680 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 681 | case Type::Decltype: |
| 682 | if (!IsStructurallyEquivalent(Context, |
| 683 | cast<DecltypeType>(T1)->getUnderlyingExpr(), |
| 684 | cast<DecltypeType>(T2)->getUnderlyingExpr())) |
| 685 | return false; |
| 686 | break; |
| 687 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 688 | case Type::Auto: |
| 689 | if (!IsStructurallyEquivalent(Context, |
| 690 | cast<AutoType>(T1)->getDeducedType(), |
| 691 | cast<AutoType>(T2)->getDeducedType())) |
| 692 | return false; |
| 693 | break; |
| 694 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 695 | case Type::Record: |
| 696 | case Type::Enum: |
| 697 | if (!IsStructurallyEquivalent(Context, |
| 698 | cast<TagType>(T1)->getDecl(), |
| 699 | cast<TagType>(T2)->getDecl())) |
| 700 | return false; |
| 701 | break; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 702 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 703 | case Type::TemplateTypeParm: { |
| 704 | const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1); |
| 705 | const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2); |
| 706 | if (Parm1->getDepth() != Parm2->getDepth()) |
| 707 | return false; |
| 708 | if (Parm1->getIndex() != Parm2->getIndex()) |
| 709 | return false; |
| 710 | if (Parm1->isParameterPack() != Parm2->isParameterPack()) |
| 711 | return false; |
| 712 | |
| 713 | // Names of template type parameters are never significant. |
| 714 | break; |
| 715 | } |
| 716 | |
| 717 | case Type::SubstTemplateTypeParm: { |
| 718 | const SubstTemplateTypeParmType *Subst1 |
| 719 | = cast<SubstTemplateTypeParmType>(T1); |
| 720 | const SubstTemplateTypeParmType *Subst2 |
| 721 | = cast<SubstTemplateTypeParmType>(T2); |
| 722 | if (!IsStructurallyEquivalent(Context, |
| 723 | QualType(Subst1->getReplacedParameter(), 0), |
| 724 | QualType(Subst2->getReplacedParameter(), 0))) |
| 725 | return false; |
| 726 | if (!IsStructurallyEquivalent(Context, |
| 727 | Subst1->getReplacementType(), |
| 728 | Subst2->getReplacementType())) |
| 729 | return false; |
| 730 | break; |
| 731 | } |
| 732 | |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 733 | case Type::SubstTemplateTypeParmPack: { |
| 734 | const SubstTemplateTypeParmPackType *Subst1 |
| 735 | = cast<SubstTemplateTypeParmPackType>(T1); |
| 736 | const SubstTemplateTypeParmPackType *Subst2 |
| 737 | = cast<SubstTemplateTypeParmPackType>(T2); |
| 738 | if (!IsStructurallyEquivalent(Context, |
| 739 | QualType(Subst1->getReplacedParameter(), 0), |
| 740 | QualType(Subst2->getReplacedParameter(), 0))) |
| 741 | return false; |
| 742 | if (!IsStructurallyEquivalent(Context, |
| 743 | Subst1->getArgumentPack(), |
| 744 | Subst2->getArgumentPack())) |
| 745 | return false; |
| 746 | break; |
| 747 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 748 | case Type::TemplateSpecialization: { |
| 749 | const TemplateSpecializationType *Spec1 |
| 750 | = cast<TemplateSpecializationType>(T1); |
| 751 | const TemplateSpecializationType *Spec2 |
| 752 | = cast<TemplateSpecializationType>(T2); |
| 753 | if (!IsStructurallyEquivalent(Context, |
| 754 | Spec1->getTemplateName(), |
| 755 | Spec2->getTemplateName())) |
| 756 | return false; |
| 757 | if (Spec1->getNumArgs() != Spec2->getNumArgs()) |
| 758 | return false; |
| 759 | for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) { |
| 760 | if (!IsStructurallyEquivalent(Context, |
| 761 | Spec1->getArg(I), Spec2->getArg(I))) |
| 762 | return false; |
| 763 | } |
| 764 | break; |
| 765 | } |
| 766 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 767 | case Type::Elaborated: { |
| 768 | const ElaboratedType *Elab1 = cast<ElaboratedType>(T1); |
| 769 | const ElaboratedType *Elab2 = cast<ElaboratedType>(T2); |
| 770 | // CHECKME: what if a keyword is ETK_None or ETK_typename ? |
| 771 | if (Elab1->getKeyword() != Elab2->getKeyword()) |
| 772 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 773 | if (!IsStructurallyEquivalent(Context, |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 774 | Elab1->getQualifier(), |
| 775 | Elab2->getQualifier())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 776 | return false; |
| 777 | if (!IsStructurallyEquivalent(Context, |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 778 | Elab1->getNamedType(), |
| 779 | Elab2->getNamedType())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 780 | return false; |
| 781 | break; |
| 782 | } |
| 783 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 784 | case Type::InjectedClassName: { |
| 785 | const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1); |
| 786 | const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2); |
| 787 | if (!IsStructurallyEquivalent(Context, |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 788 | Inj1->getInjectedSpecializationType(), |
| 789 | Inj2->getInjectedSpecializationType())) |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 790 | return false; |
| 791 | break; |
| 792 | } |
| 793 | |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 794 | case Type::DependentName: { |
| 795 | const DependentNameType *Typename1 = cast<DependentNameType>(T1); |
| 796 | const DependentNameType *Typename2 = cast<DependentNameType>(T2); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 797 | if (!IsStructurallyEquivalent(Context, |
| 798 | Typename1->getQualifier(), |
| 799 | Typename2->getQualifier())) |
| 800 | return false; |
| 801 | if (!IsStructurallyEquivalent(Typename1->getIdentifier(), |
| 802 | Typename2->getIdentifier())) |
| 803 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 804 | |
| 805 | break; |
| 806 | } |
| 807 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 808 | case Type::DependentTemplateSpecialization: { |
| 809 | const DependentTemplateSpecializationType *Spec1 = |
| 810 | cast<DependentTemplateSpecializationType>(T1); |
| 811 | const DependentTemplateSpecializationType *Spec2 = |
| 812 | cast<DependentTemplateSpecializationType>(T2); |
| 813 | if (!IsStructurallyEquivalent(Context, |
| 814 | Spec1->getQualifier(), |
| 815 | Spec2->getQualifier())) |
| 816 | return false; |
| 817 | if (!IsStructurallyEquivalent(Spec1->getIdentifier(), |
| 818 | Spec2->getIdentifier())) |
| 819 | return false; |
| 820 | if (Spec1->getNumArgs() != Spec2->getNumArgs()) |
| 821 | return false; |
| 822 | for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) { |
| 823 | if (!IsStructurallyEquivalent(Context, |
| 824 | Spec1->getArg(I), Spec2->getArg(I))) |
| 825 | return false; |
| 826 | } |
| 827 | break; |
| 828 | } |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 829 | |
| 830 | case Type::PackExpansion: |
| 831 | if (!IsStructurallyEquivalent(Context, |
| 832 | cast<PackExpansionType>(T1)->getPattern(), |
| 833 | cast<PackExpansionType>(T2)->getPattern())) |
| 834 | return false; |
| 835 | break; |
| 836 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 837 | case Type::ObjCInterface: { |
| 838 | const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1); |
| 839 | const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2); |
| 840 | if (!IsStructurallyEquivalent(Context, |
| 841 | Iface1->getDecl(), Iface2->getDecl())) |
| 842 | return false; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 843 | break; |
| 844 | } |
| 845 | |
| 846 | case Type::ObjCObject: { |
| 847 | const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1); |
| 848 | const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2); |
| 849 | if (!IsStructurallyEquivalent(Context, |
| 850 | Obj1->getBaseType(), |
| 851 | Obj2->getBaseType())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 852 | return false; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 853 | if (Obj1->getNumProtocols() != Obj2->getNumProtocols()) |
| 854 | return false; |
| 855 | for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 856 | if (!IsStructurallyEquivalent(Context, |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 857 | Obj1->getProtocol(I), |
| 858 | Obj2->getProtocol(I))) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 859 | return false; |
| 860 | } |
| 861 | break; |
| 862 | } |
| 863 | |
| 864 | case Type::ObjCObjectPointer: { |
| 865 | const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1); |
| 866 | const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2); |
| 867 | if (!IsStructurallyEquivalent(Context, |
| 868 | Ptr1->getPointeeType(), |
| 869 | Ptr2->getPointeeType())) |
| 870 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 871 | break; |
| 872 | } |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 873 | |
| 874 | case Type::Atomic: { |
| 875 | if (!IsStructurallyEquivalent(Context, |
| 876 | cast<AtomicType>(T1)->getValueType(), |
| 877 | cast<AtomicType>(T2)->getValueType())) |
| 878 | return false; |
| 879 | break; |
| 880 | } |
| 881 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 882 | case Type::Pipe: { |
| 883 | if (!IsStructurallyEquivalent(Context, |
| 884 | cast<PipeType>(T1)->getElementType(), |
| 885 | cast<PipeType>(T2)->getElementType())) |
| 886 | return false; |
| 887 | break; |
| 888 | } |
| 889 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 890 | } // end switch |
| 891 | |
| 892 | return true; |
| 893 | } |
| 894 | |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 895 | /// \brief Determine structural equivalence of two fields. |
| 896 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 897 | FieldDecl *Field1, FieldDecl *Field2) { |
| 898 | RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext()); |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 899 | |
| 900 | // For anonymous structs/unions, match up the anonymous struct/union type |
| 901 | // declarations directly, so that we don't go off searching for anonymous |
| 902 | // types |
| 903 | if (Field1->isAnonymousStructOrUnion() && |
| 904 | Field2->isAnonymousStructOrUnion()) { |
| 905 | RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl(); |
| 906 | RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl(); |
| 907 | return IsStructurallyEquivalent(Context, D1, D2); |
| 908 | } |
Sean Callanan | 969c5bd | 2013-04-26 22:49:25 +0000 | [diff] [blame] | 909 | |
| 910 | // Check for equivalent field names. |
| 911 | IdentifierInfo *Name1 = Field1->getIdentifier(); |
| 912 | IdentifierInfo *Name2 = Field2->getIdentifier(); |
| 913 | if (!::IsStructurallyEquivalent(Name1, Name2)) |
| 914 | return false; |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 915 | |
| 916 | if (!IsStructurallyEquivalent(Context, |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 917 | Field1->getType(), Field2->getType())) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 918 | if (Context.Complain) { |
| 919 | Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 920 | << Context.C2.getTypeDeclType(Owner2); |
| 921 | Context.Diag2(Field2->getLocation(), diag::note_odr_field) |
| 922 | << Field2->getDeclName() << Field2->getType(); |
| 923 | Context.Diag1(Field1->getLocation(), diag::note_odr_field) |
| 924 | << Field1->getDeclName() << Field1->getType(); |
| 925 | } |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 926 | return false; |
| 927 | } |
| 928 | |
| 929 | if (Field1->isBitField() != Field2->isBitField()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 930 | if (Context.Complain) { |
| 931 | Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 932 | << Context.C2.getTypeDeclType(Owner2); |
| 933 | if (Field1->isBitField()) { |
| 934 | Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field) |
| 935 | << Field1->getDeclName() << Field1->getType() |
| 936 | << Field1->getBitWidthValue(Context.C1); |
| 937 | Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field) |
| 938 | << Field2->getDeclName(); |
| 939 | } else { |
| 940 | Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field) |
| 941 | << Field2->getDeclName() << Field2->getType() |
| 942 | << Field2->getBitWidthValue(Context.C2); |
| 943 | Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field) |
| 944 | << Field1->getDeclName(); |
| 945 | } |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 946 | } |
| 947 | return false; |
| 948 | } |
| 949 | |
| 950 | if (Field1->isBitField()) { |
| 951 | // Make sure that the bit-fields are the same length. |
| 952 | unsigned Bits1 = Field1->getBitWidthValue(Context.C1); |
| 953 | unsigned Bits2 = Field2->getBitWidthValue(Context.C2); |
| 954 | |
| 955 | if (Bits1 != Bits2) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 956 | if (Context.Complain) { |
| 957 | Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 958 | << Context.C2.getTypeDeclType(Owner2); |
| 959 | Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field) |
| 960 | << Field2->getDeclName() << Field2->getType() << Bits2; |
| 961 | Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field) |
| 962 | << Field1->getDeclName() << Field1->getType() << Bits1; |
| 963 | } |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 964 | return false; |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | return true; |
| 969 | } |
| 970 | |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 971 | /// \brief Find the index of the given anonymous struct/union within its |
| 972 | /// context. |
| 973 | /// |
| 974 | /// \returns Returns the index of this anonymous struct/union in its context, |
| 975 | /// including the next assigned index (if none of them match). Returns an |
| 976 | /// empty option if the context is not a record, i.e.. if the anonymous |
| 977 | /// struct/union is at namespace or block scope. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 978 | static Optional<unsigned> findAnonymousStructOrUnionIndex(RecordDecl *Anon) { |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 979 | ASTContext &Context = Anon->getASTContext(); |
| 980 | QualType AnonTy = Context.getRecordType(Anon); |
| 981 | |
| 982 | RecordDecl *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext()); |
| 983 | if (!Owner) |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 984 | return None; |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 985 | |
| 986 | unsigned Index = 0; |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 987 | for (const auto *D : Owner->noload_decls()) { |
| 988 | const auto *F = dyn_cast<FieldDecl>(D); |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 989 | if (!F || !F->isAnonymousStructOrUnion()) |
| 990 | continue; |
| 991 | |
| 992 | if (Context.hasSameType(F->getType(), AnonTy)) |
| 993 | break; |
| 994 | |
| 995 | ++Index; |
| 996 | } |
| 997 | |
| 998 | return Index; |
| 999 | } |
| 1000 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1001 | /// \brief Determine structural equivalence of two records. |
| 1002 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1003 | RecordDecl *D1, RecordDecl *D2) { |
| 1004 | if (D1->isUnion() != D2->isUnion()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1005 | if (Context.Complain) { |
| 1006 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1007 | << Context.C2.getTypeDeclType(D2); |
| 1008 | Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here) |
| 1009 | << D1->getDeclName() << (unsigned)D1->getTagKind(); |
| 1010 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1011 | return false; |
| 1012 | } |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 1013 | |
| 1014 | if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) { |
| 1015 | // If both anonymous structs/unions are in a record context, make sure |
| 1016 | // they occur in the same location in the context records. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1017 | if (Optional<unsigned> Index1 = findAnonymousStructOrUnionIndex(D1)) { |
| 1018 | if (Optional<unsigned> Index2 = findAnonymousStructOrUnionIndex(D2)) { |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 1019 | if (*Index1 != *Index2) |
| 1020 | return false; |
| 1021 | } |
| 1022 | } |
| 1023 | } |
| 1024 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1025 | // If both declarations are class template specializations, we know |
| 1026 | // the ODR applies, so check the template and template arguments. |
| 1027 | ClassTemplateSpecializationDecl *Spec1 |
| 1028 | = dyn_cast<ClassTemplateSpecializationDecl>(D1); |
| 1029 | ClassTemplateSpecializationDecl *Spec2 |
| 1030 | = dyn_cast<ClassTemplateSpecializationDecl>(D2); |
| 1031 | if (Spec1 && Spec2) { |
| 1032 | // Check that the specialized templates are the same. |
| 1033 | if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(), |
| 1034 | Spec2->getSpecializedTemplate())) |
| 1035 | return false; |
| 1036 | |
| 1037 | // Check that the template arguments are the same. |
| 1038 | if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size()) |
| 1039 | return false; |
| 1040 | |
| 1041 | for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I) |
| 1042 | if (!IsStructurallyEquivalent(Context, |
| 1043 | Spec1->getTemplateArgs().get(I), |
| 1044 | Spec2->getTemplateArgs().get(I))) |
| 1045 | return false; |
| 1046 | } |
| 1047 | // 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] | 1048 | // structures are different. |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1049 | else if (Spec1 || Spec2) |
| 1050 | return false; |
| 1051 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1052 | // Compare the definitions of these two records. If either or both are |
| 1053 | // incomplete, we assume that they are equivalent. |
| 1054 | D1 = D1->getDefinition(); |
| 1055 | D2 = D2->getDefinition(); |
| 1056 | if (!D1 || !D2) |
| 1057 | return true; |
| 1058 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1059 | if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) { |
| 1060 | if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) { |
| 1061 | if (D1CXX->getNumBases() != D2CXX->getNumBases()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1062 | if (Context.Complain) { |
| 1063 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1064 | << Context.C2.getTypeDeclType(D2); |
| 1065 | Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases) |
| 1066 | << D2CXX->getNumBases(); |
| 1067 | Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases) |
| 1068 | << D1CXX->getNumBases(); |
| 1069 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1070 | return false; |
| 1071 | } |
| 1072 | |
| 1073 | // Check the base classes. |
| 1074 | for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(), |
| 1075 | BaseEnd1 = D1CXX->bases_end(), |
| 1076 | Base2 = D2CXX->bases_begin(); |
| 1077 | Base1 != BaseEnd1; |
| 1078 | ++Base1, ++Base2) { |
| 1079 | if (!IsStructurallyEquivalent(Context, |
| 1080 | Base1->getType(), Base2->getType())) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1081 | if (Context.Complain) { |
| 1082 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1083 | << Context.C2.getTypeDeclType(D2); |
| 1084 | Context.Diag2(Base2->getLocStart(), diag::note_odr_base) |
| 1085 | << Base2->getType() |
| 1086 | << Base2->getSourceRange(); |
| 1087 | Context.Diag1(Base1->getLocStart(), diag::note_odr_base) |
| 1088 | << Base1->getType() |
| 1089 | << Base1->getSourceRange(); |
| 1090 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1091 | return false; |
| 1092 | } |
| 1093 | |
| 1094 | // Check virtual vs. non-virtual inheritance mismatch. |
| 1095 | if (Base1->isVirtual() != Base2->isVirtual()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1096 | if (Context.Complain) { |
| 1097 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1098 | << Context.C2.getTypeDeclType(D2); |
| 1099 | Context.Diag2(Base2->getLocStart(), |
| 1100 | diag::note_odr_virtual_base) |
| 1101 | << Base2->isVirtual() << Base2->getSourceRange(); |
| 1102 | Context.Diag1(Base1->getLocStart(), diag::note_odr_base) |
| 1103 | << Base1->isVirtual() |
| 1104 | << Base1->getSourceRange(); |
| 1105 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1106 | return false; |
| 1107 | } |
| 1108 | } |
| 1109 | } else if (D1CXX->getNumBases() > 0) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1110 | if (Context.Complain) { |
| 1111 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1112 | << Context.C2.getTypeDeclType(D2); |
| 1113 | const CXXBaseSpecifier *Base1 = D1CXX->bases_begin(); |
| 1114 | Context.Diag1(Base1->getLocStart(), diag::note_odr_base) |
| 1115 | << Base1->getType() |
| 1116 | << Base1->getSourceRange(); |
| 1117 | Context.Diag2(D2->getLocation(), diag::note_odr_missing_base); |
| 1118 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1119 | return false; |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | // Check the fields for consistency. |
Dmitri Gribenko | 898cff0 | 2012-05-19 17:17:26 +0000 | [diff] [blame] | 1124 | RecordDecl::field_iterator Field2 = D2->field_begin(), |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1125 | Field2End = D2->field_end(); |
Dmitri Gribenko | 898cff0 | 2012-05-19 17:17:26 +0000 | [diff] [blame] | 1126 | for (RecordDecl::field_iterator Field1 = D1->field_begin(), |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1127 | Field1End = D1->field_end(); |
| 1128 | Field1 != Field1End; |
| 1129 | ++Field1, ++Field2) { |
| 1130 | if (Field2 == Field2End) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1131 | if (Context.Complain) { |
| 1132 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1133 | << Context.C2.getTypeDeclType(D2); |
| 1134 | Context.Diag1(Field1->getLocation(), diag::note_odr_field) |
| 1135 | << Field1->getDeclName() << Field1->getType(); |
| 1136 | Context.Diag2(D2->getLocation(), diag::note_odr_missing_field); |
| 1137 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1138 | return false; |
| 1139 | } |
| 1140 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1141 | if (!IsStructurallyEquivalent(Context, *Field1, *Field2)) |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 1142 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | if (Field2 != Field2End) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1146 | if (Context.Complain) { |
| 1147 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1148 | << Context.C2.getTypeDeclType(D2); |
| 1149 | Context.Diag2(Field2->getLocation(), diag::note_odr_field) |
| 1150 | << Field2->getDeclName() << Field2->getType(); |
| 1151 | Context.Diag1(D1->getLocation(), diag::note_odr_missing_field); |
| 1152 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1153 | return false; |
| 1154 | } |
| 1155 | |
| 1156 | return true; |
| 1157 | } |
| 1158 | |
| 1159 | /// \brief Determine structural equivalence of two enums. |
| 1160 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1161 | EnumDecl *D1, EnumDecl *D2) { |
| 1162 | EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(), |
| 1163 | EC2End = D2->enumerator_end(); |
| 1164 | for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(), |
| 1165 | EC1End = D1->enumerator_end(); |
| 1166 | EC1 != EC1End; ++EC1, ++EC2) { |
| 1167 | if (EC2 == EC2End) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1168 | if (Context.Complain) { |
| 1169 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1170 | << Context.C2.getTypeDeclType(D2); |
| 1171 | Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator) |
| 1172 | << EC1->getDeclName() |
| 1173 | << EC1->getInitVal().toString(10); |
| 1174 | Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator); |
| 1175 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1176 | return false; |
| 1177 | } |
| 1178 | |
| 1179 | llvm::APSInt Val1 = EC1->getInitVal(); |
| 1180 | llvm::APSInt Val2 = EC2->getInitVal(); |
Eric Christopher | 6dcc376 | 2012-07-15 00:23:57 +0000 | [diff] [blame] | 1181 | if (!llvm::APSInt::isSameValue(Val1, Val2) || |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1182 | !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1183 | if (Context.Complain) { |
| 1184 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1185 | << Context.C2.getTypeDeclType(D2); |
| 1186 | Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator) |
| 1187 | << EC2->getDeclName() |
| 1188 | << EC2->getInitVal().toString(10); |
| 1189 | Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator) |
| 1190 | << EC1->getDeclName() |
| 1191 | << EC1->getInitVal().toString(10); |
| 1192 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1193 | return false; |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | if (EC2 != EC2End) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1198 | if (Context.Complain) { |
| 1199 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1200 | << Context.C2.getTypeDeclType(D2); |
| 1201 | Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator) |
| 1202 | << EC2->getDeclName() |
| 1203 | << EC2->getInitVal().toString(10); |
| 1204 | Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator); |
| 1205 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1206 | return false; |
| 1207 | } |
| 1208 | |
| 1209 | return true; |
| 1210 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1211 | |
| 1212 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1213 | TemplateParameterList *Params1, |
| 1214 | TemplateParameterList *Params2) { |
| 1215 | if (Params1->size() != Params2->size()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1216 | if (Context.Complain) { |
| 1217 | Context.Diag2(Params2->getTemplateLoc(), |
| 1218 | diag::err_odr_different_num_template_parameters) |
| 1219 | << Params1->size() << Params2->size(); |
| 1220 | Context.Diag1(Params1->getTemplateLoc(), |
| 1221 | diag::note_odr_template_parameter_list); |
| 1222 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1223 | return false; |
| 1224 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1225 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1226 | for (unsigned I = 0, N = Params1->size(); I != N; ++I) { |
| 1227 | if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1228 | if (Context.Complain) { |
| 1229 | Context.Diag2(Params2->getParam(I)->getLocation(), |
| 1230 | diag::err_odr_different_template_parameter_kind); |
| 1231 | Context.Diag1(Params1->getParam(I)->getLocation(), |
| 1232 | diag::note_odr_template_parameter_here); |
| 1233 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1234 | return false; |
| 1235 | } |
| 1236 | |
| 1237 | if (!Context.IsStructurallyEquivalent(Params1->getParam(I), |
| 1238 | Params2->getParam(I))) { |
| 1239 | |
| 1240 | return false; |
| 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | return true; |
| 1245 | } |
| 1246 | |
| 1247 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1248 | TemplateTypeParmDecl *D1, |
| 1249 | TemplateTypeParmDecl *D2) { |
| 1250 | if (D1->isParameterPack() != D2->isParameterPack()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1251 | if (Context.Complain) { |
| 1252 | Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack) |
| 1253 | << D2->isParameterPack(); |
| 1254 | Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) |
| 1255 | << D1->isParameterPack(); |
| 1256 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1257 | return false; |
| 1258 | } |
| 1259 | |
| 1260 | return true; |
| 1261 | } |
| 1262 | |
| 1263 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1264 | NonTypeTemplateParmDecl *D1, |
| 1265 | NonTypeTemplateParmDecl *D2) { |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1266 | if (D1->isParameterPack() != D2->isParameterPack()) { |
Douglas Gregor | 3c7380b | 2012-10-26 15:36:15 +0000 | [diff] [blame] | 1267 | if (Context.Complain) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1268 | Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack) |
| 1269 | << D2->isParameterPack(); |
| 1270 | Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) |
| 1271 | << D1->isParameterPack(); |
| 1272 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1273 | return false; |
| 1274 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1275 | |
| 1276 | // Check types. |
| 1277 | if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1278 | if (Context.Complain) { |
| 1279 | Context.Diag2(D2->getLocation(), |
| 1280 | diag::err_odr_non_type_parameter_type_inconsistent) |
| 1281 | << D2->getType() << D1->getType(); |
| 1282 | Context.Diag1(D1->getLocation(), diag::note_odr_value_here) |
| 1283 | << D1->getType(); |
| 1284 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1285 | return false; |
| 1286 | } |
| 1287 | |
| 1288 | return true; |
| 1289 | } |
| 1290 | |
| 1291 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1292 | TemplateTemplateParmDecl *D1, |
| 1293 | TemplateTemplateParmDecl *D2) { |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1294 | if (D1->isParameterPack() != D2->isParameterPack()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1295 | if (Context.Complain) { |
| 1296 | Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack) |
| 1297 | << D2->isParameterPack(); |
| 1298 | Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) |
| 1299 | << D1->isParameterPack(); |
| 1300 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1301 | return false; |
| 1302 | } |
Douglas Gregor | 3c7380b | 2012-10-26 15:36:15 +0000 | [diff] [blame] | 1303 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1304 | // Check template parameter lists. |
| 1305 | return IsStructurallyEquivalent(Context, D1->getTemplateParameters(), |
| 1306 | D2->getTemplateParameters()); |
| 1307 | } |
| 1308 | |
| 1309 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1310 | ClassTemplateDecl *D1, |
| 1311 | ClassTemplateDecl *D2) { |
| 1312 | // Check template parameters. |
| 1313 | if (!IsStructurallyEquivalent(Context, |
| 1314 | D1->getTemplateParameters(), |
| 1315 | D2->getTemplateParameters())) |
| 1316 | return false; |
| 1317 | |
| 1318 | // Check the templated declaration. |
| 1319 | return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(), |
| 1320 | D2->getTemplatedDecl()); |
| 1321 | } |
| 1322 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1323 | /// \brief Determine structural equivalence of two declarations. |
| 1324 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1325 | Decl *D1, Decl *D2) { |
| 1326 | // FIXME: Check for known structural equivalences via a callback of some sort. |
| 1327 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1328 | // Check whether we already know that these two declarations are not |
| 1329 | // structurally equivalent. |
| 1330 | if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(), |
| 1331 | D2->getCanonicalDecl()))) |
| 1332 | return false; |
| 1333 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1334 | // Determine whether we've already produced a tentative equivalence for D1. |
| 1335 | Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()]; |
| 1336 | if (EquivToD1) |
| 1337 | return EquivToD1 == D2->getCanonicalDecl(); |
| 1338 | |
| 1339 | // Produce a tentative equivalence D1 <-> D2, which will be checked later. |
| 1340 | EquivToD1 = D2->getCanonicalDecl(); |
| 1341 | Context.DeclsToCheck.push_back(D1->getCanonicalDecl()); |
| 1342 | return true; |
| 1343 | } |
| 1344 | |
| 1345 | bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1, |
| 1346 | Decl *D2) { |
| 1347 | if (!::IsStructurallyEquivalent(*this, D1, D2)) |
| 1348 | return false; |
| 1349 | |
| 1350 | return !Finish(); |
| 1351 | } |
| 1352 | |
| 1353 | bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1, |
| 1354 | QualType T2) { |
| 1355 | if (!::IsStructurallyEquivalent(*this, T1, T2)) |
| 1356 | return false; |
| 1357 | |
| 1358 | return !Finish(); |
| 1359 | } |
| 1360 | |
| 1361 | bool StructuralEquivalenceContext::Finish() { |
| 1362 | while (!DeclsToCheck.empty()) { |
| 1363 | // Check the next declaration. |
| 1364 | Decl *D1 = DeclsToCheck.front(); |
| 1365 | DeclsToCheck.pop_front(); |
| 1366 | |
| 1367 | Decl *D2 = TentativeEquivalences[D1]; |
| 1368 | assert(D2 && "Unrecorded tentative equivalence?"); |
| 1369 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1370 | bool Equivalent = true; |
| 1371 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1372 | // FIXME: Switch on all declaration kinds. For now, we're just going to |
| 1373 | // check the obvious ones. |
| 1374 | if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) { |
| 1375 | if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) { |
| 1376 | // Check for equivalent structure names. |
| 1377 | IdentifierInfo *Name1 = Record1->getIdentifier(); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1378 | if (!Name1 && Record1->getTypedefNameForAnonDecl()) |
| 1379 | Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier(); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1380 | IdentifierInfo *Name2 = Record2->getIdentifier(); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1381 | if (!Name2 && Record2->getTypedefNameForAnonDecl()) |
| 1382 | Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier(); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1383 | if (!::IsStructurallyEquivalent(Name1, Name2) || |
| 1384 | !::IsStructurallyEquivalent(*this, Record1, Record2)) |
| 1385 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1386 | } else { |
| 1387 | // Record/non-record mismatch. |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1388 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1389 | } |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1390 | } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1391 | if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) { |
| 1392 | // Check for equivalent enum names. |
| 1393 | IdentifierInfo *Name1 = Enum1->getIdentifier(); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1394 | if (!Name1 && Enum1->getTypedefNameForAnonDecl()) |
| 1395 | Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier(); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1396 | IdentifierInfo *Name2 = Enum2->getIdentifier(); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1397 | if (!Name2 && Enum2->getTypedefNameForAnonDecl()) |
| 1398 | Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier(); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1399 | if (!::IsStructurallyEquivalent(Name1, Name2) || |
| 1400 | !::IsStructurallyEquivalent(*this, Enum1, Enum2)) |
| 1401 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1402 | } else { |
| 1403 | // Enum/non-enum mismatch |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1404 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1405 | } |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1406 | } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) { |
| 1407 | if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1408 | if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(), |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1409 | Typedef2->getIdentifier()) || |
| 1410 | !::IsStructurallyEquivalent(*this, |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1411 | Typedef1->getUnderlyingType(), |
| 1412 | Typedef2->getUnderlyingType())) |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1413 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1414 | } else { |
| 1415 | // Typedef/non-typedef mismatch. |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1416 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1417 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1418 | } else if (ClassTemplateDecl *ClassTemplate1 |
| 1419 | = dyn_cast<ClassTemplateDecl>(D1)) { |
| 1420 | if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) { |
| 1421 | if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(), |
| 1422 | ClassTemplate2->getIdentifier()) || |
| 1423 | !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2)) |
| 1424 | Equivalent = false; |
| 1425 | } else { |
| 1426 | // Class template/non-class-template mismatch. |
| 1427 | Equivalent = false; |
| 1428 | } |
| 1429 | } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) { |
| 1430 | if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) { |
| 1431 | if (!::IsStructurallyEquivalent(*this, TTP1, TTP2)) |
| 1432 | Equivalent = false; |
| 1433 | } else { |
| 1434 | // Kind mismatch. |
| 1435 | Equivalent = false; |
| 1436 | } |
| 1437 | } else if (NonTypeTemplateParmDecl *NTTP1 |
| 1438 | = dyn_cast<NonTypeTemplateParmDecl>(D1)) { |
| 1439 | if (NonTypeTemplateParmDecl *NTTP2 |
| 1440 | = dyn_cast<NonTypeTemplateParmDecl>(D2)) { |
| 1441 | if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2)) |
| 1442 | Equivalent = false; |
| 1443 | } else { |
| 1444 | // Kind mismatch. |
| 1445 | Equivalent = false; |
| 1446 | } |
| 1447 | } else if (TemplateTemplateParmDecl *TTP1 |
| 1448 | = dyn_cast<TemplateTemplateParmDecl>(D1)) { |
| 1449 | if (TemplateTemplateParmDecl *TTP2 |
| 1450 | = dyn_cast<TemplateTemplateParmDecl>(D2)) { |
| 1451 | if (!::IsStructurallyEquivalent(*this, TTP1, TTP2)) |
| 1452 | Equivalent = false; |
| 1453 | } else { |
| 1454 | // Kind mismatch. |
| 1455 | Equivalent = false; |
| 1456 | } |
| 1457 | } |
| 1458 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1459 | if (!Equivalent) { |
| 1460 | // Note that these two declarations are not equivalent (and we already |
| 1461 | // know about it). |
| 1462 | NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(), |
| 1463 | D2->getCanonicalDecl())); |
| 1464 | return true; |
| 1465 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1466 | // FIXME: Check other declaration kinds! |
| 1467 | } |
| 1468 | |
| 1469 | return false; |
| 1470 | } |
| 1471 | |
| 1472 | //---------------------------------------------------------------------------- |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1473 | // Import Types |
| 1474 | //---------------------------------------------------------------------------- |
| 1475 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1476 | QualType ASTNodeImporter::VisitType(const Type *T) { |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 1477 | Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node) |
| 1478 | << T->getTypeClassName(); |
| 1479 | return QualType(); |
| 1480 | } |
| 1481 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1482 | QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1483 | switch (T->getKind()) { |
John McCall | e314e27 | 2011-10-18 21:02:43 +0000 | [diff] [blame] | 1484 | #define SHARED_SINGLETON_TYPE(Expansion) |
| 1485 | #define BUILTIN_TYPE(Id, SingletonId) \ |
| 1486 | case BuiltinType::Id: return Importer.getToContext().SingletonId; |
| 1487 | #include "clang/AST/BuiltinTypes.def" |
| 1488 | |
| 1489 | // FIXME: for Char16, Char32, and NullPtr, make sure that the "to" |
| 1490 | // context supports C++. |
| 1491 | |
| 1492 | // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to" |
| 1493 | // context supports ObjC. |
| 1494 | |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1495 | case BuiltinType::Char_U: |
| 1496 | // The context we're importing from has an unsigned 'char'. If we're |
| 1497 | // importing into a context with a signed 'char', translate to |
| 1498 | // 'unsigned char' instead. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1499 | if (Importer.getToContext().getLangOpts().CharIsSigned) |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1500 | return Importer.getToContext().UnsignedCharTy; |
| 1501 | |
| 1502 | return Importer.getToContext().CharTy; |
| 1503 | |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1504 | case BuiltinType::Char_S: |
| 1505 | // The context we're importing from has an unsigned 'char'. If we're |
| 1506 | // importing into a context with a signed 'char', translate to |
| 1507 | // 'unsigned char' instead. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1508 | if (!Importer.getToContext().getLangOpts().CharIsSigned) |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1509 | return Importer.getToContext().SignedCharTy; |
| 1510 | |
| 1511 | return Importer.getToContext().CharTy; |
| 1512 | |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 1513 | case BuiltinType::WChar_S: |
| 1514 | case BuiltinType::WChar_U: |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1515 | // FIXME: If not in C++, shall we translate to the C equivalent of |
| 1516 | // wchar_t? |
| 1517 | return Importer.getToContext().WCharTy; |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1518 | } |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1519 | |
| 1520 | llvm_unreachable("Invalid BuiltinType Kind!"); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1523 | QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1524 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1525 | if (ToElementType.isNull()) |
| 1526 | return QualType(); |
| 1527 | |
| 1528 | return Importer.getToContext().getComplexType(ToElementType); |
| 1529 | } |
| 1530 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1531 | QualType ASTNodeImporter::VisitPointerType(const PointerType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1532 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 1533 | if (ToPointeeType.isNull()) |
| 1534 | return QualType(); |
| 1535 | |
| 1536 | return Importer.getToContext().getPointerType(ToPointeeType); |
| 1537 | } |
| 1538 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1539 | QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1540 | // FIXME: Check for blocks support in "to" context. |
| 1541 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 1542 | if (ToPointeeType.isNull()) |
| 1543 | return QualType(); |
| 1544 | |
| 1545 | return Importer.getToContext().getBlockPointerType(ToPointeeType); |
| 1546 | } |
| 1547 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1548 | QualType |
| 1549 | ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1550 | // FIXME: Check for C++ support in "to" context. |
| 1551 | QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); |
| 1552 | if (ToPointeeType.isNull()) |
| 1553 | return QualType(); |
| 1554 | |
| 1555 | return Importer.getToContext().getLValueReferenceType(ToPointeeType); |
| 1556 | } |
| 1557 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1558 | QualType |
| 1559 | ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1560 | // FIXME: Check for C++0x support in "to" context. |
| 1561 | QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); |
| 1562 | if (ToPointeeType.isNull()) |
| 1563 | return QualType(); |
| 1564 | |
| 1565 | return Importer.getToContext().getRValueReferenceType(ToPointeeType); |
| 1566 | } |
| 1567 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1568 | QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1569 | // FIXME: Check for C++ support in "to" context. |
| 1570 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 1571 | if (ToPointeeType.isNull()) |
| 1572 | return QualType(); |
| 1573 | |
| 1574 | QualType ClassType = Importer.Import(QualType(T->getClass(), 0)); |
| 1575 | return Importer.getToContext().getMemberPointerType(ToPointeeType, |
| 1576 | ClassType.getTypePtr()); |
| 1577 | } |
| 1578 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1579 | QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1580 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1581 | if (ToElementType.isNull()) |
| 1582 | return QualType(); |
| 1583 | |
| 1584 | return Importer.getToContext().getConstantArrayType(ToElementType, |
| 1585 | T->getSize(), |
| 1586 | T->getSizeModifier(), |
| 1587 | T->getIndexTypeCVRQualifiers()); |
| 1588 | } |
| 1589 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1590 | QualType |
| 1591 | ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1592 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1593 | if (ToElementType.isNull()) |
| 1594 | return QualType(); |
| 1595 | |
| 1596 | return Importer.getToContext().getIncompleteArrayType(ToElementType, |
| 1597 | T->getSizeModifier(), |
| 1598 | T->getIndexTypeCVRQualifiers()); |
| 1599 | } |
| 1600 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1601 | QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1602 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1603 | if (ToElementType.isNull()) |
| 1604 | return QualType(); |
| 1605 | |
| 1606 | Expr *Size = Importer.Import(T->getSizeExpr()); |
| 1607 | if (!Size) |
| 1608 | return QualType(); |
| 1609 | |
| 1610 | SourceRange Brackets = Importer.Import(T->getBracketsRange()); |
| 1611 | return Importer.getToContext().getVariableArrayType(ToElementType, Size, |
| 1612 | T->getSizeModifier(), |
| 1613 | T->getIndexTypeCVRQualifiers(), |
| 1614 | Brackets); |
| 1615 | } |
| 1616 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1617 | QualType ASTNodeImporter::VisitVectorType(const VectorType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1618 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1619 | if (ToElementType.isNull()) |
| 1620 | return QualType(); |
| 1621 | |
| 1622 | return Importer.getToContext().getVectorType(ToElementType, |
| 1623 | T->getNumElements(), |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1624 | T->getVectorKind()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1627 | QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1628 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1629 | if (ToElementType.isNull()) |
| 1630 | return QualType(); |
| 1631 | |
| 1632 | return Importer.getToContext().getExtVectorType(ToElementType, |
| 1633 | T->getNumElements()); |
| 1634 | } |
| 1635 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1636 | QualType |
| 1637 | ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1638 | // FIXME: What happens if we're importing a function without a prototype |
| 1639 | // into C++? Should we make it variadic? |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1640 | QualType ToResultType = Importer.Import(T->getReturnType()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1641 | if (ToResultType.isNull()) |
| 1642 | return QualType(); |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1643 | |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1644 | return Importer.getToContext().getFunctionNoProtoType(ToResultType, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1645 | T->getExtInfo()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 1648 | QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1649 | QualType ToResultType = Importer.Import(T->getReturnType()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1650 | if (ToResultType.isNull()) |
| 1651 | return QualType(); |
| 1652 | |
| 1653 | // Import argument types |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1654 | SmallVector<QualType, 4> ArgTypes; |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 1655 | for (const auto &A : T->param_types()) { |
| 1656 | QualType ArgType = Importer.Import(A); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1657 | if (ArgType.isNull()) |
| 1658 | return QualType(); |
| 1659 | ArgTypes.push_back(ArgType); |
| 1660 | } |
| 1661 | |
| 1662 | // Import exception types |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1663 | SmallVector<QualType, 4> ExceptionTypes; |
Aaron Ballman | b088fbe | 2014-03-17 15:38:09 +0000 | [diff] [blame] | 1664 | for (const auto &E : T->exceptions()) { |
| 1665 | QualType ExceptionType = Importer.Import(E); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1666 | if (ExceptionType.isNull()) |
| 1667 | return QualType(); |
| 1668 | ExceptionTypes.push_back(ExceptionType); |
| 1669 | } |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1670 | |
Argyrios Kyrtzidis | b41791d | 2012-09-22 01:58:06 +0000 | [diff] [blame] | 1671 | FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo(); |
| 1672 | FunctionProtoType::ExtProtoInfo ToEPI; |
| 1673 | |
| 1674 | ToEPI.ExtInfo = FromEPI.ExtInfo; |
| 1675 | ToEPI.Variadic = FromEPI.Variadic; |
| 1676 | ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn; |
| 1677 | ToEPI.TypeQuals = FromEPI.TypeQuals; |
| 1678 | ToEPI.RefQualifier = FromEPI.RefQualifier; |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 1679 | ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type; |
| 1680 | ToEPI.ExceptionSpec.Exceptions = ExceptionTypes; |
| 1681 | ToEPI.ExceptionSpec.NoexceptExpr = |
| 1682 | Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr); |
| 1683 | ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>( |
| 1684 | Importer.Import(FromEPI.ExceptionSpec.SourceDecl)); |
| 1685 | ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>( |
| 1686 | Importer.Import(FromEPI.ExceptionSpec.SourceTemplate)); |
Argyrios Kyrtzidis | b41791d | 2012-09-22 01:58:06 +0000 | [diff] [blame] | 1687 | |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 1688 | return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI); |
Argyrios Kyrtzidis | b41791d | 2012-09-22 01:58:06 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
Sean Callanan | da6df8a | 2011-08-11 16:56:07 +0000 | [diff] [blame] | 1691 | QualType ASTNodeImporter::VisitParenType(const ParenType *T) { |
| 1692 | QualType ToInnerType = Importer.Import(T->getInnerType()); |
| 1693 | if (ToInnerType.isNull()) |
| 1694 | return QualType(); |
| 1695 | |
| 1696 | return Importer.getToContext().getParenType(ToInnerType); |
| 1697 | } |
| 1698 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1699 | QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1700 | TypedefNameDecl *ToDecl |
| 1701 | = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl())); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1702 | if (!ToDecl) |
| 1703 | return QualType(); |
| 1704 | |
| 1705 | return Importer.getToContext().getTypeDeclType(ToDecl); |
| 1706 | } |
| 1707 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1708 | QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1709 | Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); |
| 1710 | if (!ToExpr) |
| 1711 | return QualType(); |
| 1712 | |
| 1713 | return Importer.getToContext().getTypeOfExprType(ToExpr); |
| 1714 | } |
| 1715 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1716 | QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1717 | QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); |
| 1718 | if (ToUnderlyingType.isNull()) |
| 1719 | return QualType(); |
| 1720 | |
| 1721 | return Importer.getToContext().getTypeOfType(ToUnderlyingType); |
| 1722 | } |
| 1723 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1724 | QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) { |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1725 | // FIXME: Make sure that the "to" context supports C++0x! |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1726 | Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); |
| 1727 | if (!ToExpr) |
| 1728 | return QualType(); |
| 1729 | |
Douglas Gregor | 81495f3 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 1730 | QualType UnderlyingType = Importer.Import(T->getUnderlyingType()); |
| 1731 | if (UnderlyingType.isNull()) |
| 1732 | return QualType(); |
| 1733 | |
| 1734 | return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1737 | QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) { |
| 1738 | QualType ToBaseType = Importer.Import(T->getBaseType()); |
| 1739 | QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); |
| 1740 | if (ToBaseType.isNull() || ToUnderlyingType.isNull()) |
| 1741 | return QualType(); |
| 1742 | |
| 1743 | return Importer.getToContext().getUnaryTransformType(ToBaseType, |
| 1744 | ToUnderlyingType, |
| 1745 | T->getUTTKind()); |
| 1746 | } |
| 1747 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1748 | QualType ASTNodeImporter::VisitAutoType(const AutoType *T) { |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 1749 | // FIXME: Make sure that the "to" context supports C++11! |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1750 | QualType FromDeduced = T->getDeducedType(); |
| 1751 | QualType ToDeduced; |
| 1752 | if (!FromDeduced.isNull()) { |
| 1753 | ToDeduced = Importer.Import(FromDeduced); |
| 1754 | if (ToDeduced.isNull()) |
| 1755 | return QualType(); |
| 1756 | } |
| 1757 | |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 1758 | return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(), |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 1759 | /*IsDependent*/false); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1762 | QualType ASTNodeImporter::VisitRecordType(const RecordType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1763 | RecordDecl *ToDecl |
| 1764 | = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl())); |
| 1765 | if (!ToDecl) |
| 1766 | return QualType(); |
| 1767 | |
| 1768 | return Importer.getToContext().getTagDeclType(ToDecl); |
| 1769 | } |
| 1770 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1771 | QualType ASTNodeImporter::VisitEnumType(const EnumType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1772 | EnumDecl *ToDecl |
| 1773 | = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl())); |
| 1774 | if (!ToDecl) |
| 1775 | return QualType(); |
| 1776 | |
| 1777 | return Importer.getToContext().getTagDeclType(ToDecl); |
| 1778 | } |
| 1779 | |
Sean Callanan | 72fe085 | 2015-04-02 23:50:08 +0000 | [diff] [blame] | 1780 | QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) { |
| 1781 | QualType FromModifiedType = T->getModifiedType(); |
| 1782 | QualType FromEquivalentType = T->getEquivalentType(); |
| 1783 | QualType ToModifiedType; |
| 1784 | QualType ToEquivalentType; |
| 1785 | |
| 1786 | if (!FromModifiedType.isNull()) { |
| 1787 | ToModifiedType = Importer.Import(FromModifiedType); |
| 1788 | if (ToModifiedType.isNull()) |
| 1789 | return QualType(); |
| 1790 | } |
| 1791 | if (!FromEquivalentType.isNull()) { |
| 1792 | ToEquivalentType = Importer.Import(FromEquivalentType); |
| 1793 | if (ToEquivalentType.isNull()) |
| 1794 | return QualType(); |
| 1795 | } |
| 1796 | |
| 1797 | return Importer.getToContext().getAttributedType(T->getAttrKind(), |
| 1798 | ToModifiedType, ToEquivalentType); |
| 1799 | } |
| 1800 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1801 | QualType ASTNodeImporter::VisitTemplateSpecializationType( |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1802 | const TemplateSpecializationType *T) { |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1803 | TemplateName ToTemplate = Importer.Import(T->getTemplateName()); |
| 1804 | if (ToTemplate.isNull()) |
| 1805 | return QualType(); |
| 1806 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1807 | SmallVector<TemplateArgument, 2> ToTemplateArgs; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1808 | if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs)) |
| 1809 | return QualType(); |
| 1810 | |
| 1811 | QualType ToCanonType; |
| 1812 | if (!QualType(T, 0).isCanonical()) { |
| 1813 | QualType FromCanonType |
| 1814 | = Importer.getFromContext().getCanonicalType(QualType(T, 0)); |
| 1815 | ToCanonType =Importer.Import(FromCanonType); |
| 1816 | if (ToCanonType.isNull()) |
| 1817 | return QualType(); |
| 1818 | } |
| 1819 | return Importer.getToContext().getTemplateSpecializationType(ToTemplate, |
| 1820 | ToTemplateArgs.data(), |
| 1821 | ToTemplateArgs.size(), |
| 1822 | ToCanonType); |
| 1823 | } |
| 1824 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1825 | QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1826 | NestedNameSpecifier *ToQualifier = nullptr; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1827 | // Note: the qualifier in an ElaboratedType is optional. |
| 1828 | if (T->getQualifier()) { |
| 1829 | ToQualifier = Importer.Import(T->getQualifier()); |
| 1830 | if (!ToQualifier) |
| 1831 | return QualType(); |
| 1832 | } |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1833 | |
| 1834 | QualType ToNamedType = Importer.Import(T->getNamedType()); |
| 1835 | if (ToNamedType.isNull()) |
| 1836 | return QualType(); |
| 1837 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1838 | return Importer.getToContext().getElaboratedType(T->getKeyword(), |
| 1839 | ToQualifier, ToNamedType); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1842 | QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1843 | ObjCInterfaceDecl *Class |
| 1844 | = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl())); |
| 1845 | if (!Class) |
| 1846 | return QualType(); |
| 1847 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1848 | return Importer.getToContext().getObjCInterfaceType(Class); |
| 1849 | } |
| 1850 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1851 | QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1852 | QualType ToBaseType = Importer.Import(T->getBaseType()); |
| 1853 | if (ToBaseType.isNull()) |
| 1854 | return QualType(); |
| 1855 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1856 | SmallVector<QualType, 4> TypeArgs; |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 1857 | for (auto TypeArg : T->getTypeArgsAsWritten()) { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1858 | QualType ImportedTypeArg = Importer.Import(TypeArg); |
| 1859 | if (ImportedTypeArg.isNull()) |
| 1860 | return QualType(); |
| 1861 | |
| 1862 | TypeArgs.push_back(ImportedTypeArg); |
| 1863 | } |
| 1864 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1865 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
Aaron Ballman | 1683f7b | 2014-03-17 15:55:30 +0000 | [diff] [blame] | 1866 | for (auto *P : T->quals()) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1867 | ObjCProtocolDecl *Protocol |
Aaron Ballman | 1683f7b | 2014-03-17 15:55:30 +0000 | [diff] [blame] | 1868 | = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P)); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1869 | if (!Protocol) |
| 1870 | return QualType(); |
| 1871 | Protocols.push_back(Protocol); |
| 1872 | } |
| 1873 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1874 | return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs, |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 1875 | Protocols, |
| 1876 | T->isKindOfTypeAsWritten()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1879 | QualType |
| 1880 | ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1881 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 1882 | if (ToPointeeType.isNull()) |
| 1883 | return QualType(); |
| 1884 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1885 | return Importer.getToContext().getObjCObjectPointerType(ToPointeeType); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 1888 | //---------------------------------------------------------------------------- |
| 1889 | // Import Declarations |
| 1890 | //---------------------------------------------------------------------------- |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 1891 | bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC, |
| 1892 | DeclContext *&LexicalDC, |
| 1893 | DeclarationName &Name, |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 1894 | NamedDecl *&ToD, |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 1895 | SourceLocation &Loc) { |
| 1896 | // Import the context of this declaration. |
| 1897 | DC = Importer.ImportContext(D->getDeclContext()); |
| 1898 | if (!DC) |
| 1899 | return true; |
| 1900 | |
| 1901 | LexicalDC = DC; |
| 1902 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 1903 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 1904 | if (!LexicalDC) |
| 1905 | return true; |
| 1906 | } |
| 1907 | |
| 1908 | // Import the name of this declaration. |
| 1909 | Name = Importer.Import(D->getDeclName()); |
| 1910 | if (D->getDeclName() && !Name) |
| 1911 | return true; |
| 1912 | |
| 1913 | // Import the location of this declaration. |
| 1914 | Loc = Importer.Import(D->getLocation()); |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 1915 | ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D)); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 1916 | return false; |
| 1917 | } |
| 1918 | |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 1919 | void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) { |
| 1920 | if (!FromD) |
| 1921 | return; |
| 1922 | |
| 1923 | if (!ToD) { |
| 1924 | ToD = Importer.Import(FromD); |
| 1925 | if (!ToD) |
| 1926 | return; |
| 1927 | } |
| 1928 | |
| 1929 | if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) { |
| 1930 | if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) { |
Sean Callanan | 19dfc93 | 2013-01-11 23:17:47 +0000 | [diff] [blame] | 1931 | if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) { |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 1932 | ImportDefinition(FromRecord, ToRecord); |
| 1933 | } |
| 1934 | } |
| 1935 | return; |
| 1936 | } |
| 1937 | |
| 1938 | if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) { |
| 1939 | if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) { |
| 1940 | if (FromEnum->getDefinition() && !ToEnum->getDefinition()) { |
| 1941 | ImportDefinition(FromEnum, ToEnum); |
| 1942 | } |
| 1943 | } |
| 1944 | return; |
| 1945 | } |
| 1946 | } |
| 1947 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1948 | void |
| 1949 | ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From, |
| 1950 | DeclarationNameInfo& To) { |
| 1951 | // NOTE: To.Name and To.Loc are already imported. |
| 1952 | // We only have to import To.LocInfo. |
| 1953 | switch (To.getName().getNameKind()) { |
| 1954 | case DeclarationName::Identifier: |
| 1955 | case DeclarationName::ObjCZeroArgSelector: |
| 1956 | case DeclarationName::ObjCOneArgSelector: |
| 1957 | case DeclarationName::ObjCMultiArgSelector: |
| 1958 | case DeclarationName::CXXUsingDirective: |
| 1959 | return; |
| 1960 | |
| 1961 | case DeclarationName::CXXOperatorName: { |
| 1962 | SourceRange Range = From.getCXXOperatorNameRange(); |
| 1963 | To.setCXXOperatorNameRange(Importer.Import(Range)); |
| 1964 | return; |
| 1965 | } |
| 1966 | case DeclarationName::CXXLiteralOperatorName: { |
| 1967 | SourceLocation Loc = From.getCXXLiteralOperatorNameLoc(); |
| 1968 | To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc)); |
| 1969 | return; |
| 1970 | } |
| 1971 | case DeclarationName::CXXConstructorName: |
| 1972 | case DeclarationName::CXXDestructorName: |
| 1973 | case DeclarationName::CXXConversionFunctionName: { |
| 1974 | TypeSourceInfo *FromTInfo = From.getNamedTypeInfo(); |
| 1975 | To.setNamedTypeInfo(Importer.Import(FromTInfo)); |
| 1976 | return; |
| 1977 | } |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1978 | } |
Douglas Gregor | 07216d1 | 2011-11-02 20:52:01 +0000 | [diff] [blame] | 1979 | llvm_unreachable("Unknown name kind."); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 1982 | void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) { |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 1983 | if (Importer.isMinimalImport() && !ForceImport) { |
Sean Callanan | 81d577c | 2011-07-22 23:46:03 +0000 | [diff] [blame] | 1984 | Importer.ImportContext(FromDC); |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 1985 | return; |
| 1986 | } |
| 1987 | |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 1988 | for (auto *From : FromDC->decls()) |
| 1989 | Importer.Import(From); |
Douglas Gregor | 968d633 | 2010-02-21 18:24:45 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 1992 | bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To, |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 1993 | ImportDefinitionKind Kind) { |
| 1994 | if (To->getDefinition() || To->isBeingDefined()) { |
| 1995 | if (Kind == IDK_Everything) |
| 1996 | ImportDeclContext(From, /*ForceImport=*/true); |
| 1997 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1998 | return false; |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 1999 | } |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2000 | |
| 2001 | To->startDefinition(); |
| 2002 | |
| 2003 | // Add base classes. |
| 2004 | if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) { |
| 2005 | CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From); |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 2006 | |
| 2007 | struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data(); |
| 2008 | struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data(); |
| 2009 | ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor; |
Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 2010 | ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers; |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 2011 | ToData.Aggregate = FromData.Aggregate; |
| 2012 | ToData.PlainOldData = FromData.PlainOldData; |
| 2013 | ToData.Empty = FromData.Empty; |
| 2014 | ToData.Polymorphic = FromData.Polymorphic; |
| 2015 | ToData.Abstract = FromData.Abstract; |
| 2016 | ToData.IsStandardLayout = FromData.IsStandardLayout; |
| 2017 | ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases; |
| 2018 | ToData.HasPrivateFields = FromData.HasPrivateFields; |
| 2019 | ToData.HasProtectedFields = FromData.HasProtectedFields; |
| 2020 | ToData.HasPublicFields = FromData.HasPublicFields; |
| 2021 | ToData.HasMutableFields = FromData.HasMutableFields; |
Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 2022 | ToData.HasVariantMembers = FromData.HasVariantMembers; |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 2023 | ToData.HasOnlyCMembers = FromData.HasOnlyCMembers; |
Richard Smith | e2648ba | 2012-05-07 01:07:30 +0000 | [diff] [blame] | 2024 | ToData.HasInClassInitializer = FromData.HasInClassInitializer; |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 2025 | ToData.HasUninitializedReferenceMember |
| 2026 | = FromData.HasUninitializedReferenceMember; |
Nico Weber | 6a6376b | 2016-02-19 01:52:46 +0000 | [diff] [blame] | 2027 | ToData.HasUninitializedFields = FromData.HasUninitializedFields; |
Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 2028 | ToData.NeedOverloadResolutionForMoveConstructor |
| 2029 | = FromData.NeedOverloadResolutionForMoveConstructor; |
| 2030 | ToData.NeedOverloadResolutionForMoveAssignment |
| 2031 | = FromData.NeedOverloadResolutionForMoveAssignment; |
| 2032 | ToData.NeedOverloadResolutionForDestructor |
| 2033 | = FromData.NeedOverloadResolutionForDestructor; |
| 2034 | ToData.DefaultedMoveConstructorIsDeleted |
| 2035 | = FromData.DefaultedMoveConstructorIsDeleted; |
| 2036 | ToData.DefaultedMoveAssignmentIsDeleted |
| 2037 | = FromData.DefaultedMoveAssignmentIsDeleted; |
| 2038 | ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted; |
Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 2039 | ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers; |
| 2040 | ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor; |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 2041 | ToData.HasConstexprNonCopyMoveConstructor |
| 2042 | = FromData.HasConstexprNonCopyMoveConstructor; |
Nico Weber | 72c57f4 | 2016-02-24 20:58:14 +0000 | [diff] [blame^] | 2043 | ToData.HasDefaultedDefaultConstructor |
| 2044 | = FromData.HasDefaultedDefaultConstructor; |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 2045 | ToData.DefaultedDefaultConstructorIsConstexpr |
| 2046 | = FromData.DefaultedDefaultConstructorIsConstexpr; |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 2047 | ToData.HasConstexprDefaultConstructor |
| 2048 | = FromData.HasConstexprDefaultConstructor; |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 2049 | ToData.HasNonLiteralTypeFieldsOrBases |
| 2050 | = FromData.HasNonLiteralTypeFieldsOrBases; |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 2051 | // ComputedVisibleConversions not imported. |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 2052 | ToData.UserProvidedDefaultConstructor |
| 2053 | = FromData.UserProvidedDefaultConstructor; |
Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 2054 | ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers; |
Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 2055 | ToData.ImplicitCopyConstructorHasConstParam |
| 2056 | = FromData.ImplicitCopyConstructorHasConstParam; |
| 2057 | ToData.ImplicitCopyAssignmentHasConstParam |
| 2058 | = FromData.ImplicitCopyAssignmentHasConstParam; |
| 2059 | ToData.HasDeclaredCopyConstructorWithConstParam |
| 2060 | = FromData.HasDeclaredCopyConstructorWithConstParam; |
| 2061 | ToData.HasDeclaredCopyAssignmentWithConstParam |
| 2062 | = FromData.HasDeclaredCopyAssignmentWithConstParam; |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 2063 | ToData.IsLambda = FromData.IsLambda; |
| 2064 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2065 | SmallVector<CXXBaseSpecifier *, 4> Bases; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2066 | for (const auto &Base1 : FromCXX->bases()) { |
| 2067 | QualType T = Importer.Import(Base1.getType()); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2068 | if (T.isNull()) |
Douglas Gregor | 96303ea | 2010-12-02 19:33:37 +0000 | [diff] [blame] | 2069 | return true; |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 2070 | |
| 2071 | SourceLocation EllipsisLoc; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2072 | if (Base1.isPackExpansion()) |
| 2073 | EllipsisLoc = Importer.Import(Base1.getEllipsisLoc()); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2074 | |
| 2075 | // Ensure that we have a definition for the base. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2076 | ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl()); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2077 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2078 | Bases.push_back( |
| 2079 | new (Importer.getToContext()) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2080 | CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()), |
| 2081 | Base1.isVirtual(), |
| 2082 | Base1.isBaseOfClass(), |
| 2083 | Base1.getAccessSpecifierAsWritten(), |
| 2084 | Importer.Import(Base1.getTypeSourceInfo()), |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 2085 | EllipsisLoc)); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2086 | } |
| 2087 | if (!Bases.empty()) |
Craig Topper | e6337e1 | 2015-12-25 00:36:02 +0000 | [diff] [blame] | 2088 | ToCXX->setBases(Bases.data(), Bases.size()); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 2091 | if (shouldForceImportDeclContext(Kind)) |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 2092 | ImportDeclContext(From, /*ForceImport=*/true); |
| 2093 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2094 | To->completeDefinition(); |
Douglas Gregor | 96303ea | 2010-12-02 19:33:37 +0000 | [diff] [blame] | 2095 | return false; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2098 | bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To, |
| 2099 | ImportDefinitionKind Kind) { |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2100 | if (To->getAnyInitializer()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2101 | return false; |
| 2102 | |
| 2103 | // FIXME: Can we really import any initializer? Alternatively, we could force |
| 2104 | // ourselves to import every declaration of a variable and then only use |
| 2105 | // getInit() here. |
| 2106 | To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer()))); |
| 2107 | |
| 2108 | // FIXME: Other bits to merge? |
| 2109 | |
| 2110 | return false; |
| 2111 | } |
| 2112 | |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2113 | bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 2114 | ImportDefinitionKind Kind) { |
| 2115 | if (To->getDefinition() || To->isBeingDefined()) { |
| 2116 | if (Kind == IDK_Everything) |
| 2117 | ImportDeclContext(From, /*ForceImport=*/true); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2118 | return false; |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 2119 | } |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2120 | |
| 2121 | To->startDefinition(); |
| 2122 | |
| 2123 | QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From)); |
| 2124 | if (T.isNull()) |
| 2125 | return true; |
| 2126 | |
| 2127 | QualType ToPromotionType = Importer.Import(From->getPromotionType()); |
| 2128 | if (ToPromotionType.isNull()) |
| 2129 | return true; |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 2130 | |
| 2131 | if (shouldForceImportDeclContext(Kind)) |
| 2132 | ImportDeclContext(From, /*ForceImport=*/true); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2133 | |
| 2134 | // FIXME: we might need to merge the number of positive or negative bits |
| 2135 | // if the enumerator lists don't match. |
| 2136 | To->completeDefinition(T, ToPromotionType, |
| 2137 | From->getNumPositiveBits(), |
| 2138 | From->getNumNegativeBits()); |
| 2139 | return false; |
| 2140 | } |
| 2141 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 2142 | TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList( |
| 2143 | TemplateParameterList *Params) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2144 | SmallVector<NamedDecl *, 4> ToParams; |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 2145 | ToParams.reserve(Params->size()); |
| 2146 | for (TemplateParameterList::iterator P = Params->begin(), |
| 2147 | PEnd = Params->end(); |
| 2148 | P != PEnd; ++P) { |
| 2149 | Decl *To = Importer.Import(*P); |
| 2150 | if (!To) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2151 | return nullptr; |
| 2152 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 2153 | ToParams.push_back(cast<NamedDecl>(To)); |
| 2154 | } |
| 2155 | |
| 2156 | return TemplateParameterList::Create(Importer.getToContext(), |
| 2157 | Importer.Import(Params->getTemplateLoc()), |
| 2158 | Importer.Import(Params->getLAngleLoc()), |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 2159 | ToParams, |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 2160 | Importer.Import(Params->getRAngleLoc())); |
| 2161 | } |
| 2162 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2163 | TemplateArgument |
| 2164 | ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) { |
| 2165 | switch (From.getKind()) { |
| 2166 | case TemplateArgument::Null: |
| 2167 | return TemplateArgument(); |
| 2168 | |
| 2169 | case TemplateArgument::Type: { |
| 2170 | QualType ToType = Importer.Import(From.getAsType()); |
| 2171 | if (ToType.isNull()) |
| 2172 | return TemplateArgument(); |
| 2173 | return TemplateArgument(ToType); |
| 2174 | } |
| 2175 | |
| 2176 | case TemplateArgument::Integral: { |
| 2177 | QualType ToType = Importer.Import(From.getIntegralType()); |
| 2178 | if (ToType.isNull()) |
| 2179 | return TemplateArgument(); |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 2180 | return TemplateArgument(From, ToType); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2181 | } |
| 2182 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2183 | case TemplateArgument::Declaration: { |
David Blaikie | 3c7dd6b | 2014-10-22 19:54:16 +0000 | [diff] [blame] | 2184 | ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl())); |
| 2185 | QualType ToType = Importer.Import(From.getParamTypeForDecl()); |
| 2186 | if (!To || ToType.isNull()) |
| 2187 | return TemplateArgument(); |
| 2188 | return TemplateArgument(To, ToType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
| 2191 | case TemplateArgument::NullPtr: { |
| 2192 | QualType ToType = Importer.Import(From.getNullPtrType()); |
| 2193 | if (ToType.isNull()) |
| 2194 | return TemplateArgument(); |
| 2195 | return TemplateArgument(ToType, /*isNullPtr*/true); |
| 2196 | } |
| 2197 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2198 | case TemplateArgument::Template: { |
| 2199 | TemplateName ToTemplate = Importer.Import(From.getAsTemplate()); |
| 2200 | if (ToTemplate.isNull()) |
| 2201 | return TemplateArgument(); |
| 2202 | |
| 2203 | return TemplateArgument(ToTemplate); |
| 2204 | } |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2205 | |
| 2206 | case TemplateArgument::TemplateExpansion: { |
| 2207 | TemplateName ToTemplate |
| 2208 | = Importer.Import(From.getAsTemplateOrTemplatePattern()); |
| 2209 | if (ToTemplate.isNull()) |
| 2210 | return TemplateArgument(); |
| 2211 | |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 2212 | return TemplateArgument(ToTemplate, From.getNumTemplateExpansions()); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2213 | } |
| 2214 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2215 | case TemplateArgument::Expression: |
| 2216 | if (Expr *ToExpr = Importer.Import(From.getAsExpr())) |
| 2217 | return TemplateArgument(ToExpr); |
| 2218 | return TemplateArgument(); |
| 2219 | |
| 2220 | case TemplateArgument::Pack: { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2221 | SmallVector<TemplateArgument, 2> ToPack; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2222 | ToPack.reserve(From.pack_size()); |
| 2223 | if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack)) |
| 2224 | return TemplateArgument(); |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 2225 | |
| 2226 | return TemplateArgument( |
| 2227 | llvm::makeArrayRef(ToPack).copy(Importer.getToContext())); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2228 | } |
| 2229 | } |
| 2230 | |
| 2231 | llvm_unreachable("Invalid template argument kind"); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2232 | } |
| 2233 | |
| 2234 | bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs, |
| 2235 | unsigned NumFromArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2236 | SmallVectorImpl<TemplateArgument> &ToArgs) { |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2237 | for (unsigned I = 0; I != NumFromArgs; ++I) { |
| 2238 | TemplateArgument To = ImportTemplateArgument(FromArgs[I]); |
| 2239 | if (To.isNull() && !FromArgs[I].isNull()) |
| 2240 | return true; |
| 2241 | |
| 2242 | ToArgs.push_back(To); |
| 2243 | } |
| 2244 | |
| 2245 | return false; |
| 2246 | } |
| 2247 | |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2248 | bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord, |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2249 | RecordDecl *ToRecord, bool Complain) { |
Sean Callanan | c665c9e | 2013-10-09 21:45:11 +0000 | [diff] [blame] | 2250 | // Eliminate a potential failure point where we attempt to re-import |
| 2251 | // something we're trying to import while completing ToRecord. |
| 2252 | Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord); |
| 2253 | if (ToOrigin) { |
| 2254 | RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin); |
| 2255 | if (ToOriginRecord) |
| 2256 | ToRecord = ToOriginRecord; |
| 2257 | } |
| 2258 | |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 2259 | StructuralEquivalenceContext Ctx(Importer.getFromContext(), |
Sean Callanan | c665c9e | 2013-10-09 21:45:11 +0000 | [diff] [blame] | 2260 | ToRecord->getASTContext(), |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2261 | Importer.getNonEquivalentDecls(), |
| 2262 | false, Complain); |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 2263 | return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2264 | } |
| 2265 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2266 | bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar, |
| 2267 | bool Complain) { |
| 2268 | StructuralEquivalenceContext Ctx( |
| 2269 | Importer.getFromContext(), Importer.getToContext(), |
| 2270 | Importer.getNonEquivalentDecls(), false, Complain); |
| 2271 | return Ctx.IsStructurallyEquivalent(FromVar, ToVar); |
| 2272 | } |
| 2273 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2274 | bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) { |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 2275 | StructuralEquivalenceContext Ctx(Importer.getFromContext(), |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2276 | Importer.getToContext(), |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2277 | Importer.getNonEquivalentDecls()); |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 2278 | return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2279 | } |
| 2280 | |
Douglas Gregor | 9115508 | 2012-11-14 22:29:20 +0000 | [diff] [blame] | 2281 | bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC, |
| 2282 | EnumConstantDecl *ToEC) |
| 2283 | { |
| 2284 | const llvm::APSInt &FromVal = FromEC->getInitVal(); |
| 2285 | const llvm::APSInt &ToVal = ToEC->getInitVal(); |
| 2286 | |
| 2287 | return FromVal.isSigned() == ToVal.isSigned() && |
| 2288 | FromVal.getBitWidth() == ToVal.getBitWidth() && |
| 2289 | FromVal == ToVal; |
| 2290 | } |
| 2291 | |
| 2292 | bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From, |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 2293 | ClassTemplateDecl *To) { |
| 2294 | StructuralEquivalenceContext Ctx(Importer.getFromContext(), |
| 2295 | Importer.getToContext(), |
| 2296 | Importer.getNonEquivalentDecls()); |
| 2297 | return Ctx.IsStructurallyEquivalent(From, To); |
| 2298 | } |
| 2299 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2300 | bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From, |
| 2301 | VarTemplateDecl *To) { |
| 2302 | StructuralEquivalenceContext Ctx(Importer.getFromContext(), |
| 2303 | Importer.getToContext(), |
| 2304 | Importer.getNonEquivalentDecls()); |
| 2305 | return Ctx.IsStructurallyEquivalent(From, To); |
| 2306 | } |
| 2307 | |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 2308 | Decl *ASTNodeImporter::VisitDecl(Decl *D) { |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 2309 | Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 2310 | << D->getDeclKindName(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2311 | return nullptr; |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 2312 | } |
| 2313 | |
Sean Callanan | 6519827 | 2011-11-17 23:20:56 +0000 | [diff] [blame] | 2314 | Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 2315 | TranslationUnitDecl *ToD = |
| 2316 | Importer.getToContext().getTranslationUnitDecl(); |
| 2317 | |
| 2318 | Importer.Imported(D, ToD); |
| 2319 | |
| 2320 | return ToD; |
| 2321 | } |
| 2322 | |
Argyrios Kyrtzidis | 544ea71 | 2016-02-18 23:08:36 +0000 | [diff] [blame] | 2323 | Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) { |
| 2324 | |
| 2325 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 2326 | SourceLocation ColonLoc = Importer.Import(D->getColonLoc()); |
| 2327 | |
| 2328 | // Import the context of this declaration. |
| 2329 | DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| 2330 | if (!DC) |
| 2331 | return nullptr; |
| 2332 | |
| 2333 | AccessSpecDecl *accessSpecDecl |
| 2334 | = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(), |
| 2335 | DC, Loc, ColonLoc); |
| 2336 | |
| 2337 | if (!accessSpecDecl) |
| 2338 | return nullptr; |
| 2339 | |
| 2340 | // Lexical DeclContext and Semantic DeclContext |
| 2341 | // is always the same for the accessSpec. |
| 2342 | accessSpecDecl->setLexicalDeclContext(DC); |
| 2343 | DC->addDeclInternal(accessSpecDecl); |
| 2344 | |
| 2345 | return accessSpecDecl; |
| 2346 | } |
| 2347 | |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2348 | Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) { |
| 2349 | // Import the major distinguishing characteristics of this namespace. |
| 2350 | DeclContext *DC, *LexicalDC; |
| 2351 | DeclarationName Name; |
| 2352 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2353 | NamedDecl *ToD; |
| 2354 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2355 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2356 | if (ToD) |
| 2357 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2358 | |
| 2359 | NamespaceDecl *MergeWithNamespace = nullptr; |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2360 | if (!Name) { |
| 2361 | // This is an anonymous namespace. Adopt an existing anonymous |
| 2362 | // namespace if we can. |
| 2363 | // FIXME: Not testable. |
| 2364 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC)) |
| 2365 | MergeWithNamespace = TU->getAnonymousNamespace(); |
| 2366 | else |
| 2367 | MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace(); |
| 2368 | } else { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2369 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2370 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 2371 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2372 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2373 | if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace)) |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2374 | continue; |
| 2375 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2376 | if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) { |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2377 | MergeWithNamespace = FoundNS; |
| 2378 | ConflictingDecls.clear(); |
| 2379 | break; |
| 2380 | } |
| 2381 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2382 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
| 2385 | if (!ConflictingDecls.empty()) { |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 2386 | Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace, |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2387 | ConflictingDecls.data(), |
| 2388 | ConflictingDecls.size()); |
| 2389 | } |
| 2390 | } |
| 2391 | |
| 2392 | // Create the "to" namespace, if needed. |
| 2393 | NamespaceDecl *ToNamespace = MergeWithNamespace; |
| 2394 | if (!ToNamespace) { |
Abramo Bagnara | b5545be | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 2395 | ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 2396 | D->isInline(), |
Abramo Bagnara | b5545be | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 2397 | Importer.Import(D->getLocStart()), |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 2398 | Loc, Name.getAsIdentifierInfo(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2399 | /*PrevDecl=*/nullptr); |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2400 | ToNamespace->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2401 | LexicalDC->addDeclInternal(ToNamespace); |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2402 | |
| 2403 | // If this is an anonymous namespace, register it as the anonymous |
| 2404 | // namespace within its context. |
| 2405 | if (!Name) { |
| 2406 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC)) |
| 2407 | TU->setAnonymousNamespace(ToNamespace); |
| 2408 | else |
| 2409 | cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace); |
| 2410 | } |
| 2411 | } |
| 2412 | Importer.Imported(D, ToNamespace); |
| 2413 | |
| 2414 | ImportDeclContext(D); |
| 2415 | |
| 2416 | return ToNamespace; |
| 2417 | } |
| 2418 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2419 | Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) { |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2420 | // Import the major distinguishing characteristics of this typedef. |
| 2421 | DeclContext *DC, *LexicalDC; |
| 2422 | DeclarationName Name; |
| 2423 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2424 | NamedDecl *ToD; |
| 2425 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2426 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2427 | if (ToD) |
| 2428 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2429 | |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2430 | // If this typedef is not in block scope, determine whether we've |
| 2431 | // seen a typedef with the same name (that we can merge with) or any |
| 2432 | // other entity by that name (which name lookup could conflict with). |
| 2433 | if (!DC->isFunctionOrMethod()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2434 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2435 | unsigned IDNS = Decl::IDNS_Ordinary; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2436 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 2437 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2438 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2439 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2440 | continue; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2441 | if (TypedefNameDecl *FoundTypedef = |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2442 | dyn_cast<TypedefNameDecl>(FoundDecls[I])) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2443 | if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(), |
| 2444 | FoundTypedef->getUnderlyingType())) |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2445 | return Importer.Imported(D, FoundTypedef); |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2446 | } |
| 2447 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2448 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2449 | } |
| 2450 | |
| 2451 | if (!ConflictingDecls.empty()) { |
| 2452 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2453 | ConflictingDecls.data(), |
| 2454 | ConflictingDecls.size()); |
| 2455 | if (!Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2456 | return nullptr; |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2457 | } |
| 2458 | } |
| 2459 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2460 | // Import the underlying type of this typedef; |
| 2461 | QualType T = Importer.Import(D->getUnderlyingType()); |
| 2462 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2463 | return nullptr; |
| 2464 | |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2465 | // Create the new typedef node. |
| 2466 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 2467 | SourceLocation StartL = Importer.Import(D->getLocStart()); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2468 | TypedefNameDecl *ToTypedef; |
| 2469 | if (IsAlias) |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 2470 | ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC, |
| 2471 | StartL, Loc, |
| 2472 | Name.getAsIdentifierInfo(), |
| 2473 | TInfo); |
| 2474 | else |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2475 | ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC, |
| 2476 | StartL, Loc, |
| 2477 | Name.getAsIdentifierInfo(), |
| 2478 | TInfo); |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 2479 | |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2480 | ToTypedef->setAccess(D->getAccess()); |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2481 | ToTypedef->setLexicalDeclContext(LexicalDC); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2482 | Importer.Imported(D, ToTypedef); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2483 | LexicalDC->addDeclInternal(ToTypedef); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2484 | |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2485 | return ToTypedef; |
| 2486 | } |
| 2487 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2488 | Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) { |
| 2489 | return VisitTypedefNameDecl(D, /*IsAlias=*/false); |
| 2490 | } |
| 2491 | |
| 2492 | Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| 2493 | return VisitTypedefNameDecl(D, /*IsAlias=*/true); |
| 2494 | } |
| 2495 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2496 | Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) { |
| 2497 | // Import the major distinguishing characteristics of this enum. |
| 2498 | DeclContext *DC, *LexicalDC; |
| 2499 | DeclarationName Name; |
| 2500 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2501 | NamedDecl *ToD; |
| 2502 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2503 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2504 | if (ToD) |
| 2505 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2506 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2507 | // Figure out what enum name we're looking for. |
| 2508 | unsigned IDNS = Decl::IDNS_Tag; |
| 2509 | DeclarationName SearchName = Name; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2510 | if (!SearchName && D->getTypedefNameForAnonDecl()) { |
| 2511 | SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName()); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2512 | IDNS = Decl::IDNS_Ordinary; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2513 | } else if (Importer.getToContext().getLangOpts().CPlusPlus) |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2514 | IDNS |= Decl::IDNS_Ordinary; |
| 2515 | |
| 2516 | // We may already have an enum of the same name; try to find and match it. |
| 2517 | if (!DC->isFunctionOrMethod() && SearchName) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2518 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2519 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 2520 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2521 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2522 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2523 | continue; |
| 2524 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2525 | Decl *Found = FoundDecls[I]; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2526 | if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) { |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2527 | if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) |
| 2528 | Found = Tag->getDecl(); |
| 2529 | } |
| 2530 | |
| 2531 | if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) { |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2532 | if (IsStructuralMatch(D, FoundEnum)) |
| 2533 | return Importer.Imported(D, FoundEnum); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2534 | } |
| 2535 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2536 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
| 2539 | if (!ConflictingDecls.empty()) { |
| 2540 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2541 | ConflictingDecls.data(), |
| 2542 | ConflictingDecls.size()); |
| 2543 | } |
| 2544 | } |
| 2545 | |
| 2546 | // Create the enum declaration. |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2547 | EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, |
| 2548 | Importer.Import(D->getLocStart()), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2549 | Loc, Name.getAsIdentifierInfo(), nullptr, |
Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2550 | D->isScoped(), D->isScopedUsingClassTag(), |
| 2551 | D->isFixed()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2552 | // Import the qualifier, if any. |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2553 | D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2554 | D2->setAccess(D->getAccess()); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2555 | D2->setLexicalDeclContext(LexicalDC); |
| 2556 | Importer.Imported(D, D2); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2557 | LexicalDC->addDeclInternal(D2); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2558 | |
| 2559 | // Import the integer type. |
| 2560 | QualType ToIntegerType = Importer.Import(D->getIntegerType()); |
| 2561 | if (ToIntegerType.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2562 | return nullptr; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2563 | D2->setIntegerType(ToIntegerType); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2564 | |
| 2565 | // Import the definition |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2566 | if (D->isCompleteDefinition() && ImportDefinition(D, D2)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2567 | return nullptr; |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2568 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2569 | return D2; |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2572 | Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) { |
| 2573 | // If this record has a definition in the translation unit we're coming from, |
| 2574 | // but this particular declaration is not that definition, import the |
| 2575 | // definition and map to that. |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 2576 | TagDecl *Definition = D->getDefinition(); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2577 | if (Definition && Definition != D) { |
| 2578 | Decl *ImportedDef = Importer.Import(Definition); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2579 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2580 | return nullptr; |
| 2581 | |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2582 | return Importer.Imported(D, ImportedDef); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2583 | } |
| 2584 | |
| 2585 | // Import the major distinguishing characteristics of this record. |
| 2586 | DeclContext *DC, *LexicalDC; |
| 2587 | DeclarationName Name; |
| 2588 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2589 | NamedDecl *ToD; |
| 2590 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2591 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2592 | if (ToD) |
| 2593 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2594 | |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2595 | // Figure out what structure name we're looking for. |
| 2596 | unsigned IDNS = Decl::IDNS_Tag; |
| 2597 | DeclarationName SearchName = Name; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2598 | if (!SearchName && D->getTypedefNameForAnonDecl()) { |
| 2599 | SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName()); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2600 | IDNS = Decl::IDNS_Ordinary; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2601 | } else if (Importer.getToContext().getLangOpts().CPlusPlus) |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2602 | IDNS |= Decl::IDNS_Ordinary; |
| 2603 | |
| 2604 | // 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] | 2605 | RecordDecl *AdoptDecl = nullptr; |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2606 | if (!DC->isFunctionOrMethod()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2607 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2608 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 2609 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2610 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2611 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2612 | continue; |
| 2613 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2614 | Decl *Found = FoundDecls[I]; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2615 | if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) { |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2616 | if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) |
| 2617 | Found = Tag->getDecl(); |
| 2618 | } |
| 2619 | |
| 2620 | if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) { |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 2621 | if (D->isAnonymousStructOrUnion() && |
| 2622 | FoundRecord->isAnonymousStructOrUnion()) { |
| 2623 | // If both anonymous structs/unions are in a record context, make sure |
| 2624 | // they occur in the same location in the context records. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 2625 | if (Optional<unsigned> Index1 |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 2626 | = findAnonymousStructOrUnionIndex(D)) { |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 2627 | if (Optional<unsigned> Index2 = |
| 2628 | findAnonymousStructOrUnionIndex(FoundRecord)) { |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 2629 | if (*Index1 != *Index2) |
| 2630 | continue; |
| 2631 | } |
| 2632 | } |
| 2633 | } |
| 2634 | |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2635 | if (RecordDecl *FoundDef = FoundRecord->getDefinition()) { |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2636 | if ((SearchName && !D->isCompleteDefinition()) |
| 2637 | || (D->isCompleteDefinition() && |
| 2638 | D->isAnonymousStructOrUnion() |
| 2639 | == FoundDef->isAnonymousStructOrUnion() && |
| 2640 | IsStructuralMatch(D, FoundDef))) { |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2641 | // The record types structurally match, or the "from" translation |
| 2642 | // unit only had a forward declaration anyway; call it the same |
| 2643 | // function. |
| 2644 | // FIXME: For C++, we should also merge methods here. |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2645 | return Importer.Imported(D, FoundDef); |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2646 | } |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2647 | } else if (!D->isCompleteDefinition()) { |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2648 | // We have a forward declaration of this type, so adopt that forward |
| 2649 | // declaration rather than building a new one. |
Sean Callanan | c94711c | 2014-03-04 18:11:50 +0000 | [diff] [blame] | 2650 | |
| 2651 | // If one or both can be completed from external storage then try one |
| 2652 | // last time to complete and compare them before doing this. |
| 2653 | |
| 2654 | if (FoundRecord->hasExternalLexicalStorage() && |
| 2655 | !FoundRecord->isCompleteDefinition()) |
| 2656 | FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord); |
| 2657 | if (D->hasExternalLexicalStorage()) |
| 2658 | D->getASTContext().getExternalSource()->CompleteType(D); |
| 2659 | |
| 2660 | if (FoundRecord->isCompleteDefinition() && |
| 2661 | D->isCompleteDefinition() && |
| 2662 | !IsStructuralMatch(D, FoundRecord)) |
| 2663 | continue; |
| 2664 | |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2665 | AdoptDecl = FoundRecord; |
| 2666 | continue; |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2667 | } else if (!SearchName) { |
| 2668 | continue; |
| 2669 | } |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2670 | } |
| 2671 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2672 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2673 | } |
| 2674 | |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2675 | if (!ConflictingDecls.empty() && SearchName) { |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2676 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2677 | ConflictingDecls.data(), |
| 2678 | ConflictingDecls.size()); |
| 2679 | } |
| 2680 | } |
| 2681 | |
| 2682 | // Create the record declaration. |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2683 | RecordDecl *D2 = AdoptDecl; |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2684 | SourceLocation StartLoc = Importer.Import(D->getLocStart()); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2685 | if (!D2) { |
John McCall | 1c70e99 | 2010-06-03 19:28:45 +0000 | [diff] [blame] | 2686 | if (isa<CXXRecordDecl>(D)) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2687 | CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(), |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2688 | D->getTagKind(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2689 | DC, StartLoc, Loc, |
| 2690 | Name.getAsIdentifierInfo()); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2691 | D2 = D2CXX; |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2692 | D2->setAccess(D->getAccess()); |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2693 | } else { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2694 | D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2695 | DC, StartLoc, Loc, Name.getAsIdentifierInfo()); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2696 | } |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2697 | |
| 2698 | D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2699 | D2->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2700 | LexicalDC->addDeclInternal(D2); |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2701 | if (D->isAnonymousStructOrUnion()) |
| 2702 | D2->setAnonymousStructOrUnion(true); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2703 | } |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2704 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2705 | Importer.Imported(D, D2); |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2706 | |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 2707 | if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2708 | return nullptr; |
| 2709 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2710 | return D2; |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2711 | } |
| 2712 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2713 | Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 2714 | // Import the major distinguishing characteristics of this enumerator. |
| 2715 | DeclContext *DC, *LexicalDC; |
| 2716 | DeclarationName Name; |
| 2717 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2718 | NamedDecl *ToD; |
| 2719 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2720 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2721 | if (ToD) |
| 2722 | return ToD; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2723 | |
| 2724 | QualType T = Importer.Import(D->getType()); |
| 2725 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2726 | return nullptr; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2727 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2728 | // Determine whether there are any other declarations with the same name and |
| 2729 | // in the same context. |
| 2730 | if (!LexicalDC->isFunctionOrMethod()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2731 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2732 | unsigned IDNS = Decl::IDNS_Ordinary; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2733 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 2734 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2735 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2736 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2737 | continue; |
Douglas Gregor | 9115508 | 2012-11-14 22:29:20 +0000 | [diff] [blame] | 2738 | |
| 2739 | if (EnumConstantDecl *FoundEnumConstant |
| 2740 | = dyn_cast<EnumConstantDecl>(FoundDecls[I])) { |
| 2741 | if (IsStructuralMatch(D, FoundEnumConstant)) |
| 2742 | return Importer.Imported(D, FoundEnumConstant); |
| 2743 | } |
| 2744 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2745 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2746 | } |
| 2747 | |
| 2748 | if (!ConflictingDecls.empty()) { |
| 2749 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2750 | ConflictingDecls.data(), |
| 2751 | ConflictingDecls.size()); |
| 2752 | if (!Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2753 | return nullptr; |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2754 | } |
| 2755 | } |
| 2756 | |
| 2757 | Expr *Init = Importer.Import(D->getInitExpr()); |
| 2758 | if (D->getInitExpr() && !Init) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2759 | return nullptr; |
| 2760 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2761 | EnumConstantDecl *ToEnumerator |
| 2762 | = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc, |
| 2763 | Name.getAsIdentifierInfo(), T, |
| 2764 | Init, D->getInitVal()); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2765 | ToEnumerator->setAccess(D->getAccess()); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2766 | ToEnumerator->setLexicalDeclContext(LexicalDC); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2767 | Importer.Imported(D, ToEnumerator); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2768 | LexicalDC->addDeclInternal(ToEnumerator); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2769 | return ToEnumerator; |
| 2770 | } |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2771 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2772 | Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) { |
| 2773 | // Import the major distinguishing characteristics of this function. |
| 2774 | DeclContext *DC, *LexicalDC; |
| 2775 | DeclarationName Name; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2776 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2777 | NamedDecl *ToD; |
| 2778 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2779 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2780 | if (ToD) |
| 2781 | return ToD; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2782 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2783 | // Try to find a function in our own ("to") context with the same name, same |
| 2784 | // type, and in the same context as the function we're importing. |
| 2785 | if (!LexicalDC->isFunctionOrMethod()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2786 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +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 | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2792 | continue; |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2793 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2794 | if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) { |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 2795 | if (FoundFunction->hasExternalFormalLinkage() && |
| 2796 | D->hasExternalFormalLinkage()) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2797 | if (Importer.IsStructurallyEquivalent(D->getType(), |
| 2798 | FoundFunction->getType())) { |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2799 | // FIXME: Actually try to merge the body and other attributes. |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2800 | return Importer.Imported(D, FoundFunction); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2801 | } |
| 2802 | |
| 2803 | // FIXME: Check for overloading more carefully, e.g., by boosting |
| 2804 | // Sema::IsOverload out to the AST library. |
| 2805 | |
| 2806 | // Function overloading is okay in C++. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2807 | if (Importer.getToContext().getLangOpts().CPlusPlus) |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2808 | continue; |
| 2809 | |
| 2810 | // Complain about inconsistent function types. |
| 2811 | Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent) |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2812 | << Name << D->getType() << FoundFunction->getType(); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2813 | Importer.ToDiag(FoundFunction->getLocation(), |
| 2814 | diag::note_odr_value_here) |
| 2815 | << FoundFunction->getType(); |
| 2816 | } |
| 2817 | } |
| 2818 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2819 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2820 | } |
| 2821 | |
| 2822 | if (!ConflictingDecls.empty()) { |
| 2823 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2824 | ConflictingDecls.data(), |
| 2825 | ConflictingDecls.size()); |
| 2826 | if (!Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2827 | return nullptr; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2828 | } |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 2829 | } |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2830 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2831 | DeclarationNameInfo NameInfo(Name, Loc); |
| 2832 | // Import additional name location/type info. |
| 2833 | ImportDeclarationNameLoc(D->getNameInfo(), NameInfo); |
| 2834 | |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 2835 | QualType FromTy = D->getType(); |
| 2836 | bool usedDifferentExceptionSpec = false; |
| 2837 | |
| 2838 | if (const FunctionProtoType * |
| 2839 | FromFPT = D->getType()->getAs<FunctionProtoType>()) { |
| 2840 | FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo(); |
| 2841 | // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the |
| 2842 | // FunctionDecl that we are importing the FunctionProtoType for. |
| 2843 | // To avoid an infinite recursion when importing, create the FunctionDecl |
| 2844 | // with a simplified function type and update it afterwards. |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 2845 | if (FromEPI.ExceptionSpec.SourceDecl || |
| 2846 | FromEPI.ExceptionSpec.SourceTemplate || |
| 2847 | FromEPI.ExceptionSpec.NoexceptExpr) { |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 2848 | FunctionProtoType::ExtProtoInfo DefaultEPI; |
| 2849 | FromTy = Importer.getFromContext().getFunctionType( |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2850 | FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI); |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 2851 | usedDifferentExceptionSpec = true; |
| 2852 | } |
| 2853 | } |
| 2854 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2855 | // Import the type. |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 2856 | QualType T = Importer.Import(FromTy); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2857 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2858 | return nullptr; |
| 2859 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2860 | // Import the function parameters. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2861 | SmallVector<ParmVarDecl *, 8> Parameters; |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 2862 | for (auto P : D->params()) { |
| 2863 | ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P)); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2864 | if (!ToP) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2865 | return nullptr; |
| 2866 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2867 | Parameters.push_back(ToP); |
| 2868 | } |
| 2869 | |
| 2870 | // Create the imported function. |
| 2871 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2872 | FunctionDecl *ToFunction = nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2873 | SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart()); |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2874 | if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) { |
| 2875 | ToFunction = CXXConstructorDecl::Create(Importer.getToContext(), |
| 2876 | cast<CXXRecordDecl>(DC), |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2877 | InnerLocStart, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2878 | NameInfo, T, TInfo, |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2879 | FromConstructor->isExplicit(), |
| 2880 | D->isInlineSpecified(), |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 2881 | D->isImplicit(), |
| 2882 | D->isConstexpr()); |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2883 | } else if (isa<CXXDestructorDecl>(D)) { |
| 2884 | ToFunction = CXXDestructorDecl::Create(Importer.getToContext(), |
| 2885 | cast<CXXRecordDecl>(DC), |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2886 | InnerLocStart, |
Craig Silverstein | af8808d | 2010-10-21 00:44:50 +0000 | [diff] [blame] | 2887 | NameInfo, T, TInfo, |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2888 | D->isInlineSpecified(), |
| 2889 | D->isImplicit()); |
| 2890 | } else if (CXXConversionDecl *FromConversion |
| 2891 | = dyn_cast<CXXConversionDecl>(D)) { |
| 2892 | ToFunction = CXXConversionDecl::Create(Importer.getToContext(), |
| 2893 | cast<CXXRecordDecl>(DC), |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2894 | InnerLocStart, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2895 | NameInfo, T, TInfo, |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2896 | D->isInlineSpecified(), |
Douglas Gregor | f2f0806 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 2897 | FromConversion->isExplicit(), |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 2898 | D->isConstexpr(), |
Douglas Gregor | f2f0806 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 2899 | Importer.Import(D->getLocEnd())); |
Douglas Gregor | a50ad13 | 2010-11-29 16:04:58 +0000 | [diff] [blame] | 2900 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 2901 | ToFunction = CXXMethodDecl::Create(Importer.getToContext(), |
| 2902 | cast<CXXRecordDecl>(DC), |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2903 | InnerLocStart, |
Douglas Gregor | a50ad13 | 2010-11-29 16:04:58 +0000 | [diff] [blame] | 2904 | NameInfo, T, TInfo, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2905 | Method->getStorageClass(), |
Douglas Gregor | f2f0806 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 2906 | Method->isInlineSpecified(), |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 2907 | D->isConstexpr(), |
Douglas Gregor | f2f0806 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 2908 | Importer.Import(D->getLocEnd())); |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2909 | } else { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2910 | ToFunction = FunctionDecl::Create(Importer.getToContext(), DC, |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2911 | InnerLocStart, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2912 | NameInfo, T, TInfo, D->getStorageClass(), |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2913 | D->isInlineSpecified(), |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 2914 | D->hasWrittenPrototype(), |
| 2915 | D->isConstexpr()); |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2916 | } |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2917 | |
| 2918 | // Import the qualifier, if any. |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2919 | ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2920 | ToFunction->setAccess(D->getAccess()); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2921 | ToFunction->setLexicalDeclContext(LexicalDC); |
John McCall | 08432c8 | 2011-01-27 02:37:01 +0000 | [diff] [blame] | 2922 | ToFunction->setVirtualAsWritten(D->isVirtualAsWritten()); |
| 2923 | ToFunction->setTrivial(D->isTrivial()); |
| 2924 | ToFunction->setPure(D->isPure()); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2925 | Importer.Imported(D, ToFunction); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 2926 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2927 | // Set the parameters. |
| 2928 | for (unsigned I = 0, N = Parameters.size(); I != N; ++I) { |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2929 | Parameters[I]->setOwningFunction(ToFunction); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2930 | ToFunction->addDeclInternal(Parameters[I]); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2931 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 2932 | ToFunction->setParams(Parameters); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2933 | |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 2934 | if (usedDifferentExceptionSpec) { |
| 2935 | // Update FunctionProtoType::ExtProtoInfo. |
| 2936 | QualType T = Importer.Import(D->getType()); |
| 2937 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2938 | return nullptr; |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 2939 | ToFunction->setType(T); |
Argyrios Kyrtzidis | b41791d | 2012-09-22 01:58:06 +0000 | [diff] [blame] | 2940 | } |
| 2941 | |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2942 | // Import the body, if any. |
| 2943 | if (Stmt *FromBody = D->getBody()) { |
| 2944 | if (Stmt *ToBody = Importer.Import(FromBody)) { |
| 2945 | ToFunction->setBody(ToBody); |
| 2946 | } |
| 2947 | } |
| 2948 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2949 | // FIXME: Other bits to merge? |
Douglas Gregor | 0eaa2bf | 2010-10-01 23:55:07 +0000 | [diff] [blame] | 2950 | |
| 2951 | // Add this function to the lexical context. |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2952 | LexicalDC->addDeclInternal(ToFunction); |
Douglas Gregor | 0eaa2bf | 2010-10-01 23:55:07 +0000 | [diff] [blame] | 2953 | |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2954 | return ToFunction; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2955 | } |
| 2956 | |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2957 | Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) { |
| 2958 | return VisitFunctionDecl(D); |
| 2959 | } |
| 2960 | |
| 2961 | Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
| 2962 | return VisitCXXMethodDecl(D); |
| 2963 | } |
| 2964 | |
| 2965 | Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
| 2966 | return VisitCXXMethodDecl(D); |
| 2967 | } |
| 2968 | |
| 2969 | Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) { |
| 2970 | return VisitCXXMethodDecl(D); |
| 2971 | } |
| 2972 | |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 2973 | static unsigned getFieldIndex(Decl *F) { |
| 2974 | RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext()); |
| 2975 | if (!Owner) |
| 2976 | return 0; |
| 2977 | |
| 2978 | unsigned Index = 1; |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 2979 | for (const auto *D : Owner->noload_decls()) { |
| 2980 | if (D == F) |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 2981 | return Index; |
| 2982 | |
| 2983 | if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D)) |
| 2984 | ++Index; |
| 2985 | } |
| 2986 | |
| 2987 | return Index; |
| 2988 | } |
| 2989 | |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2990 | Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) { |
| 2991 | // Import the major distinguishing characteristics of a variable. |
| 2992 | DeclContext *DC, *LexicalDC; |
| 2993 | DeclarationName Name; |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2994 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2995 | NamedDecl *ToD; |
| 2996 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2997 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 2998 | if (ToD) |
| 2999 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3000 | |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 3001 | // Determine whether we've already imported this field. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3002 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 3003 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3004 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 3005 | if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) { |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 3006 | // For anonymous fields, match up by index. |
| 3007 | if (!Name && getFieldIndex(D) != getFieldIndex(FoundField)) |
| 3008 | continue; |
| 3009 | |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 3010 | if (Importer.IsStructurallyEquivalent(D->getType(), |
| 3011 | FoundField->getType())) { |
| 3012 | Importer.Imported(D, FoundField); |
| 3013 | return FoundField; |
| 3014 | } |
| 3015 | |
| 3016 | Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent) |
| 3017 | << Name << D->getType() << FoundField->getType(); |
| 3018 | Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) |
| 3019 | << FoundField->getType(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3020 | return nullptr; |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 3021 | } |
| 3022 | } |
| 3023 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3024 | // Import the type. |
| 3025 | QualType T = Importer.Import(D->getType()); |
| 3026 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3027 | return nullptr; |
| 3028 | |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 3029 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 3030 | Expr *BitWidth = Importer.Import(D->getBitWidth()); |
| 3031 | if (!BitWidth && D->getBitWidth()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3032 | return nullptr; |
| 3033 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3034 | FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC, |
| 3035 | Importer.Import(D->getInnerLocStart()), |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 3036 | Loc, Name.getAsIdentifierInfo(), |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3037 | T, TInfo, BitWidth, D->isMutable(), |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3038 | D->getInClassInitStyle()); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 3039 | ToField->setAccess(D->getAccess()); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 3040 | ToField->setLexicalDeclContext(LexicalDC); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3041 | if (ToField->hasInClassInitializer()) |
| 3042 | ToField->setInClassInitializer(D->getInClassInitializer()); |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 3043 | ToField->setImplicit(D->isImplicit()); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 3044 | Importer.Imported(D, ToField); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3045 | LexicalDC->addDeclInternal(ToField); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 3046 | return ToField; |
| 3047 | } |
| 3048 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3049 | Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) { |
| 3050 | // Import the major distinguishing characteristics of a variable. |
| 3051 | DeclContext *DC, *LexicalDC; |
| 3052 | DeclarationName Name; |
| 3053 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3054 | NamedDecl *ToD; |
| 3055 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3056 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3057 | if (ToD) |
| 3058 | return ToD; |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3059 | |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 3060 | // Determine whether we've already imported this field. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3061 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 3062 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3063 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 3064 | if (IndirectFieldDecl *FoundField |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3065 | = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) { |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 3066 | // For anonymous indirect fields, match up by index. |
| 3067 | if (!Name && getFieldIndex(D) != getFieldIndex(FoundField)) |
| 3068 | continue; |
| 3069 | |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 3070 | if (Importer.IsStructurallyEquivalent(D->getType(), |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 3071 | FoundField->getType(), |
David Blaikie | 7d17010 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 3072 | !Name.isEmpty())) { |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 3073 | Importer.Imported(D, FoundField); |
| 3074 | return FoundField; |
| 3075 | } |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 3076 | |
| 3077 | // If there are more anonymous fields to check, continue. |
| 3078 | if (!Name && I < N-1) |
| 3079 | continue; |
| 3080 | |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 3081 | Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent) |
| 3082 | << Name << D->getType() << FoundField->getType(); |
| 3083 | Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) |
| 3084 | << FoundField->getType(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3085 | return nullptr; |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 3086 | } |
| 3087 | } |
| 3088 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3089 | // Import the type. |
| 3090 | QualType T = Importer.Import(D->getType()); |
| 3091 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3092 | return nullptr; |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3093 | |
| 3094 | NamedDecl **NamedChain = |
| 3095 | new (Importer.getToContext())NamedDecl*[D->getChainingSize()]; |
| 3096 | |
| 3097 | unsigned i = 0; |
Aaron Ballman | 29c9460 | 2014-03-07 18:36:15 +0000 | [diff] [blame] | 3098 | for (auto *PI : D->chain()) { |
Aaron Ballman | 1391608 | 2014-03-07 18:11:58 +0000 | [diff] [blame] | 3099 | Decl *D = Importer.Import(PI); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3100 | if (!D) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3101 | return nullptr; |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3102 | NamedChain[i++] = cast<NamedDecl>(D); |
| 3103 | } |
| 3104 | |
| 3105 | IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create( |
Aaron Ballman | 260995b | 2014-10-15 16:58:18 +0000 | [diff] [blame] | 3106 | Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T, |
| 3107 | NamedChain, D->getChainingSize()); |
| 3108 | |
| 3109 | for (const auto *Attr : D->attrs()) |
| 3110 | ToIndirectField->addAttr(Attr->clone(Importer.getToContext())); |
| 3111 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3112 | ToIndirectField->setAccess(D->getAccess()); |
| 3113 | ToIndirectField->setLexicalDeclContext(LexicalDC); |
| 3114 | Importer.Imported(D, ToIndirectField); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3115 | LexicalDC->addDeclInternal(ToIndirectField); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3116 | return ToIndirectField; |
| 3117 | } |
| 3118 | |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3119 | Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) { |
| 3120 | // Import the major distinguishing characteristics of an ivar. |
| 3121 | DeclContext *DC, *LexicalDC; |
| 3122 | DeclarationName Name; |
| 3123 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3124 | NamedDecl *ToD; |
| 3125 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3126 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3127 | if (ToD) |
| 3128 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3129 | |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3130 | // Determine whether we've already imported this ivar |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3131 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 3132 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3133 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 3134 | if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) { |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3135 | if (Importer.IsStructurallyEquivalent(D->getType(), |
| 3136 | FoundIvar->getType())) { |
| 3137 | Importer.Imported(D, FoundIvar); |
| 3138 | return FoundIvar; |
| 3139 | } |
| 3140 | |
| 3141 | Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent) |
| 3142 | << Name << D->getType() << FoundIvar->getType(); |
| 3143 | Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here) |
| 3144 | << FoundIvar->getType(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3145 | return nullptr; |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3146 | } |
| 3147 | } |
| 3148 | |
| 3149 | // Import the type. |
| 3150 | QualType T = Importer.Import(D->getType()); |
| 3151 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3152 | return nullptr; |
| 3153 | |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3154 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 3155 | Expr *BitWidth = Importer.Import(D->getBitWidth()); |
| 3156 | if (!BitWidth && D->getBitWidth()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3157 | return nullptr; |
| 3158 | |
Daniel Dunbar | fe3ead7 | 2010-04-02 20:10:03 +0000 | [diff] [blame] | 3159 | ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(), |
| 3160 | cast<ObjCContainerDecl>(DC), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3161 | Importer.Import(D->getInnerLocStart()), |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3162 | Loc, Name.getAsIdentifierInfo(), |
| 3163 | T, TInfo, D->getAccessControl(), |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 3164 | BitWidth, D->getSynthesize()); |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3165 | ToIvar->setLexicalDeclContext(LexicalDC); |
| 3166 | Importer.Imported(D, ToIvar); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3167 | LexicalDC->addDeclInternal(ToIvar); |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3168 | return ToIvar; |
| 3169 | |
| 3170 | } |
| 3171 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3172 | Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) { |
| 3173 | // Import the major distinguishing characteristics of a variable. |
| 3174 | DeclContext *DC, *LexicalDC; |
| 3175 | DeclarationName Name; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3176 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3177 | NamedDecl *ToD; |
| 3178 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3179 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3180 | if (ToD) |
| 3181 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3182 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3183 | // Try to find a variable in our own ("to") context with the same name and |
| 3184 | // in the same context as the variable we're importing. |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 3185 | if (D->isFileVarDecl()) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3186 | VarDecl *MergeWithVar = nullptr; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3187 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3188 | unsigned IDNS = Decl::IDNS_Ordinary; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3189 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 3190 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3191 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 3192 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3193 | continue; |
| 3194 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3195 | if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) { |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3196 | // 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] | 3197 | if (FoundVar->hasExternalFormalLinkage() && |
| 3198 | D->hasExternalFormalLinkage()) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3199 | if (Importer.IsStructurallyEquivalent(D->getType(), |
| 3200 | FoundVar->getType())) { |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3201 | MergeWithVar = FoundVar; |
| 3202 | break; |
| 3203 | } |
| 3204 | |
Douglas Gregor | 56521c5 | 2010-02-12 17:23:39 +0000 | [diff] [blame] | 3205 | const ArrayType *FoundArray |
| 3206 | = Importer.getToContext().getAsArrayType(FoundVar->getType()); |
| 3207 | const ArrayType *TArray |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3208 | = Importer.getToContext().getAsArrayType(D->getType()); |
Douglas Gregor | 56521c5 | 2010-02-12 17:23:39 +0000 | [diff] [blame] | 3209 | if (FoundArray && TArray) { |
| 3210 | if (isa<IncompleteArrayType>(FoundArray) && |
| 3211 | isa<ConstantArrayType>(TArray)) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3212 | // Import the type. |
| 3213 | QualType T = Importer.Import(D->getType()); |
| 3214 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3215 | return nullptr; |
| 3216 | |
Douglas Gregor | 56521c5 | 2010-02-12 17:23:39 +0000 | [diff] [blame] | 3217 | FoundVar->setType(T); |
| 3218 | MergeWithVar = FoundVar; |
| 3219 | break; |
| 3220 | } else if (isa<IncompleteArrayType>(TArray) && |
| 3221 | isa<ConstantArrayType>(FoundArray)) { |
| 3222 | MergeWithVar = FoundVar; |
| 3223 | break; |
Douglas Gregor | 2fbe558 | 2010-02-10 17:16:49 +0000 | [diff] [blame] | 3224 | } |
| 3225 | } |
| 3226 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3227 | Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent) |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3228 | << Name << D->getType() << FoundVar->getType(); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3229 | Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here) |
| 3230 | << FoundVar->getType(); |
| 3231 | } |
| 3232 | } |
| 3233 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3234 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3235 | } |
| 3236 | |
| 3237 | if (MergeWithVar) { |
| 3238 | // An equivalent variable with external linkage has been found. Link |
| 3239 | // the two declarations, then merge them. |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 3240 | Importer.Imported(D, MergeWithVar); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3241 | |
| 3242 | if (VarDecl *DDef = D->getDefinition()) { |
| 3243 | if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) { |
| 3244 | Importer.ToDiag(ExistingDef->getLocation(), |
| 3245 | diag::err_odr_variable_multiple_def) |
| 3246 | << Name; |
| 3247 | Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here); |
| 3248 | } else { |
| 3249 | Expr *Init = Importer.Import(DDef->getInit()); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3250 | MergeWithVar->setInit(Init); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 3251 | if (DDef->isInitKnownICE()) { |
| 3252 | EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt(); |
| 3253 | Eval->CheckedICE = true; |
| 3254 | Eval->IsICE = DDef->isInitICE(); |
| 3255 | } |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3256 | } |
| 3257 | } |
| 3258 | |
| 3259 | return MergeWithVar; |
| 3260 | } |
| 3261 | |
| 3262 | if (!ConflictingDecls.empty()) { |
| 3263 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 3264 | ConflictingDecls.data(), |
| 3265 | ConflictingDecls.size()); |
| 3266 | if (!Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3267 | return nullptr; |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3268 | } |
| 3269 | } |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 3270 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3271 | // Import the type. |
| 3272 | QualType T = Importer.Import(D->getType()); |
| 3273 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3274 | return nullptr; |
| 3275 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3276 | // Create the imported variable. |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 3277 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3278 | VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, |
| 3279 | Importer.Import(D->getInnerLocStart()), |
| 3280 | Loc, Name.getAsIdentifierInfo(), |
| 3281 | T, TInfo, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3282 | D->getStorageClass()); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3283 | ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 3284 | ToVar->setAccess(D->getAccess()); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 3285 | ToVar->setLexicalDeclContext(LexicalDC); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 3286 | Importer.Imported(D, ToVar); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3287 | LexicalDC->addDeclInternal(ToVar); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 3288 | |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3289 | if (!D->isFileVarDecl() && |
| 3290 | D->isUsed()) |
| 3291 | ToVar->setIsUsed(); |
| 3292 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3293 | // Merge the initializer. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3294 | if (ImportDefinition(D, ToVar)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3295 | return nullptr; |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3296 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3297 | return ToVar; |
| 3298 | } |
| 3299 | |
Douglas Gregor | 8b228d7 | 2010-02-17 21:22:52 +0000 | [diff] [blame] | 3300 | Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) { |
| 3301 | // Parameters are created in the translation unit's context, then moved |
| 3302 | // into the function declaration's context afterward. |
| 3303 | DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); |
| 3304 | |
| 3305 | // Import the name of this declaration. |
| 3306 | DeclarationName Name = Importer.Import(D->getDeclName()); |
| 3307 | if (D->getDeclName() && !Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3308 | return nullptr; |
| 3309 | |
Douglas Gregor | 8b228d7 | 2010-02-17 21:22:52 +0000 | [diff] [blame] | 3310 | // Import the location of this declaration. |
| 3311 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 3312 | |
| 3313 | // Import the parameter's type. |
| 3314 | QualType T = Importer.Import(D->getType()); |
| 3315 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3316 | return nullptr; |
| 3317 | |
Douglas Gregor | 8b228d7 | 2010-02-17 21:22:52 +0000 | [diff] [blame] | 3318 | // Create the imported parameter. |
| 3319 | ImplicitParamDecl *ToParm |
| 3320 | = ImplicitParamDecl::Create(Importer.getToContext(), DC, |
| 3321 | Loc, Name.getAsIdentifierInfo(), |
| 3322 | T); |
| 3323 | return Importer.Imported(D, ToParm); |
| 3324 | } |
| 3325 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3326 | Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) { |
| 3327 | // Parameters are created in the translation unit's context, then moved |
| 3328 | // into the function declaration's context afterward. |
| 3329 | DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); |
| 3330 | |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 3331 | // Import the name of this declaration. |
| 3332 | DeclarationName Name = Importer.Import(D->getDeclName()); |
| 3333 | if (D->getDeclName() && !Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3334 | return nullptr; |
| 3335 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3336 | // Import the location of this declaration. |
| 3337 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 3338 | |
| 3339 | // Import the parameter's type. |
| 3340 | QualType T = Importer.Import(D->getType()); |
| 3341 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3342 | return nullptr; |
| 3343 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3344 | // Create the imported parameter. |
| 3345 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 3346 | ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3347 | Importer.Import(D->getInnerLocStart()), |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3348 | Loc, Name.getAsIdentifierInfo(), |
| 3349 | T, TInfo, D->getStorageClass(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3350 | /*FIXME: Default argument*/nullptr); |
John McCall | f3cd665 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 3351 | ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg()); |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3352 | |
| 3353 | if (D->isUsed()) |
| 3354 | ToParm->setIsUsed(); |
| 3355 | |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 3356 | return Importer.Imported(D, ToParm); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3357 | } |
| 3358 | |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3359 | Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) { |
| 3360 | // Import the major distinguishing characteristics of a method. |
| 3361 | DeclContext *DC, *LexicalDC; |
| 3362 | DeclarationName Name; |
| 3363 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3364 | NamedDecl *ToD; |
| 3365 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3366 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3367 | if (ToD) |
| 3368 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3369 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3370 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 3371 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3372 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 3373 | if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) { |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3374 | if (FoundMethod->isInstanceMethod() != D->isInstanceMethod()) |
| 3375 | continue; |
| 3376 | |
| 3377 | // Check return types. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3378 | if (!Importer.IsStructurallyEquivalent(D->getReturnType(), |
| 3379 | FoundMethod->getReturnType())) { |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3380 | Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3381 | << D->isInstanceMethod() << Name << D->getReturnType() |
| 3382 | << FoundMethod->getReturnType(); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3383 | Importer.ToDiag(FoundMethod->getLocation(), |
| 3384 | diag::note_odr_objc_method_here) |
| 3385 | << D->isInstanceMethod() << Name; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3386 | return nullptr; |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3387 | } |
| 3388 | |
| 3389 | // Check the number of parameters. |
| 3390 | if (D->param_size() != FoundMethod->param_size()) { |
| 3391 | Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent) |
| 3392 | << D->isInstanceMethod() << Name |
| 3393 | << D->param_size() << FoundMethod->param_size(); |
| 3394 | Importer.ToDiag(FoundMethod->getLocation(), |
| 3395 | diag::note_odr_objc_method_here) |
| 3396 | << D->isInstanceMethod() << Name; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3397 | return nullptr; |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3398 | } |
| 3399 | |
| 3400 | // Check parameter types. |
| 3401 | for (ObjCMethodDecl::param_iterator P = D->param_begin(), |
| 3402 | PEnd = D->param_end(), FoundP = FoundMethod->param_begin(); |
| 3403 | P != PEnd; ++P, ++FoundP) { |
| 3404 | if (!Importer.IsStructurallyEquivalent((*P)->getType(), |
| 3405 | (*FoundP)->getType())) { |
| 3406 | Importer.FromDiag((*P)->getLocation(), |
| 3407 | diag::err_odr_objc_method_param_type_inconsistent) |
| 3408 | << D->isInstanceMethod() << Name |
| 3409 | << (*P)->getType() << (*FoundP)->getType(); |
| 3410 | Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here) |
| 3411 | << (*FoundP)->getType(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3412 | return nullptr; |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3413 | } |
| 3414 | } |
| 3415 | |
| 3416 | // Check variadic/non-variadic. |
| 3417 | // Check the number of parameters. |
| 3418 | if (D->isVariadic() != FoundMethod->isVariadic()) { |
| 3419 | Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent) |
| 3420 | << D->isInstanceMethod() << Name; |
| 3421 | Importer.ToDiag(FoundMethod->getLocation(), |
| 3422 | diag::note_odr_objc_method_here) |
| 3423 | << D->isInstanceMethod() << Name; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3424 | return nullptr; |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3425 | } |
| 3426 | |
| 3427 | // FIXME: Any other bits we need to merge? |
| 3428 | return Importer.Imported(D, FoundMethod); |
| 3429 | } |
| 3430 | } |
| 3431 | |
| 3432 | // Import the result type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3433 | QualType ResultTy = Importer.Import(D->getReturnType()); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3434 | if (ResultTy.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3435 | return nullptr; |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3436 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3437 | TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo()); |
Douglas Gregor | 12852d9 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 3438 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3439 | ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create( |
| 3440 | Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()), |
| 3441 | Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(), |
| 3442 | D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(), |
| 3443 | D->getImplementationControl(), D->hasRelatedResultType()); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3444 | |
| 3445 | // FIXME: When we decide to merge method definitions, we'll need to |
| 3446 | // deal with implicit parameters. |
| 3447 | |
| 3448 | // Import the parameters |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3449 | SmallVector<ParmVarDecl *, 5> ToParams; |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 3450 | for (auto *FromP : D->params()) { |
| 3451 | ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP)); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3452 | if (!ToP) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3453 | return nullptr; |
| 3454 | |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3455 | ToParams.push_back(ToP); |
| 3456 | } |
| 3457 | |
| 3458 | // Set the parameters. |
| 3459 | for (unsigned I = 0, N = ToParams.size(); I != N; ++I) { |
| 3460 | ToParams[I]->setOwningFunction(ToMethod); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3461 | ToMethod->addDeclInternal(ToParams[I]); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3462 | } |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 3463 | SmallVector<SourceLocation, 12> SelLocs; |
| 3464 | D->getSelectorLocs(SelLocs); |
| 3465 | ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3466 | |
| 3467 | ToMethod->setLexicalDeclContext(LexicalDC); |
| 3468 | Importer.Imported(D, ToMethod); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3469 | LexicalDC->addDeclInternal(ToMethod); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3470 | return ToMethod; |
| 3471 | } |
| 3472 | |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 3473 | Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) { |
| 3474 | // Import the major distinguishing characteristics of a category. |
| 3475 | DeclContext *DC, *LexicalDC; |
| 3476 | DeclarationName Name; |
| 3477 | SourceLocation Loc; |
| 3478 | NamedDecl *ToD; |
| 3479 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 3480 | return nullptr; |
| 3481 | if (ToD) |
| 3482 | return ToD; |
| 3483 | |
| 3484 | TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo()); |
| 3485 | if (!BoundInfo) |
| 3486 | return nullptr; |
| 3487 | |
| 3488 | ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create( |
| 3489 | Importer.getToContext(), DC, |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 3490 | D->getVariance(), |
| 3491 | Importer.Import(D->getVarianceLoc()), |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 3492 | D->getIndex(), |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 3493 | Importer.Import(D->getLocation()), |
| 3494 | Name.getAsIdentifierInfo(), |
| 3495 | Importer.Import(D->getColonLoc()), |
| 3496 | BoundInfo); |
| 3497 | Importer.Imported(D, Result); |
| 3498 | Result->setLexicalDeclContext(LexicalDC); |
| 3499 | return Result; |
| 3500 | } |
| 3501 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3502 | Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) { |
| 3503 | // Import the major distinguishing characteristics of a category. |
| 3504 | DeclContext *DC, *LexicalDC; |
| 3505 | DeclarationName Name; |
| 3506 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3507 | NamedDecl *ToD; |
| 3508 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3509 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3510 | if (ToD) |
| 3511 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3512 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3513 | ObjCInterfaceDecl *ToInterface |
| 3514 | = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface())); |
| 3515 | if (!ToInterface) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3516 | return nullptr; |
| 3517 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3518 | // Determine if we've already encountered this category. |
| 3519 | ObjCCategoryDecl *MergeWithCategory |
| 3520 | = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo()); |
| 3521 | ObjCCategoryDecl *ToCategory = MergeWithCategory; |
| 3522 | if (!ToCategory) { |
| 3523 | ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC, |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 3524 | Importer.Import(D->getAtStartLoc()), |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3525 | Loc, |
| 3526 | Importer.Import(D->getCategoryNameLoc()), |
Argyrios Kyrtzidis | 3a5094b | 2011-08-30 19:43:26 +0000 | [diff] [blame] | 3527 | Name.getAsIdentifierInfo(), |
Fariborz Jahanian | a7765fe | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 3528 | ToInterface, |
Douglas Gregor | ab7f0b3 | 2015-07-07 06:20:12 +0000 | [diff] [blame] | 3529 | /*TypeParamList=*/nullptr, |
Fariborz Jahanian | a7765fe | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 3530 | Importer.Import(D->getIvarLBraceLoc()), |
| 3531 | Importer.Import(D->getIvarRBraceLoc())); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3532 | ToCategory->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3533 | LexicalDC->addDeclInternal(ToCategory); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3534 | Importer.Imported(D, ToCategory); |
Douglas Gregor | ab7f0b3 | 2015-07-07 06:20:12 +0000 | [diff] [blame] | 3535 | // Import the type parameter list after calling Imported, to avoid |
| 3536 | // loops when bringing in their DeclContext. |
| 3537 | ToCategory->setTypeParamList(ImportObjCTypeParamList( |
| 3538 | D->getTypeParamList())); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3539 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3540 | // Import protocols |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3541 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 3542 | SmallVector<SourceLocation, 4> ProtocolLocs; |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3543 | ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc |
| 3544 | = D->protocol_loc_begin(); |
| 3545 | for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(), |
| 3546 | FromProtoEnd = D->protocol_end(); |
| 3547 | FromProto != FromProtoEnd; |
| 3548 | ++FromProto, ++FromProtoLoc) { |
| 3549 | ObjCProtocolDecl *ToProto |
| 3550 | = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); |
| 3551 | if (!ToProto) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3552 | return nullptr; |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3553 | Protocols.push_back(ToProto); |
| 3554 | ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); |
| 3555 | } |
| 3556 | |
| 3557 | // FIXME: If we're merging, make sure that the protocol list is the same. |
| 3558 | ToCategory->setProtocolList(Protocols.data(), Protocols.size(), |
| 3559 | ProtocolLocs.data(), Importer.getToContext()); |
| 3560 | |
| 3561 | } else { |
| 3562 | Importer.Imported(D, ToCategory); |
| 3563 | } |
| 3564 | |
| 3565 | // Import all of the members of this category. |
Douglas Gregor | 968d633 | 2010-02-21 18:24:45 +0000 | [diff] [blame] | 3566 | ImportDeclContext(D); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3567 | |
| 3568 | // If we have an implementation, import it as well. |
| 3569 | if (D->getImplementation()) { |
| 3570 | ObjCCategoryImplDecl *Impl |
Douglas Gregor | 35fd7bc | 2010-12-08 16:41:55 +0000 | [diff] [blame] | 3571 | = cast_or_null<ObjCCategoryImplDecl>( |
| 3572 | Importer.Import(D->getImplementation())); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3573 | if (!Impl) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3574 | return nullptr; |
| 3575 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3576 | ToCategory->setImplementation(Impl); |
| 3577 | } |
| 3578 | |
| 3579 | return ToCategory; |
| 3580 | } |
| 3581 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3582 | bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From, |
| 3583 | ObjCProtocolDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 3584 | ImportDefinitionKind Kind) { |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3585 | if (To->getDefinition()) { |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 3586 | if (shouldForceImportDeclContext(Kind)) |
| 3587 | ImportDeclContext(From); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3588 | return false; |
| 3589 | } |
| 3590 | |
| 3591 | // Start the protocol definition |
| 3592 | To->startDefinition(); |
| 3593 | |
| 3594 | // Import protocols |
| 3595 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 3596 | SmallVector<SourceLocation, 4> ProtocolLocs; |
| 3597 | ObjCProtocolDecl::protocol_loc_iterator |
| 3598 | FromProtoLoc = From->protocol_loc_begin(); |
| 3599 | for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(), |
| 3600 | FromProtoEnd = From->protocol_end(); |
| 3601 | FromProto != FromProtoEnd; |
| 3602 | ++FromProto, ++FromProtoLoc) { |
| 3603 | ObjCProtocolDecl *ToProto |
| 3604 | = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); |
| 3605 | if (!ToProto) |
| 3606 | return true; |
| 3607 | Protocols.push_back(ToProto); |
| 3608 | ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); |
| 3609 | } |
| 3610 | |
| 3611 | // FIXME: If we're merging, make sure that the protocol list is the same. |
| 3612 | To->setProtocolList(Protocols.data(), Protocols.size(), |
| 3613 | ProtocolLocs.data(), Importer.getToContext()); |
| 3614 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 3615 | if (shouldForceImportDeclContext(Kind)) { |
| 3616 | // Import all of the members of this protocol. |
| 3617 | ImportDeclContext(From, /*ForceImport=*/true); |
| 3618 | } |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3619 | return false; |
| 3620 | } |
| 3621 | |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3622 | Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) { |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3623 | // If this protocol has a definition in the translation unit we're coming |
| 3624 | // from, but this particular declaration is not that definition, import the |
| 3625 | // definition and map to that. |
| 3626 | ObjCProtocolDecl *Definition = D->getDefinition(); |
| 3627 | if (Definition && Definition != D) { |
| 3628 | Decl *ImportedDef = Importer.Import(Definition); |
| 3629 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3630 | return nullptr; |
| 3631 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3632 | return Importer.Imported(D, ImportedDef); |
| 3633 | } |
| 3634 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3635 | // Import the major distinguishing characteristics of a protocol. |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3636 | DeclContext *DC, *LexicalDC; |
| 3637 | DeclarationName Name; |
| 3638 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3639 | NamedDecl *ToD; |
| 3640 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3641 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3642 | if (ToD) |
| 3643 | return ToD; |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3644 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3645 | ObjCProtocolDecl *MergeWithProtocol = nullptr; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3646 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 3647 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3648 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 3649 | if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol)) |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3650 | continue; |
| 3651 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3652 | if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I]))) |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3653 | break; |
| 3654 | } |
| 3655 | |
| 3656 | ObjCProtocolDecl *ToProto = MergeWithProtocol; |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3657 | if (!ToProto) { |
| 3658 | ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, |
| 3659 | Name.getAsIdentifierInfo(), Loc, |
| 3660 | Importer.Import(D->getAtStartLoc()), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3661 | /*PrevDecl=*/nullptr); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3662 | ToProto->setLexicalDeclContext(LexicalDC); |
| 3663 | LexicalDC->addDeclInternal(ToProto); |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3664 | } |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3665 | |
| 3666 | Importer.Imported(D, ToProto); |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3667 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3668 | if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3669 | return nullptr; |
| 3670 | |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3671 | return ToProto; |
| 3672 | } |
| 3673 | |
Sean Callanan | 0aae041 | 2014-12-10 00:00:37 +0000 | [diff] [blame] | 3674 | Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 3675 | DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| 3676 | DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 3677 | |
| 3678 | SourceLocation ExternLoc = Importer.Import(D->getExternLoc()); |
| 3679 | SourceLocation LangLoc = Importer.Import(D->getLocation()); |
| 3680 | |
| 3681 | bool HasBraces = D->hasBraces(); |
| 3682 | |
Sean Callanan | b12a855 | 2014-12-10 21:22:20 +0000 | [diff] [blame] | 3683 | LinkageSpecDecl *ToLinkageSpec = |
| 3684 | LinkageSpecDecl::Create(Importer.getToContext(), |
| 3685 | DC, |
| 3686 | ExternLoc, |
| 3687 | LangLoc, |
| 3688 | D->getLanguage(), |
| 3689 | HasBraces); |
Sean Callanan | 0aae041 | 2014-12-10 00:00:37 +0000 | [diff] [blame] | 3690 | |
| 3691 | if (HasBraces) { |
| 3692 | SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc()); |
| 3693 | ToLinkageSpec->setRBraceLoc(RBraceLoc); |
| 3694 | } |
| 3695 | |
| 3696 | ToLinkageSpec->setLexicalDeclContext(LexicalDC); |
| 3697 | LexicalDC->addDeclInternal(ToLinkageSpec); |
| 3698 | |
| 3699 | Importer.Imported(D, ToLinkageSpec); |
| 3700 | |
| 3701 | return ToLinkageSpec; |
| 3702 | } |
| 3703 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3704 | bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From, |
| 3705 | ObjCInterfaceDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 3706 | ImportDefinitionKind Kind) { |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3707 | if (To->getDefinition()) { |
| 3708 | // Check consistency of superclass. |
| 3709 | ObjCInterfaceDecl *FromSuper = From->getSuperClass(); |
| 3710 | if (FromSuper) { |
| 3711 | FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper)); |
| 3712 | if (!FromSuper) |
| 3713 | return true; |
| 3714 | } |
| 3715 | |
| 3716 | ObjCInterfaceDecl *ToSuper = To->getSuperClass(); |
| 3717 | if ((bool)FromSuper != (bool)ToSuper || |
| 3718 | (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) { |
| 3719 | Importer.ToDiag(To->getLocation(), |
| 3720 | diag::err_odr_objc_superclass_inconsistent) |
| 3721 | << To->getDeclName(); |
| 3722 | if (ToSuper) |
| 3723 | Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass) |
| 3724 | << To->getSuperClass()->getDeclName(); |
| 3725 | else |
| 3726 | Importer.ToDiag(To->getLocation(), |
| 3727 | diag::note_odr_objc_missing_superclass); |
| 3728 | if (From->getSuperClass()) |
| 3729 | Importer.FromDiag(From->getSuperClassLoc(), |
| 3730 | diag::note_odr_objc_superclass) |
| 3731 | << From->getSuperClass()->getDeclName(); |
| 3732 | else |
| 3733 | Importer.FromDiag(From->getLocation(), |
| 3734 | diag::note_odr_objc_missing_superclass); |
| 3735 | } |
| 3736 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 3737 | if (shouldForceImportDeclContext(Kind)) |
| 3738 | ImportDeclContext(From); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3739 | return false; |
| 3740 | } |
| 3741 | |
| 3742 | // Start the definition. |
| 3743 | To->startDefinition(); |
| 3744 | |
| 3745 | // If this class has a superclass, import it. |
| 3746 | if (From->getSuperClass()) { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3747 | TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo()); |
| 3748 | if (!SuperTInfo) |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3749 | return true; |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3750 | |
| 3751 | To->setSuperClass(SuperTInfo); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3752 | } |
| 3753 | |
| 3754 | // Import protocols |
| 3755 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 3756 | SmallVector<SourceLocation, 4> ProtocolLocs; |
| 3757 | ObjCInterfaceDecl::protocol_loc_iterator |
| 3758 | FromProtoLoc = From->protocol_loc_begin(); |
| 3759 | |
| 3760 | for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(), |
| 3761 | FromProtoEnd = From->protocol_end(); |
| 3762 | FromProto != FromProtoEnd; |
| 3763 | ++FromProto, ++FromProtoLoc) { |
| 3764 | ObjCProtocolDecl *ToProto |
| 3765 | = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); |
| 3766 | if (!ToProto) |
| 3767 | return true; |
| 3768 | Protocols.push_back(ToProto); |
| 3769 | ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); |
| 3770 | } |
| 3771 | |
| 3772 | // FIXME: If we're merging, make sure that the protocol list is the same. |
| 3773 | To->setProtocolList(Protocols.data(), Protocols.size(), |
| 3774 | ProtocolLocs.data(), Importer.getToContext()); |
| 3775 | |
| 3776 | // Import categories. When the categories themselves are imported, they'll |
| 3777 | // hook themselves into this interface. |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 3778 | for (auto *Cat : From->known_categories()) |
| 3779 | Importer.Import(Cat); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 3780 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3781 | // If we have an @implementation, import it as well. |
| 3782 | if (From->getImplementation()) { |
| 3783 | ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>( |
| 3784 | Importer.Import(From->getImplementation())); |
| 3785 | if (!Impl) |
| 3786 | return true; |
| 3787 | |
| 3788 | To->setImplementation(Impl); |
| 3789 | } |
| 3790 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 3791 | if (shouldForceImportDeclContext(Kind)) { |
| 3792 | // Import all of the members of this class. |
| 3793 | ImportDeclContext(From, /*ForceImport=*/true); |
| 3794 | } |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3795 | return false; |
| 3796 | } |
| 3797 | |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 3798 | ObjCTypeParamList * |
| 3799 | ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) { |
| 3800 | if (!list) |
| 3801 | return nullptr; |
| 3802 | |
| 3803 | SmallVector<ObjCTypeParamDecl *, 4> toTypeParams; |
| 3804 | for (auto fromTypeParam : *list) { |
| 3805 | auto toTypeParam = cast_or_null<ObjCTypeParamDecl>( |
| 3806 | Importer.Import(fromTypeParam)); |
| 3807 | if (!toTypeParam) |
| 3808 | return nullptr; |
| 3809 | |
| 3810 | toTypeParams.push_back(toTypeParam); |
| 3811 | } |
| 3812 | |
| 3813 | return ObjCTypeParamList::create(Importer.getToContext(), |
| 3814 | Importer.Import(list->getLAngleLoc()), |
| 3815 | toTypeParams, |
| 3816 | Importer.Import(list->getRAngleLoc())); |
| 3817 | } |
| 3818 | |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3819 | Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3820 | // If this class has a definition in the translation unit we're coming from, |
| 3821 | // but this particular declaration is not that definition, import the |
| 3822 | // definition and map to that. |
| 3823 | ObjCInterfaceDecl *Definition = D->getDefinition(); |
| 3824 | if (Definition && Definition != D) { |
| 3825 | Decl *ImportedDef = Importer.Import(Definition); |
| 3826 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3827 | return nullptr; |
| 3828 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3829 | return Importer.Imported(D, ImportedDef); |
| 3830 | } |
| 3831 | |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3832 | // Import the major distinguishing characteristics of an @interface. |
| 3833 | DeclContext *DC, *LexicalDC; |
| 3834 | DeclarationName Name; |
| 3835 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3836 | NamedDecl *ToD; |
| 3837 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3838 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3839 | if (ToD) |
| 3840 | return ToD; |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3841 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3842 | // Look for an existing interface with the same name. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3843 | ObjCInterfaceDecl *MergeWithIface = nullptr; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3844 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 3845 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3846 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 3847 | if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3848 | continue; |
| 3849 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3850 | if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I]))) |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3851 | break; |
| 3852 | } |
| 3853 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3854 | // Create an interface declaration, if one does not already exist. |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3855 | ObjCInterfaceDecl *ToIface = MergeWithIface; |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3856 | if (!ToIface) { |
| 3857 | ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC, |
| 3858 | Importer.Import(D->getAtStartLoc()), |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 3859 | Name.getAsIdentifierInfo(), |
Douglas Gregor | ab7f0b3 | 2015-07-07 06:20:12 +0000 | [diff] [blame] | 3860 | /*TypeParamList=*/nullptr, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3861 | /*PrevDecl=*/nullptr, Loc, |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3862 | D->isImplicitInterfaceDecl()); |
| 3863 | ToIface->setLexicalDeclContext(LexicalDC); |
| 3864 | LexicalDC->addDeclInternal(ToIface); |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3865 | } |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3866 | Importer.Imported(D, ToIface); |
Douglas Gregor | ab7f0b3 | 2015-07-07 06:20:12 +0000 | [diff] [blame] | 3867 | // Import the type parameter list after calling Imported, to avoid |
| 3868 | // loops when bringing in their DeclContext. |
| 3869 | ToIface->setTypeParamList(ImportObjCTypeParamList( |
| 3870 | D->getTypeParamListAsWritten())); |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3871 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3872 | if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3873 | return nullptr; |
| 3874 | |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3875 | return ToIface; |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3876 | } |
| 3877 | |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 3878 | Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 3879 | ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>( |
| 3880 | Importer.Import(D->getCategoryDecl())); |
| 3881 | if (!Category) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3882 | return nullptr; |
| 3883 | |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 3884 | ObjCCategoryImplDecl *ToImpl = Category->getImplementation(); |
| 3885 | if (!ToImpl) { |
| 3886 | DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| 3887 | if (!DC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3888 | return nullptr; |
| 3889 | |
Argyrios Kyrtzidis | 4996f5f | 2011-12-09 00:31:40 +0000 | [diff] [blame] | 3890 | SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc()); |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 3891 | ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC, |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 3892 | Importer.Import(D->getIdentifier()), |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 3893 | Category->getClassInterface(), |
| 3894 | Importer.Import(D->getLocation()), |
Argyrios Kyrtzidis | 4996f5f | 2011-12-09 00:31:40 +0000 | [diff] [blame] | 3895 | Importer.Import(D->getAtStartLoc()), |
| 3896 | CategoryNameLoc); |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 3897 | |
| 3898 | DeclContext *LexicalDC = DC; |
| 3899 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 3900 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 3901 | if (!LexicalDC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3902 | return nullptr; |
| 3903 | |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 3904 | ToImpl->setLexicalDeclContext(LexicalDC); |
| 3905 | } |
| 3906 | |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3907 | LexicalDC->addDeclInternal(ToImpl); |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 3908 | Category->setImplementation(ToImpl); |
| 3909 | } |
| 3910 | |
| 3911 | Importer.Imported(D, ToImpl); |
Douglas Gregor | 35fd7bc | 2010-12-08 16:41:55 +0000 | [diff] [blame] | 3912 | ImportDeclContext(D); |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 3913 | return ToImpl; |
| 3914 | } |
| 3915 | |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 3916 | Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 3917 | // Find the corresponding interface. |
| 3918 | ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>( |
| 3919 | Importer.Import(D->getClassInterface())); |
| 3920 | if (!Iface) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3921 | return nullptr; |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 3922 | |
| 3923 | // Import the superclass, if any. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3924 | ObjCInterfaceDecl *Super = nullptr; |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 3925 | if (D->getSuperClass()) { |
| 3926 | Super = cast_or_null<ObjCInterfaceDecl>( |
| 3927 | Importer.Import(D->getSuperClass())); |
| 3928 | if (!Super) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3929 | return nullptr; |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 3930 | } |
| 3931 | |
| 3932 | ObjCImplementationDecl *Impl = Iface->getImplementation(); |
| 3933 | if (!Impl) { |
| 3934 | // We haven't imported an implementation yet. Create a new @implementation |
| 3935 | // now. |
| 3936 | Impl = ObjCImplementationDecl::Create(Importer.getToContext(), |
| 3937 | Importer.ImportContext(D->getDeclContext()), |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 3938 | Iface, Super, |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 3939 | Importer.Import(D->getLocation()), |
Fariborz Jahanian | a7765fe | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 3940 | Importer.Import(D->getAtStartLoc()), |
Argyrios Kyrtzidis | 5d2ce84 | 2013-05-03 22:31:26 +0000 | [diff] [blame] | 3941 | Importer.Import(D->getSuperClassLoc()), |
Fariborz Jahanian | a7765fe | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 3942 | Importer.Import(D->getIvarLBraceLoc()), |
| 3943 | Importer.Import(D->getIvarRBraceLoc())); |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 3944 | |
| 3945 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 3946 | DeclContext *LexicalDC |
| 3947 | = Importer.ImportContext(D->getLexicalDeclContext()); |
| 3948 | if (!LexicalDC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3949 | return nullptr; |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 3950 | Impl->setLexicalDeclContext(LexicalDC); |
| 3951 | } |
| 3952 | |
| 3953 | // Associate the implementation with the class it implements. |
| 3954 | Iface->setImplementation(Impl); |
| 3955 | Importer.Imported(D, Iface->getImplementation()); |
| 3956 | } else { |
| 3957 | Importer.Imported(D, Iface->getImplementation()); |
| 3958 | |
| 3959 | // Verify that the existing @implementation has the same superclass. |
| 3960 | if ((Super && !Impl->getSuperClass()) || |
| 3961 | (!Super && Impl->getSuperClass()) || |
Craig Topper | dcfc60f | 2014-05-07 06:57:44 +0000 | [diff] [blame] | 3962 | (Super && Impl->getSuperClass() && |
| 3963 | !declaresSameEntity(Super->getCanonicalDecl(), |
| 3964 | Impl->getSuperClass()))) { |
| 3965 | Importer.ToDiag(Impl->getLocation(), |
| 3966 | diag::err_odr_objc_superclass_inconsistent) |
| 3967 | << Iface->getDeclName(); |
| 3968 | // FIXME: It would be nice to have the location of the superclass |
| 3969 | // below. |
| 3970 | if (Impl->getSuperClass()) |
| 3971 | Importer.ToDiag(Impl->getLocation(), |
| 3972 | diag::note_odr_objc_superclass) |
| 3973 | << Impl->getSuperClass()->getDeclName(); |
| 3974 | else |
| 3975 | Importer.ToDiag(Impl->getLocation(), |
| 3976 | diag::note_odr_objc_missing_superclass); |
| 3977 | if (D->getSuperClass()) |
| 3978 | Importer.FromDiag(D->getLocation(), |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 3979 | diag::note_odr_objc_superclass) |
Craig Topper | dcfc60f | 2014-05-07 06:57:44 +0000 | [diff] [blame] | 3980 | << D->getSuperClass()->getDeclName(); |
| 3981 | else |
| 3982 | Importer.FromDiag(D->getLocation(), |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 3983 | diag::note_odr_objc_missing_superclass); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3984 | return nullptr; |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 3985 | } |
| 3986 | } |
| 3987 | |
| 3988 | // Import all of the members of this @implementation. |
| 3989 | ImportDeclContext(D); |
| 3990 | |
| 3991 | return Impl; |
| 3992 | } |
| 3993 | |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 3994 | Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { |
| 3995 | // Import the major distinguishing characteristics of an @property. |
| 3996 | DeclContext *DC, *LexicalDC; |
| 3997 | DeclarationName Name; |
| 3998 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 3999 | NamedDecl *ToD; |
| 4000 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4001 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4002 | if (ToD) |
| 4003 | return ToD; |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 4004 | |
| 4005 | // Check whether we have already imported this property. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4006 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 4007 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 4008 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 4009 | if (ObjCPropertyDecl *FoundProp |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 4010 | = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) { |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 4011 | // Check property types. |
| 4012 | if (!Importer.IsStructurallyEquivalent(D->getType(), |
| 4013 | FoundProp->getType())) { |
| 4014 | Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent) |
| 4015 | << Name << D->getType() << FoundProp->getType(); |
| 4016 | Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here) |
| 4017 | << FoundProp->getType(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4018 | return nullptr; |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 4019 | } |
| 4020 | |
| 4021 | // FIXME: Check property attributes, getters, setters, etc.? |
| 4022 | |
| 4023 | // Consider these properties to be equivalent. |
| 4024 | Importer.Imported(D, FoundProp); |
| 4025 | return FoundProp; |
| 4026 | } |
| 4027 | } |
| 4028 | |
| 4029 | // Import the type. |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4030 | TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo()); |
| 4031 | if (!TSI) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4032 | return nullptr; |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 4033 | |
| 4034 | // Create the new property. |
| 4035 | ObjCPropertyDecl *ToProperty |
| 4036 | = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc, |
| 4037 | Name.getAsIdentifierInfo(), |
| 4038 | Importer.Import(D->getAtLoc()), |
Fariborz Jahanian | 86c2f5c | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 4039 | Importer.Import(D->getLParenLoc()), |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4040 | Importer.Import(D->getType()), |
| 4041 | TSI, |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 4042 | D->getPropertyImplementation()); |
| 4043 | Importer.Imported(D, ToProperty); |
| 4044 | ToProperty->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 4045 | LexicalDC->addDeclInternal(ToProperty); |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 4046 | |
| 4047 | ToProperty->setPropertyAttributes(D->getPropertyAttributes()); |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 4048 | ToProperty->setPropertyAttributesAsWritten( |
| 4049 | D->getPropertyAttributesAsWritten()); |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 4050 | ToProperty->setGetterName(Importer.Import(D->getGetterName())); |
| 4051 | ToProperty->setSetterName(Importer.Import(D->getSetterName())); |
| 4052 | ToProperty->setGetterMethodDecl( |
| 4053 | cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl()))); |
| 4054 | ToProperty->setSetterMethodDecl( |
| 4055 | cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl()))); |
| 4056 | ToProperty->setPropertyIvarDecl( |
| 4057 | cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl()))); |
| 4058 | return ToProperty; |
| 4059 | } |
| 4060 | |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4061 | Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { |
| 4062 | ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>( |
| 4063 | Importer.Import(D->getPropertyDecl())); |
| 4064 | if (!Property) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4065 | return nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4066 | |
| 4067 | DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| 4068 | if (!DC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4069 | return nullptr; |
| 4070 | |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4071 | // Import the lexical declaration context. |
| 4072 | DeclContext *LexicalDC = DC; |
| 4073 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 4074 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 4075 | if (!LexicalDC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4076 | return nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4077 | } |
| 4078 | |
| 4079 | ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC); |
| 4080 | if (!InImpl) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4081 | return nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4082 | |
| 4083 | // Import the ivar (for an @synthesize). |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4084 | ObjCIvarDecl *Ivar = nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4085 | if (D->getPropertyIvarDecl()) { |
| 4086 | Ivar = cast_or_null<ObjCIvarDecl>( |
| 4087 | Importer.Import(D->getPropertyIvarDecl())); |
| 4088 | if (!Ivar) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4089 | return nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4090 | } |
| 4091 | |
| 4092 | ObjCPropertyImplDecl *ToImpl |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 4093 | = InImpl->FindPropertyImplDecl(Property->getIdentifier(), |
| 4094 | Property->getQueryKind()); |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4095 | if (!ToImpl) { |
| 4096 | ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC, |
| 4097 | Importer.Import(D->getLocStart()), |
| 4098 | Importer.Import(D->getLocation()), |
| 4099 | Property, |
| 4100 | D->getPropertyImplementation(), |
| 4101 | Ivar, |
| 4102 | Importer.Import(D->getPropertyIvarDeclLoc())); |
| 4103 | ToImpl->setLexicalDeclContext(LexicalDC); |
| 4104 | Importer.Imported(D, ToImpl); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 4105 | LexicalDC->addDeclInternal(ToImpl); |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4106 | } else { |
| 4107 | // Check that we have the same kind of property implementation (@synthesize |
| 4108 | // vs. @dynamic). |
| 4109 | if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) { |
| 4110 | Importer.ToDiag(ToImpl->getLocation(), |
| 4111 | diag::err_odr_objc_property_impl_kind_inconsistent) |
| 4112 | << Property->getDeclName() |
| 4113 | << (ToImpl->getPropertyImplementation() |
| 4114 | == ObjCPropertyImplDecl::Dynamic); |
| 4115 | Importer.FromDiag(D->getLocation(), |
| 4116 | diag::note_odr_objc_property_impl_kind) |
| 4117 | << D->getPropertyDecl()->getDeclName() |
| 4118 | << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4119 | return nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4120 | } |
| 4121 | |
| 4122 | // For @synthesize, check that we have the same |
| 4123 | if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize && |
| 4124 | Ivar != ToImpl->getPropertyIvarDecl()) { |
| 4125 | Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(), |
| 4126 | diag::err_odr_objc_synthesize_ivar_inconsistent) |
| 4127 | << Property->getDeclName() |
| 4128 | << ToImpl->getPropertyIvarDecl()->getDeclName() |
| 4129 | << Ivar->getDeclName(); |
| 4130 | Importer.FromDiag(D->getPropertyIvarDeclLoc(), |
| 4131 | diag::note_odr_objc_synthesize_ivar_here) |
| 4132 | << D->getPropertyIvarDecl()->getDeclName(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4133 | return nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 4134 | } |
| 4135 | |
| 4136 | // Merge the existing implementation with the new implementation. |
| 4137 | Importer.Imported(D, ToImpl); |
| 4138 | } |
| 4139 | |
| 4140 | return ToImpl; |
| 4141 | } |
| 4142 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4143 | Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
| 4144 | // For template arguments, we adopt the translation unit as our declaration |
| 4145 | // context. This context will be fixed when the actual template declaration |
| 4146 | // is created. |
| 4147 | |
| 4148 | // FIXME: Import default argument. |
| 4149 | return TemplateTypeParmDecl::Create(Importer.getToContext(), |
| 4150 | Importer.getToContext().getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 4151 | Importer.Import(D->getLocStart()), |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4152 | Importer.Import(D->getLocation()), |
| 4153 | D->getDepth(), |
| 4154 | D->getIndex(), |
| 4155 | Importer.Import(D->getIdentifier()), |
| 4156 | D->wasDeclaredWithTypename(), |
| 4157 | D->isParameterPack()); |
| 4158 | } |
| 4159 | |
| 4160 | Decl * |
| 4161 | ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
| 4162 | // Import the name of this declaration. |
| 4163 | DeclarationName Name = Importer.Import(D->getDeclName()); |
| 4164 | if (D->getDeclName() && !Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4165 | return nullptr; |
| 4166 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4167 | // Import the location of this declaration. |
| 4168 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 4169 | |
| 4170 | // Import the type of this declaration. |
| 4171 | QualType T = Importer.Import(D->getType()); |
| 4172 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4173 | return nullptr; |
| 4174 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4175 | // Import type-source information. |
| 4176 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 4177 | if (D->getTypeSourceInfo() && !TInfo) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4178 | return nullptr; |
| 4179 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4180 | // FIXME: Import default argument. |
| 4181 | |
| 4182 | return NonTypeTemplateParmDecl::Create(Importer.getToContext(), |
| 4183 | Importer.getToContext().getTranslationUnitDecl(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4184 | Importer.Import(D->getInnerLocStart()), |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4185 | Loc, D->getDepth(), D->getPosition(), |
| 4186 | Name.getAsIdentifierInfo(), |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 4187 | T, D->isParameterPack(), TInfo); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4188 | } |
| 4189 | |
| 4190 | Decl * |
| 4191 | ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
| 4192 | // Import the name of this declaration. |
| 4193 | DeclarationName Name = Importer.Import(D->getDeclName()); |
| 4194 | if (D->getDeclName() && !Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4195 | return nullptr; |
| 4196 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4197 | // Import the location of this declaration. |
| 4198 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 4199 | |
| 4200 | // Import template parameters. |
| 4201 | TemplateParameterList *TemplateParams |
| 4202 | = ImportTemplateParameterList(D->getTemplateParameters()); |
| 4203 | if (!TemplateParams) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4204 | return nullptr; |
| 4205 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4206 | // FIXME: Import default argument. |
| 4207 | |
| 4208 | return TemplateTemplateParmDecl::Create(Importer.getToContext(), |
| 4209 | Importer.getToContext().getTranslationUnitDecl(), |
| 4210 | Loc, D->getDepth(), D->getPosition(), |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 4211 | D->isParameterPack(), |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4212 | Name.getAsIdentifierInfo(), |
| 4213 | TemplateParams); |
| 4214 | } |
| 4215 | |
| 4216 | Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 4217 | // If this record has a definition in the translation unit we're coming from, |
| 4218 | // but this particular declaration is not that definition, import the |
| 4219 | // definition and map to that. |
| 4220 | CXXRecordDecl *Definition |
| 4221 | = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition()); |
| 4222 | if (Definition && Definition != D->getTemplatedDecl()) { |
| 4223 | Decl *ImportedDef |
| 4224 | = Importer.Import(Definition->getDescribedClassTemplate()); |
| 4225 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4226 | return nullptr; |
| 4227 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4228 | return Importer.Imported(D, ImportedDef); |
| 4229 | } |
| 4230 | |
| 4231 | // Import the major distinguishing characteristics of this class template. |
| 4232 | DeclContext *DC, *LexicalDC; |
| 4233 | DeclarationName Name; |
| 4234 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4235 | NamedDecl *ToD; |
| 4236 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4237 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4238 | if (ToD) |
| 4239 | return ToD; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4240 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4241 | // We may already have a template of the same name; try to find and match it. |
| 4242 | if (!DC->isFunctionOrMethod()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4243 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4244 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 4245 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 4246 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 4247 | if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4248 | continue; |
| 4249 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 4250 | Decl *Found = FoundDecls[I]; |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4251 | if (ClassTemplateDecl *FoundTemplate |
| 4252 | = dyn_cast<ClassTemplateDecl>(Found)) { |
| 4253 | if (IsStructuralMatch(D, FoundTemplate)) { |
| 4254 | // The class templates structurally match; call it the same template. |
| 4255 | // FIXME: We may be filling in a forward declaration here. Handle |
| 4256 | // this case! |
| 4257 | Importer.Imported(D->getTemplatedDecl(), |
| 4258 | FoundTemplate->getTemplatedDecl()); |
| 4259 | return Importer.Imported(D, FoundTemplate); |
| 4260 | } |
| 4261 | } |
| 4262 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 4263 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4264 | } |
| 4265 | |
| 4266 | if (!ConflictingDecls.empty()) { |
| 4267 | Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary, |
| 4268 | ConflictingDecls.data(), |
| 4269 | ConflictingDecls.size()); |
| 4270 | } |
| 4271 | |
| 4272 | if (!Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4273 | return nullptr; |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4274 | } |
| 4275 | |
| 4276 | CXXRecordDecl *DTemplated = D->getTemplatedDecl(); |
| 4277 | |
| 4278 | // Create the declaration that is being templated. |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4279 | SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart()); |
| 4280 | SourceLocation IdLoc = Importer.Import(DTemplated->getLocation()); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4281 | CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(), |
| 4282 | DTemplated->getTagKind(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4283 | DC, StartLoc, IdLoc, |
| 4284 | Name.getAsIdentifierInfo()); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4285 | D2Templated->setAccess(DTemplated->getAccess()); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4286 | D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc())); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4287 | D2Templated->setLexicalDeclContext(LexicalDC); |
| 4288 | |
| 4289 | // Create the class template declaration itself. |
| 4290 | TemplateParameterList *TemplateParams |
| 4291 | = ImportTemplateParameterList(D->getTemplateParameters()); |
| 4292 | if (!TemplateParams) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4293 | return nullptr; |
| 4294 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4295 | ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC, |
| 4296 | Loc, Name, TemplateParams, |
| 4297 | D2Templated, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4298 | /*PrevDecl=*/nullptr); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4299 | D2Templated->setDescribedClassTemplate(D2); |
| 4300 | |
| 4301 | D2->setAccess(D->getAccess()); |
| 4302 | D2->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 4303 | LexicalDC->addDeclInternal(D2); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4304 | |
| 4305 | // Note the relationship between the class templates. |
| 4306 | Importer.Imported(D, D2); |
| 4307 | Importer.Imported(DTemplated, D2Templated); |
| 4308 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 4309 | if (DTemplated->isCompleteDefinition() && |
| 4310 | !D2Templated->isCompleteDefinition()) { |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4311 | // FIXME: Import definition! |
| 4312 | } |
| 4313 | |
| 4314 | return D2; |
| 4315 | } |
| 4316 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4317 | Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl( |
| 4318 | ClassTemplateSpecializationDecl *D) { |
| 4319 | // If this record has a definition in the translation unit we're coming from, |
| 4320 | // but this particular declaration is not that definition, import the |
| 4321 | // definition and map to that. |
| 4322 | TagDecl *Definition = D->getDefinition(); |
| 4323 | if (Definition && Definition != D) { |
| 4324 | Decl *ImportedDef = Importer.Import(Definition); |
| 4325 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4326 | return nullptr; |
| 4327 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4328 | return Importer.Imported(D, ImportedDef); |
| 4329 | } |
| 4330 | |
| 4331 | ClassTemplateDecl *ClassTemplate |
| 4332 | = cast_or_null<ClassTemplateDecl>(Importer.Import( |
| 4333 | D->getSpecializedTemplate())); |
| 4334 | if (!ClassTemplate) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4335 | return nullptr; |
| 4336 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4337 | // Import the context of this declaration. |
| 4338 | DeclContext *DC = ClassTemplate->getDeclContext(); |
| 4339 | if (!DC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4340 | return nullptr; |
| 4341 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4342 | DeclContext *LexicalDC = DC; |
| 4343 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 4344 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 4345 | if (!LexicalDC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4346 | return nullptr; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4347 | } |
| 4348 | |
| 4349 | // Import the location of this declaration. |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4350 | SourceLocation StartLoc = Importer.Import(D->getLocStart()); |
| 4351 | SourceLocation IdLoc = Importer.Import(D->getLocation()); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4352 | |
| 4353 | // Import template arguments. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4354 | SmallVector<TemplateArgument, 2> TemplateArgs; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4355 | if (ImportTemplateArguments(D->getTemplateArgs().data(), |
| 4356 | D->getTemplateArgs().size(), |
| 4357 | TemplateArgs)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4358 | return nullptr; |
| 4359 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4360 | // Try to find an existing specialization with these template arguments. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4361 | void *InsertPos = nullptr; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4362 | ClassTemplateSpecializationDecl *D2 |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 4363 | = ClassTemplate->findSpecialization(TemplateArgs, InsertPos); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4364 | if (D2) { |
| 4365 | // We already have a class template specialization with these template |
| 4366 | // arguments. |
| 4367 | |
| 4368 | // FIXME: Check for specialization vs. instantiation errors. |
| 4369 | |
| 4370 | if (RecordDecl *FoundDef = D2->getDefinition()) { |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 4371 | if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) { |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4372 | // The record types structurally match, or the "from" translation |
| 4373 | // unit only had a forward declaration anyway; call it the same |
| 4374 | // function. |
| 4375 | return Importer.Imported(D, FoundDef); |
| 4376 | } |
| 4377 | } |
| 4378 | } else { |
| 4379 | // Create a new specialization. |
| 4380 | D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(), |
| 4381 | D->getTagKind(), DC, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4382 | StartLoc, IdLoc, |
| 4383 | ClassTemplate, |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4384 | TemplateArgs.data(), |
| 4385 | TemplateArgs.size(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4386 | /*PrevDecl=*/nullptr); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4387 | D2->setSpecializationKind(D->getSpecializationKind()); |
| 4388 | |
| 4389 | // Add this specialization to the class template. |
| 4390 | ClassTemplate->AddSpecialization(D2, InsertPos); |
| 4391 | |
| 4392 | // Import the qualifier, if any. |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4393 | D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4394 | |
| 4395 | // Add the specialization to this context. |
| 4396 | D2->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 4397 | LexicalDC->addDeclInternal(D2); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4398 | } |
| 4399 | Importer.Imported(D, D2); |
| 4400 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 4401 | if (D->isCompleteDefinition() && ImportDefinition(D, D2)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4402 | return nullptr; |
| 4403 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4404 | return D2; |
| 4405 | } |
| 4406 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4407 | Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) { |
| 4408 | // If this variable has a definition in the translation unit we're coming |
| 4409 | // from, |
| 4410 | // but this particular declaration is not that definition, import the |
| 4411 | // definition and map to that. |
| 4412 | VarDecl *Definition = |
| 4413 | cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition()); |
| 4414 | if (Definition && Definition != D->getTemplatedDecl()) { |
| 4415 | Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate()); |
| 4416 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4417 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4418 | |
| 4419 | return Importer.Imported(D, ImportedDef); |
| 4420 | } |
| 4421 | |
| 4422 | // Import the major distinguishing characteristics of this variable template. |
| 4423 | DeclContext *DC, *LexicalDC; |
| 4424 | DeclarationName Name; |
| 4425 | SourceLocation Loc; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4426 | NamedDecl *ToD; |
| 4427 | if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4428 | return nullptr; |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4429 | if (ToD) |
| 4430 | return ToD; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4431 | |
| 4432 | // We may already have a template of the same name; try to find and match it. |
| 4433 | assert(!DC->isFunctionOrMethod() && |
| 4434 | "Variable templates cannot be declared at function scope"); |
| 4435 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 4436 | SmallVector<NamedDecl *, 2> FoundDecls; |
Sean Callanan | 4947532 | 2014-12-10 03:09:41 +0000 | [diff] [blame] | 4437 | DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4438 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 4439 | if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
| 4440 | continue; |
| 4441 | |
| 4442 | Decl *Found = FoundDecls[I]; |
| 4443 | if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) { |
| 4444 | if (IsStructuralMatch(D, FoundTemplate)) { |
| 4445 | // The variable templates structurally match; call it the same template. |
| 4446 | Importer.Imported(D->getTemplatedDecl(), |
| 4447 | FoundTemplate->getTemplatedDecl()); |
| 4448 | return Importer.Imported(D, FoundTemplate); |
| 4449 | } |
| 4450 | } |
| 4451 | |
| 4452 | ConflictingDecls.push_back(FoundDecls[I]); |
| 4453 | } |
| 4454 | |
| 4455 | if (!ConflictingDecls.empty()) { |
| 4456 | Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary, |
| 4457 | ConflictingDecls.data(), |
| 4458 | ConflictingDecls.size()); |
| 4459 | } |
| 4460 | |
| 4461 | if (!Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4462 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4463 | |
| 4464 | VarDecl *DTemplated = D->getTemplatedDecl(); |
| 4465 | |
| 4466 | // Import the type. |
| 4467 | QualType T = Importer.Import(DTemplated->getType()); |
| 4468 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4469 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4470 | |
| 4471 | // Create the declaration that is being templated. |
| 4472 | SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart()); |
| 4473 | SourceLocation IdLoc = Importer.Import(DTemplated->getLocation()); |
| 4474 | TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo()); |
| 4475 | VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc, |
| 4476 | IdLoc, Name.getAsIdentifierInfo(), T, |
| 4477 | TInfo, DTemplated->getStorageClass()); |
| 4478 | D2Templated->setAccess(DTemplated->getAccess()); |
| 4479 | D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc())); |
| 4480 | D2Templated->setLexicalDeclContext(LexicalDC); |
| 4481 | |
| 4482 | // Importer.Imported(DTemplated, D2Templated); |
| 4483 | // LexicalDC->addDeclInternal(D2Templated); |
| 4484 | |
| 4485 | // Merge the initializer. |
| 4486 | if (ImportDefinition(DTemplated, D2Templated)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4487 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4488 | |
| 4489 | // Create the variable template declaration itself. |
| 4490 | TemplateParameterList *TemplateParams = |
| 4491 | ImportTemplateParameterList(D->getTemplateParameters()); |
| 4492 | if (!TemplateParams) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4493 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4494 | |
| 4495 | VarTemplateDecl *D2 = VarTemplateDecl::Create( |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 4496 | Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4497 | D2Templated->setDescribedVarTemplate(D2); |
| 4498 | |
| 4499 | D2->setAccess(D->getAccess()); |
| 4500 | D2->setLexicalDeclContext(LexicalDC); |
| 4501 | LexicalDC->addDeclInternal(D2); |
| 4502 | |
| 4503 | // Note the relationship between the variable templates. |
| 4504 | Importer.Imported(D, D2); |
| 4505 | Importer.Imported(DTemplated, D2Templated); |
| 4506 | |
| 4507 | if (DTemplated->isThisDeclarationADefinition() && |
| 4508 | !D2Templated->isThisDeclarationADefinition()) { |
| 4509 | // FIXME: Import definition! |
| 4510 | } |
| 4511 | |
| 4512 | return D2; |
| 4513 | } |
| 4514 | |
| 4515 | Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl( |
| 4516 | VarTemplateSpecializationDecl *D) { |
| 4517 | // If this record has a definition in the translation unit we're coming from, |
| 4518 | // but this particular declaration is not that definition, import the |
| 4519 | // definition and map to that. |
| 4520 | VarDecl *Definition = D->getDefinition(); |
| 4521 | if (Definition && Definition != D) { |
| 4522 | Decl *ImportedDef = Importer.Import(Definition); |
| 4523 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4524 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4525 | |
| 4526 | return Importer.Imported(D, ImportedDef); |
| 4527 | } |
| 4528 | |
| 4529 | VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>( |
| 4530 | Importer.Import(D->getSpecializedTemplate())); |
| 4531 | if (!VarTemplate) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4532 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4533 | |
| 4534 | // Import the context of this declaration. |
| 4535 | DeclContext *DC = VarTemplate->getDeclContext(); |
| 4536 | if (!DC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4537 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4538 | |
| 4539 | DeclContext *LexicalDC = DC; |
| 4540 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 4541 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 4542 | if (!LexicalDC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4543 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4544 | } |
| 4545 | |
| 4546 | // Import the location of this declaration. |
| 4547 | SourceLocation StartLoc = Importer.Import(D->getLocStart()); |
| 4548 | SourceLocation IdLoc = Importer.Import(D->getLocation()); |
| 4549 | |
| 4550 | // Import template arguments. |
| 4551 | SmallVector<TemplateArgument, 2> TemplateArgs; |
| 4552 | if (ImportTemplateArguments(D->getTemplateArgs().data(), |
| 4553 | D->getTemplateArgs().size(), TemplateArgs)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4554 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4555 | |
| 4556 | // Try to find an existing specialization with these template arguments. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4557 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4558 | VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization( |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 4559 | TemplateArgs, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4560 | if (D2) { |
| 4561 | // We already have a variable template specialization with these template |
| 4562 | // arguments. |
| 4563 | |
| 4564 | // FIXME: Check for specialization vs. instantiation errors. |
| 4565 | |
| 4566 | if (VarDecl *FoundDef = D2->getDefinition()) { |
| 4567 | if (!D->isThisDeclarationADefinition() || |
| 4568 | IsStructuralMatch(D, FoundDef)) { |
| 4569 | // The record types structurally match, or the "from" translation |
| 4570 | // unit only had a forward declaration anyway; call it the same |
| 4571 | // variable. |
| 4572 | return Importer.Imported(D, FoundDef); |
| 4573 | } |
| 4574 | } |
| 4575 | } else { |
| 4576 | |
| 4577 | // Import the type. |
| 4578 | QualType T = Importer.Import(D->getType()); |
| 4579 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4580 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4581 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 4582 | |
| 4583 | // Create a new specialization. |
| 4584 | D2 = VarTemplateSpecializationDecl::Create( |
| 4585 | Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo, |
| 4586 | D->getStorageClass(), TemplateArgs.data(), TemplateArgs.size()); |
| 4587 | D2->setSpecializationKind(D->getSpecializationKind()); |
| 4588 | D2->setTemplateArgsInfo(D->getTemplateArgsInfo()); |
| 4589 | |
| 4590 | // Add this specialization to the class template. |
| 4591 | VarTemplate->AddSpecialization(D2, InsertPos); |
| 4592 | |
| 4593 | // Import the qualifier, if any. |
| 4594 | D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
| 4595 | |
| 4596 | // Add the specialization to this context. |
| 4597 | D2->setLexicalDeclContext(LexicalDC); |
| 4598 | LexicalDC->addDeclInternal(D2); |
| 4599 | } |
| 4600 | Importer.Imported(D, D2); |
| 4601 | |
| 4602 | if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4603 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4604 | |
| 4605 | return D2; |
| 4606 | } |
| 4607 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 4608 | //---------------------------------------------------------------------------- |
| 4609 | // Import Statements |
| 4610 | //---------------------------------------------------------------------------- |
| 4611 | |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4612 | DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) { |
| 4613 | if (DG.isNull()) |
| 4614 | return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0); |
| 4615 | size_t NumDecls = DG.end() - DG.begin(); |
| 4616 | SmallVector<Decl *, 1> ToDecls(NumDecls); |
| 4617 | auto &_Importer = this->Importer; |
| 4618 | std::transform(DG.begin(), DG.end(), ToDecls.begin(), |
| 4619 | [&_Importer](Decl *D) -> Decl * { |
| 4620 | return _Importer.Import(D); |
| 4621 | }); |
| 4622 | return DeclGroupRef::Create(Importer.getToContext(), |
| 4623 | ToDecls.begin(), |
| 4624 | NumDecls); |
| 4625 | } |
| 4626 | |
| 4627 | Stmt *ASTNodeImporter::VisitStmt(Stmt *S) { |
| 4628 | Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node) |
| 4629 | << S->getStmtClassName(); |
| 4630 | return nullptr; |
| 4631 | } |
| 4632 | |
| 4633 | Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) { |
| 4634 | DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup()); |
| 4635 | for (Decl *ToD : ToDG) { |
| 4636 | if (!ToD) |
| 4637 | return nullptr; |
| 4638 | } |
| 4639 | SourceLocation ToStartLoc = Importer.Import(S->getStartLoc()); |
| 4640 | SourceLocation ToEndLoc = Importer.Import(S->getEndLoc()); |
| 4641 | return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc); |
| 4642 | } |
| 4643 | |
| 4644 | Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) { |
| 4645 | SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc()); |
| 4646 | return new (Importer.getToContext()) NullStmt(ToSemiLoc, |
| 4647 | S->hasLeadingEmptyMacro()); |
| 4648 | } |
| 4649 | |
| 4650 | Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) { |
| 4651 | SmallVector<Stmt *, 4> ToStmts(S->size()); |
| 4652 | auto &_Importer = this->Importer; |
| 4653 | std::transform(S->body_begin(), S->body_end(), ToStmts.begin(), |
| 4654 | [&_Importer](Stmt *CS) -> Stmt * { |
| 4655 | return _Importer.Import(CS); |
| 4656 | }); |
| 4657 | for (Stmt *ToS : ToStmts) { |
| 4658 | if (!ToS) |
| 4659 | return nullptr; |
| 4660 | } |
| 4661 | SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc()); |
| 4662 | SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc()); |
| 4663 | return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(), |
| 4664 | ToStmts, |
| 4665 | ToLBraceLoc, ToRBraceLoc); |
| 4666 | } |
| 4667 | |
| 4668 | Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) { |
| 4669 | Expr *ToLHS = Importer.Import(S->getLHS()); |
| 4670 | if (!ToLHS) |
| 4671 | return nullptr; |
| 4672 | Expr *ToRHS = Importer.Import(S->getRHS()); |
| 4673 | if (!ToRHS && S->getRHS()) |
| 4674 | return nullptr; |
| 4675 | SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc()); |
| 4676 | SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc()); |
| 4677 | SourceLocation ToColonLoc = Importer.Import(S->getColonLoc()); |
| 4678 | return new (Importer.getToContext()) CaseStmt(ToLHS, ToRHS, |
| 4679 | ToCaseLoc, ToEllipsisLoc, |
| 4680 | ToColonLoc); |
| 4681 | } |
| 4682 | |
| 4683 | Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) { |
| 4684 | SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc()); |
| 4685 | SourceLocation ToColonLoc = Importer.Import(S->getColonLoc()); |
| 4686 | Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); |
| 4687 | if (!ToSubStmt && S->getSubStmt()) |
| 4688 | return nullptr; |
| 4689 | return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc, |
| 4690 | ToSubStmt); |
| 4691 | } |
| 4692 | |
| 4693 | Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) { |
| 4694 | SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc()); |
| 4695 | LabelDecl *ToLabelDecl = |
| 4696 | cast_or_null<LabelDecl>(Importer.Import(S->getDecl())); |
| 4697 | if (!ToLabelDecl && S->getDecl()) |
| 4698 | return nullptr; |
| 4699 | Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); |
| 4700 | if (!ToSubStmt && S->getSubStmt()) |
| 4701 | return nullptr; |
| 4702 | return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl, |
| 4703 | ToSubStmt); |
| 4704 | } |
| 4705 | |
| 4706 | Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) { |
| 4707 | SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc()); |
| 4708 | ArrayRef<const Attr*> FromAttrs(S->getAttrs()); |
| 4709 | SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size()); |
| 4710 | ASTContext &_ToContext = Importer.getToContext(); |
| 4711 | std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(), |
| 4712 | [&_ToContext](const Attr *A) -> const Attr * { |
| 4713 | return A->clone(_ToContext); |
| 4714 | }); |
| 4715 | for (const Attr *ToA : ToAttrs) { |
| 4716 | if (!ToA) |
| 4717 | return nullptr; |
| 4718 | } |
| 4719 | Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); |
| 4720 | if (!ToSubStmt && S->getSubStmt()) |
| 4721 | return nullptr; |
| 4722 | return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc, |
| 4723 | ToAttrs, ToSubStmt); |
| 4724 | } |
| 4725 | |
| 4726 | Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) { |
| 4727 | SourceLocation ToIfLoc = Importer.Import(S->getIfLoc()); |
| 4728 | VarDecl *ToConditionVariable = nullptr; |
| 4729 | if (VarDecl *FromConditionVariable = S->getConditionVariable()) { |
| 4730 | ToConditionVariable = |
| 4731 | dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); |
| 4732 | if (!ToConditionVariable) |
| 4733 | return nullptr; |
| 4734 | } |
| 4735 | Expr *ToCondition = Importer.Import(S->getCond()); |
| 4736 | if (!ToCondition && S->getCond()) |
| 4737 | return nullptr; |
| 4738 | Stmt *ToThenStmt = Importer.Import(S->getThen()); |
| 4739 | if (!ToThenStmt && S->getThen()) |
| 4740 | return nullptr; |
| 4741 | SourceLocation ToElseLoc = Importer.Import(S->getElseLoc()); |
| 4742 | Stmt *ToElseStmt = Importer.Import(S->getElse()); |
| 4743 | if (!ToElseStmt && S->getElse()) |
| 4744 | return nullptr; |
| 4745 | return new (Importer.getToContext()) IfStmt(Importer.getToContext(), |
| 4746 | ToIfLoc, ToConditionVariable, |
| 4747 | ToCondition, ToThenStmt, |
| 4748 | ToElseLoc, ToElseStmt); |
| 4749 | } |
| 4750 | |
| 4751 | Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) { |
| 4752 | VarDecl *ToConditionVariable = nullptr; |
| 4753 | if (VarDecl *FromConditionVariable = S->getConditionVariable()) { |
| 4754 | ToConditionVariable = |
| 4755 | dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); |
| 4756 | if (!ToConditionVariable) |
| 4757 | return nullptr; |
| 4758 | } |
| 4759 | Expr *ToCondition = Importer.Import(S->getCond()); |
| 4760 | if (!ToCondition && S->getCond()) |
| 4761 | return nullptr; |
| 4762 | SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt( |
| 4763 | Importer.getToContext(), ToConditionVariable, |
| 4764 | ToCondition); |
| 4765 | Stmt *ToBody = Importer.Import(S->getBody()); |
| 4766 | if (!ToBody && S->getBody()) |
| 4767 | return nullptr; |
| 4768 | ToStmt->setBody(ToBody); |
| 4769 | ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc())); |
| 4770 | // Now we have to re-chain the cases. |
| 4771 | SwitchCase *LastChainedSwitchCase = nullptr; |
| 4772 | for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr; |
| 4773 | SC = SC->getNextSwitchCase()) { |
| 4774 | SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC)); |
| 4775 | if (!ToSC) |
| 4776 | return nullptr; |
| 4777 | if (LastChainedSwitchCase) |
| 4778 | LastChainedSwitchCase->setNextSwitchCase(ToSC); |
| 4779 | else |
| 4780 | ToStmt->setSwitchCaseList(ToSC); |
| 4781 | LastChainedSwitchCase = ToSC; |
| 4782 | } |
| 4783 | return ToStmt; |
| 4784 | } |
| 4785 | |
| 4786 | Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) { |
| 4787 | VarDecl *ToConditionVariable = nullptr; |
| 4788 | if (VarDecl *FromConditionVariable = S->getConditionVariable()) { |
| 4789 | ToConditionVariable = |
| 4790 | dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); |
| 4791 | if (!ToConditionVariable) |
| 4792 | return nullptr; |
| 4793 | } |
| 4794 | Expr *ToCondition = Importer.Import(S->getCond()); |
| 4795 | if (!ToCondition && S->getCond()) |
| 4796 | return nullptr; |
| 4797 | Stmt *ToBody = Importer.Import(S->getBody()); |
| 4798 | if (!ToBody && S->getBody()) |
| 4799 | return nullptr; |
| 4800 | SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc()); |
| 4801 | return new (Importer.getToContext()) WhileStmt(Importer.getToContext(), |
| 4802 | ToConditionVariable, |
| 4803 | ToCondition, ToBody, |
| 4804 | ToWhileLoc); |
| 4805 | } |
| 4806 | |
| 4807 | Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) { |
| 4808 | Stmt *ToBody = Importer.Import(S->getBody()); |
| 4809 | if (!ToBody && S->getBody()) |
| 4810 | return nullptr; |
| 4811 | Expr *ToCondition = Importer.Import(S->getCond()); |
| 4812 | if (!ToCondition && S->getCond()) |
| 4813 | return nullptr; |
| 4814 | SourceLocation ToDoLoc = Importer.Import(S->getDoLoc()); |
| 4815 | SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc()); |
| 4816 | SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); |
| 4817 | return new (Importer.getToContext()) DoStmt(ToBody, ToCondition, |
| 4818 | ToDoLoc, ToWhileLoc, |
| 4819 | ToRParenLoc); |
| 4820 | } |
| 4821 | |
| 4822 | Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) { |
| 4823 | Stmt *ToInit = Importer.Import(S->getInit()); |
| 4824 | if (!ToInit && S->getInit()) |
| 4825 | return nullptr; |
| 4826 | Expr *ToCondition = Importer.Import(S->getCond()); |
| 4827 | if (!ToCondition && S->getCond()) |
| 4828 | return nullptr; |
| 4829 | VarDecl *ToConditionVariable = nullptr; |
| 4830 | if (VarDecl *FromConditionVariable = S->getConditionVariable()) { |
| 4831 | ToConditionVariable = |
| 4832 | dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); |
| 4833 | if (!ToConditionVariable) |
| 4834 | return nullptr; |
| 4835 | } |
| 4836 | Expr *ToInc = Importer.Import(S->getInc()); |
| 4837 | if (!ToInc && S->getInc()) |
| 4838 | return nullptr; |
| 4839 | Stmt *ToBody = Importer.Import(S->getBody()); |
| 4840 | if (!ToBody && S->getBody()) |
| 4841 | return nullptr; |
| 4842 | SourceLocation ToForLoc = Importer.Import(S->getForLoc()); |
| 4843 | SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc()); |
| 4844 | SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); |
| 4845 | return new (Importer.getToContext()) ForStmt(Importer.getToContext(), |
| 4846 | ToInit, ToCondition, |
| 4847 | ToConditionVariable, |
| 4848 | ToInc, ToBody, |
| 4849 | ToForLoc, ToLParenLoc, |
| 4850 | ToRParenLoc); |
| 4851 | } |
| 4852 | |
| 4853 | Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) { |
| 4854 | LabelDecl *ToLabel = nullptr; |
| 4855 | if (LabelDecl *FromLabel = S->getLabel()) { |
| 4856 | ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel)); |
| 4857 | if (!ToLabel) |
| 4858 | return nullptr; |
| 4859 | } |
| 4860 | SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc()); |
| 4861 | SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc()); |
| 4862 | return new (Importer.getToContext()) GotoStmt(ToLabel, |
| 4863 | ToGotoLoc, ToLabelLoc); |
| 4864 | } |
| 4865 | |
| 4866 | Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) { |
| 4867 | SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc()); |
| 4868 | SourceLocation ToStarLoc = Importer.Import(S->getStarLoc()); |
| 4869 | Expr *ToTarget = Importer.Import(S->getTarget()); |
| 4870 | if (!ToTarget && S->getTarget()) |
| 4871 | return nullptr; |
| 4872 | return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc, |
| 4873 | ToTarget); |
| 4874 | } |
| 4875 | |
| 4876 | Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) { |
| 4877 | SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc()); |
| 4878 | return new (Importer.getToContext()) ContinueStmt(ToContinueLoc); |
| 4879 | } |
| 4880 | |
| 4881 | Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) { |
| 4882 | SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc()); |
| 4883 | return new (Importer.getToContext()) BreakStmt(ToBreakLoc); |
| 4884 | } |
| 4885 | |
| 4886 | Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) { |
| 4887 | SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc()); |
| 4888 | Expr *ToRetExpr = Importer.Import(S->getRetValue()); |
| 4889 | if (!ToRetExpr && S->getRetValue()) |
| 4890 | return nullptr; |
| 4891 | VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate()); |
| 4892 | VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate)); |
| 4893 | if (!ToNRVOCandidate && NRVOCandidate) |
| 4894 | return nullptr; |
| 4895 | return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr, |
| 4896 | ToNRVOCandidate); |
| 4897 | } |
| 4898 | |
| 4899 | Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) { |
| 4900 | SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc()); |
| 4901 | VarDecl *ToExceptionDecl = nullptr; |
| 4902 | if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) { |
| 4903 | ToExceptionDecl = |
| 4904 | dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl)); |
| 4905 | if (!ToExceptionDecl) |
| 4906 | return nullptr; |
| 4907 | } |
| 4908 | Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock()); |
| 4909 | if (!ToHandlerBlock && S->getHandlerBlock()) |
| 4910 | return nullptr; |
| 4911 | return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc, |
| 4912 | ToExceptionDecl, |
| 4913 | ToHandlerBlock); |
| 4914 | } |
| 4915 | |
| 4916 | Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) { |
| 4917 | SourceLocation ToTryLoc = Importer.Import(S->getTryLoc()); |
| 4918 | Stmt *ToTryBlock = Importer.Import(S->getTryBlock()); |
| 4919 | if (!ToTryBlock && S->getTryBlock()) |
| 4920 | return nullptr; |
| 4921 | SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers()); |
| 4922 | for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) { |
| 4923 | CXXCatchStmt *FromHandler = S->getHandler(HI); |
| 4924 | if (Stmt *ToHandler = Importer.Import(FromHandler)) |
| 4925 | ToHandlers[HI] = ToHandler; |
| 4926 | else |
| 4927 | return nullptr; |
| 4928 | } |
| 4929 | return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock, |
| 4930 | ToHandlers); |
| 4931 | } |
| 4932 | |
| 4933 | Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) { |
| 4934 | DeclStmt *ToRange = |
| 4935 | dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt())); |
| 4936 | if (!ToRange && S->getRangeStmt()) |
| 4937 | return nullptr; |
| 4938 | DeclStmt *ToBeginEnd = |
| 4939 | dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginEndStmt())); |
| 4940 | if (!ToBeginEnd && S->getBeginEndStmt()) |
| 4941 | return nullptr; |
| 4942 | Expr *ToCond = Importer.Import(S->getCond()); |
| 4943 | if (!ToCond && S->getCond()) |
| 4944 | return nullptr; |
| 4945 | Expr *ToInc = Importer.Import(S->getInc()); |
| 4946 | if (!ToInc && S->getInc()) |
| 4947 | return nullptr; |
| 4948 | DeclStmt *ToLoopVar = |
| 4949 | dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt())); |
| 4950 | if (!ToLoopVar && S->getLoopVarStmt()) |
| 4951 | return nullptr; |
| 4952 | Stmt *ToBody = Importer.Import(S->getBody()); |
| 4953 | if (!ToBody && S->getBody()) |
| 4954 | return nullptr; |
| 4955 | SourceLocation ToForLoc = Importer.Import(S->getForLoc()); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 4956 | SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc()); |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4957 | SourceLocation ToColonLoc = Importer.Import(S->getColonLoc()); |
| 4958 | SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); |
| 4959 | return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBeginEnd, |
| 4960 | ToCond, ToInc, |
| 4961 | ToLoopVar, ToBody, |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 4962 | ToForLoc, ToCoawaitLoc, |
| 4963 | ToColonLoc, ToRParenLoc); |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 4964 | } |
| 4965 | |
| 4966 | Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) { |
| 4967 | Stmt *ToElem = Importer.Import(S->getElement()); |
| 4968 | if (!ToElem && S->getElement()) |
| 4969 | return nullptr; |
| 4970 | Expr *ToCollect = Importer.Import(S->getCollection()); |
| 4971 | if (!ToCollect && S->getCollection()) |
| 4972 | return nullptr; |
| 4973 | Stmt *ToBody = Importer.Import(S->getBody()); |
| 4974 | if (!ToBody && S->getBody()) |
| 4975 | return nullptr; |
| 4976 | SourceLocation ToForLoc = Importer.Import(S->getForLoc()); |
| 4977 | SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); |
| 4978 | return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem, |
| 4979 | ToCollect, |
| 4980 | ToBody, ToForLoc, |
| 4981 | ToRParenLoc); |
| 4982 | } |
| 4983 | |
| 4984 | Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) { |
| 4985 | SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc()); |
| 4986 | SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); |
| 4987 | VarDecl *ToExceptionDecl = nullptr; |
| 4988 | if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) { |
| 4989 | ToExceptionDecl = |
| 4990 | dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl)); |
| 4991 | if (!ToExceptionDecl) |
| 4992 | return nullptr; |
| 4993 | } |
| 4994 | Stmt *ToBody = Importer.Import(S->getCatchBody()); |
| 4995 | if (!ToBody && S->getCatchBody()) |
| 4996 | return nullptr; |
| 4997 | return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc, |
| 4998 | ToRParenLoc, |
| 4999 | ToExceptionDecl, |
| 5000 | ToBody); |
| 5001 | } |
| 5002 | |
| 5003 | Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) { |
| 5004 | SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc()); |
| 5005 | Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody()); |
| 5006 | if (!ToAtFinallyStmt && S->getFinallyBody()) |
| 5007 | return nullptr; |
| 5008 | return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc, |
| 5009 | ToAtFinallyStmt); |
| 5010 | } |
| 5011 | |
| 5012 | Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) { |
| 5013 | SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc()); |
| 5014 | Stmt *ToAtTryStmt = Importer.Import(S->getTryBody()); |
| 5015 | if (!ToAtTryStmt && S->getTryBody()) |
| 5016 | return nullptr; |
| 5017 | SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts()); |
| 5018 | for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) { |
| 5019 | ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI); |
| 5020 | if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt)) |
| 5021 | ToCatchStmts[CI] = ToCatchStmt; |
| 5022 | else |
| 5023 | return nullptr; |
| 5024 | } |
| 5025 | Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt()); |
| 5026 | if (!ToAtFinallyStmt && S->getFinallyStmt()) |
| 5027 | return nullptr; |
| 5028 | return ObjCAtTryStmt::Create(Importer.getToContext(), |
| 5029 | ToAtTryLoc, ToAtTryStmt, |
| 5030 | ToCatchStmts.begin(), ToCatchStmts.size(), |
| 5031 | ToAtFinallyStmt); |
| 5032 | } |
| 5033 | |
| 5034 | Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt |
| 5035 | (ObjCAtSynchronizedStmt *S) { |
| 5036 | SourceLocation ToAtSynchronizedLoc = |
| 5037 | Importer.Import(S->getAtSynchronizedLoc()); |
| 5038 | Expr *ToSynchExpr = Importer.Import(S->getSynchExpr()); |
| 5039 | if (!ToSynchExpr && S->getSynchExpr()) |
| 5040 | return nullptr; |
| 5041 | Stmt *ToSynchBody = Importer.Import(S->getSynchBody()); |
| 5042 | if (!ToSynchBody && S->getSynchBody()) |
| 5043 | return nullptr; |
| 5044 | return new (Importer.getToContext()) ObjCAtSynchronizedStmt( |
| 5045 | ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody); |
| 5046 | } |
| 5047 | |
| 5048 | Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) { |
| 5049 | SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc()); |
| 5050 | Expr *ToThrow = Importer.Import(S->getThrowExpr()); |
| 5051 | if (!ToThrow && S->getThrowExpr()) |
| 5052 | return nullptr; |
| 5053 | return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow); |
| 5054 | } |
| 5055 | |
| 5056 | Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt |
| 5057 | (ObjCAutoreleasePoolStmt *S) { |
| 5058 | SourceLocation ToAtLoc = Importer.Import(S->getAtLoc()); |
| 5059 | Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); |
| 5060 | if (!ToSubStmt && S->getSubStmt()) |
| 5061 | return nullptr; |
| 5062 | return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc, |
| 5063 | ToSubStmt); |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 5064 | } |
| 5065 | |
| 5066 | //---------------------------------------------------------------------------- |
| 5067 | // Import Expressions |
| 5068 | //---------------------------------------------------------------------------- |
| 5069 | Expr *ASTNodeImporter::VisitExpr(Expr *E) { |
| 5070 | Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node) |
| 5071 | << E->getStmtClassName(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5072 | return nullptr; |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 5073 | } |
| 5074 | |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 5075 | Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) { |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 5076 | ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl())); |
| 5077 | if (!ToD) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5078 | return nullptr; |
Chandler Carruth | 8d26bb0 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 5079 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5080 | NamedDecl *FoundD = nullptr; |
Chandler Carruth | 8d26bb0 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 5081 | if (E->getDecl() != E->getFoundDecl()) { |
| 5082 | FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl())); |
| 5083 | if (!FoundD) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5084 | return nullptr; |
Chandler Carruth | 8d26bb0 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 5085 | } |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 5086 | |
| 5087 | QualType T = Importer.Import(E->getType()); |
| 5088 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5089 | return nullptr; |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5090 | |
| 5091 | DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(), |
| 5092 | Importer.Import(E->getQualifierLoc()), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 5093 | Importer.Import(E->getTemplateKeywordLoc()), |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5094 | ToD, |
Alexey Bataev | 19acc3d | 2015-01-12 10:17:46 +0000 | [diff] [blame] | 5095 | E->refersToEnclosingVariableOrCapture(), |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5096 | Importer.Import(E->getLocation()), |
| 5097 | T, E->getValueKind(), |
| 5098 | FoundD, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5099 | /*FIXME:TemplateArgs=*/nullptr); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5100 | if (E->hadMultipleCandidates()) |
| 5101 | DRE->setHadMultipleCandidates(true); |
| 5102 | return DRE; |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 5103 | } |
| 5104 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 5105 | Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) { |
| 5106 | QualType T = Importer.Import(E->getType()); |
| 5107 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5108 | return nullptr; |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 5109 | |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 5110 | return IntegerLiteral::Create(Importer.getToContext(), |
| 5111 | E->getValue(), T, |
| 5112 | Importer.Import(E->getLocation())); |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 5113 | } |
| 5114 | |
Douglas Gregor | 623421d | 2010-02-18 02:21:22 +0000 | [diff] [blame] | 5115 | Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) { |
| 5116 | QualType T = Importer.Import(E->getType()); |
| 5117 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5118 | return nullptr; |
| 5119 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5120 | return new (Importer.getToContext()) CharacterLiteral(E->getValue(), |
| 5121 | E->getKind(), T, |
Douglas Gregor | 623421d | 2010-02-18 02:21:22 +0000 | [diff] [blame] | 5122 | Importer.Import(E->getLocation())); |
| 5123 | } |
| 5124 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5125 | Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) { |
| 5126 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 5127 | if (!SubExpr) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5128 | return nullptr; |
| 5129 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5130 | return new (Importer.getToContext()) |
| 5131 | ParenExpr(Importer.Import(E->getLParen()), |
| 5132 | Importer.Import(E->getRParen()), |
| 5133 | SubExpr); |
| 5134 | } |
| 5135 | |
| 5136 | Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) { |
| 5137 | QualType T = Importer.Import(E->getType()); |
| 5138 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5139 | return nullptr; |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5140 | |
| 5141 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 5142 | if (!SubExpr) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5143 | return nullptr; |
| 5144 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5145 | return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5146 | T, E->getValueKind(), |
| 5147 | E->getObjectKind(), |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5148 | Importer.Import(E->getOperatorLoc())); |
| 5149 | } |
| 5150 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5151 | Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr( |
| 5152 | UnaryExprOrTypeTraitExpr *E) { |
Douglas Gregor | d8552cd | 2010-02-19 01:24:23 +0000 | [diff] [blame] | 5153 | QualType ResultType = Importer.Import(E->getType()); |
| 5154 | |
| 5155 | if (E->isArgumentType()) { |
| 5156 | TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo()); |
| 5157 | if (!TInfo) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5158 | return nullptr; |
| 5159 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5160 | return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(), |
| 5161 | TInfo, ResultType, |
Douglas Gregor | d8552cd | 2010-02-19 01:24:23 +0000 | [diff] [blame] | 5162 | Importer.Import(E->getOperatorLoc()), |
| 5163 | Importer.Import(E->getRParenLoc())); |
| 5164 | } |
| 5165 | |
| 5166 | Expr *SubExpr = Importer.Import(E->getArgumentExpr()); |
| 5167 | if (!SubExpr) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5168 | return nullptr; |
| 5169 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5170 | return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(), |
| 5171 | SubExpr, ResultType, |
Douglas Gregor | d8552cd | 2010-02-19 01:24:23 +0000 | [diff] [blame] | 5172 | Importer.Import(E->getOperatorLoc()), |
| 5173 | Importer.Import(E->getRParenLoc())); |
| 5174 | } |
| 5175 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5176 | Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) { |
| 5177 | QualType T = Importer.Import(E->getType()); |
| 5178 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5179 | return nullptr; |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5180 | |
| 5181 | Expr *LHS = Importer.Import(E->getLHS()); |
| 5182 | if (!LHS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5183 | return nullptr; |
| 5184 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5185 | Expr *RHS = Importer.Import(E->getRHS()); |
| 5186 | if (!RHS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5187 | return nullptr; |
| 5188 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5189 | return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5190 | T, E->getValueKind(), |
| 5191 | E->getObjectKind(), |
Lang Hames | 5de91cc | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 5192 | Importer.Import(E->getOperatorLoc()), |
| 5193 | E->isFPContractable()); |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5194 | } |
| 5195 | |
| 5196 | Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) { |
| 5197 | QualType T = Importer.Import(E->getType()); |
| 5198 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5199 | return nullptr; |
| 5200 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5201 | QualType CompLHSType = Importer.Import(E->getComputationLHSType()); |
| 5202 | if (CompLHSType.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5203 | return nullptr; |
| 5204 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5205 | QualType CompResultType = Importer.Import(E->getComputationResultType()); |
| 5206 | if (CompResultType.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5207 | return nullptr; |
| 5208 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5209 | Expr *LHS = Importer.Import(E->getLHS()); |
| 5210 | if (!LHS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5211 | return nullptr; |
| 5212 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5213 | Expr *RHS = Importer.Import(E->getRHS()); |
| 5214 | if (!RHS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5215 | return nullptr; |
| 5216 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5217 | return new (Importer.getToContext()) |
| 5218 | CompoundAssignOperator(LHS, RHS, E->getOpcode(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5219 | T, E->getValueKind(), |
| 5220 | E->getObjectKind(), |
| 5221 | CompLHSType, CompResultType, |
Lang Hames | 5de91cc | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 5222 | Importer.Import(E->getOperatorLoc()), |
| 5223 | E->isFPContractable()); |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 5224 | } |
| 5225 | |
Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 5226 | static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) { |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5227 | if (E->path_empty()) return false; |
| 5228 | |
| 5229 | // TODO: import cast paths |
| 5230 | return true; |
| 5231 | } |
| 5232 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 5233 | Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 5234 | QualType T = Importer.Import(E->getType()); |
| 5235 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5236 | return nullptr; |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 5237 | |
| 5238 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 5239 | if (!SubExpr) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5240 | return nullptr; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5241 | |
| 5242 | CXXCastPath BasePath; |
| 5243 | if (ImportCastPath(E, BasePath)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5244 | return nullptr; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5245 | |
| 5246 | return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5247 | SubExpr, &BasePath, E->getValueKind()); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 5248 | } |
| 5249 | |
Douglas Gregor | 5481d32 | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 5250 | Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) { |
| 5251 | QualType T = Importer.Import(E->getType()); |
| 5252 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5253 | return nullptr; |
| 5254 | |
Douglas Gregor | 5481d32 | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 5255 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 5256 | if (!SubExpr) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5257 | return nullptr; |
Douglas Gregor | 5481d32 | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 5258 | |
| 5259 | TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten()); |
| 5260 | if (!TInfo && E->getTypeInfoAsWritten()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5261 | return nullptr; |
| 5262 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5263 | CXXCastPath BasePath; |
| 5264 | if (ImportCastPath(E, BasePath)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5265 | return nullptr; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5266 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5267 | return CStyleCastExpr::Create(Importer.getToContext(), T, |
| 5268 | E->getValueKind(), E->getCastKind(), |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5269 | SubExpr, &BasePath, TInfo, |
| 5270 | Importer.Import(E->getLParenLoc()), |
| 5271 | Importer.Import(E->getRParenLoc())); |
Douglas Gregor | 5481d32 | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 5272 | } |
| 5273 | |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 5274 | Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 5275 | QualType T = Importer.Import(E->getType()); |
| 5276 | if (T.isNull()) |
| 5277 | return nullptr; |
| 5278 | |
| 5279 | CXXConstructorDecl *ToCCD = |
| 5280 | dyn_cast<CXXConstructorDecl>(Importer.Import(E->getConstructor())); |
| 5281 | if (!ToCCD && E->getConstructor()) |
| 5282 | return nullptr; |
| 5283 | |
| 5284 | size_t NumArgs = E->getNumArgs(); |
| 5285 | SmallVector<Expr *, 1> ToArgs(NumArgs); |
| 5286 | ASTImporter &_Importer = Importer; |
| 5287 | std::transform(E->arg_begin(), E->arg_end(), ToArgs.begin(), |
| 5288 | [&_Importer](Expr *AE) -> Expr * { |
| 5289 | return _Importer.Import(AE); |
| 5290 | }); |
| 5291 | for (Expr *ToA : ToArgs) { |
| 5292 | if (!ToA) |
| 5293 | return nullptr; |
| 5294 | } |
| 5295 | |
| 5296 | return CXXConstructExpr::Create(Importer.getToContext(), T, |
| 5297 | Importer.Import(E->getLocation()), |
| 5298 | ToCCD, E->isElidable(), |
| 5299 | ToArgs, E->hadMultipleCandidates(), |
| 5300 | E->isListInitialization(), |
| 5301 | E->isStdInitListInitialization(), |
| 5302 | E->requiresZeroInitialization(), |
| 5303 | E->getConstructionKind(), |
| 5304 | Importer.Import(E->getParenOrBraceRange())); |
| 5305 | } |
| 5306 | |
| 5307 | Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) { |
| 5308 | QualType T = Importer.Import(E->getType()); |
| 5309 | if (T.isNull()) |
| 5310 | return nullptr; |
| 5311 | |
| 5312 | Expr *ToBase = Importer.Import(E->getBase()); |
| 5313 | if (!ToBase && E->getBase()) |
| 5314 | return nullptr; |
| 5315 | |
| 5316 | ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl())); |
| 5317 | if (!ToMember && E->getMemberDecl()) |
| 5318 | return nullptr; |
| 5319 | |
| 5320 | DeclAccessPair ToFoundDecl = DeclAccessPair::make( |
| 5321 | dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())), |
| 5322 | E->getFoundDecl().getAccess()); |
| 5323 | |
| 5324 | DeclarationNameInfo ToMemberNameInfo( |
| 5325 | Importer.Import(E->getMemberNameInfo().getName()), |
| 5326 | Importer.Import(E->getMemberNameInfo().getLoc())); |
| 5327 | |
| 5328 | if (E->hasExplicitTemplateArgs()) { |
| 5329 | return nullptr; // FIXME: handle template arguments |
| 5330 | } |
| 5331 | |
| 5332 | return MemberExpr::Create(Importer.getToContext(), ToBase, |
| 5333 | E->isArrow(), |
| 5334 | Importer.Import(E->getOperatorLoc()), |
| 5335 | Importer.Import(E->getQualifierLoc()), |
| 5336 | Importer.Import(E->getTemplateKeywordLoc()), |
| 5337 | ToMember, ToFoundDecl, ToMemberNameInfo, |
| 5338 | nullptr, T, E->getValueKind(), |
| 5339 | E->getObjectKind()); |
| 5340 | } |
| 5341 | |
| 5342 | Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) { |
| 5343 | QualType T = Importer.Import(E->getType()); |
| 5344 | if (T.isNull()) |
| 5345 | return nullptr; |
| 5346 | |
| 5347 | Expr *ToCallee = Importer.Import(E->getCallee()); |
| 5348 | if (!ToCallee && E->getCallee()) |
| 5349 | return nullptr; |
| 5350 | |
| 5351 | unsigned NumArgs = E->getNumArgs(); |
| 5352 | |
| 5353 | llvm::SmallVector<Expr *, 2> ToArgs(NumArgs); |
| 5354 | |
| 5355 | for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) { |
| 5356 | Expr *FromArg = E->getArg(ai); |
| 5357 | Expr *ToArg = Importer.Import(FromArg); |
| 5358 | if (!ToArg) |
| 5359 | return nullptr; |
| 5360 | ToArgs[ai] = ToArg; |
| 5361 | } |
| 5362 | |
| 5363 | Expr **ToArgs_Copied = new (Importer.getToContext()) |
| 5364 | Expr*[NumArgs]; |
| 5365 | |
| 5366 | for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) |
| 5367 | ToArgs_Copied[ai] = ToArgs[ai]; |
| 5368 | |
| 5369 | return new (Importer.getToContext()) |
| 5370 | CallExpr(Importer.getToContext(), ToCallee, |
Craig Topper | c005cc0 | 2015-09-27 03:44:08 +0000 | [diff] [blame] | 5371 | llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(), |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 5372 | Importer.Import(E->getRParenLoc())); |
| 5373 | } |
| 5374 | |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 5375 | ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager, |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 5376 | ASTContext &FromContext, FileManager &FromFileManager, |
| 5377 | bool MinimalImport) |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 5378 | : ToContext(ToContext), FromContext(FromContext), |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 5379 | ToFileManager(ToFileManager), FromFileManager(FromFileManager), |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 5380 | Minimal(MinimalImport), LastDiagFromFrom(false) |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 5381 | { |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 5382 | ImportedDecls[FromContext.getTranslationUnitDecl()] |
| 5383 | = ToContext.getTranslationUnitDecl(); |
| 5384 | } |
| 5385 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 5386 | ASTImporter::~ASTImporter() { } |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 5387 | |
| 5388 | QualType ASTImporter::Import(QualType FromT) { |
| 5389 | if (FromT.isNull()) |
| 5390 | return QualType(); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5391 | |
| 5392 | const Type *fromTy = FromT.getTypePtr(); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 5393 | |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 5394 | // Check whether we've already imported this type. |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5395 | llvm::DenseMap<const Type *, const Type *>::iterator Pos |
| 5396 | = ImportedTypes.find(fromTy); |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 5397 | if (Pos != ImportedTypes.end()) |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5398 | return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 5399 | |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 5400 | // Import the type |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 5401 | ASTNodeImporter Importer(*this); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5402 | QualType ToT = Importer.Visit(fromTy); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 5403 | if (ToT.isNull()) |
| 5404 | return ToT; |
| 5405 | |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 5406 | // Record the imported type. |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5407 | ImportedTypes[fromTy] = ToT.getTypePtr(); |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 5408 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5409 | return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 5410 | } |
| 5411 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 5412 | TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) { |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 5413 | if (!FromTSI) |
| 5414 | return FromTSI; |
| 5415 | |
| 5416 | // FIXME: For now we just create a "trivial" type source info based |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 5417 | // 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] | 5418 | QualType T = Import(FromTSI->getType()); |
| 5419 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5420 | return nullptr; |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 5421 | |
| 5422 | return ToContext.getTrivialTypeSourceInfo(T, |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 5423 | Import(FromTSI->getTypeLoc().getLocStart())); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 5424 | } |
| 5425 | |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 5426 | Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) { |
| 5427 | llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD); |
| 5428 | if (Pos != ImportedDecls.end()) { |
| 5429 | Decl *ToD = Pos->second; |
| 5430 | ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD); |
| 5431 | return ToD; |
| 5432 | } else { |
| 5433 | return nullptr; |
| 5434 | } |
| 5435 | } |
| 5436 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 5437 | Decl *ASTImporter::Import(Decl *FromD) { |
| 5438 | if (!FromD) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5439 | return nullptr; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 5440 | |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 5441 | ASTNodeImporter Importer(*this); |
| 5442 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 5443 | // Check whether we've already imported this declaration. |
| 5444 | llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 5445 | if (Pos != ImportedDecls.end()) { |
| 5446 | Decl *ToD = Pos->second; |
| 5447 | Importer.ImportDefinitionIfNeeded(FromD, ToD); |
| 5448 | return ToD; |
| 5449 | } |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 5450 | |
| 5451 | // Import the type |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 5452 | Decl *ToD = Importer.Visit(FromD); |
| 5453 | if (!ToD) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5454 | return nullptr; |
| 5455 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 5456 | // Record the imported declaration. |
| 5457 | ImportedDecls[FromD] = ToD; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 5458 | |
| 5459 | if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) { |
| 5460 | // Keep track of anonymous tags that have an associated typedef. |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5461 | if (FromTag->getTypedefNameForAnonDecl()) |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 5462 | AnonTagsWithPendingTypedefs.push_back(FromTag); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5463 | } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 5464 | // When we've finished transforming a typedef, see whether it was the |
| 5465 | // typedef for an anonymous tag. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 5466 | for (SmallVectorImpl<TagDecl *>::iterator |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 5467 | FromTag = AnonTagsWithPendingTypedefs.begin(), |
| 5468 | FromTagEnd = AnonTagsWithPendingTypedefs.end(); |
| 5469 | FromTag != FromTagEnd; ++FromTag) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5470 | if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 5471 | if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) { |
| 5472 | // We found the typedef for an anonymous tag; link them. |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5473 | ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD)); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 5474 | AnonTagsWithPendingTypedefs.erase(FromTag); |
| 5475 | break; |
| 5476 | } |
| 5477 | } |
| 5478 | } |
| 5479 | } |
| 5480 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 5481 | return ToD; |
| 5482 | } |
| 5483 | |
| 5484 | DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) { |
| 5485 | if (!FromDC) |
| 5486 | return FromDC; |
| 5487 | |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 5488 | DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC))); |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 5489 | if (!ToDC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5490 | return nullptr; |
| 5491 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 5492 | // When we're using a record/enum/Objective-C class/protocol as a context, we |
| 5493 | // need it to have a definition. |
| 5494 | if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) { |
Douglas Gregor | 63db971 | 2012-01-25 01:13:20 +0000 | [diff] [blame] | 5495 | RecordDecl *FromRecord = cast<RecordDecl>(FromDC); |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 5496 | if (ToRecord->isCompleteDefinition()) { |
| 5497 | // Do nothing. |
| 5498 | } else if (FromRecord->isCompleteDefinition()) { |
| 5499 | ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord, |
| 5500 | ASTNodeImporter::IDK_Basic); |
| 5501 | } else { |
| 5502 | CompleteDecl(ToRecord); |
| 5503 | } |
| 5504 | } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) { |
| 5505 | EnumDecl *FromEnum = cast<EnumDecl>(FromDC); |
| 5506 | if (ToEnum->isCompleteDefinition()) { |
| 5507 | // Do nothing. |
| 5508 | } else if (FromEnum->isCompleteDefinition()) { |
| 5509 | ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum, |
| 5510 | ASTNodeImporter::IDK_Basic); |
| 5511 | } else { |
| 5512 | CompleteDecl(ToEnum); |
| 5513 | } |
| 5514 | } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) { |
| 5515 | ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC); |
| 5516 | if (ToClass->getDefinition()) { |
| 5517 | // Do nothing. |
| 5518 | } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) { |
| 5519 | ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass, |
| 5520 | ASTNodeImporter::IDK_Basic); |
| 5521 | } else { |
| 5522 | CompleteDecl(ToClass); |
| 5523 | } |
| 5524 | } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) { |
| 5525 | ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC); |
| 5526 | if (ToProto->getDefinition()) { |
| 5527 | // Do nothing. |
| 5528 | } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) { |
| 5529 | ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto, |
| 5530 | ASTNodeImporter::IDK_Basic); |
| 5531 | } else { |
| 5532 | CompleteDecl(ToProto); |
| 5533 | } |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 5534 | } |
| 5535 | |
| 5536 | return ToDC; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 5537 | } |
| 5538 | |
| 5539 | Expr *ASTImporter::Import(Expr *FromE) { |
| 5540 | if (!FromE) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5541 | return nullptr; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 5542 | |
| 5543 | return cast_or_null<Expr>(Import(cast<Stmt>(FromE))); |
| 5544 | } |
| 5545 | |
| 5546 | Stmt *ASTImporter::Import(Stmt *FromS) { |
| 5547 | if (!FromS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5548 | return nullptr; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 5549 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 5550 | // Check whether we've already imported this declaration. |
| 5551 | llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS); |
| 5552 | if (Pos != ImportedStmts.end()) |
| 5553 | return Pos->second; |
| 5554 | |
| 5555 | // Import the type |
| 5556 | ASTNodeImporter Importer(*this); |
| 5557 | Stmt *ToS = Importer.Visit(FromS); |
| 5558 | if (!ToS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5559 | return nullptr; |
| 5560 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 5561 | // Record the imported declaration. |
| 5562 | ImportedStmts[FromS] = ToS; |
| 5563 | return ToS; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 5564 | } |
| 5565 | |
| 5566 | NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) { |
| 5567 | if (!FromNNS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5568 | return nullptr; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 5569 | |
Douglas Gregor | 90ebf25 | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 5570 | NestedNameSpecifier *prefix = Import(FromNNS->getPrefix()); |
| 5571 | |
| 5572 | switch (FromNNS->getKind()) { |
| 5573 | case NestedNameSpecifier::Identifier: |
| 5574 | if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) { |
| 5575 | return NestedNameSpecifier::Create(ToContext, prefix, II); |
| 5576 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5577 | return nullptr; |
Douglas Gregor | 90ebf25 | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 5578 | |
| 5579 | case NestedNameSpecifier::Namespace: |
| 5580 | if (NamespaceDecl *NS = |
| 5581 | cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) { |
| 5582 | return NestedNameSpecifier::Create(ToContext, prefix, NS); |
| 5583 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5584 | return nullptr; |
Douglas Gregor | 90ebf25 | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 5585 | |
| 5586 | case NestedNameSpecifier::NamespaceAlias: |
| 5587 | if (NamespaceAliasDecl *NSAD = |
| 5588 | cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) { |
| 5589 | return NestedNameSpecifier::Create(ToContext, prefix, NSAD); |
| 5590 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5591 | return nullptr; |
Douglas Gregor | 90ebf25 | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 5592 | |
| 5593 | case NestedNameSpecifier::Global: |
| 5594 | return NestedNameSpecifier::GlobalSpecifier(ToContext); |
| 5595 | |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 5596 | case NestedNameSpecifier::Super: |
| 5597 | if (CXXRecordDecl *RD = |
| 5598 | cast<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) { |
| 5599 | return NestedNameSpecifier::SuperSpecifier(ToContext, RD); |
| 5600 | } |
| 5601 | return nullptr; |
| 5602 | |
Douglas Gregor | 90ebf25 | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 5603 | case NestedNameSpecifier::TypeSpec: |
| 5604 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 5605 | QualType T = Import(QualType(FromNNS->getAsType(), 0u)); |
| 5606 | if (!T.isNull()) { |
| 5607 | bool bTemplate = FromNNS->getKind() == |
| 5608 | NestedNameSpecifier::TypeSpecWithTemplate; |
| 5609 | return NestedNameSpecifier::Create(ToContext, prefix, |
| 5610 | bTemplate, T.getTypePtr()); |
| 5611 | } |
| 5612 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5613 | return nullptr; |
Douglas Gregor | 90ebf25 | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 5614 | } |
| 5615 | |
| 5616 | llvm_unreachable("Invalid nested name specifier kind"); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 5617 | } |
| 5618 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5619 | NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) { |
| 5620 | // FIXME: Implement! |
| 5621 | return NestedNameSpecifierLoc(); |
| 5622 | } |
| 5623 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 5624 | TemplateName ASTImporter::Import(TemplateName From) { |
| 5625 | switch (From.getKind()) { |
| 5626 | case TemplateName::Template: |
| 5627 | if (TemplateDecl *ToTemplate |
| 5628 | = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl()))) |
| 5629 | return TemplateName(ToTemplate); |
| 5630 | |
| 5631 | return TemplateName(); |
| 5632 | |
| 5633 | case TemplateName::OverloadedTemplate: { |
| 5634 | OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate(); |
| 5635 | UnresolvedSet<2> ToTemplates; |
| 5636 | for (OverloadedTemplateStorage::iterator I = FromStorage->begin(), |
| 5637 | E = FromStorage->end(); |
| 5638 | I != E; ++I) { |
| 5639 | if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I))) |
| 5640 | ToTemplates.addDecl(To); |
| 5641 | else |
| 5642 | return TemplateName(); |
| 5643 | } |
| 5644 | return ToContext.getOverloadedTemplateName(ToTemplates.begin(), |
| 5645 | ToTemplates.end()); |
| 5646 | } |
| 5647 | |
| 5648 | case TemplateName::QualifiedTemplate: { |
| 5649 | QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName(); |
| 5650 | NestedNameSpecifier *Qualifier = Import(QTN->getQualifier()); |
| 5651 | if (!Qualifier) |
| 5652 | return TemplateName(); |
| 5653 | |
| 5654 | if (TemplateDecl *ToTemplate |
| 5655 | = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl()))) |
| 5656 | return ToContext.getQualifiedTemplateName(Qualifier, |
| 5657 | QTN->hasTemplateKeyword(), |
| 5658 | ToTemplate); |
| 5659 | |
| 5660 | return TemplateName(); |
| 5661 | } |
| 5662 | |
| 5663 | case TemplateName::DependentTemplate: { |
| 5664 | DependentTemplateName *DTN = From.getAsDependentTemplateName(); |
| 5665 | NestedNameSpecifier *Qualifier = Import(DTN->getQualifier()); |
| 5666 | if (!Qualifier) |
| 5667 | return TemplateName(); |
| 5668 | |
| 5669 | if (DTN->isIdentifier()) { |
| 5670 | return ToContext.getDependentTemplateName(Qualifier, |
| 5671 | Import(DTN->getIdentifier())); |
| 5672 | } |
| 5673 | |
| 5674 | return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator()); |
| 5675 | } |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5676 | |
| 5677 | case TemplateName::SubstTemplateTemplateParm: { |
| 5678 | SubstTemplateTemplateParmStorage *subst |
| 5679 | = From.getAsSubstTemplateTemplateParm(); |
| 5680 | TemplateTemplateParmDecl *param |
| 5681 | = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter())); |
| 5682 | if (!param) |
| 5683 | return TemplateName(); |
| 5684 | |
| 5685 | TemplateName replacement = Import(subst->getReplacement()); |
| 5686 | if (replacement.isNull()) return TemplateName(); |
| 5687 | |
| 5688 | return ToContext.getSubstTemplateTemplateParm(param, replacement); |
| 5689 | } |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 5690 | |
| 5691 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 5692 | SubstTemplateTemplateParmPackStorage *SubstPack |
| 5693 | = From.getAsSubstTemplateTemplateParmPack(); |
| 5694 | TemplateTemplateParmDecl *Param |
| 5695 | = cast_or_null<TemplateTemplateParmDecl>( |
| 5696 | Import(SubstPack->getParameterPack())); |
| 5697 | if (!Param) |
| 5698 | return TemplateName(); |
| 5699 | |
| 5700 | ASTNodeImporter Importer(*this); |
| 5701 | TemplateArgument ArgPack |
| 5702 | = Importer.ImportTemplateArgument(SubstPack->getArgumentPack()); |
| 5703 | if (ArgPack.isNull()) |
| 5704 | return TemplateName(); |
| 5705 | |
| 5706 | return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack); |
| 5707 | } |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 5708 | } |
| 5709 | |
| 5710 | llvm_unreachable("Invalid template name kind"); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 5711 | } |
| 5712 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 5713 | SourceLocation ASTImporter::Import(SourceLocation FromLoc) { |
| 5714 | if (FromLoc.isInvalid()) |
| 5715 | return SourceLocation(); |
| 5716 | |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 5717 | SourceManager &FromSM = FromContext.getSourceManager(); |
| 5718 | |
| 5719 | // For now, map everything down to its spelling location, so that we |
Chandler Carruth | 2536641 | 2011-07-15 00:04:35 +0000 | [diff] [blame] | 5720 | // don't have to import macro expansions. |
| 5721 | // FIXME: Import macro expansions! |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 5722 | FromLoc = FromSM.getSpellingLoc(FromLoc); |
| 5723 | std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc); |
| 5724 | SourceManager &ToSM = ToContext.getSourceManager(); |
Sean Callanan | 238d897 | 2014-12-10 01:26:39 +0000 | [diff] [blame] | 5725 | FileID ToFileID = Import(Decomposed.first); |
| 5726 | if (ToFileID.isInvalid()) |
| 5727 | return SourceLocation(); |
Sean Callanan | 59721b3 | 2015-04-28 18:41:46 +0000 | [diff] [blame] | 5728 | SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID) |
| 5729 | .getLocWithOffset(Decomposed.second); |
| 5730 | return ret; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 5731 | } |
| 5732 | |
| 5733 | SourceRange ASTImporter::Import(SourceRange FromRange) { |
| 5734 | return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd())); |
| 5735 | } |
| 5736 | |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 5737 | FileID ASTImporter::Import(FileID FromID) { |
Sebastian Redl | 99219f1 | 2010-09-30 01:03:06 +0000 | [diff] [blame] | 5738 | llvm::DenseMap<FileID, FileID>::iterator Pos |
| 5739 | = ImportedFileIDs.find(FromID); |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 5740 | if (Pos != ImportedFileIDs.end()) |
| 5741 | return Pos->second; |
| 5742 | |
| 5743 | SourceManager &FromSM = FromContext.getSourceManager(); |
| 5744 | SourceManager &ToSM = ToContext.getSourceManager(); |
| 5745 | const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID); |
Chandler Carruth | 2536641 | 2011-07-15 00:04:35 +0000 | [diff] [blame] | 5746 | assert(FromSLoc.isFile() && "Cannot handle macro expansions yet"); |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 5747 | |
| 5748 | // Include location of this file. |
| 5749 | SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc()); |
| 5750 | |
| 5751 | // Map the FileID for to the "to" source manager. |
| 5752 | FileID ToID; |
| 5753 | const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache(); |
Sean Callanan | 25d34af | 2015-04-30 00:44:21 +0000 | [diff] [blame] | 5754 | if (Cache->OrigEntry && Cache->OrigEntry->getDir()) { |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 5755 | // FIXME: We probably want to use getVirtualFile(), so we don't hit the |
| 5756 | // disk again |
| 5757 | // FIXME: We definitely want to re-use the existing MemoryBuffer, rather |
| 5758 | // than mmap the files several times. |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 5759 | const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName()); |
Sean Callanan | 238d897 | 2014-12-10 01:26:39 +0000 | [diff] [blame] | 5760 | if (!Entry) |
| 5761 | return FileID(); |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 5762 | ToID = ToSM.createFileID(Entry, ToIncludeLoc, |
| 5763 | FromSLoc.getFile().getFileCharacteristic()); |
| 5764 | } else { |
| 5765 | // FIXME: We want to re-use the existing MemoryBuffer! |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 5766 | const llvm::MemoryBuffer * |
| 5767 | FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM); |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 5768 | std::unique_ptr<llvm::MemoryBuffer> ToBuf |
Chris Lattner | 58c7934 | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 5769 | = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(), |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 5770 | FromBuf->getBufferIdentifier()); |
David Blaikie | 50a5f97 | 2014-08-29 07:59:55 +0000 | [diff] [blame] | 5771 | ToID = ToSM.createFileID(std::move(ToBuf), |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 5772 | FromSLoc.getFile().getFileCharacteristic()); |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 5773 | } |
| 5774 | |
| 5775 | |
Sebastian Redl | 99219f1 | 2010-09-30 01:03:06 +0000 | [diff] [blame] | 5776 | ImportedFileIDs[FromID] = ToID; |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 5777 | return ToID; |
| 5778 | } |
| 5779 | |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 5780 | void ASTImporter::ImportDefinition(Decl *From) { |
| 5781 | Decl *To = Import(From); |
| 5782 | if (!To) |
| 5783 | return; |
| 5784 | |
| 5785 | if (DeclContext *FromDC = cast<DeclContext>(From)) { |
| 5786 | ASTNodeImporter Importer(*this); |
Sean Callanan | 53a6bff | 2011-07-19 22:38:25 +0000 | [diff] [blame] | 5787 | |
| 5788 | if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) { |
| 5789 | if (!ToRecord->getDefinition()) { |
| 5790 | Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord, |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 5791 | ASTNodeImporter::IDK_Everything); |
Sean Callanan | 53a6bff | 2011-07-19 22:38:25 +0000 | [diff] [blame] | 5792 | return; |
| 5793 | } |
| 5794 | } |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 5795 | |
| 5796 | if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) { |
| 5797 | if (!ToEnum->getDefinition()) { |
| 5798 | Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 5799 | ASTNodeImporter::IDK_Everything); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 5800 | return; |
| 5801 | } |
| 5802 | } |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 5803 | |
| 5804 | if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) { |
| 5805 | if (!ToIFace->getDefinition()) { |
| 5806 | Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 5807 | ASTNodeImporter::IDK_Everything); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 5808 | return; |
| 5809 | } |
| 5810 | } |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 5811 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 5812 | if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) { |
| 5813 | if (!ToProto->getDefinition()) { |
| 5814 | Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 5815 | ASTNodeImporter::IDK_Everything); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 5816 | return; |
| 5817 | } |
| 5818 | } |
| 5819 | |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 5820 | Importer.ImportDeclContext(FromDC, true); |
| 5821 | } |
| 5822 | } |
| 5823 | |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 5824 | DeclarationName ASTImporter::Import(DeclarationName FromName) { |
| 5825 | if (!FromName) |
| 5826 | return DeclarationName(); |
| 5827 | |
| 5828 | switch (FromName.getNameKind()) { |
| 5829 | case DeclarationName::Identifier: |
| 5830 | return Import(FromName.getAsIdentifierInfo()); |
| 5831 | |
| 5832 | case DeclarationName::ObjCZeroArgSelector: |
| 5833 | case DeclarationName::ObjCOneArgSelector: |
| 5834 | case DeclarationName::ObjCMultiArgSelector: |
| 5835 | return Import(FromName.getObjCSelector()); |
| 5836 | |
| 5837 | case DeclarationName::CXXConstructorName: { |
| 5838 | QualType T = Import(FromName.getCXXNameType()); |
| 5839 | if (T.isNull()) |
| 5840 | return DeclarationName(); |
| 5841 | |
| 5842 | return ToContext.DeclarationNames.getCXXConstructorName( |
| 5843 | ToContext.getCanonicalType(T)); |
| 5844 | } |
| 5845 | |
| 5846 | case DeclarationName::CXXDestructorName: { |
| 5847 | QualType T = Import(FromName.getCXXNameType()); |
| 5848 | if (T.isNull()) |
| 5849 | return DeclarationName(); |
| 5850 | |
| 5851 | return ToContext.DeclarationNames.getCXXDestructorName( |
| 5852 | ToContext.getCanonicalType(T)); |
| 5853 | } |
| 5854 | |
| 5855 | case DeclarationName::CXXConversionFunctionName: { |
| 5856 | QualType T = Import(FromName.getCXXNameType()); |
| 5857 | if (T.isNull()) |
| 5858 | return DeclarationName(); |
| 5859 | |
| 5860 | return ToContext.DeclarationNames.getCXXConversionFunctionName( |
| 5861 | ToContext.getCanonicalType(T)); |
| 5862 | } |
| 5863 | |
| 5864 | case DeclarationName::CXXOperatorName: |
| 5865 | return ToContext.DeclarationNames.getCXXOperatorName( |
| 5866 | FromName.getCXXOverloadedOperator()); |
| 5867 | |
| 5868 | case DeclarationName::CXXLiteralOperatorName: |
| 5869 | return ToContext.DeclarationNames.getCXXLiteralOperatorName( |
| 5870 | Import(FromName.getCXXLiteralIdentifier())); |
| 5871 | |
| 5872 | case DeclarationName::CXXUsingDirective: |
| 5873 | // FIXME: STATICS! |
| 5874 | return DeclarationName::getUsingDirectiveName(); |
| 5875 | } |
| 5876 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 5877 | llvm_unreachable("Invalid DeclarationName Kind!"); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 5878 | } |
| 5879 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 5880 | IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 5881 | if (!FromId) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5882 | return nullptr; |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 5883 | |
| 5884 | return &ToContext.Idents.get(FromId->getName()); |
| 5885 | } |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 5886 | |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 5887 | Selector ASTImporter::Import(Selector FromSel) { |
| 5888 | if (FromSel.isNull()) |
| 5889 | return Selector(); |
| 5890 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5891 | SmallVector<IdentifierInfo *, 4> Idents; |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 5892 | Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0))); |
| 5893 | for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I) |
| 5894 | Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I))); |
| 5895 | return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data()); |
| 5896 | } |
| 5897 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 5898 | DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name, |
| 5899 | DeclContext *DC, |
| 5900 | unsigned IDNS, |
| 5901 | NamedDecl **Decls, |
| 5902 | unsigned NumDecls) { |
| 5903 | return Name; |
| 5904 | } |
| 5905 | |
| 5906 | DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) { |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 5907 | if (LastDiagFromFrom) |
| 5908 | ToContext.getDiagnostics().notePriorDiagnosticFrom( |
| 5909 | FromContext.getDiagnostics()); |
| 5910 | LastDiagFromFrom = false; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 5911 | return ToContext.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 5912 | } |
| 5913 | |
| 5914 | DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) { |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 5915 | if (!LastDiagFromFrom) |
| 5916 | FromContext.getDiagnostics().notePriorDiagnosticFrom( |
| 5917 | ToContext.getDiagnostics()); |
| 5918 | LastDiagFromFrom = true; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 5919 | return FromContext.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 5920 | } |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 5921 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 5922 | void ASTImporter::CompleteDecl (Decl *D) { |
| 5923 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 5924 | if (!ID->getDefinition()) |
| 5925 | ID->startDefinition(); |
| 5926 | } |
| 5927 | else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
| 5928 | if (!PD->getDefinition()) |
| 5929 | PD->startDefinition(); |
| 5930 | } |
| 5931 | else if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 5932 | if (!TD->getDefinition() && !TD->isBeingDefined()) { |
| 5933 | TD->startDefinition(); |
| 5934 | TD->setCompleteDefinition(true); |
| 5935 | } |
| 5936 | } |
| 5937 | else { |
| 5938 | assert (0 && "CompleteDecl called on a Decl that can't be completed"); |
| 5939 | } |
| 5940 | } |
| 5941 | |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 5942 | Decl *ASTImporter::Imported(Decl *From, Decl *To) { |
| 5943 | ImportedDecls[From] = To; |
| 5944 | return To; |
Daniel Dunbar | 9ced542 | 2010-02-13 20:24:39 +0000 | [diff] [blame] | 5945 | } |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 5946 | |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 5947 | bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To, |
| 5948 | bool Complain) { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5949 | llvm::DenseMap<const Type *, const Type *>::iterator Pos |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 5950 | = ImportedTypes.find(From.getTypePtr()); |
| 5951 | if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To)) |
| 5952 | return true; |
| 5953 | |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 5954 | StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls, |
| 5955 | false, Complain); |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 5956 | return Ctx.IsStructurallyEquivalent(From, To); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 5957 | } |