blob: f4698ee229b6fdfd3a82ad563be44d9b221495be [file] [log] [blame]
Douglas Gregor96e578d2010-02-05 17:54:41 +00001//===--- 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 Gregor96e578d2010-02-05 17:54:41 +000015#include "clang/AST/ASTContext.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000016#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor5c73e912010-02-11 00:48:18 +000017#include "clang/AST/DeclCXX.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000019#include "clang/AST/DeclVisitor.h"
Douglas Gregor7eeb5972010-02-11 19:21:55 +000020#include "clang/AST/StmtVisitor.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000021#include "clang/AST/TypeVisitor.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000022#include "clang/Basic/FileManager.h"
23#include "clang/Basic/SourceManager.h"
24#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor3996e242010-02-15 22:01:00 +000025#include <deque>
Douglas Gregor96e578d2010-02-05 17:54:41 +000026
Douglas Gregor3c2404b2011-11-03 18:07:07 +000027namespace clang {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000028 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor7eeb5972010-02-11 19:21:55 +000029 public DeclVisitor<ASTNodeImporter, Decl *>,
30 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor96e578d2010-02-05 17:54:41 +000031 ASTImporter &Importer;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000032
Douglas Gregor96e578d2010-02-05 17:54:41 +000033 public:
34 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
35
36 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor62d311f2010-02-09 19:21:46 +000037 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor7eeb5972010-02-11 19:21:55 +000038 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +000039
40 // Importing types
John McCall424cec92011-01-19 06:33:43 +000041 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 Gregor96e578d2010-02-05 17:54:41 +000052 // FIXME: DependentSizedArrayType
53 // FIXME: DependentSizedExtVectorType
John McCall424cec92011-01-19 06:33:43 +000054 QualType VisitVectorType(const VectorType *T);
55 QualType VisitExtVectorType(const ExtVectorType *T);
56 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
57 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000058 // FIXME: UnresolvedUsingType
Sean Callananda6df8a2011-08-11 16:56:07 +000059 QualType VisitParenType(const ParenType *T);
John McCall424cec92011-01-19 06:33:43 +000060 QualType VisitTypedefType(const TypedefType *T);
61 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000062 // FIXME: DependentTypeOfExprType
John McCall424cec92011-01-19 06:33:43 +000063 QualType VisitTypeOfType(const TypeOfType *T);
64 QualType VisitDecltypeType(const DecltypeType *T);
Alexis Hunte852b102011-05-24 22:41:36 +000065 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith30482bc2011-02-20 03:19:35 +000066 QualType VisitAutoType(const AutoType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000067 QualType VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000068 // FIXME: DependentDecltypeType
John McCall424cec92011-01-19 06:33:43 +000069 QualType VisitRecordType(const RecordType *T);
70 QualType VisitEnumType(const EnumType *T);
Sean Callanan72fe0852015-04-02 23:50:08 +000071 QualType VisitAttributedType(const AttributedType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000072 QualType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000073 // FIXME: SubstTemplateTypeParmType
John McCall424cec92011-01-19 06:33:43 +000074 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
75 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +000076 // FIXME: DependentNameType
John McCallc392f372010-06-11 00:33:02 +000077 // FIXME: DependentTemplateSpecializationType
John McCall424cec92011-01-19 06:33:43 +000078 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
79 QualType VisitObjCObjectType(const ObjCObjectType *T);
80 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000081
Douglas Gregor95d82832012-01-24 18:36:04 +000082 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +000083 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
84 DeclContext *&LexicalDC, DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +000085 NamedDecl *&ToD, SourceLocation &Loc);
Craig Topper36250ad2014-05-12 05:36:57 +000086 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +000087 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
88 DeclarationNameInfo& To);
Douglas Gregor0a791672011-01-18 03:11:38 +000089 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000090
91 typedef DesignatedInitExpr::Designator Designator;
92 Designator ImportDesignator(const Designator &D);
93
Douglas Gregor2e15c842012-02-01 21:00:38 +000094
Douglas Gregor95d82832012-01-24 18:36:04 +000095 /// \brief What we should import from the definition.
96 enum ImportDefinitionKind {
97 /// \brief Import the default subset of the definition, which might be
98 /// nothing (if minimal import is set) or might be everything (if minimal
99 /// import is not set).
100 IDK_Default,
101 /// \brief Import everything.
102 IDK_Everything,
103 /// \brief Import only the bare bones needed to establish a valid
104 /// DeclContext.
105 IDK_Basic
106 };
107
Douglas Gregor2e15c842012-02-01 21:00:38 +0000108 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
109 return IDK == IDK_Everything ||
110 (IDK == IDK_Default && !Importer.isMinimalImport());
111 }
112
Douglas Gregord451ea92011-07-29 23:31:30 +0000113 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000114 ImportDefinitionKind Kind = IDK_Default);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000115 bool ImportDefinition(VarDecl *From, VarDecl *To,
116 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregord451ea92011-07-29 23:31:30 +0000117 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000118 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000119 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000120 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000121 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000122 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregora082a492010-11-30 19:14:50 +0000123 TemplateParameterList *ImportTemplateParameterList(
124 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000125 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
126 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
127 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000128 SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000129 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
130 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000131 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
132 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000133 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000134 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Douglas Gregora082a492010-11-30 19:14:50 +0000135 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000136 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000137 Decl *VisitDecl(Decl *D);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +0000138 Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000139 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000140 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000141 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000142 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000143 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000144 Decl *VisitLabelDecl(LabelDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000145 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000146 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000147 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000148 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000149 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
150 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
151 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
152 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000153 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000154 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000155 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000156 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000157 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000158 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000159 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000160 Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000161 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000162 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Sean Callanan0aae0412014-12-10 00:00:37 +0000163 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000164
165 ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list);
Douglas Gregor45635322010-02-16 01:20:57 +0000166 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000167 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000168 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000169 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000170 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000171 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
172 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
173 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
174 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000175 Decl *VisitClassTemplateSpecializationDecl(
176 ClassTemplateSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000177 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
178 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
179
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000180 // Importing statements
Sean Callanan59721b32015-04-28 18:41:46 +0000181 DeclGroupRef ImportDeclGroup(DeclGroupRef DG);
182
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000183 Stmt *VisitStmt(Stmt *S);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000184 Stmt *VisitGCCAsmStmt(GCCAsmStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000185 Stmt *VisitDeclStmt(DeclStmt *S);
186 Stmt *VisitNullStmt(NullStmt *S);
187 Stmt *VisitCompoundStmt(CompoundStmt *S);
188 Stmt *VisitCaseStmt(CaseStmt *S);
189 Stmt *VisitDefaultStmt(DefaultStmt *S);
190 Stmt *VisitLabelStmt(LabelStmt *S);
191 Stmt *VisitAttributedStmt(AttributedStmt *S);
192 Stmt *VisitIfStmt(IfStmt *S);
193 Stmt *VisitSwitchStmt(SwitchStmt *S);
194 Stmt *VisitWhileStmt(WhileStmt *S);
195 Stmt *VisitDoStmt(DoStmt *S);
196 Stmt *VisitForStmt(ForStmt *S);
197 Stmt *VisitGotoStmt(GotoStmt *S);
198 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S);
199 Stmt *VisitContinueStmt(ContinueStmt *S);
200 Stmt *VisitBreakStmt(BreakStmt *S);
201 Stmt *VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000202 // FIXME: MSAsmStmt
203 // FIXME: SEHExceptStmt
204 // FIXME: SEHFinallyStmt
205 // FIXME: SEHTryStmt
206 // FIXME: SEHLeaveStmt
207 // FIXME: CapturedStmt
208 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S);
209 Stmt *VisitCXXTryStmt(CXXTryStmt *S);
210 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S);
211 // FIXME: MSDependentExistsStmt
212 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
213 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
214 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
215 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
216 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
217 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
218 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000219
220 // Importing expressions
221 Expr *VisitExpr(Expr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000222 Expr *VisitVAArgExpr(VAArgExpr *E);
223 Expr *VisitGNUNullExpr(GNUNullExpr *E);
224 Expr *VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000225 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000226 Expr *VisitImplicitValueInitExpr(ImplicitValueInitExpr *ILE);
227 Expr *VisitDesignatedInitExpr(DesignatedInitExpr *E);
228 Expr *VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000229 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000230 Expr *VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000231 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000232 Expr *VisitStringLiteral(StringLiteral *E);
233 Expr *VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
234 Expr *VisitAtomicExpr(AtomicExpr *E);
235 Expr *VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000236 Expr *VisitParenExpr(ParenExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000237 Expr *VisitParenListExpr(ParenListExpr *E);
238 Expr *VisitStmtExpr(StmtExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000239 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000240 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000241 Expr *VisitBinaryOperator(BinaryOperator *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000242 Expr *VisitConditionalOperator(ConditionalOperator *E);
243 Expr *VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
244 Expr *VisitOpaqueValueExpr(OpaqueValueExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000245 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000246 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor5481d322010-02-19 01:32:14 +0000247 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000248 Expr *VisitCXXConstructExpr(CXXConstructExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000249 Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
250 Expr *VisitCXXThisExpr(CXXThisExpr *E);
251 Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000252 Expr *VisitMemberExpr(MemberExpr *E);
253 Expr *VisitCallExpr(CallExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000254 Expr *VisitInitListExpr(InitListExpr *E);
Sean Callanandd2c1742016-05-16 20:48:03 +0000255 Expr *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
256 Expr *VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000257
258 template<typename IIter, typename OIter>
259 void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) {
260 typedef typename std::remove_reference<decltype(*Obegin)>::type ItemT;
261 ASTImporter &ImporterRef = Importer;
262 std::transform(Ibegin, Iend, Obegin,
263 [&ImporterRef](ItemT From) -> ItemT {
264 return ImporterRef.Import(From);
Sean Callanan8bca9962016-03-28 21:43:01 +0000265 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000266 }
267
268 template<typename IIter, typename OIter>
269 bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
270 typedef typename std::remove_reference<decltype(**Obegin)>::type ItemT;
271 ASTImporter &ImporterRef = Importer;
272 bool Failed = false;
273 std::transform(Ibegin, Iend, Obegin,
274 [&ImporterRef, &Failed](ItemT *From) -> ItemT * {
275 ItemT *To = ImporterRef.Import(From);
276 if (!To && From)
277 Failed = true;
278 return To;
279 });
280 return Failed;
Sean Callanan8bca9962016-03-28 21:43:01 +0000281 }
Douglas Gregor96e578d2010-02-05 17:54:41 +0000282 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000283}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000284
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000285using namespace clang;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000286
287//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000288// Structural Equivalence
289//----------------------------------------------------------------------------
290
291namespace {
292 struct StructuralEquivalenceContext {
293 /// \brief AST contexts for which we are checking structural equivalence.
294 ASTContext &C1, &C2;
295
Douglas Gregor3996e242010-02-15 22:01:00 +0000296 /// \brief The set of "tentative" equivalences between two canonical
297 /// declarations, mapping from a declaration in the first context to the
298 /// declaration in the second context that we believe to be equivalent.
299 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
300
301 /// \brief Queue of declarations in the first context whose equivalence
302 /// with a declaration in the second context still needs to be verified.
303 std::deque<Decl *> DeclsToCheck;
304
Douglas Gregorb4964f72010-02-15 23:54:17 +0000305 /// \brief Declaration (from, to) pairs that are known not to be equivalent
306 /// (which we have already complained about).
307 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
308
Douglas Gregor3996e242010-02-15 22:01:00 +0000309 /// \brief Whether we're being strict about the spelling of types when
310 /// unifying two types.
311 bool StrictTypeSpelling;
Douglas Gregordd6006f2012-07-17 21:16:27 +0000312
313 /// \brief Whether to complain about failures.
314 bool Complain;
315
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000316 /// \brief \c true if the last diagnostic came from C2.
317 bool LastDiagFromC2;
318
Douglas Gregor3996e242010-02-15 22:01:00 +0000319 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000320 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregordd6006f2012-07-17 21:16:27 +0000321 bool StrictTypeSpelling = false,
322 bool Complain = true)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000323 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000324 StrictTypeSpelling(StrictTypeSpelling), Complain(Complain),
325 LastDiagFromC2(false) {}
Douglas Gregor3996e242010-02-15 22:01:00 +0000326
327 /// \brief Determine whether the two declarations are structurally
328 /// equivalent.
329 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
330
331 /// \brief Determine whether the two types are structurally equivalent.
332 bool IsStructurallyEquivalent(QualType T1, QualType T2);
333
334 private:
335 /// \brief Finish checking all of the structural equivalences.
336 ///
337 /// \returns true if an error occurred, false otherwise.
338 bool Finish();
339
340 public:
341 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000342 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000343 if (LastDiagFromC2)
344 C1.getDiagnostics().notePriorDiagnosticFrom(C2.getDiagnostics());
345 LastDiagFromC2 = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000346 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000347 }
348
349 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000350 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000351 if (!LastDiagFromC2)
352 C2.getDiagnostics().notePriorDiagnosticFrom(C1.getDiagnostics());
353 LastDiagFromC2 = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000354 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000355 }
356 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000357}
Douglas Gregor3996e242010-02-15 22:01:00 +0000358
359static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
360 QualType T1, QualType T2);
361static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
362 Decl *D1, Decl *D2);
363
Douglas Gregor3996e242010-02-15 22:01:00 +0000364/// \brief Determine structural equivalence of two expressions.
365static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
366 Expr *E1, Expr *E2) {
367 if (!E1 || !E2)
368 return E1 == E2;
369
370 // FIXME: Actually perform a structural comparison!
371 return true;
372}
373
374/// \brief Determine whether two identifiers are equivalent.
375static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
376 const IdentifierInfo *Name2) {
377 if (!Name1 || !Name2)
378 return Name1 == Name2;
379
380 return Name1->getName() == Name2->getName();
381}
382
383/// \brief Determine whether two nested-name-specifiers are equivalent.
384static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
385 NestedNameSpecifier *NNS1,
386 NestedNameSpecifier *NNS2) {
387 // FIXME: Implement!
388 return true;
389}
390
391/// \brief Determine whether two template arguments are equivalent.
392static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
393 const TemplateArgument &Arg1,
394 const TemplateArgument &Arg2) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000395 if (Arg1.getKind() != Arg2.getKind())
396 return false;
397
398 switch (Arg1.getKind()) {
399 case TemplateArgument::Null:
400 return true;
401
402 case TemplateArgument::Type:
403 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
Eli Friedmanb826a002012-09-26 02:36:12 +0000404
Douglas Gregore2e50d332010-12-01 01:36:18 +0000405 case TemplateArgument::Integral:
406 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
407 Arg2.getIntegralType()))
408 return false;
409
Eric Christopher6dcc3762012-07-15 00:23:57 +0000410 return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral());
Douglas Gregore2e50d332010-12-01 01:36:18 +0000411
412 case TemplateArgument::Declaration:
413 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +0000414
415 case TemplateArgument::NullPtr:
416 return true; // FIXME: Is this correct?
417
Douglas Gregore2e50d332010-12-01 01:36:18 +0000418 case TemplateArgument::Template:
419 return IsStructurallyEquivalent(Context,
420 Arg1.getAsTemplate(),
421 Arg2.getAsTemplate());
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000422
423 case TemplateArgument::TemplateExpansion:
424 return IsStructurallyEquivalent(Context,
425 Arg1.getAsTemplateOrTemplatePattern(),
426 Arg2.getAsTemplateOrTemplatePattern());
427
Douglas Gregore2e50d332010-12-01 01:36:18 +0000428 case TemplateArgument::Expression:
429 return IsStructurallyEquivalent(Context,
430 Arg1.getAsExpr(), Arg2.getAsExpr());
431
432 case TemplateArgument::Pack:
433 if (Arg1.pack_size() != Arg2.pack_size())
434 return false;
435
436 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
437 if (!IsStructurallyEquivalent(Context,
438 Arg1.pack_begin()[I],
439 Arg2.pack_begin()[I]))
440 return false;
441
442 return true;
443 }
444
445 llvm_unreachable("Invalid template argument kind");
Douglas Gregor3996e242010-02-15 22:01:00 +0000446}
447
448/// \brief Determine structural equivalence for the common part of array
449/// types.
450static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
451 const ArrayType *Array1,
452 const ArrayType *Array2) {
453 if (!IsStructurallyEquivalent(Context,
454 Array1->getElementType(),
455 Array2->getElementType()))
456 return false;
457 if (Array1->getSizeModifier() != Array2->getSizeModifier())
458 return false;
459 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
460 return false;
461
462 return true;
463}
464
465/// \brief Determine structural equivalence of two types.
466static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
467 QualType T1, QualType T2) {
468 if (T1.isNull() || T2.isNull())
469 return T1.isNull() && T2.isNull();
470
471 if (!Context.StrictTypeSpelling) {
472 // We aren't being strict about token-to-token equivalence of types,
473 // so map down to the canonical type.
474 T1 = Context.C1.getCanonicalType(T1);
475 T2 = Context.C2.getCanonicalType(T2);
476 }
477
478 if (T1.getQualifiers() != T2.getQualifiers())
479 return false;
480
Douglas Gregorb4964f72010-02-15 23:54:17 +0000481 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000482
Douglas Gregorb4964f72010-02-15 23:54:17 +0000483 if (T1->getTypeClass() != T2->getTypeClass()) {
484 // Compare function types with prototypes vs. without prototypes as if
485 // both did not have prototypes.
486 if (T1->getTypeClass() == Type::FunctionProto &&
487 T2->getTypeClass() == Type::FunctionNoProto)
488 TC = Type::FunctionNoProto;
489 else if (T1->getTypeClass() == Type::FunctionNoProto &&
490 T2->getTypeClass() == Type::FunctionProto)
491 TC = Type::FunctionNoProto;
492 else
493 return false;
494 }
495
496 switch (TC) {
497 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000498 // FIXME: Deal with Char_S/Char_U.
499 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
500 return false;
501 break;
502
503 case Type::Complex:
504 if (!IsStructurallyEquivalent(Context,
505 cast<ComplexType>(T1)->getElementType(),
506 cast<ComplexType>(T2)->getElementType()))
507 return false;
508 break;
509
Reid Kleckner0503a872013-12-05 01:23:43 +0000510 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +0000511 case Type::Decayed:
512 if (!IsStructurallyEquivalent(Context,
Reid Kleckner0503a872013-12-05 01:23:43 +0000513 cast<AdjustedType>(T1)->getOriginalType(),
514 cast<AdjustedType>(T2)->getOriginalType()))
Reid Kleckner8a365022013-06-24 17:51:48 +0000515 return false;
516 break;
517
Douglas Gregor3996e242010-02-15 22:01:00 +0000518 case Type::Pointer:
519 if (!IsStructurallyEquivalent(Context,
520 cast<PointerType>(T1)->getPointeeType(),
521 cast<PointerType>(T2)->getPointeeType()))
522 return false;
523 break;
524
525 case Type::BlockPointer:
526 if (!IsStructurallyEquivalent(Context,
527 cast<BlockPointerType>(T1)->getPointeeType(),
528 cast<BlockPointerType>(T2)->getPointeeType()))
529 return false;
530 break;
531
532 case Type::LValueReference:
533 case Type::RValueReference: {
534 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
535 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
536 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
537 return false;
538 if (Ref1->isInnerRef() != Ref2->isInnerRef())
539 return false;
540 if (!IsStructurallyEquivalent(Context,
541 Ref1->getPointeeTypeAsWritten(),
542 Ref2->getPointeeTypeAsWritten()))
543 return false;
544 break;
545 }
546
547 case Type::MemberPointer: {
548 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
549 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
550 if (!IsStructurallyEquivalent(Context,
551 MemPtr1->getPointeeType(),
552 MemPtr2->getPointeeType()))
553 return false;
554 if (!IsStructurallyEquivalent(Context,
555 QualType(MemPtr1->getClass(), 0),
556 QualType(MemPtr2->getClass(), 0)))
557 return false;
558 break;
559 }
560
561 case Type::ConstantArray: {
562 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
563 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
Eric Christopher6dcc3762012-07-15 00:23:57 +0000564 if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000565 return false;
566
567 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
568 return false;
569 break;
570 }
571
572 case Type::IncompleteArray:
573 if (!IsArrayStructurallyEquivalent(Context,
574 cast<ArrayType>(T1),
575 cast<ArrayType>(T2)))
576 return false;
577 break;
578
579 case Type::VariableArray: {
580 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
581 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
582 if (!IsStructurallyEquivalent(Context,
583 Array1->getSizeExpr(), Array2->getSizeExpr()))
584 return false;
585
586 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
587 return false;
588
589 break;
590 }
591
592 case Type::DependentSizedArray: {
593 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
594 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
595 if (!IsStructurallyEquivalent(Context,
596 Array1->getSizeExpr(), Array2->getSizeExpr()))
597 return false;
598
599 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
600 return false;
601
602 break;
603 }
604
605 case Type::DependentSizedExtVector: {
606 const DependentSizedExtVectorType *Vec1
607 = cast<DependentSizedExtVectorType>(T1);
608 const DependentSizedExtVectorType *Vec2
609 = cast<DependentSizedExtVectorType>(T2);
610 if (!IsStructurallyEquivalent(Context,
611 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
612 return false;
613 if (!IsStructurallyEquivalent(Context,
614 Vec1->getElementType(),
615 Vec2->getElementType()))
616 return false;
617 break;
618 }
619
620 case Type::Vector:
621 case Type::ExtVector: {
622 const VectorType *Vec1 = cast<VectorType>(T1);
623 const VectorType *Vec2 = cast<VectorType>(T2);
624 if (!IsStructurallyEquivalent(Context,
625 Vec1->getElementType(),
626 Vec2->getElementType()))
627 return false;
628 if (Vec1->getNumElements() != Vec2->getNumElements())
629 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000630 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000631 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000632 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000633 }
634
635 case Type::FunctionProto: {
636 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
637 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
Alp Toker9cacbab2014-01-20 20:26:09 +0000638 if (Proto1->getNumParams() != Proto2->getNumParams())
Douglas Gregor3996e242010-02-15 22:01:00 +0000639 return false;
Alp Toker9cacbab2014-01-20 20:26:09 +0000640 for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) {
641 if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I),
642 Proto2->getParamType(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000643 return false;
644 }
645 if (Proto1->isVariadic() != Proto2->isVariadic())
646 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000647 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor3996e242010-02-15 22:01:00 +0000648 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000649 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
650 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
651 return false;
652 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
653 if (!IsStructurallyEquivalent(Context,
654 Proto1->getExceptionType(I),
655 Proto2->getExceptionType(I)))
656 return false;
657 }
658 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000659 if (!IsStructurallyEquivalent(Context,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000660 Proto1->getNoexceptExpr(),
661 Proto2->getNoexceptExpr()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000662 return false;
663 }
664 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
665 return false;
666
667 // Fall through to check the bits common with FunctionNoProtoType.
668 }
669
670 case Type::FunctionNoProto: {
671 const FunctionType *Function1 = cast<FunctionType>(T1);
672 const FunctionType *Function2 = cast<FunctionType>(T2);
Alp Toker314cc812014-01-25 16:55:45 +0000673 if (!IsStructurallyEquivalent(Context, Function1->getReturnType(),
674 Function2->getReturnType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000675 return false;
Justin Bogner62c04de2016-03-20 16:58:03 +0000676 if (Function1->getExtInfo() != Function2->getExtInfo())
677 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000678 break;
679 }
680
681 case Type::UnresolvedUsing:
682 if (!IsStructurallyEquivalent(Context,
683 cast<UnresolvedUsingType>(T1)->getDecl(),
684 cast<UnresolvedUsingType>(T2)->getDecl()))
685 return false;
686
687 break;
John McCall81904512011-01-06 01:58:22 +0000688
689 case Type::Attributed:
690 if (!IsStructurallyEquivalent(Context,
691 cast<AttributedType>(T1)->getModifiedType(),
692 cast<AttributedType>(T2)->getModifiedType()))
693 return false;
694 if (!IsStructurallyEquivalent(Context,
695 cast<AttributedType>(T1)->getEquivalentType(),
696 cast<AttributedType>(T2)->getEquivalentType()))
697 return false;
698 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000699
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000700 case Type::Paren:
701 if (!IsStructurallyEquivalent(Context,
702 cast<ParenType>(T1)->getInnerType(),
703 cast<ParenType>(T2)->getInnerType()))
704 return false;
705 break;
706
Douglas Gregor3996e242010-02-15 22:01:00 +0000707 case Type::Typedef:
708 if (!IsStructurallyEquivalent(Context,
709 cast<TypedefType>(T1)->getDecl(),
710 cast<TypedefType>(T2)->getDecl()))
711 return false;
712 break;
713
714 case Type::TypeOfExpr:
715 if (!IsStructurallyEquivalent(Context,
716 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
717 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
718 return false;
719 break;
720
721 case Type::TypeOf:
722 if (!IsStructurallyEquivalent(Context,
723 cast<TypeOfType>(T1)->getUnderlyingType(),
724 cast<TypeOfType>(T2)->getUnderlyingType()))
725 return false;
726 break;
Alexis Hunte852b102011-05-24 22:41:36 +0000727
728 case Type::UnaryTransform:
729 if (!IsStructurallyEquivalent(Context,
730 cast<UnaryTransformType>(T1)->getUnderlyingType(),
731 cast<UnaryTransformType>(T1)->getUnderlyingType()))
732 return false;
733 break;
734
Douglas Gregor3996e242010-02-15 22:01:00 +0000735 case Type::Decltype:
736 if (!IsStructurallyEquivalent(Context,
737 cast<DecltypeType>(T1)->getUnderlyingExpr(),
738 cast<DecltypeType>(T2)->getUnderlyingExpr()))
739 return false;
740 break;
741
Richard Smith30482bc2011-02-20 03:19:35 +0000742 case Type::Auto:
743 if (!IsStructurallyEquivalent(Context,
744 cast<AutoType>(T1)->getDeducedType(),
745 cast<AutoType>(T2)->getDeducedType()))
746 return false;
747 break;
748
Douglas Gregor3996e242010-02-15 22:01:00 +0000749 case Type::Record:
750 case Type::Enum:
751 if (!IsStructurallyEquivalent(Context,
752 cast<TagType>(T1)->getDecl(),
753 cast<TagType>(T2)->getDecl()))
754 return false;
755 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000756
Douglas Gregor3996e242010-02-15 22:01:00 +0000757 case Type::TemplateTypeParm: {
758 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
759 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
760 if (Parm1->getDepth() != Parm2->getDepth())
761 return false;
762 if (Parm1->getIndex() != Parm2->getIndex())
763 return false;
764 if (Parm1->isParameterPack() != Parm2->isParameterPack())
765 return false;
766
767 // Names of template type parameters are never significant.
768 break;
769 }
770
771 case Type::SubstTemplateTypeParm: {
772 const SubstTemplateTypeParmType *Subst1
773 = cast<SubstTemplateTypeParmType>(T1);
774 const SubstTemplateTypeParmType *Subst2
775 = cast<SubstTemplateTypeParmType>(T2);
776 if (!IsStructurallyEquivalent(Context,
777 QualType(Subst1->getReplacedParameter(), 0),
778 QualType(Subst2->getReplacedParameter(), 0)))
779 return false;
780 if (!IsStructurallyEquivalent(Context,
781 Subst1->getReplacementType(),
782 Subst2->getReplacementType()))
783 return false;
784 break;
785 }
786
Douglas Gregorfb322d82011-01-14 05:11:40 +0000787 case Type::SubstTemplateTypeParmPack: {
788 const SubstTemplateTypeParmPackType *Subst1
789 = cast<SubstTemplateTypeParmPackType>(T1);
790 const SubstTemplateTypeParmPackType *Subst2
791 = cast<SubstTemplateTypeParmPackType>(T2);
792 if (!IsStructurallyEquivalent(Context,
793 QualType(Subst1->getReplacedParameter(), 0),
794 QualType(Subst2->getReplacedParameter(), 0)))
795 return false;
796 if (!IsStructurallyEquivalent(Context,
797 Subst1->getArgumentPack(),
798 Subst2->getArgumentPack()))
799 return false;
800 break;
801 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000802 case Type::TemplateSpecialization: {
803 const TemplateSpecializationType *Spec1
804 = cast<TemplateSpecializationType>(T1);
805 const TemplateSpecializationType *Spec2
806 = cast<TemplateSpecializationType>(T2);
807 if (!IsStructurallyEquivalent(Context,
808 Spec1->getTemplateName(),
809 Spec2->getTemplateName()))
810 return false;
811 if (Spec1->getNumArgs() != Spec2->getNumArgs())
812 return false;
813 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
814 if (!IsStructurallyEquivalent(Context,
815 Spec1->getArg(I), Spec2->getArg(I)))
816 return false;
817 }
818 break;
819 }
820
Abramo Bagnara6150c882010-05-11 21:36:43 +0000821 case Type::Elaborated: {
822 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
823 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
824 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
825 if (Elab1->getKeyword() != Elab2->getKeyword())
826 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000827 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000828 Elab1->getQualifier(),
829 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000830 return false;
831 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000832 Elab1->getNamedType(),
833 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000834 return false;
835 break;
836 }
837
John McCalle78aac42010-03-10 03:28:59 +0000838 case Type::InjectedClassName: {
839 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
840 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
841 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000842 Inj1->getInjectedSpecializationType(),
843 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000844 return false;
845 break;
846 }
847
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000848 case Type::DependentName: {
849 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
850 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000851 if (!IsStructurallyEquivalent(Context,
852 Typename1->getQualifier(),
853 Typename2->getQualifier()))
854 return false;
855 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
856 Typename2->getIdentifier()))
857 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000858
859 break;
860 }
861
John McCallc392f372010-06-11 00:33:02 +0000862 case Type::DependentTemplateSpecialization: {
863 const DependentTemplateSpecializationType *Spec1 =
864 cast<DependentTemplateSpecializationType>(T1);
865 const DependentTemplateSpecializationType *Spec2 =
866 cast<DependentTemplateSpecializationType>(T2);
867 if (!IsStructurallyEquivalent(Context,
868 Spec1->getQualifier(),
869 Spec2->getQualifier()))
870 return false;
871 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
872 Spec2->getIdentifier()))
873 return false;
874 if (Spec1->getNumArgs() != Spec2->getNumArgs())
875 return false;
876 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
877 if (!IsStructurallyEquivalent(Context,
878 Spec1->getArg(I), Spec2->getArg(I)))
879 return false;
880 }
881 break;
882 }
Douglas Gregord2fa7662010-12-20 02:24:11 +0000883
884 case Type::PackExpansion:
885 if (!IsStructurallyEquivalent(Context,
886 cast<PackExpansionType>(T1)->getPattern(),
887 cast<PackExpansionType>(T2)->getPattern()))
888 return false;
889 break;
890
Douglas Gregor3996e242010-02-15 22:01:00 +0000891 case Type::ObjCInterface: {
892 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
893 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
894 if (!IsStructurallyEquivalent(Context,
895 Iface1->getDecl(), Iface2->getDecl()))
896 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000897 break;
898 }
899
900 case Type::ObjCObject: {
901 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
902 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
903 if (!IsStructurallyEquivalent(Context,
904 Obj1->getBaseType(),
905 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000906 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000907 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
908 return false;
909 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000910 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000911 Obj1->getProtocol(I),
912 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000913 return false;
914 }
915 break;
916 }
917
918 case Type::ObjCObjectPointer: {
919 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
920 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
921 if (!IsStructurallyEquivalent(Context,
922 Ptr1->getPointeeType(),
923 Ptr2->getPointeeType()))
924 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000925 break;
926 }
Eli Friedman0dfb8892011-10-06 23:00:33 +0000927
928 case Type::Atomic: {
929 if (!IsStructurallyEquivalent(Context,
930 cast<AtomicType>(T1)->getValueType(),
931 cast<AtomicType>(T2)->getValueType()))
932 return false;
933 break;
934 }
935
Xiuli Pan9c14e282016-01-09 12:53:17 +0000936 case Type::Pipe: {
937 if (!IsStructurallyEquivalent(Context,
938 cast<PipeType>(T1)->getElementType(),
939 cast<PipeType>(T2)->getElementType()))
940 return false;
941 break;
942 }
943
Douglas Gregor3996e242010-02-15 22:01:00 +0000944 } // end switch
945
946 return true;
947}
948
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000949/// \brief Determine structural equivalence of two fields.
950static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
951 FieldDecl *Field1, FieldDecl *Field2) {
952 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000953
954 // For anonymous structs/unions, match up the anonymous struct/union type
955 // declarations directly, so that we don't go off searching for anonymous
956 // types
957 if (Field1->isAnonymousStructOrUnion() &&
958 Field2->isAnonymousStructOrUnion()) {
959 RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl();
960 RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl();
961 return IsStructurallyEquivalent(Context, D1, D2);
962 }
Sean Callanan969c5bd2013-04-26 22:49:25 +0000963
964 // Check for equivalent field names.
965 IdentifierInfo *Name1 = Field1->getIdentifier();
966 IdentifierInfo *Name2 = Field2->getIdentifier();
967 if (!::IsStructurallyEquivalent(Name1, Name2))
968 return false;
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000969
970 if (!IsStructurallyEquivalent(Context,
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000971 Field1->getType(), Field2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000972 if (Context.Complain) {
973 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
974 << Context.C2.getTypeDeclType(Owner2);
975 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
976 << Field2->getDeclName() << Field2->getType();
977 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
978 << Field1->getDeclName() << Field1->getType();
979 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000980 return false;
981 }
982
983 if (Field1->isBitField() != Field2->isBitField()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000984 if (Context.Complain) {
985 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
986 << Context.C2.getTypeDeclType(Owner2);
987 if (Field1->isBitField()) {
988 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
989 << Field1->getDeclName() << Field1->getType()
990 << Field1->getBitWidthValue(Context.C1);
991 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
992 << Field2->getDeclName();
993 } else {
994 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
995 << Field2->getDeclName() << Field2->getType()
996 << Field2->getBitWidthValue(Context.C2);
997 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
998 << Field1->getDeclName();
999 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001000 }
1001 return false;
1002 }
1003
1004 if (Field1->isBitField()) {
1005 // Make sure that the bit-fields are the same length.
1006 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
1007 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
1008
1009 if (Bits1 != Bits2) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001010 if (Context.Complain) {
1011 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1012 << Context.C2.getTypeDeclType(Owner2);
1013 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
1014 << Field2->getDeclName() << Field2->getType() << Bits2;
1015 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
1016 << Field1->getDeclName() << Field1->getType() << Bits1;
1017 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001018 return false;
1019 }
1020 }
1021
1022 return true;
1023}
1024
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001025/// \brief Find the index of the given anonymous struct/union within its
1026/// context.
1027///
1028/// \returns Returns the index of this anonymous struct/union in its context,
1029/// including the next assigned index (if none of them match). Returns an
1030/// empty option if the context is not a record, i.e.. if the anonymous
1031/// struct/union is at namespace or block scope.
David Blaikie05785d12013-02-20 22:23:23 +00001032static Optional<unsigned> findAnonymousStructOrUnionIndex(RecordDecl *Anon) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001033 ASTContext &Context = Anon->getASTContext();
1034 QualType AnonTy = Context.getRecordType(Anon);
1035
1036 RecordDecl *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext());
1037 if (!Owner)
David Blaikie7a30dc52013-02-21 01:47:18 +00001038 return None;
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001039
1040 unsigned Index = 0;
Aaron Ballman629afae2014-03-07 19:56:05 +00001041 for (const auto *D : Owner->noload_decls()) {
1042 const auto *F = dyn_cast<FieldDecl>(D);
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001043 if (!F || !F->isAnonymousStructOrUnion())
1044 continue;
1045
1046 if (Context.hasSameType(F->getType(), AnonTy))
1047 break;
1048
1049 ++Index;
1050 }
1051
1052 return Index;
1053}
1054
Douglas Gregor3996e242010-02-15 22:01:00 +00001055/// \brief Determine structural equivalence of two records.
1056static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1057 RecordDecl *D1, RecordDecl *D2) {
1058 if (D1->isUnion() != D2->isUnion()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001059 if (Context.Complain) {
1060 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1061 << Context.C2.getTypeDeclType(D2);
1062 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
1063 << D1->getDeclName() << (unsigned)D1->getTagKind();
1064 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001065 return false;
1066 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001067
1068 if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) {
1069 // If both anonymous structs/unions are in a record context, make sure
1070 // they occur in the same location in the context records.
David Blaikie05785d12013-02-20 22:23:23 +00001071 if (Optional<unsigned> Index1 = findAnonymousStructOrUnionIndex(D1)) {
1072 if (Optional<unsigned> Index2 = findAnonymousStructOrUnionIndex(D2)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001073 if (*Index1 != *Index2)
1074 return false;
1075 }
1076 }
1077 }
1078
Douglas Gregore2e50d332010-12-01 01:36:18 +00001079 // If both declarations are class template specializations, we know
1080 // the ODR applies, so check the template and template arguments.
1081 ClassTemplateSpecializationDecl *Spec1
1082 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
1083 ClassTemplateSpecializationDecl *Spec2
1084 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
1085 if (Spec1 && Spec2) {
1086 // Check that the specialized templates are the same.
1087 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
1088 Spec2->getSpecializedTemplate()))
1089 return false;
1090
1091 // Check that the template arguments are the same.
1092 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
1093 return false;
1094
1095 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
1096 if (!IsStructurallyEquivalent(Context,
1097 Spec1->getTemplateArgs().get(I),
1098 Spec2->getTemplateArgs().get(I)))
1099 return false;
1100 }
1101 // If one is a class template specialization and the other is not, these
Chris Lattner57540c52011-04-15 05:22:18 +00001102 // structures are different.
Douglas Gregore2e50d332010-12-01 01:36:18 +00001103 else if (Spec1 || Spec2)
1104 return false;
1105
Douglas Gregorb4964f72010-02-15 23:54:17 +00001106 // Compare the definitions of these two records. If either or both are
1107 // incomplete, we assume that they are equivalent.
1108 D1 = D1->getDefinition();
1109 D2 = D2->getDefinition();
1110 if (!D1 || !D2)
1111 return true;
1112
Douglas Gregor3996e242010-02-15 22:01:00 +00001113 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
1114 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
1115 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001116 if (Context.Complain) {
1117 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1118 << Context.C2.getTypeDeclType(D2);
1119 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
1120 << D2CXX->getNumBases();
1121 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
1122 << D1CXX->getNumBases();
1123 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001124 return false;
1125 }
1126
1127 // Check the base classes.
1128 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
1129 BaseEnd1 = D1CXX->bases_end(),
1130 Base2 = D2CXX->bases_begin();
1131 Base1 != BaseEnd1;
1132 ++Base1, ++Base2) {
1133 if (!IsStructurallyEquivalent(Context,
1134 Base1->getType(), Base2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001135 if (Context.Complain) {
1136 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1137 << Context.C2.getTypeDeclType(D2);
1138 Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
1139 << Base2->getType()
1140 << Base2->getSourceRange();
1141 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1142 << Base1->getType()
1143 << Base1->getSourceRange();
1144 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001145 return false;
1146 }
1147
1148 // Check virtual vs. non-virtual inheritance mismatch.
1149 if (Base1->isVirtual() != Base2->isVirtual()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001150 if (Context.Complain) {
1151 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1152 << Context.C2.getTypeDeclType(D2);
1153 Context.Diag2(Base2->getLocStart(),
1154 diag::note_odr_virtual_base)
1155 << Base2->isVirtual() << Base2->getSourceRange();
1156 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1157 << Base1->isVirtual()
1158 << Base1->getSourceRange();
1159 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001160 return false;
1161 }
1162 }
1163 } else if (D1CXX->getNumBases() > 0) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001164 if (Context.Complain) {
1165 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1166 << Context.C2.getTypeDeclType(D2);
1167 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
1168 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1169 << Base1->getType()
1170 << Base1->getSourceRange();
1171 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
1172 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001173 return false;
1174 }
1175 }
1176
1177 // Check the fields for consistency.
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001178 RecordDecl::field_iterator Field2 = D2->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001179 Field2End = D2->field_end();
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001180 for (RecordDecl::field_iterator Field1 = D1->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001181 Field1End = D1->field_end();
1182 Field1 != Field1End;
1183 ++Field1, ++Field2) {
1184 if (Field2 == Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001185 if (Context.Complain) {
1186 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1187 << Context.C2.getTypeDeclType(D2);
1188 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
1189 << Field1->getDeclName() << Field1->getType();
1190 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
1191 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001192 return false;
1193 }
1194
David Blaikie40ed2972012-06-06 20:45:41 +00001195 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001196 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001197 }
1198
1199 if (Field2 != Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001200 if (Context.Complain) {
1201 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1202 << Context.C2.getTypeDeclType(D2);
1203 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1204 << Field2->getDeclName() << Field2->getType();
1205 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1206 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001207 return false;
1208 }
1209
1210 return true;
1211}
1212
1213/// \brief Determine structural equivalence of two enums.
1214static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1215 EnumDecl *D1, EnumDecl *D2) {
1216 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1217 EC2End = D2->enumerator_end();
1218 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1219 EC1End = D1->enumerator_end();
1220 EC1 != EC1End; ++EC1, ++EC2) {
1221 if (EC2 == EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001222 if (Context.Complain) {
1223 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1224 << Context.C2.getTypeDeclType(D2);
1225 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1226 << EC1->getDeclName()
1227 << EC1->getInitVal().toString(10);
1228 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1229 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001230 return false;
1231 }
1232
1233 llvm::APSInt Val1 = EC1->getInitVal();
1234 llvm::APSInt Val2 = EC2->getInitVal();
Eric Christopher6dcc3762012-07-15 00:23:57 +00001235 if (!llvm::APSInt::isSameValue(Val1, Val2) ||
Douglas Gregor3996e242010-02-15 22:01:00 +00001236 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001237 if (Context.Complain) {
1238 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1239 << Context.C2.getTypeDeclType(D2);
1240 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1241 << EC2->getDeclName()
1242 << EC2->getInitVal().toString(10);
1243 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1244 << EC1->getDeclName()
1245 << EC1->getInitVal().toString(10);
1246 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001247 return false;
1248 }
1249 }
1250
1251 if (EC2 != EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001252 if (Context.Complain) {
1253 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1254 << Context.C2.getTypeDeclType(D2);
1255 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1256 << EC2->getDeclName()
1257 << EC2->getInitVal().toString(10);
1258 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1259 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001260 return false;
1261 }
1262
1263 return true;
1264}
Douglas Gregora082a492010-11-30 19:14:50 +00001265
1266static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1267 TemplateParameterList *Params1,
1268 TemplateParameterList *Params2) {
1269 if (Params1->size() != Params2->size()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001270 if (Context.Complain) {
1271 Context.Diag2(Params2->getTemplateLoc(),
1272 diag::err_odr_different_num_template_parameters)
1273 << Params1->size() << Params2->size();
1274 Context.Diag1(Params1->getTemplateLoc(),
1275 diag::note_odr_template_parameter_list);
1276 }
Douglas Gregora082a492010-11-30 19:14:50 +00001277 return false;
1278 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001279
Douglas Gregora082a492010-11-30 19:14:50 +00001280 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1281 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001282 if (Context.Complain) {
1283 Context.Diag2(Params2->getParam(I)->getLocation(),
1284 diag::err_odr_different_template_parameter_kind);
1285 Context.Diag1(Params1->getParam(I)->getLocation(),
1286 diag::note_odr_template_parameter_here);
1287 }
Douglas Gregora082a492010-11-30 19:14:50 +00001288 return false;
1289 }
1290
1291 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1292 Params2->getParam(I))) {
1293
1294 return false;
1295 }
1296 }
1297
1298 return true;
1299}
1300
1301static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1302 TemplateTypeParmDecl *D1,
1303 TemplateTypeParmDecl *D2) {
1304 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001305 if (Context.Complain) {
1306 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1307 << D2->isParameterPack();
1308 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1309 << D1->isParameterPack();
1310 }
Douglas Gregora082a492010-11-30 19:14:50 +00001311 return false;
1312 }
1313
1314 return true;
1315}
1316
1317static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1318 NonTypeTemplateParmDecl *D1,
1319 NonTypeTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001320 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001321 if (Context.Complain) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001322 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1323 << D2->isParameterPack();
1324 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1325 << D1->isParameterPack();
1326 }
Douglas Gregora082a492010-11-30 19:14:50 +00001327 return false;
1328 }
Douglas Gregora082a492010-11-30 19:14:50 +00001329
1330 // Check types.
1331 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001332 if (Context.Complain) {
1333 Context.Diag2(D2->getLocation(),
1334 diag::err_odr_non_type_parameter_type_inconsistent)
1335 << D2->getType() << D1->getType();
1336 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1337 << D1->getType();
1338 }
Douglas Gregora082a492010-11-30 19:14:50 +00001339 return false;
1340 }
1341
1342 return true;
1343}
1344
1345static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1346 TemplateTemplateParmDecl *D1,
1347 TemplateTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001348 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001349 if (Context.Complain) {
1350 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1351 << D2->isParameterPack();
1352 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1353 << D1->isParameterPack();
1354 }
Douglas Gregora082a492010-11-30 19:14:50 +00001355 return false;
1356 }
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001357
Douglas Gregora082a492010-11-30 19:14:50 +00001358 // Check template parameter lists.
1359 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1360 D2->getTemplateParameters());
1361}
1362
1363static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1364 ClassTemplateDecl *D1,
1365 ClassTemplateDecl *D2) {
1366 // Check template parameters.
1367 if (!IsStructurallyEquivalent(Context,
1368 D1->getTemplateParameters(),
1369 D2->getTemplateParameters()))
1370 return false;
1371
1372 // Check the templated declaration.
1373 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1374 D2->getTemplatedDecl());
1375}
1376
Douglas Gregor3996e242010-02-15 22:01:00 +00001377/// \brief Determine structural equivalence of two declarations.
1378static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1379 Decl *D1, Decl *D2) {
1380 // FIXME: Check for known structural equivalences via a callback of some sort.
1381
Douglas Gregorb4964f72010-02-15 23:54:17 +00001382 // Check whether we already know that these two declarations are not
1383 // structurally equivalent.
1384 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1385 D2->getCanonicalDecl())))
1386 return false;
1387
Douglas Gregor3996e242010-02-15 22:01:00 +00001388 // Determine whether we've already produced a tentative equivalence for D1.
1389 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1390 if (EquivToD1)
1391 return EquivToD1 == D2->getCanonicalDecl();
1392
1393 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1394 EquivToD1 = D2->getCanonicalDecl();
1395 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1396 return true;
1397}
1398
1399bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1400 Decl *D2) {
1401 if (!::IsStructurallyEquivalent(*this, D1, D2))
1402 return false;
1403
1404 return !Finish();
1405}
1406
1407bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1408 QualType T2) {
1409 if (!::IsStructurallyEquivalent(*this, T1, T2))
1410 return false;
1411
1412 return !Finish();
1413}
1414
1415bool StructuralEquivalenceContext::Finish() {
1416 while (!DeclsToCheck.empty()) {
1417 // Check the next declaration.
1418 Decl *D1 = DeclsToCheck.front();
1419 DeclsToCheck.pop_front();
1420
1421 Decl *D2 = TentativeEquivalences[D1];
1422 assert(D2 && "Unrecorded tentative equivalence?");
1423
Douglas Gregorb4964f72010-02-15 23:54:17 +00001424 bool Equivalent = true;
1425
Douglas Gregor3996e242010-02-15 22:01:00 +00001426 // FIXME: Switch on all declaration kinds. For now, we're just going to
1427 // check the obvious ones.
1428 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1429 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1430 // Check for equivalent structure names.
1431 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001432 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1433 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001434 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001435 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1436 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001437 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1438 !::IsStructurallyEquivalent(*this, Record1, Record2))
1439 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001440 } else {
1441 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001442 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001443 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001444 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001445 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1446 // Check for equivalent enum names.
1447 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001448 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1449 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001450 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001451 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1452 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001453 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1454 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1455 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001456 } else {
1457 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001458 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001459 }
Richard Smithdda56e42011-04-15 14:24:37 +00001460 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1461 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001462 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001463 Typedef2->getIdentifier()) ||
1464 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001465 Typedef1->getUnderlyingType(),
1466 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001467 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001468 } else {
1469 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001470 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001471 }
Douglas Gregora082a492010-11-30 19:14:50 +00001472 } else if (ClassTemplateDecl *ClassTemplate1
1473 = dyn_cast<ClassTemplateDecl>(D1)) {
1474 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1475 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1476 ClassTemplate2->getIdentifier()) ||
1477 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1478 Equivalent = false;
1479 } else {
1480 // Class template/non-class-template mismatch.
1481 Equivalent = false;
1482 }
1483 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1484 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1485 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1486 Equivalent = false;
1487 } else {
1488 // Kind mismatch.
1489 Equivalent = false;
1490 }
1491 } else if (NonTypeTemplateParmDecl *NTTP1
1492 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1493 if (NonTypeTemplateParmDecl *NTTP2
1494 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1495 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1496 Equivalent = false;
1497 } else {
1498 // Kind mismatch.
1499 Equivalent = false;
1500 }
1501 } else if (TemplateTemplateParmDecl *TTP1
1502 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1503 if (TemplateTemplateParmDecl *TTP2
1504 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1505 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1506 Equivalent = false;
1507 } else {
1508 // Kind mismatch.
1509 Equivalent = false;
1510 }
1511 }
1512
Douglas Gregorb4964f72010-02-15 23:54:17 +00001513 if (!Equivalent) {
1514 // Note that these two declarations are not equivalent (and we already
1515 // know about it).
1516 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1517 D2->getCanonicalDecl()));
1518 return true;
1519 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001520 // FIXME: Check other declaration kinds!
1521 }
1522
1523 return false;
1524}
1525
1526//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001527// Import Types
1528//----------------------------------------------------------------------------
1529
John McCall424cec92011-01-19 06:33:43 +00001530QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +00001531 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1532 << T->getTypeClassName();
1533 return QualType();
1534}
1535
John McCall424cec92011-01-19 06:33:43 +00001536QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001537 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +00001538#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
1539 case BuiltinType::Id: \
1540 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +00001541#include "clang/Basic/OpenCLImageTypes.def"
John McCalle314e272011-10-18 21:02:43 +00001542#define SHARED_SINGLETON_TYPE(Expansion)
1543#define BUILTIN_TYPE(Id, SingletonId) \
1544 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1545#include "clang/AST/BuiltinTypes.def"
1546
1547 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1548 // context supports C++.
1549
1550 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1551 // context supports ObjC.
1552
Douglas Gregor96e578d2010-02-05 17:54:41 +00001553 case BuiltinType::Char_U:
1554 // The context we're importing from has an unsigned 'char'. If we're
1555 // importing into a context with a signed 'char', translate to
1556 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001557 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001558 return Importer.getToContext().UnsignedCharTy;
1559
1560 return Importer.getToContext().CharTy;
1561
Douglas Gregor96e578d2010-02-05 17:54:41 +00001562 case BuiltinType::Char_S:
1563 // The context we're importing from has an unsigned 'char'. If we're
1564 // importing into a context with a signed 'char', translate to
1565 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001566 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001567 return Importer.getToContext().SignedCharTy;
1568
1569 return Importer.getToContext().CharTy;
1570
Chris Lattnerad3467e2010-12-25 23:25:43 +00001571 case BuiltinType::WChar_S:
1572 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001573 // FIXME: If not in C++, shall we translate to the C equivalent of
1574 // wchar_t?
1575 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001576 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001577
1578 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001579}
1580
John McCall424cec92011-01-19 06:33:43 +00001581QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001582 QualType ToElementType = Importer.Import(T->getElementType());
1583 if (ToElementType.isNull())
1584 return QualType();
1585
1586 return Importer.getToContext().getComplexType(ToElementType);
1587}
1588
John McCall424cec92011-01-19 06:33:43 +00001589QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001590 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1591 if (ToPointeeType.isNull())
1592 return QualType();
1593
1594 return Importer.getToContext().getPointerType(ToPointeeType);
1595}
1596
John McCall424cec92011-01-19 06:33:43 +00001597QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001598 // FIXME: Check for blocks support in "to" context.
1599 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1600 if (ToPointeeType.isNull())
1601 return QualType();
1602
1603 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1604}
1605
John McCall424cec92011-01-19 06:33:43 +00001606QualType
1607ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001608 // FIXME: Check for C++ support in "to" context.
1609 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1610 if (ToPointeeType.isNull())
1611 return QualType();
1612
1613 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1614}
1615
John McCall424cec92011-01-19 06:33:43 +00001616QualType
1617ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001618 // FIXME: Check for C++0x support in "to" context.
1619 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1620 if (ToPointeeType.isNull())
1621 return QualType();
1622
1623 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1624}
1625
John McCall424cec92011-01-19 06:33:43 +00001626QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001627 // FIXME: Check for C++ support in "to" context.
1628 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1629 if (ToPointeeType.isNull())
1630 return QualType();
1631
1632 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1633 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1634 ClassType.getTypePtr());
1635}
1636
John McCall424cec92011-01-19 06:33:43 +00001637QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001638 QualType ToElementType = Importer.Import(T->getElementType());
1639 if (ToElementType.isNull())
1640 return QualType();
1641
1642 return Importer.getToContext().getConstantArrayType(ToElementType,
1643 T->getSize(),
1644 T->getSizeModifier(),
1645 T->getIndexTypeCVRQualifiers());
1646}
1647
John McCall424cec92011-01-19 06:33:43 +00001648QualType
1649ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001650 QualType ToElementType = Importer.Import(T->getElementType());
1651 if (ToElementType.isNull())
1652 return QualType();
1653
1654 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1655 T->getSizeModifier(),
1656 T->getIndexTypeCVRQualifiers());
1657}
1658
John McCall424cec92011-01-19 06:33:43 +00001659QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001660 QualType ToElementType = Importer.Import(T->getElementType());
1661 if (ToElementType.isNull())
1662 return QualType();
1663
1664 Expr *Size = Importer.Import(T->getSizeExpr());
1665 if (!Size)
1666 return QualType();
1667
1668 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1669 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1670 T->getSizeModifier(),
1671 T->getIndexTypeCVRQualifiers(),
1672 Brackets);
1673}
1674
John McCall424cec92011-01-19 06:33:43 +00001675QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001676 QualType ToElementType = Importer.Import(T->getElementType());
1677 if (ToElementType.isNull())
1678 return QualType();
1679
1680 return Importer.getToContext().getVectorType(ToElementType,
1681 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001682 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001683}
1684
John McCall424cec92011-01-19 06:33:43 +00001685QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001686 QualType ToElementType = Importer.Import(T->getElementType());
1687 if (ToElementType.isNull())
1688 return QualType();
1689
1690 return Importer.getToContext().getExtVectorType(ToElementType,
1691 T->getNumElements());
1692}
1693
John McCall424cec92011-01-19 06:33:43 +00001694QualType
1695ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001696 // FIXME: What happens if we're importing a function without a prototype
1697 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +00001698 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001699 if (ToResultType.isNull())
1700 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001701
Douglas Gregor96e578d2010-02-05 17:54:41 +00001702 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001703 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001704}
1705
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001706QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +00001707 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001708 if (ToResultType.isNull())
1709 return QualType();
1710
1711 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001712 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00001713 for (const auto &A : T->param_types()) {
1714 QualType ArgType = Importer.Import(A);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001715 if (ArgType.isNull())
1716 return QualType();
1717 ArgTypes.push_back(ArgType);
1718 }
1719
1720 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001721 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +00001722 for (const auto &E : T->exceptions()) {
1723 QualType ExceptionType = Importer.Import(E);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001724 if (ExceptionType.isNull())
1725 return QualType();
1726 ExceptionTypes.push_back(ExceptionType);
1727 }
John McCalldb40c7f2010-12-14 08:05:40 +00001728
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001729 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1730 FunctionProtoType::ExtProtoInfo ToEPI;
1731
1732 ToEPI.ExtInfo = FromEPI.ExtInfo;
1733 ToEPI.Variadic = FromEPI.Variadic;
1734 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1735 ToEPI.TypeQuals = FromEPI.TypeQuals;
1736 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +00001737 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1738 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
1739 ToEPI.ExceptionSpec.NoexceptExpr =
1740 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
1741 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
1742 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
1743 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
1744 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001745
Jordan Rose5c382722013-03-08 21:51:21 +00001746 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001747}
1748
Sean Callananda6df8a2011-08-11 16:56:07 +00001749QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1750 QualType ToInnerType = Importer.Import(T->getInnerType());
1751 if (ToInnerType.isNull())
1752 return QualType();
1753
1754 return Importer.getToContext().getParenType(ToInnerType);
1755}
1756
John McCall424cec92011-01-19 06:33:43 +00001757QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +00001758 TypedefNameDecl *ToDecl
1759 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001760 if (!ToDecl)
1761 return QualType();
1762
1763 return Importer.getToContext().getTypeDeclType(ToDecl);
1764}
1765
John McCall424cec92011-01-19 06:33:43 +00001766QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001767 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1768 if (!ToExpr)
1769 return QualType();
1770
1771 return Importer.getToContext().getTypeOfExprType(ToExpr);
1772}
1773
John McCall424cec92011-01-19 06:33:43 +00001774QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001775 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1776 if (ToUnderlyingType.isNull())
1777 return QualType();
1778
1779 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1780}
1781
John McCall424cec92011-01-19 06:33:43 +00001782QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001783 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +00001784 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1785 if (!ToExpr)
1786 return QualType();
1787
Douglas Gregor81495f32012-02-12 18:42:33 +00001788 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1789 if (UnderlyingType.isNull())
1790 return QualType();
1791
1792 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001793}
1794
Alexis Hunte852b102011-05-24 22:41:36 +00001795QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1796 QualType ToBaseType = Importer.Import(T->getBaseType());
1797 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1798 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1799 return QualType();
1800
1801 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1802 ToUnderlyingType,
1803 T->getUTTKind());
1804}
1805
Richard Smith30482bc2011-02-20 03:19:35 +00001806QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00001807 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +00001808 QualType FromDeduced = T->getDeducedType();
1809 QualType ToDeduced;
1810 if (!FromDeduced.isNull()) {
1811 ToDeduced = Importer.Import(FromDeduced);
1812 if (ToDeduced.isNull())
1813 return QualType();
1814 }
1815
Richard Smithe301ba22015-11-11 02:02:15 +00001816 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +00001817 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +00001818}
1819
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001820QualType ASTNodeImporter::VisitInjectedClassNameType(
1821 const InjectedClassNameType *T) {
1822 CXXRecordDecl *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl()));
1823 if (!D)
1824 return QualType();
1825
1826 QualType InjType = Importer.Import(T->getInjectedSpecializationType());
1827 if (InjType.isNull())
1828 return QualType();
1829
1830 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
1831 // See comments in InjectedClassNameType definition for details
1832 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
1833 enum {
1834 TypeAlignmentInBits = 4,
1835 TypeAlignment = 1 << TypeAlignmentInBits
1836 };
1837
1838 return QualType(new (Importer.getToContext(), TypeAlignment)
1839 InjectedClassNameType(D, InjType), 0);
1840}
1841
John McCall424cec92011-01-19 06:33:43 +00001842QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001843 RecordDecl *ToDecl
1844 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1845 if (!ToDecl)
1846 return QualType();
1847
1848 return Importer.getToContext().getTagDeclType(ToDecl);
1849}
1850
John McCall424cec92011-01-19 06:33:43 +00001851QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001852 EnumDecl *ToDecl
1853 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1854 if (!ToDecl)
1855 return QualType();
1856
1857 return Importer.getToContext().getTagDeclType(ToDecl);
1858}
1859
Sean Callanan72fe0852015-04-02 23:50:08 +00001860QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
1861 QualType FromModifiedType = T->getModifiedType();
1862 QualType FromEquivalentType = T->getEquivalentType();
1863 QualType ToModifiedType;
1864 QualType ToEquivalentType;
1865
1866 if (!FromModifiedType.isNull()) {
1867 ToModifiedType = Importer.Import(FromModifiedType);
1868 if (ToModifiedType.isNull())
1869 return QualType();
1870 }
1871 if (!FromEquivalentType.isNull()) {
1872 ToEquivalentType = Importer.Import(FromEquivalentType);
1873 if (ToEquivalentType.isNull())
1874 return QualType();
1875 }
1876
1877 return Importer.getToContext().getAttributedType(T->getAttrKind(),
1878 ToModifiedType, ToEquivalentType);
1879}
1880
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001881
1882QualType ASTNodeImporter::VisitTemplateTypeParmType(
1883 const TemplateTypeParmType *T) {
1884 TemplateTypeParmDecl *ParmDecl =
1885 cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl()));
1886 if (!ParmDecl && T->getDecl())
1887 return QualType();
1888
1889 return Importer.getToContext().getTemplateTypeParmType(
1890 T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl);
1891}
1892
Douglas Gregore2e50d332010-12-01 01:36:18 +00001893QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001894 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001895 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1896 if (ToTemplate.isNull())
1897 return QualType();
1898
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001899 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001900 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1901 return QualType();
1902
1903 QualType ToCanonType;
1904 if (!QualType(T, 0).isCanonical()) {
1905 QualType FromCanonType
1906 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1907 ToCanonType =Importer.Import(FromCanonType);
1908 if (ToCanonType.isNull())
1909 return QualType();
1910 }
1911 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
David Majnemer6fbeee32016-07-07 04:43:07 +00001912 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +00001913 ToCanonType);
1914}
1915
John McCall424cec92011-01-19 06:33:43 +00001916QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Craig Topper36250ad2014-05-12 05:36:57 +00001917 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001918 // Note: the qualifier in an ElaboratedType is optional.
1919 if (T->getQualifier()) {
1920 ToQualifier = Importer.Import(T->getQualifier());
1921 if (!ToQualifier)
1922 return QualType();
1923 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001924
1925 QualType ToNamedType = Importer.Import(T->getNamedType());
1926 if (ToNamedType.isNull())
1927 return QualType();
1928
Abramo Bagnara6150c882010-05-11 21:36:43 +00001929 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1930 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001931}
1932
John McCall424cec92011-01-19 06:33:43 +00001933QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001934 ObjCInterfaceDecl *Class
1935 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1936 if (!Class)
1937 return QualType();
1938
John McCall8b07ec22010-05-15 11:32:37 +00001939 return Importer.getToContext().getObjCInterfaceType(Class);
1940}
1941
John McCall424cec92011-01-19 06:33:43 +00001942QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001943 QualType ToBaseType = Importer.Import(T->getBaseType());
1944 if (ToBaseType.isNull())
1945 return QualType();
1946
Douglas Gregore9d95f12015-07-07 03:57:35 +00001947 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00001948 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00001949 QualType ImportedTypeArg = Importer.Import(TypeArg);
1950 if (ImportedTypeArg.isNull())
1951 return QualType();
1952
1953 TypeArgs.push_back(ImportedTypeArg);
1954 }
1955
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001956 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001957 for (auto *P : T->quals()) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001958 ObjCProtocolDecl *Protocol
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001959 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001960 if (!Protocol)
1961 return QualType();
1962 Protocols.push_back(Protocol);
1963 }
1964
Douglas Gregore9d95f12015-07-07 03:57:35 +00001965 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001966 Protocols,
1967 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001968}
1969
John McCall424cec92011-01-19 06:33:43 +00001970QualType
1971ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001972 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1973 if (ToPointeeType.isNull())
1974 return QualType();
1975
John McCall8b07ec22010-05-15 11:32:37 +00001976 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001977}
1978
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001979//----------------------------------------------------------------------------
1980// Import Declarations
1981//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001982bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1983 DeclContext *&LexicalDC,
1984 DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +00001985 NamedDecl *&ToD,
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001986 SourceLocation &Loc) {
1987 // Import the context of this declaration.
1988 DC = Importer.ImportContext(D->getDeclContext());
1989 if (!DC)
1990 return true;
1991
1992 LexicalDC = DC;
1993 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1994 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1995 if (!LexicalDC)
1996 return true;
1997 }
1998
1999 // Import the name of this declaration.
2000 Name = Importer.Import(D->getDeclName());
2001 if (D->getDeclName() && !Name)
2002 return true;
2003
2004 // Import the location of this declaration.
2005 Loc = Importer.Import(D->getLocation());
Sean Callanan59721b32015-04-28 18:41:46 +00002006 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002007 return false;
2008}
2009
Douglas Gregord451ea92011-07-29 23:31:30 +00002010void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
2011 if (!FromD)
2012 return;
2013
2014 if (!ToD) {
2015 ToD = Importer.Import(FromD);
2016 if (!ToD)
2017 return;
2018 }
2019
2020 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
2021 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +00002022 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +00002023 ImportDefinition(FromRecord, ToRecord);
2024 }
2025 }
2026 return;
2027 }
2028
2029 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
2030 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
2031 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
2032 ImportDefinition(FromEnum, ToEnum);
2033 }
2034 }
2035 return;
2036 }
2037}
2038
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002039void
2040ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
2041 DeclarationNameInfo& To) {
2042 // NOTE: To.Name and To.Loc are already imported.
2043 // We only have to import To.LocInfo.
2044 switch (To.getName().getNameKind()) {
2045 case DeclarationName::Identifier:
2046 case DeclarationName::ObjCZeroArgSelector:
2047 case DeclarationName::ObjCOneArgSelector:
2048 case DeclarationName::ObjCMultiArgSelector:
2049 case DeclarationName::CXXUsingDirective:
2050 return;
2051
2052 case DeclarationName::CXXOperatorName: {
2053 SourceRange Range = From.getCXXOperatorNameRange();
2054 To.setCXXOperatorNameRange(Importer.Import(Range));
2055 return;
2056 }
2057 case DeclarationName::CXXLiteralOperatorName: {
2058 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
2059 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
2060 return;
2061 }
2062 case DeclarationName::CXXConstructorName:
2063 case DeclarationName::CXXDestructorName:
2064 case DeclarationName::CXXConversionFunctionName: {
2065 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
2066 To.setNamedTypeInfo(Importer.Import(FromTInfo));
2067 return;
2068 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002069 }
Douglas Gregor07216d12011-11-02 20:52:01 +00002070 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002071}
2072
Douglas Gregor2e15c842012-02-01 21:00:38 +00002073void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00002074 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00002075 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00002076 return;
2077 }
2078
Aaron Ballman629afae2014-03-07 19:56:05 +00002079 for (auto *From : FromDC->decls())
2080 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +00002081}
2082
Douglas Gregord451ea92011-07-29 23:31:30 +00002083bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00002084 ImportDefinitionKind Kind) {
2085 if (To->getDefinition() || To->isBeingDefined()) {
2086 if (Kind == IDK_Everything)
2087 ImportDeclContext(From, /*ForceImport=*/true);
2088
Douglas Gregore2e50d332010-12-01 01:36:18 +00002089 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00002090 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00002091
2092 To->startDefinition();
2093
2094 // Add base classes.
2095 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
2096 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002097
2098 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
2099 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
2100 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00002101 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002102 ToData.Aggregate = FromData.Aggregate;
2103 ToData.PlainOldData = FromData.PlainOldData;
2104 ToData.Empty = FromData.Empty;
2105 ToData.Polymorphic = FromData.Polymorphic;
2106 ToData.Abstract = FromData.Abstract;
2107 ToData.IsStandardLayout = FromData.IsStandardLayout;
2108 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
2109 ToData.HasPrivateFields = FromData.HasPrivateFields;
2110 ToData.HasProtectedFields = FromData.HasProtectedFields;
2111 ToData.HasPublicFields = FromData.HasPublicFields;
2112 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00002113 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00002114 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00002115 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00002116 ToData.HasUninitializedReferenceMember
2117 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00002118 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00002119 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
2120 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +00002121 ToData.NeedOverloadResolutionForMoveConstructor
2122 = FromData.NeedOverloadResolutionForMoveConstructor;
2123 ToData.NeedOverloadResolutionForMoveAssignment
2124 = FromData.NeedOverloadResolutionForMoveAssignment;
2125 ToData.NeedOverloadResolutionForDestructor
2126 = FromData.NeedOverloadResolutionForDestructor;
2127 ToData.DefaultedMoveConstructorIsDeleted
2128 = FromData.DefaultedMoveConstructorIsDeleted;
2129 ToData.DefaultedMoveAssignmentIsDeleted
2130 = FromData.DefaultedMoveAssignmentIsDeleted;
2131 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00002132 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
2133 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002134 ToData.HasConstexprNonCopyMoveConstructor
2135 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00002136 ToData.HasDefaultedDefaultConstructor
2137 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00002138 ToData.DefaultedDefaultConstructorIsConstexpr
2139 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00002140 ToData.HasConstexprDefaultConstructor
2141 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002142 ToData.HasNonLiteralTypeFieldsOrBases
2143 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00002144 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002145 ToData.UserProvidedDefaultConstructor
2146 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00002147 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smith1c33fe82012-11-28 06:23:12 +00002148 ToData.ImplicitCopyConstructorHasConstParam
2149 = FromData.ImplicitCopyConstructorHasConstParam;
2150 ToData.ImplicitCopyAssignmentHasConstParam
2151 = FromData.ImplicitCopyAssignmentHasConstParam;
2152 ToData.HasDeclaredCopyConstructorWithConstParam
2153 = FromData.HasDeclaredCopyConstructorWithConstParam;
2154 ToData.HasDeclaredCopyAssignmentWithConstParam
2155 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00002156 ToData.IsLambda = FromData.IsLambda;
2157
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002158 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00002159 for (const auto &Base1 : FromCXX->bases()) {
2160 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +00002161 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00002162 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00002163
2164 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00002165 if (Base1.isPackExpansion())
2166 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00002167
2168 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00002169 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Douglas Gregord451ea92011-07-29 23:31:30 +00002170
Douglas Gregore2e50d332010-12-01 01:36:18 +00002171 Bases.push_back(
2172 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00002173 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
2174 Base1.isVirtual(),
2175 Base1.isBaseOfClass(),
2176 Base1.getAccessSpecifierAsWritten(),
2177 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00002178 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00002179 }
2180 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00002181 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00002182 }
2183
Douglas Gregor2e15c842012-02-01 21:00:38 +00002184 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00002185 ImportDeclContext(From, /*ForceImport=*/true);
2186
Douglas Gregore2e50d332010-12-01 01:36:18 +00002187 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00002188 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002189}
2190
Larisse Voufo39a1e502013-08-06 01:03:05 +00002191bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
2192 ImportDefinitionKind Kind) {
Sean Callanan59721b32015-04-28 18:41:46 +00002193 if (To->getAnyInitializer())
Larisse Voufo39a1e502013-08-06 01:03:05 +00002194 return false;
2195
2196 // FIXME: Can we really import any initializer? Alternatively, we could force
2197 // ourselves to import every declaration of a variable and then only use
2198 // getInit() here.
2199 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
2200
2201 // FIXME: Other bits to merge?
2202
2203 return false;
2204}
2205
Douglas Gregord451ea92011-07-29 23:31:30 +00002206bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00002207 ImportDefinitionKind Kind) {
2208 if (To->getDefinition() || To->isBeingDefined()) {
2209 if (Kind == IDK_Everything)
2210 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002211 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002212 }
Douglas Gregord451ea92011-07-29 23:31:30 +00002213
2214 To->startDefinition();
2215
2216 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
2217 if (T.isNull())
2218 return true;
2219
2220 QualType ToPromotionType = Importer.Import(From->getPromotionType());
2221 if (ToPromotionType.isNull())
2222 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002223
2224 if (shouldForceImportDeclContext(Kind))
2225 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002226
2227 // FIXME: we might need to merge the number of positive or negative bits
2228 // if the enumerator lists don't match.
2229 To->completeDefinition(T, ToPromotionType,
2230 From->getNumPositiveBits(),
2231 From->getNumNegativeBits());
2232 return false;
2233}
2234
Douglas Gregora082a492010-11-30 19:14:50 +00002235TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
2236 TemplateParameterList *Params) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002237 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregora082a492010-11-30 19:14:50 +00002238 ToParams.reserve(Params->size());
2239 for (TemplateParameterList::iterator P = Params->begin(),
2240 PEnd = Params->end();
2241 P != PEnd; ++P) {
2242 Decl *To = Importer.Import(*P);
2243 if (!To)
Craig Topper36250ad2014-05-12 05:36:57 +00002244 return nullptr;
2245
Douglas Gregora082a492010-11-30 19:14:50 +00002246 ToParams.push_back(cast<NamedDecl>(To));
2247 }
2248
2249 return TemplateParameterList::Create(Importer.getToContext(),
2250 Importer.Import(Params->getTemplateLoc()),
2251 Importer.Import(Params->getLAngleLoc()),
David Majnemer902f8c62015-12-27 07:16:27 +00002252 ToParams,
Douglas Gregora082a492010-11-30 19:14:50 +00002253 Importer.Import(Params->getRAngleLoc()));
2254}
2255
Douglas Gregore2e50d332010-12-01 01:36:18 +00002256TemplateArgument
2257ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
2258 switch (From.getKind()) {
2259 case TemplateArgument::Null:
2260 return TemplateArgument();
2261
2262 case TemplateArgument::Type: {
2263 QualType ToType = Importer.Import(From.getAsType());
2264 if (ToType.isNull())
2265 return TemplateArgument();
2266 return TemplateArgument(ToType);
2267 }
2268
2269 case TemplateArgument::Integral: {
2270 QualType ToType = Importer.Import(From.getIntegralType());
2271 if (ToType.isNull())
2272 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00002273 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00002274 }
2275
Eli Friedmanb826a002012-09-26 02:36:12 +00002276 case TemplateArgument::Declaration: {
David Blaikie3c7dd6b2014-10-22 19:54:16 +00002277 ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
2278 QualType ToType = Importer.Import(From.getParamTypeForDecl());
2279 if (!To || ToType.isNull())
2280 return TemplateArgument();
2281 return TemplateArgument(To, ToType);
Eli Friedmanb826a002012-09-26 02:36:12 +00002282 }
2283
2284 case TemplateArgument::NullPtr: {
2285 QualType ToType = Importer.Import(From.getNullPtrType());
2286 if (ToType.isNull())
2287 return TemplateArgument();
2288 return TemplateArgument(ToType, /*isNullPtr*/true);
2289 }
2290
Douglas Gregore2e50d332010-12-01 01:36:18 +00002291 case TemplateArgument::Template: {
2292 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
2293 if (ToTemplate.isNull())
2294 return TemplateArgument();
2295
2296 return TemplateArgument(ToTemplate);
2297 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002298
2299 case TemplateArgument::TemplateExpansion: {
2300 TemplateName ToTemplate
2301 = Importer.Import(From.getAsTemplateOrTemplatePattern());
2302 if (ToTemplate.isNull())
2303 return TemplateArgument();
2304
Douglas Gregore1d60df2011-01-14 23:41:42 +00002305 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002306 }
2307
Douglas Gregore2e50d332010-12-01 01:36:18 +00002308 case TemplateArgument::Expression:
2309 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2310 return TemplateArgument(ToExpr);
2311 return TemplateArgument();
2312
2313 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002314 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002315 ToPack.reserve(From.pack_size());
2316 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2317 return TemplateArgument();
Benjamin Kramercce63472015-08-05 09:40:22 +00002318
2319 return TemplateArgument(
2320 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00002321 }
2322 }
2323
2324 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00002325}
2326
2327bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2328 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002329 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00002330 for (unsigned I = 0; I != NumFromArgs; ++I) {
2331 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2332 if (To.isNull() && !FromArgs[I].isNull())
2333 return true;
2334
2335 ToArgs.push_back(To);
2336 }
2337
2338 return false;
2339}
2340
Douglas Gregor5c73e912010-02-11 00:48:18 +00002341bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002342 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00002343 // Eliminate a potential failure point where we attempt to re-import
2344 // something we're trying to import while completing ToRecord.
2345 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
2346 if (ToOrigin) {
2347 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
2348 if (ToOriginRecord)
2349 ToRecord = ToOriginRecord;
2350 }
2351
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002352 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00002353 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002354 Importer.getNonEquivalentDecls(),
2355 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002356 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002357}
2358
Larisse Voufo39a1e502013-08-06 01:03:05 +00002359bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
2360 bool Complain) {
2361 StructuralEquivalenceContext Ctx(
2362 Importer.getFromContext(), Importer.getToContext(),
2363 Importer.getNonEquivalentDecls(), false, Complain);
2364 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
2365}
2366
Douglas Gregor98c10182010-02-12 22:17:39 +00002367bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002368 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002369 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002370 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002371 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002372}
2373
Douglas Gregor91155082012-11-14 22:29:20 +00002374bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
2375 EnumConstantDecl *ToEC)
2376{
2377 const llvm::APSInt &FromVal = FromEC->getInitVal();
2378 const llvm::APSInt &ToVal = ToEC->getInitVal();
2379
2380 return FromVal.isSigned() == ToVal.isSigned() &&
2381 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2382 FromVal == ToVal;
2383}
2384
2385bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002386 ClassTemplateDecl *To) {
2387 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2388 Importer.getToContext(),
2389 Importer.getNonEquivalentDecls());
2390 return Ctx.IsStructurallyEquivalent(From, To);
2391}
2392
Larisse Voufo39a1e502013-08-06 01:03:05 +00002393bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2394 VarTemplateDecl *To) {
2395 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2396 Importer.getToContext(),
2397 Importer.getNonEquivalentDecls());
2398 return Ctx.IsStructurallyEquivalent(From, To);
2399}
2400
Douglas Gregore4c83e42010-02-09 22:48:33 +00002401Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002402 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002403 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00002404 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00002405}
2406
Sean Callanan65198272011-11-17 23:20:56 +00002407Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2408 TranslationUnitDecl *ToD =
2409 Importer.getToContext().getTranslationUnitDecl();
2410
2411 Importer.Imported(D, ToD);
2412
2413 return ToD;
2414}
2415
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002416Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
2417
2418 SourceLocation Loc = Importer.Import(D->getLocation());
2419 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
2420
2421 // Import the context of this declaration.
2422 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2423 if (!DC)
2424 return nullptr;
2425
2426 AccessSpecDecl *accessSpecDecl
2427 = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(),
2428 DC, Loc, ColonLoc);
2429
2430 if (!accessSpecDecl)
2431 return nullptr;
2432
2433 // Lexical DeclContext and Semantic DeclContext
2434 // is always the same for the accessSpec.
2435 accessSpecDecl->setLexicalDeclContext(DC);
2436 DC->addDeclInternal(accessSpecDecl);
2437
2438 return accessSpecDecl;
2439}
2440
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002441Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2442 // Import the major distinguishing characteristics of this namespace.
2443 DeclContext *DC, *LexicalDC;
2444 DeclarationName Name;
2445 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002446 NamedDecl *ToD;
2447 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002448 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002449 if (ToD)
2450 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002451
2452 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002453 if (!Name) {
2454 // This is an anonymous namespace. Adopt an existing anonymous
2455 // namespace if we can.
2456 // FIXME: Not testable.
2457 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2458 MergeWithNamespace = TU->getAnonymousNamespace();
2459 else
2460 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2461 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002462 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002463 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002464 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002465 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2466 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002467 continue;
2468
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002469 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002470 MergeWithNamespace = FoundNS;
2471 ConflictingDecls.clear();
2472 break;
2473 }
2474
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002475 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002476 }
2477
2478 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002479 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002480 ConflictingDecls.data(),
2481 ConflictingDecls.size());
2482 }
2483 }
2484
2485 // Create the "to" namespace, if needed.
2486 NamespaceDecl *ToNamespace = MergeWithNamespace;
2487 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002488 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002489 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002490 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00002491 Loc, Name.getAsIdentifierInfo(),
Craig Topper36250ad2014-05-12 05:36:57 +00002492 /*PrevDecl=*/nullptr);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002493 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002494 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002495
2496 // If this is an anonymous namespace, register it as the anonymous
2497 // namespace within its context.
2498 if (!Name) {
2499 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2500 TU->setAnonymousNamespace(ToNamespace);
2501 else
2502 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2503 }
2504 }
2505 Importer.Imported(D, ToNamespace);
2506
2507 ImportDeclContext(D);
2508
2509 return ToNamespace;
2510}
2511
Richard Smithdda56e42011-04-15 14:24:37 +00002512Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002513 // Import the major distinguishing characteristics of this typedef.
2514 DeclContext *DC, *LexicalDC;
2515 DeclarationName Name;
2516 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002517 NamedDecl *ToD;
2518 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002519 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002520 if (ToD)
2521 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002522
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002523 // If this typedef is not in block scope, determine whether we've
2524 // seen a typedef with the same name (that we can merge with) or any
2525 // other entity by that name (which name lookup could conflict with).
2526 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002527 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002528 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002529 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002530 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002531 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2532 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002533 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00002534 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002535 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002536 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2537 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002538 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002539 }
2540
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002541 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002542 }
2543
2544 if (!ConflictingDecls.empty()) {
2545 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2546 ConflictingDecls.data(),
2547 ConflictingDecls.size());
2548 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002549 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002550 }
2551 }
2552
Douglas Gregorb4964f72010-02-15 23:54:17 +00002553 // Import the underlying type of this typedef;
2554 QualType T = Importer.Import(D->getUnderlyingType());
2555 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002556 return nullptr;
2557
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002558 // Create the new typedef node.
2559 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002560 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00002561 TypedefNameDecl *ToTypedef;
2562 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002563 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2564 StartL, Loc,
2565 Name.getAsIdentifierInfo(),
2566 TInfo);
2567 else
Richard Smithdda56e42011-04-15 14:24:37 +00002568 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2569 StartL, Loc,
2570 Name.getAsIdentifierInfo(),
2571 TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002572
Douglas Gregordd483172010-02-22 17:42:47 +00002573 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002574 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002575 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00002576 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002577
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002578 return ToTypedef;
2579}
2580
Richard Smithdda56e42011-04-15 14:24:37 +00002581Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2582 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2583}
2584
2585Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2586 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2587}
2588
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002589Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
2590 // Import the major distinguishing characteristics of this label.
2591 DeclContext *DC, *LexicalDC;
2592 DeclarationName Name;
2593 SourceLocation Loc;
2594 NamedDecl *ToD;
2595 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2596 return nullptr;
2597 if (ToD)
2598 return ToD;
2599
2600 assert(LexicalDC->isFunctionOrMethod());
2601
2602 LabelDecl *ToLabel = D->isGnuLocal()
2603 ? LabelDecl::Create(Importer.getToContext(),
2604 DC, Importer.Import(D->getLocation()),
2605 Name.getAsIdentifierInfo(),
2606 Importer.Import(D->getLocStart()))
2607 : LabelDecl::Create(Importer.getToContext(),
2608 DC, Importer.Import(D->getLocation()),
2609 Name.getAsIdentifierInfo());
2610 Importer.Imported(D, ToLabel);
2611
2612 LabelStmt *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt()));
2613 if (!Label)
2614 return nullptr;
2615
2616 ToLabel->setStmt(Label);
2617 ToLabel->setLexicalDeclContext(LexicalDC);
2618 LexicalDC->addDeclInternal(ToLabel);
2619 return ToLabel;
2620}
2621
Douglas Gregor98c10182010-02-12 22:17:39 +00002622Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2623 // Import the major distinguishing characteristics of this enum.
2624 DeclContext *DC, *LexicalDC;
2625 DeclarationName Name;
2626 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002627 NamedDecl *ToD;
2628 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002629 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002630 if (ToD)
2631 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002632
Douglas Gregor98c10182010-02-12 22:17:39 +00002633 // Figure out what enum name we're looking for.
2634 unsigned IDNS = Decl::IDNS_Tag;
2635 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002636 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2637 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002638 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002639 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002640 IDNS |= Decl::IDNS_Ordinary;
2641
2642 // We may already have an enum of the same name; try to find and match it.
2643 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002644 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002645 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002646 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002647 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2648 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002649 continue;
2650
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002651 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002652 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002653 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2654 Found = Tag->getDecl();
2655 }
2656
2657 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002658 if (IsStructuralMatch(D, FoundEnum))
2659 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002660 }
2661
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002662 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002663 }
2664
2665 if (!ConflictingDecls.empty()) {
2666 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2667 ConflictingDecls.data(),
2668 ConflictingDecls.size());
2669 }
2670 }
2671
2672 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002673 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2674 Importer.Import(D->getLocStart()),
Craig Topper36250ad2014-05-12 05:36:57 +00002675 Loc, Name.getAsIdentifierInfo(), nullptr,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002676 D->isScoped(), D->isScopedUsingClassTag(),
2677 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002678 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002679 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002680 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002681 D2->setLexicalDeclContext(LexicalDC);
2682 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00002683 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002684
2685 // Import the integer type.
2686 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2687 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002688 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00002689 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002690
2691 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002692 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00002693 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002694
Douglas Gregor3996e242010-02-15 22:01:00 +00002695 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002696}
2697
Douglas Gregor5c73e912010-02-11 00:48:18 +00002698Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2699 // If this record has a definition in the translation unit we're coming from,
2700 // but this particular declaration is not that definition, import the
2701 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002702 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002703 if (Definition && Definition != D) {
2704 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002705 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00002706 return nullptr;
2707
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002708 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002709 }
2710
2711 // Import the major distinguishing characteristics of this record.
2712 DeclContext *DC, *LexicalDC;
2713 DeclarationName Name;
2714 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002715 NamedDecl *ToD;
2716 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002717 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002718 if (ToD)
2719 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002720
Douglas Gregor5c73e912010-02-11 00:48:18 +00002721 // Figure out what structure name we're looking for.
2722 unsigned IDNS = Decl::IDNS_Tag;
2723 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002724 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2725 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002726 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002727 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002728 IDNS |= Decl::IDNS_Ordinary;
2729
2730 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00002731 RecordDecl *AdoptDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002732 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002733 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002734 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002735 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002736 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2737 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002738 continue;
2739
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002740 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002741 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002742 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2743 Found = Tag->getDecl();
2744 }
2745
2746 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002747 if (D->isAnonymousStructOrUnion() &&
2748 FoundRecord->isAnonymousStructOrUnion()) {
2749 // If both anonymous structs/unions are in a record context, make sure
2750 // they occur in the same location in the context records.
David Blaikie05785d12013-02-20 22:23:23 +00002751 if (Optional<unsigned> Index1
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002752 = findAnonymousStructOrUnionIndex(D)) {
David Blaikie05785d12013-02-20 22:23:23 +00002753 if (Optional<unsigned> Index2 =
2754 findAnonymousStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002755 if (*Index1 != *Index2)
2756 continue;
2757 }
2758 }
2759 }
2760
Douglas Gregor25791052010-02-12 00:09:27 +00002761 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002762 if ((SearchName && !D->isCompleteDefinition())
2763 || (D->isCompleteDefinition() &&
2764 D->isAnonymousStructOrUnion()
2765 == FoundDef->isAnonymousStructOrUnion() &&
2766 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002767 // The record types structurally match, or the "from" translation
2768 // unit only had a forward declaration anyway; call it the same
2769 // function.
2770 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002771 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002772 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002773 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002774 // We have a forward declaration of this type, so adopt that forward
2775 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00002776
2777 // If one or both can be completed from external storage then try one
2778 // last time to complete and compare them before doing this.
2779
2780 if (FoundRecord->hasExternalLexicalStorage() &&
2781 !FoundRecord->isCompleteDefinition())
2782 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2783 if (D->hasExternalLexicalStorage())
2784 D->getASTContext().getExternalSource()->CompleteType(D);
2785
2786 if (FoundRecord->isCompleteDefinition() &&
2787 D->isCompleteDefinition() &&
2788 !IsStructuralMatch(D, FoundRecord))
2789 continue;
2790
Douglas Gregor25791052010-02-12 00:09:27 +00002791 AdoptDecl = FoundRecord;
2792 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002793 } else if (!SearchName) {
2794 continue;
2795 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002796 }
2797
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002798 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002799 }
2800
Douglas Gregordd6006f2012-07-17 21:16:27 +00002801 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002802 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2803 ConflictingDecls.data(),
2804 ConflictingDecls.size());
2805 }
2806 }
2807
2808 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002809 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002810 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002811 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002812 CXXRecordDecl *D2CXX = nullptr;
2813 if (CXXRecordDecl *DCXX = llvm::dyn_cast<CXXRecordDecl>(D)) {
2814 if (DCXX->isLambda()) {
2815 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
2816 D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(),
2817 DC, TInfo, Loc,
2818 DCXX->isDependentLambda(),
2819 DCXX->isGenericLambda(),
2820 DCXX->getLambdaCaptureDefault());
2821 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
2822 if (DCXX->getLambdaContextDecl() && !CDecl)
2823 return nullptr;
Sean Callanan041cceb2016-05-14 05:43:57 +00002824 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl);
2825 } else if (DCXX->isInjectedClassName()) {
2826 // We have to be careful to do a similar dance to the one in
2827 // Sema::ActOnStartCXXMemberDeclarations
2828 CXXRecordDecl *const PrevDecl = nullptr;
2829 const bool DelayTypeCreation = true;
2830 D2CXX = CXXRecordDecl::Create(
2831 Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc,
2832 Name.getAsIdentifierInfo(), PrevDecl, DelayTypeCreation);
2833 Importer.getToContext().getTypeDeclType(
2834 D2CXX, llvm::dyn_cast<CXXRecordDecl>(DC));
Sean Callanan8bca9962016-03-28 21:43:01 +00002835 } else {
2836 D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
2837 D->getTagKind(),
2838 DC, StartLoc, Loc,
2839 Name.getAsIdentifierInfo());
2840 }
Douglas Gregor3996e242010-02-15 22:01:00 +00002841 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002842 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002843 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002844 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002845 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002846 }
Douglas Gregor14454802011-02-25 02:25:35 +00002847
2848 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002849 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002850 LexicalDC->addDeclInternal(D2);
Douglas Gregordd6006f2012-07-17 21:16:27 +00002851 if (D->isAnonymousStructOrUnion())
2852 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002853 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002854
Douglas Gregor3996e242010-02-15 22:01:00 +00002855 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002856
Douglas Gregor95d82832012-01-24 18:36:04 +00002857 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00002858 return nullptr;
2859
Douglas Gregor3996e242010-02-15 22:01:00 +00002860 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002861}
2862
Douglas Gregor98c10182010-02-12 22:17:39 +00002863Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2864 // Import the major distinguishing characteristics of this enumerator.
2865 DeclContext *DC, *LexicalDC;
2866 DeclarationName Name;
2867 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002868 NamedDecl *ToD;
2869 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002870 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002871 if (ToD)
2872 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002873
2874 QualType T = Importer.Import(D->getType());
2875 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002876 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002877
Douglas Gregor98c10182010-02-12 22:17:39 +00002878 // Determine whether there are any other declarations with the same name and
2879 // in the same context.
2880 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002881 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002882 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002883 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002884 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002885 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2886 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002887 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002888
2889 if (EnumConstantDecl *FoundEnumConstant
2890 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
2891 if (IsStructuralMatch(D, FoundEnumConstant))
2892 return Importer.Imported(D, FoundEnumConstant);
2893 }
2894
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002895 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002896 }
2897
2898 if (!ConflictingDecls.empty()) {
2899 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2900 ConflictingDecls.data(),
2901 ConflictingDecls.size());
2902 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002903 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002904 }
2905 }
2906
2907 Expr *Init = Importer.Import(D->getInitExpr());
2908 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00002909 return nullptr;
2910
Douglas Gregor98c10182010-02-12 22:17:39 +00002911 EnumConstantDecl *ToEnumerator
2912 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2913 Name.getAsIdentifierInfo(), T,
2914 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002915 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002916 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002917 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002918 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002919 return ToEnumerator;
2920}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002921
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002922Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2923 // Import the major distinguishing characteristics of this function.
2924 DeclContext *DC, *LexicalDC;
2925 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002926 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002927 NamedDecl *ToD;
2928 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002929 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002930 if (ToD)
2931 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002932
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002933 // Try to find a function in our own ("to") context with the same name, same
2934 // type, and in the same context as the function we're importing.
2935 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002936 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002937 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002938 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002939 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002940 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2941 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002942 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002943
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002944 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002945 if (FoundFunction->hasExternalFormalLinkage() &&
2946 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002947 if (Importer.IsStructurallyEquivalent(D->getType(),
2948 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002949 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002950 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002951 }
2952
2953 // FIXME: Check for overloading more carefully, e.g., by boosting
2954 // Sema::IsOverload out to the AST library.
2955
2956 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002957 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002958 continue;
2959
2960 // Complain about inconsistent function types.
2961 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002962 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002963 Importer.ToDiag(FoundFunction->getLocation(),
2964 diag::note_odr_value_here)
2965 << FoundFunction->getType();
2966 }
2967 }
2968
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002969 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002970 }
2971
2972 if (!ConflictingDecls.empty()) {
2973 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2974 ConflictingDecls.data(),
2975 ConflictingDecls.size());
2976 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002977 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002978 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002979 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002980
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002981 DeclarationNameInfo NameInfo(Name, Loc);
2982 // Import additional name location/type info.
2983 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2984
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002985 QualType FromTy = D->getType();
2986 bool usedDifferentExceptionSpec = false;
2987
2988 if (const FunctionProtoType *
2989 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2990 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2991 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2992 // FunctionDecl that we are importing the FunctionProtoType for.
2993 // To avoid an infinite recursion when importing, create the FunctionDecl
2994 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00002995 if (FromEPI.ExceptionSpec.SourceDecl ||
2996 FromEPI.ExceptionSpec.SourceTemplate ||
2997 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002998 FunctionProtoType::ExtProtoInfo DefaultEPI;
2999 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00003000 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003001 usedDifferentExceptionSpec = true;
3002 }
3003 }
3004
Douglas Gregorb4964f72010-02-15 23:54:17 +00003005 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003006 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00003007 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003008 return nullptr;
3009
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003010 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003011 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00003012 for (auto P : D->parameters()) {
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00003013 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003014 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003015 return nullptr;
3016
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003017 Parameters.push_back(ToP);
3018 }
3019
3020 // Create the imported function.
3021 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Craig Topper36250ad2014-05-12 05:36:57 +00003022 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003023 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Douglas Gregor00eace12010-02-21 18:29:16 +00003024 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
3025 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
3026 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00003027 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003028 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00003029 FromConstructor->isExplicit(),
3030 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00003031 D->isImplicit(),
3032 D->isConstexpr());
Sean Callanandd2c1742016-05-16 20:48:03 +00003033 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
3034 SmallVector<CXXCtorInitializer *, 4> CtorInitializers;
3035 for (CXXCtorInitializer *I : FromConstructor->inits()) {
3036 CXXCtorInitializer *ToI =
3037 cast_or_null<CXXCtorInitializer>(Importer.Import(I));
3038 if (!ToI && I)
3039 return nullptr;
3040 CtorInitializers.push_back(ToI);
3041 }
3042 CXXCtorInitializer **Memory =
3043 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
3044 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
3045 CXXConstructorDecl *ToCtor = llvm::cast<CXXConstructorDecl>(ToFunction);
3046 ToCtor->setCtorInitializers(Memory);
3047 ToCtor->setNumCtorInitializers(NumInitializers);
3048 }
Douglas Gregor00eace12010-02-21 18:29:16 +00003049 } else if (isa<CXXDestructorDecl>(D)) {
3050 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
3051 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00003052 InnerLocStart,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00003053 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00003054 D->isInlineSpecified(),
3055 D->isImplicit());
3056 } else if (CXXConversionDecl *FromConversion
3057 = dyn_cast<CXXConversionDecl>(D)) {
3058 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
3059 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00003060 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003061 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00003062 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00003063 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00003064 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00003065 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00003066 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
3067 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
3068 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00003069 InnerLocStart,
Douglas Gregora50ad132010-11-29 16:04:58 +00003070 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003071 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00003072 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00003073 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00003074 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00003075 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003076 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Sean Callanan59721b32015-04-28 18:41:46 +00003077 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003078 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00003079 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00003080 D->hasWrittenPrototype(),
3081 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00003082 }
John McCall3e11ebe2010-03-15 10:12:16 +00003083
3084 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00003085 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003086 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00003087 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00003088 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
3089 ToFunction->setTrivial(D->isTrivial());
3090 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00003091 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003092
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003093 // Set the parameters.
3094 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003095 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00003096 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003097 }
David Blaikie9c70e042011-09-21 18:16:56 +00003098 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003099
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003100 if (usedDifferentExceptionSpec) {
3101 // Update FunctionProtoType::ExtProtoInfo.
3102 QualType T = Importer.Import(D->getType());
3103 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003104 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003105 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00003106 }
3107
Sean Callanan59721b32015-04-28 18:41:46 +00003108 // Import the body, if any.
3109 if (Stmt *FromBody = D->getBody()) {
3110 if (Stmt *ToBody = Importer.Import(FromBody)) {
3111 ToFunction->setBody(ToBody);
3112 }
3113 }
3114
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003115 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003116
3117 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00003118 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003119
Douglas Gregor43f54792010-02-17 02:12:47 +00003120 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003121}
3122
Douglas Gregor00eace12010-02-21 18:29:16 +00003123Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
3124 return VisitFunctionDecl(D);
3125}
3126
3127Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
3128 return VisitCXXMethodDecl(D);
3129}
3130
3131Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
3132 return VisitCXXMethodDecl(D);
3133}
3134
3135Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
3136 return VisitCXXMethodDecl(D);
3137}
3138
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003139static unsigned getFieldIndex(Decl *F) {
3140 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
3141 if (!Owner)
3142 return 0;
3143
3144 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00003145 for (const auto *D : Owner->noload_decls()) {
3146 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003147 return Index;
3148
3149 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
3150 ++Index;
3151 }
3152
3153 return Index;
3154}
3155
Douglas Gregor5c73e912010-02-11 00:48:18 +00003156Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
3157 // Import the major distinguishing characteristics of a variable.
3158 DeclContext *DC, *LexicalDC;
3159 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00003160 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003161 NamedDecl *ToD;
3162 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003163 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003164 if (ToD)
3165 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003166
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003167 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003168 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003169 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003170 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3171 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003172 // For anonymous fields, match up by index.
3173 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
3174 continue;
3175
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003176 if (Importer.IsStructurallyEquivalent(D->getType(),
3177 FoundField->getType())) {
3178 Importer.Imported(D, FoundField);
3179 return FoundField;
3180 }
3181
3182 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3183 << Name << D->getType() << FoundField->getType();
3184 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3185 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003186 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003187 }
3188 }
3189
Douglas Gregorb4964f72010-02-15 23:54:17 +00003190 // Import the type.
3191 QualType T = Importer.Import(D->getType());
3192 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003193 return nullptr;
3194
Douglas Gregor5c73e912010-02-11 00:48:18 +00003195 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3196 Expr *BitWidth = Importer.Import(D->getBitWidth());
3197 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00003198 return nullptr;
3199
Abramo Bagnaradff19302011-03-08 08:55:46 +00003200 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
3201 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00003202 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00003203 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00003204 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00003205 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003206 ToField->setLexicalDeclContext(LexicalDC);
Sean Callanan3a83ea72016-03-03 02:22:05 +00003207 if (Expr *FromInitializer = D->getInClassInitializer()) {
Sean Callananbb33f582016-03-03 01:21:28 +00003208 Expr *ToInitializer = Importer.Import(FromInitializer);
3209 if (ToInitializer)
3210 ToField->setInClassInitializer(ToInitializer);
3211 else
3212 return nullptr;
3213 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003214 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003215 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00003216 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003217 return ToField;
3218}
3219
Francois Pichet783dd6e2010-11-21 06:08:52 +00003220Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
3221 // Import the major distinguishing characteristics of a variable.
3222 DeclContext *DC, *LexicalDC;
3223 DeclarationName Name;
3224 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003225 NamedDecl *ToD;
3226 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003227 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003228 if (ToD)
3229 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003230
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003231 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003232 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003233 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003234 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003235 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003236 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003237 // For anonymous indirect fields, match up by index.
3238 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
3239 continue;
3240
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003241 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003242 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003243 !Name.isEmpty())) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003244 Importer.Imported(D, FoundField);
3245 return FoundField;
3246 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003247
3248 // If there are more anonymous fields to check, continue.
3249 if (!Name && I < N-1)
3250 continue;
3251
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003252 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3253 << Name << D->getType() << FoundField->getType();
3254 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3255 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003256 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003257 }
3258 }
3259
Francois Pichet783dd6e2010-11-21 06:08:52 +00003260 // Import the type.
3261 QualType T = Importer.Import(D->getType());
3262 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003263 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003264
3265 NamedDecl **NamedChain =
3266 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
3267
3268 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00003269 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00003270 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003271 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00003272 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003273 NamedChain[i++] = cast<NamedDecl>(D);
3274 }
3275
3276 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
Aaron Ballman260995b2014-10-15 16:58:18 +00003277 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
David Majnemer59f77922016-06-24 04:05:48 +00003278 {NamedChain, D->getChainingSize()});
Aaron Ballman260995b2014-10-15 16:58:18 +00003279
3280 for (const auto *Attr : D->attrs())
3281 ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
3282
Francois Pichet783dd6e2010-11-21 06:08:52 +00003283 ToIndirectField->setAccess(D->getAccess());
3284 ToIndirectField->setLexicalDeclContext(LexicalDC);
3285 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00003286 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003287 return ToIndirectField;
3288}
3289
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003290Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
3291 // Import the major distinguishing characteristics of an ivar.
3292 DeclContext *DC, *LexicalDC;
3293 DeclarationName Name;
3294 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003295 NamedDecl *ToD;
3296 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003297 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003298 if (ToD)
3299 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003300
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003301 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003302 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003303 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003304 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3305 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003306 if (Importer.IsStructurallyEquivalent(D->getType(),
3307 FoundIvar->getType())) {
3308 Importer.Imported(D, FoundIvar);
3309 return FoundIvar;
3310 }
3311
3312 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
3313 << Name << D->getType() << FoundIvar->getType();
3314 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3315 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003316 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003317 }
3318 }
3319
3320 // Import the type.
3321 QualType T = Importer.Import(D->getType());
3322 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003323 return nullptr;
3324
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003325 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3326 Expr *BitWidth = Importer.Import(D->getBitWidth());
3327 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00003328 return nullptr;
3329
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00003330 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
3331 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003332 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003333 Loc, Name.getAsIdentifierInfo(),
3334 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003335 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003336 ToIvar->setLexicalDeclContext(LexicalDC);
3337 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003338 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003339 return ToIvar;
3340
3341}
3342
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003343Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
3344 // Import the major distinguishing characteristics of a variable.
3345 DeclContext *DC, *LexicalDC;
3346 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003347 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003348 NamedDecl *ToD;
3349 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003350 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003351 if (ToD)
3352 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003353
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003354 // Try to find a variable in our own ("to") context with the same name and
3355 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00003356 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00003357 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003358 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003359 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003360 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003361 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003362 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3363 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003364 continue;
3365
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003366 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003367 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00003368 if (FoundVar->hasExternalFormalLinkage() &&
3369 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003370 if (Importer.IsStructurallyEquivalent(D->getType(),
3371 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003372 MergeWithVar = FoundVar;
3373 break;
3374 }
3375
Douglas Gregor56521c52010-02-12 17:23:39 +00003376 const ArrayType *FoundArray
3377 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3378 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00003379 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00003380 if (FoundArray && TArray) {
3381 if (isa<IncompleteArrayType>(FoundArray) &&
3382 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003383 // Import the type.
3384 QualType T = Importer.Import(D->getType());
3385 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003386 return nullptr;
3387
Douglas Gregor56521c52010-02-12 17:23:39 +00003388 FoundVar->setType(T);
3389 MergeWithVar = FoundVar;
3390 break;
3391 } else if (isa<IncompleteArrayType>(TArray) &&
3392 isa<ConstantArrayType>(FoundArray)) {
3393 MergeWithVar = FoundVar;
3394 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003395 }
3396 }
3397
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003398 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003399 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003400 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3401 << FoundVar->getType();
3402 }
3403 }
3404
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003405 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003406 }
3407
3408 if (MergeWithVar) {
3409 // An equivalent variable with external linkage has been found. Link
3410 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003411 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003412
3413 if (VarDecl *DDef = D->getDefinition()) {
3414 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
3415 Importer.ToDiag(ExistingDef->getLocation(),
3416 diag::err_odr_variable_multiple_def)
3417 << Name;
3418 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
3419 } else {
3420 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00003421 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00003422 if (DDef->isInitKnownICE()) {
3423 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
3424 Eval->CheckedICE = true;
3425 Eval->IsICE = DDef->isInitICE();
3426 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003427 }
3428 }
3429
3430 return MergeWithVar;
3431 }
3432
3433 if (!ConflictingDecls.empty()) {
3434 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3435 ConflictingDecls.data(),
3436 ConflictingDecls.size());
3437 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003438 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003439 }
3440 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003441
Douglas Gregorb4964f72010-02-15 23:54:17 +00003442 // Import the type.
3443 QualType T = Importer.Import(D->getType());
3444 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003445 return nullptr;
3446
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003447 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003448 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00003449 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
3450 Importer.Import(D->getInnerLocStart()),
3451 Loc, Name.getAsIdentifierInfo(),
3452 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003453 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00003454 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003455 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003456 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003457 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003458 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003459
Sean Callanan59721b32015-04-28 18:41:46 +00003460 if (!D->isFileVarDecl() &&
3461 D->isUsed())
3462 ToVar->setIsUsed();
3463
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003464 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00003465 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00003466 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003467
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003468 return ToVar;
3469}
3470
Douglas Gregor8b228d72010-02-17 21:22:52 +00003471Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
3472 // Parameters are created in the translation unit's context, then moved
3473 // into the function declaration's context afterward.
3474 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3475
3476 // Import the name of this declaration.
3477 DeclarationName Name = Importer.Import(D->getDeclName());
3478 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003479 return nullptr;
3480
Douglas Gregor8b228d72010-02-17 21:22:52 +00003481 // Import the location of this declaration.
3482 SourceLocation Loc = Importer.Import(D->getLocation());
3483
3484 // Import the parameter's type.
3485 QualType T = Importer.Import(D->getType());
3486 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003487 return nullptr;
3488
Douglas Gregor8b228d72010-02-17 21:22:52 +00003489 // Create the imported parameter.
3490 ImplicitParamDecl *ToParm
3491 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
3492 Loc, Name.getAsIdentifierInfo(),
3493 T);
3494 return Importer.Imported(D, ToParm);
3495}
3496
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003497Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
3498 // Parameters are created in the translation unit's context, then moved
3499 // into the function declaration's context afterward.
3500 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3501
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003502 // Import the name of this declaration.
3503 DeclarationName Name = Importer.Import(D->getDeclName());
3504 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003505 return nullptr;
3506
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003507 // Import the location of this declaration.
3508 SourceLocation Loc = Importer.Import(D->getLocation());
3509
3510 // Import the parameter's type.
3511 QualType T = Importer.Import(D->getType());
3512 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003513 return nullptr;
3514
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003515 // Create the imported parameter.
3516 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3517 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003518 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003519 Loc, Name.getAsIdentifierInfo(),
3520 T, TInfo, D->getStorageClass(),
Craig Topper36250ad2014-05-12 05:36:57 +00003521 /*FIXME: Default argument*/nullptr);
John McCallf3cd6652010-03-12 18:31:32 +00003522 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Sean Callanan59721b32015-04-28 18:41:46 +00003523
3524 if (D->isUsed())
3525 ToParm->setIsUsed();
3526
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003527 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003528}
3529
Douglas Gregor43f54792010-02-17 02:12:47 +00003530Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3531 // Import the major distinguishing characteristics of a method.
3532 DeclContext *DC, *LexicalDC;
3533 DeclarationName Name;
3534 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003535 NamedDecl *ToD;
3536 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003537 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003538 if (ToD)
3539 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003540
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003541 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003542 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003543 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3544 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003545 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3546 continue;
3547
3548 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003549 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3550 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003551 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003552 << D->isInstanceMethod() << Name << D->getReturnType()
3553 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00003554 Importer.ToDiag(FoundMethod->getLocation(),
3555 diag::note_odr_objc_method_here)
3556 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003557 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003558 }
3559
3560 // Check the number of parameters.
3561 if (D->param_size() != FoundMethod->param_size()) {
3562 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3563 << D->isInstanceMethod() << Name
3564 << D->param_size() << FoundMethod->param_size();
3565 Importer.ToDiag(FoundMethod->getLocation(),
3566 diag::note_odr_objc_method_here)
3567 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003568 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003569 }
3570
3571 // Check parameter types.
3572 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
3573 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3574 P != PEnd; ++P, ++FoundP) {
3575 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3576 (*FoundP)->getType())) {
3577 Importer.FromDiag((*P)->getLocation(),
3578 diag::err_odr_objc_method_param_type_inconsistent)
3579 << D->isInstanceMethod() << Name
3580 << (*P)->getType() << (*FoundP)->getType();
3581 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3582 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003583 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003584 }
3585 }
3586
3587 // Check variadic/non-variadic.
3588 // Check the number of parameters.
3589 if (D->isVariadic() != FoundMethod->isVariadic()) {
3590 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3591 << D->isInstanceMethod() << Name;
3592 Importer.ToDiag(FoundMethod->getLocation(),
3593 diag::note_odr_objc_method_here)
3594 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003595 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003596 }
3597
3598 // FIXME: Any other bits we need to merge?
3599 return Importer.Imported(D, FoundMethod);
3600 }
3601 }
3602
3603 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00003604 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003605 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003606 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003607
Alp Toker314cc812014-01-25 16:55:45 +00003608 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00003609
Alp Toker314cc812014-01-25 16:55:45 +00003610 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
3611 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3612 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3613 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3614 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003615
3616 // FIXME: When we decide to merge method definitions, we'll need to
3617 // deal with implicit parameters.
3618
3619 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003620 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003621 for (auto *FromP : D->parameters()) {
Aaron Ballman43b68be2014-03-07 17:50:17 +00003622 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00003623 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003624 return nullptr;
3625
Douglas Gregor43f54792010-02-17 02:12:47 +00003626 ToParams.push_back(ToP);
3627 }
3628
3629 // Set the parameters.
3630 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3631 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003632 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003633 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003634 SmallVector<SourceLocation, 12> SelLocs;
3635 D->getSelectorLocs(SelLocs);
3636 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003637
3638 ToMethod->setLexicalDeclContext(LexicalDC);
3639 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003640 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003641 return ToMethod;
3642}
3643
Douglas Gregor85f3f952015-07-07 03:57:15 +00003644Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3645 // Import the major distinguishing characteristics of a category.
3646 DeclContext *DC, *LexicalDC;
3647 DeclarationName Name;
3648 SourceLocation Loc;
3649 NamedDecl *ToD;
3650 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3651 return nullptr;
3652 if (ToD)
3653 return ToD;
3654
3655 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3656 if (!BoundInfo)
3657 return nullptr;
3658
3659 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
3660 Importer.getToContext(), DC,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00003661 D->getVariance(),
3662 Importer.Import(D->getVarianceLoc()),
Douglas Gregore83b9562015-07-07 03:57:53 +00003663 D->getIndex(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003664 Importer.Import(D->getLocation()),
3665 Name.getAsIdentifierInfo(),
3666 Importer.Import(D->getColonLoc()),
3667 BoundInfo);
3668 Importer.Imported(D, Result);
3669 Result->setLexicalDeclContext(LexicalDC);
3670 return Result;
3671}
3672
Douglas Gregor84c51c32010-02-18 01:47:50 +00003673Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3674 // Import the major distinguishing characteristics of a category.
3675 DeclContext *DC, *LexicalDC;
3676 DeclarationName Name;
3677 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003678 NamedDecl *ToD;
3679 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003680 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003681 if (ToD)
3682 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003683
Douglas Gregor84c51c32010-02-18 01:47:50 +00003684 ObjCInterfaceDecl *ToInterface
3685 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3686 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00003687 return nullptr;
3688
Douglas Gregor84c51c32010-02-18 01:47:50 +00003689 // Determine if we've already encountered this category.
3690 ObjCCategoryDecl *MergeWithCategory
3691 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3692 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3693 if (!ToCategory) {
3694 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003695 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003696 Loc,
3697 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003698 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003699 ToInterface,
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003700 /*TypeParamList=*/nullptr,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003701 Importer.Import(D->getIvarLBraceLoc()),
3702 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003703 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003704 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003705 Importer.Imported(D, ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003706 // Import the type parameter list after calling Imported, to avoid
3707 // loops when bringing in their DeclContext.
3708 ToCategory->setTypeParamList(ImportObjCTypeParamList(
3709 D->getTypeParamList()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003710
Douglas Gregor84c51c32010-02-18 01:47:50 +00003711 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003712 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3713 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003714 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3715 = D->protocol_loc_begin();
3716 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3717 FromProtoEnd = D->protocol_end();
3718 FromProto != FromProtoEnd;
3719 ++FromProto, ++FromProtoLoc) {
3720 ObjCProtocolDecl *ToProto
3721 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3722 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00003723 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003724 Protocols.push_back(ToProto);
3725 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3726 }
3727
3728 // FIXME: If we're merging, make sure that the protocol list is the same.
3729 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3730 ProtocolLocs.data(), Importer.getToContext());
3731
3732 } else {
3733 Importer.Imported(D, ToCategory);
3734 }
3735
3736 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003737 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003738
3739 // If we have an implementation, import it as well.
3740 if (D->getImplementation()) {
3741 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003742 = cast_or_null<ObjCCategoryImplDecl>(
3743 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003744 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00003745 return nullptr;
3746
Douglas Gregor84c51c32010-02-18 01:47:50 +00003747 ToCategory->setImplementation(Impl);
3748 }
3749
3750 return ToCategory;
3751}
3752
Douglas Gregor2aa53772012-01-24 17:42:07 +00003753bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3754 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003755 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003756 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003757 if (shouldForceImportDeclContext(Kind))
3758 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003759 return false;
3760 }
3761
3762 // Start the protocol definition
3763 To->startDefinition();
3764
3765 // Import protocols
3766 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3767 SmallVector<SourceLocation, 4> ProtocolLocs;
3768 ObjCProtocolDecl::protocol_loc_iterator
3769 FromProtoLoc = From->protocol_loc_begin();
3770 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3771 FromProtoEnd = From->protocol_end();
3772 FromProto != FromProtoEnd;
3773 ++FromProto, ++FromProtoLoc) {
3774 ObjCProtocolDecl *ToProto
3775 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3776 if (!ToProto)
3777 return true;
3778 Protocols.push_back(ToProto);
3779 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3780 }
3781
3782 // FIXME: If we're merging, make sure that the protocol list is the same.
3783 To->setProtocolList(Protocols.data(), Protocols.size(),
3784 ProtocolLocs.data(), Importer.getToContext());
3785
Douglas Gregor2e15c842012-02-01 21:00:38 +00003786 if (shouldForceImportDeclContext(Kind)) {
3787 // Import all of the members of this protocol.
3788 ImportDeclContext(From, /*ForceImport=*/true);
3789 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003790 return false;
3791}
3792
Douglas Gregor98d156a2010-02-17 16:12:00 +00003793Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003794 // If this protocol has a definition in the translation unit we're coming
3795 // from, but this particular declaration is not that definition, import the
3796 // definition and map to that.
3797 ObjCProtocolDecl *Definition = D->getDefinition();
3798 if (Definition && Definition != D) {
3799 Decl *ImportedDef = Importer.Import(Definition);
3800 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003801 return nullptr;
3802
Douglas Gregor2aa53772012-01-24 17:42:07 +00003803 return Importer.Imported(D, ImportedDef);
3804 }
3805
Douglas Gregor84c51c32010-02-18 01:47:50 +00003806 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003807 DeclContext *DC, *LexicalDC;
3808 DeclarationName Name;
3809 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003810 NamedDecl *ToD;
3811 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003812 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003813 if (ToD)
3814 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00003815
Craig Topper36250ad2014-05-12 05:36:57 +00003816 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003817 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003818 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003819 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3820 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003821 continue;
3822
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003823 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003824 break;
3825 }
3826
3827 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003828 if (!ToProto) {
3829 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3830 Name.getAsIdentifierInfo(), Loc,
3831 Importer.Import(D->getAtStartLoc()),
Craig Topper36250ad2014-05-12 05:36:57 +00003832 /*PrevDecl=*/nullptr);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003833 ToProto->setLexicalDeclContext(LexicalDC);
3834 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003835 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003836
3837 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003838
Douglas Gregor2aa53772012-01-24 17:42:07 +00003839 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00003840 return nullptr;
3841
Douglas Gregor98d156a2010-02-17 16:12:00 +00003842 return ToProto;
3843}
3844
Sean Callanan0aae0412014-12-10 00:00:37 +00003845Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3846 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3847 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3848
3849 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3850 SourceLocation LangLoc = Importer.Import(D->getLocation());
3851
3852 bool HasBraces = D->hasBraces();
3853
Sean Callananb12a8552014-12-10 21:22:20 +00003854 LinkageSpecDecl *ToLinkageSpec =
3855 LinkageSpecDecl::Create(Importer.getToContext(),
3856 DC,
3857 ExternLoc,
3858 LangLoc,
3859 D->getLanguage(),
3860 HasBraces);
Sean Callanan0aae0412014-12-10 00:00:37 +00003861
3862 if (HasBraces) {
3863 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3864 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3865 }
3866
3867 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3868 LexicalDC->addDeclInternal(ToLinkageSpec);
3869
3870 Importer.Imported(D, ToLinkageSpec);
3871
3872 return ToLinkageSpec;
3873}
3874
Douglas Gregor2aa53772012-01-24 17:42:07 +00003875bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3876 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003877 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003878 if (To->getDefinition()) {
3879 // Check consistency of superclass.
3880 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3881 if (FromSuper) {
3882 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3883 if (!FromSuper)
3884 return true;
3885 }
3886
3887 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3888 if ((bool)FromSuper != (bool)ToSuper ||
3889 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3890 Importer.ToDiag(To->getLocation(),
3891 diag::err_odr_objc_superclass_inconsistent)
3892 << To->getDeclName();
3893 if (ToSuper)
3894 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3895 << To->getSuperClass()->getDeclName();
3896 else
3897 Importer.ToDiag(To->getLocation(),
3898 diag::note_odr_objc_missing_superclass);
3899 if (From->getSuperClass())
3900 Importer.FromDiag(From->getSuperClassLoc(),
3901 diag::note_odr_objc_superclass)
3902 << From->getSuperClass()->getDeclName();
3903 else
3904 Importer.FromDiag(From->getLocation(),
3905 diag::note_odr_objc_missing_superclass);
3906 }
3907
Douglas Gregor2e15c842012-02-01 21:00:38 +00003908 if (shouldForceImportDeclContext(Kind))
3909 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003910 return false;
3911 }
3912
3913 // Start the definition.
3914 To->startDefinition();
3915
3916 // If this class has a superclass, import it.
3917 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00003918 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3919 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00003920 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00003921
3922 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003923 }
3924
3925 // Import protocols
3926 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3927 SmallVector<SourceLocation, 4> ProtocolLocs;
3928 ObjCInterfaceDecl::protocol_loc_iterator
3929 FromProtoLoc = From->protocol_loc_begin();
3930
3931 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3932 FromProtoEnd = From->protocol_end();
3933 FromProto != FromProtoEnd;
3934 ++FromProto, ++FromProtoLoc) {
3935 ObjCProtocolDecl *ToProto
3936 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3937 if (!ToProto)
3938 return true;
3939 Protocols.push_back(ToProto);
3940 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3941 }
3942
3943 // FIXME: If we're merging, make sure that the protocol list is the same.
3944 To->setProtocolList(Protocols.data(), Protocols.size(),
3945 ProtocolLocs.data(), Importer.getToContext());
3946
3947 // Import categories. When the categories themselves are imported, they'll
3948 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00003949 for (auto *Cat : From->known_categories())
3950 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003951
Douglas Gregor2aa53772012-01-24 17:42:07 +00003952 // If we have an @implementation, import it as well.
3953 if (From->getImplementation()) {
3954 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3955 Importer.Import(From->getImplementation()));
3956 if (!Impl)
3957 return true;
3958
3959 To->setImplementation(Impl);
3960 }
3961
Douglas Gregor2e15c842012-02-01 21:00:38 +00003962 if (shouldForceImportDeclContext(Kind)) {
3963 // Import all of the members of this class.
3964 ImportDeclContext(From, /*ForceImport=*/true);
3965 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003966 return false;
3967}
3968
Douglas Gregor85f3f952015-07-07 03:57:15 +00003969ObjCTypeParamList *
3970ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3971 if (!list)
3972 return nullptr;
3973
3974 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
3975 for (auto fromTypeParam : *list) {
3976 auto toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3977 Importer.Import(fromTypeParam));
3978 if (!toTypeParam)
3979 return nullptr;
3980
3981 toTypeParams.push_back(toTypeParam);
3982 }
3983
3984 return ObjCTypeParamList::create(Importer.getToContext(),
3985 Importer.Import(list->getLAngleLoc()),
3986 toTypeParams,
3987 Importer.Import(list->getRAngleLoc()));
3988}
3989
Douglas Gregor45635322010-02-16 01:20:57 +00003990Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003991 // If this class has a definition in the translation unit we're coming from,
3992 // but this particular declaration is not that definition, import the
3993 // definition and map to that.
3994 ObjCInterfaceDecl *Definition = D->getDefinition();
3995 if (Definition && Definition != D) {
3996 Decl *ImportedDef = Importer.Import(Definition);
3997 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003998 return nullptr;
3999
Douglas Gregor2aa53772012-01-24 17:42:07 +00004000 return Importer.Imported(D, ImportedDef);
4001 }
4002
Douglas Gregor45635322010-02-16 01:20:57 +00004003 // Import the major distinguishing characteristics of an @interface.
4004 DeclContext *DC, *LexicalDC;
4005 DeclarationName Name;
4006 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004007 NamedDecl *ToD;
4008 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004009 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004010 if (ToD)
4011 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00004012
Douglas Gregor2aa53772012-01-24 17:42:07 +00004013 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00004014 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004015 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004016 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004017 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4018 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00004019 continue;
4020
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004021 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00004022 break;
4023 }
4024
Douglas Gregor2aa53772012-01-24 17:42:07 +00004025 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00004026 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004027 if (!ToIface) {
4028 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
4029 Importer.Import(D->getAtStartLoc()),
Douglas Gregor85f3f952015-07-07 03:57:15 +00004030 Name.getAsIdentifierInfo(),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004031 /*TypeParamList=*/nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00004032 /*PrevDecl=*/nullptr, Loc,
Douglas Gregor2aa53772012-01-24 17:42:07 +00004033 D->isImplicitInterfaceDecl());
4034 ToIface->setLexicalDeclContext(LexicalDC);
4035 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00004036 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00004037 Importer.Imported(D, ToIface);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004038 // Import the type parameter list after calling Imported, to avoid
4039 // loops when bringing in their DeclContext.
4040 ToIface->setTypeParamList(ImportObjCTypeParamList(
4041 D->getTypeParamListAsWritten()));
Douglas Gregor45635322010-02-16 01:20:57 +00004042
Douglas Gregor2aa53772012-01-24 17:42:07 +00004043 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00004044 return nullptr;
4045
Douglas Gregor98d156a2010-02-17 16:12:00 +00004046 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00004047}
4048
Douglas Gregor4da9d682010-12-07 15:32:12 +00004049Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
4050 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
4051 Importer.Import(D->getCategoryDecl()));
4052 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00004053 return nullptr;
4054
Douglas Gregor4da9d682010-12-07 15:32:12 +00004055 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
4056 if (!ToImpl) {
4057 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4058 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004059 return nullptr;
4060
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00004061 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00004062 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00004063 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00004064 Category->getClassInterface(),
4065 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00004066 Importer.Import(D->getAtStartLoc()),
4067 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004068
4069 DeclContext *LexicalDC = DC;
4070 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4071 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4072 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004073 return nullptr;
4074
Douglas Gregor4da9d682010-12-07 15:32:12 +00004075 ToImpl->setLexicalDeclContext(LexicalDC);
4076 }
4077
Sean Callanan95e74be2011-10-21 02:57:43 +00004078 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004079 Category->setImplementation(ToImpl);
4080 }
4081
4082 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00004083 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004084 return ToImpl;
4085}
4086
Douglas Gregorda8025c2010-12-07 01:26:03 +00004087Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
4088 // Find the corresponding interface.
4089 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
4090 Importer.Import(D->getClassInterface()));
4091 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00004092 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004093
4094 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00004095 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004096 if (D->getSuperClass()) {
4097 Super = cast_or_null<ObjCInterfaceDecl>(
4098 Importer.Import(D->getSuperClass()));
4099 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00004100 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004101 }
4102
4103 ObjCImplementationDecl *Impl = Iface->getImplementation();
4104 if (!Impl) {
4105 // We haven't imported an implementation yet. Create a new @implementation
4106 // now.
4107 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
4108 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00004109 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00004110 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00004111 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00004112 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00004113 Importer.Import(D->getIvarLBraceLoc()),
4114 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00004115
4116 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4117 DeclContext *LexicalDC
4118 = Importer.ImportContext(D->getLexicalDeclContext());
4119 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004120 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004121 Impl->setLexicalDeclContext(LexicalDC);
4122 }
4123
4124 // Associate the implementation with the class it implements.
4125 Iface->setImplementation(Impl);
4126 Importer.Imported(D, Iface->getImplementation());
4127 } else {
4128 Importer.Imported(D, Iface->getImplementation());
4129
4130 // Verify that the existing @implementation has the same superclass.
4131 if ((Super && !Impl->getSuperClass()) ||
4132 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004133 (Super && Impl->getSuperClass() &&
4134 !declaresSameEntity(Super->getCanonicalDecl(),
4135 Impl->getSuperClass()))) {
4136 Importer.ToDiag(Impl->getLocation(),
4137 diag::err_odr_objc_superclass_inconsistent)
4138 << Iface->getDeclName();
4139 // FIXME: It would be nice to have the location of the superclass
4140 // below.
4141 if (Impl->getSuperClass())
4142 Importer.ToDiag(Impl->getLocation(),
4143 diag::note_odr_objc_superclass)
4144 << Impl->getSuperClass()->getDeclName();
4145 else
4146 Importer.ToDiag(Impl->getLocation(),
4147 diag::note_odr_objc_missing_superclass);
4148 if (D->getSuperClass())
4149 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004150 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004151 << D->getSuperClass()->getDeclName();
4152 else
4153 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004154 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00004155 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004156 }
4157 }
4158
4159 // Import all of the members of this @implementation.
4160 ImportDeclContext(D);
4161
4162 return Impl;
4163}
4164
Douglas Gregora11c4582010-02-17 18:02:10 +00004165Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
4166 // Import the major distinguishing characteristics of an @property.
4167 DeclContext *DC, *LexicalDC;
4168 DeclarationName Name;
4169 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004170 NamedDecl *ToD;
4171 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004172 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004173 if (ToD)
4174 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004175
4176 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004177 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004178 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004179 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004180 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004181 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004182 // Check property types.
4183 if (!Importer.IsStructurallyEquivalent(D->getType(),
4184 FoundProp->getType())) {
4185 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
4186 << Name << D->getType() << FoundProp->getType();
4187 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4188 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00004189 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004190 }
4191
4192 // FIXME: Check property attributes, getters, setters, etc.?
4193
4194 // Consider these properties to be equivalent.
4195 Importer.Imported(D, FoundProp);
4196 return FoundProp;
4197 }
4198 }
4199
4200 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00004201 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
4202 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00004203 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004204
4205 // Create the new property.
4206 ObjCPropertyDecl *ToProperty
4207 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
4208 Name.getAsIdentifierInfo(),
4209 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00004210 Importer.Import(D->getLParenLoc()),
Douglas Gregor813a0662015-06-19 18:14:38 +00004211 Importer.Import(D->getType()),
4212 TSI,
Douglas Gregora11c4582010-02-17 18:02:10 +00004213 D->getPropertyImplementation());
4214 Importer.Imported(D, ToProperty);
4215 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004216 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004217
4218 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004219 ToProperty->setPropertyAttributesAsWritten(
4220 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00004221 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
4222 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
4223 ToProperty->setGetterMethodDecl(
4224 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
4225 ToProperty->setSetterMethodDecl(
4226 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
4227 ToProperty->setPropertyIvarDecl(
4228 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
4229 return ToProperty;
4230}
4231
Douglas Gregor14a49e22010-12-07 18:32:03 +00004232Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4233 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
4234 Importer.Import(D->getPropertyDecl()));
4235 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00004236 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004237
4238 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4239 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004240 return nullptr;
4241
Douglas Gregor14a49e22010-12-07 18:32:03 +00004242 // Import the lexical declaration context.
4243 DeclContext *LexicalDC = DC;
4244 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4245 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4246 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004247 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004248 }
4249
4250 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
4251 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00004252 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004253
4254 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004255 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004256 if (D->getPropertyIvarDecl()) {
4257 Ivar = cast_or_null<ObjCIvarDecl>(
4258 Importer.Import(D->getPropertyIvarDecl()));
4259 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00004260 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004261 }
4262
4263 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004264 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4265 Property->getQueryKind());
Douglas Gregor14a49e22010-12-07 18:32:03 +00004266 if (!ToImpl) {
4267 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
4268 Importer.Import(D->getLocStart()),
4269 Importer.Import(D->getLocation()),
4270 Property,
4271 D->getPropertyImplementation(),
4272 Ivar,
4273 Importer.Import(D->getPropertyIvarDeclLoc()));
4274 ToImpl->setLexicalDeclContext(LexicalDC);
4275 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00004276 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004277 } else {
4278 // Check that we have the same kind of property implementation (@synthesize
4279 // vs. @dynamic).
4280 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
4281 Importer.ToDiag(ToImpl->getLocation(),
4282 diag::err_odr_objc_property_impl_kind_inconsistent)
4283 << Property->getDeclName()
4284 << (ToImpl->getPropertyImplementation()
4285 == ObjCPropertyImplDecl::Dynamic);
4286 Importer.FromDiag(D->getLocation(),
4287 diag::note_odr_objc_property_impl_kind)
4288 << D->getPropertyDecl()->getDeclName()
4289 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00004290 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004291 }
4292
4293 // For @synthesize, check that we have the same
4294 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4295 Ivar != ToImpl->getPropertyIvarDecl()) {
4296 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
4297 diag::err_odr_objc_synthesize_ivar_inconsistent)
4298 << Property->getDeclName()
4299 << ToImpl->getPropertyIvarDecl()->getDeclName()
4300 << Ivar->getDeclName();
4301 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
4302 diag::note_odr_objc_synthesize_ivar_here)
4303 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00004304 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004305 }
4306
4307 // Merge the existing implementation with the new implementation.
4308 Importer.Imported(D, ToImpl);
4309 }
4310
4311 return ToImpl;
4312}
4313
Douglas Gregora082a492010-11-30 19:14:50 +00004314Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
4315 // For template arguments, we adopt the translation unit as our declaration
4316 // context. This context will be fixed when the actual template declaration
4317 // is created.
4318
4319 // FIXME: Import default argument.
4320 return TemplateTypeParmDecl::Create(Importer.getToContext(),
4321 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00004322 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004323 Importer.Import(D->getLocation()),
4324 D->getDepth(),
4325 D->getIndex(),
4326 Importer.Import(D->getIdentifier()),
4327 D->wasDeclaredWithTypename(),
4328 D->isParameterPack());
4329}
4330
4331Decl *
4332ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
4333 // Import the name of this declaration.
4334 DeclarationName Name = Importer.Import(D->getDeclName());
4335 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004336 return nullptr;
4337
Douglas Gregora082a492010-11-30 19:14:50 +00004338 // Import the location of this declaration.
4339 SourceLocation Loc = Importer.Import(D->getLocation());
4340
4341 // Import the type of this declaration.
4342 QualType T = Importer.Import(D->getType());
4343 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004344 return nullptr;
4345
Douglas Gregora082a492010-11-30 19:14:50 +00004346 // Import type-source information.
4347 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4348 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00004349 return nullptr;
4350
Douglas Gregora082a492010-11-30 19:14:50 +00004351 // FIXME: Import default argument.
4352
4353 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
4354 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004355 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004356 Loc, D->getDepth(), D->getPosition(),
4357 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00004358 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00004359}
4360
4361Decl *
4362ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4363 // Import the name of this declaration.
4364 DeclarationName Name = Importer.Import(D->getDeclName());
4365 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004366 return nullptr;
4367
Douglas Gregora082a492010-11-30 19:14:50 +00004368 // Import the location of this declaration.
4369 SourceLocation Loc = Importer.Import(D->getLocation());
4370
4371 // Import template parameters.
4372 TemplateParameterList *TemplateParams
4373 = ImportTemplateParameterList(D->getTemplateParameters());
4374 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004375 return nullptr;
4376
Douglas Gregora082a492010-11-30 19:14:50 +00004377 // FIXME: Import default argument.
4378
4379 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
4380 Importer.getToContext().getTranslationUnitDecl(),
4381 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00004382 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00004383 Name.getAsIdentifierInfo(),
4384 TemplateParams);
4385}
4386
4387Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
4388 // If this record has a definition in the translation unit we're coming from,
4389 // but this particular declaration is not that definition, import the
4390 // definition and map to that.
4391 CXXRecordDecl *Definition
4392 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
4393 if (Definition && Definition != D->getTemplatedDecl()) {
4394 Decl *ImportedDef
4395 = Importer.Import(Definition->getDescribedClassTemplate());
4396 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004397 return nullptr;
4398
Douglas Gregora082a492010-11-30 19:14:50 +00004399 return Importer.Imported(D, ImportedDef);
4400 }
4401
4402 // Import the major distinguishing characteristics of this class template.
4403 DeclContext *DC, *LexicalDC;
4404 DeclarationName Name;
4405 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004406 NamedDecl *ToD;
4407 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004408 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004409 if (ToD)
4410 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004411
Douglas Gregora082a492010-11-30 19:14:50 +00004412 // We may already have a template of the same name; try to find and match it.
4413 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004414 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004415 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004416 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004417 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4418 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00004419 continue;
4420
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004421 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00004422 if (ClassTemplateDecl *FoundTemplate
4423 = dyn_cast<ClassTemplateDecl>(Found)) {
4424 if (IsStructuralMatch(D, FoundTemplate)) {
4425 // The class templates structurally match; call it the same template.
4426 // FIXME: We may be filling in a forward declaration here. Handle
4427 // this case!
4428 Importer.Imported(D->getTemplatedDecl(),
4429 FoundTemplate->getTemplatedDecl());
4430 return Importer.Imported(D, FoundTemplate);
4431 }
4432 }
4433
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004434 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00004435 }
4436
4437 if (!ConflictingDecls.empty()) {
4438 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4439 ConflictingDecls.data(),
4440 ConflictingDecls.size());
4441 }
4442
4443 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004444 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00004445 }
4446
4447 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
4448
4449 // Create the declaration that is being templated.
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004450 // Create the declaration that is being templated.
4451 CXXRecordDecl *D2Templated = cast_or_null<CXXRecordDecl>(
4452 Importer.Import(DTemplated));
4453 if (!D2Templated)
4454 return nullptr;
4455
4456 // Resolve possible cyclic import.
4457 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
4458 return AlreadyImported;
4459
Douglas Gregora082a492010-11-30 19:14:50 +00004460 // Create the class template declaration itself.
4461 TemplateParameterList *TemplateParams
4462 = ImportTemplateParameterList(D->getTemplateParameters());
4463 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004464 return nullptr;
4465
Douglas Gregora082a492010-11-30 19:14:50 +00004466 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4467 Loc, Name, TemplateParams,
4468 D2Templated,
Craig Topper36250ad2014-05-12 05:36:57 +00004469 /*PrevDecl=*/nullptr);
Douglas Gregora082a492010-11-30 19:14:50 +00004470 D2Templated->setDescribedClassTemplate(D2);
4471
4472 D2->setAccess(D->getAccess());
4473 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004474 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004475
4476 // Note the relationship between the class templates.
4477 Importer.Imported(D, D2);
4478 Importer.Imported(DTemplated, D2Templated);
4479
John McCallf937c022011-10-07 06:10:15 +00004480 if (DTemplated->isCompleteDefinition() &&
4481 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004482 // FIXME: Import definition!
4483 }
4484
4485 return D2;
4486}
4487
Douglas Gregore2e50d332010-12-01 01:36:18 +00004488Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4489 ClassTemplateSpecializationDecl *D) {
4490 // If this record has a definition in the translation unit we're coming from,
4491 // but this particular declaration is not that definition, import the
4492 // definition and map to that.
4493 TagDecl *Definition = D->getDefinition();
4494 if (Definition && Definition != D) {
4495 Decl *ImportedDef = Importer.Import(Definition);
4496 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004497 return nullptr;
4498
Douglas Gregore2e50d332010-12-01 01:36:18 +00004499 return Importer.Imported(D, ImportedDef);
4500 }
4501
4502 ClassTemplateDecl *ClassTemplate
4503 = cast_or_null<ClassTemplateDecl>(Importer.Import(
4504 D->getSpecializedTemplate()));
4505 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004506 return nullptr;
4507
Douglas Gregore2e50d332010-12-01 01:36:18 +00004508 // Import the context of this declaration.
4509 DeclContext *DC = ClassTemplate->getDeclContext();
4510 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004511 return nullptr;
4512
Douglas Gregore2e50d332010-12-01 01:36:18 +00004513 DeclContext *LexicalDC = DC;
4514 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4515 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4516 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004517 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004518 }
4519
4520 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004521 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4522 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004523
4524 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004525 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004526 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4527 D->getTemplateArgs().size(),
4528 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004529 return nullptr;
4530
Douglas Gregore2e50d332010-12-01 01:36:18 +00004531 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004532 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004533 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004534 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004535 if (D2) {
4536 // We already have a class template specialization with these template
4537 // arguments.
4538
4539 // FIXME: Check for specialization vs. instantiation errors.
4540
4541 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004542 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004543 // The record types structurally match, or the "from" translation
4544 // unit only had a forward declaration anyway; call it the same
4545 // function.
4546 return Importer.Imported(D, FoundDef);
4547 }
4548 }
4549 } else {
4550 // Create a new specialization.
4551 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4552 D->getTagKind(), DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004553 StartLoc, IdLoc,
4554 ClassTemplate,
David Majnemer8b622692016-07-03 21:17:51 +00004555 TemplateArgs,
Craig Topper36250ad2014-05-12 05:36:57 +00004556 /*PrevDecl=*/nullptr);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004557 D2->setSpecializationKind(D->getSpecializationKind());
4558
4559 // Add this specialization to the class template.
4560 ClassTemplate->AddSpecialization(D2, InsertPos);
4561
4562 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004563 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00004564
4565 // Add the specialization to this context.
4566 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004567 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004568 }
4569 Importer.Imported(D, D2);
4570
John McCallf937c022011-10-07 06:10:15 +00004571 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004572 return nullptr;
4573
Douglas Gregore2e50d332010-12-01 01:36:18 +00004574 return D2;
4575}
4576
Larisse Voufo39a1e502013-08-06 01:03:05 +00004577Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4578 // If this variable has a definition in the translation unit we're coming
4579 // from,
4580 // but this particular declaration is not that definition, import the
4581 // definition and map to that.
4582 VarDecl *Definition =
4583 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4584 if (Definition && Definition != D->getTemplatedDecl()) {
4585 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4586 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004587 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004588
4589 return Importer.Imported(D, ImportedDef);
4590 }
4591
4592 // Import the major distinguishing characteristics of this variable template.
4593 DeclContext *DC, *LexicalDC;
4594 DeclarationName Name;
4595 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004596 NamedDecl *ToD;
4597 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004598 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004599 if (ToD)
4600 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004601
4602 // We may already have a template of the same name; try to find and match it.
4603 assert(!DC->isFunctionOrMethod() &&
4604 "Variable templates cannot be declared at function scope");
4605 SmallVector<NamedDecl *, 4> ConflictingDecls;
4606 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004607 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004608 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4609 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4610 continue;
4611
4612 Decl *Found = FoundDecls[I];
4613 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
4614 if (IsStructuralMatch(D, FoundTemplate)) {
4615 // The variable templates structurally match; call it the same template.
4616 Importer.Imported(D->getTemplatedDecl(),
4617 FoundTemplate->getTemplatedDecl());
4618 return Importer.Imported(D, FoundTemplate);
4619 }
4620 }
4621
4622 ConflictingDecls.push_back(FoundDecls[I]);
4623 }
4624
4625 if (!ConflictingDecls.empty()) {
4626 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4627 ConflictingDecls.data(),
4628 ConflictingDecls.size());
4629 }
4630
4631 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004632 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004633
4634 VarDecl *DTemplated = D->getTemplatedDecl();
4635
4636 // Import the type.
4637 QualType T = Importer.Import(DTemplated->getType());
4638 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004639 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004640
4641 // Create the declaration that is being templated.
4642 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4643 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
4644 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo());
4645 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc,
4646 IdLoc, Name.getAsIdentifierInfo(), T,
4647 TInfo, DTemplated->getStorageClass());
4648 D2Templated->setAccess(DTemplated->getAccess());
4649 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
4650 D2Templated->setLexicalDeclContext(LexicalDC);
4651
4652 // Importer.Imported(DTemplated, D2Templated);
4653 // LexicalDC->addDeclInternal(D2Templated);
4654
4655 // Merge the initializer.
4656 if (ImportDefinition(DTemplated, D2Templated))
Craig Topper36250ad2014-05-12 05:36:57 +00004657 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004658
4659 // Create the variable template declaration itself.
4660 TemplateParameterList *TemplateParams =
4661 ImportTemplateParameterList(D->getTemplateParameters());
4662 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004663 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004664
4665 VarTemplateDecl *D2 = VarTemplateDecl::Create(
Richard Smithbeef3452014-01-16 23:39:20 +00004666 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004667 D2Templated->setDescribedVarTemplate(D2);
4668
4669 D2->setAccess(D->getAccess());
4670 D2->setLexicalDeclContext(LexicalDC);
4671 LexicalDC->addDeclInternal(D2);
4672
4673 // Note the relationship between the variable templates.
4674 Importer.Imported(D, D2);
4675 Importer.Imported(DTemplated, D2Templated);
4676
4677 if (DTemplated->isThisDeclarationADefinition() &&
4678 !D2Templated->isThisDeclarationADefinition()) {
4679 // FIXME: Import definition!
4680 }
4681
4682 return D2;
4683}
4684
4685Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4686 VarTemplateSpecializationDecl *D) {
4687 // If this record has a definition in the translation unit we're coming from,
4688 // but this particular declaration is not that definition, import the
4689 // definition and map to that.
4690 VarDecl *Definition = D->getDefinition();
4691 if (Definition && Definition != D) {
4692 Decl *ImportedDef = Importer.Import(Definition);
4693 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004694 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004695
4696 return Importer.Imported(D, ImportedDef);
4697 }
4698
4699 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
4700 Importer.Import(D->getSpecializedTemplate()));
4701 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004702 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004703
4704 // Import the context of this declaration.
4705 DeclContext *DC = VarTemplate->getDeclContext();
4706 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004707 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004708
4709 DeclContext *LexicalDC = DC;
4710 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4711 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4712 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004713 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004714 }
4715
4716 // Import the location of this declaration.
4717 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4718 SourceLocation IdLoc = Importer.Import(D->getLocation());
4719
4720 // Import template arguments.
4721 SmallVector<TemplateArgument, 2> TemplateArgs;
4722 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4723 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004724 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004725
4726 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004727 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004728 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004729 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004730 if (D2) {
4731 // We already have a variable template specialization with these template
4732 // arguments.
4733
4734 // FIXME: Check for specialization vs. instantiation errors.
4735
4736 if (VarDecl *FoundDef = D2->getDefinition()) {
4737 if (!D->isThisDeclarationADefinition() ||
4738 IsStructuralMatch(D, FoundDef)) {
4739 // The record types structurally match, or the "from" translation
4740 // unit only had a forward declaration anyway; call it the same
4741 // variable.
4742 return Importer.Imported(D, FoundDef);
4743 }
4744 }
4745 } else {
4746
4747 // Import the type.
4748 QualType T = Importer.Import(D->getType());
4749 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004750 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004751 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4752
4753 // Create a new specialization.
4754 D2 = VarTemplateSpecializationDecl::Create(
4755 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
David Majnemer8b622692016-07-03 21:17:51 +00004756 D->getStorageClass(), TemplateArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004757 D2->setSpecializationKind(D->getSpecializationKind());
4758 D2->setTemplateArgsInfo(D->getTemplateArgsInfo());
4759
4760 // Add this specialization to the class template.
4761 VarTemplate->AddSpecialization(D2, InsertPos);
4762
4763 // Import the qualifier, if any.
4764 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4765
4766 // Add the specialization to this context.
4767 D2->setLexicalDeclContext(LexicalDC);
4768 LexicalDC->addDeclInternal(D2);
4769 }
4770 Importer.Imported(D, D2);
4771
4772 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004773 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004774
4775 return D2;
4776}
4777
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004778//----------------------------------------------------------------------------
4779// Import Statements
4780//----------------------------------------------------------------------------
4781
Sean Callanan59721b32015-04-28 18:41:46 +00004782DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4783 if (DG.isNull())
4784 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4785 size_t NumDecls = DG.end() - DG.begin();
4786 SmallVector<Decl *, 1> ToDecls(NumDecls);
4787 auto &_Importer = this->Importer;
4788 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4789 [&_Importer](Decl *D) -> Decl * {
4790 return _Importer.Import(D);
4791 });
4792 return DeclGroupRef::Create(Importer.getToContext(),
4793 ToDecls.begin(),
4794 NumDecls);
4795}
4796
4797 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4798 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4799 << S->getStmtClassName();
4800 return nullptr;
4801 }
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004802
4803
4804Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
4805 SmallVector<IdentifierInfo *, 4> Names;
4806 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4807 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
4808 if (!ToII)
4809 return nullptr;
4810 Names.push_back(ToII);
4811 }
4812 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4813 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
4814 if (!ToII)
4815 return nullptr;
4816 Names.push_back(ToII);
4817 }
4818
4819 SmallVector<StringLiteral *, 4> Clobbers;
4820 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
4821 StringLiteral *Clobber = cast_or_null<StringLiteral>(
4822 Importer.Import(S->getClobberStringLiteral(I)));
4823 if (!Clobber)
4824 return nullptr;
4825 Clobbers.push_back(Clobber);
4826 }
4827
4828 SmallVector<StringLiteral *, 4> Constraints;
4829 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4830 StringLiteral *Output = cast_or_null<StringLiteral>(
4831 Importer.Import(S->getOutputConstraintLiteral(I)));
4832 if (!Output)
4833 return nullptr;
4834 Constraints.push_back(Output);
4835 }
4836
4837 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4838 StringLiteral *Input = cast_or_null<StringLiteral>(
4839 Importer.Import(S->getInputConstraintLiteral(I)));
4840 if (!Input)
4841 return nullptr;
4842 Constraints.push_back(Input);
4843 }
4844
4845 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
4846 if (ImportArrayChecked(S->begin_outputs(), S->end_outputs(), Exprs.begin()))
4847 return nullptr;
4848
4849 if (ImportArrayChecked(S->begin_inputs(), S->end_inputs(),
4850 Exprs.begin() + S->getNumOutputs()))
4851 return nullptr;
4852
4853 StringLiteral *AsmStr = cast_or_null<StringLiteral>(
4854 Importer.Import(S->getAsmString()));
4855 if (!AsmStr)
4856 return nullptr;
4857
4858 return new (Importer.getToContext()) GCCAsmStmt(
4859 Importer.getToContext(),
4860 Importer.Import(S->getAsmLoc()),
4861 S->isSimple(),
4862 S->isVolatile(),
4863 S->getNumOutputs(),
4864 S->getNumInputs(),
4865 Names.data(),
4866 Constraints.data(),
4867 Exprs.data(),
4868 AsmStr,
4869 S->getNumClobbers(),
4870 Clobbers.data(),
4871 Importer.Import(S->getRParenLoc()));
4872}
4873
Sean Callanan59721b32015-04-28 18:41:46 +00004874Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4875 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
4876 for (Decl *ToD : ToDG) {
4877 if (!ToD)
4878 return nullptr;
4879 }
4880 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4881 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4882 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4883}
4884
4885Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4886 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4887 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4888 S->hasLeadingEmptyMacro());
4889}
4890
4891Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004892 llvm::SmallVector<Stmt *, 8> ToStmts(S->size());
Sean Callanan8bca9962016-03-28 21:43:01 +00004893
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004894 if (ImportArrayChecked(S->body_begin(), S->body_end(), ToStmts.begin()))
Sean Callanan8bca9962016-03-28 21:43:01 +00004895 return nullptr;
4896
Sean Callanan59721b32015-04-28 18:41:46 +00004897 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4898 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
4899 return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(),
4900 ToStmts,
4901 ToLBraceLoc, ToRBraceLoc);
4902}
4903
4904Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4905 Expr *ToLHS = Importer.Import(S->getLHS());
4906 if (!ToLHS)
4907 return nullptr;
4908 Expr *ToRHS = Importer.Import(S->getRHS());
4909 if (!ToRHS && S->getRHS())
4910 return nullptr;
4911 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4912 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4913 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4914 return new (Importer.getToContext()) CaseStmt(ToLHS, ToRHS,
4915 ToCaseLoc, ToEllipsisLoc,
4916 ToColonLoc);
4917}
4918
4919Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4920 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4921 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4922 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4923 if (!ToSubStmt && S->getSubStmt())
4924 return nullptr;
4925 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4926 ToSubStmt);
4927}
4928
4929Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4930 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
4931 LabelDecl *ToLabelDecl =
4932 cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
4933 if (!ToLabelDecl && S->getDecl())
4934 return nullptr;
4935 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4936 if (!ToSubStmt && S->getSubStmt())
4937 return nullptr;
4938 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4939 ToSubStmt);
4940}
4941
4942Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4943 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4944 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4945 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
4946 ASTContext &_ToContext = Importer.getToContext();
4947 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
4948 [&_ToContext](const Attr *A) -> const Attr * {
4949 return A->clone(_ToContext);
4950 });
4951 for (const Attr *ToA : ToAttrs) {
4952 if (!ToA)
4953 return nullptr;
4954 }
4955 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4956 if (!ToSubStmt && S->getSubStmt())
4957 return nullptr;
4958 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4959 ToAttrs, ToSubStmt);
4960}
4961
4962Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4963 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
4964 VarDecl *ToConditionVariable = nullptr;
4965 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4966 ToConditionVariable =
4967 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4968 if (!ToConditionVariable)
4969 return nullptr;
4970 }
4971 Expr *ToCondition = Importer.Import(S->getCond());
4972 if (!ToCondition && S->getCond())
4973 return nullptr;
4974 Stmt *ToThenStmt = Importer.Import(S->getThen());
4975 if (!ToThenStmt && S->getThen())
4976 return nullptr;
4977 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4978 Stmt *ToElseStmt = Importer.Import(S->getElse());
4979 if (!ToElseStmt && S->getElse())
4980 return nullptr;
4981 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
Richard Smithb130fe72016-06-23 19:16:49 +00004982 ToIfLoc, S->isConstexpr(),
4983 ToConditionVariable,
Sean Callanan59721b32015-04-28 18:41:46 +00004984 ToCondition, ToThenStmt,
4985 ToElseLoc, ToElseStmt);
4986}
4987
4988Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
4989 VarDecl *ToConditionVariable = nullptr;
4990 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4991 ToConditionVariable =
4992 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4993 if (!ToConditionVariable)
4994 return nullptr;
4995 }
4996 Expr *ToCondition = Importer.Import(S->getCond());
4997 if (!ToCondition && S->getCond())
4998 return nullptr;
4999 SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt(
5000 Importer.getToContext(), ToConditionVariable,
5001 ToCondition);
5002 Stmt *ToBody = Importer.Import(S->getBody());
5003 if (!ToBody && S->getBody())
5004 return nullptr;
5005 ToStmt->setBody(ToBody);
5006 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
5007 // Now we have to re-chain the cases.
5008 SwitchCase *LastChainedSwitchCase = nullptr;
5009 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
5010 SC = SC->getNextSwitchCase()) {
5011 SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
5012 if (!ToSC)
5013 return nullptr;
5014 if (LastChainedSwitchCase)
5015 LastChainedSwitchCase->setNextSwitchCase(ToSC);
5016 else
5017 ToStmt->setSwitchCaseList(ToSC);
5018 LastChainedSwitchCase = ToSC;
5019 }
5020 return ToStmt;
5021}
5022
5023Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
5024 VarDecl *ToConditionVariable = nullptr;
5025 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5026 ToConditionVariable =
5027 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5028 if (!ToConditionVariable)
5029 return nullptr;
5030 }
5031 Expr *ToCondition = Importer.Import(S->getCond());
5032 if (!ToCondition && S->getCond())
5033 return nullptr;
5034 Stmt *ToBody = Importer.Import(S->getBody());
5035 if (!ToBody && S->getBody())
5036 return nullptr;
5037 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
5038 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
5039 ToConditionVariable,
5040 ToCondition, ToBody,
5041 ToWhileLoc);
5042}
5043
5044Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5045 Stmt *ToBody = Importer.Import(S->getBody());
5046 if (!ToBody && S->getBody())
5047 return nullptr;
5048 Expr *ToCondition = Importer.Import(S->getCond());
5049 if (!ToCondition && S->getCond())
5050 return nullptr;
5051 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
5052 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
5053 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5054 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
5055 ToDoLoc, ToWhileLoc,
5056 ToRParenLoc);
5057}
5058
5059Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
5060 Stmt *ToInit = Importer.Import(S->getInit());
5061 if (!ToInit && S->getInit())
5062 return nullptr;
5063 Expr *ToCondition = Importer.Import(S->getCond());
5064 if (!ToCondition && S->getCond())
5065 return nullptr;
5066 VarDecl *ToConditionVariable = nullptr;
5067 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5068 ToConditionVariable =
5069 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5070 if (!ToConditionVariable)
5071 return nullptr;
5072 }
5073 Expr *ToInc = Importer.Import(S->getInc());
5074 if (!ToInc && S->getInc())
5075 return nullptr;
5076 Stmt *ToBody = Importer.Import(S->getBody());
5077 if (!ToBody && S->getBody())
5078 return nullptr;
5079 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5080 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
5081 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5082 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
5083 ToInit, ToCondition,
5084 ToConditionVariable,
5085 ToInc, ToBody,
5086 ToForLoc, ToLParenLoc,
5087 ToRParenLoc);
5088}
5089
5090Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5091 LabelDecl *ToLabel = nullptr;
5092 if (LabelDecl *FromLabel = S->getLabel()) {
5093 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
5094 if (!ToLabel)
5095 return nullptr;
5096 }
5097 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
5098 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
5099 return new (Importer.getToContext()) GotoStmt(ToLabel,
5100 ToGotoLoc, ToLabelLoc);
5101}
5102
5103Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5104 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
5105 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
5106 Expr *ToTarget = Importer.Import(S->getTarget());
5107 if (!ToTarget && S->getTarget())
5108 return nullptr;
5109 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
5110 ToTarget);
5111}
5112
5113Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5114 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
5115 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
5116}
5117
5118Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5119 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
5120 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
5121}
5122
5123Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5124 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
5125 Expr *ToRetExpr = Importer.Import(S->getRetValue());
5126 if (!ToRetExpr && S->getRetValue())
5127 return nullptr;
5128 VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate());
5129 VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
5130 if (!ToNRVOCandidate && NRVOCandidate)
5131 return nullptr;
5132 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
5133 ToNRVOCandidate);
5134}
5135
5136Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5137 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
5138 VarDecl *ToExceptionDecl = nullptr;
5139 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
5140 ToExceptionDecl =
5141 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5142 if (!ToExceptionDecl)
5143 return nullptr;
5144 }
5145 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
5146 if (!ToHandlerBlock && S->getHandlerBlock())
5147 return nullptr;
5148 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
5149 ToExceptionDecl,
5150 ToHandlerBlock);
5151}
5152
5153Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5154 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
5155 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
5156 if (!ToTryBlock && S->getTryBlock())
5157 return nullptr;
5158 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5159 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5160 CXXCatchStmt *FromHandler = S->getHandler(HI);
5161 if (Stmt *ToHandler = Importer.Import(FromHandler))
5162 ToHandlers[HI] = ToHandler;
5163 else
5164 return nullptr;
5165 }
5166 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
5167 ToHandlers);
5168}
5169
5170Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
5171 DeclStmt *ToRange =
5172 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
5173 if (!ToRange && S->getRangeStmt())
5174 return nullptr;
Richard Smith01694c32016-03-20 10:33:40 +00005175 DeclStmt *ToBegin =
5176 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
5177 if (!ToBegin && S->getBeginStmt())
5178 return nullptr;
5179 DeclStmt *ToEnd =
5180 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
5181 if (!ToEnd && S->getEndStmt())
Sean Callanan59721b32015-04-28 18:41:46 +00005182 return nullptr;
5183 Expr *ToCond = Importer.Import(S->getCond());
5184 if (!ToCond && S->getCond())
5185 return nullptr;
5186 Expr *ToInc = Importer.Import(S->getInc());
5187 if (!ToInc && S->getInc())
5188 return nullptr;
5189 DeclStmt *ToLoopVar =
5190 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
5191 if (!ToLoopVar && S->getLoopVarStmt())
5192 return nullptr;
5193 Stmt *ToBody = Importer.Import(S->getBody());
5194 if (!ToBody && S->getBody())
5195 return nullptr;
5196 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
Richard Smith9f690bd2015-10-27 06:02:45 +00005197 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00005198 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
5199 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
Richard Smith01694c32016-03-20 10:33:40 +00005200 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
Sean Callanan59721b32015-04-28 18:41:46 +00005201 ToCond, ToInc,
5202 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00005203 ToForLoc, ToCoawaitLoc,
5204 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005205}
5206
5207Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5208 Stmt *ToElem = Importer.Import(S->getElement());
5209 if (!ToElem && S->getElement())
5210 return nullptr;
5211 Expr *ToCollect = Importer.Import(S->getCollection());
5212 if (!ToCollect && S->getCollection())
5213 return nullptr;
5214 Stmt *ToBody = Importer.Import(S->getBody());
5215 if (!ToBody && S->getBody())
5216 return nullptr;
5217 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5218 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5219 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
5220 ToCollect,
5221 ToBody, ToForLoc,
5222 ToRParenLoc);
5223}
5224
5225Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5226 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
5227 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5228 VarDecl *ToExceptionDecl = nullptr;
5229 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
5230 ToExceptionDecl =
5231 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5232 if (!ToExceptionDecl)
5233 return nullptr;
5234 }
5235 Stmt *ToBody = Importer.Import(S->getCatchBody());
5236 if (!ToBody && S->getCatchBody())
5237 return nullptr;
5238 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
5239 ToRParenLoc,
5240 ToExceptionDecl,
5241 ToBody);
5242}
5243
5244Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5245 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
5246 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
5247 if (!ToAtFinallyStmt && S->getFinallyBody())
5248 return nullptr;
5249 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
5250 ToAtFinallyStmt);
5251}
5252
5253Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5254 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
5255 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
5256 if (!ToAtTryStmt && S->getTryBody())
5257 return nullptr;
5258 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5259 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5260 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
5261 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
5262 ToCatchStmts[CI] = ToCatchStmt;
5263 else
5264 return nullptr;
5265 }
5266 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
5267 if (!ToAtFinallyStmt && S->getFinallyStmt())
5268 return nullptr;
5269 return ObjCAtTryStmt::Create(Importer.getToContext(),
5270 ToAtTryLoc, ToAtTryStmt,
5271 ToCatchStmts.begin(), ToCatchStmts.size(),
5272 ToAtFinallyStmt);
5273}
5274
5275Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
5276 (ObjCAtSynchronizedStmt *S) {
5277 SourceLocation ToAtSynchronizedLoc =
5278 Importer.Import(S->getAtSynchronizedLoc());
5279 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
5280 if (!ToSynchExpr && S->getSynchExpr())
5281 return nullptr;
5282 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
5283 if (!ToSynchBody && S->getSynchBody())
5284 return nullptr;
5285 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5286 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5287}
5288
5289Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5290 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5291 Expr *ToThrow = Importer.Import(S->getThrowExpr());
5292 if (!ToThrow && S->getThrowExpr())
5293 return nullptr;
5294 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5295}
5296
5297Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5298 (ObjCAutoreleasePoolStmt *S) {
5299 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5300 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5301 if (!ToSubStmt && S->getSubStmt())
5302 return nullptr;
5303 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5304 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005305}
5306
5307//----------------------------------------------------------------------------
5308// Import Expressions
5309//----------------------------------------------------------------------------
5310Expr *ASTNodeImporter::VisitExpr(Expr *E) {
5311 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5312 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00005313 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005314}
5315
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005316Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
5317 QualType T = Importer.Import(E->getType());
5318 if (T.isNull())
5319 return nullptr;
5320
5321 Expr *SubExpr = Importer.Import(E->getSubExpr());
5322 if (!SubExpr && E->getSubExpr())
5323 return nullptr;
5324
5325 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
5326 if (!TInfo)
5327 return nullptr;
5328
5329 return new (Importer.getToContext()) VAArgExpr(
5330 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
5331 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
5332}
5333
5334
5335Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
5336 QualType T = Importer.Import(E->getType());
5337 if (T.isNull())
5338 return nullptr;
5339
5340 return new (Importer.getToContext()) GNUNullExpr(
5341 T, Importer.Import(E->getExprLoc()));
5342}
5343
5344Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
5345 QualType T = Importer.Import(E->getType());
5346 if (T.isNull())
5347 return nullptr;
5348
5349 StringLiteral *SL = cast_or_null<StringLiteral>(
5350 Importer.Import(E->getFunctionName()));
5351 if (!SL && E->getFunctionName())
5352 return nullptr;
5353
5354 return new (Importer.getToContext()) PredefinedExpr(
5355 Importer.Import(E->getExprLoc()), T, E->getIdentType(), SL);
5356}
5357
Douglas Gregor52f820e2010-02-19 01:17:02 +00005358Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00005359 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
5360 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005361 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005362
Craig Topper36250ad2014-05-12 05:36:57 +00005363 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005364 if (E->getDecl() != E->getFoundDecl()) {
5365 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5366 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00005367 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005368 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00005369
5370 QualType T = Importer.Import(E->getType());
5371 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005372 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005373
5374 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5375 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00005376 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005377 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00005378 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005379 Importer.Import(E->getLocation()),
5380 T, E->getValueKind(),
5381 FoundD,
Craig Topper36250ad2014-05-12 05:36:57 +00005382 /*FIXME:TemplateArgs=*/nullptr);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005383 if (E->hadMultipleCandidates())
5384 DRE->setHadMultipleCandidates(true);
5385 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00005386}
5387
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005388Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
5389 QualType T = Importer.Import(E->getType());
5390 if (T.isNull())
5391 return NULL;
5392
5393 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
5394}
5395
5396ASTNodeImporter::Designator
5397ASTNodeImporter::ImportDesignator(const Designator &D) {
5398 if (D.isFieldDesignator()) {
5399 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
5400 // Caller checks for import error
5401 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
5402 Importer.Import(D.getFieldLoc()));
5403 }
5404 if (D.isArrayDesignator())
5405 return Designator(D.getFirstExprIndex(),
5406 Importer.Import(D.getLBracketLoc()),
5407 Importer.Import(D.getRBracketLoc()));
5408
5409 assert(D.isArrayRangeDesignator());
5410 return Designator(D.getFirstExprIndex(),
5411 Importer.Import(D.getLBracketLoc()),
5412 Importer.Import(D.getEllipsisLoc()),
5413 Importer.Import(D.getRBracketLoc()));
5414}
5415
5416
5417Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
5418 Expr *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
5419 if (!Init)
5420 return nullptr;
5421
5422 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
5423 // List elements from the second, the first is Init itself
5424 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
5425 if (Expr *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
5426 IndexExprs[I - 1] = Arg;
5427 else
5428 return nullptr;
5429 }
5430
5431 SmallVector<Designator, 4> Designators(DIE->size());
David Majnemerf7e36092016-06-23 00:15:04 +00005432 llvm::transform(DIE->designators(), Designators.begin(),
5433 [this](const Designator &D) -> Designator {
5434 return ImportDesignator(D);
5435 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005436
David Majnemerf7e36092016-06-23 00:15:04 +00005437 for (const Designator &D : DIE->designators())
5438 if (D.isFieldDesignator() && !D.getFieldName())
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005439 return nullptr;
5440
5441 return DesignatedInitExpr::Create(
David Majnemerf7e36092016-06-23 00:15:04 +00005442 Importer.getToContext(), Designators,
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005443 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
5444 DIE->usesGNUSyntax(), Init);
5445}
5446
5447Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
5448 QualType T = Importer.Import(E->getType());
5449 if (T.isNull())
5450 return nullptr;
5451
5452 return new (Importer.getToContext())
5453 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
5454}
5455
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005456Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5457 QualType T = Importer.Import(E->getType());
5458 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005459 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005460
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005461 return IntegerLiteral::Create(Importer.getToContext(),
5462 E->getValue(), T,
5463 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005464}
5465
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005466Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
5467 QualType T = Importer.Import(E->getType());
5468 if (T.isNull())
5469 return nullptr;
5470
5471 return FloatingLiteral::Create(Importer.getToContext(),
5472 E->getValue(), E->isExact(), T,
5473 Importer.Import(E->getLocation()));
5474}
5475
Douglas Gregor623421d2010-02-18 02:21:22 +00005476Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5477 QualType T = Importer.Import(E->getType());
5478 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005479 return nullptr;
5480
Douglas Gregorfb65e592011-07-27 05:40:30 +00005481 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5482 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005483 Importer.Import(E->getLocation()));
5484}
5485
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005486Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
5487 QualType T = Importer.Import(E->getType());
5488 if (T.isNull())
5489 return nullptr;
5490
5491 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
5492 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
5493
5494 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
5495 E->getKind(), E->isPascal(), T,
5496 Locations.data(), Locations.size());
5497}
5498
5499Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
5500 QualType T = Importer.Import(E->getType());
5501 if (T.isNull())
5502 return nullptr;
5503
5504 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
5505 if (!TInfo)
5506 return nullptr;
5507
5508 Expr *Init = Importer.Import(E->getInitializer());
5509 if (!Init)
5510 return nullptr;
5511
5512 return new (Importer.getToContext()) CompoundLiteralExpr(
5513 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
5514 Init, E->isFileScope());
5515}
5516
5517Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
5518 QualType T = Importer.Import(E->getType());
5519 if (T.isNull())
5520 return nullptr;
5521
5522 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
5523 if (ImportArrayChecked(
5524 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
5525 Exprs.begin()))
5526 return nullptr;
5527
5528 return new (Importer.getToContext()) AtomicExpr(
5529 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
5530 Importer.Import(E->getRParenLoc()));
5531}
5532
5533Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
5534 QualType T = Importer.Import(E->getType());
5535 if (T.isNull())
5536 return nullptr;
5537
5538 LabelDecl *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
5539 if (!ToLabel)
5540 return nullptr;
5541
5542 return new (Importer.getToContext()) AddrLabelExpr(
5543 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
5544 ToLabel, T);
5545}
5546
Douglas Gregorc74247e2010-02-19 01:07:06 +00005547Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5548 Expr *SubExpr = Importer.Import(E->getSubExpr());
5549 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005550 return nullptr;
5551
Douglas Gregorc74247e2010-02-19 01:07:06 +00005552 return new (Importer.getToContext())
5553 ParenExpr(Importer.Import(E->getLParen()),
5554 Importer.Import(E->getRParen()),
5555 SubExpr);
5556}
5557
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005558Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
5559 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
5560 if (ImportArrayChecked(
5561 E->getExprs(), E->getExprs() + E->getNumExprs(), Exprs.begin()))
5562 return nullptr;
5563
5564 return new (Importer.getToContext()) ParenListExpr(
5565 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
5566 Exprs, Importer.Import(E->getLParenLoc()));
5567}
5568
5569Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
5570 QualType T = Importer.Import(E->getType());
5571 if (T.isNull())
5572 return nullptr;
5573
5574 CompoundStmt *ToSubStmt = cast_or_null<CompoundStmt>(
5575 Importer.Import(E->getSubStmt()));
5576 if (!ToSubStmt && E->getSubStmt())
5577 return nullptr;
5578
5579 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
5580 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
5581}
5582
Douglas Gregorc74247e2010-02-19 01:07:06 +00005583Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5584 QualType T = Importer.Import(E->getType());
5585 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005586 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005587
5588 Expr *SubExpr = Importer.Import(E->getSubExpr());
5589 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005590 return nullptr;
5591
Douglas Gregorc74247e2010-02-19 01:07:06 +00005592 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005593 T, E->getValueKind(),
5594 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00005595 Importer.Import(E->getOperatorLoc()));
5596}
5597
Peter Collingbournee190dee2011-03-11 19:24:49 +00005598Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
5599 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005600 QualType ResultType = Importer.Import(E->getType());
5601
5602 if (E->isArgumentType()) {
5603 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5604 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005605 return nullptr;
5606
Peter Collingbournee190dee2011-03-11 19:24:49 +00005607 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5608 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005609 Importer.Import(E->getOperatorLoc()),
5610 Importer.Import(E->getRParenLoc()));
5611 }
5612
5613 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5614 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005615 return nullptr;
5616
Peter Collingbournee190dee2011-03-11 19:24:49 +00005617 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5618 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005619 Importer.Import(E->getOperatorLoc()),
5620 Importer.Import(E->getRParenLoc()));
5621}
5622
Douglas Gregorc74247e2010-02-19 01:07:06 +00005623Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5624 QualType T = Importer.Import(E->getType());
5625 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005626 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005627
5628 Expr *LHS = Importer.Import(E->getLHS());
5629 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005630 return nullptr;
5631
Douglas Gregorc74247e2010-02-19 01:07:06 +00005632 Expr *RHS = Importer.Import(E->getRHS());
5633 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005634 return nullptr;
5635
Douglas Gregorc74247e2010-02-19 01:07:06 +00005636 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005637 T, E->getValueKind(),
5638 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005639 Importer.Import(E->getOperatorLoc()),
5640 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005641}
5642
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005643Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
5644 QualType T = Importer.Import(E->getType());
5645 if (T.isNull())
5646 return nullptr;
5647
5648 Expr *ToLHS = Importer.Import(E->getLHS());
5649 if (!ToLHS)
5650 return nullptr;
5651
5652 Expr *ToRHS = Importer.Import(E->getRHS());
5653 if (!ToRHS)
5654 return nullptr;
5655
5656 Expr *ToCond = Importer.Import(E->getCond());
5657 if (!ToCond)
5658 return nullptr;
5659
5660 return new (Importer.getToContext()) ConditionalOperator(
5661 ToCond, Importer.Import(E->getQuestionLoc()),
5662 ToLHS, Importer.Import(E->getColonLoc()),
5663 ToRHS, T, E->getValueKind(), E->getObjectKind());
5664}
5665
5666Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
5667 BinaryConditionalOperator *E) {
5668 QualType T = Importer.Import(E->getType());
5669 if (T.isNull())
5670 return nullptr;
5671
5672 Expr *Common = Importer.Import(E->getCommon());
5673 if (!Common)
5674 return nullptr;
5675
5676 Expr *Cond = Importer.Import(E->getCond());
5677 if (!Cond)
5678 return nullptr;
5679
5680 OpaqueValueExpr *OpaqueValue = cast_or_null<OpaqueValueExpr>(
5681 Importer.Import(E->getOpaqueValue()));
5682 if (!OpaqueValue)
5683 return nullptr;
5684
5685 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
5686 if (!TrueExpr)
5687 return nullptr;
5688
5689 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
5690 if (!FalseExpr)
5691 return nullptr;
5692
5693 return new (Importer.getToContext()) BinaryConditionalOperator(
5694 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
5695 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
5696 T, E->getValueKind(), E->getObjectKind());
5697}
5698
5699Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
5700 QualType T = Importer.Import(E->getType());
5701 if (T.isNull())
5702 return nullptr;
5703
5704 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
5705 if (!SourceExpr && E->getSourceExpr())
5706 return nullptr;
5707
5708 return new (Importer.getToContext()) OpaqueValueExpr(
5709 Importer.Import(E->getExprLoc()), T, E->getValueKind(),
5710 E->getObjectKind(), SourceExpr);
5711}
5712
Douglas Gregorc74247e2010-02-19 01:07:06 +00005713Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5714 QualType T = Importer.Import(E->getType());
5715 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005716 return nullptr;
5717
Douglas Gregorc74247e2010-02-19 01:07:06 +00005718 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5719 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005720 return nullptr;
5721
Douglas Gregorc74247e2010-02-19 01:07:06 +00005722 QualType CompResultType = Importer.Import(E->getComputationResultType());
5723 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005724 return nullptr;
5725
Douglas Gregorc74247e2010-02-19 01:07:06 +00005726 Expr *LHS = Importer.Import(E->getLHS());
5727 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005728 return nullptr;
5729
Douglas Gregorc74247e2010-02-19 01:07:06 +00005730 Expr *RHS = Importer.Import(E->getRHS());
5731 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005732 return nullptr;
5733
Douglas Gregorc74247e2010-02-19 01:07:06 +00005734 return new (Importer.getToContext())
5735 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005736 T, E->getValueKind(),
5737 E->getObjectKind(),
5738 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005739 Importer.Import(E->getOperatorLoc()),
5740 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005741}
5742
Benjamin Kramer8aef5962011-03-26 12:38:21 +00005743static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallcf142162010-08-07 06:22:56 +00005744 if (E->path_empty()) return false;
5745
5746 // TODO: import cast paths
5747 return true;
5748}
5749
Douglas Gregor98c10182010-02-12 22:17:39 +00005750Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5751 QualType T = Importer.Import(E->getType());
5752 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005753 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005754
5755 Expr *SubExpr = Importer.Import(E->getSubExpr());
5756 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005757 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005758
5759 CXXCastPath BasePath;
5760 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005761 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005762
5763 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005764 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005765}
5766
Douglas Gregor5481d322010-02-19 01:32:14 +00005767Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
5768 QualType T = Importer.Import(E->getType());
5769 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005770 return nullptr;
5771
Douglas Gregor5481d322010-02-19 01:32:14 +00005772 Expr *SubExpr = Importer.Import(E->getSubExpr());
5773 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005774 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00005775
5776 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5777 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00005778 return nullptr;
5779
John McCallcf142162010-08-07 06:22:56 +00005780 CXXCastPath BasePath;
5781 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005782 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005783
John McCall7decc9e2010-11-18 06:31:45 +00005784 return CStyleCastExpr::Create(Importer.getToContext(), T,
5785 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00005786 SubExpr, &BasePath, TInfo,
5787 Importer.Import(E->getLParenLoc()),
5788 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00005789}
5790
Sean Callanan59721b32015-04-28 18:41:46 +00005791Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
5792 QualType T = Importer.Import(E->getType());
5793 if (T.isNull())
5794 return nullptr;
5795
5796 CXXConstructorDecl *ToCCD =
Sean Callanandd2c1742016-05-16 20:48:03 +00005797 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
Richard Smithc2bebe92016-05-11 20:37:46 +00005798 if (!ToCCD)
Sean Callanan59721b32015-04-28 18:41:46 +00005799 return nullptr;
5800
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005801 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
5802 if (ImportArrayChecked(E->getArgs(), E->getArgs() + E->getNumArgs(),
5803 ToArgs.begin()))
Sean Callanan8bca9962016-03-28 21:43:01 +00005804 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005805
5806 return CXXConstructExpr::Create(Importer.getToContext(), T,
5807 Importer.Import(E->getLocation()),
Richard Smithc83bf822016-06-10 00:58:19 +00005808 ToCCD, E->isElidable(),
Sean Callanan59721b32015-04-28 18:41:46 +00005809 ToArgs, E->hadMultipleCandidates(),
5810 E->isListInitialization(),
5811 E->isStdInitListInitialization(),
5812 E->requiresZeroInitialization(),
5813 E->getConstructionKind(),
5814 Importer.Import(E->getParenOrBraceRange()));
5815}
5816
Sean Callanan8bca9962016-03-28 21:43:01 +00005817Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
5818 QualType T = Importer.Import(E->getType());
5819 if (T.isNull())
5820 return nullptr;
5821
5822 Expr *ToFn = Importer.Import(E->getCallee());
5823 if (!ToFn)
5824 return nullptr;
5825
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005826 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Sean Callanan8bca9962016-03-28 21:43:01 +00005827
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005828 if (ImportArrayChecked(E->arg_begin(), E->arg_end(), ToArgs.begin()))
Sean Callanan8bca9962016-03-28 21:43:01 +00005829 return nullptr;
5830
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005831 return new (Importer.getToContext()) CXXMemberCallExpr(
5832 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
5833 Importer.Import(E->getRParenLoc()));
Sean Callanan8bca9962016-03-28 21:43:01 +00005834}
5835
5836Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
5837 QualType T = Importer.Import(E->getType());
5838 if (T.isNull())
5839 return nullptr;
5840
5841 return new (Importer.getToContext())
5842 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
5843}
5844
5845Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
5846 QualType T = Importer.Import(E->getType());
5847 if (T.isNull())
5848 return nullptr;
5849
5850 return new (Importer.getToContext())
5851 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
5852}
5853
5854
Sean Callanan59721b32015-04-28 18:41:46 +00005855Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
5856 QualType T = Importer.Import(E->getType());
5857 if (T.isNull())
5858 return nullptr;
5859
5860 Expr *ToBase = Importer.Import(E->getBase());
5861 if (!ToBase && E->getBase())
5862 return nullptr;
5863
5864 ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
5865 if (!ToMember && E->getMemberDecl())
5866 return nullptr;
5867
5868 DeclAccessPair ToFoundDecl = DeclAccessPair::make(
5869 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
5870 E->getFoundDecl().getAccess());
5871
5872 DeclarationNameInfo ToMemberNameInfo(
5873 Importer.Import(E->getMemberNameInfo().getName()),
5874 Importer.Import(E->getMemberNameInfo().getLoc()));
5875
5876 if (E->hasExplicitTemplateArgs()) {
5877 return nullptr; // FIXME: handle template arguments
5878 }
5879
5880 return MemberExpr::Create(Importer.getToContext(), ToBase,
5881 E->isArrow(),
5882 Importer.Import(E->getOperatorLoc()),
5883 Importer.Import(E->getQualifierLoc()),
5884 Importer.Import(E->getTemplateKeywordLoc()),
5885 ToMember, ToFoundDecl, ToMemberNameInfo,
5886 nullptr, T, E->getValueKind(),
5887 E->getObjectKind());
5888}
5889
5890Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
5891 QualType T = Importer.Import(E->getType());
5892 if (T.isNull())
5893 return nullptr;
5894
5895 Expr *ToCallee = Importer.Import(E->getCallee());
5896 if (!ToCallee && E->getCallee())
5897 return nullptr;
5898
5899 unsigned NumArgs = E->getNumArgs();
5900
5901 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
5902
5903 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) {
5904 Expr *FromArg = E->getArg(ai);
5905 Expr *ToArg = Importer.Import(FromArg);
5906 if (!ToArg)
5907 return nullptr;
5908 ToArgs[ai] = ToArg;
5909 }
5910
5911 Expr **ToArgs_Copied = new (Importer.getToContext())
5912 Expr*[NumArgs];
5913
5914 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
5915 ToArgs_Copied[ai] = ToArgs[ai];
5916
5917 return new (Importer.getToContext())
5918 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00005919 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00005920 Importer.Import(E->getRParenLoc()));
5921}
5922
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005923Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
5924 QualType T = Importer.Import(ILE->getType());
Sean Callanan8bca9962016-03-28 21:43:01 +00005925 if (T.isNull())
5926 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00005927
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005928 llvm::SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
5929 if (ImportArrayChecked(
5930 ILE->getInits(), ILE->getInits() + ILE->getNumInits(), Exprs.begin()))
Sean Callanan8bca9962016-03-28 21:43:01 +00005931 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00005932
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005933 ASTContext &ToCtx = Importer.getToContext();
5934 InitListExpr *To = new (ToCtx) InitListExpr(
5935 ToCtx, Importer.Import(ILE->getLBraceLoc()),
5936 Exprs, Importer.Import(ILE->getLBraceLoc()));
5937 To->setType(T);
5938
5939 if (ILE->hasArrayFiller()) {
5940 Expr *Filler = Importer.Import(ILE->getArrayFiller());
5941 if (!Filler)
5942 return nullptr;
5943 To->setArrayFiller(Filler);
5944 }
5945
5946 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
5947 FieldDecl *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
5948 if (!ToFD)
5949 return nullptr;
5950 To->setInitializedFieldInUnion(ToFD);
5951 }
5952
5953 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
5954 InitListExpr *ToSyntForm = cast_or_null<InitListExpr>(
5955 Importer.Import(SyntForm));
5956 if (!ToSyntForm)
5957 return nullptr;
5958 To->setSyntacticForm(ToSyntForm);
5959 }
5960
5961 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
5962 To->setValueDependent(ILE->isValueDependent());
5963 To->setInstantiationDependent(ILE->isInstantiationDependent());
5964
5965 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00005966}
5967
Sean Callanandd2c1742016-05-16 20:48:03 +00005968Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
5969 FieldDecl *ToField = llvm::dyn_cast_or_null<FieldDecl>(
5970 Importer.Import(DIE->getField()));
5971 if (!ToField && DIE->getField())
5972 return nullptr;
5973
5974 return CXXDefaultInitExpr::Create(
5975 Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField);
5976}
5977
5978Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
5979 QualType ToType = Importer.Import(E->getType());
5980 if (ToType.isNull() && !E->getType().isNull())
5981 return nullptr;
5982 ExprValueKind VK = E->getValueKind();
5983 CastKind CK = E->getCastKind();
5984 Expr *ToOp = Importer.Import(E->getSubExpr());
5985 if (!ToOp && E->getSubExpr())
5986 return nullptr;
5987 CXXCastPath BasePath;
5988 if (ImportCastPath(E, BasePath))
5989 return nullptr;
5990 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten());
5991 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc());
5992 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc());
5993 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets());
5994
5995 if (isa<CXXStaticCastExpr>(E)) {
5996 return CXXStaticCastExpr::Create(
5997 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
5998 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
5999 } else if (isa<CXXDynamicCastExpr>(E)) {
6000 return CXXDynamicCastExpr::Create(
6001 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6002 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6003 } else if (isa<CXXReinterpretCastExpr>(E)) {
6004 return CXXReinterpretCastExpr::Create(
6005 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6006 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6007 } else {
6008 return nullptr;
6009 }
6010}
6011
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006012ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00006013 ASTContext &FromContext, FileManager &FromFileManager,
6014 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00006015 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00006016 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Richard Smith5bb4cdf2012-12-20 02:22:15 +00006017 Minimal(MinimalImport), LastDiagFromFrom(false)
Douglas Gregor0a791672011-01-18 03:11:38 +00006018{
Douglas Gregor62d311f2010-02-09 19:21:46 +00006019 ImportedDecls[FromContext.getTranslationUnitDecl()]
6020 = ToContext.getTranslationUnitDecl();
6021}
6022
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +00006023ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00006024
6025QualType ASTImporter::Import(QualType FromT) {
6026 if (FromT.isNull())
6027 return QualType();
John McCall424cec92011-01-19 06:33:43 +00006028
6029 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00006030
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006031 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00006032 llvm::DenseMap<const Type *, const Type *>::iterator Pos
6033 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006034 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00006035 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006036
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006037 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00006038 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00006039 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00006040 if (ToT.isNull())
6041 return ToT;
6042
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006043 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00006044 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006045
John McCall424cec92011-01-19 06:33:43 +00006046 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006047}
6048
Douglas Gregor62d311f2010-02-09 19:21:46 +00006049TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006050 if (!FromTSI)
6051 return FromTSI;
6052
6053 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00006054 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006055 QualType T = Import(FromTSI->getType());
6056 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006057 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006058
6059 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00006060 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00006061}
6062
Sean Callanan59721b32015-04-28 18:41:46 +00006063Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
6064 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
6065 if (Pos != ImportedDecls.end()) {
6066 Decl *ToD = Pos->second;
6067 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
6068 return ToD;
6069 } else {
6070 return nullptr;
6071 }
6072}
6073
Douglas Gregor62d311f2010-02-09 19:21:46 +00006074Decl *ASTImporter::Import(Decl *FromD) {
6075 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00006076 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006077
Douglas Gregord451ea92011-07-29 23:31:30 +00006078 ASTNodeImporter Importer(*this);
6079
Douglas Gregor62d311f2010-02-09 19:21:46 +00006080 // Check whether we've already imported this declaration.
6081 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00006082 if (Pos != ImportedDecls.end()) {
6083 Decl *ToD = Pos->second;
6084 Importer.ImportDefinitionIfNeeded(FromD, ToD);
6085 return ToD;
6086 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00006087
6088 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00006089 Decl *ToD = Importer.Visit(FromD);
6090 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00006091 return nullptr;
6092
Douglas Gregor62d311f2010-02-09 19:21:46 +00006093 // Record the imported declaration.
6094 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00006095
6096 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
6097 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00006098 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00006099 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00006100 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00006101 // When we've finished transforming a typedef, see whether it was the
6102 // typedef for an anonymous tag.
Craig Topper2341c0d2013-07-04 03:08:24 +00006103 for (SmallVectorImpl<TagDecl *>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00006104 FromTag = AnonTagsWithPendingTypedefs.begin(),
6105 FromTagEnd = AnonTagsWithPendingTypedefs.end();
6106 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00006107 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00006108 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
6109 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00006110 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00006111 AnonTagsWithPendingTypedefs.erase(FromTag);
6112 break;
6113 }
6114 }
6115 }
6116 }
6117
Douglas Gregor62d311f2010-02-09 19:21:46 +00006118 return ToD;
6119}
6120
6121DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
6122 if (!FromDC)
6123 return FromDC;
6124
Douglas Gregor95d82832012-01-24 18:36:04 +00006125 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00006126 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00006127 return nullptr;
6128
Douglas Gregor2e15c842012-02-01 21:00:38 +00006129 // When we're using a record/enum/Objective-C class/protocol as a context, we
6130 // need it to have a definition.
6131 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00006132 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006133 if (ToRecord->isCompleteDefinition()) {
6134 // Do nothing.
6135 } else if (FromRecord->isCompleteDefinition()) {
6136 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
6137 ASTNodeImporter::IDK_Basic);
6138 } else {
6139 CompleteDecl(ToRecord);
6140 }
6141 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
6142 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
6143 if (ToEnum->isCompleteDefinition()) {
6144 // Do nothing.
6145 } else if (FromEnum->isCompleteDefinition()) {
6146 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
6147 ASTNodeImporter::IDK_Basic);
6148 } else {
6149 CompleteDecl(ToEnum);
6150 }
6151 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
6152 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
6153 if (ToClass->getDefinition()) {
6154 // Do nothing.
6155 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
6156 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
6157 ASTNodeImporter::IDK_Basic);
6158 } else {
6159 CompleteDecl(ToClass);
6160 }
6161 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
6162 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
6163 if (ToProto->getDefinition()) {
6164 // Do nothing.
6165 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
6166 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
6167 ASTNodeImporter::IDK_Basic);
6168 } else {
6169 CompleteDecl(ToProto);
6170 }
Douglas Gregor95d82832012-01-24 18:36:04 +00006171 }
6172
6173 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006174}
6175
6176Expr *ASTImporter::Import(Expr *FromE) {
6177 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00006178 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006179
6180 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
6181}
6182
6183Stmt *ASTImporter::Import(Stmt *FromS) {
6184 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00006185 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006186
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006187 // Check whether we've already imported this declaration.
6188 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
6189 if (Pos != ImportedStmts.end())
6190 return Pos->second;
6191
6192 // Import the type
6193 ASTNodeImporter Importer(*this);
6194 Stmt *ToS = Importer.Visit(FromS);
6195 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00006196 return nullptr;
6197
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006198 // Record the imported declaration.
6199 ImportedStmts[FromS] = ToS;
6200 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006201}
6202
6203NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
6204 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00006205 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006206
Douglas Gregor90ebf252011-04-27 16:48:40 +00006207 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
6208
6209 switch (FromNNS->getKind()) {
6210 case NestedNameSpecifier::Identifier:
6211 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
6212 return NestedNameSpecifier::Create(ToContext, prefix, II);
6213 }
Craig Topper36250ad2014-05-12 05:36:57 +00006214 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006215
6216 case NestedNameSpecifier::Namespace:
6217 if (NamespaceDecl *NS =
6218 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
6219 return NestedNameSpecifier::Create(ToContext, prefix, NS);
6220 }
Craig Topper36250ad2014-05-12 05:36:57 +00006221 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006222
6223 case NestedNameSpecifier::NamespaceAlias:
6224 if (NamespaceAliasDecl *NSAD =
6225 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
6226 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
6227 }
Craig Topper36250ad2014-05-12 05:36:57 +00006228 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006229
6230 case NestedNameSpecifier::Global:
6231 return NestedNameSpecifier::GlobalSpecifier(ToContext);
6232
Nikola Smiljanic67860242014-09-26 00:28:20 +00006233 case NestedNameSpecifier::Super:
6234 if (CXXRecordDecl *RD =
6235 cast<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
6236 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
6237 }
6238 return nullptr;
6239
Douglas Gregor90ebf252011-04-27 16:48:40 +00006240 case NestedNameSpecifier::TypeSpec:
6241 case NestedNameSpecifier::TypeSpecWithTemplate: {
6242 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
6243 if (!T.isNull()) {
6244 bool bTemplate = FromNNS->getKind() ==
6245 NestedNameSpecifier::TypeSpecWithTemplate;
6246 return NestedNameSpecifier::Create(ToContext, prefix,
6247 bTemplate, T.getTypePtr());
6248 }
6249 }
Craig Topper36250ad2014-05-12 05:36:57 +00006250 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006251 }
6252
6253 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00006254}
6255
Douglas Gregor14454802011-02-25 02:25:35 +00006256NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
6257 // FIXME: Implement!
6258 return NestedNameSpecifierLoc();
6259}
6260
Douglas Gregore2e50d332010-12-01 01:36:18 +00006261TemplateName ASTImporter::Import(TemplateName From) {
6262 switch (From.getKind()) {
6263 case TemplateName::Template:
6264 if (TemplateDecl *ToTemplate
6265 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
6266 return TemplateName(ToTemplate);
6267
6268 return TemplateName();
6269
6270 case TemplateName::OverloadedTemplate: {
6271 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
6272 UnresolvedSet<2> ToTemplates;
6273 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
6274 E = FromStorage->end();
6275 I != E; ++I) {
6276 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
6277 ToTemplates.addDecl(To);
6278 else
6279 return TemplateName();
6280 }
6281 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
6282 ToTemplates.end());
6283 }
6284
6285 case TemplateName::QualifiedTemplate: {
6286 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
6287 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
6288 if (!Qualifier)
6289 return TemplateName();
6290
6291 if (TemplateDecl *ToTemplate
6292 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
6293 return ToContext.getQualifiedTemplateName(Qualifier,
6294 QTN->hasTemplateKeyword(),
6295 ToTemplate);
6296
6297 return TemplateName();
6298 }
6299
6300 case TemplateName::DependentTemplate: {
6301 DependentTemplateName *DTN = From.getAsDependentTemplateName();
6302 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
6303 if (!Qualifier)
6304 return TemplateName();
6305
6306 if (DTN->isIdentifier()) {
6307 return ToContext.getDependentTemplateName(Qualifier,
6308 Import(DTN->getIdentifier()));
6309 }
6310
6311 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
6312 }
John McCalld9dfe3a2011-06-30 08:33:18 +00006313
6314 case TemplateName::SubstTemplateTemplateParm: {
6315 SubstTemplateTemplateParmStorage *subst
6316 = From.getAsSubstTemplateTemplateParm();
6317 TemplateTemplateParmDecl *param
6318 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
6319 if (!param)
6320 return TemplateName();
6321
6322 TemplateName replacement = Import(subst->getReplacement());
6323 if (replacement.isNull()) return TemplateName();
6324
6325 return ToContext.getSubstTemplateTemplateParm(param, replacement);
6326 }
Douglas Gregor5590be02011-01-15 06:45:20 +00006327
6328 case TemplateName::SubstTemplateTemplateParmPack: {
6329 SubstTemplateTemplateParmPackStorage *SubstPack
6330 = From.getAsSubstTemplateTemplateParmPack();
6331 TemplateTemplateParmDecl *Param
6332 = cast_or_null<TemplateTemplateParmDecl>(
6333 Import(SubstPack->getParameterPack()));
6334 if (!Param)
6335 return TemplateName();
6336
6337 ASTNodeImporter Importer(*this);
6338 TemplateArgument ArgPack
6339 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
6340 if (ArgPack.isNull())
6341 return TemplateName();
6342
6343 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
6344 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00006345 }
6346
6347 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00006348}
6349
Douglas Gregor62d311f2010-02-09 19:21:46 +00006350SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
6351 if (FromLoc.isInvalid())
6352 return SourceLocation();
6353
Douglas Gregor811663e2010-02-10 00:15:17 +00006354 SourceManager &FromSM = FromContext.getSourceManager();
6355
6356 // For now, map everything down to its spelling location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00006357 // don't have to import macro expansions.
6358 // FIXME: Import macro expansions!
Douglas Gregor811663e2010-02-10 00:15:17 +00006359 FromLoc = FromSM.getSpellingLoc(FromLoc);
6360 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
6361 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00006362 FileID ToFileID = Import(Decomposed.first);
6363 if (ToFileID.isInvalid())
6364 return SourceLocation();
Sean Callanan59721b32015-04-28 18:41:46 +00006365 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
6366 .getLocWithOffset(Decomposed.second);
6367 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006368}
6369
6370SourceRange ASTImporter::Import(SourceRange FromRange) {
6371 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
6372}
6373
Douglas Gregor811663e2010-02-10 00:15:17 +00006374FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00006375 llvm::DenseMap<FileID, FileID>::iterator Pos
6376 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00006377 if (Pos != ImportedFileIDs.end())
6378 return Pos->second;
6379
6380 SourceManager &FromSM = FromContext.getSourceManager();
6381 SourceManager &ToSM = ToContext.getSourceManager();
6382 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00006383 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00006384
6385 // Include location of this file.
6386 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
6387
6388 // Map the FileID for to the "to" source manager.
6389 FileID ToID;
6390 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan25d34af2015-04-30 00:44:21 +00006391 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
Douglas Gregor811663e2010-02-10 00:15:17 +00006392 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
6393 // disk again
6394 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
6395 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00006396 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Sean Callanan238d8972014-12-10 01:26:39 +00006397 if (!Entry)
6398 return FileID();
Douglas Gregor811663e2010-02-10 00:15:17 +00006399 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
6400 FromSLoc.getFile().getFileCharacteristic());
6401 } else {
6402 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006403 const llvm::MemoryBuffer *
6404 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00006405 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00006406 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00006407 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00006408 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00006409 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00006410 }
6411
6412
Sebastian Redl99219f12010-09-30 01:03:06 +00006413 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00006414 return ToID;
6415}
6416
Sean Callanandd2c1742016-05-16 20:48:03 +00006417CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
6418 Expr *ToExpr = Import(From->getInit());
6419 if (!ToExpr && From->getInit())
6420 return nullptr;
6421
6422 if (From->isBaseInitializer()) {
6423 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
6424 if (!ToTInfo && From->getTypeSourceInfo())
6425 return nullptr;
6426
6427 return new (ToContext) CXXCtorInitializer(
6428 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
6429 ToExpr, Import(From->getRParenLoc()),
6430 From->isPackExpansion() ? Import(From->getEllipsisLoc())
6431 : SourceLocation());
6432 } else if (From->isMemberInitializer()) {
6433 FieldDecl *ToField =
6434 llvm::cast_or_null<FieldDecl>(Import(From->getMember()));
6435 if (!ToField && From->getMember())
6436 return nullptr;
6437
6438 return new (ToContext) CXXCtorInitializer(
6439 ToContext, ToField, Import(From->getMemberLocation()),
6440 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
6441 } else if (From->isIndirectMemberInitializer()) {
6442 IndirectFieldDecl *ToIField = llvm::cast_or_null<IndirectFieldDecl>(
6443 Import(From->getIndirectMember()));
6444 if (!ToIField && From->getIndirectMember())
6445 return nullptr;
6446
6447 return new (ToContext) CXXCtorInitializer(
6448 ToContext, ToIField, Import(From->getMemberLocation()),
6449 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
6450 } else if (From->isDelegatingInitializer()) {
6451 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
6452 if (!ToTInfo && From->getTypeSourceInfo())
6453 return nullptr;
6454
6455 return new (ToContext)
6456 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
6457 ToExpr, Import(From->getRParenLoc()));
6458 } else if (unsigned NumArrayIndices = From->getNumArrayIndices()) {
6459 FieldDecl *ToField =
6460 llvm::cast_or_null<FieldDecl>(Import(From->getMember()));
6461 if (!ToField && From->getMember())
6462 return nullptr;
6463
6464 SmallVector<VarDecl *, 4> ToAIs(NumArrayIndices);
6465
6466 for (unsigned AII = 0; AII < NumArrayIndices; ++AII) {
6467 VarDecl *ToArrayIndex =
6468 dyn_cast_or_null<VarDecl>(Import(From->getArrayIndex(AII)));
6469 if (!ToArrayIndex && From->getArrayIndex(AII))
6470 return nullptr;
6471 }
6472
6473 return CXXCtorInitializer::Create(
6474 ToContext, ToField, Import(From->getMemberLocation()),
6475 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()),
6476 ToAIs.data(), NumArrayIndices);
6477 } else {
6478 return nullptr;
6479 }
6480}
6481
6482
Douglas Gregor0a791672011-01-18 03:11:38 +00006483void ASTImporter::ImportDefinition(Decl *From) {
6484 Decl *To = Import(From);
6485 if (!To)
6486 return;
6487
6488 if (DeclContext *FromDC = cast<DeclContext>(From)) {
6489 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00006490
6491 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
6492 if (!ToRecord->getDefinition()) {
6493 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00006494 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00006495 return;
6496 }
6497 }
Douglas Gregord451ea92011-07-29 23:31:30 +00006498
6499 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
6500 if (!ToEnum->getDefinition()) {
6501 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006502 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00006503 return;
6504 }
6505 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00006506
6507 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
6508 if (!ToIFace->getDefinition()) {
6509 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006510 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00006511 return;
6512 }
6513 }
Douglas Gregord451ea92011-07-29 23:31:30 +00006514
Douglas Gregor2aa53772012-01-24 17:42:07 +00006515 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
6516 if (!ToProto->getDefinition()) {
6517 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006518 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00006519 return;
6520 }
6521 }
6522
Douglas Gregor0a791672011-01-18 03:11:38 +00006523 Importer.ImportDeclContext(FromDC, true);
6524 }
6525}
6526
Douglas Gregor96e578d2010-02-05 17:54:41 +00006527DeclarationName ASTImporter::Import(DeclarationName FromName) {
6528 if (!FromName)
6529 return DeclarationName();
6530
6531 switch (FromName.getNameKind()) {
6532 case DeclarationName::Identifier:
6533 return Import(FromName.getAsIdentifierInfo());
6534
6535 case DeclarationName::ObjCZeroArgSelector:
6536 case DeclarationName::ObjCOneArgSelector:
6537 case DeclarationName::ObjCMultiArgSelector:
6538 return Import(FromName.getObjCSelector());
6539
6540 case DeclarationName::CXXConstructorName: {
6541 QualType T = Import(FromName.getCXXNameType());
6542 if (T.isNull())
6543 return DeclarationName();
6544
6545 return ToContext.DeclarationNames.getCXXConstructorName(
6546 ToContext.getCanonicalType(T));
6547 }
6548
6549 case DeclarationName::CXXDestructorName: {
6550 QualType T = Import(FromName.getCXXNameType());
6551 if (T.isNull())
6552 return DeclarationName();
6553
6554 return ToContext.DeclarationNames.getCXXDestructorName(
6555 ToContext.getCanonicalType(T));
6556 }
6557
6558 case DeclarationName::CXXConversionFunctionName: {
6559 QualType T = Import(FromName.getCXXNameType());
6560 if (T.isNull())
6561 return DeclarationName();
6562
6563 return ToContext.DeclarationNames.getCXXConversionFunctionName(
6564 ToContext.getCanonicalType(T));
6565 }
6566
6567 case DeclarationName::CXXOperatorName:
6568 return ToContext.DeclarationNames.getCXXOperatorName(
6569 FromName.getCXXOverloadedOperator());
6570
6571 case DeclarationName::CXXLiteralOperatorName:
6572 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
6573 Import(FromName.getCXXLiteralIdentifier()));
6574
6575 case DeclarationName::CXXUsingDirective:
6576 // FIXME: STATICS!
6577 return DeclarationName::getUsingDirectiveName();
6578 }
6579
David Blaikiee4d798f2012-01-20 21:50:17 +00006580 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00006581}
6582
Douglas Gregore2e50d332010-12-01 01:36:18 +00006583IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00006584 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00006585 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006586
Sean Callananf94ef1d2016-05-14 06:11:19 +00006587 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
6588
6589 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
6590 ToId->setBuiltinID(FromId->getBuiltinID());
6591
6592 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006593}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006594
Douglas Gregor43f54792010-02-17 02:12:47 +00006595Selector ASTImporter::Import(Selector FromSel) {
6596 if (FromSel.isNull())
6597 return Selector();
6598
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006599 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00006600 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
6601 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
6602 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
6603 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
6604}
6605
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006606DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
6607 DeclContext *DC,
6608 unsigned IDNS,
6609 NamedDecl **Decls,
6610 unsigned NumDecls) {
6611 return Name;
6612}
6613
6614DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00006615 if (LastDiagFromFrom)
6616 ToContext.getDiagnostics().notePriorDiagnosticFrom(
6617 FromContext.getDiagnostics());
6618 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006619 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006620}
6621
6622DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00006623 if (!LastDiagFromFrom)
6624 FromContext.getDiagnostics().notePriorDiagnosticFrom(
6625 ToContext.getDiagnostics());
6626 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006627 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006628}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00006629
Douglas Gregor2e15c842012-02-01 21:00:38 +00006630void ASTImporter::CompleteDecl (Decl *D) {
6631 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
6632 if (!ID->getDefinition())
6633 ID->startDefinition();
6634 }
6635 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
6636 if (!PD->getDefinition())
6637 PD->startDefinition();
6638 }
6639 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
6640 if (!TD->getDefinition() && !TD->isBeingDefined()) {
6641 TD->startDefinition();
6642 TD->setCompleteDefinition(true);
6643 }
6644 }
6645 else {
6646 assert (0 && "CompleteDecl called on a Decl that can't be completed");
6647 }
6648}
6649
Douglas Gregor8cdbe642010-02-12 23:44:20 +00006650Decl *ASTImporter::Imported(Decl *From, Decl *To) {
Sean Callanan8bca9962016-03-28 21:43:01 +00006651 if (From->hasAttrs()) {
6652 for (Attr *FromAttr : From->getAttrs())
6653 To->addAttr(FromAttr->clone(To->getASTContext()));
6654 }
6655 if (From->isUsed()) {
6656 To->setIsUsed();
6657 }
Sean Callanandd2c1742016-05-16 20:48:03 +00006658 if (From->isImplicit()) {
6659 To->setImplicit();
6660 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00006661 ImportedDecls[From] = To;
6662 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00006663}
Douglas Gregorb4964f72010-02-15 23:54:17 +00006664
Douglas Gregordd6006f2012-07-17 21:16:27 +00006665bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
6666 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00006667 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00006668 = ImportedTypes.find(From.getTypePtr());
6669 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
6670 return true;
6671
Douglas Gregordd6006f2012-07-17 21:16:27 +00006672 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
6673 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00006674 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00006675}