blob: 3598dccf0a18fdbc8b7e6b6ad82b67f00fcfe497 [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);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000255
256 template<typename IIter, typename OIter>
257 void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) {
258 typedef typename std::remove_reference<decltype(*Obegin)>::type ItemT;
259 ASTImporter &ImporterRef = Importer;
260 std::transform(Ibegin, Iend, Obegin,
261 [&ImporterRef](ItemT From) -> ItemT {
262 return ImporterRef.Import(From);
Sean Callanan8bca9962016-03-28 21:43:01 +0000263 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000264 }
265
266 template<typename IIter, typename OIter>
267 bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
268 typedef typename std::remove_reference<decltype(**Obegin)>::type ItemT;
269 ASTImporter &ImporterRef = Importer;
270 bool Failed = false;
271 std::transform(Ibegin, Iend, Obegin,
272 [&ImporterRef, &Failed](ItemT *From) -> ItemT * {
273 ItemT *To = ImporterRef.Import(From);
274 if (!To && From)
275 Failed = true;
276 return To;
277 });
278 return Failed;
Sean Callanan8bca9962016-03-28 21:43:01 +0000279 }
Douglas Gregor96e578d2010-02-05 17:54:41 +0000280 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000281}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000282
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000283using namespace clang;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000284
285//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000286// Structural Equivalence
287//----------------------------------------------------------------------------
288
289namespace {
290 struct StructuralEquivalenceContext {
291 /// \brief AST contexts for which we are checking structural equivalence.
292 ASTContext &C1, &C2;
293
Douglas Gregor3996e242010-02-15 22:01:00 +0000294 /// \brief The set of "tentative" equivalences between two canonical
295 /// declarations, mapping from a declaration in the first context to the
296 /// declaration in the second context that we believe to be equivalent.
297 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
298
299 /// \brief Queue of declarations in the first context whose equivalence
300 /// with a declaration in the second context still needs to be verified.
301 std::deque<Decl *> DeclsToCheck;
302
Douglas Gregorb4964f72010-02-15 23:54:17 +0000303 /// \brief Declaration (from, to) pairs that are known not to be equivalent
304 /// (which we have already complained about).
305 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
306
Douglas Gregor3996e242010-02-15 22:01:00 +0000307 /// \brief Whether we're being strict about the spelling of types when
308 /// unifying two types.
309 bool StrictTypeSpelling;
Douglas Gregordd6006f2012-07-17 21:16:27 +0000310
311 /// \brief Whether to complain about failures.
312 bool Complain;
313
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000314 /// \brief \c true if the last diagnostic came from C2.
315 bool LastDiagFromC2;
316
Douglas Gregor3996e242010-02-15 22:01:00 +0000317 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000318 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregordd6006f2012-07-17 21:16:27 +0000319 bool StrictTypeSpelling = false,
320 bool Complain = true)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000321 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000322 StrictTypeSpelling(StrictTypeSpelling), Complain(Complain),
323 LastDiagFromC2(false) {}
Douglas Gregor3996e242010-02-15 22:01:00 +0000324
325 /// \brief Determine whether the two declarations are structurally
326 /// equivalent.
327 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
328
329 /// \brief Determine whether the two types are structurally equivalent.
330 bool IsStructurallyEquivalent(QualType T1, QualType T2);
331
332 private:
333 /// \brief Finish checking all of the structural equivalences.
334 ///
335 /// \returns true if an error occurred, false otherwise.
336 bool Finish();
337
338 public:
339 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000340 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000341 if (LastDiagFromC2)
342 C1.getDiagnostics().notePriorDiagnosticFrom(C2.getDiagnostics());
343 LastDiagFromC2 = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000344 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000345 }
346
347 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000348 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000349 if (!LastDiagFromC2)
350 C2.getDiagnostics().notePriorDiagnosticFrom(C1.getDiagnostics());
351 LastDiagFromC2 = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000352 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000353 }
354 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000355}
Douglas Gregor3996e242010-02-15 22:01:00 +0000356
357static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
358 QualType T1, QualType T2);
359static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
360 Decl *D1, Decl *D2);
361
Douglas Gregor3996e242010-02-15 22:01:00 +0000362/// \brief Determine structural equivalence of two expressions.
363static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
364 Expr *E1, Expr *E2) {
365 if (!E1 || !E2)
366 return E1 == E2;
367
368 // FIXME: Actually perform a structural comparison!
369 return true;
370}
371
372/// \brief Determine whether two identifiers are equivalent.
373static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
374 const IdentifierInfo *Name2) {
375 if (!Name1 || !Name2)
376 return Name1 == Name2;
377
378 return Name1->getName() == Name2->getName();
379}
380
381/// \brief Determine whether two nested-name-specifiers are equivalent.
382static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
383 NestedNameSpecifier *NNS1,
384 NestedNameSpecifier *NNS2) {
385 // FIXME: Implement!
386 return true;
387}
388
389/// \brief Determine whether two template arguments are equivalent.
390static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
391 const TemplateArgument &Arg1,
392 const TemplateArgument &Arg2) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000393 if (Arg1.getKind() != Arg2.getKind())
394 return false;
395
396 switch (Arg1.getKind()) {
397 case TemplateArgument::Null:
398 return true;
399
400 case TemplateArgument::Type:
401 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
Eli Friedmanb826a002012-09-26 02:36:12 +0000402
Douglas Gregore2e50d332010-12-01 01:36:18 +0000403 case TemplateArgument::Integral:
404 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
405 Arg2.getIntegralType()))
406 return false;
407
Eric Christopher6dcc3762012-07-15 00:23:57 +0000408 return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral());
Douglas Gregore2e50d332010-12-01 01:36:18 +0000409
410 case TemplateArgument::Declaration:
411 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +0000412
413 case TemplateArgument::NullPtr:
414 return true; // FIXME: Is this correct?
415
Douglas Gregore2e50d332010-12-01 01:36:18 +0000416 case TemplateArgument::Template:
417 return IsStructurallyEquivalent(Context,
418 Arg1.getAsTemplate(),
419 Arg2.getAsTemplate());
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000420
421 case TemplateArgument::TemplateExpansion:
422 return IsStructurallyEquivalent(Context,
423 Arg1.getAsTemplateOrTemplatePattern(),
424 Arg2.getAsTemplateOrTemplatePattern());
425
Douglas Gregore2e50d332010-12-01 01:36:18 +0000426 case TemplateArgument::Expression:
427 return IsStructurallyEquivalent(Context,
428 Arg1.getAsExpr(), Arg2.getAsExpr());
429
430 case TemplateArgument::Pack:
431 if (Arg1.pack_size() != Arg2.pack_size())
432 return false;
433
434 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
435 if (!IsStructurallyEquivalent(Context,
436 Arg1.pack_begin()[I],
437 Arg2.pack_begin()[I]))
438 return false;
439
440 return true;
441 }
442
443 llvm_unreachable("Invalid template argument kind");
Douglas Gregor3996e242010-02-15 22:01:00 +0000444}
445
446/// \brief Determine structural equivalence for the common part of array
447/// types.
448static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
449 const ArrayType *Array1,
450 const ArrayType *Array2) {
451 if (!IsStructurallyEquivalent(Context,
452 Array1->getElementType(),
453 Array2->getElementType()))
454 return false;
455 if (Array1->getSizeModifier() != Array2->getSizeModifier())
456 return false;
457 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
458 return false;
459
460 return true;
461}
462
463/// \brief Determine structural equivalence of two types.
464static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
465 QualType T1, QualType T2) {
466 if (T1.isNull() || T2.isNull())
467 return T1.isNull() && T2.isNull();
468
469 if (!Context.StrictTypeSpelling) {
470 // We aren't being strict about token-to-token equivalence of types,
471 // so map down to the canonical type.
472 T1 = Context.C1.getCanonicalType(T1);
473 T2 = Context.C2.getCanonicalType(T2);
474 }
475
476 if (T1.getQualifiers() != T2.getQualifiers())
477 return false;
478
Douglas Gregorb4964f72010-02-15 23:54:17 +0000479 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000480
Douglas Gregorb4964f72010-02-15 23:54:17 +0000481 if (T1->getTypeClass() != T2->getTypeClass()) {
482 // Compare function types with prototypes vs. without prototypes as if
483 // both did not have prototypes.
484 if (T1->getTypeClass() == Type::FunctionProto &&
485 T2->getTypeClass() == Type::FunctionNoProto)
486 TC = Type::FunctionNoProto;
487 else if (T1->getTypeClass() == Type::FunctionNoProto &&
488 T2->getTypeClass() == Type::FunctionProto)
489 TC = Type::FunctionNoProto;
490 else
491 return false;
492 }
493
494 switch (TC) {
495 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000496 // FIXME: Deal with Char_S/Char_U.
497 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
498 return false;
499 break;
500
501 case Type::Complex:
502 if (!IsStructurallyEquivalent(Context,
503 cast<ComplexType>(T1)->getElementType(),
504 cast<ComplexType>(T2)->getElementType()))
505 return false;
506 break;
507
Reid Kleckner0503a872013-12-05 01:23:43 +0000508 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +0000509 case Type::Decayed:
510 if (!IsStructurallyEquivalent(Context,
Reid Kleckner0503a872013-12-05 01:23:43 +0000511 cast<AdjustedType>(T1)->getOriginalType(),
512 cast<AdjustedType>(T2)->getOriginalType()))
Reid Kleckner8a365022013-06-24 17:51:48 +0000513 return false;
514 break;
515
Douglas Gregor3996e242010-02-15 22:01:00 +0000516 case Type::Pointer:
517 if (!IsStructurallyEquivalent(Context,
518 cast<PointerType>(T1)->getPointeeType(),
519 cast<PointerType>(T2)->getPointeeType()))
520 return false;
521 break;
522
523 case Type::BlockPointer:
524 if (!IsStructurallyEquivalent(Context,
525 cast<BlockPointerType>(T1)->getPointeeType(),
526 cast<BlockPointerType>(T2)->getPointeeType()))
527 return false;
528 break;
529
530 case Type::LValueReference:
531 case Type::RValueReference: {
532 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
533 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
534 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
535 return false;
536 if (Ref1->isInnerRef() != Ref2->isInnerRef())
537 return false;
538 if (!IsStructurallyEquivalent(Context,
539 Ref1->getPointeeTypeAsWritten(),
540 Ref2->getPointeeTypeAsWritten()))
541 return false;
542 break;
543 }
544
545 case Type::MemberPointer: {
546 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
547 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
548 if (!IsStructurallyEquivalent(Context,
549 MemPtr1->getPointeeType(),
550 MemPtr2->getPointeeType()))
551 return false;
552 if (!IsStructurallyEquivalent(Context,
553 QualType(MemPtr1->getClass(), 0),
554 QualType(MemPtr2->getClass(), 0)))
555 return false;
556 break;
557 }
558
559 case Type::ConstantArray: {
560 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
561 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
Eric Christopher6dcc3762012-07-15 00:23:57 +0000562 if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000563 return false;
564
565 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
566 return false;
567 break;
568 }
569
570 case Type::IncompleteArray:
571 if (!IsArrayStructurallyEquivalent(Context,
572 cast<ArrayType>(T1),
573 cast<ArrayType>(T2)))
574 return false;
575 break;
576
577 case Type::VariableArray: {
578 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
579 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
580 if (!IsStructurallyEquivalent(Context,
581 Array1->getSizeExpr(), Array2->getSizeExpr()))
582 return false;
583
584 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
585 return false;
586
587 break;
588 }
589
590 case Type::DependentSizedArray: {
591 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
592 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
593 if (!IsStructurallyEquivalent(Context,
594 Array1->getSizeExpr(), Array2->getSizeExpr()))
595 return false;
596
597 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
598 return false;
599
600 break;
601 }
602
603 case Type::DependentSizedExtVector: {
604 const DependentSizedExtVectorType *Vec1
605 = cast<DependentSizedExtVectorType>(T1);
606 const DependentSizedExtVectorType *Vec2
607 = cast<DependentSizedExtVectorType>(T2);
608 if (!IsStructurallyEquivalent(Context,
609 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
610 return false;
611 if (!IsStructurallyEquivalent(Context,
612 Vec1->getElementType(),
613 Vec2->getElementType()))
614 return false;
615 break;
616 }
617
618 case Type::Vector:
619 case Type::ExtVector: {
620 const VectorType *Vec1 = cast<VectorType>(T1);
621 const VectorType *Vec2 = cast<VectorType>(T2);
622 if (!IsStructurallyEquivalent(Context,
623 Vec1->getElementType(),
624 Vec2->getElementType()))
625 return false;
626 if (Vec1->getNumElements() != Vec2->getNumElements())
627 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000628 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000629 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000630 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000631 }
632
633 case Type::FunctionProto: {
634 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
635 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
Alp Toker9cacbab2014-01-20 20:26:09 +0000636 if (Proto1->getNumParams() != Proto2->getNumParams())
Douglas Gregor3996e242010-02-15 22:01:00 +0000637 return false;
Alp Toker9cacbab2014-01-20 20:26:09 +0000638 for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) {
639 if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I),
640 Proto2->getParamType(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000641 return false;
642 }
643 if (Proto1->isVariadic() != Proto2->isVariadic())
644 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000645 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor3996e242010-02-15 22:01:00 +0000646 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000647 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
648 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
649 return false;
650 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
651 if (!IsStructurallyEquivalent(Context,
652 Proto1->getExceptionType(I),
653 Proto2->getExceptionType(I)))
654 return false;
655 }
656 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000657 if (!IsStructurallyEquivalent(Context,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000658 Proto1->getNoexceptExpr(),
659 Proto2->getNoexceptExpr()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000660 return false;
661 }
662 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
663 return false;
664
665 // Fall through to check the bits common with FunctionNoProtoType.
666 }
667
668 case Type::FunctionNoProto: {
669 const FunctionType *Function1 = cast<FunctionType>(T1);
670 const FunctionType *Function2 = cast<FunctionType>(T2);
Alp Toker314cc812014-01-25 16:55:45 +0000671 if (!IsStructurallyEquivalent(Context, Function1->getReturnType(),
672 Function2->getReturnType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000673 return false;
Justin Bogner62c04de2016-03-20 16:58:03 +0000674 if (Function1->getExtInfo() != Function2->getExtInfo())
675 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000676 break;
677 }
678
679 case Type::UnresolvedUsing:
680 if (!IsStructurallyEquivalent(Context,
681 cast<UnresolvedUsingType>(T1)->getDecl(),
682 cast<UnresolvedUsingType>(T2)->getDecl()))
683 return false;
684
685 break;
John McCall81904512011-01-06 01:58:22 +0000686
687 case Type::Attributed:
688 if (!IsStructurallyEquivalent(Context,
689 cast<AttributedType>(T1)->getModifiedType(),
690 cast<AttributedType>(T2)->getModifiedType()))
691 return false;
692 if (!IsStructurallyEquivalent(Context,
693 cast<AttributedType>(T1)->getEquivalentType(),
694 cast<AttributedType>(T2)->getEquivalentType()))
695 return false;
696 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000697
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000698 case Type::Paren:
699 if (!IsStructurallyEquivalent(Context,
700 cast<ParenType>(T1)->getInnerType(),
701 cast<ParenType>(T2)->getInnerType()))
702 return false;
703 break;
704
Douglas Gregor3996e242010-02-15 22:01:00 +0000705 case Type::Typedef:
706 if (!IsStructurallyEquivalent(Context,
707 cast<TypedefType>(T1)->getDecl(),
708 cast<TypedefType>(T2)->getDecl()))
709 return false;
710 break;
711
712 case Type::TypeOfExpr:
713 if (!IsStructurallyEquivalent(Context,
714 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
715 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
716 return false;
717 break;
718
719 case Type::TypeOf:
720 if (!IsStructurallyEquivalent(Context,
721 cast<TypeOfType>(T1)->getUnderlyingType(),
722 cast<TypeOfType>(T2)->getUnderlyingType()))
723 return false;
724 break;
Alexis Hunte852b102011-05-24 22:41:36 +0000725
726 case Type::UnaryTransform:
727 if (!IsStructurallyEquivalent(Context,
728 cast<UnaryTransformType>(T1)->getUnderlyingType(),
729 cast<UnaryTransformType>(T1)->getUnderlyingType()))
730 return false;
731 break;
732
Douglas Gregor3996e242010-02-15 22:01:00 +0000733 case Type::Decltype:
734 if (!IsStructurallyEquivalent(Context,
735 cast<DecltypeType>(T1)->getUnderlyingExpr(),
736 cast<DecltypeType>(T2)->getUnderlyingExpr()))
737 return false;
738 break;
739
Richard Smith30482bc2011-02-20 03:19:35 +0000740 case Type::Auto:
741 if (!IsStructurallyEquivalent(Context,
742 cast<AutoType>(T1)->getDeducedType(),
743 cast<AutoType>(T2)->getDeducedType()))
744 return false;
745 break;
746
Douglas Gregor3996e242010-02-15 22:01:00 +0000747 case Type::Record:
748 case Type::Enum:
749 if (!IsStructurallyEquivalent(Context,
750 cast<TagType>(T1)->getDecl(),
751 cast<TagType>(T2)->getDecl()))
752 return false;
753 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000754
Douglas Gregor3996e242010-02-15 22:01:00 +0000755 case Type::TemplateTypeParm: {
756 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
757 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
758 if (Parm1->getDepth() != Parm2->getDepth())
759 return false;
760 if (Parm1->getIndex() != Parm2->getIndex())
761 return false;
762 if (Parm1->isParameterPack() != Parm2->isParameterPack())
763 return false;
764
765 // Names of template type parameters are never significant.
766 break;
767 }
768
769 case Type::SubstTemplateTypeParm: {
770 const SubstTemplateTypeParmType *Subst1
771 = cast<SubstTemplateTypeParmType>(T1);
772 const SubstTemplateTypeParmType *Subst2
773 = cast<SubstTemplateTypeParmType>(T2);
774 if (!IsStructurallyEquivalent(Context,
775 QualType(Subst1->getReplacedParameter(), 0),
776 QualType(Subst2->getReplacedParameter(), 0)))
777 return false;
778 if (!IsStructurallyEquivalent(Context,
779 Subst1->getReplacementType(),
780 Subst2->getReplacementType()))
781 return false;
782 break;
783 }
784
Douglas Gregorfb322d82011-01-14 05:11:40 +0000785 case Type::SubstTemplateTypeParmPack: {
786 const SubstTemplateTypeParmPackType *Subst1
787 = cast<SubstTemplateTypeParmPackType>(T1);
788 const SubstTemplateTypeParmPackType *Subst2
789 = cast<SubstTemplateTypeParmPackType>(T2);
790 if (!IsStructurallyEquivalent(Context,
791 QualType(Subst1->getReplacedParameter(), 0),
792 QualType(Subst2->getReplacedParameter(), 0)))
793 return false;
794 if (!IsStructurallyEquivalent(Context,
795 Subst1->getArgumentPack(),
796 Subst2->getArgumentPack()))
797 return false;
798 break;
799 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000800 case Type::TemplateSpecialization: {
801 const TemplateSpecializationType *Spec1
802 = cast<TemplateSpecializationType>(T1);
803 const TemplateSpecializationType *Spec2
804 = cast<TemplateSpecializationType>(T2);
805 if (!IsStructurallyEquivalent(Context,
806 Spec1->getTemplateName(),
807 Spec2->getTemplateName()))
808 return false;
809 if (Spec1->getNumArgs() != Spec2->getNumArgs())
810 return false;
811 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
812 if (!IsStructurallyEquivalent(Context,
813 Spec1->getArg(I), Spec2->getArg(I)))
814 return false;
815 }
816 break;
817 }
818
Abramo Bagnara6150c882010-05-11 21:36:43 +0000819 case Type::Elaborated: {
820 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
821 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
822 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
823 if (Elab1->getKeyword() != Elab2->getKeyword())
824 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000825 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000826 Elab1->getQualifier(),
827 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000828 return false;
829 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000830 Elab1->getNamedType(),
831 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000832 return false;
833 break;
834 }
835
John McCalle78aac42010-03-10 03:28:59 +0000836 case Type::InjectedClassName: {
837 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
838 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
839 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000840 Inj1->getInjectedSpecializationType(),
841 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000842 return false;
843 break;
844 }
845
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000846 case Type::DependentName: {
847 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
848 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000849 if (!IsStructurallyEquivalent(Context,
850 Typename1->getQualifier(),
851 Typename2->getQualifier()))
852 return false;
853 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
854 Typename2->getIdentifier()))
855 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000856
857 break;
858 }
859
John McCallc392f372010-06-11 00:33:02 +0000860 case Type::DependentTemplateSpecialization: {
861 const DependentTemplateSpecializationType *Spec1 =
862 cast<DependentTemplateSpecializationType>(T1);
863 const DependentTemplateSpecializationType *Spec2 =
864 cast<DependentTemplateSpecializationType>(T2);
865 if (!IsStructurallyEquivalent(Context,
866 Spec1->getQualifier(),
867 Spec2->getQualifier()))
868 return false;
869 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
870 Spec2->getIdentifier()))
871 return false;
872 if (Spec1->getNumArgs() != Spec2->getNumArgs())
873 return false;
874 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
875 if (!IsStructurallyEquivalent(Context,
876 Spec1->getArg(I), Spec2->getArg(I)))
877 return false;
878 }
879 break;
880 }
Douglas Gregord2fa7662010-12-20 02:24:11 +0000881
882 case Type::PackExpansion:
883 if (!IsStructurallyEquivalent(Context,
884 cast<PackExpansionType>(T1)->getPattern(),
885 cast<PackExpansionType>(T2)->getPattern()))
886 return false;
887 break;
888
Douglas Gregor3996e242010-02-15 22:01:00 +0000889 case Type::ObjCInterface: {
890 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
891 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
892 if (!IsStructurallyEquivalent(Context,
893 Iface1->getDecl(), Iface2->getDecl()))
894 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000895 break;
896 }
897
898 case Type::ObjCObject: {
899 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
900 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
901 if (!IsStructurallyEquivalent(Context,
902 Obj1->getBaseType(),
903 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000904 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000905 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
906 return false;
907 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000908 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000909 Obj1->getProtocol(I),
910 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000911 return false;
912 }
913 break;
914 }
915
916 case Type::ObjCObjectPointer: {
917 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
918 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
919 if (!IsStructurallyEquivalent(Context,
920 Ptr1->getPointeeType(),
921 Ptr2->getPointeeType()))
922 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000923 break;
924 }
Eli Friedman0dfb8892011-10-06 23:00:33 +0000925
926 case Type::Atomic: {
927 if (!IsStructurallyEquivalent(Context,
928 cast<AtomicType>(T1)->getValueType(),
929 cast<AtomicType>(T2)->getValueType()))
930 return false;
931 break;
932 }
933
Xiuli Pan9c14e282016-01-09 12:53:17 +0000934 case Type::Pipe: {
935 if (!IsStructurallyEquivalent(Context,
936 cast<PipeType>(T1)->getElementType(),
937 cast<PipeType>(T2)->getElementType()))
938 return false;
939 break;
940 }
941
Douglas Gregor3996e242010-02-15 22:01:00 +0000942 } // end switch
943
944 return true;
945}
946
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000947/// \brief Determine structural equivalence of two fields.
948static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
949 FieldDecl *Field1, FieldDecl *Field2) {
950 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000951
952 // For anonymous structs/unions, match up the anonymous struct/union type
953 // declarations directly, so that we don't go off searching for anonymous
954 // types
955 if (Field1->isAnonymousStructOrUnion() &&
956 Field2->isAnonymousStructOrUnion()) {
957 RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl();
958 RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl();
959 return IsStructurallyEquivalent(Context, D1, D2);
960 }
Sean Callanan969c5bd2013-04-26 22:49:25 +0000961
962 // Check for equivalent field names.
963 IdentifierInfo *Name1 = Field1->getIdentifier();
964 IdentifierInfo *Name2 = Field2->getIdentifier();
965 if (!::IsStructurallyEquivalent(Name1, Name2))
966 return false;
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000967
968 if (!IsStructurallyEquivalent(Context,
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000969 Field1->getType(), Field2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000970 if (Context.Complain) {
971 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
972 << Context.C2.getTypeDeclType(Owner2);
973 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
974 << Field2->getDeclName() << Field2->getType();
975 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
976 << Field1->getDeclName() << Field1->getType();
977 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000978 return false;
979 }
980
981 if (Field1->isBitField() != Field2->isBitField()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000982 if (Context.Complain) {
983 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
984 << Context.C2.getTypeDeclType(Owner2);
985 if (Field1->isBitField()) {
986 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
987 << Field1->getDeclName() << Field1->getType()
988 << Field1->getBitWidthValue(Context.C1);
989 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
990 << Field2->getDeclName();
991 } else {
992 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
993 << Field2->getDeclName() << Field2->getType()
994 << Field2->getBitWidthValue(Context.C2);
995 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
996 << Field1->getDeclName();
997 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000998 }
999 return false;
1000 }
1001
1002 if (Field1->isBitField()) {
1003 // Make sure that the bit-fields are the same length.
1004 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
1005 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
1006
1007 if (Bits1 != Bits2) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001008 if (Context.Complain) {
1009 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1010 << Context.C2.getTypeDeclType(Owner2);
1011 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
1012 << Field2->getDeclName() << Field2->getType() << Bits2;
1013 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
1014 << Field1->getDeclName() << Field1->getType() << Bits1;
1015 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001016 return false;
1017 }
1018 }
1019
1020 return true;
1021}
1022
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001023/// \brief Find the index of the given anonymous struct/union within its
1024/// context.
1025///
1026/// \returns Returns the index of this anonymous struct/union in its context,
1027/// including the next assigned index (if none of them match). Returns an
1028/// empty option if the context is not a record, i.e.. if the anonymous
1029/// struct/union is at namespace or block scope.
David Blaikie05785d12013-02-20 22:23:23 +00001030static Optional<unsigned> findAnonymousStructOrUnionIndex(RecordDecl *Anon) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001031 ASTContext &Context = Anon->getASTContext();
1032 QualType AnonTy = Context.getRecordType(Anon);
1033
1034 RecordDecl *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext());
1035 if (!Owner)
David Blaikie7a30dc52013-02-21 01:47:18 +00001036 return None;
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001037
1038 unsigned Index = 0;
Aaron Ballman629afae2014-03-07 19:56:05 +00001039 for (const auto *D : Owner->noload_decls()) {
1040 const auto *F = dyn_cast<FieldDecl>(D);
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001041 if (!F || !F->isAnonymousStructOrUnion())
1042 continue;
1043
1044 if (Context.hasSameType(F->getType(), AnonTy))
1045 break;
1046
1047 ++Index;
1048 }
1049
1050 return Index;
1051}
1052
Douglas Gregor3996e242010-02-15 22:01:00 +00001053/// \brief Determine structural equivalence of two records.
1054static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1055 RecordDecl *D1, RecordDecl *D2) {
1056 if (D1->isUnion() != D2->isUnion()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001057 if (Context.Complain) {
1058 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1059 << Context.C2.getTypeDeclType(D2);
1060 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
1061 << D1->getDeclName() << (unsigned)D1->getTagKind();
1062 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001063 return false;
1064 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001065
1066 if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) {
1067 // If both anonymous structs/unions are in a record context, make sure
1068 // they occur in the same location in the context records.
David Blaikie05785d12013-02-20 22:23:23 +00001069 if (Optional<unsigned> Index1 = findAnonymousStructOrUnionIndex(D1)) {
1070 if (Optional<unsigned> Index2 = findAnonymousStructOrUnionIndex(D2)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001071 if (*Index1 != *Index2)
1072 return false;
1073 }
1074 }
1075 }
1076
Douglas Gregore2e50d332010-12-01 01:36:18 +00001077 // If both declarations are class template specializations, we know
1078 // the ODR applies, so check the template and template arguments.
1079 ClassTemplateSpecializationDecl *Spec1
1080 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
1081 ClassTemplateSpecializationDecl *Spec2
1082 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
1083 if (Spec1 && Spec2) {
1084 // Check that the specialized templates are the same.
1085 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
1086 Spec2->getSpecializedTemplate()))
1087 return false;
1088
1089 // Check that the template arguments are the same.
1090 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
1091 return false;
1092
1093 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
1094 if (!IsStructurallyEquivalent(Context,
1095 Spec1->getTemplateArgs().get(I),
1096 Spec2->getTemplateArgs().get(I)))
1097 return false;
1098 }
1099 // If one is a class template specialization and the other is not, these
Chris Lattner57540c52011-04-15 05:22:18 +00001100 // structures are different.
Douglas Gregore2e50d332010-12-01 01:36:18 +00001101 else if (Spec1 || Spec2)
1102 return false;
1103
Douglas Gregorb4964f72010-02-15 23:54:17 +00001104 // Compare the definitions of these two records. If either or both are
1105 // incomplete, we assume that they are equivalent.
1106 D1 = D1->getDefinition();
1107 D2 = D2->getDefinition();
1108 if (!D1 || !D2)
1109 return true;
1110
Douglas Gregor3996e242010-02-15 22:01:00 +00001111 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
1112 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
1113 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001114 if (Context.Complain) {
1115 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1116 << Context.C2.getTypeDeclType(D2);
1117 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
1118 << D2CXX->getNumBases();
1119 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
1120 << D1CXX->getNumBases();
1121 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001122 return false;
1123 }
1124
1125 // Check the base classes.
1126 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
1127 BaseEnd1 = D1CXX->bases_end(),
1128 Base2 = D2CXX->bases_begin();
1129 Base1 != BaseEnd1;
1130 ++Base1, ++Base2) {
1131 if (!IsStructurallyEquivalent(Context,
1132 Base1->getType(), Base2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001133 if (Context.Complain) {
1134 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1135 << Context.C2.getTypeDeclType(D2);
1136 Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
1137 << Base2->getType()
1138 << Base2->getSourceRange();
1139 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1140 << Base1->getType()
1141 << Base1->getSourceRange();
1142 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001143 return false;
1144 }
1145
1146 // Check virtual vs. non-virtual inheritance mismatch.
1147 if (Base1->isVirtual() != Base2->isVirtual()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001148 if (Context.Complain) {
1149 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1150 << Context.C2.getTypeDeclType(D2);
1151 Context.Diag2(Base2->getLocStart(),
1152 diag::note_odr_virtual_base)
1153 << Base2->isVirtual() << Base2->getSourceRange();
1154 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1155 << Base1->isVirtual()
1156 << Base1->getSourceRange();
1157 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001158 return false;
1159 }
1160 }
1161 } else if (D1CXX->getNumBases() > 0) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001162 if (Context.Complain) {
1163 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1164 << Context.C2.getTypeDeclType(D2);
1165 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
1166 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1167 << Base1->getType()
1168 << Base1->getSourceRange();
1169 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
1170 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001171 return false;
1172 }
1173 }
1174
1175 // Check the fields for consistency.
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001176 RecordDecl::field_iterator Field2 = D2->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001177 Field2End = D2->field_end();
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001178 for (RecordDecl::field_iterator Field1 = D1->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001179 Field1End = D1->field_end();
1180 Field1 != Field1End;
1181 ++Field1, ++Field2) {
1182 if (Field2 == Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001183 if (Context.Complain) {
1184 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1185 << Context.C2.getTypeDeclType(D2);
1186 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
1187 << Field1->getDeclName() << Field1->getType();
1188 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
1189 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001190 return false;
1191 }
1192
David Blaikie40ed2972012-06-06 20:45:41 +00001193 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001194 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001195 }
1196
1197 if (Field2 != Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001198 if (Context.Complain) {
1199 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1200 << Context.C2.getTypeDeclType(D2);
1201 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1202 << Field2->getDeclName() << Field2->getType();
1203 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1204 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001205 return false;
1206 }
1207
1208 return true;
1209}
1210
1211/// \brief Determine structural equivalence of two enums.
1212static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1213 EnumDecl *D1, EnumDecl *D2) {
1214 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1215 EC2End = D2->enumerator_end();
1216 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1217 EC1End = D1->enumerator_end();
1218 EC1 != EC1End; ++EC1, ++EC2) {
1219 if (EC2 == EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001220 if (Context.Complain) {
1221 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1222 << Context.C2.getTypeDeclType(D2);
1223 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1224 << EC1->getDeclName()
1225 << EC1->getInitVal().toString(10);
1226 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1227 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001228 return false;
1229 }
1230
1231 llvm::APSInt Val1 = EC1->getInitVal();
1232 llvm::APSInt Val2 = EC2->getInitVal();
Eric Christopher6dcc3762012-07-15 00:23:57 +00001233 if (!llvm::APSInt::isSameValue(Val1, Val2) ||
Douglas Gregor3996e242010-02-15 22:01:00 +00001234 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001235 if (Context.Complain) {
1236 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1237 << Context.C2.getTypeDeclType(D2);
1238 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1239 << EC2->getDeclName()
1240 << EC2->getInitVal().toString(10);
1241 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1242 << EC1->getDeclName()
1243 << EC1->getInitVal().toString(10);
1244 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001245 return false;
1246 }
1247 }
1248
1249 if (EC2 != EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001250 if (Context.Complain) {
1251 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1252 << Context.C2.getTypeDeclType(D2);
1253 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1254 << EC2->getDeclName()
1255 << EC2->getInitVal().toString(10);
1256 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1257 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001258 return false;
1259 }
1260
1261 return true;
1262}
Douglas Gregora082a492010-11-30 19:14:50 +00001263
1264static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1265 TemplateParameterList *Params1,
1266 TemplateParameterList *Params2) {
1267 if (Params1->size() != Params2->size()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001268 if (Context.Complain) {
1269 Context.Diag2(Params2->getTemplateLoc(),
1270 diag::err_odr_different_num_template_parameters)
1271 << Params1->size() << Params2->size();
1272 Context.Diag1(Params1->getTemplateLoc(),
1273 diag::note_odr_template_parameter_list);
1274 }
Douglas Gregora082a492010-11-30 19:14:50 +00001275 return false;
1276 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001277
Douglas Gregora082a492010-11-30 19:14:50 +00001278 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1279 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001280 if (Context.Complain) {
1281 Context.Diag2(Params2->getParam(I)->getLocation(),
1282 diag::err_odr_different_template_parameter_kind);
1283 Context.Diag1(Params1->getParam(I)->getLocation(),
1284 diag::note_odr_template_parameter_here);
1285 }
Douglas Gregora082a492010-11-30 19:14:50 +00001286 return false;
1287 }
1288
1289 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1290 Params2->getParam(I))) {
1291
1292 return false;
1293 }
1294 }
1295
1296 return true;
1297}
1298
1299static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1300 TemplateTypeParmDecl *D1,
1301 TemplateTypeParmDecl *D2) {
1302 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001303 if (Context.Complain) {
1304 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1305 << D2->isParameterPack();
1306 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1307 << D1->isParameterPack();
1308 }
Douglas Gregora082a492010-11-30 19:14:50 +00001309 return false;
1310 }
1311
1312 return true;
1313}
1314
1315static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1316 NonTypeTemplateParmDecl *D1,
1317 NonTypeTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001318 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001319 if (Context.Complain) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001320 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1321 << D2->isParameterPack();
1322 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1323 << D1->isParameterPack();
1324 }
Douglas Gregora082a492010-11-30 19:14:50 +00001325 return false;
1326 }
Douglas Gregora082a492010-11-30 19:14:50 +00001327
1328 // Check types.
1329 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001330 if (Context.Complain) {
1331 Context.Diag2(D2->getLocation(),
1332 diag::err_odr_non_type_parameter_type_inconsistent)
1333 << D2->getType() << D1->getType();
1334 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1335 << D1->getType();
1336 }
Douglas Gregora082a492010-11-30 19:14:50 +00001337 return false;
1338 }
1339
1340 return true;
1341}
1342
1343static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1344 TemplateTemplateParmDecl *D1,
1345 TemplateTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001346 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001347 if (Context.Complain) {
1348 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1349 << D2->isParameterPack();
1350 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1351 << D1->isParameterPack();
1352 }
Douglas Gregora082a492010-11-30 19:14:50 +00001353 return false;
1354 }
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001355
Douglas Gregora082a492010-11-30 19:14:50 +00001356 // Check template parameter lists.
1357 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1358 D2->getTemplateParameters());
1359}
1360
1361static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1362 ClassTemplateDecl *D1,
1363 ClassTemplateDecl *D2) {
1364 // Check template parameters.
1365 if (!IsStructurallyEquivalent(Context,
1366 D1->getTemplateParameters(),
1367 D2->getTemplateParameters()))
1368 return false;
1369
1370 // Check the templated declaration.
1371 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1372 D2->getTemplatedDecl());
1373}
1374
Douglas Gregor3996e242010-02-15 22:01:00 +00001375/// \brief Determine structural equivalence of two declarations.
1376static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1377 Decl *D1, Decl *D2) {
1378 // FIXME: Check for known structural equivalences via a callback of some sort.
1379
Douglas Gregorb4964f72010-02-15 23:54:17 +00001380 // Check whether we already know that these two declarations are not
1381 // structurally equivalent.
1382 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1383 D2->getCanonicalDecl())))
1384 return false;
1385
Douglas Gregor3996e242010-02-15 22:01:00 +00001386 // Determine whether we've already produced a tentative equivalence for D1.
1387 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1388 if (EquivToD1)
1389 return EquivToD1 == D2->getCanonicalDecl();
1390
1391 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1392 EquivToD1 = D2->getCanonicalDecl();
1393 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1394 return true;
1395}
1396
1397bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1398 Decl *D2) {
1399 if (!::IsStructurallyEquivalent(*this, D1, D2))
1400 return false;
1401
1402 return !Finish();
1403}
1404
1405bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1406 QualType T2) {
1407 if (!::IsStructurallyEquivalent(*this, T1, T2))
1408 return false;
1409
1410 return !Finish();
1411}
1412
1413bool StructuralEquivalenceContext::Finish() {
1414 while (!DeclsToCheck.empty()) {
1415 // Check the next declaration.
1416 Decl *D1 = DeclsToCheck.front();
1417 DeclsToCheck.pop_front();
1418
1419 Decl *D2 = TentativeEquivalences[D1];
1420 assert(D2 && "Unrecorded tentative equivalence?");
1421
Douglas Gregorb4964f72010-02-15 23:54:17 +00001422 bool Equivalent = true;
1423
Douglas Gregor3996e242010-02-15 22:01:00 +00001424 // FIXME: Switch on all declaration kinds. For now, we're just going to
1425 // check the obvious ones.
1426 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1427 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1428 // Check for equivalent structure names.
1429 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001430 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1431 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001432 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001433 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1434 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001435 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1436 !::IsStructurallyEquivalent(*this, Record1, Record2))
1437 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001438 } else {
1439 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001440 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001441 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001442 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001443 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1444 // Check for equivalent enum names.
1445 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001446 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1447 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001448 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001449 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1450 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001451 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1452 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1453 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001454 } else {
1455 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001456 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001457 }
Richard Smithdda56e42011-04-15 14:24:37 +00001458 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1459 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001460 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001461 Typedef2->getIdentifier()) ||
1462 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001463 Typedef1->getUnderlyingType(),
1464 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001465 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001466 } else {
1467 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001468 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001469 }
Douglas Gregora082a492010-11-30 19:14:50 +00001470 } else if (ClassTemplateDecl *ClassTemplate1
1471 = dyn_cast<ClassTemplateDecl>(D1)) {
1472 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1473 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1474 ClassTemplate2->getIdentifier()) ||
1475 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1476 Equivalent = false;
1477 } else {
1478 // Class template/non-class-template mismatch.
1479 Equivalent = false;
1480 }
1481 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1482 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1483 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1484 Equivalent = false;
1485 } else {
1486 // Kind mismatch.
1487 Equivalent = false;
1488 }
1489 } else if (NonTypeTemplateParmDecl *NTTP1
1490 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1491 if (NonTypeTemplateParmDecl *NTTP2
1492 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1493 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1494 Equivalent = false;
1495 } else {
1496 // Kind mismatch.
1497 Equivalent = false;
1498 }
1499 } else if (TemplateTemplateParmDecl *TTP1
1500 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1501 if (TemplateTemplateParmDecl *TTP2
1502 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1503 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1504 Equivalent = false;
1505 } else {
1506 // Kind mismatch.
1507 Equivalent = false;
1508 }
1509 }
1510
Douglas Gregorb4964f72010-02-15 23:54:17 +00001511 if (!Equivalent) {
1512 // Note that these two declarations are not equivalent (and we already
1513 // know about it).
1514 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1515 D2->getCanonicalDecl()));
1516 return true;
1517 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001518 // FIXME: Check other declaration kinds!
1519 }
1520
1521 return false;
1522}
1523
1524//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001525// Import Types
1526//----------------------------------------------------------------------------
1527
John McCall424cec92011-01-19 06:33:43 +00001528QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +00001529 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1530 << T->getTypeClassName();
1531 return QualType();
1532}
1533
John McCall424cec92011-01-19 06:33:43 +00001534QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001535 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +00001536#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
1537 case BuiltinType::Id: \
1538 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +00001539#include "clang/Basic/OpenCLImageTypes.def"
John McCalle314e272011-10-18 21:02:43 +00001540#define SHARED_SINGLETON_TYPE(Expansion)
1541#define BUILTIN_TYPE(Id, SingletonId) \
1542 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1543#include "clang/AST/BuiltinTypes.def"
1544
1545 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1546 // context supports C++.
1547
1548 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1549 // context supports ObjC.
1550
Douglas Gregor96e578d2010-02-05 17:54:41 +00001551 case BuiltinType::Char_U:
1552 // The context we're importing from has an unsigned 'char'. If we're
1553 // importing into a context with a signed 'char', translate to
1554 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001555 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001556 return Importer.getToContext().UnsignedCharTy;
1557
1558 return Importer.getToContext().CharTy;
1559
Douglas Gregor96e578d2010-02-05 17:54:41 +00001560 case BuiltinType::Char_S:
1561 // The context we're importing from has an unsigned 'char'. If we're
1562 // importing into a context with a signed 'char', translate to
1563 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001564 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001565 return Importer.getToContext().SignedCharTy;
1566
1567 return Importer.getToContext().CharTy;
1568
Chris Lattnerad3467e2010-12-25 23:25:43 +00001569 case BuiltinType::WChar_S:
1570 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001571 // FIXME: If not in C++, shall we translate to the C equivalent of
1572 // wchar_t?
1573 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001574 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001575
1576 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001577}
1578
John McCall424cec92011-01-19 06:33:43 +00001579QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001580 QualType ToElementType = Importer.Import(T->getElementType());
1581 if (ToElementType.isNull())
1582 return QualType();
1583
1584 return Importer.getToContext().getComplexType(ToElementType);
1585}
1586
John McCall424cec92011-01-19 06:33:43 +00001587QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001588 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1589 if (ToPointeeType.isNull())
1590 return QualType();
1591
1592 return Importer.getToContext().getPointerType(ToPointeeType);
1593}
1594
John McCall424cec92011-01-19 06:33:43 +00001595QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001596 // FIXME: Check for blocks support in "to" context.
1597 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1598 if (ToPointeeType.isNull())
1599 return QualType();
1600
1601 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1602}
1603
John McCall424cec92011-01-19 06:33:43 +00001604QualType
1605ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001606 // FIXME: Check for C++ support in "to" context.
1607 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1608 if (ToPointeeType.isNull())
1609 return QualType();
1610
1611 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1612}
1613
John McCall424cec92011-01-19 06:33:43 +00001614QualType
1615ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001616 // FIXME: Check for C++0x support in "to" context.
1617 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1618 if (ToPointeeType.isNull())
1619 return QualType();
1620
1621 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1622}
1623
John McCall424cec92011-01-19 06:33:43 +00001624QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001625 // FIXME: Check for C++ support in "to" context.
1626 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1627 if (ToPointeeType.isNull())
1628 return QualType();
1629
1630 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1631 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1632 ClassType.getTypePtr());
1633}
1634
John McCall424cec92011-01-19 06:33:43 +00001635QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001636 QualType ToElementType = Importer.Import(T->getElementType());
1637 if (ToElementType.isNull())
1638 return QualType();
1639
1640 return Importer.getToContext().getConstantArrayType(ToElementType,
1641 T->getSize(),
1642 T->getSizeModifier(),
1643 T->getIndexTypeCVRQualifiers());
1644}
1645
John McCall424cec92011-01-19 06:33:43 +00001646QualType
1647ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001648 QualType ToElementType = Importer.Import(T->getElementType());
1649 if (ToElementType.isNull())
1650 return QualType();
1651
1652 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1653 T->getSizeModifier(),
1654 T->getIndexTypeCVRQualifiers());
1655}
1656
John McCall424cec92011-01-19 06:33:43 +00001657QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001658 QualType ToElementType = Importer.Import(T->getElementType());
1659 if (ToElementType.isNull())
1660 return QualType();
1661
1662 Expr *Size = Importer.Import(T->getSizeExpr());
1663 if (!Size)
1664 return QualType();
1665
1666 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1667 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1668 T->getSizeModifier(),
1669 T->getIndexTypeCVRQualifiers(),
1670 Brackets);
1671}
1672
John McCall424cec92011-01-19 06:33:43 +00001673QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001674 QualType ToElementType = Importer.Import(T->getElementType());
1675 if (ToElementType.isNull())
1676 return QualType();
1677
1678 return Importer.getToContext().getVectorType(ToElementType,
1679 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001680 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001681}
1682
John McCall424cec92011-01-19 06:33:43 +00001683QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001684 QualType ToElementType = Importer.Import(T->getElementType());
1685 if (ToElementType.isNull())
1686 return QualType();
1687
1688 return Importer.getToContext().getExtVectorType(ToElementType,
1689 T->getNumElements());
1690}
1691
John McCall424cec92011-01-19 06:33:43 +00001692QualType
1693ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001694 // FIXME: What happens if we're importing a function without a prototype
1695 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +00001696 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001697 if (ToResultType.isNull())
1698 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001699
Douglas Gregor96e578d2010-02-05 17:54:41 +00001700 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001701 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001702}
1703
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001704QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +00001705 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001706 if (ToResultType.isNull())
1707 return QualType();
1708
1709 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001710 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00001711 for (const auto &A : T->param_types()) {
1712 QualType ArgType = Importer.Import(A);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001713 if (ArgType.isNull())
1714 return QualType();
1715 ArgTypes.push_back(ArgType);
1716 }
1717
1718 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001719 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +00001720 for (const auto &E : T->exceptions()) {
1721 QualType ExceptionType = Importer.Import(E);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001722 if (ExceptionType.isNull())
1723 return QualType();
1724 ExceptionTypes.push_back(ExceptionType);
1725 }
John McCalldb40c7f2010-12-14 08:05:40 +00001726
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001727 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1728 FunctionProtoType::ExtProtoInfo ToEPI;
1729
1730 ToEPI.ExtInfo = FromEPI.ExtInfo;
1731 ToEPI.Variadic = FromEPI.Variadic;
1732 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1733 ToEPI.TypeQuals = FromEPI.TypeQuals;
1734 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +00001735 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1736 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
1737 ToEPI.ExceptionSpec.NoexceptExpr =
1738 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
1739 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
1740 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
1741 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
1742 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001743
Jordan Rose5c382722013-03-08 21:51:21 +00001744 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001745}
1746
Sean Callananda6df8a2011-08-11 16:56:07 +00001747QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1748 QualType ToInnerType = Importer.Import(T->getInnerType());
1749 if (ToInnerType.isNull())
1750 return QualType();
1751
1752 return Importer.getToContext().getParenType(ToInnerType);
1753}
1754
John McCall424cec92011-01-19 06:33:43 +00001755QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +00001756 TypedefNameDecl *ToDecl
1757 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001758 if (!ToDecl)
1759 return QualType();
1760
1761 return Importer.getToContext().getTypeDeclType(ToDecl);
1762}
1763
John McCall424cec92011-01-19 06:33:43 +00001764QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001765 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1766 if (!ToExpr)
1767 return QualType();
1768
1769 return Importer.getToContext().getTypeOfExprType(ToExpr);
1770}
1771
John McCall424cec92011-01-19 06:33:43 +00001772QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001773 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1774 if (ToUnderlyingType.isNull())
1775 return QualType();
1776
1777 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1778}
1779
John McCall424cec92011-01-19 06:33:43 +00001780QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001781 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +00001782 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1783 if (!ToExpr)
1784 return QualType();
1785
Douglas Gregor81495f32012-02-12 18:42:33 +00001786 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1787 if (UnderlyingType.isNull())
1788 return QualType();
1789
1790 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001791}
1792
Alexis Hunte852b102011-05-24 22:41:36 +00001793QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1794 QualType ToBaseType = Importer.Import(T->getBaseType());
1795 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1796 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1797 return QualType();
1798
1799 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1800 ToUnderlyingType,
1801 T->getUTTKind());
1802}
1803
Richard Smith30482bc2011-02-20 03:19:35 +00001804QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00001805 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +00001806 QualType FromDeduced = T->getDeducedType();
1807 QualType ToDeduced;
1808 if (!FromDeduced.isNull()) {
1809 ToDeduced = Importer.Import(FromDeduced);
1810 if (ToDeduced.isNull())
1811 return QualType();
1812 }
1813
Richard Smithe301ba22015-11-11 02:02:15 +00001814 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +00001815 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +00001816}
1817
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001818QualType ASTNodeImporter::VisitInjectedClassNameType(
1819 const InjectedClassNameType *T) {
1820 CXXRecordDecl *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl()));
1821 if (!D)
1822 return QualType();
1823
1824 QualType InjType = Importer.Import(T->getInjectedSpecializationType());
1825 if (InjType.isNull())
1826 return QualType();
1827
1828 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
1829 // See comments in InjectedClassNameType definition for details
1830 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
1831 enum {
1832 TypeAlignmentInBits = 4,
1833 TypeAlignment = 1 << TypeAlignmentInBits
1834 };
1835
1836 return QualType(new (Importer.getToContext(), TypeAlignment)
1837 InjectedClassNameType(D, InjType), 0);
1838}
1839
John McCall424cec92011-01-19 06:33:43 +00001840QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001841 RecordDecl *ToDecl
1842 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1843 if (!ToDecl)
1844 return QualType();
1845
1846 return Importer.getToContext().getTagDeclType(ToDecl);
1847}
1848
John McCall424cec92011-01-19 06:33:43 +00001849QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001850 EnumDecl *ToDecl
1851 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1852 if (!ToDecl)
1853 return QualType();
1854
1855 return Importer.getToContext().getTagDeclType(ToDecl);
1856}
1857
Sean Callanan72fe0852015-04-02 23:50:08 +00001858QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
1859 QualType FromModifiedType = T->getModifiedType();
1860 QualType FromEquivalentType = T->getEquivalentType();
1861 QualType ToModifiedType;
1862 QualType ToEquivalentType;
1863
1864 if (!FromModifiedType.isNull()) {
1865 ToModifiedType = Importer.Import(FromModifiedType);
1866 if (ToModifiedType.isNull())
1867 return QualType();
1868 }
1869 if (!FromEquivalentType.isNull()) {
1870 ToEquivalentType = Importer.Import(FromEquivalentType);
1871 if (ToEquivalentType.isNull())
1872 return QualType();
1873 }
1874
1875 return Importer.getToContext().getAttributedType(T->getAttrKind(),
1876 ToModifiedType, ToEquivalentType);
1877}
1878
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001879
1880QualType ASTNodeImporter::VisitTemplateTypeParmType(
1881 const TemplateTypeParmType *T) {
1882 TemplateTypeParmDecl *ParmDecl =
1883 cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl()));
1884 if (!ParmDecl && T->getDecl())
1885 return QualType();
1886
1887 return Importer.getToContext().getTemplateTypeParmType(
1888 T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl);
1889}
1890
Douglas Gregore2e50d332010-12-01 01:36:18 +00001891QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001892 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001893 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1894 if (ToTemplate.isNull())
1895 return QualType();
1896
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001897 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001898 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1899 return QualType();
1900
1901 QualType ToCanonType;
1902 if (!QualType(T, 0).isCanonical()) {
1903 QualType FromCanonType
1904 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1905 ToCanonType =Importer.Import(FromCanonType);
1906 if (ToCanonType.isNull())
1907 return QualType();
1908 }
1909 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1910 ToTemplateArgs.data(),
1911 ToTemplateArgs.size(),
1912 ToCanonType);
1913}
1914
John McCall424cec92011-01-19 06:33:43 +00001915QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Craig Topper36250ad2014-05-12 05:36:57 +00001916 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001917 // Note: the qualifier in an ElaboratedType is optional.
1918 if (T->getQualifier()) {
1919 ToQualifier = Importer.Import(T->getQualifier());
1920 if (!ToQualifier)
1921 return QualType();
1922 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001923
1924 QualType ToNamedType = Importer.Import(T->getNamedType());
1925 if (ToNamedType.isNull())
1926 return QualType();
1927
Abramo Bagnara6150c882010-05-11 21:36:43 +00001928 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1929 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001930}
1931
John McCall424cec92011-01-19 06:33:43 +00001932QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001933 ObjCInterfaceDecl *Class
1934 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1935 if (!Class)
1936 return QualType();
1937
John McCall8b07ec22010-05-15 11:32:37 +00001938 return Importer.getToContext().getObjCInterfaceType(Class);
1939}
1940
John McCall424cec92011-01-19 06:33:43 +00001941QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001942 QualType ToBaseType = Importer.Import(T->getBaseType());
1943 if (ToBaseType.isNull())
1944 return QualType();
1945
Douglas Gregore9d95f12015-07-07 03:57:35 +00001946 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00001947 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00001948 QualType ImportedTypeArg = Importer.Import(TypeArg);
1949 if (ImportedTypeArg.isNull())
1950 return QualType();
1951
1952 TypeArgs.push_back(ImportedTypeArg);
1953 }
1954
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001955 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001956 for (auto *P : T->quals()) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001957 ObjCProtocolDecl *Protocol
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001958 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001959 if (!Protocol)
1960 return QualType();
1961 Protocols.push_back(Protocol);
1962 }
1963
Douglas Gregore9d95f12015-07-07 03:57:35 +00001964 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001965 Protocols,
1966 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001967}
1968
John McCall424cec92011-01-19 06:33:43 +00001969QualType
1970ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001971 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1972 if (ToPointeeType.isNull())
1973 return QualType();
1974
John McCall8b07ec22010-05-15 11:32:37 +00001975 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001976}
1977
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001978//----------------------------------------------------------------------------
1979// Import Declarations
1980//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001981bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1982 DeclContext *&LexicalDC,
1983 DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +00001984 NamedDecl *&ToD,
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001985 SourceLocation &Loc) {
1986 // Import the context of this declaration.
1987 DC = Importer.ImportContext(D->getDeclContext());
1988 if (!DC)
1989 return true;
1990
1991 LexicalDC = DC;
1992 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1993 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1994 if (!LexicalDC)
1995 return true;
1996 }
1997
1998 // Import the name of this declaration.
1999 Name = Importer.Import(D->getDeclName());
2000 if (D->getDeclName() && !Name)
2001 return true;
2002
2003 // Import the location of this declaration.
2004 Loc = Importer.Import(D->getLocation());
Sean Callanan59721b32015-04-28 18:41:46 +00002005 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002006 return false;
2007}
2008
Douglas Gregord451ea92011-07-29 23:31:30 +00002009void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
2010 if (!FromD)
2011 return;
2012
2013 if (!ToD) {
2014 ToD = Importer.Import(FromD);
2015 if (!ToD)
2016 return;
2017 }
2018
2019 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
2020 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +00002021 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +00002022 ImportDefinition(FromRecord, ToRecord);
2023 }
2024 }
2025 return;
2026 }
2027
2028 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
2029 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
2030 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
2031 ImportDefinition(FromEnum, ToEnum);
2032 }
2033 }
2034 return;
2035 }
2036}
2037
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002038void
2039ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
2040 DeclarationNameInfo& To) {
2041 // NOTE: To.Name and To.Loc are already imported.
2042 // We only have to import To.LocInfo.
2043 switch (To.getName().getNameKind()) {
2044 case DeclarationName::Identifier:
2045 case DeclarationName::ObjCZeroArgSelector:
2046 case DeclarationName::ObjCOneArgSelector:
2047 case DeclarationName::ObjCMultiArgSelector:
2048 case DeclarationName::CXXUsingDirective:
2049 return;
2050
2051 case DeclarationName::CXXOperatorName: {
2052 SourceRange Range = From.getCXXOperatorNameRange();
2053 To.setCXXOperatorNameRange(Importer.Import(Range));
2054 return;
2055 }
2056 case DeclarationName::CXXLiteralOperatorName: {
2057 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
2058 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
2059 return;
2060 }
2061 case DeclarationName::CXXConstructorName:
2062 case DeclarationName::CXXDestructorName:
2063 case DeclarationName::CXXConversionFunctionName: {
2064 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
2065 To.setNamedTypeInfo(Importer.Import(FromTInfo));
2066 return;
2067 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002068 }
Douglas Gregor07216d12011-11-02 20:52:01 +00002069 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002070}
2071
Douglas Gregor2e15c842012-02-01 21:00:38 +00002072void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00002073 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00002074 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00002075 return;
2076 }
2077
Aaron Ballman629afae2014-03-07 19:56:05 +00002078 for (auto *From : FromDC->decls())
2079 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +00002080}
2081
Douglas Gregord451ea92011-07-29 23:31:30 +00002082bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00002083 ImportDefinitionKind Kind) {
2084 if (To->getDefinition() || To->isBeingDefined()) {
2085 if (Kind == IDK_Everything)
2086 ImportDeclContext(From, /*ForceImport=*/true);
2087
Douglas Gregore2e50d332010-12-01 01:36:18 +00002088 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00002089 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00002090
2091 To->startDefinition();
2092
2093 // Add base classes.
2094 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
2095 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002096
2097 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
2098 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
2099 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00002100 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002101 ToData.Aggregate = FromData.Aggregate;
2102 ToData.PlainOldData = FromData.PlainOldData;
2103 ToData.Empty = FromData.Empty;
2104 ToData.Polymorphic = FromData.Polymorphic;
2105 ToData.Abstract = FromData.Abstract;
2106 ToData.IsStandardLayout = FromData.IsStandardLayout;
2107 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
2108 ToData.HasPrivateFields = FromData.HasPrivateFields;
2109 ToData.HasProtectedFields = FromData.HasProtectedFields;
2110 ToData.HasPublicFields = FromData.HasPublicFields;
2111 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00002112 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00002113 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00002114 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00002115 ToData.HasUninitializedReferenceMember
2116 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00002117 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00002118 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
2119 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +00002120 ToData.NeedOverloadResolutionForMoveConstructor
2121 = FromData.NeedOverloadResolutionForMoveConstructor;
2122 ToData.NeedOverloadResolutionForMoveAssignment
2123 = FromData.NeedOverloadResolutionForMoveAssignment;
2124 ToData.NeedOverloadResolutionForDestructor
2125 = FromData.NeedOverloadResolutionForDestructor;
2126 ToData.DefaultedMoveConstructorIsDeleted
2127 = FromData.DefaultedMoveConstructorIsDeleted;
2128 ToData.DefaultedMoveAssignmentIsDeleted
2129 = FromData.DefaultedMoveAssignmentIsDeleted;
2130 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00002131 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
2132 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002133 ToData.HasConstexprNonCopyMoveConstructor
2134 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00002135 ToData.HasDefaultedDefaultConstructor
2136 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00002137 ToData.DefaultedDefaultConstructorIsConstexpr
2138 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00002139 ToData.HasConstexprDefaultConstructor
2140 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002141 ToData.HasNonLiteralTypeFieldsOrBases
2142 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00002143 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002144 ToData.UserProvidedDefaultConstructor
2145 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00002146 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smith1c33fe82012-11-28 06:23:12 +00002147 ToData.ImplicitCopyConstructorHasConstParam
2148 = FromData.ImplicitCopyConstructorHasConstParam;
2149 ToData.ImplicitCopyAssignmentHasConstParam
2150 = FromData.ImplicitCopyAssignmentHasConstParam;
2151 ToData.HasDeclaredCopyConstructorWithConstParam
2152 = FromData.HasDeclaredCopyConstructorWithConstParam;
2153 ToData.HasDeclaredCopyAssignmentWithConstParam
2154 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00002155 ToData.IsLambda = FromData.IsLambda;
2156
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002157 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00002158 for (const auto &Base1 : FromCXX->bases()) {
2159 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +00002160 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00002161 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00002162
2163 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00002164 if (Base1.isPackExpansion())
2165 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00002166
2167 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00002168 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Douglas Gregord451ea92011-07-29 23:31:30 +00002169
Douglas Gregore2e50d332010-12-01 01:36:18 +00002170 Bases.push_back(
2171 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00002172 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
2173 Base1.isVirtual(),
2174 Base1.isBaseOfClass(),
2175 Base1.getAccessSpecifierAsWritten(),
2176 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00002177 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00002178 }
2179 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00002180 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00002181 }
2182
Douglas Gregor2e15c842012-02-01 21:00:38 +00002183 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00002184 ImportDeclContext(From, /*ForceImport=*/true);
2185
Douglas Gregore2e50d332010-12-01 01:36:18 +00002186 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00002187 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002188}
2189
Larisse Voufo39a1e502013-08-06 01:03:05 +00002190bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
2191 ImportDefinitionKind Kind) {
Sean Callanan59721b32015-04-28 18:41:46 +00002192 if (To->getAnyInitializer())
Larisse Voufo39a1e502013-08-06 01:03:05 +00002193 return false;
2194
2195 // FIXME: Can we really import any initializer? Alternatively, we could force
2196 // ourselves to import every declaration of a variable and then only use
2197 // getInit() here.
2198 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
2199
2200 // FIXME: Other bits to merge?
2201
2202 return false;
2203}
2204
Douglas Gregord451ea92011-07-29 23:31:30 +00002205bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00002206 ImportDefinitionKind Kind) {
2207 if (To->getDefinition() || To->isBeingDefined()) {
2208 if (Kind == IDK_Everything)
2209 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002210 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002211 }
Douglas Gregord451ea92011-07-29 23:31:30 +00002212
2213 To->startDefinition();
2214
2215 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
2216 if (T.isNull())
2217 return true;
2218
2219 QualType ToPromotionType = Importer.Import(From->getPromotionType());
2220 if (ToPromotionType.isNull())
2221 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002222
2223 if (shouldForceImportDeclContext(Kind))
2224 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002225
2226 // FIXME: we might need to merge the number of positive or negative bits
2227 // if the enumerator lists don't match.
2228 To->completeDefinition(T, ToPromotionType,
2229 From->getNumPositiveBits(),
2230 From->getNumNegativeBits());
2231 return false;
2232}
2233
Douglas Gregora082a492010-11-30 19:14:50 +00002234TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
2235 TemplateParameterList *Params) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002236 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregora082a492010-11-30 19:14:50 +00002237 ToParams.reserve(Params->size());
2238 for (TemplateParameterList::iterator P = Params->begin(),
2239 PEnd = Params->end();
2240 P != PEnd; ++P) {
2241 Decl *To = Importer.Import(*P);
2242 if (!To)
Craig Topper36250ad2014-05-12 05:36:57 +00002243 return nullptr;
2244
Douglas Gregora082a492010-11-30 19:14:50 +00002245 ToParams.push_back(cast<NamedDecl>(To));
2246 }
2247
2248 return TemplateParameterList::Create(Importer.getToContext(),
2249 Importer.Import(Params->getTemplateLoc()),
2250 Importer.Import(Params->getLAngleLoc()),
David Majnemer902f8c62015-12-27 07:16:27 +00002251 ToParams,
Douglas Gregora082a492010-11-30 19:14:50 +00002252 Importer.Import(Params->getRAngleLoc()));
2253}
2254
Douglas Gregore2e50d332010-12-01 01:36:18 +00002255TemplateArgument
2256ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
2257 switch (From.getKind()) {
2258 case TemplateArgument::Null:
2259 return TemplateArgument();
2260
2261 case TemplateArgument::Type: {
2262 QualType ToType = Importer.Import(From.getAsType());
2263 if (ToType.isNull())
2264 return TemplateArgument();
2265 return TemplateArgument(ToType);
2266 }
2267
2268 case TemplateArgument::Integral: {
2269 QualType ToType = Importer.Import(From.getIntegralType());
2270 if (ToType.isNull())
2271 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00002272 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00002273 }
2274
Eli Friedmanb826a002012-09-26 02:36:12 +00002275 case TemplateArgument::Declaration: {
David Blaikie3c7dd6b2014-10-22 19:54:16 +00002276 ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
2277 QualType ToType = Importer.Import(From.getParamTypeForDecl());
2278 if (!To || ToType.isNull())
2279 return TemplateArgument();
2280 return TemplateArgument(To, ToType);
Eli Friedmanb826a002012-09-26 02:36:12 +00002281 }
2282
2283 case TemplateArgument::NullPtr: {
2284 QualType ToType = Importer.Import(From.getNullPtrType());
2285 if (ToType.isNull())
2286 return TemplateArgument();
2287 return TemplateArgument(ToType, /*isNullPtr*/true);
2288 }
2289
Douglas Gregore2e50d332010-12-01 01:36:18 +00002290 case TemplateArgument::Template: {
2291 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
2292 if (ToTemplate.isNull())
2293 return TemplateArgument();
2294
2295 return TemplateArgument(ToTemplate);
2296 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002297
2298 case TemplateArgument::TemplateExpansion: {
2299 TemplateName ToTemplate
2300 = Importer.Import(From.getAsTemplateOrTemplatePattern());
2301 if (ToTemplate.isNull())
2302 return TemplateArgument();
2303
Douglas Gregore1d60df2011-01-14 23:41:42 +00002304 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002305 }
2306
Douglas Gregore2e50d332010-12-01 01:36:18 +00002307 case TemplateArgument::Expression:
2308 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2309 return TemplateArgument(ToExpr);
2310 return TemplateArgument();
2311
2312 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002313 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002314 ToPack.reserve(From.pack_size());
2315 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2316 return TemplateArgument();
Benjamin Kramercce63472015-08-05 09:40:22 +00002317
2318 return TemplateArgument(
2319 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00002320 }
2321 }
2322
2323 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00002324}
2325
2326bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2327 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002328 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00002329 for (unsigned I = 0; I != NumFromArgs; ++I) {
2330 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2331 if (To.isNull() && !FromArgs[I].isNull())
2332 return true;
2333
2334 ToArgs.push_back(To);
2335 }
2336
2337 return false;
2338}
2339
Douglas Gregor5c73e912010-02-11 00:48:18 +00002340bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002341 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00002342 // Eliminate a potential failure point where we attempt to re-import
2343 // something we're trying to import while completing ToRecord.
2344 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
2345 if (ToOrigin) {
2346 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
2347 if (ToOriginRecord)
2348 ToRecord = ToOriginRecord;
2349 }
2350
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002351 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00002352 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002353 Importer.getNonEquivalentDecls(),
2354 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002355 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002356}
2357
Larisse Voufo39a1e502013-08-06 01:03:05 +00002358bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
2359 bool Complain) {
2360 StructuralEquivalenceContext Ctx(
2361 Importer.getFromContext(), Importer.getToContext(),
2362 Importer.getNonEquivalentDecls(), false, Complain);
2363 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
2364}
2365
Douglas Gregor98c10182010-02-12 22:17:39 +00002366bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002367 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002368 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002369 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002370 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002371}
2372
Douglas Gregor91155082012-11-14 22:29:20 +00002373bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
2374 EnumConstantDecl *ToEC)
2375{
2376 const llvm::APSInt &FromVal = FromEC->getInitVal();
2377 const llvm::APSInt &ToVal = ToEC->getInitVal();
2378
2379 return FromVal.isSigned() == ToVal.isSigned() &&
2380 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2381 FromVal == ToVal;
2382}
2383
2384bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002385 ClassTemplateDecl *To) {
2386 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2387 Importer.getToContext(),
2388 Importer.getNonEquivalentDecls());
2389 return Ctx.IsStructurallyEquivalent(From, To);
2390}
2391
Larisse Voufo39a1e502013-08-06 01:03:05 +00002392bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2393 VarTemplateDecl *To) {
2394 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2395 Importer.getToContext(),
2396 Importer.getNonEquivalentDecls());
2397 return Ctx.IsStructurallyEquivalent(From, To);
2398}
2399
Douglas Gregore4c83e42010-02-09 22:48:33 +00002400Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002401 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002402 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00002403 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00002404}
2405
Sean Callanan65198272011-11-17 23:20:56 +00002406Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2407 TranslationUnitDecl *ToD =
2408 Importer.getToContext().getTranslationUnitDecl();
2409
2410 Importer.Imported(D, ToD);
2411
2412 return ToD;
2413}
2414
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002415Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
2416
2417 SourceLocation Loc = Importer.Import(D->getLocation());
2418 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
2419
2420 // Import the context of this declaration.
2421 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2422 if (!DC)
2423 return nullptr;
2424
2425 AccessSpecDecl *accessSpecDecl
2426 = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(),
2427 DC, Loc, ColonLoc);
2428
2429 if (!accessSpecDecl)
2430 return nullptr;
2431
2432 // Lexical DeclContext and Semantic DeclContext
2433 // is always the same for the accessSpec.
2434 accessSpecDecl->setLexicalDeclContext(DC);
2435 DC->addDeclInternal(accessSpecDecl);
2436
2437 return accessSpecDecl;
2438}
2439
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002440Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2441 // Import the major distinguishing characteristics of this namespace.
2442 DeclContext *DC, *LexicalDC;
2443 DeclarationName Name;
2444 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002445 NamedDecl *ToD;
2446 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002447 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002448 if (ToD)
2449 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002450
2451 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002452 if (!Name) {
2453 // This is an anonymous namespace. Adopt an existing anonymous
2454 // namespace if we can.
2455 // FIXME: Not testable.
2456 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2457 MergeWithNamespace = TU->getAnonymousNamespace();
2458 else
2459 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2460 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002461 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002462 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002463 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002464 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2465 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002466 continue;
2467
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002468 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002469 MergeWithNamespace = FoundNS;
2470 ConflictingDecls.clear();
2471 break;
2472 }
2473
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002474 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002475 }
2476
2477 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002478 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002479 ConflictingDecls.data(),
2480 ConflictingDecls.size());
2481 }
2482 }
2483
2484 // Create the "to" namespace, if needed.
2485 NamespaceDecl *ToNamespace = MergeWithNamespace;
2486 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002487 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002488 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002489 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00002490 Loc, Name.getAsIdentifierInfo(),
Craig Topper36250ad2014-05-12 05:36:57 +00002491 /*PrevDecl=*/nullptr);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002492 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002493 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002494
2495 // If this is an anonymous namespace, register it as the anonymous
2496 // namespace within its context.
2497 if (!Name) {
2498 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2499 TU->setAnonymousNamespace(ToNamespace);
2500 else
2501 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2502 }
2503 }
2504 Importer.Imported(D, ToNamespace);
2505
2506 ImportDeclContext(D);
2507
2508 return ToNamespace;
2509}
2510
Richard Smithdda56e42011-04-15 14:24:37 +00002511Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002512 // Import the major distinguishing characteristics of this typedef.
2513 DeclContext *DC, *LexicalDC;
2514 DeclarationName Name;
2515 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002516 NamedDecl *ToD;
2517 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002518 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002519 if (ToD)
2520 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002521
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002522 // If this typedef is not in block scope, determine whether we've
2523 // seen a typedef with the same name (that we can merge with) or any
2524 // other entity by that name (which name lookup could conflict with).
2525 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002526 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002527 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002528 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002529 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002530 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2531 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002532 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00002533 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002534 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002535 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2536 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002537 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002538 }
2539
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002540 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002541 }
2542
2543 if (!ConflictingDecls.empty()) {
2544 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2545 ConflictingDecls.data(),
2546 ConflictingDecls.size());
2547 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002548 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002549 }
2550 }
2551
Douglas Gregorb4964f72010-02-15 23:54:17 +00002552 // Import the underlying type of this typedef;
2553 QualType T = Importer.Import(D->getUnderlyingType());
2554 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002555 return nullptr;
2556
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002557 // Create the new typedef node.
2558 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002559 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00002560 TypedefNameDecl *ToTypedef;
2561 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002562 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2563 StartL, Loc,
2564 Name.getAsIdentifierInfo(),
2565 TInfo);
2566 else
Richard Smithdda56e42011-04-15 14:24:37 +00002567 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2568 StartL, Loc,
2569 Name.getAsIdentifierInfo(),
2570 TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002571
Douglas Gregordd483172010-02-22 17:42:47 +00002572 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002573 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002574 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00002575 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002576
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002577 return ToTypedef;
2578}
2579
Richard Smithdda56e42011-04-15 14:24:37 +00002580Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2581 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2582}
2583
2584Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2585 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2586}
2587
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002588Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
2589 // Import the major distinguishing characteristics of this label.
2590 DeclContext *DC, *LexicalDC;
2591 DeclarationName Name;
2592 SourceLocation Loc;
2593 NamedDecl *ToD;
2594 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2595 return nullptr;
2596 if (ToD)
2597 return ToD;
2598
2599 assert(LexicalDC->isFunctionOrMethod());
2600
2601 LabelDecl *ToLabel = D->isGnuLocal()
2602 ? LabelDecl::Create(Importer.getToContext(),
2603 DC, Importer.Import(D->getLocation()),
2604 Name.getAsIdentifierInfo(),
2605 Importer.Import(D->getLocStart()))
2606 : LabelDecl::Create(Importer.getToContext(),
2607 DC, Importer.Import(D->getLocation()),
2608 Name.getAsIdentifierInfo());
2609 Importer.Imported(D, ToLabel);
2610
2611 LabelStmt *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt()));
2612 if (!Label)
2613 return nullptr;
2614
2615 ToLabel->setStmt(Label);
2616 ToLabel->setLexicalDeclContext(LexicalDC);
2617 LexicalDC->addDeclInternal(ToLabel);
2618 return ToLabel;
2619}
2620
Douglas Gregor98c10182010-02-12 22:17:39 +00002621Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2622 // Import the major distinguishing characteristics of this enum.
2623 DeclContext *DC, *LexicalDC;
2624 DeclarationName Name;
2625 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002626 NamedDecl *ToD;
2627 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002628 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002629 if (ToD)
2630 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002631
Douglas Gregor98c10182010-02-12 22:17:39 +00002632 // Figure out what enum name we're looking for.
2633 unsigned IDNS = Decl::IDNS_Tag;
2634 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002635 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2636 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002637 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002638 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002639 IDNS |= Decl::IDNS_Ordinary;
2640
2641 // We may already have an enum of the same name; try to find and match it.
2642 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002643 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002644 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002645 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002646 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2647 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002648 continue;
2649
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002650 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002651 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002652 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2653 Found = Tag->getDecl();
2654 }
2655
2656 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002657 if (IsStructuralMatch(D, FoundEnum))
2658 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002659 }
2660
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002661 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002662 }
2663
2664 if (!ConflictingDecls.empty()) {
2665 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2666 ConflictingDecls.data(),
2667 ConflictingDecls.size());
2668 }
2669 }
2670
2671 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002672 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2673 Importer.Import(D->getLocStart()),
Craig Topper36250ad2014-05-12 05:36:57 +00002674 Loc, Name.getAsIdentifierInfo(), nullptr,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002675 D->isScoped(), D->isScopedUsingClassTag(),
2676 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002677 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002678 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002679 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002680 D2->setLexicalDeclContext(LexicalDC);
2681 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00002682 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002683
2684 // Import the integer type.
2685 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2686 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002687 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00002688 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002689
2690 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002691 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00002692 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002693
Douglas Gregor3996e242010-02-15 22:01:00 +00002694 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002695}
2696
Douglas Gregor5c73e912010-02-11 00:48:18 +00002697Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2698 // If this record has a definition in the translation unit we're coming from,
2699 // but this particular declaration is not that definition, import the
2700 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002701 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002702 if (Definition && Definition != D) {
2703 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002704 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00002705 return nullptr;
2706
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002707 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002708 }
2709
2710 // Import the major distinguishing characteristics of this record.
2711 DeclContext *DC, *LexicalDC;
2712 DeclarationName Name;
2713 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002714 NamedDecl *ToD;
2715 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002716 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002717 if (ToD)
2718 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002719
Douglas Gregor5c73e912010-02-11 00:48:18 +00002720 // Figure out what structure name we're looking for.
2721 unsigned IDNS = Decl::IDNS_Tag;
2722 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002723 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2724 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002725 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002726 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002727 IDNS |= Decl::IDNS_Ordinary;
2728
2729 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00002730 RecordDecl *AdoptDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002731 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002732 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002733 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002734 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002735 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2736 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002737 continue;
2738
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002739 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002740 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002741 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2742 Found = Tag->getDecl();
2743 }
2744
2745 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002746 if (D->isAnonymousStructOrUnion() &&
2747 FoundRecord->isAnonymousStructOrUnion()) {
2748 // If both anonymous structs/unions are in a record context, make sure
2749 // they occur in the same location in the context records.
David Blaikie05785d12013-02-20 22:23:23 +00002750 if (Optional<unsigned> Index1
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002751 = findAnonymousStructOrUnionIndex(D)) {
David Blaikie05785d12013-02-20 22:23:23 +00002752 if (Optional<unsigned> Index2 =
2753 findAnonymousStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002754 if (*Index1 != *Index2)
2755 continue;
2756 }
2757 }
2758 }
2759
Douglas Gregor25791052010-02-12 00:09:27 +00002760 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002761 if ((SearchName && !D->isCompleteDefinition())
2762 || (D->isCompleteDefinition() &&
2763 D->isAnonymousStructOrUnion()
2764 == FoundDef->isAnonymousStructOrUnion() &&
2765 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002766 // The record types structurally match, or the "from" translation
2767 // unit only had a forward declaration anyway; call it the same
2768 // function.
2769 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002770 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002771 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002772 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002773 // We have a forward declaration of this type, so adopt that forward
2774 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00002775
2776 // If one or both can be completed from external storage then try one
2777 // last time to complete and compare them before doing this.
2778
2779 if (FoundRecord->hasExternalLexicalStorage() &&
2780 !FoundRecord->isCompleteDefinition())
2781 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2782 if (D->hasExternalLexicalStorage())
2783 D->getASTContext().getExternalSource()->CompleteType(D);
2784
2785 if (FoundRecord->isCompleteDefinition() &&
2786 D->isCompleteDefinition() &&
2787 !IsStructuralMatch(D, FoundRecord))
2788 continue;
2789
Douglas Gregor25791052010-02-12 00:09:27 +00002790 AdoptDecl = FoundRecord;
2791 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002792 } else if (!SearchName) {
2793 continue;
2794 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002795 }
2796
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002797 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002798 }
2799
Douglas Gregordd6006f2012-07-17 21:16:27 +00002800 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002801 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2802 ConflictingDecls.data(),
2803 ConflictingDecls.size());
2804 }
2805 }
2806
2807 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002808 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002809 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002810 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002811 CXXRecordDecl *D2CXX = nullptr;
2812 if (CXXRecordDecl *DCXX = llvm::dyn_cast<CXXRecordDecl>(D)) {
2813 if (DCXX->isLambda()) {
2814 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
2815 D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(),
2816 DC, TInfo, Loc,
2817 DCXX->isDependentLambda(),
2818 DCXX->isGenericLambda(),
2819 DCXX->getLambdaCaptureDefault());
2820 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
2821 if (DCXX->getLambdaContextDecl() && !CDecl)
2822 return nullptr;
2823 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(),
2824 CDecl);
2825 } else {
2826 D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
2827 D->getTagKind(),
2828 DC, StartLoc, Loc,
2829 Name.getAsIdentifierInfo());
2830 }
Douglas Gregor3996e242010-02-15 22:01:00 +00002831 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002832 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002833 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002834 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002835 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002836 }
Douglas Gregor14454802011-02-25 02:25:35 +00002837
2838 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002839 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002840 LexicalDC->addDeclInternal(D2);
Douglas Gregordd6006f2012-07-17 21:16:27 +00002841 if (D->isAnonymousStructOrUnion())
2842 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002843 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002844
Douglas Gregor3996e242010-02-15 22:01:00 +00002845 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002846
Douglas Gregor95d82832012-01-24 18:36:04 +00002847 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00002848 return nullptr;
2849
Douglas Gregor3996e242010-02-15 22:01:00 +00002850 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002851}
2852
Douglas Gregor98c10182010-02-12 22:17:39 +00002853Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2854 // Import the major distinguishing characteristics of this enumerator.
2855 DeclContext *DC, *LexicalDC;
2856 DeclarationName Name;
2857 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002858 NamedDecl *ToD;
2859 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002860 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002861 if (ToD)
2862 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002863
2864 QualType T = Importer.Import(D->getType());
2865 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002866 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002867
Douglas Gregor98c10182010-02-12 22:17:39 +00002868 // Determine whether there are any other declarations with the same name and
2869 // in the same context.
2870 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002871 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002872 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002873 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002874 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002875 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2876 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002877 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002878
2879 if (EnumConstantDecl *FoundEnumConstant
2880 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
2881 if (IsStructuralMatch(D, FoundEnumConstant))
2882 return Importer.Imported(D, FoundEnumConstant);
2883 }
2884
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002885 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002886 }
2887
2888 if (!ConflictingDecls.empty()) {
2889 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2890 ConflictingDecls.data(),
2891 ConflictingDecls.size());
2892 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002893 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002894 }
2895 }
2896
2897 Expr *Init = Importer.Import(D->getInitExpr());
2898 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00002899 return nullptr;
2900
Douglas Gregor98c10182010-02-12 22:17:39 +00002901 EnumConstantDecl *ToEnumerator
2902 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2903 Name.getAsIdentifierInfo(), T,
2904 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002905 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002906 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002907 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002908 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002909 return ToEnumerator;
2910}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002911
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002912Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2913 // Import the major distinguishing characteristics of this function.
2914 DeclContext *DC, *LexicalDC;
2915 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002916 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002917 NamedDecl *ToD;
2918 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002919 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002920 if (ToD)
2921 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002922
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002923 // Try to find a function in our own ("to") context with the same name, same
2924 // type, and in the same context as the function we're importing.
2925 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002926 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002927 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002928 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002929 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002930 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2931 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002932 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002933
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002934 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002935 if (FoundFunction->hasExternalFormalLinkage() &&
2936 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002937 if (Importer.IsStructurallyEquivalent(D->getType(),
2938 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002939 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002940 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002941 }
2942
2943 // FIXME: Check for overloading more carefully, e.g., by boosting
2944 // Sema::IsOverload out to the AST library.
2945
2946 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002947 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002948 continue;
2949
2950 // Complain about inconsistent function types.
2951 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002952 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002953 Importer.ToDiag(FoundFunction->getLocation(),
2954 diag::note_odr_value_here)
2955 << FoundFunction->getType();
2956 }
2957 }
2958
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002959 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002960 }
2961
2962 if (!ConflictingDecls.empty()) {
2963 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2964 ConflictingDecls.data(),
2965 ConflictingDecls.size());
2966 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002967 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002968 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002969 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002970
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002971 DeclarationNameInfo NameInfo(Name, Loc);
2972 // Import additional name location/type info.
2973 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2974
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002975 QualType FromTy = D->getType();
2976 bool usedDifferentExceptionSpec = false;
2977
2978 if (const FunctionProtoType *
2979 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2980 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2981 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2982 // FunctionDecl that we are importing the FunctionProtoType for.
2983 // To avoid an infinite recursion when importing, create the FunctionDecl
2984 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00002985 if (FromEPI.ExceptionSpec.SourceDecl ||
2986 FromEPI.ExceptionSpec.SourceTemplate ||
2987 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002988 FunctionProtoType::ExtProtoInfo DefaultEPI;
2989 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00002990 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002991 usedDifferentExceptionSpec = true;
2992 }
2993 }
2994
Douglas Gregorb4964f72010-02-15 23:54:17 +00002995 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002996 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002997 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002998 return nullptr;
2999
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003000 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003001 SmallVector<ParmVarDecl *, 8> Parameters;
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00003002 for (auto P : D->params()) {
3003 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003004 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003005 return nullptr;
3006
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003007 Parameters.push_back(ToP);
3008 }
3009
3010 // Create the imported function.
3011 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Craig Topper36250ad2014-05-12 05:36:57 +00003012 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003013 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Douglas Gregor00eace12010-02-21 18:29:16 +00003014 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
3015 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
3016 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00003017 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003018 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00003019 FromConstructor->isExplicit(),
3020 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00003021 D->isImplicit(),
3022 D->isConstexpr());
Sean Callanan55d486d2016-05-14 05:20:31 +00003023 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
3024 SmallVector<CXXCtorInitializer *, 4> CtorInitializers;
3025 for (CXXCtorInitializer *I : FromConstructor->inits()) {
3026 CXXCtorInitializer *ToI =
3027 cast_or_null<CXXCtorInitializer>(Importer.Import(I));
3028 if (!ToI && I)
3029 return nullptr;
3030 CtorInitializers.push_back(ToI);
3031 }
3032 CXXCtorInitializer **Memory =
3033 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
3034 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
3035 CXXConstructorDecl *ToCtor = llvm::cast<CXXConstructorDecl>(ToFunction);
3036 ToCtor->setCtorInitializers(Memory);
3037 ToCtor->setNumCtorInitializers(NumInitializers);
3038 }
Douglas Gregor00eace12010-02-21 18:29:16 +00003039 } else if (isa<CXXDestructorDecl>(D)) {
3040 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
3041 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00003042 InnerLocStart,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00003043 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00003044 D->isInlineSpecified(),
3045 D->isImplicit());
3046 } else if (CXXConversionDecl *FromConversion
3047 = dyn_cast<CXXConversionDecl>(D)) {
3048 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
3049 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00003050 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003051 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00003052 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00003053 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00003054 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00003055 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00003056 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
3057 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
3058 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00003059 InnerLocStart,
Douglas Gregora50ad132010-11-29 16:04:58 +00003060 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003061 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00003062 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00003063 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00003064 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00003065 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003066 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Sean Callanan59721b32015-04-28 18:41:46 +00003067 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003068 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00003069 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00003070 D->hasWrittenPrototype(),
3071 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00003072 }
John McCall3e11ebe2010-03-15 10:12:16 +00003073
3074 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00003075 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003076 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00003077 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00003078 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
3079 ToFunction->setTrivial(D->isTrivial());
3080 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00003081 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003082
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003083 // Set the parameters.
3084 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003085 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00003086 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003087 }
David Blaikie9c70e042011-09-21 18:16:56 +00003088 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003089
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003090 if (usedDifferentExceptionSpec) {
3091 // Update FunctionProtoType::ExtProtoInfo.
3092 QualType T = Importer.Import(D->getType());
3093 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003094 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003095 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00003096 }
3097
Sean Callanan59721b32015-04-28 18:41:46 +00003098 // Import the body, if any.
3099 if (Stmt *FromBody = D->getBody()) {
3100 if (Stmt *ToBody = Importer.Import(FromBody)) {
3101 ToFunction->setBody(ToBody);
3102 }
3103 }
3104
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003105 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003106
3107 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00003108 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003109
Douglas Gregor43f54792010-02-17 02:12:47 +00003110 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003111}
3112
Douglas Gregor00eace12010-02-21 18:29:16 +00003113Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
3114 return VisitFunctionDecl(D);
3115}
3116
3117Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
3118 return VisitCXXMethodDecl(D);
3119}
3120
3121Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
3122 return VisitCXXMethodDecl(D);
3123}
3124
3125Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
3126 return VisitCXXMethodDecl(D);
3127}
3128
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003129static unsigned getFieldIndex(Decl *F) {
3130 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
3131 if (!Owner)
3132 return 0;
3133
3134 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00003135 for (const auto *D : Owner->noload_decls()) {
3136 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003137 return Index;
3138
3139 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
3140 ++Index;
3141 }
3142
3143 return Index;
3144}
3145
Douglas Gregor5c73e912010-02-11 00:48:18 +00003146Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
3147 // Import the major distinguishing characteristics of a variable.
3148 DeclContext *DC, *LexicalDC;
3149 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00003150 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003151 NamedDecl *ToD;
3152 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003153 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003154 if (ToD)
3155 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003156
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003157 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003158 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003159 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003160 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3161 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003162 // For anonymous fields, match up by index.
3163 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
3164 continue;
3165
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003166 if (Importer.IsStructurallyEquivalent(D->getType(),
3167 FoundField->getType())) {
3168 Importer.Imported(D, FoundField);
3169 return FoundField;
3170 }
3171
3172 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3173 << Name << D->getType() << FoundField->getType();
3174 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3175 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003176 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003177 }
3178 }
3179
Douglas Gregorb4964f72010-02-15 23:54:17 +00003180 // Import the type.
3181 QualType T = Importer.Import(D->getType());
3182 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003183 return nullptr;
3184
Douglas Gregor5c73e912010-02-11 00:48:18 +00003185 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3186 Expr *BitWidth = Importer.Import(D->getBitWidth());
3187 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00003188 return nullptr;
3189
Abramo Bagnaradff19302011-03-08 08:55:46 +00003190 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
3191 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00003192 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00003193 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00003194 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00003195 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003196 ToField->setLexicalDeclContext(LexicalDC);
Sean Callanan3a83ea72016-03-03 02:22:05 +00003197 if (Expr *FromInitializer = D->getInClassInitializer()) {
Sean Callananbb33f582016-03-03 01:21:28 +00003198 Expr *ToInitializer = Importer.Import(FromInitializer);
3199 if (ToInitializer)
3200 ToField->setInClassInitializer(ToInitializer);
3201 else
3202 return nullptr;
3203 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003204 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003205 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00003206 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003207 return ToField;
3208}
3209
Francois Pichet783dd6e2010-11-21 06:08:52 +00003210Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
3211 // Import the major distinguishing characteristics of a variable.
3212 DeclContext *DC, *LexicalDC;
3213 DeclarationName Name;
3214 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003215 NamedDecl *ToD;
3216 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003217 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003218 if (ToD)
3219 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003220
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003221 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003222 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003223 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003224 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003225 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003226 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003227 // For anonymous indirect fields, match up by index.
3228 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
3229 continue;
3230
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003231 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003232 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003233 !Name.isEmpty())) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003234 Importer.Imported(D, FoundField);
3235 return FoundField;
3236 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003237
3238 // If there are more anonymous fields to check, continue.
3239 if (!Name && I < N-1)
3240 continue;
3241
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003242 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3243 << Name << D->getType() << FoundField->getType();
3244 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3245 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003246 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003247 }
3248 }
3249
Francois Pichet783dd6e2010-11-21 06:08:52 +00003250 // Import the type.
3251 QualType T = Importer.Import(D->getType());
3252 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003253 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003254
3255 NamedDecl **NamedChain =
3256 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
3257
3258 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00003259 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00003260 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003261 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00003262 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003263 NamedChain[i++] = cast<NamedDecl>(D);
3264 }
3265
3266 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
Aaron Ballman260995b2014-10-15 16:58:18 +00003267 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
3268 NamedChain, D->getChainingSize());
3269
3270 for (const auto *Attr : D->attrs())
3271 ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
3272
Francois Pichet783dd6e2010-11-21 06:08:52 +00003273 ToIndirectField->setAccess(D->getAccess());
3274 ToIndirectField->setLexicalDeclContext(LexicalDC);
3275 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00003276 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003277 return ToIndirectField;
3278}
3279
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003280Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
3281 // Import the major distinguishing characteristics of an ivar.
3282 DeclContext *DC, *LexicalDC;
3283 DeclarationName Name;
3284 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003285 NamedDecl *ToD;
3286 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003287 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003288 if (ToD)
3289 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003290
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003291 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003292 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003293 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003294 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3295 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003296 if (Importer.IsStructurallyEquivalent(D->getType(),
3297 FoundIvar->getType())) {
3298 Importer.Imported(D, FoundIvar);
3299 return FoundIvar;
3300 }
3301
3302 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
3303 << Name << D->getType() << FoundIvar->getType();
3304 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3305 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003306 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003307 }
3308 }
3309
3310 // Import the type.
3311 QualType T = Importer.Import(D->getType());
3312 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003313 return nullptr;
3314
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003315 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3316 Expr *BitWidth = Importer.Import(D->getBitWidth());
3317 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00003318 return nullptr;
3319
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00003320 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
3321 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003322 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003323 Loc, Name.getAsIdentifierInfo(),
3324 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003325 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003326 ToIvar->setLexicalDeclContext(LexicalDC);
3327 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003328 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003329 return ToIvar;
3330
3331}
3332
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003333Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
3334 // Import the major distinguishing characteristics of a variable.
3335 DeclContext *DC, *LexicalDC;
3336 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003337 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003338 NamedDecl *ToD;
3339 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003340 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003341 if (ToD)
3342 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003343
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003344 // Try to find a variable in our own ("to") context with the same name and
3345 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00003346 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00003347 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003348 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003349 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003350 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003351 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003352 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3353 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003354 continue;
3355
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003356 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003357 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00003358 if (FoundVar->hasExternalFormalLinkage() &&
3359 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003360 if (Importer.IsStructurallyEquivalent(D->getType(),
3361 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003362 MergeWithVar = FoundVar;
3363 break;
3364 }
3365
Douglas Gregor56521c52010-02-12 17:23:39 +00003366 const ArrayType *FoundArray
3367 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3368 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00003369 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00003370 if (FoundArray && TArray) {
3371 if (isa<IncompleteArrayType>(FoundArray) &&
3372 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003373 // Import the type.
3374 QualType T = Importer.Import(D->getType());
3375 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003376 return nullptr;
3377
Douglas Gregor56521c52010-02-12 17:23:39 +00003378 FoundVar->setType(T);
3379 MergeWithVar = FoundVar;
3380 break;
3381 } else if (isa<IncompleteArrayType>(TArray) &&
3382 isa<ConstantArrayType>(FoundArray)) {
3383 MergeWithVar = FoundVar;
3384 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003385 }
3386 }
3387
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003388 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003389 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003390 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3391 << FoundVar->getType();
3392 }
3393 }
3394
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003395 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003396 }
3397
3398 if (MergeWithVar) {
3399 // An equivalent variable with external linkage has been found. Link
3400 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003401 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003402
3403 if (VarDecl *DDef = D->getDefinition()) {
3404 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
3405 Importer.ToDiag(ExistingDef->getLocation(),
3406 diag::err_odr_variable_multiple_def)
3407 << Name;
3408 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
3409 } else {
3410 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00003411 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00003412 if (DDef->isInitKnownICE()) {
3413 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
3414 Eval->CheckedICE = true;
3415 Eval->IsICE = DDef->isInitICE();
3416 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003417 }
3418 }
3419
3420 return MergeWithVar;
3421 }
3422
3423 if (!ConflictingDecls.empty()) {
3424 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3425 ConflictingDecls.data(),
3426 ConflictingDecls.size());
3427 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003428 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003429 }
3430 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003431
Douglas Gregorb4964f72010-02-15 23:54:17 +00003432 // Import the type.
3433 QualType T = Importer.Import(D->getType());
3434 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003435 return nullptr;
3436
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003437 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003438 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00003439 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
3440 Importer.Import(D->getInnerLocStart()),
3441 Loc, Name.getAsIdentifierInfo(),
3442 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003443 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00003444 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003445 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003446 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003447 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003448 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003449
Sean Callanan59721b32015-04-28 18:41:46 +00003450 if (!D->isFileVarDecl() &&
3451 D->isUsed())
3452 ToVar->setIsUsed();
3453
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003454 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00003455 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00003456 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003457
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003458 return ToVar;
3459}
3460
Douglas Gregor8b228d72010-02-17 21:22:52 +00003461Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
3462 // Parameters are created in the translation unit's context, then moved
3463 // into the function declaration's context afterward.
3464 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3465
3466 // Import the name of this declaration.
3467 DeclarationName Name = Importer.Import(D->getDeclName());
3468 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003469 return nullptr;
3470
Douglas Gregor8b228d72010-02-17 21:22:52 +00003471 // Import the location of this declaration.
3472 SourceLocation Loc = Importer.Import(D->getLocation());
3473
3474 // Import the parameter's type.
3475 QualType T = Importer.Import(D->getType());
3476 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003477 return nullptr;
3478
Douglas Gregor8b228d72010-02-17 21:22:52 +00003479 // Create the imported parameter.
3480 ImplicitParamDecl *ToParm
3481 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
3482 Loc, Name.getAsIdentifierInfo(),
3483 T);
3484 return Importer.Imported(D, ToParm);
3485}
3486
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003487Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
3488 // Parameters are created in the translation unit's context, then moved
3489 // into the function declaration's context afterward.
3490 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3491
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003492 // Import the name of this declaration.
3493 DeclarationName Name = Importer.Import(D->getDeclName());
3494 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003495 return nullptr;
3496
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003497 // Import the location of this declaration.
3498 SourceLocation Loc = Importer.Import(D->getLocation());
3499
3500 // Import the parameter's type.
3501 QualType T = Importer.Import(D->getType());
3502 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003503 return nullptr;
3504
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003505 // Create the imported parameter.
3506 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3507 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003508 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003509 Loc, Name.getAsIdentifierInfo(),
3510 T, TInfo, D->getStorageClass(),
Craig Topper36250ad2014-05-12 05:36:57 +00003511 /*FIXME: Default argument*/nullptr);
John McCallf3cd6652010-03-12 18:31:32 +00003512 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Sean Callanan59721b32015-04-28 18:41:46 +00003513
3514 if (D->isUsed())
3515 ToParm->setIsUsed();
3516
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003517 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003518}
3519
Douglas Gregor43f54792010-02-17 02:12:47 +00003520Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3521 // Import the major distinguishing characteristics of a method.
3522 DeclContext *DC, *LexicalDC;
3523 DeclarationName Name;
3524 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003525 NamedDecl *ToD;
3526 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003527 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003528 if (ToD)
3529 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003530
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003531 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003532 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003533 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3534 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003535 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3536 continue;
3537
3538 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003539 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3540 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003541 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003542 << D->isInstanceMethod() << Name << D->getReturnType()
3543 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00003544 Importer.ToDiag(FoundMethod->getLocation(),
3545 diag::note_odr_objc_method_here)
3546 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003547 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003548 }
3549
3550 // Check the number of parameters.
3551 if (D->param_size() != FoundMethod->param_size()) {
3552 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3553 << D->isInstanceMethod() << Name
3554 << D->param_size() << FoundMethod->param_size();
3555 Importer.ToDiag(FoundMethod->getLocation(),
3556 diag::note_odr_objc_method_here)
3557 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003558 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003559 }
3560
3561 // Check parameter types.
3562 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
3563 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3564 P != PEnd; ++P, ++FoundP) {
3565 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3566 (*FoundP)->getType())) {
3567 Importer.FromDiag((*P)->getLocation(),
3568 diag::err_odr_objc_method_param_type_inconsistent)
3569 << D->isInstanceMethod() << Name
3570 << (*P)->getType() << (*FoundP)->getType();
3571 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3572 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003573 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003574 }
3575 }
3576
3577 // Check variadic/non-variadic.
3578 // Check the number of parameters.
3579 if (D->isVariadic() != FoundMethod->isVariadic()) {
3580 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3581 << D->isInstanceMethod() << Name;
3582 Importer.ToDiag(FoundMethod->getLocation(),
3583 diag::note_odr_objc_method_here)
3584 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003585 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003586 }
3587
3588 // FIXME: Any other bits we need to merge?
3589 return Importer.Imported(D, FoundMethod);
3590 }
3591 }
3592
3593 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00003594 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003595 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003596 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003597
Alp Toker314cc812014-01-25 16:55:45 +00003598 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00003599
Alp Toker314cc812014-01-25 16:55:45 +00003600 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
3601 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3602 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3603 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3604 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003605
3606 // FIXME: When we decide to merge method definitions, we'll need to
3607 // deal with implicit parameters.
3608
3609 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003610 SmallVector<ParmVarDecl *, 5> ToParams;
Aaron Ballman43b68be2014-03-07 17:50:17 +00003611 for (auto *FromP : D->params()) {
3612 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00003613 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003614 return nullptr;
3615
Douglas Gregor43f54792010-02-17 02:12:47 +00003616 ToParams.push_back(ToP);
3617 }
3618
3619 // Set the parameters.
3620 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3621 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003622 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003623 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003624 SmallVector<SourceLocation, 12> SelLocs;
3625 D->getSelectorLocs(SelLocs);
3626 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003627
3628 ToMethod->setLexicalDeclContext(LexicalDC);
3629 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003630 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003631 return ToMethod;
3632}
3633
Douglas Gregor85f3f952015-07-07 03:57:15 +00003634Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3635 // Import the major distinguishing characteristics of a category.
3636 DeclContext *DC, *LexicalDC;
3637 DeclarationName Name;
3638 SourceLocation Loc;
3639 NamedDecl *ToD;
3640 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3641 return nullptr;
3642 if (ToD)
3643 return ToD;
3644
3645 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3646 if (!BoundInfo)
3647 return nullptr;
3648
3649 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
3650 Importer.getToContext(), DC,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00003651 D->getVariance(),
3652 Importer.Import(D->getVarianceLoc()),
Douglas Gregore83b9562015-07-07 03:57:53 +00003653 D->getIndex(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003654 Importer.Import(D->getLocation()),
3655 Name.getAsIdentifierInfo(),
3656 Importer.Import(D->getColonLoc()),
3657 BoundInfo);
3658 Importer.Imported(D, Result);
3659 Result->setLexicalDeclContext(LexicalDC);
3660 return Result;
3661}
3662
Douglas Gregor84c51c32010-02-18 01:47:50 +00003663Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3664 // Import the major distinguishing characteristics of a category.
3665 DeclContext *DC, *LexicalDC;
3666 DeclarationName Name;
3667 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003668 NamedDecl *ToD;
3669 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003670 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003671 if (ToD)
3672 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003673
Douglas Gregor84c51c32010-02-18 01:47:50 +00003674 ObjCInterfaceDecl *ToInterface
3675 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3676 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00003677 return nullptr;
3678
Douglas Gregor84c51c32010-02-18 01:47:50 +00003679 // Determine if we've already encountered this category.
3680 ObjCCategoryDecl *MergeWithCategory
3681 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3682 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3683 if (!ToCategory) {
3684 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003685 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003686 Loc,
3687 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003688 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003689 ToInterface,
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003690 /*TypeParamList=*/nullptr,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003691 Importer.Import(D->getIvarLBraceLoc()),
3692 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003693 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003694 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003695 Importer.Imported(D, ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003696 // Import the type parameter list after calling Imported, to avoid
3697 // loops when bringing in their DeclContext.
3698 ToCategory->setTypeParamList(ImportObjCTypeParamList(
3699 D->getTypeParamList()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003700
Douglas Gregor84c51c32010-02-18 01:47:50 +00003701 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003702 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3703 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003704 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3705 = D->protocol_loc_begin();
3706 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3707 FromProtoEnd = D->protocol_end();
3708 FromProto != FromProtoEnd;
3709 ++FromProto, ++FromProtoLoc) {
3710 ObjCProtocolDecl *ToProto
3711 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3712 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00003713 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003714 Protocols.push_back(ToProto);
3715 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3716 }
3717
3718 // FIXME: If we're merging, make sure that the protocol list is the same.
3719 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3720 ProtocolLocs.data(), Importer.getToContext());
3721
3722 } else {
3723 Importer.Imported(D, ToCategory);
3724 }
3725
3726 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003727 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003728
3729 // If we have an implementation, import it as well.
3730 if (D->getImplementation()) {
3731 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003732 = cast_or_null<ObjCCategoryImplDecl>(
3733 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003734 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00003735 return nullptr;
3736
Douglas Gregor84c51c32010-02-18 01:47:50 +00003737 ToCategory->setImplementation(Impl);
3738 }
3739
3740 return ToCategory;
3741}
3742
Douglas Gregor2aa53772012-01-24 17:42:07 +00003743bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3744 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003745 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003746 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003747 if (shouldForceImportDeclContext(Kind))
3748 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003749 return false;
3750 }
3751
3752 // Start the protocol definition
3753 To->startDefinition();
3754
3755 // Import protocols
3756 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3757 SmallVector<SourceLocation, 4> ProtocolLocs;
3758 ObjCProtocolDecl::protocol_loc_iterator
3759 FromProtoLoc = From->protocol_loc_begin();
3760 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3761 FromProtoEnd = From->protocol_end();
3762 FromProto != FromProtoEnd;
3763 ++FromProto, ++FromProtoLoc) {
3764 ObjCProtocolDecl *ToProto
3765 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3766 if (!ToProto)
3767 return true;
3768 Protocols.push_back(ToProto);
3769 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3770 }
3771
3772 // FIXME: If we're merging, make sure that the protocol list is the same.
3773 To->setProtocolList(Protocols.data(), Protocols.size(),
3774 ProtocolLocs.data(), Importer.getToContext());
3775
Douglas Gregor2e15c842012-02-01 21:00:38 +00003776 if (shouldForceImportDeclContext(Kind)) {
3777 // Import all of the members of this protocol.
3778 ImportDeclContext(From, /*ForceImport=*/true);
3779 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003780 return false;
3781}
3782
Douglas Gregor98d156a2010-02-17 16:12:00 +00003783Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003784 // If this protocol has a definition in the translation unit we're coming
3785 // from, but this particular declaration is not that definition, import the
3786 // definition and map to that.
3787 ObjCProtocolDecl *Definition = D->getDefinition();
3788 if (Definition && Definition != D) {
3789 Decl *ImportedDef = Importer.Import(Definition);
3790 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003791 return nullptr;
3792
Douglas Gregor2aa53772012-01-24 17:42:07 +00003793 return Importer.Imported(D, ImportedDef);
3794 }
3795
Douglas Gregor84c51c32010-02-18 01:47:50 +00003796 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003797 DeclContext *DC, *LexicalDC;
3798 DeclarationName Name;
3799 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003800 NamedDecl *ToD;
3801 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003802 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003803 if (ToD)
3804 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00003805
Craig Topper36250ad2014-05-12 05:36:57 +00003806 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003807 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003808 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003809 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3810 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003811 continue;
3812
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003813 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003814 break;
3815 }
3816
3817 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003818 if (!ToProto) {
3819 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3820 Name.getAsIdentifierInfo(), Loc,
3821 Importer.Import(D->getAtStartLoc()),
Craig Topper36250ad2014-05-12 05:36:57 +00003822 /*PrevDecl=*/nullptr);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003823 ToProto->setLexicalDeclContext(LexicalDC);
3824 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003825 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003826
3827 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003828
Douglas Gregor2aa53772012-01-24 17:42:07 +00003829 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00003830 return nullptr;
3831
Douglas Gregor98d156a2010-02-17 16:12:00 +00003832 return ToProto;
3833}
3834
Sean Callanan0aae0412014-12-10 00:00:37 +00003835Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3836 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3837 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3838
3839 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3840 SourceLocation LangLoc = Importer.Import(D->getLocation());
3841
3842 bool HasBraces = D->hasBraces();
3843
Sean Callananb12a8552014-12-10 21:22:20 +00003844 LinkageSpecDecl *ToLinkageSpec =
3845 LinkageSpecDecl::Create(Importer.getToContext(),
3846 DC,
3847 ExternLoc,
3848 LangLoc,
3849 D->getLanguage(),
3850 HasBraces);
Sean Callanan0aae0412014-12-10 00:00:37 +00003851
3852 if (HasBraces) {
3853 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3854 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3855 }
3856
3857 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3858 LexicalDC->addDeclInternal(ToLinkageSpec);
3859
3860 Importer.Imported(D, ToLinkageSpec);
3861
3862 return ToLinkageSpec;
3863}
3864
Douglas Gregor2aa53772012-01-24 17:42:07 +00003865bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3866 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003867 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003868 if (To->getDefinition()) {
3869 // Check consistency of superclass.
3870 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3871 if (FromSuper) {
3872 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3873 if (!FromSuper)
3874 return true;
3875 }
3876
3877 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3878 if ((bool)FromSuper != (bool)ToSuper ||
3879 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3880 Importer.ToDiag(To->getLocation(),
3881 diag::err_odr_objc_superclass_inconsistent)
3882 << To->getDeclName();
3883 if (ToSuper)
3884 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3885 << To->getSuperClass()->getDeclName();
3886 else
3887 Importer.ToDiag(To->getLocation(),
3888 diag::note_odr_objc_missing_superclass);
3889 if (From->getSuperClass())
3890 Importer.FromDiag(From->getSuperClassLoc(),
3891 diag::note_odr_objc_superclass)
3892 << From->getSuperClass()->getDeclName();
3893 else
3894 Importer.FromDiag(From->getLocation(),
3895 diag::note_odr_objc_missing_superclass);
3896 }
3897
Douglas Gregor2e15c842012-02-01 21:00:38 +00003898 if (shouldForceImportDeclContext(Kind))
3899 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003900 return false;
3901 }
3902
3903 // Start the definition.
3904 To->startDefinition();
3905
3906 // If this class has a superclass, import it.
3907 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00003908 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3909 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00003910 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00003911
3912 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003913 }
3914
3915 // Import protocols
3916 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3917 SmallVector<SourceLocation, 4> ProtocolLocs;
3918 ObjCInterfaceDecl::protocol_loc_iterator
3919 FromProtoLoc = From->protocol_loc_begin();
3920
3921 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3922 FromProtoEnd = From->protocol_end();
3923 FromProto != FromProtoEnd;
3924 ++FromProto, ++FromProtoLoc) {
3925 ObjCProtocolDecl *ToProto
3926 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3927 if (!ToProto)
3928 return true;
3929 Protocols.push_back(ToProto);
3930 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3931 }
3932
3933 // FIXME: If we're merging, make sure that the protocol list is the same.
3934 To->setProtocolList(Protocols.data(), Protocols.size(),
3935 ProtocolLocs.data(), Importer.getToContext());
3936
3937 // Import categories. When the categories themselves are imported, they'll
3938 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00003939 for (auto *Cat : From->known_categories())
3940 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003941
Douglas Gregor2aa53772012-01-24 17:42:07 +00003942 // If we have an @implementation, import it as well.
3943 if (From->getImplementation()) {
3944 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3945 Importer.Import(From->getImplementation()));
3946 if (!Impl)
3947 return true;
3948
3949 To->setImplementation(Impl);
3950 }
3951
Douglas Gregor2e15c842012-02-01 21:00:38 +00003952 if (shouldForceImportDeclContext(Kind)) {
3953 // Import all of the members of this class.
3954 ImportDeclContext(From, /*ForceImport=*/true);
3955 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003956 return false;
3957}
3958
Douglas Gregor85f3f952015-07-07 03:57:15 +00003959ObjCTypeParamList *
3960ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3961 if (!list)
3962 return nullptr;
3963
3964 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
3965 for (auto fromTypeParam : *list) {
3966 auto toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3967 Importer.Import(fromTypeParam));
3968 if (!toTypeParam)
3969 return nullptr;
3970
3971 toTypeParams.push_back(toTypeParam);
3972 }
3973
3974 return ObjCTypeParamList::create(Importer.getToContext(),
3975 Importer.Import(list->getLAngleLoc()),
3976 toTypeParams,
3977 Importer.Import(list->getRAngleLoc()));
3978}
3979
Douglas Gregor45635322010-02-16 01:20:57 +00003980Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003981 // If this class has a definition in the translation unit we're coming from,
3982 // but this particular declaration is not that definition, import the
3983 // definition and map to that.
3984 ObjCInterfaceDecl *Definition = D->getDefinition();
3985 if (Definition && Definition != D) {
3986 Decl *ImportedDef = Importer.Import(Definition);
3987 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003988 return nullptr;
3989
Douglas Gregor2aa53772012-01-24 17:42:07 +00003990 return Importer.Imported(D, ImportedDef);
3991 }
3992
Douglas Gregor45635322010-02-16 01:20:57 +00003993 // Import the major distinguishing characteristics of an @interface.
3994 DeclContext *DC, *LexicalDC;
3995 DeclarationName Name;
3996 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003997 NamedDecl *ToD;
3998 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003999 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004000 if (ToD)
4001 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00004002
Douglas Gregor2aa53772012-01-24 17:42:07 +00004003 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00004004 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004005 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004006 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004007 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4008 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00004009 continue;
4010
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004011 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00004012 break;
4013 }
4014
Douglas Gregor2aa53772012-01-24 17:42:07 +00004015 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00004016 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004017 if (!ToIface) {
4018 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
4019 Importer.Import(D->getAtStartLoc()),
Douglas Gregor85f3f952015-07-07 03:57:15 +00004020 Name.getAsIdentifierInfo(),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004021 /*TypeParamList=*/nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00004022 /*PrevDecl=*/nullptr, Loc,
Douglas Gregor2aa53772012-01-24 17:42:07 +00004023 D->isImplicitInterfaceDecl());
4024 ToIface->setLexicalDeclContext(LexicalDC);
4025 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00004026 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00004027 Importer.Imported(D, ToIface);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004028 // Import the type parameter list after calling Imported, to avoid
4029 // loops when bringing in their DeclContext.
4030 ToIface->setTypeParamList(ImportObjCTypeParamList(
4031 D->getTypeParamListAsWritten()));
Douglas Gregor45635322010-02-16 01:20:57 +00004032
Douglas Gregor2aa53772012-01-24 17:42:07 +00004033 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00004034 return nullptr;
4035
Douglas Gregor98d156a2010-02-17 16:12:00 +00004036 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00004037}
4038
Douglas Gregor4da9d682010-12-07 15:32:12 +00004039Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
4040 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
4041 Importer.Import(D->getCategoryDecl()));
4042 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00004043 return nullptr;
4044
Douglas Gregor4da9d682010-12-07 15:32:12 +00004045 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
4046 if (!ToImpl) {
4047 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4048 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004049 return nullptr;
4050
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00004051 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00004052 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00004053 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00004054 Category->getClassInterface(),
4055 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00004056 Importer.Import(D->getAtStartLoc()),
4057 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004058
4059 DeclContext *LexicalDC = DC;
4060 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4061 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4062 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004063 return nullptr;
4064
Douglas Gregor4da9d682010-12-07 15:32:12 +00004065 ToImpl->setLexicalDeclContext(LexicalDC);
4066 }
4067
Sean Callanan95e74be2011-10-21 02:57:43 +00004068 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004069 Category->setImplementation(ToImpl);
4070 }
4071
4072 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00004073 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004074 return ToImpl;
4075}
4076
Douglas Gregorda8025c2010-12-07 01:26:03 +00004077Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
4078 // Find the corresponding interface.
4079 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
4080 Importer.Import(D->getClassInterface()));
4081 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00004082 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004083
4084 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00004085 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004086 if (D->getSuperClass()) {
4087 Super = cast_or_null<ObjCInterfaceDecl>(
4088 Importer.Import(D->getSuperClass()));
4089 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00004090 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004091 }
4092
4093 ObjCImplementationDecl *Impl = Iface->getImplementation();
4094 if (!Impl) {
4095 // We haven't imported an implementation yet. Create a new @implementation
4096 // now.
4097 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
4098 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00004099 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00004100 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00004101 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00004102 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00004103 Importer.Import(D->getIvarLBraceLoc()),
4104 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00004105
4106 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4107 DeclContext *LexicalDC
4108 = Importer.ImportContext(D->getLexicalDeclContext());
4109 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004110 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004111 Impl->setLexicalDeclContext(LexicalDC);
4112 }
4113
4114 // Associate the implementation with the class it implements.
4115 Iface->setImplementation(Impl);
4116 Importer.Imported(D, Iface->getImplementation());
4117 } else {
4118 Importer.Imported(D, Iface->getImplementation());
4119
4120 // Verify that the existing @implementation has the same superclass.
4121 if ((Super && !Impl->getSuperClass()) ||
4122 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004123 (Super && Impl->getSuperClass() &&
4124 !declaresSameEntity(Super->getCanonicalDecl(),
4125 Impl->getSuperClass()))) {
4126 Importer.ToDiag(Impl->getLocation(),
4127 diag::err_odr_objc_superclass_inconsistent)
4128 << Iface->getDeclName();
4129 // FIXME: It would be nice to have the location of the superclass
4130 // below.
4131 if (Impl->getSuperClass())
4132 Importer.ToDiag(Impl->getLocation(),
4133 diag::note_odr_objc_superclass)
4134 << Impl->getSuperClass()->getDeclName();
4135 else
4136 Importer.ToDiag(Impl->getLocation(),
4137 diag::note_odr_objc_missing_superclass);
4138 if (D->getSuperClass())
4139 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004140 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004141 << D->getSuperClass()->getDeclName();
4142 else
4143 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004144 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00004145 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004146 }
4147 }
4148
4149 // Import all of the members of this @implementation.
4150 ImportDeclContext(D);
4151
4152 return Impl;
4153}
4154
Douglas Gregora11c4582010-02-17 18:02:10 +00004155Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
4156 // Import the major distinguishing characteristics of an @property.
4157 DeclContext *DC, *LexicalDC;
4158 DeclarationName Name;
4159 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004160 NamedDecl *ToD;
4161 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004162 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004163 if (ToD)
4164 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004165
4166 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004167 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004168 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004169 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004170 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004171 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004172 // Check property types.
4173 if (!Importer.IsStructurallyEquivalent(D->getType(),
4174 FoundProp->getType())) {
4175 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
4176 << Name << D->getType() << FoundProp->getType();
4177 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4178 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00004179 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004180 }
4181
4182 // FIXME: Check property attributes, getters, setters, etc.?
4183
4184 // Consider these properties to be equivalent.
4185 Importer.Imported(D, FoundProp);
4186 return FoundProp;
4187 }
4188 }
4189
4190 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00004191 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
4192 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00004193 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004194
4195 // Create the new property.
4196 ObjCPropertyDecl *ToProperty
4197 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
4198 Name.getAsIdentifierInfo(),
4199 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00004200 Importer.Import(D->getLParenLoc()),
Douglas Gregor813a0662015-06-19 18:14:38 +00004201 Importer.Import(D->getType()),
4202 TSI,
Douglas Gregora11c4582010-02-17 18:02:10 +00004203 D->getPropertyImplementation());
4204 Importer.Imported(D, ToProperty);
4205 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004206 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004207
4208 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004209 ToProperty->setPropertyAttributesAsWritten(
4210 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00004211 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
4212 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
4213 ToProperty->setGetterMethodDecl(
4214 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
4215 ToProperty->setSetterMethodDecl(
4216 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
4217 ToProperty->setPropertyIvarDecl(
4218 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
4219 return ToProperty;
4220}
4221
Douglas Gregor14a49e22010-12-07 18:32:03 +00004222Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4223 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
4224 Importer.Import(D->getPropertyDecl()));
4225 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00004226 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004227
4228 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4229 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004230 return nullptr;
4231
Douglas Gregor14a49e22010-12-07 18:32:03 +00004232 // Import the lexical declaration context.
4233 DeclContext *LexicalDC = DC;
4234 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4235 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4236 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004237 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004238 }
4239
4240 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
4241 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00004242 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004243
4244 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004245 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004246 if (D->getPropertyIvarDecl()) {
4247 Ivar = cast_or_null<ObjCIvarDecl>(
4248 Importer.Import(D->getPropertyIvarDecl()));
4249 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00004250 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004251 }
4252
4253 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004254 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4255 Property->getQueryKind());
Douglas Gregor14a49e22010-12-07 18:32:03 +00004256 if (!ToImpl) {
4257 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
4258 Importer.Import(D->getLocStart()),
4259 Importer.Import(D->getLocation()),
4260 Property,
4261 D->getPropertyImplementation(),
4262 Ivar,
4263 Importer.Import(D->getPropertyIvarDeclLoc()));
4264 ToImpl->setLexicalDeclContext(LexicalDC);
4265 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00004266 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004267 } else {
4268 // Check that we have the same kind of property implementation (@synthesize
4269 // vs. @dynamic).
4270 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
4271 Importer.ToDiag(ToImpl->getLocation(),
4272 diag::err_odr_objc_property_impl_kind_inconsistent)
4273 << Property->getDeclName()
4274 << (ToImpl->getPropertyImplementation()
4275 == ObjCPropertyImplDecl::Dynamic);
4276 Importer.FromDiag(D->getLocation(),
4277 diag::note_odr_objc_property_impl_kind)
4278 << D->getPropertyDecl()->getDeclName()
4279 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00004280 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004281 }
4282
4283 // For @synthesize, check that we have the same
4284 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4285 Ivar != ToImpl->getPropertyIvarDecl()) {
4286 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
4287 diag::err_odr_objc_synthesize_ivar_inconsistent)
4288 << Property->getDeclName()
4289 << ToImpl->getPropertyIvarDecl()->getDeclName()
4290 << Ivar->getDeclName();
4291 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
4292 diag::note_odr_objc_synthesize_ivar_here)
4293 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00004294 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004295 }
4296
4297 // Merge the existing implementation with the new implementation.
4298 Importer.Imported(D, ToImpl);
4299 }
4300
4301 return ToImpl;
4302}
4303
Douglas Gregora082a492010-11-30 19:14:50 +00004304Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
4305 // For template arguments, we adopt the translation unit as our declaration
4306 // context. This context will be fixed when the actual template declaration
4307 // is created.
4308
4309 // FIXME: Import default argument.
4310 return TemplateTypeParmDecl::Create(Importer.getToContext(),
4311 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00004312 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004313 Importer.Import(D->getLocation()),
4314 D->getDepth(),
4315 D->getIndex(),
4316 Importer.Import(D->getIdentifier()),
4317 D->wasDeclaredWithTypename(),
4318 D->isParameterPack());
4319}
4320
4321Decl *
4322ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
4323 // Import the name of this declaration.
4324 DeclarationName Name = Importer.Import(D->getDeclName());
4325 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004326 return nullptr;
4327
Douglas Gregora082a492010-11-30 19:14:50 +00004328 // Import the location of this declaration.
4329 SourceLocation Loc = Importer.Import(D->getLocation());
4330
4331 // Import the type of this declaration.
4332 QualType T = Importer.Import(D->getType());
4333 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004334 return nullptr;
4335
Douglas Gregora082a492010-11-30 19:14:50 +00004336 // Import type-source information.
4337 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4338 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00004339 return nullptr;
4340
Douglas Gregora082a492010-11-30 19:14:50 +00004341 // FIXME: Import default argument.
4342
4343 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
4344 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004345 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004346 Loc, D->getDepth(), D->getPosition(),
4347 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00004348 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00004349}
4350
4351Decl *
4352ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4353 // Import the name of this declaration.
4354 DeclarationName Name = Importer.Import(D->getDeclName());
4355 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004356 return nullptr;
4357
Douglas Gregora082a492010-11-30 19:14:50 +00004358 // Import the location of this declaration.
4359 SourceLocation Loc = Importer.Import(D->getLocation());
4360
4361 // Import template parameters.
4362 TemplateParameterList *TemplateParams
4363 = ImportTemplateParameterList(D->getTemplateParameters());
4364 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004365 return nullptr;
4366
Douglas Gregora082a492010-11-30 19:14:50 +00004367 // FIXME: Import default argument.
4368
4369 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
4370 Importer.getToContext().getTranslationUnitDecl(),
4371 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00004372 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00004373 Name.getAsIdentifierInfo(),
4374 TemplateParams);
4375}
4376
4377Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
4378 // If this record has a definition in the translation unit we're coming from,
4379 // but this particular declaration is not that definition, import the
4380 // definition and map to that.
4381 CXXRecordDecl *Definition
4382 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
4383 if (Definition && Definition != D->getTemplatedDecl()) {
4384 Decl *ImportedDef
4385 = Importer.Import(Definition->getDescribedClassTemplate());
4386 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004387 return nullptr;
4388
Douglas Gregora082a492010-11-30 19:14:50 +00004389 return Importer.Imported(D, ImportedDef);
4390 }
4391
4392 // Import the major distinguishing characteristics of this class template.
4393 DeclContext *DC, *LexicalDC;
4394 DeclarationName Name;
4395 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004396 NamedDecl *ToD;
4397 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004398 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004399 if (ToD)
4400 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004401
Douglas Gregora082a492010-11-30 19:14:50 +00004402 // We may already have a template of the same name; try to find and match it.
4403 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004404 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004405 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004406 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004407 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4408 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00004409 continue;
4410
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004411 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00004412 if (ClassTemplateDecl *FoundTemplate
4413 = dyn_cast<ClassTemplateDecl>(Found)) {
4414 if (IsStructuralMatch(D, FoundTemplate)) {
4415 // The class templates structurally match; call it the same template.
4416 // FIXME: We may be filling in a forward declaration here. Handle
4417 // this case!
4418 Importer.Imported(D->getTemplatedDecl(),
4419 FoundTemplate->getTemplatedDecl());
4420 return Importer.Imported(D, FoundTemplate);
4421 }
4422 }
4423
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004424 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00004425 }
4426
4427 if (!ConflictingDecls.empty()) {
4428 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4429 ConflictingDecls.data(),
4430 ConflictingDecls.size());
4431 }
4432
4433 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004434 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00004435 }
4436
4437 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
4438
4439 // Create the declaration that is being templated.
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004440 // Create the declaration that is being templated.
4441 CXXRecordDecl *D2Templated = cast_or_null<CXXRecordDecl>(
4442 Importer.Import(DTemplated));
4443 if (!D2Templated)
4444 return nullptr;
4445
4446 // Resolve possible cyclic import.
4447 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
4448 return AlreadyImported;
4449
Douglas Gregora082a492010-11-30 19:14:50 +00004450 // Create the class template declaration itself.
4451 TemplateParameterList *TemplateParams
4452 = ImportTemplateParameterList(D->getTemplateParameters());
4453 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004454 return nullptr;
4455
Douglas Gregora082a492010-11-30 19:14:50 +00004456 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4457 Loc, Name, TemplateParams,
4458 D2Templated,
Craig Topper36250ad2014-05-12 05:36:57 +00004459 /*PrevDecl=*/nullptr);
Douglas Gregora082a492010-11-30 19:14:50 +00004460 D2Templated->setDescribedClassTemplate(D2);
4461
4462 D2->setAccess(D->getAccess());
4463 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004464 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004465
4466 // Note the relationship between the class templates.
4467 Importer.Imported(D, D2);
4468 Importer.Imported(DTemplated, D2Templated);
4469
John McCallf937c022011-10-07 06:10:15 +00004470 if (DTemplated->isCompleteDefinition() &&
4471 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004472 // FIXME: Import definition!
4473 }
4474
4475 return D2;
4476}
4477
Douglas Gregore2e50d332010-12-01 01:36:18 +00004478Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4479 ClassTemplateSpecializationDecl *D) {
4480 // If this record has a definition in the translation unit we're coming from,
4481 // but this particular declaration is not that definition, import the
4482 // definition and map to that.
4483 TagDecl *Definition = D->getDefinition();
4484 if (Definition && Definition != D) {
4485 Decl *ImportedDef = Importer.Import(Definition);
4486 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004487 return nullptr;
4488
Douglas Gregore2e50d332010-12-01 01:36:18 +00004489 return Importer.Imported(D, ImportedDef);
4490 }
4491
4492 ClassTemplateDecl *ClassTemplate
4493 = cast_or_null<ClassTemplateDecl>(Importer.Import(
4494 D->getSpecializedTemplate()));
4495 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004496 return nullptr;
4497
Douglas Gregore2e50d332010-12-01 01:36:18 +00004498 // Import the context of this declaration.
4499 DeclContext *DC = ClassTemplate->getDeclContext();
4500 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004501 return nullptr;
4502
Douglas Gregore2e50d332010-12-01 01:36:18 +00004503 DeclContext *LexicalDC = DC;
4504 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4505 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4506 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004507 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004508 }
4509
4510 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004511 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4512 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004513
4514 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004515 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004516 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4517 D->getTemplateArgs().size(),
4518 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004519 return nullptr;
4520
Douglas Gregore2e50d332010-12-01 01:36:18 +00004521 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004522 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004523 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004524 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004525 if (D2) {
4526 // We already have a class template specialization with these template
4527 // arguments.
4528
4529 // FIXME: Check for specialization vs. instantiation errors.
4530
4531 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004532 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004533 // The record types structurally match, or the "from" translation
4534 // unit only had a forward declaration anyway; call it the same
4535 // function.
4536 return Importer.Imported(D, FoundDef);
4537 }
4538 }
4539 } else {
4540 // Create a new specialization.
4541 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4542 D->getTagKind(), DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004543 StartLoc, IdLoc,
4544 ClassTemplate,
Douglas Gregore2e50d332010-12-01 01:36:18 +00004545 TemplateArgs.data(),
4546 TemplateArgs.size(),
Craig Topper36250ad2014-05-12 05:36:57 +00004547 /*PrevDecl=*/nullptr);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004548 D2->setSpecializationKind(D->getSpecializationKind());
4549
4550 // Add this specialization to the class template.
4551 ClassTemplate->AddSpecialization(D2, InsertPos);
4552
4553 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004554 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00004555
4556 // Add the specialization to this context.
4557 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004558 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004559 }
4560 Importer.Imported(D, D2);
4561
John McCallf937c022011-10-07 06:10:15 +00004562 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004563 return nullptr;
4564
Douglas Gregore2e50d332010-12-01 01:36:18 +00004565 return D2;
4566}
4567
Larisse Voufo39a1e502013-08-06 01:03:05 +00004568Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4569 // If this variable has a definition in the translation unit we're coming
4570 // from,
4571 // but this particular declaration is not that definition, import the
4572 // definition and map to that.
4573 VarDecl *Definition =
4574 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4575 if (Definition && Definition != D->getTemplatedDecl()) {
4576 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4577 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004578 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004579
4580 return Importer.Imported(D, ImportedDef);
4581 }
4582
4583 // Import the major distinguishing characteristics of this variable template.
4584 DeclContext *DC, *LexicalDC;
4585 DeclarationName Name;
4586 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004587 NamedDecl *ToD;
4588 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004589 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004590 if (ToD)
4591 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004592
4593 // We may already have a template of the same name; try to find and match it.
4594 assert(!DC->isFunctionOrMethod() &&
4595 "Variable templates cannot be declared at function scope");
4596 SmallVector<NamedDecl *, 4> ConflictingDecls;
4597 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004598 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004599 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4600 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4601 continue;
4602
4603 Decl *Found = FoundDecls[I];
4604 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
4605 if (IsStructuralMatch(D, FoundTemplate)) {
4606 // The variable templates structurally match; call it the same template.
4607 Importer.Imported(D->getTemplatedDecl(),
4608 FoundTemplate->getTemplatedDecl());
4609 return Importer.Imported(D, FoundTemplate);
4610 }
4611 }
4612
4613 ConflictingDecls.push_back(FoundDecls[I]);
4614 }
4615
4616 if (!ConflictingDecls.empty()) {
4617 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4618 ConflictingDecls.data(),
4619 ConflictingDecls.size());
4620 }
4621
4622 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004623 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004624
4625 VarDecl *DTemplated = D->getTemplatedDecl();
4626
4627 // Import the type.
4628 QualType T = Importer.Import(DTemplated->getType());
4629 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004630 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004631
4632 // Create the declaration that is being templated.
4633 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4634 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
4635 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo());
4636 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc,
4637 IdLoc, Name.getAsIdentifierInfo(), T,
4638 TInfo, DTemplated->getStorageClass());
4639 D2Templated->setAccess(DTemplated->getAccess());
4640 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
4641 D2Templated->setLexicalDeclContext(LexicalDC);
4642
4643 // Importer.Imported(DTemplated, D2Templated);
4644 // LexicalDC->addDeclInternal(D2Templated);
4645
4646 // Merge the initializer.
4647 if (ImportDefinition(DTemplated, D2Templated))
Craig Topper36250ad2014-05-12 05:36:57 +00004648 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004649
4650 // Create the variable template declaration itself.
4651 TemplateParameterList *TemplateParams =
4652 ImportTemplateParameterList(D->getTemplateParameters());
4653 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004654 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004655
4656 VarTemplateDecl *D2 = VarTemplateDecl::Create(
Richard Smithbeef3452014-01-16 23:39:20 +00004657 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004658 D2Templated->setDescribedVarTemplate(D2);
4659
4660 D2->setAccess(D->getAccess());
4661 D2->setLexicalDeclContext(LexicalDC);
4662 LexicalDC->addDeclInternal(D2);
4663
4664 // Note the relationship between the variable templates.
4665 Importer.Imported(D, D2);
4666 Importer.Imported(DTemplated, D2Templated);
4667
4668 if (DTemplated->isThisDeclarationADefinition() &&
4669 !D2Templated->isThisDeclarationADefinition()) {
4670 // FIXME: Import definition!
4671 }
4672
4673 return D2;
4674}
4675
4676Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4677 VarTemplateSpecializationDecl *D) {
4678 // If this record has a definition in the translation unit we're coming from,
4679 // but this particular declaration is not that definition, import the
4680 // definition and map to that.
4681 VarDecl *Definition = D->getDefinition();
4682 if (Definition && Definition != D) {
4683 Decl *ImportedDef = Importer.Import(Definition);
4684 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004685 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004686
4687 return Importer.Imported(D, ImportedDef);
4688 }
4689
4690 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
4691 Importer.Import(D->getSpecializedTemplate()));
4692 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004693 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004694
4695 // Import the context of this declaration.
4696 DeclContext *DC = VarTemplate->getDeclContext();
4697 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004698 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004699
4700 DeclContext *LexicalDC = DC;
4701 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4702 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4703 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004704 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004705 }
4706
4707 // Import the location of this declaration.
4708 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4709 SourceLocation IdLoc = Importer.Import(D->getLocation());
4710
4711 // Import template arguments.
4712 SmallVector<TemplateArgument, 2> TemplateArgs;
4713 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4714 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004715 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004716
4717 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004718 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004719 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004720 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004721 if (D2) {
4722 // We already have a variable template specialization with these template
4723 // arguments.
4724
4725 // FIXME: Check for specialization vs. instantiation errors.
4726
4727 if (VarDecl *FoundDef = D2->getDefinition()) {
4728 if (!D->isThisDeclarationADefinition() ||
4729 IsStructuralMatch(D, FoundDef)) {
4730 // The record types structurally match, or the "from" translation
4731 // unit only had a forward declaration anyway; call it the same
4732 // variable.
4733 return Importer.Imported(D, FoundDef);
4734 }
4735 }
4736 } else {
4737
4738 // Import the type.
4739 QualType T = Importer.Import(D->getType());
4740 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004741 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004742 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4743
4744 // Create a new specialization.
4745 D2 = VarTemplateSpecializationDecl::Create(
4746 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
4747 D->getStorageClass(), TemplateArgs.data(), TemplateArgs.size());
4748 D2->setSpecializationKind(D->getSpecializationKind());
4749 D2->setTemplateArgsInfo(D->getTemplateArgsInfo());
4750
4751 // Add this specialization to the class template.
4752 VarTemplate->AddSpecialization(D2, InsertPos);
4753
4754 // Import the qualifier, if any.
4755 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4756
4757 // Add the specialization to this context.
4758 D2->setLexicalDeclContext(LexicalDC);
4759 LexicalDC->addDeclInternal(D2);
4760 }
4761 Importer.Imported(D, D2);
4762
4763 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004764 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004765
4766 return D2;
4767}
4768
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004769//----------------------------------------------------------------------------
4770// Import Statements
4771//----------------------------------------------------------------------------
4772
Sean Callanan59721b32015-04-28 18:41:46 +00004773DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4774 if (DG.isNull())
4775 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4776 size_t NumDecls = DG.end() - DG.begin();
4777 SmallVector<Decl *, 1> ToDecls(NumDecls);
4778 auto &_Importer = this->Importer;
4779 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4780 [&_Importer](Decl *D) -> Decl * {
4781 return _Importer.Import(D);
4782 });
4783 return DeclGroupRef::Create(Importer.getToContext(),
4784 ToDecls.begin(),
4785 NumDecls);
4786}
4787
4788 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4789 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4790 << S->getStmtClassName();
4791 return nullptr;
4792 }
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004793
4794
4795Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
4796 SmallVector<IdentifierInfo *, 4> Names;
4797 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4798 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
4799 if (!ToII)
4800 return nullptr;
4801 Names.push_back(ToII);
4802 }
4803 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4804 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
4805 if (!ToII)
4806 return nullptr;
4807 Names.push_back(ToII);
4808 }
4809
4810 SmallVector<StringLiteral *, 4> Clobbers;
4811 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
4812 StringLiteral *Clobber = cast_or_null<StringLiteral>(
4813 Importer.Import(S->getClobberStringLiteral(I)));
4814 if (!Clobber)
4815 return nullptr;
4816 Clobbers.push_back(Clobber);
4817 }
4818
4819 SmallVector<StringLiteral *, 4> Constraints;
4820 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4821 StringLiteral *Output = cast_or_null<StringLiteral>(
4822 Importer.Import(S->getOutputConstraintLiteral(I)));
4823 if (!Output)
4824 return nullptr;
4825 Constraints.push_back(Output);
4826 }
4827
4828 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4829 StringLiteral *Input = cast_or_null<StringLiteral>(
4830 Importer.Import(S->getInputConstraintLiteral(I)));
4831 if (!Input)
4832 return nullptr;
4833 Constraints.push_back(Input);
4834 }
4835
4836 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
4837 if (ImportArrayChecked(S->begin_outputs(), S->end_outputs(), Exprs.begin()))
4838 return nullptr;
4839
4840 if (ImportArrayChecked(S->begin_inputs(), S->end_inputs(),
4841 Exprs.begin() + S->getNumOutputs()))
4842 return nullptr;
4843
4844 StringLiteral *AsmStr = cast_or_null<StringLiteral>(
4845 Importer.Import(S->getAsmString()));
4846 if (!AsmStr)
4847 return nullptr;
4848
4849 return new (Importer.getToContext()) GCCAsmStmt(
4850 Importer.getToContext(),
4851 Importer.Import(S->getAsmLoc()),
4852 S->isSimple(),
4853 S->isVolatile(),
4854 S->getNumOutputs(),
4855 S->getNumInputs(),
4856 Names.data(),
4857 Constraints.data(),
4858 Exprs.data(),
4859 AsmStr,
4860 S->getNumClobbers(),
4861 Clobbers.data(),
4862 Importer.Import(S->getRParenLoc()));
4863}
4864
Sean Callanan59721b32015-04-28 18:41:46 +00004865Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4866 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
4867 for (Decl *ToD : ToDG) {
4868 if (!ToD)
4869 return nullptr;
4870 }
4871 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4872 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4873 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4874}
4875
4876Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4877 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4878 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4879 S->hasLeadingEmptyMacro());
4880}
4881
4882Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004883 llvm::SmallVector<Stmt *, 8> ToStmts(S->size());
Sean Callanan8bca9962016-03-28 21:43:01 +00004884
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004885 if (ImportArrayChecked(S->body_begin(), S->body_end(), ToStmts.begin()))
Sean Callanan8bca9962016-03-28 21:43:01 +00004886 return nullptr;
4887
Sean Callanan59721b32015-04-28 18:41:46 +00004888 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4889 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
4890 return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(),
4891 ToStmts,
4892 ToLBraceLoc, ToRBraceLoc);
4893}
4894
4895Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4896 Expr *ToLHS = Importer.Import(S->getLHS());
4897 if (!ToLHS)
4898 return nullptr;
4899 Expr *ToRHS = Importer.Import(S->getRHS());
4900 if (!ToRHS && S->getRHS())
4901 return nullptr;
4902 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4903 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4904 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4905 return new (Importer.getToContext()) CaseStmt(ToLHS, ToRHS,
4906 ToCaseLoc, ToEllipsisLoc,
4907 ToColonLoc);
4908}
4909
4910Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4911 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4912 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4913 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4914 if (!ToSubStmt && S->getSubStmt())
4915 return nullptr;
4916 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4917 ToSubStmt);
4918}
4919
4920Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4921 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
4922 LabelDecl *ToLabelDecl =
4923 cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
4924 if (!ToLabelDecl && S->getDecl())
4925 return nullptr;
4926 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4927 if (!ToSubStmt && S->getSubStmt())
4928 return nullptr;
4929 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4930 ToSubStmt);
4931}
4932
4933Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4934 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4935 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4936 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
4937 ASTContext &_ToContext = Importer.getToContext();
4938 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
4939 [&_ToContext](const Attr *A) -> const Attr * {
4940 return A->clone(_ToContext);
4941 });
4942 for (const Attr *ToA : ToAttrs) {
4943 if (!ToA)
4944 return nullptr;
4945 }
4946 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4947 if (!ToSubStmt && S->getSubStmt())
4948 return nullptr;
4949 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4950 ToAttrs, ToSubStmt);
4951}
4952
4953Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4954 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
4955 VarDecl *ToConditionVariable = nullptr;
4956 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4957 ToConditionVariable =
4958 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4959 if (!ToConditionVariable)
4960 return nullptr;
4961 }
4962 Expr *ToCondition = Importer.Import(S->getCond());
4963 if (!ToCondition && S->getCond())
4964 return nullptr;
4965 Stmt *ToThenStmt = Importer.Import(S->getThen());
4966 if (!ToThenStmt && S->getThen())
4967 return nullptr;
4968 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4969 Stmt *ToElseStmt = Importer.Import(S->getElse());
4970 if (!ToElseStmt && S->getElse())
4971 return nullptr;
4972 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
4973 ToIfLoc, ToConditionVariable,
4974 ToCondition, ToThenStmt,
4975 ToElseLoc, ToElseStmt);
4976}
4977
4978Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
4979 VarDecl *ToConditionVariable = nullptr;
4980 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4981 ToConditionVariable =
4982 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4983 if (!ToConditionVariable)
4984 return nullptr;
4985 }
4986 Expr *ToCondition = Importer.Import(S->getCond());
4987 if (!ToCondition && S->getCond())
4988 return nullptr;
4989 SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt(
4990 Importer.getToContext(), ToConditionVariable,
4991 ToCondition);
4992 Stmt *ToBody = Importer.Import(S->getBody());
4993 if (!ToBody && S->getBody())
4994 return nullptr;
4995 ToStmt->setBody(ToBody);
4996 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4997 // Now we have to re-chain the cases.
4998 SwitchCase *LastChainedSwitchCase = nullptr;
4999 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
5000 SC = SC->getNextSwitchCase()) {
5001 SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
5002 if (!ToSC)
5003 return nullptr;
5004 if (LastChainedSwitchCase)
5005 LastChainedSwitchCase->setNextSwitchCase(ToSC);
5006 else
5007 ToStmt->setSwitchCaseList(ToSC);
5008 LastChainedSwitchCase = ToSC;
5009 }
5010 return ToStmt;
5011}
5012
5013Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
5014 VarDecl *ToConditionVariable = nullptr;
5015 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5016 ToConditionVariable =
5017 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5018 if (!ToConditionVariable)
5019 return nullptr;
5020 }
5021 Expr *ToCondition = Importer.Import(S->getCond());
5022 if (!ToCondition && S->getCond())
5023 return nullptr;
5024 Stmt *ToBody = Importer.Import(S->getBody());
5025 if (!ToBody && S->getBody())
5026 return nullptr;
5027 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
5028 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
5029 ToConditionVariable,
5030 ToCondition, ToBody,
5031 ToWhileLoc);
5032}
5033
5034Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5035 Stmt *ToBody = Importer.Import(S->getBody());
5036 if (!ToBody && S->getBody())
5037 return nullptr;
5038 Expr *ToCondition = Importer.Import(S->getCond());
5039 if (!ToCondition && S->getCond())
5040 return nullptr;
5041 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
5042 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
5043 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5044 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
5045 ToDoLoc, ToWhileLoc,
5046 ToRParenLoc);
5047}
5048
5049Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
5050 Stmt *ToInit = Importer.Import(S->getInit());
5051 if (!ToInit && S->getInit())
5052 return nullptr;
5053 Expr *ToCondition = Importer.Import(S->getCond());
5054 if (!ToCondition && S->getCond())
5055 return nullptr;
5056 VarDecl *ToConditionVariable = nullptr;
5057 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5058 ToConditionVariable =
5059 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5060 if (!ToConditionVariable)
5061 return nullptr;
5062 }
5063 Expr *ToInc = Importer.Import(S->getInc());
5064 if (!ToInc && S->getInc())
5065 return nullptr;
5066 Stmt *ToBody = Importer.Import(S->getBody());
5067 if (!ToBody && S->getBody())
5068 return nullptr;
5069 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5070 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
5071 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5072 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
5073 ToInit, ToCondition,
5074 ToConditionVariable,
5075 ToInc, ToBody,
5076 ToForLoc, ToLParenLoc,
5077 ToRParenLoc);
5078}
5079
5080Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5081 LabelDecl *ToLabel = nullptr;
5082 if (LabelDecl *FromLabel = S->getLabel()) {
5083 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
5084 if (!ToLabel)
5085 return nullptr;
5086 }
5087 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
5088 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
5089 return new (Importer.getToContext()) GotoStmt(ToLabel,
5090 ToGotoLoc, ToLabelLoc);
5091}
5092
5093Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5094 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
5095 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
5096 Expr *ToTarget = Importer.Import(S->getTarget());
5097 if (!ToTarget && S->getTarget())
5098 return nullptr;
5099 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
5100 ToTarget);
5101}
5102
5103Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5104 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
5105 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
5106}
5107
5108Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5109 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
5110 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
5111}
5112
5113Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5114 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
5115 Expr *ToRetExpr = Importer.Import(S->getRetValue());
5116 if (!ToRetExpr && S->getRetValue())
5117 return nullptr;
5118 VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate());
5119 VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
5120 if (!ToNRVOCandidate && NRVOCandidate)
5121 return nullptr;
5122 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
5123 ToNRVOCandidate);
5124}
5125
5126Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5127 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
5128 VarDecl *ToExceptionDecl = nullptr;
5129 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
5130 ToExceptionDecl =
5131 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5132 if (!ToExceptionDecl)
5133 return nullptr;
5134 }
5135 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
5136 if (!ToHandlerBlock && S->getHandlerBlock())
5137 return nullptr;
5138 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
5139 ToExceptionDecl,
5140 ToHandlerBlock);
5141}
5142
5143Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5144 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
5145 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
5146 if (!ToTryBlock && S->getTryBlock())
5147 return nullptr;
5148 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5149 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5150 CXXCatchStmt *FromHandler = S->getHandler(HI);
5151 if (Stmt *ToHandler = Importer.Import(FromHandler))
5152 ToHandlers[HI] = ToHandler;
5153 else
5154 return nullptr;
5155 }
5156 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
5157 ToHandlers);
5158}
5159
5160Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
5161 DeclStmt *ToRange =
5162 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
5163 if (!ToRange && S->getRangeStmt())
5164 return nullptr;
Richard Smith01694c32016-03-20 10:33:40 +00005165 DeclStmt *ToBegin =
5166 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
5167 if (!ToBegin && S->getBeginStmt())
5168 return nullptr;
5169 DeclStmt *ToEnd =
5170 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
5171 if (!ToEnd && S->getEndStmt())
Sean Callanan59721b32015-04-28 18:41:46 +00005172 return nullptr;
5173 Expr *ToCond = Importer.Import(S->getCond());
5174 if (!ToCond && S->getCond())
5175 return nullptr;
5176 Expr *ToInc = Importer.Import(S->getInc());
5177 if (!ToInc && S->getInc())
5178 return nullptr;
5179 DeclStmt *ToLoopVar =
5180 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
5181 if (!ToLoopVar && S->getLoopVarStmt())
5182 return nullptr;
5183 Stmt *ToBody = Importer.Import(S->getBody());
5184 if (!ToBody && S->getBody())
5185 return nullptr;
5186 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
Richard Smith9f690bd2015-10-27 06:02:45 +00005187 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00005188 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
5189 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
Richard Smith01694c32016-03-20 10:33:40 +00005190 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
Sean Callanan59721b32015-04-28 18:41:46 +00005191 ToCond, ToInc,
5192 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00005193 ToForLoc, ToCoawaitLoc,
5194 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005195}
5196
5197Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5198 Stmt *ToElem = Importer.Import(S->getElement());
5199 if (!ToElem && S->getElement())
5200 return nullptr;
5201 Expr *ToCollect = Importer.Import(S->getCollection());
5202 if (!ToCollect && S->getCollection())
5203 return nullptr;
5204 Stmt *ToBody = Importer.Import(S->getBody());
5205 if (!ToBody && S->getBody())
5206 return nullptr;
5207 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5208 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5209 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
5210 ToCollect,
5211 ToBody, ToForLoc,
5212 ToRParenLoc);
5213}
5214
5215Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5216 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
5217 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5218 VarDecl *ToExceptionDecl = nullptr;
5219 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
5220 ToExceptionDecl =
5221 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5222 if (!ToExceptionDecl)
5223 return nullptr;
5224 }
5225 Stmt *ToBody = Importer.Import(S->getCatchBody());
5226 if (!ToBody && S->getCatchBody())
5227 return nullptr;
5228 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
5229 ToRParenLoc,
5230 ToExceptionDecl,
5231 ToBody);
5232}
5233
5234Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5235 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
5236 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
5237 if (!ToAtFinallyStmt && S->getFinallyBody())
5238 return nullptr;
5239 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
5240 ToAtFinallyStmt);
5241}
5242
5243Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5244 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
5245 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
5246 if (!ToAtTryStmt && S->getTryBody())
5247 return nullptr;
5248 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5249 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5250 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
5251 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
5252 ToCatchStmts[CI] = ToCatchStmt;
5253 else
5254 return nullptr;
5255 }
5256 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
5257 if (!ToAtFinallyStmt && S->getFinallyStmt())
5258 return nullptr;
5259 return ObjCAtTryStmt::Create(Importer.getToContext(),
5260 ToAtTryLoc, ToAtTryStmt,
5261 ToCatchStmts.begin(), ToCatchStmts.size(),
5262 ToAtFinallyStmt);
5263}
5264
5265Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
5266 (ObjCAtSynchronizedStmt *S) {
5267 SourceLocation ToAtSynchronizedLoc =
5268 Importer.Import(S->getAtSynchronizedLoc());
5269 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
5270 if (!ToSynchExpr && S->getSynchExpr())
5271 return nullptr;
5272 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
5273 if (!ToSynchBody && S->getSynchBody())
5274 return nullptr;
5275 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5276 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5277}
5278
5279Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5280 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5281 Expr *ToThrow = Importer.Import(S->getThrowExpr());
5282 if (!ToThrow && S->getThrowExpr())
5283 return nullptr;
5284 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5285}
5286
5287Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5288 (ObjCAutoreleasePoolStmt *S) {
5289 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5290 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5291 if (!ToSubStmt && S->getSubStmt())
5292 return nullptr;
5293 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5294 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005295}
5296
5297//----------------------------------------------------------------------------
5298// Import Expressions
5299//----------------------------------------------------------------------------
5300Expr *ASTNodeImporter::VisitExpr(Expr *E) {
5301 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5302 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00005303 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005304}
5305
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005306Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
5307 QualType T = Importer.Import(E->getType());
5308 if (T.isNull())
5309 return nullptr;
5310
5311 Expr *SubExpr = Importer.Import(E->getSubExpr());
5312 if (!SubExpr && E->getSubExpr())
5313 return nullptr;
5314
5315 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
5316 if (!TInfo)
5317 return nullptr;
5318
5319 return new (Importer.getToContext()) VAArgExpr(
5320 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
5321 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
5322}
5323
5324
5325Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
5326 QualType T = Importer.Import(E->getType());
5327 if (T.isNull())
5328 return nullptr;
5329
5330 return new (Importer.getToContext()) GNUNullExpr(
5331 T, Importer.Import(E->getExprLoc()));
5332}
5333
5334Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
5335 QualType T = Importer.Import(E->getType());
5336 if (T.isNull())
5337 return nullptr;
5338
5339 StringLiteral *SL = cast_or_null<StringLiteral>(
5340 Importer.Import(E->getFunctionName()));
5341 if (!SL && E->getFunctionName())
5342 return nullptr;
5343
5344 return new (Importer.getToContext()) PredefinedExpr(
5345 Importer.Import(E->getExprLoc()), T, E->getIdentType(), SL);
5346}
5347
Douglas Gregor52f820e2010-02-19 01:17:02 +00005348Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00005349 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
5350 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005351 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005352
Craig Topper36250ad2014-05-12 05:36:57 +00005353 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005354 if (E->getDecl() != E->getFoundDecl()) {
5355 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5356 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00005357 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005358 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00005359
5360 QualType T = Importer.Import(E->getType());
5361 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005362 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005363
5364 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5365 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00005366 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005367 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00005368 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005369 Importer.Import(E->getLocation()),
5370 T, E->getValueKind(),
5371 FoundD,
Craig Topper36250ad2014-05-12 05:36:57 +00005372 /*FIXME:TemplateArgs=*/nullptr);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005373 if (E->hadMultipleCandidates())
5374 DRE->setHadMultipleCandidates(true);
5375 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00005376}
5377
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005378Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
5379 QualType T = Importer.Import(E->getType());
5380 if (T.isNull())
5381 return NULL;
5382
5383 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
5384}
5385
5386ASTNodeImporter::Designator
5387ASTNodeImporter::ImportDesignator(const Designator &D) {
5388 if (D.isFieldDesignator()) {
5389 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
5390 // Caller checks for import error
5391 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
5392 Importer.Import(D.getFieldLoc()));
5393 }
5394 if (D.isArrayDesignator())
5395 return Designator(D.getFirstExprIndex(),
5396 Importer.Import(D.getLBracketLoc()),
5397 Importer.Import(D.getRBracketLoc()));
5398
5399 assert(D.isArrayRangeDesignator());
5400 return Designator(D.getFirstExprIndex(),
5401 Importer.Import(D.getLBracketLoc()),
5402 Importer.Import(D.getEllipsisLoc()),
5403 Importer.Import(D.getRBracketLoc()));
5404}
5405
5406
5407Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
5408 Expr *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
5409 if (!Init)
5410 return nullptr;
5411
5412 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
5413 // List elements from the second, the first is Init itself
5414 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
5415 if (Expr *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
5416 IndexExprs[I - 1] = Arg;
5417 else
5418 return nullptr;
5419 }
5420
5421 SmallVector<Designator, 4> Designators(DIE->size());
5422 std::transform(DIE->designators_begin(), DIE->designators_end(),
5423 Designators.begin(),
5424 [this](const Designator &D) -> Designator {
5425 return ImportDesignator(D);
5426 });
5427
5428 for (auto I = DIE->designators_begin(), E = DIE->designators_end(); I != E;
5429 ++I)
5430 if (I->isFieldDesignator() && !I->getFieldName())
5431 return nullptr;
5432
5433 return DesignatedInitExpr::Create(
5434 Importer.getToContext(), Designators.data(), Designators.size(),
5435 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
5436 DIE->usesGNUSyntax(), Init);
5437}
5438
5439Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
5440 QualType T = Importer.Import(E->getType());
5441 if (T.isNull())
5442 return nullptr;
5443
5444 return new (Importer.getToContext())
5445 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
5446}
5447
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005448Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5449 QualType T = Importer.Import(E->getType());
5450 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005451 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005452
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005453 return IntegerLiteral::Create(Importer.getToContext(),
5454 E->getValue(), T,
5455 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005456}
5457
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005458Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
5459 QualType T = Importer.Import(E->getType());
5460 if (T.isNull())
5461 return nullptr;
5462
5463 return FloatingLiteral::Create(Importer.getToContext(),
5464 E->getValue(), E->isExact(), T,
5465 Importer.Import(E->getLocation()));
5466}
5467
Douglas Gregor623421d2010-02-18 02:21:22 +00005468Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5469 QualType T = Importer.Import(E->getType());
5470 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005471 return nullptr;
5472
Douglas Gregorfb65e592011-07-27 05:40:30 +00005473 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5474 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005475 Importer.Import(E->getLocation()));
5476}
5477
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005478Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
5479 QualType T = Importer.Import(E->getType());
5480 if (T.isNull())
5481 return nullptr;
5482
5483 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
5484 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
5485
5486 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
5487 E->getKind(), E->isPascal(), T,
5488 Locations.data(), Locations.size());
5489}
5490
5491Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
5492 QualType T = Importer.Import(E->getType());
5493 if (T.isNull())
5494 return nullptr;
5495
5496 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
5497 if (!TInfo)
5498 return nullptr;
5499
5500 Expr *Init = Importer.Import(E->getInitializer());
5501 if (!Init)
5502 return nullptr;
5503
5504 return new (Importer.getToContext()) CompoundLiteralExpr(
5505 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
5506 Init, E->isFileScope());
5507}
5508
5509Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
5510 QualType T = Importer.Import(E->getType());
5511 if (T.isNull())
5512 return nullptr;
5513
5514 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
5515 if (ImportArrayChecked(
5516 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
5517 Exprs.begin()))
5518 return nullptr;
5519
5520 return new (Importer.getToContext()) AtomicExpr(
5521 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
5522 Importer.Import(E->getRParenLoc()));
5523}
5524
5525Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
5526 QualType T = Importer.Import(E->getType());
5527 if (T.isNull())
5528 return nullptr;
5529
5530 LabelDecl *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
5531 if (!ToLabel)
5532 return nullptr;
5533
5534 return new (Importer.getToContext()) AddrLabelExpr(
5535 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
5536 ToLabel, T);
5537}
5538
Douglas Gregorc74247e2010-02-19 01:07:06 +00005539Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5540 Expr *SubExpr = Importer.Import(E->getSubExpr());
5541 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005542 return nullptr;
5543
Douglas Gregorc74247e2010-02-19 01:07:06 +00005544 return new (Importer.getToContext())
5545 ParenExpr(Importer.Import(E->getLParen()),
5546 Importer.Import(E->getRParen()),
5547 SubExpr);
5548}
5549
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005550Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
5551 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
5552 if (ImportArrayChecked(
5553 E->getExprs(), E->getExprs() + E->getNumExprs(), Exprs.begin()))
5554 return nullptr;
5555
5556 return new (Importer.getToContext()) ParenListExpr(
5557 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
5558 Exprs, Importer.Import(E->getLParenLoc()));
5559}
5560
5561Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
5562 QualType T = Importer.Import(E->getType());
5563 if (T.isNull())
5564 return nullptr;
5565
5566 CompoundStmt *ToSubStmt = cast_or_null<CompoundStmt>(
5567 Importer.Import(E->getSubStmt()));
5568 if (!ToSubStmt && E->getSubStmt())
5569 return nullptr;
5570
5571 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
5572 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
5573}
5574
Douglas Gregorc74247e2010-02-19 01:07:06 +00005575Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5576 QualType T = Importer.Import(E->getType());
5577 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005578 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005579
5580 Expr *SubExpr = Importer.Import(E->getSubExpr());
5581 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005582 return nullptr;
5583
Douglas Gregorc74247e2010-02-19 01:07:06 +00005584 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005585 T, E->getValueKind(),
5586 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00005587 Importer.Import(E->getOperatorLoc()));
5588}
5589
Peter Collingbournee190dee2011-03-11 19:24:49 +00005590Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
5591 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005592 QualType ResultType = Importer.Import(E->getType());
5593
5594 if (E->isArgumentType()) {
5595 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5596 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005597 return nullptr;
5598
Peter Collingbournee190dee2011-03-11 19:24:49 +00005599 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5600 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005601 Importer.Import(E->getOperatorLoc()),
5602 Importer.Import(E->getRParenLoc()));
5603 }
5604
5605 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5606 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005607 return nullptr;
5608
Peter Collingbournee190dee2011-03-11 19:24:49 +00005609 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5610 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005611 Importer.Import(E->getOperatorLoc()),
5612 Importer.Import(E->getRParenLoc()));
5613}
5614
Douglas Gregorc74247e2010-02-19 01:07:06 +00005615Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5616 QualType T = Importer.Import(E->getType());
5617 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005618 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005619
5620 Expr *LHS = Importer.Import(E->getLHS());
5621 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005622 return nullptr;
5623
Douglas Gregorc74247e2010-02-19 01:07:06 +00005624 Expr *RHS = Importer.Import(E->getRHS());
5625 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005626 return nullptr;
5627
Douglas Gregorc74247e2010-02-19 01:07:06 +00005628 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005629 T, E->getValueKind(),
5630 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005631 Importer.Import(E->getOperatorLoc()),
5632 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005633}
5634
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005635Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
5636 QualType T = Importer.Import(E->getType());
5637 if (T.isNull())
5638 return nullptr;
5639
5640 Expr *ToLHS = Importer.Import(E->getLHS());
5641 if (!ToLHS)
5642 return nullptr;
5643
5644 Expr *ToRHS = Importer.Import(E->getRHS());
5645 if (!ToRHS)
5646 return nullptr;
5647
5648 Expr *ToCond = Importer.Import(E->getCond());
5649 if (!ToCond)
5650 return nullptr;
5651
5652 return new (Importer.getToContext()) ConditionalOperator(
5653 ToCond, Importer.Import(E->getQuestionLoc()),
5654 ToLHS, Importer.Import(E->getColonLoc()),
5655 ToRHS, T, E->getValueKind(), E->getObjectKind());
5656}
5657
5658Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
5659 BinaryConditionalOperator *E) {
5660 QualType T = Importer.Import(E->getType());
5661 if (T.isNull())
5662 return nullptr;
5663
5664 Expr *Common = Importer.Import(E->getCommon());
5665 if (!Common)
5666 return nullptr;
5667
5668 Expr *Cond = Importer.Import(E->getCond());
5669 if (!Cond)
5670 return nullptr;
5671
5672 OpaqueValueExpr *OpaqueValue = cast_or_null<OpaqueValueExpr>(
5673 Importer.Import(E->getOpaqueValue()));
5674 if (!OpaqueValue)
5675 return nullptr;
5676
5677 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
5678 if (!TrueExpr)
5679 return nullptr;
5680
5681 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
5682 if (!FalseExpr)
5683 return nullptr;
5684
5685 return new (Importer.getToContext()) BinaryConditionalOperator(
5686 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
5687 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
5688 T, E->getValueKind(), E->getObjectKind());
5689}
5690
5691Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
5692 QualType T = Importer.Import(E->getType());
5693 if (T.isNull())
5694 return nullptr;
5695
5696 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
5697 if (!SourceExpr && E->getSourceExpr())
5698 return nullptr;
5699
5700 return new (Importer.getToContext()) OpaqueValueExpr(
5701 Importer.Import(E->getExprLoc()), T, E->getValueKind(),
5702 E->getObjectKind(), SourceExpr);
5703}
5704
Douglas Gregorc74247e2010-02-19 01:07:06 +00005705Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5706 QualType T = Importer.Import(E->getType());
5707 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005708 return nullptr;
5709
Douglas Gregorc74247e2010-02-19 01:07:06 +00005710 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5711 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005712 return nullptr;
5713
Douglas Gregorc74247e2010-02-19 01:07:06 +00005714 QualType CompResultType = Importer.Import(E->getComputationResultType());
5715 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005716 return nullptr;
5717
Douglas Gregorc74247e2010-02-19 01:07:06 +00005718 Expr *LHS = Importer.Import(E->getLHS());
5719 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005720 return nullptr;
5721
Douglas Gregorc74247e2010-02-19 01:07:06 +00005722 Expr *RHS = Importer.Import(E->getRHS());
5723 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005724 return nullptr;
5725
Douglas Gregorc74247e2010-02-19 01:07:06 +00005726 return new (Importer.getToContext())
5727 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005728 T, E->getValueKind(),
5729 E->getObjectKind(),
5730 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005731 Importer.Import(E->getOperatorLoc()),
5732 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005733}
5734
Benjamin Kramer8aef5962011-03-26 12:38:21 +00005735static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallcf142162010-08-07 06:22:56 +00005736 if (E->path_empty()) return false;
5737
5738 // TODO: import cast paths
5739 return true;
5740}
5741
Douglas Gregor98c10182010-02-12 22:17:39 +00005742Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5743 QualType T = Importer.Import(E->getType());
5744 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005745 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005746
5747 Expr *SubExpr = Importer.Import(E->getSubExpr());
5748 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005749 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005750
5751 CXXCastPath BasePath;
5752 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005753 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005754
5755 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005756 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005757}
5758
Douglas Gregor5481d322010-02-19 01:32:14 +00005759Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
5760 QualType T = Importer.Import(E->getType());
5761 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005762 return nullptr;
5763
Douglas Gregor5481d322010-02-19 01:32:14 +00005764 Expr *SubExpr = Importer.Import(E->getSubExpr());
5765 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005766 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00005767
5768 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5769 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00005770 return nullptr;
5771
John McCallcf142162010-08-07 06:22:56 +00005772 CXXCastPath BasePath;
5773 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005774 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005775
John McCall7decc9e2010-11-18 06:31:45 +00005776 return CStyleCastExpr::Create(Importer.getToContext(), T,
5777 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00005778 SubExpr, &BasePath, TInfo,
5779 Importer.Import(E->getLParenLoc()),
5780 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00005781}
5782
Sean Callanan59721b32015-04-28 18:41:46 +00005783Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
5784 QualType T = Importer.Import(E->getType());
5785 if (T.isNull())
5786 return nullptr;
5787
Richard Smithc2bebe92016-05-11 20:37:46 +00005788 NamedDecl *ToFound =
5789 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl()));
5790 if (!ToFound)
5791 return nullptr;
5792
Sean Callanan59721b32015-04-28 18:41:46 +00005793 CXXConstructorDecl *ToCCD =
5794 dyn_cast<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
Richard Smithc2bebe92016-05-11 20:37:46 +00005795 if (!ToCCD)
Sean Callanan59721b32015-04-28 18:41:46 +00005796 return nullptr;
5797
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005798 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
5799 if (ImportArrayChecked(E->getArgs(), E->getArgs() + E->getNumArgs(),
5800 ToArgs.begin()))
Sean Callanan8bca9962016-03-28 21:43:01 +00005801 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005802
5803 return CXXConstructExpr::Create(Importer.getToContext(), T,
5804 Importer.Import(E->getLocation()),
Richard Smithc2bebe92016-05-11 20:37:46 +00005805 ToFound, ToCCD, E->isElidable(),
Sean Callanan59721b32015-04-28 18:41:46 +00005806 ToArgs, E->hadMultipleCandidates(),
5807 E->isListInitialization(),
5808 E->isStdInitListInitialization(),
5809 E->requiresZeroInitialization(),
5810 E->getConstructionKind(),
5811 Importer.Import(E->getParenOrBraceRange()));
5812}
5813
Sean Callanan8bca9962016-03-28 21:43:01 +00005814Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
5815 QualType T = Importer.Import(E->getType());
5816 if (T.isNull())
5817 return nullptr;
5818
5819 Expr *ToFn = Importer.Import(E->getCallee());
5820 if (!ToFn)
5821 return nullptr;
5822
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005823 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Sean Callanan8bca9962016-03-28 21:43:01 +00005824
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005825 if (ImportArrayChecked(E->arg_begin(), E->arg_end(), ToArgs.begin()))
Sean Callanan8bca9962016-03-28 21:43:01 +00005826 return nullptr;
5827
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005828 return new (Importer.getToContext()) CXXMemberCallExpr(
5829 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
5830 Importer.Import(E->getRParenLoc()));
Sean Callanan8bca9962016-03-28 21:43:01 +00005831}
5832
5833Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
5834 QualType T = Importer.Import(E->getType());
5835 if (T.isNull())
5836 return nullptr;
5837
5838 return new (Importer.getToContext())
5839 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
5840}
5841
5842Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
5843 QualType T = Importer.Import(E->getType());
5844 if (T.isNull())
5845 return nullptr;
5846
5847 return new (Importer.getToContext())
5848 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
5849}
5850
5851
Sean Callanan59721b32015-04-28 18:41:46 +00005852Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
5853 QualType T = Importer.Import(E->getType());
5854 if (T.isNull())
5855 return nullptr;
5856
5857 Expr *ToBase = Importer.Import(E->getBase());
5858 if (!ToBase && E->getBase())
5859 return nullptr;
5860
5861 ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
5862 if (!ToMember && E->getMemberDecl())
5863 return nullptr;
5864
5865 DeclAccessPair ToFoundDecl = DeclAccessPair::make(
5866 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
5867 E->getFoundDecl().getAccess());
5868
5869 DeclarationNameInfo ToMemberNameInfo(
5870 Importer.Import(E->getMemberNameInfo().getName()),
5871 Importer.Import(E->getMemberNameInfo().getLoc()));
5872
5873 if (E->hasExplicitTemplateArgs()) {
5874 return nullptr; // FIXME: handle template arguments
5875 }
5876
5877 return MemberExpr::Create(Importer.getToContext(), ToBase,
5878 E->isArrow(),
5879 Importer.Import(E->getOperatorLoc()),
5880 Importer.Import(E->getQualifierLoc()),
5881 Importer.Import(E->getTemplateKeywordLoc()),
5882 ToMember, ToFoundDecl, ToMemberNameInfo,
5883 nullptr, T, E->getValueKind(),
5884 E->getObjectKind());
5885}
5886
5887Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
5888 QualType T = Importer.Import(E->getType());
5889 if (T.isNull())
5890 return nullptr;
5891
5892 Expr *ToCallee = Importer.Import(E->getCallee());
5893 if (!ToCallee && E->getCallee())
5894 return nullptr;
5895
5896 unsigned NumArgs = E->getNumArgs();
5897
5898 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
5899
5900 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) {
5901 Expr *FromArg = E->getArg(ai);
5902 Expr *ToArg = Importer.Import(FromArg);
5903 if (!ToArg)
5904 return nullptr;
5905 ToArgs[ai] = ToArg;
5906 }
5907
5908 Expr **ToArgs_Copied = new (Importer.getToContext())
5909 Expr*[NumArgs];
5910
5911 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
5912 ToArgs_Copied[ai] = ToArgs[ai];
5913
5914 return new (Importer.getToContext())
5915 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00005916 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00005917 Importer.Import(E->getRParenLoc()));
5918}
5919
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005920Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
5921 QualType T = Importer.Import(ILE->getType());
Sean Callanan8bca9962016-03-28 21:43:01 +00005922 if (T.isNull())
5923 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00005924
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005925 llvm::SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
5926 if (ImportArrayChecked(
5927 ILE->getInits(), ILE->getInits() + ILE->getNumInits(), Exprs.begin()))
Sean Callanan8bca9962016-03-28 21:43:01 +00005928 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00005929
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005930 ASTContext &ToCtx = Importer.getToContext();
5931 InitListExpr *To = new (ToCtx) InitListExpr(
5932 ToCtx, Importer.Import(ILE->getLBraceLoc()),
5933 Exprs, Importer.Import(ILE->getLBraceLoc()));
5934 To->setType(T);
5935
5936 if (ILE->hasArrayFiller()) {
5937 Expr *Filler = Importer.Import(ILE->getArrayFiller());
5938 if (!Filler)
5939 return nullptr;
5940 To->setArrayFiller(Filler);
5941 }
5942
5943 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
5944 FieldDecl *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
5945 if (!ToFD)
5946 return nullptr;
5947 To->setInitializedFieldInUnion(ToFD);
5948 }
5949
5950 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
5951 InitListExpr *ToSyntForm = cast_or_null<InitListExpr>(
5952 Importer.Import(SyntForm));
5953 if (!ToSyntForm)
5954 return nullptr;
5955 To->setSyntacticForm(ToSyntForm);
5956 }
5957
5958 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
5959 To->setValueDependent(ILE->isValueDependent());
5960 To->setInstantiationDependent(ILE->isInstantiationDependent());
5961
5962 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00005963}
5964
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005965ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00005966 ASTContext &FromContext, FileManager &FromFileManager,
5967 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00005968 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00005969 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Richard Smith5bb4cdf2012-12-20 02:22:15 +00005970 Minimal(MinimalImport), LastDiagFromFrom(false)
Douglas Gregor0a791672011-01-18 03:11:38 +00005971{
Douglas Gregor62d311f2010-02-09 19:21:46 +00005972 ImportedDecls[FromContext.getTranslationUnitDecl()]
5973 = ToContext.getTranslationUnitDecl();
5974}
5975
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +00005976ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00005977
5978QualType ASTImporter::Import(QualType FromT) {
5979 if (FromT.isNull())
5980 return QualType();
John McCall424cec92011-01-19 06:33:43 +00005981
5982 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00005983
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005984 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00005985 llvm::DenseMap<const Type *, const Type *>::iterator Pos
5986 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005987 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00005988 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00005989
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005990 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00005991 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00005992 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00005993 if (ToT.isNull())
5994 return ToT;
5995
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005996 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00005997 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005998
John McCall424cec92011-01-19 06:33:43 +00005999 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006000}
6001
Douglas Gregor62d311f2010-02-09 19:21:46 +00006002TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006003 if (!FromTSI)
6004 return FromTSI;
6005
6006 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00006007 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006008 QualType T = Import(FromTSI->getType());
6009 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006010 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006011
6012 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00006013 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00006014}
6015
Sean Callanan59721b32015-04-28 18:41:46 +00006016Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
6017 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
6018 if (Pos != ImportedDecls.end()) {
6019 Decl *ToD = Pos->second;
6020 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
6021 return ToD;
6022 } else {
6023 return nullptr;
6024 }
6025}
6026
Douglas Gregor62d311f2010-02-09 19:21:46 +00006027Decl *ASTImporter::Import(Decl *FromD) {
6028 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00006029 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006030
Douglas Gregord451ea92011-07-29 23:31:30 +00006031 ASTNodeImporter Importer(*this);
6032
Douglas Gregor62d311f2010-02-09 19:21:46 +00006033 // Check whether we've already imported this declaration.
6034 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00006035 if (Pos != ImportedDecls.end()) {
6036 Decl *ToD = Pos->second;
6037 Importer.ImportDefinitionIfNeeded(FromD, ToD);
6038 return ToD;
6039 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00006040
6041 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00006042 Decl *ToD = Importer.Visit(FromD);
6043 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00006044 return nullptr;
6045
Douglas Gregor62d311f2010-02-09 19:21:46 +00006046 // Record the imported declaration.
6047 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00006048
6049 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
6050 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00006051 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00006052 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00006053 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00006054 // When we've finished transforming a typedef, see whether it was the
6055 // typedef for an anonymous tag.
Craig Topper2341c0d2013-07-04 03:08:24 +00006056 for (SmallVectorImpl<TagDecl *>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00006057 FromTag = AnonTagsWithPendingTypedefs.begin(),
6058 FromTagEnd = AnonTagsWithPendingTypedefs.end();
6059 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00006060 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00006061 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
6062 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00006063 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00006064 AnonTagsWithPendingTypedefs.erase(FromTag);
6065 break;
6066 }
6067 }
6068 }
6069 }
6070
Douglas Gregor62d311f2010-02-09 19:21:46 +00006071 return ToD;
6072}
6073
6074DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
6075 if (!FromDC)
6076 return FromDC;
6077
Douglas Gregor95d82832012-01-24 18:36:04 +00006078 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00006079 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00006080 return nullptr;
6081
Douglas Gregor2e15c842012-02-01 21:00:38 +00006082 // When we're using a record/enum/Objective-C class/protocol as a context, we
6083 // need it to have a definition.
6084 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00006085 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006086 if (ToRecord->isCompleteDefinition()) {
6087 // Do nothing.
6088 } else if (FromRecord->isCompleteDefinition()) {
6089 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
6090 ASTNodeImporter::IDK_Basic);
6091 } else {
6092 CompleteDecl(ToRecord);
6093 }
6094 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
6095 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
6096 if (ToEnum->isCompleteDefinition()) {
6097 // Do nothing.
6098 } else if (FromEnum->isCompleteDefinition()) {
6099 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
6100 ASTNodeImporter::IDK_Basic);
6101 } else {
6102 CompleteDecl(ToEnum);
6103 }
6104 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
6105 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
6106 if (ToClass->getDefinition()) {
6107 // Do nothing.
6108 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
6109 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
6110 ASTNodeImporter::IDK_Basic);
6111 } else {
6112 CompleteDecl(ToClass);
6113 }
6114 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
6115 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
6116 if (ToProto->getDefinition()) {
6117 // Do nothing.
6118 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
6119 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
6120 ASTNodeImporter::IDK_Basic);
6121 } else {
6122 CompleteDecl(ToProto);
6123 }
Douglas Gregor95d82832012-01-24 18:36:04 +00006124 }
6125
6126 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006127}
6128
6129Expr *ASTImporter::Import(Expr *FromE) {
6130 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00006131 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006132
6133 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
6134}
6135
6136Stmt *ASTImporter::Import(Stmt *FromS) {
6137 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00006138 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006139
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006140 // Check whether we've already imported this declaration.
6141 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
6142 if (Pos != ImportedStmts.end())
6143 return Pos->second;
6144
6145 // Import the type
6146 ASTNodeImporter Importer(*this);
6147 Stmt *ToS = Importer.Visit(FromS);
6148 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00006149 return nullptr;
6150
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006151 // Record the imported declaration.
6152 ImportedStmts[FromS] = ToS;
6153 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006154}
6155
6156NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
6157 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00006158 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006159
Douglas Gregor90ebf252011-04-27 16:48:40 +00006160 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
6161
6162 switch (FromNNS->getKind()) {
6163 case NestedNameSpecifier::Identifier:
6164 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
6165 return NestedNameSpecifier::Create(ToContext, prefix, II);
6166 }
Craig Topper36250ad2014-05-12 05:36:57 +00006167 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006168
6169 case NestedNameSpecifier::Namespace:
6170 if (NamespaceDecl *NS =
6171 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
6172 return NestedNameSpecifier::Create(ToContext, prefix, NS);
6173 }
Craig Topper36250ad2014-05-12 05:36:57 +00006174 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006175
6176 case NestedNameSpecifier::NamespaceAlias:
6177 if (NamespaceAliasDecl *NSAD =
6178 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
6179 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
6180 }
Craig Topper36250ad2014-05-12 05:36:57 +00006181 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006182
6183 case NestedNameSpecifier::Global:
6184 return NestedNameSpecifier::GlobalSpecifier(ToContext);
6185
Nikola Smiljanic67860242014-09-26 00:28:20 +00006186 case NestedNameSpecifier::Super:
6187 if (CXXRecordDecl *RD =
6188 cast<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
6189 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
6190 }
6191 return nullptr;
6192
Douglas Gregor90ebf252011-04-27 16:48:40 +00006193 case NestedNameSpecifier::TypeSpec:
6194 case NestedNameSpecifier::TypeSpecWithTemplate: {
6195 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
6196 if (!T.isNull()) {
6197 bool bTemplate = FromNNS->getKind() ==
6198 NestedNameSpecifier::TypeSpecWithTemplate;
6199 return NestedNameSpecifier::Create(ToContext, prefix,
6200 bTemplate, T.getTypePtr());
6201 }
6202 }
Craig Topper36250ad2014-05-12 05:36:57 +00006203 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006204 }
6205
6206 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00006207}
6208
Douglas Gregor14454802011-02-25 02:25:35 +00006209NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
6210 // FIXME: Implement!
6211 return NestedNameSpecifierLoc();
6212}
6213
Douglas Gregore2e50d332010-12-01 01:36:18 +00006214TemplateName ASTImporter::Import(TemplateName From) {
6215 switch (From.getKind()) {
6216 case TemplateName::Template:
6217 if (TemplateDecl *ToTemplate
6218 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
6219 return TemplateName(ToTemplate);
6220
6221 return TemplateName();
6222
6223 case TemplateName::OverloadedTemplate: {
6224 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
6225 UnresolvedSet<2> ToTemplates;
6226 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
6227 E = FromStorage->end();
6228 I != E; ++I) {
6229 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
6230 ToTemplates.addDecl(To);
6231 else
6232 return TemplateName();
6233 }
6234 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
6235 ToTemplates.end());
6236 }
6237
6238 case TemplateName::QualifiedTemplate: {
6239 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
6240 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
6241 if (!Qualifier)
6242 return TemplateName();
6243
6244 if (TemplateDecl *ToTemplate
6245 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
6246 return ToContext.getQualifiedTemplateName(Qualifier,
6247 QTN->hasTemplateKeyword(),
6248 ToTemplate);
6249
6250 return TemplateName();
6251 }
6252
6253 case TemplateName::DependentTemplate: {
6254 DependentTemplateName *DTN = From.getAsDependentTemplateName();
6255 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
6256 if (!Qualifier)
6257 return TemplateName();
6258
6259 if (DTN->isIdentifier()) {
6260 return ToContext.getDependentTemplateName(Qualifier,
6261 Import(DTN->getIdentifier()));
6262 }
6263
6264 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
6265 }
John McCalld9dfe3a2011-06-30 08:33:18 +00006266
6267 case TemplateName::SubstTemplateTemplateParm: {
6268 SubstTemplateTemplateParmStorage *subst
6269 = From.getAsSubstTemplateTemplateParm();
6270 TemplateTemplateParmDecl *param
6271 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
6272 if (!param)
6273 return TemplateName();
6274
6275 TemplateName replacement = Import(subst->getReplacement());
6276 if (replacement.isNull()) return TemplateName();
6277
6278 return ToContext.getSubstTemplateTemplateParm(param, replacement);
6279 }
Douglas Gregor5590be02011-01-15 06:45:20 +00006280
6281 case TemplateName::SubstTemplateTemplateParmPack: {
6282 SubstTemplateTemplateParmPackStorage *SubstPack
6283 = From.getAsSubstTemplateTemplateParmPack();
6284 TemplateTemplateParmDecl *Param
6285 = cast_or_null<TemplateTemplateParmDecl>(
6286 Import(SubstPack->getParameterPack()));
6287 if (!Param)
6288 return TemplateName();
6289
6290 ASTNodeImporter Importer(*this);
6291 TemplateArgument ArgPack
6292 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
6293 if (ArgPack.isNull())
6294 return TemplateName();
6295
6296 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
6297 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00006298 }
6299
6300 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00006301}
6302
Douglas Gregor62d311f2010-02-09 19:21:46 +00006303SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
6304 if (FromLoc.isInvalid())
6305 return SourceLocation();
6306
Douglas Gregor811663e2010-02-10 00:15:17 +00006307 SourceManager &FromSM = FromContext.getSourceManager();
6308
6309 // For now, map everything down to its spelling location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00006310 // don't have to import macro expansions.
6311 // FIXME: Import macro expansions!
Douglas Gregor811663e2010-02-10 00:15:17 +00006312 FromLoc = FromSM.getSpellingLoc(FromLoc);
6313 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
6314 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00006315 FileID ToFileID = Import(Decomposed.first);
6316 if (ToFileID.isInvalid())
6317 return SourceLocation();
Sean Callanan59721b32015-04-28 18:41:46 +00006318 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
6319 .getLocWithOffset(Decomposed.second);
6320 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006321}
6322
6323SourceRange ASTImporter::Import(SourceRange FromRange) {
6324 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
6325}
6326
Douglas Gregor811663e2010-02-10 00:15:17 +00006327FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00006328 llvm::DenseMap<FileID, FileID>::iterator Pos
6329 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00006330 if (Pos != ImportedFileIDs.end())
6331 return Pos->second;
6332
6333 SourceManager &FromSM = FromContext.getSourceManager();
6334 SourceManager &ToSM = ToContext.getSourceManager();
6335 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00006336 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00006337
6338 // Include location of this file.
6339 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
6340
6341 // Map the FileID for to the "to" source manager.
6342 FileID ToID;
6343 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan25d34af2015-04-30 00:44:21 +00006344 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
Douglas Gregor811663e2010-02-10 00:15:17 +00006345 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
6346 // disk again
6347 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
6348 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00006349 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Sean Callanan238d8972014-12-10 01:26:39 +00006350 if (!Entry)
6351 return FileID();
Douglas Gregor811663e2010-02-10 00:15:17 +00006352 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
6353 FromSLoc.getFile().getFileCharacteristic());
6354 } else {
6355 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006356 const llvm::MemoryBuffer *
6357 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00006358 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00006359 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00006360 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00006361 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00006362 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00006363 }
6364
6365
Sebastian Redl99219f12010-09-30 01:03:06 +00006366 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00006367 return ToID;
6368}
6369
Sean Callanan55d486d2016-05-14 05:20:31 +00006370CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
6371 Expr *ToExpr = Import(From->getInit());
6372 if (!ToExpr && From->getInit())
6373 return nullptr;
6374
6375 if (From->isBaseInitializer()) {
6376 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
6377 if (!ToTInfo && From->getTypeSourceInfo())
6378 return nullptr;
6379
6380 return new (ToContext) CXXCtorInitializer(
6381 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
6382 ToExpr, Import(From->getRParenLoc()),
6383 From->isPackExpansion() ? Import(From->getEllipsisLoc())
6384 : SourceLocation());
6385 } else if (From->isMemberInitializer()) {
6386 FieldDecl *ToField =
6387 llvm::cast_or_null<FieldDecl>(Import(From->getMember()));
6388 if (!ToField && From->getMember())
6389 return nullptr;
6390
6391 return new (ToContext) CXXCtorInitializer(
6392 ToContext, ToField, Import(From->getMemberLocation()),
6393 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
6394 } else if (From->isIndirectMemberInitializer()) {
6395 IndirectFieldDecl *ToIField = llvm::cast_or_null<IndirectFieldDecl>(
6396 Import(From->getIndirectMember()));
6397 if (!ToIField && From->getIndirectMember())
6398 return nullptr;
6399
6400 return new (ToContext) CXXCtorInitializer(
6401 ToContext, ToIField, Import(From->getMemberLocation()),
6402 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
6403 } else if (From->isDelegatingInitializer()) {
6404 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
6405 if (!ToTInfo && From->getTypeSourceInfo())
6406 return nullptr;
6407
6408 return new (ToContext)
6409 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
6410 ToExpr, Import(From->getRParenLoc()));
6411 } else if (unsigned NumArrayIndices = From->getNumArrayIndices()) {
6412 FieldDecl *ToField =
6413 llvm::cast_or_null<FieldDecl>(Import(From->getMember()));
6414 if (!ToField && From->getMember())
6415 return nullptr;
6416
6417 SmallVector<VarDecl *, 4> ToAIs(NumArrayIndices);
6418
6419 for (unsigned AII = 0; AII < NumArrayIndices; ++AII) {
6420 VarDecl *ToArrayIndex =
6421 dyn_cast_or_null<VarDecl>(Import(From->getArrayIndex(AII)));
6422 if (!ToArrayIndex && From->getArrayIndex(AII))
6423 return nullptr;
6424 }
6425
6426 return CXXCtorInitializer::Create(
6427 ToContext, ToField, Import(From->getMemberLocation()),
6428 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()),
6429 ToAIs.data(), NumArrayIndices);
6430 } else {
6431 return nullptr;
6432 }
6433}
6434
6435
Douglas Gregor0a791672011-01-18 03:11:38 +00006436void ASTImporter::ImportDefinition(Decl *From) {
6437 Decl *To = Import(From);
6438 if (!To)
6439 return;
6440
6441 if (DeclContext *FromDC = cast<DeclContext>(From)) {
6442 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00006443
6444 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
6445 if (!ToRecord->getDefinition()) {
6446 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00006447 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00006448 return;
6449 }
6450 }
Douglas Gregord451ea92011-07-29 23:31:30 +00006451
6452 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
6453 if (!ToEnum->getDefinition()) {
6454 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006455 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00006456 return;
6457 }
6458 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00006459
6460 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
6461 if (!ToIFace->getDefinition()) {
6462 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006463 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00006464 return;
6465 }
6466 }
Douglas Gregord451ea92011-07-29 23:31:30 +00006467
Douglas Gregor2aa53772012-01-24 17:42:07 +00006468 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
6469 if (!ToProto->getDefinition()) {
6470 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006471 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00006472 return;
6473 }
6474 }
6475
Douglas Gregor0a791672011-01-18 03:11:38 +00006476 Importer.ImportDeclContext(FromDC, true);
6477 }
6478}
6479
Douglas Gregor96e578d2010-02-05 17:54:41 +00006480DeclarationName ASTImporter::Import(DeclarationName FromName) {
6481 if (!FromName)
6482 return DeclarationName();
6483
6484 switch (FromName.getNameKind()) {
6485 case DeclarationName::Identifier:
6486 return Import(FromName.getAsIdentifierInfo());
6487
6488 case DeclarationName::ObjCZeroArgSelector:
6489 case DeclarationName::ObjCOneArgSelector:
6490 case DeclarationName::ObjCMultiArgSelector:
6491 return Import(FromName.getObjCSelector());
6492
6493 case DeclarationName::CXXConstructorName: {
6494 QualType T = Import(FromName.getCXXNameType());
6495 if (T.isNull())
6496 return DeclarationName();
6497
6498 return ToContext.DeclarationNames.getCXXConstructorName(
6499 ToContext.getCanonicalType(T));
6500 }
6501
6502 case DeclarationName::CXXDestructorName: {
6503 QualType T = Import(FromName.getCXXNameType());
6504 if (T.isNull())
6505 return DeclarationName();
6506
6507 return ToContext.DeclarationNames.getCXXDestructorName(
6508 ToContext.getCanonicalType(T));
6509 }
6510
6511 case DeclarationName::CXXConversionFunctionName: {
6512 QualType T = Import(FromName.getCXXNameType());
6513 if (T.isNull())
6514 return DeclarationName();
6515
6516 return ToContext.DeclarationNames.getCXXConversionFunctionName(
6517 ToContext.getCanonicalType(T));
6518 }
6519
6520 case DeclarationName::CXXOperatorName:
6521 return ToContext.DeclarationNames.getCXXOperatorName(
6522 FromName.getCXXOverloadedOperator());
6523
6524 case DeclarationName::CXXLiteralOperatorName:
6525 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
6526 Import(FromName.getCXXLiteralIdentifier()));
6527
6528 case DeclarationName::CXXUsingDirective:
6529 // FIXME: STATICS!
6530 return DeclarationName::getUsingDirectiveName();
6531 }
6532
David Blaikiee4d798f2012-01-20 21:50:17 +00006533 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00006534}
6535
Douglas Gregore2e50d332010-12-01 01:36:18 +00006536IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00006537 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00006538 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006539
6540 return &ToContext.Idents.get(FromId->getName());
6541}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006542
Douglas Gregor43f54792010-02-17 02:12:47 +00006543Selector ASTImporter::Import(Selector FromSel) {
6544 if (FromSel.isNull())
6545 return Selector();
6546
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006547 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00006548 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
6549 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
6550 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
6551 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
6552}
6553
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006554DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
6555 DeclContext *DC,
6556 unsigned IDNS,
6557 NamedDecl **Decls,
6558 unsigned NumDecls) {
6559 return Name;
6560}
6561
6562DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00006563 if (LastDiagFromFrom)
6564 ToContext.getDiagnostics().notePriorDiagnosticFrom(
6565 FromContext.getDiagnostics());
6566 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006567 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006568}
6569
6570DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00006571 if (!LastDiagFromFrom)
6572 FromContext.getDiagnostics().notePriorDiagnosticFrom(
6573 ToContext.getDiagnostics());
6574 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006575 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006576}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00006577
Douglas Gregor2e15c842012-02-01 21:00:38 +00006578void ASTImporter::CompleteDecl (Decl *D) {
6579 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
6580 if (!ID->getDefinition())
6581 ID->startDefinition();
6582 }
6583 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
6584 if (!PD->getDefinition())
6585 PD->startDefinition();
6586 }
6587 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
6588 if (!TD->getDefinition() && !TD->isBeingDefined()) {
6589 TD->startDefinition();
6590 TD->setCompleteDefinition(true);
6591 }
6592 }
6593 else {
6594 assert (0 && "CompleteDecl called on a Decl that can't be completed");
6595 }
6596}
6597
Douglas Gregor8cdbe642010-02-12 23:44:20 +00006598Decl *ASTImporter::Imported(Decl *From, Decl *To) {
Sean Callanan8bca9962016-03-28 21:43:01 +00006599 if (From->hasAttrs()) {
6600 for (Attr *FromAttr : From->getAttrs())
6601 To->addAttr(FromAttr->clone(To->getASTContext()));
6602 }
6603 if (From->isUsed()) {
6604 To->setIsUsed();
6605 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00006606 ImportedDecls[From] = To;
6607 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00006608}
Douglas Gregorb4964f72010-02-15 23:54:17 +00006609
Douglas Gregordd6006f2012-07-17 21:16:27 +00006610bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
6611 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00006612 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00006613 = ImportedTypes.find(From.getTypePtr());
6614 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
6615 return true;
6616
Douglas Gregordd6006f2012-07-17 21:16:27 +00006617 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
6618 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00006619 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00006620}