blob: 76b495f84d504992b7f437bbba5db1138a5dab7f [file] [log] [blame]
Douglas Gregor96e578d2010-02-05 17:54:41 +00001//===--- ASTImporter.cpp - Importing ASTs from other Contexts ---*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ASTImporter class which imports AST nodes from one
11// context into another context.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/AST/ASTImporter.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000015#include "clang/AST/ASTContext.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000016#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor5c73e912010-02-11 00:48:18 +000017#include "clang/AST/DeclCXX.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000019#include "clang/AST/DeclVisitor.h"
Douglas Gregor7eeb5972010-02-11 19:21:55 +000020#include "clang/AST/StmtVisitor.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000021#include "clang/AST/TypeVisitor.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000022#include "clang/Basic/FileManager.h"
23#include "clang/Basic/SourceManager.h"
24#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor3996e242010-02-15 22:01:00 +000025#include <deque>
Douglas Gregor96e578d2010-02-05 17:54:41 +000026
Douglas Gregor3c2404b2011-11-03 18:07:07 +000027namespace clang {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000028 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor7eeb5972010-02-11 19:21:55 +000029 public DeclVisitor<ASTNodeImporter, Decl *>,
30 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor96e578d2010-02-05 17:54:41 +000031 ASTImporter &Importer;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000032
Douglas Gregor96e578d2010-02-05 17:54:41 +000033 public:
34 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
35
36 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor62d311f2010-02-09 19:21:46 +000037 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor7eeb5972010-02-11 19:21:55 +000038 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +000039
40 // Importing types
John McCall424cec92011-01-19 06:33:43 +000041 QualType VisitType(const Type *T);
42 QualType VisitBuiltinType(const BuiltinType *T);
43 QualType VisitComplexType(const ComplexType *T);
44 QualType VisitPointerType(const PointerType *T);
45 QualType VisitBlockPointerType(const BlockPointerType *T);
46 QualType VisitLValueReferenceType(const LValueReferenceType *T);
47 QualType VisitRValueReferenceType(const RValueReferenceType *T);
48 QualType VisitMemberPointerType(const MemberPointerType *T);
49 QualType VisitConstantArrayType(const ConstantArrayType *T);
50 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
51 QualType VisitVariableArrayType(const VariableArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000052 // FIXME: DependentSizedArrayType
53 // FIXME: DependentSizedExtVectorType
John McCall424cec92011-01-19 06:33:43 +000054 QualType VisitVectorType(const VectorType *T);
55 QualType VisitExtVectorType(const ExtVectorType *T);
56 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
57 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000058 // FIXME: UnresolvedUsingType
Sean Callananda6df8a2011-08-11 16:56:07 +000059 QualType VisitParenType(const ParenType *T);
John McCall424cec92011-01-19 06:33:43 +000060 QualType VisitTypedefType(const TypedefType *T);
61 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000062 // FIXME: DependentTypeOfExprType
John McCall424cec92011-01-19 06:33:43 +000063 QualType VisitTypeOfType(const TypeOfType *T);
64 QualType VisitDecltypeType(const DecltypeType *T);
Alexis Hunte852b102011-05-24 22:41:36 +000065 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith30482bc2011-02-20 03:19:35 +000066 QualType VisitAutoType(const AutoType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000067 QualType VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000068 // FIXME: DependentDecltypeType
John McCall424cec92011-01-19 06:33:43 +000069 QualType VisitRecordType(const RecordType *T);
70 QualType VisitEnumType(const EnumType *T);
Sean Callanan72fe0852015-04-02 23:50:08 +000071 QualType VisitAttributedType(const AttributedType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000072 QualType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000073 // FIXME: SubstTemplateTypeParmType
John McCall424cec92011-01-19 06:33:43 +000074 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
75 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +000076 // FIXME: DependentNameType
John McCallc392f372010-06-11 00:33:02 +000077 // FIXME: DependentTemplateSpecializationType
John McCall424cec92011-01-19 06:33:43 +000078 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
79 QualType VisitObjCObjectType(const ObjCObjectType *T);
80 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000081
Douglas Gregor95d82832012-01-24 18:36:04 +000082 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +000083 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
84 DeclContext *&LexicalDC, DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +000085 NamedDecl *&ToD, SourceLocation &Loc);
Craig Topper36250ad2014-05-12 05:36:57 +000086 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +000087 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
88 DeclarationNameInfo& To);
Douglas Gregor0a791672011-01-18 03:11:38 +000089 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000090
91 typedef DesignatedInitExpr::Designator Designator;
92 Designator ImportDesignator(const Designator &D);
93
Douglas Gregor2e15c842012-02-01 21:00:38 +000094
Douglas Gregor95d82832012-01-24 18:36:04 +000095 /// \brief What we should import from the definition.
96 enum ImportDefinitionKind {
97 /// \brief Import the default subset of the definition, which might be
98 /// nothing (if minimal import is set) or might be everything (if minimal
99 /// import is not set).
100 IDK_Default,
101 /// \brief Import everything.
102 IDK_Everything,
103 /// \brief Import only the bare bones needed to establish a valid
104 /// DeclContext.
105 IDK_Basic
106 };
107
Douglas Gregor2e15c842012-02-01 21:00:38 +0000108 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
109 return IDK == IDK_Everything ||
110 (IDK == IDK_Default && !Importer.isMinimalImport());
111 }
112
Douglas Gregord451ea92011-07-29 23:31:30 +0000113 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000114 ImportDefinitionKind Kind = IDK_Default);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000115 bool ImportDefinition(VarDecl *From, VarDecl *To,
116 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregord451ea92011-07-29 23:31:30 +0000117 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000118 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000119 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000120 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000121 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000122 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregora082a492010-11-30 19:14:50 +0000123 TemplateParameterList *ImportTemplateParameterList(
124 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000125 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
126 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
127 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000128 SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000129 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
130 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000131 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
132 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000133 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000134 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Douglas Gregora082a492010-11-30 19:14:50 +0000135 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000136 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000137 Decl *VisitDecl(Decl *D);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +0000138 Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000139 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000140 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000141 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000142 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000143 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000144 Decl *VisitLabelDecl(LabelDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000145 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000146 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000147 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000148 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000149 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
150 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
151 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
152 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000153 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000154 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000155 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000156 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000157 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000158 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000159 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000160 Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000161 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000162 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Sean Callanan0aae0412014-12-10 00:00:37 +0000163 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000164
165 ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list);
Douglas Gregor45635322010-02-16 01:20:57 +0000166 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000167 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000168 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000169 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000170 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000171 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
172 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
173 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
174 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000175 Decl *VisitClassTemplateSpecializationDecl(
176 ClassTemplateSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000177 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
178 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
179
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000180 // Importing statements
Sean Callanan59721b32015-04-28 18:41:46 +0000181 DeclGroupRef ImportDeclGroup(DeclGroupRef DG);
182
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000183 Stmt *VisitStmt(Stmt *S);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000184 Stmt *VisitGCCAsmStmt(GCCAsmStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000185 Stmt *VisitDeclStmt(DeclStmt *S);
186 Stmt *VisitNullStmt(NullStmt *S);
187 Stmt *VisitCompoundStmt(CompoundStmt *S);
188 Stmt *VisitCaseStmt(CaseStmt *S);
189 Stmt *VisitDefaultStmt(DefaultStmt *S);
190 Stmt *VisitLabelStmt(LabelStmt *S);
191 Stmt *VisitAttributedStmt(AttributedStmt *S);
192 Stmt *VisitIfStmt(IfStmt *S);
193 Stmt *VisitSwitchStmt(SwitchStmt *S);
194 Stmt *VisitWhileStmt(WhileStmt *S);
195 Stmt *VisitDoStmt(DoStmt *S);
196 Stmt *VisitForStmt(ForStmt *S);
197 Stmt *VisitGotoStmt(GotoStmt *S);
198 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S);
199 Stmt *VisitContinueStmt(ContinueStmt *S);
200 Stmt *VisitBreakStmt(BreakStmt *S);
201 Stmt *VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000202 // FIXME: MSAsmStmt
203 // FIXME: SEHExceptStmt
204 // FIXME: SEHFinallyStmt
205 // FIXME: SEHTryStmt
206 // FIXME: SEHLeaveStmt
207 // FIXME: CapturedStmt
208 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S);
209 Stmt *VisitCXXTryStmt(CXXTryStmt *S);
210 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S);
211 // FIXME: MSDependentExistsStmt
212 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
213 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
214 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
215 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
216 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
217 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
218 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000219
220 // Importing expressions
221 Expr *VisitExpr(Expr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000222 Expr *VisitVAArgExpr(VAArgExpr *E);
223 Expr *VisitGNUNullExpr(GNUNullExpr *E);
224 Expr *VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000225 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000226 Expr *VisitImplicitValueInitExpr(ImplicitValueInitExpr *ILE);
227 Expr *VisitDesignatedInitExpr(DesignatedInitExpr *E);
228 Expr *VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000229 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000230 Expr *VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000231 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000232 Expr *VisitStringLiteral(StringLiteral *E);
233 Expr *VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
234 Expr *VisitAtomicExpr(AtomicExpr *E);
235 Expr *VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000236 Expr *VisitParenExpr(ParenExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000237 Expr *VisitParenListExpr(ParenListExpr *E);
238 Expr *VisitStmtExpr(StmtExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000239 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000240 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000241 Expr *VisitBinaryOperator(BinaryOperator *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000242 Expr *VisitConditionalOperator(ConditionalOperator *E);
243 Expr *VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
244 Expr *VisitOpaqueValueExpr(OpaqueValueExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000245 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000246 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor5481d322010-02-19 01:32:14 +0000247 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000248 Expr *VisitCXXConstructExpr(CXXConstructExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000249 Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
250 Expr *VisitCXXThisExpr(CXXThisExpr *E);
251 Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000252 Expr *VisitMemberExpr(MemberExpr *E);
253 Expr *VisitCallExpr(CallExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000254 Expr *VisitInitListExpr(InitListExpr *E);
Sean Callanandd2c1742016-05-16 20:48:03 +0000255 Expr *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
256 Expr *VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000257
258 template<typename IIter, typename OIter>
259 void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) {
260 typedef typename std::remove_reference<decltype(*Obegin)>::type ItemT;
261 ASTImporter &ImporterRef = Importer;
262 std::transform(Ibegin, Iend, Obegin,
263 [&ImporterRef](ItemT From) -> ItemT {
264 return ImporterRef.Import(From);
Sean Callanan8bca9962016-03-28 21:43:01 +0000265 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000266 }
267
268 template<typename IIter, typename OIter>
269 bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
270 typedef typename std::remove_reference<decltype(**Obegin)>::type ItemT;
271 ASTImporter &ImporterRef = Importer;
272 bool Failed = false;
273 std::transform(Ibegin, Iend, Obegin,
274 [&ImporterRef, &Failed](ItemT *From) -> ItemT * {
275 ItemT *To = ImporterRef.Import(From);
276 if (!To && From)
277 Failed = true;
278 return To;
279 });
280 return Failed;
Sean Callanan8bca9962016-03-28 21:43:01 +0000281 }
Douglas Gregor96e578d2010-02-05 17:54:41 +0000282 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000283}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000284
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000285using namespace clang;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000286
287//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000288// Structural Equivalence
289//----------------------------------------------------------------------------
290
291namespace {
292 struct StructuralEquivalenceContext {
293 /// \brief AST contexts for which we are checking structural equivalence.
294 ASTContext &C1, &C2;
295
Douglas Gregor3996e242010-02-15 22:01:00 +0000296 /// \brief The set of "tentative" equivalences between two canonical
297 /// declarations, mapping from a declaration in the first context to the
298 /// declaration in the second context that we believe to be equivalent.
299 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
300
301 /// \brief Queue of declarations in the first context whose equivalence
302 /// with a declaration in the second context still needs to be verified.
303 std::deque<Decl *> DeclsToCheck;
304
Douglas Gregorb4964f72010-02-15 23:54:17 +0000305 /// \brief Declaration (from, to) pairs that are known not to be equivalent
306 /// (which we have already complained about).
307 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
308
Douglas Gregor3996e242010-02-15 22:01:00 +0000309 /// \brief Whether we're being strict about the spelling of types when
310 /// unifying two types.
311 bool StrictTypeSpelling;
Douglas Gregordd6006f2012-07-17 21:16:27 +0000312
313 /// \brief Whether to complain about failures.
314 bool Complain;
315
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000316 /// \brief \c true if the last diagnostic came from C2.
317 bool LastDiagFromC2;
318
Douglas Gregor3996e242010-02-15 22:01:00 +0000319 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000320 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregordd6006f2012-07-17 21:16:27 +0000321 bool StrictTypeSpelling = false,
322 bool Complain = true)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000323 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000324 StrictTypeSpelling(StrictTypeSpelling), Complain(Complain),
325 LastDiagFromC2(false) {}
Douglas Gregor3996e242010-02-15 22:01:00 +0000326
327 /// \brief Determine whether the two declarations are structurally
328 /// equivalent.
329 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
330
331 /// \brief Determine whether the two types are structurally equivalent.
332 bool IsStructurallyEquivalent(QualType T1, QualType T2);
333
334 private:
335 /// \brief Finish checking all of the structural equivalences.
336 ///
337 /// \returns true if an error occurred, false otherwise.
338 bool Finish();
339
340 public:
341 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000342 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000343 if (LastDiagFromC2)
344 C1.getDiagnostics().notePriorDiagnosticFrom(C2.getDiagnostics());
345 LastDiagFromC2 = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000346 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000347 }
348
349 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000350 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000351 if (!LastDiagFromC2)
352 C2.getDiagnostics().notePriorDiagnosticFrom(C1.getDiagnostics());
353 LastDiagFromC2 = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000354 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000355 }
356 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000357}
Douglas Gregor3996e242010-02-15 22:01:00 +0000358
359static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
360 QualType T1, QualType T2);
361static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
362 Decl *D1, Decl *D2);
363
Douglas Gregor3996e242010-02-15 22:01:00 +0000364/// \brief Determine structural equivalence of two expressions.
365static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
366 Expr *E1, Expr *E2) {
367 if (!E1 || !E2)
368 return E1 == E2;
369
370 // FIXME: Actually perform a structural comparison!
371 return true;
372}
373
374/// \brief Determine whether two identifiers are equivalent.
375static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
376 const IdentifierInfo *Name2) {
377 if (!Name1 || !Name2)
378 return Name1 == Name2;
379
380 return Name1->getName() == Name2->getName();
381}
382
383/// \brief Determine whether two nested-name-specifiers are equivalent.
384static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
385 NestedNameSpecifier *NNS1,
386 NestedNameSpecifier *NNS2) {
387 // FIXME: Implement!
388 return true;
389}
390
391/// \brief Determine whether two template arguments are equivalent.
392static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
393 const TemplateArgument &Arg1,
394 const TemplateArgument &Arg2) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000395 if (Arg1.getKind() != Arg2.getKind())
396 return false;
397
398 switch (Arg1.getKind()) {
399 case TemplateArgument::Null:
400 return true;
401
402 case TemplateArgument::Type:
403 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
Eli Friedmanb826a002012-09-26 02:36:12 +0000404
Douglas Gregore2e50d332010-12-01 01:36:18 +0000405 case TemplateArgument::Integral:
406 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
407 Arg2.getIntegralType()))
408 return false;
409
Eric Christopher6dcc3762012-07-15 00:23:57 +0000410 return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral());
Douglas Gregore2e50d332010-12-01 01:36:18 +0000411
412 case TemplateArgument::Declaration:
413 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +0000414
415 case TemplateArgument::NullPtr:
416 return true; // FIXME: Is this correct?
417
Douglas Gregore2e50d332010-12-01 01:36:18 +0000418 case TemplateArgument::Template:
419 return IsStructurallyEquivalent(Context,
420 Arg1.getAsTemplate(),
421 Arg2.getAsTemplate());
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000422
423 case TemplateArgument::TemplateExpansion:
424 return IsStructurallyEquivalent(Context,
425 Arg1.getAsTemplateOrTemplatePattern(),
426 Arg2.getAsTemplateOrTemplatePattern());
427
Douglas Gregore2e50d332010-12-01 01:36:18 +0000428 case TemplateArgument::Expression:
429 return IsStructurallyEquivalent(Context,
430 Arg1.getAsExpr(), Arg2.getAsExpr());
431
432 case TemplateArgument::Pack:
433 if (Arg1.pack_size() != Arg2.pack_size())
434 return false;
435
436 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
437 if (!IsStructurallyEquivalent(Context,
438 Arg1.pack_begin()[I],
439 Arg2.pack_begin()[I]))
440 return false;
441
442 return true;
443 }
444
445 llvm_unreachable("Invalid template argument kind");
Douglas Gregor3996e242010-02-15 22:01:00 +0000446}
447
448/// \brief Determine structural equivalence for the common part of array
449/// types.
450static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
451 const ArrayType *Array1,
452 const ArrayType *Array2) {
453 if (!IsStructurallyEquivalent(Context,
454 Array1->getElementType(),
455 Array2->getElementType()))
456 return false;
457 if (Array1->getSizeModifier() != Array2->getSizeModifier())
458 return false;
459 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
460 return false;
461
462 return true;
463}
464
465/// \brief Determine structural equivalence of two types.
466static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
467 QualType T1, QualType T2) {
468 if (T1.isNull() || T2.isNull())
469 return T1.isNull() && T2.isNull();
470
471 if (!Context.StrictTypeSpelling) {
472 // We aren't being strict about token-to-token equivalence of types,
473 // so map down to the canonical type.
474 T1 = Context.C1.getCanonicalType(T1);
475 T2 = Context.C2.getCanonicalType(T2);
476 }
477
478 if (T1.getQualifiers() != T2.getQualifiers())
479 return false;
480
Douglas Gregorb4964f72010-02-15 23:54:17 +0000481 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000482
Douglas Gregorb4964f72010-02-15 23:54:17 +0000483 if (T1->getTypeClass() != T2->getTypeClass()) {
484 // Compare function types with prototypes vs. without prototypes as if
485 // both did not have prototypes.
486 if (T1->getTypeClass() == Type::FunctionProto &&
487 T2->getTypeClass() == Type::FunctionNoProto)
488 TC = Type::FunctionNoProto;
489 else if (T1->getTypeClass() == Type::FunctionNoProto &&
490 T2->getTypeClass() == Type::FunctionProto)
491 TC = Type::FunctionNoProto;
492 else
493 return false;
494 }
495
496 switch (TC) {
497 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000498 // FIXME: Deal with Char_S/Char_U.
499 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
500 return false;
501 break;
502
503 case Type::Complex:
504 if (!IsStructurallyEquivalent(Context,
505 cast<ComplexType>(T1)->getElementType(),
506 cast<ComplexType>(T2)->getElementType()))
507 return false;
508 break;
509
Reid Kleckner0503a872013-12-05 01:23:43 +0000510 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +0000511 case Type::Decayed:
512 if (!IsStructurallyEquivalent(Context,
Reid Kleckner0503a872013-12-05 01:23:43 +0000513 cast<AdjustedType>(T1)->getOriginalType(),
514 cast<AdjustedType>(T2)->getOriginalType()))
Reid Kleckner8a365022013-06-24 17:51:48 +0000515 return false;
516 break;
517
Douglas Gregor3996e242010-02-15 22:01:00 +0000518 case Type::Pointer:
519 if (!IsStructurallyEquivalent(Context,
520 cast<PointerType>(T1)->getPointeeType(),
521 cast<PointerType>(T2)->getPointeeType()))
522 return false;
523 break;
524
525 case Type::BlockPointer:
526 if (!IsStructurallyEquivalent(Context,
527 cast<BlockPointerType>(T1)->getPointeeType(),
528 cast<BlockPointerType>(T2)->getPointeeType()))
529 return false;
530 break;
531
532 case Type::LValueReference:
533 case Type::RValueReference: {
534 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
535 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
536 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
537 return false;
538 if (Ref1->isInnerRef() != Ref2->isInnerRef())
539 return false;
540 if (!IsStructurallyEquivalent(Context,
541 Ref1->getPointeeTypeAsWritten(),
542 Ref2->getPointeeTypeAsWritten()))
543 return false;
544 break;
545 }
546
547 case Type::MemberPointer: {
548 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
549 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
550 if (!IsStructurallyEquivalent(Context,
551 MemPtr1->getPointeeType(),
552 MemPtr2->getPointeeType()))
553 return false;
554 if (!IsStructurallyEquivalent(Context,
555 QualType(MemPtr1->getClass(), 0),
556 QualType(MemPtr2->getClass(), 0)))
557 return false;
558 break;
559 }
560
561 case Type::ConstantArray: {
562 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
563 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
Eric Christopher6dcc3762012-07-15 00:23:57 +0000564 if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000565 return false;
566
567 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
568 return false;
569 break;
570 }
571
572 case Type::IncompleteArray:
573 if (!IsArrayStructurallyEquivalent(Context,
574 cast<ArrayType>(T1),
575 cast<ArrayType>(T2)))
576 return false;
577 break;
578
579 case Type::VariableArray: {
580 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
581 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
582 if (!IsStructurallyEquivalent(Context,
583 Array1->getSizeExpr(), Array2->getSizeExpr()))
584 return false;
585
586 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
587 return false;
588
589 break;
590 }
591
592 case Type::DependentSizedArray: {
593 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
594 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
595 if (!IsStructurallyEquivalent(Context,
596 Array1->getSizeExpr(), Array2->getSizeExpr()))
597 return false;
598
599 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
600 return false;
601
602 break;
603 }
604
605 case Type::DependentSizedExtVector: {
606 const DependentSizedExtVectorType *Vec1
607 = cast<DependentSizedExtVectorType>(T1);
608 const DependentSizedExtVectorType *Vec2
609 = cast<DependentSizedExtVectorType>(T2);
610 if (!IsStructurallyEquivalent(Context,
611 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
612 return false;
613 if (!IsStructurallyEquivalent(Context,
614 Vec1->getElementType(),
615 Vec2->getElementType()))
616 return false;
617 break;
618 }
619
620 case Type::Vector:
621 case Type::ExtVector: {
622 const VectorType *Vec1 = cast<VectorType>(T1);
623 const VectorType *Vec2 = cast<VectorType>(T2);
624 if (!IsStructurallyEquivalent(Context,
625 Vec1->getElementType(),
626 Vec2->getElementType()))
627 return false;
628 if (Vec1->getNumElements() != Vec2->getNumElements())
629 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000630 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000631 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000632 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000633 }
634
635 case Type::FunctionProto: {
636 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
637 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
Alp Toker9cacbab2014-01-20 20:26:09 +0000638 if (Proto1->getNumParams() != Proto2->getNumParams())
Douglas Gregor3996e242010-02-15 22:01:00 +0000639 return false;
Alp Toker9cacbab2014-01-20 20:26:09 +0000640 for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) {
641 if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I),
642 Proto2->getParamType(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000643 return false;
644 }
645 if (Proto1->isVariadic() != Proto2->isVariadic())
646 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000647 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor3996e242010-02-15 22:01:00 +0000648 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000649 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
650 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
651 return false;
652 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
653 if (!IsStructurallyEquivalent(Context,
654 Proto1->getExceptionType(I),
655 Proto2->getExceptionType(I)))
656 return false;
657 }
658 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000659 if (!IsStructurallyEquivalent(Context,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000660 Proto1->getNoexceptExpr(),
661 Proto2->getNoexceptExpr()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000662 return false;
663 }
664 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
665 return false;
666
667 // Fall through to check the bits common with FunctionNoProtoType.
668 }
669
670 case Type::FunctionNoProto: {
671 const FunctionType *Function1 = cast<FunctionType>(T1);
672 const FunctionType *Function2 = cast<FunctionType>(T2);
Alp Toker314cc812014-01-25 16:55:45 +0000673 if (!IsStructurallyEquivalent(Context, Function1->getReturnType(),
674 Function2->getReturnType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000675 return false;
Justin Bogner62c04de2016-03-20 16:58:03 +0000676 if (Function1->getExtInfo() != Function2->getExtInfo())
677 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000678 break;
679 }
680
681 case Type::UnresolvedUsing:
682 if (!IsStructurallyEquivalent(Context,
683 cast<UnresolvedUsingType>(T1)->getDecl(),
684 cast<UnresolvedUsingType>(T2)->getDecl()))
685 return false;
686
687 break;
John McCall81904512011-01-06 01:58:22 +0000688
689 case Type::Attributed:
690 if (!IsStructurallyEquivalent(Context,
691 cast<AttributedType>(T1)->getModifiedType(),
692 cast<AttributedType>(T2)->getModifiedType()))
693 return false;
694 if (!IsStructurallyEquivalent(Context,
695 cast<AttributedType>(T1)->getEquivalentType(),
696 cast<AttributedType>(T2)->getEquivalentType()))
697 return false;
698 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000699
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000700 case Type::Paren:
701 if (!IsStructurallyEquivalent(Context,
702 cast<ParenType>(T1)->getInnerType(),
703 cast<ParenType>(T2)->getInnerType()))
704 return false;
705 break;
706
Douglas Gregor3996e242010-02-15 22:01:00 +0000707 case Type::Typedef:
708 if (!IsStructurallyEquivalent(Context,
709 cast<TypedefType>(T1)->getDecl(),
710 cast<TypedefType>(T2)->getDecl()))
711 return false;
712 break;
713
714 case Type::TypeOfExpr:
715 if (!IsStructurallyEquivalent(Context,
716 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
717 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
718 return false;
719 break;
720
721 case Type::TypeOf:
722 if (!IsStructurallyEquivalent(Context,
723 cast<TypeOfType>(T1)->getUnderlyingType(),
724 cast<TypeOfType>(T2)->getUnderlyingType()))
725 return false;
726 break;
Alexis Hunte852b102011-05-24 22:41:36 +0000727
728 case Type::UnaryTransform:
729 if (!IsStructurallyEquivalent(Context,
730 cast<UnaryTransformType>(T1)->getUnderlyingType(),
731 cast<UnaryTransformType>(T1)->getUnderlyingType()))
732 return false;
733 break;
734
Douglas Gregor3996e242010-02-15 22:01:00 +0000735 case Type::Decltype:
736 if (!IsStructurallyEquivalent(Context,
737 cast<DecltypeType>(T1)->getUnderlyingExpr(),
738 cast<DecltypeType>(T2)->getUnderlyingExpr()))
739 return false;
740 break;
741
Richard Smith30482bc2011-02-20 03:19:35 +0000742 case Type::Auto:
743 if (!IsStructurallyEquivalent(Context,
744 cast<AutoType>(T1)->getDeducedType(),
745 cast<AutoType>(T2)->getDeducedType()))
746 return false;
747 break;
748
Douglas Gregor3996e242010-02-15 22:01:00 +0000749 case Type::Record:
750 case Type::Enum:
751 if (!IsStructurallyEquivalent(Context,
752 cast<TagType>(T1)->getDecl(),
753 cast<TagType>(T2)->getDecl()))
754 return false;
755 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000756
Douglas Gregor3996e242010-02-15 22:01:00 +0000757 case Type::TemplateTypeParm: {
758 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
759 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
760 if (Parm1->getDepth() != Parm2->getDepth())
761 return false;
762 if (Parm1->getIndex() != Parm2->getIndex())
763 return false;
764 if (Parm1->isParameterPack() != Parm2->isParameterPack())
765 return false;
766
767 // Names of template type parameters are never significant.
768 break;
769 }
770
771 case Type::SubstTemplateTypeParm: {
772 const SubstTemplateTypeParmType *Subst1
773 = cast<SubstTemplateTypeParmType>(T1);
774 const SubstTemplateTypeParmType *Subst2
775 = cast<SubstTemplateTypeParmType>(T2);
776 if (!IsStructurallyEquivalent(Context,
777 QualType(Subst1->getReplacedParameter(), 0),
778 QualType(Subst2->getReplacedParameter(), 0)))
779 return false;
780 if (!IsStructurallyEquivalent(Context,
781 Subst1->getReplacementType(),
782 Subst2->getReplacementType()))
783 return false;
784 break;
785 }
786
Douglas Gregorfb322d82011-01-14 05:11:40 +0000787 case Type::SubstTemplateTypeParmPack: {
788 const SubstTemplateTypeParmPackType *Subst1
789 = cast<SubstTemplateTypeParmPackType>(T1);
790 const SubstTemplateTypeParmPackType *Subst2
791 = cast<SubstTemplateTypeParmPackType>(T2);
792 if (!IsStructurallyEquivalent(Context,
793 QualType(Subst1->getReplacedParameter(), 0),
794 QualType(Subst2->getReplacedParameter(), 0)))
795 return false;
796 if (!IsStructurallyEquivalent(Context,
797 Subst1->getArgumentPack(),
798 Subst2->getArgumentPack()))
799 return false;
800 break;
801 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000802 case Type::TemplateSpecialization: {
803 const TemplateSpecializationType *Spec1
804 = cast<TemplateSpecializationType>(T1);
805 const TemplateSpecializationType *Spec2
806 = cast<TemplateSpecializationType>(T2);
807 if (!IsStructurallyEquivalent(Context,
808 Spec1->getTemplateName(),
809 Spec2->getTemplateName()))
810 return false;
811 if (Spec1->getNumArgs() != Spec2->getNumArgs())
812 return false;
813 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
814 if (!IsStructurallyEquivalent(Context,
815 Spec1->getArg(I), Spec2->getArg(I)))
816 return false;
817 }
818 break;
819 }
820
Abramo Bagnara6150c882010-05-11 21:36:43 +0000821 case Type::Elaborated: {
822 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
823 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
824 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
825 if (Elab1->getKeyword() != Elab2->getKeyword())
826 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000827 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000828 Elab1->getQualifier(),
829 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000830 return false;
831 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000832 Elab1->getNamedType(),
833 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000834 return false;
835 break;
836 }
837
John McCalle78aac42010-03-10 03:28:59 +0000838 case Type::InjectedClassName: {
839 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
840 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
841 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000842 Inj1->getInjectedSpecializationType(),
843 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000844 return false;
845 break;
846 }
847
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000848 case Type::DependentName: {
849 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
850 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000851 if (!IsStructurallyEquivalent(Context,
852 Typename1->getQualifier(),
853 Typename2->getQualifier()))
854 return false;
855 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
856 Typename2->getIdentifier()))
857 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000858
859 break;
860 }
861
John McCallc392f372010-06-11 00:33:02 +0000862 case Type::DependentTemplateSpecialization: {
863 const DependentTemplateSpecializationType *Spec1 =
864 cast<DependentTemplateSpecializationType>(T1);
865 const DependentTemplateSpecializationType *Spec2 =
866 cast<DependentTemplateSpecializationType>(T2);
867 if (!IsStructurallyEquivalent(Context,
868 Spec1->getQualifier(),
869 Spec2->getQualifier()))
870 return false;
871 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
872 Spec2->getIdentifier()))
873 return false;
874 if (Spec1->getNumArgs() != Spec2->getNumArgs())
875 return false;
876 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
877 if (!IsStructurallyEquivalent(Context,
878 Spec1->getArg(I), Spec2->getArg(I)))
879 return false;
880 }
881 break;
882 }
Douglas Gregord2fa7662010-12-20 02:24:11 +0000883
884 case Type::PackExpansion:
885 if (!IsStructurallyEquivalent(Context,
886 cast<PackExpansionType>(T1)->getPattern(),
887 cast<PackExpansionType>(T2)->getPattern()))
888 return false;
889 break;
890
Douglas Gregor3996e242010-02-15 22:01:00 +0000891 case Type::ObjCInterface: {
892 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
893 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
894 if (!IsStructurallyEquivalent(Context,
895 Iface1->getDecl(), Iface2->getDecl()))
896 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000897 break;
898 }
899
900 case Type::ObjCObject: {
901 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
902 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
903 if (!IsStructurallyEquivalent(Context,
904 Obj1->getBaseType(),
905 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000906 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000907 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
908 return false;
909 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000910 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000911 Obj1->getProtocol(I),
912 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000913 return false;
914 }
915 break;
916 }
917
918 case Type::ObjCObjectPointer: {
919 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
920 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
921 if (!IsStructurallyEquivalent(Context,
922 Ptr1->getPointeeType(),
923 Ptr2->getPointeeType()))
924 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000925 break;
926 }
Eli Friedman0dfb8892011-10-06 23:00:33 +0000927
928 case Type::Atomic: {
929 if (!IsStructurallyEquivalent(Context,
930 cast<AtomicType>(T1)->getValueType(),
931 cast<AtomicType>(T2)->getValueType()))
932 return false;
933 break;
934 }
935
Xiuli Pan9c14e282016-01-09 12:53:17 +0000936 case Type::Pipe: {
937 if (!IsStructurallyEquivalent(Context,
938 cast<PipeType>(T1)->getElementType(),
939 cast<PipeType>(T2)->getElementType()))
940 return false;
941 break;
942 }
943
Douglas Gregor3996e242010-02-15 22:01:00 +0000944 } // end switch
945
946 return true;
947}
948
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000949/// \brief Determine structural equivalence of two fields.
950static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
951 FieldDecl *Field1, FieldDecl *Field2) {
952 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000953
954 // For anonymous structs/unions, match up the anonymous struct/union type
955 // declarations directly, so that we don't go off searching for anonymous
956 // types
957 if (Field1->isAnonymousStructOrUnion() &&
958 Field2->isAnonymousStructOrUnion()) {
959 RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl();
960 RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl();
961 return IsStructurallyEquivalent(Context, D1, D2);
962 }
Sean Callanan969c5bd2013-04-26 22:49:25 +0000963
964 // Check for equivalent field names.
965 IdentifierInfo *Name1 = Field1->getIdentifier();
966 IdentifierInfo *Name2 = Field2->getIdentifier();
967 if (!::IsStructurallyEquivalent(Name1, Name2))
968 return false;
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000969
970 if (!IsStructurallyEquivalent(Context,
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000971 Field1->getType(), Field2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000972 if (Context.Complain) {
973 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
974 << Context.C2.getTypeDeclType(Owner2);
975 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
976 << Field2->getDeclName() << Field2->getType();
977 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
978 << Field1->getDeclName() << Field1->getType();
979 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000980 return false;
981 }
982
983 if (Field1->isBitField() != Field2->isBitField()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000984 if (Context.Complain) {
985 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
986 << Context.C2.getTypeDeclType(Owner2);
987 if (Field1->isBitField()) {
988 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
989 << Field1->getDeclName() << Field1->getType()
990 << Field1->getBitWidthValue(Context.C1);
991 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
992 << Field2->getDeclName();
993 } else {
994 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
995 << Field2->getDeclName() << Field2->getType()
996 << Field2->getBitWidthValue(Context.C2);
997 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
998 << Field1->getDeclName();
999 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001000 }
1001 return false;
1002 }
1003
1004 if (Field1->isBitField()) {
1005 // Make sure that the bit-fields are the same length.
1006 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
1007 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
1008
1009 if (Bits1 != Bits2) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001010 if (Context.Complain) {
1011 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1012 << Context.C2.getTypeDeclType(Owner2);
1013 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
1014 << Field2->getDeclName() << Field2->getType() << Bits2;
1015 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
1016 << Field1->getDeclName() << Field1->getType() << Bits1;
1017 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001018 return false;
1019 }
1020 }
1021
1022 return true;
1023}
1024
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001025/// \brief Find the index of the given anonymous struct/union within its
1026/// context.
1027///
1028/// \returns Returns the index of this anonymous struct/union in its context,
1029/// including the next assigned index (if none of them match). Returns an
1030/// empty option if the context is not a record, i.e.. if the anonymous
1031/// struct/union is at namespace or block scope.
Sean Callanan488f8612016-07-14 19:53:44 +00001032static Optional<unsigned> findUntaggedStructOrUnionIndex(RecordDecl *Anon) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001033 ASTContext &Context = Anon->getASTContext();
1034 QualType AnonTy = Context.getRecordType(Anon);
1035
1036 RecordDecl *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext());
1037 if (!Owner)
David Blaikie7a30dc52013-02-21 01:47:18 +00001038 return None;
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001039
1040 unsigned Index = 0;
Aaron Ballman629afae2014-03-07 19:56:05 +00001041 for (const auto *D : Owner->noload_decls()) {
1042 const auto *F = dyn_cast<FieldDecl>(D);
Sean Callanan488f8612016-07-14 19:53:44 +00001043 if (!F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001044 continue;
1045
Sean Callanan488f8612016-07-14 19:53:44 +00001046 if (F->isAnonymousStructOrUnion()) {
1047 if (Context.hasSameType(F->getType(), AnonTy))
1048 break;
1049 ++Index;
1050 continue;
1051 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001052
Sean Callanan488f8612016-07-14 19:53:44 +00001053 // If the field looks like this:
1054 // struct { ... } A;
1055 QualType FieldType = F->getType();
1056 if (const auto *RecType = dyn_cast<RecordType>(FieldType)) {
1057 const RecordDecl *RecDecl = RecType->getDecl();
1058 if (RecDecl->getDeclContext() == Owner &&
1059 !RecDecl->getIdentifier()) {
1060 if (Context.hasSameType(FieldType, AnonTy))
1061 break;
1062 ++Index;
1063 continue;
1064 }
1065 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001066 }
1067
1068 return Index;
1069}
1070
Douglas Gregor3996e242010-02-15 22:01:00 +00001071/// \brief Determine structural equivalence of two records.
1072static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1073 RecordDecl *D1, RecordDecl *D2) {
1074 if (D1->isUnion() != D2->isUnion()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001075 if (Context.Complain) {
1076 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1077 << Context.C2.getTypeDeclType(D2);
1078 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
1079 << D1->getDeclName() << (unsigned)D1->getTagKind();
1080 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001081 return false;
1082 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001083
1084 if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) {
1085 // If both anonymous structs/unions are in a record context, make sure
1086 // they occur in the same location in the context records.
Sean Callanan488f8612016-07-14 19:53:44 +00001087 if (Optional<unsigned> Index1 = findUntaggedStructOrUnionIndex(D1)) {
1088 if (Optional<unsigned> Index2 = findUntaggedStructOrUnionIndex(D2)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001089 if (*Index1 != *Index2)
1090 return false;
1091 }
1092 }
1093 }
1094
Douglas Gregore2e50d332010-12-01 01:36:18 +00001095 // If both declarations are class template specializations, we know
1096 // the ODR applies, so check the template and template arguments.
1097 ClassTemplateSpecializationDecl *Spec1
1098 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
1099 ClassTemplateSpecializationDecl *Spec2
1100 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
1101 if (Spec1 && Spec2) {
1102 // Check that the specialized templates are the same.
1103 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
1104 Spec2->getSpecializedTemplate()))
1105 return false;
1106
1107 // Check that the template arguments are the same.
1108 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
1109 return false;
1110
1111 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
1112 if (!IsStructurallyEquivalent(Context,
1113 Spec1->getTemplateArgs().get(I),
1114 Spec2->getTemplateArgs().get(I)))
1115 return false;
1116 }
1117 // If one is a class template specialization and the other is not, these
Chris Lattner57540c52011-04-15 05:22:18 +00001118 // structures are different.
Douglas Gregore2e50d332010-12-01 01:36:18 +00001119 else if (Spec1 || Spec2)
1120 return false;
1121
Douglas Gregorb4964f72010-02-15 23:54:17 +00001122 // Compare the definitions of these two records. If either or both are
1123 // incomplete, we assume that they are equivalent.
1124 D1 = D1->getDefinition();
1125 D2 = D2->getDefinition();
1126 if (!D1 || !D2)
1127 return true;
1128
Douglas Gregor3996e242010-02-15 22:01:00 +00001129 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
1130 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
1131 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001132 if (Context.Complain) {
1133 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1134 << Context.C2.getTypeDeclType(D2);
1135 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
1136 << D2CXX->getNumBases();
1137 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
1138 << D1CXX->getNumBases();
1139 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001140 return false;
1141 }
1142
1143 // Check the base classes.
1144 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
1145 BaseEnd1 = D1CXX->bases_end(),
1146 Base2 = D2CXX->bases_begin();
1147 Base1 != BaseEnd1;
1148 ++Base1, ++Base2) {
1149 if (!IsStructurallyEquivalent(Context,
1150 Base1->getType(), Base2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001151 if (Context.Complain) {
1152 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1153 << Context.C2.getTypeDeclType(D2);
1154 Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
1155 << Base2->getType()
1156 << Base2->getSourceRange();
1157 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1158 << Base1->getType()
1159 << Base1->getSourceRange();
1160 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001161 return false;
1162 }
1163
1164 // Check virtual vs. non-virtual inheritance mismatch.
1165 if (Base1->isVirtual() != Base2->isVirtual()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001166 if (Context.Complain) {
1167 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1168 << Context.C2.getTypeDeclType(D2);
1169 Context.Diag2(Base2->getLocStart(),
1170 diag::note_odr_virtual_base)
1171 << Base2->isVirtual() << Base2->getSourceRange();
1172 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1173 << Base1->isVirtual()
1174 << Base1->getSourceRange();
1175 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001176 return false;
1177 }
1178 }
1179 } else if (D1CXX->getNumBases() > 0) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001180 if (Context.Complain) {
1181 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1182 << Context.C2.getTypeDeclType(D2);
1183 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
1184 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1185 << Base1->getType()
1186 << Base1->getSourceRange();
1187 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
1188 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001189 return false;
1190 }
1191 }
1192
1193 // Check the fields for consistency.
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001194 RecordDecl::field_iterator Field2 = D2->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001195 Field2End = D2->field_end();
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001196 for (RecordDecl::field_iterator Field1 = D1->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001197 Field1End = D1->field_end();
1198 Field1 != Field1End;
1199 ++Field1, ++Field2) {
1200 if (Field2 == Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001201 if (Context.Complain) {
1202 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1203 << Context.C2.getTypeDeclType(D2);
1204 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
1205 << Field1->getDeclName() << Field1->getType();
1206 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
1207 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001208 return false;
1209 }
1210
David Blaikie40ed2972012-06-06 20:45:41 +00001211 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001212 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001213 }
1214
1215 if (Field2 != Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001216 if (Context.Complain) {
1217 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1218 << Context.C2.getTypeDeclType(D2);
1219 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1220 << Field2->getDeclName() << Field2->getType();
1221 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1222 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001223 return false;
1224 }
1225
1226 return true;
1227}
1228
1229/// \brief Determine structural equivalence of two enums.
1230static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1231 EnumDecl *D1, EnumDecl *D2) {
1232 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1233 EC2End = D2->enumerator_end();
1234 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1235 EC1End = D1->enumerator_end();
1236 EC1 != EC1End; ++EC1, ++EC2) {
1237 if (EC2 == EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001238 if (Context.Complain) {
1239 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1240 << Context.C2.getTypeDeclType(D2);
1241 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1242 << EC1->getDeclName()
1243 << EC1->getInitVal().toString(10);
1244 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1245 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001246 return false;
1247 }
1248
1249 llvm::APSInt Val1 = EC1->getInitVal();
1250 llvm::APSInt Val2 = EC2->getInitVal();
Eric Christopher6dcc3762012-07-15 00:23:57 +00001251 if (!llvm::APSInt::isSameValue(Val1, Val2) ||
Douglas Gregor3996e242010-02-15 22:01:00 +00001252 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001253 if (Context.Complain) {
1254 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1255 << Context.C2.getTypeDeclType(D2);
1256 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1257 << EC2->getDeclName()
1258 << EC2->getInitVal().toString(10);
1259 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1260 << EC1->getDeclName()
1261 << EC1->getInitVal().toString(10);
1262 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001263 return false;
1264 }
1265 }
1266
1267 if (EC2 != EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001268 if (Context.Complain) {
1269 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1270 << Context.C2.getTypeDeclType(D2);
1271 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1272 << EC2->getDeclName()
1273 << EC2->getInitVal().toString(10);
1274 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1275 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001276 return false;
1277 }
1278
1279 return true;
1280}
Douglas Gregora082a492010-11-30 19:14:50 +00001281
1282static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1283 TemplateParameterList *Params1,
1284 TemplateParameterList *Params2) {
1285 if (Params1->size() != Params2->size()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001286 if (Context.Complain) {
1287 Context.Diag2(Params2->getTemplateLoc(),
1288 diag::err_odr_different_num_template_parameters)
1289 << Params1->size() << Params2->size();
1290 Context.Diag1(Params1->getTemplateLoc(),
1291 diag::note_odr_template_parameter_list);
1292 }
Douglas Gregora082a492010-11-30 19:14:50 +00001293 return false;
1294 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001295
Douglas Gregora082a492010-11-30 19:14:50 +00001296 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1297 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001298 if (Context.Complain) {
1299 Context.Diag2(Params2->getParam(I)->getLocation(),
1300 diag::err_odr_different_template_parameter_kind);
1301 Context.Diag1(Params1->getParam(I)->getLocation(),
1302 diag::note_odr_template_parameter_here);
1303 }
Douglas Gregora082a492010-11-30 19:14:50 +00001304 return false;
1305 }
1306
1307 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1308 Params2->getParam(I))) {
1309
1310 return false;
1311 }
1312 }
1313
1314 return true;
1315}
1316
1317static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1318 TemplateTypeParmDecl *D1,
1319 TemplateTypeParmDecl *D2) {
1320 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001321 if (Context.Complain) {
1322 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1323 << D2->isParameterPack();
1324 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1325 << D1->isParameterPack();
1326 }
Douglas Gregora082a492010-11-30 19:14:50 +00001327 return false;
1328 }
1329
1330 return true;
1331}
1332
1333static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1334 NonTypeTemplateParmDecl *D1,
1335 NonTypeTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001336 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001337 if (Context.Complain) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001338 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1339 << D2->isParameterPack();
1340 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1341 << D1->isParameterPack();
1342 }
Douglas Gregora082a492010-11-30 19:14:50 +00001343 return false;
1344 }
Douglas Gregora082a492010-11-30 19:14:50 +00001345
1346 // Check types.
1347 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001348 if (Context.Complain) {
1349 Context.Diag2(D2->getLocation(),
1350 diag::err_odr_non_type_parameter_type_inconsistent)
1351 << D2->getType() << D1->getType();
1352 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1353 << D1->getType();
1354 }
Douglas Gregora082a492010-11-30 19:14:50 +00001355 return false;
1356 }
1357
1358 return true;
1359}
1360
1361static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1362 TemplateTemplateParmDecl *D1,
1363 TemplateTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001364 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001365 if (Context.Complain) {
1366 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1367 << D2->isParameterPack();
1368 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1369 << D1->isParameterPack();
1370 }
Douglas Gregora082a492010-11-30 19:14:50 +00001371 return false;
1372 }
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001373
Douglas Gregora082a492010-11-30 19:14:50 +00001374 // Check template parameter lists.
1375 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1376 D2->getTemplateParameters());
1377}
1378
1379static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1380 ClassTemplateDecl *D1,
1381 ClassTemplateDecl *D2) {
1382 // Check template parameters.
1383 if (!IsStructurallyEquivalent(Context,
1384 D1->getTemplateParameters(),
1385 D2->getTemplateParameters()))
1386 return false;
1387
1388 // Check the templated declaration.
1389 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1390 D2->getTemplatedDecl());
1391}
1392
Douglas Gregor3996e242010-02-15 22:01:00 +00001393/// \brief Determine structural equivalence of two declarations.
1394static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1395 Decl *D1, Decl *D2) {
1396 // FIXME: Check for known structural equivalences via a callback of some sort.
1397
Douglas Gregorb4964f72010-02-15 23:54:17 +00001398 // Check whether we already know that these two declarations are not
1399 // structurally equivalent.
1400 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1401 D2->getCanonicalDecl())))
1402 return false;
1403
Douglas Gregor3996e242010-02-15 22:01:00 +00001404 // Determine whether we've already produced a tentative equivalence for D1.
1405 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1406 if (EquivToD1)
1407 return EquivToD1 == D2->getCanonicalDecl();
1408
1409 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1410 EquivToD1 = D2->getCanonicalDecl();
1411 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1412 return true;
1413}
1414
1415bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1416 Decl *D2) {
1417 if (!::IsStructurallyEquivalent(*this, D1, D2))
1418 return false;
1419
1420 return !Finish();
1421}
1422
1423bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1424 QualType T2) {
1425 if (!::IsStructurallyEquivalent(*this, T1, T2))
1426 return false;
1427
1428 return !Finish();
1429}
1430
1431bool StructuralEquivalenceContext::Finish() {
1432 while (!DeclsToCheck.empty()) {
1433 // Check the next declaration.
1434 Decl *D1 = DeclsToCheck.front();
1435 DeclsToCheck.pop_front();
1436
1437 Decl *D2 = TentativeEquivalences[D1];
1438 assert(D2 && "Unrecorded tentative equivalence?");
1439
Douglas Gregorb4964f72010-02-15 23:54:17 +00001440 bool Equivalent = true;
1441
Douglas Gregor3996e242010-02-15 22:01:00 +00001442 // FIXME: Switch on all declaration kinds. For now, we're just going to
1443 // check the obvious ones.
1444 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1445 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1446 // Check for equivalent structure names.
1447 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001448 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1449 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001450 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001451 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1452 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001453 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1454 !::IsStructurallyEquivalent(*this, Record1, Record2))
1455 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001456 } else {
1457 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001458 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001459 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001460 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001461 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1462 // Check for equivalent enum names.
1463 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001464 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1465 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001466 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001467 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1468 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001469 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1470 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1471 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001472 } else {
1473 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001474 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001475 }
Richard Smithdda56e42011-04-15 14:24:37 +00001476 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1477 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001478 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001479 Typedef2->getIdentifier()) ||
1480 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001481 Typedef1->getUnderlyingType(),
1482 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001483 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001484 } else {
1485 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001486 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001487 }
Douglas Gregora082a492010-11-30 19:14:50 +00001488 } else if (ClassTemplateDecl *ClassTemplate1
1489 = dyn_cast<ClassTemplateDecl>(D1)) {
1490 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1491 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1492 ClassTemplate2->getIdentifier()) ||
1493 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1494 Equivalent = false;
1495 } else {
1496 // Class template/non-class-template mismatch.
1497 Equivalent = false;
1498 }
1499 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1500 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1501 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1502 Equivalent = false;
1503 } else {
1504 // Kind mismatch.
1505 Equivalent = false;
1506 }
1507 } else if (NonTypeTemplateParmDecl *NTTP1
1508 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1509 if (NonTypeTemplateParmDecl *NTTP2
1510 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1511 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1512 Equivalent = false;
1513 } else {
1514 // Kind mismatch.
1515 Equivalent = false;
1516 }
1517 } else if (TemplateTemplateParmDecl *TTP1
1518 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1519 if (TemplateTemplateParmDecl *TTP2
1520 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1521 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1522 Equivalent = false;
1523 } else {
1524 // Kind mismatch.
1525 Equivalent = false;
1526 }
1527 }
1528
Douglas Gregorb4964f72010-02-15 23:54:17 +00001529 if (!Equivalent) {
1530 // Note that these two declarations are not equivalent (and we already
1531 // know about it).
1532 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1533 D2->getCanonicalDecl()));
1534 return true;
1535 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001536 // FIXME: Check other declaration kinds!
1537 }
1538
1539 return false;
1540}
1541
1542//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001543// Import Types
1544//----------------------------------------------------------------------------
1545
John McCall424cec92011-01-19 06:33:43 +00001546QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +00001547 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1548 << T->getTypeClassName();
1549 return QualType();
1550}
1551
John McCall424cec92011-01-19 06:33:43 +00001552QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001553 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +00001554#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
1555 case BuiltinType::Id: \
1556 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +00001557#include "clang/Basic/OpenCLImageTypes.def"
John McCalle314e272011-10-18 21:02:43 +00001558#define SHARED_SINGLETON_TYPE(Expansion)
1559#define BUILTIN_TYPE(Id, SingletonId) \
1560 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1561#include "clang/AST/BuiltinTypes.def"
1562
1563 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1564 // context supports C++.
1565
1566 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1567 // context supports ObjC.
1568
Douglas Gregor96e578d2010-02-05 17:54:41 +00001569 case BuiltinType::Char_U:
1570 // The context we're importing from has an unsigned 'char'. If we're
1571 // importing into a context with a signed 'char', translate to
1572 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001573 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001574 return Importer.getToContext().UnsignedCharTy;
1575
1576 return Importer.getToContext().CharTy;
1577
Douglas Gregor96e578d2010-02-05 17:54:41 +00001578 case BuiltinType::Char_S:
1579 // The context we're importing from has an unsigned 'char'. If we're
1580 // importing into a context with a signed 'char', translate to
1581 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001582 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001583 return Importer.getToContext().SignedCharTy;
1584
1585 return Importer.getToContext().CharTy;
1586
Chris Lattnerad3467e2010-12-25 23:25:43 +00001587 case BuiltinType::WChar_S:
1588 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001589 // FIXME: If not in C++, shall we translate to the C equivalent of
1590 // wchar_t?
1591 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001592 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001593
1594 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001595}
1596
John McCall424cec92011-01-19 06:33:43 +00001597QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001598 QualType ToElementType = Importer.Import(T->getElementType());
1599 if (ToElementType.isNull())
1600 return QualType();
1601
1602 return Importer.getToContext().getComplexType(ToElementType);
1603}
1604
John McCall424cec92011-01-19 06:33:43 +00001605QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001606 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1607 if (ToPointeeType.isNull())
1608 return QualType();
1609
1610 return Importer.getToContext().getPointerType(ToPointeeType);
1611}
1612
John McCall424cec92011-01-19 06:33:43 +00001613QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001614 // FIXME: Check for blocks support in "to" context.
1615 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1616 if (ToPointeeType.isNull())
1617 return QualType();
1618
1619 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1620}
1621
John McCall424cec92011-01-19 06:33:43 +00001622QualType
1623ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001624 // FIXME: Check for C++ support in "to" context.
1625 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1626 if (ToPointeeType.isNull())
1627 return QualType();
1628
1629 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1630}
1631
John McCall424cec92011-01-19 06:33:43 +00001632QualType
1633ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001634 // FIXME: Check for C++0x support in "to" context.
1635 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1636 if (ToPointeeType.isNull())
1637 return QualType();
1638
1639 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1640}
1641
John McCall424cec92011-01-19 06:33:43 +00001642QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001643 // FIXME: Check for C++ support in "to" context.
1644 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1645 if (ToPointeeType.isNull())
1646 return QualType();
1647
1648 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1649 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1650 ClassType.getTypePtr());
1651}
1652
John McCall424cec92011-01-19 06:33:43 +00001653QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001654 QualType ToElementType = Importer.Import(T->getElementType());
1655 if (ToElementType.isNull())
1656 return QualType();
1657
1658 return Importer.getToContext().getConstantArrayType(ToElementType,
1659 T->getSize(),
1660 T->getSizeModifier(),
1661 T->getIndexTypeCVRQualifiers());
1662}
1663
John McCall424cec92011-01-19 06:33:43 +00001664QualType
1665ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001666 QualType ToElementType = Importer.Import(T->getElementType());
1667 if (ToElementType.isNull())
1668 return QualType();
1669
1670 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1671 T->getSizeModifier(),
1672 T->getIndexTypeCVRQualifiers());
1673}
1674
John McCall424cec92011-01-19 06:33:43 +00001675QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001676 QualType ToElementType = Importer.Import(T->getElementType());
1677 if (ToElementType.isNull())
1678 return QualType();
1679
1680 Expr *Size = Importer.Import(T->getSizeExpr());
1681 if (!Size)
1682 return QualType();
1683
1684 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1685 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1686 T->getSizeModifier(),
1687 T->getIndexTypeCVRQualifiers(),
1688 Brackets);
1689}
1690
John McCall424cec92011-01-19 06:33:43 +00001691QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001692 QualType ToElementType = Importer.Import(T->getElementType());
1693 if (ToElementType.isNull())
1694 return QualType();
1695
1696 return Importer.getToContext().getVectorType(ToElementType,
1697 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001698 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001699}
1700
John McCall424cec92011-01-19 06:33:43 +00001701QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001702 QualType ToElementType = Importer.Import(T->getElementType());
1703 if (ToElementType.isNull())
1704 return QualType();
1705
1706 return Importer.getToContext().getExtVectorType(ToElementType,
1707 T->getNumElements());
1708}
1709
John McCall424cec92011-01-19 06:33:43 +00001710QualType
1711ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001712 // FIXME: What happens if we're importing a function without a prototype
1713 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +00001714 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001715 if (ToResultType.isNull())
1716 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001717
Douglas Gregor96e578d2010-02-05 17:54:41 +00001718 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001719 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001720}
1721
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001722QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +00001723 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001724 if (ToResultType.isNull())
1725 return QualType();
1726
1727 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001728 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00001729 for (const auto &A : T->param_types()) {
1730 QualType ArgType = Importer.Import(A);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001731 if (ArgType.isNull())
1732 return QualType();
1733 ArgTypes.push_back(ArgType);
1734 }
1735
1736 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001737 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +00001738 for (const auto &E : T->exceptions()) {
1739 QualType ExceptionType = Importer.Import(E);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001740 if (ExceptionType.isNull())
1741 return QualType();
1742 ExceptionTypes.push_back(ExceptionType);
1743 }
John McCalldb40c7f2010-12-14 08:05:40 +00001744
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001745 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1746 FunctionProtoType::ExtProtoInfo ToEPI;
1747
1748 ToEPI.ExtInfo = FromEPI.ExtInfo;
1749 ToEPI.Variadic = FromEPI.Variadic;
1750 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1751 ToEPI.TypeQuals = FromEPI.TypeQuals;
1752 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +00001753 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1754 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
1755 ToEPI.ExceptionSpec.NoexceptExpr =
1756 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
1757 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
1758 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
1759 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
1760 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001761
Jordan Rose5c382722013-03-08 21:51:21 +00001762 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001763}
1764
Sean Callananda6df8a2011-08-11 16:56:07 +00001765QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1766 QualType ToInnerType = Importer.Import(T->getInnerType());
1767 if (ToInnerType.isNull())
1768 return QualType();
1769
1770 return Importer.getToContext().getParenType(ToInnerType);
1771}
1772
John McCall424cec92011-01-19 06:33:43 +00001773QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +00001774 TypedefNameDecl *ToDecl
1775 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001776 if (!ToDecl)
1777 return QualType();
1778
1779 return Importer.getToContext().getTypeDeclType(ToDecl);
1780}
1781
John McCall424cec92011-01-19 06:33:43 +00001782QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001783 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1784 if (!ToExpr)
1785 return QualType();
1786
1787 return Importer.getToContext().getTypeOfExprType(ToExpr);
1788}
1789
John McCall424cec92011-01-19 06:33:43 +00001790QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001791 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1792 if (ToUnderlyingType.isNull())
1793 return QualType();
1794
1795 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1796}
1797
John McCall424cec92011-01-19 06:33:43 +00001798QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001799 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +00001800 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1801 if (!ToExpr)
1802 return QualType();
1803
Douglas Gregor81495f32012-02-12 18:42:33 +00001804 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1805 if (UnderlyingType.isNull())
1806 return QualType();
1807
1808 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001809}
1810
Alexis Hunte852b102011-05-24 22:41:36 +00001811QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1812 QualType ToBaseType = Importer.Import(T->getBaseType());
1813 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1814 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1815 return QualType();
1816
1817 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1818 ToUnderlyingType,
1819 T->getUTTKind());
1820}
1821
Richard Smith30482bc2011-02-20 03:19:35 +00001822QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00001823 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +00001824 QualType FromDeduced = T->getDeducedType();
1825 QualType ToDeduced;
1826 if (!FromDeduced.isNull()) {
1827 ToDeduced = Importer.Import(FromDeduced);
1828 if (ToDeduced.isNull())
1829 return QualType();
1830 }
1831
Richard Smithe301ba22015-11-11 02:02:15 +00001832 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +00001833 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +00001834}
1835
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001836QualType ASTNodeImporter::VisitInjectedClassNameType(
1837 const InjectedClassNameType *T) {
1838 CXXRecordDecl *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl()));
1839 if (!D)
1840 return QualType();
1841
1842 QualType InjType = Importer.Import(T->getInjectedSpecializationType());
1843 if (InjType.isNull())
1844 return QualType();
1845
1846 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
1847 // See comments in InjectedClassNameType definition for details
1848 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
1849 enum {
1850 TypeAlignmentInBits = 4,
1851 TypeAlignment = 1 << TypeAlignmentInBits
1852 };
1853
1854 return QualType(new (Importer.getToContext(), TypeAlignment)
1855 InjectedClassNameType(D, InjType), 0);
1856}
1857
John McCall424cec92011-01-19 06:33:43 +00001858QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001859 RecordDecl *ToDecl
1860 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1861 if (!ToDecl)
1862 return QualType();
1863
1864 return Importer.getToContext().getTagDeclType(ToDecl);
1865}
1866
John McCall424cec92011-01-19 06:33:43 +00001867QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001868 EnumDecl *ToDecl
1869 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1870 if (!ToDecl)
1871 return QualType();
1872
1873 return Importer.getToContext().getTagDeclType(ToDecl);
1874}
1875
Sean Callanan72fe0852015-04-02 23:50:08 +00001876QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
1877 QualType FromModifiedType = T->getModifiedType();
1878 QualType FromEquivalentType = T->getEquivalentType();
1879 QualType ToModifiedType;
1880 QualType ToEquivalentType;
1881
1882 if (!FromModifiedType.isNull()) {
1883 ToModifiedType = Importer.Import(FromModifiedType);
1884 if (ToModifiedType.isNull())
1885 return QualType();
1886 }
1887 if (!FromEquivalentType.isNull()) {
1888 ToEquivalentType = Importer.Import(FromEquivalentType);
1889 if (ToEquivalentType.isNull())
1890 return QualType();
1891 }
1892
1893 return Importer.getToContext().getAttributedType(T->getAttrKind(),
1894 ToModifiedType, ToEquivalentType);
1895}
1896
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001897
1898QualType ASTNodeImporter::VisitTemplateTypeParmType(
1899 const TemplateTypeParmType *T) {
1900 TemplateTypeParmDecl *ParmDecl =
1901 cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl()));
1902 if (!ParmDecl && T->getDecl())
1903 return QualType();
1904
1905 return Importer.getToContext().getTemplateTypeParmType(
1906 T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl);
1907}
1908
Douglas Gregore2e50d332010-12-01 01:36:18 +00001909QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001910 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001911 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1912 if (ToTemplate.isNull())
1913 return QualType();
1914
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001915 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001916 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1917 return QualType();
1918
1919 QualType ToCanonType;
1920 if (!QualType(T, 0).isCanonical()) {
1921 QualType FromCanonType
1922 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1923 ToCanonType =Importer.Import(FromCanonType);
1924 if (ToCanonType.isNull())
1925 return QualType();
1926 }
1927 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
David Majnemer6fbeee32016-07-07 04:43:07 +00001928 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +00001929 ToCanonType);
1930}
1931
John McCall424cec92011-01-19 06:33:43 +00001932QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Craig Topper36250ad2014-05-12 05:36:57 +00001933 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001934 // Note: the qualifier in an ElaboratedType is optional.
1935 if (T->getQualifier()) {
1936 ToQualifier = Importer.Import(T->getQualifier());
1937 if (!ToQualifier)
1938 return QualType();
1939 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001940
1941 QualType ToNamedType = Importer.Import(T->getNamedType());
1942 if (ToNamedType.isNull())
1943 return QualType();
1944
Abramo Bagnara6150c882010-05-11 21:36:43 +00001945 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1946 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001947}
1948
John McCall424cec92011-01-19 06:33:43 +00001949QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001950 ObjCInterfaceDecl *Class
1951 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1952 if (!Class)
1953 return QualType();
1954
John McCall8b07ec22010-05-15 11:32:37 +00001955 return Importer.getToContext().getObjCInterfaceType(Class);
1956}
1957
John McCall424cec92011-01-19 06:33:43 +00001958QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001959 QualType ToBaseType = Importer.Import(T->getBaseType());
1960 if (ToBaseType.isNull())
1961 return QualType();
1962
Douglas Gregore9d95f12015-07-07 03:57:35 +00001963 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00001964 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00001965 QualType ImportedTypeArg = Importer.Import(TypeArg);
1966 if (ImportedTypeArg.isNull())
1967 return QualType();
1968
1969 TypeArgs.push_back(ImportedTypeArg);
1970 }
1971
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001972 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001973 for (auto *P : T->quals()) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001974 ObjCProtocolDecl *Protocol
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001975 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001976 if (!Protocol)
1977 return QualType();
1978 Protocols.push_back(Protocol);
1979 }
1980
Douglas Gregore9d95f12015-07-07 03:57:35 +00001981 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001982 Protocols,
1983 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001984}
1985
John McCall424cec92011-01-19 06:33:43 +00001986QualType
1987ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001988 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1989 if (ToPointeeType.isNull())
1990 return QualType();
1991
John McCall8b07ec22010-05-15 11:32:37 +00001992 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001993}
1994
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001995//----------------------------------------------------------------------------
1996// Import Declarations
1997//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001998bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1999 DeclContext *&LexicalDC,
2000 DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +00002001 NamedDecl *&ToD,
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002002 SourceLocation &Loc) {
2003 // Import the context of this declaration.
2004 DC = Importer.ImportContext(D->getDeclContext());
2005 if (!DC)
2006 return true;
2007
2008 LexicalDC = DC;
2009 if (D->getDeclContext() != D->getLexicalDeclContext()) {
2010 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
2011 if (!LexicalDC)
2012 return true;
2013 }
2014
2015 // Import the name of this declaration.
2016 Name = Importer.Import(D->getDeclName());
2017 if (D->getDeclName() && !Name)
2018 return true;
2019
2020 // Import the location of this declaration.
2021 Loc = Importer.Import(D->getLocation());
Sean Callanan59721b32015-04-28 18:41:46 +00002022 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002023 return false;
2024}
2025
Douglas Gregord451ea92011-07-29 23:31:30 +00002026void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
2027 if (!FromD)
2028 return;
2029
2030 if (!ToD) {
2031 ToD = Importer.Import(FromD);
2032 if (!ToD)
2033 return;
2034 }
2035
2036 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
2037 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +00002038 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +00002039 ImportDefinition(FromRecord, ToRecord);
2040 }
2041 }
2042 return;
2043 }
2044
2045 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
2046 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
2047 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
2048 ImportDefinition(FromEnum, ToEnum);
2049 }
2050 }
2051 return;
2052 }
2053}
2054
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002055void
2056ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
2057 DeclarationNameInfo& To) {
2058 // NOTE: To.Name and To.Loc are already imported.
2059 // We only have to import To.LocInfo.
2060 switch (To.getName().getNameKind()) {
2061 case DeclarationName::Identifier:
2062 case DeclarationName::ObjCZeroArgSelector:
2063 case DeclarationName::ObjCOneArgSelector:
2064 case DeclarationName::ObjCMultiArgSelector:
2065 case DeclarationName::CXXUsingDirective:
2066 return;
2067
2068 case DeclarationName::CXXOperatorName: {
2069 SourceRange Range = From.getCXXOperatorNameRange();
2070 To.setCXXOperatorNameRange(Importer.Import(Range));
2071 return;
2072 }
2073 case DeclarationName::CXXLiteralOperatorName: {
2074 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
2075 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
2076 return;
2077 }
2078 case DeclarationName::CXXConstructorName:
2079 case DeclarationName::CXXDestructorName:
2080 case DeclarationName::CXXConversionFunctionName: {
2081 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
2082 To.setNamedTypeInfo(Importer.Import(FromTInfo));
2083 return;
2084 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002085 }
Douglas Gregor07216d12011-11-02 20:52:01 +00002086 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002087}
2088
Douglas Gregor2e15c842012-02-01 21:00:38 +00002089void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00002090 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00002091 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00002092 return;
2093 }
2094
Aaron Ballman629afae2014-03-07 19:56:05 +00002095 for (auto *From : FromDC->decls())
2096 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +00002097}
2098
Douglas Gregord451ea92011-07-29 23:31:30 +00002099bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00002100 ImportDefinitionKind Kind) {
2101 if (To->getDefinition() || To->isBeingDefined()) {
2102 if (Kind == IDK_Everything)
2103 ImportDeclContext(From, /*ForceImport=*/true);
2104
Douglas Gregore2e50d332010-12-01 01:36:18 +00002105 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00002106 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00002107
2108 To->startDefinition();
2109
2110 // Add base classes.
2111 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
2112 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002113
2114 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
2115 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
2116 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00002117 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002118 ToData.Aggregate = FromData.Aggregate;
2119 ToData.PlainOldData = FromData.PlainOldData;
2120 ToData.Empty = FromData.Empty;
2121 ToData.Polymorphic = FromData.Polymorphic;
2122 ToData.Abstract = FromData.Abstract;
2123 ToData.IsStandardLayout = FromData.IsStandardLayout;
2124 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
2125 ToData.HasPrivateFields = FromData.HasPrivateFields;
2126 ToData.HasProtectedFields = FromData.HasProtectedFields;
2127 ToData.HasPublicFields = FromData.HasPublicFields;
2128 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00002129 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00002130 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00002131 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00002132 ToData.HasUninitializedReferenceMember
2133 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00002134 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00002135 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
2136 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +00002137 ToData.NeedOverloadResolutionForMoveConstructor
2138 = FromData.NeedOverloadResolutionForMoveConstructor;
2139 ToData.NeedOverloadResolutionForMoveAssignment
2140 = FromData.NeedOverloadResolutionForMoveAssignment;
2141 ToData.NeedOverloadResolutionForDestructor
2142 = FromData.NeedOverloadResolutionForDestructor;
2143 ToData.DefaultedMoveConstructorIsDeleted
2144 = FromData.DefaultedMoveConstructorIsDeleted;
2145 ToData.DefaultedMoveAssignmentIsDeleted
2146 = FromData.DefaultedMoveAssignmentIsDeleted;
2147 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00002148 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
2149 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002150 ToData.HasConstexprNonCopyMoveConstructor
2151 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00002152 ToData.HasDefaultedDefaultConstructor
2153 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00002154 ToData.DefaultedDefaultConstructorIsConstexpr
2155 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00002156 ToData.HasConstexprDefaultConstructor
2157 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002158 ToData.HasNonLiteralTypeFieldsOrBases
2159 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00002160 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002161 ToData.UserProvidedDefaultConstructor
2162 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00002163 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smith1c33fe82012-11-28 06:23:12 +00002164 ToData.ImplicitCopyConstructorHasConstParam
2165 = FromData.ImplicitCopyConstructorHasConstParam;
2166 ToData.ImplicitCopyAssignmentHasConstParam
2167 = FromData.ImplicitCopyAssignmentHasConstParam;
2168 ToData.HasDeclaredCopyConstructorWithConstParam
2169 = FromData.HasDeclaredCopyConstructorWithConstParam;
2170 ToData.HasDeclaredCopyAssignmentWithConstParam
2171 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00002172 ToData.IsLambda = FromData.IsLambda;
2173
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002174 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00002175 for (const auto &Base1 : FromCXX->bases()) {
2176 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +00002177 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00002178 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00002179
2180 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00002181 if (Base1.isPackExpansion())
2182 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00002183
2184 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00002185 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Douglas Gregord451ea92011-07-29 23:31:30 +00002186
Douglas Gregore2e50d332010-12-01 01:36:18 +00002187 Bases.push_back(
2188 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00002189 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
2190 Base1.isVirtual(),
2191 Base1.isBaseOfClass(),
2192 Base1.getAccessSpecifierAsWritten(),
2193 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00002194 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00002195 }
2196 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00002197 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00002198 }
2199
Douglas Gregor2e15c842012-02-01 21:00:38 +00002200 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00002201 ImportDeclContext(From, /*ForceImport=*/true);
2202
Douglas Gregore2e50d332010-12-01 01:36:18 +00002203 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00002204 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002205}
2206
Larisse Voufo39a1e502013-08-06 01:03:05 +00002207bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
2208 ImportDefinitionKind Kind) {
Sean Callanan59721b32015-04-28 18:41:46 +00002209 if (To->getAnyInitializer())
Larisse Voufo39a1e502013-08-06 01:03:05 +00002210 return false;
2211
2212 // FIXME: Can we really import any initializer? Alternatively, we could force
2213 // ourselves to import every declaration of a variable and then only use
2214 // getInit() here.
2215 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
2216
2217 // FIXME: Other bits to merge?
2218
2219 return false;
2220}
2221
Douglas Gregord451ea92011-07-29 23:31:30 +00002222bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00002223 ImportDefinitionKind Kind) {
2224 if (To->getDefinition() || To->isBeingDefined()) {
2225 if (Kind == IDK_Everything)
2226 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002227 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002228 }
Douglas Gregord451ea92011-07-29 23:31:30 +00002229
2230 To->startDefinition();
2231
2232 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
2233 if (T.isNull())
2234 return true;
2235
2236 QualType ToPromotionType = Importer.Import(From->getPromotionType());
2237 if (ToPromotionType.isNull())
2238 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002239
2240 if (shouldForceImportDeclContext(Kind))
2241 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002242
2243 // FIXME: we might need to merge the number of positive or negative bits
2244 // if the enumerator lists don't match.
2245 To->completeDefinition(T, ToPromotionType,
2246 From->getNumPositiveBits(),
2247 From->getNumNegativeBits());
2248 return false;
2249}
2250
Douglas Gregora082a492010-11-30 19:14:50 +00002251TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
2252 TemplateParameterList *Params) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002253 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregora082a492010-11-30 19:14:50 +00002254 ToParams.reserve(Params->size());
2255 for (TemplateParameterList::iterator P = Params->begin(),
2256 PEnd = Params->end();
2257 P != PEnd; ++P) {
2258 Decl *To = Importer.Import(*P);
2259 if (!To)
Craig Topper36250ad2014-05-12 05:36:57 +00002260 return nullptr;
2261
Douglas Gregora082a492010-11-30 19:14:50 +00002262 ToParams.push_back(cast<NamedDecl>(To));
2263 }
2264
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00002265 Expr *ToRequiresClause;
2266 if (Expr *const R = Params->getRequiresClause()) {
2267 ToRequiresClause = Importer.Import(R);
2268 if (!ToRequiresClause)
2269 return nullptr;
2270 } else {
2271 ToRequiresClause = nullptr;
2272 }
2273
Douglas Gregora082a492010-11-30 19:14:50 +00002274 return TemplateParameterList::Create(Importer.getToContext(),
2275 Importer.Import(Params->getTemplateLoc()),
2276 Importer.Import(Params->getLAngleLoc()),
David Majnemer902f8c62015-12-27 07:16:27 +00002277 ToParams,
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00002278 Importer.Import(Params->getRAngleLoc()),
2279 ToRequiresClause);
Douglas Gregora082a492010-11-30 19:14:50 +00002280}
2281
Douglas Gregore2e50d332010-12-01 01:36:18 +00002282TemplateArgument
2283ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
2284 switch (From.getKind()) {
2285 case TemplateArgument::Null:
2286 return TemplateArgument();
2287
2288 case TemplateArgument::Type: {
2289 QualType ToType = Importer.Import(From.getAsType());
2290 if (ToType.isNull())
2291 return TemplateArgument();
2292 return TemplateArgument(ToType);
2293 }
2294
2295 case TemplateArgument::Integral: {
2296 QualType ToType = Importer.Import(From.getIntegralType());
2297 if (ToType.isNull())
2298 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00002299 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00002300 }
2301
Eli Friedmanb826a002012-09-26 02:36:12 +00002302 case TemplateArgument::Declaration: {
David Blaikie3c7dd6b2014-10-22 19:54:16 +00002303 ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
2304 QualType ToType = Importer.Import(From.getParamTypeForDecl());
2305 if (!To || ToType.isNull())
2306 return TemplateArgument();
2307 return TemplateArgument(To, ToType);
Eli Friedmanb826a002012-09-26 02:36:12 +00002308 }
2309
2310 case TemplateArgument::NullPtr: {
2311 QualType ToType = Importer.Import(From.getNullPtrType());
2312 if (ToType.isNull())
2313 return TemplateArgument();
2314 return TemplateArgument(ToType, /*isNullPtr*/true);
2315 }
2316
Douglas Gregore2e50d332010-12-01 01:36:18 +00002317 case TemplateArgument::Template: {
2318 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
2319 if (ToTemplate.isNull())
2320 return TemplateArgument();
2321
2322 return TemplateArgument(ToTemplate);
2323 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002324
2325 case TemplateArgument::TemplateExpansion: {
2326 TemplateName ToTemplate
2327 = Importer.Import(From.getAsTemplateOrTemplatePattern());
2328 if (ToTemplate.isNull())
2329 return TemplateArgument();
2330
Douglas Gregore1d60df2011-01-14 23:41:42 +00002331 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002332 }
2333
Douglas Gregore2e50d332010-12-01 01:36:18 +00002334 case TemplateArgument::Expression:
2335 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2336 return TemplateArgument(ToExpr);
2337 return TemplateArgument();
2338
2339 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002340 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002341 ToPack.reserve(From.pack_size());
2342 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2343 return TemplateArgument();
Benjamin Kramercce63472015-08-05 09:40:22 +00002344
2345 return TemplateArgument(
2346 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00002347 }
2348 }
2349
2350 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00002351}
2352
2353bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2354 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002355 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00002356 for (unsigned I = 0; I != NumFromArgs; ++I) {
2357 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2358 if (To.isNull() && !FromArgs[I].isNull())
2359 return true;
2360
2361 ToArgs.push_back(To);
2362 }
2363
2364 return false;
2365}
2366
Douglas Gregor5c73e912010-02-11 00:48:18 +00002367bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002368 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00002369 // Eliminate a potential failure point where we attempt to re-import
2370 // something we're trying to import while completing ToRecord.
2371 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
2372 if (ToOrigin) {
2373 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
2374 if (ToOriginRecord)
2375 ToRecord = ToOriginRecord;
2376 }
2377
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002378 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00002379 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002380 Importer.getNonEquivalentDecls(),
2381 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002382 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002383}
2384
Larisse Voufo39a1e502013-08-06 01:03:05 +00002385bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
2386 bool Complain) {
2387 StructuralEquivalenceContext Ctx(
2388 Importer.getFromContext(), Importer.getToContext(),
2389 Importer.getNonEquivalentDecls(), false, Complain);
2390 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
2391}
2392
Douglas Gregor98c10182010-02-12 22:17:39 +00002393bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002394 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002395 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002396 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002397 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002398}
2399
Douglas Gregor91155082012-11-14 22:29:20 +00002400bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
2401 EnumConstantDecl *ToEC)
2402{
2403 const llvm::APSInt &FromVal = FromEC->getInitVal();
2404 const llvm::APSInt &ToVal = ToEC->getInitVal();
2405
2406 return FromVal.isSigned() == ToVal.isSigned() &&
2407 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2408 FromVal == ToVal;
2409}
2410
2411bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002412 ClassTemplateDecl *To) {
2413 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2414 Importer.getToContext(),
2415 Importer.getNonEquivalentDecls());
2416 return Ctx.IsStructurallyEquivalent(From, To);
2417}
2418
Larisse Voufo39a1e502013-08-06 01:03:05 +00002419bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2420 VarTemplateDecl *To) {
2421 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2422 Importer.getToContext(),
2423 Importer.getNonEquivalentDecls());
2424 return Ctx.IsStructurallyEquivalent(From, To);
2425}
2426
Douglas Gregore4c83e42010-02-09 22:48:33 +00002427Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002428 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002429 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00002430 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00002431}
2432
Sean Callanan65198272011-11-17 23:20:56 +00002433Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2434 TranslationUnitDecl *ToD =
2435 Importer.getToContext().getTranslationUnitDecl();
2436
2437 Importer.Imported(D, ToD);
2438
2439 return ToD;
2440}
2441
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002442Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
2443
2444 SourceLocation Loc = Importer.Import(D->getLocation());
2445 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
2446
2447 // Import the context of this declaration.
2448 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2449 if (!DC)
2450 return nullptr;
2451
2452 AccessSpecDecl *accessSpecDecl
2453 = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(),
2454 DC, Loc, ColonLoc);
2455
2456 if (!accessSpecDecl)
2457 return nullptr;
2458
2459 // Lexical DeclContext and Semantic DeclContext
2460 // is always the same for the accessSpec.
2461 accessSpecDecl->setLexicalDeclContext(DC);
2462 DC->addDeclInternal(accessSpecDecl);
2463
2464 return accessSpecDecl;
2465}
2466
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002467Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2468 // Import the major distinguishing characteristics of this namespace.
2469 DeclContext *DC, *LexicalDC;
2470 DeclarationName Name;
2471 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002472 NamedDecl *ToD;
2473 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002474 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002475 if (ToD)
2476 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002477
2478 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002479 if (!Name) {
2480 // This is an anonymous namespace. Adopt an existing anonymous
2481 // namespace if we can.
2482 // FIXME: Not testable.
2483 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2484 MergeWithNamespace = TU->getAnonymousNamespace();
2485 else
2486 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2487 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002488 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002489 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002490 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002491 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2492 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002493 continue;
2494
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002495 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002496 MergeWithNamespace = FoundNS;
2497 ConflictingDecls.clear();
2498 break;
2499 }
2500
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002501 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002502 }
2503
2504 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002505 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002506 ConflictingDecls.data(),
2507 ConflictingDecls.size());
2508 }
2509 }
2510
2511 // Create the "to" namespace, if needed.
2512 NamespaceDecl *ToNamespace = MergeWithNamespace;
2513 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002514 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002515 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002516 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00002517 Loc, Name.getAsIdentifierInfo(),
Craig Topper36250ad2014-05-12 05:36:57 +00002518 /*PrevDecl=*/nullptr);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002519 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002520 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002521
2522 // If this is an anonymous namespace, register it as the anonymous
2523 // namespace within its context.
2524 if (!Name) {
2525 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2526 TU->setAnonymousNamespace(ToNamespace);
2527 else
2528 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2529 }
2530 }
2531 Importer.Imported(D, ToNamespace);
2532
2533 ImportDeclContext(D);
2534
2535 return ToNamespace;
2536}
2537
Richard Smithdda56e42011-04-15 14:24:37 +00002538Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002539 // Import the major distinguishing characteristics of this typedef.
2540 DeclContext *DC, *LexicalDC;
2541 DeclarationName Name;
2542 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002543 NamedDecl *ToD;
2544 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002545 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002546 if (ToD)
2547 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002548
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002549 // If this typedef is not in block scope, determine whether we've
2550 // seen a typedef with the same name (that we can merge with) or any
2551 // other entity by that name (which name lookup could conflict with).
2552 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002553 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002554 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002555 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002556 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002557 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2558 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002559 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00002560 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002561 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002562 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2563 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002564 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002565 }
2566
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002567 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002568 }
2569
2570 if (!ConflictingDecls.empty()) {
2571 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2572 ConflictingDecls.data(),
2573 ConflictingDecls.size());
2574 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002575 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002576 }
2577 }
2578
Douglas Gregorb4964f72010-02-15 23:54:17 +00002579 // Import the underlying type of this typedef;
2580 QualType T = Importer.Import(D->getUnderlyingType());
2581 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002582 return nullptr;
2583
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002584 // Create the new typedef node.
2585 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002586 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00002587 TypedefNameDecl *ToTypedef;
2588 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002589 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2590 StartL, Loc,
2591 Name.getAsIdentifierInfo(),
2592 TInfo);
2593 else
Richard Smithdda56e42011-04-15 14:24:37 +00002594 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2595 StartL, Loc,
2596 Name.getAsIdentifierInfo(),
2597 TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002598
Douglas Gregordd483172010-02-22 17:42:47 +00002599 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002600 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002601 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00002602 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002603
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002604 return ToTypedef;
2605}
2606
Richard Smithdda56e42011-04-15 14:24:37 +00002607Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2608 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2609}
2610
2611Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2612 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2613}
2614
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002615Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
2616 // Import the major distinguishing characteristics of this label.
2617 DeclContext *DC, *LexicalDC;
2618 DeclarationName Name;
2619 SourceLocation Loc;
2620 NamedDecl *ToD;
2621 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2622 return nullptr;
2623 if (ToD)
2624 return ToD;
2625
2626 assert(LexicalDC->isFunctionOrMethod());
2627
2628 LabelDecl *ToLabel = D->isGnuLocal()
2629 ? LabelDecl::Create(Importer.getToContext(),
2630 DC, Importer.Import(D->getLocation()),
2631 Name.getAsIdentifierInfo(),
2632 Importer.Import(D->getLocStart()))
2633 : LabelDecl::Create(Importer.getToContext(),
2634 DC, Importer.Import(D->getLocation()),
2635 Name.getAsIdentifierInfo());
2636 Importer.Imported(D, ToLabel);
2637
2638 LabelStmt *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt()));
2639 if (!Label)
2640 return nullptr;
2641
2642 ToLabel->setStmt(Label);
2643 ToLabel->setLexicalDeclContext(LexicalDC);
2644 LexicalDC->addDeclInternal(ToLabel);
2645 return ToLabel;
2646}
2647
Douglas Gregor98c10182010-02-12 22:17:39 +00002648Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2649 // Import the major distinguishing characteristics of this enum.
2650 DeclContext *DC, *LexicalDC;
2651 DeclarationName Name;
2652 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002653 NamedDecl *ToD;
2654 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002655 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002656 if (ToD)
2657 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002658
Douglas Gregor98c10182010-02-12 22:17:39 +00002659 // Figure out what enum name we're looking for.
2660 unsigned IDNS = Decl::IDNS_Tag;
2661 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002662 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2663 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002664 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002665 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002666 IDNS |= Decl::IDNS_Ordinary;
2667
2668 // We may already have an enum of the same name; try to find and match it.
2669 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002670 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002671 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002672 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002673 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2674 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002675 continue;
2676
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002677 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002678 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002679 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2680 Found = Tag->getDecl();
2681 }
2682
2683 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002684 if (IsStructuralMatch(D, FoundEnum))
2685 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002686 }
2687
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002688 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002689 }
2690
2691 if (!ConflictingDecls.empty()) {
2692 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2693 ConflictingDecls.data(),
2694 ConflictingDecls.size());
2695 }
2696 }
2697
2698 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002699 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2700 Importer.Import(D->getLocStart()),
Craig Topper36250ad2014-05-12 05:36:57 +00002701 Loc, Name.getAsIdentifierInfo(), nullptr,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002702 D->isScoped(), D->isScopedUsingClassTag(),
2703 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002704 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002705 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002706 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002707 D2->setLexicalDeclContext(LexicalDC);
2708 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00002709 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002710
2711 // Import the integer type.
2712 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2713 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002714 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00002715 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002716
2717 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002718 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00002719 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002720
Douglas Gregor3996e242010-02-15 22:01:00 +00002721 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002722}
2723
Douglas Gregor5c73e912010-02-11 00:48:18 +00002724Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2725 // If this record has a definition in the translation unit we're coming from,
2726 // but this particular declaration is not that definition, import the
2727 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002728 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002729 if (Definition && Definition != D) {
2730 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002731 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00002732 return nullptr;
2733
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002734 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002735 }
2736
2737 // Import the major distinguishing characteristics of this record.
2738 DeclContext *DC, *LexicalDC;
2739 DeclarationName Name;
2740 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002741 NamedDecl *ToD;
2742 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002743 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002744 if (ToD)
2745 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002746
Douglas Gregor5c73e912010-02-11 00:48:18 +00002747 // Figure out what structure name we're looking for.
2748 unsigned IDNS = Decl::IDNS_Tag;
2749 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002750 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2751 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002752 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002753 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002754 IDNS |= Decl::IDNS_Ordinary;
2755
2756 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00002757 RecordDecl *AdoptDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002758 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002759 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002760 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002761 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002762 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2763 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002764 continue;
2765
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002766 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002767 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002768 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2769 Found = Tag->getDecl();
2770 }
2771
2772 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002773 if (D->isAnonymousStructOrUnion() &&
2774 FoundRecord->isAnonymousStructOrUnion()) {
2775 // If both anonymous structs/unions are in a record context, make sure
2776 // they occur in the same location in the context records.
David Blaikie05785d12013-02-20 22:23:23 +00002777 if (Optional<unsigned> Index1
Sean Callanan488f8612016-07-14 19:53:44 +00002778 = findUntaggedStructOrUnionIndex(D)) {
David Blaikie05785d12013-02-20 22:23:23 +00002779 if (Optional<unsigned> Index2 =
Sean Callanan488f8612016-07-14 19:53:44 +00002780 findUntaggedStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002781 if (*Index1 != *Index2)
2782 continue;
2783 }
2784 }
2785 }
2786
Douglas Gregor25791052010-02-12 00:09:27 +00002787 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002788 if ((SearchName && !D->isCompleteDefinition())
2789 || (D->isCompleteDefinition() &&
2790 D->isAnonymousStructOrUnion()
2791 == FoundDef->isAnonymousStructOrUnion() &&
2792 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002793 // The record types structurally match, or the "from" translation
2794 // unit only had a forward declaration anyway; call it the same
2795 // function.
2796 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002797 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002798 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002799 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002800 // We have a forward declaration of this type, so adopt that forward
2801 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00002802
2803 // If one or both can be completed from external storage then try one
2804 // last time to complete and compare them before doing this.
2805
2806 if (FoundRecord->hasExternalLexicalStorage() &&
2807 !FoundRecord->isCompleteDefinition())
2808 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2809 if (D->hasExternalLexicalStorage())
2810 D->getASTContext().getExternalSource()->CompleteType(D);
2811
2812 if (FoundRecord->isCompleteDefinition() &&
2813 D->isCompleteDefinition() &&
2814 !IsStructuralMatch(D, FoundRecord))
2815 continue;
2816
Douglas Gregor25791052010-02-12 00:09:27 +00002817 AdoptDecl = FoundRecord;
2818 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002819 } else if (!SearchName) {
2820 continue;
2821 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002822 }
2823
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002824 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002825 }
2826
Douglas Gregordd6006f2012-07-17 21:16:27 +00002827 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002828 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2829 ConflictingDecls.data(),
2830 ConflictingDecls.size());
2831 }
2832 }
2833
2834 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002835 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002836 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002837 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002838 CXXRecordDecl *D2CXX = nullptr;
2839 if (CXXRecordDecl *DCXX = llvm::dyn_cast<CXXRecordDecl>(D)) {
2840 if (DCXX->isLambda()) {
2841 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
2842 D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(),
2843 DC, TInfo, Loc,
2844 DCXX->isDependentLambda(),
2845 DCXX->isGenericLambda(),
2846 DCXX->getLambdaCaptureDefault());
2847 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
2848 if (DCXX->getLambdaContextDecl() && !CDecl)
2849 return nullptr;
Sean Callanan041cceb2016-05-14 05:43:57 +00002850 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl);
2851 } else if (DCXX->isInjectedClassName()) {
2852 // We have to be careful to do a similar dance to the one in
2853 // Sema::ActOnStartCXXMemberDeclarations
2854 CXXRecordDecl *const PrevDecl = nullptr;
2855 const bool DelayTypeCreation = true;
2856 D2CXX = CXXRecordDecl::Create(
2857 Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc,
2858 Name.getAsIdentifierInfo(), PrevDecl, DelayTypeCreation);
2859 Importer.getToContext().getTypeDeclType(
2860 D2CXX, llvm::dyn_cast<CXXRecordDecl>(DC));
Sean Callanan8bca9962016-03-28 21:43:01 +00002861 } else {
2862 D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
2863 D->getTagKind(),
2864 DC, StartLoc, Loc,
2865 Name.getAsIdentifierInfo());
2866 }
Douglas Gregor3996e242010-02-15 22:01:00 +00002867 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002868 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002869 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002870 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002871 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002872 }
Douglas Gregor14454802011-02-25 02:25:35 +00002873
2874 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002875 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002876 LexicalDC->addDeclInternal(D2);
Douglas Gregordd6006f2012-07-17 21:16:27 +00002877 if (D->isAnonymousStructOrUnion())
2878 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002879 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002880
Douglas Gregor3996e242010-02-15 22:01:00 +00002881 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002882
Douglas Gregor95d82832012-01-24 18:36:04 +00002883 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00002884 return nullptr;
2885
Douglas Gregor3996e242010-02-15 22:01:00 +00002886 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002887}
2888
Douglas Gregor98c10182010-02-12 22:17:39 +00002889Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2890 // Import the major distinguishing characteristics of this enumerator.
2891 DeclContext *DC, *LexicalDC;
2892 DeclarationName Name;
2893 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002894 NamedDecl *ToD;
2895 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002896 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002897 if (ToD)
2898 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002899
2900 QualType T = Importer.Import(D->getType());
2901 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002902 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002903
Douglas Gregor98c10182010-02-12 22:17:39 +00002904 // Determine whether there are any other declarations with the same name and
2905 // in the same context.
2906 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002907 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002908 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002909 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002910 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002911 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2912 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002913 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002914
2915 if (EnumConstantDecl *FoundEnumConstant
2916 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
2917 if (IsStructuralMatch(D, FoundEnumConstant))
2918 return Importer.Imported(D, FoundEnumConstant);
2919 }
2920
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002921 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002922 }
2923
2924 if (!ConflictingDecls.empty()) {
2925 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2926 ConflictingDecls.data(),
2927 ConflictingDecls.size());
2928 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002929 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002930 }
2931 }
2932
2933 Expr *Init = Importer.Import(D->getInitExpr());
2934 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00002935 return nullptr;
2936
Douglas Gregor98c10182010-02-12 22:17:39 +00002937 EnumConstantDecl *ToEnumerator
2938 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2939 Name.getAsIdentifierInfo(), T,
2940 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002941 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002942 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002943 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002944 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002945 return ToEnumerator;
2946}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002947
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002948Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2949 // Import the major distinguishing characteristics of this function.
2950 DeclContext *DC, *LexicalDC;
2951 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002952 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002953 NamedDecl *ToD;
2954 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002955 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002956 if (ToD)
2957 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002958
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002959 // Try to find a function in our own ("to") context with the same name, same
2960 // type, and in the same context as the function we're importing.
2961 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002962 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002963 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002964 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002965 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002966 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2967 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002968 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002969
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002970 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002971 if (FoundFunction->hasExternalFormalLinkage() &&
2972 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002973 if (Importer.IsStructurallyEquivalent(D->getType(),
2974 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002975 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002976 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002977 }
2978
2979 // FIXME: Check for overloading more carefully, e.g., by boosting
2980 // Sema::IsOverload out to the AST library.
2981
2982 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002983 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002984 continue;
2985
2986 // Complain about inconsistent function types.
2987 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002988 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002989 Importer.ToDiag(FoundFunction->getLocation(),
2990 diag::note_odr_value_here)
2991 << FoundFunction->getType();
2992 }
2993 }
2994
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002995 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002996 }
2997
2998 if (!ConflictingDecls.empty()) {
2999 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3000 ConflictingDecls.data(),
3001 ConflictingDecls.size());
3002 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003003 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003004 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003005 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00003006
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003007 DeclarationNameInfo NameInfo(Name, Loc);
3008 // Import additional name location/type info.
3009 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3010
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003011 QualType FromTy = D->getType();
3012 bool usedDifferentExceptionSpec = false;
3013
3014 if (const FunctionProtoType *
3015 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
3016 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
3017 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
3018 // FunctionDecl that we are importing the FunctionProtoType for.
3019 // To avoid an infinite recursion when importing, create the FunctionDecl
3020 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00003021 if (FromEPI.ExceptionSpec.SourceDecl ||
3022 FromEPI.ExceptionSpec.SourceTemplate ||
3023 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003024 FunctionProtoType::ExtProtoInfo DefaultEPI;
3025 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00003026 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003027 usedDifferentExceptionSpec = true;
3028 }
3029 }
3030
Douglas Gregorb4964f72010-02-15 23:54:17 +00003031 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003032 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00003033 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003034 return nullptr;
3035
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003036 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003037 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00003038 for (auto P : D->parameters()) {
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00003039 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003040 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003041 return nullptr;
3042
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003043 Parameters.push_back(ToP);
3044 }
3045
3046 // Create the imported function.
3047 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Craig Topper36250ad2014-05-12 05:36:57 +00003048 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003049 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Douglas Gregor00eace12010-02-21 18:29:16 +00003050 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
3051 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
3052 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00003053 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003054 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00003055 FromConstructor->isExplicit(),
3056 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00003057 D->isImplicit(),
3058 D->isConstexpr());
Sean Callanandd2c1742016-05-16 20:48:03 +00003059 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
3060 SmallVector<CXXCtorInitializer *, 4> CtorInitializers;
3061 for (CXXCtorInitializer *I : FromConstructor->inits()) {
3062 CXXCtorInitializer *ToI =
3063 cast_or_null<CXXCtorInitializer>(Importer.Import(I));
3064 if (!ToI && I)
3065 return nullptr;
3066 CtorInitializers.push_back(ToI);
3067 }
3068 CXXCtorInitializer **Memory =
3069 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
3070 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
3071 CXXConstructorDecl *ToCtor = llvm::cast<CXXConstructorDecl>(ToFunction);
3072 ToCtor->setCtorInitializers(Memory);
3073 ToCtor->setNumCtorInitializers(NumInitializers);
3074 }
Douglas Gregor00eace12010-02-21 18:29:16 +00003075 } else if (isa<CXXDestructorDecl>(D)) {
3076 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
3077 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00003078 InnerLocStart,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00003079 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00003080 D->isInlineSpecified(),
3081 D->isImplicit());
3082 } else if (CXXConversionDecl *FromConversion
3083 = dyn_cast<CXXConversionDecl>(D)) {
3084 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
3085 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00003086 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003087 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00003088 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00003089 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00003090 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00003091 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00003092 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
3093 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
3094 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00003095 InnerLocStart,
Douglas Gregora50ad132010-11-29 16:04:58 +00003096 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003097 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00003098 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00003099 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00003100 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00003101 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003102 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Sean Callanan59721b32015-04-28 18:41:46 +00003103 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003104 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00003105 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00003106 D->hasWrittenPrototype(),
3107 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00003108 }
John McCall3e11ebe2010-03-15 10:12:16 +00003109
3110 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00003111 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003112 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00003113 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00003114 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
3115 ToFunction->setTrivial(D->isTrivial());
3116 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00003117 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003118
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003119 // Set the parameters.
3120 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003121 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00003122 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003123 }
David Blaikie9c70e042011-09-21 18:16:56 +00003124 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003125
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003126 if (usedDifferentExceptionSpec) {
3127 // Update FunctionProtoType::ExtProtoInfo.
3128 QualType T = Importer.Import(D->getType());
3129 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003130 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003131 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00003132 }
3133
Sean Callanan59721b32015-04-28 18:41:46 +00003134 // Import the body, if any.
3135 if (Stmt *FromBody = D->getBody()) {
3136 if (Stmt *ToBody = Importer.Import(FromBody)) {
3137 ToFunction->setBody(ToBody);
3138 }
3139 }
3140
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003141 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003142
3143 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00003144 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003145
Douglas Gregor43f54792010-02-17 02:12:47 +00003146 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003147}
3148
Douglas Gregor00eace12010-02-21 18:29:16 +00003149Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
3150 return VisitFunctionDecl(D);
3151}
3152
3153Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
3154 return VisitCXXMethodDecl(D);
3155}
3156
3157Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
3158 return VisitCXXMethodDecl(D);
3159}
3160
3161Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
3162 return VisitCXXMethodDecl(D);
3163}
3164
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003165static unsigned getFieldIndex(Decl *F) {
3166 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
3167 if (!Owner)
3168 return 0;
3169
3170 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00003171 for (const auto *D : Owner->noload_decls()) {
3172 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003173 return Index;
3174
3175 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
3176 ++Index;
3177 }
3178
3179 return Index;
3180}
3181
Douglas Gregor5c73e912010-02-11 00:48:18 +00003182Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
3183 // Import the major distinguishing characteristics of a variable.
3184 DeclContext *DC, *LexicalDC;
3185 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00003186 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003187 NamedDecl *ToD;
3188 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003189 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003190 if (ToD)
3191 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003192
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003193 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003194 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003195 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003196 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3197 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003198 // For anonymous fields, match up by index.
3199 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
3200 continue;
3201
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003202 if (Importer.IsStructurallyEquivalent(D->getType(),
3203 FoundField->getType())) {
3204 Importer.Imported(D, FoundField);
3205 return FoundField;
3206 }
3207
3208 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3209 << Name << D->getType() << FoundField->getType();
3210 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3211 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003212 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003213 }
3214 }
3215
Douglas Gregorb4964f72010-02-15 23:54:17 +00003216 // Import the type.
3217 QualType T = Importer.Import(D->getType());
3218 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003219 return nullptr;
3220
Douglas Gregor5c73e912010-02-11 00:48:18 +00003221 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3222 Expr *BitWidth = Importer.Import(D->getBitWidth());
3223 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00003224 return nullptr;
3225
Abramo Bagnaradff19302011-03-08 08:55:46 +00003226 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
3227 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00003228 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00003229 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00003230 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00003231 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003232 ToField->setLexicalDeclContext(LexicalDC);
Sean Callanan3a83ea72016-03-03 02:22:05 +00003233 if (Expr *FromInitializer = D->getInClassInitializer()) {
Sean Callananbb33f582016-03-03 01:21:28 +00003234 Expr *ToInitializer = Importer.Import(FromInitializer);
3235 if (ToInitializer)
3236 ToField->setInClassInitializer(ToInitializer);
3237 else
3238 return nullptr;
3239 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003240 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003241 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00003242 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003243 return ToField;
3244}
3245
Francois Pichet783dd6e2010-11-21 06:08:52 +00003246Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
3247 // Import the major distinguishing characteristics of a variable.
3248 DeclContext *DC, *LexicalDC;
3249 DeclarationName Name;
3250 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003251 NamedDecl *ToD;
3252 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003253 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003254 if (ToD)
3255 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003256
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003257 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003258 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003259 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003260 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003261 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003262 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003263 // For anonymous indirect fields, match up by index.
3264 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
3265 continue;
3266
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003267 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003268 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003269 !Name.isEmpty())) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003270 Importer.Imported(D, FoundField);
3271 return FoundField;
3272 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003273
3274 // If there are more anonymous fields to check, continue.
3275 if (!Name && I < N-1)
3276 continue;
3277
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003278 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3279 << Name << D->getType() << FoundField->getType();
3280 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3281 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003282 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003283 }
3284 }
3285
Francois Pichet783dd6e2010-11-21 06:08:52 +00003286 // Import the type.
3287 QualType T = Importer.Import(D->getType());
3288 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003289 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003290
3291 NamedDecl **NamedChain =
3292 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
3293
3294 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00003295 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00003296 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003297 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00003298 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003299 NamedChain[i++] = cast<NamedDecl>(D);
3300 }
3301
3302 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
Aaron Ballman260995b2014-10-15 16:58:18 +00003303 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
David Majnemer59f77922016-06-24 04:05:48 +00003304 {NamedChain, D->getChainingSize()});
Aaron Ballman260995b2014-10-15 16:58:18 +00003305
3306 for (const auto *Attr : D->attrs())
3307 ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
3308
Francois Pichet783dd6e2010-11-21 06:08:52 +00003309 ToIndirectField->setAccess(D->getAccess());
3310 ToIndirectField->setLexicalDeclContext(LexicalDC);
3311 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00003312 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003313 return ToIndirectField;
3314}
3315
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003316Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
3317 // Import the major distinguishing characteristics of an ivar.
3318 DeclContext *DC, *LexicalDC;
3319 DeclarationName Name;
3320 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003321 NamedDecl *ToD;
3322 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003323 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003324 if (ToD)
3325 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003326
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003327 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003328 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003329 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003330 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3331 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003332 if (Importer.IsStructurallyEquivalent(D->getType(),
3333 FoundIvar->getType())) {
3334 Importer.Imported(D, FoundIvar);
3335 return FoundIvar;
3336 }
3337
3338 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
3339 << Name << D->getType() << FoundIvar->getType();
3340 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3341 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003342 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003343 }
3344 }
3345
3346 // Import the type.
3347 QualType T = Importer.Import(D->getType());
3348 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003349 return nullptr;
3350
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003351 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3352 Expr *BitWidth = Importer.Import(D->getBitWidth());
3353 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00003354 return nullptr;
3355
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00003356 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
3357 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003358 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003359 Loc, Name.getAsIdentifierInfo(),
3360 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003361 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003362 ToIvar->setLexicalDeclContext(LexicalDC);
3363 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003364 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003365 return ToIvar;
3366
3367}
3368
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003369Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
3370 // Import the major distinguishing characteristics of a variable.
3371 DeclContext *DC, *LexicalDC;
3372 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003373 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003374 NamedDecl *ToD;
3375 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003376 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003377 if (ToD)
3378 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003379
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003380 // Try to find a variable in our own ("to") context with the same name and
3381 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00003382 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00003383 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003384 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003385 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003386 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003387 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003388 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3389 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003390 continue;
3391
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003392 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003393 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00003394 if (FoundVar->hasExternalFormalLinkage() &&
3395 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003396 if (Importer.IsStructurallyEquivalent(D->getType(),
3397 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003398 MergeWithVar = FoundVar;
3399 break;
3400 }
3401
Douglas Gregor56521c52010-02-12 17:23:39 +00003402 const ArrayType *FoundArray
3403 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3404 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00003405 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00003406 if (FoundArray && TArray) {
3407 if (isa<IncompleteArrayType>(FoundArray) &&
3408 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003409 // Import the type.
3410 QualType T = Importer.Import(D->getType());
3411 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003412 return nullptr;
3413
Douglas Gregor56521c52010-02-12 17:23:39 +00003414 FoundVar->setType(T);
3415 MergeWithVar = FoundVar;
3416 break;
3417 } else if (isa<IncompleteArrayType>(TArray) &&
3418 isa<ConstantArrayType>(FoundArray)) {
3419 MergeWithVar = FoundVar;
3420 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003421 }
3422 }
3423
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003424 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003425 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003426 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3427 << FoundVar->getType();
3428 }
3429 }
3430
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003431 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003432 }
3433
3434 if (MergeWithVar) {
3435 // An equivalent variable with external linkage has been found. Link
3436 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003437 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003438
3439 if (VarDecl *DDef = D->getDefinition()) {
3440 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
3441 Importer.ToDiag(ExistingDef->getLocation(),
3442 diag::err_odr_variable_multiple_def)
3443 << Name;
3444 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
3445 } else {
3446 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00003447 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00003448 if (DDef->isInitKnownICE()) {
3449 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
3450 Eval->CheckedICE = true;
3451 Eval->IsICE = DDef->isInitICE();
3452 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003453 }
3454 }
3455
3456 return MergeWithVar;
3457 }
3458
3459 if (!ConflictingDecls.empty()) {
3460 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3461 ConflictingDecls.data(),
3462 ConflictingDecls.size());
3463 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003464 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003465 }
3466 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003467
Douglas Gregorb4964f72010-02-15 23:54:17 +00003468 // Import the type.
3469 QualType T = Importer.Import(D->getType());
3470 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003471 return nullptr;
3472
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003473 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003474 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00003475 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
3476 Importer.Import(D->getInnerLocStart()),
3477 Loc, Name.getAsIdentifierInfo(),
3478 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003479 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00003480 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003481 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003482 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003483 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003484 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003485
Sean Callanan59721b32015-04-28 18:41:46 +00003486 if (!D->isFileVarDecl() &&
3487 D->isUsed())
3488 ToVar->setIsUsed();
3489
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003490 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00003491 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00003492 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003493
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003494 return ToVar;
3495}
3496
Douglas Gregor8b228d72010-02-17 21:22:52 +00003497Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
3498 // Parameters are created in the translation unit's context, then moved
3499 // into the function declaration's context afterward.
3500 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3501
3502 // Import the name of this declaration.
3503 DeclarationName Name = Importer.Import(D->getDeclName());
3504 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003505 return nullptr;
3506
Douglas Gregor8b228d72010-02-17 21:22:52 +00003507 // Import the location of this declaration.
3508 SourceLocation Loc = Importer.Import(D->getLocation());
3509
3510 // Import the parameter's type.
3511 QualType T = Importer.Import(D->getType());
3512 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003513 return nullptr;
3514
Douglas Gregor8b228d72010-02-17 21:22:52 +00003515 // Create the imported parameter.
3516 ImplicitParamDecl *ToParm
3517 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
3518 Loc, Name.getAsIdentifierInfo(),
3519 T);
3520 return Importer.Imported(D, ToParm);
3521}
3522
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003523Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
3524 // Parameters are created in the translation unit's context, then moved
3525 // into the function declaration's context afterward.
3526 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3527
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003528 // Import the name of this declaration.
3529 DeclarationName Name = Importer.Import(D->getDeclName());
3530 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003531 return nullptr;
3532
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003533 // Import the location of this declaration.
3534 SourceLocation Loc = Importer.Import(D->getLocation());
3535
3536 // Import the parameter's type.
3537 QualType T = Importer.Import(D->getType());
3538 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003539 return nullptr;
3540
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003541 // Create the imported parameter.
3542 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3543 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003544 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003545 Loc, Name.getAsIdentifierInfo(),
3546 T, TInfo, D->getStorageClass(),
Craig Topper36250ad2014-05-12 05:36:57 +00003547 /*FIXME: Default argument*/nullptr);
John McCallf3cd6652010-03-12 18:31:32 +00003548 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Sean Callanan59721b32015-04-28 18:41:46 +00003549
3550 if (D->isUsed())
3551 ToParm->setIsUsed();
3552
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003553 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003554}
3555
Douglas Gregor43f54792010-02-17 02:12:47 +00003556Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3557 // Import the major distinguishing characteristics of a method.
3558 DeclContext *DC, *LexicalDC;
3559 DeclarationName Name;
3560 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003561 NamedDecl *ToD;
3562 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003563 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003564 if (ToD)
3565 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003566
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003567 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003568 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003569 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3570 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003571 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3572 continue;
3573
3574 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003575 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3576 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003577 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003578 << D->isInstanceMethod() << Name << D->getReturnType()
3579 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00003580 Importer.ToDiag(FoundMethod->getLocation(),
3581 diag::note_odr_objc_method_here)
3582 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003583 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003584 }
3585
3586 // Check the number of parameters.
3587 if (D->param_size() != FoundMethod->param_size()) {
3588 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3589 << D->isInstanceMethod() << Name
3590 << D->param_size() << FoundMethod->param_size();
3591 Importer.ToDiag(FoundMethod->getLocation(),
3592 diag::note_odr_objc_method_here)
3593 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003594 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003595 }
3596
3597 // Check parameter types.
3598 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
3599 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3600 P != PEnd; ++P, ++FoundP) {
3601 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3602 (*FoundP)->getType())) {
3603 Importer.FromDiag((*P)->getLocation(),
3604 diag::err_odr_objc_method_param_type_inconsistent)
3605 << D->isInstanceMethod() << Name
3606 << (*P)->getType() << (*FoundP)->getType();
3607 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3608 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003609 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003610 }
3611 }
3612
3613 // Check variadic/non-variadic.
3614 // Check the number of parameters.
3615 if (D->isVariadic() != FoundMethod->isVariadic()) {
3616 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3617 << D->isInstanceMethod() << Name;
3618 Importer.ToDiag(FoundMethod->getLocation(),
3619 diag::note_odr_objc_method_here)
3620 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003621 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003622 }
3623
3624 // FIXME: Any other bits we need to merge?
3625 return Importer.Imported(D, FoundMethod);
3626 }
3627 }
3628
3629 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00003630 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003631 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003632 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003633
Alp Toker314cc812014-01-25 16:55:45 +00003634 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00003635
Alp Toker314cc812014-01-25 16:55:45 +00003636 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
3637 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3638 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3639 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3640 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003641
3642 // FIXME: When we decide to merge method definitions, we'll need to
3643 // deal with implicit parameters.
3644
3645 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003646 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003647 for (auto *FromP : D->parameters()) {
Aaron Ballman43b68be2014-03-07 17:50:17 +00003648 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00003649 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003650 return nullptr;
3651
Douglas Gregor43f54792010-02-17 02:12:47 +00003652 ToParams.push_back(ToP);
3653 }
3654
3655 // Set the parameters.
3656 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3657 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003658 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003659 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003660 SmallVector<SourceLocation, 12> SelLocs;
3661 D->getSelectorLocs(SelLocs);
3662 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003663
3664 ToMethod->setLexicalDeclContext(LexicalDC);
3665 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003666 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003667 return ToMethod;
3668}
3669
Douglas Gregor85f3f952015-07-07 03:57:15 +00003670Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3671 // Import the major distinguishing characteristics of a category.
3672 DeclContext *DC, *LexicalDC;
3673 DeclarationName Name;
3674 SourceLocation Loc;
3675 NamedDecl *ToD;
3676 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3677 return nullptr;
3678 if (ToD)
3679 return ToD;
3680
3681 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3682 if (!BoundInfo)
3683 return nullptr;
3684
3685 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
3686 Importer.getToContext(), DC,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00003687 D->getVariance(),
3688 Importer.Import(D->getVarianceLoc()),
Douglas Gregore83b9562015-07-07 03:57:53 +00003689 D->getIndex(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003690 Importer.Import(D->getLocation()),
3691 Name.getAsIdentifierInfo(),
3692 Importer.Import(D->getColonLoc()),
3693 BoundInfo);
3694 Importer.Imported(D, Result);
3695 Result->setLexicalDeclContext(LexicalDC);
3696 return Result;
3697}
3698
Douglas Gregor84c51c32010-02-18 01:47:50 +00003699Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3700 // Import the major distinguishing characteristics of a category.
3701 DeclContext *DC, *LexicalDC;
3702 DeclarationName Name;
3703 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003704 NamedDecl *ToD;
3705 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003706 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003707 if (ToD)
3708 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003709
Douglas Gregor84c51c32010-02-18 01:47:50 +00003710 ObjCInterfaceDecl *ToInterface
3711 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3712 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00003713 return nullptr;
3714
Douglas Gregor84c51c32010-02-18 01:47:50 +00003715 // Determine if we've already encountered this category.
3716 ObjCCategoryDecl *MergeWithCategory
3717 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3718 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3719 if (!ToCategory) {
3720 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003721 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003722 Loc,
3723 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003724 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003725 ToInterface,
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003726 /*TypeParamList=*/nullptr,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003727 Importer.Import(D->getIvarLBraceLoc()),
3728 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003729 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003730 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003731 Importer.Imported(D, ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003732 // Import the type parameter list after calling Imported, to avoid
3733 // loops when bringing in their DeclContext.
3734 ToCategory->setTypeParamList(ImportObjCTypeParamList(
3735 D->getTypeParamList()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003736
Douglas Gregor84c51c32010-02-18 01:47:50 +00003737 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003738 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3739 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003740 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3741 = D->protocol_loc_begin();
3742 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3743 FromProtoEnd = D->protocol_end();
3744 FromProto != FromProtoEnd;
3745 ++FromProto, ++FromProtoLoc) {
3746 ObjCProtocolDecl *ToProto
3747 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3748 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00003749 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003750 Protocols.push_back(ToProto);
3751 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3752 }
3753
3754 // FIXME: If we're merging, make sure that the protocol list is the same.
3755 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3756 ProtocolLocs.data(), Importer.getToContext());
3757
3758 } else {
3759 Importer.Imported(D, ToCategory);
3760 }
3761
3762 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003763 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003764
3765 // If we have an implementation, import it as well.
3766 if (D->getImplementation()) {
3767 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003768 = cast_or_null<ObjCCategoryImplDecl>(
3769 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003770 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00003771 return nullptr;
3772
Douglas Gregor84c51c32010-02-18 01:47:50 +00003773 ToCategory->setImplementation(Impl);
3774 }
3775
3776 return ToCategory;
3777}
3778
Douglas Gregor2aa53772012-01-24 17:42:07 +00003779bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3780 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003781 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003782 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003783 if (shouldForceImportDeclContext(Kind))
3784 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003785 return false;
3786 }
3787
3788 // Start the protocol definition
3789 To->startDefinition();
3790
3791 // Import protocols
3792 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3793 SmallVector<SourceLocation, 4> ProtocolLocs;
3794 ObjCProtocolDecl::protocol_loc_iterator
3795 FromProtoLoc = From->protocol_loc_begin();
3796 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3797 FromProtoEnd = From->protocol_end();
3798 FromProto != FromProtoEnd;
3799 ++FromProto, ++FromProtoLoc) {
3800 ObjCProtocolDecl *ToProto
3801 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3802 if (!ToProto)
3803 return true;
3804 Protocols.push_back(ToProto);
3805 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3806 }
3807
3808 // FIXME: If we're merging, make sure that the protocol list is the same.
3809 To->setProtocolList(Protocols.data(), Protocols.size(),
3810 ProtocolLocs.data(), Importer.getToContext());
3811
Douglas Gregor2e15c842012-02-01 21:00:38 +00003812 if (shouldForceImportDeclContext(Kind)) {
3813 // Import all of the members of this protocol.
3814 ImportDeclContext(From, /*ForceImport=*/true);
3815 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003816 return false;
3817}
3818
Douglas Gregor98d156a2010-02-17 16:12:00 +00003819Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003820 // If this protocol has a definition in the translation unit we're coming
3821 // from, but this particular declaration is not that definition, import the
3822 // definition and map to that.
3823 ObjCProtocolDecl *Definition = D->getDefinition();
3824 if (Definition && Definition != D) {
3825 Decl *ImportedDef = Importer.Import(Definition);
3826 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003827 return nullptr;
3828
Douglas Gregor2aa53772012-01-24 17:42:07 +00003829 return Importer.Imported(D, ImportedDef);
3830 }
3831
Douglas Gregor84c51c32010-02-18 01:47:50 +00003832 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003833 DeclContext *DC, *LexicalDC;
3834 DeclarationName Name;
3835 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003836 NamedDecl *ToD;
3837 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003838 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003839 if (ToD)
3840 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00003841
Craig Topper36250ad2014-05-12 05:36:57 +00003842 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003843 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003844 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003845 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3846 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003847 continue;
3848
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003849 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003850 break;
3851 }
3852
3853 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003854 if (!ToProto) {
3855 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3856 Name.getAsIdentifierInfo(), Loc,
3857 Importer.Import(D->getAtStartLoc()),
Craig Topper36250ad2014-05-12 05:36:57 +00003858 /*PrevDecl=*/nullptr);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003859 ToProto->setLexicalDeclContext(LexicalDC);
3860 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003861 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003862
3863 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003864
Douglas Gregor2aa53772012-01-24 17:42:07 +00003865 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00003866 return nullptr;
3867
Douglas Gregor98d156a2010-02-17 16:12:00 +00003868 return ToProto;
3869}
3870
Sean Callanan0aae0412014-12-10 00:00:37 +00003871Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3872 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3873 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3874
3875 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3876 SourceLocation LangLoc = Importer.Import(D->getLocation());
3877
3878 bool HasBraces = D->hasBraces();
3879
Sean Callananb12a8552014-12-10 21:22:20 +00003880 LinkageSpecDecl *ToLinkageSpec =
3881 LinkageSpecDecl::Create(Importer.getToContext(),
3882 DC,
3883 ExternLoc,
3884 LangLoc,
3885 D->getLanguage(),
3886 HasBraces);
Sean Callanan0aae0412014-12-10 00:00:37 +00003887
3888 if (HasBraces) {
3889 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3890 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3891 }
3892
3893 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3894 LexicalDC->addDeclInternal(ToLinkageSpec);
3895
3896 Importer.Imported(D, ToLinkageSpec);
3897
3898 return ToLinkageSpec;
3899}
3900
Douglas Gregor2aa53772012-01-24 17:42:07 +00003901bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3902 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003903 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003904 if (To->getDefinition()) {
3905 // Check consistency of superclass.
3906 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3907 if (FromSuper) {
3908 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3909 if (!FromSuper)
3910 return true;
3911 }
3912
3913 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3914 if ((bool)FromSuper != (bool)ToSuper ||
3915 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3916 Importer.ToDiag(To->getLocation(),
3917 diag::err_odr_objc_superclass_inconsistent)
3918 << To->getDeclName();
3919 if (ToSuper)
3920 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3921 << To->getSuperClass()->getDeclName();
3922 else
3923 Importer.ToDiag(To->getLocation(),
3924 diag::note_odr_objc_missing_superclass);
3925 if (From->getSuperClass())
3926 Importer.FromDiag(From->getSuperClassLoc(),
3927 diag::note_odr_objc_superclass)
3928 << From->getSuperClass()->getDeclName();
3929 else
3930 Importer.FromDiag(From->getLocation(),
3931 diag::note_odr_objc_missing_superclass);
3932 }
3933
Douglas Gregor2e15c842012-02-01 21:00:38 +00003934 if (shouldForceImportDeclContext(Kind))
3935 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003936 return false;
3937 }
3938
3939 // Start the definition.
3940 To->startDefinition();
3941
3942 // If this class has a superclass, import it.
3943 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00003944 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3945 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00003946 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00003947
3948 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003949 }
3950
3951 // Import protocols
3952 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3953 SmallVector<SourceLocation, 4> ProtocolLocs;
3954 ObjCInterfaceDecl::protocol_loc_iterator
3955 FromProtoLoc = From->protocol_loc_begin();
3956
3957 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3958 FromProtoEnd = From->protocol_end();
3959 FromProto != FromProtoEnd;
3960 ++FromProto, ++FromProtoLoc) {
3961 ObjCProtocolDecl *ToProto
3962 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3963 if (!ToProto)
3964 return true;
3965 Protocols.push_back(ToProto);
3966 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3967 }
3968
3969 // FIXME: If we're merging, make sure that the protocol list is the same.
3970 To->setProtocolList(Protocols.data(), Protocols.size(),
3971 ProtocolLocs.data(), Importer.getToContext());
3972
3973 // Import categories. When the categories themselves are imported, they'll
3974 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00003975 for (auto *Cat : From->known_categories())
3976 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003977
Douglas Gregor2aa53772012-01-24 17:42:07 +00003978 // If we have an @implementation, import it as well.
3979 if (From->getImplementation()) {
3980 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3981 Importer.Import(From->getImplementation()));
3982 if (!Impl)
3983 return true;
3984
3985 To->setImplementation(Impl);
3986 }
3987
Douglas Gregor2e15c842012-02-01 21:00:38 +00003988 if (shouldForceImportDeclContext(Kind)) {
3989 // Import all of the members of this class.
3990 ImportDeclContext(From, /*ForceImport=*/true);
3991 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003992 return false;
3993}
3994
Douglas Gregor85f3f952015-07-07 03:57:15 +00003995ObjCTypeParamList *
3996ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3997 if (!list)
3998 return nullptr;
3999
4000 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
4001 for (auto fromTypeParam : *list) {
4002 auto toTypeParam = cast_or_null<ObjCTypeParamDecl>(
4003 Importer.Import(fromTypeParam));
4004 if (!toTypeParam)
4005 return nullptr;
4006
4007 toTypeParams.push_back(toTypeParam);
4008 }
4009
4010 return ObjCTypeParamList::create(Importer.getToContext(),
4011 Importer.Import(list->getLAngleLoc()),
4012 toTypeParams,
4013 Importer.Import(list->getRAngleLoc()));
4014}
4015
Douglas Gregor45635322010-02-16 01:20:57 +00004016Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004017 // If this class has a definition in the translation unit we're coming from,
4018 // but this particular declaration is not that definition, import the
4019 // definition and map to that.
4020 ObjCInterfaceDecl *Definition = D->getDefinition();
4021 if (Definition && Definition != D) {
4022 Decl *ImportedDef = Importer.Import(Definition);
4023 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004024 return nullptr;
4025
Douglas Gregor2aa53772012-01-24 17:42:07 +00004026 return Importer.Imported(D, ImportedDef);
4027 }
4028
Douglas Gregor45635322010-02-16 01:20:57 +00004029 // Import the major distinguishing characteristics of an @interface.
4030 DeclContext *DC, *LexicalDC;
4031 DeclarationName Name;
4032 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004033 NamedDecl *ToD;
4034 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004035 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004036 if (ToD)
4037 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00004038
Douglas Gregor2aa53772012-01-24 17:42:07 +00004039 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00004040 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004041 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004042 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004043 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4044 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00004045 continue;
4046
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004047 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00004048 break;
4049 }
4050
Douglas Gregor2aa53772012-01-24 17:42:07 +00004051 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00004052 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004053 if (!ToIface) {
4054 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
4055 Importer.Import(D->getAtStartLoc()),
Douglas Gregor85f3f952015-07-07 03:57:15 +00004056 Name.getAsIdentifierInfo(),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004057 /*TypeParamList=*/nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00004058 /*PrevDecl=*/nullptr, Loc,
Douglas Gregor2aa53772012-01-24 17:42:07 +00004059 D->isImplicitInterfaceDecl());
4060 ToIface->setLexicalDeclContext(LexicalDC);
4061 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00004062 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00004063 Importer.Imported(D, ToIface);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004064 // Import the type parameter list after calling Imported, to avoid
4065 // loops when bringing in their DeclContext.
4066 ToIface->setTypeParamList(ImportObjCTypeParamList(
4067 D->getTypeParamListAsWritten()));
Douglas Gregor45635322010-02-16 01:20:57 +00004068
Douglas Gregor2aa53772012-01-24 17:42:07 +00004069 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00004070 return nullptr;
4071
Douglas Gregor98d156a2010-02-17 16:12:00 +00004072 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00004073}
4074
Douglas Gregor4da9d682010-12-07 15:32:12 +00004075Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
4076 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
4077 Importer.Import(D->getCategoryDecl()));
4078 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00004079 return nullptr;
4080
Douglas Gregor4da9d682010-12-07 15:32:12 +00004081 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
4082 if (!ToImpl) {
4083 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4084 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004085 return nullptr;
4086
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00004087 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00004088 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00004089 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00004090 Category->getClassInterface(),
4091 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00004092 Importer.Import(D->getAtStartLoc()),
4093 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004094
4095 DeclContext *LexicalDC = DC;
4096 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4097 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4098 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004099 return nullptr;
4100
Douglas Gregor4da9d682010-12-07 15:32:12 +00004101 ToImpl->setLexicalDeclContext(LexicalDC);
4102 }
4103
Sean Callanan95e74be2011-10-21 02:57:43 +00004104 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004105 Category->setImplementation(ToImpl);
4106 }
4107
4108 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00004109 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004110 return ToImpl;
4111}
4112
Douglas Gregorda8025c2010-12-07 01:26:03 +00004113Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
4114 // Find the corresponding interface.
4115 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
4116 Importer.Import(D->getClassInterface()));
4117 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00004118 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004119
4120 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00004121 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004122 if (D->getSuperClass()) {
4123 Super = cast_or_null<ObjCInterfaceDecl>(
4124 Importer.Import(D->getSuperClass()));
4125 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00004126 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004127 }
4128
4129 ObjCImplementationDecl *Impl = Iface->getImplementation();
4130 if (!Impl) {
4131 // We haven't imported an implementation yet. Create a new @implementation
4132 // now.
4133 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
4134 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00004135 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00004136 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00004137 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00004138 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00004139 Importer.Import(D->getIvarLBraceLoc()),
4140 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00004141
4142 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4143 DeclContext *LexicalDC
4144 = Importer.ImportContext(D->getLexicalDeclContext());
4145 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004146 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004147 Impl->setLexicalDeclContext(LexicalDC);
4148 }
4149
4150 // Associate the implementation with the class it implements.
4151 Iface->setImplementation(Impl);
4152 Importer.Imported(D, Iface->getImplementation());
4153 } else {
4154 Importer.Imported(D, Iface->getImplementation());
4155
4156 // Verify that the existing @implementation has the same superclass.
4157 if ((Super && !Impl->getSuperClass()) ||
4158 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004159 (Super && Impl->getSuperClass() &&
4160 !declaresSameEntity(Super->getCanonicalDecl(),
4161 Impl->getSuperClass()))) {
4162 Importer.ToDiag(Impl->getLocation(),
4163 diag::err_odr_objc_superclass_inconsistent)
4164 << Iface->getDeclName();
4165 // FIXME: It would be nice to have the location of the superclass
4166 // below.
4167 if (Impl->getSuperClass())
4168 Importer.ToDiag(Impl->getLocation(),
4169 diag::note_odr_objc_superclass)
4170 << Impl->getSuperClass()->getDeclName();
4171 else
4172 Importer.ToDiag(Impl->getLocation(),
4173 diag::note_odr_objc_missing_superclass);
4174 if (D->getSuperClass())
4175 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004176 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004177 << D->getSuperClass()->getDeclName();
4178 else
4179 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004180 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00004181 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004182 }
4183 }
4184
4185 // Import all of the members of this @implementation.
4186 ImportDeclContext(D);
4187
4188 return Impl;
4189}
4190
Douglas Gregora11c4582010-02-17 18:02:10 +00004191Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
4192 // Import the major distinguishing characteristics of an @property.
4193 DeclContext *DC, *LexicalDC;
4194 DeclarationName Name;
4195 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004196 NamedDecl *ToD;
4197 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004198 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004199 if (ToD)
4200 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004201
4202 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004203 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004204 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004205 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004206 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004207 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004208 // Check property types.
4209 if (!Importer.IsStructurallyEquivalent(D->getType(),
4210 FoundProp->getType())) {
4211 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
4212 << Name << D->getType() << FoundProp->getType();
4213 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4214 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00004215 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004216 }
4217
4218 // FIXME: Check property attributes, getters, setters, etc.?
4219
4220 // Consider these properties to be equivalent.
4221 Importer.Imported(D, FoundProp);
4222 return FoundProp;
4223 }
4224 }
4225
4226 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00004227 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
4228 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00004229 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004230
4231 // Create the new property.
4232 ObjCPropertyDecl *ToProperty
4233 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
4234 Name.getAsIdentifierInfo(),
4235 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00004236 Importer.Import(D->getLParenLoc()),
Douglas Gregor813a0662015-06-19 18:14:38 +00004237 Importer.Import(D->getType()),
4238 TSI,
Douglas Gregora11c4582010-02-17 18:02:10 +00004239 D->getPropertyImplementation());
4240 Importer.Imported(D, ToProperty);
4241 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004242 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004243
4244 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004245 ToProperty->setPropertyAttributesAsWritten(
4246 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00004247 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
4248 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
4249 ToProperty->setGetterMethodDecl(
4250 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
4251 ToProperty->setSetterMethodDecl(
4252 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
4253 ToProperty->setPropertyIvarDecl(
4254 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
4255 return ToProperty;
4256}
4257
Douglas Gregor14a49e22010-12-07 18:32:03 +00004258Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4259 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
4260 Importer.Import(D->getPropertyDecl()));
4261 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00004262 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004263
4264 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4265 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004266 return nullptr;
4267
Douglas Gregor14a49e22010-12-07 18:32:03 +00004268 // Import the lexical declaration context.
4269 DeclContext *LexicalDC = DC;
4270 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4271 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4272 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004273 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004274 }
4275
4276 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
4277 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00004278 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004279
4280 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004281 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004282 if (D->getPropertyIvarDecl()) {
4283 Ivar = cast_or_null<ObjCIvarDecl>(
4284 Importer.Import(D->getPropertyIvarDecl()));
4285 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00004286 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004287 }
4288
4289 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004290 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4291 Property->getQueryKind());
Douglas Gregor14a49e22010-12-07 18:32:03 +00004292 if (!ToImpl) {
4293 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
4294 Importer.Import(D->getLocStart()),
4295 Importer.Import(D->getLocation()),
4296 Property,
4297 D->getPropertyImplementation(),
4298 Ivar,
4299 Importer.Import(D->getPropertyIvarDeclLoc()));
4300 ToImpl->setLexicalDeclContext(LexicalDC);
4301 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00004302 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004303 } else {
4304 // Check that we have the same kind of property implementation (@synthesize
4305 // vs. @dynamic).
4306 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
4307 Importer.ToDiag(ToImpl->getLocation(),
4308 diag::err_odr_objc_property_impl_kind_inconsistent)
4309 << Property->getDeclName()
4310 << (ToImpl->getPropertyImplementation()
4311 == ObjCPropertyImplDecl::Dynamic);
4312 Importer.FromDiag(D->getLocation(),
4313 diag::note_odr_objc_property_impl_kind)
4314 << D->getPropertyDecl()->getDeclName()
4315 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00004316 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004317 }
4318
4319 // For @synthesize, check that we have the same
4320 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4321 Ivar != ToImpl->getPropertyIvarDecl()) {
4322 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
4323 diag::err_odr_objc_synthesize_ivar_inconsistent)
4324 << Property->getDeclName()
4325 << ToImpl->getPropertyIvarDecl()->getDeclName()
4326 << Ivar->getDeclName();
4327 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
4328 diag::note_odr_objc_synthesize_ivar_here)
4329 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00004330 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004331 }
4332
4333 // Merge the existing implementation with the new implementation.
4334 Importer.Imported(D, ToImpl);
4335 }
4336
4337 return ToImpl;
4338}
4339
Douglas Gregora082a492010-11-30 19:14:50 +00004340Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
4341 // For template arguments, we adopt the translation unit as our declaration
4342 // context. This context will be fixed when the actual template declaration
4343 // is created.
4344
4345 // FIXME: Import default argument.
4346 return TemplateTypeParmDecl::Create(Importer.getToContext(),
4347 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00004348 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004349 Importer.Import(D->getLocation()),
4350 D->getDepth(),
4351 D->getIndex(),
4352 Importer.Import(D->getIdentifier()),
4353 D->wasDeclaredWithTypename(),
4354 D->isParameterPack());
4355}
4356
4357Decl *
4358ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
4359 // Import the name of this declaration.
4360 DeclarationName Name = Importer.Import(D->getDeclName());
4361 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004362 return nullptr;
4363
Douglas Gregora082a492010-11-30 19:14:50 +00004364 // Import the location of this declaration.
4365 SourceLocation Loc = Importer.Import(D->getLocation());
4366
4367 // Import the type of this declaration.
4368 QualType T = Importer.Import(D->getType());
4369 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004370 return nullptr;
4371
Douglas Gregora082a492010-11-30 19:14:50 +00004372 // Import type-source information.
4373 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4374 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00004375 return nullptr;
4376
Douglas Gregora082a492010-11-30 19:14:50 +00004377 // FIXME: Import default argument.
4378
4379 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
4380 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004381 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004382 Loc, D->getDepth(), D->getPosition(),
4383 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00004384 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00004385}
4386
4387Decl *
4388ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4389 // Import the name of this declaration.
4390 DeclarationName Name = Importer.Import(D->getDeclName());
4391 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004392 return nullptr;
4393
Douglas Gregora082a492010-11-30 19:14:50 +00004394 // Import the location of this declaration.
4395 SourceLocation Loc = Importer.Import(D->getLocation());
4396
4397 // Import template parameters.
4398 TemplateParameterList *TemplateParams
4399 = ImportTemplateParameterList(D->getTemplateParameters());
4400 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004401 return nullptr;
4402
Douglas Gregora082a492010-11-30 19:14:50 +00004403 // FIXME: Import default argument.
4404
4405 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
4406 Importer.getToContext().getTranslationUnitDecl(),
4407 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00004408 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00004409 Name.getAsIdentifierInfo(),
4410 TemplateParams);
4411}
4412
4413Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
4414 // If this record has a definition in the translation unit we're coming from,
4415 // but this particular declaration is not that definition, import the
4416 // definition and map to that.
4417 CXXRecordDecl *Definition
4418 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
4419 if (Definition && Definition != D->getTemplatedDecl()) {
4420 Decl *ImportedDef
4421 = Importer.Import(Definition->getDescribedClassTemplate());
4422 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004423 return nullptr;
4424
Douglas Gregora082a492010-11-30 19:14:50 +00004425 return Importer.Imported(D, ImportedDef);
4426 }
4427
4428 // Import the major distinguishing characteristics of this class template.
4429 DeclContext *DC, *LexicalDC;
4430 DeclarationName Name;
4431 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004432 NamedDecl *ToD;
4433 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004434 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004435 if (ToD)
4436 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004437
Douglas Gregora082a492010-11-30 19:14:50 +00004438 // We may already have a template of the same name; try to find and match it.
4439 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004440 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004441 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004442 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004443 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4444 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00004445 continue;
4446
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004447 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00004448 if (ClassTemplateDecl *FoundTemplate
4449 = dyn_cast<ClassTemplateDecl>(Found)) {
4450 if (IsStructuralMatch(D, FoundTemplate)) {
4451 // The class templates structurally match; call it the same template.
4452 // FIXME: We may be filling in a forward declaration here. Handle
4453 // this case!
4454 Importer.Imported(D->getTemplatedDecl(),
4455 FoundTemplate->getTemplatedDecl());
4456 return Importer.Imported(D, FoundTemplate);
4457 }
4458 }
4459
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004460 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00004461 }
4462
4463 if (!ConflictingDecls.empty()) {
4464 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4465 ConflictingDecls.data(),
4466 ConflictingDecls.size());
4467 }
4468
4469 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004470 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00004471 }
4472
4473 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
4474
4475 // Create the declaration that is being templated.
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004476 // Create the declaration that is being templated.
4477 CXXRecordDecl *D2Templated = cast_or_null<CXXRecordDecl>(
4478 Importer.Import(DTemplated));
4479 if (!D2Templated)
4480 return nullptr;
4481
4482 // Resolve possible cyclic import.
4483 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
4484 return AlreadyImported;
4485
Douglas Gregora082a492010-11-30 19:14:50 +00004486 // Create the class template declaration itself.
4487 TemplateParameterList *TemplateParams
4488 = ImportTemplateParameterList(D->getTemplateParameters());
4489 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004490 return nullptr;
4491
Douglas Gregora082a492010-11-30 19:14:50 +00004492 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4493 Loc, Name, TemplateParams,
4494 D2Templated,
Craig Topper36250ad2014-05-12 05:36:57 +00004495 /*PrevDecl=*/nullptr);
Douglas Gregora082a492010-11-30 19:14:50 +00004496 D2Templated->setDescribedClassTemplate(D2);
4497
4498 D2->setAccess(D->getAccess());
4499 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004500 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004501
4502 // Note the relationship between the class templates.
4503 Importer.Imported(D, D2);
4504 Importer.Imported(DTemplated, D2Templated);
4505
John McCallf937c022011-10-07 06:10:15 +00004506 if (DTemplated->isCompleteDefinition() &&
4507 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004508 // FIXME: Import definition!
4509 }
4510
4511 return D2;
4512}
4513
Douglas Gregore2e50d332010-12-01 01:36:18 +00004514Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4515 ClassTemplateSpecializationDecl *D) {
4516 // If this record has a definition in the translation unit we're coming from,
4517 // but this particular declaration is not that definition, import the
4518 // definition and map to that.
4519 TagDecl *Definition = D->getDefinition();
4520 if (Definition && Definition != D) {
4521 Decl *ImportedDef = Importer.Import(Definition);
4522 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004523 return nullptr;
4524
Douglas Gregore2e50d332010-12-01 01:36:18 +00004525 return Importer.Imported(D, ImportedDef);
4526 }
4527
4528 ClassTemplateDecl *ClassTemplate
4529 = cast_or_null<ClassTemplateDecl>(Importer.Import(
4530 D->getSpecializedTemplate()));
4531 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004532 return nullptr;
4533
Douglas Gregore2e50d332010-12-01 01:36:18 +00004534 // Import the context of this declaration.
4535 DeclContext *DC = ClassTemplate->getDeclContext();
4536 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004537 return nullptr;
4538
Douglas Gregore2e50d332010-12-01 01:36:18 +00004539 DeclContext *LexicalDC = DC;
4540 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4541 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4542 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004543 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004544 }
4545
4546 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004547 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4548 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004549
4550 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004551 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004552 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4553 D->getTemplateArgs().size(),
4554 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004555 return nullptr;
4556
Douglas Gregore2e50d332010-12-01 01:36:18 +00004557 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004558 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004559 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004560 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004561 if (D2) {
4562 // We already have a class template specialization with these template
4563 // arguments.
4564
4565 // FIXME: Check for specialization vs. instantiation errors.
4566
4567 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004568 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004569 // The record types structurally match, or the "from" translation
4570 // unit only had a forward declaration anyway; call it the same
4571 // function.
4572 return Importer.Imported(D, FoundDef);
4573 }
4574 }
4575 } else {
4576 // Create a new specialization.
4577 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4578 D->getTagKind(), DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004579 StartLoc, IdLoc,
4580 ClassTemplate,
David Majnemer8b622692016-07-03 21:17:51 +00004581 TemplateArgs,
Craig Topper36250ad2014-05-12 05:36:57 +00004582 /*PrevDecl=*/nullptr);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004583 D2->setSpecializationKind(D->getSpecializationKind());
4584
4585 // Add this specialization to the class template.
4586 ClassTemplate->AddSpecialization(D2, InsertPos);
4587
4588 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004589 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00004590
4591 // Add the specialization to this context.
4592 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004593 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004594 }
4595 Importer.Imported(D, D2);
4596
John McCallf937c022011-10-07 06:10:15 +00004597 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004598 return nullptr;
4599
Douglas Gregore2e50d332010-12-01 01:36:18 +00004600 return D2;
4601}
4602
Larisse Voufo39a1e502013-08-06 01:03:05 +00004603Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4604 // If this variable has a definition in the translation unit we're coming
4605 // from,
4606 // but this particular declaration is not that definition, import the
4607 // definition and map to that.
4608 VarDecl *Definition =
4609 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4610 if (Definition && Definition != D->getTemplatedDecl()) {
4611 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4612 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004613 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004614
4615 return Importer.Imported(D, ImportedDef);
4616 }
4617
4618 // Import the major distinguishing characteristics of this variable template.
4619 DeclContext *DC, *LexicalDC;
4620 DeclarationName Name;
4621 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004622 NamedDecl *ToD;
4623 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004624 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004625 if (ToD)
4626 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004627
4628 // We may already have a template of the same name; try to find and match it.
4629 assert(!DC->isFunctionOrMethod() &&
4630 "Variable templates cannot be declared at function scope");
4631 SmallVector<NamedDecl *, 4> ConflictingDecls;
4632 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004633 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004634 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4635 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4636 continue;
4637
4638 Decl *Found = FoundDecls[I];
4639 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
4640 if (IsStructuralMatch(D, FoundTemplate)) {
4641 // The variable templates structurally match; call it the same template.
4642 Importer.Imported(D->getTemplatedDecl(),
4643 FoundTemplate->getTemplatedDecl());
4644 return Importer.Imported(D, FoundTemplate);
4645 }
4646 }
4647
4648 ConflictingDecls.push_back(FoundDecls[I]);
4649 }
4650
4651 if (!ConflictingDecls.empty()) {
4652 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4653 ConflictingDecls.data(),
4654 ConflictingDecls.size());
4655 }
4656
4657 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004658 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004659
4660 VarDecl *DTemplated = D->getTemplatedDecl();
4661
4662 // Import the type.
4663 QualType T = Importer.Import(DTemplated->getType());
4664 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004665 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004666
4667 // Create the declaration that is being templated.
4668 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4669 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
4670 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo());
4671 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc,
4672 IdLoc, Name.getAsIdentifierInfo(), T,
4673 TInfo, DTemplated->getStorageClass());
4674 D2Templated->setAccess(DTemplated->getAccess());
4675 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
4676 D2Templated->setLexicalDeclContext(LexicalDC);
4677
4678 // Importer.Imported(DTemplated, D2Templated);
4679 // LexicalDC->addDeclInternal(D2Templated);
4680
4681 // Merge the initializer.
4682 if (ImportDefinition(DTemplated, D2Templated))
Craig Topper36250ad2014-05-12 05:36:57 +00004683 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004684
4685 // Create the variable template declaration itself.
4686 TemplateParameterList *TemplateParams =
4687 ImportTemplateParameterList(D->getTemplateParameters());
4688 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004689 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004690
4691 VarTemplateDecl *D2 = VarTemplateDecl::Create(
Richard Smithbeef3452014-01-16 23:39:20 +00004692 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004693 D2Templated->setDescribedVarTemplate(D2);
4694
4695 D2->setAccess(D->getAccess());
4696 D2->setLexicalDeclContext(LexicalDC);
4697 LexicalDC->addDeclInternal(D2);
4698
4699 // Note the relationship between the variable templates.
4700 Importer.Imported(D, D2);
4701 Importer.Imported(DTemplated, D2Templated);
4702
4703 if (DTemplated->isThisDeclarationADefinition() &&
4704 !D2Templated->isThisDeclarationADefinition()) {
4705 // FIXME: Import definition!
4706 }
4707
4708 return D2;
4709}
4710
4711Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4712 VarTemplateSpecializationDecl *D) {
4713 // If this record has a definition in the translation unit we're coming from,
4714 // but this particular declaration is not that definition, import the
4715 // definition and map to that.
4716 VarDecl *Definition = D->getDefinition();
4717 if (Definition && Definition != D) {
4718 Decl *ImportedDef = Importer.Import(Definition);
4719 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004720 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004721
4722 return Importer.Imported(D, ImportedDef);
4723 }
4724
4725 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
4726 Importer.Import(D->getSpecializedTemplate()));
4727 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004728 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004729
4730 // Import the context of this declaration.
4731 DeclContext *DC = VarTemplate->getDeclContext();
4732 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004733 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004734
4735 DeclContext *LexicalDC = DC;
4736 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4737 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4738 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004739 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004740 }
4741
4742 // Import the location of this declaration.
4743 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4744 SourceLocation IdLoc = Importer.Import(D->getLocation());
4745
4746 // Import template arguments.
4747 SmallVector<TemplateArgument, 2> TemplateArgs;
4748 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4749 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004750 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004751
4752 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004753 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004754 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004755 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004756 if (D2) {
4757 // We already have a variable template specialization with these template
4758 // arguments.
4759
4760 // FIXME: Check for specialization vs. instantiation errors.
4761
4762 if (VarDecl *FoundDef = D2->getDefinition()) {
4763 if (!D->isThisDeclarationADefinition() ||
4764 IsStructuralMatch(D, FoundDef)) {
4765 // The record types structurally match, or the "from" translation
4766 // unit only had a forward declaration anyway; call it the same
4767 // variable.
4768 return Importer.Imported(D, FoundDef);
4769 }
4770 }
4771 } else {
4772
4773 // Import the type.
4774 QualType T = Importer.Import(D->getType());
4775 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004776 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004777 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4778
4779 // Create a new specialization.
4780 D2 = VarTemplateSpecializationDecl::Create(
4781 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
David Majnemer8b622692016-07-03 21:17:51 +00004782 D->getStorageClass(), TemplateArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004783 D2->setSpecializationKind(D->getSpecializationKind());
4784 D2->setTemplateArgsInfo(D->getTemplateArgsInfo());
4785
4786 // Add this specialization to the class template.
4787 VarTemplate->AddSpecialization(D2, InsertPos);
4788
4789 // Import the qualifier, if any.
4790 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4791
4792 // Add the specialization to this context.
4793 D2->setLexicalDeclContext(LexicalDC);
4794 LexicalDC->addDeclInternal(D2);
4795 }
4796 Importer.Imported(D, D2);
4797
4798 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004799 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004800
4801 return D2;
4802}
4803
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004804//----------------------------------------------------------------------------
4805// Import Statements
4806//----------------------------------------------------------------------------
4807
Sean Callanan59721b32015-04-28 18:41:46 +00004808DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4809 if (DG.isNull())
4810 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4811 size_t NumDecls = DG.end() - DG.begin();
4812 SmallVector<Decl *, 1> ToDecls(NumDecls);
4813 auto &_Importer = this->Importer;
4814 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4815 [&_Importer](Decl *D) -> Decl * {
4816 return _Importer.Import(D);
4817 });
4818 return DeclGroupRef::Create(Importer.getToContext(),
4819 ToDecls.begin(),
4820 NumDecls);
4821}
4822
4823 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4824 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4825 << S->getStmtClassName();
4826 return nullptr;
4827 }
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004828
4829
4830Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
4831 SmallVector<IdentifierInfo *, 4> Names;
4832 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4833 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
4834 if (!ToII)
4835 return nullptr;
4836 Names.push_back(ToII);
4837 }
4838 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4839 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
4840 if (!ToII)
4841 return nullptr;
4842 Names.push_back(ToII);
4843 }
4844
4845 SmallVector<StringLiteral *, 4> Clobbers;
4846 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
4847 StringLiteral *Clobber = cast_or_null<StringLiteral>(
4848 Importer.Import(S->getClobberStringLiteral(I)));
4849 if (!Clobber)
4850 return nullptr;
4851 Clobbers.push_back(Clobber);
4852 }
4853
4854 SmallVector<StringLiteral *, 4> Constraints;
4855 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4856 StringLiteral *Output = cast_or_null<StringLiteral>(
4857 Importer.Import(S->getOutputConstraintLiteral(I)));
4858 if (!Output)
4859 return nullptr;
4860 Constraints.push_back(Output);
4861 }
4862
4863 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4864 StringLiteral *Input = cast_or_null<StringLiteral>(
4865 Importer.Import(S->getInputConstraintLiteral(I)));
4866 if (!Input)
4867 return nullptr;
4868 Constraints.push_back(Input);
4869 }
4870
4871 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
4872 if (ImportArrayChecked(S->begin_outputs(), S->end_outputs(), Exprs.begin()))
4873 return nullptr;
4874
4875 if (ImportArrayChecked(S->begin_inputs(), S->end_inputs(),
4876 Exprs.begin() + S->getNumOutputs()))
4877 return nullptr;
4878
4879 StringLiteral *AsmStr = cast_or_null<StringLiteral>(
4880 Importer.Import(S->getAsmString()));
4881 if (!AsmStr)
4882 return nullptr;
4883
4884 return new (Importer.getToContext()) GCCAsmStmt(
4885 Importer.getToContext(),
4886 Importer.Import(S->getAsmLoc()),
4887 S->isSimple(),
4888 S->isVolatile(),
4889 S->getNumOutputs(),
4890 S->getNumInputs(),
4891 Names.data(),
4892 Constraints.data(),
4893 Exprs.data(),
4894 AsmStr,
4895 S->getNumClobbers(),
4896 Clobbers.data(),
4897 Importer.Import(S->getRParenLoc()));
4898}
4899
Sean Callanan59721b32015-04-28 18:41:46 +00004900Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4901 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
4902 for (Decl *ToD : ToDG) {
4903 if (!ToD)
4904 return nullptr;
4905 }
4906 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4907 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4908 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4909}
4910
4911Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4912 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4913 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4914 S->hasLeadingEmptyMacro());
4915}
4916
4917Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004918 llvm::SmallVector<Stmt *, 8> ToStmts(S->size());
Sean Callanan8bca9962016-03-28 21:43:01 +00004919
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004920 if (ImportArrayChecked(S->body_begin(), S->body_end(), ToStmts.begin()))
Sean Callanan8bca9962016-03-28 21:43:01 +00004921 return nullptr;
4922
Sean Callanan59721b32015-04-28 18:41:46 +00004923 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4924 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
4925 return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(),
4926 ToStmts,
4927 ToLBraceLoc, ToRBraceLoc);
4928}
4929
4930Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4931 Expr *ToLHS = Importer.Import(S->getLHS());
4932 if (!ToLHS)
4933 return nullptr;
4934 Expr *ToRHS = Importer.Import(S->getRHS());
4935 if (!ToRHS && S->getRHS())
4936 return nullptr;
4937 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4938 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4939 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4940 return new (Importer.getToContext()) CaseStmt(ToLHS, ToRHS,
4941 ToCaseLoc, ToEllipsisLoc,
4942 ToColonLoc);
4943}
4944
4945Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4946 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4947 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4948 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4949 if (!ToSubStmt && S->getSubStmt())
4950 return nullptr;
4951 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4952 ToSubStmt);
4953}
4954
4955Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4956 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
4957 LabelDecl *ToLabelDecl =
4958 cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
4959 if (!ToLabelDecl && S->getDecl())
4960 return nullptr;
4961 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4962 if (!ToSubStmt && S->getSubStmt())
4963 return nullptr;
4964 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4965 ToSubStmt);
4966}
4967
4968Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4969 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4970 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4971 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
4972 ASTContext &_ToContext = Importer.getToContext();
4973 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
4974 [&_ToContext](const Attr *A) -> const Attr * {
4975 return A->clone(_ToContext);
4976 });
4977 for (const Attr *ToA : ToAttrs) {
4978 if (!ToA)
4979 return nullptr;
4980 }
4981 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4982 if (!ToSubStmt && S->getSubStmt())
4983 return nullptr;
4984 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4985 ToAttrs, ToSubStmt);
4986}
4987
4988Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4989 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
Richard Smitha547eb22016-07-14 00:11:03 +00004990 Stmt *ToInit = Importer.Import(S->getInit());
4991 if (!ToInit && S->getInit())
4992 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004993 VarDecl *ToConditionVariable = nullptr;
4994 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4995 ToConditionVariable =
4996 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4997 if (!ToConditionVariable)
4998 return nullptr;
4999 }
5000 Expr *ToCondition = Importer.Import(S->getCond());
5001 if (!ToCondition && S->getCond())
5002 return nullptr;
5003 Stmt *ToThenStmt = Importer.Import(S->getThen());
5004 if (!ToThenStmt && S->getThen())
5005 return nullptr;
5006 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
5007 Stmt *ToElseStmt = Importer.Import(S->getElse());
5008 if (!ToElseStmt && S->getElse())
5009 return nullptr;
5010 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
Richard Smithb130fe72016-06-23 19:16:49 +00005011 ToIfLoc, S->isConstexpr(),
Richard Smitha547eb22016-07-14 00:11:03 +00005012 ToInit,
Richard Smithb130fe72016-06-23 19:16:49 +00005013 ToConditionVariable,
Sean Callanan59721b32015-04-28 18:41:46 +00005014 ToCondition, ToThenStmt,
5015 ToElseLoc, ToElseStmt);
5016}
5017
5018Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
Richard Smitha547eb22016-07-14 00:11:03 +00005019 Stmt *ToInit = Importer.Import(S->getInit());
5020 if (!ToInit && S->getInit())
5021 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005022 VarDecl *ToConditionVariable = nullptr;
5023 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5024 ToConditionVariable =
5025 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5026 if (!ToConditionVariable)
5027 return nullptr;
5028 }
5029 Expr *ToCondition = Importer.Import(S->getCond());
5030 if (!ToCondition && S->getCond())
5031 return nullptr;
5032 SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt(
Richard Smitha547eb22016-07-14 00:11:03 +00005033 Importer.getToContext(), ToInit,
5034 ToConditionVariable, ToCondition);
Sean Callanan59721b32015-04-28 18:41:46 +00005035 Stmt *ToBody = Importer.Import(S->getBody());
5036 if (!ToBody && S->getBody())
5037 return nullptr;
5038 ToStmt->setBody(ToBody);
5039 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
5040 // Now we have to re-chain the cases.
5041 SwitchCase *LastChainedSwitchCase = nullptr;
5042 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
5043 SC = SC->getNextSwitchCase()) {
5044 SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
5045 if (!ToSC)
5046 return nullptr;
5047 if (LastChainedSwitchCase)
5048 LastChainedSwitchCase->setNextSwitchCase(ToSC);
5049 else
5050 ToStmt->setSwitchCaseList(ToSC);
5051 LastChainedSwitchCase = ToSC;
5052 }
5053 return ToStmt;
5054}
5055
5056Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
5057 VarDecl *ToConditionVariable = nullptr;
5058 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5059 ToConditionVariable =
5060 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5061 if (!ToConditionVariable)
5062 return nullptr;
5063 }
5064 Expr *ToCondition = Importer.Import(S->getCond());
5065 if (!ToCondition && S->getCond())
5066 return nullptr;
5067 Stmt *ToBody = Importer.Import(S->getBody());
5068 if (!ToBody && S->getBody())
5069 return nullptr;
5070 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
5071 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
5072 ToConditionVariable,
5073 ToCondition, ToBody,
5074 ToWhileLoc);
5075}
5076
5077Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5078 Stmt *ToBody = Importer.Import(S->getBody());
5079 if (!ToBody && S->getBody())
5080 return nullptr;
5081 Expr *ToCondition = Importer.Import(S->getCond());
5082 if (!ToCondition && S->getCond())
5083 return nullptr;
5084 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
5085 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
5086 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5087 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
5088 ToDoLoc, ToWhileLoc,
5089 ToRParenLoc);
5090}
5091
5092Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
5093 Stmt *ToInit = Importer.Import(S->getInit());
5094 if (!ToInit && S->getInit())
5095 return nullptr;
5096 Expr *ToCondition = Importer.Import(S->getCond());
5097 if (!ToCondition && S->getCond())
5098 return nullptr;
5099 VarDecl *ToConditionVariable = nullptr;
5100 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5101 ToConditionVariable =
5102 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5103 if (!ToConditionVariable)
5104 return nullptr;
5105 }
5106 Expr *ToInc = Importer.Import(S->getInc());
5107 if (!ToInc && S->getInc())
5108 return nullptr;
5109 Stmt *ToBody = Importer.Import(S->getBody());
5110 if (!ToBody && S->getBody())
5111 return nullptr;
5112 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5113 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
5114 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5115 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
5116 ToInit, ToCondition,
5117 ToConditionVariable,
5118 ToInc, ToBody,
5119 ToForLoc, ToLParenLoc,
5120 ToRParenLoc);
5121}
5122
5123Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5124 LabelDecl *ToLabel = nullptr;
5125 if (LabelDecl *FromLabel = S->getLabel()) {
5126 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
5127 if (!ToLabel)
5128 return nullptr;
5129 }
5130 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
5131 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
5132 return new (Importer.getToContext()) GotoStmt(ToLabel,
5133 ToGotoLoc, ToLabelLoc);
5134}
5135
5136Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5137 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
5138 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
5139 Expr *ToTarget = Importer.Import(S->getTarget());
5140 if (!ToTarget && S->getTarget())
5141 return nullptr;
5142 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
5143 ToTarget);
5144}
5145
5146Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5147 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
5148 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
5149}
5150
5151Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5152 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
5153 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
5154}
5155
5156Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5157 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
5158 Expr *ToRetExpr = Importer.Import(S->getRetValue());
5159 if (!ToRetExpr && S->getRetValue())
5160 return nullptr;
5161 VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate());
5162 VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
5163 if (!ToNRVOCandidate && NRVOCandidate)
5164 return nullptr;
5165 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
5166 ToNRVOCandidate);
5167}
5168
5169Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5170 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
5171 VarDecl *ToExceptionDecl = nullptr;
5172 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
5173 ToExceptionDecl =
5174 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5175 if (!ToExceptionDecl)
5176 return nullptr;
5177 }
5178 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
5179 if (!ToHandlerBlock && S->getHandlerBlock())
5180 return nullptr;
5181 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
5182 ToExceptionDecl,
5183 ToHandlerBlock);
5184}
5185
5186Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5187 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
5188 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
5189 if (!ToTryBlock && S->getTryBlock())
5190 return nullptr;
5191 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5192 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5193 CXXCatchStmt *FromHandler = S->getHandler(HI);
5194 if (Stmt *ToHandler = Importer.Import(FromHandler))
5195 ToHandlers[HI] = ToHandler;
5196 else
5197 return nullptr;
5198 }
5199 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
5200 ToHandlers);
5201}
5202
5203Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
5204 DeclStmt *ToRange =
5205 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
5206 if (!ToRange && S->getRangeStmt())
5207 return nullptr;
Richard Smith01694c32016-03-20 10:33:40 +00005208 DeclStmt *ToBegin =
5209 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
5210 if (!ToBegin && S->getBeginStmt())
5211 return nullptr;
5212 DeclStmt *ToEnd =
5213 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
5214 if (!ToEnd && S->getEndStmt())
Sean Callanan59721b32015-04-28 18:41:46 +00005215 return nullptr;
5216 Expr *ToCond = Importer.Import(S->getCond());
5217 if (!ToCond && S->getCond())
5218 return nullptr;
5219 Expr *ToInc = Importer.Import(S->getInc());
5220 if (!ToInc && S->getInc())
5221 return nullptr;
5222 DeclStmt *ToLoopVar =
5223 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
5224 if (!ToLoopVar && S->getLoopVarStmt())
5225 return nullptr;
5226 Stmt *ToBody = Importer.Import(S->getBody());
5227 if (!ToBody && S->getBody())
5228 return nullptr;
5229 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
Richard Smith9f690bd2015-10-27 06:02:45 +00005230 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00005231 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
5232 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
Richard Smith01694c32016-03-20 10:33:40 +00005233 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
Sean Callanan59721b32015-04-28 18:41:46 +00005234 ToCond, ToInc,
5235 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00005236 ToForLoc, ToCoawaitLoc,
5237 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005238}
5239
5240Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5241 Stmt *ToElem = Importer.Import(S->getElement());
5242 if (!ToElem && S->getElement())
5243 return nullptr;
5244 Expr *ToCollect = Importer.Import(S->getCollection());
5245 if (!ToCollect && S->getCollection())
5246 return nullptr;
5247 Stmt *ToBody = Importer.Import(S->getBody());
5248 if (!ToBody && S->getBody())
5249 return nullptr;
5250 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5251 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5252 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
5253 ToCollect,
5254 ToBody, ToForLoc,
5255 ToRParenLoc);
5256}
5257
5258Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5259 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
5260 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5261 VarDecl *ToExceptionDecl = nullptr;
5262 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
5263 ToExceptionDecl =
5264 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5265 if (!ToExceptionDecl)
5266 return nullptr;
5267 }
5268 Stmt *ToBody = Importer.Import(S->getCatchBody());
5269 if (!ToBody && S->getCatchBody())
5270 return nullptr;
5271 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
5272 ToRParenLoc,
5273 ToExceptionDecl,
5274 ToBody);
5275}
5276
5277Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5278 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
5279 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
5280 if (!ToAtFinallyStmt && S->getFinallyBody())
5281 return nullptr;
5282 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
5283 ToAtFinallyStmt);
5284}
5285
5286Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5287 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
5288 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
5289 if (!ToAtTryStmt && S->getTryBody())
5290 return nullptr;
5291 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5292 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5293 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
5294 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
5295 ToCatchStmts[CI] = ToCatchStmt;
5296 else
5297 return nullptr;
5298 }
5299 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
5300 if (!ToAtFinallyStmt && S->getFinallyStmt())
5301 return nullptr;
5302 return ObjCAtTryStmt::Create(Importer.getToContext(),
5303 ToAtTryLoc, ToAtTryStmt,
5304 ToCatchStmts.begin(), ToCatchStmts.size(),
5305 ToAtFinallyStmt);
5306}
5307
5308Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
5309 (ObjCAtSynchronizedStmt *S) {
5310 SourceLocation ToAtSynchronizedLoc =
5311 Importer.Import(S->getAtSynchronizedLoc());
5312 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
5313 if (!ToSynchExpr && S->getSynchExpr())
5314 return nullptr;
5315 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
5316 if (!ToSynchBody && S->getSynchBody())
5317 return nullptr;
5318 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5319 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5320}
5321
5322Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5323 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5324 Expr *ToThrow = Importer.Import(S->getThrowExpr());
5325 if (!ToThrow && S->getThrowExpr())
5326 return nullptr;
5327 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5328}
5329
5330Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5331 (ObjCAutoreleasePoolStmt *S) {
5332 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5333 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5334 if (!ToSubStmt && S->getSubStmt())
5335 return nullptr;
5336 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5337 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005338}
5339
5340//----------------------------------------------------------------------------
5341// Import Expressions
5342//----------------------------------------------------------------------------
5343Expr *ASTNodeImporter::VisitExpr(Expr *E) {
5344 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5345 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00005346 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005347}
5348
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005349Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
5350 QualType T = Importer.Import(E->getType());
5351 if (T.isNull())
5352 return nullptr;
5353
5354 Expr *SubExpr = Importer.Import(E->getSubExpr());
5355 if (!SubExpr && E->getSubExpr())
5356 return nullptr;
5357
5358 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
5359 if (!TInfo)
5360 return nullptr;
5361
5362 return new (Importer.getToContext()) VAArgExpr(
5363 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
5364 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
5365}
5366
5367
5368Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
5369 QualType T = Importer.Import(E->getType());
5370 if (T.isNull())
5371 return nullptr;
5372
5373 return new (Importer.getToContext()) GNUNullExpr(
5374 T, Importer.Import(E->getExprLoc()));
5375}
5376
5377Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
5378 QualType T = Importer.Import(E->getType());
5379 if (T.isNull())
5380 return nullptr;
5381
5382 StringLiteral *SL = cast_or_null<StringLiteral>(
5383 Importer.Import(E->getFunctionName()));
5384 if (!SL && E->getFunctionName())
5385 return nullptr;
5386
5387 return new (Importer.getToContext()) PredefinedExpr(
5388 Importer.Import(E->getExprLoc()), T, E->getIdentType(), SL);
5389}
5390
Douglas Gregor52f820e2010-02-19 01:17:02 +00005391Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00005392 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
5393 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005394 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005395
Craig Topper36250ad2014-05-12 05:36:57 +00005396 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005397 if (E->getDecl() != E->getFoundDecl()) {
5398 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5399 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00005400 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005401 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00005402
5403 QualType T = Importer.Import(E->getType());
5404 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005405 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005406
5407 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5408 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00005409 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005410 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00005411 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005412 Importer.Import(E->getLocation()),
5413 T, E->getValueKind(),
5414 FoundD,
Craig Topper36250ad2014-05-12 05:36:57 +00005415 /*FIXME:TemplateArgs=*/nullptr);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005416 if (E->hadMultipleCandidates())
5417 DRE->setHadMultipleCandidates(true);
5418 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00005419}
5420
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005421Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
5422 QualType T = Importer.Import(E->getType());
5423 if (T.isNull())
5424 return NULL;
5425
5426 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
5427}
5428
5429ASTNodeImporter::Designator
5430ASTNodeImporter::ImportDesignator(const Designator &D) {
5431 if (D.isFieldDesignator()) {
5432 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
5433 // Caller checks for import error
5434 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
5435 Importer.Import(D.getFieldLoc()));
5436 }
5437 if (D.isArrayDesignator())
5438 return Designator(D.getFirstExprIndex(),
5439 Importer.Import(D.getLBracketLoc()),
5440 Importer.Import(D.getRBracketLoc()));
5441
5442 assert(D.isArrayRangeDesignator());
5443 return Designator(D.getFirstExprIndex(),
5444 Importer.Import(D.getLBracketLoc()),
5445 Importer.Import(D.getEllipsisLoc()),
5446 Importer.Import(D.getRBracketLoc()));
5447}
5448
5449
5450Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
5451 Expr *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
5452 if (!Init)
5453 return nullptr;
5454
5455 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
5456 // List elements from the second, the first is Init itself
5457 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
5458 if (Expr *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
5459 IndexExprs[I - 1] = Arg;
5460 else
5461 return nullptr;
5462 }
5463
5464 SmallVector<Designator, 4> Designators(DIE->size());
David Majnemerf7e36092016-06-23 00:15:04 +00005465 llvm::transform(DIE->designators(), Designators.begin(),
5466 [this](const Designator &D) -> Designator {
5467 return ImportDesignator(D);
5468 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005469
David Majnemerf7e36092016-06-23 00:15:04 +00005470 for (const Designator &D : DIE->designators())
5471 if (D.isFieldDesignator() && !D.getFieldName())
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005472 return nullptr;
5473
5474 return DesignatedInitExpr::Create(
David Majnemerf7e36092016-06-23 00:15:04 +00005475 Importer.getToContext(), Designators,
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005476 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
5477 DIE->usesGNUSyntax(), Init);
5478}
5479
5480Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
5481 QualType T = Importer.Import(E->getType());
5482 if (T.isNull())
5483 return nullptr;
5484
5485 return new (Importer.getToContext())
5486 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
5487}
5488
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005489Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5490 QualType T = Importer.Import(E->getType());
5491 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005492 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005493
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005494 return IntegerLiteral::Create(Importer.getToContext(),
5495 E->getValue(), T,
5496 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005497}
5498
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005499Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
5500 QualType T = Importer.Import(E->getType());
5501 if (T.isNull())
5502 return nullptr;
5503
5504 return FloatingLiteral::Create(Importer.getToContext(),
5505 E->getValue(), E->isExact(), T,
5506 Importer.Import(E->getLocation()));
5507}
5508
Douglas Gregor623421d2010-02-18 02:21:22 +00005509Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5510 QualType T = Importer.Import(E->getType());
5511 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005512 return nullptr;
5513
Douglas Gregorfb65e592011-07-27 05:40:30 +00005514 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5515 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005516 Importer.Import(E->getLocation()));
5517}
5518
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005519Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
5520 QualType T = Importer.Import(E->getType());
5521 if (T.isNull())
5522 return nullptr;
5523
5524 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
5525 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
5526
5527 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
5528 E->getKind(), E->isPascal(), T,
5529 Locations.data(), Locations.size());
5530}
5531
5532Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
5533 QualType T = Importer.Import(E->getType());
5534 if (T.isNull())
5535 return nullptr;
5536
5537 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
5538 if (!TInfo)
5539 return nullptr;
5540
5541 Expr *Init = Importer.Import(E->getInitializer());
5542 if (!Init)
5543 return nullptr;
5544
5545 return new (Importer.getToContext()) CompoundLiteralExpr(
5546 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
5547 Init, E->isFileScope());
5548}
5549
5550Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
5551 QualType T = Importer.Import(E->getType());
5552 if (T.isNull())
5553 return nullptr;
5554
5555 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
5556 if (ImportArrayChecked(
5557 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
5558 Exprs.begin()))
5559 return nullptr;
5560
5561 return new (Importer.getToContext()) AtomicExpr(
5562 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
5563 Importer.Import(E->getRParenLoc()));
5564}
5565
5566Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
5567 QualType T = Importer.Import(E->getType());
5568 if (T.isNull())
5569 return nullptr;
5570
5571 LabelDecl *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
5572 if (!ToLabel)
5573 return nullptr;
5574
5575 return new (Importer.getToContext()) AddrLabelExpr(
5576 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
5577 ToLabel, T);
5578}
5579
Douglas Gregorc74247e2010-02-19 01:07:06 +00005580Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5581 Expr *SubExpr = Importer.Import(E->getSubExpr());
5582 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005583 return nullptr;
5584
Douglas Gregorc74247e2010-02-19 01:07:06 +00005585 return new (Importer.getToContext())
5586 ParenExpr(Importer.Import(E->getLParen()),
5587 Importer.Import(E->getRParen()),
5588 SubExpr);
5589}
5590
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005591Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
5592 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
5593 if (ImportArrayChecked(
5594 E->getExprs(), E->getExprs() + E->getNumExprs(), Exprs.begin()))
5595 return nullptr;
5596
5597 return new (Importer.getToContext()) ParenListExpr(
5598 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
5599 Exprs, Importer.Import(E->getLParenLoc()));
5600}
5601
5602Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
5603 QualType T = Importer.Import(E->getType());
5604 if (T.isNull())
5605 return nullptr;
5606
5607 CompoundStmt *ToSubStmt = cast_or_null<CompoundStmt>(
5608 Importer.Import(E->getSubStmt()));
5609 if (!ToSubStmt && E->getSubStmt())
5610 return nullptr;
5611
5612 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
5613 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
5614}
5615
Douglas Gregorc74247e2010-02-19 01:07:06 +00005616Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5617 QualType T = Importer.Import(E->getType());
5618 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005619 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005620
5621 Expr *SubExpr = Importer.Import(E->getSubExpr());
5622 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005623 return nullptr;
5624
Douglas Gregorc74247e2010-02-19 01:07:06 +00005625 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005626 T, E->getValueKind(),
5627 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00005628 Importer.Import(E->getOperatorLoc()));
5629}
5630
Peter Collingbournee190dee2011-03-11 19:24:49 +00005631Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
5632 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005633 QualType ResultType = Importer.Import(E->getType());
5634
5635 if (E->isArgumentType()) {
5636 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5637 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005638 return nullptr;
5639
Peter Collingbournee190dee2011-03-11 19:24:49 +00005640 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5641 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005642 Importer.Import(E->getOperatorLoc()),
5643 Importer.Import(E->getRParenLoc()));
5644 }
5645
5646 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5647 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005648 return nullptr;
5649
Peter Collingbournee190dee2011-03-11 19:24:49 +00005650 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5651 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005652 Importer.Import(E->getOperatorLoc()),
5653 Importer.Import(E->getRParenLoc()));
5654}
5655
Douglas Gregorc74247e2010-02-19 01:07:06 +00005656Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5657 QualType T = Importer.Import(E->getType());
5658 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005659 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005660
5661 Expr *LHS = Importer.Import(E->getLHS());
5662 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005663 return nullptr;
5664
Douglas Gregorc74247e2010-02-19 01:07:06 +00005665 Expr *RHS = Importer.Import(E->getRHS());
5666 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005667 return nullptr;
5668
Douglas Gregorc74247e2010-02-19 01:07:06 +00005669 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005670 T, E->getValueKind(),
5671 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005672 Importer.Import(E->getOperatorLoc()),
5673 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005674}
5675
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005676Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
5677 QualType T = Importer.Import(E->getType());
5678 if (T.isNull())
5679 return nullptr;
5680
5681 Expr *ToLHS = Importer.Import(E->getLHS());
5682 if (!ToLHS)
5683 return nullptr;
5684
5685 Expr *ToRHS = Importer.Import(E->getRHS());
5686 if (!ToRHS)
5687 return nullptr;
5688
5689 Expr *ToCond = Importer.Import(E->getCond());
5690 if (!ToCond)
5691 return nullptr;
5692
5693 return new (Importer.getToContext()) ConditionalOperator(
5694 ToCond, Importer.Import(E->getQuestionLoc()),
5695 ToLHS, Importer.Import(E->getColonLoc()),
5696 ToRHS, T, E->getValueKind(), E->getObjectKind());
5697}
5698
5699Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
5700 BinaryConditionalOperator *E) {
5701 QualType T = Importer.Import(E->getType());
5702 if (T.isNull())
5703 return nullptr;
5704
5705 Expr *Common = Importer.Import(E->getCommon());
5706 if (!Common)
5707 return nullptr;
5708
5709 Expr *Cond = Importer.Import(E->getCond());
5710 if (!Cond)
5711 return nullptr;
5712
5713 OpaqueValueExpr *OpaqueValue = cast_or_null<OpaqueValueExpr>(
5714 Importer.Import(E->getOpaqueValue()));
5715 if (!OpaqueValue)
5716 return nullptr;
5717
5718 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
5719 if (!TrueExpr)
5720 return nullptr;
5721
5722 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
5723 if (!FalseExpr)
5724 return nullptr;
5725
5726 return new (Importer.getToContext()) BinaryConditionalOperator(
5727 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
5728 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
5729 T, E->getValueKind(), E->getObjectKind());
5730}
5731
5732Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
5733 QualType T = Importer.Import(E->getType());
5734 if (T.isNull())
5735 return nullptr;
5736
5737 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
5738 if (!SourceExpr && E->getSourceExpr())
5739 return nullptr;
5740
5741 return new (Importer.getToContext()) OpaqueValueExpr(
5742 Importer.Import(E->getExprLoc()), T, E->getValueKind(),
5743 E->getObjectKind(), SourceExpr);
5744}
5745
Douglas Gregorc74247e2010-02-19 01:07:06 +00005746Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5747 QualType T = Importer.Import(E->getType());
5748 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005749 return nullptr;
5750
Douglas Gregorc74247e2010-02-19 01:07:06 +00005751 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5752 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005753 return nullptr;
5754
Douglas Gregorc74247e2010-02-19 01:07:06 +00005755 QualType CompResultType = Importer.Import(E->getComputationResultType());
5756 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005757 return nullptr;
5758
Douglas Gregorc74247e2010-02-19 01:07:06 +00005759 Expr *LHS = Importer.Import(E->getLHS());
5760 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005761 return nullptr;
5762
Douglas Gregorc74247e2010-02-19 01:07:06 +00005763 Expr *RHS = Importer.Import(E->getRHS());
5764 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005765 return nullptr;
5766
Douglas Gregorc74247e2010-02-19 01:07:06 +00005767 return new (Importer.getToContext())
5768 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005769 T, E->getValueKind(),
5770 E->getObjectKind(),
5771 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005772 Importer.Import(E->getOperatorLoc()),
5773 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005774}
5775
Benjamin Kramer8aef5962011-03-26 12:38:21 +00005776static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallcf142162010-08-07 06:22:56 +00005777 if (E->path_empty()) return false;
5778
5779 // TODO: import cast paths
5780 return true;
5781}
5782
Douglas Gregor98c10182010-02-12 22:17:39 +00005783Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5784 QualType T = Importer.Import(E->getType());
5785 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005786 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005787
5788 Expr *SubExpr = Importer.Import(E->getSubExpr());
5789 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005790 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005791
5792 CXXCastPath BasePath;
5793 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005794 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005795
5796 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005797 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005798}
5799
Douglas Gregor5481d322010-02-19 01:32:14 +00005800Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
5801 QualType T = Importer.Import(E->getType());
5802 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005803 return nullptr;
5804
Douglas Gregor5481d322010-02-19 01:32:14 +00005805 Expr *SubExpr = Importer.Import(E->getSubExpr());
5806 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005807 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00005808
5809 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5810 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00005811 return nullptr;
5812
John McCallcf142162010-08-07 06:22:56 +00005813 CXXCastPath BasePath;
5814 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005815 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005816
John McCall7decc9e2010-11-18 06:31:45 +00005817 return CStyleCastExpr::Create(Importer.getToContext(), T,
5818 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00005819 SubExpr, &BasePath, TInfo,
5820 Importer.Import(E->getLParenLoc()),
5821 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00005822}
5823
Sean Callanan59721b32015-04-28 18:41:46 +00005824Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
5825 QualType T = Importer.Import(E->getType());
5826 if (T.isNull())
5827 return nullptr;
5828
5829 CXXConstructorDecl *ToCCD =
Sean Callanandd2c1742016-05-16 20:48:03 +00005830 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
Richard Smithc2bebe92016-05-11 20:37:46 +00005831 if (!ToCCD)
Sean Callanan59721b32015-04-28 18:41:46 +00005832 return nullptr;
5833
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005834 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
5835 if (ImportArrayChecked(E->getArgs(), E->getArgs() + E->getNumArgs(),
5836 ToArgs.begin()))
Sean Callanan8bca9962016-03-28 21:43:01 +00005837 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005838
5839 return CXXConstructExpr::Create(Importer.getToContext(), T,
5840 Importer.Import(E->getLocation()),
Richard Smithc83bf822016-06-10 00:58:19 +00005841 ToCCD, E->isElidable(),
Sean Callanan59721b32015-04-28 18:41:46 +00005842 ToArgs, E->hadMultipleCandidates(),
5843 E->isListInitialization(),
5844 E->isStdInitListInitialization(),
5845 E->requiresZeroInitialization(),
5846 E->getConstructionKind(),
5847 Importer.Import(E->getParenOrBraceRange()));
5848}
5849
Sean Callanan8bca9962016-03-28 21:43:01 +00005850Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
5851 QualType T = Importer.Import(E->getType());
5852 if (T.isNull())
5853 return nullptr;
5854
5855 Expr *ToFn = Importer.Import(E->getCallee());
5856 if (!ToFn)
5857 return nullptr;
5858
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005859 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Sean Callanan8bca9962016-03-28 21:43:01 +00005860
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005861 if (ImportArrayChecked(E->arg_begin(), E->arg_end(), ToArgs.begin()))
Sean Callanan8bca9962016-03-28 21:43:01 +00005862 return nullptr;
5863
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005864 return new (Importer.getToContext()) CXXMemberCallExpr(
5865 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
5866 Importer.Import(E->getRParenLoc()));
Sean Callanan8bca9962016-03-28 21:43:01 +00005867}
5868
5869Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
5870 QualType T = Importer.Import(E->getType());
5871 if (T.isNull())
5872 return nullptr;
5873
5874 return new (Importer.getToContext())
5875 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
5876}
5877
5878Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
5879 QualType T = Importer.Import(E->getType());
5880 if (T.isNull())
5881 return nullptr;
5882
5883 return new (Importer.getToContext())
5884 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
5885}
5886
5887
Sean Callanan59721b32015-04-28 18:41:46 +00005888Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
5889 QualType T = Importer.Import(E->getType());
5890 if (T.isNull())
5891 return nullptr;
5892
5893 Expr *ToBase = Importer.Import(E->getBase());
5894 if (!ToBase && E->getBase())
5895 return nullptr;
5896
5897 ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
5898 if (!ToMember && E->getMemberDecl())
5899 return nullptr;
5900
5901 DeclAccessPair ToFoundDecl = DeclAccessPair::make(
5902 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
5903 E->getFoundDecl().getAccess());
5904
5905 DeclarationNameInfo ToMemberNameInfo(
5906 Importer.Import(E->getMemberNameInfo().getName()),
5907 Importer.Import(E->getMemberNameInfo().getLoc()));
5908
5909 if (E->hasExplicitTemplateArgs()) {
5910 return nullptr; // FIXME: handle template arguments
5911 }
5912
5913 return MemberExpr::Create(Importer.getToContext(), ToBase,
5914 E->isArrow(),
5915 Importer.Import(E->getOperatorLoc()),
5916 Importer.Import(E->getQualifierLoc()),
5917 Importer.Import(E->getTemplateKeywordLoc()),
5918 ToMember, ToFoundDecl, ToMemberNameInfo,
5919 nullptr, T, E->getValueKind(),
5920 E->getObjectKind());
5921}
5922
5923Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
5924 QualType T = Importer.Import(E->getType());
5925 if (T.isNull())
5926 return nullptr;
5927
5928 Expr *ToCallee = Importer.Import(E->getCallee());
5929 if (!ToCallee && E->getCallee())
5930 return nullptr;
5931
5932 unsigned NumArgs = E->getNumArgs();
5933
5934 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
5935
5936 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) {
5937 Expr *FromArg = E->getArg(ai);
5938 Expr *ToArg = Importer.Import(FromArg);
5939 if (!ToArg)
5940 return nullptr;
5941 ToArgs[ai] = ToArg;
5942 }
5943
5944 Expr **ToArgs_Copied = new (Importer.getToContext())
5945 Expr*[NumArgs];
5946
5947 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
5948 ToArgs_Copied[ai] = ToArgs[ai];
5949
5950 return new (Importer.getToContext())
5951 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00005952 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00005953 Importer.Import(E->getRParenLoc()));
5954}
5955
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005956Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
5957 QualType T = Importer.Import(ILE->getType());
Sean Callanan8bca9962016-03-28 21:43:01 +00005958 if (T.isNull())
5959 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00005960
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005961 llvm::SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
5962 if (ImportArrayChecked(
5963 ILE->getInits(), ILE->getInits() + ILE->getNumInits(), Exprs.begin()))
Sean Callanan8bca9962016-03-28 21:43:01 +00005964 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00005965
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005966 ASTContext &ToCtx = Importer.getToContext();
5967 InitListExpr *To = new (ToCtx) InitListExpr(
5968 ToCtx, Importer.Import(ILE->getLBraceLoc()),
5969 Exprs, Importer.Import(ILE->getLBraceLoc()));
5970 To->setType(T);
5971
5972 if (ILE->hasArrayFiller()) {
5973 Expr *Filler = Importer.Import(ILE->getArrayFiller());
5974 if (!Filler)
5975 return nullptr;
5976 To->setArrayFiller(Filler);
5977 }
5978
5979 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
5980 FieldDecl *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
5981 if (!ToFD)
5982 return nullptr;
5983 To->setInitializedFieldInUnion(ToFD);
5984 }
5985
5986 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
5987 InitListExpr *ToSyntForm = cast_or_null<InitListExpr>(
5988 Importer.Import(SyntForm));
5989 if (!ToSyntForm)
5990 return nullptr;
5991 To->setSyntacticForm(ToSyntForm);
5992 }
5993
5994 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
5995 To->setValueDependent(ILE->isValueDependent());
5996 To->setInstantiationDependent(ILE->isInstantiationDependent());
5997
5998 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00005999}
6000
Sean Callanandd2c1742016-05-16 20:48:03 +00006001Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
6002 FieldDecl *ToField = llvm::dyn_cast_or_null<FieldDecl>(
6003 Importer.Import(DIE->getField()));
6004 if (!ToField && DIE->getField())
6005 return nullptr;
6006
6007 return CXXDefaultInitExpr::Create(
6008 Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField);
6009}
6010
6011Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
6012 QualType ToType = Importer.Import(E->getType());
6013 if (ToType.isNull() && !E->getType().isNull())
6014 return nullptr;
6015 ExprValueKind VK = E->getValueKind();
6016 CastKind CK = E->getCastKind();
6017 Expr *ToOp = Importer.Import(E->getSubExpr());
6018 if (!ToOp && E->getSubExpr())
6019 return nullptr;
6020 CXXCastPath BasePath;
6021 if (ImportCastPath(E, BasePath))
6022 return nullptr;
6023 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten());
6024 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc());
6025 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc());
6026 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets());
6027
6028 if (isa<CXXStaticCastExpr>(E)) {
6029 return CXXStaticCastExpr::Create(
6030 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6031 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6032 } else if (isa<CXXDynamicCastExpr>(E)) {
6033 return CXXDynamicCastExpr::Create(
6034 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6035 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6036 } else if (isa<CXXReinterpretCastExpr>(E)) {
6037 return CXXReinterpretCastExpr::Create(
6038 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6039 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6040 } else {
6041 return nullptr;
6042 }
6043}
6044
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006045ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00006046 ASTContext &FromContext, FileManager &FromFileManager,
6047 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00006048 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00006049 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Richard Smith5bb4cdf2012-12-20 02:22:15 +00006050 Minimal(MinimalImport), LastDiagFromFrom(false)
Douglas Gregor0a791672011-01-18 03:11:38 +00006051{
Douglas Gregor62d311f2010-02-09 19:21:46 +00006052 ImportedDecls[FromContext.getTranslationUnitDecl()]
6053 = ToContext.getTranslationUnitDecl();
6054}
6055
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +00006056ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00006057
6058QualType ASTImporter::Import(QualType FromT) {
6059 if (FromT.isNull())
6060 return QualType();
John McCall424cec92011-01-19 06:33:43 +00006061
6062 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00006063
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006064 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00006065 llvm::DenseMap<const Type *, const Type *>::iterator Pos
6066 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006067 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00006068 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006069
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006070 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00006071 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00006072 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00006073 if (ToT.isNull())
6074 return ToT;
6075
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006076 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00006077 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006078
John McCall424cec92011-01-19 06:33:43 +00006079 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006080}
6081
Douglas Gregor62d311f2010-02-09 19:21:46 +00006082TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006083 if (!FromTSI)
6084 return FromTSI;
6085
6086 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00006087 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006088 QualType T = Import(FromTSI->getType());
6089 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006090 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006091
6092 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00006093 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00006094}
6095
Sean Callanan59721b32015-04-28 18:41:46 +00006096Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
6097 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
6098 if (Pos != ImportedDecls.end()) {
6099 Decl *ToD = Pos->second;
6100 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
6101 return ToD;
6102 } else {
6103 return nullptr;
6104 }
6105}
6106
Douglas Gregor62d311f2010-02-09 19:21:46 +00006107Decl *ASTImporter::Import(Decl *FromD) {
6108 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00006109 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006110
Douglas Gregord451ea92011-07-29 23:31:30 +00006111 ASTNodeImporter Importer(*this);
6112
Douglas Gregor62d311f2010-02-09 19:21:46 +00006113 // Check whether we've already imported this declaration.
6114 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00006115 if (Pos != ImportedDecls.end()) {
6116 Decl *ToD = Pos->second;
6117 Importer.ImportDefinitionIfNeeded(FromD, ToD);
6118 return ToD;
6119 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00006120
6121 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00006122 Decl *ToD = Importer.Visit(FromD);
6123 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00006124 return nullptr;
6125
Douglas Gregor62d311f2010-02-09 19:21:46 +00006126 // Record the imported declaration.
6127 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00006128
6129 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
6130 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00006131 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00006132 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00006133 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00006134 // When we've finished transforming a typedef, see whether it was the
6135 // typedef for an anonymous tag.
Craig Topper2341c0d2013-07-04 03:08:24 +00006136 for (SmallVectorImpl<TagDecl *>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00006137 FromTag = AnonTagsWithPendingTypedefs.begin(),
6138 FromTagEnd = AnonTagsWithPendingTypedefs.end();
6139 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00006140 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00006141 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
6142 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00006143 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00006144 AnonTagsWithPendingTypedefs.erase(FromTag);
6145 break;
6146 }
6147 }
6148 }
6149 }
6150
Douglas Gregor62d311f2010-02-09 19:21:46 +00006151 return ToD;
6152}
6153
6154DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
6155 if (!FromDC)
6156 return FromDC;
6157
Douglas Gregor95d82832012-01-24 18:36:04 +00006158 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00006159 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00006160 return nullptr;
6161
Douglas Gregor2e15c842012-02-01 21:00:38 +00006162 // When we're using a record/enum/Objective-C class/protocol as a context, we
6163 // need it to have a definition.
6164 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00006165 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006166 if (ToRecord->isCompleteDefinition()) {
6167 // Do nothing.
6168 } else if (FromRecord->isCompleteDefinition()) {
6169 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
6170 ASTNodeImporter::IDK_Basic);
6171 } else {
6172 CompleteDecl(ToRecord);
6173 }
6174 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
6175 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
6176 if (ToEnum->isCompleteDefinition()) {
6177 // Do nothing.
6178 } else if (FromEnum->isCompleteDefinition()) {
6179 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
6180 ASTNodeImporter::IDK_Basic);
6181 } else {
6182 CompleteDecl(ToEnum);
6183 }
6184 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
6185 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
6186 if (ToClass->getDefinition()) {
6187 // Do nothing.
6188 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
6189 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
6190 ASTNodeImporter::IDK_Basic);
6191 } else {
6192 CompleteDecl(ToClass);
6193 }
6194 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
6195 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
6196 if (ToProto->getDefinition()) {
6197 // Do nothing.
6198 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
6199 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
6200 ASTNodeImporter::IDK_Basic);
6201 } else {
6202 CompleteDecl(ToProto);
6203 }
Douglas Gregor95d82832012-01-24 18:36:04 +00006204 }
6205
6206 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006207}
6208
6209Expr *ASTImporter::Import(Expr *FromE) {
6210 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00006211 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006212
6213 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
6214}
6215
6216Stmt *ASTImporter::Import(Stmt *FromS) {
6217 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00006218 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006219
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006220 // Check whether we've already imported this declaration.
6221 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
6222 if (Pos != ImportedStmts.end())
6223 return Pos->second;
6224
6225 // Import the type
6226 ASTNodeImporter Importer(*this);
6227 Stmt *ToS = Importer.Visit(FromS);
6228 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00006229 return nullptr;
6230
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006231 // Record the imported declaration.
6232 ImportedStmts[FromS] = ToS;
6233 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006234}
6235
6236NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
6237 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00006238 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006239
Douglas Gregor90ebf252011-04-27 16:48:40 +00006240 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
6241
6242 switch (FromNNS->getKind()) {
6243 case NestedNameSpecifier::Identifier:
6244 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
6245 return NestedNameSpecifier::Create(ToContext, prefix, II);
6246 }
Craig Topper36250ad2014-05-12 05:36:57 +00006247 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006248
6249 case NestedNameSpecifier::Namespace:
6250 if (NamespaceDecl *NS =
6251 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
6252 return NestedNameSpecifier::Create(ToContext, prefix, NS);
6253 }
Craig Topper36250ad2014-05-12 05:36:57 +00006254 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006255
6256 case NestedNameSpecifier::NamespaceAlias:
6257 if (NamespaceAliasDecl *NSAD =
6258 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
6259 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
6260 }
Craig Topper36250ad2014-05-12 05:36:57 +00006261 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006262
6263 case NestedNameSpecifier::Global:
6264 return NestedNameSpecifier::GlobalSpecifier(ToContext);
6265
Nikola Smiljanic67860242014-09-26 00:28:20 +00006266 case NestedNameSpecifier::Super:
6267 if (CXXRecordDecl *RD =
6268 cast<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
6269 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
6270 }
6271 return nullptr;
6272
Douglas Gregor90ebf252011-04-27 16:48:40 +00006273 case NestedNameSpecifier::TypeSpec:
6274 case NestedNameSpecifier::TypeSpecWithTemplate: {
6275 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
6276 if (!T.isNull()) {
6277 bool bTemplate = FromNNS->getKind() ==
6278 NestedNameSpecifier::TypeSpecWithTemplate;
6279 return NestedNameSpecifier::Create(ToContext, prefix,
6280 bTemplate, T.getTypePtr());
6281 }
6282 }
Craig Topper36250ad2014-05-12 05:36:57 +00006283 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006284 }
6285
6286 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00006287}
6288
Douglas Gregor14454802011-02-25 02:25:35 +00006289NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
6290 // FIXME: Implement!
6291 return NestedNameSpecifierLoc();
6292}
6293
Douglas Gregore2e50d332010-12-01 01:36:18 +00006294TemplateName ASTImporter::Import(TemplateName From) {
6295 switch (From.getKind()) {
6296 case TemplateName::Template:
6297 if (TemplateDecl *ToTemplate
6298 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
6299 return TemplateName(ToTemplate);
6300
6301 return TemplateName();
6302
6303 case TemplateName::OverloadedTemplate: {
6304 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
6305 UnresolvedSet<2> ToTemplates;
6306 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
6307 E = FromStorage->end();
6308 I != E; ++I) {
6309 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
6310 ToTemplates.addDecl(To);
6311 else
6312 return TemplateName();
6313 }
6314 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
6315 ToTemplates.end());
6316 }
6317
6318 case TemplateName::QualifiedTemplate: {
6319 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
6320 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
6321 if (!Qualifier)
6322 return TemplateName();
6323
6324 if (TemplateDecl *ToTemplate
6325 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
6326 return ToContext.getQualifiedTemplateName(Qualifier,
6327 QTN->hasTemplateKeyword(),
6328 ToTemplate);
6329
6330 return TemplateName();
6331 }
6332
6333 case TemplateName::DependentTemplate: {
6334 DependentTemplateName *DTN = From.getAsDependentTemplateName();
6335 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
6336 if (!Qualifier)
6337 return TemplateName();
6338
6339 if (DTN->isIdentifier()) {
6340 return ToContext.getDependentTemplateName(Qualifier,
6341 Import(DTN->getIdentifier()));
6342 }
6343
6344 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
6345 }
John McCalld9dfe3a2011-06-30 08:33:18 +00006346
6347 case TemplateName::SubstTemplateTemplateParm: {
6348 SubstTemplateTemplateParmStorage *subst
6349 = From.getAsSubstTemplateTemplateParm();
6350 TemplateTemplateParmDecl *param
6351 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
6352 if (!param)
6353 return TemplateName();
6354
6355 TemplateName replacement = Import(subst->getReplacement());
6356 if (replacement.isNull()) return TemplateName();
6357
6358 return ToContext.getSubstTemplateTemplateParm(param, replacement);
6359 }
Douglas Gregor5590be02011-01-15 06:45:20 +00006360
6361 case TemplateName::SubstTemplateTemplateParmPack: {
6362 SubstTemplateTemplateParmPackStorage *SubstPack
6363 = From.getAsSubstTemplateTemplateParmPack();
6364 TemplateTemplateParmDecl *Param
6365 = cast_or_null<TemplateTemplateParmDecl>(
6366 Import(SubstPack->getParameterPack()));
6367 if (!Param)
6368 return TemplateName();
6369
6370 ASTNodeImporter Importer(*this);
6371 TemplateArgument ArgPack
6372 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
6373 if (ArgPack.isNull())
6374 return TemplateName();
6375
6376 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
6377 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00006378 }
6379
6380 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00006381}
6382
Douglas Gregor62d311f2010-02-09 19:21:46 +00006383SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
6384 if (FromLoc.isInvalid())
6385 return SourceLocation();
6386
Douglas Gregor811663e2010-02-10 00:15:17 +00006387 SourceManager &FromSM = FromContext.getSourceManager();
6388
6389 // For now, map everything down to its spelling location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00006390 // don't have to import macro expansions.
6391 // FIXME: Import macro expansions!
Douglas Gregor811663e2010-02-10 00:15:17 +00006392 FromLoc = FromSM.getSpellingLoc(FromLoc);
6393 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
6394 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00006395 FileID ToFileID = Import(Decomposed.first);
6396 if (ToFileID.isInvalid())
6397 return SourceLocation();
Sean Callanan59721b32015-04-28 18:41:46 +00006398 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
6399 .getLocWithOffset(Decomposed.second);
6400 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006401}
6402
6403SourceRange ASTImporter::Import(SourceRange FromRange) {
6404 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
6405}
6406
Douglas Gregor811663e2010-02-10 00:15:17 +00006407FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00006408 llvm::DenseMap<FileID, FileID>::iterator Pos
6409 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00006410 if (Pos != ImportedFileIDs.end())
6411 return Pos->second;
6412
6413 SourceManager &FromSM = FromContext.getSourceManager();
6414 SourceManager &ToSM = ToContext.getSourceManager();
6415 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00006416 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00006417
6418 // Include location of this file.
6419 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
6420
6421 // Map the FileID for to the "to" source manager.
6422 FileID ToID;
6423 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan25d34af2015-04-30 00:44:21 +00006424 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
Douglas Gregor811663e2010-02-10 00:15:17 +00006425 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
6426 // disk again
6427 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
6428 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00006429 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Sean Callanan238d8972014-12-10 01:26:39 +00006430 if (!Entry)
6431 return FileID();
Douglas Gregor811663e2010-02-10 00:15:17 +00006432 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
6433 FromSLoc.getFile().getFileCharacteristic());
6434 } else {
6435 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006436 const llvm::MemoryBuffer *
6437 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00006438 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00006439 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00006440 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00006441 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00006442 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00006443 }
6444
6445
Sebastian Redl99219f12010-09-30 01:03:06 +00006446 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00006447 return ToID;
6448}
6449
Sean Callanandd2c1742016-05-16 20:48:03 +00006450CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
6451 Expr *ToExpr = Import(From->getInit());
6452 if (!ToExpr && From->getInit())
6453 return nullptr;
6454
6455 if (From->isBaseInitializer()) {
6456 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
6457 if (!ToTInfo && From->getTypeSourceInfo())
6458 return nullptr;
6459
6460 return new (ToContext) CXXCtorInitializer(
6461 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
6462 ToExpr, Import(From->getRParenLoc()),
6463 From->isPackExpansion() ? Import(From->getEllipsisLoc())
6464 : SourceLocation());
6465 } else if (From->isMemberInitializer()) {
6466 FieldDecl *ToField =
6467 llvm::cast_or_null<FieldDecl>(Import(From->getMember()));
6468 if (!ToField && From->getMember())
6469 return nullptr;
6470
6471 return new (ToContext) CXXCtorInitializer(
6472 ToContext, ToField, Import(From->getMemberLocation()),
6473 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
6474 } else if (From->isIndirectMemberInitializer()) {
6475 IndirectFieldDecl *ToIField = llvm::cast_or_null<IndirectFieldDecl>(
6476 Import(From->getIndirectMember()));
6477 if (!ToIField && From->getIndirectMember())
6478 return nullptr;
6479
6480 return new (ToContext) CXXCtorInitializer(
6481 ToContext, ToIField, Import(From->getMemberLocation()),
6482 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
6483 } else if (From->isDelegatingInitializer()) {
6484 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
6485 if (!ToTInfo && From->getTypeSourceInfo())
6486 return nullptr;
6487
6488 return new (ToContext)
6489 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
6490 ToExpr, Import(From->getRParenLoc()));
6491 } else if (unsigned NumArrayIndices = From->getNumArrayIndices()) {
6492 FieldDecl *ToField =
6493 llvm::cast_or_null<FieldDecl>(Import(From->getMember()));
6494 if (!ToField && From->getMember())
6495 return nullptr;
6496
6497 SmallVector<VarDecl *, 4> ToAIs(NumArrayIndices);
6498
6499 for (unsigned AII = 0; AII < NumArrayIndices; ++AII) {
6500 VarDecl *ToArrayIndex =
6501 dyn_cast_or_null<VarDecl>(Import(From->getArrayIndex(AII)));
6502 if (!ToArrayIndex && From->getArrayIndex(AII))
6503 return nullptr;
6504 }
6505
6506 return CXXCtorInitializer::Create(
6507 ToContext, ToField, Import(From->getMemberLocation()),
6508 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()),
6509 ToAIs.data(), NumArrayIndices);
6510 } else {
6511 return nullptr;
6512 }
6513}
6514
6515
Douglas Gregor0a791672011-01-18 03:11:38 +00006516void ASTImporter::ImportDefinition(Decl *From) {
6517 Decl *To = Import(From);
6518 if (!To)
6519 return;
6520
6521 if (DeclContext *FromDC = cast<DeclContext>(From)) {
6522 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00006523
6524 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
6525 if (!ToRecord->getDefinition()) {
6526 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00006527 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00006528 return;
6529 }
6530 }
Douglas Gregord451ea92011-07-29 23:31:30 +00006531
6532 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
6533 if (!ToEnum->getDefinition()) {
6534 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006535 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00006536 return;
6537 }
6538 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00006539
6540 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
6541 if (!ToIFace->getDefinition()) {
6542 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006543 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00006544 return;
6545 }
6546 }
Douglas Gregord451ea92011-07-29 23:31:30 +00006547
Douglas Gregor2aa53772012-01-24 17:42:07 +00006548 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
6549 if (!ToProto->getDefinition()) {
6550 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006551 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00006552 return;
6553 }
6554 }
6555
Douglas Gregor0a791672011-01-18 03:11:38 +00006556 Importer.ImportDeclContext(FromDC, true);
6557 }
6558}
6559
Douglas Gregor96e578d2010-02-05 17:54:41 +00006560DeclarationName ASTImporter::Import(DeclarationName FromName) {
6561 if (!FromName)
6562 return DeclarationName();
6563
6564 switch (FromName.getNameKind()) {
6565 case DeclarationName::Identifier:
6566 return Import(FromName.getAsIdentifierInfo());
6567
6568 case DeclarationName::ObjCZeroArgSelector:
6569 case DeclarationName::ObjCOneArgSelector:
6570 case DeclarationName::ObjCMultiArgSelector:
6571 return Import(FromName.getObjCSelector());
6572
6573 case DeclarationName::CXXConstructorName: {
6574 QualType T = Import(FromName.getCXXNameType());
6575 if (T.isNull())
6576 return DeclarationName();
6577
6578 return ToContext.DeclarationNames.getCXXConstructorName(
6579 ToContext.getCanonicalType(T));
6580 }
6581
6582 case DeclarationName::CXXDestructorName: {
6583 QualType T = Import(FromName.getCXXNameType());
6584 if (T.isNull())
6585 return DeclarationName();
6586
6587 return ToContext.DeclarationNames.getCXXDestructorName(
6588 ToContext.getCanonicalType(T));
6589 }
6590
6591 case DeclarationName::CXXConversionFunctionName: {
6592 QualType T = Import(FromName.getCXXNameType());
6593 if (T.isNull())
6594 return DeclarationName();
6595
6596 return ToContext.DeclarationNames.getCXXConversionFunctionName(
6597 ToContext.getCanonicalType(T));
6598 }
6599
6600 case DeclarationName::CXXOperatorName:
6601 return ToContext.DeclarationNames.getCXXOperatorName(
6602 FromName.getCXXOverloadedOperator());
6603
6604 case DeclarationName::CXXLiteralOperatorName:
6605 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
6606 Import(FromName.getCXXLiteralIdentifier()));
6607
6608 case DeclarationName::CXXUsingDirective:
6609 // FIXME: STATICS!
6610 return DeclarationName::getUsingDirectiveName();
6611 }
6612
David Blaikiee4d798f2012-01-20 21:50:17 +00006613 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00006614}
6615
Douglas Gregore2e50d332010-12-01 01:36:18 +00006616IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00006617 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00006618 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006619
Sean Callananf94ef1d2016-05-14 06:11:19 +00006620 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
6621
6622 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
6623 ToId->setBuiltinID(FromId->getBuiltinID());
6624
6625 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006626}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006627
Douglas Gregor43f54792010-02-17 02:12:47 +00006628Selector ASTImporter::Import(Selector FromSel) {
6629 if (FromSel.isNull())
6630 return Selector();
6631
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006632 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00006633 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
6634 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
6635 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
6636 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
6637}
6638
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006639DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
6640 DeclContext *DC,
6641 unsigned IDNS,
6642 NamedDecl **Decls,
6643 unsigned NumDecls) {
6644 return Name;
6645}
6646
6647DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00006648 if (LastDiagFromFrom)
6649 ToContext.getDiagnostics().notePriorDiagnosticFrom(
6650 FromContext.getDiagnostics());
6651 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006652 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006653}
6654
6655DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00006656 if (!LastDiagFromFrom)
6657 FromContext.getDiagnostics().notePriorDiagnosticFrom(
6658 ToContext.getDiagnostics());
6659 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006660 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006661}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00006662
Douglas Gregor2e15c842012-02-01 21:00:38 +00006663void ASTImporter::CompleteDecl (Decl *D) {
6664 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
6665 if (!ID->getDefinition())
6666 ID->startDefinition();
6667 }
6668 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
6669 if (!PD->getDefinition())
6670 PD->startDefinition();
6671 }
6672 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
6673 if (!TD->getDefinition() && !TD->isBeingDefined()) {
6674 TD->startDefinition();
6675 TD->setCompleteDefinition(true);
6676 }
6677 }
6678 else {
6679 assert (0 && "CompleteDecl called on a Decl that can't be completed");
6680 }
6681}
6682
Douglas Gregor8cdbe642010-02-12 23:44:20 +00006683Decl *ASTImporter::Imported(Decl *From, Decl *To) {
Sean Callanan8bca9962016-03-28 21:43:01 +00006684 if (From->hasAttrs()) {
6685 for (Attr *FromAttr : From->getAttrs())
6686 To->addAttr(FromAttr->clone(To->getASTContext()));
6687 }
6688 if (From->isUsed()) {
6689 To->setIsUsed();
6690 }
Sean Callanandd2c1742016-05-16 20:48:03 +00006691 if (From->isImplicit()) {
6692 To->setImplicit();
6693 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00006694 ImportedDecls[From] = To;
6695 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00006696}
Douglas Gregorb4964f72010-02-15 23:54:17 +00006697
Douglas Gregordd6006f2012-07-17 21:16:27 +00006698bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
6699 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00006700 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00006701 = ImportedTypes.find(From.getTypePtr());
6702 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
6703 return true;
6704
Douglas Gregordd6006f2012-07-17 21:16:27 +00006705 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
6706 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00006707 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00006708}