blob: e783854448bbae76776fe4a0cc1253729986c262 [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);
Aleksei Sidorina693b372016-09-28 10:16:56 +000043 QualType VisitDecayedType(const DecayedType *T);
John McCall424cec92011-01-19 06:33:43 +000044 QualType VisitComplexType(const ComplexType *T);
45 QualType VisitPointerType(const PointerType *T);
46 QualType VisitBlockPointerType(const BlockPointerType *T);
47 QualType VisitLValueReferenceType(const LValueReferenceType *T);
48 QualType VisitRValueReferenceType(const RValueReferenceType *T);
49 QualType VisitMemberPointerType(const MemberPointerType *T);
50 QualType VisitConstantArrayType(const ConstantArrayType *T);
51 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
52 QualType VisitVariableArrayType(const VariableArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000053 // FIXME: DependentSizedArrayType
54 // FIXME: DependentSizedExtVectorType
John McCall424cec92011-01-19 06:33:43 +000055 QualType VisitVectorType(const VectorType *T);
56 QualType VisitExtVectorType(const ExtVectorType *T);
57 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
58 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000059 // FIXME: UnresolvedUsingType
Sean Callananda6df8a2011-08-11 16:56:07 +000060 QualType VisitParenType(const ParenType *T);
John McCall424cec92011-01-19 06:33:43 +000061 QualType VisitTypedefType(const TypedefType *T);
62 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000063 // FIXME: DependentTypeOfExprType
John McCall424cec92011-01-19 06:33:43 +000064 QualType VisitTypeOfType(const TypeOfType *T);
65 QualType VisitDecltypeType(const DecltypeType *T);
Alexis Hunte852b102011-05-24 22:41:36 +000066 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith30482bc2011-02-20 03:19:35 +000067 QualType VisitAutoType(const AutoType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000068 QualType VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000069 // FIXME: DependentDecltypeType
John McCall424cec92011-01-19 06:33:43 +000070 QualType VisitRecordType(const RecordType *T);
71 QualType VisitEnumType(const EnumType *T);
Sean Callanan72fe0852015-04-02 23:50:08 +000072 QualType VisitAttributedType(const AttributedType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000073 QualType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000074 // FIXME: SubstTemplateTypeParmType
John McCall424cec92011-01-19 06:33:43 +000075 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
76 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +000077 // FIXME: DependentNameType
John McCallc392f372010-06-11 00:33:02 +000078 // FIXME: DependentTemplateSpecializationType
John McCall424cec92011-01-19 06:33:43 +000079 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
80 QualType VisitObjCObjectType(const ObjCObjectType *T);
81 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000082
Douglas Gregor95d82832012-01-24 18:36:04 +000083 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +000084 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
85 DeclContext *&LexicalDC, DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +000086 NamedDecl *&ToD, SourceLocation &Loc);
Craig Topper36250ad2014-05-12 05:36:57 +000087 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +000088 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
89 DeclarationNameInfo& To);
Douglas Gregor0a791672011-01-18 03:11:38 +000090 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000091
Aleksei Sidorina693b372016-09-28 10:16:56 +000092 bool ImportCastPath(CastExpr *E, CXXCastPath &Path);
93
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000094 typedef DesignatedInitExpr::Designator Designator;
95 Designator ImportDesignator(const Designator &D);
96
Douglas Gregor2e15c842012-02-01 21:00:38 +000097
Douglas Gregor95d82832012-01-24 18:36:04 +000098 /// \brief What we should import from the definition.
99 enum ImportDefinitionKind {
100 /// \brief Import the default subset of the definition, which might be
101 /// nothing (if minimal import is set) or might be everything (if minimal
102 /// import is not set).
103 IDK_Default,
104 /// \brief Import everything.
105 IDK_Everything,
106 /// \brief Import only the bare bones needed to establish a valid
107 /// DeclContext.
108 IDK_Basic
109 };
110
Douglas Gregor2e15c842012-02-01 21:00:38 +0000111 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
112 return IDK == IDK_Everything ||
113 (IDK == IDK_Default && !Importer.isMinimalImport());
114 }
115
Douglas Gregord451ea92011-07-29 23:31:30 +0000116 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000117 ImportDefinitionKind Kind = IDK_Default);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000118 bool ImportDefinition(VarDecl *From, VarDecl *To,
119 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregord451ea92011-07-29 23:31:30 +0000120 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000121 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000122 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000123 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000124 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000125 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregora082a492010-11-30 19:14:50 +0000126 TemplateParameterList *ImportTemplateParameterList(
127 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000128 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000129 TemplateArgumentLoc ImportTemplateArgumentLoc(
130 const TemplateArgumentLoc &TALoc, bool &Error);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000131 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
132 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000133 SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000134 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
135 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000136 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
137 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000138 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000139 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Douglas Gregora082a492010-11-30 19:14:50 +0000140 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000141 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000142 Decl *VisitDecl(Decl *D);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +0000143 Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000144 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000145 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000146 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000147 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000148 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000149 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000150 Decl *VisitLabelDecl(LabelDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000151 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000152 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000153 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000154 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000155 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
156 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
157 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
158 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000159 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000160 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000161 Decl *VisitFriendDecl(FriendDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000162 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000163 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000164 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000165 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000166 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000167 Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000168 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000169 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Sean Callanan0aae0412014-12-10 00:00:37 +0000170 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000171
172 ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list);
Douglas Gregor45635322010-02-16 01:20:57 +0000173 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000174 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000175 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000176 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000177 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000178 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
179 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
180 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
181 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000182 Decl *VisitClassTemplateSpecializationDecl(
183 ClassTemplateSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000184 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
185 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
186
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000187 // Importing statements
Sean Callanan59721b32015-04-28 18:41:46 +0000188 DeclGroupRef ImportDeclGroup(DeclGroupRef DG);
189
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000190 Stmt *VisitStmt(Stmt *S);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000191 Stmt *VisitGCCAsmStmt(GCCAsmStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000192 Stmt *VisitDeclStmt(DeclStmt *S);
193 Stmt *VisitNullStmt(NullStmt *S);
194 Stmt *VisitCompoundStmt(CompoundStmt *S);
195 Stmt *VisitCaseStmt(CaseStmt *S);
196 Stmt *VisitDefaultStmt(DefaultStmt *S);
197 Stmt *VisitLabelStmt(LabelStmt *S);
198 Stmt *VisitAttributedStmt(AttributedStmt *S);
199 Stmt *VisitIfStmt(IfStmt *S);
200 Stmt *VisitSwitchStmt(SwitchStmt *S);
201 Stmt *VisitWhileStmt(WhileStmt *S);
202 Stmt *VisitDoStmt(DoStmt *S);
203 Stmt *VisitForStmt(ForStmt *S);
204 Stmt *VisitGotoStmt(GotoStmt *S);
205 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S);
206 Stmt *VisitContinueStmt(ContinueStmt *S);
207 Stmt *VisitBreakStmt(BreakStmt *S);
208 Stmt *VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000209 // FIXME: MSAsmStmt
210 // FIXME: SEHExceptStmt
211 // FIXME: SEHFinallyStmt
212 // FIXME: SEHTryStmt
213 // FIXME: SEHLeaveStmt
214 // FIXME: CapturedStmt
215 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S);
216 Stmt *VisitCXXTryStmt(CXXTryStmt *S);
217 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S);
218 // FIXME: MSDependentExistsStmt
219 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
220 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
221 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
222 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
223 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
224 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
225 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000226
227 // Importing expressions
228 Expr *VisitExpr(Expr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000229 Expr *VisitVAArgExpr(VAArgExpr *E);
230 Expr *VisitGNUNullExpr(GNUNullExpr *E);
231 Expr *VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000232 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000233 Expr *VisitImplicitValueInitExpr(ImplicitValueInitExpr *ILE);
234 Expr *VisitDesignatedInitExpr(DesignatedInitExpr *E);
235 Expr *VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000236 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000237 Expr *VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000238 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000239 Expr *VisitStringLiteral(StringLiteral *E);
240 Expr *VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
241 Expr *VisitAtomicExpr(AtomicExpr *E);
242 Expr *VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000243 Expr *VisitParenExpr(ParenExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000244 Expr *VisitParenListExpr(ParenListExpr *E);
245 Expr *VisitStmtExpr(StmtExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000246 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000247 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000248 Expr *VisitBinaryOperator(BinaryOperator *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000249 Expr *VisitConditionalOperator(ConditionalOperator *E);
250 Expr *VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
251 Expr *VisitOpaqueValueExpr(OpaqueValueExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000252 Expr *VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
253 Expr *VisitExpressionTraitExpr(ExpressionTraitExpr *E);
254 Expr *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000255 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000256 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000257 Expr *VisitExplicitCastExpr(ExplicitCastExpr *E);
258 Expr *VisitOffsetOfExpr(OffsetOfExpr *OE);
259 Expr *VisitCXXThrowExpr(CXXThrowExpr *E);
260 Expr *VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
261 Expr *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
262 Expr *VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
263 Expr *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
264 Expr *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE);
265 Expr *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
266 Expr *VisitCXXNewExpr(CXXNewExpr *CE);
267 Expr *VisitCXXDeleteExpr(CXXDeleteExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000268 Expr *VisitCXXConstructExpr(CXXConstructExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000269 Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000270 Expr *VisitExprWithCleanups(ExprWithCleanups *EWC);
Sean Callanan8bca9962016-03-28 21:43:01 +0000271 Expr *VisitCXXThisExpr(CXXThisExpr *E);
272 Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000273 Expr *VisitMemberExpr(MemberExpr *E);
274 Expr *VisitCallExpr(CallExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000275 Expr *VisitInitListExpr(InitListExpr *E);
Sean Callanandd2c1742016-05-16 20:48:03 +0000276 Expr *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
277 Expr *VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000278
279 template<typename IIter, typename OIter>
280 void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) {
281 typedef typename std::remove_reference<decltype(*Obegin)>::type ItemT;
282 ASTImporter &ImporterRef = Importer;
283 std::transform(Ibegin, Iend, Obegin,
284 [&ImporterRef](ItemT From) -> ItemT {
285 return ImporterRef.Import(From);
Sean Callanan8bca9962016-03-28 21:43:01 +0000286 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000287 }
288
289 template<typename IIter, typename OIter>
290 bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
291 typedef typename std::remove_reference<decltype(**Obegin)>::type ItemT;
292 ASTImporter &ImporterRef = Importer;
293 bool Failed = false;
294 std::transform(Ibegin, Iend, Obegin,
295 [&ImporterRef, &Failed](ItemT *From) -> ItemT * {
Aleksei Sidorina693b372016-09-28 10:16:56 +0000296 ItemT *To = cast_or_null<ItemT>(
297 ImporterRef.Import(From));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000298 if (!To && From)
299 Failed = true;
300 return To;
301 });
302 return Failed;
Sean Callanan8bca9962016-03-28 21:43:01 +0000303 }
Aleksei Sidorina693b372016-09-28 10:16:56 +0000304
305 template<typename InContainerTy, typename OutContainerTy>
306 bool ImportContainerChecked(const InContainerTy &InContainer,
307 OutContainerTy &OutContainer) {
308 return ImportArrayChecked(InContainer.begin(), InContainer.end(),
309 OutContainer.begin());
310 }
311
312 template<typename InContainerTy, typename OIter>
313 bool ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
314 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
315 }
Douglas Gregor96e578d2010-02-05 17:54:41 +0000316 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000317}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000318
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000319using namespace clang;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000320
321//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000322// Structural Equivalence
323//----------------------------------------------------------------------------
324
325namespace {
326 struct StructuralEquivalenceContext {
327 /// \brief AST contexts for which we are checking structural equivalence.
328 ASTContext &C1, &C2;
329
Douglas Gregor3996e242010-02-15 22:01:00 +0000330 /// \brief The set of "tentative" equivalences between two canonical
331 /// declarations, mapping from a declaration in the first context to the
332 /// declaration in the second context that we believe to be equivalent.
333 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
334
335 /// \brief Queue of declarations in the first context whose equivalence
336 /// with a declaration in the second context still needs to be verified.
337 std::deque<Decl *> DeclsToCheck;
338
Douglas Gregorb4964f72010-02-15 23:54:17 +0000339 /// \brief Declaration (from, to) pairs that are known not to be equivalent
340 /// (which we have already complained about).
341 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
342
Douglas Gregor3996e242010-02-15 22:01:00 +0000343 /// \brief Whether we're being strict about the spelling of types when
344 /// unifying two types.
345 bool StrictTypeSpelling;
Douglas Gregordd6006f2012-07-17 21:16:27 +0000346
347 /// \brief Whether to complain about failures.
348 bool Complain;
349
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000350 /// \brief \c true if the last diagnostic came from C2.
351 bool LastDiagFromC2;
352
Douglas Gregor3996e242010-02-15 22:01:00 +0000353 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000354 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregordd6006f2012-07-17 21:16:27 +0000355 bool StrictTypeSpelling = false,
356 bool Complain = true)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000357 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000358 StrictTypeSpelling(StrictTypeSpelling), Complain(Complain),
359 LastDiagFromC2(false) {}
Douglas Gregor3996e242010-02-15 22:01:00 +0000360
361 /// \brief Determine whether the two declarations are structurally
362 /// equivalent.
363 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
364
365 /// \brief Determine whether the two types are structurally equivalent.
366 bool IsStructurallyEquivalent(QualType T1, QualType T2);
367
368 private:
369 /// \brief Finish checking all of the structural equivalences.
370 ///
371 /// \returns true if an error occurred, false otherwise.
372 bool Finish();
373
374 public:
375 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000376 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000377 if (LastDiagFromC2)
378 C1.getDiagnostics().notePriorDiagnosticFrom(C2.getDiagnostics());
379 LastDiagFromC2 = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000380 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000381 }
382
383 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000384 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000385 if (!LastDiagFromC2)
386 C2.getDiagnostics().notePriorDiagnosticFrom(C1.getDiagnostics());
387 LastDiagFromC2 = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000388 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000389 }
390 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000391}
Douglas Gregor3996e242010-02-15 22:01:00 +0000392
393static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
394 QualType T1, QualType T2);
395static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
396 Decl *D1, Decl *D2);
397
Douglas Gregor3996e242010-02-15 22:01:00 +0000398/// \brief Determine structural equivalence of two expressions.
399static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
400 Expr *E1, Expr *E2) {
401 if (!E1 || !E2)
402 return E1 == E2;
403
404 // FIXME: Actually perform a structural comparison!
405 return true;
406}
407
408/// \brief Determine whether two identifiers are equivalent.
409static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
410 const IdentifierInfo *Name2) {
411 if (!Name1 || !Name2)
412 return Name1 == Name2;
413
414 return Name1->getName() == Name2->getName();
415}
416
417/// \brief Determine whether two nested-name-specifiers are equivalent.
418static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
419 NestedNameSpecifier *NNS1,
420 NestedNameSpecifier *NNS2) {
421 // FIXME: Implement!
422 return true;
423}
424
425/// \brief Determine whether two template arguments are equivalent.
426static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
427 const TemplateArgument &Arg1,
428 const TemplateArgument &Arg2) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000429 if (Arg1.getKind() != Arg2.getKind())
430 return false;
431
432 switch (Arg1.getKind()) {
433 case TemplateArgument::Null:
434 return true;
435
436 case TemplateArgument::Type:
437 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
Eli Friedmanb826a002012-09-26 02:36:12 +0000438
Douglas Gregore2e50d332010-12-01 01:36:18 +0000439 case TemplateArgument::Integral:
440 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
441 Arg2.getIntegralType()))
442 return false;
443
Eric Christopher6dcc3762012-07-15 00:23:57 +0000444 return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral());
Douglas Gregore2e50d332010-12-01 01:36:18 +0000445
446 case TemplateArgument::Declaration:
447 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +0000448
449 case TemplateArgument::NullPtr:
450 return true; // FIXME: Is this correct?
451
Douglas Gregore2e50d332010-12-01 01:36:18 +0000452 case TemplateArgument::Template:
453 return IsStructurallyEquivalent(Context,
454 Arg1.getAsTemplate(),
455 Arg2.getAsTemplate());
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000456
457 case TemplateArgument::TemplateExpansion:
458 return IsStructurallyEquivalent(Context,
459 Arg1.getAsTemplateOrTemplatePattern(),
460 Arg2.getAsTemplateOrTemplatePattern());
461
Douglas Gregore2e50d332010-12-01 01:36:18 +0000462 case TemplateArgument::Expression:
463 return IsStructurallyEquivalent(Context,
464 Arg1.getAsExpr(), Arg2.getAsExpr());
465
466 case TemplateArgument::Pack:
467 if (Arg1.pack_size() != Arg2.pack_size())
468 return false;
469
470 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
471 if (!IsStructurallyEquivalent(Context,
472 Arg1.pack_begin()[I],
473 Arg2.pack_begin()[I]))
474 return false;
475
476 return true;
477 }
478
479 llvm_unreachable("Invalid template argument kind");
Douglas Gregor3996e242010-02-15 22:01:00 +0000480}
481
482/// \brief Determine structural equivalence for the common part of array
483/// types.
484static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
485 const ArrayType *Array1,
486 const ArrayType *Array2) {
487 if (!IsStructurallyEquivalent(Context,
488 Array1->getElementType(),
489 Array2->getElementType()))
490 return false;
491 if (Array1->getSizeModifier() != Array2->getSizeModifier())
492 return false;
493 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
494 return false;
495
496 return true;
497}
498
499/// \brief Determine structural equivalence of two types.
500static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
501 QualType T1, QualType T2) {
502 if (T1.isNull() || T2.isNull())
503 return T1.isNull() && T2.isNull();
504
505 if (!Context.StrictTypeSpelling) {
506 // We aren't being strict about token-to-token equivalence of types,
507 // so map down to the canonical type.
508 T1 = Context.C1.getCanonicalType(T1);
509 T2 = Context.C2.getCanonicalType(T2);
510 }
511
512 if (T1.getQualifiers() != T2.getQualifiers())
513 return false;
514
Douglas Gregorb4964f72010-02-15 23:54:17 +0000515 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000516
Douglas Gregorb4964f72010-02-15 23:54:17 +0000517 if (T1->getTypeClass() != T2->getTypeClass()) {
518 // Compare function types with prototypes vs. without prototypes as if
519 // both did not have prototypes.
520 if (T1->getTypeClass() == Type::FunctionProto &&
521 T2->getTypeClass() == Type::FunctionNoProto)
522 TC = Type::FunctionNoProto;
523 else if (T1->getTypeClass() == Type::FunctionNoProto &&
524 T2->getTypeClass() == Type::FunctionProto)
525 TC = Type::FunctionNoProto;
526 else
527 return false;
528 }
529
530 switch (TC) {
531 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000532 // FIXME: Deal with Char_S/Char_U.
533 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
534 return false;
535 break;
536
537 case Type::Complex:
538 if (!IsStructurallyEquivalent(Context,
539 cast<ComplexType>(T1)->getElementType(),
540 cast<ComplexType>(T2)->getElementType()))
541 return false;
542 break;
543
Reid Kleckner0503a872013-12-05 01:23:43 +0000544 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +0000545 case Type::Decayed:
546 if (!IsStructurallyEquivalent(Context,
Reid Kleckner0503a872013-12-05 01:23:43 +0000547 cast<AdjustedType>(T1)->getOriginalType(),
548 cast<AdjustedType>(T2)->getOriginalType()))
Reid Kleckner8a365022013-06-24 17:51:48 +0000549 return false;
550 break;
551
Douglas Gregor3996e242010-02-15 22:01:00 +0000552 case Type::Pointer:
553 if (!IsStructurallyEquivalent(Context,
554 cast<PointerType>(T1)->getPointeeType(),
555 cast<PointerType>(T2)->getPointeeType()))
556 return false;
557 break;
558
559 case Type::BlockPointer:
560 if (!IsStructurallyEquivalent(Context,
561 cast<BlockPointerType>(T1)->getPointeeType(),
562 cast<BlockPointerType>(T2)->getPointeeType()))
563 return false;
564 break;
565
566 case Type::LValueReference:
567 case Type::RValueReference: {
568 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
569 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
570 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
571 return false;
572 if (Ref1->isInnerRef() != Ref2->isInnerRef())
573 return false;
574 if (!IsStructurallyEquivalent(Context,
575 Ref1->getPointeeTypeAsWritten(),
576 Ref2->getPointeeTypeAsWritten()))
577 return false;
578 break;
579 }
580
581 case Type::MemberPointer: {
582 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
583 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
584 if (!IsStructurallyEquivalent(Context,
585 MemPtr1->getPointeeType(),
586 MemPtr2->getPointeeType()))
587 return false;
588 if (!IsStructurallyEquivalent(Context,
589 QualType(MemPtr1->getClass(), 0),
590 QualType(MemPtr2->getClass(), 0)))
591 return false;
592 break;
593 }
594
595 case Type::ConstantArray: {
596 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
597 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
Eric Christopher6dcc3762012-07-15 00:23:57 +0000598 if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000599 return false;
600
601 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
602 return false;
603 break;
604 }
605
606 case Type::IncompleteArray:
607 if (!IsArrayStructurallyEquivalent(Context,
608 cast<ArrayType>(T1),
609 cast<ArrayType>(T2)))
610 return false;
611 break;
612
613 case Type::VariableArray: {
614 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
615 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
616 if (!IsStructurallyEquivalent(Context,
617 Array1->getSizeExpr(), Array2->getSizeExpr()))
618 return false;
619
620 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
621 return false;
622
623 break;
624 }
625
626 case Type::DependentSizedArray: {
627 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
628 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
629 if (!IsStructurallyEquivalent(Context,
630 Array1->getSizeExpr(), Array2->getSizeExpr()))
631 return false;
632
633 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
634 return false;
635
636 break;
637 }
638
639 case Type::DependentSizedExtVector: {
640 const DependentSizedExtVectorType *Vec1
641 = cast<DependentSizedExtVectorType>(T1);
642 const DependentSizedExtVectorType *Vec2
643 = cast<DependentSizedExtVectorType>(T2);
644 if (!IsStructurallyEquivalent(Context,
645 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
646 return false;
647 if (!IsStructurallyEquivalent(Context,
648 Vec1->getElementType(),
649 Vec2->getElementType()))
650 return false;
651 break;
652 }
653
654 case Type::Vector:
655 case Type::ExtVector: {
656 const VectorType *Vec1 = cast<VectorType>(T1);
657 const VectorType *Vec2 = cast<VectorType>(T2);
658 if (!IsStructurallyEquivalent(Context,
659 Vec1->getElementType(),
660 Vec2->getElementType()))
661 return false;
662 if (Vec1->getNumElements() != Vec2->getNumElements())
663 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000664 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000665 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000666 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000667 }
668
669 case Type::FunctionProto: {
670 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
671 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
Alp Toker9cacbab2014-01-20 20:26:09 +0000672 if (Proto1->getNumParams() != Proto2->getNumParams())
Douglas Gregor3996e242010-02-15 22:01:00 +0000673 return false;
Alp Toker9cacbab2014-01-20 20:26:09 +0000674 for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) {
675 if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I),
676 Proto2->getParamType(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000677 return false;
678 }
679 if (Proto1->isVariadic() != Proto2->isVariadic())
680 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000681 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor3996e242010-02-15 22:01:00 +0000682 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000683 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
684 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
685 return false;
686 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
687 if (!IsStructurallyEquivalent(Context,
688 Proto1->getExceptionType(I),
689 Proto2->getExceptionType(I)))
690 return false;
691 }
692 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000693 if (!IsStructurallyEquivalent(Context,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000694 Proto1->getNoexceptExpr(),
695 Proto2->getNoexceptExpr()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000696 return false;
697 }
698 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
699 return false;
700
701 // Fall through to check the bits common with FunctionNoProtoType.
702 }
703
704 case Type::FunctionNoProto: {
705 const FunctionType *Function1 = cast<FunctionType>(T1);
706 const FunctionType *Function2 = cast<FunctionType>(T2);
Alp Toker314cc812014-01-25 16:55:45 +0000707 if (!IsStructurallyEquivalent(Context, Function1->getReturnType(),
708 Function2->getReturnType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000709 return false;
Justin Bogner62c04de2016-03-20 16:58:03 +0000710 if (Function1->getExtInfo() != Function2->getExtInfo())
711 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000712 break;
713 }
714
715 case Type::UnresolvedUsing:
716 if (!IsStructurallyEquivalent(Context,
717 cast<UnresolvedUsingType>(T1)->getDecl(),
718 cast<UnresolvedUsingType>(T2)->getDecl()))
719 return false;
720
721 break;
John McCall81904512011-01-06 01:58:22 +0000722
723 case Type::Attributed:
724 if (!IsStructurallyEquivalent(Context,
725 cast<AttributedType>(T1)->getModifiedType(),
726 cast<AttributedType>(T2)->getModifiedType()))
727 return false;
728 if (!IsStructurallyEquivalent(Context,
729 cast<AttributedType>(T1)->getEquivalentType(),
730 cast<AttributedType>(T2)->getEquivalentType()))
731 return false;
732 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000733
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000734 case Type::Paren:
735 if (!IsStructurallyEquivalent(Context,
736 cast<ParenType>(T1)->getInnerType(),
737 cast<ParenType>(T2)->getInnerType()))
738 return false;
739 break;
740
Douglas Gregor3996e242010-02-15 22:01:00 +0000741 case Type::Typedef:
742 if (!IsStructurallyEquivalent(Context,
743 cast<TypedefType>(T1)->getDecl(),
744 cast<TypedefType>(T2)->getDecl()))
745 return false;
746 break;
747
748 case Type::TypeOfExpr:
749 if (!IsStructurallyEquivalent(Context,
750 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
751 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
752 return false;
753 break;
754
755 case Type::TypeOf:
756 if (!IsStructurallyEquivalent(Context,
757 cast<TypeOfType>(T1)->getUnderlyingType(),
758 cast<TypeOfType>(T2)->getUnderlyingType()))
759 return false;
760 break;
Alexis Hunte852b102011-05-24 22:41:36 +0000761
762 case Type::UnaryTransform:
763 if (!IsStructurallyEquivalent(Context,
764 cast<UnaryTransformType>(T1)->getUnderlyingType(),
765 cast<UnaryTransformType>(T1)->getUnderlyingType()))
766 return false;
767 break;
768
Douglas Gregor3996e242010-02-15 22:01:00 +0000769 case Type::Decltype:
770 if (!IsStructurallyEquivalent(Context,
771 cast<DecltypeType>(T1)->getUnderlyingExpr(),
772 cast<DecltypeType>(T2)->getUnderlyingExpr()))
773 return false;
774 break;
775
Richard Smith30482bc2011-02-20 03:19:35 +0000776 case Type::Auto:
777 if (!IsStructurallyEquivalent(Context,
778 cast<AutoType>(T1)->getDeducedType(),
779 cast<AutoType>(T2)->getDeducedType()))
780 return false;
781 break;
782
Douglas Gregor3996e242010-02-15 22:01:00 +0000783 case Type::Record:
784 case Type::Enum:
785 if (!IsStructurallyEquivalent(Context,
786 cast<TagType>(T1)->getDecl(),
787 cast<TagType>(T2)->getDecl()))
788 return false;
789 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000790
Douglas Gregor3996e242010-02-15 22:01:00 +0000791 case Type::TemplateTypeParm: {
792 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
793 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
794 if (Parm1->getDepth() != Parm2->getDepth())
795 return false;
796 if (Parm1->getIndex() != Parm2->getIndex())
797 return false;
798 if (Parm1->isParameterPack() != Parm2->isParameterPack())
799 return false;
800
801 // Names of template type parameters are never significant.
802 break;
803 }
804
805 case Type::SubstTemplateTypeParm: {
806 const SubstTemplateTypeParmType *Subst1
807 = cast<SubstTemplateTypeParmType>(T1);
808 const SubstTemplateTypeParmType *Subst2
809 = cast<SubstTemplateTypeParmType>(T2);
810 if (!IsStructurallyEquivalent(Context,
811 QualType(Subst1->getReplacedParameter(), 0),
812 QualType(Subst2->getReplacedParameter(), 0)))
813 return false;
814 if (!IsStructurallyEquivalent(Context,
815 Subst1->getReplacementType(),
816 Subst2->getReplacementType()))
817 return false;
818 break;
819 }
820
Douglas Gregorfb322d82011-01-14 05:11:40 +0000821 case Type::SubstTemplateTypeParmPack: {
822 const SubstTemplateTypeParmPackType *Subst1
823 = cast<SubstTemplateTypeParmPackType>(T1);
824 const SubstTemplateTypeParmPackType *Subst2
825 = cast<SubstTemplateTypeParmPackType>(T2);
826 if (!IsStructurallyEquivalent(Context,
827 QualType(Subst1->getReplacedParameter(), 0),
828 QualType(Subst2->getReplacedParameter(), 0)))
829 return false;
830 if (!IsStructurallyEquivalent(Context,
831 Subst1->getArgumentPack(),
832 Subst2->getArgumentPack()))
833 return false;
834 break;
835 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000836 case Type::TemplateSpecialization: {
837 const TemplateSpecializationType *Spec1
838 = cast<TemplateSpecializationType>(T1);
839 const TemplateSpecializationType *Spec2
840 = cast<TemplateSpecializationType>(T2);
841 if (!IsStructurallyEquivalent(Context,
842 Spec1->getTemplateName(),
843 Spec2->getTemplateName()))
844 return false;
845 if (Spec1->getNumArgs() != Spec2->getNumArgs())
846 return false;
847 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
848 if (!IsStructurallyEquivalent(Context,
849 Spec1->getArg(I), Spec2->getArg(I)))
850 return false;
851 }
852 break;
853 }
854
Abramo Bagnara6150c882010-05-11 21:36:43 +0000855 case Type::Elaborated: {
856 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
857 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
858 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
859 if (Elab1->getKeyword() != Elab2->getKeyword())
860 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000861 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000862 Elab1->getQualifier(),
863 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000864 return false;
865 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000866 Elab1->getNamedType(),
867 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000868 return false;
869 break;
870 }
871
John McCalle78aac42010-03-10 03:28:59 +0000872 case Type::InjectedClassName: {
873 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
874 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
875 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000876 Inj1->getInjectedSpecializationType(),
877 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000878 return false;
879 break;
880 }
881
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000882 case Type::DependentName: {
883 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
884 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000885 if (!IsStructurallyEquivalent(Context,
886 Typename1->getQualifier(),
887 Typename2->getQualifier()))
888 return false;
889 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
890 Typename2->getIdentifier()))
891 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000892
893 break;
894 }
895
John McCallc392f372010-06-11 00:33:02 +0000896 case Type::DependentTemplateSpecialization: {
897 const DependentTemplateSpecializationType *Spec1 =
898 cast<DependentTemplateSpecializationType>(T1);
899 const DependentTemplateSpecializationType *Spec2 =
900 cast<DependentTemplateSpecializationType>(T2);
901 if (!IsStructurallyEquivalent(Context,
902 Spec1->getQualifier(),
903 Spec2->getQualifier()))
904 return false;
905 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
906 Spec2->getIdentifier()))
907 return false;
908 if (Spec1->getNumArgs() != Spec2->getNumArgs())
909 return false;
910 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
911 if (!IsStructurallyEquivalent(Context,
912 Spec1->getArg(I), Spec2->getArg(I)))
913 return false;
914 }
915 break;
916 }
Douglas Gregord2fa7662010-12-20 02:24:11 +0000917
918 case Type::PackExpansion:
919 if (!IsStructurallyEquivalent(Context,
920 cast<PackExpansionType>(T1)->getPattern(),
921 cast<PackExpansionType>(T2)->getPattern()))
922 return false;
923 break;
924
Douglas Gregor3996e242010-02-15 22:01:00 +0000925 case Type::ObjCInterface: {
926 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
927 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
928 if (!IsStructurallyEquivalent(Context,
929 Iface1->getDecl(), Iface2->getDecl()))
930 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000931 break;
932 }
933
Manman Rene6be26c2016-09-13 17:25:08 +0000934 case Type::ObjCTypeParam: {
935 const ObjCTypeParamType *Obj1 = cast<ObjCTypeParamType>(T1);
936 const ObjCTypeParamType *Obj2 = cast<ObjCTypeParamType>(T2);
937 if (!IsStructurallyEquivalent(Context, Obj1->getDecl(),
938 Obj2->getDecl()))
939 return false;
940
941 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
942 return false;
943 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
944 if (!IsStructurallyEquivalent(Context,
945 Obj1->getProtocol(I),
946 Obj2->getProtocol(I)))
947 return false;
948 }
949 break;
950 }
John McCall8b07ec22010-05-15 11:32:37 +0000951 case Type::ObjCObject: {
952 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
953 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
954 if (!IsStructurallyEquivalent(Context,
955 Obj1->getBaseType(),
956 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000957 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000958 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
959 return false;
960 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000961 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000962 Obj1->getProtocol(I),
963 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000964 return false;
965 }
966 break;
967 }
968
969 case Type::ObjCObjectPointer: {
970 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
971 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
972 if (!IsStructurallyEquivalent(Context,
973 Ptr1->getPointeeType(),
974 Ptr2->getPointeeType()))
975 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000976 break;
977 }
Eli Friedman0dfb8892011-10-06 23:00:33 +0000978
979 case Type::Atomic: {
980 if (!IsStructurallyEquivalent(Context,
981 cast<AtomicType>(T1)->getValueType(),
982 cast<AtomicType>(T2)->getValueType()))
983 return false;
984 break;
985 }
986
Xiuli Pan9c14e282016-01-09 12:53:17 +0000987 case Type::Pipe: {
988 if (!IsStructurallyEquivalent(Context,
989 cast<PipeType>(T1)->getElementType(),
990 cast<PipeType>(T2)->getElementType()))
991 return false;
992 break;
993 }
994
Douglas Gregor3996e242010-02-15 22:01:00 +0000995 } // end switch
996
997 return true;
998}
999
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001000/// \brief Determine structural equivalence of two fields.
1001static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1002 FieldDecl *Field1, FieldDecl *Field2) {
1003 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001004
1005 // For anonymous structs/unions, match up the anonymous struct/union type
1006 // declarations directly, so that we don't go off searching for anonymous
1007 // types
1008 if (Field1->isAnonymousStructOrUnion() &&
1009 Field2->isAnonymousStructOrUnion()) {
1010 RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl();
1011 RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl();
1012 return IsStructurallyEquivalent(Context, D1, D2);
1013 }
Sean Callanan969c5bd2013-04-26 22:49:25 +00001014
1015 // Check for equivalent field names.
1016 IdentifierInfo *Name1 = Field1->getIdentifier();
1017 IdentifierInfo *Name2 = Field2->getIdentifier();
1018 if (!::IsStructurallyEquivalent(Name1, Name2))
1019 return false;
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001020
1021 if (!IsStructurallyEquivalent(Context,
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001022 Field1->getType(), Field2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001023 if (Context.Complain) {
1024 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1025 << Context.C2.getTypeDeclType(Owner2);
1026 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1027 << Field2->getDeclName() << Field2->getType();
1028 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
1029 << Field1->getDeclName() << Field1->getType();
1030 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001031 return false;
1032 }
1033
1034 if (Field1->isBitField() != Field2->isBitField()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001035 if (Context.Complain) {
1036 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1037 << Context.C2.getTypeDeclType(Owner2);
1038 if (Field1->isBitField()) {
1039 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
1040 << Field1->getDeclName() << Field1->getType()
1041 << Field1->getBitWidthValue(Context.C1);
1042 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
1043 << Field2->getDeclName();
1044 } else {
1045 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
1046 << Field2->getDeclName() << Field2->getType()
1047 << Field2->getBitWidthValue(Context.C2);
1048 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
1049 << Field1->getDeclName();
1050 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001051 }
1052 return false;
1053 }
1054
1055 if (Field1->isBitField()) {
1056 // Make sure that the bit-fields are the same length.
1057 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
1058 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
1059
1060 if (Bits1 != Bits2) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001061 if (Context.Complain) {
1062 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1063 << Context.C2.getTypeDeclType(Owner2);
1064 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
1065 << Field2->getDeclName() << Field2->getType() << Bits2;
1066 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
1067 << Field1->getDeclName() << Field1->getType() << Bits1;
1068 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001069 return false;
1070 }
1071 }
1072
1073 return true;
1074}
1075
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001076/// \brief Find the index of the given anonymous struct/union within its
1077/// context.
1078///
1079/// \returns Returns the index of this anonymous struct/union in its context,
1080/// including the next assigned index (if none of them match). Returns an
1081/// empty option if the context is not a record, i.e.. if the anonymous
1082/// struct/union is at namespace or block scope.
Sean Callanan488f8612016-07-14 19:53:44 +00001083static Optional<unsigned> findUntaggedStructOrUnionIndex(RecordDecl *Anon) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001084 ASTContext &Context = Anon->getASTContext();
1085 QualType AnonTy = Context.getRecordType(Anon);
1086
1087 RecordDecl *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext());
1088 if (!Owner)
David Blaikie7a30dc52013-02-21 01:47:18 +00001089 return None;
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001090
1091 unsigned Index = 0;
Aaron Ballman629afae2014-03-07 19:56:05 +00001092 for (const auto *D : Owner->noload_decls()) {
1093 const auto *F = dyn_cast<FieldDecl>(D);
Sean Callanan488f8612016-07-14 19:53:44 +00001094 if (!F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001095 continue;
1096
Sean Callanan488f8612016-07-14 19:53:44 +00001097 if (F->isAnonymousStructOrUnion()) {
1098 if (Context.hasSameType(F->getType(), AnonTy))
1099 break;
1100 ++Index;
1101 continue;
1102 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001103
Sean Callanan488f8612016-07-14 19:53:44 +00001104 // If the field looks like this:
1105 // struct { ... } A;
1106 QualType FieldType = F->getType();
1107 if (const auto *RecType = dyn_cast<RecordType>(FieldType)) {
1108 const RecordDecl *RecDecl = RecType->getDecl();
1109 if (RecDecl->getDeclContext() == Owner &&
1110 !RecDecl->getIdentifier()) {
1111 if (Context.hasSameType(FieldType, AnonTy))
1112 break;
1113 ++Index;
1114 continue;
1115 }
1116 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001117 }
1118
1119 return Index;
1120}
1121
Douglas Gregor3996e242010-02-15 22:01:00 +00001122/// \brief Determine structural equivalence of two records.
1123static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1124 RecordDecl *D1, RecordDecl *D2) {
1125 if (D1->isUnion() != D2->isUnion()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001126 if (Context.Complain) {
1127 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1128 << Context.C2.getTypeDeclType(D2);
1129 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
1130 << D1->getDeclName() << (unsigned)D1->getTagKind();
1131 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001132 return false;
1133 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001134
1135 if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) {
1136 // If both anonymous structs/unions are in a record context, make sure
1137 // they occur in the same location in the context records.
Sean Callanan488f8612016-07-14 19:53:44 +00001138 if (Optional<unsigned> Index1 = findUntaggedStructOrUnionIndex(D1)) {
1139 if (Optional<unsigned> Index2 = findUntaggedStructOrUnionIndex(D2)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001140 if (*Index1 != *Index2)
1141 return false;
1142 }
1143 }
1144 }
1145
Douglas Gregore2e50d332010-12-01 01:36:18 +00001146 // If both declarations are class template specializations, we know
1147 // the ODR applies, so check the template and template arguments.
1148 ClassTemplateSpecializationDecl *Spec1
1149 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
1150 ClassTemplateSpecializationDecl *Spec2
1151 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
1152 if (Spec1 && Spec2) {
1153 // Check that the specialized templates are the same.
1154 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
1155 Spec2->getSpecializedTemplate()))
1156 return false;
1157
1158 // Check that the template arguments are the same.
1159 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
1160 return false;
1161
1162 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
1163 if (!IsStructurallyEquivalent(Context,
1164 Spec1->getTemplateArgs().get(I),
1165 Spec2->getTemplateArgs().get(I)))
1166 return false;
1167 }
1168 // If one is a class template specialization and the other is not, these
Chris Lattner57540c52011-04-15 05:22:18 +00001169 // structures are different.
Douglas Gregore2e50d332010-12-01 01:36:18 +00001170 else if (Spec1 || Spec2)
1171 return false;
1172
Douglas Gregorb4964f72010-02-15 23:54:17 +00001173 // Compare the definitions of these two records. If either or both are
1174 // incomplete, we assume that they are equivalent.
1175 D1 = D1->getDefinition();
1176 D2 = D2->getDefinition();
1177 if (!D1 || !D2)
1178 return true;
1179
Douglas Gregor3996e242010-02-15 22:01:00 +00001180 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
1181 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
1182 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
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.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
1187 << D2CXX->getNumBases();
1188 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
1189 << D1CXX->getNumBases();
1190 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001191 return false;
1192 }
1193
1194 // Check the base classes.
1195 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
1196 BaseEnd1 = D1CXX->bases_end(),
1197 Base2 = D2CXX->bases_begin();
1198 Base1 != BaseEnd1;
1199 ++Base1, ++Base2) {
1200 if (!IsStructurallyEquivalent(Context,
1201 Base1->getType(), Base2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001202 if (Context.Complain) {
1203 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1204 << Context.C2.getTypeDeclType(D2);
1205 Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
1206 << Base2->getType()
1207 << Base2->getSourceRange();
1208 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1209 << Base1->getType()
1210 << Base1->getSourceRange();
1211 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001212 return false;
1213 }
1214
1215 // Check virtual vs. non-virtual inheritance mismatch.
1216 if (Base1->isVirtual() != Base2->isVirtual()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001217 if (Context.Complain) {
1218 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1219 << Context.C2.getTypeDeclType(D2);
1220 Context.Diag2(Base2->getLocStart(),
1221 diag::note_odr_virtual_base)
1222 << Base2->isVirtual() << Base2->getSourceRange();
1223 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1224 << Base1->isVirtual()
1225 << Base1->getSourceRange();
1226 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001227 return false;
1228 }
1229 }
1230 } else if (D1CXX->getNumBases() > 0) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001231 if (Context.Complain) {
1232 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1233 << Context.C2.getTypeDeclType(D2);
1234 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
1235 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1236 << Base1->getType()
1237 << Base1->getSourceRange();
1238 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
1239 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001240 return false;
1241 }
1242 }
1243
1244 // Check the fields for consistency.
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001245 RecordDecl::field_iterator Field2 = D2->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001246 Field2End = D2->field_end();
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001247 for (RecordDecl::field_iterator Field1 = D1->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001248 Field1End = D1->field_end();
1249 Field1 != Field1End;
1250 ++Field1, ++Field2) {
1251 if (Field2 == Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001252 if (Context.Complain) {
1253 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1254 << Context.C2.getTypeDeclType(D2);
1255 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
1256 << Field1->getDeclName() << Field1->getType();
1257 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
1258 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001259 return false;
1260 }
1261
David Blaikie40ed2972012-06-06 20:45:41 +00001262 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001263 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001264 }
1265
1266 if (Field2 != Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001267 if (Context.Complain) {
1268 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1269 << Context.C2.getTypeDeclType(D2);
1270 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1271 << Field2->getDeclName() << Field2->getType();
1272 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1273 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001274 return false;
1275 }
1276
1277 return true;
1278}
1279
1280/// \brief Determine structural equivalence of two enums.
1281static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1282 EnumDecl *D1, EnumDecl *D2) {
1283 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1284 EC2End = D2->enumerator_end();
1285 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1286 EC1End = D1->enumerator_end();
1287 EC1 != EC1End; ++EC1, ++EC2) {
1288 if (EC2 == EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001289 if (Context.Complain) {
1290 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1291 << Context.C2.getTypeDeclType(D2);
1292 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1293 << EC1->getDeclName()
1294 << EC1->getInitVal().toString(10);
1295 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1296 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001297 return false;
1298 }
1299
1300 llvm::APSInt Val1 = EC1->getInitVal();
1301 llvm::APSInt Val2 = EC2->getInitVal();
Eric Christopher6dcc3762012-07-15 00:23:57 +00001302 if (!llvm::APSInt::isSameValue(Val1, Val2) ||
Douglas Gregor3996e242010-02-15 22:01:00 +00001303 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001304 if (Context.Complain) {
1305 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1306 << Context.C2.getTypeDeclType(D2);
1307 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1308 << EC2->getDeclName()
1309 << EC2->getInitVal().toString(10);
1310 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1311 << EC1->getDeclName()
1312 << EC1->getInitVal().toString(10);
1313 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001314 return false;
1315 }
1316 }
1317
1318 if (EC2 != EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001319 if (Context.Complain) {
1320 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1321 << Context.C2.getTypeDeclType(D2);
1322 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1323 << EC2->getDeclName()
1324 << EC2->getInitVal().toString(10);
1325 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1326 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001327 return false;
1328 }
1329
1330 return true;
1331}
Douglas Gregora082a492010-11-30 19:14:50 +00001332
1333static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1334 TemplateParameterList *Params1,
1335 TemplateParameterList *Params2) {
1336 if (Params1->size() != Params2->size()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001337 if (Context.Complain) {
1338 Context.Diag2(Params2->getTemplateLoc(),
1339 diag::err_odr_different_num_template_parameters)
1340 << Params1->size() << Params2->size();
1341 Context.Diag1(Params1->getTemplateLoc(),
1342 diag::note_odr_template_parameter_list);
1343 }
Douglas Gregora082a492010-11-30 19:14:50 +00001344 return false;
1345 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001346
Douglas Gregora082a492010-11-30 19:14:50 +00001347 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1348 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001349 if (Context.Complain) {
1350 Context.Diag2(Params2->getParam(I)->getLocation(),
1351 diag::err_odr_different_template_parameter_kind);
1352 Context.Diag1(Params1->getParam(I)->getLocation(),
1353 diag::note_odr_template_parameter_here);
1354 }
Douglas Gregora082a492010-11-30 19:14:50 +00001355 return false;
1356 }
1357
1358 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1359 Params2->getParam(I))) {
1360
1361 return false;
1362 }
1363 }
1364
1365 return true;
1366}
1367
1368static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1369 TemplateTypeParmDecl *D1,
1370 TemplateTypeParmDecl *D2) {
1371 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001372 if (Context.Complain) {
1373 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1374 << D2->isParameterPack();
1375 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1376 << D1->isParameterPack();
1377 }
Douglas Gregora082a492010-11-30 19:14:50 +00001378 return false;
1379 }
1380
1381 return true;
1382}
1383
1384static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1385 NonTypeTemplateParmDecl *D1,
1386 NonTypeTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001387 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001388 if (Context.Complain) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001389 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1390 << D2->isParameterPack();
1391 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1392 << D1->isParameterPack();
1393 }
Douglas Gregora082a492010-11-30 19:14:50 +00001394 return false;
1395 }
Douglas Gregora082a492010-11-30 19:14:50 +00001396
1397 // Check types.
1398 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001399 if (Context.Complain) {
1400 Context.Diag2(D2->getLocation(),
1401 diag::err_odr_non_type_parameter_type_inconsistent)
1402 << D2->getType() << D1->getType();
1403 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1404 << D1->getType();
1405 }
Douglas Gregora082a492010-11-30 19:14:50 +00001406 return false;
1407 }
1408
1409 return true;
1410}
1411
1412static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1413 TemplateTemplateParmDecl *D1,
1414 TemplateTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001415 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001416 if (Context.Complain) {
1417 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1418 << D2->isParameterPack();
1419 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1420 << D1->isParameterPack();
1421 }
Douglas Gregora082a492010-11-30 19:14:50 +00001422 return false;
1423 }
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001424
Douglas Gregora082a492010-11-30 19:14:50 +00001425 // Check template parameter lists.
1426 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1427 D2->getTemplateParameters());
1428}
1429
1430static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1431 ClassTemplateDecl *D1,
1432 ClassTemplateDecl *D2) {
1433 // Check template parameters.
1434 if (!IsStructurallyEquivalent(Context,
1435 D1->getTemplateParameters(),
1436 D2->getTemplateParameters()))
1437 return false;
1438
1439 // Check the templated declaration.
1440 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1441 D2->getTemplatedDecl());
1442}
1443
Douglas Gregor3996e242010-02-15 22:01:00 +00001444/// \brief Determine structural equivalence of two declarations.
1445static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1446 Decl *D1, Decl *D2) {
1447 // FIXME: Check for known structural equivalences via a callback of some sort.
1448
Douglas Gregorb4964f72010-02-15 23:54:17 +00001449 // Check whether we already know that these two declarations are not
1450 // structurally equivalent.
1451 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1452 D2->getCanonicalDecl())))
1453 return false;
1454
Douglas Gregor3996e242010-02-15 22:01:00 +00001455 // Determine whether we've already produced a tentative equivalence for D1.
1456 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1457 if (EquivToD1)
1458 return EquivToD1 == D2->getCanonicalDecl();
1459
1460 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1461 EquivToD1 = D2->getCanonicalDecl();
1462 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1463 return true;
1464}
1465
1466bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1467 Decl *D2) {
1468 if (!::IsStructurallyEquivalent(*this, D1, D2))
1469 return false;
1470
1471 return !Finish();
1472}
1473
1474bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1475 QualType T2) {
1476 if (!::IsStructurallyEquivalent(*this, T1, T2))
1477 return false;
1478
1479 return !Finish();
1480}
1481
1482bool StructuralEquivalenceContext::Finish() {
1483 while (!DeclsToCheck.empty()) {
1484 // Check the next declaration.
1485 Decl *D1 = DeclsToCheck.front();
1486 DeclsToCheck.pop_front();
1487
1488 Decl *D2 = TentativeEquivalences[D1];
1489 assert(D2 && "Unrecorded tentative equivalence?");
1490
Douglas Gregorb4964f72010-02-15 23:54:17 +00001491 bool Equivalent = true;
1492
Douglas Gregor3996e242010-02-15 22:01:00 +00001493 // FIXME: Switch on all declaration kinds. For now, we're just going to
1494 // check the obvious ones.
1495 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1496 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1497 // Check for equivalent structure names.
1498 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001499 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1500 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001501 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001502 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1503 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001504 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1505 !::IsStructurallyEquivalent(*this, Record1, Record2))
1506 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001507 } else {
1508 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001509 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001510 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001511 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001512 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1513 // Check for equivalent enum names.
1514 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001515 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1516 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001517 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001518 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1519 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001520 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1521 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1522 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001523 } else {
1524 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001525 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001526 }
Richard Smithdda56e42011-04-15 14:24:37 +00001527 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1528 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001529 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001530 Typedef2->getIdentifier()) ||
1531 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001532 Typedef1->getUnderlyingType(),
1533 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001534 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001535 } else {
1536 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001537 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001538 }
Douglas Gregora082a492010-11-30 19:14:50 +00001539 } else if (ClassTemplateDecl *ClassTemplate1
1540 = dyn_cast<ClassTemplateDecl>(D1)) {
1541 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1542 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1543 ClassTemplate2->getIdentifier()) ||
1544 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1545 Equivalent = false;
1546 } else {
1547 // Class template/non-class-template mismatch.
1548 Equivalent = false;
1549 }
1550 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1551 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1552 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1553 Equivalent = false;
1554 } else {
1555 // Kind mismatch.
1556 Equivalent = false;
1557 }
1558 } else if (NonTypeTemplateParmDecl *NTTP1
1559 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1560 if (NonTypeTemplateParmDecl *NTTP2
1561 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1562 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1563 Equivalent = false;
1564 } else {
1565 // Kind mismatch.
1566 Equivalent = false;
1567 }
1568 } else if (TemplateTemplateParmDecl *TTP1
1569 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1570 if (TemplateTemplateParmDecl *TTP2
1571 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1572 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1573 Equivalent = false;
1574 } else {
1575 // Kind mismatch.
1576 Equivalent = false;
1577 }
1578 }
1579
Douglas Gregorb4964f72010-02-15 23:54:17 +00001580 if (!Equivalent) {
1581 // Note that these two declarations are not equivalent (and we already
1582 // know about it).
1583 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1584 D2->getCanonicalDecl()));
1585 return true;
1586 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001587 // FIXME: Check other declaration kinds!
1588 }
1589
1590 return false;
1591}
1592
1593//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001594// Import Types
1595//----------------------------------------------------------------------------
1596
John McCall424cec92011-01-19 06:33:43 +00001597QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +00001598 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1599 << T->getTypeClassName();
1600 return QualType();
1601}
1602
John McCall424cec92011-01-19 06:33:43 +00001603QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001604 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +00001605#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
1606 case BuiltinType::Id: \
1607 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +00001608#include "clang/Basic/OpenCLImageTypes.def"
John McCalle314e272011-10-18 21:02:43 +00001609#define SHARED_SINGLETON_TYPE(Expansion)
1610#define BUILTIN_TYPE(Id, SingletonId) \
1611 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1612#include "clang/AST/BuiltinTypes.def"
1613
1614 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1615 // context supports C++.
1616
1617 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1618 // context supports ObjC.
1619
Douglas Gregor96e578d2010-02-05 17:54:41 +00001620 case BuiltinType::Char_U:
1621 // The context we're importing from has an unsigned 'char'. If we're
1622 // importing into a context with a signed 'char', translate to
1623 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001624 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001625 return Importer.getToContext().UnsignedCharTy;
1626
1627 return Importer.getToContext().CharTy;
1628
Douglas Gregor96e578d2010-02-05 17:54:41 +00001629 case BuiltinType::Char_S:
1630 // The context we're importing from has an unsigned 'char'. If we're
1631 // importing into a context with a signed 'char', translate to
1632 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001633 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001634 return Importer.getToContext().SignedCharTy;
1635
1636 return Importer.getToContext().CharTy;
1637
Chris Lattnerad3467e2010-12-25 23:25:43 +00001638 case BuiltinType::WChar_S:
1639 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001640 // FIXME: If not in C++, shall we translate to the C equivalent of
1641 // wchar_t?
1642 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001643 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001644
1645 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001646}
1647
Aleksei Sidorina693b372016-09-28 10:16:56 +00001648QualType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
1649 QualType OrigT = Importer.Import(T->getOriginalType());
1650 if (OrigT.isNull())
1651 return QualType();
1652
1653 return Importer.getToContext().getDecayedType(OrigT);
1654}
1655
John McCall424cec92011-01-19 06:33:43 +00001656QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001657 QualType ToElementType = Importer.Import(T->getElementType());
1658 if (ToElementType.isNull())
1659 return QualType();
1660
1661 return Importer.getToContext().getComplexType(ToElementType);
1662}
1663
John McCall424cec92011-01-19 06:33:43 +00001664QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001665 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1666 if (ToPointeeType.isNull())
1667 return QualType();
1668
1669 return Importer.getToContext().getPointerType(ToPointeeType);
1670}
1671
John McCall424cec92011-01-19 06:33:43 +00001672QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001673 // FIXME: Check for blocks support in "to" context.
1674 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1675 if (ToPointeeType.isNull())
1676 return QualType();
1677
1678 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1679}
1680
John McCall424cec92011-01-19 06:33:43 +00001681QualType
1682ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001683 // FIXME: Check for C++ support in "to" context.
1684 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1685 if (ToPointeeType.isNull())
1686 return QualType();
1687
1688 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1689}
1690
John McCall424cec92011-01-19 06:33:43 +00001691QualType
1692ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001693 // FIXME: Check for C++0x support in "to" context.
1694 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1695 if (ToPointeeType.isNull())
1696 return QualType();
1697
1698 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1699}
1700
John McCall424cec92011-01-19 06:33:43 +00001701QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001702 // FIXME: Check for C++ support in "to" context.
1703 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1704 if (ToPointeeType.isNull())
1705 return QualType();
1706
1707 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1708 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1709 ClassType.getTypePtr());
1710}
1711
John McCall424cec92011-01-19 06:33:43 +00001712QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001713 QualType ToElementType = Importer.Import(T->getElementType());
1714 if (ToElementType.isNull())
1715 return QualType();
1716
1717 return Importer.getToContext().getConstantArrayType(ToElementType,
1718 T->getSize(),
1719 T->getSizeModifier(),
1720 T->getIndexTypeCVRQualifiers());
1721}
1722
John McCall424cec92011-01-19 06:33:43 +00001723QualType
1724ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001725 QualType ToElementType = Importer.Import(T->getElementType());
1726 if (ToElementType.isNull())
1727 return QualType();
1728
1729 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1730 T->getSizeModifier(),
1731 T->getIndexTypeCVRQualifiers());
1732}
1733
John McCall424cec92011-01-19 06:33:43 +00001734QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001735 QualType ToElementType = Importer.Import(T->getElementType());
1736 if (ToElementType.isNull())
1737 return QualType();
1738
1739 Expr *Size = Importer.Import(T->getSizeExpr());
1740 if (!Size)
1741 return QualType();
1742
1743 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1744 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1745 T->getSizeModifier(),
1746 T->getIndexTypeCVRQualifiers(),
1747 Brackets);
1748}
1749
John McCall424cec92011-01-19 06:33:43 +00001750QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001751 QualType ToElementType = Importer.Import(T->getElementType());
1752 if (ToElementType.isNull())
1753 return QualType();
1754
1755 return Importer.getToContext().getVectorType(ToElementType,
1756 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001757 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001758}
1759
John McCall424cec92011-01-19 06:33:43 +00001760QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001761 QualType ToElementType = Importer.Import(T->getElementType());
1762 if (ToElementType.isNull())
1763 return QualType();
1764
1765 return Importer.getToContext().getExtVectorType(ToElementType,
1766 T->getNumElements());
1767}
1768
John McCall424cec92011-01-19 06:33:43 +00001769QualType
1770ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001771 // FIXME: What happens if we're importing a function without a prototype
1772 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +00001773 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001774 if (ToResultType.isNull())
1775 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001776
Douglas Gregor96e578d2010-02-05 17:54:41 +00001777 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001778 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001779}
1780
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001781QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +00001782 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001783 if (ToResultType.isNull())
1784 return QualType();
1785
1786 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001787 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00001788 for (const auto &A : T->param_types()) {
1789 QualType ArgType = Importer.Import(A);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001790 if (ArgType.isNull())
1791 return QualType();
1792 ArgTypes.push_back(ArgType);
1793 }
1794
1795 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001796 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +00001797 for (const auto &E : T->exceptions()) {
1798 QualType ExceptionType = Importer.Import(E);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001799 if (ExceptionType.isNull())
1800 return QualType();
1801 ExceptionTypes.push_back(ExceptionType);
1802 }
John McCalldb40c7f2010-12-14 08:05:40 +00001803
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001804 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1805 FunctionProtoType::ExtProtoInfo ToEPI;
1806
1807 ToEPI.ExtInfo = FromEPI.ExtInfo;
1808 ToEPI.Variadic = FromEPI.Variadic;
1809 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1810 ToEPI.TypeQuals = FromEPI.TypeQuals;
1811 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +00001812 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1813 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
1814 ToEPI.ExceptionSpec.NoexceptExpr =
1815 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
1816 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
1817 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
1818 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
1819 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001820
Jordan Rose5c382722013-03-08 21:51:21 +00001821 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001822}
1823
Sean Callananda6df8a2011-08-11 16:56:07 +00001824QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1825 QualType ToInnerType = Importer.Import(T->getInnerType());
1826 if (ToInnerType.isNull())
1827 return QualType();
1828
1829 return Importer.getToContext().getParenType(ToInnerType);
1830}
1831
John McCall424cec92011-01-19 06:33:43 +00001832QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +00001833 TypedefNameDecl *ToDecl
1834 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001835 if (!ToDecl)
1836 return QualType();
1837
1838 return Importer.getToContext().getTypeDeclType(ToDecl);
1839}
1840
John McCall424cec92011-01-19 06:33:43 +00001841QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001842 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1843 if (!ToExpr)
1844 return QualType();
1845
1846 return Importer.getToContext().getTypeOfExprType(ToExpr);
1847}
1848
John McCall424cec92011-01-19 06:33:43 +00001849QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001850 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1851 if (ToUnderlyingType.isNull())
1852 return QualType();
1853
1854 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1855}
1856
John McCall424cec92011-01-19 06:33:43 +00001857QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001858 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +00001859 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1860 if (!ToExpr)
1861 return QualType();
1862
Douglas Gregor81495f32012-02-12 18:42:33 +00001863 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1864 if (UnderlyingType.isNull())
1865 return QualType();
1866
1867 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001868}
1869
Alexis Hunte852b102011-05-24 22:41:36 +00001870QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1871 QualType ToBaseType = Importer.Import(T->getBaseType());
1872 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1873 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1874 return QualType();
1875
1876 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1877 ToUnderlyingType,
1878 T->getUTTKind());
1879}
1880
Richard Smith30482bc2011-02-20 03:19:35 +00001881QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00001882 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +00001883 QualType FromDeduced = T->getDeducedType();
1884 QualType ToDeduced;
1885 if (!FromDeduced.isNull()) {
1886 ToDeduced = Importer.Import(FromDeduced);
1887 if (ToDeduced.isNull())
1888 return QualType();
1889 }
1890
Richard Smithe301ba22015-11-11 02:02:15 +00001891 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +00001892 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +00001893}
1894
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001895QualType ASTNodeImporter::VisitInjectedClassNameType(
1896 const InjectedClassNameType *T) {
1897 CXXRecordDecl *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl()));
1898 if (!D)
1899 return QualType();
1900
1901 QualType InjType = Importer.Import(T->getInjectedSpecializationType());
1902 if (InjType.isNull())
1903 return QualType();
1904
1905 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
1906 // See comments in InjectedClassNameType definition for details
1907 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
1908 enum {
1909 TypeAlignmentInBits = 4,
1910 TypeAlignment = 1 << TypeAlignmentInBits
1911 };
1912
1913 return QualType(new (Importer.getToContext(), TypeAlignment)
1914 InjectedClassNameType(D, InjType), 0);
1915}
1916
John McCall424cec92011-01-19 06:33:43 +00001917QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001918 RecordDecl *ToDecl
1919 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1920 if (!ToDecl)
1921 return QualType();
1922
1923 return Importer.getToContext().getTagDeclType(ToDecl);
1924}
1925
John McCall424cec92011-01-19 06:33:43 +00001926QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001927 EnumDecl *ToDecl
1928 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1929 if (!ToDecl)
1930 return QualType();
1931
1932 return Importer.getToContext().getTagDeclType(ToDecl);
1933}
1934
Sean Callanan72fe0852015-04-02 23:50:08 +00001935QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
1936 QualType FromModifiedType = T->getModifiedType();
1937 QualType FromEquivalentType = T->getEquivalentType();
1938 QualType ToModifiedType;
1939 QualType ToEquivalentType;
1940
1941 if (!FromModifiedType.isNull()) {
1942 ToModifiedType = Importer.Import(FromModifiedType);
1943 if (ToModifiedType.isNull())
1944 return QualType();
1945 }
1946 if (!FromEquivalentType.isNull()) {
1947 ToEquivalentType = Importer.Import(FromEquivalentType);
1948 if (ToEquivalentType.isNull())
1949 return QualType();
1950 }
1951
1952 return Importer.getToContext().getAttributedType(T->getAttrKind(),
1953 ToModifiedType, ToEquivalentType);
1954}
1955
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001956
1957QualType ASTNodeImporter::VisitTemplateTypeParmType(
1958 const TemplateTypeParmType *T) {
1959 TemplateTypeParmDecl *ParmDecl =
1960 cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl()));
1961 if (!ParmDecl && T->getDecl())
1962 return QualType();
1963
1964 return Importer.getToContext().getTemplateTypeParmType(
1965 T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl);
1966}
1967
Douglas Gregore2e50d332010-12-01 01:36:18 +00001968QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001969 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001970 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1971 if (ToTemplate.isNull())
1972 return QualType();
1973
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001974 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001975 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1976 return QualType();
1977
1978 QualType ToCanonType;
1979 if (!QualType(T, 0).isCanonical()) {
1980 QualType FromCanonType
1981 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1982 ToCanonType =Importer.Import(FromCanonType);
1983 if (ToCanonType.isNull())
1984 return QualType();
1985 }
1986 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
David Majnemer6fbeee32016-07-07 04:43:07 +00001987 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +00001988 ToCanonType);
1989}
1990
John McCall424cec92011-01-19 06:33:43 +00001991QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Craig Topper36250ad2014-05-12 05:36:57 +00001992 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001993 // Note: the qualifier in an ElaboratedType is optional.
1994 if (T->getQualifier()) {
1995 ToQualifier = Importer.Import(T->getQualifier());
1996 if (!ToQualifier)
1997 return QualType();
1998 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001999
2000 QualType ToNamedType = Importer.Import(T->getNamedType());
2001 if (ToNamedType.isNull())
2002 return QualType();
2003
Abramo Bagnara6150c882010-05-11 21:36:43 +00002004 return Importer.getToContext().getElaboratedType(T->getKeyword(),
2005 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00002006}
2007
John McCall424cec92011-01-19 06:33:43 +00002008QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00002009 ObjCInterfaceDecl *Class
2010 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
2011 if (!Class)
2012 return QualType();
2013
John McCall8b07ec22010-05-15 11:32:37 +00002014 return Importer.getToContext().getObjCInterfaceType(Class);
2015}
2016
John McCall424cec92011-01-19 06:33:43 +00002017QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00002018 QualType ToBaseType = Importer.Import(T->getBaseType());
2019 if (ToBaseType.isNull())
2020 return QualType();
2021
Douglas Gregore9d95f12015-07-07 03:57:35 +00002022 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00002023 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00002024 QualType ImportedTypeArg = Importer.Import(TypeArg);
2025 if (ImportedTypeArg.isNull())
2026 return QualType();
2027
2028 TypeArgs.push_back(ImportedTypeArg);
2029 }
2030
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002031 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00002032 for (auto *P : T->quals()) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00002033 ObjCProtocolDecl *Protocol
Aaron Ballman1683f7b2014-03-17 15:55:30 +00002034 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +00002035 if (!Protocol)
2036 return QualType();
2037 Protocols.push_back(Protocol);
2038 }
2039
Douglas Gregore9d95f12015-07-07 03:57:35 +00002040 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00002041 Protocols,
2042 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00002043}
2044
John McCall424cec92011-01-19 06:33:43 +00002045QualType
2046ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00002047 QualType ToPointeeType = Importer.Import(T->getPointeeType());
2048 if (ToPointeeType.isNull())
2049 return QualType();
2050
John McCall8b07ec22010-05-15 11:32:37 +00002051 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00002052}
2053
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002054//----------------------------------------------------------------------------
2055// Import Declarations
2056//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002057bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
2058 DeclContext *&LexicalDC,
2059 DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +00002060 NamedDecl *&ToD,
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002061 SourceLocation &Loc) {
2062 // Import the context of this declaration.
2063 DC = Importer.ImportContext(D->getDeclContext());
2064 if (!DC)
2065 return true;
2066
2067 LexicalDC = DC;
2068 if (D->getDeclContext() != D->getLexicalDeclContext()) {
2069 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
2070 if (!LexicalDC)
2071 return true;
2072 }
2073
2074 // Import the name of this declaration.
2075 Name = Importer.Import(D->getDeclName());
2076 if (D->getDeclName() && !Name)
2077 return true;
2078
2079 // Import the location of this declaration.
2080 Loc = Importer.Import(D->getLocation());
Sean Callanan59721b32015-04-28 18:41:46 +00002081 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002082 return false;
2083}
2084
Douglas Gregord451ea92011-07-29 23:31:30 +00002085void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
2086 if (!FromD)
2087 return;
2088
2089 if (!ToD) {
2090 ToD = Importer.Import(FromD);
2091 if (!ToD)
2092 return;
2093 }
2094
2095 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
2096 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +00002097 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +00002098 ImportDefinition(FromRecord, ToRecord);
2099 }
2100 }
2101 return;
2102 }
2103
2104 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
2105 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
2106 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
2107 ImportDefinition(FromEnum, ToEnum);
2108 }
2109 }
2110 return;
2111 }
2112}
2113
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002114void
2115ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
2116 DeclarationNameInfo& To) {
2117 // NOTE: To.Name and To.Loc are already imported.
2118 // We only have to import To.LocInfo.
2119 switch (To.getName().getNameKind()) {
2120 case DeclarationName::Identifier:
2121 case DeclarationName::ObjCZeroArgSelector:
2122 case DeclarationName::ObjCOneArgSelector:
2123 case DeclarationName::ObjCMultiArgSelector:
2124 case DeclarationName::CXXUsingDirective:
2125 return;
2126
2127 case DeclarationName::CXXOperatorName: {
2128 SourceRange Range = From.getCXXOperatorNameRange();
2129 To.setCXXOperatorNameRange(Importer.Import(Range));
2130 return;
2131 }
2132 case DeclarationName::CXXLiteralOperatorName: {
2133 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
2134 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
2135 return;
2136 }
2137 case DeclarationName::CXXConstructorName:
2138 case DeclarationName::CXXDestructorName:
2139 case DeclarationName::CXXConversionFunctionName: {
2140 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
2141 To.setNamedTypeInfo(Importer.Import(FromTInfo));
2142 return;
2143 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002144 }
Douglas Gregor07216d12011-11-02 20:52:01 +00002145 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002146}
2147
Douglas Gregor2e15c842012-02-01 21:00:38 +00002148void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00002149 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00002150 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00002151 return;
2152 }
2153
Aaron Ballman629afae2014-03-07 19:56:05 +00002154 for (auto *From : FromDC->decls())
2155 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +00002156}
2157
Douglas Gregord451ea92011-07-29 23:31:30 +00002158bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00002159 ImportDefinitionKind Kind) {
2160 if (To->getDefinition() || To->isBeingDefined()) {
2161 if (Kind == IDK_Everything)
2162 ImportDeclContext(From, /*ForceImport=*/true);
2163
Douglas Gregore2e50d332010-12-01 01:36:18 +00002164 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00002165 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00002166
2167 To->startDefinition();
2168
2169 // Add base classes.
2170 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
2171 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002172
2173 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
2174 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
2175 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00002176 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002177 ToData.Aggregate = FromData.Aggregate;
2178 ToData.PlainOldData = FromData.PlainOldData;
2179 ToData.Empty = FromData.Empty;
2180 ToData.Polymorphic = FromData.Polymorphic;
2181 ToData.Abstract = FromData.Abstract;
2182 ToData.IsStandardLayout = FromData.IsStandardLayout;
2183 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
2184 ToData.HasPrivateFields = FromData.HasPrivateFields;
2185 ToData.HasProtectedFields = FromData.HasProtectedFields;
2186 ToData.HasPublicFields = FromData.HasPublicFields;
2187 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00002188 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00002189 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00002190 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00002191 ToData.HasUninitializedReferenceMember
2192 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00002193 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00002194 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
2195 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +00002196 ToData.NeedOverloadResolutionForMoveConstructor
2197 = FromData.NeedOverloadResolutionForMoveConstructor;
2198 ToData.NeedOverloadResolutionForMoveAssignment
2199 = FromData.NeedOverloadResolutionForMoveAssignment;
2200 ToData.NeedOverloadResolutionForDestructor
2201 = FromData.NeedOverloadResolutionForDestructor;
2202 ToData.DefaultedMoveConstructorIsDeleted
2203 = FromData.DefaultedMoveConstructorIsDeleted;
2204 ToData.DefaultedMoveAssignmentIsDeleted
2205 = FromData.DefaultedMoveAssignmentIsDeleted;
2206 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00002207 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
2208 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002209 ToData.HasConstexprNonCopyMoveConstructor
2210 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00002211 ToData.HasDefaultedDefaultConstructor
2212 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00002213 ToData.DefaultedDefaultConstructorIsConstexpr
2214 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00002215 ToData.HasConstexprDefaultConstructor
2216 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002217 ToData.HasNonLiteralTypeFieldsOrBases
2218 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00002219 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002220 ToData.UserProvidedDefaultConstructor
2221 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00002222 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smith1c33fe82012-11-28 06:23:12 +00002223 ToData.ImplicitCopyConstructorHasConstParam
2224 = FromData.ImplicitCopyConstructorHasConstParam;
2225 ToData.ImplicitCopyAssignmentHasConstParam
2226 = FromData.ImplicitCopyAssignmentHasConstParam;
2227 ToData.HasDeclaredCopyConstructorWithConstParam
2228 = FromData.HasDeclaredCopyConstructorWithConstParam;
2229 ToData.HasDeclaredCopyAssignmentWithConstParam
2230 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00002231 ToData.IsLambda = FromData.IsLambda;
2232
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002233 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00002234 for (const auto &Base1 : FromCXX->bases()) {
2235 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +00002236 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00002237 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00002238
2239 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00002240 if (Base1.isPackExpansion())
2241 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00002242
2243 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00002244 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Douglas Gregord451ea92011-07-29 23:31:30 +00002245
Douglas Gregore2e50d332010-12-01 01:36:18 +00002246 Bases.push_back(
2247 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00002248 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
2249 Base1.isVirtual(),
2250 Base1.isBaseOfClass(),
2251 Base1.getAccessSpecifierAsWritten(),
2252 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00002253 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00002254 }
2255 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00002256 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00002257 }
2258
Douglas Gregor2e15c842012-02-01 21:00:38 +00002259 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00002260 ImportDeclContext(From, /*ForceImport=*/true);
2261
Douglas Gregore2e50d332010-12-01 01:36:18 +00002262 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00002263 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002264}
2265
Larisse Voufo39a1e502013-08-06 01:03:05 +00002266bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
2267 ImportDefinitionKind Kind) {
Sean Callanan59721b32015-04-28 18:41:46 +00002268 if (To->getAnyInitializer())
Larisse Voufo39a1e502013-08-06 01:03:05 +00002269 return false;
2270
2271 // FIXME: Can we really import any initializer? Alternatively, we could force
2272 // ourselves to import every declaration of a variable and then only use
2273 // getInit() here.
2274 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
2275
2276 // FIXME: Other bits to merge?
2277
2278 return false;
2279}
2280
Douglas Gregord451ea92011-07-29 23:31:30 +00002281bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00002282 ImportDefinitionKind Kind) {
2283 if (To->getDefinition() || To->isBeingDefined()) {
2284 if (Kind == IDK_Everything)
2285 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002286 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002287 }
Douglas Gregord451ea92011-07-29 23:31:30 +00002288
2289 To->startDefinition();
2290
2291 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
2292 if (T.isNull())
2293 return true;
2294
2295 QualType ToPromotionType = Importer.Import(From->getPromotionType());
2296 if (ToPromotionType.isNull())
2297 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002298
2299 if (shouldForceImportDeclContext(Kind))
2300 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002301
2302 // FIXME: we might need to merge the number of positive or negative bits
2303 // if the enumerator lists don't match.
2304 To->completeDefinition(T, ToPromotionType,
2305 From->getNumPositiveBits(),
2306 From->getNumNegativeBits());
2307 return false;
2308}
2309
Douglas Gregora082a492010-11-30 19:14:50 +00002310TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
2311 TemplateParameterList *Params) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00002312 SmallVector<NamedDecl *, 4> ToParams(Params->size());
2313 if (ImportContainerChecked(*Params, ToParams))
2314 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00002315
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00002316 Expr *ToRequiresClause;
2317 if (Expr *const R = Params->getRequiresClause()) {
2318 ToRequiresClause = Importer.Import(R);
2319 if (!ToRequiresClause)
2320 return nullptr;
2321 } else {
2322 ToRequiresClause = nullptr;
2323 }
2324
Douglas Gregora082a492010-11-30 19:14:50 +00002325 return TemplateParameterList::Create(Importer.getToContext(),
2326 Importer.Import(Params->getTemplateLoc()),
2327 Importer.Import(Params->getLAngleLoc()),
David Majnemer902f8c62015-12-27 07:16:27 +00002328 ToParams,
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00002329 Importer.Import(Params->getRAngleLoc()),
2330 ToRequiresClause);
Douglas Gregora082a492010-11-30 19:14:50 +00002331}
2332
Douglas Gregore2e50d332010-12-01 01:36:18 +00002333TemplateArgument
2334ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
2335 switch (From.getKind()) {
2336 case TemplateArgument::Null:
2337 return TemplateArgument();
2338
2339 case TemplateArgument::Type: {
2340 QualType ToType = Importer.Import(From.getAsType());
2341 if (ToType.isNull())
2342 return TemplateArgument();
2343 return TemplateArgument(ToType);
2344 }
2345
2346 case TemplateArgument::Integral: {
2347 QualType ToType = Importer.Import(From.getIntegralType());
2348 if (ToType.isNull())
2349 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00002350 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00002351 }
2352
Eli Friedmanb826a002012-09-26 02:36:12 +00002353 case TemplateArgument::Declaration: {
David Blaikie3c7dd6b2014-10-22 19:54:16 +00002354 ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
2355 QualType ToType = Importer.Import(From.getParamTypeForDecl());
2356 if (!To || ToType.isNull())
2357 return TemplateArgument();
2358 return TemplateArgument(To, ToType);
Eli Friedmanb826a002012-09-26 02:36:12 +00002359 }
2360
2361 case TemplateArgument::NullPtr: {
2362 QualType ToType = Importer.Import(From.getNullPtrType());
2363 if (ToType.isNull())
2364 return TemplateArgument();
2365 return TemplateArgument(ToType, /*isNullPtr*/true);
2366 }
2367
Douglas Gregore2e50d332010-12-01 01:36:18 +00002368 case TemplateArgument::Template: {
2369 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
2370 if (ToTemplate.isNull())
2371 return TemplateArgument();
2372
2373 return TemplateArgument(ToTemplate);
2374 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002375
2376 case TemplateArgument::TemplateExpansion: {
2377 TemplateName ToTemplate
2378 = Importer.Import(From.getAsTemplateOrTemplatePattern());
2379 if (ToTemplate.isNull())
2380 return TemplateArgument();
2381
Douglas Gregore1d60df2011-01-14 23:41:42 +00002382 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002383 }
2384
Douglas Gregore2e50d332010-12-01 01:36:18 +00002385 case TemplateArgument::Expression:
2386 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2387 return TemplateArgument(ToExpr);
2388 return TemplateArgument();
2389
2390 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002391 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002392 ToPack.reserve(From.pack_size());
2393 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2394 return TemplateArgument();
Benjamin Kramercce63472015-08-05 09:40:22 +00002395
2396 return TemplateArgument(
2397 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00002398 }
2399 }
2400
2401 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00002402}
2403
Aleksei Sidorina693b372016-09-28 10:16:56 +00002404TemplateArgumentLoc ASTNodeImporter::ImportTemplateArgumentLoc(
2405 const TemplateArgumentLoc &TALoc, bool &Error) {
2406 Error = false;
2407 TemplateArgument Arg = ImportTemplateArgument(TALoc.getArgument());
2408 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
2409 TemplateArgumentLocInfo ToInfo;
2410 if (Arg.getKind() == TemplateArgument::Expression) {
2411 Expr *E = Importer.Import(FromInfo.getAsExpr());
2412 ToInfo = TemplateArgumentLocInfo(E);
2413 if (!E)
2414 Error = true;
2415 } else if (Arg.getKind() == TemplateArgument::Type) {
2416 if (TypeSourceInfo *TSI = Importer.Import(FromInfo.getAsTypeSourceInfo()))
2417 ToInfo = TemplateArgumentLocInfo(TSI);
2418 else
2419 Error = true;
2420 } else {
2421 ToInfo = TemplateArgumentLocInfo(
2422 Importer.Import(FromInfo.getTemplateQualifierLoc()),
2423 Importer.Import(FromInfo.getTemplateNameLoc()),
2424 Importer.Import(FromInfo.getTemplateEllipsisLoc()));
2425 }
2426 return TemplateArgumentLoc(Arg, ToInfo);
2427}
2428
Douglas Gregore2e50d332010-12-01 01:36:18 +00002429bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2430 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002431 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00002432 for (unsigned I = 0; I != NumFromArgs; ++I) {
2433 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2434 if (To.isNull() && !FromArgs[I].isNull())
2435 return true;
2436
2437 ToArgs.push_back(To);
2438 }
2439
2440 return false;
2441}
2442
Douglas Gregor5c73e912010-02-11 00:48:18 +00002443bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002444 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00002445 // Eliminate a potential failure point where we attempt to re-import
2446 // something we're trying to import while completing ToRecord.
2447 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
2448 if (ToOrigin) {
2449 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
2450 if (ToOriginRecord)
2451 ToRecord = ToOriginRecord;
2452 }
2453
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002454 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00002455 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002456 Importer.getNonEquivalentDecls(),
2457 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002458 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002459}
2460
Larisse Voufo39a1e502013-08-06 01:03:05 +00002461bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
2462 bool Complain) {
2463 StructuralEquivalenceContext Ctx(
2464 Importer.getFromContext(), Importer.getToContext(),
2465 Importer.getNonEquivalentDecls(), false, Complain);
2466 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
2467}
2468
Douglas Gregor98c10182010-02-12 22:17:39 +00002469bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002470 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002471 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002472 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002473 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002474}
2475
Douglas Gregor91155082012-11-14 22:29:20 +00002476bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
2477 EnumConstantDecl *ToEC)
2478{
2479 const llvm::APSInt &FromVal = FromEC->getInitVal();
2480 const llvm::APSInt &ToVal = ToEC->getInitVal();
2481
2482 return FromVal.isSigned() == ToVal.isSigned() &&
2483 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2484 FromVal == ToVal;
2485}
2486
2487bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002488 ClassTemplateDecl *To) {
2489 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2490 Importer.getToContext(),
2491 Importer.getNonEquivalentDecls());
2492 return Ctx.IsStructurallyEquivalent(From, To);
2493}
2494
Larisse Voufo39a1e502013-08-06 01:03:05 +00002495bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2496 VarTemplateDecl *To) {
2497 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2498 Importer.getToContext(),
2499 Importer.getNonEquivalentDecls());
2500 return Ctx.IsStructurallyEquivalent(From, To);
2501}
2502
Douglas Gregore4c83e42010-02-09 22:48:33 +00002503Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002504 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002505 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00002506 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00002507}
2508
Sean Callanan65198272011-11-17 23:20:56 +00002509Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2510 TranslationUnitDecl *ToD =
2511 Importer.getToContext().getTranslationUnitDecl();
2512
2513 Importer.Imported(D, ToD);
2514
2515 return ToD;
2516}
2517
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002518Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
2519
2520 SourceLocation Loc = Importer.Import(D->getLocation());
2521 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
2522
2523 // Import the context of this declaration.
2524 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2525 if (!DC)
2526 return nullptr;
2527
2528 AccessSpecDecl *accessSpecDecl
2529 = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(),
2530 DC, Loc, ColonLoc);
2531
2532 if (!accessSpecDecl)
2533 return nullptr;
2534
2535 // Lexical DeclContext and Semantic DeclContext
2536 // is always the same for the accessSpec.
2537 accessSpecDecl->setLexicalDeclContext(DC);
2538 DC->addDeclInternal(accessSpecDecl);
2539
2540 return accessSpecDecl;
2541}
2542
Aleksei Sidorina693b372016-09-28 10:16:56 +00002543Decl *ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
2544 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2545 if (!DC)
2546 return nullptr;
2547
2548 DeclContext *LexicalDC = DC;
2549
2550 // Import the location of this declaration.
2551 SourceLocation Loc = Importer.Import(D->getLocation());
2552
2553 Expr *AssertExpr = Importer.Import(D->getAssertExpr());
2554 if (!AssertExpr)
2555 return nullptr;
2556
2557 StringLiteral *FromMsg = D->getMessage();
2558 StringLiteral *ToMsg = cast_or_null<StringLiteral>(Importer.Import(FromMsg));
2559 if (!ToMsg && FromMsg)
2560 return nullptr;
2561
2562 StaticAssertDecl *ToD = StaticAssertDecl::Create(
2563 Importer.getToContext(), DC, Loc, AssertExpr, ToMsg,
2564 Importer.Import(D->getRParenLoc()), D->isFailed());
2565
2566 ToD->setLexicalDeclContext(LexicalDC);
2567 LexicalDC->addDeclInternal(ToD);
2568 Importer.Imported(D, ToD);
2569 return ToD;
2570}
2571
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002572Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2573 // Import the major distinguishing characteristics of this namespace.
2574 DeclContext *DC, *LexicalDC;
2575 DeclarationName Name;
2576 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002577 NamedDecl *ToD;
2578 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002579 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002580 if (ToD)
2581 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002582
2583 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002584 if (!Name) {
2585 // This is an anonymous namespace. Adopt an existing anonymous
2586 // namespace if we can.
2587 // FIXME: Not testable.
2588 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2589 MergeWithNamespace = TU->getAnonymousNamespace();
2590 else
2591 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2592 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002593 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002594 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002595 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002596 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2597 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002598 continue;
2599
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002600 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002601 MergeWithNamespace = FoundNS;
2602 ConflictingDecls.clear();
2603 break;
2604 }
2605
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002606 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002607 }
2608
2609 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002610 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002611 ConflictingDecls.data(),
2612 ConflictingDecls.size());
2613 }
2614 }
2615
2616 // Create the "to" namespace, if needed.
2617 NamespaceDecl *ToNamespace = MergeWithNamespace;
2618 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002619 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002620 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002621 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00002622 Loc, Name.getAsIdentifierInfo(),
Craig Topper36250ad2014-05-12 05:36:57 +00002623 /*PrevDecl=*/nullptr);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002624 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002625 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002626
2627 // If this is an anonymous namespace, register it as the anonymous
2628 // namespace within its context.
2629 if (!Name) {
2630 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2631 TU->setAnonymousNamespace(ToNamespace);
2632 else
2633 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2634 }
2635 }
2636 Importer.Imported(D, ToNamespace);
2637
2638 ImportDeclContext(D);
2639
2640 return ToNamespace;
2641}
2642
Richard Smithdda56e42011-04-15 14:24:37 +00002643Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002644 // Import the major distinguishing characteristics of this typedef.
2645 DeclContext *DC, *LexicalDC;
2646 DeclarationName Name;
2647 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002648 NamedDecl *ToD;
2649 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002650 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002651 if (ToD)
2652 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002653
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002654 // If this typedef is not in block scope, determine whether we've
2655 // seen a typedef with the same name (that we can merge with) or any
2656 // other entity by that name (which name lookup could conflict with).
2657 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002658 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002659 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002660 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002661 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002662 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2663 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002664 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00002665 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002666 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002667 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2668 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002669 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002670 }
2671
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002672 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002673 }
2674
2675 if (!ConflictingDecls.empty()) {
2676 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2677 ConflictingDecls.data(),
2678 ConflictingDecls.size());
2679 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002680 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002681 }
2682 }
2683
Douglas Gregorb4964f72010-02-15 23:54:17 +00002684 // Import the underlying type of this typedef;
2685 QualType T = Importer.Import(D->getUnderlyingType());
2686 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002687 return nullptr;
2688
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002689 // Create the new typedef node.
2690 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002691 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00002692 TypedefNameDecl *ToTypedef;
2693 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002694 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2695 StartL, Loc,
2696 Name.getAsIdentifierInfo(),
2697 TInfo);
2698 else
Richard Smithdda56e42011-04-15 14:24:37 +00002699 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2700 StartL, Loc,
2701 Name.getAsIdentifierInfo(),
2702 TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002703
Douglas Gregordd483172010-02-22 17:42:47 +00002704 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002705 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002706 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00002707 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002708
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002709 return ToTypedef;
2710}
2711
Richard Smithdda56e42011-04-15 14:24:37 +00002712Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2713 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2714}
2715
2716Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2717 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2718}
2719
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002720Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
2721 // Import the major distinguishing characteristics of this label.
2722 DeclContext *DC, *LexicalDC;
2723 DeclarationName Name;
2724 SourceLocation Loc;
2725 NamedDecl *ToD;
2726 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2727 return nullptr;
2728 if (ToD)
2729 return ToD;
2730
2731 assert(LexicalDC->isFunctionOrMethod());
2732
2733 LabelDecl *ToLabel = D->isGnuLocal()
2734 ? LabelDecl::Create(Importer.getToContext(),
2735 DC, Importer.Import(D->getLocation()),
2736 Name.getAsIdentifierInfo(),
2737 Importer.Import(D->getLocStart()))
2738 : LabelDecl::Create(Importer.getToContext(),
2739 DC, Importer.Import(D->getLocation()),
2740 Name.getAsIdentifierInfo());
2741 Importer.Imported(D, ToLabel);
2742
2743 LabelStmt *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt()));
2744 if (!Label)
2745 return nullptr;
2746
2747 ToLabel->setStmt(Label);
2748 ToLabel->setLexicalDeclContext(LexicalDC);
2749 LexicalDC->addDeclInternal(ToLabel);
2750 return ToLabel;
2751}
2752
Douglas Gregor98c10182010-02-12 22:17:39 +00002753Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2754 // Import the major distinguishing characteristics of this enum.
2755 DeclContext *DC, *LexicalDC;
2756 DeclarationName Name;
2757 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002758 NamedDecl *ToD;
2759 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002760 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002761 if (ToD)
2762 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002763
Douglas Gregor98c10182010-02-12 22:17:39 +00002764 // Figure out what enum name we're looking for.
2765 unsigned IDNS = Decl::IDNS_Tag;
2766 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002767 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2768 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002769 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002770 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002771 IDNS |= Decl::IDNS_Ordinary;
2772
2773 // We may already have an enum of the same name; try to find and match it.
2774 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002775 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002776 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002777 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002778 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2779 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002780 continue;
2781
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002782 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002783 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002784 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2785 Found = Tag->getDecl();
2786 }
2787
2788 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002789 if (IsStructuralMatch(D, FoundEnum))
2790 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002791 }
2792
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002793 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002794 }
2795
2796 if (!ConflictingDecls.empty()) {
2797 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2798 ConflictingDecls.data(),
2799 ConflictingDecls.size());
2800 }
2801 }
2802
2803 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002804 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2805 Importer.Import(D->getLocStart()),
Craig Topper36250ad2014-05-12 05:36:57 +00002806 Loc, Name.getAsIdentifierInfo(), nullptr,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002807 D->isScoped(), D->isScopedUsingClassTag(),
2808 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002809 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002810 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002811 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002812 D2->setLexicalDeclContext(LexicalDC);
2813 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00002814 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002815
2816 // Import the integer type.
2817 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2818 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002819 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00002820 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002821
2822 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002823 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00002824 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002825
Douglas Gregor3996e242010-02-15 22:01:00 +00002826 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002827}
2828
Douglas Gregor5c73e912010-02-11 00:48:18 +00002829Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2830 // If this record has a definition in the translation unit we're coming from,
2831 // but this particular declaration is not that definition, import the
2832 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002833 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002834 if (Definition && Definition != D) {
2835 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002836 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00002837 return nullptr;
2838
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002839 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002840 }
2841
2842 // Import the major distinguishing characteristics of this record.
2843 DeclContext *DC, *LexicalDC;
2844 DeclarationName Name;
2845 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002846 NamedDecl *ToD;
2847 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002848 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002849 if (ToD)
2850 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002851
Douglas Gregor5c73e912010-02-11 00:48:18 +00002852 // Figure out what structure name we're looking for.
2853 unsigned IDNS = Decl::IDNS_Tag;
2854 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002855 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2856 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002857 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002858 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002859 IDNS |= Decl::IDNS_Ordinary;
2860
2861 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00002862 RecordDecl *AdoptDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002863 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002864 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002865 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002866 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002867 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2868 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002869 continue;
2870
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002871 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002872 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002873 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2874 Found = Tag->getDecl();
2875 }
2876
2877 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002878 if (D->isAnonymousStructOrUnion() &&
2879 FoundRecord->isAnonymousStructOrUnion()) {
2880 // If both anonymous structs/unions are in a record context, make sure
2881 // they occur in the same location in the context records.
David Blaikie05785d12013-02-20 22:23:23 +00002882 if (Optional<unsigned> Index1
Sean Callanan488f8612016-07-14 19:53:44 +00002883 = findUntaggedStructOrUnionIndex(D)) {
David Blaikie05785d12013-02-20 22:23:23 +00002884 if (Optional<unsigned> Index2 =
Sean Callanan488f8612016-07-14 19:53:44 +00002885 findUntaggedStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002886 if (*Index1 != *Index2)
2887 continue;
2888 }
2889 }
2890 }
2891
Douglas Gregor25791052010-02-12 00:09:27 +00002892 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002893 if ((SearchName && !D->isCompleteDefinition())
2894 || (D->isCompleteDefinition() &&
2895 D->isAnonymousStructOrUnion()
2896 == FoundDef->isAnonymousStructOrUnion() &&
2897 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002898 // The record types structurally match, or the "from" translation
2899 // unit only had a forward declaration anyway; call it the same
2900 // function.
2901 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002902 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002903 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002904 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002905 // We have a forward declaration of this type, so adopt that forward
2906 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00002907
2908 // If one or both can be completed from external storage then try one
2909 // last time to complete and compare them before doing this.
2910
2911 if (FoundRecord->hasExternalLexicalStorage() &&
2912 !FoundRecord->isCompleteDefinition())
2913 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2914 if (D->hasExternalLexicalStorage())
2915 D->getASTContext().getExternalSource()->CompleteType(D);
2916
2917 if (FoundRecord->isCompleteDefinition() &&
2918 D->isCompleteDefinition() &&
2919 !IsStructuralMatch(D, FoundRecord))
2920 continue;
2921
Douglas Gregor25791052010-02-12 00:09:27 +00002922 AdoptDecl = FoundRecord;
2923 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002924 } else if (!SearchName) {
2925 continue;
2926 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002927 }
2928
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002929 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002930 }
2931
Douglas Gregordd6006f2012-07-17 21:16:27 +00002932 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002933 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2934 ConflictingDecls.data(),
2935 ConflictingDecls.size());
2936 }
2937 }
2938
2939 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002940 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002941 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002942 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002943 CXXRecordDecl *D2CXX = nullptr;
2944 if (CXXRecordDecl *DCXX = llvm::dyn_cast<CXXRecordDecl>(D)) {
2945 if (DCXX->isLambda()) {
2946 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
2947 D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(),
2948 DC, TInfo, Loc,
2949 DCXX->isDependentLambda(),
2950 DCXX->isGenericLambda(),
2951 DCXX->getLambdaCaptureDefault());
2952 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
2953 if (DCXX->getLambdaContextDecl() && !CDecl)
2954 return nullptr;
Sean Callanan041cceb2016-05-14 05:43:57 +00002955 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl);
2956 } else if (DCXX->isInjectedClassName()) {
2957 // We have to be careful to do a similar dance to the one in
2958 // Sema::ActOnStartCXXMemberDeclarations
2959 CXXRecordDecl *const PrevDecl = nullptr;
2960 const bool DelayTypeCreation = true;
2961 D2CXX = CXXRecordDecl::Create(
2962 Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc,
2963 Name.getAsIdentifierInfo(), PrevDecl, DelayTypeCreation);
2964 Importer.getToContext().getTypeDeclType(
2965 D2CXX, llvm::dyn_cast<CXXRecordDecl>(DC));
Sean Callanan8bca9962016-03-28 21:43:01 +00002966 } else {
2967 D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
2968 D->getTagKind(),
2969 DC, StartLoc, Loc,
2970 Name.getAsIdentifierInfo());
2971 }
Douglas Gregor3996e242010-02-15 22:01:00 +00002972 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002973 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002974 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002975 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002976 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002977 }
Douglas Gregor14454802011-02-25 02:25:35 +00002978
2979 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002980 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002981 LexicalDC->addDeclInternal(D2);
Douglas Gregordd6006f2012-07-17 21:16:27 +00002982 if (D->isAnonymousStructOrUnion())
2983 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002984 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002985
Douglas Gregor3996e242010-02-15 22:01:00 +00002986 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002987
Douglas Gregor95d82832012-01-24 18:36:04 +00002988 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00002989 return nullptr;
2990
Douglas Gregor3996e242010-02-15 22:01:00 +00002991 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002992}
2993
Douglas Gregor98c10182010-02-12 22:17:39 +00002994Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2995 // Import the major distinguishing characteristics of this enumerator.
2996 DeclContext *DC, *LexicalDC;
2997 DeclarationName Name;
2998 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002999 NamedDecl *ToD;
3000 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003001 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003002 if (ToD)
3003 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00003004
3005 QualType T = Importer.Import(D->getType());
3006 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003007 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00003008
Douglas Gregor98c10182010-02-12 22:17:39 +00003009 // Determine whether there are any other declarations with the same name and
3010 // in the same context.
3011 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003012 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00003013 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003014 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003015 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003016 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3017 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00003018 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00003019
3020 if (EnumConstantDecl *FoundEnumConstant
3021 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
3022 if (IsStructuralMatch(D, FoundEnumConstant))
3023 return Importer.Imported(D, FoundEnumConstant);
3024 }
3025
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003026 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00003027 }
3028
3029 if (!ConflictingDecls.empty()) {
3030 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3031 ConflictingDecls.data(),
3032 ConflictingDecls.size());
3033 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003034 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00003035 }
3036 }
3037
3038 Expr *Init = Importer.Import(D->getInitExpr());
3039 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00003040 return nullptr;
3041
Douglas Gregor98c10182010-02-12 22:17:39 +00003042 EnumConstantDecl *ToEnumerator
3043 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
3044 Name.getAsIdentifierInfo(), T,
3045 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00003046 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00003047 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003048 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00003049 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00003050 return ToEnumerator;
3051}
Douglas Gregor5c73e912010-02-11 00:48:18 +00003052
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003053Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
3054 // Import the major distinguishing characteristics of this function.
3055 DeclContext *DC, *LexicalDC;
3056 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003057 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003058 NamedDecl *ToD;
3059 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003060 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003061 if (ToD)
3062 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003063
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003064 // Try to find a function in our own ("to") context with the same name, same
3065 // type, and in the same context as the function we're importing.
3066 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003067 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003068 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003069 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003070 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003071 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3072 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003073 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003074
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003075 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00003076 if (FoundFunction->hasExternalFormalLinkage() &&
3077 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003078 if (Importer.IsStructurallyEquivalent(D->getType(),
3079 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003080 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003081 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003082 }
3083
3084 // FIXME: Check for overloading more carefully, e.g., by boosting
3085 // Sema::IsOverload out to the AST library.
3086
3087 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003088 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003089 continue;
3090
3091 // Complain about inconsistent function types.
3092 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003093 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003094 Importer.ToDiag(FoundFunction->getLocation(),
3095 diag::note_odr_value_here)
3096 << FoundFunction->getType();
3097 }
3098 }
3099
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003100 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003101 }
3102
3103 if (!ConflictingDecls.empty()) {
3104 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3105 ConflictingDecls.data(),
3106 ConflictingDecls.size());
3107 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003108 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003109 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003110 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00003111
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003112 DeclarationNameInfo NameInfo(Name, Loc);
3113 // Import additional name location/type info.
3114 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3115
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003116 QualType FromTy = D->getType();
3117 bool usedDifferentExceptionSpec = false;
3118
3119 if (const FunctionProtoType *
3120 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
3121 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
3122 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
3123 // FunctionDecl that we are importing the FunctionProtoType for.
3124 // To avoid an infinite recursion when importing, create the FunctionDecl
3125 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00003126 if (FromEPI.ExceptionSpec.SourceDecl ||
3127 FromEPI.ExceptionSpec.SourceTemplate ||
3128 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003129 FunctionProtoType::ExtProtoInfo DefaultEPI;
3130 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00003131 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003132 usedDifferentExceptionSpec = true;
3133 }
3134 }
3135
Douglas Gregorb4964f72010-02-15 23:54:17 +00003136 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003137 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00003138 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003139 return nullptr;
3140
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003141 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003142 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00003143 for (auto P : D->parameters()) {
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00003144 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003145 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003146 return nullptr;
3147
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003148 Parameters.push_back(ToP);
3149 }
3150
3151 // Create the imported function.
3152 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Craig Topper36250ad2014-05-12 05:36:57 +00003153 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003154 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Douglas Gregor00eace12010-02-21 18:29:16 +00003155 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
3156 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
3157 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00003158 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003159 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00003160 FromConstructor->isExplicit(),
3161 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00003162 D->isImplicit(),
3163 D->isConstexpr());
Sean Callanandd2c1742016-05-16 20:48:03 +00003164 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
3165 SmallVector<CXXCtorInitializer *, 4> CtorInitializers;
3166 for (CXXCtorInitializer *I : FromConstructor->inits()) {
3167 CXXCtorInitializer *ToI =
3168 cast_or_null<CXXCtorInitializer>(Importer.Import(I));
3169 if (!ToI && I)
3170 return nullptr;
3171 CtorInitializers.push_back(ToI);
3172 }
3173 CXXCtorInitializer **Memory =
3174 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
3175 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
3176 CXXConstructorDecl *ToCtor = llvm::cast<CXXConstructorDecl>(ToFunction);
3177 ToCtor->setCtorInitializers(Memory);
3178 ToCtor->setNumCtorInitializers(NumInitializers);
3179 }
Douglas Gregor00eace12010-02-21 18:29:16 +00003180 } else if (isa<CXXDestructorDecl>(D)) {
3181 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
3182 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00003183 InnerLocStart,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00003184 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00003185 D->isInlineSpecified(),
3186 D->isImplicit());
3187 } else if (CXXConversionDecl *FromConversion
3188 = dyn_cast<CXXConversionDecl>(D)) {
3189 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
3190 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00003191 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003192 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00003193 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00003194 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00003195 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00003196 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00003197 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
3198 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
3199 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00003200 InnerLocStart,
Douglas Gregora50ad132010-11-29 16:04:58 +00003201 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003202 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00003203 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00003204 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00003205 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00003206 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003207 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Sean Callanan59721b32015-04-28 18:41:46 +00003208 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003209 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00003210 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00003211 D->hasWrittenPrototype(),
3212 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00003213 }
John McCall3e11ebe2010-03-15 10:12:16 +00003214
3215 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00003216 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003217 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00003218 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00003219 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
3220 ToFunction->setTrivial(D->isTrivial());
3221 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00003222 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003223
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003224 // Set the parameters.
3225 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003226 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00003227 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003228 }
David Blaikie9c70e042011-09-21 18:16:56 +00003229 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003230
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003231 if (usedDifferentExceptionSpec) {
3232 // Update FunctionProtoType::ExtProtoInfo.
3233 QualType T = Importer.Import(D->getType());
3234 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003235 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003236 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00003237 }
3238
Sean Callanan59721b32015-04-28 18:41:46 +00003239 // Import the body, if any.
3240 if (Stmt *FromBody = D->getBody()) {
3241 if (Stmt *ToBody = Importer.Import(FromBody)) {
3242 ToFunction->setBody(ToBody);
3243 }
3244 }
3245
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003246 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003247
3248 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00003249 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003250
Douglas Gregor43f54792010-02-17 02:12:47 +00003251 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003252}
3253
Douglas Gregor00eace12010-02-21 18:29:16 +00003254Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
3255 return VisitFunctionDecl(D);
3256}
3257
3258Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
3259 return VisitCXXMethodDecl(D);
3260}
3261
3262Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
3263 return VisitCXXMethodDecl(D);
3264}
3265
3266Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
3267 return VisitCXXMethodDecl(D);
3268}
3269
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003270static unsigned getFieldIndex(Decl *F) {
3271 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
3272 if (!Owner)
3273 return 0;
3274
3275 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00003276 for (const auto *D : Owner->noload_decls()) {
3277 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003278 return Index;
3279
3280 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
3281 ++Index;
3282 }
3283
3284 return Index;
3285}
3286
Douglas Gregor5c73e912010-02-11 00:48:18 +00003287Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
3288 // Import the major distinguishing characteristics of a variable.
3289 DeclContext *DC, *LexicalDC;
3290 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00003291 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003292 NamedDecl *ToD;
3293 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003294 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003295 if (ToD)
3296 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003297
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003298 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003299 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003300 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003301 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3302 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003303 // For anonymous fields, match up by index.
3304 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
3305 continue;
3306
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003307 if (Importer.IsStructurallyEquivalent(D->getType(),
3308 FoundField->getType())) {
3309 Importer.Imported(D, FoundField);
3310 return FoundField;
3311 }
3312
3313 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3314 << Name << D->getType() << FoundField->getType();
3315 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3316 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003317 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003318 }
3319 }
3320
Douglas Gregorb4964f72010-02-15 23:54:17 +00003321 // Import the type.
3322 QualType T = Importer.Import(D->getType());
3323 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003324 return nullptr;
3325
Douglas Gregor5c73e912010-02-11 00:48:18 +00003326 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3327 Expr *BitWidth = Importer.Import(D->getBitWidth());
3328 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00003329 return nullptr;
3330
Abramo Bagnaradff19302011-03-08 08:55:46 +00003331 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
3332 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00003333 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00003334 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00003335 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00003336 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003337 ToField->setLexicalDeclContext(LexicalDC);
Sean Callanan3a83ea72016-03-03 02:22:05 +00003338 if (Expr *FromInitializer = D->getInClassInitializer()) {
Sean Callananbb33f582016-03-03 01:21:28 +00003339 Expr *ToInitializer = Importer.Import(FromInitializer);
3340 if (ToInitializer)
3341 ToField->setInClassInitializer(ToInitializer);
3342 else
3343 return nullptr;
3344 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003345 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003346 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00003347 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003348 return ToField;
3349}
3350
Francois Pichet783dd6e2010-11-21 06:08:52 +00003351Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
3352 // Import the major distinguishing characteristics of a variable.
3353 DeclContext *DC, *LexicalDC;
3354 DeclarationName Name;
3355 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003356 NamedDecl *ToD;
3357 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003358 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003359 if (ToD)
3360 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003361
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003362 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003363 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003364 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003365 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003366 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003367 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003368 // For anonymous indirect fields, match up by index.
3369 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
3370 continue;
3371
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003372 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003373 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003374 !Name.isEmpty())) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003375 Importer.Imported(D, FoundField);
3376 return FoundField;
3377 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003378
3379 // If there are more anonymous fields to check, continue.
3380 if (!Name && I < N-1)
3381 continue;
3382
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003383 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3384 << Name << D->getType() << FoundField->getType();
3385 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3386 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003387 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003388 }
3389 }
3390
Francois Pichet783dd6e2010-11-21 06:08:52 +00003391 // Import the type.
3392 QualType T = Importer.Import(D->getType());
3393 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003394 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003395
3396 NamedDecl **NamedChain =
3397 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
3398
3399 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00003400 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00003401 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003402 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00003403 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003404 NamedChain[i++] = cast<NamedDecl>(D);
3405 }
3406
3407 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
Aaron Ballman260995b2014-10-15 16:58:18 +00003408 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
David Majnemer59f77922016-06-24 04:05:48 +00003409 {NamedChain, D->getChainingSize()});
Aaron Ballman260995b2014-10-15 16:58:18 +00003410
3411 for (const auto *Attr : D->attrs())
3412 ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
3413
Francois Pichet783dd6e2010-11-21 06:08:52 +00003414 ToIndirectField->setAccess(D->getAccess());
3415 ToIndirectField->setLexicalDeclContext(LexicalDC);
3416 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00003417 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003418 return ToIndirectField;
3419}
3420
Aleksei Sidorina693b372016-09-28 10:16:56 +00003421Decl *ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
3422 // Import the major distinguishing characteristics of a declaration.
3423 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3424 DeclContext *LexicalDC = D->getDeclContext() == D->getLexicalDeclContext()
3425 ? DC : Importer.ImportContext(D->getLexicalDeclContext());
3426 if (!DC || !LexicalDC)
3427 return nullptr;
3428
3429 // Determine whether we've already imported this decl.
3430 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
3431 auto *RD = cast<CXXRecordDecl>(DC);
3432 FriendDecl *ImportedFriend = RD->getFirstFriend();
3433 StructuralEquivalenceContext Context(
3434 Importer.getFromContext(), Importer.getToContext(),
3435 Importer.getNonEquivalentDecls(), false, false);
3436
3437 while (ImportedFriend) {
3438 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
3439 if (Context.IsStructurallyEquivalent(D->getFriendDecl(),
3440 ImportedFriend->getFriendDecl()))
3441 return Importer.Imported(D, ImportedFriend);
3442
3443 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
3444 if (Importer.IsStructurallyEquivalent(
3445 D->getFriendType()->getType(),
3446 ImportedFriend->getFriendType()->getType(), true))
3447 return Importer.Imported(D, ImportedFriend);
3448 }
3449 ImportedFriend = ImportedFriend->getNextFriend();
3450 }
3451
3452 // Not found. Create it.
3453 FriendDecl::FriendUnion ToFU;
3454 if (NamedDecl *FriendD = D->getFriendDecl())
3455 ToFU = cast_or_null<NamedDecl>(Importer.Import(FriendD));
3456 else
3457 ToFU = Importer.Import(D->getFriendType());
3458 if (!ToFU)
3459 return nullptr;
3460
3461 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
3462 TemplateParameterList **FromTPLists =
3463 D->getTrailingObjects<TemplateParameterList *>();
3464 for (unsigned I = 0; I < D->NumTPLists; I++) {
3465 TemplateParameterList *List = ImportTemplateParameterList(FromTPLists[I]);
3466 if (!List)
3467 return nullptr;
3468 ToTPLists[I] = List;
3469 }
3470
3471 FriendDecl *FrD = FriendDecl::Create(Importer.getToContext(), DC,
3472 Importer.Import(D->getLocation()),
3473 ToFU, Importer.Import(D->getFriendLoc()),
3474 ToTPLists);
3475
3476 Importer.Imported(D, FrD);
3477 RD->pushFriendDecl(FrD);
3478
3479 FrD->setAccess(D->getAccess());
3480 FrD->setLexicalDeclContext(LexicalDC);
3481 LexicalDC->addDeclInternal(FrD);
3482 return FrD;
3483}
3484
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003485Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
3486 // Import the major distinguishing characteristics of an ivar.
3487 DeclContext *DC, *LexicalDC;
3488 DeclarationName Name;
3489 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003490 NamedDecl *ToD;
3491 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003492 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003493 if (ToD)
3494 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003495
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003496 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003497 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003498 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003499 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3500 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003501 if (Importer.IsStructurallyEquivalent(D->getType(),
3502 FoundIvar->getType())) {
3503 Importer.Imported(D, FoundIvar);
3504 return FoundIvar;
3505 }
3506
3507 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
3508 << Name << D->getType() << FoundIvar->getType();
3509 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3510 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003511 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003512 }
3513 }
3514
3515 // Import the type.
3516 QualType T = Importer.Import(D->getType());
3517 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003518 return nullptr;
3519
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003520 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3521 Expr *BitWidth = Importer.Import(D->getBitWidth());
3522 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00003523 return nullptr;
3524
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00003525 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
3526 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003527 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003528 Loc, Name.getAsIdentifierInfo(),
3529 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003530 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003531 ToIvar->setLexicalDeclContext(LexicalDC);
3532 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003533 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003534 return ToIvar;
3535
3536}
3537
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003538Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
3539 // Import the major distinguishing characteristics of a variable.
3540 DeclContext *DC, *LexicalDC;
3541 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003542 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003543 NamedDecl *ToD;
3544 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003545 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003546 if (ToD)
3547 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003548
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003549 // Try to find a variable in our own ("to") context with the same name and
3550 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00003551 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00003552 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003553 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003554 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003555 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003556 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003557 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3558 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003559 continue;
3560
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003561 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003562 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00003563 if (FoundVar->hasExternalFormalLinkage() &&
3564 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003565 if (Importer.IsStructurallyEquivalent(D->getType(),
3566 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003567 MergeWithVar = FoundVar;
3568 break;
3569 }
3570
Douglas Gregor56521c52010-02-12 17:23:39 +00003571 const ArrayType *FoundArray
3572 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3573 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00003574 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00003575 if (FoundArray && TArray) {
3576 if (isa<IncompleteArrayType>(FoundArray) &&
3577 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003578 // Import the type.
3579 QualType T = Importer.Import(D->getType());
3580 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003581 return nullptr;
3582
Douglas Gregor56521c52010-02-12 17:23:39 +00003583 FoundVar->setType(T);
3584 MergeWithVar = FoundVar;
3585 break;
3586 } else if (isa<IncompleteArrayType>(TArray) &&
3587 isa<ConstantArrayType>(FoundArray)) {
3588 MergeWithVar = FoundVar;
3589 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003590 }
3591 }
3592
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003593 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003594 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003595 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3596 << FoundVar->getType();
3597 }
3598 }
3599
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003600 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003601 }
3602
3603 if (MergeWithVar) {
3604 // An equivalent variable with external linkage has been found. Link
3605 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003606 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003607
3608 if (VarDecl *DDef = D->getDefinition()) {
3609 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
3610 Importer.ToDiag(ExistingDef->getLocation(),
3611 diag::err_odr_variable_multiple_def)
3612 << Name;
3613 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
3614 } else {
3615 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00003616 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00003617 if (DDef->isInitKnownICE()) {
3618 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
3619 Eval->CheckedICE = true;
3620 Eval->IsICE = DDef->isInitICE();
3621 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003622 }
3623 }
3624
3625 return MergeWithVar;
3626 }
3627
3628 if (!ConflictingDecls.empty()) {
3629 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3630 ConflictingDecls.data(),
3631 ConflictingDecls.size());
3632 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003633 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003634 }
3635 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003636
Douglas Gregorb4964f72010-02-15 23:54:17 +00003637 // Import the type.
3638 QualType T = Importer.Import(D->getType());
3639 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003640 return nullptr;
3641
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003642 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003643 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00003644 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
3645 Importer.Import(D->getInnerLocStart()),
3646 Loc, Name.getAsIdentifierInfo(),
3647 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003648 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00003649 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003650 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003651 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003652 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003653 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003654
Sean Callanan59721b32015-04-28 18:41:46 +00003655 if (!D->isFileVarDecl() &&
3656 D->isUsed())
3657 ToVar->setIsUsed();
3658
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003659 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00003660 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00003661 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003662
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003663 return ToVar;
3664}
3665
Douglas Gregor8b228d72010-02-17 21:22:52 +00003666Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
3667 // Parameters are created in the translation unit's context, then moved
3668 // into the function declaration's context afterward.
3669 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3670
3671 // Import the name of this declaration.
3672 DeclarationName Name = Importer.Import(D->getDeclName());
3673 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003674 return nullptr;
3675
Douglas Gregor8b228d72010-02-17 21:22:52 +00003676 // Import the location of this declaration.
3677 SourceLocation Loc = Importer.Import(D->getLocation());
3678
3679 // Import the parameter's type.
3680 QualType T = Importer.Import(D->getType());
3681 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003682 return nullptr;
3683
Douglas Gregor8b228d72010-02-17 21:22:52 +00003684 // Create the imported parameter.
3685 ImplicitParamDecl *ToParm
3686 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
3687 Loc, Name.getAsIdentifierInfo(),
3688 T);
3689 return Importer.Imported(D, ToParm);
3690}
3691
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003692Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
3693 // Parameters are created in the translation unit's context, then moved
3694 // into the function declaration's context afterward.
3695 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3696
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003697 // Import the name of this declaration.
3698 DeclarationName Name = Importer.Import(D->getDeclName());
3699 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003700 return nullptr;
3701
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003702 // Import the location of this declaration.
3703 SourceLocation Loc = Importer.Import(D->getLocation());
3704
3705 // Import the parameter's type.
3706 QualType T = Importer.Import(D->getType());
3707 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003708 return nullptr;
3709
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003710 // Create the imported parameter.
3711 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3712 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003713 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003714 Loc, Name.getAsIdentifierInfo(),
3715 T, TInfo, D->getStorageClass(),
Craig Topper36250ad2014-05-12 05:36:57 +00003716 /*FIXME: Default argument*/nullptr);
John McCallf3cd6652010-03-12 18:31:32 +00003717 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Sean Callanan59721b32015-04-28 18:41:46 +00003718
3719 if (D->isUsed())
3720 ToParm->setIsUsed();
3721
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003722 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003723}
3724
Douglas Gregor43f54792010-02-17 02:12:47 +00003725Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3726 // Import the major distinguishing characteristics of a method.
3727 DeclContext *DC, *LexicalDC;
3728 DeclarationName Name;
3729 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003730 NamedDecl *ToD;
3731 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003732 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003733 if (ToD)
3734 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003735
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003736 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003737 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003738 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3739 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003740 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3741 continue;
3742
3743 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003744 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3745 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003746 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003747 << D->isInstanceMethod() << Name << D->getReturnType()
3748 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00003749 Importer.ToDiag(FoundMethod->getLocation(),
3750 diag::note_odr_objc_method_here)
3751 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003752 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003753 }
3754
3755 // Check the number of parameters.
3756 if (D->param_size() != FoundMethod->param_size()) {
3757 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3758 << D->isInstanceMethod() << Name
3759 << D->param_size() << FoundMethod->param_size();
3760 Importer.ToDiag(FoundMethod->getLocation(),
3761 diag::note_odr_objc_method_here)
3762 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003763 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003764 }
3765
3766 // Check parameter types.
3767 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
3768 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3769 P != PEnd; ++P, ++FoundP) {
3770 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3771 (*FoundP)->getType())) {
3772 Importer.FromDiag((*P)->getLocation(),
3773 diag::err_odr_objc_method_param_type_inconsistent)
3774 << D->isInstanceMethod() << Name
3775 << (*P)->getType() << (*FoundP)->getType();
3776 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3777 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003778 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003779 }
3780 }
3781
3782 // Check variadic/non-variadic.
3783 // Check the number of parameters.
3784 if (D->isVariadic() != FoundMethod->isVariadic()) {
3785 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3786 << D->isInstanceMethod() << Name;
3787 Importer.ToDiag(FoundMethod->getLocation(),
3788 diag::note_odr_objc_method_here)
3789 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003790 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003791 }
3792
3793 // FIXME: Any other bits we need to merge?
3794 return Importer.Imported(D, FoundMethod);
3795 }
3796 }
3797
3798 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00003799 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003800 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003801 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003802
Alp Toker314cc812014-01-25 16:55:45 +00003803 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00003804
Alp Toker314cc812014-01-25 16:55:45 +00003805 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
3806 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3807 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3808 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3809 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003810
3811 // FIXME: When we decide to merge method definitions, we'll need to
3812 // deal with implicit parameters.
3813
3814 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003815 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003816 for (auto *FromP : D->parameters()) {
Aaron Ballman43b68be2014-03-07 17:50:17 +00003817 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00003818 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003819 return nullptr;
3820
Douglas Gregor43f54792010-02-17 02:12:47 +00003821 ToParams.push_back(ToP);
3822 }
3823
3824 // Set the parameters.
3825 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3826 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003827 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003828 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003829 SmallVector<SourceLocation, 12> SelLocs;
3830 D->getSelectorLocs(SelLocs);
3831 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003832
3833 ToMethod->setLexicalDeclContext(LexicalDC);
3834 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003835 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003836 return ToMethod;
3837}
3838
Douglas Gregor85f3f952015-07-07 03:57:15 +00003839Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3840 // Import the major distinguishing characteristics of a category.
3841 DeclContext *DC, *LexicalDC;
3842 DeclarationName Name;
3843 SourceLocation Loc;
3844 NamedDecl *ToD;
3845 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3846 return nullptr;
3847 if (ToD)
3848 return ToD;
3849
3850 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3851 if (!BoundInfo)
3852 return nullptr;
3853
3854 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
3855 Importer.getToContext(), DC,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00003856 D->getVariance(),
3857 Importer.Import(D->getVarianceLoc()),
Douglas Gregore83b9562015-07-07 03:57:53 +00003858 D->getIndex(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003859 Importer.Import(D->getLocation()),
3860 Name.getAsIdentifierInfo(),
3861 Importer.Import(D->getColonLoc()),
3862 BoundInfo);
3863 Importer.Imported(D, Result);
3864 Result->setLexicalDeclContext(LexicalDC);
3865 return Result;
3866}
3867
Douglas Gregor84c51c32010-02-18 01:47:50 +00003868Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3869 // Import the major distinguishing characteristics of a category.
3870 DeclContext *DC, *LexicalDC;
3871 DeclarationName Name;
3872 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003873 NamedDecl *ToD;
3874 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003875 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003876 if (ToD)
3877 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003878
Douglas Gregor84c51c32010-02-18 01:47:50 +00003879 ObjCInterfaceDecl *ToInterface
3880 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3881 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00003882 return nullptr;
3883
Douglas Gregor84c51c32010-02-18 01:47:50 +00003884 // Determine if we've already encountered this category.
3885 ObjCCategoryDecl *MergeWithCategory
3886 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3887 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3888 if (!ToCategory) {
3889 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003890 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003891 Loc,
3892 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003893 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003894 ToInterface,
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003895 /*TypeParamList=*/nullptr,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003896 Importer.Import(D->getIvarLBraceLoc()),
3897 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003898 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003899 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003900 Importer.Imported(D, ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003901 // Import the type parameter list after calling Imported, to avoid
3902 // loops when bringing in their DeclContext.
3903 ToCategory->setTypeParamList(ImportObjCTypeParamList(
3904 D->getTypeParamList()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003905
Douglas Gregor84c51c32010-02-18 01:47:50 +00003906 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003907 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3908 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003909 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3910 = D->protocol_loc_begin();
3911 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3912 FromProtoEnd = D->protocol_end();
3913 FromProto != FromProtoEnd;
3914 ++FromProto, ++FromProtoLoc) {
3915 ObjCProtocolDecl *ToProto
3916 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3917 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00003918 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003919 Protocols.push_back(ToProto);
3920 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3921 }
3922
3923 // FIXME: If we're merging, make sure that the protocol list is the same.
3924 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3925 ProtocolLocs.data(), Importer.getToContext());
3926
3927 } else {
3928 Importer.Imported(D, ToCategory);
3929 }
3930
3931 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003932 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003933
3934 // If we have an implementation, import it as well.
3935 if (D->getImplementation()) {
3936 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003937 = cast_or_null<ObjCCategoryImplDecl>(
3938 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003939 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00003940 return nullptr;
3941
Douglas Gregor84c51c32010-02-18 01:47:50 +00003942 ToCategory->setImplementation(Impl);
3943 }
3944
3945 return ToCategory;
3946}
3947
Douglas Gregor2aa53772012-01-24 17:42:07 +00003948bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3949 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003950 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003951 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003952 if (shouldForceImportDeclContext(Kind))
3953 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003954 return false;
3955 }
3956
3957 // Start the protocol definition
3958 To->startDefinition();
3959
3960 // Import protocols
3961 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3962 SmallVector<SourceLocation, 4> ProtocolLocs;
3963 ObjCProtocolDecl::protocol_loc_iterator
3964 FromProtoLoc = From->protocol_loc_begin();
3965 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3966 FromProtoEnd = From->protocol_end();
3967 FromProto != FromProtoEnd;
3968 ++FromProto, ++FromProtoLoc) {
3969 ObjCProtocolDecl *ToProto
3970 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3971 if (!ToProto)
3972 return true;
3973 Protocols.push_back(ToProto);
3974 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3975 }
3976
3977 // FIXME: If we're merging, make sure that the protocol list is the same.
3978 To->setProtocolList(Protocols.data(), Protocols.size(),
3979 ProtocolLocs.data(), Importer.getToContext());
3980
Douglas Gregor2e15c842012-02-01 21:00:38 +00003981 if (shouldForceImportDeclContext(Kind)) {
3982 // Import all of the members of this protocol.
3983 ImportDeclContext(From, /*ForceImport=*/true);
3984 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003985 return false;
3986}
3987
Douglas Gregor98d156a2010-02-17 16:12:00 +00003988Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003989 // If this protocol has a definition in the translation unit we're coming
3990 // from, but this particular declaration is not that definition, import the
3991 // definition and map to that.
3992 ObjCProtocolDecl *Definition = D->getDefinition();
3993 if (Definition && Definition != D) {
3994 Decl *ImportedDef = Importer.Import(Definition);
3995 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003996 return nullptr;
3997
Douglas Gregor2aa53772012-01-24 17:42:07 +00003998 return Importer.Imported(D, ImportedDef);
3999 }
4000
Douglas Gregor84c51c32010-02-18 01:47:50 +00004001 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00004002 DeclContext *DC, *LexicalDC;
4003 DeclarationName Name;
4004 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004005 NamedDecl *ToD;
4006 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004007 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004008 if (ToD)
4009 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00004010
Craig Topper36250ad2014-05-12 05:36:57 +00004011 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004012 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004013 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004014 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4015 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004016 continue;
4017
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004018 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004019 break;
4020 }
4021
4022 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004023 if (!ToProto) {
4024 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
4025 Name.getAsIdentifierInfo(), Loc,
4026 Importer.Import(D->getAtStartLoc()),
Craig Topper36250ad2014-05-12 05:36:57 +00004027 /*PrevDecl=*/nullptr);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004028 ToProto->setLexicalDeclContext(LexicalDC);
4029 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004030 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00004031
4032 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004033
Douglas Gregor2aa53772012-01-24 17:42:07 +00004034 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00004035 return nullptr;
4036
Douglas Gregor98d156a2010-02-17 16:12:00 +00004037 return ToProto;
4038}
4039
Sean Callanan0aae0412014-12-10 00:00:37 +00004040Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
4041 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4042 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4043
4044 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
4045 SourceLocation LangLoc = Importer.Import(D->getLocation());
4046
4047 bool HasBraces = D->hasBraces();
4048
Sean Callananb12a8552014-12-10 21:22:20 +00004049 LinkageSpecDecl *ToLinkageSpec =
4050 LinkageSpecDecl::Create(Importer.getToContext(),
4051 DC,
4052 ExternLoc,
4053 LangLoc,
4054 D->getLanguage(),
4055 HasBraces);
Sean Callanan0aae0412014-12-10 00:00:37 +00004056
4057 if (HasBraces) {
4058 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
4059 ToLinkageSpec->setRBraceLoc(RBraceLoc);
4060 }
4061
4062 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
4063 LexicalDC->addDeclInternal(ToLinkageSpec);
4064
4065 Importer.Imported(D, ToLinkageSpec);
4066
4067 return ToLinkageSpec;
4068}
4069
Douglas Gregor2aa53772012-01-24 17:42:07 +00004070bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
4071 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004072 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004073 if (To->getDefinition()) {
4074 // Check consistency of superclass.
4075 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
4076 if (FromSuper) {
4077 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
4078 if (!FromSuper)
4079 return true;
4080 }
4081
4082 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
4083 if ((bool)FromSuper != (bool)ToSuper ||
4084 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
4085 Importer.ToDiag(To->getLocation(),
4086 diag::err_odr_objc_superclass_inconsistent)
4087 << To->getDeclName();
4088 if (ToSuper)
4089 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
4090 << To->getSuperClass()->getDeclName();
4091 else
4092 Importer.ToDiag(To->getLocation(),
4093 diag::note_odr_objc_missing_superclass);
4094 if (From->getSuperClass())
4095 Importer.FromDiag(From->getSuperClassLoc(),
4096 diag::note_odr_objc_superclass)
4097 << From->getSuperClass()->getDeclName();
4098 else
4099 Importer.FromDiag(From->getLocation(),
4100 diag::note_odr_objc_missing_superclass);
4101 }
4102
Douglas Gregor2e15c842012-02-01 21:00:38 +00004103 if (shouldForceImportDeclContext(Kind))
4104 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004105 return false;
4106 }
4107
4108 // Start the definition.
4109 To->startDefinition();
4110
4111 // If this class has a superclass, import it.
4112 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00004113 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
4114 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00004115 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00004116
4117 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004118 }
4119
4120 // Import protocols
4121 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4122 SmallVector<SourceLocation, 4> ProtocolLocs;
4123 ObjCInterfaceDecl::protocol_loc_iterator
4124 FromProtoLoc = From->protocol_loc_begin();
4125
4126 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
4127 FromProtoEnd = From->protocol_end();
4128 FromProto != FromProtoEnd;
4129 ++FromProto, ++FromProtoLoc) {
4130 ObjCProtocolDecl *ToProto
4131 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
4132 if (!ToProto)
4133 return true;
4134 Protocols.push_back(ToProto);
4135 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
4136 }
4137
4138 // FIXME: If we're merging, make sure that the protocol list is the same.
4139 To->setProtocolList(Protocols.data(), Protocols.size(),
4140 ProtocolLocs.data(), Importer.getToContext());
4141
4142 // Import categories. When the categories themselves are imported, they'll
4143 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00004144 for (auto *Cat : From->known_categories())
4145 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004146
Douglas Gregor2aa53772012-01-24 17:42:07 +00004147 // If we have an @implementation, import it as well.
4148 if (From->getImplementation()) {
4149 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
4150 Importer.Import(From->getImplementation()));
4151 if (!Impl)
4152 return true;
4153
4154 To->setImplementation(Impl);
4155 }
4156
Douglas Gregor2e15c842012-02-01 21:00:38 +00004157 if (shouldForceImportDeclContext(Kind)) {
4158 // Import all of the members of this class.
4159 ImportDeclContext(From, /*ForceImport=*/true);
4160 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00004161 return false;
4162}
4163
Douglas Gregor85f3f952015-07-07 03:57:15 +00004164ObjCTypeParamList *
4165ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
4166 if (!list)
4167 return nullptr;
4168
4169 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
4170 for (auto fromTypeParam : *list) {
4171 auto toTypeParam = cast_or_null<ObjCTypeParamDecl>(
4172 Importer.Import(fromTypeParam));
4173 if (!toTypeParam)
4174 return nullptr;
4175
4176 toTypeParams.push_back(toTypeParam);
4177 }
4178
4179 return ObjCTypeParamList::create(Importer.getToContext(),
4180 Importer.Import(list->getLAngleLoc()),
4181 toTypeParams,
4182 Importer.Import(list->getRAngleLoc()));
4183}
4184
Douglas Gregor45635322010-02-16 01:20:57 +00004185Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004186 // If this class has a definition in the translation unit we're coming from,
4187 // but this particular declaration is not that definition, import the
4188 // definition and map to that.
4189 ObjCInterfaceDecl *Definition = D->getDefinition();
4190 if (Definition && Definition != D) {
4191 Decl *ImportedDef = Importer.Import(Definition);
4192 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004193 return nullptr;
4194
Douglas Gregor2aa53772012-01-24 17:42:07 +00004195 return Importer.Imported(D, ImportedDef);
4196 }
4197
Douglas Gregor45635322010-02-16 01:20:57 +00004198 // Import the major distinguishing characteristics of an @interface.
4199 DeclContext *DC, *LexicalDC;
4200 DeclarationName Name;
4201 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004202 NamedDecl *ToD;
4203 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004204 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004205 if (ToD)
4206 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00004207
Douglas Gregor2aa53772012-01-24 17:42:07 +00004208 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00004209 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004210 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004211 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004212 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4213 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00004214 continue;
4215
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004216 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00004217 break;
4218 }
4219
Douglas Gregor2aa53772012-01-24 17:42:07 +00004220 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00004221 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004222 if (!ToIface) {
4223 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
4224 Importer.Import(D->getAtStartLoc()),
Douglas Gregor85f3f952015-07-07 03:57:15 +00004225 Name.getAsIdentifierInfo(),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004226 /*TypeParamList=*/nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00004227 /*PrevDecl=*/nullptr, Loc,
Douglas Gregor2aa53772012-01-24 17:42:07 +00004228 D->isImplicitInterfaceDecl());
4229 ToIface->setLexicalDeclContext(LexicalDC);
4230 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00004231 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00004232 Importer.Imported(D, ToIface);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004233 // Import the type parameter list after calling Imported, to avoid
4234 // loops when bringing in their DeclContext.
4235 ToIface->setTypeParamList(ImportObjCTypeParamList(
4236 D->getTypeParamListAsWritten()));
Douglas Gregor45635322010-02-16 01:20:57 +00004237
Douglas Gregor2aa53772012-01-24 17:42:07 +00004238 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00004239 return nullptr;
4240
Douglas Gregor98d156a2010-02-17 16:12:00 +00004241 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00004242}
4243
Douglas Gregor4da9d682010-12-07 15:32:12 +00004244Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
4245 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
4246 Importer.Import(D->getCategoryDecl()));
4247 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00004248 return nullptr;
4249
Douglas Gregor4da9d682010-12-07 15:32:12 +00004250 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
4251 if (!ToImpl) {
4252 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4253 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004254 return nullptr;
4255
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00004256 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00004257 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00004258 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00004259 Category->getClassInterface(),
4260 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00004261 Importer.Import(D->getAtStartLoc()),
4262 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004263
4264 DeclContext *LexicalDC = DC;
4265 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4266 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4267 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004268 return nullptr;
4269
Douglas Gregor4da9d682010-12-07 15:32:12 +00004270 ToImpl->setLexicalDeclContext(LexicalDC);
4271 }
4272
Sean Callanan95e74be2011-10-21 02:57:43 +00004273 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004274 Category->setImplementation(ToImpl);
4275 }
4276
4277 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00004278 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004279 return ToImpl;
4280}
4281
Douglas Gregorda8025c2010-12-07 01:26:03 +00004282Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
4283 // Find the corresponding interface.
4284 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
4285 Importer.Import(D->getClassInterface()));
4286 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00004287 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004288
4289 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00004290 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004291 if (D->getSuperClass()) {
4292 Super = cast_or_null<ObjCInterfaceDecl>(
4293 Importer.Import(D->getSuperClass()));
4294 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00004295 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004296 }
4297
4298 ObjCImplementationDecl *Impl = Iface->getImplementation();
4299 if (!Impl) {
4300 // We haven't imported an implementation yet. Create a new @implementation
4301 // now.
4302 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
4303 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00004304 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00004305 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00004306 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00004307 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00004308 Importer.Import(D->getIvarLBraceLoc()),
4309 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00004310
4311 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4312 DeclContext *LexicalDC
4313 = Importer.ImportContext(D->getLexicalDeclContext());
4314 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004315 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004316 Impl->setLexicalDeclContext(LexicalDC);
4317 }
4318
4319 // Associate the implementation with the class it implements.
4320 Iface->setImplementation(Impl);
4321 Importer.Imported(D, Iface->getImplementation());
4322 } else {
4323 Importer.Imported(D, Iface->getImplementation());
4324
4325 // Verify that the existing @implementation has the same superclass.
4326 if ((Super && !Impl->getSuperClass()) ||
4327 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004328 (Super && Impl->getSuperClass() &&
4329 !declaresSameEntity(Super->getCanonicalDecl(),
4330 Impl->getSuperClass()))) {
4331 Importer.ToDiag(Impl->getLocation(),
4332 diag::err_odr_objc_superclass_inconsistent)
4333 << Iface->getDeclName();
4334 // FIXME: It would be nice to have the location of the superclass
4335 // below.
4336 if (Impl->getSuperClass())
4337 Importer.ToDiag(Impl->getLocation(),
4338 diag::note_odr_objc_superclass)
4339 << Impl->getSuperClass()->getDeclName();
4340 else
4341 Importer.ToDiag(Impl->getLocation(),
4342 diag::note_odr_objc_missing_superclass);
4343 if (D->getSuperClass())
4344 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004345 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004346 << D->getSuperClass()->getDeclName();
4347 else
4348 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004349 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00004350 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004351 }
4352 }
4353
4354 // Import all of the members of this @implementation.
4355 ImportDeclContext(D);
4356
4357 return Impl;
4358}
4359
Douglas Gregora11c4582010-02-17 18:02:10 +00004360Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
4361 // Import the major distinguishing characteristics of an @property.
4362 DeclContext *DC, *LexicalDC;
4363 DeclarationName Name;
4364 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004365 NamedDecl *ToD;
4366 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004367 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004368 if (ToD)
4369 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004370
4371 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004372 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004373 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004374 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004375 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004376 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004377 // Check property types.
4378 if (!Importer.IsStructurallyEquivalent(D->getType(),
4379 FoundProp->getType())) {
4380 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
4381 << Name << D->getType() << FoundProp->getType();
4382 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4383 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00004384 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004385 }
4386
4387 // FIXME: Check property attributes, getters, setters, etc.?
4388
4389 // Consider these properties to be equivalent.
4390 Importer.Imported(D, FoundProp);
4391 return FoundProp;
4392 }
4393 }
4394
4395 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00004396 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
4397 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00004398 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004399
4400 // Create the new property.
4401 ObjCPropertyDecl *ToProperty
4402 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
4403 Name.getAsIdentifierInfo(),
4404 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00004405 Importer.Import(D->getLParenLoc()),
Douglas Gregor813a0662015-06-19 18:14:38 +00004406 Importer.Import(D->getType()),
4407 TSI,
Douglas Gregora11c4582010-02-17 18:02:10 +00004408 D->getPropertyImplementation());
4409 Importer.Imported(D, ToProperty);
4410 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004411 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004412
4413 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004414 ToProperty->setPropertyAttributesAsWritten(
4415 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00004416 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
4417 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
4418 ToProperty->setGetterMethodDecl(
4419 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
4420 ToProperty->setSetterMethodDecl(
4421 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
4422 ToProperty->setPropertyIvarDecl(
4423 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
4424 return ToProperty;
4425}
4426
Douglas Gregor14a49e22010-12-07 18:32:03 +00004427Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4428 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
4429 Importer.Import(D->getPropertyDecl()));
4430 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00004431 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004432
4433 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4434 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004435 return nullptr;
4436
Douglas Gregor14a49e22010-12-07 18:32:03 +00004437 // Import the lexical declaration context.
4438 DeclContext *LexicalDC = DC;
4439 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4440 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4441 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004442 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004443 }
4444
4445 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
4446 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00004447 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004448
4449 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004450 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004451 if (D->getPropertyIvarDecl()) {
4452 Ivar = cast_or_null<ObjCIvarDecl>(
4453 Importer.Import(D->getPropertyIvarDecl()));
4454 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00004455 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004456 }
4457
4458 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004459 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4460 Property->getQueryKind());
Douglas Gregor14a49e22010-12-07 18:32:03 +00004461 if (!ToImpl) {
4462 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
4463 Importer.Import(D->getLocStart()),
4464 Importer.Import(D->getLocation()),
4465 Property,
4466 D->getPropertyImplementation(),
4467 Ivar,
4468 Importer.Import(D->getPropertyIvarDeclLoc()));
4469 ToImpl->setLexicalDeclContext(LexicalDC);
4470 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00004471 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004472 } else {
4473 // Check that we have the same kind of property implementation (@synthesize
4474 // vs. @dynamic).
4475 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
4476 Importer.ToDiag(ToImpl->getLocation(),
4477 diag::err_odr_objc_property_impl_kind_inconsistent)
4478 << Property->getDeclName()
4479 << (ToImpl->getPropertyImplementation()
4480 == ObjCPropertyImplDecl::Dynamic);
4481 Importer.FromDiag(D->getLocation(),
4482 diag::note_odr_objc_property_impl_kind)
4483 << D->getPropertyDecl()->getDeclName()
4484 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00004485 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004486 }
4487
4488 // For @synthesize, check that we have the same
4489 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4490 Ivar != ToImpl->getPropertyIvarDecl()) {
4491 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
4492 diag::err_odr_objc_synthesize_ivar_inconsistent)
4493 << Property->getDeclName()
4494 << ToImpl->getPropertyIvarDecl()->getDeclName()
4495 << Ivar->getDeclName();
4496 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
4497 diag::note_odr_objc_synthesize_ivar_here)
4498 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00004499 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004500 }
4501
4502 // Merge the existing implementation with the new implementation.
4503 Importer.Imported(D, ToImpl);
4504 }
4505
4506 return ToImpl;
4507}
4508
Douglas Gregora082a492010-11-30 19:14:50 +00004509Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
4510 // For template arguments, we adopt the translation unit as our declaration
4511 // context. This context will be fixed when the actual template declaration
4512 // is created.
4513
4514 // FIXME: Import default argument.
4515 return TemplateTypeParmDecl::Create(Importer.getToContext(),
4516 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00004517 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004518 Importer.Import(D->getLocation()),
4519 D->getDepth(),
4520 D->getIndex(),
4521 Importer.Import(D->getIdentifier()),
4522 D->wasDeclaredWithTypename(),
4523 D->isParameterPack());
4524}
4525
4526Decl *
4527ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
4528 // Import the name of this declaration.
4529 DeclarationName Name = Importer.Import(D->getDeclName());
4530 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004531 return nullptr;
4532
Douglas Gregora082a492010-11-30 19:14:50 +00004533 // Import the location of this declaration.
4534 SourceLocation Loc = Importer.Import(D->getLocation());
4535
4536 // Import the type of this declaration.
4537 QualType T = Importer.Import(D->getType());
4538 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004539 return nullptr;
4540
Douglas Gregora082a492010-11-30 19:14:50 +00004541 // Import type-source information.
4542 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4543 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00004544 return nullptr;
4545
Douglas Gregora082a492010-11-30 19:14:50 +00004546 // FIXME: Import default argument.
4547
4548 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
4549 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004550 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004551 Loc, D->getDepth(), D->getPosition(),
4552 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00004553 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00004554}
4555
4556Decl *
4557ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4558 // Import the name of this declaration.
4559 DeclarationName Name = Importer.Import(D->getDeclName());
4560 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004561 return nullptr;
4562
Douglas Gregora082a492010-11-30 19:14:50 +00004563 // Import the location of this declaration.
4564 SourceLocation Loc = Importer.Import(D->getLocation());
4565
4566 // Import template parameters.
4567 TemplateParameterList *TemplateParams
4568 = ImportTemplateParameterList(D->getTemplateParameters());
4569 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004570 return nullptr;
4571
Douglas Gregora082a492010-11-30 19:14:50 +00004572 // FIXME: Import default argument.
4573
4574 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
4575 Importer.getToContext().getTranslationUnitDecl(),
4576 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00004577 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00004578 Name.getAsIdentifierInfo(),
4579 TemplateParams);
4580}
4581
4582Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
4583 // If this record has a definition in the translation unit we're coming from,
4584 // but this particular declaration is not that definition, import the
4585 // definition and map to that.
4586 CXXRecordDecl *Definition
4587 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
4588 if (Definition && Definition != D->getTemplatedDecl()) {
4589 Decl *ImportedDef
4590 = Importer.Import(Definition->getDescribedClassTemplate());
4591 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004592 return nullptr;
4593
Douglas Gregora082a492010-11-30 19:14:50 +00004594 return Importer.Imported(D, ImportedDef);
4595 }
4596
4597 // Import the major distinguishing characteristics of this class template.
4598 DeclContext *DC, *LexicalDC;
4599 DeclarationName Name;
4600 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004601 NamedDecl *ToD;
4602 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004603 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004604 if (ToD)
4605 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004606
Douglas Gregora082a492010-11-30 19:14:50 +00004607 // We may already have a template of the same name; try to find and match it.
4608 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004609 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004610 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004611 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004612 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4613 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00004614 continue;
4615
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004616 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00004617 if (ClassTemplateDecl *FoundTemplate
4618 = dyn_cast<ClassTemplateDecl>(Found)) {
4619 if (IsStructuralMatch(D, FoundTemplate)) {
4620 // The class templates structurally match; call it the same template.
4621 // FIXME: We may be filling in a forward declaration here. Handle
4622 // this case!
4623 Importer.Imported(D->getTemplatedDecl(),
4624 FoundTemplate->getTemplatedDecl());
4625 return Importer.Imported(D, FoundTemplate);
4626 }
4627 }
4628
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004629 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00004630 }
4631
4632 if (!ConflictingDecls.empty()) {
4633 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4634 ConflictingDecls.data(),
4635 ConflictingDecls.size());
4636 }
4637
4638 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004639 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00004640 }
4641
4642 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
4643
4644 // Create the declaration that is being templated.
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004645 // Create the declaration that is being templated.
4646 CXXRecordDecl *D2Templated = cast_or_null<CXXRecordDecl>(
4647 Importer.Import(DTemplated));
4648 if (!D2Templated)
4649 return nullptr;
4650
4651 // Resolve possible cyclic import.
4652 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
4653 return AlreadyImported;
4654
Douglas Gregora082a492010-11-30 19:14:50 +00004655 // Create the class template declaration itself.
4656 TemplateParameterList *TemplateParams
4657 = ImportTemplateParameterList(D->getTemplateParameters());
4658 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004659 return nullptr;
4660
Douglas Gregora082a492010-11-30 19:14:50 +00004661 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4662 Loc, Name, TemplateParams,
4663 D2Templated,
Craig Topper36250ad2014-05-12 05:36:57 +00004664 /*PrevDecl=*/nullptr);
Douglas Gregora082a492010-11-30 19:14:50 +00004665 D2Templated->setDescribedClassTemplate(D2);
4666
4667 D2->setAccess(D->getAccess());
4668 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004669 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004670
4671 // Note the relationship between the class templates.
4672 Importer.Imported(D, D2);
4673 Importer.Imported(DTemplated, D2Templated);
4674
John McCallf937c022011-10-07 06:10:15 +00004675 if (DTemplated->isCompleteDefinition() &&
4676 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004677 // FIXME: Import definition!
4678 }
4679
4680 return D2;
4681}
4682
Douglas Gregore2e50d332010-12-01 01:36:18 +00004683Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4684 ClassTemplateSpecializationDecl *D) {
4685 // If this record has a definition in the translation unit we're coming from,
4686 // but this particular declaration is not that definition, import the
4687 // definition and map to that.
4688 TagDecl *Definition = D->getDefinition();
4689 if (Definition && Definition != D) {
4690 Decl *ImportedDef = Importer.Import(Definition);
4691 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004692 return nullptr;
4693
Douglas Gregore2e50d332010-12-01 01:36:18 +00004694 return Importer.Imported(D, ImportedDef);
4695 }
4696
4697 ClassTemplateDecl *ClassTemplate
4698 = cast_or_null<ClassTemplateDecl>(Importer.Import(
4699 D->getSpecializedTemplate()));
4700 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004701 return nullptr;
4702
Douglas Gregore2e50d332010-12-01 01:36:18 +00004703 // Import the context of this declaration.
4704 DeclContext *DC = ClassTemplate->getDeclContext();
4705 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004706 return nullptr;
4707
Douglas Gregore2e50d332010-12-01 01:36:18 +00004708 DeclContext *LexicalDC = DC;
4709 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4710 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4711 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004712 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004713 }
4714
4715 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004716 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4717 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004718
4719 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004720 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004721 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4722 D->getTemplateArgs().size(),
4723 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004724 return nullptr;
4725
Douglas Gregore2e50d332010-12-01 01:36:18 +00004726 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004727 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004728 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004729 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004730 if (D2) {
4731 // We already have a class template specialization with these template
4732 // arguments.
4733
4734 // FIXME: Check for specialization vs. instantiation errors.
4735
4736 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004737 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004738 // The record types structurally match, or the "from" translation
4739 // unit only had a forward declaration anyway; call it the same
4740 // function.
4741 return Importer.Imported(D, FoundDef);
4742 }
4743 }
4744 } else {
4745 // Create a new specialization.
4746 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4747 D->getTagKind(), DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004748 StartLoc, IdLoc,
4749 ClassTemplate,
David Majnemer8b622692016-07-03 21:17:51 +00004750 TemplateArgs,
Craig Topper36250ad2014-05-12 05:36:57 +00004751 /*PrevDecl=*/nullptr);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004752 D2->setSpecializationKind(D->getSpecializationKind());
4753
4754 // Add this specialization to the class template.
4755 ClassTemplate->AddSpecialization(D2, InsertPos);
4756
4757 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004758 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00004759
4760 // Add the specialization to this context.
4761 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004762 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004763 }
4764 Importer.Imported(D, D2);
4765
John McCallf937c022011-10-07 06:10:15 +00004766 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004767 return nullptr;
4768
Douglas Gregore2e50d332010-12-01 01:36:18 +00004769 return D2;
4770}
4771
Larisse Voufo39a1e502013-08-06 01:03:05 +00004772Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4773 // If this variable has a definition in the translation unit we're coming
4774 // from,
4775 // but this particular declaration is not that definition, import the
4776 // definition and map to that.
4777 VarDecl *Definition =
4778 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4779 if (Definition && Definition != D->getTemplatedDecl()) {
4780 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4781 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004782 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004783
4784 return Importer.Imported(D, ImportedDef);
4785 }
4786
4787 // Import the major distinguishing characteristics of this variable template.
4788 DeclContext *DC, *LexicalDC;
4789 DeclarationName Name;
4790 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004791 NamedDecl *ToD;
4792 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004793 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004794 if (ToD)
4795 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004796
4797 // We may already have a template of the same name; try to find and match it.
4798 assert(!DC->isFunctionOrMethod() &&
4799 "Variable templates cannot be declared at function scope");
4800 SmallVector<NamedDecl *, 4> ConflictingDecls;
4801 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004802 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004803 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4804 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4805 continue;
4806
4807 Decl *Found = FoundDecls[I];
4808 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
4809 if (IsStructuralMatch(D, FoundTemplate)) {
4810 // The variable templates structurally match; call it the same template.
4811 Importer.Imported(D->getTemplatedDecl(),
4812 FoundTemplate->getTemplatedDecl());
4813 return Importer.Imported(D, FoundTemplate);
4814 }
4815 }
4816
4817 ConflictingDecls.push_back(FoundDecls[I]);
4818 }
4819
4820 if (!ConflictingDecls.empty()) {
4821 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4822 ConflictingDecls.data(),
4823 ConflictingDecls.size());
4824 }
4825
4826 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004827 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004828
4829 VarDecl *DTemplated = D->getTemplatedDecl();
4830
4831 // Import the type.
4832 QualType T = Importer.Import(DTemplated->getType());
4833 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004834 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004835
4836 // Create the declaration that is being templated.
4837 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4838 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
4839 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo());
4840 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc,
4841 IdLoc, Name.getAsIdentifierInfo(), T,
4842 TInfo, DTemplated->getStorageClass());
4843 D2Templated->setAccess(DTemplated->getAccess());
4844 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
4845 D2Templated->setLexicalDeclContext(LexicalDC);
4846
4847 // Importer.Imported(DTemplated, D2Templated);
4848 // LexicalDC->addDeclInternal(D2Templated);
4849
4850 // Merge the initializer.
4851 if (ImportDefinition(DTemplated, D2Templated))
Craig Topper36250ad2014-05-12 05:36:57 +00004852 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004853
4854 // Create the variable template declaration itself.
4855 TemplateParameterList *TemplateParams =
4856 ImportTemplateParameterList(D->getTemplateParameters());
4857 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004858 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004859
4860 VarTemplateDecl *D2 = VarTemplateDecl::Create(
Richard Smithbeef3452014-01-16 23:39:20 +00004861 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004862 D2Templated->setDescribedVarTemplate(D2);
4863
4864 D2->setAccess(D->getAccess());
4865 D2->setLexicalDeclContext(LexicalDC);
4866 LexicalDC->addDeclInternal(D2);
4867
4868 // Note the relationship between the variable templates.
4869 Importer.Imported(D, D2);
4870 Importer.Imported(DTemplated, D2Templated);
4871
4872 if (DTemplated->isThisDeclarationADefinition() &&
4873 !D2Templated->isThisDeclarationADefinition()) {
4874 // FIXME: Import definition!
4875 }
4876
4877 return D2;
4878}
4879
4880Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4881 VarTemplateSpecializationDecl *D) {
4882 // If this record has a definition in the translation unit we're coming from,
4883 // but this particular declaration is not that definition, import the
4884 // definition and map to that.
4885 VarDecl *Definition = D->getDefinition();
4886 if (Definition && Definition != D) {
4887 Decl *ImportedDef = Importer.Import(Definition);
4888 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004889 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004890
4891 return Importer.Imported(D, ImportedDef);
4892 }
4893
4894 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
4895 Importer.Import(D->getSpecializedTemplate()));
4896 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004897 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004898
4899 // Import the context of this declaration.
4900 DeclContext *DC = VarTemplate->getDeclContext();
4901 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004902 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004903
4904 DeclContext *LexicalDC = DC;
4905 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4906 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4907 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004908 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004909 }
4910
4911 // Import the location of this declaration.
4912 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4913 SourceLocation IdLoc = Importer.Import(D->getLocation());
4914
4915 // Import template arguments.
4916 SmallVector<TemplateArgument, 2> TemplateArgs;
4917 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4918 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004919 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004920
4921 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004922 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004923 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004924 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004925 if (D2) {
4926 // We already have a variable template specialization with these template
4927 // arguments.
4928
4929 // FIXME: Check for specialization vs. instantiation errors.
4930
4931 if (VarDecl *FoundDef = D2->getDefinition()) {
4932 if (!D->isThisDeclarationADefinition() ||
4933 IsStructuralMatch(D, FoundDef)) {
4934 // The record types structurally match, or the "from" translation
4935 // unit only had a forward declaration anyway; call it the same
4936 // variable.
4937 return Importer.Imported(D, FoundDef);
4938 }
4939 }
4940 } else {
4941
4942 // Import the type.
4943 QualType T = Importer.Import(D->getType());
4944 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004945 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004946 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4947
4948 // Create a new specialization.
4949 D2 = VarTemplateSpecializationDecl::Create(
4950 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
David Majnemer8b622692016-07-03 21:17:51 +00004951 D->getStorageClass(), TemplateArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004952 D2->setSpecializationKind(D->getSpecializationKind());
4953 D2->setTemplateArgsInfo(D->getTemplateArgsInfo());
4954
4955 // Add this specialization to the class template.
4956 VarTemplate->AddSpecialization(D2, InsertPos);
4957
4958 // Import the qualifier, if any.
4959 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4960
4961 // Add the specialization to this context.
4962 D2->setLexicalDeclContext(LexicalDC);
4963 LexicalDC->addDeclInternal(D2);
4964 }
4965 Importer.Imported(D, D2);
4966
4967 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004968 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004969
4970 return D2;
4971}
4972
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004973//----------------------------------------------------------------------------
4974// Import Statements
4975//----------------------------------------------------------------------------
4976
Sean Callanan59721b32015-04-28 18:41:46 +00004977DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4978 if (DG.isNull())
4979 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4980 size_t NumDecls = DG.end() - DG.begin();
4981 SmallVector<Decl *, 1> ToDecls(NumDecls);
4982 auto &_Importer = this->Importer;
4983 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4984 [&_Importer](Decl *D) -> Decl * {
4985 return _Importer.Import(D);
4986 });
4987 return DeclGroupRef::Create(Importer.getToContext(),
4988 ToDecls.begin(),
4989 NumDecls);
4990}
4991
4992 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4993 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4994 << S->getStmtClassName();
4995 return nullptr;
4996 }
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004997
4998
4999Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
5000 SmallVector<IdentifierInfo *, 4> Names;
5001 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
5002 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
5003 if (!ToII)
5004 return nullptr;
5005 Names.push_back(ToII);
5006 }
5007 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
5008 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
5009 if (!ToII)
5010 return nullptr;
5011 Names.push_back(ToII);
5012 }
5013
5014 SmallVector<StringLiteral *, 4> Clobbers;
5015 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
5016 StringLiteral *Clobber = cast_or_null<StringLiteral>(
5017 Importer.Import(S->getClobberStringLiteral(I)));
5018 if (!Clobber)
5019 return nullptr;
5020 Clobbers.push_back(Clobber);
5021 }
5022
5023 SmallVector<StringLiteral *, 4> Constraints;
5024 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
5025 StringLiteral *Output = cast_or_null<StringLiteral>(
5026 Importer.Import(S->getOutputConstraintLiteral(I)));
5027 if (!Output)
5028 return nullptr;
5029 Constraints.push_back(Output);
5030 }
5031
5032 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
5033 StringLiteral *Input = cast_or_null<StringLiteral>(
5034 Importer.Import(S->getInputConstraintLiteral(I)));
5035 if (!Input)
5036 return nullptr;
5037 Constraints.push_back(Input);
5038 }
5039
5040 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005041 if (ImportContainerChecked(S->outputs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005042 return nullptr;
5043
Aleksei Sidorina693b372016-09-28 10:16:56 +00005044 if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005045 return nullptr;
5046
5047 StringLiteral *AsmStr = cast_or_null<StringLiteral>(
5048 Importer.Import(S->getAsmString()));
5049 if (!AsmStr)
5050 return nullptr;
5051
5052 return new (Importer.getToContext()) GCCAsmStmt(
5053 Importer.getToContext(),
5054 Importer.Import(S->getAsmLoc()),
5055 S->isSimple(),
5056 S->isVolatile(),
5057 S->getNumOutputs(),
5058 S->getNumInputs(),
5059 Names.data(),
5060 Constraints.data(),
5061 Exprs.data(),
5062 AsmStr,
5063 S->getNumClobbers(),
5064 Clobbers.data(),
5065 Importer.Import(S->getRParenLoc()));
5066}
5067
Sean Callanan59721b32015-04-28 18:41:46 +00005068Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
5069 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
5070 for (Decl *ToD : ToDG) {
5071 if (!ToD)
5072 return nullptr;
5073 }
5074 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
5075 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
5076 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
5077}
5078
5079Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
5080 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
5081 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
5082 S->hasLeadingEmptyMacro());
5083}
5084
5085Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005086 llvm::SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005087
5088 if (ImportContainerChecked(S->body(), ToStmts))
Sean Callanan8bca9962016-03-28 21:43:01 +00005089 return nullptr;
5090
Sean Callanan59721b32015-04-28 18:41:46 +00005091 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
5092 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
5093 return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(),
5094 ToStmts,
5095 ToLBraceLoc, ToRBraceLoc);
5096}
5097
5098Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
5099 Expr *ToLHS = Importer.Import(S->getLHS());
5100 if (!ToLHS)
5101 return nullptr;
5102 Expr *ToRHS = Importer.Import(S->getRHS());
5103 if (!ToRHS && S->getRHS())
5104 return nullptr;
5105 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
5106 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
5107 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
5108 return new (Importer.getToContext()) CaseStmt(ToLHS, ToRHS,
5109 ToCaseLoc, ToEllipsisLoc,
5110 ToColonLoc);
5111}
5112
5113Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
5114 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
5115 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
5116 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5117 if (!ToSubStmt && S->getSubStmt())
5118 return nullptr;
5119 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
5120 ToSubStmt);
5121}
5122
5123Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
5124 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
5125 LabelDecl *ToLabelDecl =
5126 cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
5127 if (!ToLabelDecl && S->getDecl())
5128 return nullptr;
5129 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5130 if (!ToSubStmt && S->getSubStmt())
5131 return nullptr;
5132 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
5133 ToSubStmt);
5134}
5135
5136Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
5137 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
5138 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
5139 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
5140 ASTContext &_ToContext = Importer.getToContext();
5141 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
5142 [&_ToContext](const Attr *A) -> const Attr * {
5143 return A->clone(_ToContext);
5144 });
5145 for (const Attr *ToA : ToAttrs) {
5146 if (!ToA)
5147 return nullptr;
5148 }
5149 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5150 if (!ToSubStmt && S->getSubStmt())
5151 return nullptr;
5152 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
5153 ToAttrs, ToSubStmt);
5154}
5155
5156Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
5157 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
Richard Smitha547eb22016-07-14 00:11:03 +00005158 Stmt *ToInit = Importer.Import(S->getInit());
5159 if (!ToInit && S->getInit())
5160 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005161 VarDecl *ToConditionVariable = nullptr;
5162 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5163 ToConditionVariable =
5164 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5165 if (!ToConditionVariable)
5166 return nullptr;
5167 }
5168 Expr *ToCondition = Importer.Import(S->getCond());
5169 if (!ToCondition && S->getCond())
5170 return nullptr;
5171 Stmt *ToThenStmt = Importer.Import(S->getThen());
5172 if (!ToThenStmt && S->getThen())
5173 return nullptr;
5174 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
5175 Stmt *ToElseStmt = Importer.Import(S->getElse());
5176 if (!ToElseStmt && S->getElse())
5177 return nullptr;
5178 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
Richard Smithb130fe72016-06-23 19:16:49 +00005179 ToIfLoc, S->isConstexpr(),
Richard Smitha547eb22016-07-14 00:11:03 +00005180 ToInit,
Richard Smithb130fe72016-06-23 19:16:49 +00005181 ToConditionVariable,
Sean Callanan59721b32015-04-28 18:41:46 +00005182 ToCondition, ToThenStmt,
5183 ToElseLoc, ToElseStmt);
5184}
5185
5186Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
Richard Smitha547eb22016-07-14 00:11:03 +00005187 Stmt *ToInit = Importer.Import(S->getInit());
5188 if (!ToInit && S->getInit())
5189 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005190 VarDecl *ToConditionVariable = nullptr;
5191 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5192 ToConditionVariable =
5193 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5194 if (!ToConditionVariable)
5195 return nullptr;
5196 }
5197 Expr *ToCondition = Importer.Import(S->getCond());
5198 if (!ToCondition && S->getCond())
5199 return nullptr;
5200 SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt(
Richard Smitha547eb22016-07-14 00:11:03 +00005201 Importer.getToContext(), ToInit,
5202 ToConditionVariable, ToCondition);
Sean Callanan59721b32015-04-28 18:41:46 +00005203 Stmt *ToBody = Importer.Import(S->getBody());
5204 if (!ToBody && S->getBody())
5205 return nullptr;
5206 ToStmt->setBody(ToBody);
5207 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
5208 // Now we have to re-chain the cases.
5209 SwitchCase *LastChainedSwitchCase = nullptr;
5210 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
5211 SC = SC->getNextSwitchCase()) {
5212 SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
5213 if (!ToSC)
5214 return nullptr;
5215 if (LastChainedSwitchCase)
5216 LastChainedSwitchCase->setNextSwitchCase(ToSC);
5217 else
5218 ToStmt->setSwitchCaseList(ToSC);
5219 LastChainedSwitchCase = ToSC;
5220 }
5221 return ToStmt;
5222}
5223
5224Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
5225 VarDecl *ToConditionVariable = nullptr;
5226 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5227 ToConditionVariable =
5228 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5229 if (!ToConditionVariable)
5230 return nullptr;
5231 }
5232 Expr *ToCondition = Importer.Import(S->getCond());
5233 if (!ToCondition && S->getCond())
5234 return nullptr;
5235 Stmt *ToBody = Importer.Import(S->getBody());
5236 if (!ToBody && S->getBody())
5237 return nullptr;
5238 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
5239 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
5240 ToConditionVariable,
5241 ToCondition, ToBody,
5242 ToWhileLoc);
5243}
5244
5245Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5246 Stmt *ToBody = Importer.Import(S->getBody());
5247 if (!ToBody && S->getBody())
5248 return nullptr;
5249 Expr *ToCondition = Importer.Import(S->getCond());
5250 if (!ToCondition && S->getCond())
5251 return nullptr;
5252 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
5253 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
5254 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5255 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
5256 ToDoLoc, ToWhileLoc,
5257 ToRParenLoc);
5258}
5259
5260Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
5261 Stmt *ToInit = Importer.Import(S->getInit());
5262 if (!ToInit && S->getInit())
5263 return nullptr;
5264 Expr *ToCondition = Importer.Import(S->getCond());
5265 if (!ToCondition && S->getCond())
5266 return nullptr;
5267 VarDecl *ToConditionVariable = nullptr;
5268 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5269 ToConditionVariable =
5270 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5271 if (!ToConditionVariable)
5272 return nullptr;
5273 }
5274 Expr *ToInc = Importer.Import(S->getInc());
5275 if (!ToInc && S->getInc())
5276 return nullptr;
5277 Stmt *ToBody = Importer.Import(S->getBody());
5278 if (!ToBody && S->getBody())
5279 return nullptr;
5280 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5281 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
5282 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5283 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
5284 ToInit, ToCondition,
5285 ToConditionVariable,
5286 ToInc, ToBody,
5287 ToForLoc, ToLParenLoc,
5288 ToRParenLoc);
5289}
5290
5291Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5292 LabelDecl *ToLabel = nullptr;
5293 if (LabelDecl *FromLabel = S->getLabel()) {
5294 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
5295 if (!ToLabel)
5296 return nullptr;
5297 }
5298 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
5299 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
5300 return new (Importer.getToContext()) GotoStmt(ToLabel,
5301 ToGotoLoc, ToLabelLoc);
5302}
5303
5304Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5305 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
5306 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
5307 Expr *ToTarget = Importer.Import(S->getTarget());
5308 if (!ToTarget && S->getTarget())
5309 return nullptr;
5310 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
5311 ToTarget);
5312}
5313
5314Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5315 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
5316 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
5317}
5318
5319Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5320 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
5321 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
5322}
5323
5324Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5325 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
5326 Expr *ToRetExpr = Importer.Import(S->getRetValue());
5327 if (!ToRetExpr && S->getRetValue())
5328 return nullptr;
5329 VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate());
5330 VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
5331 if (!ToNRVOCandidate && NRVOCandidate)
5332 return nullptr;
5333 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
5334 ToNRVOCandidate);
5335}
5336
5337Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5338 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
5339 VarDecl *ToExceptionDecl = nullptr;
5340 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
5341 ToExceptionDecl =
5342 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5343 if (!ToExceptionDecl)
5344 return nullptr;
5345 }
5346 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
5347 if (!ToHandlerBlock && S->getHandlerBlock())
5348 return nullptr;
5349 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
5350 ToExceptionDecl,
5351 ToHandlerBlock);
5352}
5353
5354Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5355 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
5356 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
5357 if (!ToTryBlock && S->getTryBlock())
5358 return nullptr;
5359 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5360 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5361 CXXCatchStmt *FromHandler = S->getHandler(HI);
5362 if (Stmt *ToHandler = Importer.Import(FromHandler))
5363 ToHandlers[HI] = ToHandler;
5364 else
5365 return nullptr;
5366 }
5367 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
5368 ToHandlers);
5369}
5370
5371Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
5372 DeclStmt *ToRange =
5373 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
5374 if (!ToRange && S->getRangeStmt())
5375 return nullptr;
Richard Smith01694c32016-03-20 10:33:40 +00005376 DeclStmt *ToBegin =
5377 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
5378 if (!ToBegin && S->getBeginStmt())
5379 return nullptr;
5380 DeclStmt *ToEnd =
5381 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
5382 if (!ToEnd && S->getEndStmt())
Sean Callanan59721b32015-04-28 18:41:46 +00005383 return nullptr;
5384 Expr *ToCond = Importer.Import(S->getCond());
5385 if (!ToCond && S->getCond())
5386 return nullptr;
5387 Expr *ToInc = Importer.Import(S->getInc());
5388 if (!ToInc && S->getInc())
5389 return nullptr;
5390 DeclStmt *ToLoopVar =
5391 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
5392 if (!ToLoopVar && S->getLoopVarStmt())
5393 return nullptr;
5394 Stmt *ToBody = Importer.Import(S->getBody());
5395 if (!ToBody && S->getBody())
5396 return nullptr;
5397 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
Richard Smith9f690bd2015-10-27 06:02:45 +00005398 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00005399 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
5400 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
Richard Smith01694c32016-03-20 10:33:40 +00005401 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
Sean Callanan59721b32015-04-28 18:41:46 +00005402 ToCond, ToInc,
5403 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00005404 ToForLoc, ToCoawaitLoc,
5405 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005406}
5407
5408Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5409 Stmt *ToElem = Importer.Import(S->getElement());
5410 if (!ToElem && S->getElement())
5411 return nullptr;
5412 Expr *ToCollect = Importer.Import(S->getCollection());
5413 if (!ToCollect && S->getCollection())
5414 return nullptr;
5415 Stmt *ToBody = Importer.Import(S->getBody());
5416 if (!ToBody && S->getBody())
5417 return nullptr;
5418 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5419 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5420 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
5421 ToCollect,
5422 ToBody, ToForLoc,
5423 ToRParenLoc);
5424}
5425
5426Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5427 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
5428 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5429 VarDecl *ToExceptionDecl = nullptr;
5430 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
5431 ToExceptionDecl =
5432 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5433 if (!ToExceptionDecl)
5434 return nullptr;
5435 }
5436 Stmt *ToBody = Importer.Import(S->getCatchBody());
5437 if (!ToBody && S->getCatchBody())
5438 return nullptr;
5439 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
5440 ToRParenLoc,
5441 ToExceptionDecl,
5442 ToBody);
5443}
5444
5445Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5446 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
5447 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
5448 if (!ToAtFinallyStmt && S->getFinallyBody())
5449 return nullptr;
5450 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
5451 ToAtFinallyStmt);
5452}
5453
5454Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5455 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
5456 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
5457 if (!ToAtTryStmt && S->getTryBody())
5458 return nullptr;
5459 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5460 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5461 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
5462 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
5463 ToCatchStmts[CI] = ToCatchStmt;
5464 else
5465 return nullptr;
5466 }
5467 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
5468 if (!ToAtFinallyStmt && S->getFinallyStmt())
5469 return nullptr;
5470 return ObjCAtTryStmt::Create(Importer.getToContext(),
5471 ToAtTryLoc, ToAtTryStmt,
5472 ToCatchStmts.begin(), ToCatchStmts.size(),
5473 ToAtFinallyStmt);
5474}
5475
5476Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
5477 (ObjCAtSynchronizedStmt *S) {
5478 SourceLocation ToAtSynchronizedLoc =
5479 Importer.Import(S->getAtSynchronizedLoc());
5480 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
5481 if (!ToSynchExpr && S->getSynchExpr())
5482 return nullptr;
5483 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
5484 if (!ToSynchBody && S->getSynchBody())
5485 return nullptr;
5486 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5487 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5488}
5489
5490Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5491 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5492 Expr *ToThrow = Importer.Import(S->getThrowExpr());
5493 if (!ToThrow && S->getThrowExpr())
5494 return nullptr;
5495 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5496}
5497
5498Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5499 (ObjCAutoreleasePoolStmt *S) {
5500 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5501 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5502 if (!ToSubStmt && S->getSubStmt())
5503 return nullptr;
5504 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5505 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005506}
5507
5508//----------------------------------------------------------------------------
5509// Import Expressions
5510//----------------------------------------------------------------------------
5511Expr *ASTNodeImporter::VisitExpr(Expr *E) {
5512 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5513 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00005514 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005515}
5516
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005517Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
5518 QualType T = Importer.Import(E->getType());
5519 if (T.isNull())
5520 return nullptr;
5521
5522 Expr *SubExpr = Importer.Import(E->getSubExpr());
5523 if (!SubExpr && E->getSubExpr())
5524 return nullptr;
5525
5526 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
5527 if (!TInfo)
5528 return nullptr;
5529
5530 return new (Importer.getToContext()) VAArgExpr(
5531 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
5532 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
5533}
5534
5535
5536Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
5537 QualType T = Importer.Import(E->getType());
5538 if (T.isNull())
5539 return nullptr;
5540
5541 return new (Importer.getToContext()) GNUNullExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005542 T, Importer.Import(E->getLocStart()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005543}
5544
5545Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
5546 QualType T = Importer.Import(E->getType());
5547 if (T.isNull())
5548 return nullptr;
5549
5550 StringLiteral *SL = cast_or_null<StringLiteral>(
5551 Importer.Import(E->getFunctionName()));
5552 if (!SL && E->getFunctionName())
5553 return nullptr;
5554
5555 return new (Importer.getToContext()) PredefinedExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005556 Importer.Import(E->getLocStart()), T, E->getIdentType(), SL);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005557}
5558
Douglas Gregor52f820e2010-02-19 01:17:02 +00005559Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00005560 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
5561 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005562 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005563
Craig Topper36250ad2014-05-12 05:36:57 +00005564 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005565 if (E->getDecl() != E->getFoundDecl()) {
5566 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5567 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00005568 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005569 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00005570
5571 QualType T = Importer.Import(E->getType());
5572 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005573 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005574
Aleksei Sidorina693b372016-09-28 10:16:56 +00005575
5576 TemplateArgumentListInfo ToTAInfo;
5577 TemplateArgumentListInfo *ResInfo = nullptr;
5578 if (E->hasExplicitTemplateArgs()) {
5579 for (const auto &FromLoc : E->template_arguments()) {
5580 bool Error = false;
5581 TemplateArgumentLoc ToTALoc = ImportTemplateArgumentLoc(FromLoc, Error);
5582 if (Error)
5583 return nullptr;
5584 ToTAInfo.addArgument(ToTALoc);
5585 }
5586 ResInfo = &ToTAInfo;
5587 }
5588
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005589 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5590 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00005591 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005592 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00005593 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005594 Importer.Import(E->getLocation()),
5595 T, E->getValueKind(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005596 FoundD, ResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005597 if (E->hadMultipleCandidates())
5598 DRE->setHadMultipleCandidates(true);
5599 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00005600}
5601
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005602Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
5603 QualType T = Importer.Import(E->getType());
5604 if (T.isNull())
Aleksei Sidorina693b372016-09-28 10:16:56 +00005605 return nullptr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005606
5607 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
5608}
5609
5610ASTNodeImporter::Designator
5611ASTNodeImporter::ImportDesignator(const Designator &D) {
5612 if (D.isFieldDesignator()) {
5613 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
5614 // Caller checks for import error
5615 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
5616 Importer.Import(D.getFieldLoc()));
5617 }
5618 if (D.isArrayDesignator())
5619 return Designator(D.getFirstExprIndex(),
5620 Importer.Import(D.getLBracketLoc()),
5621 Importer.Import(D.getRBracketLoc()));
5622
5623 assert(D.isArrayRangeDesignator());
5624 return Designator(D.getFirstExprIndex(),
5625 Importer.Import(D.getLBracketLoc()),
5626 Importer.Import(D.getEllipsisLoc()),
5627 Importer.Import(D.getRBracketLoc()));
5628}
5629
5630
5631Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
5632 Expr *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
5633 if (!Init)
5634 return nullptr;
5635
5636 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
5637 // List elements from the second, the first is Init itself
5638 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
5639 if (Expr *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
5640 IndexExprs[I - 1] = Arg;
5641 else
5642 return nullptr;
5643 }
5644
5645 SmallVector<Designator, 4> Designators(DIE->size());
David Majnemerf7e36092016-06-23 00:15:04 +00005646 llvm::transform(DIE->designators(), Designators.begin(),
5647 [this](const Designator &D) -> Designator {
5648 return ImportDesignator(D);
5649 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005650
David Majnemerf7e36092016-06-23 00:15:04 +00005651 for (const Designator &D : DIE->designators())
5652 if (D.isFieldDesignator() && !D.getFieldName())
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005653 return nullptr;
5654
5655 return DesignatedInitExpr::Create(
David Majnemerf7e36092016-06-23 00:15:04 +00005656 Importer.getToContext(), Designators,
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005657 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
5658 DIE->usesGNUSyntax(), Init);
5659}
5660
5661Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
5662 QualType T = Importer.Import(E->getType());
5663 if (T.isNull())
5664 return nullptr;
5665
5666 return new (Importer.getToContext())
5667 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
5668}
5669
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005670Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5671 QualType T = Importer.Import(E->getType());
5672 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005673 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005674
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005675 return IntegerLiteral::Create(Importer.getToContext(),
5676 E->getValue(), T,
5677 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005678}
5679
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005680Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
5681 QualType T = Importer.Import(E->getType());
5682 if (T.isNull())
5683 return nullptr;
5684
5685 return FloatingLiteral::Create(Importer.getToContext(),
5686 E->getValue(), E->isExact(), T,
5687 Importer.Import(E->getLocation()));
5688}
5689
Douglas Gregor623421d2010-02-18 02:21:22 +00005690Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5691 QualType T = Importer.Import(E->getType());
5692 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005693 return nullptr;
5694
Douglas Gregorfb65e592011-07-27 05:40:30 +00005695 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5696 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005697 Importer.Import(E->getLocation()));
5698}
5699
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005700Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
5701 QualType T = Importer.Import(E->getType());
5702 if (T.isNull())
5703 return nullptr;
5704
5705 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
5706 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
5707
5708 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
5709 E->getKind(), E->isPascal(), T,
5710 Locations.data(), Locations.size());
5711}
5712
5713Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
5714 QualType T = Importer.Import(E->getType());
5715 if (T.isNull())
5716 return nullptr;
5717
5718 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
5719 if (!TInfo)
5720 return nullptr;
5721
5722 Expr *Init = Importer.Import(E->getInitializer());
5723 if (!Init)
5724 return nullptr;
5725
5726 return new (Importer.getToContext()) CompoundLiteralExpr(
5727 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
5728 Init, E->isFileScope());
5729}
5730
5731Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
5732 QualType T = Importer.Import(E->getType());
5733 if (T.isNull())
5734 return nullptr;
5735
5736 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
5737 if (ImportArrayChecked(
5738 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
5739 Exprs.begin()))
5740 return nullptr;
5741
5742 return new (Importer.getToContext()) AtomicExpr(
5743 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
5744 Importer.Import(E->getRParenLoc()));
5745}
5746
5747Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
5748 QualType T = Importer.Import(E->getType());
5749 if (T.isNull())
5750 return nullptr;
5751
5752 LabelDecl *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
5753 if (!ToLabel)
5754 return nullptr;
5755
5756 return new (Importer.getToContext()) AddrLabelExpr(
5757 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
5758 ToLabel, T);
5759}
5760
Douglas Gregorc74247e2010-02-19 01:07:06 +00005761Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5762 Expr *SubExpr = Importer.Import(E->getSubExpr());
5763 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005764 return nullptr;
5765
Douglas Gregorc74247e2010-02-19 01:07:06 +00005766 return new (Importer.getToContext())
5767 ParenExpr(Importer.Import(E->getLParen()),
5768 Importer.Import(E->getRParen()),
5769 SubExpr);
5770}
5771
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005772Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
5773 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005774 if (ImportContainerChecked(E->exprs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005775 return nullptr;
5776
5777 return new (Importer.getToContext()) ParenListExpr(
5778 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
5779 Exprs, Importer.Import(E->getLParenLoc()));
5780}
5781
5782Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
5783 QualType T = Importer.Import(E->getType());
5784 if (T.isNull())
5785 return nullptr;
5786
5787 CompoundStmt *ToSubStmt = cast_or_null<CompoundStmt>(
5788 Importer.Import(E->getSubStmt()));
5789 if (!ToSubStmt && E->getSubStmt())
5790 return nullptr;
5791
5792 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
5793 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
5794}
5795
Douglas Gregorc74247e2010-02-19 01:07:06 +00005796Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5797 QualType T = Importer.Import(E->getType());
5798 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005799 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005800
5801 Expr *SubExpr = Importer.Import(E->getSubExpr());
5802 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005803 return nullptr;
5804
Douglas Gregorc74247e2010-02-19 01:07:06 +00005805 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005806 T, E->getValueKind(),
5807 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00005808 Importer.Import(E->getOperatorLoc()));
5809}
5810
Peter Collingbournee190dee2011-03-11 19:24:49 +00005811Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
5812 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005813 QualType ResultType = Importer.Import(E->getType());
5814
5815 if (E->isArgumentType()) {
5816 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5817 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005818 return nullptr;
5819
Peter Collingbournee190dee2011-03-11 19:24:49 +00005820 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5821 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005822 Importer.Import(E->getOperatorLoc()),
5823 Importer.Import(E->getRParenLoc()));
5824 }
5825
5826 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5827 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005828 return nullptr;
5829
Peter Collingbournee190dee2011-03-11 19:24:49 +00005830 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5831 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005832 Importer.Import(E->getOperatorLoc()),
5833 Importer.Import(E->getRParenLoc()));
5834}
5835
Douglas Gregorc74247e2010-02-19 01:07:06 +00005836Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5837 QualType T = Importer.Import(E->getType());
5838 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005839 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005840
5841 Expr *LHS = Importer.Import(E->getLHS());
5842 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005843 return nullptr;
5844
Douglas Gregorc74247e2010-02-19 01:07:06 +00005845 Expr *RHS = Importer.Import(E->getRHS());
5846 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005847 return nullptr;
5848
Douglas Gregorc74247e2010-02-19 01:07:06 +00005849 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005850 T, E->getValueKind(),
5851 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005852 Importer.Import(E->getOperatorLoc()),
5853 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005854}
5855
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005856Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
5857 QualType T = Importer.Import(E->getType());
5858 if (T.isNull())
5859 return nullptr;
5860
5861 Expr *ToLHS = Importer.Import(E->getLHS());
5862 if (!ToLHS)
5863 return nullptr;
5864
5865 Expr *ToRHS = Importer.Import(E->getRHS());
5866 if (!ToRHS)
5867 return nullptr;
5868
5869 Expr *ToCond = Importer.Import(E->getCond());
5870 if (!ToCond)
5871 return nullptr;
5872
5873 return new (Importer.getToContext()) ConditionalOperator(
5874 ToCond, Importer.Import(E->getQuestionLoc()),
5875 ToLHS, Importer.Import(E->getColonLoc()),
5876 ToRHS, T, E->getValueKind(), E->getObjectKind());
5877}
5878
5879Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
5880 BinaryConditionalOperator *E) {
5881 QualType T = Importer.Import(E->getType());
5882 if (T.isNull())
5883 return nullptr;
5884
5885 Expr *Common = Importer.Import(E->getCommon());
5886 if (!Common)
5887 return nullptr;
5888
5889 Expr *Cond = Importer.Import(E->getCond());
5890 if (!Cond)
5891 return nullptr;
5892
5893 OpaqueValueExpr *OpaqueValue = cast_or_null<OpaqueValueExpr>(
5894 Importer.Import(E->getOpaqueValue()));
5895 if (!OpaqueValue)
5896 return nullptr;
5897
5898 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
5899 if (!TrueExpr)
5900 return nullptr;
5901
5902 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
5903 if (!FalseExpr)
5904 return nullptr;
5905
5906 return new (Importer.getToContext()) BinaryConditionalOperator(
5907 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
5908 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
5909 T, E->getValueKind(), E->getObjectKind());
5910}
5911
Aleksei Sidorina693b372016-09-28 10:16:56 +00005912Expr *ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
5913 QualType T = Importer.Import(E->getType());
5914 if (T.isNull())
5915 return nullptr;
5916
5917 TypeSourceInfo *ToQueried = Importer.Import(E->getQueriedTypeSourceInfo());
5918 if (!ToQueried)
5919 return nullptr;
5920
5921 Expr *Dim = Importer.Import(E->getDimensionExpression());
5922 if (!Dim && E->getDimensionExpression())
5923 return nullptr;
5924
5925 return new (Importer.getToContext()) ArrayTypeTraitExpr(
5926 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5927 E->getValue(), Dim, Importer.Import(E->getLocEnd()), T);
5928}
5929
5930Expr *ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
5931 QualType T = Importer.Import(E->getType());
5932 if (T.isNull())
5933 return nullptr;
5934
5935 Expr *ToQueried = Importer.Import(E->getQueriedExpression());
5936 if (!ToQueried)
5937 return nullptr;
5938
5939 return new (Importer.getToContext()) ExpressionTraitExpr(
5940 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5941 E->getValue(), Importer.Import(E->getLocEnd()), T);
5942}
5943
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005944Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
5945 QualType T = Importer.Import(E->getType());
5946 if (T.isNull())
5947 return nullptr;
5948
5949 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
5950 if (!SourceExpr && E->getSourceExpr())
5951 return nullptr;
5952
5953 return new (Importer.getToContext()) OpaqueValueExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005954 Importer.Import(E->getLocation()), T, E->getValueKind(),
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005955 E->getObjectKind(), SourceExpr);
5956}
5957
Aleksei Sidorina693b372016-09-28 10:16:56 +00005958Expr *ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
5959 QualType T = Importer.Import(E->getType());
5960 if (T.isNull())
5961 return nullptr;
5962
5963 Expr *ToLHS = Importer.Import(E->getLHS());
5964 if (!ToLHS)
5965 return nullptr;
5966
5967 Expr *ToRHS = Importer.Import(E->getRHS());
5968 if (!ToRHS)
5969 return nullptr;
5970
5971 return new (Importer.getToContext()) ArraySubscriptExpr(
5972 ToLHS, ToRHS, T, E->getValueKind(), E->getObjectKind(),
5973 Importer.Import(E->getRBracketLoc()));
5974}
5975
Douglas Gregorc74247e2010-02-19 01:07:06 +00005976Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5977 QualType T = Importer.Import(E->getType());
5978 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005979 return nullptr;
5980
Douglas Gregorc74247e2010-02-19 01:07:06 +00005981 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5982 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005983 return nullptr;
5984
Douglas Gregorc74247e2010-02-19 01:07:06 +00005985 QualType CompResultType = Importer.Import(E->getComputationResultType());
5986 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005987 return nullptr;
5988
Douglas Gregorc74247e2010-02-19 01:07:06 +00005989 Expr *LHS = Importer.Import(E->getLHS());
5990 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005991 return nullptr;
5992
Douglas Gregorc74247e2010-02-19 01:07:06 +00005993 Expr *RHS = Importer.Import(E->getRHS());
5994 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005995 return nullptr;
5996
Douglas Gregorc74247e2010-02-19 01:07:06 +00005997 return new (Importer.getToContext())
5998 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005999 T, E->getValueKind(),
6000 E->getObjectKind(),
6001 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00006002 Importer.Import(E->getOperatorLoc()),
6003 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006004}
6005
Aleksei Sidorina693b372016-09-28 10:16:56 +00006006bool ASTNodeImporter::ImportCastPath(CastExpr *CE, CXXCastPath &Path) {
6007 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
6008 if (CXXBaseSpecifier *Spec = Importer.Import(*I))
6009 Path.push_back(Spec);
6010 else
6011 return true;
6012 }
6013 return false;
John McCallcf142162010-08-07 06:22:56 +00006014}
6015
Douglas Gregor98c10182010-02-12 22:17:39 +00006016Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
6017 QualType T = Importer.Import(E->getType());
6018 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006019 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00006020
6021 Expr *SubExpr = Importer.Import(E->getSubExpr());
6022 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00006023 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00006024
6025 CXXCastPath BasePath;
6026 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00006027 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00006028
6029 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00006030 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00006031}
6032
Aleksei Sidorina693b372016-09-28 10:16:56 +00006033Expr *ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
Douglas Gregor5481d322010-02-19 01:32:14 +00006034 QualType T = Importer.Import(E->getType());
6035 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006036 return nullptr;
6037
Douglas Gregor5481d322010-02-19 01:32:14 +00006038 Expr *SubExpr = Importer.Import(E->getSubExpr());
6039 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00006040 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00006041
6042 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
6043 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00006044 return nullptr;
6045
John McCallcf142162010-08-07 06:22:56 +00006046 CXXCastPath BasePath;
6047 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00006048 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00006049
Aleksei Sidorina693b372016-09-28 10:16:56 +00006050 switch (E->getStmtClass()) {
6051 case Stmt::CStyleCastExprClass: {
6052 CStyleCastExpr *CCE = cast<CStyleCastExpr>(E);
6053 return CStyleCastExpr::Create(Importer.getToContext(), T,
6054 E->getValueKind(), E->getCastKind(),
6055 SubExpr, &BasePath, TInfo,
6056 Importer.Import(CCE->getLParenLoc()),
6057 Importer.Import(CCE->getRParenLoc()));
6058 }
6059
6060 case Stmt::CXXFunctionalCastExprClass: {
6061 CXXFunctionalCastExpr *FCE = cast<CXXFunctionalCastExpr>(E);
6062 return CXXFunctionalCastExpr::Create(Importer.getToContext(), T,
6063 E->getValueKind(), TInfo,
6064 E->getCastKind(), SubExpr, &BasePath,
6065 Importer.Import(FCE->getLParenLoc()),
6066 Importer.Import(FCE->getRParenLoc()));
6067 }
6068
6069 case Stmt::ObjCBridgedCastExprClass: {
6070 ObjCBridgedCastExpr *OCE = cast<ObjCBridgedCastExpr>(E);
6071 return new (Importer.getToContext()) ObjCBridgedCastExpr(
6072 Importer.Import(OCE->getLParenLoc()), OCE->getBridgeKind(),
6073 E->getCastKind(), Importer.Import(OCE->getBridgeKeywordLoc()),
6074 TInfo, SubExpr);
6075 }
6076 default:
6077 break; // just fall through
6078 }
6079
6080 CXXNamedCastExpr *Named = cast<CXXNamedCastExpr>(E);
6081 SourceLocation ExprLoc = Importer.Import(Named->getOperatorLoc()),
6082 RParenLoc = Importer.Import(Named->getRParenLoc());
6083 SourceRange Brackets = Importer.Import(Named->getAngleBrackets());
6084
6085 switch (E->getStmtClass()) {
6086 case Stmt::CXXStaticCastExprClass:
6087 return CXXStaticCastExpr::Create(Importer.getToContext(), T,
6088 E->getValueKind(), E->getCastKind(),
6089 SubExpr, &BasePath, TInfo,
6090 ExprLoc, RParenLoc, Brackets);
6091
6092 case Stmt::CXXDynamicCastExprClass:
6093 return CXXDynamicCastExpr::Create(Importer.getToContext(), T,
6094 E->getValueKind(), E->getCastKind(),
6095 SubExpr, &BasePath, TInfo,
6096 ExprLoc, RParenLoc, Brackets);
6097
6098 case Stmt::CXXReinterpretCastExprClass:
6099 return CXXReinterpretCastExpr::Create(Importer.getToContext(), T,
6100 E->getValueKind(), E->getCastKind(),
6101 SubExpr, &BasePath, TInfo,
6102 ExprLoc, RParenLoc, Brackets);
6103
6104 case Stmt::CXXConstCastExprClass:
6105 return CXXConstCastExpr::Create(Importer.getToContext(), T,
6106 E->getValueKind(), SubExpr, TInfo, ExprLoc,
6107 RParenLoc, Brackets);
6108 default:
6109 llvm_unreachable("Cast expression of unsupported type!");
6110 return nullptr;
6111 }
6112}
6113
6114Expr *ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *OE) {
6115 QualType T = Importer.Import(OE->getType());
6116 if (T.isNull())
6117 return nullptr;
6118
6119 SmallVector<OffsetOfNode, 4> Nodes;
6120 for (int I = 0, E = OE->getNumComponents(); I < E; ++I) {
6121 const OffsetOfNode &Node = OE->getComponent(I);
6122
6123 switch (Node.getKind()) {
6124 case OffsetOfNode::Array:
6125 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()),
6126 Node.getArrayExprIndex(),
6127 Importer.Import(Node.getLocEnd())));
6128 break;
6129
6130 case OffsetOfNode::Base: {
6131 CXXBaseSpecifier *BS = Importer.Import(Node.getBase());
6132 if (!BS && Node.getBase())
6133 return nullptr;
6134 Nodes.push_back(OffsetOfNode(BS));
6135 break;
6136 }
6137 case OffsetOfNode::Field: {
6138 FieldDecl *FD = cast_or_null<FieldDecl>(Importer.Import(Node.getField()));
6139 if (!FD)
6140 return nullptr;
6141 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), FD,
6142 Importer.Import(Node.getLocEnd())));
6143 break;
6144 }
6145 case OffsetOfNode::Identifier: {
6146 IdentifierInfo *ToII = Importer.Import(Node.getFieldName());
6147 if (!ToII)
6148 return nullptr;
6149 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), ToII,
6150 Importer.Import(Node.getLocEnd())));
6151 break;
6152 }
6153 }
6154 }
6155
6156 SmallVector<Expr *, 4> Exprs(OE->getNumExpressions());
6157 for (int I = 0, E = OE->getNumExpressions(); I < E; ++I) {
6158 Expr *ToIndexExpr = Importer.Import(OE->getIndexExpr(I));
6159 if (!ToIndexExpr)
6160 return nullptr;
6161 Exprs[I] = ToIndexExpr;
6162 }
6163
6164 TypeSourceInfo *TInfo = Importer.Import(OE->getTypeSourceInfo());
6165 if (!TInfo && OE->getTypeSourceInfo())
6166 return nullptr;
6167
6168 return OffsetOfExpr::Create(Importer.getToContext(), T,
6169 Importer.Import(OE->getOperatorLoc()),
6170 TInfo, Nodes, Exprs,
6171 Importer.Import(OE->getRParenLoc()));
6172}
6173
6174Expr *ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
6175 QualType T = Importer.Import(E->getType());
6176 if (T.isNull())
6177 return nullptr;
6178
6179 Expr *Operand = Importer.Import(E->getOperand());
6180 if (!Operand)
6181 return nullptr;
6182
6183 CanThrowResult CanThrow;
6184 if (E->isValueDependent())
6185 CanThrow = CT_Dependent;
6186 else
6187 CanThrow = E->getValue() ? CT_Can : CT_Cannot;
6188
6189 return new (Importer.getToContext()) CXXNoexceptExpr(
6190 T, Operand, CanThrow,
6191 Importer.Import(E->getLocStart()), Importer.Import(E->getLocEnd()));
6192}
6193
6194Expr *ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
6195 QualType T = Importer.Import(E->getType());
6196 if (T.isNull())
6197 return nullptr;
6198
6199 Expr *SubExpr = Importer.Import(E->getSubExpr());
6200 if (!SubExpr && E->getSubExpr())
6201 return nullptr;
6202
6203 return new (Importer.getToContext()) CXXThrowExpr(
6204 SubExpr, T, Importer.Import(E->getThrowLoc()),
6205 E->isThrownVariableInScope());
6206}
6207
6208Expr *ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
6209 ParmVarDecl *Param = cast_or_null<ParmVarDecl>(
6210 Importer.Import(E->getParam()));
6211 if (!Param)
6212 return nullptr;
6213
6214 return CXXDefaultArgExpr::Create(
6215 Importer.getToContext(), Importer.Import(E->getUsedLocation()), Param);
6216}
6217
6218Expr *ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
6219 QualType T = Importer.Import(E->getType());
6220 if (T.isNull())
6221 return nullptr;
6222
6223 TypeSourceInfo *TypeInfo = Importer.Import(E->getTypeSourceInfo());
6224 if (!TypeInfo)
6225 return nullptr;
6226
6227 return new (Importer.getToContext()) CXXScalarValueInitExpr(
6228 T, TypeInfo, Importer.Import(E->getRParenLoc()));
6229}
6230
6231Expr *ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
6232 Expr *SubExpr = Importer.Import(E->getSubExpr());
6233 if (!SubExpr)
6234 return nullptr;
6235
6236 auto *Dtor = cast_or_null<CXXDestructorDecl>(
6237 Importer.Import(const_cast<CXXDestructorDecl *>(
6238 E->getTemporary()->getDestructor())));
6239 if (!Dtor)
6240 return nullptr;
6241
6242 ASTContext &ToCtx = Importer.getToContext();
6243 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, Dtor);
6244 return CXXBindTemporaryExpr::Create(ToCtx, Temp, SubExpr);
6245}
6246
6247Expr *ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE) {
6248 QualType T = Importer.Import(CE->getType());
6249 if (T.isNull())
6250 return nullptr;
6251
6252 SmallVector<Expr *, 8> Args(CE->getNumArgs());
6253 if (ImportContainerChecked(CE->arguments(), Args))
6254 return nullptr;
6255
6256 auto *Ctor = cast_or_null<CXXConstructorDecl>(
6257 Importer.Import(CE->getConstructor()));
6258 if (!Ctor)
6259 return nullptr;
6260
6261 return CXXTemporaryObjectExpr::Create(
6262 Importer.getToContext(), T,
6263 Importer.Import(CE->getLocStart()),
6264 Ctor,
6265 CE->isElidable(),
6266 Args,
6267 CE->hadMultipleCandidates(),
6268 CE->isListInitialization(),
6269 CE->isStdInitListInitialization(),
6270 CE->requiresZeroInitialization(),
6271 CE->getConstructionKind(),
6272 Importer.Import(CE->getParenOrBraceRange()));
6273}
6274
6275Expr *
6276ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
6277 QualType T = Importer.Import(E->getType());
6278 if (T.isNull())
6279 return nullptr;
6280
6281 Expr *TempE = Importer.Import(E->GetTemporaryExpr());
6282 if (!TempE)
6283 return nullptr;
6284
6285 ValueDecl *ExtendedBy = cast_or_null<ValueDecl>(
6286 Importer.Import(const_cast<ValueDecl *>(E->getExtendingDecl())));
6287 if (!ExtendedBy && E->getExtendingDecl())
6288 return nullptr;
6289
6290 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
6291 T, TempE, E->isBoundToLvalueReference());
6292
6293 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
6294 ToMTE->setExtendingDecl(ExtendedBy, E->getManglingNumber());
6295 return ToMTE;
6296}
6297
6298Expr *ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *CE) {
6299 QualType T = Importer.Import(CE->getType());
6300 if (T.isNull())
6301 return nullptr;
6302
6303 SmallVector<Expr *, 4> PlacementArgs(CE->getNumPlacementArgs());
6304 if (ImportContainerChecked(CE->placement_arguments(), PlacementArgs))
6305 return nullptr;
6306
6307 FunctionDecl *OperatorNewDecl = cast_or_null<FunctionDecl>(
6308 Importer.Import(CE->getOperatorNew()));
6309 if (!OperatorNewDecl && CE->getOperatorNew())
6310 return nullptr;
6311
6312 FunctionDecl *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
6313 Importer.Import(CE->getOperatorDelete()));
6314 if (!OperatorDeleteDecl && CE->getOperatorDelete())
6315 return nullptr;
6316
6317 Expr *ToInit = Importer.Import(CE->getInitializer());
6318 if (!ToInit && CE->getInitializer())
6319 return nullptr;
6320
6321 TypeSourceInfo *TInfo = Importer.Import(CE->getAllocatedTypeSourceInfo());
6322 if (!TInfo)
6323 return nullptr;
6324
6325 Expr *ToArrSize = Importer.Import(CE->getArraySize());
6326 if (!ToArrSize && CE->getArraySize())
6327 return nullptr;
6328
6329 return new (Importer.getToContext()) CXXNewExpr(
6330 Importer.getToContext(),
6331 CE->isGlobalNew(),
6332 OperatorNewDecl, OperatorDeleteDecl,
Richard Smithb2f0f052016-10-10 18:54:32 +00006333 CE->passAlignment(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00006334 CE->doesUsualArrayDeleteWantSize(),
6335 PlacementArgs,
6336 Importer.Import(CE->getTypeIdParens()),
6337 ToArrSize, CE->getInitializationStyle(), ToInit, T, TInfo,
6338 Importer.Import(CE->getSourceRange()),
6339 Importer.Import(CE->getDirectInitRange()));
6340}
6341
6342Expr *ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
6343 QualType T = Importer.Import(E->getType());
6344 if (T.isNull())
6345 return nullptr;
6346
6347 FunctionDecl *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
6348 Importer.Import(E->getOperatorDelete()));
6349 if (!OperatorDeleteDecl && E->getOperatorDelete())
6350 return nullptr;
6351
6352 Expr *ToArg = Importer.Import(E->getArgument());
6353 if (!ToArg && E->getArgument())
6354 return nullptr;
6355
6356 return new (Importer.getToContext()) CXXDeleteExpr(
6357 T, E->isGlobalDelete(),
6358 E->isArrayForm(),
6359 E->isArrayFormAsWritten(),
6360 E->doesUsualArrayDeleteWantSize(),
6361 OperatorDeleteDecl,
6362 ToArg,
6363 Importer.Import(E->getLocStart()));
Douglas Gregor5481d322010-02-19 01:32:14 +00006364}
6365
Sean Callanan59721b32015-04-28 18:41:46 +00006366Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
6367 QualType T = Importer.Import(E->getType());
6368 if (T.isNull())
6369 return nullptr;
6370
6371 CXXConstructorDecl *ToCCD =
Sean Callanandd2c1742016-05-16 20:48:03 +00006372 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
Richard Smithc2bebe92016-05-11 20:37:46 +00006373 if (!ToCCD)
Sean Callanan59721b32015-04-28 18:41:46 +00006374 return nullptr;
6375
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006376 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006377 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006378 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006379
6380 return CXXConstructExpr::Create(Importer.getToContext(), T,
6381 Importer.Import(E->getLocation()),
Richard Smithc83bf822016-06-10 00:58:19 +00006382 ToCCD, E->isElidable(),
Sean Callanan59721b32015-04-28 18:41:46 +00006383 ToArgs, E->hadMultipleCandidates(),
6384 E->isListInitialization(),
6385 E->isStdInitListInitialization(),
6386 E->requiresZeroInitialization(),
6387 E->getConstructionKind(),
6388 Importer.Import(E->getParenOrBraceRange()));
6389}
6390
Aleksei Sidorina693b372016-09-28 10:16:56 +00006391Expr *ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *EWC) {
6392 Expr *SubExpr = Importer.Import(EWC->getSubExpr());
6393 if (!SubExpr && EWC->getSubExpr())
6394 return nullptr;
6395
6396 SmallVector<ExprWithCleanups::CleanupObject, 8> Objs(EWC->getNumObjects());
6397 for (unsigned I = 0, E = EWC->getNumObjects(); I < E; I++)
6398 if (ExprWithCleanups::CleanupObject Obj =
6399 cast_or_null<BlockDecl>(Importer.Import(EWC->getObject(I))))
6400 Objs[I] = Obj;
6401 else
6402 return nullptr;
6403
6404 return ExprWithCleanups::Create(Importer.getToContext(),
6405 SubExpr, EWC->cleanupsHaveSideEffects(),
6406 Objs);
6407}
6408
Sean Callanan8bca9962016-03-28 21:43:01 +00006409Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
6410 QualType T = Importer.Import(E->getType());
6411 if (T.isNull())
6412 return nullptr;
6413
6414 Expr *ToFn = Importer.Import(E->getCallee());
6415 if (!ToFn)
6416 return nullptr;
6417
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006418 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006419 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006420 return nullptr;
6421
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006422 return new (Importer.getToContext()) CXXMemberCallExpr(
6423 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
6424 Importer.Import(E->getRParenLoc()));
Sean Callanan8bca9962016-03-28 21:43:01 +00006425}
6426
6427Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
6428 QualType T = Importer.Import(E->getType());
6429 if (T.isNull())
6430 return nullptr;
6431
6432 return new (Importer.getToContext())
6433 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
6434}
6435
6436Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
6437 QualType T = Importer.Import(E->getType());
6438 if (T.isNull())
6439 return nullptr;
6440
6441 return new (Importer.getToContext())
6442 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
6443}
6444
6445
Sean Callanan59721b32015-04-28 18:41:46 +00006446Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
6447 QualType T = Importer.Import(E->getType());
6448 if (T.isNull())
6449 return nullptr;
6450
6451 Expr *ToBase = Importer.Import(E->getBase());
6452 if (!ToBase && E->getBase())
6453 return nullptr;
6454
6455 ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
6456 if (!ToMember && E->getMemberDecl())
6457 return nullptr;
6458
6459 DeclAccessPair ToFoundDecl = DeclAccessPair::make(
6460 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
6461 E->getFoundDecl().getAccess());
6462
6463 DeclarationNameInfo ToMemberNameInfo(
6464 Importer.Import(E->getMemberNameInfo().getName()),
6465 Importer.Import(E->getMemberNameInfo().getLoc()));
6466
6467 if (E->hasExplicitTemplateArgs()) {
6468 return nullptr; // FIXME: handle template arguments
6469 }
6470
6471 return MemberExpr::Create(Importer.getToContext(), ToBase,
6472 E->isArrow(),
6473 Importer.Import(E->getOperatorLoc()),
6474 Importer.Import(E->getQualifierLoc()),
6475 Importer.Import(E->getTemplateKeywordLoc()),
6476 ToMember, ToFoundDecl, ToMemberNameInfo,
6477 nullptr, T, E->getValueKind(),
6478 E->getObjectKind());
6479}
6480
6481Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
6482 QualType T = Importer.Import(E->getType());
6483 if (T.isNull())
6484 return nullptr;
6485
6486 Expr *ToCallee = Importer.Import(E->getCallee());
6487 if (!ToCallee && E->getCallee())
6488 return nullptr;
6489
6490 unsigned NumArgs = E->getNumArgs();
6491
6492 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
6493
6494 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) {
6495 Expr *FromArg = E->getArg(ai);
6496 Expr *ToArg = Importer.Import(FromArg);
6497 if (!ToArg)
6498 return nullptr;
6499 ToArgs[ai] = ToArg;
6500 }
6501
6502 Expr **ToArgs_Copied = new (Importer.getToContext())
6503 Expr*[NumArgs];
6504
6505 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
6506 ToArgs_Copied[ai] = ToArgs[ai];
6507
6508 return new (Importer.getToContext())
6509 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00006510 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00006511 Importer.Import(E->getRParenLoc()));
6512}
6513
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006514Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
6515 QualType T = Importer.Import(ILE->getType());
Sean Callanan8bca9962016-03-28 21:43:01 +00006516 if (T.isNull())
6517 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006518
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006519 llvm::SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006520 if (ImportContainerChecked(ILE->inits(), Exprs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006521 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006522
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006523 ASTContext &ToCtx = Importer.getToContext();
6524 InitListExpr *To = new (ToCtx) InitListExpr(
6525 ToCtx, Importer.Import(ILE->getLBraceLoc()),
6526 Exprs, Importer.Import(ILE->getLBraceLoc()));
6527 To->setType(T);
6528
6529 if (ILE->hasArrayFiller()) {
6530 Expr *Filler = Importer.Import(ILE->getArrayFiller());
6531 if (!Filler)
6532 return nullptr;
6533 To->setArrayFiller(Filler);
6534 }
6535
6536 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
6537 FieldDecl *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
6538 if (!ToFD)
6539 return nullptr;
6540 To->setInitializedFieldInUnion(ToFD);
6541 }
6542
6543 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
6544 InitListExpr *ToSyntForm = cast_or_null<InitListExpr>(
6545 Importer.Import(SyntForm));
6546 if (!ToSyntForm)
6547 return nullptr;
6548 To->setSyntacticForm(ToSyntForm);
6549 }
6550
6551 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
6552 To->setValueDependent(ILE->isValueDependent());
6553 To->setInstantiationDependent(ILE->isInstantiationDependent());
6554
6555 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00006556}
6557
Sean Callanandd2c1742016-05-16 20:48:03 +00006558Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
6559 FieldDecl *ToField = llvm::dyn_cast_or_null<FieldDecl>(
6560 Importer.Import(DIE->getField()));
6561 if (!ToField && DIE->getField())
6562 return nullptr;
6563
6564 return CXXDefaultInitExpr::Create(
6565 Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField);
6566}
6567
6568Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
6569 QualType ToType = Importer.Import(E->getType());
6570 if (ToType.isNull() && !E->getType().isNull())
6571 return nullptr;
6572 ExprValueKind VK = E->getValueKind();
6573 CastKind CK = E->getCastKind();
6574 Expr *ToOp = Importer.Import(E->getSubExpr());
6575 if (!ToOp && E->getSubExpr())
6576 return nullptr;
6577 CXXCastPath BasePath;
6578 if (ImportCastPath(E, BasePath))
6579 return nullptr;
6580 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten());
6581 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc());
6582 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc());
6583 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets());
6584
6585 if (isa<CXXStaticCastExpr>(E)) {
6586 return CXXStaticCastExpr::Create(
6587 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6588 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6589 } else if (isa<CXXDynamicCastExpr>(E)) {
6590 return CXXDynamicCastExpr::Create(
6591 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6592 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6593 } else if (isa<CXXReinterpretCastExpr>(E)) {
6594 return CXXReinterpretCastExpr::Create(
6595 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6596 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6597 } else {
6598 return nullptr;
6599 }
6600}
6601
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006602ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00006603 ASTContext &FromContext, FileManager &FromFileManager,
6604 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00006605 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00006606 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Richard Smith5bb4cdf2012-12-20 02:22:15 +00006607 Minimal(MinimalImport), LastDiagFromFrom(false)
Douglas Gregor0a791672011-01-18 03:11:38 +00006608{
Douglas Gregor62d311f2010-02-09 19:21:46 +00006609 ImportedDecls[FromContext.getTranslationUnitDecl()]
6610 = ToContext.getTranslationUnitDecl();
6611}
6612
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +00006613ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00006614
6615QualType ASTImporter::Import(QualType FromT) {
6616 if (FromT.isNull())
6617 return QualType();
John McCall424cec92011-01-19 06:33:43 +00006618
6619 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00006620
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006621 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00006622 llvm::DenseMap<const Type *, const Type *>::iterator Pos
6623 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006624 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00006625 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006626
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006627 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00006628 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00006629 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00006630 if (ToT.isNull())
6631 return ToT;
6632
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006633 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00006634 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006635
John McCall424cec92011-01-19 06:33:43 +00006636 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006637}
6638
Douglas Gregor62d311f2010-02-09 19:21:46 +00006639TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006640 if (!FromTSI)
6641 return FromTSI;
6642
6643 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00006644 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006645 QualType T = Import(FromTSI->getType());
6646 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006647 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006648
6649 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00006650 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00006651}
6652
Sean Callanan59721b32015-04-28 18:41:46 +00006653Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
6654 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
6655 if (Pos != ImportedDecls.end()) {
6656 Decl *ToD = Pos->second;
6657 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
6658 return ToD;
6659 } else {
6660 return nullptr;
6661 }
6662}
6663
Douglas Gregor62d311f2010-02-09 19:21:46 +00006664Decl *ASTImporter::Import(Decl *FromD) {
6665 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00006666 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006667
Douglas Gregord451ea92011-07-29 23:31:30 +00006668 ASTNodeImporter Importer(*this);
6669
Douglas Gregor62d311f2010-02-09 19:21:46 +00006670 // Check whether we've already imported this declaration.
6671 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00006672 if (Pos != ImportedDecls.end()) {
6673 Decl *ToD = Pos->second;
6674 Importer.ImportDefinitionIfNeeded(FromD, ToD);
6675 return ToD;
6676 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00006677
6678 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00006679 Decl *ToD = Importer.Visit(FromD);
6680 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00006681 return nullptr;
6682
Douglas Gregor62d311f2010-02-09 19:21:46 +00006683 // Record the imported declaration.
6684 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00006685
6686 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
6687 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00006688 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00006689 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00006690 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00006691 // When we've finished transforming a typedef, see whether it was the
6692 // typedef for an anonymous tag.
Craig Topper2341c0d2013-07-04 03:08:24 +00006693 for (SmallVectorImpl<TagDecl *>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00006694 FromTag = AnonTagsWithPendingTypedefs.begin(),
6695 FromTagEnd = AnonTagsWithPendingTypedefs.end();
6696 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00006697 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00006698 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
6699 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00006700 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00006701 AnonTagsWithPendingTypedefs.erase(FromTag);
6702 break;
6703 }
6704 }
6705 }
6706 }
6707
Douglas Gregor62d311f2010-02-09 19:21:46 +00006708 return ToD;
6709}
6710
6711DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
6712 if (!FromDC)
6713 return FromDC;
6714
Douglas Gregor95d82832012-01-24 18:36:04 +00006715 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00006716 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00006717 return nullptr;
6718
Douglas Gregor2e15c842012-02-01 21:00:38 +00006719 // When we're using a record/enum/Objective-C class/protocol as a context, we
6720 // need it to have a definition.
6721 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00006722 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006723 if (ToRecord->isCompleteDefinition()) {
6724 // Do nothing.
6725 } else if (FromRecord->isCompleteDefinition()) {
6726 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
6727 ASTNodeImporter::IDK_Basic);
6728 } else {
6729 CompleteDecl(ToRecord);
6730 }
6731 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
6732 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
6733 if (ToEnum->isCompleteDefinition()) {
6734 // Do nothing.
6735 } else if (FromEnum->isCompleteDefinition()) {
6736 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
6737 ASTNodeImporter::IDK_Basic);
6738 } else {
6739 CompleteDecl(ToEnum);
6740 }
6741 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
6742 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
6743 if (ToClass->getDefinition()) {
6744 // Do nothing.
6745 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
6746 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
6747 ASTNodeImporter::IDK_Basic);
6748 } else {
6749 CompleteDecl(ToClass);
6750 }
6751 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
6752 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
6753 if (ToProto->getDefinition()) {
6754 // Do nothing.
6755 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
6756 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
6757 ASTNodeImporter::IDK_Basic);
6758 } else {
6759 CompleteDecl(ToProto);
6760 }
Douglas Gregor95d82832012-01-24 18:36:04 +00006761 }
6762
6763 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006764}
6765
6766Expr *ASTImporter::Import(Expr *FromE) {
6767 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00006768 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006769
6770 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
6771}
6772
6773Stmt *ASTImporter::Import(Stmt *FromS) {
6774 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00006775 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006776
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006777 // Check whether we've already imported this declaration.
6778 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
6779 if (Pos != ImportedStmts.end())
6780 return Pos->second;
6781
6782 // Import the type
6783 ASTNodeImporter Importer(*this);
6784 Stmt *ToS = Importer.Visit(FromS);
6785 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00006786 return nullptr;
6787
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006788 // Record the imported declaration.
6789 ImportedStmts[FromS] = ToS;
6790 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006791}
6792
6793NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
6794 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00006795 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006796
Douglas Gregor90ebf252011-04-27 16:48:40 +00006797 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
6798
6799 switch (FromNNS->getKind()) {
6800 case NestedNameSpecifier::Identifier:
6801 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
6802 return NestedNameSpecifier::Create(ToContext, prefix, II);
6803 }
Craig Topper36250ad2014-05-12 05:36:57 +00006804 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006805
6806 case NestedNameSpecifier::Namespace:
6807 if (NamespaceDecl *NS =
6808 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
6809 return NestedNameSpecifier::Create(ToContext, prefix, NS);
6810 }
Craig Topper36250ad2014-05-12 05:36:57 +00006811 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006812
6813 case NestedNameSpecifier::NamespaceAlias:
6814 if (NamespaceAliasDecl *NSAD =
6815 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
6816 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
6817 }
Craig Topper36250ad2014-05-12 05:36:57 +00006818 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006819
6820 case NestedNameSpecifier::Global:
6821 return NestedNameSpecifier::GlobalSpecifier(ToContext);
6822
Nikola Smiljanic67860242014-09-26 00:28:20 +00006823 case NestedNameSpecifier::Super:
6824 if (CXXRecordDecl *RD =
6825 cast<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
6826 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
6827 }
6828 return nullptr;
6829
Douglas Gregor90ebf252011-04-27 16:48:40 +00006830 case NestedNameSpecifier::TypeSpec:
6831 case NestedNameSpecifier::TypeSpecWithTemplate: {
6832 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
6833 if (!T.isNull()) {
6834 bool bTemplate = FromNNS->getKind() ==
6835 NestedNameSpecifier::TypeSpecWithTemplate;
6836 return NestedNameSpecifier::Create(ToContext, prefix,
6837 bTemplate, T.getTypePtr());
6838 }
6839 }
Craig Topper36250ad2014-05-12 05:36:57 +00006840 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006841 }
6842
6843 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00006844}
6845
Douglas Gregor14454802011-02-25 02:25:35 +00006846NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
6847 // FIXME: Implement!
6848 return NestedNameSpecifierLoc();
6849}
6850
Douglas Gregore2e50d332010-12-01 01:36:18 +00006851TemplateName ASTImporter::Import(TemplateName From) {
6852 switch (From.getKind()) {
6853 case TemplateName::Template:
6854 if (TemplateDecl *ToTemplate
6855 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
6856 return TemplateName(ToTemplate);
6857
6858 return TemplateName();
6859
6860 case TemplateName::OverloadedTemplate: {
6861 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
6862 UnresolvedSet<2> ToTemplates;
6863 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
6864 E = FromStorage->end();
6865 I != E; ++I) {
6866 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
6867 ToTemplates.addDecl(To);
6868 else
6869 return TemplateName();
6870 }
6871 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
6872 ToTemplates.end());
6873 }
6874
6875 case TemplateName::QualifiedTemplate: {
6876 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
6877 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
6878 if (!Qualifier)
6879 return TemplateName();
6880
6881 if (TemplateDecl *ToTemplate
6882 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
6883 return ToContext.getQualifiedTemplateName(Qualifier,
6884 QTN->hasTemplateKeyword(),
6885 ToTemplate);
6886
6887 return TemplateName();
6888 }
6889
6890 case TemplateName::DependentTemplate: {
6891 DependentTemplateName *DTN = From.getAsDependentTemplateName();
6892 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
6893 if (!Qualifier)
6894 return TemplateName();
6895
6896 if (DTN->isIdentifier()) {
6897 return ToContext.getDependentTemplateName(Qualifier,
6898 Import(DTN->getIdentifier()));
6899 }
6900
6901 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
6902 }
John McCalld9dfe3a2011-06-30 08:33:18 +00006903
6904 case TemplateName::SubstTemplateTemplateParm: {
6905 SubstTemplateTemplateParmStorage *subst
6906 = From.getAsSubstTemplateTemplateParm();
6907 TemplateTemplateParmDecl *param
6908 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
6909 if (!param)
6910 return TemplateName();
6911
6912 TemplateName replacement = Import(subst->getReplacement());
6913 if (replacement.isNull()) return TemplateName();
6914
6915 return ToContext.getSubstTemplateTemplateParm(param, replacement);
6916 }
Douglas Gregor5590be02011-01-15 06:45:20 +00006917
6918 case TemplateName::SubstTemplateTemplateParmPack: {
6919 SubstTemplateTemplateParmPackStorage *SubstPack
6920 = From.getAsSubstTemplateTemplateParmPack();
6921 TemplateTemplateParmDecl *Param
6922 = cast_or_null<TemplateTemplateParmDecl>(
6923 Import(SubstPack->getParameterPack()));
6924 if (!Param)
6925 return TemplateName();
6926
6927 ASTNodeImporter Importer(*this);
6928 TemplateArgument ArgPack
6929 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
6930 if (ArgPack.isNull())
6931 return TemplateName();
6932
6933 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
6934 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00006935 }
6936
6937 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00006938}
6939
Douglas Gregor62d311f2010-02-09 19:21:46 +00006940SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
6941 if (FromLoc.isInvalid())
6942 return SourceLocation();
6943
Douglas Gregor811663e2010-02-10 00:15:17 +00006944 SourceManager &FromSM = FromContext.getSourceManager();
6945
Sean Callanan24c5fe62016-11-07 20:42:25 +00006946 // For now, map everything down to its file location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00006947 // don't have to import macro expansions.
6948 // FIXME: Import macro expansions!
Sean Callanan24c5fe62016-11-07 20:42:25 +00006949 FromLoc = FromSM.getFileLoc(FromLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00006950 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
6951 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00006952 FileID ToFileID = Import(Decomposed.first);
6953 if (ToFileID.isInvalid())
6954 return SourceLocation();
Sean Callanan59721b32015-04-28 18:41:46 +00006955 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
6956 .getLocWithOffset(Decomposed.second);
6957 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006958}
6959
6960SourceRange ASTImporter::Import(SourceRange FromRange) {
6961 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
6962}
6963
Douglas Gregor811663e2010-02-10 00:15:17 +00006964FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00006965 llvm::DenseMap<FileID, FileID>::iterator Pos
6966 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00006967 if (Pos != ImportedFileIDs.end())
6968 return Pos->second;
6969
6970 SourceManager &FromSM = FromContext.getSourceManager();
6971 SourceManager &ToSM = ToContext.getSourceManager();
6972 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00006973 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00006974
6975 // Include location of this file.
6976 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
6977
6978 // Map the FileID for to the "to" source manager.
6979 FileID ToID;
6980 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan25d34af2015-04-30 00:44:21 +00006981 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
Douglas Gregor811663e2010-02-10 00:15:17 +00006982 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
6983 // disk again
6984 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
6985 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00006986 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Sean Callanan238d8972014-12-10 01:26:39 +00006987 if (!Entry)
6988 return FileID();
Douglas Gregor811663e2010-02-10 00:15:17 +00006989 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
6990 FromSLoc.getFile().getFileCharacteristic());
6991 } else {
6992 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006993 const llvm::MemoryBuffer *
6994 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00006995 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00006996 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00006997 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00006998 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00006999 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00007000 }
7001
7002
Sebastian Redl99219f12010-09-30 01:03:06 +00007003 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00007004 return ToID;
7005}
7006
Sean Callanandd2c1742016-05-16 20:48:03 +00007007CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
7008 Expr *ToExpr = Import(From->getInit());
7009 if (!ToExpr && From->getInit())
7010 return nullptr;
7011
7012 if (From->isBaseInitializer()) {
7013 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
7014 if (!ToTInfo && From->getTypeSourceInfo())
7015 return nullptr;
7016
7017 return new (ToContext) CXXCtorInitializer(
7018 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
7019 ToExpr, Import(From->getRParenLoc()),
7020 From->isPackExpansion() ? Import(From->getEllipsisLoc())
7021 : SourceLocation());
7022 } else if (From->isMemberInitializer()) {
7023 FieldDecl *ToField =
7024 llvm::cast_or_null<FieldDecl>(Import(From->getMember()));
7025 if (!ToField && From->getMember())
7026 return nullptr;
7027
7028 return new (ToContext) CXXCtorInitializer(
7029 ToContext, ToField, Import(From->getMemberLocation()),
7030 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
7031 } else if (From->isIndirectMemberInitializer()) {
7032 IndirectFieldDecl *ToIField = llvm::cast_or_null<IndirectFieldDecl>(
7033 Import(From->getIndirectMember()));
7034 if (!ToIField && From->getIndirectMember())
7035 return nullptr;
7036
7037 return new (ToContext) CXXCtorInitializer(
7038 ToContext, ToIField, Import(From->getMemberLocation()),
7039 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
7040 } else if (From->isDelegatingInitializer()) {
7041 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
7042 if (!ToTInfo && From->getTypeSourceInfo())
7043 return nullptr;
7044
7045 return new (ToContext)
7046 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
7047 ToExpr, Import(From->getRParenLoc()));
7048 } else if (unsigned NumArrayIndices = From->getNumArrayIndices()) {
7049 FieldDecl *ToField =
7050 llvm::cast_or_null<FieldDecl>(Import(From->getMember()));
7051 if (!ToField && From->getMember())
7052 return nullptr;
7053
7054 SmallVector<VarDecl *, 4> ToAIs(NumArrayIndices);
7055
7056 for (unsigned AII = 0; AII < NumArrayIndices; ++AII) {
7057 VarDecl *ToArrayIndex =
7058 dyn_cast_or_null<VarDecl>(Import(From->getArrayIndex(AII)));
7059 if (!ToArrayIndex && From->getArrayIndex(AII))
7060 return nullptr;
7061 }
7062
7063 return CXXCtorInitializer::Create(
7064 ToContext, ToField, Import(From->getMemberLocation()),
7065 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()),
7066 ToAIs.data(), NumArrayIndices);
7067 } else {
7068 return nullptr;
7069 }
7070}
7071
7072
Aleksei Sidorina693b372016-09-28 10:16:56 +00007073CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
7074 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
7075 if (Pos != ImportedCXXBaseSpecifiers.end())
7076 return Pos->second;
7077
7078 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
7079 Import(BaseSpec->getSourceRange()),
7080 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
7081 BaseSpec->getAccessSpecifierAsWritten(),
7082 Import(BaseSpec->getTypeSourceInfo()),
7083 Import(BaseSpec->getEllipsisLoc()));
7084 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
7085 return Imported;
7086}
7087
Douglas Gregor0a791672011-01-18 03:11:38 +00007088void ASTImporter::ImportDefinition(Decl *From) {
7089 Decl *To = Import(From);
7090 if (!To)
7091 return;
7092
7093 if (DeclContext *FromDC = cast<DeclContext>(From)) {
7094 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007095
7096 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
7097 if (!ToRecord->getDefinition()) {
7098 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00007099 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007100 return;
7101 }
7102 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007103
7104 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
7105 if (!ToEnum->getDefinition()) {
7106 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007107 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00007108 return;
7109 }
7110 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00007111
7112 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
7113 if (!ToIFace->getDefinition()) {
7114 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007115 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007116 return;
7117 }
7118 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007119
Douglas Gregor2aa53772012-01-24 17:42:07 +00007120 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
7121 if (!ToProto->getDefinition()) {
7122 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007123 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007124 return;
7125 }
7126 }
7127
Douglas Gregor0a791672011-01-18 03:11:38 +00007128 Importer.ImportDeclContext(FromDC, true);
7129 }
7130}
7131
Douglas Gregor96e578d2010-02-05 17:54:41 +00007132DeclarationName ASTImporter::Import(DeclarationName FromName) {
7133 if (!FromName)
7134 return DeclarationName();
7135
7136 switch (FromName.getNameKind()) {
7137 case DeclarationName::Identifier:
7138 return Import(FromName.getAsIdentifierInfo());
7139
7140 case DeclarationName::ObjCZeroArgSelector:
7141 case DeclarationName::ObjCOneArgSelector:
7142 case DeclarationName::ObjCMultiArgSelector:
7143 return Import(FromName.getObjCSelector());
7144
7145 case DeclarationName::CXXConstructorName: {
7146 QualType T = Import(FromName.getCXXNameType());
7147 if (T.isNull())
7148 return DeclarationName();
7149
7150 return ToContext.DeclarationNames.getCXXConstructorName(
7151 ToContext.getCanonicalType(T));
7152 }
7153
7154 case DeclarationName::CXXDestructorName: {
7155 QualType T = Import(FromName.getCXXNameType());
7156 if (T.isNull())
7157 return DeclarationName();
7158
7159 return ToContext.DeclarationNames.getCXXDestructorName(
7160 ToContext.getCanonicalType(T));
7161 }
7162
7163 case DeclarationName::CXXConversionFunctionName: {
7164 QualType T = Import(FromName.getCXXNameType());
7165 if (T.isNull())
7166 return DeclarationName();
7167
7168 return ToContext.DeclarationNames.getCXXConversionFunctionName(
7169 ToContext.getCanonicalType(T));
7170 }
7171
7172 case DeclarationName::CXXOperatorName:
7173 return ToContext.DeclarationNames.getCXXOperatorName(
7174 FromName.getCXXOverloadedOperator());
7175
7176 case DeclarationName::CXXLiteralOperatorName:
7177 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
7178 Import(FromName.getCXXLiteralIdentifier()));
7179
7180 case DeclarationName::CXXUsingDirective:
7181 // FIXME: STATICS!
7182 return DeclarationName::getUsingDirectiveName();
7183 }
7184
David Blaikiee4d798f2012-01-20 21:50:17 +00007185 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00007186}
7187
Douglas Gregore2e50d332010-12-01 01:36:18 +00007188IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00007189 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00007190 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007191
Sean Callananf94ef1d2016-05-14 06:11:19 +00007192 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
7193
7194 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
7195 ToId->setBuiltinID(FromId->getBuiltinID());
7196
7197 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007198}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007199
Douglas Gregor43f54792010-02-17 02:12:47 +00007200Selector ASTImporter::Import(Selector FromSel) {
7201 if (FromSel.isNull())
7202 return Selector();
7203
Chris Lattner0e62c1c2011-07-23 10:55:15 +00007204 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00007205 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
7206 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
7207 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
7208 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
7209}
7210
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007211DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
7212 DeclContext *DC,
7213 unsigned IDNS,
7214 NamedDecl **Decls,
7215 unsigned NumDecls) {
7216 return Name;
7217}
7218
7219DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007220 if (LastDiagFromFrom)
7221 ToContext.getDiagnostics().notePriorDiagnosticFrom(
7222 FromContext.getDiagnostics());
7223 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007224 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007225}
7226
7227DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007228 if (!LastDiagFromFrom)
7229 FromContext.getDiagnostics().notePriorDiagnosticFrom(
7230 ToContext.getDiagnostics());
7231 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007232 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007233}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007234
Douglas Gregor2e15c842012-02-01 21:00:38 +00007235void ASTImporter::CompleteDecl (Decl *D) {
7236 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
7237 if (!ID->getDefinition())
7238 ID->startDefinition();
7239 }
7240 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
7241 if (!PD->getDefinition())
7242 PD->startDefinition();
7243 }
7244 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
7245 if (!TD->getDefinition() && !TD->isBeingDefined()) {
7246 TD->startDefinition();
7247 TD->setCompleteDefinition(true);
7248 }
7249 }
7250 else {
7251 assert (0 && "CompleteDecl called on a Decl that can't be completed");
7252 }
7253}
7254
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007255Decl *ASTImporter::Imported(Decl *From, Decl *To) {
Sean Callanan8bca9962016-03-28 21:43:01 +00007256 if (From->hasAttrs()) {
7257 for (Attr *FromAttr : From->getAttrs())
7258 To->addAttr(FromAttr->clone(To->getASTContext()));
7259 }
7260 if (From->isUsed()) {
7261 To->setIsUsed();
7262 }
Sean Callanandd2c1742016-05-16 20:48:03 +00007263 if (From->isImplicit()) {
7264 To->setImplicit();
7265 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007266 ImportedDecls[From] = To;
7267 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00007268}
Douglas Gregorb4964f72010-02-15 23:54:17 +00007269
Douglas Gregordd6006f2012-07-17 21:16:27 +00007270bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
7271 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00007272 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00007273 = ImportedTypes.find(From.getTypePtr());
7274 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
7275 return true;
7276
Douglas Gregordd6006f2012-07-17 21:16:27 +00007277 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
7278 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00007279 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00007280}