blob: f80ce35df2bce0aa9b7a32afd50fa72094cf10d3 [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);
Gabor Horvath0866c2f2016-11-23 15:24:23 +000042 QualType VisitAtomicType(const AtomicType *T);
John McCall424cec92011-01-19 06:33:43 +000043 QualType VisitBuiltinType(const BuiltinType *T);
Aleksei Sidorina693b372016-09-28 10:16:56 +000044 QualType VisitDecayedType(const DecayedType *T);
John McCall424cec92011-01-19 06:33:43 +000045 QualType VisitComplexType(const ComplexType *T);
46 QualType VisitPointerType(const PointerType *T);
47 QualType VisitBlockPointerType(const BlockPointerType *T);
48 QualType VisitLValueReferenceType(const LValueReferenceType *T);
49 QualType VisitRValueReferenceType(const RValueReferenceType *T);
50 QualType VisitMemberPointerType(const MemberPointerType *T);
51 QualType VisitConstantArrayType(const ConstantArrayType *T);
52 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
53 QualType VisitVariableArrayType(const VariableArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000054 // FIXME: DependentSizedArrayType
55 // FIXME: DependentSizedExtVectorType
John McCall424cec92011-01-19 06:33:43 +000056 QualType VisitVectorType(const VectorType *T);
57 QualType VisitExtVectorType(const ExtVectorType *T);
58 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
59 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000060 // FIXME: UnresolvedUsingType
Sean Callananda6df8a2011-08-11 16:56:07 +000061 QualType VisitParenType(const ParenType *T);
John McCall424cec92011-01-19 06:33:43 +000062 QualType VisitTypedefType(const TypedefType *T);
63 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000064 // FIXME: DependentTypeOfExprType
John McCall424cec92011-01-19 06:33:43 +000065 QualType VisitTypeOfType(const TypeOfType *T);
66 QualType VisitDecltypeType(const DecltypeType *T);
Alexis Hunte852b102011-05-24 22:41:36 +000067 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith30482bc2011-02-20 03:19:35 +000068 QualType VisitAutoType(const AutoType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000069 QualType VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000070 // FIXME: DependentDecltypeType
John McCall424cec92011-01-19 06:33:43 +000071 QualType VisitRecordType(const RecordType *T);
72 QualType VisitEnumType(const EnumType *T);
Sean Callanan72fe0852015-04-02 23:50:08 +000073 QualType VisitAttributedType(const AttributedType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000074 QualType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
Aleksei Sidorin855086d2017-01-23 09:30:36 +000075 QualType VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T);
John McCall424cec92011-01-19 06:33:43 +000076 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
77 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +000078 // FIXME: DependentNameType
John McCallc392f372010-06-11 00:33:02 +000079 // FIXME: DependentTemplateSpecializationType
John McCall424cec92011-01-19 06:33:43 +000080 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
81 QualType VisitObjCObjectType(const ObjCObjectType *T);
82 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000083
Douglas Gregor95d82832012-01-24 18:36:04 +000084 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +000085 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
86 DeclContext *&LexicalDC, DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +000087 NamedDecl *&ToD, SourceLocation &Loc);
Craig Topper36250ad2014-05-12 05:36:57 +000088 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +000089 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
90 DeclarationNameInfo& To);
Douglas Gregor0a791672011-01-18 03:11:38 +000091 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000092
Aleksei Sidorina693b372016-09-28 10:16:56 +000093 bool ImportCastPath(CastExpr *E, CXXCastPath &Path);
94
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000095 typedef DesignatedInitExpr::Designator Designator;
96 Designator ImportDesignator(const Designator &D);
97
Douglas Gregor2e15c842012-02-01 21:00:38 +000098
Douglas Gregor95d82832012-01-24 18:36:04 +000099 /// \brief What we should import from the definition.
100 enum ImportDefinitionKind {
101 /// \brief Import the default subset of the definition, which might be
102 /// nothing (if minimal import is set) or might be everything (if minimal
103 /// import is not set).
104 IDK_Default,
105 /// \brief Import everything.
106 IDK_Everything,
107 /// \brief Import only the bare bones needed to establish a valid
108 /// DeclContext.
109 IDK_Basic
110 };
111
Douglas Gregor2e15c842012-02-01 21:00:38 +0000112 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
113 return IDK == IDK_Everything ||
114 (IDK == IDK_Default && !Importer.isMinimalImport());
115 }
116
Douglas Gregord451ea92011-07-29 23:31:30 +0000117 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000118 ImportDefinitionKind Kind = IDK_Default);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000119 bool ImportDefinition(VarDecl *From, VarDecl *To,
120 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregord451ea92011-07-29 23:31:30 +0000121 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000122 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000123 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000124 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000125 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000126 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregora082a492010-11-30 19:14:50 +0000127 TemplateParameterList *ImportTemplateParameterList(
128 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000129 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000130 TemplateArgumentLoc ImportTemplateArgumentLoc(
131 const TemplateArgumentLoc &TALoc, bool &Error);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000132 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
133 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000134 SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000135 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
136 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000137 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
138 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000139 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000140 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Douglas Gregora082a492010-11-30 19:14:50 +0000141 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000142 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000143 Decl *VisitDecl(Decl *D);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +0000144 Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000145 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000146 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000147 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000148 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000149 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000150 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000151 Decl *VisitLabelDecl(LabelDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000152 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000153 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000154 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000155 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000156 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
157 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
158 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
159 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000160 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000161 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000162 Decl *VisitFriendDecl(FriendDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000163 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000164 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000165 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000166 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000167 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000168 Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000169 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000170 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Sean Callanan0aae0412014-12-10 00:00:37 +0000171 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000172
173 ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list);
Douglas Gregor45635322010-02-16 01:20:57 +0000174 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000175 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000176 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000177 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000178 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000179 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
180 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
181 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
182 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000183 Decl *VisitClassTemplateSpecializationDecl(
184 ClassTemplateSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000185 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
186 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
187
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000188 // Importing statements
Sean Callanan59721b32015-04-28 18:41:46 +0000189 DeclGroupRef ImportDeclGroup(DeclGroupRef DG);
190
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000191 Stmt *VisitStmt(Stmt *S);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000192 Stmt *VisitGCCAsmStmt(GCCAsmStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000193 Stmt *VisitDeclStmt(DeclStmt *S);
194 Stmt *VisitNullStmt(NullStmt *S);
195 Stmt *VisitCompoundStmt(CompoundStmt *S);
196 Stmt *VisitCaseStmt(CaseStmt *S);
197 Stmt *VisitDefaultStmt(DefaultStmt *S);
198 Stmt *VisitLabelStmt(LabelStmt *S);
199 Stmt *VisitAttributedStmt(AttributedStmt *S);
200 Stmt *VisitIfStmt(IfStmt *S);
201 Stmt *VisitSwitchStmt(SwitchStmt *S);
202 Stmt *VisitWhileStmt(WhileStmt *S);
203 Stmt *VisitDoStmt(DoStmt *S);
204 Stmt *VisitForStmt(ForStmt *S);
205 Stmt *VisitGotoStmt(GotoStmt *S);
206 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S);
207 Stmt *VisitContinueStmt(ContinueStmt *S);
208 Stmt *VisitBreakStmt(BreakStmt *S);
209 Stmt *VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000210 // FIXME: MSAsmStmt
211 // FIXME: SEHExceptStmt
212 // FIXME: SEHFinallyStmt
213 // FIXME: SEHTryStmt
214 // FIXME: SEHLeaveStmt
215 // FIXME: CapturedStmt
216 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S);
217 Stmt *VisitCXXTryStmt(CXXTryStmt *S);
218 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S);
219 // FIXME: MSDependentExistsStmt
220 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
221 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
222 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
223 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
224 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
225 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
226 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000227
228 // Importing expressions
229 Expr *VisitExpr(Expr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000230 Expr *VisitVAArgExpr(VAArgExpr *E);
231 Expr *VisitGNUNullExpr(GNUNullExpr *E);
232 Expr *VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000233 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000234 Expr *VisitImplicitValueInitExpr(ImplicitValueInitExpr *ILE);
235 Expr *VisitDesignatedInitExpr(DesignatedInitExpr *E);
236 Expr *VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000237 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000238 Expr *VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000239 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000240 Expr *VisitStringLiteral(StringLiteral *E);
241 Expr *VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
242 Expr *VisitAtomicExpr(AtomicExpr *E);
243 Expr *VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000244 Expr *VisitParenExpr(ParenExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000245 Expr *VisitParenListExpr(ParenListExpr *E);
246 Expr *VisitStmtExpr(StmtExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000247 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000248 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000249 Expr *VisitBinaryOperator(BinaryOperator *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000250 Expr *VisitConditionalOperator(ConditionalOperator *E);
251 Expr *VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
252 Expr *VisitOpaqueValueExpr(OpaqueValueExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000253 Expr *VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
254 Expr *VisitExpressionTraitExpr(ExpressionTraitExpr *E);
255 Expr *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000256 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000257 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000258 Expr *VisitExplicitCastExpr(ExplicitCastExpr *E);
259 Expr *VisitOffsetOfExpr(OffsetOfExpr *OE);
260 Expr *VisitCXXThrowExpr(CXXThrowExpr *E);
261 Expr *VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
262 Expr *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
263 Expr *VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
264 Expr *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
265 Expr *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE);
266 Expr *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
267 Expr *VisitCXXNewExpr(CXXNewExpr *CE);
268 Expr *VisitCXXDeleteExpr(CXXDeleteExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000269 Expr *VisitCXXConstructExpr(CXXConstructExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000270 Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000271 Expr *VisitExprWithCleanups(ExprWithCleanups *EWC);
Sean Callanan8bca9962016-03-28 21:43:01 +0000272 Expr *VisitCXXThisExpr(CXXThisExpr *E);
273 Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000274 Expr *VisitMemberExpr(MemberExpr *E);
275 Expr *VisitCallExpr(CallExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000276 Expr *VisitInitListExpr(InitListExpr *E);
Richard Smith30e304e2016-12-14 00:03:17 +0000277 Expr *VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
278 Expr *VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
Sean Callanandd2c1742016-05-16 20:48:03 +0000279 Expr *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
280 Expr *VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000281 Expr *VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
282
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000283
284 template<typename IIter, typename OIter>
285 void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) {
286 typedef typename std::remove_reference<decltype(*Obegin)>::type ItemT;
287 ASTImporter &ImporterRef = Importer;
288 std::transform(Ibegin, Iend, Obegin,
289 [&ImporterRef](ItemT From) -> ItemT {
290 return ImporterRef.Import(From);
Sean Callanan8bca9962016-03-28 21:43:01 +0000291 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000292 }
293
294 template<typename IIter, typename OIter>
295 bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
296 typedef typename std::remove_reference<decltype(**Obegin)>::type ItemT;
297 ASTImporter &ImporterRef = Importer;
298 bool Failed = false;
299 std::transform(Ibegin, Iend, Obegin,
300 [&ImporterRef, &Failed](ItemT *From) -> ItemT * {
Aleksei Sidorina693b372016-09-28 10:16:56 +0000301 ItemT *To = cast_or_null<ItemT>(
302 ImporterRef.Import(From));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000303 if (!To && From)
304 Failed = true;
305 return To;
306 });
307 return Failed;
Sean Callanan8bca9962016-03-28 21:43:01 +0000308 }
Aleksei Sidorina693b372016-09-28 10:16:56 +0000309
310 template<typename InContainerTy, typename OutContainerTy>
311 bool ImportContainerChecked(const InContainerTy &InContainer,
312 OutContainerTy &OutContainer) {
313 return ImportArrayChecked(InContainer.begin(), InContainer.end(),
314 OutContainer.begin());
315 }
316
317 template<typename InContainerTy, typename OIter>
318 bool ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
319 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
320 }
Douglas Gregor96e578d2010-02-05 17:54:41 +0000321 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000322}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000323
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000324using namespace clang;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000325
326//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000327// Structural Equivalence
328//----------------------------------------------------------------------------
329
330namespace {
331 struct StructuralEquivalenceContext {
332 /// \brief AST contexts for which we are checking structural equivalence.
333 ASTContext &C1, &C2;
334
Douglas Gregor3996e242010-02-15 22:01:00 +0000335 /// \brief The set of "tentative" equivalences between two canonical
336 /// declarations, mapping from a declaration in the first context to the
337 /// declaration in the second context that we believe to be equivalent.
338 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
339
340 /// \brief Queue of declarations in the first context whose equivalence
341 /// with a declaration in the second context still needs to be verified.
342 std::deque<Decl *> DeclsToCheck;
343
Douglas Gregorb4964f72010-02-15 23:54:17 +0000344 /// \brief Declaration (from, to) pairs that are known not to be equivalent
345 /// (which we have already complained about).
346 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
347
Douglas Gregor3996e242010-02-15 22:01:00 +0000348 /// \brief Whether we're being strict about the spelling of types when
349 /// unifying two types.
350 bool StrictTypeSpelling;
Douglas Gregordd6006f2012-07-17 21:16:27 +0000351
352 /// \brief Whether to complain about failures.
353 bool Complain;
354
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000355 /// \brief \c true if the last diagnostic came from C2.
356 bool LastDiagFromC2;
357
Douglas Gregor3996e242010-02-15 22:01:00 +0000358 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000359 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregordd6006f2012-07-17 21:16:27 +0000360 bool StrictTypeSpelling = false,
361 bool Complain = true)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000362 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000363 StrictTypeSpelling(StrictTypeSpelling), Complain(Complain),
364 LastDiagFromC2(false) {}
Douglas Gregor3996e242010-02-15 22:01:00 +0000365
366 /// \brief Determine whether the two declarations are structurally
367 /// equivalent.
368 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
369
370 /// \brief Determine whether the two types are structurally equivalent.
371 bool IsStructurallyEquivalent(QualType T1, QualType T2);
372
373 private:
374 /// \brief Finish checking all of the structural equivalences.
375 ///
376 /// \returns true if an error occurred, false otherwise.
377 bool Finish();
378
379 public:
380 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000381 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000382 if (LastDiagFromC2)
383 C1.getDiagnostics().notePriorDiagnosticFrom(C2.getDiagnostics());
384 LastDiagFromC2 = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000385 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000386 }
387
388 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000389 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000390 if (!LastDiagFromC2)
391 C2.getDiagnostics().notePriorDiagnosticFrom(C1.getDiagnostics());
392 LastDiagFromC2 = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000393 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000394 }
395 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000396}
Douglas Gregor3996e242010-02-15 22:01:00 +0000397
398static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
399 QualType T1, QualType T2);
400static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
401 Decl *D1, Decl *D2);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000402static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
403 const TemplateArgument &Arg1,
404 const TemplateArgument &Arg2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000405
Douglas Gregor3996e242010-02-15 22:01:00 +0000406/// \brief Determine structural equivalence of two expressions.
407static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
408 Expr *E1, Expr *E2) {
409 if (!E1 || !E2)
410 return E1 == E2;
411
412 // FIXME: Actually perform a structural comparison!
413 return true;
414}
415
416/// \brief Determine whether two identifiers are equivalent.
417static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
418 const IdentifierInfo *Name2) {
419 if (!Name1 || !Name2)
420 return Name1 == Name2;
421
422 return Name1->getName() == Name2->getName();
423}
424
425/// \brief Determine whether two nested-name-specifiers are equivalent.
426static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
427 NestedNameSpecifier *NNS1,
428 NestedNameSpecifier *NNS2) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000429 if (NNS1->getKind() != NNS2->getKind())
430 return false;
431
432 NestedNameSpecifier *Prefix1 = NNS1->getPrefix(),
433 *Prefix2 = NNS2->getPrefix();
434 if ((bool)Prefix1 != (bool)Prefix2)
435 return false;
436
437 if (Prefix1)
438 if (!IsStructurallyEquivalent(Context, Prefix1, Prefix2))
439 return false;
440
441 switch (NNS1->getKind()) {
442 case NestedNameSpecifier::Identifier:
443 return IsStructurallyEquivalent(NNS1->getAsIdentifier(),
444 NNS2->getAsIdentifier());
445 case NestedNameSpecifier::Namespace:
446 return IsStructurallyEquivalent(Context, NNS1->getAsNamespace(),
447 NNS2->getAsNamespace());
448 case NestedNameSpecifier::NamespaceAlias:
449 return IsStructurallyEquivalent(Context, NNS1->getAsNamespaceAlias(),
450 NNS2->getAsNamespaceAlias());
451 case NestedNameSpecifier::TypeSpec:
452 case NestedNameSpecifier::TypeSpecWithTemplate:
453 return IsStructurallyEquivalent(Context, QualType(NNS1->getAsType(), 0),
454 QualType(NNS2->getAsType(), 0));
455 case NestedNameSpecifier::Global:
456 return true;
457 case NestedNameSpecifier::Super:
458 return IsStructurallyEquivalent(Context, NNS1->getAsRecordDecl(),
459 NNS2->getAsRecordDecl());
460 }
461 return false;
462}
463
464static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
465 const TemplateName &N1,
466 const TemplateName &N2) {
467 if (N1.getKind() != N2.getKind())
468 return false;
469 switch (N1.getKind()) {
470 case TemplateName::Template:
471 return IsStructurallyEquivalent(Context, N1.getAsTemplateDecl(),
472 N2.getAsTemplateDecl());
473
474 case TemplateName::OverloadedTemplate: {
475 OverloadedTemplateStorage *OS1 = N1.getAsOverloadedTemplate(),
476 *OS2 = N2.getAsOverloadedTemplate();
477 OverloadedTemplateStorage::iterator I1 = OS1->begin(), I2 = OS2->begin(),
478 E1 = OS1->end(), E2 = OS2->end();
479 for (; I1 != E1 && I2 != E2; ++I1, ++I2)
480 if (!IsStructurallyEquivalent(Context, *I1, *I2))
481 return false;
482 return I1 == E1 && I2 == E2;
483 }
484
485 case TemplateName::QualifiedTemplate: {
486 QualifiedTemplateName *QN1 = N1.getAsQualifiedTemplateName(),
487 *QN2 = N2.getAsQualifiedTemplateName();
488 return IsStructurallyEquivalent(Context, QN1->getDecl(), QN2->getDecl()) &&
489 IsStructurallyEquivalent(Context, QN1->getQualifier(),
490 QN2->getQualifier());
491 }
492
493 case TemplateName::DependentTemplate: {
494 DependentTemplateName *DN1 = N1.getAsDependentTemplateName(),
495 *DN2 = N2.getAsDependentTemplateName();
496 if (!IsStructurallyEquivalent(Context, DN1->getQualifier(),
497 DN2->getQualifier()))
498 return false;
499 if (DN1->isIdentifier() && DN2->isIdentifier())
500 return IsStructurallyEquivalent(DN1->getIdentifier(),
501 DN2->getIdentifier());
502 else if (DN1->isOverloadedOperator() && DN2->isOverloadedOperator())
503 return DN1->getOperator() == DN2->getOperator();
504 return false;
505 }
506
507 case TemplateName::SubstTemplateTemplateParm: {
508 SubstTemplateTemplateParmStorage *TS1 = N1.getAsSubstTemplateTemplateParm(),
509 *TS2 = N2.getAsSubstTemplateTemplateParm();
510 return IsStructurallyEquivalent(Context, TS1->getParameter(),
511 TS2->getParameter()) &&
512 IsStructurallyEquivalent(Context, TS1->getReplacement(),
513 TS2->getReplacement());
514 }
515 case TemplateName::SubstTemplateTemplateParmPack: {
516 SubstTemplateTemplateParmPackStorage
517 *P1 = N1.getAsSubstTemplateTemplateParmPack(),
518 *P2 = N2.getAsSubstTemplateTemplateParmPack();
519 return IsStructurallyEquivalent(Context, P1->getArgumentPack(),
520 P2->getArgumentPack()) &&
521 IsStructurallyEquivalent(Context, P1->getParameterPack(),
522 P2->getParameterPack());
523 }
524 }
525 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000526}
527
528/// \brief Determine whether two template arguments are equivalent.
529static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
530 const TemplateArgument &Arg1,
531 const TemplateArgument &Arg2) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000532 if (Arg1.getKind() != Arg2.getKind())
533 return false;
534
535 switch (Arg1.getKind()) {
536 case TemplateArgument::Null:
537 return true;
538
539 case TemplateArgument::Type:
540 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
Eli Friedmanb826a002012-09-26 02:36:12 +0000541
Douglas Gregore2e50d332010-12-01 01:36:18 +0000542 case TemplateArgument::Integral:
543 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
544 Arg2.getIntegralType()))
545 return false;
546
Eric Christopher6dcc3762012-07-15 00:23:57 +0000547 return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral());
Douglas Gregore2e50d332010-12-01 01:36:18 +0000548
549 case TemplateArgument::Declaration:
550 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +0000551
552 case TemplateArgument::NullPtr:
553 return true; // FIXME: Is this correct?
554
Douglas Gregore2e50d332010-12-01 01:36:18 +0000555 case TemplateArgument::Template:
556 return IsStructurallyEquivalent(Context,
557 Arg1.getAsTemplate(),
558 Arg2.getAsTemplate());
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000559
560 case TemplateArgument::TemplateExpansion:
561 return IsStructurallyEquivalent(Context,
562 Arg1.getAsTemplateOrTemplatePattern(),
563 Arg2.getAsTemplateOrTemplatePattern());
564
Douglas Gregore2e50d332010-12-01 01:36:18 +0000565 case TemplateArgument::Expression:
566 return IsStructurallyEquivalent(Context,
567 Arg1.getAsExpr(), Arg2.getAsExpr());
568
569 case TemplateArgument::Pack:
570 if (Arg1.pack_size() != Arg2.pack_size())
571 return false;
572
573 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
574 if (!IsStructurallyEquivalent(Context,
575 Arg1.pack_begin()[I],
576 Arg2.pack_begin()[I]))
577 return false;
578
579 return true;
580 }
581
582 llvm_unreachable("Invalid template argument kind");
Douglas Gregor3996e242010-02-15 22:01:00 +0000583}
584
585/// \brief Determine structural equivalence for the common part of array
586/// types.
587static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
588 const ArrayType *Array1,
589 const ArrayType *Array2) {
590 if (!IsStructurallyEquivalent(Context,
591 Array1->getElementType(),
592 Array2->getElementType()))
593 return false;
594 if (Array1->getSizeModifier() != Array2->getSizeModifier())
595 return false;
596 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
597 return false;
598
599 return true;
600}
601
602/// \brief Determine structural equivalence of two types.
603static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
604 QualType T1, QualType T2) {
605 if (T1.isNull() || T2.isNull())
606 return T1.isNull() && T2.isNull();
607
608 if (!Context.StrictTypeSpelling) {
609 // We aren't being strict about token-to-token equivalence of types,
610 // so map down to the canonical type.
611 T1 = Context.C1.getCanonicalType(T1);
612 T2 = Context.C2.getCanonicalType(T2);
613 }
614
615 if (T1.getQualifiers() != T2.getQualifiers())
616 return false;
617
Douglas Gregorb4964f72010-02-15 23:54:17 +0000618 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000619
Douglas Gregorb4964f72010-02-15 23:54:17 +0000620 if (T1->getTypeClass() != T2->getTypeClass()) {
621 // Compare function types with prototypes vs. without prototypes as if
622 // both did not have prototypes.
623 if (T1->getTypeClass() == Type::FunctionProto &&
624 T2->getTypeClass() == Type::FunctionNoProto)
625 TC = Type::FunctionNoProto;
626 else if (T1->getTypeClass() == Type::FunctionNoProto &&
627 T2->getTypeClass() == Type::FunctionProto)
628 TC = Type::FunctionNoProto;
629 else
630 return false;
631 }
632
633 switch (TC) {
634 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000635 // FIXME: Deal with Char_S/Char_U.
636 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
637 return false;
638 break;
639
640 case Type::Complex:
641 if (!IsStructurallyEquivalent(Context,
642 cast<ComplexType>(T1)->getElementType(),
643 cast<ComplexType>(T2)->getElementType()))
644 return false;
645 break;
646
Reid Kleckner0503a872013-12-05 01:23:43 +0000647 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +0000648 case Type::Decayed:
649 if (!IsStructurallyEquivalent(Context,
Reid Kleckner0503a872013-12-05 01:23:43 +0000650 cast<AdjustedType>(T1)->getOriginalType(),
651 cast<AdjustedType>(T2)->getOriginalType()))
Reid Kleckner8a365022013-06-24 17:51:48 +0000652 return false;
653 break;
654
Douglas Gregor3996e242010-02-15 22:01:00 +0000655 case Type::Pointer:
656 if (!IsStructurallyEquivalent(Context,
657 cast<PointerType>(T1)->getPointeeType(),
658 cast<PointerType>(T2)->getPointeeType()))
659 return false;
660 break;
661
662 case Type::BlockPointer:
663 if (!IsStructurallyEquivalent(Context,
664 cast<BlockPointerType>(T1)->getPointeeType(),
665 cast<BlockPointerType>(T2)->getPointeeType()))
666 return false;
667 break;
668
669 case Type::LValueReference:
670 case Type::RValueReference: {
671 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
672 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
673 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
674 return false;
675 if (Ref1->isInnerRef() != Ref2->isInnerRef())
676 return false;
677 if (!IsStructurallyEquivalent(Context,
678 Ref1->getPointeeTypeAsWritten(),
679 Ref2->getPointeeTypeAsWritten()))
680 return false;
681 break;
682 }
683
684 case Type::MemberPointer: {
685 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
686 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
687 if (!IsStructurallyEquivalent(Context,
688 MemPtr1->getPointeeType(),
689 MemPtr2->getPointeeType()))
690 return false;
691 if (!IsStructurallyEquivalent(Context,
692 QualType(MemPtr1->getClass(), 0),
693 QualType(MemPtr2->getClass(), 0)))
694 return false;
695 break;
696 }
697
698 case Type::ConstantArray: {
699 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
700 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
Eric Christopher6dcc3762012-07-15 00:23:57 +0000701 if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000702 return false;
703
704 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
705 return false;
706 break;
707 }
708
709 case Type::IncompleteArray:
710 if (!IsArrayStructurallyEquivalent(Context,
711 cast<ArrayType>(T1),
712 cast<ArrayType>(T2)))
713 return false;
714 break;
715
716 case Type::VariableArray: {
717 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
718 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
719 if (!IsStructurallyEquivalent(Context,
720 Array1->getSizeExpr(), Array2->getSizeExpr()))
721 return false;
722
723 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
724 return false;
725
726 break;
727 }
728
729 case Type::DependentSizedArray: {
730 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
731 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
732 if (!IsStructurallyEquivalent(Context,
733 Array1->getSizeExpr(), Array2->getSizeExpr()))
734 return false;
735
736 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
737 return false;
738
739 break;
740 }
741
742 case Type::DependentSizedExtVector: {
743 const DependentSizedExtVectorType *Vec1
744 = cast<DependentSizedExtVectorType>(T1);
745 const DependentSizedExtVectorType *Vec2
746 = cast<DependentSizedExtVectorType>(T2);
747 if (!IsStructurallyEquivalent(Context,
748 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
749 return false;
750 if (!IsStructurallyEquivalent(Context,
751 Vec1->getElementType(),
752 Vec2->getElementType()))
753 return false;
754 break;
755 }
756
757 case Type::Vector:
758 case Type::ExtVector: {
759 const VectorType *Vec1 = cast<VectorType>(T1);
760 const VectorType *Vec2 = cast<VectorType>(T2);
761 if (!IsStructurallyEquivalent(Context,
762 Vec1->getElementType(),
763 Vec2->getElementType()))
764 return false;
765 if (Vec1->getNumElements() != Vec2->getNumElements())
766 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000767 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000768 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000769 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000770 }
771
772 case Type::FunctionProto: {
773 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
774 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
Alp Toker9cacbab2014-01-20 20:26:09 +0000775 if (Proto1->getNumParams() != Proto2->getNumParams())
Douglas Gregor3996e242010-02-15 22:01:00 +0000776 return false;
Alp Toker9cacbab2014-01-20 20:26:09 +0000777 for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) {
778 if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I),
779 Proto2->getParamType(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000780 return false;
781 }
782 if (Proto1->isVariadic() != Proto2->isVariadic())
783 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000784 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor3996e242010-02-15 22:01:00 +0000785 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000786 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
787 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
788 return false;
789 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
790 if (!IsStructurallyEquivalent(Context,
791 Proto1->getExceptionType(I),
792 Proto2->getExceptionType(I)))
793 return false;
794 }
795 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000796 if (!IsStructurallyEquivalent(Context,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000797 Proto1->getNoexceptExpr(),
798 Proto2->getNoexceptExpr()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000799 return false;
800 }
801 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
802 return false;
803
804 // Fall through to check the bits common with FunctionNoProtoType.
805 }
806
807 case Type::FunctionNoProto: {
808 const FunctionType *Function1 = cast<FunctionType>(T1);
809 const FunctionType *Function2 = cast<FunctionType>(T2);
Alp Toker314cc812014-01-25 16:55:45 +0000810 if (!IsStructurallyEquivalent(Context, Function1->getReturnType(),
811 Function2->getReturnType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000812 return false;
Justin Bogner62c04de2016-03-20 16:58:03 +0000813 if (Function1->getExtInfo() != Function2->getExtInfo())
814 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000815 break;
816 }
817
818 case Type::UnresolvedUsing:
819 if (!IsStructurallyEquivalent(Context,
820 cast<UnresolvedUsingType>(T1)->getDecl(),
821 cast<UnresolvedUsingType>(T2)->getDecl()))
822 return false;
823
824 break;
John McCall81904512011-01-06 01:58:22 +0000825
826 case Type::Attributed:
827 if (!IsStructurallyEquivalent(Context,
828 cast<AttributedType>(T1)->getModifiedType(),
829 cast<AttributedType>(T2)->getModifiedType()))
830 return false;
831 if (!IsStructurallyEquivalent(Context,
832 cast<AttributedType>(T1)->getEquivalentType(),
833 cast<AttributedType>(T2)->getEquivalentType()))
834 return false;
835 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000836
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000837 case Type::Paren:
838 if (!IsStructurallyEquivalent(Context,
839 cast<ParenType>(T1)->getInnerType(),
840 cast<ParenType>(T2)->getInnerType()))
841 return false;
842 break;
843
Douglas Gregor3996e242010-02-15 22:01:00 +0000844 case Type::Typedef:
845 if (!IsStructurallyEquivalent(Context,
846 cast<TypedefType>(T1)->getDecl(),
847 cast<TypedefType>(T2)->getDecl()))
848 return false;
849 break;
850
851 case Type::TypeOfExpr:
852 if (!IsStructurallyEquivalent(Context,
853 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
854 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
855 return false;
856 break;
857
858 case Type::TypeOf:
859 if (!IsStructurallyEquivalent(Context,
860 cast<TypeOfType>(T1)->getUnderlyingType(),
861 cast<TypeOfType>(T2)->getUnderlyingType()))
862 return false;
863 break;
Alexis Hunte852b102011-05-24 22:41:36 +0000864
865 case Type::UnaryTransform:
866 if (!IsStructurallyEquivalent(Context,
867 cast<UnaryTransformType>(T1)->getUnderlyingType(),
868 cast<UnaryTransformType>(T1)->getUnderlyingType()))
869 return false;
870 break;
871
Douglas Gregor3996e242010-02-15 22:01:00 +0000872 case Type::Decltype:
873 if (!IsStructurallyEquivalent(Context,
874 cast<DecltypeType>(T1)->getUnderlyingExpr(),
875 cast<DecltypeType>(T2)->getUnderlyingExpr()))
876 return false;
877 break;
878
Richard Smith30482bc2011-02-20 03:19:35 +0000879 case Type::Auto:
880 if (!IsStructurallyEquivalent(Context,
881 cast<AutoType>(T1)->getDeducedType(),
882 cast<AutoType>(T2)->getDeducedType()))
883 return false;
884 break;
885
Richard Smith600b5262017-01-26 20:40:47 +0000886 case Type::DeducedTemplateSpecialization: {
887 auto *DT1 = cast<DeducedTemplateSpecializationType>(T1);
888 auto *DT2 = cast<DeducedTemplateSpecializationType>(T2);
889 if (!IsStructurallyEquivalent(Context,
890 DT1->getTemplateName(),
891 DT2->getTemplateName()))
892 return false;
893 if (!IsStructurallyEquivalent(Context,
894 DT1->getDeducedType(),
895 DT2->getDeducedType()))
896 return false;
897 break;
898 }
899
Douglas Gregor3996e242010-02-15 22:01:00 +0000900 case Type::Record:
901 case Type::Enum:
902 if (!IsStructurallyEquivalent(Context,
903 cast<TagType>(T1)->getDecl(),
904 cast<TagType>(T2)->getDecl()))
905 return false;
906 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000907
Douglas Gregor3996e242010-02-15 22:01:00 +0000908 case Type::TemplateTypeParm: {
909 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
910 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
911 if (Parm1->getDepth() != Parm2->getDepth())
912 return false;
913 if (Parm1->getIndex() != Parm2->getIndex())
914 return false;
915 if (Parm1->isParameterPack() != Parm2->isParameterPack())
916 return false;
917
918 // Names of template type parameters are never significant.
919 break;
920 }
921
922 case Type::SubstTemplateTypeParm: {
923 const SubstTemplateTypeParmType *Subst1
924 = cast<SubstTemplateTypeParmType>(T1);
925 const SubstTemplateTypeParmType *Subst2
926 = cast<SubstTemplateTypeParmType>(T2);
927 if (!IsStructurallyEquivalent(Context,
928 QualType(Subst1->getReplacedParameter(), 0),
929 QualType(Subst2->getReplacedParameter(), 0)))
930 return false;
931 if (!IsStructurallyEquivalent(Context,
932 Subst1->getReplacementType(),
933 Subst2->getReplacementType()))
934 return false;
935 break;
936 }
937
Douglas Gregorfb322d82011-01-14 05:11:40 +0000938 case Type::SubstTemplateTypeParmPack: {
939 const SubstTemplateTypeParmPackType *Subst1
940 = cast<SubstTemplateTypeParmPackType>(T1);
941 const SubstTemplateTypeParmPackType *Subst2
942 = cast<SubstTemplateTypeParmPackType>(T2);
943 if (!IsStructurallyEquivalent(Context,
944 QualType(Subst1->getReplacedParameter(), 0),
945 QualType(Subst2->getReplacedParameter(), 0)))
946 return false;
947 if (!IsStructurallyEquivalent(Context,
948 Subst1->getArgumentPack(),
949 Subst2->getArgumentPack()))
950 return false;
951 break;
952 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000953 case Type::TemplateSpecialization: {
954 const TemplateSpecializationType *Spec1
955 = cast<TemplateSpecializationType>(T1);
956 const TemplateSpecializationType *Spec2
957 = cast<TemplateSpecializationType>(T2);
958 if (!IsStructurallyEquivalent(Context,
959 Spec1->getTemplateName(),
960 Spec2->getTemplateName()))
961 return false;
962 if (Spec1->getNumArgs() != Spec2->getNumArgs())
963 return false;
964 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
965 if (!IsStructurallyEquivalent(Context,
966 Spec1->getArg(I), Spec2->getArg(I)))
967 return false;
968 }
969 break;
970 }
971
Abramo Bagnara6150c882010-05-11 21:36:43 +0000972 case Type::Elaborated: {
973 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
974 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
975 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
976 if (Elab1->getKeyword() != Elab2->getKeyword())
977 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000978 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000979 Elab1->getQualifier(),
980 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000981 return false;
982 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000983 Elab1->getNamedType(),
984 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000985 return false;
986 break;
987 }
988
John McCalle78aac42010-03-10 03:28:59 +0000989 case Type::InjectedClassName: {
990 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
991 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
992 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000993 Inj1->getInjectedSpecializationType(),
994 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000995 return false;
996 break;
997 }
998
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000999 case Type::DependentName: {
1000 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
1001 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +00001002 if (!IsStructurallyEquivalent(Context,
1003 Typename1->getQualifier(),
1004 Typename2->getQualifier()))
1005 return false;
1006 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
1007 Typename2->getIdentifier()))
1008 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001009
1010 break;
1011 }
1012
John McCallc392f372010-06-11 00:33:02 +00001013 case Type::DependentTemplateSpecialization: {
1014 const DependentTemplateSpecializationType *Spec1 =
1015 cast<DependentTemplateSpecializationType>(T1);
1016 const DependentTemplateSpecializationType *Spec2 =
1017 cast<DependentTemplateSpecializationType>(T2);
1018 if (!IsStructurallyEquivalent(Context,
1019 Spec1->getQualifier(),
1020 Spec2->getQualifier()))
1021 return false;
1022 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
1023 Spec2->getIdentifier()))
1024 return false;
1025 if (Spec1->getNumArgs() != Spec2->getNumArgs())
1026 return false;
1027 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
1028 if (!IsStructurallyEquivalent(Context,
1029 Spec1->getArg(I), Spec2->getArg(I)))
1030 return false;
1031 }
1032 break;
1033 }
Douglas Gregord2fa7662010-12-20 02:24:11 +00001034
1035 case Type::PackExpansion:
1036 if (!IsStructurallyEquivalent(Context,
1037 cast<PackExpansionType>(T1)->getPattern(),
1038 cast<PackExpansionType>(T2)->getPattern()))
1039 return false;
1040 break;
1041
Douglas Gregor3996e242010-02-15 22:01:00 +00001042 case Type::ObjCInterface: {
1043 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
1044 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
1045 if (!IsStructurallyEquivalent(Context,
1046 Iface1->getDecl(), Iface2->getDecl()))
1047 return false;
John McCall8b07ec22010-05-15 11:32:37 +00001048 break;
1049 }
1050
Manman Rene6be26c2016-09-13 17:25:08 +00001051 case Type::ObjCTypeParam: {
1052 const ObjCTypeParamType *Obj1 = cast<ObjCTypeParamType>(T1);
1053 const ObjCTypeParamType *Obj2 = cast<ObjCTypeParamType>(T2);
1054 if (!IsStructurallyEquivalent(Context, Obj1->getDecl(),
1055 Obj2->getDecl()))
1056 return false;
1057
1058 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
1059 return false;
1060 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
1061 if (!IsStructurallyEquivalent(Context,
1062 Obj1->getProtocol(I),
1063 Obj2->getProtocol(I)))
1064 return false;
1065 }
1066 break;
1067 }
John McCall8b07ec22010-05-15 11:32:37 +00001068 case Type::ObjCObject: {
1069 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
1070 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
1071 if (!IsStructurallyEquivalent(Context,
1072 Obj1->getBaseType(),
1073 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +00001074 return false;
John McCall8b07ec22010-05-15 11:32:37 +00001075 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
1076 return false;
1077 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001078 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +00001079 Obj1->getProtocol(I),
1080 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +00001081 return false;
1082 }
1083 break;
1084 }
1085
1086 case Type::ObjCObjectPointer: {
1087 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
1088 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
1089 if (!IsStructurallyEquivalent(Context,
1090 Ptr1->getPointeeType(),
1091 Ptr2->getPointeeType()))
1092 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001093 break;
1094 }
Eli Friedman0dfb8892011-10-06 23:00:33 +00001095
1096 case Type::Atomic: {
1097 if (!IsStructurallyEquivalent(Context,
1098 cast<AtomicType>(T1)->getValueType(),
1099 cast<AtomicType>(T2)->getValueType()))
1100 return false;
1101 break;
1102 }
1103
Xiuli Pan9c14e282016-01-09 12:53:17 +00001104 case Type::Pipe: {
1105 if (!IsStructurallyEquivalent(Context,
1106 cast<PipeType>(T1)->getElementType(),
1107 cast<PipeType>(T2)->getElementType()))
1108 return false;
1109 break;
1110 }
1111
Douglas Gregor3996e242010-02-15 22:01:00 +00001112 } // end switch
1113
1114 return true;
1115}
1116
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001117/// \brief Determine structural equivalence of two fields.
1118static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1119 FieldDecl *Field1, FieldDecl *Field2) {
1120 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001121
1122 // For anonymous structs/unions, match up the anonymous struct/union type
1123 // declarations directly, so that we don't go off searching for anonymous
1124 // types
1125 if (Field1->isAnonymousStructOrUnion() &&
1126 Field2->isAnonymousStructOrUnion()) {
1127 RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl();
1128 RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl();
1129 return IsStructurallyEquivalent(Context, D1, D2);
1130 }
Sean Callanan969c5bd2013-04-26 22:49:25 +00001131
1132 // Check for equivalent field names.
1133 IdentifierInfo *Name1 = Field1->getIdentifier();
1134 IdentifierInfo *Name2 = Field2->getIdentifier();
1135 if (!::IsStructurallyEquivalent(Name1, Name2))
1136 return false;
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001137
1138 if (!IsStructurallyEquivalent(Context,
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001139 Field1->getType(), Field2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001140 if (Context.Complain) {
1141 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1142 << Context.C2.getTypeDeclType(Owner2);
1143 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1144 << Field2->getDeclName() << Field2->getType();
1145 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
1146 << Field1->getDeclName() << Field1->getType();
1147 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001148 return false;
1149 }
1150
1151 if (Field1->isBitField() != Field2->isBitField()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001152 if (Context.Complain) {
1153 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1154 << Context.C2.getTypeDeclType(Owner2);
1155 if (Field1->isBitField()) {
1156 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
1157 << Field1->getDeclName() << Field1->getType()
1158 << Field1->getBitWidthValue(Context.C1);
1159 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
1160 << Field2->getDeclName();
1161 } else {
1162 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
1163 << Field2->getDeclName() << Field2->getType()
1164 << Field2->getBitWidthValue(Context.C2);
1165 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
1166 << Field1->getDeclName();
1167 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001168 }
1169 return false;
1170 }
1171
1172 if (Field1->isBitField()) {
1173 // Make sure that the bit-fields are the same length.
1174 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
1175 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
1176
1177 if (Bits1 != Bits2) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001178 if (Context.Complain) {
1179 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1180 << Context.C2.getTypeDeclType(Owner2);
1181 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
1182 << Field2->getDeclName() << Field2->getType() << Bits2;
1183 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
1184 << Field1->getDeclName() << Field1->getType() << Bits1;
1185 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001186 return false;
1187 }
1188 }
1189
1190 return true;
1191}
1192
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001193/// \brief Find the index of the given anonymous struct/union within its
1194/// context.
1195///
1196/// \returns Returns the index of this anonymous struct/union in its context,
1197/// including the next assigned index (if none of them match). Returns an
1198/// empty option if the context is not a record, i.e.. if the anonymous
1199/// struct/union is at namespace or block scope.
Sean Callanan488f8612016-07-14 19:53:44 +00001200static Optional<unsigned> findUntaggedStructOrUnionIndex(RecordDecl *Anon) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001201 ASTContext &Context = Anon->getASTContext();
1202 QualType AnonTy = Context.getRecordType(Anon);
1203
1204 RecordDecl *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext());
1205 if (!Owner)
David Blaikie7a30dc52013-02-21 01:47:18 +00001206 return None;
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001207
1208 unsigned Index = 0;
Aaron Ballman629afae2014-03-07 19:56:05 +00001209 for (const auto *D : Owner->noload_decls()) {
1210 const auto *F = dyn_cast<FieldDecl>(D);
Sean Callanan488f8612016-07-14 19:53:44 +00001211 if (!F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001212 continue;
1213
Sean Callanan488f8612016-07-14 19:53:44 +00001214 if (F->isAnonymousStructOrUnion()) {
1215 if (Context.hasSameType(F->getType(), AnonTy))
1216 break;
1217 ++Index;
1218 continue;
1219 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001220
Sean Callanan488f8612016-07-14 19:53:44 +00001221 // If the field looks like this:
1222 // struct { ... } A;
1223 QualType FieldType = F->getType();
1224 if (const auto *RecType = dyn_cast<RecordType>(FieldType)) {
1225 const RecordDecl *RecDecl = RecType->getDecl();
1226 if (RecDecl->getDeclContext() == Owner &&
1227 !RecDecl->getIdentifier()) {
1228 if (Context.hasSameType(FieldType, AnonTy))
1229 break;
1230 ++Index;
1231 continue;
1232 }
1233 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001234 }
1235
1236 return Index;
1237}
1238
Douglas Gregor3996e242010-02-15 22:01:00 +00001239/// \brief Determine structural equivalence of two records.
1240static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1241 RecordDecl *D1, RecordDecl *D2) {
1242 if (D1->isUnion() != D2->isUnion()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001243 if (Context.Complain) {
1244 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1245 << Context.C2.getTypeDeclType(D2);
1246 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
1247 << D1->getDeclName() << (unsigned)D1->getTagKind();
1248 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001249 return false;
1250 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001251
1252 if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) {
1253 // If both anonymous structs/unions are in a record context, make sure
1254 // they occur in the same location in the context records.
Sean Callanan488f8612016-07-14 19:53:44 +00001255 if (Optional<unsigned> Index1 = findUntaggedStructOrUnionIndex(D1)) {
1256 if (Optional<unsigned> Index2 = findUntaggedStructOrUnionIndex(D2)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001257 if (*Index1 != *Index2)
1258 return false;
1259 }
1260 }
1261 }
1262
Douglas Gregore2e50d332010-12-01 01:36:18 +00001263 // If both declarations are class template specializations, we know
1264 // the ODR applies, so check the template and template arguments.
1265 ClassTemplateSpecializationDecl *Spec1
1266 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
1267 ClassTemplateSpecializationDecl *Spec2
1268 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
1269 if (Spec1 && Spec2) {
1270 // Check that the specialized templates are the same.
1271 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
1272 Spec2->getSpecializedTemplate()))
1273 return false;
1274
1275 // Check that the template arguments are the same.
1276 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
1277 return false;
1278
1279 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
1280 if (!IsStructurallyEquivalent(Context,
1281 Spec1->getTemplateArgs().get(I),
1282 Spec2->getTemplateArgs().get(I)))
1283 return false;
1284 }
1285 // If one is a class template specialization and the other is not, these
Chris Lattner57540c52011-04-15 05:22:18 +00001286 // structures are different.
Douglas Gregore2e50d332010-12-01 01:36:18 +00001287 else if (Spec1 || Spec2)
1288 return false;
1289
Douglas Gregorb4964f72010-02-15 23:54:17 +00001290 // Compare the definitions of these two records. If either or both are
1291 // incomplete, we assume that they are equivalent.
1292 D1 = D1->getDefinition();
1293 D2 = D2->getDefinition();
1294 if (!D1 || !D2)
1295 return true;
1296
Douglas Gregor3996e242010-02-15 22:01:00 +00001297 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
1298 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
1299 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001300 if (Context.Complain) {
1301 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1302 << Context.C2.getTypeDeclType(D2);
1303 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
1304 << D2CXX->getNumBases();
1305 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
1306 << D1CXX->getNumBases();
1307 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001308 return false;
1309 }
1310
1311 // Check the base classes.
1312 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
1313 BaseEnd1 = D1CXX->bases_end(),
1314 Base2 = D2CXX->bases_begin();
1315 Base1 != BaseEnd1;
1316 ++Base1, ++Base2) {
1317 if (!IsStructurallyEquivalent(Context,
1318 Base1->getType(), Base2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001319 if (Context.Complain) {
1320 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1321 << Context.C2.getTypeDeclType(D2);
1322 Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
1323 << Base2->getType()
1324 << Base2->getSourceRange();
1325 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1326 << Base1->getType()
1327 << Base1->getSourceRange();
1328 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001329 return false;
1330 }
1331
1332 // Check virtual vs. non-virtual inheritance mismatch.
1333 if (Base1->isVirtual() != Base2->isVirtual()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001334 if (Context.Complain) {
1335 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1336 << Context.C2.getTypeDeclType(D2);
1337 Context.Diag2(Base2->getLocStart(),
1338 diag::note_odr_virtual_base)
1339 << Base2->isVirtual() << Base2->getSourceRange();
1340 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1341 << Base1->isVirtual()
1342 << Base1->getSourceRange();
1343 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001344 return false;
1345 }
1346 }
1347 } else if (D1CXX->getNumBases() > 0) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001348 if (Context.Complain) {
1349 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1350 << Context.C2.getTypeDeclType(D2);
1351 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
1352 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1353 << Base1->getType()
1354 << Base1->getSourceRange();
1355 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
1356 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001357 return false;
1358 }
1359 }
1360
1361 // Check the fields for consistency.
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001362 RecordDecl::field_iterator Field2 = D2->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001363 Field2End = D2->field_end();
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001364 for (RecordDecl::field_iterator Field1 = D1->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001365 Field1End = D1->field_end();
1366 Field1 != Field1End;
1367 ++Field1, ++Field2) {
1368 if (Field2 == Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001369 if (Context.Complain) {
1370 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1371 << Context.C2.getTypeDeclType(D2);
1372 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
1373 << Field1->getDeclName() << Field1->getType();
1374 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
1375 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001376 return false;
1377 }
1378
David Blaikie40ed2972012-06-06 20:45:41 +00001379 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001380 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001381 }
1382
1383 if (Field2 != Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001384 if (Context.Complain) {
1385 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1386 << Context.C2.getTypeDeclType(D2);
1387 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1388 << Field2->getDeclName() << Field2->getType();
1389 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1390 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001391 return false;
1392 }
1393
1394 return true;
1395}
1396
1397/// \brief Determine structural equivalence of two enums.
1398static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1399 EnumDecl *D1, EnumDecl *D2) {
1400 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1401 EC2End = D2->enumerator_end();
1402 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1403 EC1End = D1->enumerator_end();
1404 EC1 != EC1End; ++EC1, ++EC2) {
1405 if (EC2 == EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001406 if (Context.Complain) {
1407 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1408 << Context.C2.getTypeDeclType(D2);
1409 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1410 << EC1->getDeclName()
1411 << EC1->getInitVal().toString(10);
1412 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1413 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001414 return false;
1415 }
1416
1417 llvm::APSInt Val1 = EC1->getInitVal();
1418 llvm::APSInt Val2 = EC2->getInitVal();
Eric Christopher6dcc3762012-07-15 00:23:57 +00001419 if (!llvm::APSInt::isSameValue(Val1, Val2) ||
Douglas Gregor3996e242010-02-15 22:01:00 +00001420 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001421 if (Context.Complain) {
1422 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1423 << Context.C2.getTypeDeclType(D2);
1424 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1425 << EC2->getDeclName()
1426 << EC2->getInitVal().toString(10);
1427 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1428 << EC1->getDeclName()
1429 << EC1->getInitVal().toString(10);
1430 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001431 return false;
1432 }
1433 }
1434
1435 if (EC2 != EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001436 if (Context.Complain) {
1437 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1438 << Context.C2.getTypeDeclType(D2);
1439 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1440 << EC2->getDeclName()
1441 << EC2->getInitVal().toString(10);
1442 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1443 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001444 return false;
1445 }
1446
1447 return true;
1448}
Douglas Gregora082a492010-11-30 19:14:50 +00001449
1450static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1451 TemplateParameterList *Params1,
1452 TemplateParameterList *Params2) {
1453 if (Params1->size() != Params2->size()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001454 if (Context.Complain) {
1455 Context.Diag2(Params2->getTemplateLoc(),
1456 diag::err_odr_different_num_template_parameters)
1457 << Params1->size() << Params2->size();
1458 Context.Diag1(Params1->getTemplateLoc(),
1459 diag::note_odr_template_parameter_list);
1460 }
Douglas Gregora082a492010-11-30 19:14:50 +00001461 return false;
1462 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001463
Douglas Gregora082a492010-11-30 19:14:50 +00001464 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1465 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001466 if (Context.Complain) {
1467 Context.Diag2(Params2->getParam(I)->getLocation(),
1468 diag::err_odr_different_template_parameter_kind);
1469 Context.Diag1(Params1->getParam(I)->getLocation(),
1470 diag::note_odr_template_parameter_here);
1471 }
Douglas Gregora082a492010-11-30 19:14:50 +00001472 return false;
1473 }
1474
1475 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1476 Params2->getParam(I))) {
1477
1478 return false;
1479 }
1480 }
1481
1482 return true;
1483}
1484
1485static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1486 TemplateTypeParmDecl *D1,
1487 TemplateTypeParmDecl *D2) {
1488 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001489 if (Context.Complain) {
1490 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1491 << D2->isParameterPack();
1492 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1493 << D1->isParameterPack();
1494 }
Douglas Gregora082a492010-11-30 19:14:50 +00001495 return false;
1496 }
1497
1498 return true;
1499}
1500
1501static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1502 NonTypeTemplateParmDecl *D1,
1503 NonTypeTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001504 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001505 if (Context.Complain) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001506 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1507 << D2->isParameterPack();
1508 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1509 << D1->isParameterPack();
1510 }
Douglas Gregora082a492010-11-30 19:14:50 +00001511 return false;
1512 }
Douglas Gregora082a492010-11-30 19:14:50 +00001513
1514 // Check types.
1515 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001516 if (Context.Complain) {
1517 Context.Diag2(D2->getLocation(),
1518 diag::err_odr_non_type_parameter_type_inconsistent)
1519 << D2->getType() << D1->getType();
1520 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1521 << D1->getType();
1522 }
Douglas Gregora082a492010-11-30 19:14:50 +00001523 return false;
1524 }
1525
1526 return true;
1527}
1528
1529static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1530 TemplateTemplateParmDecl *D1,
1531 TemplateTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001532 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001533 if (Context.Complain) {
1534 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1535 << D2->isParameterPack();
1536 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1537 << D1->isParameterPack();
1538 }
Douglas Gregora082a492010-11-30 19:14:50 +00001539 return false;
1540 }
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001541
Douglas Gregora082a492010-11-30 19:14:50 +00001542 // Check template parameter lists.
1543 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1544 D2->getTemplateParameters());
1545}
1546
1547static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1548 ClassTemplateDecl *D1,
1549 ClassTemplateDecl *D2) {
1550 // Check template parameters.
1551 if (!IsStructurallyEquivalent(Context,
1552 D1->getTemplateParameters(),
1553 D2->getTemplateParameters()))
1554 return false;
1555
1556 // Check the templated declaration.
1557 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1558 D2->getTemplatedDecl());
1559}
1560
Douglas Gregor3996e242010-02-15 22:01:00 +00001561/// \brief Determine structural equivalence of two declarations.
1562static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1563 Decl *D1, Decl *D2) {
1564 // FIXME: Check for known structural equivalences via a callback of some sort.
1565
Douglas Gregorb4964f72010-02-15 23:54:17 +00001566 // Check whether we already know that these two declarations are not
1567 // structurally equivalent.
1568 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1569 D2->getCanonicalDecl())))
1570 return false;
1571
Douglas Gregor3996e242010-02-15 22:01:00 +00001572 // Determine whether we've already produced a tentative equivalence for D1.
1573 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1574 if (EquivToD1)
1575 return EquivToD1 == D2->getCanonicalDecl();
1576
1577 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1578 EquivToD1 = D2->getCanonicalDecl();
1579 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1580 return true;
1581}
1582
1583bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1584 Decl *D2) {
1585 if (!::IsStructurallyEquivalent(*this, D1, D2))
1586 return false;
1587
1588 return !Finish();
1589}
1590
1591bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1592 QualType T2) {
1593 if (!::IsStructurallyEquivalent(*this, T1, T2))
1594 return false;
1595
1596 return !Finish();
1597}
1598
1599bool StructuralEquivalenceContext::Finish() {
1600 while (!DeclsToCheck.empty()) {
1601 // Check the next declaration.
1602 Decl *D1 = DeclsToCheck.front();
1603 DeclsToCheck.pop_front();
1604
1605 Decl *D2 = TentativeEquivalences[D1];
1606 assert(D2 && "Unrecorded tentative equivalence?");
1607
Douglas Gregorb4964f72010-02-15 23:54:17 +00001608 bool Equivalent = true;
1609
Douglas Gregor3996e242010-02-15 22:01:00 +00001610 // FIXME: Switch on all declaration kinds. For now, we're just going to
1611 // check the obvious ones.
1612 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1613 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1614 // Check for equivalent structure names.
1615 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001616 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1617 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001618 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001619 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1620 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001621 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1622 !::IsStructurallyEquivalent(*this, Record1, Record2))
1623 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001624 } else {
1625 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001626 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001627 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001628 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001629 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1630 // Check for equivalent enum names.
1631 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001632 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1633 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001634 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001635 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1636 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001637 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1638 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1639 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001640 } else {
1641 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001642 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001643 }
Richard Smithdda56e42011-04-15 14:24:37 +00001644 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1645 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001646 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001647 Typedef2->getIdentifier()) ||
1648 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001649 Typedef1->getUnderlyingType(),
1650 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001651 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001652 } else {
1653 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001654 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001655 }
Douglas Gregora082a492010-11-30 19:14:50 +00001656 } else if (ClassTemplateDecl *ClassTemplate1
1657 = dyn_cast<ClassTemplateDecl>(D1)) {
1658 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1659 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1660 ClassTemplate2->getIdentifier()) ||
1661 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1662 Equivalent = false;
1663 } else {
1664 // Class template/non-class-template mismatch.
1665 Equivalent = false;
1666 }
1667 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1668 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1669 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1670 Equivalent = false;
1671 } else {
1672 // Kind mismatch.
1673 Equivalent = false;
1674 }
1675 } else if (NonTypeTemplateParmDecl *NTTP1
1676 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1677 if (NonTypeTemplateParmDecl *NTTP2
1678 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1679 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1680 Equivalent = false;
1681 } else {
1682 // Kind mismatch.
1683 Equivalent = false;
1684 }
1685 } else if (TemplateTemplateParmDecl *TTP1
1686 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1687 if (TemplateTemplateParmDecl *TTP2
1688 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1689 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1690 Equivalent = false;
1691 } else {
1692 // Kind mismatch.
1693 Equivalent = false;
1694 }
1695 }
1696
Douglas Gregorb4964f72010-02-15 23:54:17 +00001697 if (!Equivalent) {
1698 // Note that these two declarations are not equivalent (and we already
1699 // know about it).
1700 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1701 D2->getCanonicalDecl()));
1702 return true;
1703 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001704 // FIXME: Check other declaration kinds!
1705 }
1706
1707 return false;
1708}
1709
1710//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001711// Import Types
1712//----------------------------------------------------------------------------
1713
John McCall424cec92011-01-19 06:33:43 +00001714QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +00001715 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1716 << T->getTypeClassName();
1717 return QualType();
1718}
1719
Gabor Horvath0866c2f2016-11-23 15:24:23 +00001720QualType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
1721 QualType UnderlyingType = Importer.Import(T->getValueType());
1722 if(UnderlyingType.isNull())
1723 return QualType();
1724
1725 return Importer.getToContext().getAtomicType(UnderlyingType);
1726}
1727
John McCall424cec92011-01-19 06:33:43 +00001728QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001729 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +00001730#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
1731 case BuiltinType::Id: \
1732 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +00001733#include "clang/Basic/OpenCLImageTypes.def"
John McCalle314e272011-10-18 21:02:43 +00001734#define SHARED_SINGLETON_TYPE(Expansion)
1735#define BUILTIN_TYPE(Id, SingletonId) \
1736 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1737#include "clang/AST/BuiltinTypes.def"
1738
1739 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1740 // context supports C++.
1741
1742 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1743 // context supports ObjC.
1744
Douglas Gregor96e578d2010-02-05 17:54:41 +00001745 case BuiltinType::Char_U:
1746 // The context we're importing from has an unsigned 'char'. If we're
1747 // importing into a context with a signed 'char', translate to
1748 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001749 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001750 return Importer.getToContext().UnsignedCharTy;
1751
1752 return Importer.getToContext().CharTy;
1753
Douglas Gregor96e578d2010-02-05 17:54:41 +00001754 case BuiltinType::Char_S:
1755 // The context we're importing from has an unsigned 'char'. If we're
1756 // importing into a context with a signed 'char', translate to
1757 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001758 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001759 return Importer.getToContext().SignedCharTy;
1760
1761 return Importer.getToContext().CharTy;
1762
Chris Lattnerad3467e2010-12-25 23:25:43 +00001763 case BuiltinType::WChar_S:
1764 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001765 // FIXME: If not in C++, shall we translate to the C equivalent of
1766 // wchar_t?
1767 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001768 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001769
1770 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001771}
1772
Aleksei Sidorina693b372016-09-28 10:16:56 +00001773QualType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
1774 QualType OrigT = Importer.Import(T->getOriginalType());
1775 if (OrigT.isNull())
1776 return QualType();
1777
1778 return Importer.getToContext().getDecayedType(OrigT);
1779}
1780
John McCall424cec92011-01-19 06:33:43 +00001781QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001782 QualType ToElementType = Importer.Import(T->getElementType());
1783 if (ToElementType.isNull())
1784 return QualType();
1785
1786 return Importer.getToContext().getComplexType(ToElementType);
1787}
1788
John McCall424cec92011-01-19 06:33:43 +00001789QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001790 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1791 if (ToPointeeType.isNull())
1792 return QualType();
1793
1794 return Importer.getToContext().getPointerType(ToPointeeType);
1795}
1796
John McCall424cec92011-01-19 06:33:43 +00001797QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001798 // FIXME: Check for blocks support in "to" context.
1799 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1800 if (ToPointeeType.isNull())
1801 return QualType();
1802
1803 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1804}
1805
John McCall424cec92011-01-19 06:33:43 +00001806QualType
1807ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001808 // FIXME: Check for C++ support in "to" context.
1809 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1810 if (ToPointeeType.isNull())
1811 return QualType();
1812
1813 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1814}
1815
John McCall424cec92011-01-19 06:33:43 +00001816QualType
1817ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001818 // FIXME: Check for C++0x support in "to" context.
1819 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1820 if (ToPointeeType.isNull())
1821 return QualType();
1822
1823 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1824}
1825
John McCall424cec92011-01-19 06:33:43 +00001826QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001827 // FIXME: Check for C++ support in "to" context.
1828 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1829 if (ToPointeeType.isNull())
1830 return QualType();
1831
1832 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1833 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1834 ClassType.getTypePtr());
1835}
1836
John McCall424cec92011-01-19 06:33:43 +00001837QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001838 QualType ToElementType = Importer.Import(T->getElementType());
1839 if (ToElementType.isNull())
1840 return QualType();
1841
1842 return Importer.getToContext().getConstantArrayType(ToElementType,
1843 T->getSize(),
1844 T->getSizeModifier(),
1845 T->getIndexTypeCVRQualifiers());
1846}
1847
John McCall424cec92011-01-19 06:33:43 +00001848QualType
1849ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001850 QualType ToElementType = Importer.Import(T->getElementType());
1851 if (ToElementType.isNull())
1852 return QualType();
1853
1854 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1855 T->getSizeModifier(),
1856 T->getIndexTypeCVRQualifiers());
1857}
1858
John McCall424cec92011-01-19 06:33:43 +00001859QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001860 QualType ToElementType = Importer.Import(T->getElementType());
1861 if (ToElementType.isNull())
1862 return QualType();
1863
1864 Expr *Size = Importer.Import(T->getSizeExpr());
1865 if (!Size)
1866 return QualType();
1867
1868 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1869 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1870 T->getSizeModifier(),
1871 T->getIndexTypeCVRQualifiers(),
1872 Brackets);
1873}
1874
John McCall424cec92011-01-19 06:33:43 +00001875QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001876 QualType ToElementType = Importer.Import(T->getElementType());
1877 if (ToElementType.isNull())
1878 return QualType();
1879
1880 return Importer.getToContext().getVectorType(ToElementType,
1881 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001882 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001883}
1884
John McCall424cec92011-01-19 06:33:43 +00001885QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001886 QualType ToElementType = Importer.Import(T->getElementType());
1887 if (ToElementType.isNull())
1888 return QualType();
1889
1890 return Importer.getToContext().getExtVectorType(ToElementType,
1891 T->getNumElements());
1892}
1893
John McCall424cec92011-01-19 06:33:43 +00001894QualType
1895ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001896 // FIXME: What happens if we're importing a function without a prototype
1897 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +00001898 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001899 if (ToResultType.isNull())
1900 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001901
Douglas Gregor96e578d2010-02-05 17:54:41 +00001902 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001903 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001904}
1905
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001906QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +00001907 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001908 if (ToResultType.isNull())
1909 return QualType();
1910
1911 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001912 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00001913 for (const auto &A : T->param_types()) {
1914 QualType ArgType = Importer.Import(A);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001915 if (ArgType.isNull())
1916 return QualType();
1917 ArgTypes.push_back(ArgType);
1918 }
1919
1920 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001921 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +00001922 for (const auto &E : T->exceptions()) {
1923 QualType ExceptionType = Importer.Import(E);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001924 if (ExceptionType.isNull())
1925 return QualType();
1926 ExceptionTypes.push_back(ExceptionType);
1927 }
John McCalldb40c7f2010-12-14 08:05:40 +00001928
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001929 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1930 FunctionProtoType::ExtProtoInfo ToEPI;
1931
1932 ToEPI.ExtInfo = FromEPI.ExtInfo;
1933 ToEPI.Variadic = FromEPI.Variadic;
1934 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1935 ToEPI.TypeQuals = FromEPI.TypeQuals;
1936 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +00001937 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1938 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
1939 ToEPI.ExceptionSpec.NoexceptExpr =
1940 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
1941 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
1942 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
1943 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
1944 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001945
Jordan Rose5c382722013-03-08 21:51:21 +00001946 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001947}
1948
Sean Callananda6df8a2011-08-11 16:56:07 +00001949QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1950 QualType ToInnerType = Importer.Import(T->getInnerType());
1951 if (ToInnerType.isNull())
1952 return QualType();
1953
1954 return Importer.getToContext().getParenType(ToInnerType);
1955}
1956
John McCall424cec92011-01-19 06:33:43 +00001957QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +00001958 TypedefNameDecl *ToDecl
1959 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001960 if (!ToDecl)
1961 return QualType();
1962
1963 return Importer.getToContext().getTypeDeclType(ToDecl);
1964}
1965
John McCall424cec92011-01-19 06:33:43 +00001966QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001967 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1968 if (!ToExpr)
1969 return QualType();
1970
1971 return Importer.getToContext().getTypeOfExprType(ToExpr);
1972}
1973
John McCall424cec92011-01-19 06:33:43 +00001974QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001975 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1976 if (ToUnderlyingType.isNull())
1977 return QualType();
1978
1979 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1980}
1981
John McCall424cec92011-01-19 06:33:43 +00001982QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001983 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +00001984 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1985 if (!ToExpr)
1986 return QualType();
1987
Douglas Gregor81495f32012-02-12 18:42:33 +00001988 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1989 if (UnderlyingType.isNull())
1990 return QualType();
1991
1992 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001993}
1994
Alexis Hunte852b102011-05-24 22:41:36 +00001995QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1996 QualType ToBaseType = Importer.Import(T->getBaseType());
1997 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1998 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1999 return QualType();
2000
2001 return Importer.getToContext().getUnaryTransformType(ToBaseType,
2002 ToUnderlyingType,
2003 T->getUTTKind());
2004}
2005
Richard Smith30482bc2011-02-20 03:19:35 +00002006QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00002007 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +00002008 QualType FromDeduced = T->getDeducedType();
2009 QualType ToDeduced;
2010 if (!FromDeduced.isNull()) {
2011 ToDeduced = Importer.Import(FromDeduced);
2012 if (ToDeduced.isNull())
2013 return QualType();
2014 }
2015
Richard Smithe301ba22015-11-11 02:02:15 +00002016 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +00002017 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +00002018}
2019
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002020QualType ASTNodeImporter::VisitInjectedClassNameType(
2021 const InjectedClassNameType *T) {
2022 CXXRecordDecl *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl()));
2023 if (!D)
2024 return QualType();
2025
2026 QualType InjType = Importer.Import(T->getInjectedSpecializationType());
2027 if (InjType.isNull())
2028 return QualType();
2029
2030 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
2031 // See comments in InjectedClassNameType definition for details
2032 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
2033 enum {
2034 TypeAlignmentInBits = 4,
2035 TypeAlignment = 1 << TypeAlignmentInBits
2036 };
2037
2038 return QualType(new (Importer.getToContext(), TypeAlignment)
2039 InjectedClassNameType(D, InjType), 0);
2040}
2041
John McCall424cec92011-01-19 06:33:43 +00002042QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00002043 RecordDecl *ToDecl
2044 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
2045 if (!ToDecl)
2046 return QualType();
2047
2048 return Importer.getToContext().getTagDeclType(ToDecl);
2049}
2050
John McCall424cec92011-01-19 06:33:43 +00002051QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00002052 EnumDecl *ToDecl
2053 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
2054 if (!ToDecl)
2055 return QualType();
2056
2057 return Importer.getToContext().getTagDeclType(ToDecl);
2058}
2059
Sean Callanan72fe0852015-04-02 23:50:08 +00002060QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
2061 QualType FromModifiedType = T->getModifiedType();
2062 QualType FromEquivalentType = T->getEquivalentType();
2063 QualType ToModifiedType;
2064 QualType ToEquivalentType;
2065
2066 if (!FromModifiedType.isNull()) {
2067 ToModifiedType = Importer.Import(FromModifiedType);
2068 if (ToModifiedType.isNull())
2069 return QualType();
2070 }
2071 if (!FromEquivalentType.isNull()) {
2072 ToEquivalentType = Importer.Import(FromEquivalentType);
2073 if (ToEquivalentType.isNull())
2074 return QualType();
2075 }
2076
2077 return Importer.getToContext().getAttributedType(T->getAttrKind(),
2078 ToModifiedType, ToEquivalentType);
2079}
2080
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002081
2082QualType ASTNodeImporter::VisitTemplateTypeParmType(
2083 const TemplateTypeParmType *T) {
2084 TemplateTypeParmDecl *ParmDecl =
2085 cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl()));
2086 if (!ParmDecl && T->getDecl())
2087 return QualType();
2088
2089 return Importer.getToContext().getTemplateTypeParmType(
2090 T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl);
2091}
2092
Aleksei Sidorin855086d2017-01-23 09:30:36 +00002093QualType ASTNodeImporter::VisitSubstTemplateTypeParmType(
2094 const SubstTemplateTypeParmType *T) {
2095 const TemplateTypeParmType *Replaced =
2096 cast_or_null<TemplateTypeParmType>(Importer.Import(
2097 QualType(T->getReplacedParameter(), 0)).getTypePtr());
2098 if (!Replaced)
2099 return QualType();
2100
2101 QualType Replacement = Importer.Import(T->getReplacementType());
2102 if (Replacement.isNull())
2103 return QualType();
2104 Replacement = Replacement.getCanonicalType();
2105
2106 return Importer.getToContext().getSubstTemplateTypeParmType(
2107 Replaced, Replacement);
2108}
2109
Douglas Gregore2e50d332010-12-01 01:36:18 +00002110QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00002111 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00002112 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
2113 if (ToTemplate.isNull())
2114 return QualType();
2115
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002116 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002117 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
2118 return QualType();
2119
2120 QualType ToCanonType;
2121 if (!QualType(T, 0).isCanonical()) {
2122 QualType FromCanonType
2123 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
2124 ToCanonType =Importer.Import(FromCanonType);
2125 if (ToCanonType.isNull())
2126 return QualType();
2127 }
2128 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
David Majnemer6fbeee32016-07-07 04:43:07 +00002129 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +00002130 ToCanonType);
2131}
2132
John McCall424cec92011-01-19 06:33:43 +00002133QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Craig Topper36250ad2014-05-12 05:36:57 +00002134 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara6150c882010-05-11 21:36:43 +00002135 // Note: the qualifier in an ElaboratedType is optional.
2136 if (T->getQualifier()) {
2137 ToQualifier = Importer.Import(T->getQualifier());
2138 if (!ToQualifier)
2139 return QualType();
2140 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00002141
2142 QualType ToNamedType = Importer.Import(T->getNamedType());
2143 if (ToNamedType.isNull())
2144 return QualType();
2145
Abramo Bagnara6150c882010-05-11 21:36:43 +00002146 return Importer.getToContext().getElaboratedType(T->getKeyword(),
2147 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00002148}
2149
John McCall424cec92011-01-19 06:33:43 +00002150QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00002151 ObjCInterfaceDecl *Class
2152 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
2153 if (!Class)
2154 return QualType();
2155
John McCall8b07ec22010-05-15 11:32:37 +00002156 return Importer.getToContext().getObjCInterfaceType(Class);
2157}
2158
John McCall424cec92011-01-19 06:33:43 +00002159QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00002160 QualType ToBaseType = Importer.Import(T->getBaseType());
2161 if (ToBaseType.isNull())
2162 return QualType();
2163
Douglas Gregore9d95f12015-07-07 03:57:35 +00002164 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00002165 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00002166 QualType ImportedTypeArg = Importer.Import(TypeArg);
2167 if (ImportedTypeArg.isNull())
2168 return QualType();
2169
2170 TypeArgs.push_back(ImportedTypeArg);
2171 }
2172
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002173 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00002174 for (auto *P : T->quals()) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00002175 ObjCProtocolDecl *Protocol
Aaron Ballman1683f7b2014-03-17 15:55:30 +00002176 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +00002177 if (!Protocol)
2178 return QualType();
2179 Protocols.push_back(Protocol);
2180 }
2181
Douglas Gregore9d95f12015-07-07 03:57:35 +00002182 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00002183 Protocols,
2184 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00002185}
2186
John McCall424cec92011-01-19 06:33:43 +00002187QualType
2188ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00002189 QualType ToPointeeType = Importer.Import(T->getPointeeType());
2190 if (ToPointeeType.isNull())
2191 return QualType();
2192
John McCall8b07ec22010-05-15 11:32:37 +00002193 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00002194}
2195
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002196//----------------------------------------------------------------------------
2197// Import Declarations
2198//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002199bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
2200 DeclContext *&LexicalDC,
2201 DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +00002202 NamedDecl *&ToD,
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002203 SourceLocation &Loc) {
2204 // Import the context of this declaration.
2205 DC = Importer.ImportContext(D->getDeclContext());
2206 if (!DC)
2207 return true;
2208
2209 LexicalDC = DC;
2210 if (D->getDeclContext() != D->getLexicalDeclContext()) {
2211 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
2212 if (!LexicalDC)
2213 return true;
2214 }
2215
2216 // Import the name of this declaration.
2217 Name = Importer.Import(D->getDeclName());
2218 if (D->getDeclName() && !Name)
2219 return true;
2220
2221 // Import the location of this declaration.
2222 Loc = Importer.Import(D->getLocation());
Sean Callanan59721b32015-04-28 18:41:46 +00002223 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002224 return false;
2225}
2226
Douglas Gregord451ea92011-07-29 23:31:30 +00002227void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
2228 if (!FromD)
2229 return;
2230
2231 if (!ToD) {
2232 ToD = Importer.Import(FromD);
2233 if (!ToD)
2234 return;
2235 }
2236
2237 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
2238 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +00002239 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +00002240 ImportDefinition(FromRecord, ToRecord);
2241 }
2242 }
2243 return;
2244 }
2245
2246 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
2247 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
2248 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
2249 ImportDefinition(FromEnum, ToEnum);
2250 }
2251 }
2252 return;
2253 }
2254}
2255
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002256void
2257ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
2258 DeclarationNameInfo& To) {
2259 // NOTE: To.Name and To.Loc are already imported.
2260 // We only have to import To.LocInfo.
2261 switch (To.getName().getNameKind()) {
2262 case DeclarationName::Identifier:
2263 case DeclarationName::ObjCZeroArgSelector:
2264 case DeclarationName::ObjCOneArgSelector:
2265 case DeclarationName::ObjCMultiArgSelector:
2266 case DeclarationName::CXXUsingDirective:
2267 return;
2268
2269 case DeclarationName::CXXOperatorName: {
2270 SourceRange Range = From.getCXXOperatorNameRange();
2271 To.setCXXOperatorNameRange(Importer.Import(Range));
2272 return;
2273 }
2274 case DeclarationName::CXXLiteralOperatorName: {
2275 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
2276 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
2277 return;
2278 }
2279 case DeclarationName::CXXConstructorName:
2280 case DeclarationName::CXXDestructorName:
2281 case DeclarationName::CXXConversionFunctionName: {
2282 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
2283 To.setNamedTypeInfo(Importer.Import(FromTInfo));
2284 return;
2285 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002286 }
Douglas Gregor07216d12011-11-02 20:52:01 +00002287 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002288}
2289
Douglas Gregor2e15c842012-02-01 21:00:38 +00002290void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00002291 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00002292 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00002293 return;
2294 }
2295
Aaron Ballman629afae2014-03-07 19:56:05 +00002296 for (auto *From : FromDC->decls())
2297 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +00002298}
2299
Douglas Gregord451ea92011-07-29 23:31:30 +00002300bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00002301 ImportDefinitionKind Kind) {
2302 if (To->getDefinition() || To->isBeingDefined()) {
2303 if (Kind == IDK_Everything)
2304 ImportDeclContext(From, /*ForceImport=*/true);
2305
Douglas Gregore2e50d332010-12-01 01:36:18 +00002306 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00002307 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00002308
2309 To->startDefinition();
2310
2311 // Add base classes.
2312 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
2313 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002314
2315 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
2316 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
2317 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00002318 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002319 ToData.Aggregate = FromData.Aggregate;
2320 ToData.PlainOldData = FromData.PlainOldData;
2321 ToData.Empty = FromData.Empty;
2322 ToData.Polymorphic = FromData.Polymorphic;
2323 ToData.Abstract = FromData.Abstract;
2324 ToData.IsStandardLayout = FromData.IsStandardLayout;
2325 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
2326 ToData.HasPrivateFields = FromData.HasPrivateFields;
2327 ToData.HasProtectedFields = FromData.HasProtectedFields;
2328 ToData.HasPublicFields = FromData.HasPublicFields;
2329 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00002330 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00002331 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00002332 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00002333 ToData.HasUninitializedReferenceMember
2334 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00002335 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00002336 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
2337 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +00002338 ToData.NeedOverloadResolutionForMoveConstructor
2339 = FromData.NeedOverloadResolutionForMoveConstructor;
2340 ToData.NeedOverloadResolutionForMoveAssignment
2341 = FromData.NeedOverloadResolutionForMoveAssignment;
2342 ToData.NeedOverloadResolutionForDestructor
2343 = FromData.NeedOverloadResolutionForDestructor;
2344 ToData.DefaultedMoveConstructorIsDeleted
2345 = FromData.DefaultedMoveConstructorIsDeleted;
2346 ToData.DefaultedMoveAssignmentIsDeleted
2347 = FromData.DefaultedMoveAssignmentIsDeleted;
2348 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00002349 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
2350 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002351 ToData.HasConstexprNonCopyMoveConstructor
2352 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00002353 ToData.HasDefaultedDefaultConstructor
2354 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00002355 ToData.DefaultedDefaultConstructorIsConstexpr
2356 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00002357 ToData.HasConstexprDefaultConstructor
2358 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002359 ToData.HasNonLiteralTypeFieldsOrBases
2360 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00002361 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002362 ToData.UserProvidedDefaultConstructor
2363 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00002364 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smith1c33fe82012-11-28 06:23:12 +00002365 ToData.ImplicitCopyConstructorHasConstParam
2366 = FromData.ImplicitCopyConstructorHasConstParam;
2367 ToData.ImplicitCopyAssignmentHasConstParam
2368 = FromData.ImplicitCopyAssignmentHasConstParam;
2369 ToData.HasDeclaredCopyConstructorWithConstParam
2370 = FromData.HasDeclaredCopyConstructorWithConstParam;
2371 ToData.HasDeclaredCopyAssignmentWithConstParam
2372 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00002373 ToData.IsLambda = FromData.IsLambda;
2374
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002375 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00002376 for (const auto &Base1 : FromCXX->bases()) {
2377 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +00002378 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00002379 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00002380
2381 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00002382 if (Base1.isPackExpansion())
2383 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00002384
2385 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00002386 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Douglas Gregord451ea92011-07-29 23:31:30 +00002387
Douglas Gregore2e50d332010-12-01 01:36:18 +00002388 Bases.push_back(
2389 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00002390 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
2391 Base1.isVirtual(),
2392 Base1.isBaseOfClass(),
2393 Base1.getAccessSpecifierAsWritten(),
2394 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00002395 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00002396 }
2397 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00002398 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00002399 }
2400
Douglas Gregor2e15c842012-02-01 21:00:38 +00002401 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00002402 ImportDeclContext(From, /*ForceImport=*/true);
2403
Douglas Gregore2e50d332010-12-01 01:36:18 +00002404 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00002405 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002406}
2407
Larisse Voufo39a1e502013-08-06 01:03:05 +00002408bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
2409 ImportDefinitionKind Kind) {
Sean Callanan59721b32015-04-28 18:41:46 +00002410 if (To->getAnyInitializer())
Larisse Voufo39a1e502013-08-06 01:03:05 +00002411 return false;
2412
2413 // FIXME: Can we really import any initializer? Alternatively, we could force
2414 // ourselves to import every declaration of a variable and then only use
2415 // getInit() here.
2416 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
2417
2418 // FIXME: Other bits to merge?
2419
2420 return false;
2421}
2422
Douglas Gregord451ea92011-07-29 23:31:30 +00002423bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00002424 ImportDefinitionKind Kind) {
2425 if (To->getDefinition() || To->isBeingDefined()) {
2426 if (Kind == IDK_Everything)
2427 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002428 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002429 }
Douglas Gregord451ea92011-07-29 23:31:30 +00002430
2431 To->startDefinition();
2432
2433 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
2434 if (T.isNull())
2435 return true;
2436
2437 QualType ToPromotionType = Importer.Import(From->getPromotionType());
2438 if (ToPromotionType.isNull())
2439 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002440
2441 if (shouldForceImportDeclContext(Kind))
2442 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002443
2444 // FIXME: we might need to merge the number of positive or negative bits
2445 // if the enumerator lists don't match.
2446 To->completeDefinition(T, ToPromotionType,
2447 From->getNumPositiveBits(),
2448 From->getNumNegativeBits());
2449 return false;
2450}
2451
Douglas Gregora082a492010-11-30 19:14:50 +00002452TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
2453 TemplateParameterList *Params) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00002454 SmallVector<NamedDecl *, 4> ToParams(Params->size());
2455 if (ImportContainerChecked(*Params, ToParams))
2456 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00002457
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00002458 Expr *ToRequiresClause;
2459 if (Expr *const R = Params->getRequiresClause()) {
2460 ToRequiresClause = Importer.Import(R);
2461 if (!ToRequiresClause)
2462 return nullptr;
2463 } else {
2464 ToRequiresClause = nullptr;
2465 }
2466
Douglas Gregora082a492010-11-30 19:14:50 +00002467 return TemplateParameterList::Create(Importer.getToContext(),
2468 Importer.Import(Params->getTemplateLoc()),
2469 Importer.Import(Params->getLAngleLoc()),
David Majnemer902f8c62015-12-27 07:16:27 +00002470 ToParams,
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00002471 Importer.Import(Params->getRAngleLoc()),
2472 ToRequiresClause);
Douglas Gregora082a492010-11-30 19:14:50 +00002473}
2474
Douglas Gregore2e50d332010-12-01 01:36:18 +00002475TemplateArgument
2476ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
2477 switch (From.getKind()) {
2478 case TemplateArgument::Null:
2479 return TemplateArgument();
2480
2481 case TemplateArgument::Type: {
2482 QualType ToType = Importer.Import(From.getAsType());
2483 if (ToType.isNull())
2484 return TemplateArgument();
2485 return TemplateArgument(ToType);
2486 }
2487
2488 case TemplateArgument::Integral: {
2489 QualType ToType = Importer.Import(From.getIntegralType());
2490 if (ToType.isNull())
2491 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00002492 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00002493 }
2494
Eli Friedmanb826a002012-09-26 02:36:12 +00002495 case TemplateArgument::Declaration: {
David Blaikie3c7dd6b2014-10-22 19:54:16 +00002496 ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
2497 QualType ToType = Importer.Import(From.getParamTypeForDecl());
2498 if (!To || ToType.isNull())
2499 return TemplateArgument();
2500 return TemplateArgument(To, ToType);
Eli Friedmanb826a002012-09-26 02:36:12 +00002501 }
2502
2503 case TemplateArgument::NullPtr: {
2504 QualType ToType = Importer.Import(From.getNullPtrType());
2505 if (ToType.isNull())
2506 return TemplateArgument();
2507 return TemplateArgument(ToType, /*isNullPtr*/true);
2508 }
2509
Douglas Gregore2e50d332010-12-01 01:36:18 +00002510 case TemplateArgument::Template: {
2511 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
2512 if (ToTemplate.isNull())
2513 return TemplateArgument();
2514
2515 return TemplateArgument(ToTemplate);
2516 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002517
2518 case TemplateArgument::TemplateExpansion: {
2519 TemplateName ToTemplate
2520 = Importer.Import(From.getAsTemplateOrTemplatePattern());
2521 if (ToTemplate.isNull())
2522 return TemplateArgument();
2523
Douglas Gregore1d60df2011-01-14 23:41:42 +00002524 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002525 }
2526
Douglas Gregore2e50d332010-12-01 01:36:18 +00002527 case TemplateArgument::Expression:
2528 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2529 return TemplateArgument(ToExpr);
2530 return TemplateArgument();
2531
2532 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002533 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002534 ToPack.reserve(From.pack_size());
2535 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2536 return TemplateArgument();
Benjamin Kramercce63472015-08-05 09:40:22 +00002537
2538 return TemplateArgument(
2539 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00002540 }
2541 }
2542
2543 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00002544}
2545
Aleksei Sidorina693b372016-09-28 10:16:56 +00002546TemplateArgumentLoc ASTNodeImporter::ImportTemplateArgumentLoc(
2547 const TemplateArgumentLoc &TALoc, bool &Error) {
2548 Error = false;
2549 TemplateArgument Arg = ImportTemplateArgument(TALoc.getArgument());
2550 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
2551 TemplateArgumentLocInfo ToInfo;
2552 if (Arg.getKind() == TemplateArgument::Expression) {
2553 Expr *E = Importer.Import(FromInfo.getAsExpr());
2554 ToInfo = TemplateArgumentLocInfo(E);
2555 if (!E)
2556 Error = true;
2557 } else if (Arg.getKind() == TemplateArgument::Type) {
2558 if (TypeSourceInfo *TSI = Importer.Import(FromInfo.getAsTypeSourceInfo()))
2559 ToInfo = TemplateArgumentLocInfo(TSI);
2560 else
2561 Error = true;
2562 } else {
2563 ToInfo = TemplateArgumentLocInfo(
2564 Importer.Import(FromInfo.getTemplateQualifierLoc()),
2565 Importer.Import(FromInfo.getTemplateNameLoc()),
2566 Importer.Import(FromInfo.getTemplateEllipsisLoc()));
2567 }
2568 return TemplateArgumentLoc(Arg, ToInfo);
2569}
2570
Douglas Gregore2e50d332010-12-01 01:36:18 +00002571bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2572 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002573 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00002574 for (unsigned I = 0; I != NumFromArgs; ++I) {
2575 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2576 if (To.isNull() && !FromArgs[I].isNull())
2577 return true;
2578
2579 ToArgs.push_back(To);
2580 }
2581
2582 return false;
2583}
2584
Douglas Gregor5c73e912010-02-11 00:48:18 +00002585bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002586 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00002587 // Eliminate a potential failure point where we attempt to re-import
2588 // something we're trying to import while completing ToRecord.
2589 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
2590 if (ToOrigin) {
2591 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
2592 if (ToOriginRecord)
2593 ToRecord = ToOriginRecord;
2594 }
2595
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002596 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00002597 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002598 Importer.getNonEquivalentDecls(),
2599 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002600 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002601}
2602
Larisse Voufo39a1e502013-08-06 01:03:05 +00002603bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
2604 bool Complain) {
2605 StructuralEquivalenceContext Ctx(
2606 Importer.getFromContext(), Importer.getToContext(),
2607 Importer.getNonEquivalentDecls(), false, Complain);
2608 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
2609}
2610
Douglas Gregor98c10182010-02-12 22:17:39 +00002611bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002612 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002613 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002614 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002615 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002616}
2617
Douglas Gregor91155082012-11-14 22:29:20 +00002618bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
2619 EnumConstantDecl *ToEC)
2620{
2621 const llvm::APSInt &FromVal = FromEC->getInitVal();
2622 const llvm::APSInt &ToVal = ToEC->getInitVal();
2623
2624 return FromVal.isSigned() == ToVal.isSigned() &&
2625 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2626 FromVal == ToVal;
2627}
2628
2629bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002630 ClassTemplateDecl *To) {
2631 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2632 Importer.getToContext(),
2633 Importer.getNonEquivalentDecls());
2634 return Ctx.IsStructurallyEquivalent(From, To);
2635}
2636
Larisse Voufo39a1e502013-08-06 01:03:05 +00002637bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2638 VarTemplateDecl *To) {
2639 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2640 Importer.getToContext(),
2641 Importer.getNonEquivalentDecls());
2642 return Ctx.IsStructurallyEquivalent(From, To);
2643}
2644
Douglas Gregore4c83e42010-02-09 22:48:33 +00002645Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002646 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002647 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00002648 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00002649}
2650
Sean Callanan65198272011-11-17 23:20:56 +00002651Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2652 TranslationUnitDecl *ToD =
2653 Importer.getToContext().getTranslationUnitDecl();
2654
2655 Importer.Imported(D, ToD);
2656
2657 return ToD;
2658}
2659
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002660Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
2661
2662 SourceLocation Loc = Importer.Import(D->getLocation());
2663 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
2664
2665 // Import the context of this declaration.
2666 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2667 if (!DC)
2668 return nullptr;
2669
2670 AccessSpecDecl *accessSpecDecl
2671 = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(),
2672 DC, Loc, ColonLoc);
2673
2674 if (!accessSpecDecl)
2675 return nullptr;
2676
2677 // Lexical DeclContext and Semantic DeclContext
2678 // is always the same for the accessSpec.
2679 accessSpecDecl->setLexicalDeclContext(DC);
2680 DC->addDeclInternal(accessSpecDecl);
2681
2682 return accessSpecDecl;
2683}
2684
Aleksei Sidorina693b372016-09-28 10:16:56 +00002685Decl *ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
2686 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2687 if (!DC)
2688 return nullptr;
2689
2690 DeclContext *LexicalDC = DC;
2691
2692 // Import the location of this declaration.
2693 SourceLocation Loc = Importer.Import(D->getLocation());
2694
2695 Expr *AssertExpr = Importer.Import(D->getAssertExpr());
2696 if (!AssertExpr)
2697 return nullptr;
2698
2699 StringLiteral *FromMsg = D->getMessage();
2700 StringLiteral *ToMsg = cast_or_null<StringLiteral>(Importer.Import(FromMsg));
2701 if (!ToMsg && FromMsg)
2702 return nullptr;
2703
2704 StaticAssertDecl *ToD = StaticAssertDecl::Create(
2705 Importer.getToContext(), DC, Loc, AssertExpr, ToMsg,
2706 Importer.Import(D->getRParenLoc()), D->isFailed());
2707
2708 ToD->setLexicalDeclContext(LexicalDC);
2709 LexicalDC->addDeclInternal(ToD);
2710 Importer.Imported(D, ToD);
2711 return ToD;
2712}
2713
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002714Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2715 // Import the major distinguishing characteristics of this namespace.
2716 DeclContext *DC, *LexicalDC;
2717 DeclarationName Name;
2718 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002719 NamedDecl *ToD;
2720 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002721 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002722 if (ToD)
2723 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002724
2725 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002726 if (!Name) {
2727 // This is an anonymous namespace. Adopt an existing anonymous
2728 // namespace if we can.
2729 // FIXME: Not testable.
2730 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2731 MergeWithNamespace = TU->getAnonymousNamespace();
2732 else
2733 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2734 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002735 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002736 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002737 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002738 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2739 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002740 continue;
2741
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002742 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002743 MergeWithNamespace = FoundNS;
2744 ConflictingDecls.clear();
2745 break;
2746 }
2747
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002748 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002749 }
2750
2751 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002752 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002753 ConflictingDecls.data(),
2754 ConflictingDecls.size());
2755 }
2756 }
2757
2758 // Create the "to" namespace, if needed.
2759 NamespaceDecl *ToNamespace = MergeWithNamespace;
2760 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002761 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002762 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002763 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00002764 Loc, Name.getAsIdentifierInfo(),
Craig Topper36250ad2014-05-12 05:36:57 +00002765 /*PrevDecl=*/nullptr);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002766 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002767 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002768
2769 // If this is an anonymous namespace, register it as the anonymous
2770 // namespace within its context.
2771 if (!Name) {
2772 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2773 TU->setAnonymousNamespace(ToNamespace);
2774 else
2775 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2776 }
2777 }
2778 Importer.Imported(D, ToNamespace);
2779
2780 ImportDeclContext(D);
2781
2782 return ToNamespace;
2783}
2784
Richard Smithdda56e42011-04-15 14:24:37 +00002785Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002786 // Import the major distinguishing characteristics of this typedef.
2787 DeclContext *DC, *LexicalDC;
2788 DeclarationName Name;
2789 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002790 NamedDecl *ToD;
2791 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002792 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002793 if (ToD)
2794 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002795
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002796 // If this typedef is not in block scope, determine whether we've
2797 // seen a typedef with the same name (that we can merge with) or any
2798 // other entity by that name (which name lookup could conflict with).
2799 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002800 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002801 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002802 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002803 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002804 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2805 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002806 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00002807 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002808 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002809 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2810 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002811 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002812 }
2813
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002814 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002815 }
2816
2817 if (!ConflictingDecls.empty()) {
2818 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2819 ConflictingDecls.data(),
2820 ConflictingDecls.size());
2821 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002822 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002823 }
2824 }
2825
Douglas Gregorb4964f72010-02-15 23:54:17 +00002826 // Import the underlying type of this typedef;
2827 QualType T = Importer.Import(D->getUnderlyingType());
2828 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002829 return nullptr;
2830
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002831 // Create the new typedef node.
2832 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002833 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00002834 TypedefNameDecl *ToTypedef;
2835 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002836 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2837 StartL, Loc,
2838 Name.getAsIdentifierInfo(),
2839 TInfo);
2840 else
Richard Smithdda56e42011-04-15 14:24:37 +00002841 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2842 StartL, Loc,
2843 Name.getAsIdentifierInfo(),
2844 TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002845
Douglas Gregordd483172010-02-22 17:42:47 +00002846 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002847 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002848 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00002849 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002850
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002851 return ToTypedef;
2852}
2853
Richard Smithdda56e42011-04-15 14:24:37 +00002854Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2855 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2856}
2857
2858Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2859 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2860}
2861
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002862Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
2863 // Import the major distinguishing characteristics of this label.
2864 DeclContext *DC, *LexicalDC;
2865 DeclarationName Name;
2866 SourceLocation Loc;
2867 NamedDecl *ToD;
2868 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2869 return nullptr;
2870 if (ToD)
2871 return ToD;
2872
2873 assert(LexicalDC->isFunctionOrMethod());
2874
2875 LabelDecl *ToLabel = D->isGnuLocal()
2876 ? LabelDecl::Create(Importer.getToContext(),
2877 DC, Importer.Import(D->getLocation()),
2878 Name.getAsIdentifierInfo(),
2879 Importer.Import(D->getLocStart()))
2880 : LabelDecl::Create(Importer.getToContext(),
2881 DC, Importer.Import(D->getLocation()),
2882 Name.getAsIdentifierInfo());
2883 Importer.Imported(D, ToLabel);
2884
2885 LabelStmt *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt()));
2886 if (!Label)
2887 return nullptr;
2888
2889 ToLabel->setStmt(Label);
2890 ToLabel->setLexicalDeclContext(LexicalDC);
2891 LexicalDC->addDeclInternal(ToLabel);
2892 return ToLabel;
2893}
2894
Douglas Gregor98c10182010-02-12 22:17:39 +00002895Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2896 // Import the major distinguishing characteristics of this enum.
2897 DeclContext *DC, *LexicalDC;
2898 DeclarationName Name;
2899 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002900 NamedDecl *ToD;
2901 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002902 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002903 if (ToD)
2904 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002905
Douglas Gregor98c10182010-02-12 22:17:39 +00002906 // Figure out what enum name we're looking for.
2907 unsigned IDNS = Decl::IDNS_Tag;
2908 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002909 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2910 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002911 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002912 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002913 IDNS |= Decl::IDNS_Ordinary;
2914
2915 // We may already have an enum of the same name; try to find and match it.
2916 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002917 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002918 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002919 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002920 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2921 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002922 continue;
2923
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002924 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002925 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002926 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2927 Found = Tag->getDecl();
2928 }
2929
2930 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002931 if (IsStructuralMatch(D, FoundEnum))
2932 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002933 }
2934
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002935 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002936 }
2937
2938 if (!ConflictingDecls.empty()) {
2939 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2940 ConflictingDecls.data(),
2941 ConflictingDecls.size());
2942 }
2943 }
2944
2945 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002946 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2947 Importer.Import(D->getLocStart()),
Craig Topper36250ad2014-05-12 05:36:57 +00002948 Loc, Name.getAsIdentifierInfo(), nullptr,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002949 D->isScoped(), D->isScopedUsingClassTag(),
2950 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002951 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002952 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002953 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002954 D2->setLexicalDeclContext(LexicalDC);
2955 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00002956 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002957
2958 // Import the integer type.
2959 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2960 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002961 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00002962 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002963
2964 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002965 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00002966 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002967
Douglas Gregor3996e242010-02-15 22:01:00 +00002968 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002969}
2970
Douglas Gregor5c73e912010-02-11 00:48:18 +00002971Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2972 // If this record has a definition in the translation unit we're coming from,
2973 // but this particular declaration is not that definition, import the
2974 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002975 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002976 if (Definition && Definition != D) {
2977 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002978 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00002979 return nullptr;
2980
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002981 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002982 }
2983
2984 // Import the major distinguishing characteristics of this record.
2985 DeclContext *DC, *LexicalDC;
2986 DeclarationName Name;
2987 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002988 NamedDecl *ToD;
2989 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002990 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002991 if (ToD)
2992 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002993
Douglas Gregor5c73e912010-02-11 00:48:18 +00002994 // Figure out what structure name we're looking for.
2995 unsigned IDNS = Decl::IDNS_Tag;
2996 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002997 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2998 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002999 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00003000 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00003001 IDNS |= Decl::IDNS_Ordinary;
3002
3003 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00003004 RecordDecl *AdoptDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00003005 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003006 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003007 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003008 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003009 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3010 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00003011 continue;
3012
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003013 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00003014 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00003015 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
3016 Found = Tag->getDecl();
3017 }
3018
3019 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003020 if (D->isAnonymousStructOrUnion() &&
3021 FoundRecord->isAnonymousStructOrUnion()) {
3022 // If both anonymous structs/unions are in a record context, make sure
3023 // they occur in the same location in the context records.
David Blaikie05785d12013-02-20 22:23:23 +00003024 if (Optional<unsigned> Index1
Sean Callanan488f8612016-07-14 19:53:44 +00003025 = findUntaggedStructOrUnionIndex(D)) {
David Blaikie05785d12013-02-20 22:23:23 +00003026 if (Optional<unsigned> Index2 =
Sean Callanan488f8612016-07-14 19:53:44 +00003027 findUntaggedStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003028 if (*Index1 != *Index2)
3029 continue;
3030 }
3031 }
3032 }
3033
Douglas Gregor25791052010-02-12 00:09:27 +00003034 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00003035 if ((SearchName && !D->isCompleteDefinition())
3036 || (D->isCompleteDefinition() &&
3037 D->isAnonymousStructOrUnion()
3038 == FoundDef->isAnonymousStructOrUnion() &&
3039 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00003040 // The record types structurally match, or the "from" translation
3041 // unit only had a forward declaration anyway; call it the same
3042 // function.
3043 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003044 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00003045 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003046 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00003047 // We have a forward declaration of this type, so adopt that forward
3048 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00003049
3050 // If one or both can be completed from external storage then try one
3051 // last time to complete and compare them before doing this.
3052
3053 if (FoundRecord->hasExternalLexicalStorage() &&
3054 !FoundRecord->isCompleteDefinition())
3055 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
3056 if (D->hasExternalLexicalStorage())
3057 D->getASTContext().getExternalSource()->CompleteType(D);
3058
3059 if (FoundRecord->isCompleteDefinition() &&
3060 D->isCompleteDefinition() &&
3061 !IsStructuralMatch(D, FoundRecord))
3062 continue;
3063
Douglas Gregor25791052010-02-12 00:09:27 +00003064 AdoptDecl = FoundRecord;
3065 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00003066 } else if (!SearchName) {
3067 continue;
3068 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00003069 }
3070
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003071 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003072 }
3073
Douglas Gregordd6006f2012-07-17 21:16:27 +00003074 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00003075 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3076 ConflictingDecls.data(),
3077 ConflictingDecls.size());
3078 }
3079 }
3080
3081 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00003082 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003083 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00003084 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00003085 CXXRecordDecl *D2CXX = nullptr;
3086 if (CXXRecordDecl *DCXX = llvm::dyn_cast<CXXRecordDecl>(D)) {
3087 if (DCXX->isLambda()) {
3088 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
3089 D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(),
3090 DC, TInfo, Loc,
3091 DCXX->isDependentLambda(),
3092 DCXX->isGenericLambda(),
3093 DCXX->getLambdaCaptureDefault());
3094 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
3095 if (DCXX->getLambdaContextDecl() && !CDecl)
3096 return nullptr;
Sean Callanan041cceb2016-05-14 05:43:57 +00003097 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl);
3098 } else if (DCXX->isInjectedClassName()) {
3099 // We have to be careful to do a similar dance to the one in
3100 // Sema::ActOnStartCXXMemberDeclarations
3101 CXXRecordDecl *const PrevDecl = nullptr;
3102 const bool DelayTypeCreation = true;
3103 D2CXX = CXXRecordDecl::Create(
3104 Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc,
3105 Name.getAsIdentifierInfo(), PrevDecl, DelayTypeCreation);
3106 Importer.getToContext().getTypeDeclType(
3107 D2CXX, llvm::dyn_cast<CXXRecordDecl>(DC));
Sean Callanan8bca9962016-03-28 21:43:01 +00003108 } else {
3109 D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
3110 D->getTagKind(),
3111 DC, StartLoc, Loc,
3112 Name.getAsIdentifierInfo());
3113 }
Douglas Gregor3996e242010-02-15 22:01:00 +00003114 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00003115 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00003116 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00003117 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003118 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003119 }
Douglas Gregor14454802011-02-25 02:25:35 +00003120
3121 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00003122 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003123 LexicalDC->addDeclInternal(D2);
Douglas Gregordd6006f2012-07-17 21:16:27 +00003124 if (D->isAnonymousStructOrUnion())
3125 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003126 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003127
Douglas Gregor3996e242010-02-15 22:01:00 +00003128 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00003129
Douglas Gregor95d82832012-01-24 18:36:04 +00003130 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00003131 return nullptr;
3132
Douglas Gregor3996e242010-02-15 22:01:00 +00003133 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00003134}
3135
Douglas Gregor98c10182010-02-12 22:17:39 +00003136Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
3137 // Import the major distinguishing characteristics of this enumerator.
3138 DeclContext *DC, *LexicalDC;
3139 DeclarationName Name;
3140 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003141 NamedDecl *ToD;
3142 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003143 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003144 if (ToD)
3145 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00003146
3147 QualType T = Importer.Import(D->getType());
3148 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003149 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00003150
Douglas Gregor98c10182010-02-12 22:17:39 +00003151 // Determine whether there are any other declarations with the same name and
3152 // in the same context.
3153 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003154 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00003155 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003156 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003157 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003158 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3159 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00003160 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00003161
3162 if (EnumConstantDecl *FoundEnumConstant
3163 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
3164 if (IsStructuralMatch(D, FoundEnumConstant))
3165 return Importer.Imported(D, FoundEnumConstant);
3166 }
3167
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003168 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00003169 }
3170
3171 if (!ConflictingDecls.empty()) {
3172 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3173 ConflictingDecls.data(),
3174 ConflictingDecls.size());
3175 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003176 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00003177 }
3178 }
3179
3180 Expr *Init = Importer.Import(D->getInitExpr());
3181 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00003182 return nullptr;
3183
Douglas Gregor98c10182010-02-12 22:17:39 +00003184 EnumConstantDecl *ToEnumerator
3185 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
3186 Name.getAsIdentifierInfo(), T,
3187 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00003188 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00003189 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003190 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00003191 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00003192 return ToEnumerator;
3193}
Douglas Gregor5c73e912010-02-11 00:48:18 +00003194
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003195Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
3196 // Import the major distinguishing characteristics of this function.
3197 DeclContext *DC, *LexicalDC;
3198 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003199 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003200 NamedDecl *ToD;
3201 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003202 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003203 if (ToD)
3204 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003205
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003206 // Try to find a function in our own ("to") context with the same name, same
3207 // type, and in the same context as the function we're importing.
3208 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003209 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003210 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003211 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003212 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003213 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3214 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003215 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003216
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003217 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00003218 if (FoundFunction->hasExternalFormalLinkage() &&
3219 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003220 if (Importer.IsStructurallyEquivalent(D->getType(),
3221 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003222 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003223 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003224 }
3225
3226 // FIXME: Check for overloading more carefully, e.g., by boosting
3227 // Sema::IsOverload out to the AST library.
3228
3229 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003230 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003231 continue;
3232
3233 // Complain about inconsistent function types.
3234 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003235 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003236 Importer.ToDiag(FoundFunction->getLocation(),
3237 diag::note_odr_value_here)
3238 << FoundFunction->getType();
3239 }
3240 }
3241
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003242 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003243 }
3244
3245 if (!ConflictingDecls.empty()) {
3246 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3247 ConflictingDecls.data(),
3248 ConflictingDecls.size());
3249 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003250 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003251 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003252 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00003253
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003254 DeclarationNameInfo NameInfo(Name, Loc);
3255 // Import additional name location/type info.
3256 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3257
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003258 QualType FromTy = D->getType();
3259 bool usedDifferentExceptionSpec = false;
3260
3261 if (const FunctionProtoType *
3262 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
3263 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
3264 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
3265 // FunctionDecl that we are importing the FunctionProtoType for.
3266 // To avoid an infinite recursion when importing, create the FunctionDecl
3267 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00003268 if (FromEPI.ExceptionSpec.SourceDecl ||
3269 FromEPI.ExceptionSpec.SourceTemplate ||
3270 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003271 FunctionProtoType::ExtProtoInfo DefaultEPI;
3272 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00003273 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003274 usedDifferentExceptionSpec = true;
3275 }
3276 }
3277
Douglas Gregorb4964f72010-02-15 23:54:17 +00003278 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003279 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00003280 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003281 return nullptr;
3282
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003283 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003284 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00003285 for (auto P : D->parameters()) {
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00003286 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003287 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003288 return nullptr;
3289
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003290 Parameters.push_back(ToP);
3291 }
3292
3293 // Create the imported function.
3294 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Craig Topper36250ad2014-05-12 05:36:57 +00003295 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003296 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Douglas Gregor00eace12010-02-21 18:29:16 +00003297 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
3298 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
3299 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00003300 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003301 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00003302 FromConstructor->isExplicit(),
3303 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00003304 D->isImplicit(),
3305 D->isConstexpr());
Sean Callanandd2c1742016-05-16 20:48:03 +00003306 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
3307 SmallVector<CXXCtorInitializer *, 4> CtorInitializers;
3308 for (CXXCtorInitializer *I : FromConstructor->inits()) {
3309 CXXCtorInitializer *ToI =
3310 cast_or_null<CXXCtorInitializer>(Importer.Import(I));
3311 if (!ToI && I)
3312 return nullptr;
3313 CtorInitializers.push_back(ToI);
3314 }
3315 CXXCtorInitializer **Memory =
3316 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
3317 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
3318 CXXConstructorDecl *ToCtor = llvm::cast<CXXConstructorDecl>(ToFunction);
3319 ToCtor->setCtorInitializers(Memory);
3320 ToCtor->setNumCtorInitializers(NumInitializers);
3321 }
Douglas Gregor00eace12010-02-21 18:29:16 +00003322 } else if (isa<CXXDestructorDecl>(D)) {
3323 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
3324 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00003325 InnerLocStart,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00003326 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00003327 D->isInlineSpecified(),
3328 D->isImplicit());
3329 } else if (CXXConversionDecl *FromConversion
3330 = dyn_cast<CXXConversionDecl>(D)) {
3331 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
3332 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00003333 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003334 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00003335 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00003336 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00003337 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00003338 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00003339 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
3340 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
3341 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00003342 InnerLocStart,
Douglas Gregora50ad132010-11-29 16:04:58 +00003343 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003344 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00003345 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00003346 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00003347 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00003348 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003349 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Sean Callanan59721b32015-04-28 18:41:46 +00003350 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003351 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00003352 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00003353 D->hasWrittenPrototype(),
3354 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00003355 }
John McCall3e11ebe2010-03-15 10:12:16 +00003356
3357 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00003358 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003359 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00003360 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00003361 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
3362 ToFunction->setTrivial(D->isTrivial());
3363 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00003364 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003365
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003366 // Set the parameters.
3367 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003368 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00003369 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003370 }
David Blaikie9c70e042011-09-21 18:16:56 +00003371 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003372
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003373 if (usedDifferentExceptionSpec) {
3374 // Update FunctionProtoType::ExtProtoInfo.
3375 QualType T = Importer.Import(D->getType());
3376 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003377 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003378 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00003379 }
3380
Sean Callanan59721b32015-04-28 18:41:46 +00003381 // Import the body, if any.
3382 if (Stmt *FromBody = D->getBody()) {
3383 if (Stmt *ToBody = Importer.Import(FromBody)) {
3384 ToFunction->setBody(ToBody);
3385 }
3386 }
3387
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003388 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003389
3390 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00003391 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003392
Douglas Gregor43f54792010-02-17 02:12:47 +00003393 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003394}
3395
Douglas Gregor00eace12010-02-21 18:29:16 +00003396Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
3397 return VisitFunctionDecl(D);
3398}
3399
3400Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
3401 return VisitCXXMethodDecl(D);
3402}
3403
3404Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
3405 return VisitCXXMethodDecl(D);
3406}
3407
3408Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
3409 return VisitCXXMethodDecl(D);
3410}
3411
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003412static unsigned getFieldIndex(Decl *F) {
3413 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
3414 if (!Owner)
3415 return 0;
3416
3417 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00003418 for (const auto *D : Owner->noload_decls()) {
3419 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003420 return Index;
3421
3422 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
3423 ++Index;
3424 }
3425
3426 return Index;
3427}
3428
Douglas Gregor5c73e912010-02-11 00:48:18 +00003429Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
3430 // Import the major distinguishing characteristics of a variable.
3431 DeclContext *DC, *LexicalDC;
3432 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00003433 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003434 NamedDecl *ToD;
3435 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003436 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003437 if (ToD)
3438 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003439
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003440 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003441 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003442 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003443 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3444 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003445 // For anonymous fields, match up by index.
3446 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
3447 continue;
3448
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003449 if (Importer.IsStructurallyEquivalent(D->getType(),
3450 FoundField->getType())) {
3451 Importer.Imported(D, FoundField);
3452 return FoundField;
3453 }
3454
3455 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3456 << Name << D->getType() << FoundField->getType();
3457 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3458 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003459 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003460 }
3461 }
3462
Douglas Gregorb4964f72010-02-15 23:54:17 +00003463 // Import the type.
3464 QualType T = Importer.Import(D->getType());
3465 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003466 return nullptr;
3467
Douglas Gregor5c73e912010-02-11 00:48:18 +00003468 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3469 Expr *BitWidth = Importer.Import(D->getBitWidth());
3470 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00003471 return nullptr;
3472
Abramo Bagnaradff19302011-03-08 08:55:46 +00003473 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
3474 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00003475 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00003476 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00003477 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00003478 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003479 ToField->setLexicalDeclContext(LexicalDC);
Sean Callanan3a83ea72016-03-03 02:22:05 +00003480 if (Expr *FromInitializer = D->getInClassInitializer()) {
Sean Callananbb33f582016-03-03 01:21:28 +00003481 Expr *ToInitializer = Importer.Import(FromInitializer);
3482 if (ToInitializer)
3483 ToField->setInClassInitializer(ToInitializer);
3484 else
3485 return nullptr;
3486 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003487 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003488 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00003489 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003490 return ToField;
3491}
3492
Francois Pichet783dd6e2010-11-21 06:08:52 +00003493Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
3494 // Import the major distinguishing characteristics of a variable.
3495 DeclContext *DC, *LexicalDC;
3496 DeclarationName Name;
3497 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003498 NamedDecl *ToD;
3499 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003500 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003501 if (ToD)
3502 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003503
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003504 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003505 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003506 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003507 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003508 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003509 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003510 // For anonymous indirect fields, match up by index.
3511 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
3512 continue;
3513
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003514 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003515 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003516 !Name.isEmpty())) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003517 Importer.Imported(D, FoundField);
3518 return FoundField;
3519 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003520
3521 // If there are more anonymous fields to check, continue.
3522 if (!Name && I < N-1)
3523 continue;
3524
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003525 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3526 << Name << D->getType() << FoundField->getType();
3527 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3528 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003529 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003530 }
3531 }
3532
Francois Pichet783dd6e2010-11-21 06:08:52 +00003533 // Import the type.
3534 QualType T = Importer.Import(D->getType());
3535 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003536 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003537
3538 NamedDecl **NamedChain =
3539 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
3540
3541 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00003542 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00003543 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003544 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00003545 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003546 NamedChain[i++] = cast<NamedDecl>(D);
3547 }
3548
3549 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
Aaron Ballman260995b2014-10-15 16:58:18 +00003550 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
David Majnemer59f77922016-06-24 04:05:48 +00003551 {NamedChain, D->getChainingSize()});
Aaron Ballman260995b2014-10-15 16:58:18 +00003552
3553 for (const auto *Attr : D->attrs())
3554 ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
3555
Francois Pichet783dd6e2010-11-21 06:08:52 +00003556 ToIndirectField->setAccess(D->getAccess());
3557 ToIndirectField->setLexicalDeclContext(LexicalDC);
3558 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00003559 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003560 return ToIndirectField;
3561}
3562
Aleksei Sidorina693b372016-09-28 10:16:56 +00003563Decl *ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
3564 // Import the major distinguishing characteristics of a declaration.
3565 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3566 DeclContext *LexicalDC = D->getDeclContext() == D->getLexicalDeclContext()
3567 ? DC : Importer.ImportContext(D->getLexicalDeclContext());
3568 if (!DC || !LexicalDC)
3569 return nullptr;
3570
3571 // Determine whether we've already imported this decl.
3572 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
3573 auto *RD = cast<CXXRecordDecl>(DC);
3574 FriendDecl *ImportedFriend = RD->getFirstFriend();
3575 StructuralEquivalenceContext Context(
3576 Importer.getFromContext(), Importer.getToContext(),
3577 Importer.getNonEquivalentDecls(), false, false);
3578
3579 while (ImportedFriend) {
3580 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
3581 if (Context.IsStructurallyEquivalent(D->getFriendDecl(),
3582 ImportedFriend->getFriendDecl()))
3583 return Importer.Imported(D, ImportedFriend);
3584
3585 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
3586 if (Importer.IsStructurallyEquivalent(
3587 D->getFriendType()->getType(),
3588 ImportedFriend->getFriendType()->getType(), true))
3589 return Importer.Imported(D, ImportedFriend);
3590 }
3591 ImportedFriend = ImportedFriend->getNextFriend();
3592 }
3593
3594 // Not found. Create it.
3595 FriendDecl::FriendUnion ToFU;
3596 if (NamedDecl *FriendD = D->getFriendDecl())
3597 ToFU = cast_or_null<NamedDecl>(Importer.Import(FriendD));
3598 else
3599 ToFU = Importer.Import(D->getFriendType());
3600 if (!ToFU)
3601 return nullptr;
3602
3603 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
3604 TemplateParameterList **FromTPLists =
3605 D->getTrailingObjects<TemplateParameterList *>();
3606 for (unsigned I = 0; I < D->NumTPLists; I++) {
3607 TemplateParameterList *List = ImportTemplateParameterList(FromTPLists[I]);
3608 if (!List)
3609 return nullptr;
3610 ToTPLists[I] = List;
3611 }
3612
3613 FriendDecl *FrD = FriendDecl::Create(Importer.getToContext(), DC,
3614 Importer.Import(D->getLocation()),
3615 ToFU, Importer.Import(D->getFriendLoc()),
3616 ToTPLists);
3617
3618 Importer.Imported(D, FrD);
3619 RD->pushFriendDecl(FrD);
3620
3621 FrD->setAccess(D->getAccess());
3622 FrD->setLexicalDeclContext(LexicalDC);
3623 LexicalDC->addDeclInternal(FrD);
3624 return FrD;
3625}
3626
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003627Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
3628 // Import the major distinguishing characteristics of an ivar.
3629 DeclContext *DC, *LexicalDC;
3630 DeclarationName Name;
3631 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003632 NamedDecl *ToD;
3633 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003634 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003635 if (ToD)
3636 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003637
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003638 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003639 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003640 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003641 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3642 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003643 if (Importer.IsStructurallyEquivalent(D->getType(),
3644 FoundIvar->getType())) {
3645 Importer.Imported(D, FoundIvar);
3646 return FoundIvar;
3647 }
3648
3649 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
3650 << Name << D->getType() << FoundIvar->getType();
3651 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3652 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003653 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003654 }
3655 }
3656
3657 // Import the type.
3658 QualType T = Importer.Import(D->getType());
3659 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003660 return nullptr;
3661
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003662 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3663 Expr *BitWidth = Importer.Import(D->getBitWidth());
3664 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00003665 return nullptr;
3666
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00003667 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
3668 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003669 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003670 Loc, Name.getAsIdentifierInfo(),
3671 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003672 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003673 ToIvar->setLexicalDeclContext(LexicalDC);
3674 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003675 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003676 return ToIvar;
3677
3678}
3679
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003680Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
3681 // Import the major distinguishing characteristics of a variable.
3682 DeclContext *DC, *LexicalDC;
3683 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003684 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003685 NamedDecl *ToD;
3686 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003687 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003688 if (ToD)
3689 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003690
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003691 // Try to find a variable in our own ("to") context with the same name and
3692 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00003693 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00003694 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003695 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003696 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003697 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003698 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003699 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3700 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003701 continue;
3702
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003703 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003704 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00003705 if (FoundVar->hasExternalFormalLinkage() &&
3706 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003707 if (Importer.IsStructurallyEquivalent(D->getType(),
3708 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003709 MergeWithVar = FoundVar;
3710 break;
3711 }
3712
Douglas Gregor56521c52010-02-12 17:23:39 +00003713 const ArrayType *FoundArray
3714 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3715 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00003716 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00003717 if (FoundArray && TArray) {
3718 if (isa<IncompleteArrayType>(FoundArray) &&
3719 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003720 // Import the type.
3721 QualType T = Importer.Import(D->getType());
3722 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003723 return nullptr;
3724
Douglas Gregor56521c52010-02-12 17:23:39 +00003725 FoundVar->setType(T);
3726 MergeWithVar = FoundVar;
3727 break;
3728 } else if (isa<IncompleteArrayType>(TArray) &&
3729 isa<ConstantArrayType>(FoundArray)) {
3730 MergeWithVar = FoundVar;
3731 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003732 }
3733 }
3734
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003735 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003736 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003737 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3738 << FoundVar->getType();
3739 }
3740 }
3741
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003742 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003743 }
3744
3745 if (MergeWithVar) {
3746 // An equivalent variable with external linkage has been found. Link
3747 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003748 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003749
3750 if (VarDecl *DDef = D->getDefinition()) {
3751 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
3752 Importer.ToDiag(ExistingDef->getLocation(),
3753 diag::err_odr_variable_multiple_def)
3754 << Name;
3755 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
3756 } else {
3757 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00003758 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00003759 if (DDef->isInitKnownICE()) {
3760 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
3761 Eval->CheckedICE = true;
3762 Eval->IsICE = DDef->isInitICE();
3763 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003764 }
3765 }
3766
3767 return MergeWithVar;
3768 }
3769
3770 if (!ConflictingDecls.empty()) {
3771 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3772 ConflictingDecls.data(),
3773 ConflictingDecls.size());
3774 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003775 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003776 }
3777 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003778
Douglas Gregorb4964f72010-02-15 23:54:17 +00003779 // Import the type.
3780 QualType T = Importer.Import(D->getType());
3781 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003782 return nullptr;
3783
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003784 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003785 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00003786 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
3787 Importer.Import(D->getInnerLocStart()),
3788 Loc, Name.getAsIdentifierInfo(),
3789 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003790 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00003791 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003792 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003793 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003794 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003795 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003796
Sean Callanan59721b32015-04-28 18:41:46 +00003797 if (!D->isFileVarDecl() &&
3798 D->isUsed())
3799 ToVar->setIsUsed();
3800
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003801 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00003802 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00003803 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003804
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003805 if (D->isConstexpr())
3806 ToVar->setConstexpr(true);
3807
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003808 return ToVar;
3809}
3810
Douglas Gregor8b228d72010-02-17 21:22:52 +00003811Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
3812 // Parameters are created in the translation unit's context, then moved
3813 // into the function declaration's context afterward.
3814 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3815
3816 // Import the name of this declaration.
3817 DeclarationName Name = Importer.Import(D->getDeclName());
3818 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003819 return nullptr;
3820
Douglas Gregor8b228d72010-02-17 21:22:52 +00003821 // Import the location of this declaration.
3822 SourceLocation Loc = Importer.Import(D->getLocation());
3823
3824 // Import the parameter's type.
3825 QualType T = Importer.Import(D->getType());
3826 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003827 return nullptr;
3828
Douglas Gregor8b228d72010-02-17 21:22:52 +00003829 // Create the imported parameter.
3830 ImplicitParamDecl *ToParm
3831 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
3832 Loc, Name.getAsIdentifierInfo(),
3833 T);
3834 return Importer.Imported(D, ToParm);
3835}
3836
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003837Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
3838 // Parameters are created in the translation unit's context, then moved
3839 // into the function declaration's context afterward.
3840 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3841
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003842 // Import the name of this declaration.
3843 DeclarationName Name = Importer.Import(D->getDeclName());
3844 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003845 return nullptr;
3846
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003847 // Import the location of this declaration.
3848 SourceLocation Loc = Importer.Import(D->getLocation());
3849
3850 // Import the parameter's type.
3851 QualType T = Importer.Import(D->getType());
3852 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003853 return nullptr;
3854
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003855 // Create the imported parameter.
3856 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3857 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003858 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003859 Loc, Name.getAsIdentifierInfo(),
3860 T, TInfo, D->getStorageClass(),
Craig Topper36250ad2014-05-12 05:36:57 +00003861 /*FIXME: Default argument*/nullptr);
John McCallf3cd6652010-03-12 18:31:32 +00003862 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Sean Callanan59721b32015-04-28 18:41:46 +00003863
3864 if (D->isUsed())
3865 ToParm->setIsUsed();
3866
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003867 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003868}
3869
Douglas Gregor43f54792010-02-17 02:12:47 +00003870Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3871 // Import the major distinguishing characteristics of a method.
3872 DeclContext *DC, *LexicalDC;
3873 DeclarationName Name;
3874 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003875 NamedDecl *ToD;
3876 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003877 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003878 if (ToD)
3879 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003880
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003881 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003882 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003883 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3884 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003885 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3886 continue;
3887
3888 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003889 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3890 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003891 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003892 << D->isInstanceMethod() << Name << D->getReturnType()
3893 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00003894 Importer.ToDiag(FoundMethod->getLocation(),
3895 diag::note_odr_objc_method_here)
3896 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003897 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003898 }
3899
3900 // Check the number of parameters.
3901 if (D->param_size() != FoundMethod->param_size()) {
3902 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3903 << D->isInstanceMethod() << Name
3904 << D->param_size() << FoundMethod->param_size();
3905 Importer.ToDiag(FoundMethod->getLocation(),
3906 diag::note_odr_objc_method_here)
3907 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003908 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003909 }
3910
3911 // Check parameter types.
3912 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
3913 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3914 P != PEnd; ++P, ++FoundP) {
3915 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3916 (*FoundP)->getType())) {
3917 Importer.FromDiag((*P)->getLocation(),
3918 diag::err_odr_objc_method_param_type_inconsistent)
3919 << D->isInstanceMethod() << Name
3920 << (*P)->getType() << (*FoundP)->getType();
3921 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3922 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003923 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003924 }
3925 }
3926
3927 // Check variadic/non-variadic.
3928 // Check the number of parameters.
3929 if (D->isVariadic() != FoundMethod->isVariadic()) {
3930 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3931 << D->isInstanceMethod() << Name;
3932 Importer.ToDiag(FoundMethod->getLocation(),
3933 diag::note_odr_objc_method_here)
3934 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003935 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003936 }
3937
3938 // FIXME: Any other bits we need to merge?
3939 return Importer.Imported(D, FoundMethod);
3940 }
3941 }
3942
3943 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00003944 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003945 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003946 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003947
Alp Toker314cc812014-01-25 16:55:45 +00003948 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00003949
Alp Toker314cc812014-01-25 16:55:45 +00003950 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
3951 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3952 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3953 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3954 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003955
3956 // FIXME: When we decide to merge method definitions, we'll need to
3957 // deal with implicit parameters.
3958
3959 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003960 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003961 for (auto *FromP : D->parameters()) {
Aaron Ballman43b68be2014-03-07 17:50:17 +00003962 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00003963 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003964 return nullptr;
3965
Douglas Gregor43f54792010-02-17 02:12:47 +00003966 ToParams.push_back(ToP);
3967 }
3968
3969 // Set the parameters.
3970 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3971 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003972 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003973 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003974 SmallVector<SourceLocation, 12> SelLocs;
3975 D->getSelectorLocs(SelLocs);
3976 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003977
3978 ToMethod->setLexicalDeclContext(LexicalDC);
3979 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003980 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003981 return ToMethod;
3982}
3983
Douglas Gregor85f3f952015-07-07 03:57:15 +00003984Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3985 // Import the major distinguishing characteristics of a category.
3986 DeclContext *DC, *LexicalDC;
3987 DeclarationName Name;
3988 SourceLocation Loc;
3989 NamedDecl *ToD;
3990 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3991 return nullptr;
3992 if (ToD)
3993 return ToD;
3994
3995 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3996 if (!BoundInfo)
3997 return nullptr;
3998
3999 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
4000 Importer.getToContext(), DC,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00004001 D->getVariance(),
4002 Importer.Import(D->getVarianceLoc()),
Douglas Gregore83b9562015-07-07 03:57:53 +00004003 D->getIndex(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00004004 Importer.Import(D->getLocation()),
4005 Name.getAsIdentifierInfo(),
4006 Importer.Import(D->getColonLoc()),
4007 BoundInfo);
4008 Importer.Imported(D, Result);
4009 Result->setLexicalDeclContext(LexicalDC);
4010 return Result;
4011}
4012
Douglas Gregor84c51c32010-02-18 01:47:50 +00004013Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
4014 // Import the major distinguishing characteristics of a category.
4015 DeclContext *DC, *LexicalDC;
4016 DeclarationName Name;
4017 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004018 NamedDecl *ToD;
4019 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004020 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004021 if (ToD)
4022 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004023
Douglas Gregor84c51c32010-02-18 01:47:50 +00004024 ObjCInterfaceDecl *ToInterface
4025 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
4026 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00004027 return nullptr;
4028
Douglas Gregor84c51c32010-02-18 01:47:50 +00004029 // Determine if we've already encountered this category.
4030 ObjCCategoryDecl *MergeWithCategory
4031 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
4032 ObjCCategoryDecl *ToCategory = MergeWithCategory;
4033 if (!ToCategory) {
4034 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00004035 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00004036 Loc,
4037 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00004038 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00004039 ToInterface,
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004040 /*TypeParamList=*/nullptr,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00004041 Importer.Import(D->getIvarLBraceLoc()),
4042 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00004043 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004044 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00004045 Importer.Imported(D, ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004046 // Import the type parameter list after calling Imported, to avoid
4047 // loops when bringing in their DeclContext.
4048 ToCategory->setTypeParamList(ImportObjCTypeParamList(
4049 D->getTypeParamList()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00004050
Douglas Gregor84c51c32010-02-18 01:47:50 +00004051 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004052 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4053 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00004054 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
4055 = D->protocol_loc_begin();
4056 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
4057 FromProtoEnd = D->protocol_end();
4058 FromProto != FromProtoEnd;
4059 ++FromProto, ++FromProtoLoc) {
4060 ObjCProtocolDecl *ToProto
4061 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
4062 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00004063 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00004064 Protocols.push_back(ToProto);
4065 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
4066 }
4067
4068 // FIXME: If we're merging, make sure that the protocol list is the same.
4069 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
4070 ProtocolLocs.data(), Importer.getToContext());
4071
4072 } else {
4073 Importer.Imported(D, ToCategory);
4074 }
4075
4076 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00004077 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00004078
4079 // If we have an implementation, import it as well.
4080 if (D->getImplementation()) {
4081 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00004082 = cast_or_null<ObjCCategoryImplDecl>(
4083 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00004084 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00004085 return nullptr;
4086
Douglas Gregor84c51c32010-02-18 01:47:50 +00004087 ToCategory->setImplementation(Impl);
4088 }
4089
4090 return ToCategory;
4091}
4092
Douglas Gregor2aa53772012-01-24 17:42:07 +00004093bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
4094 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004095 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004096 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00004097 if (shouldForceImportDeclContext(Kind))
4098 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004099 return false;
4100 }
4101
4102 // Start the protocol definition
4103 To->startDefinition();
4104
4105 // Import protocols
4106 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4107 SmallVector<SourceLocation, 4> ProtocolLocs;
4108 ObjCProtocolDecl::protocol_loc_iterator
4109 FromProtoLoc = From->protocol_loc_begin();
4110 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
4111 FromProtoEnd = From->protocol_end();
4112 FromProto != FromProtoEnd;
4113 ++FromProto, ++FromProtoLoc) {
4114 ObjCProtocolDecl *ToProto
4115 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
4116 if (!ToProto)
4117 return true;
4118 Protocols.push_back(ToProto);
4119 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
4120 }
4121
4122 // FIXME: If we're merging, make sure that the protocol list is the same.
4123 To->setProtocolList(Protocols.data(), Protocols.size(),
4124 ProtocolLocs.data(), Importer.getToContext());
4125
Douglas Gregor2e15c842012-02-01 21:00:38 +00004126 if (shouldForceImportDeclContext(Kind)) {
4127 // Import all of the members of this protocol.
4128 ImportDeclContext(From, /*ForceImport=*/true);
4129 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00004130 return false;
4131}
4132
Douglas Gregor98d156a2010-02-17 16:12:00 +00004133Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004134 // If this protocol has a definition in the translation unit we're coming
4135 // from, but this particular declaration is not that definition, import the
4136 // definition and map to that.
4137 ObjCProtocolDecl *Definition = D->getDefinition();
4138 if (Definition && Definition != D) {
4139 Decl *ImportedDef = Importer.Import(Definition);
4140 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004141 return nullptr;
4142
Douglas Gregor2aa53772012-01-24 17:42:07 +00004143 return Importer.Imported(D, ImportedDef);
4144 }
4145
Douglas Gregor84c51c32010-02-18 01:47:50 +00004146 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00004147 DeclContext *DC, *LexicalDC;
4148 DeclarationName Name;
4149 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004150 NamedDecl *ToD;
4151 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004152 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004153 if (ToD)
4154 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00004155
Craig Topper36250ad2014-05-12 05:36:57 +00004156 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004157 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004158 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004159 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4160 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004161 continue;
4162
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004163 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004164 break;
4165 }
4166
4167 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004168 if (!ToProto) {
4169 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
4170 Name.getAsIdentifierInfo(), Loc,
4171 Importer.Import(D->getAtStartLoc()),
Craig Topper36250ad2014-05-12 05:36:57 +00004172 /*PrevDecl=*/nullptr);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004173 ToProto->setLexicalDeclContext(LexicalDC);
4174 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004175 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00004176
4177 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004178
Douglas Gregor2aa53772012-01-24 17:42:07 +00004179 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00004180 return nullptr;
4181
Douglas Gregor98d156a2010-02-17 16:12:00 +00004182 return ToProto;
4183}
4184
Sean Callanan0aae0412014-12-10 00:00:37 +00004185Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
4186 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4187 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4188
4189 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
4190 SourceLocation LangLoc = Importer.Import(D->getLocation());
4191
4192 bool HasBraces = D->hasBraces();
4193
Sean Callananb12a8552014-12-10 21:22:20 +00004194 LinkageSpecDecl *ToLinkageSpec =
4195 LinkageSpecDecl::Create(Importer.getToContext(),
4196 DC,
4197 ExternLoc,
4198 LangLoc,
4199 D->getLanguage(),
4200 HasBraces);
Sean Callanan0aae0412014-12-10 00:00:37 +00004201
4202 if (HasBraces) {
4203 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
4204 ToLinkageSpec->setRBraceLoc(RBraceLoc);
4205 }
4206
4207 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
4208 LexicalDC->addDeclInternal(ToLinkageSpec);
4209
4210 Importer.Imported(D, ToLinkageSpec);
4211
4212 return ToLinkageSpec;
4213}
4214
Douglas Gregor2aa53772012-01-24 17:42:07 +00004215bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
4216 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004217 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004218 if (To->getDefinition()) {
4219 // Check consistency of superclass.
4220 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
4221 if (FromSuper) {
4222 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
4223 if (!FromSuper)
4224 return true;
4225 }
4226
4227 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
4228 if ((bool)FromSuper != (bool)ToSuper ||
4229 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
4230 Importer.ToDiag(To->getLocation(),
4231 diag::err_odr_objc_superclass_inconsistent)
4232 << To->getDeclName();
4233 if (ToSuper)
4234 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
4235 << To->getSuperClass()->getDeclName();
4236 else
4237 Importer.ToDiag(To->getLocation(),
4238 diag::note_odr_objc_missing_superclass);
4239 if (From->getSuperClass())
4240 Importer.FromDiag(From->getSuperClassLoc(),
4241 diag::note_odr_objc_superclass)
4242 << From->getSuperClass()->getDeclName();
4243 else
4244 Importer.FromDiag(From->getLocation(),
4245 diag::note_odr_objc_missing_superclass);
4246 }
4247
Douglas Gregor2e15c842012-02-01 21:00:38 +00004248 if (shouldForceImportDeclContext(Kind))
4249 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004250 return false;
4251 }
4252
4253 // Start the definition.
4254 To->startDefinition();
4255
4256 // If this class has a superclass, import it.
4257 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00004258 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
4259 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00004260 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00004261
4262 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004263 }
4264
4265 // Import protocols
4266 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4267 SmallVector<SourceLocation, 4> ProtocolLocs;
4268 ObjCInterfaceDecl::protocol_loc_iterator
4269 FromProtoLoc = From->protocol_loc_begin();
4270
4271 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
4272 FromProtoEnd = From->protocol_end();
4273 FromProto != FromProtoEnd;
4274 ++FromProto, ++FromProtoLoc) {
4275 ObjCProtocolDecl *ToProto
4276 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
4277 if (!ToProto)
4278 return true;
4279 Protocols.push_back(ToProto);
4280 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
4281 }
4282
4283 // FIXME: If we're merging, make sure that the protocol list is the same.
4284 To->setProtocolList(Protocols.data(), Protocols.size(),
4285 ProtocolLocs.data(), Importer.getToContext());
4286
4287 // Import categories. When the categories themselves are imported, they'll
4288 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00004289 for (auto *Cat : From->known_categories())
4290 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004291
Douglas Gregor2aa53772012-01-24 17:42:07 +00004292 // If we have an @implementation, import it as well.
4293 if (From->getImplementation()) {
4294 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
4295 Importer.Import(From->getImplementation()));
4296 if (!Impl)
4297 return true;
4298
4299 To->setImplementation(Impl);
4300 }
4301
Douglas Gregor2e15c842012-02-01 21:00:38 +00004302 if (shouldForceImportDeclContext(Kind)) {
4303 // Import all of the members of this class.
4304 ImportDeclContext(From, /*ForceImport=*/true);
4305 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00004306 return false;
4307}
4308
Douglas Gregor85f3f952015-07-07 03:57:15 +00004309ObjCTypeParamList *
4310ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
4311 if (!list)
4312 return nullptr;
4313
4314 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
4315 for (auto fromTypeParam : *list) {
4316 auto toTypeParam = cast_or_null<ObjCTypeParamDecl>(
4317 Importer.Import(fromTypeParam));
4318 if (!toTypeParam)
4319 return nullptr;
4320
4321 toTypeParams.push_back(toTypeParam);
4322 }
4323
4324 return ObjCTypeParamList::create(Importer.getToContext(),
4325 Importer.Import(list->getLAngleLoc()),
4326 toTypeParams,
4327 Importer.Import(list->getRAngleLoc()));
4328}
4329
Douglas Gregor45635322010-02-16 01:20:57 +00004330Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004331 // If this class has a definition in the translation unit we're coming from,
4332 // but this particular declaration is not that definition, import the
4333 // definition and map to that.
4334 ObjCInterfaceDecl *Definition = D->getDefinition();
4335 if (Definition && Definition != D) {
4336 Decl *ImportedDef = Importer.Import(Definition);
4337 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004338 return nullptr;
4339
Douglas Gregor2aa53772012-01-24 17:42:07 +00004340 return Importer.Imported(D, ImportedDef);
4341 }
4342
Douglas Gregor45635322010-02-16 01:20:57 +00004343 // Import the major distinguishing characteristics of an @interface.
4344 DeclContext *DC, *LexicalDC;
4345 DeclarationName Name;
4346 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004347 NamedDecl *ToD;
4348 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004349 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004350 if (ToD)
4351 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00004352
Douglas Gregor2aa53772012-01-24 17:42:07 +00004353 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00004354 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004355 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004356 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004357 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4358 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00004359 continue;
4360
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004361 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00004362 break;
4363 }
4364
Douglas Gregor2aa53772012-01-24 17:42:07 +00004365 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00004366 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004367 if (!ToIface) {
4368 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
4369 Importer.Import(D->getAtStartLoc()),
Douglas Gregor85f3f952015-07-07 03:57:15 +00004370 Name.getAsIdentifierInfo(),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004371 /*TypeParamList=*/nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00004372 /*PrevDecl=*/nullptr, Loc,
Douglas Gregor2aa53772012-01-24 17:42:07 +00004373 D->isImplicitInterfaceDecl());
4374 ToIface->setLexicalDeclContext(LexicalDC);
4375 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00004376 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00004377 Importer.Imported(D, ToIface);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004378 // Import the type parameter list after calling Imported, to avoid
4379 // loops when bringing in their DeclContext.
4380 ToIface->setTypeParamList(ImportObjCTypeParamList(
4381 D->getTypeParamListAsWritten()));
Douglas Gregor45635322010-02-16 01:20:57 +00004382
Douglas Gregor2aa53772012-01-24 17:42:07 +00004383 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00004384 return nullptr;
4385
Douglas Gregor98d156a2010-02-17 16:12:00 +00004386 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00004387}
4388
Douglas Gregor4da9d682010-12-07 15:32:12 +00004389Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
4390 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
4391 Importer.Import(D->getCategoryDecl()));
4392 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00004393 return nullptr;
4394
Douglas Gregor4da9d682010-12-07 15:32:12 +00004395 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
4396 if (!ToImpl) {
4397 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4398 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004399 return nullptr;
4400
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00004401 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00004402 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00004403 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00004404 Category->getClassInterface(),
4405 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00004406 Importer.Import(D->getAtStartLoc()),
4407 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004408
4409 DeclContext *LexicalDC = DC;
4410 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4411 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4412 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004413 return nullptr;
4414
Douglas Gregor4da9d682010-12-07 15:32:12 +00004415 ToImpl->setLexicalDeclContext(LexicalDC);
4416 }
4417
Sean Callanan95e74be2011-10-21 02:57:43 +00004418 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004419 Category->setImplementation(ToImpl);
4420 }
4421
4422 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00004423 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004424 return ToImpl;
4425}
4426
Douglas Gregorda8025c2010-12-07 01:26:03 +00004427Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
4428 // Find the corresponding interface.
4429 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
4430 Importer.Import(D->getClassInterface()));
4431 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00004432 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004433
4434 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00004435 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004436 if (D->getSuperClass()) {
4437 Super = cast_or_null<ObjCInterfaceDecl>(
4438 Importer.Import(D->getSuperClass()));
4439 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00004440 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004441 }
4442
4443 ObjCImplementationDecl *Impl = Iface->getImplementation();
4444 if (!Impl) {
4445 // We haven't imported an implementation yet. Create a new @implementation
4446 // now.
4447 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
4448 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00004449 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00004450 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00004451 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00004452 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00004453 Importer.Import(D->getIvarLBraceLoc()),
4454 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00004455
4456 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4457 DeclContext *LexicalDC
4458 = Importer.ImportContext(D->getLexicalDeclContext());
4459 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004460 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004461 Impl->setLexicalDeclContext(LexicalDC);
4462 }
4463
4464 // Associate the implementation with the class it implements.
4465 Iface->setImplementation(Impl);
4466 Importer.Imported(D, Iface->getImplementation());
4467 } else {
4468 Importer.Imported(D, Iface->getImplementation());
4469
4470 // Verify that the existing @implementation has the same superclass.
4471 if ((Super && !Impl->getSuperClass()) ||
4472 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004473 (Super && Impl->getSuperClass() &&
4474 !declaresSameEntity(Super->getCanonicalDecl(),
4475 Impl->getSuperClass()))) {
4476 Importer.ToDiag(Impl->getLocation(),
4477 diag::err_odr_objc_superclass_inconsistent)
4478 << Iface->getDeclName();
4479 // FIXME: It would be nice to have the location of the superclass
4480 // below.
4481 if (Impl->getSuperClass())
4482 Importer.ToDiag(Impl->getLocation(),
4483 diag::note_odr_objc_superclass)
4484 << Impl->getSuperClass()->getDeclName();
4485 else
4486 Importer.ToDiag(Impl->getLocation(),
4487 diag::note_odr_objc_missing_superclass);
4488 if (D->getSuperClass())
4489 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004490 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004491 << D->getSuperClass()->getDeclName();
4492 else
4493 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004494 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00004495 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004496 }
4497 }
4498
4499 // Import all of the members of this @implementation.
4500 ImportDeclContext(D);
4501
4502 return Impl;
4503}
4504
Douglas Gregora11c4582010-02-17 18:02:10 +00004505Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
4506 // Import the major distinguishing characteristics of an @property.
4507 DeclContext *DC, *LexicalDC;
4508 DeclarationName Name;
4509 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004510 NamedDecl *ToD;
4511 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004512 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004513 if (ToD)
4514 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004515
4516 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004517 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004518 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004519 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004520 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004521 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004522 // Check property types.
4523 if (!Importer.IsStructurallyEquivalent(D->getType(),
4524 FoundProp->getType())) {
4525 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
4526 << Name << D->getType() << FoundProp->getType();
4527 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4528 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00004529 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004530 }
4531
4532 // FIXME: Check property attributes, getters, setters, etc.?
4533
4534 // Consider these properties to be equivalent.
4535 Importer.Imported(D, FoundProp);
4536 return FoundProp;
4537 }
4538 }
4539
4540 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00004541 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
4542 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00004543 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004544
4545 // Create the new property.
4546 ObjCPropertyDecl *ToProperty
4547 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
4548 Name.getAsIdentifierInfo(),
4549 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00004550 Importer.Import(D->getLParenLoc()),
Douglas Gregor813a0662015-06-19 18:14:38 +00004551 Importer.Import(D->getType()),
4552 TSI,
Douglas Gregora11c4582010-02-17 18:02:10 +00004553 D->getPropertyImplementation());
4554 Importer.Imported(D, ToProperty);
4555 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004556 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004557
4558 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004559 ToProperty->setPropertyAttributesAsWritten(
4560 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00004561 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
4562 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
4563 ToProperty->setGetterMethodDecl(
4564 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
4565 ToProperty->setSetterMethodDecl(
4566 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
4567 ToProperty->setPropertyIvarDecl(
4568 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
4569 return ToProperty;
4570}
4571
Douglas Gregor14a49e22010-12-07 18:32:03 +00004572Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4573 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
4574 Importer.Import(D->getPropertyDecl()));
4575 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00004576 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004577
4578 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4579 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004580 return nullptr;
4581
Douglas Gregor14a49e22010-12-07 18:32:03 +00004582 // Import the lexical declaration context.
4583 DeclContext *LexicalDC = DC;
4584 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4585 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4586 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004587 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004588 }
4589
4590 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
4591 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00004592 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004593
4594 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004595 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004596 if (D->getPropertyIvarDecl()) {
4597 Ivar = cast_or_null<ObjCIvarDecl>(
4598 Importer.Import(D->getPropertyIvarDecl()));
4599 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00004600 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004601 }
4602
4603 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004604 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4605 Property->getQueryKind());
Douglas Gregor14a49e22010-12-07 18:32:03 +00004606 if (!ToImpl) {
4607 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
4608 Importer.Import(D->getLocStart()),
4609 Importer.Import(D->getLocation()),
4610 Property,
4611 D->getPropertyImplementation(),
4612 Ivar,
4613 Importer.Import(D->getPropertyIvarDeclLoc()));
4614 ToImpl->setLexicalDeclContext(LexicalDC);
4615 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00004616 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004617 } else {
4618 // Check that we have the same kind of property implementation (@synthesize
4619 // vs. @dynamic).
4620 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
4621 Importer.ToDiag(ToImpl->getLocation(),
4622 diag::err_odr_objc_property_impl_kind_inconsistent)
4623 << Property->getDeclName()
4624 << (ToImpl->getPropertyImplementation()
4625 == ObjCPropertyImplDecl::Dynamic);
4626 Importer.FromDiag(D->getLocation(),
4627 diag::note_odr_objc_property_impl_kind)
4628 << D->getPropertyDecl()->getDeclName()
4629 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00004630 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004631 }
4632
4633 // For @synthesize, check that we have the same
4634 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4635 Ivar != ToImpl->getPropertyIvarDecl()) {
4636 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
4637 diag::err_odr_objc_synthesize_ivar_inconsistent)
4638 << Property->getDeclName()
4639 << ToImpl->getPropertyIvarDecl()->getDeclName()
4640 << Ivar->getDeclName();
4641 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
4642 diag::note_odr_objc_synthesize_ivar_here)
4643 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00004644 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004645 }
4646
4647 // Merge the existing implementation with the new implementation.
4648 Importer.Imported(D, ToImpl);
4649 }
4650
4651 return ToImpl;
4652}
4653
Douglas Gregora082a492010-11-30 19:14:50 +00004654Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
4655 // For template arguments, we adopt the translation unit as our declaration
4656 // context. This context will be fixed when the actual template declaration
4657 // is created.
4658
4659 // FIXME: Import default argument.
4660 return TemplateTypeParmDecl::Create(Importer.getToContext(),
4661 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00004662 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004663 Importer.Import(D->getLocation()),
4664 D->getDepth(),
4665 D->getIndex(),
4666 Importer.Import(D->getIdentifier()),
4667 D->wasDeclaredWithTypename(),
4668 D->isParameterPack());
4669}
4670
4671Decl *
4672ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
4673 // Import the name of this declaration.
4674 DeclarationName Name = Importer.Import(D->getDeclName());
4675 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004676 return nullptr;
4677
Douglas Gregora082a492010-11-30 19:14:50 +00004678 // Import the location of this declaration.
4679 SourceLocation Loc = Importer.Import(D->getLocation());
4680
4681 // Import the type of this declaration.
4682 QualType T = Importer.Import(D->getType());
4683 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004684 return nullptr;
4685
Douglas Gregora082a492010-11-30 19:14:50 +00004686 // Import type-source information.
4687 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4688 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00004689 return nullptr;
4690
Douglas Gregora082a492010-11-30 19:14:50 +00004691 // FIXME: Import default argument.
4692
4693 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
4694 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004695 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004696 Loc, D->getDepth(), D->getPosition(),
4697 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00004698 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00004699}
4700
4701Decl *
4702ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4703 // Import the name of this declaration.
4704 DeclarationName Name = Importer.Import(D->getDeclName());
4705 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004706 return nullptr;
4707
Douglas Gregora082a492010-11-30 19:14:50 +00004708 // Import the location of this declaration.
4709 SourceLocation Loc = Importer.Import(D->getLocation());
4710
4711 // Import template parameters.
4712 TemplateParameterList *TemplateParams
4713 = ImportTemplateParameterList(D->getTemplateParameters());
4714 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004715 return nullptr;
4716
Douglas Gregora082a492010-11-30 19:14:50 +00004717 // FIXME: Import default argument.
4718
4719 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
4720 Importer.getToContext().getTranslationUnitDecl(),
4721 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00004722 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00004723 Name.getAsIdentifierInfo(),
4724 TemplateParams);
4725}
4726
4727Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
4728 // If this record has a definition in the translation unit we're coming from,
4729 // but this particular declaration is not that definition, import the
4730 // definition and map to that.
4731 CXXRecordDecl *Definition
4732 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
4733 if (Definition && Definition != D->getTemplatedDecl()) {
4734 Decl *ImportedDef
4735 = Importer.Import(Definition->getDescribedClassTemplate());
4736 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004737 return nullptr;
4738
Douglas Gregora082a492010-11-30 19:14:50 +00004739 return Importer.Imported(D, ImportedDef);
4740 }
4741
4742 // Import the major distinguishing characteristics of this class template.
4743 DeclContext *DC, *LexicalDC;
4744 DeclarationName Name;
4745 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004746 NamedDecl *ToD;
4747 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004748 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004749 if (ToD)
4750 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004751
Douglas Gregora082a492010-11-30 19:14:50 +00004752 // We may already have a template of the same name; try to find and match it.
4753 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004754 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004755 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004756 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004757 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4758 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00004759 continue;
4760
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004761 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00004762 if (ClassTemplateDecl *FoundTemplate
4763 = dyn_cast<ClassTemplateDecl>(Found)) {
4764 if (IsStructuralMatch(D, FoundTemplate)) {
4765 // The class templates structurally match; call it the same template.
4766 // FIXME: We may be filling in a forward declaration here. Handle
4767 // this case!
4768 Importer.Imported(D->getTemplatedDecl(),
4769 FoundTemplate->getTemplatedDecl());
4770 return Importer.Imported(D, FoundTemplate);
4771 }
4772 }
4773
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004774 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00004775 }
4776
4777 if (!ConflictingDecls.empty()) {
4778 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4779 ConflictingDecls.data(),
4780 ConflictingDecls.size());
4781 }
4782
4783 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004784 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00004785 }
4786
4787 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
4788
4789 // Create the declaration that is being templated.
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004790 // Create the declaration that is being templated.
4791 CXXRecordDecl *D2Templated = cast_or_null<CXXRecordDecl>(
4792 Importer.Import(DTemplated));
4793 if (!D2Templated)
4794 return nullptr;
4795
4796 // Resolve possible cyclic import.
4797 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
4798 return AlreadyImported;
4799
Douglas Gregora082a492010-11-30 19:14:50 +00004800 // Create the class template declaration itself.
4801 TemplateParameterList *TemplateParams
4802 = ImportTemplateParameterList(D->getTemplateParameters());
4803 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004804 return nullptr;
4805
Douglas Gregora082a492010-11-30 19:14:50 +00004806 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4807 Loc, Name, TemplateParams,
Vassil Vassilev352e4412017-01-12 09:16:26 +00004808 D2Templated);
Douglas Gregora082a492010-11-30 19:14:50 +00004809 D2Templated->setDescribedClassTemplate(D2);
4810
4811 D2->setAccess(D->getAccess());
4812 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004813 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004814
4815 // Note the relationship between the class templates.
4816 Importer.Imported(D, D2);
4817 Importer.Imported(DTemplated, D2Templated);
4818
John McCallf937c022011-10-07 06:10:15 +00004819 if (DTemplated->isCompleteDefinition() &&
4820 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004821 // FIXME: Import definition!
4822 }
4823
4824 return D2;
4825}
4826
Douglas Gregore2e50d332010-12-01 01:36:18 +00004827Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4828 ClassTemplateSpecializationDecl *D) {
4829 // If this record has a definition in the translation unit we're coming from,
4830 // but this particular declaration is not that definition, import the
4831 // definition and map to that.
4832 TagDecl *Definition = D->getDefinition();
4833 if (Definition && Definition != D) {
4834 Decl *ImportedDef = Importer.Import(Definition);
4835 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004836 return nullptr;
4837
Douglas Gregore2e50d332010-12-01 01:36:18 +00004838 return Importer.Imported(D, ImportedDef);
4839 }
4840
4841 ClassTemplateDecl *ClassTemplate
4842 = cast_or_null<ClassTemplateDecl>(Importer.Import(
4843 D->getSpecializedTemplate()));
4844 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004845 return nullptr;
4846
Douglas Gregore2e50d332010-12-01 01:36:18 +00004847 // Import the context of this declaration.
4848 DeclContext *DC = ClassTemplate->getDeclContext();
4849 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004850 return nullptr;
4851
Douglas Gregore2e50d332010-12-01 01:36:18 +00004852 DeclContext *LexicalDC = DC;
4853 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4854 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4855 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004856 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004857 }
4858
4859 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004860 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4861 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004862
4863 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004864 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004865 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4866 D->getTemplateArgs().size(),
4867 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004868 return nullptr;
4869
Douglas Gregore2e50d332010-12-01 01:36:18 +00004870 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004871 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004872 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004873 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004874 if (D2) {
4875 // We already have a class template specialization with these template
4876 // arguments.
4877
4878 // FIXME: Check for specialization vs. instantiation errors.
4879
4880 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004881 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004882 // The record types structurally match, or the "from" translation
4883 // unit only had a forward declaration anyway; call it the same
4884 // function.
4885 return Importer.Imported(D, FoundDef);
4886 }
4887 }
4888 } else {
4889 // Create a new specialization.
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004890 if (ClassTemplatePartialSpecializationDecl *PartialSpec =
4891 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
4892
4893 // Import TemplateArgumentListInfo
4894 TemplateArgumentListInfo ToTAInfo;
4895 auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
4896 for (unsigned I = 0, E = ASTTemplateArgs.NumTemplateArgs; I < E; ++I) {
4897 bool Error = false;
4898 auto ToLoc = ImportTemplateArgumentLoc(ASTTemplateArgs[I], Error);
4899 if (Error)
4900 return nullptr;
4901 ToTAInfo.addArgument(ToLoc);
4902 }
4903
4904 QualType CanonInjType = Importer.Import(
4905 PartialSpec->getInjectedSpecializationType());
4906 if (CanonInjType.isNull())
4907 return nullptr;
4908 CanonInjType = CanonInjType.getCanonicalType();
4909
4910 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4911 PartialSpec->getTemplateParameters());
4912 if (!ToTPList && PartialSpec->getTemplateParameters())
4913 return nullptr;
4914
4915 D2 = ClassTemplatePartialSpecializationDecl::Create(
4916 Importer.getToContext(), D->getTagKind(), DC, StartLoc, IdLoc,
4917 ToTPList, ClassTemplate,
4918 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
4919 ToTAInfo, CanonInjType, nullptr);
4920
4921 } else {
4922 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4923 D->getTagKind(), DC,
4924 StartLoc, IdLoc,
4925 ClassTemplate,
4926 TemplateArgs,
4927 /*PrevDecl=*/nullptr);
4928 }
4929
Douglas Gregore2e50d332010-12-01 01:36:18 +00004930 D2->setSpecializationKind(D->getSpecializationKind());
4931
4932 // Add this specialization to the class template.
4933 ClassTemplate->AddSpecialization(D2, InsertPos);
4934
4935 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004936 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004937
4938 Importer.Imported(D, D2);
4939
4940 if (auto *TSI = D->getTypeAsWritten()) {
4941 TypeSourceInfo *TInfo = Importer.Import(TSI);
4942 if (!TInfo)
4943 return nullptr;
4944 D2->setTypeAsWritten(TInfo);
4945 D2->setTemplateKeywordLoc(Importer.Import(D->getTemplateKeywordLoc()));
4946 D2->setExternLoc(Importer.Import(D->getExternLoc()));
4947 }
4948
4949 SourceLocation POI = Importer.Import(D->getPointOfInstantiation());
4950 if (POI.isValid())
4951 D2->setPointOfInstantiation(POI);
4952 else if (D->getPointOfInstantiation().isValid())
4953 return nullptr;
4954
4955 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
4956
Douglas Gregore2e50d332010-12-01 01:36:18 +00004957 // Add the specialization to this context.
4958 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004959 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004960 }
4961 Importer.Imported(D, D2);
John McCallf937c022011-10-07 06:10:15 +00004962 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004963 return nullptr;
4964
Douglas Gregore2e50d332010-12-01 01:36:18 +00004965 return D2;
4966}
4967
Larisse Voufo39a1e502013-08-06 01:03:05 +00004968Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4969 // If this variable has a definition in the translation unit we're coming
4970 // from,
4971 // but this particular declaration is not that definition, import the
4972 // definition and map to that.
4973 VarDecl *Definition =
4974 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4975 if (Definition && Definition != D->getTemplatedDecl()) {
4976 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4977 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004978 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004979
4980 return Importer.Imported(D, ImportedDef);
4981 }
4982
4983 // Import the major distinguishing characteristics of this variable template.
4984 DeclContext *DC, *LexicalDC;
4985 DeclarationName Name;
4986 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004987 NamedDecl *ToD;
4988 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004989 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004990 if (ToD)
4991 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004992
4993 // We may already have a template of the same name; try to find and match it.
4994 assert(!DC->isFunctionOrMethod() &&
4995 "Variable templates cannot be declared at function scope");
4996 SmallVector<NamedDecl *, 4> ConflictingDecls;
4997 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004998 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004999 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
5000 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
5001 continue;
5002
5003 Decl *Found = FoundDecls[I];
5004 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
5005 if (IsStructuralMatch(D, FoundTemplate)) {
5006 // The variable templates structurally match; call it the same template.
5007 Importer.Imported(D->getTemplatedDecl(),
5008 FoundTemplate->getTemplatedDecl());
5009 return Importer.Imported(D, FoundTemplate);
5010 }
5011 }
5012
5013 ConflictingDecls.push_back(FoundDecls[I]);
5014 }
5015
5016 if (!ConflictingDecls.empty()) {
5017 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
5018 ConflictingDecls.data(),
5019 ConflictingDecls.size());
5020 }
5021
5022 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00005023 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005024
5025 VarDecl *DTemplated = D->getTemplatedDecl();
5026
5027 // Import the type.
5028 QualType T = Importer.Import(DTemplated->getType());
5029 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005030 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005031
5032 // Create the declaration that is being templated.
5033 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
5034 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
5035 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo());
5036 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc,
5037 IdLoc, Name.getAsIdentifierInfo(), T,
5038 TInfo, DTemplated->getStorageClass());
5039 D2Templated->setAccess(DTemplated->getAccess());
5040 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
5041 D2Templated->setLexicalDeclContext(LexicalDC);
5042
5043 // Importer.Imported(DTemplated, D2Templated);
5044 // LexicalDC->addDeclInternal(D2Templated);
5045
5046 // Merge the initializer.
5047 if (ImportDefinition(DTemplated, D2Templated))
Craig Topper36250ad2014-05-12 05:36:57 +00005048 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005049
5050 // Create the variable template declaration itself.
5051 TemplateParameterList *TemplateParams =
5052 ImportTemplateParameterList(D->getTemplateParameters());
5053 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00005054 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005055
5056 VarTemplateDecl *D2 = VarTemplateDecl::Create(
Richard Smithbeef3452014-01-16 23:39:20 +00005057 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005058 D2Templated->setDescribedVarTemplate(D2);
5059
5060 D2->setAccess(D->getAccess());
5061 D2->setLexicalDeclContext(LexicalDC);
5062 LexicalDC->addDeclInternal(D2);
5063
5064 // Note the relationship between the variable templates.
5065 Importer.Imported(D, D2);
5066 Importer.Imported(DTemplated, D2Templated);
5067
5068 if (DTemplated->isThisDeclarationADefinition() &&
5069 !D2Templated->isThisDeclarationADefinition()) {
5070 // FIXME: Import definition!
5071 }
5072
5073 return D2;
5074}
5075
5076Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
5077 VarTemplateSpecializationDecl *D) {
5078 // If this record has a definition in the translation unit we're coming from,
5079 // but this particular declaration is not that definition, import the
5080 // definition and map to that.
5081 VarDecl *Definition = D->getDefinition();
5082 if (Definition && Definition != D) {
5083 Decl *ImportedDef = Importer.Import(Definition);
5084 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00005085 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005086
5087 return Importer.Imported(D, ImportedDef);
5088 }
5089
5090 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
5091 Importer.Import(D->getSpecializedTemplate()));
5092 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00005093 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005094
5095 // Import the context of this declaration.
5096 DeclContext *DC = VarTemplate->getDeclContext();
5097 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00005098 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005099
5100 DeclContext *LexicalDC = DC;
5101 if (D->getDeclContext() != D->getLexicalDeclContext()) {
5102 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
5103 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00005104 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005105 }
5106
5107 // Import the location of this declaration.
5108 SourceLocation StartLoc = Importer.Import(D->getLocStart());
5109 SourceLocation IdLoc = Importer.Import(D->getLocation());
5110
5111 // Import template arguments.
5112 SmallVector<TemplateArgument, 2> TemplateArgs;
5113 if (ImportTemplateArguments(D->getTemplateArgs().data(),
5114 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00005115 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005116
5117 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005118 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005119 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00005120 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005121 if (D2) {
5122 // We already have a variable template specialization with these template
5123 // arguments.
5124
5125 // FIXME: Check for specialization vs. instantiation errors.
5126
5127 if (VarDecl *FoundDef = D2->getDefinition()) {
5128 if (!D->isThisDeclarationADefinition() ||
5129 IsStructuralMatch(D, FoundDef)) {
5130 // The record types structurally match, or the "from" translation
5131 // unit only had a forward declaration anyway; call it the same
5132 // variable.
5133 return Importer.Imported(D, FoundDef);
5134 }
5135 }
5136 } else {
5137
5138 // Import the type.
5139 QualType T = Importer.Import(D->getType());
5140 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005141 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005142 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
5143
5144 // Create a new specialization.
5145 D2 = VarTemplateSpecializationDecl::Create(
5146 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
David Majnemer8b622692016-07-03 21:17:51 +00005147 D->getStorageClass(), TemplateArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005148 D2->setSpecializationKind(D->getSpecializationKind());
5149 D2->setTemplateArgsInfo(D->getTemplateArgsInfo());
5150
5151 // Add this specialization to the class template.
5152 VarTemplate->AddSpecialization(D2, InsertPos);
5153
5154 // Import the qualifier, if any.
5155 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
5156
5157 // Add the specialization to this context.
5158 D2->setLexicalDeclContext(LexicalDC);
5159 LexicalDC->addDeclInternal(D2);
5160 }
5161 Importer.Imported(D, D2);
5162
5163 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00005164 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005165
5166 return D2;
5167}
5168
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005169//----------------------------------------------------------------------------
5170// Import Statements
5171//----------------------------------------------------------------------------
5172
Sean Callanan59721b32015-04-28 18:41:46 +00005173DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
5174 if (DG.isNull())
5175 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
5176 size_t NumDecls = DG.end() - DG.begin();
5177 SmallVector<Decl *, 1> ToDecls(NumDecls);
5178 auto &_Importer = this->Importer;
5179 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
5180 [&_Importer](Decl *D) -> Decl * {
5181 return _Importer.Import(D);
5182 });
5183 return DeclGroupRef::Create(Importer.getToContext(),
5184 ToDecls.begin(),
5185 NumDecls);
5186}
5187
5188 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
5189 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
5190 << S->getStmtClassName();
5191 return nullptr;
5192 }
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005193
5194
5195Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
5196 SmallVector<IdentifierInfo *, 4> Names;
5197 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
5198 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
5199 if (!ToII)
5200 return nullptr;
5201 Names.push_back(ToII);
5202 }
5203 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
5204 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
5205 if (!ToII)
5206 return nullptr;
5207 Names.push_back(ToII);
5208 }
5209
5210 SmallVector<StringLiteral *, 4> Clobbers;
5211 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
5212 StringLiteral *Clobber = cast_or_null<StringLiteral>(
5213 Importer.Import(S->getClobberStringLiteral(I)));
5214 if (!Clobber)
5215 return nullptr;
5216 Clobbers.push_back(Clobber);
5217 }
5218
5219 SmallVector<StringLiteral *, 4> Constraints;
5220 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
5221 StringLiteral *Output = cast_or_null<StringLiteral>(
5222 Importer.Import(S->getOutputConstraintLiteral(I)));
5223 if (!Output)
5224 return nullptr;
5225 Constraints.push_back(Output);
5226 }
5227
5228 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
5229 StringLiteral *Input = cast_or_null<StringLiteral>(
5230 Importer.Import(S->getInputConstraintLiteral(I)));
5231 if (!Input)
5232 return nullptr;
5233 Constraints.push_back(Input);
5234 }
5235
5236 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005237 if (ImportContainerChecked(S->outputs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005238 return nullptr;
5239
Aleksei Sidorina693b372016-09-28 10:16:56 +00005240 if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005241 return nullptr;
5242
5243 StringLiteral *AsmStr = cast_or_null<StringLiteral>(
5244 Importer.Import(S->getAsmString()));
5245 if (!AsmStr)
5246 return nullptr;
5247
5248 return new (Importer.getToContext()) GCCAsmStmt(
5249 Importer.getToContext(),
5250 Importer.Import(S->getAsmLoc()),
5251 S->isSimple(),
5252 S->isVolatile(),
5253 S->getNumOutputs(),
5254 S->getNumInputs(),
5255 Names.data(),
5256 Constraints.data(),
5257 Exprs.data(),
5258 AsmStr,
5259 S->getNumClobbers(),
5260 Clobbers.data(),
5261 Importer.Import(S->getRParenLoc()));
5262}
5263
Sean Callanan59721b32015-04-28 18:41:46 +00005264Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
5265 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
5266 for (Decl *ToD : ToDG) {
5267 if (!ToD)
5268 return nullptr;
5269 }
5270 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
5271 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
5272 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
5273}
5274
5275Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
5276 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
5277 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
5278 S->hasLeadingEmptyMacro());
5279}
5280
5281Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005282 llvm::SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005283
5284 if (ImportContainerChecked(S->body(), ToStmts))
Sean Callanan8bca9962016-03-28 21:43:01 +00005285 return nullptr;
5286
Sean Callanan59721b32015-04-28 18:41:46 +00005287 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
5288 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
5289 return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(),
5290 ToStmts,
5291 ToLBraceLoc, ToRBraceLoc);
5292}
5293
5294Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
5295 Expr *ToLHS = Importer.Import(S->getLHS());
5296 if (!ToLHS)
5297 return nullptr;
5298 Expr *ToRHS = Importer.Import(S->getRHS());
5299 if (!ToRHS && S->getRHS())
5300 return nullptr;
5301 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
5302 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
5303 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
5304 return new (Importer.getToContext()) CaseStmt(ToLHS, ToRHS,
5305 ToCaseLoc, ToEllipsisLoc,
5306 ToColonLoc);
5307}
5308
5309Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
5310 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
5311 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
5312 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5313 if (!ToSubStmt && S->getSubStmt())
5314 return nullptr;
5315 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
5316 ToSubStmt);
5317}
5318
5319Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
5320 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
5321 LabelDecl *ToLabelDecl =
5322 cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
5323 if (!ToLabelDecl && S->getDecl())
5324 return nullptr;
5325 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5326 if (!ToSubStmt && S->getSubStmt())
5327 return nullptr;
5328 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
5329 ToSubStmt);
5330}
5331
5332Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
5333 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
5334 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
5335 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
5336 ASTContext &_ToContext = Importer.getToContext();
5337 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
5338 [&_ToContext](const Attr *A) -> const Attr * {
5339 return A->clone(_ToContext);
5340 });
5341 for (const Attr *ToA : ToAttrs) {
5342 if (!ToA)
5343 return nullptr;
5344 }
5345 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5346 if (!ToSubStmt && S->getSubStmt())
5347 return nullptr;
5348 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
5349 ToAttrs, ToSubStmt);
5350}
5351
5352Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
5353 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
Richard Smitha547eb22016-07-14 00:11:03 +00005354 Stmt *ToInit = Importer.Import(S->getInit());
5355 if (!ToInit && S->getInit())
5356 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005357 VarDecl *ToConditionVariable = nullptr;
5358 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5359 ToConditionVariable =
5360 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5361 if (!ToConditionVariable)
5362 return nullptr;
5363 }
5364 Expr *ToCondition = Importer.Import(S->getCond());
5365 if (!ToCondition && S->getCond())
5366 return nullptr;
5367 Stmt *ToThenStmt = Importer.Import(S->getThen());
5368 if (!ToThenStmt && S->getThen())
5369 return nullptr;
5370 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
5371 Stmt *ToElseStmt = Importer.Import(S->getElse());
5372 if (!ToElseStmt && S->getElse())
5373 return nullptr;
5374 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
Richard Smithb130fe72016-06-23 19:16:49 +00005375 ToIfLoc, S->isConstexpr(),
Richard Smitha547eb22016-07-14 00:11:03 +00005376 ToInit,
Richard Smithb130fe72016-06-23 19:16:49 +00005377 ToConditionVariable,
Sean Callanan59721b32015-04-28 18:41:46 +00005378 ToCondition, ToThenStmt,
5379 ToElseLoc, ToElseStmt);
5380}
5381
5382Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
Richard Smitha547eb22016-07-14 00:11:03 +00005383 Stmt *ToInit = Importer.Import(S->getInit());
5384 if (!ToInit && S->getInit())
5385 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005386 VarDecl *ToConditionVariable = nullptr;
5387 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5388 ToConditionVariable =
5389 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5390 if (!ToConditionVariable)
5391 return nullptr;
5392 }
5393 Expr *ToCondition = Importer.Import(S->getCond());
5394 if (!ToCondition && S->getCond())
5395 return nullptr;
5396 SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt(
Richard Smitha547eb22016-07-14 00:11:03 +00005397 Importer.getToContext(), ToInit,
5398 ToConditionVariable, ToCondition);
Sean Callanan59721b32015-04-28 18:41:46 +00005399 Stmt *ToBody = Importer.Import(S->getBody());
5400 if (!ToBody && S->getBody())
5401 return nullptr;
5402 ToStmt->setBody(ToBody);
5403 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
5404 // Now we have to re-chain the cases.
5405 SwitchCase *LastChainedSwitchCase = nullptr;
5406 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
5407 SC = SC->getNextSwitchCase()) {
5408 SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
5409 if (!ToSC)
5410 return nullptr;
5411 if (LastChainedSwitchCase)
5412 LastChainedSwitchCase->setNextSwitchCase(ToSC);
5413 else
5414 ToStmt->setSwitchCaseList(ToSC);
5415 LastChainedSwitchCase = ToSC;
5416 }
5417 return ToStmt;
5418}
5419
5420Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
5421 VarDecl *ToConditionVariable = nullptr;
5422 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5423 ToConditionVariable =
5424 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5425 if (!ToConditionVariable)
5426 return nullptr;
5427 }
5428 Expr *ToCondition = Importer.Import(S->getCond());
5429 if (!ToCondition && S->getCond())
5430 return nullptr;
5431 Stmt *ToBody = Importer.Import(S->getBody());
5432 if (!ToBody && S->getBody())
5433 return nullptr;
5434 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
5435 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
5436 ToConditionVariable,
5437 ToCondition, ToBody,
5438 ToWhileLoc);
5439}
5440
5441Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5442 Stmt *ToBody = Importer.Import(S->getBody());
5443 if (!ToBody && S->getBody())
5444 return nullptr;
5445 Expr *ToCondition = Importer.Import(S->getCond());
5446 if (!ToCondition && S->getCond())
5447 return nullptr;
5448 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
5449 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
5450 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5451 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
5452 ToDoLoc, ToWhileLoc,
5453 ToRParenLoc);
5454}
5455
5456Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
5457 Stmt *ToInit = Importer.Import(S->getInit());
5458 if (!ToInit && S->getInit())
5459 return nullptr;
5460 Expr *ToCondition = Importer.Import(S->getCond());
5461 if (!ToCondition && S->getCond())
5462 return nullptr;
5463 VarDecl *ToConditionVariable = nullptr;
5464 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5465 ToConditionVariable =
5466 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5467 if (!ToConditionVariable)
5468 return nullptr;
5469 }
5470 Expr *ToInc = Importer.Import(S->getInc());
5471 if (!ToInc && S->getInc())
5472 return nullptr;
5473 Stmt *ToBody = Importer.Import(S->getBody());
5474 if (!ToBody && S->getBody())
5475 return nullptr;
5476 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5477 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
5478 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5479 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
5480 ToInit, ToCondition,
5481 ToConditionVariable,
5482 ToInc, ToBody,
5483 ToForLoc, ToLParenLoc,
5484 ToRParenLoc);
5485}
5486
5487Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5488 LabelDecl *ToLabel = nullptr;
5489 if (LabelDecl *FromLabel = S->getLabel()) {
5490 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
5491 if (!ToLabel)
5492 return nullptr;
5493 }
5494 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
5495 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
5496 return new (Importer.getToContext()) GotoStmt(ToLabel,
5497 ToGotoLoc, ToLabelLoc);
5498}
5499
5500Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5501 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
5502 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
5503 Expr *ToTarget = Importer.Import(S->getTarget());
5504 if (!ToTarget && S->getTarget())
5505 return nullptr;
5506 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
5507 ToTarget);
5508}
5509
5510Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5511 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
5512 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
5513}
5514
5515Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5516 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
5517 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
5518}
5519
5520Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5521 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
5522 Expr *ToRetExpr = Importer.Import(S->getRetValue());
5523 if (!ToRetExpr && S->getRetValue())
5524 return nullptr;
5525 VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate());
5526 VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
5527 if (!ToNRVOCandidate && NRVOCandidate)
5528 return nullptr;
5529 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
5530 ToNRVOCandidate);
5531}
5532
5533Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5534 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
5535 VarDecl *ToExceptionDecl = nullptr;
5536 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
5537 ToExceptionDecl =
5538 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5539 if (!ToExceptionDecl)
5540 return nullptr;
5541 }
5542 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
5543 if (!ToHandlerBlock && S->getHandlerBlock())
5544 return nullptr;
5545 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
5546 ToExceptionDecl,
5547 ToHandlerBlock);
5548}
5549
5550Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5551 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
5552 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
5553 if (!ToTryBlock && S->getTryBlock())
5554 return nullptr;
5555 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5556 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5557 CXXCatchStmt *FromHandler = S->getHandler(HI);
5558 if (Stmt *ToHandler = Importer.Import(FromHandler))
5559 ToHandlers[HI] = ToHandler;
5560 else
5561 return nullptr;
5562 }
5563 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
5564 ToHandlers);
5565}
5566
5567Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
5568 DeclStmt *ToRange =
5569 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
5570 if (!ToRange && S->getRangeStmt())
5571 return nullptr;
Richard Smith01694c32016-03-20 10:33:40 +00005572 DeclStmt *ToBegin =
5573 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
5574 if (!ToBegin && S->getBeginStmt())
5575 return nullptr;
5576 DeclStmt *ToEnd =
5577 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
5578 if (!ToEnd && S->getEndStmt())
Sean Callanan59721b32015-04-28 18:41:46 +00005579 return nullptr;
5580 Expr *ToCond = Importer.Import(S->getCond());
5581 if (!ToCond && S->getCond())
5582 return nullptr;
5583 Expr *ToInc = Importer.Import(S->getInc());
5584 if (!ToInc && S->getInc())
5585 return nullptr;
5586 DeclStmt *ToLoopVar =
5587 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
5588 if (!ToLoopVar && S->getLoopVarStmt())
5589 return nullptr;
5590 Stmt *ToBody = Importer.Import(S->getBody());
5591 if (!ToBody && S->getBody())
5592 return nullptr;
5593 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
Richard Smith9f690bd2015-10-27 06:02:45 +00005594 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00005595 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
5596 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
Richard Smith01694c32016-03-20 10:33:40 +00005597 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
Sean Callanan59721b32015-04-28 18:41:46 +00005598 ToCond, ToInc,
5599 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00005600 ToForLoc, ToCoawaitLoc,
5601 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005602}
5603
5604Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5605 Stmt *ToElem = Importer.Import(S->getElement());
5606 if (!ToElem && S->getElement())
5607 return nullptr;
5608 Expr *ToCollect = Importer.Import(S->getCollection());
5609 if (!ToCollect && S->getCollection())
5610 return nullptr;
5611 Stmt *ToBody = Importer.Import(S->getBody());
5612 if (!ToBody && S->getBody())
5613 return nullptr;
5614 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5615 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5616 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
5617 ToCollect,
5618 ToBody, ToForLoc,
5619 ToRParenLoc);
5620}
5621
5622Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5623 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
5624 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5625 VarDecl *ToExceptionDecl = nullptr;
5626 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
5627 ToExceptionDecl =
5628 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5629 if (!ToExceptionDecl)
5630 return nullptr;
5631 }
5632 Stmt *ToBody = Importer.Import(S->getCatchBody());
5633 if (!ToBody && S->getCatchBody())
5634 return nullptr;
5635 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
5636 ToRParenLoc,
5637 ToExceptionDecl,
5638 ToBody);
5639}
5640
5641Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5642 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
5643 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
5644 if (!ToAtFinallyStmt && S->getFinallyBody())
5645 return nullptr;
5646 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
5647 ToAtFinallyStmt);
5648}
5649
5650Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5651 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
5652 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
5653 if (!ToAtTryStmt && S->getTryBody())
5654 return nullptr;
5655 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5656 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5657 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
5658 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
5659 ToCatchStmts[CI] = ToCatchStmt;
5660 else
5661 return nullptr;
5662 }
5663 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
5664 if (!ToAtFinallyStmt && S->getFinallyStmt())
5665 return nullptr;
5666 return ObjCAtTryStmt::Create(Importer.getToContext(),
5667 ToAtTryLoc, ToAtTryStmt,
5668 ToCatchStmts.begin(), ToCatchStmts.size(),
5669 ToAtFinallyStmt);
5670}
5671
5672Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
5673 (ObjCAtSynchronizedStmt *S) {
5674 SourceLocation ToAtSynchronizedLoc =
5675 Importer.Import(S->getAtSynchronizedLoc());
5676 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
5677 if (!ToSynchExpr && S->getSynchExpr())
5678 return nullptr;
5679 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
5680 if (!ToSynchBody && S->getSynchBody())
5681 return nullptr;
5682 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5683 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5684}
5685
5686Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5687 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5688 Expr *ToThrow = Importer.Import(S->getThrowExpr());
5689 if (!ToThrow && S->getThrowExpr())
5690 return nullptr;
5691 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5692}
5693
5694Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5695 (ObjCAutoreleasePoolStmt *S) {
5696 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5697 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5698 if (!ToSubStmt && S->getSubStmt())
5699 return nullptr;
5700 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5701 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005702}
5703
5704//----------------------------------------------------------------------------
5705// Import Expressions
5706//----------------------------------------------------------------------------
5707Expr *ASTNodeImporter::VisitExpr(Expr *E) {
5708 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5709 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00005710 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005711}
5712
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005713Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
5714 QualType T = Importer.Import(E->getType());
5715 if (T.isNull())
5716 return nullptr;
5717
5718 Expr *SubExpr = Importer.Import(E->getSubExpr());
5719 if (!SubExpr && E->getSubExpr())
5720 return nullptr;
5721
5722 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
5723 if (!TInfo)
5724 return nullptr;
5725
5726 return new (Importer.getToContext()) VAArgExpr(
5727 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
5728 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
5729}
5730
5731
5732Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
5733 QualType T = Importer.Import(E->getType());
5734 if (T.isNull())
5735 return nullptr;
5736
5737 return new (Importer.getToContext()) GNUNullExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005738 T, Importer.Import(E->getLocStart()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005739}
5740
5741Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
5742 QualType T = Importer.Import(E->getType());
5743 if (T.isNull())
5744 return nullptr;
5745
5746 StringLiteral *SL = cast_or_null<StringLiteral>(
5747 Importer.Import(E->getFunctionName()));
5748 if (!SL && E->getFunctionName())
5749 return nullptr;
5750
5751 return new (Importer.getToContext()) PredefinedExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005752 Importer.Import(E->getLocStart()), T, E->getIdentType(), SL);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005753}
5754
Douglas Gregor52f820e2010-02-19 01:17:02 +00005755Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00005756 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
5757 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005758 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005759
Craig Topper36250ad2014-05-12 05:36:57 +00005760 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005761 if (E->getDecl() != E->getFoundDecl()) {
5762 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5763 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00005764 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005765 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00005766
5767 QualType T = Importer.Import(E->getType());
5768 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005769 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005770
Aleksei Sidorina693b372016-09-28 10:16:56 +00005771
5772 TemplateArgumentListInfo ToTAInfo;
5773 TemplateArgumentListInfo *ResInfo = nullptr;
5774 if (E->hasExplicitTemplateArgs()) {
5775 for (const auto &FromLoc : E->template_arguments()) {
5776 bool Error = false;
5777 TemplateArgumentLoc ToTALoc = ImportTemplateArgumentLoc(FromLoc, Error);
5778 if (Error)
5779 return nullptr;
5780 ToTAInfo.addArgument(ToTALoc);
5781 }
5782 ResInfo = &ToTAInfo;
5783 }
5784
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005785 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5786 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00005787 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005788 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00005789 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005790 Importer.Import(E->getLocation()),
5791 T, E->getValueKind(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005792 FoundD, ResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005793 if (E->hadMultipleCandidates())
5794 DRE->setHadMultipleCandidates(true);
5795 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00005796}
5797
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005798Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
5799 QualType T = Importer.Import(E->getType());
5800 if (T.isNull())
Aleksei Sidorina693b372016-09-28 10:16:56 +00005801 return nullptr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005802
5803 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
5804}
5805
5806ASTNodeImporter::Designator
5807ASTNodeImporter::ImportDesignator(const Designator &D) {
5808 if (D.isFieldDesignator()) {
5809 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
5810 // Caller checks for import error
5811 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
5812 Importer.Import(D.getFieldLoc()));
5813 }
5814 if (D.isArrayDesignator())
5815 return Designator(D.getFirstExprIndex(),
5816 Importer.Import(D.getLBracketLoc()),
5817 Importer.Import(D.getRBracketLoc()));
5818
5819 assert(D.isArrayRangeDesignator());
5820 return Designator(D.getFirstExprIndex(),
5821 Importer.Import(D.getLBracketLoc()),
5822 Importer.Import(D.getEllipsisLoc()),
5823 Importer.Import(D.getRBracketLoc()));
5824}
5825
5826
5827Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
5828 Expr *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
5829 if (!Init)
5830 return nullptr;
5831
5832 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
5833 // List elements from the second, the first is Init itself
5834 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
5835 if (Expr *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
5836 IndexExprs[I - 1] = Arg;
5837 else
5838 return nullptr;
5839 }
5840
5841 SmallVector<Designator, 4> Designators(DIE->size());
David Majnemerf7e36092016-06-23 00:15:04 +00005842 llvm::transform(DIE->designators(), Designators.begin(),
5843 [this](const Designator &D) -> Designator {
5844 return ImportDesignator(D);
5845 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005846
David Majnemerf7e36092016-06-23 00:15:04 +00005847 for (const Designator &D : DIE->designators())
5848 if (D.isFieldDesignator() && !D.getFieldName())
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005849 return nullptr;
5850
5851 return DesignatedInitExpr::Create(
David Majnemerf7e36092016-06-23 00:15:04 +00005852 Importer.getToContext(), Designators,
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005853 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
5854 DIE->usesGNUSyntax(), Init);
5855}
5856
5857Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
5858 QualType T = Importer.Import(E->getType());
5859 if (T.isNull())
5860 return nullptr;
5861
5862 return new (Importer.getToContext())
5863 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
5864}
5865
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005866Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5867 QualType T = Importer.Import(E->getType());
5868 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005869 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005870
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005871 return IntegerLiteral::Create(Importer.getToContext(),
5872 E->getValue(), T,
5873 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005874}
5875
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005876Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
5877 QualType T = Importer.Import(E->getType());
5878 if (T.isNull())
5879 return nullptr;
5880
5881 return FloatingLiteral::Create(Importer.getToContext(),
5882 E->getValue(), E->isExact(), T,
5883 Importer.Import(E->getLocation()));
5884}
5885
Douglas Gregor623421d2010-02-18 02:21:22 +00005886Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5887 QualType T = Importer.Import(E->getType());
5888 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005889 return nullptr;
5890
Douglas Gregorfb65e592011-07-27 05:40:30 +00005891 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5892 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005893 Importer.Import(E->getLocation()));
5894}
5895
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005896Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
5897 QualType T = Importer.Import(E->getType());
5898 if (T.isNull())
5899 return nullptr;
5900
5901 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
5902 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
5903
5904 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
5905 E->getKind(), E->isPascal(), T,
5906 Locations.data(), Locations.size());
5907}
5908
5909Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
5910 QualType T = Importer.Import(E->getType());
5911 if (T.isNull())
5912 return nullptr;
5913
5914 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
5915 if (!TInfo)
5916 return nullptr;
5917
5918 Expr *Init = Importer.Import(E->getInitializer());
5919 if (!Init)
5920 return nullptr;
5921
5922 return new (Importer.getToContext()) CompoundLiteralExpr(
5923 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
5924 Init, E->isFileScope());
5925}
5926
5927Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
5928 QualType T = Importer.Import(E->getType());
5929 if (T.isNull())
5930 return nullptr;
5931
5932 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
5933 if (ImportArrayChecked(
5934 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
5935 Exprs.begin()))
5936 return nullptr;
5937
5938 return new (Importer.getToContext()) AtomicExpr(
5939 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
5940 Importer.Import(E->getRParenLoc()));
5941}
5942
5943Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
5944 QualType T = Importer.Import(E->getType());
5945 if (T.isNull())
5946 return nullptr;
5947
5948 LabelDecl *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
5949 if (!ToLabel)
5950 return nullptr;
5951
5952 return new (Importer.getToContext()) AddrLabelExpr(
5953 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
5954 ToLabel, T);
5955}
5956
Douglas Gregorc74247e2010-02-19 01:07:06 +00005957Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5958 Expr *SubExpr = Importer.Import(E->getSubExpr());
5959 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005960 return nullptr;
5961
Douglas Gregorc74247e2010-02-19 01:07:06 +00005962 return new (Importer.getToContext())
5963 ParenExpr(Importer.Import(E->getLParen()),
5964 Importer.Import(E->getRParen()),
5965 SubExpr);
5966}
5967
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005968Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
5969 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005970 if (ImportContainerChecked(E->exprs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005971 return nullptr;
5972
5973 return new (Importer.getToContext()) ParenListExpr(
5974 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
5975 Exprs, Importer.Import(E->getLParenLoc()));
5976}
5977
5978Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
5979 QualType T = Importer.Import(E->getType());
5980 if (T.isNull())
5981 return nullptr;
5982
5983 CompoundStmt *ToSubStmt = cast_or_null<CompoundStmt>(
5984 Importer.Import(E->getSubStmt()));
5985 if (!ToSubStmt && E->getSubStmt())
5986 return nullptr;
5987
5988 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
5989 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
5990}
5991
Douglas Gregorc74247e2010-02-19 01:07:06 +00005992Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5993 QualType T = Importer.Import(E->getType());
5994 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005995 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005996
5997 Expr *SubExpr = Importer.Import(E->getSubExpr());
5998 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005999 return nullptr;
6000
Douglas Gregorc74247e2010-02-19 01:07:06 +00006001 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00006002 T, E->getValueKind(),
6003 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00006004 Importer.Import(E->getOperatorLoc()));
6005}
6006
Peter Collingbournee190dee2011-03-11 19:24:49 +00006007Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
6008 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00006009 QualType ResultType = Importer.Import(E->getType());
6010
6011 if (E->isArgumentType()) {
6012 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
6013 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00006014 return nullptr;
6015
Peter Collingbournee190dee2011-03-11 19:24:49 +00006016 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
6017 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00006018 Importer.Import(E->getOperatorLoc()),
6019 Importer.Import(E->getRParenLoc()));
6020 }
6021
6022 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
6023 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00006024 return nullptr;
6025
Peter Collingbournee190dee2011-03-11 19:24:49 +00006026 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
6027 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00006028 Importer.Import(E->getOperatorLoc()),
6029 Importer.Import(E->getRParenLoc()));
6030}
6031
Douglas Gregorc74247e2010-02-19 01:07:06 +00006032Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
6033 QualType T = Importer.Import(E->getType());
6034 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006035 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00006036
6037 Expr *LHS = Importer.Import(E->getLHS());
6038 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00006039 return nullptr;
6040
Douglas Gregorc74247e2010-02-19 01:07:06 +00006041 Expr *RHS = Importer.Import(E->getRHS());
6042 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00006043 return nullptr;
6044
Douglas Gregorc74247e2010-02-19 01:07:06 +00006045 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00006046 T, E->getValueKind(),
6047 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00006048 Importer.Import(E->getOperatorLoc()),
6049 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006050}
6051
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006052Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
6053 QualType T = Importer.Import(E->getType());
6054 if (T.isNull())
6055 return nullptr;
6056
6057 Expr *ToLHS = Importer.Import(E->getLHS());
6058 if (!ToLHS)
6059 return nullptr;
6060
6061 Expr *ToRHS = Importer.Import(E->getRHS());
6062 if (!ToRHS)
6063 return nullptr;
6064
6065 Expr *ToCond = Importer.Import(E->getCond());
6066 if (!ToCond)
6067 return nullptr;
6068
6069 return new (Importer.getToContext()) ConditionalOperator(
6070 ToCond, Importer.Import(E->getQuestionLoc()),
6071 ToLHS, Importer.Import(E->getColonLoc()),
6072 ToRHS, T, E->getValueKind(), E->getObjectKind());
6073}
6074
6075Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
6076 BinaryConditionalOperator *E) {
6077 QualType T = Importer.Import(E->getType());
6078 if (T.isNull())
6079 return nullptr;
6080
6081 Expr *Common = Importer.Import(E->getCommon());
6082 if (!Common)
6083 return nullptr;
6084
6085 Expr *Cond = Importer.Import(E->getCond());
6086 if (!Cond)
6087 return nullptr;
6088
6089 OpaqueValueExpr *OpaqueValue = cast_or_null<OpaqueValueExpr>(
6090 Importer.Import(E->getOpaqueValue()));
6091 if (!OpaqueValue)
6092 return nullptr;
6093
6094 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
6095 if (!TrueExpr)
6096 return nullptr;
6097
6098 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
6099 if (!FalseExpr)
6100 return nullptr;
6101
6102 return new (Importer.getToContext()) BinaryConditionalOperator(
6103 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
6104 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
6105 T, E->getValueKind(), E->getObjectKind());
6106}
6107
Aleksei Sidorina693b372016-09-28 10:16:56 +00006108Expr *ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
6109 QualType T = Importer.Import(E->getType());
6110 if (T.isNull())
6111 return nullptr;
6112
6113 TypeSourceInfo *ToQueried = Importer.Import(E->getQueriedTypeSourceInfo());
6114 if (!ToQueried)
6115 return nullptr;
6116
6117 Expr *Dim = Importer.Import(E->getDimensionExpression());
6118 if (!Dim && E->getDimensionExpression())
6119 return nullptr;
6120
6121 return new (Importer.getToContext()) ArrayTypeTraitExpr(
6122 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
6123 E->getValue(), Dim, Importer.Import(E->getLocEnd()), T);
6124}
6125
6126Expr *ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
6127 QualType T = Importer.Import(E->getType());
6128 if (T.isNull())
6129 return nullptr;
6130
6131 Expr *ToQueried = Importer.Import(E->getQueriedExpression());
6132 if (!ToQueried)
6133 return nullptr;
6134
6135 return new (Importer.getToContext()) ExpressionTraitExpr(
6136 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
6137 E->getValue(), Importer.Import(E->getLocEnd()), T);
6138}
6139
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006140Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
6141 QualType T = Importer.Import(E->getType());
6142 if (T.isNull())
6143 return nullptr;
6144
6145 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
6146 if (!SourceExpr && E->getSourceExpr())
6147 return nullptr;
6148
6149 return new (Importer.getToContext()) OpaqueValueExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006150 Importer.Import(E->getLocation()), T, E->getValueKind(),
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006151 E->getObjectKind(), SourceExpr);
6152}
6153
Aleksei Sidorina693b372016-09-28 10:16:56 +00006154Expr *ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
6155 QualType T = Importer.Import(E->getType());
6156 if (T.isNull())
6157 return nullptr;
6158
6159 Expr *ToLHS = Importer.Import(E->getLHS());
6160 if (!ToLHS)
6161 return nullptr;
6162
6163 Expr *ToRHS = Importer.Import(E->getRHS());
6164 if (!ToRHS)
6165 return nullptr;
6166
6167 return new (Importer.getToContext()) ArraySubscriptExpr(
6168 ToLHS, ToRHS, T, E->getValueKind(), E->getObjectKind(),
6169 Importer.Import(E->getRBracketLoc()));
6170}
6171
Douglas Gregorc74247e2010-02-19 01:07:06 +00006172Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
6173 QualType T = Importer.Import(E->getType());
6174 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006175 return nullptr;
6176
Douglas Gregorc74247e2010-02-19 01:07:06 +00006177 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
6178 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006179 return nullptr;
6180
Douglas Gregorc74247e2010-02-19 01:07:06 +00006181 QualType CompResultType = Importer.Import(E->getComputationResultType());
6182 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006183 return nullptr;
6184
Douglas Gregorc74247e2010-02-19 01:07:06 +00006185 Expr *LHS = Importer.Import(E->getLHS());
6186 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00006187 return nullptr;
6188
Douglas Gregorc74247e2010-02-19 01:07:06 +00006189 Expr *RHS = Importer.Import(E->getRHS());
6190 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00006191 return nullptr;
6192
Douglas Gregorc74247e2010-02-19 01:07:06 +00006193 return new (Importer.getToContext())
6194 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00006195 T, E->getValueKind(),
6196 E->getObjectKind(),
6197 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00006198 Importer.Import(E->getOperatorLoc()),
6199 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006200}
6201
Aleksei Sidorina693b372016-09-28 10:16:56 +00006202bool ASTNodeImporter::ImportCastPath(CastExpr *CE, CXXCastPath &Path) {
6203 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
6204 if (CXXBaseSpecifier *Spec = Importer.Import(*I))
6205 Path.push_back(Spec);
6206 else
6207 return true;
6208 }
6209 return false;
John McCallcf142162010-08-07 06:22:56 +00006210}
6211
Douglas Gregor98c10182010-02-12 22:17:39 +00006212Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
6213 QualType T = Importer.Import(E->getType());
6214 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006215 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00006216
6217 Expr *SubExpr = Importer.Import(E->getSubExpr());
6218 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00006219 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00006220
6221 CXXCastPath BasePath;
6222 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00006223 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00006224
6225 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00006226 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00006227}
6228
Aleksei Sidorina693b372016-09-28 10:16:56 +00006229Expr *ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
Douglas Gregor5481d322010-02-19 01:32:14 +00006230 QualType T = Importer.Import(E->getType());
6231 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006232 return nullptr;
6233
Douglas Gregor5481d322010-02-19 01:32:14 +00006234 Expr *SubExpr = Importer.Import(E->getSubExpr());
6235 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00006236 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00006237
6238 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
6239 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00006240 return nullptr;
6241
John McCallcf142162010-08-07 06:22:56 +00006242 CXXCastPath BasePath;
6243 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00006244 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00006245
Aleksei Sidorina693b372016-09-28 10:16:56 +00006246 switch (E->getStmtClass()) {
6247 case Stmt::CStyleCastExprClass: {
6248 CStyleCastExpr *CCE = cast<CStyleCastExpr>(E);
6249 return CStyleCastExpr::Create(Importer.getToContext(), T,
6250 E->getValueKind(), E->getCastKind(),
6251 SubExpr, &BasePath, TInfo,
6252 Importer.Import(CCE->getLParenLoc()),
6253 Importer.Import(CCE->getRParenLoc()));
6254 }
6255
6256 case Stmt::CXXFunctionalCastExprClass: {
6257 CXXFunctionalCastExpr *FCE = cast<CXXFunctionalCastExpr>(E);
6258 return CXXFunctionalCastExpr::Create(Importer.getToContext(), T,
6259 E->getValueKind(), TInfo,
6260 E->getCastKind(), SubExpr, &BasePath,
6261 Importer.Import(FCE->getLParenLoc()),
6262 Importer.Import(FCE->getRParenLoc()));
6263 }
6264
6265 case Stmt::ObjCBridgedCastExprClass: {
6266 ObjCBridgedCastExpr *OCE = cast<ObjCBridgedCastExpr>(E);
6267 return new (Importer.getToContext()) ObjCBridgedCastExpr(
6268 Importer.Import(OCE->getLParenLoc()), OCE->getBridgeKind(),
6269 E->getCastKind(), Importer.Import(OCE->getBridgeKeywordLoc()),
6270 TInfo, SubExpr);
6271 }
6272 default:
6273 break; // just fall through
6274 }
6275
6276 CXXNamedCastExpr *Named = cast<CXXNamedCastExpr>(E);
6277 SourceLocation ExprLoc = Importer.Import(Named->getOperatorLoc()),
6278 RParenLoc = Importer.Import(Named->getRParenLoc());
6279 SourceRange Brackets = Importer.Import(Named->getAngleBrackets());
6280
6281 switch (E->getStmtClass()) {
6282 case Stmt::CXXStaticCastExprClass:
6283 return CXXStaticCastExpr::Create(Importer.getToContext(), T,
6284 E->getValueKind(), E->getCastKind(),
6285 SubExpr, &BasePath, TInfo,
6286 ExprLoc, RParenLoc, Brackets);
6287
6288 case Stmt::CXXDynamicCastExprClass:
6289 return CXXDynamicCastExpr::Create(Importer.getToContext(), T,
6290 E->getValueKind(), E->getCastKind(),
6291 SubExpr, &BasePath, TInfo,
6292 ExprLoc, RParenLoc, Brackets);
6293
6294 case Stmt::CXXReinterpretCastExprClass:
6295 return CXXReinterpretCastExpr::Create(Importer.getToContext(), T,
6296 E->getValueKind(), E->getCastKind(),
6297 SubExpr, &BasePath, TInfo,
6298 ExprLoc, RParenLoc, Brackets);
6299
6300 case Stmt::CXXConstCastExprClass:
6301 return CXXConstCastExpr::Create(Importer.getToContext(), T,
6302 E->getValueKind(), SubExpr, TInfo, ExprLoc,
6303 RParenLoc, Brackets);
6304 default:
6305 llvm_unreachable("Cast expression of unsupported type!");
6306 return nullptr;
6307 }
6308}
6309
6310Expr *ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *OE) {
6311 QualType T = Importer.Import(OE->getType());
6312 if (T.isNull())
6313 return nullptr;
6314
6315 SmallVector<OffsetOfNode, 4> Nodes;
6316 for (int I = 0, E = OE->getNumComponents(); I < E; ++I) {
6317 const OffsetOfNode &Node = OE->getComponent(I);
6318
6319 switch (Node.getKind()) {
6320 case OffsetOfNode::Array:
6321 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()),
6322 Node.getArrayExprIndex(),
6323 Importer.Import(Node.getLocEnd())));
6324 break;
6325
6326 case OffsetOfNode::Base: {
6327 CXXBaseSpecifier *BS = Importer.Import(Node.getBase());
6328 if (!BS && Node.getBase())
6329 return nullptr;
6330 Nodes.push_back(OffsetOfNode(BS));
6331 break;
6332 }
6333 case OffsetOfNode::Field: {
6334 FieldDecl *FD = cast_or_null<FieldDecl>(Importer.Import(Node.getField()));
6335 if (!FD)
6336 return nullptr;
6337 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), FD,
6338 Importer.Import(Node.getLocEnd())));
6339 break;
6340 }
6341 case OffsetOfNode::Identifier: {
6342 IdentifierInfo *ToII = Importer.Import(Node.getFieldName());
6343 if (!ToII)
6344 return nullptr;
6345 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), ToII,
6346 Importer.Import(Node.getLocEnd())));
6347 break;
6348 }
6349 }
6350 }
6351
6352 SmallVector<Expr *, 4> Exprs(OE->getNumExpressions());
6353 for (int I = 0, E = OE->getNumExpressions(); I < E; ++I) {
6354 Expr *ToIndexExpr = Importer.Import(OE->getIndexExpr(I));
6355 if (!ToIndexExpr)
6356 return nullptr;
6357 Exprs[I] = ToIndexExpr;
6358 }
6359
6360 TypeSourceInfo *TInfo = Importer.Import(OE->getTypeSourceInfo());
6361 if (!TInfo && OE->getTypeSourceInfo())
6362 return nullptr;
6363
6364 return OffsetOfExpr::Create(Importer.getToContext(), T,
6365 Importer.Import(OE->getOperatorLoc()),
6366 TInfo, Nodes, Exprs,
6367 Importer.Import(OE->getRParenLoc()));
6368}
6369
6370Expr *ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
6371 QualType T = Importer.Import(E->getType());
6372 if (T.isNull())
6373 return nullptr;
6374
6375 Expr *Operand = Importer.Import(E->getOperand());
6376 if (!Operand)
6377 return nullptr;
6378
6379 CanThrowResult CanThrow;
6380 if (E->isValueDependent())
6381 CanThrow = CT_Dependent;
6382 else
6383 CanThrow = E->getValue() ? CT_Can : CT_Cannot;
6384
6385 return new (Importer.getToContext()) CXXNoexceptExpr(
6386 T, Operand, CanThrow,
6387 Importer.Import(E->getLocStart()), Importer.Import(E->getLocEnd()));
6388}
6389
6390Expr *ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
6391 QualType T = Importer.Import(E->getType());
6392 if (T.isNull())
6393 return nullptr;
6394
6395 Expr *SubExpr = Importer.Import(E->getSubExpr());
6396 if (!SubExpr && E->getSubExpr())
6397 return nullptr;
6398
6399 return new (Importer.getToContext()) CXXThrowExpr(
6400 SubExpr, T, Importer.Import(E->getThrowLoc()),
6401 E->isThrownVariableInScope());
6402}
6403
6404Expr *ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
6405 ParmVarDecl *Param = cast_or_null<ParmVarDecl>(
6406 Importer.Import(E->getParam()));
6407 if (!Param)
6408 return nullptr;
6409
6410 return CXXDefaultArgExpr::Create(
6411 Importer.getToContext(), Importer.Import(E->getUsedLocation()), Param);
6412}
6413
6414Expr *ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
6415 QualType T = Importer.Import(E->getType());
6416 if (T.isNull())
6417 return nullptr;
6418
6419 TypeSourceInfo *TypeInfo = Importer.Import(E->getTypeSourceInfo());
6420 if (!TypeInfo)
6421 return nullptr;
6422
6423 return new (Importer.getToContext()) CXXScalarValueInitExpr(
6424 T, TypeInfo, Importer.Import(E->getRParenLoc()));
6425}
6426
6427Expr *ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
6428 Expr *SubExpr = Importer.Import(E->getSubExpr());
6429 if (!SubExpr)
6430 return nullptr;
6431
6432 auto *Dtor = cast_or_null<CXXDestructorDecl>(
6433 Importer.Import(const_cast<CXXDestructorDecl *>(
6434 E->getTemporary()->getDestructor())));
6435 if (!Dtor)
6436 return nullptr;
6437
6438 ASTContext &ToCtx = Importer.getToContext();
6439 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, Dtor);
6440 return CXXBindTemporaryExpr::Create(ToCtx, Temp, SubExpr);
6441}
6442
6443Expr *ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE) {
6444 QualType T = Importer.Import(CE->getType());
6445 if (T.isNull())
6446 return nullptr;
6447
6448 SmallVector<Expr *, 8> Args(CE->getNumArgs());
6449 if (ImportContainerChecked(CE->arguments(), Args))
6450 return nullptr;
6451
6452 auto *Ctor = cast_or_null<CXXConstructorDecl>(
6453 Importer.Import(CE->getConstructor()));
6454 if (!Ctor)
6455 return nullptr;
6456
6457 return CXXTemporaryObjectExpr::Create(
6458 Importer.getToContext(), T,
6459 Importer.Import(CE->getLocStart()),
6460 Ctor,
6461 CE->isElidable(),
6462 Args,
6463 CE->hadMultipleCandidates(),
6464 CE->isListInitialization(),
6465 CE->isStdInitListInitialization(),
6466 CE->requiresZeroInitialization(),
6467 CE->getConstructionKind(),
6468 Importer.Import(CE->getParenOrBraceRange()));
6469}
6470
6471Expr *
6472ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
6473 QualType T = Importer.Import(E->getType());
6474 if (T.isNull())
6475 return nullptr;
6476
6477 Expr *TempE = Importer.Import(E->GetTemporaryExpr());
6478 if (!TempE)
6479 return nullptr;
6480
6481 ValueDecl *ExtendedBy = cast_or_null<ValueDecl>(
6482 Importer.Import(const_cast<ValueDecl *>(E->getExtendingDecl())));
6483 if (!ExtendedBy && E->getExtendingDecl())
6484 return nullptr;
6485
6486 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
6487 T, TempE, E->isBoundToLvalueReference());
6488
6489 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
6490 ToMTE->setExtendingDecl(ExtendedBy, E->getManglingNumber());
6491 return ToMTE;
6492}
6493
6494Expr *ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *CE) {
6495 QualType T = Importer.Import(CE->getType());
6496 if (T.isNull())
6497 return nullptr;
6498
6499 SmallVector<Expr *, 4> PlacementArgs(CE->getNumPlacementArgs());
6500 if (ImportContainerChecked(CE->placement_arguments(), PlacementArgs))
6501 return nullptr;
6502
6503 FunctionDecl *OperatorNewDecl = cast_or_null<FunctionDecl>(
6504 Importer.Import(CE->getOperatorNew()));
6505 if (!OperatorNewDecl && CE->getOperatorNew())
6506 return nullptr;
6507
6508 FunctionDecl *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
6509 Importer.Import(CE->getOperatorDelete()));
6510 if (!OperatorDeleteDecl && CE->getOperatorDelete())
6511 return nullptr;
6512
6513 Expr *ToInit = Importer.Import(CE->getInitializer());
6514 if (!ToInit && CE->getInitializer())
6515 return nullptr;
6516
6517 TypeSourceInfo *TInfo = Importer.Import(CE->getAllocatedTypeSourceInfo());
6518 if (!TInfo)
6519 return nullptr;
6520
6521 Expr *ToArrSize = Importer.Import(CE->getArraySize());
6522 if (!ToArrSize && CE->getArraySize())
6523 return nullptr;
6524
6525 return new (Importer.getToContext()) CXXNewExpr(
6526 Importer.getToContext(),
6527 CE->isGlobalNew(),
6528 OperatorNewDecl, OperatorDeleteDecl,
Richard Smithb2f0f052016-10-10 18:54:32 +00006529 CE->passAlignment(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00006530 CE->doesUsualArrayDeleteWantSize(),
6531 PlacementArgs,
6532 Importer.Import(CE->getTypeIdParens()),
6533 ToArrSize, CE->getInitializationStyle(), ToInit, T, TInfo,
6534 Importer.Import(CE->getSourceRange()),
6535 Importer.Import(CE->getDirectInitRange()));
6536}
6537
6538Expr *ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
6539 QualType T = Importer.Import(E->getType());
6540 if (T.isNull())
6541 return nullptr;
6542
6543 FunctionDecl *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
6544 Importer.Import(E->getOperatorDelete()));
6545 if (!OperatorDeleteDecl && E->getOperatorDelete())
6546 return nullptr;
6547
6548 Expr *ToArg = Importer.Import(E->getArgument());
6549 if (!ToArg && E->getArgument())
6550 return nullptr;
6551
6552 return new (Importer.getToContext()) CXXDeleteExpr(
6553 T, E->isGlobalDelete(),
6554 E->isArrayForm(),
6555 E->isArrayFormAsWritten(),
6556 E->doesUsualArrayDeleteWantSize(),
6557 OperatorDeleteDecl,
6558 ToArg,
6559 Importer.Import(E->getLocStart()));
Douglas Gregor5481d322010-02-19 01:32:14 +00006560}
6561
Sean Callanan59721b32015-04-28 18:41:46 +00006562Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
6563 QualType T = Importer.Import(E->getType());
6564 if (T.isNull())
6565 return nullptr;
6566
6567 CXXConstructorDecl *ToCCD =
Sean Callanandd2c1742016-05-16 20:48:03 +00006568 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
Richard Smithc2bebe92016-05-11 20:37:46 +00006569 if (!ToCCD)
Sean Callanan59721b32015-04-28 18:41:46 +00006570 return nullptr;
6571
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006572 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006573 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006574 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006575
6576 return CXXConstructExpr::Create(Importer.getToContext(), T,
6577 Importer.Import(E->getLocation()),
Richard Smithc83bf822016-06-10 00:58:19 +00006578 ToCCD, E->isElidable(),
Sean Callanan59721b32015-04-28 18:41:46 +00006579 ToArgs, E->hadMultipleCandidates(),
6580 E->isListInitialization(),
6581 E->isStdInitListInitialization(),
6582 E->requiresZeroInitialization(),
6583 E->getConstructionKind(),
6584 Importer.Import(E->getParenOrBraceRange()));
6585}
6586
Aleksei Sidorina693b372016-09-28 10:16:56 +00006587Expr *ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *EWC) {
6588 Expr *SubExpr = Importer.Import(EWC->getSubExpr());
6589 if (!SubExpr && EWC->getSubExpr())
6590 return nullptr;
6591
6592 SmallVector<ExprWithCleanups::CleanupObject, 8> Objs(EWC->getNumObjects());
6593 for (unsigned I = 0, E = EWC->getNumObjects(); I < E; I++)
6594 if (ExprWithCleanups::CleanupObject Obj =
6595 cast_or_null<BlockDecl>(Importer.Import(EWC->getObject(I))))
6596 Objs[I] = Obj;
6597 else
6598 return nullptr;
6599
6600 return ExprWithCleanups::Create(Importer.getToContext(),
6601 SubExpr, EWC->cleanupsHaveSideEffects(),
6602 Objs);
6603}
6604
Sean Callanan8bca9962016-03-28 21:43:01 +00006605Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
6606 QualType T = Importer.Import(E->getType());
6607 if (T.isNull())
6608 return nullptr;
6609
6610 Expr *ToFn = Importer.Import(E->getCallee());
6611 if (!ToFn)
6612 return nullptr;
6613
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006614 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006615 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006616 return nullptr;
6617
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006618 return new (Importer.getToContext()) CXXMemberCallExpr(
6619 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
6620 Importer.Import(E->getRParenLoc()));
Sean Callanan8bca9962016-03-28 21:43:01 +00006621}
6622
6623Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
6624 QualType T = Importer.Import(E->getType());
6625 if (T.isNull())
6626 return nullptr;
6627
6628 return new (Importer.getToContext())
6629 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
6630}
6631
6632Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
6633 QualType T = Importer.Import(E->getType());
6634 if (T.isNull())
6635 return nullptr;
6636
6637 return new (Importer.getToContext())
6638 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
6639}
6640
6641
Sean Callanan59721b32015-04-28 18:41:46 +00006642Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
6643 QualType T = Importer.Import(E->getType());
6644 if (T.isNull())
6645 return nullptr;
6646
6647 Expr *ToBase = Importer.Import(E->getBase());
6648 if (!ToBase && E->getBase())
6649 return nullptr;
6650
6651 ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
6652 if (!ToMember && E->getMemberDecl())
6653 return nullptr;
6654
6655 DeclAccessPair ToFoundDecl = DeclAccessPair::make(
6656 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
6657 E->getFoundDecl().getAccess());
6658
6659 DeclarationNameInfo ToMemberNameInfo(
6660 Importer.Import(E->getMemberNameInfo().getName()),
6661 Importer.Import(E->getMemberNameInfo().getLoc()));
6662
6663 if (E->hasExplicitTemplateArgs()) {
6664 return nullptr; // FIXME: handle template arguments
6665 }
6666
6667 return MemberExpr::Create(Importer.getToContext(), ToBase,
6668 E->isArrow(),
6669 Importer.Import(E->getOperatorLoc()),
6670 Importer.Import(E->getQualifierLoc()),
6671 Importer.Import(E->getTemplateKeywordLoc()),
6672 ToMember, ToFoundDecl, ToMemberNameInfo,
6673 nullptr, T, E->getValueKind(),
6674 E->getObjectKind());
6675}
6676
6677Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
6678 QualType T = Importer.Import(E->getType());
6679 if (T.isNull())
6680 return nullptr;
6681
6682 Expr *ToCallee = Importer.Import(E->getCallee());
6683 if (!ToCallee && E->getCallee())
6684 return nullptr;
6685
6686 unsigned NumArgs = E->getNumArgs();
6687
6688 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
6689
6690 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) {
6691 Expr *FromArg = E->getArg(ai);
6692 Expr *ToArg = Importer.Import(FromArg);
6693 if (!ToArg)
6694 return nullptr;
6695 ToArgs[ai] = ToArg;
6696 }
6697
6698 Expr **ToArgs_Copied = new (Importer.getToContext())
6699 Expr*[NumArgs];
6700
6701 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
6702 ToArgs_Copied[ai] = ToArgs[ai];
6703
6704 return new (Importer.getToContext())
6705 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00006706 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00006707 Importer.Import(E->getRParenLoc()));
6708}
6709
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006710Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
6711 QualType T = Importer.Import(ILE->getType());
Sean Callanan8bca9962016-03-28 21:43:01 +00006712 if (T.isNull())
6713 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006714
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006715 llvm::SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006716 if (ImportContainerChecked(ILE->inits(), Exprs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006717 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006718
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006719 ASTContext &ToCtx = Importer.getToContext();
6720 InitListExpr *To = new (ToCtx) InitListExpr(
6721 ToCtx, Importer.Import(ILE->getLBraceLoc()),
6722 Exprs, Importer.Import(ILE->getLBraceLoc()));
6723 To->setType(T);
6724
6725 if (ILE->hasArrayFiller()) {
6726 Expr *Filler = Importer.Import(ILE->getArrayFiller());
6727 if (!Filler)
6728 return nullptr;
6729 To->setArrayFiller(Filler);
6730 }
6731
6732 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
6733 FieldDecl *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
6734 if (!ToFD)
6735 return nullptr;
6736 To->setInitializedFieldInUnion(ToFD);
6737 }
6738
6739 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
6740 InitListExpr *ToSyntForm = cast_or_null<InitListExpr>(
6741 Importer.Import(SyntForm));
6742 if (!ToSyntForm)
6743 return nullptr;
6744 To->setSyntacticForm(ToSyntForm);
6745 }
6746
6747 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
6748 To->setValueDependent(ILE->isValueDependent());
6749 To->setInstantiationDependent(ILE->isInstantiationDependent());
6750
6751 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00006752}
6753
Richard Smith30e304e2016-12-14 00:03:17 +00006754Expr *ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
6755 QualType ToType = Importer.Import(E->getType());
6756 if (ToType.isNull())
6757 return nullptr;
6758
6759 Expr *ToCommon = Importer.Import(E->getCommonExpr());
6760 if (!ToCommon && E->getCommonExpr())
6761 return nullptr;
6762
6763 Expr *ToSubExpr = Importer.Import(E->getSubExpr());
6764 if (!ToSubExpr && E->getSubExpr())
6765 return nullptr;
6766
6767 return new (Importer.getToContext())
6768 ArrayInitLoopExpr(ToType, ToCommon, ToSubExpr);
6769}
6770
6771Expr *ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
6772 QualType ToType = Importer.Import(E->getType());
6773 if (ToType.isNull())
6774 return nullptr;
6775 return new (Importer.getToContext()) ArrayInitIndexExpr(ToType);
6776}
6777
Sean Callanandd2c1742016-05-16 20:48:03 +00006778Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
6779 FieldDecl *ToField = llvm::dyn_cast_or_null<FieldDecl>(
6780 Importer.Import(DIE->getField()));
6781 if (!ToField && DIE->getField())
6782 return nullptr;
6783
6784 return CXXDefaultInitExpr::Create(
6785 Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField);
6786}
6787
6788Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
6789 QualType ToType = Importer.Import(E->getType());
6790 if (ToType.isNull() && !E->getType().isNull())
6791 return nullptr;
6792 ExprValueKind VK = E->getValueKind();
6793 CastKind CK = E->getCastKind();
6794 Expr *ToOp = Importer.Import(E->getSubExpr());
6795 if (!ToOp && E->getSubExpr())
6796 return nullptr;
6797 CXXCastPath BasePath;
6798 if (ImportCastPath(E, BasePath))
6799 return nullptr;
6800 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten());
6801 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc());
6802 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc());
6803 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets());
6804
6805 if (isa<CXXStaticCastExpr>(E)) {
6806 return CXXStaticCastExpr::Create(
6807 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6808 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6809 } else if (isa<CXXDynamicCastExpr>(E)) {
6810 return CXXDynamicCastExpr::Create(
6811 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6812 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6813 } else if (isa<CXXReinterpretCastExpr>(E)) {
6814 return CXXReinterpretCastExpr::Create(
6815 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6816 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6817 } else {
6818 return nullptr;
6819 }
6820}
6821
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006822
6823Expr *ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
6824 SubstNonTypeTemplateParmExpr *E) {
6825 QualType T = Importer.Import(E->getType());
6826 if (T.isNull())
6827 return nullptr;
6828
6829 NonTypeTemplateParmDecl *Param = cast_or_null<NonTypeTemplateParmDecl>(
6830 Importer.Import(E->getParameter()));
6831 if (!Param)
6832 return nullptr;
6833
6834 Expr *Replacement = Importer.Import(E->getReplacement());
6835 if (!Replacement)
6836 return nullptr;
6837
6838 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
6839 T, E->getValueKind(), Importer.Import(E->getExprLoc()), Param,
6840 Replacement);
6841}
6842
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006843ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00006844 ASTContext &FromContext, FileManager &FromFileManager,
6845 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00006846 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00006847 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Richard Smith5bb4cdf2012-12-20 02:22:15 +00006848 Minimal(MinimalImport), LastDiagFromFrom(false)
Douglas Gregor0a791672011-01-18 03:11:38 +00006849{
Douglas Gregor62d311f2010-02-09 19:21:46 +00006850 ImportedDecls[FromContext.getTranslationUnitDecl()]
6851 = ToContext.getTranslationUnitDecl();
6852}
6853
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +00006854ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00006855
6856QualType ASTImporter::Import(QualType FromT) {
6857 if (FromT.isNull())
6858 return QualType();
John McCall424cec92011-01-19 06:33:43 +00006859
6860 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00006861
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006862 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00006863 llvm::DenseMap<const Type *, const Type *>::iterator Pos
6864 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006865 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00006866 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006867
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006868 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00006869 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00006870 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00006871 if (ToT.isNull())
6872 return ToT;
6873
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006874 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00006875 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006876
John McCall424cec92011-01-19 06:33:43 +00006877 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006878}
6879
Douglas Gregor62d311f2010-02-09 19:21:46 +00006880TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006881 if (!FromTSI)
6882 return FromTSI;
6883
6884 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00006885 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006886 QualType T = Import(FromTSI->getType());
6887 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006888 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006889
6890 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00006891 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00006892}
6893
Sean Callanan59721b32015-04-28 18:41:46 +00006894Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
6895 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
6896 if (Pos != ImportedDecls.end()) {
6897 Decl *ToD = Pos->second;
6898 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
6899 return ToD;
6900 } else {
6901 return nullptr;
6902 }
6903}
6904
Douglas Gregor62d311f2010-02-09 19:21:46 +00006905Decl *ASTImporter::Import(Decl *FromD) {
6906 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00006907 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006908
Douglas Gregord451ea92011-07-29 23:31:30 +00006909 ASTNodeImporter Importer(*this);
6910
Douglas Gregor62d311f2010-02-09 19:21:46 +00006911 // Check whether we've already imported this declaration.
6912 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00006913 if (Pos != ImportedDecls.end()) {
6914 Decl *ToD = Pos->second;
6915 Importer.ImportDefinitionIfNeeded(FromD, ToD);
6916 return ToD;
6917 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00006918
6919 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00006920 Decl *ToD = Importer.Visit(FromD);
6921 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00006922 return nullptr;
6923
Douglas Gregor62d311f2010-02-09 19:21:46 +00006924 // Record the imported declaration.
6925 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00006926
6927 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
6928 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00006929 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00006930 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00006931 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00006932 // When we've finished transforming a typedef, see whether it was the
6933 // typedef for an anonymous tag.
Craig Topper2341c0d2013-07-04 03:08:24 +00006934 for (SmallVectorImpl<TagDecl *>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00006935 FromTag = AnonTagsWithPendingTypedefs.begin(),
6936 FromTagEnd = AnonTagsWithPendingTypedefs.end();
6937 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00006938 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00006939 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
6940 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00006941 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00006942 AnonTagsWithPendingTypedefs.erase(FromTag);
6943 break;
6944 }
6945 }
6946 }
6947 }
6948
Douglas Gregor62d311f2010-02-09 19:21:46 +00006949 return ToD;
6950}
6951
6952DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
6953 if (!FromDC)
6954 return FromDC;
6955
Douglas Gregor95d82832012-01-24 18:36:04 +00006956 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00006957 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00006958 return nullptr;
6959
Douglas Gregor2e15c842012-02-01 21:00:38 +00006960 // When we're using a record/enum/Objective-C class/protocol as a context, we
6961 // need it to have a definition.
6962 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00006963 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006964 if (ToRecord->isCompleteDefinition()) {
6965 // Do nothing.
6966 } else if (FromRecord->isCompleteDefinition()) {
6967 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
6968 ASTNodeImporter::IDK_Basic);
6969 } else {
6970 CompleteDecl(ToRecord);
6971 }
6972 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
6973 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
6974 if (ToEnum->isCompleteDefinition()) {
6975 // Do nothing.
6976 } else if (FromEnum->isCompleteDefinition()) {
6977 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
6978 ASTNodeImporter::IDK_Basic);
6979 } else {
6980 CompleteDecl(ToEnum);
6981 }
6982 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
6983 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
6984 if (ToClass->getDefinition()) {
6985 // Do nothing.
6986 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
6987 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
6988 ASTNodeImporter::IDK_Basic);
6989 } else {
6990 CompleteDecl(ToClass);
6991 }
6992 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
6993 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
6994 if (ToProto->getDefinition()) {
6995 // Do nothing.
6996 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
6997 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
6998 ASTNodeImporter::IDK_Basic);
6999 } else {
7000 CompleteDecl(ToProto);
7001 }
Douglas Gregor95d82832012-01-24 18:36:04 +00007002 }
7003
7004 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007005}
7006
7007Expr *ASTImporter::Import(Expr *FromE) {
7008 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00007009 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007010
7011 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
7012}
7013
7014Stmt *ASTImporter::Import(Stmt *FromS) {
7015 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00007016 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007017
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007018 // Check whether we've already imported this declaration.
7019 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
7020 if (Pos != ImportedStmts.end())
7021 return Pos->second;
7022
7023 // Import the type
7024 ASTNodeImporter Importer(*this);
7025 Stmt *ToS = Importer.Visit(FromS);
7026 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00007027 return nullptr;
7028
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007029 // Record the imported declaration.
7030 ImportedStmts[FromS] = ToS;
7031 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007032}
7033
7034NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
7035 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00007036 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007037
Douglas Gregor90ebf252011-04-27 16:48:40 +00007038 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
7039
7040 switch (FromNNS->getKind()) {
7041 case NestedNameSpecifier::Identifier:
7042 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
7043 return NestedNameSpecifier::Create(ToContext, prefix, II);
7044 }
Craig Topper36250ad2014-05-12 05:36:57 +00007045 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007046
7047 case NestedNameSpecifier::Namespace:
7048 if (NamespaceDecl *NS =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007049 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00007050 return NestedNameSpecifier::Create(ToContext, prefix, NS);
7051 }
Craig Topper36250ad2014-05-12 05:36:57 +00007052 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007053
7054 case NestedNameSpecifier::NamespaceAlias:
7055 if (NamespaceAliasDecl *NSAD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007056 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00007057 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
7058 }
Craig Topper36250ad2014-05-12 05:36:57 +00007059 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007060
7061 case NestedNameSpecifier::Global:
7062 return NestedNameSpecifier::GlobalSpecifier(ToContext);
7063
Nikola Smiljanic67860242014-09-26 00:28:20 +00007064 case NestedNameSpecifier::Super:
7065 if (CXXRecordDecl *RD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007066 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
Nikola Smiljanic67860242014-09-26 00:28:20 +00007067 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
7068 }
7069 return nullptr;
7070
Douglas Gregor90ebf252011-04-27 16:48:40 +00007071 case NestedNameSpecifier::TypeSpec:
7072 case NestedNameSpecifier::TypeSpecWithTemplate: {
7073 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
7074 if (!T.isNull()) {
7075 bool bTemplate = FromNNS->getKind() ==
7076 NestedNameSpecifier::TypeSpecWithTemplate;
7077 return NestedNameSpecifier::Create(ToContext, prefix,
7078 bTemplate, T.getTypePtr());
7079 }
7080 }
Craig Topper36250ad2014-05-12 05:36:57 +00007081 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007082 }
7083
7084 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00007085}
7086
Douglas Gregor14454802011-02-25 02:25:35 +00007087NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007088 // Copied from NestedNameSpecifier mostly.
7089 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
7090 NestedNameSpecifierLoc NNS = FromNNS;
7091
7092 // Push each of the nested-name-specifiers's onto a stack for
7093 // serialization in reverse order.
7094 while (NNS) {
7095 NestedNames.push_back(NNS);
7096 NNS = NNS.getPrefix();
7097 }
7098
7099 NestedNameSpecifierLocBuilder Builder;
7100
7101 while (!NestedNames.empty()) {
7102 NNS = NestedNames.pop_back_val();
7103 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
7104 if (!Spec)
7105 return NestedNameSpecifierLoc();
7106
7107 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
7108 switch (Kind) {
7109 case NestedNameSpecifier::Identifier:
7110 Builder.Extend(getToContext(),
7111 Spec->getAsIdentifier(),
7112 Import(NNS.getLocalBeginLoc()),
7113 Import(NNS.getLocalEndLoc()));
7114 break;
7115
7116 case NestedNameSpecifier::Namespace:
7117 Builder.Extend(getToContext(),
7118 Spec->getAsNamespace(),
7119 Import(NNS.getLocalBeginLoc()),
7120 Import(NNS.getLocalEndLoc()));
7121 break;
7122
7123 case NestedNameSpecifier::NamespaceAlias:
7124 Builder.Extend(getToContext(),
7125 Spec->getAsNamespaceAlias(),
7126 Import(NNS.getLocalBeginLoc()),
7127 Import(NNS.getLocalEndLoc()));
7128 break;
7129
7130 case NestedNameSpecifier::TypeSpec:
7131 case NestedNameSpecifier::TypeSpecWithTemplate: {
7132 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
7133 QualType(Spec->getAsType(), 0));
7134 Builder.Extend(getToContext(),
7135 Import(NNS.getLocalBeginLoc()),
7136 TSI->getTypeLoc(),
7137 Import(NNS.getLocalEndLoc()));
7138 break;
7139 }
7140
7141 case NestedNameSpecifier::Global:
7142 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
7143 break;
7144
7145 case NestedNameSpecifier::Super: {
7146 SourceRange ToRange = Import(NNS.getSourceRange());
7147 Builder.MakeSuper(getToContext(),
7148 Spec->getAsRecordDecl(),
7149 ToRange.getBegin(),
7150 ToRange.getEnd());
7151 }
7152 }
7153 }
7154
7155 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00007156}
7157
Douglas Gregore2e50d332010-12-01 01:36:18 +00007158TemplateName ASTImporter::Import(TemplateName From) {
7159 switch (From.getKind()) {
7160 case TemplateName::Template:
7161 if (TemplateDecl *ToTemplate
7162 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
7163 return TemplateName(ToTemplate);
7164
7165 return TemplateName();
7166
7167 case TemplateName::OverloadedTemplate: {
7168 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
7169 UnresolvedSet<2> ToTemplates;
7170 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
7171 E = FromStorage->end();
7172 I != E; ++I) {
7173 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
7174 ToTemplates.addDecl(To);
7175 else
7176 return TemplateName();
7177 }
7178 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
7179 ToTemplates.end());
7180 }
7181
7182 case TemplateName::QualifiedTemplate: {
7183 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
7184 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
7185 if (!Qualifier)
7186 return TemplateName();
7187
7188 if (TemplateDecl *ToTemplate
7189 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
7190 return ToContext.getQualifiedTemplateName(Qualifier,
7191 QTN->hasTemplateKeyword(),
7192 ToTemplate);
7193
7194 return TemplateName();
7195 }
7196
7197 case TemplateName::DependentTemplate: {
7198 DependentTemplateName *DTN = From.getAsDependentTemplateName();
7199 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
7200 if (!Qualifier)
7201 return TemplateName();
7202
7203 if (DTN->isIdentifier()) {
7204 return ToContext.getDependentTemplateName(Qualifier,
7205 Import(DTN->getIdentifier()));
7206 }
7207
7208 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
7209 }
John McCalld9dfe3a2011-06-30 08:33:18 +00007210
7211 case TemplateName::SubstTemplateTemplateParm: {
7212 SubstTemplateTemplateParmStorage *subst
7213 = From.getAsSubstTemplateTemplateParm();
7214 TemplateTemplateParmDecl *param
7215 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
7216 if (!param)
7217 return TemplateName();
7218
7219 TemplateName replacement = Import(subst->getReplacement());
7220 if (replacement.isNull()) return TemplateName();
7221
7222 return ToContext.getSubstTemplateTemplateParm(param, replacement);
7223 }
Douglas Gregor5590be02011-01-15 06:45:20 +00007224
7225 case TemplateName::SubstTemplateTemplateParmPack: {
7226 SubstTemplateTemplateParmPackStorage *SubstPack
7227 = From.getAsSubstTemplateTemplateParmPack();
7228 TemplateTemplateParmDecl *Param
7229 = cast_or_null<TemplateTemplateParmDecl>(
7230 Import(SubstPack->getParameterPack()));
7231 if (!Param)
7232 return TemplateName();
7233
7234 ASTNodeImporter Importer(*this);
7235 TemplateArgument ArgPack
7236 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
7237 if (ArgPack.isNull())
7238 return TemplateName();
7239
7240 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
7241 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00007242 }
7243
7244 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00007245}
7246
Douglas Gregor62d311f2010-02-09 19:21:46 +00007247SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
7248 if (FromLoc.isInvalid())
7249 return SourceLocation();
7250
Douglas Gregor811663e2010-02-10 00:15:17 +00007251 SourceManager &FromSM = FromContext.getSourceManager();
7252
Sean Callanan24c5fe62016-11-07 20:42:25 +00007253 // For now, map everything down to its file location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00007254 // don't have to import macro expansions.
7255 // FIXME: Import macro expansions!
Sean Callanan24c5fe62016-11-07 20:42:25 +00007256 FromLoc = FromSM.getFileLoc(FromLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00007257 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
7258 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00007259 FileID ToFileID = Import(Decomposed.first);
7260 if (ToFileID.isInvalid())
7261 return SourceLocation();
Sean Callanan59721b32015-04-28 18:41:46 +00007262 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
7263 .getLocWithOffset(Decomposed.second);
7264 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007265}
7266
7267SourceRange ASTImporter::Import(SourceRange FromRange) {
7268 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
7269}
7270
Douglas Gregor811663e2010-02-10 00:15:17 +00007271FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00007272 llvm::DenseMap<FileID, FileID>::iterator Pos
7273 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00007274 if (Pos != ImportedFileIDs.end())
7275 return Pos->second;
7276
7277 SourceManager &FromSM = FromContext.getSourceManager();
7278 SourceManager &ToSM = ToContext.getSourceManager();
7279 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00007280 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00007281
7282 // Include location of this file.
7283 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
7284
7285 // Map the FileID for to the "to" source manager.
7286 FileID ToID;
7287 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan25d34af2015-04-30 00:44:21 +00007288 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
Douglas Gregor811663e2010-02-10 00:15:17 +00007289 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
7290 // disk again
7291 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
7292 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00007293 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Sean Callanan238d8972014-12-10 01:26:39 +00007294 if (!Entry)
7295 return FileID();
Douglas Gregor811663e2010-02-10 00:15:17 +00007296 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
7297 FromSLoc.getFile().getFileCharacteristic());
7298 } else {
7299 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007300 const llvm::MemoryBuffer *
7301 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00007302 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00007303 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00007304 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00007305 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00007306 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00007307 }
7308
7309
Sebastian Redl99219f12010-09-30 01:03:06 +00007310 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00007311 return ToID;
7312}
7313
Sean Callanandd2c1742016-05-16 20:48:03 +00007314CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
7315 Expr *ToExpr = Import(From->getInit());
7316 if (!ToExpr && From->getInit())
7317 return nullptr;
7318
7319 if (From->isBaseInitializer()) {
7320 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
7321 if (!ToTInfo && From->getTypeSourceInfo())
7322 return nullptr;
7323
7324 return new (ToContext) CXXCtorInitializer(
7325 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
7326 ToExpr, Import(From->getRParenLoc()),
7327 From->isPackExpansion() ? Import(From->getEllipsisLoc())
7328 : SourceLocation());
7329 } else if (From->isMemberInitializer()) {
7330 FieldDecl *ToField =
7331 llvm::cast_or_null<FieldDecl>(Import(From->getMember()));
7332 if (!ToField && From->getMember())
7333 return nullptr;
7334
7335 return new (ToContext) CXXCtorInitializer(
7336 ToContext, ToField, Import(From->getMemberLocation()),
7337 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
7338 } else if (From->isIndirectMemberInitializer()) {
7339 IndirectFieldDecl *ToIField = llvm::cast_or_null<IndirectFieldDecl>(
7340 Import(From->getIndirectMember()));
7341 if (!ToIField && From->getIndirectMember())
7342 return nullptr;
7343
7344 return new (ToContext) CXXCtorInitializer(
7345 ToContext, ToIField, Import(From->getMemberLocation()),
7346 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
7347 } else if (From->isDelegatingInitializer()) {
7348 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
7349 if (!ToTInfo && From->getTypeSourceInfo())
7350 return nullptr;
7351
7352 return new (ToContext)
7353 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
7354 ToExpr, Import(From->getRParenLoc()));
Sean Callanandd2c1742016-05-16 20:48:03 +00007355 } else {
7356 return nullptr;
7357 }
7358}
7359
7360
Aleksei Sidorina693b372016-09-28 10:16:56 +00007361CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
7362 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
7363 if (Pos != ImportedCXXBaseSpecifiers.end())
7364 return Pos->second;
7365
7366 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
7367 Import(BaseSpec->getSourceRange()),
7368 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
7369 BaseSpec->getAccessSpecifierAsWritten(),
7370 Import(BaseSpec->getTypeSourceInfo()),
7371 Import(BaseSpec->getEllipsisLoc()));
7372 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
7373 return Imported;
7374}
7375
Douglas Gregor0a791672011-01-18 03:11:38 +00007376void ASTImporter::ImportDefinition(Decl *From) {
7377 Decl *To = Import(From);
7378 if (!To)
7379 return;
7380
7381 if (DeclContext *FromDC = cast<DeclContext>(From)) {
7382 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007383
7384 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
7385 if (!ToRecord->getDefinition()) {
7386 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00007387 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007388 return;
7389 }
7390 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007391
7392 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
7393 if (!ToEnum->getDefinition()) {
7394 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007395 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00007396 return;
7397 }
7398 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00007399
7400 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
7401 if (!ToIFace->getDefinition()) {
7402 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007403 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007404 return;
7405 }
7406 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007407
Douglas Gregor2aa53772012-01-24 17:42:07 +00007408 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
7409 if (!ToProto->getDefinition()) {
7410 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007411 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007412 return;
7413 }
7414 }
7415
Douglas Gregor0a791672011-01-18 03:11:38 +00007416 Importer.ImportDeclContext(FromDC, true);
7417 }
7418}
7419
Douglas Gregor96e578d2010-02-05 17:54:41 +00007420DeclarationName ASTImporter::Import(DeclarationName FromName) {
7421 if (!FromName)
7422 return DeclarationName();
7423
7424 switch (FromName.getNameKind()) {
7425 case DeclarationName::Identifier:
7426 return Import(FromName.getAsIdentifierInfo());
7427
7428 case DeclarationName::ObjCZeroArgSelector:
7429 case DeclarationName::ObjCOneArgSelector:
7430 case DeclarationName::ObjCMultiArgSelector:
7431 return Import(FromName.getObjCSelector());
7432
7433 case DeclarationName::CXXConstructorName: {
7434 QualType T = Import(FromName.getCXXNameType());
7435 if (T.isNull())
7436 return DeclarationName();
7437
7438 return ToContext.DeclarationNames.getCXXConstructorName(
7439 ToContext.getCanonicalType(T));
7440 }
7441
7442 case DeclarationName::CXXDestructorName: {
7443 QualType T = Import(FromName.getCXXNameType());
7444 if (T.isNull())
7445 return DeclarationName();
7446
7447 return ToContext.DeclarationNames.getCXXDestructorName(
7448 ToContext.getCanonicalType(T));
7449 }
7450
7451 case DeclarationName::CXXConversionFunctionName: {
7452 QualType T = Import(FromName.getCXXNameType());
7453 if (T.isNull())
7454 return DeclarationName();
7455
7456 return ToContext.DeclarationNames.getCXXConversionFunctionName(
7457 ToContext.getCanonicalType(T));
7458 }
7459
7460 case DeclarationName::CXXOperatorName:
7461 return ToContext.DeclarationNames.getCXXOperatorName(
7462 FromName.getCXXOverloadedOperator());
7463
7464 case DeclarationName::CXXLiteralOperatorName:
7465 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
7466 Import(FromName.getCXXLiteralIdentifier()));
7467
7468 case DeclarationName::CXXUsingDirective:
7469 // FIXME: STATICS!
7470 return DeclarationName::getUsingDirectiveName();
7471 }
7472
David Blaikiee4d798f2012-01-20 21:50:17 +00007473 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00007474}
7475
Douglas Gregore2e50d332010-12-01 01:36:18 +00007476IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00007477 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00007478 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007479
Sean Callananf94ef1d2016-05-14 06:11:19 +00007480 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
7481
7482 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
7483 ToId->setBuiltinID(FromId->getBuiltinID());
7484
7485 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007486}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007487
Douglas Gregor43f54792010-02-17 02:12:47 +00007488Selector ASTImporter::Import(Selector FromSel) {
7489 if (FromSel.isNull())
7490 return Selector();
7491
Chris Lattner0e62c1c2011-07-23 10:55:15 +00007492 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00007493 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
7494 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
7495 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
7496 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
7497}
7498
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007499DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
7500 DeclContext *DC,
7501 unsigned IDNS,
7502 NamedDecl **Decls,
7503 unsigned NumDecls) {
7504 return Name;
7505}
7506
7507DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007508 if (LastDiagFromFrom)
7509 ToContext.getDiagnostics().notePriorDiagnosticFrom(
7510 FromContext.getDiagnostics());
7511 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007512 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007513}
7514
7515DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007516 if (!LastDiagFromFrom)
7517 FromContext.getDiagnostics().notePriorDiagnosticFrom(
7518 ToContext.getDiagnostics());
7519 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007520 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007521}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007522
Douglas Gregor2e15c842012-02-01 21:00:38 +00007523void ASTImporter::CompleteDecl (Decl *D) {
7524 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
7525 if (!ID->getDefinition())
7526 ID->startDefinition();
7527 }
7528 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
7529 if (!PD->getDefinition())
7530 PD->startDefinition();
7531 }
7532 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
7533 if (!TD->getDefinition() && !TD->isBeingDefined()) {
7534 TD->startDefinition();
7535 TD->setCompleteDefinition(true);
7536 }
7537 }
7538 else {
7539 assert (0 && "CompleteDecl called on a Decl that can't be completed");
7540 }
7541}
7542
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007543Decl *ASTImporter::Imported(Decl *From, Decl *To) {
Sean Callanan8bca9962016-03-28 21:43:01 +00007544 if (From->hasAttrs()) {
7545 for (Attr *FromAttr : From->getAttrs())
7546 To->addAttr(FromAttr->clone(To->getASTContext()));
7547 }
7548 if (From->isUsed()) {
7549 To->setIsUsed();
7550 }
Sean Callanandd2c1742016-05-16 20:48:03 +00007551 if (From->isImplicit()) {
7552 To->setImplicit();
7553 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007554 ImportedDecls[From] = To;
7555 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00007556}
Douglas Gregorb4964f72010-02-15 23:54:17 +00007557
Douglas Gregordd6006f2012-07-17 21:16:27 +00007558bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
7559 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00007560 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00007561 = ImportedTypes.find(From.getTypePtr());
7562 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
7563 return true;
7564
Douglas Gregordd6006f2012-07-17 21:16:27 +00007565 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
7566 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00007567 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00007568}