blob: 7d241bbbfbf8bb597d86d73bd1db5c29f3605ca4 [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;
32
33 public:
34 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
35
36 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor62d311f2010-02-09 19:21:46 +000037 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor7eeb5972010-02-11 19:21:55 +000038 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +000039
40 // Importing types
John McCall424cec92011-01-19 06:33:43 +000041 QualType VisitType(const Type *T);
42 QualType VisitBuiltinType(const BuiltinType *T);
43 QualType VisitComplexType(const ComplexType *T);
44 QualType VisitPointerType(const PointerType *T);
45 QualType VisitBlockPointerType(const BlockPointerType *T);
46 QualType VisitLValueReferenceType(const LValueReferenceType *T);
47 QualType VisitRValueReferenceType(const RValueReferenceType *T);
48 QualType VisitMemberPointerType(const MemberPointerType *T);
49 QualType VisitConstantArrayType(const ConstantArrayType *T);
50 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
51 QualType VisitVariableArrayType(const VariableArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000052 // FIXME: DependentSizedArrayType
53 // FIXME: DependentSizedExtVectorType
John McCall424cec92011-01-19 06:33:43 +000054 QualType VisitVectorType(const VectorType *T);
55 QualType VisitExtVectorType(const ExtVectorType *T);
56 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
57 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000058 // FIXME: UnresolvedUsingType
Sean Callananda6df8a2011-08-11 16:56:07 +000059 QualType VisitParenType(const ParenType *T);
John McCall424cec92011-01-19 06:33:43 +000060 QualType VisitTypedefType(const TypedefType *T);
61 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000062 // FIXME: DependentTypeOfExprType
John McCall424cec92011-01-19 06:33:43 +000063 QualType VisitTypeOfType(const TypeOfType *T);
64 QualType VisitDecltypeType(const DecltypeType *T);
Alexis Hunte852b102011-05-24 22:41:36 +000065 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith30482bc2011-02-20 03:19:35 +000066 QualType VisitAutoType(const AutoType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000067 // FIXME: DependentDecltypeType
John McCall424cec92011-01-19 06:33:43 +000068 QualType VisitRecordType(const RecordType *T);
69 QualType VisitEnumType(const EnumType *T);
Sean Callanan72fe0852015-04-02 23:50:08 +000070 QualType VisitAttributedType(const AttributedType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000071 // FIXME: TemplateTypeParmType
72 // FIXME: SubstTemplateTypeParmType
John McCall424cec92011-01-19 06:33:43 +000073 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
74 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +000075 // FIXME: DependentNameType
John McCallc392f372010-06-11 00:33:02 +000076 // FIXME: DependentTemplateSpecializationType
John McCall424cec92011-01-19 06:33:43 +000077 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
78 QualType VisitObjCObjectType(const ObjCObjectType *T);
79 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000080
Douglas Gregor95d82832012-01-24 18:36:04 +000081 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +000082 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
83 DeclContext *&LexicalDC, DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +000084 NamedDecl *&ToD, SourceLocation &Loc);
Craig Topper36250ad2014-05-12 05:36:57 +000085 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +000086 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
87 DeclarationNameInfo& To);
Douglas Gregor0a791672011-01-18 03:11:38 +000088 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Douglas Gregor2e15c842012-02-01 21:00:38 +000089
Douglas Gregor95d82832012-01-24 18:36:04 +000090 /// \brief What we should import from the definition.
91 enum ImportDefinitionKind {
92 /// \brief Import the default subset of the definition, which might be
93 /// nothing (if minimal import is set) or might be everything (if minimal
94 /// import is not set).
95 IDK_Default,
96 /// \brief Import everything.
97 IDK_Everything,
98 /// \brief Import only the bare bones needed to establish a valid
99 /// DeclContext.
100 IDK_Basic
101 };
102
Douglas Gregor2e15c842012-02-01 21:00:38 +0000103 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
104 return IDK == IDK_Everything ||
105 (IDK == IDK_Default && !Importer.isMinimalImport());
106 }
107
Douglas Gregord451ea92011-07-29 23:31:30 +0000108 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000109 ImportDefinitionKind Kind = IDK_Default);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000110 bool ImportDefinition(VarDecl *From, VarDecl *To,
111 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregord451ea92011-07-29 23:31:30 +0000112 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000113 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000114 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000115 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000116 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000117 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregora082a492010-11-30 19:14:50 +0000118 TemplateParameterList *ImportTemplateParameterList(
119 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000120 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
121 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
122 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000123 SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000124 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
125 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000126 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
127 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000128 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000129 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Douglas Gregora082a492010-11-30 19:14:50 +0000130 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000131 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000132 Decl *VisitDecl(Decl *D);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +0000133 Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000134 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000135 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000136 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000137 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000138 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000139 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000140 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000141 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000142 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000143 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
144 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
145 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
146 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000147 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000148 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000149 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000150 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000151 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000152 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000153 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000154 Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000155 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000156 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Sean Callanan0aae0412014-12-10 00:00:37 +0000157 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000158
159 ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list);
Douglas Gregor45635322010-02-16 01:20:57 +0000160 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000161 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000162 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000163 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000164 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000165 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
166 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
167 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
168 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000169 Decl *VisitClassTemplateSpecializationDecl(
170 ClassTemplateSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000171 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
172 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
173
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000174 // Importing statements
Sean Callanan59721b32015-04-28 18:41:46 +0000175 DeclGroupRef ImportDeclGroup(DeclGroupRef DG);
176
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000177 Stmt *VisitStmt(Stmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000178 Stmt *VisitDeclStmt(DeclStmt *S);
179 Stmt *VisitNullStmt(NullStmt *S);
180 Stmt *VisitCompoundStmt(CompoundStmt *S);
181 Stmt *VisitCaseStmt(CaseStmt *S);
182 Stmt *VisitDefaultStmt(DefaultStmt *S);
183 Stmt *VisitLabelStmt(LabelStmt *S);
184 Stmt *VisitAttributedStmt(AttributedStmt *S);
185 Stmt *VisitIfStmt(IfStmt *S);
186 Stmt *VisitSwitchStmt(SwitchStmt *S);
187 Stmt *VisitWhileStmt(WhileStmt *S);
188 Stmt *VisitDoStmt(DoStmt *S);
189 Stmt *VisitForStmt(ForStmt *S);
190 Stmt *VisitGotoStmt(GotoStmt *S);
191 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S);
192 Stmt *VisitContinueStmt(ContinueStmt *S);
193 Stmt *VisitBreakStmt(BreakStmt *S);
194 Stmt *VisitReturnStmt(ReturnStmt *S);
195 // FIXME: GCCAsmStmt
196 // FIXME: MSAsmStmt
197 // FIXME: SEHExceptStmt
198 // FIXME: SEHFinallyStmt
199 // FIXME: SEHTryStmt
200 // FIXME: SEHLeaveStmt
201 // FIXME: CapturedStmt
202 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S);
203 Stmt *VisitCXXTryStmt(CXXTryStmt *S);
204 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S);
205 // FIXME: MSDependentExistsStmt
206 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
207 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
208 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
209 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
210 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
211 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
212 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000213
214 // Importing expressions
215 Expr *VisitExpr(Expr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000216 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000217 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000218 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000219 Expr *VisitParenExpr(ParenExpr *E);
220 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000221 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000222 Expr *VisitBinaryOperator(BinaryOperator *E);
223 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000224 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor5481d322010-02-19 01:32:14 +0000225 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000226 Expr *VisitCXXConstructExpr(CXXConstructExpr *E);
227 Expr *VisitMemberExpr(MemberExpr *E);
228 Expr *VisitCallExpr(CallExpr *E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000229 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000230}
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000231using namespace clang;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000232
233//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000234// Structural Equivalence
235//----------------------------------------------------------------------------
236
237namespace {
238 struct StructuralEquivalenceContext {
239 /// \brief AST contexts for which we are checking structural equivalence.
240 ASTContext &C1, &C2;
241
Douglas Gregor3996e242010-02-15 22:01:00 +0000242 /// \brief The set of "tentative" equivalences between two canonical
243 /// declarations, mapping from a declaration in the first context to the
244 /// declaration in the second context that we believe to be equivalent.
245 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
246
247 /// \brief Queue of declarations in the first context whose equivalence
248 /// with a declaration in the second context still needs to be verified.
249 std::deque<Decl *> DeclsToCheck;
250
Douglas Gregorb4964f72010-02-15 23:54:17 +0000251 /// \brief Declaration (from, to) pairs that are known not to be equivalent
252 /// (which we have already complained about).
253 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
254
Douglas Gregor3996e242010-02-15 22:01:00 +0000255 /// \brief Whether we're being strict about the spelling of types when
256 /// unifying two types.
257 bool StrictTypeSpelling;
Douglas Gregordd6006f2012-07-17 21:16:27 +0000258
259 /// \brief Whether to complain about failures.
260 bool Complain;
261
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000262 /// \brief \c true if the last diagnostic came from C2.
263 bool LastDiagFromC2;
264
Douglas Gregor3996e242010-02-15 22:01:00 +0000265 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000266 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregordd6006f2012-07-17 21:16:27 +0000267 bool StrictTypeSpelling = false,
268 bool Complain = true)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000269 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000270 StrictTypeSpelling(StrictTypeSpelling), Complain(Complain),
271 LastDiagFromC2(false) {}
Douglas Gregor3996e242010-02-15 22:01:00 +0000272
273 /// \brief Determine whether the two declarations are structurally
274 /// equivalent.
275 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
276
277 /// \brief Determine whether the two types are structurally equivalent.
278 bool IsStructurallyEquivalent(QualType T1, QualType T2);
279
280 private:
281 /// \brief Finish checking all of the structural equivalences.
282 ///
283 /// \returns true if an error occurred, false otherwise.
284 bool Finish();
285
286 public:
287 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000288 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000289 if (LastDiagFromC2)
290 C1.getDiagnostics().notePriorDiagnosticFrom(C2.getDiagnostics());
291 LastDiagFromC2 = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000292 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000293 }
294
295 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000296 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000297 if (!LastDiagFromC2)
298 C2.getDiagnostics().notePriorDiagnosticFrom(C1.getDiagnostics());
299 LastDiagFromC2 = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000300 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000301 }
302 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000303}
Douglas Gregor3996e242010-02-15 22:01:00 +0000304
305static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
306 QualType T1, QualType T2);
307static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
308 Decl *D1, Decl *D2);
309
Douglas Gregor3996e242010-02-15 22:01:00 +0000310/// \brief Determine structural equivalence of two expressions.
311static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
312 Expr *E1, Expr *E2) {
313 if (!E1 || !E2)
314 return E1 == E2;
315
316 // FIXME: Actually perform a structural comparison!
317 return true;
318}
319
320/// \brief Determine whether two identifiers are equivalent.
321static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
322 const IdentifierInfo *Name2) {
323 if (!Name1 || !Name2)
324 return Name1 == Name2;
325
326 return Name1->getName() == Name2->getName();
327}
328
329/// \brief Determine whether two nested-name-specifiers are equivalent.
330static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
331 NestedNameSpecifier *NNS1,
332 NestedNameSpecifier *NNS2) {
333 // FIXME: Implement!
334 return true;
335}
336
337/// \brief Determine whether two template arguments are equivalent.
338static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
339 const TemplateArgument &Arg1,
340 const TemplateArgument &Arg2) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000341 if (Arg1.getKind() != Arg2.getKind())
342 return false;
343
344 switch (Arg1.getKind()) {
345 case TemplateArgument::Null:
346 return true;
347
348 case TemplateArgument::Type:
349 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
Eli Friedmanb826a002012-09-26 02:36:12 +0000350
Douglas Gregore2e50d332010-12-01 01:36:18 +0000351 case TemplateArgument::Integral:
352 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
353 Arg2.getIntegralType()))
354 return false;
355
Eric Christopher6dcc3762012-07-15 00:23:57 +0000356 return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral());
Douglas Gregore2e50d332010-12-01 01:36:18 +0000357
358 case TemplateArgument::Declaration:
359 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +0000360
361 case TemplateArgument::NullPtr:
362 return true; // FIXME: Is this correct?
363
Douglas Gregore2e50d332010-12-01 01:36:18 +0000364 case TemplateArgument::Template:
365 return IsStructurallyEquivalent(Context,
366 Arg1.getAsTemplate(),
367 Arg2.getAsTemplate());
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000368
369 case TemplateArgument::TemplateExpansion:
370 return IsStructurallyEquivalent(Context,
371 Arg1.getAsTemplateOrTemplatePattern(),
372 Arg2.getAsTemplateOrTemplatePattern());
373
Douglas Gregore2e50d332010-12-01 01:36:18 +0000374 case TemplateArgument::Expression:
375 return IsStructurallyEquivalent(Context,
376 Arg1.getAsExpr(), Arg2.getAsExpr());
377
378 case TemplateArgument::Pack:
379 if (Arg1.pack_size() != Arg2.pack_size())
380 return false;
381
382 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
383 if (!IsStructurallyEquivalent(Context,
384 Arg1.pack_begin()[I],
385 Arg2.pack_begin()[I]))
386 return false;
387
388 return true;
389 }
390
391 llvm_unreachable("Invalid template argument kind");
Douglas Gregor3996e242010-02-15 22:01:00 +0000392}
393
394/// \brief Determine structural equivalence for the common part of array
395/// types.
396static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
397 const ArrayType *Array1,
398 const ArrayType *Array2) {
399 if (!IsStructurallyEquivalent(Context,
400 Array1->getElementType(),
401 Array2->getElementType()))
402 return false;
403 if (Array1->getSizeModifier() != Array2->getSizeModifier())
404 return false;
405 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
406 return false;
407
408 return true;
409}
410
411/// \brief Determine structural equivalence of two types.
412static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
413 QualType T1, QualType T2) {
414 if (T1.isNull() || T2.isNull())
415 return T1.isNull() && T2.isNull();
416
417 if (!Context.StrictTypeSpelling) {
418 // We aren't being strict about token-to-token equivalence of types,
419 // so map down to the canonical type.
420 T1 = Context.C1.getCanonicalType(T1);
421 T2 = Context.C2.getCanonicalType(T2);
422 }
423
424 if (T1.getQualifiers() != T2.getQualifiers())
425 return false;
426
Douglas Gregorb4964f72010-02-15 23:54:17 +0000427 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000428
Douglas Gregorb4964f72010-02-15 23:54:17 +0000429 if (T1->getTypeClass() != T2->getTypeClass()) {
430 // Compare function types with prototypes vs. without prototypes as if
431 // both did not have prototypes.
432 if (T1->getTypeClass() == Type::FunctionProto &&
433 T2->getTypeClass() == Type::FunctionNoProto)
434 TC = Type::FunctionNoProto;
435 else if (T1->getTypeClass() == Type::FunctionNoProto &&
436 T2->getTypeClass() == Type::FunctionProto)
437 TC = Type::FunctionNoProto;
438 else
439 return false;
440 }
441
442 switch (TC) {
443 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000444 // FIXME: Deal with Char_S/Char_U.
445 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
446 return false;
447 break;
448
449 case Type::Complex:
450 if (!IsStructurallyEquivalent(Context,
451 cast<ComplexType>(T1)->getElementType(),
452 cast<ComplexType>(T2)->getElementType()))
453 return false;
454 break;
455
Reid Kleckner0503a872013-12-05 01:23:43 +0000456 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +0000457 case Type::Decayed:
458 if (!IsStructurallyEquivalent(Context,
Reid Kleckner0503a872013-12-05 01:23:43 +0000459 cast<AdjustedType>(T1)->getOriginalType(),
460 cast<AdjustedType>(T2)->getOriginalType()))
Reid Kleckner8a365022013-06-24 17:51:48 +0000461 return false;
462 break;
463
Douglas Gregor3996e242010-02-15 22:01:00 +0000464 case Type::Pointer:
465 if (!IsStructurallyEquivalent(Context,
466 cast<PointerType>(T1)->getPointeeType(),
467 cast<PointerType>(T2)->getPointeeType()))
468 return false;
469 break;
470
471 case Type::BlockPointer:
472 if (!IsStructurallyEquivalent(Context,
473 cast<BlockPointerType>(T1)->getPointeeType(),
474 cast<BlockPointerType>(T2)->getPointeeType()))
475 return false;
476 break;
477
478 case Type::LValueReference:
479 case Type::RValueReference: {
480 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
481 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
482 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
483 return false;
484 if (Ref1->isInnerRef() != Ref2->isInnerRef())
485 return false;
486 if (!IsStructurallyEquivalent(Context,
487 Ref1->getPointeeTypeAsWritten(),
488 Ref2->getPointeeTypeAsWritten()))
489 return false;
490 break;
491 }
492
493 case Type::MemberPointer: {
494 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
495 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
496 if (!IsStructurallyEquivalent(Context,
497 MemPtr1->getPointeeType(),
498 MemPtr2->getPointeeType()))
499 return false;
500 if (!IsStructurallyEquivalent(Context,
501 QualType(MemPtr1->getClass(), 0),
502 QualType(MemPtr2->getClass(), 0)))
503 return false;
504 break;
505 }
506
507 case Type::ConstantArray: {
508 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
509 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
Eric Christopher6dcc3762012-07-15 00:23:57 +0000510 if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000511 return false;
512
513 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
514 return false;
515 break;
516 }
517
518 case Type::IncompleteArray:
519 if (!IsArrayStructurallyEquivalent(Context,
520 cast<ArrayType>(T1),
521 cast<ArrayType>(T2)))
522 return false;
523 break;
524
525 case Type::VariableArray: {
526 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
527 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
528 if (!IsStructurallyEquivalent(Context,
529 Array1->getSizeExpr(), Array2->getSizeExpr()))
530 return false;
531
532 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
533 return false;
534
535 break;
536 }
537
538 case Type::DependentSizedArray: {
539 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
540 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
541 if (!IsStructurallyEquivalent(Context,
542 Array1->getSizeExpr(), Array2->getSizeExpr()))
543 return false;
544
545 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
546 return false;
547
548 break;
549 }
550
551 case Type::DependentSizedExtVector: {
552 const DependentSizedExtVectorType *Vec1
553 = cast<DependentSizedExtVectorType>(T1);
554 const DependentSizedExtVectorType *Vec2
555 = cast<DependentSizedExtVectorType>(T2);
556 if (!IsStructurallyEquivalent(Context,
557 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
558 return false;
559 if (!IsStructurallyEquivalent(Context,
560 Vec1->getElementType(),
561 Vec2->getElementType()))
562 return false;
563 break;
564 }
565
566 case Type::Vector:
567 case Type::ExtVector: {
568 const VectorType *Vec1 = cast<VectorType>(T1);
569 const VectorType *Vec2 = cast<VectorType>(T2);
570 if (!IsStructurallyEquivalent(Context,
571 Vec1->getElementType(),
572 Vec2->getElementType()))
573 return false;
574 if (Vec1->getNumElements() != Vec2->getNumElements())
575 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000576 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000577 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000578 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000579 }
580
581 case Type::FunctionProto: {
582 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
583 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
Alp Toker9cacbab2014-01-20 20:26:09 +0000584 if (Proto1->getNumParams() != Proto2->getNumParams())
Douglas Gregor3996e242010-02-15 22:01:00 +0000585 return false;
Alp Toker9cacbab2014-01-20 20:26:09 +0000586 for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) {
587 if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I),
588 Proto2->getParamType(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000589 return false;
590 }
591 if (Proto1->isVariadic() != Proto2->isVariadic())
592 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000593 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor3996e242010-02-15 22:01:00 +0000594 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000595 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
596 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
597 return false;
598 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
599 if (!IsStructurallyEquivalent(Context,
600 Proto1->getExceptionType(I),
601 Proto2->getExceptionType(I)))
602 return false;
603 }
604 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000605 if (!IsStructurallyEquivalent(Context,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000606 Proto1->getNoexceptExpr(),
607 Proto2->getNoexceptExpr()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000608 return false;
609 }
610 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
611 return false;
612
613 // Fall through to check the bits common with FunctionNoProtoType.
614 }
615
616 case Type::FunctionNoProto: {
617 const FunctionType *Function1 = cast<FunctionType>(T1);
618 const FunctionType *Function2 = cast<FunctionType>(T2);
Alp Toker314cc812014-01-25 16:55:45 +0000619 if (!IsStructurallyEquivalent(Context, Function1->getReturnType(),
620 Function2->getReturnType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000621 return false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000622 if (Function1->getExtInfo() != Function2->getExtInfo())
623 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000624 break;
625 }
626
627 case Type::UnresolvedUsing:
628 if (!IsStructurallyEquivalent(Context,
629 cast<UnresolvedUsingType>(T1)->getDecl(),
630 cast<UnresolvedUsingType>(T2)->getDecl()))
631 return false;
632
633 break;
John McCall81904512011-01-06 01:58:22 +0000634
635 case Type::Attributed:
636 if (!IsStructurallyEquivalent(Context,
637 cast<AttributedType>(T1)->getModifiedType(),
638 cast<AttributedType>(T2)->getModifiedType()))
639 return false;
640 if (!IsStructurallyEquivalent(Context,
641 cast<AttributedType>(T1)->getEquivalentType(),
642 cast<AttributedType>(T2)->getEquivalentType()))
643 return false;
644 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000645
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000646 case Type::Paren:
647 if (!IsStructurallyEquivalent(Context,
648 cast<ParenType>(T1)->getInnerType(),
649 cast<ParenType>(T2)->getInnerType()))
650 return false;
651 break;
652
Douglas Gregor3996e242010-02-15 22:01:00 +0000653 case Type::Typedef:
654 if (!IsStructurallyEquivalent(Context,
655 cast<TypedefType>(T1)->getDecl(),
656 cast<TypedefType>(T2)->getDecl()))
657 return false;
658 break;
659
660 case Type::TypeOfExpr:
661 if (!IsStructurallyEquivalent(Context,
662 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
663 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
664 return false;
665 break;
666
667 case Type::TypeOf:
668 if (!IsStructurallyEquivalent(Context,
669 cast<TypeOfType>(T1)->getUnderlyingType(),
670 cast<TypeOfType>(T2)->getUnderlyingType()))
671 return false;
672 break;
Alexis Hunte852b102011-05-24 22:41:36 +0000673
674 case Type::UnaryTransform:
675 if (!IsStructurallyEquivalent(Context,
676 cast<UnaryTransformType>(T1)->getUnderlyingType(),
677 cast<UnaryTransformType>(T1)->getUnderlyingType()))
678 return false;
679 break;
680
Douglas Gregor3996e242010-02-15 22:01:00 +0000681 case Type::Decltype:
682 if (!IsStructurallyEquivalent(Context,
683 cast<DecltypeType>(T1)->getUnderlyingExpr(),
684 cast<DecltypeType>(T2)->getUnderlyingExpr()))
685 return false;
686 break;
687
Richard Smith30482bc2011-02-20 03:19:35 +0000688 case Type::Auto:
689 if (!IsStructurallyEquivalent(Context,
690 cast<AutoType>(T1)->getDeducedType(),
691 cast<AutoType>(T2)->getDeducedType()))
692 return false;
693 break;
694
Douglas Gregor3996e242010-02-15 22:01:00 +0000695 case Type::Record:
696 case Type::Enum:
697 if (!IsStructurallyEquivalent(Context,
698 cast<TagType>(T1)->getDecl(),
699 cast<TagType>(T2)->getDecl()))
700 return false;
701 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000702
Douglas Gregor3996e242010-02-15 22:01:00 +0000703 case Type::TemplateTypeParm: {
704 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
705 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
706 if (Parm1->getDepth() != Parm2->getDepth())
707 return false;
708 if (Parm1->getIndex() != Parm2->getIndex())
709 return false;
710 if (Parm1->isParameterPack() != Parm2->isParameterPack())
711 return false;
712
713 // Names of template type parameters are never significant.
714 break;
715 }
716
717 case Type::SubstTemplateTypeParm: {
718 const SubstTemplateTypeParmType *Subst1
719 = cast<SubstTemplateTypeParmType>(T1);
720 const SubstTemplateTypeParmType *Subst2
721 = cast<SubstTemplateTypeParmType>(T2);
722 if (!IsStructurallyEquivalent(Context,
723 QualType(Subst1->getReplacedParameter(), 0),
724 QualType(Subst2->getReplacedParameter(), 0)))
725 return false;
726 if (!IsStructurallyEquivalent(Context,
727 Subst1->getReplacementType(),
728 Subst2->getReplacementType()))
729 return false;
730 break;
731 }
732
Douglas Gregorfb322d82011-01-14 05:11:40 +0000733 case Type::SubstTemplateTypeParmPack: {
734 const SubstTemplateTypeParmPackType *Subst1
735 = cast<SubstTemplateTypeParmPackType>(T1);
736 const SubstTemplateTypeParmPackType *Subst2
737 = cast<SubstTemplateTypeParmPackType>(T2);
738 if (!IsStructurallyEquivalent(Context,
739 QualType(Subst1->getReplacedParameter(), 0),
740 QualType(Subst2->getReplacedParameter(), 0)))
741 return false;
742 if (!IsStructurallyEquivalent(Context,
743 Subst1->getArgumentPack(),
744 Subst2->getArgumentPack()))
745 return false;
746 break;
747 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000748 case Type::TemplateSpecialization: {
749 const TemplateSpecializationType *Spec1
750 = cast<TemplateSpecializationType>(T1);
751 const TemplateSpecializationType *Spec2
752 = cast<TemplateSpecializationType>(T2);
753 if (!IsStructurallyEquivalent(Context,
754 Spec1->getTemplateName(),
755 Spec2->getTemplateName()))
756 return false;
757 if (Spec1->getNumArgs() != Spec2->getNumArgs())
758 return false;
759 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
760 if (!IsStructurallyEquivalent(Context,
761 Spec1->getArg(I), Spec2->getArg(I)))
762 return false;
763 }
764 break;
765 }
766
Abramo Bagnara6150c882010-05-11 21:36:43 +0000767 case Type::Elaborated: {
768 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
769 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
770 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
771 if (Elab1->getKeyword() != Elab2->getKeyword())
772 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000773 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000774 Elab1->getQualifier(),
775 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000776 return false;
777 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000778 Elab1->getNamedType(),
779 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000780 return false;
781 break;
782 }
783
John McCalle78aac42010-03-10 03:28:59 +0000784 case Type::InjectedClassName: {
785 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
786 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
787 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000788 Inj1->getInjectedSpecializationType(),
789 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000790 return false;
791 break;
792 }
793
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000794 case Type::DependentName: {
795 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
796 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000797 if (!IsStructurallyEquivalent(Context,
798 Typename1->getQualifier(),
799 Typename2->getQualifier()))
800 return false;
801 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
802 Typename2->getIdentifier()))
803 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000804
805 break;
806 }
807
John McCallc392f372010-06-11 00:33:02 +0000808 case Type::DependentTemplateSpecialization: {
809 const DependentTemplateSpecializationType *Spec1 =
810 cast<DependentTemplateSpecializationType>(T1);
811 const DependentTemplateSpecializationType *Spec2 =
812 cast<DependentTemplateSpecializationType>(T2);
813 if (!IsStructurallyEquivalent(Context,
814 Spec1->getQualifier(),
815 Spec2->getQualifier()))
816 return false;
817 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
818 Spec2->getIdentifier()))
819 return false;
820 if (Spec1->getNumArgs() != Spec2->getNumArgs())
821 return false;
822 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
823 if (!IsStructurallyEquivalent(Context,
824 Spec1->getArg(I), Spec2->getArg(I)))
825 return false;
826 }
827 break;
828 }
Douglas Gregord2fa7662010-12-20 02:24:11 +0000829
830 case Type::PackExpansion:
831 if (!IsStructurallyEquivalent(Context,
832 cast<PackExpansionType>(T1)->getPattern(),
833 cast<PackExpansionType>(T2)->getPattern()))
834 return false;
835 break;
836
Douglas Gregor3996e242010-02-15 22:01:00 +0000837 case Type::ObjCInterface: {
838 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
839 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
840 if (!IsStructurallyEquivalent(Context,
841 Iface1->getDecl(), Iface2->getDecl()))
842 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000843 break;
844 }
845
846 case Type::ObjCObject: {
847 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
848 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
849 if (!IsStructurallyEquivalent(Context,
850 Obj1->getBaseType(),
851 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000852 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000853 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
854 return false;
855 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000856 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000857 Obj1->getProtocol(I),
858 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000859 return false;
860 }
861 break;
862 }
863
864 case Type::ObjCObjectPointer: {
865 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
866 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
867 if (!IsStructurallyEquivalent(Context,
868 Ptr1->getPointeeType(),
869 Ptr2->getPointeeType()))
870 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000871 break;
872 }
Eli Friedman0dfb8892011-10-06 23:00:33 +0000873
874 case Type::Atomic: {
875 if (!IsStructurallyEquivalent(Context,
876 cast<AtomicType>(T1)->getValueType(),
877 cast<AtomicType>(T2)->getValueType()))
878 return false;
879 break;
880 }
881
Xiuli Pan9c14e282016-01-09 12:53:17 +0000882 case Type::Pipe: {
883 if (!IsStructurallyEquivalent(Context,
884 cast<PipeType>(T1)->getElementType(),
885 cast<PipeType>(T2)->getElementType()))
886 return false;
887 break;
888 }
889
Douglas Gregor3996e242010-02-15 22:01:00 +0000890 } // end switch
891
892 return true;
893}
894
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000895/// \brief Determine structural equivalence of two fields.
896static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
897 FieldDecl *Field1, FieldDecl *Field2) {
898 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000899
900 // For anonymous structs/unions, match up the anonymous struct/union type
901 // declarations directly, so that we don't go off searching for anonymous
902 // types
903 if (Field1->isAnonymousStructOrUnion() &&
904 Field2->isAnonymousStructOrUnion()) {
905 RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl();
906 RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl();
907 return IsStructurallyEquivalent(Context, D1, D2);
908 }
Sean Callanan969c5bd2013-04-26 22:49:25 +0000909
910 // Check for equivalent field names.
911 IdentifierInfo *Name1 = Field1->getIdentifier();
912 IdentifierInfo *Name2 = Field2->getIdentifier();
913 if (!::IsStructurallyEquivalent(Name1, Name2))
914 return false;
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000915
916 if (!IsStructurallyEquivalent(Context,
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000917 Field1->getType(), Field2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000918 if (Context.Complain) {
919 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
920 << Context.C2.getTypeDeclType(Owner2);
921 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
922 << Field2->getDeclName() << Field2->getType();
923 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
924 << Field1->getDeclName() << Field1->getType();
925 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000926 return false;
927 }
928
929 if (Field1->isBitField() != Field2->isBitField()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000930 if (Context.Complain) {
931 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
932 << Context.C2.getTypeDeclType(Owner2);
933 if (Field1->isBitField()) {
934 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
935 << Field1->getDeclName() << Field1->getType()
936 << Field1->getBitWidthValue(Context.C1);
937 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
938 << Field2->getDeclName();
939 } else {
940 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
941 << Field2->getDeclName() << Field2->getType()
942 << Field2->getBitWidthValue(Context.C2);
943 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
944 << Field1->getDeclName();
945 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000946 }
947 return false;
948 }
949
950 if (Field1->isBitField()) {
951 // Make sure that the bit-fields are the same length.
952 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
953 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
954
955 if (Bits1 != Bits2) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000956 if (Context.Complain) {
957 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
958 << Context.C2.getTypeDeclType(Owner2);
959 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
960 << Field2->getDeclName() << Field2->getType() << Bits2;
961 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
962 << Field1->getDeclName() << Field1->getType() << Bits1;
963 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000964 return false;
965 }
966 }
967
968 return true;
969}
970
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000971/// \brief Find the index of the given anonymous struct/union within its
972/// context.
973///
974/// \returns Returns the index of this anonymous struct/union in its context,
975/// including the next assigned index (if none of them match). Returns an
976/// empty option if the context is not a record, i.e.. if the anonymous
977/// struct/union is at namespace or block scope.
David Blaikie05785d12013-02-20 22:23:23 +0000978static Optional<unsigned> findAnonymousStructOrUnionIndex(RecordDecl *Anon) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000979 ASTContext &Context = Anon->getASTContext();
980 QualType AnonTy = Context.getRecordType(Anon);
981
982 RecordDecl *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext());
983 if (!Owner)
David Blaikie7a30dc52013-02-21 01:47:18 +0000984 return None;
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000985
986 unsigned Index = 0;
Aaron Ballman629afae2014-03-07 19:56:05 +0000987 for (const auto *D : Owner->noload_decls()) {
988 const auto *F = dyn_cast<FieldDecl>(D);
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000989 if (!F || !F->isAnonymousStructOrUnion())
990 continue;
991
992 if (Context.hasSameType(F->getType(), AnonTy))
993 break;
994
995 ++Index;
996 }
997
998 return Index;
999}
1000
Douglas Gregor3996e242010-02-15 22:01:00 +00001001/// \brief Determine structural equivalence of two records.
1002static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1003 RecordDecl *D1, RecordDecl *D2) {
1004 if (D1->isUnion() != D2->isUnion()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001005 if (Context.Complain) {
1006 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1007 << Context.C2.getTypeDeclType(D2);
1008 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
1009 << D1->getDeclName() << (unsigned)D1->getTagKind();
1010 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001011 return false;
1012 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001013
1014 if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) {
1015 // If both anonymous structs/unions are in a record context, make sure
1016 // they occur in the same location in the context records.
David Blaikie05785d12013-02-20 22:23:23 +00001017 if (Optional<unsigned> Index1 = findAnonymousStructOrUnionIndex(D1)) {
1018 if (Optional<unsigned> Index2 = findAnonymousStructOrUnionIndex(D2)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001019 if (*Index1 != *Index2)
1020 return false;
1021 }
1022 }
1023 }
1024
Douglas Gregore2e50d332010-12-01 01:36:18 +00001025 // If both declarations are class template specializations, we know
1026 // the ODR applies, so check the template and template arguments.
1027 ClassTemplateSpecializationDecl *Spec1
1028 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
1029 ClassTemplateSpecializationDecl *Spec2
1030 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
1031 if (Spec1 && Spec2) {
1032 // Check that the specialized templates are the same.
1033 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
1034 Spec2->getSpecializedTemplate()))
1035 return false;
1036
1037 // Check that the template arguments are the same.
1038 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
1039 return false;
1040
1041 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
1042 if (!IsStructurallyEquivalent(Context,
1043 Spec1->getTemplateArgs().get(I),
1044 Spec2->getTemplateArgs().get(I)))
1045 return false;
1046 }
1047 // If one is a class template specialization and the other is not, these
Chris Lattner57540c52011-04-15 05:22:18 +00001048 // structures are different.
Douglas Gregore2e50d332010-12-01 01:36:18 +00001049 else if (Spec1 || Spec2)
1050 return false;
1051
Douglas Gregorb4964f72010-02-15 23:54:17 +00001052 // Compare the definitions of these two records. If either or both are
1053 // incomplete, we assume that they are equivalent.
1054 D1 = D1->getDefinition();
1055 D2 = D2->getDefinition();
1056 if (!D1 || !D2)
1057 return true;
1058
Douglas Gregor3996e242010-02-15 22:01:00 +00001059 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
1060 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
1061 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001062 if (Context.Complain) {
1063 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1064 << Context.C2.getTypeDeclType(D2);
1065 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
1066 << D2CXX->getNumBases();
1067 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
1068 << D1CXX->getNumBases();
1069 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001070 return false;
1071 }
1072
1073 // Check the base classes.
1074 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
1075 BaseEnd1 = D1CXX->bases_end(),
1076 Base2 = D2CXX->bases_begin();
1077 Base1 != BaseEnd1;
1078 ++Base1, ++Base2) {
1079 if (!IsStructurallyEquivalent(Context,
1080 Base1->getType(), Base2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001081 if (Context.Complain) {
1082 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1083 << Context.C2.getTypeDeclType(D2);
1084 Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
1085 << Base2->getType()
1086 << Base2->getSourceRange();
1087 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1088 << Base1->getType()
1089 << Base1->getSourceRange();
1090 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001091 return false;
1092 }
1093
1094 // Check virtual vs. non-virtual inheritance mismatch.
1095 if (Base1->isVirtual() != Base2->isVirtual()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001096 if (Context.Complain) {
1097 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1098 << Context.C2.getTypeDeclType(D2);
1099 Context.Diag2(Base2->getLocStart(),
1100 diag::note_odr_virtual_base)
1101 << Base2->isVirtual() << Base2->getSourceRange();
1102 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1103 << Base1->isVirtual()
1104 << Base1->getSourceRange();
1105 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001106 return false;
1107 }
1108 }
1109 } else if (D1CXX->getNumBases() > 0) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001110 if (Context.Complain) {
1111 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1112 << Context.C2.getTypeDeclType(D2);
1113 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
1114 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1115 << Base1->getType()
1116 << Base1->getSourceRange();
1117 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
1118 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001119 return false;
1120 }
1121 }
1122
1123 // Check the fields for consistency.
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001124 RecordDecl::field_iterator Field2 = D2->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001125 Field2End = D2->field_end();
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001126 for (RecordDecl::field_iterator Field1 = D1->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001127 Field1End = D1->field_end();
1128 Field1 != Field1End;
1129 ++Field1, ++Field2) {
1130 if (Field2 == Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001131 if (Context.Complain) {
1132 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1133 << Context.C2.getTypeDeclType(D2);
1134 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
1135 << Field1->getDeclName() << Field1->getType();
1136 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
1137 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001138 return false;
1139 }
1140
David Blaikie40ed2972012-06-06 20:45:41 +00001141 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001142 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001143 }
1144
1145 if (Field2 != Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001146 if (Context.Complain) {
1147 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1148 << Context.C2.getTypeDeclType(D2);
1149 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1150 << Field2->getDeclName() << Field2->getType();
1151 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1152 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001153 return false;
1154 }
1155
1156 return true;
1157}
1158
1159/// \brief Determine structural equivalence of two enums.
1160static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1161 EnumDecl *D1, EnumDecl *D2) {
1162 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1163 EC2End = D2->enumerator_end();
1164 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1165 EC1End = D1->enumerator_end();
1166 EC1 != EC1End; ++EC1, ++EC2) {
1167 if (EC2 == EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001168 if (Context.Complain) {
1169 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1170 << Context.C2.getTypeDeclType(D2);
1171 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1172 << EC1->getDeclName()
1173 << EC1->getInitVal().toString(10);
1174 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1175 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001176 return false;
1177 }
1178
1179 llvm::APSInt Val1 = EC1->getInitVal();
1180 llvm::APSInt Val2 = EC2->getInitVal();
Eric Christopher6dcc3762012-07-15 00:23:57 +00001181 if (!llvm::APSInt::isSameValue(Val1, Val2) ||
Douglas Gregor3996e242010-02-15 22:01:00 +00001182 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
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(EC2->getLocation(), diag::note_odr_enumerator)
1187 << EC2->getDeclName()
1188 << EC2->getInitVal().toString(10);
1189 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1190 << EC1->getDeclName()
1191 << EC1->getInitVal().toString(10);
1192 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001193 return false;
1194 }
1195 }
1196
1197 if (EC2 != EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001198 if (Context.Complain) {
1199 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1200 << Context.C2.getTypeDeclType(D2);
1201 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1202 << EC2->getDeclName()
1203 << EC2->getInitVal().toString(10);
1204 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1205 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001206 return false;
1207 }
1208
1209 return true;
1210}
Douglas Gregora082a492010-11-30 19:14:50 +00001211
1212static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1213 TemplateParameterList *Params1,
1214 TemplateParameterList *Params2) {
1215 if (Params1->size() != Params2->size()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001216 if (Context.Complain) {
1217 Context.Diag2(Params2->getTemplateLoc(),
1218 diag::err_odr_different_num_template_parameters)
1219 << Params1->size() << Params2->size();
1220 Context.Diag1(Params1->getTemplateLoc(),
1221 diag::note_odr_template_parameter_list);
1222 }
Douglas Gregora082a492010-11-30 19:14:50 +00001223 return false;
1224 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001225
Douglas Gregora082a492010-11-30 19:14:50 +00001226 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1227 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001228 if (Context.Complain) {
1229 Context.Diag2(Params2->getParam(I)->getLocation(),
1230 diag::err_odr_different_template_parameter_kind);
1231 Context.Diag1(Params1->getParam(I)->getLocation(),
1232 diag::note_odr_template_parameter_here);
1233 }
Douglas Gregora082a492010-11-30 19:14:50 +00001234 return false;
1235 }
1236
1237 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1238 Params2->getParam(I))) {
1239
1240 return false;
1241 }
1242 }
1243
1244 return true;
1245}
1246
1247static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1248 TemplateTypeParmDecl *D1,
1249 TemplateTypeParmDecl *D2) {
1250 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001251 if (Context.Complain) {
1252 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1253 << D2->isParameterPack();
1254 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1255 << D1->isParameterPack();
1256 }
Douglas Gregora082a492010-11-30 19:14:50 +00001257 return false;
1258 }
1259
1260 return true;
1261}
1262
1263static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1264 NonTypeTemplateParmDecl *D1,
1265 NonTypeTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001266 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001267 if (Context.Complain) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001268 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1269 << D2->isParameterPack();
1270 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1271 << D1->isParameterPack();
1272 }
Douglas Gregora082a492010-11-30 19:14:50 +00001273 return false;
1274 }
Douglas Gregora082a492010-11-30 19:14:50 +00001275
1276 // Check types.
1277 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001278 if (Context.Complain) {
1279 Context.Diag2(D2->getLocation(),
1280 diag::err_odr_non_type_parameter_type_inconsistent)
1281 << D2->getType() << D1->getType();
1282 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1283 << D1->getType();
1284 }
Douglas Gregora082a492010-11-30 19:14:50 +00001285 return false;
1286 }
1287
1288 return true;
1289}
1290
1291static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1292 TemplateTemplateParmDecl *D1,
1293 TemplateTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001294 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001295 if (Context.Complain) {
1296 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1297 << D2->isParameterPack();
1298 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1299 << D1->isParameterPack();
1300 }
Douglas Gregora082a492010-11-30 19:14:50 +00001301 return false;
1302 }
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001303
Douglas Gregora082a492010-11-30 19:14:50 +00001304 // Check template parameter lists.
1305 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1306 D2->getTemplateParameters());
1307}
1308
1309static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1310 ClassTemplateDecl *D1,
1311 ClassTemplateDecl *D2) {
1312 // Check template parameters.
1313 if (!IsStructurallyEquivalent(Context,
1314 D1->getTemplateParameters(),
1315 D2->getTemplateParameters()))
1316 return false;
1317
1318 // Check the templated declaration.
1319 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1320 D2->getTemplatedDecl());
1321}
1322
Douglas Gregor3996e242010-02-15 22:01:00 +00001323/// \brief Determine structural equivalence of two declarations.
1324static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1325 Decl *D1, Decl *D2) {
1326 // FIXME: Check for known structural equivalences via a callback of some sort.
1327
Douglas Gregorb4964f72010-02-15 23:54:17 +00001328 // Check whether we already know that these two declarations are not
1329 // structurally equivalent.
1330 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1331 D2->getCanonicalDecl())))
1332 return false;
1333
Douglas Gregor3996e242010-02-15 22:01:00 +00001334 // Determine whether we've already produced a tentative equivalence for D1.
1335 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1336 if (EquivToD1)
1337 return EquivToD1 == D2->getCanonicalDecl();
1338
1339 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1340 EquivToD1 = D2->getCanonicalDecl();
1341 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1342 return true;
1343}
1344
1345bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1346 Decl *D2) {
1347 if (!::IsStructurallyEquivalent(*this, D1, D2))
1348 return false;
1349
1350 return !Finish();
1351}
1352
1353bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1354 QualType T2) {
1355 if (!::IsStructurallyEquivalent(*this, T1, T2))
1356 return false;
1357
1358 return !Finish();
1359}
1360
1361bool StructuralEquivalenceContext::Finish() {
1362 while (!DeclsToCheck.empty()) {
1363 // Check the next declaration.
1364 Decl *D1 = DeclsToCheck.front();
1365 DeclsToCheck.pop_front();
1366
1367 Decl *D2 = TentativeEquivalences[D1];
1368 assert(D2 && "Unrecorded tentative equivalence?");
1369
Douglas Gregorb4964f72010-02-15 23:54:17 +00001370 bool Equivalent = true;
1371
Douglas Gregor3996e242010-02-15 22:01:00 +00001372 // FIXME: Switch on all declaration kinds. For now, we're just going to
1373 // check the obvious ones.
1374 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1375 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1376 // Check for equivalent structure names.
1377 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001378 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1379 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001380 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001381 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1382 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001383 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1384 !::IsStructurallyEquivalent(*this, Record1, Record2))
1385 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001386 } else {
1387 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001388 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001389 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001390 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001391 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1392 // Check for equivalent enum names.
1393 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001394 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1395 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001396 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001397 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1398 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001399 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1400 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1401 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001402 } else {
1403 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001404 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001405 }
Richard Smithdda56e42011-04-15 14:24:37 +00001406 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1407 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001408 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001409 Typedef2->getIdentifier()) ||
1410 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001411 Typedef1->getUnderlyingType(),
1412 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001413 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001414 } else {
1415 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001416 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001417 }
Douglas Gregora082a492010-11-30 19:14:50 +00001418 } else if (ClassTemplateDecl *ClassTemplate1
1419 = dyn_cast<ClassTemplateDecl>(D1)) {
1420 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1421 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1422 ClassTemplate2->getIdentifier()) ||
1423 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1424 Equivalent = false;
1425 } else {
1426 // Class template/non-class-template mismatch.
1427 Equivalent = false;
1428 }
1429 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1430 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1431 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1432 Equivalent = false;
1433 } else {
1434 // Kind mismatch.
1435 Equivalent = false;
1436 }
1437 } else if (NonTypeTemplateParmDecl *NTTP1
1438 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1439 if (NonTypeTemplateParmDecl *NTTP2
1440 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1441 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1442 Equivalent = false;
1443 } else {
1444 // Kind mismatch.
1445 Equivalent = false;
1446 }
1447 } else if (TemplateTemplateParmDecl *TTP1
1448 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1449 if (TemplateTemplateParmDecl *TTP2
1450 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1451 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1452 Equivalent = false;
1453 } else {
1454 // Kind mismatch.
1455 Equivalent = false;
1456 }
1457 }
1458
Douglas Gregorb4964f72010-02-15 23:54:17 +00001459 if (!Equivalent) {
1460 // Note that these two declarations are not equivalent (and we already
1461 // know about it).
1462 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1463 D2->getCanonicalDecl()));
1464 return true;
1465 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001466 // FIXME: Check other declaration kinds!
1467 }
1468
1469 return false;
1470}
1471
1472//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001473// Import Types
1474//----------------------------------------------------------------------------
1475
John McCall424cec92011-01-19 06:33:43 +00001476QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +00001477 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1478 << T->getTypeClassName();
1479 return QualType();
1480}
1481
John McCall424cec92011-01-19 06:33:43 +00001482QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001483 switch (T->getKind()) {
John McCalle314e272011-10-18 21:02:43 +00001484#define SHARED_SINGLETON_TYPE(Expansion)
1485#define BUILTIN_TYPE(Id, SingletonId) \
1486 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1487#include "clang/AST/BuiltinTypes.def"
1488
1489 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1490 // context supports C++.
1491
1492 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1493 // context supports ObjC.
1494
Douglas Gregor96e578d2010-02-05 17:54:41 +00001495 case BuiltinType::Char_U:
1496 // The context we're importing from has an unsigned 'char'. If we're
1497 // importing into a context with a signed 'char', translate to
1498 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001499 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001500 return Importer.getToContext().UnsignedCharTy;
1501
1502 return Importer.getToContext().CharTy;
1503
Douglas Gregor96e578d2010-02-05 17:54:41 +00001504 case BuiltinType::Char_S:
1505 // The context we're importing from has an unsigned 'char'. If we're
1506 // importing into a context with a signed 'char', translate to
1507 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001508 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001509 return Importer.getToContext().SignedCharTy;
1510
1511 return Importer.getToContext().CharTy;
1512
Chris Lattnerad3467e2010-12-25 23:25:43 +00001513 case BuiltinType::WChar_S:
1514 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001515 // FIXME: If not in C++, shall we translate to the C equivalent of
1516 // wchar_t?
1517 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001518 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001519
1520 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001521}
1522
John McCall424cec92011-01-19 06:33:43 +00001523QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001524 QualType ToElementType = Importer.Import(T->getElementType());
1525 if (ToElementType.isNull())
1526 return QualType();
1527
1528 return Importer.getToContext().getComplexType(ToElementType);
1529}
1530
John McCall424cec92011-01-19 06:33:43 +00001531QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001532 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1533 if (ToPointeeType.isNull())
1534 return QualType();
1535
1536 return Importer.getToContext().getPointerType(ToPointeeType);
1537}
1538
John McCall424cec92011-01-19 06:33:43 +00001539QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001540 // FIXME: Check for blocks support in "to" context.
1541 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1542 if (ToPointeeType.isNull())
1543 return QualType();
1544
1545 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1546}
1547
John McCall424cec92011-01-19 06:33:43 +00001548QualType
1549ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001550 // FIXME: Check for C++ support in "to" context.
1551 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1552 if (ToPointeeType.isNull())
1553 return QualType();
1554
1555 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1556}
1557
John McCall424cec92011-01-19 06:33:43 +00001558QualType
1559ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001560 // FIXME: Check for C++0x support in "to" context.
1561 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1562 if (ToPointeeType.isNull())
1563 return QualType();
1564
1565 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1566}
1567
John McCall424cec92011-01-19 06:33:43 +00001568QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001569 // FIXME: Check for C++ support in "to" context.
1570 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1571 if (ToPointeeType.isNull())
1572 return QualType();
1573
1574 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1575 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1576 ClassType.getTypePtr());
1577}
1578
John McCall424cec92011-01-19 06:33:43 +00001579QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001580 QualType ToElementType = Importer.Import(T->getElementType());
1581 if (ToElementType.isNull())
1582 return QualType();
1583
1584 return Importer.getToContext().getConstantArrayType(ToElementType,
1585 T->getSize(),
1586 T->getSizeModifier(),
1587 T->getIndexTypeCVRQualifiers());
1588}
1589
John McCall424cec92011-01-19 06:33:43 +00001590QualType
1591ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001592 QualType ToElementType = Importer.Import(T->getElementType());
1593 if (ToElementType.isNull())
1594 return QualType();
1595
1596 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1597 T->getSizeModifier(),
1598 T->getIndexTypeCVRQualifiers());
1599}
1600
John McCall424cec92011-01-19 06:33:43 +00001601QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001602 QualType ToElementType = Importer.Import(T->getElementType());
1603 if (ToElementType.isNull())
1604 return QualType();
1605
1606 Expr *Size = Importer.Import(T->getSizeExpr());
1607 if (!Size)
1608 return QualType();
1609
1610 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1611 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1612 T->getSizeModifier(),
1613 T->getIndexTypeCVRQualifiers(),
1614 Brackets);
1615}
1616
John McCall424cec92011-01-19 06:33:43 +00001617QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001618 QualType ToElementType = Importer.Import(T->getElementType());
1619 if (ToElementType.isNull())
1620 return QualType();
1621
1622 return Importer.getToContext().getVectorType(ToElementType,
1623 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001624 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001625}
1626
John McCall424cec92011-01-19 06:33:43 +00001627QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001628 QualType ToElementType = Importer.Import(T->getElementType());
1629 if (ToElementType.isNull())
1630 return QualType();
1631
1632 return Importer.getToContext().getExtVectorType(ToElementType,
1633 T->getNumElements());
1634}
1635
John McCall424cec92011-01-19 06:33:43 +00001636QualType
1637ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001638 // FIXME: What happens if we're importing a function without a prototype
1639 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +00001640 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001641 if (ToResultType.isNull())
1642 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001643
Douglas Gregor96e578d2010-02-05 17:54:41 +00001644 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001645 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001646}
1647
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001648QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +00001649 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001650 if (ToResultType.isNull())
1651 return QualType();
1652
1653 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001654 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00001655 for (const auto &A : T->param_types()) {
1656 QualType ArgType = Importer.Import(A);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001657 if (ArgType.isNull())
1658 return QualType();
1659 ArgTypes.push_back(ArgType);
1660 }
1661
1662 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001663 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +00001664 for (const auto &E : T->exceptions()) {
1665 QualType ExceptionType = Importer.Import(E);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001666 if (ExceptionType.isNull())
1667 return QualType();
1668 ExceptionTypes.push_back(ExceptionType);
1669 }
John McCalldb40c7f2010-12-14 08:05:40 +00001670
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001671 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1672 FunctionProtoType::ExtProtoInfo ToEPI;
1673
1674 ToEPI.ExtInfo = FromEPI.ExtInfo;
1675 ToEPI.Variadic = FromEPI.Variadic;
1676 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1677 ToEPI.TypeQuals = FromEPI.TypeQuals;
1678 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +00001679 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1680 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
1681 ToEPI.ExceptionSpec.NoexceptExpr =
1682 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
1683 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
1684 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
1685 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
1686 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001687
Jordan Rose5c382722013-03-08 21:51:21 +00001688 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001689}
1690
Sean Callananda6df8a2011-08-11 16:56:07 +00001691QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1692 QualType ToInnerType = Importer.Import(T->getInnerType());
1693 if (ToInnerType.isNull())
1694 return QualType();
1695
1696 return Importer.getToContext().getParenType(ToInnerType);
1697}
1698
John McCall424cec92011-01-19 06:33:43 +00001699QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +00001700 TypedefNameDecl *ToDecl
1701 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001702 if (!ToDecl)
1703 return QualType();
1704
1705 return Importer.getToContext().getTypeDeclType(ToDecl);
1706}
1707
John McCall424cec92011-01-19 06:33:43 +00001708QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001709 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1710 if (!ToExpr)
1711 return QualType();
1712
1713 return Importer.getToContext().getTypeOfExprType(ToExpr);
1714}
1715
John McCall424cec92011-01-19 06:33:43 +00001716QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001717 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1718 if (ToUnderlyingType.isNull())
1719 return QualType();
1720
1721 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1722}
1723
John McCall424cec92011-01-19 06:33:43 +00001724QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001725 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +00001726 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1727 if (!ToExpr)
1728 return QualType();
1729
Douglas Gregor81495f32012-02-12 18:42:33 +00001730 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1731 if (UnderlyingType.isNull())
1732 return QualType();
1733
1734 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001735}
1736
Alexis Hunte852b102011-05-24 22:41:36 +00001737QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1738 QualType ToBaseType = Importer.Import(T->getBaseType());
1739 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1740 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1741 return QualType();
1742
1743 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1744 ToUnderlyingType,
1745 T->getUTTKind());
1746}
1747
Richard Smith30482bc2011-02-20 03:19:35 +00001748QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00001749 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +00001750 QualType FromDeduced = T->getDeducedType();
1751 QualType ToDeduced;
1752 if (!FromDeduced.isNull()) {
1753 ToDeduced = Importer.Import(FromDeduced);
1754 if (ToDeduced.isNull())
1755 return QualType();
1756 }
1757
Richard Smithe301ba22015-11-11 02:02:15 +00001758 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +00001759 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +00001760}
1761
John McCall424cec92011-01-19 06:33:43 +00001762QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001763 RecordDecl *ToDecl
1764 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1765 if (!ToDecl)
1766 return QualType();
1767
1768 return Importer.getToContext().getTagDeclType(ToDecl);
1769}
1770
John McCall424cec92011-01-19 06:33:43 +00001771QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001772 EnumDecl *ToDecl
1773 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1774 if (!ToDecl)
1775 return QualType();
1776
1777 return Importer.getToContext().getTagDeclType(ToDecl);
1778}
1779
Sean Callanan72fe0852015-04-02 23:50:08 +00001780QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
1781 QualType FromModifiedType = T->getModifiedType();
1782 QualType FromEquivalentType = T->getEquivalentType();
1783 QualType ToModifiedType;
1784 QualType ToEquivalentType;
1785
1786 if (!FromModifiedType.isNull()) {
1787 ToModifiedType = Importer.Import(FromModifiedType);
1788 if (ToModifiedType.isNull())
1789 return QualType();
1790 }
1791 if (!FromEquivalentType.isNull()) {
1792 ToEquivalentType = Importer.Import(FromEquivalentType);
1793 if (ToEquivalentType.isNull())
1794 return QualType();
1795 }
1796
1797 return Importer.getToContext().getAttributedType(T->getAttrKind(),
1798 ToModifiedType, ToEquivalentType);
1799}
1800
Douglas Gregore2e50d332010-12-01 01:36:18 +00001801QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001802 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001803 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1804 if (ToTemplate.isNull())
1805 return QualType();
1806
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001807 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001808 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1809 return QualType();
1810
1811 QualType ToCanonType;
1812 if (!QualType(T, 0).isCanonical()) {
1813 QualType FromCanonType
1814 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1815 ToCanonType =Importer.Import(FromCanonType);
1816 if (ToCanonType.isNull())
1817 return QualType();
1818 }
1819 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1820 ToTemplateArgs.data(),
1821 ToTemplateArgs.size(),
1822 ToCanonType);
1823}
1824
John McCall424cec92011-01-19 06:33:43 +00001825QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Craig Topper36250ad2014-05-12 05:36:57 +00001826 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001827 // Note: the qualifier in an ElaboratedType is optional.
1828 if (T->getQualifier()) {
1829 ToQualifier = Importer.Import(T->getQualifier());
1830 if (!ToQualifier)
1831 return QualType();
1832 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001833
1834 QualType ToNamedType = Importer.Import(T->getNamedType());
1835 if (ToNamedType.isNull())
1836 return QualType();
1837
Abramo Bagnara6150c882010-05-11 21:36:43 +00001838 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1839 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001840}
1841
John McCall424cec92011-01-19 06:33:43 +00001842QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001843 ObjCInterfaceDecl *Class
1844 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1845 if (!Class)
1846 return QualType();
1847
John McCall8b07ec22010-05-15 11:32:37 +00001848 return Importer.getToContext().getObjCInterfaceType(Class);
1849}
1850
John McCall424cec92011-01-19 06:33:43 +00001851QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001852 QualType ToBaseType = Importer.Import(T->getBaseType());
1853 if (ToBaseType.isNull())
1854 return QualType();
1855
Douglas Gregore9d95f12015-07-07 03:57:35 +00001856 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00001857 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00001858 QualType ImportedTypeArg = Importer.Import(TypeArg);
1859 if (ImportedTypeArg.isNull())
1860 return QualType();
1861
1862 TypeArgs.push_back(ImportedTypeArg);
1863 }
1864
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001865 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001866 for (auto *P : T->quals()) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001867 ObjCProtocolDecl *Protocol
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001868 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001869 if (!Protocol)
1870 return QualType();
1871 Protocols.push_back(Protocol);
1872 }
1873
Douglas Gregore9d95f12015-07-07 03:57:35 +00001874 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001875 Protocols,
1876 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001877}
1878
John McCall424cec92011-01-19 06:33:43 +00001879QualType
1880ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001881 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1882 if (ToPointeeType.isNull())
1883 return QualType();
1884
John McCall8b07ec22010-05-15 11:32:37 +00001885 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001886}
1887
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001888//----------------------------------------------------------------------------
1889// Import Declarations
1890//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001891bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1892 DeclContext *&LexicalDC,
1893 DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +00001894 NamedDecl *&ToD,
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001895 SourceLocation &Loc) {
1896 // Import the context of this declaration.
1897 DC = Importer.ImportContext(D->getDeclContext());
1898 if (!DC)
1899 return true;
1900
1901 LexicalDC = DC;
1902 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1903 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1904 if (!LexicalDC)
1905 return true;
1906 }
1907
1908 // Import the name of this declaration.
1909 Name = Importer.Import(D->getDeclName());
1910 if (D->getDeclName() && !Name)
1911 return true;
1912
1913 // Import the location of this declaration.
1914 Loc = Importer.Import(D->getLocation());
Sean Callanan59721b32015-04-28 18:41:46 +00001915 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001916 return false;
1917}
1918
Douglas Gregord451ea92011-07-29 23:31:30 +00001919void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1920 if (!FromD)
1921 return;
1922
1923 if (!ToD) {
1924 ToD = Importer.Import(FromD);
1925 if (!ToD)
1926 return;
1927 }
1928
1929 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1930 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +00001931 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001932 ImportDefinition(FromRecord, ToRecord);
1933 }
1934 }
1935 return;
1936 }
1937
1938 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1939 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1940 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1941 ImportDefinition(FromEnum, ToEnum);
1942 }
1943 }
1944 return;
1945 }
1946}
1947
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001948void
1949ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1950 DeclarationNameInfo& To) {
1951 // NOTE: To.Name and To.Loc are already imported.
1952 // We only have to import To.LocInfo.
1953 switch (To.getName().getNameKind()) {
1954 case DeclarationName::Identifier:
1955 case DeclarationName::ObjCZeroArgSelector:
1956 case DeclarationName::ObjCOneArgSelector:
1957 case DeclarationName::ObjCMultiArgSelector:
1958 case DeclarationName::CXXUsingDirective:
1959 return;
1960
1961 case DeclarationName::CXXOperatorName: {
1962 SourceRange Range = From.getCXXOperatorNameRange();
1963 To.setCXXOperatorNameRange(Importer.Import(Range));
1964 return;
1965 }
1966 case DeclarationName::CXXLiteralOperatorName: {
1967 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1968 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1969 return;
1970 }
1971 case DeclarationName::CXXConstructorName:
1972 case DeclarationName::CXXDestructorName:
1973 case DeclarationName::CXXConversionFunctionName: {
1974 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1975 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1976 return;
1977 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001978 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001979 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001980}
1981
Douglas Gregor2e15c842012-02-01 21:00:38 +00001982void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001983 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001984 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001985 return;
1986 }
1987
Aaron Ballman629afae2014-03-07 19:56:05 +00001988 for (auto *From : FromDC->decls())
1989 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +00001990}
1991
Douglas Gregord451ea92011-07-29 23:31:30 +00001992bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001993 ImportDefinitionKind Kind) {
1994 if (To->getDefinition() || To->isBeingDefined()) {
1995 if (Kind == IDK_Everything)
1996 ImportDeclContext(From, /*ForceImport=*/true);
1997
Douglas Gregore2e50d332010-12-01 01:36:18 +00001998 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001999 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00002000
2001 To->startDefinition();
2002
2003 // Add base classes.
2004 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
2005 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002006
2007 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
2008 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
2009 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00002010 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002011 ToData.Aggregate = FromData.Aggregate;
2012 ToData.PlainOldData = FromData.PlainOldData;
2013 ToData.Empty = FromData.Empty;
2014 ToData.Polymorphic = FromData.Polymorphic;
2015 ToData.Abstract = FromData.Abstract;
2016 ToData.IsStandardLayout = FromData.IsStandardLayout;
2017 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
2018 ToData.HasPrivateFields = FromData.HasPrivateFields;
2019 ToData.HasProtectedFields = FromData.HasProtectedFields;
2020 ToData.HasPublicFields = FromData.HasPublicFields;
2021 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00002022 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00002023 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00002024 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00002025 ToData.HasUninitializedReferenceMember
2026 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00002027 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith6b02d462012-12-08 08:32:28 +00002028 ToData.NeedOverloadResolutionForMoveConstructor
2029 = FromData.NeedOverloadResolutionForMoveConstructor;
2030 ToData.NeedOverloadResolutionForMoveAssignment
2031 = FromData.NeedOverloadResolutionForMoveAssignment;
2032 ToData.NeedOverloadResolutionForDestructor
2033 = FromData.NeedOverloadResolutionForDestructor;
2034 ToData.DefaultedMoveConstructorIsDeleted
2035 = FromData.DefaultedMoveConstructorIsDeleted;
2036 ToData.DefaultedMoveAssignmentIsDeleted
2037 = FromData.DefaultedMoveAssignmentIsDeleted;
2038 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00002039 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
2040 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002041 ToData.HasConstexprNonCopyMoveConstructor
2042 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00002043 ToData.HasDefaultedDefaultConstructor
2044 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00002045 ToData.DefaultedDefaultConstructorIsConstexpr
2046 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00002047 ToData.HasConstexprDefaultConstructor
2048 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002049 ToData.HasNonLiteralTypeFieldsOrBases
2050 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00002051 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002052 ToData.UserProvidedDefaultConstructor
2053 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00002054 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smith1c33fe82012-11-28 06:23:12 +00002055 ToData.ImplicitCopyConstructorHasConstParam
2056 = FromData.ImplicitCopyConstructorHasConstParam;
2057 ToData.ImplicitCopyAssignmentHasConstParam
2058 = FromData.ImplicitCopyAssignmentHasConstParam;
2059 ToData.HasDeclaredCopyConstructorWithConstParam
2060 = FromData.HasDeclaredCopyConstructorWithConstParam;
2061 ToData.HasDeclaredCopyAssignmentWithConstParam
2062 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00002063 ToData.IsLambda = FromData.IsLambda;
2064
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002065 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00002066 for (const auto &Base1 : FromCXX->bases()) {
2067 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +00002068 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00002069 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00002070
2071 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00002072 if (Base1.isPackExpansion())
2073 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00002074
2075 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00002076 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Douglas Gregord451ea92011-07-29 23:31:30 +00002077
Douglas Gregore2e50d332010-12-01 01:36:18 +00002078 Bases.push_back(
2079 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00002080 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
2081 Base1.isVirtual(),
2082 Base1.isBaseOfClass(),
2083 Base1.getAccessSpecifierAsWritten(),
2084 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00002085 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00002086 }
2087 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00002088 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00002089 }
2090
Douglas Gregor2e15c842012-02-01 21:00:38 +00002091 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00002092 ImportDeclContext(From, /*ForceImport=*/true);
2093
Douglas Gregore2e50d332010-12-01 01:36:18 +00002094 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00002095 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002096}
2097
Larisse Voufo39a1e502013-08-06 01:03:05 +00002098bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
2099 ImportDefinitionKind Kind) {
Sean Callanan59721b32015-04-28 18:41:46 +00002100 if (To->getAnyInitializer())
Larisse Voufo39a1e502013-08-06 01:03:05 +00002101 return false;
2102
2103 // FIXME: Can we really import any initializer? Alternatively, we could force
2104 // ourselves to import every declaration of a variable and then only use
2105 // getInit() here.
2106 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
2107
2108 // FIXME: Other bits to merge?
2109
2110 return false;
2111}
2112
Douglas Gregord451ea92011-07-29 23:31:30 +00002113bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00002114 ImportDefinitionKind Kind) {
2115 if (To->getDefinition() || To->isBeingDefined()) {
2116 if (Kind == IDK_Everything)
2117 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002118 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002119 }
Douglas Gregord451ea92011-07-29 23:31:30 +00002120
2121 To->startDefinition();
2122
2123 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
2124 if (T.isNull())
2125 return true;
2126
2127 QualType ToPromotionType = Importer.Import(From->getPromotionType());
2128 if (ToPromotionType.isNull())
2129 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002130
2131 if (shouldForceImportDeclContext(Kind))
2132 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002133
2134 // FIXME: we might need to merge the number of positive or negative bits
2135 // if the enumerator lists don't match.
2136 To->completeDefinition(T, ToPromotionType,
2137 From->getNumPositiveBits(),
2138 From->getNumNegativeBits());
2139 return false;
2140}
2141
Douglas Gregora082a492010-11-30 19:14:50 +00002142TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
2143 TemplateParameterList *Params) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002144 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregora082a492010-11-30 19:14:50 +00002145 ToParams.reserve(Params->size());
2146 for (TemplateParameterList::iterator P = Params->begin(),
2147 PEnd = Params->end();
2148 P != PEnd; ++P) {
2149 Decl *To = Importer.Import(*P);
2150 if (!To)
Craig Topper36250ad2014-05-12 05:36:57 +00002151 return nullptr;
2152
Douglas Gregora082a492010-11-30 19:14:50 +00002153 ToParams.push_back(cast<NamedDecl>(To));
2154 }
2155
2156 return TemplateParameterList::Create(Importer.getToContext(),
2157 Importer.Import(Params->getTemplateLoc()),
2158 Importer.Import(Params->getLAngleLoc()),
David Majnemer902f8c62015-12-27 07:16:27 +00002159 ToParams,
Douglas Gregora082a492010-11-30 19:14:50 +00002160 Importer.Import(Params->getRAngleLoc()));
2161}
2162
Douglas Gregore2e50d332010-12-01 01:36:18 +00002163TemplateArgument
2164ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
2165 switch (From.getKind()) {
2166 case TemplateArgument::Null:
2167 return TemplateArgument();
2168
2169 case TemplateArgument::Type: {
2170 QualType ToType = Importer.Import(From.getAsType());
2171 if (ToType.isNull())
2172 return TemplateArgument();
2173 return TemplateArgument(ToType);
2174 }
2175
2176 case TemplateArgument::Integral: {
2177 QualType ToType = Importer.Import(From.getIntegralType());
2178 if (ToType.isNull())
2179 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00002180 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00002181 }
2182
Eli Friedmanb826a002012-09-26 02:36:12 +00002183 case TemplateArgument::Declaration: {
David Blaikie3c7dd6b2014-10-22 19:54:16 +00002184 ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
2185 QualType ToType = Importer.Import(From.getParamTypeForDecl());
2186 if (!To || ToType.isNull())
2187 return TemplateArgument();
2188 return TemplateArgument(To, ToType);
Eli Friedmanb826a002012-09-26 02:36:12 +00002189 }
2190
2191 case TemplateArgument::NullPtr: {
2192 QualType ToType = Importer.Import(From.getNullPtrType());
2193 if (ToType.isNull())
2194 return TemplateArgument();
2195 return TemplateArgument(ToType, /*isNullPtr*/true);
2196 }
2197
Douglas Gregore2e50d332010-12-01 01:36:18 +00002198 case TemplateArgument::Template: {
2199 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
2200 if (ToTemplate.isNull())
2201 return TemplateArgument();
2202
2203 return TemplateArgument(ToTemplate);
2204 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002205
2206 case TemplateArgument::TemplateExpansion: {
2207 TemplateName ToTemplate
2208 = Importer.Import(From.getAsTemplateOrTemplatePattern());
2209 if (ToTemplate.isNull())
2210 return TemplateArgument();
2211
Douglas Gregore1d60df2011-01-14 23:41:42 +00002212 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002213 }
2214
Douglas Gregore2e50d332010-12-01 01:36:18 +00002215 case TemplateArgument::Expression:
2216 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2217 return TemplateArgument(ToExpr);
2218 return TemplateArgument();
2219
2220 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002221 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002222 ToPack.reserve(From.pack_size());
2223 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2224 return TemplateArgument();
Benjamin Kramercce63472015-08-05 09:40:22 +00002225
2226 return TemplateArgument(
2227 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00002228 }
2229 }
2230
2231 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00002232}
2233
2234bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2235 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002236 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00002237 for (unsigned I = 0; I != NumFromArgs; ++I) {
2238 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2239 if (To.isNull() && !FromArgs[I].isNull())
2240 return true;
2241
2242 ToArgs.push_back(To);
2243 }
2244
2245 return false;
2246}
2247
Douglas Gregor5c73e912010-02-11 00:48:18 +00002248bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002249 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00002250 // Eliminate a potential failure point where we attempt to re-import
2251 // something we're trying to import while completing ToRecord.
2252 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
2253 if (ToOrigin) {
2254 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
2255 if (ToOriginRecord)
2256 ToRecord = ToOriginRecord;
2257 }
2258
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002259 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00002260 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002261 Importer.getNonEquivalentDecls(),
2262 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002263 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002264}
2265
Larisse Voufo39a1e502013-08-06 01:03:05 +00002266bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
2267 bool Complain) {
2268 StructuralEquivalenceContext Ctx(
2269 Importer.getFromContext(), Importer.getToContext(),
2270 Importer.getNonEquivalentDecls(), false, Complain);
2271 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
2272}
2273
Douglas Gregor98c10182010-02-12 22:17:39 +00002274bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002275 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002276 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002277 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002278 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002279}
2280
Douglas Gregor91155082012-11-14 22:29:20 +00002281bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
2282 EnumConstantDecl *ToEC)
2283{
2284 const llvm::APSInt &FromVal = FromEC->getInitVal();
2285 const llvm::APSInt &ToVal = ToEC->getInitVal();
2286
2287 return FromVal.isSigned() == ToVal.isSigned() &&
2288 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2289 FromVal == ToVal;
2290}
2291
2292bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002293 ClassTemplateDecl *To) {
2294 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2295 Importer.getToContext(),
2296 Importer.getNonEquivalentDecls());
2297 return Ctx.IsStructurallyEquivalent(From, To);
2298}
2299
Larisse Voufo39a1e502013-08-06 01:03:05 +00002300bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2301 VarTemplateDecl *To) {
2302 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2303 Importer.getToContext(),
2304 Importer.getNonEquivalentDecls());
2305 return Ctx.IsStructurallyEquivalent(From, To);
2306}
2307
Douglas Gregore4c83e42010-02-09 22:48:33 +00002308Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002309 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002310 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00002311 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00002312}
2313
Sean Callanan65198272011-11-17 23:20:56 +00002314Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2315 TranslationUnitDecl *ToD =
2316 Importer.getToContext().getTranslationUnitDecl();
2317
2318 Importer.Imported(D, ToD);
2319
2320 return ToD;
2321}
2322
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002323Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
2324
2325 SourceLocation Loc = Importer.Import(D->getLocation());
2326 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
2327
2328 // Import the context of this declaration.
2329 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2330 if (!DC)
2331 return nullptr;
2332
2333 AccessSpecDecl *accessSpecDecl
2334 = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(),
2335 DC, Loc, ColonLoc);
2336
2337 if (!accessSpecDecl)
2338 return nullptr;
2339
2340 // Lexical DeclContext and Semantic DeclContext
2341 // is always the same for the accessSpec.
2342 accessSpecDecl->setLexicalDeclContext(DC);
2343 DC->addDeclInternal(accessSpecDecl);
2344
2345 return accessSpecDecl;
2346}
2347
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002348Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2349 // Import the major distinguishing characteristics of this namespace.
2350 DeclContext *DC, *LexicalDC;
2351 DeclarationName Name;
2352 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002353 NamedDecl *ToD;
2354 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002355 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002356 if (ToD)
2357 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002358
2359 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002360 if (!Name) {
2361 // This is an anonymous namespace. Adopt an existing anonymous
2362 // namespace if we can.
2363 // FIXME: Not testable.
2364 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2365 MergeWithNamespace = TU->getAnonymousNamespace();
2366 else
2367 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2368 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002369 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002370 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002371 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002372 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2373 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002374 continue;
2375
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002376 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002377 MergeWithNamespace = FoundNS;
2378 ConflictingDecls.clear();
2379 break;
2380 }
2381
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002382 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002383 }
2384
2385 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002386 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002387 ConflictingDecls.data(),
2388 ConflictingDecls.size());
2389 }
2390 }
2391
2392 // Create the "to" namespace, if needed.
2393 NamespaceDecl *ToNamespace = MergeWithNamespace;
2394 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002395 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002396 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002397 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00002398 Loc, Name.getAsIdentifierInfo(),
Craig Topper36250ad2014-05-12 05:36:57 +00002399 /*PrevDecl=*/nullptr);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002400 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002401 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002402
2403 // If this is an anonymous namespace, register it as the anonymous
2404 // namespace within its context.
2405 if (!Name) {
2406 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2407 TU->setAnonymousNamespace(ToNamespace);
2408 else
2409 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2410 }
2411 }
2412 Importer.Imported(D, ToNamespace);
2413
2414 ImportDeclContext(D);
2415
2416 return ToNamespace;
2417}
2418
Richard Smithdda56e42011-04-15 14:24:37 +00002419Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002420 // Import the major distinguishing characteristics of this typedef.
2421 DeclContext *DC, *LexicalDC;
2422 DeclarationName Name;
2423 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002424 NamedDecl *ToD;
2425 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002426 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002427 if (ToD)
2428 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002429
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002430 // If this typedef is not in block scope, determine whether we've
2431 // seen a typedef with the same name (that we can merge with) or any
2432 // other entity by that name (which name lookup could conflict with).
2433 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002434 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002435 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002436 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002437 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002438 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2439 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002440 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00002441 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002442 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002443 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2444 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002445 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002446 }
2447
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002448 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002449 }
2450
2451 if (!ConflictingDecls.empty()) {
2452 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2453 ConflictingDecls.data(),
2454 ConflictingDecls.size());
2455 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002456 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002457 }
2458 }
2459
Douglas Gregorb4964f72010-02-15 23:54:17 +00002460 // Import the underlying type of this typedef;
2461 QualType T = Importer.Import(D->getUnderlyingType());
2462 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002463 return nullptr;
2464
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002465 // Create the new typedef node.
2466 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002467 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00002468 TypedefNameDecl *ToTypedef;
2469 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002470 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2471 StartL, Loc,
2472 Name.getAsIdentifierInfo(),
2473 TInfo);
2474 else
Richard Smithdda56e42011-04-15 14:24:37 +00002475 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2476 StartL, Loc,
2477 Name.getAsIdentifierInfo(),
2478 TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002479
Douglas Gregordd483172010-02-22 17:42:47 +00002480 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002481 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002482 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00002483 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002484
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002485 return ToTypedef;
2486}
2487
Richard Smithdda56e42011-04-15 14:24:37 +00002488Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2489 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2490}
2491
2492Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2493 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2494}
2495
Douglas Gregor98c10182010-02-12 22:17:39 +00002496Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2497 // Import the major distinguishing characteristics of this enum.
2498 DeclContext *DC, *LexicalDC;
2499 DeclarationName Name;
2500 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002501 NamedDecl *ToD;
2502 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002503 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002504 if (ToD)
2505 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002506
Douglas Gregor98c10182010-02-12 22:17:39 +00002507 // Figure out what enum name we're looking for.
2508 unsigned IDNS = Decl::IDNS_Tag;
2509 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002510 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2511 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002512 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002513 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002514 IDNS |= Decl::IDNS_Ordinary;
2515
2516 // We may already have an enum of the same name; try to find and match it.
2517 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002518 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002519 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002520 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002521 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2522 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002523 continue;
2524
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002525 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002526 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002527 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2528 Found = Tag->getDecl();
2529 }
2530
2531 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002532 if (IsStructuralMatch(D, FoundEnum))
2533 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002534 }
2535
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002536 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002537 }
2538
2539 if (!ConflictingDecls.empty()) {
2540 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2541 ConflictingDecls.data(),
2542 ConflictingDecls.size());
2543 }
2544 }
2545
2546 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002547 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2548 Importer.Import(D->getLocStart()),
Craig Topper36250ad2014-05-12 05:36:57 +00002549 Loc, Name.getAsIdentifierInfo(), nullptr,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002550 D->isScoped(), D->isScopedUsingClassTag(),
2551 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002552 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002553 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002554 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002555 D2->setLexicalDeclContext(LexicalDC);
2556 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00002557 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002558
2559 // Import the integer type.
2560 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2561 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002562 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00002563 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002564
2565 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002566 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00002567 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002568
Douglas Gregor3996e242010-02-15 22:01:00 +00002569 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002570}
2571
Douglas Gregor5c73e912010-02-11 00:48:18 +00002572Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2573 // If this record has a definition in the translation unit we're coming from,
2574 // but this particular declaration is not that definition, import the
2575 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002576 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002577 if (Definition && Definition != D) {
2578 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002579 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00002580 return nullptr;
2581
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002582 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002583 }
2584
2585 // Import the major distinguishing characteristics of this record.
2586 DeclContext *DC, *LexicalDC;
2587 DeclarationName Name;
2588 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002589 NamedDecl *ToD;
2590 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002591 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002592 if (ToD)
2593 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002594
Douglas Gregor5c73e912010-02-11 00:48:18 +00002595 // Figure out what structure name we're looking for.
2596 unsigned IDNS = Decl::IDNS_Tag;
2597 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002598 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2599 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002600 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002601 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002602 IDNS |= Decl::IDNS_Ordinary;
2603
2604 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00002605 RecordDecl *AdoptDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002606 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002607 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002608 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002609 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002610 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2611 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002612 continue;
2613
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002614 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002615 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002616 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2617 Found = Tag->getDecl();
2618 }
2619
2620 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002621 if (D->isAnonymousStructOrUnion() &&
2622 FoundRecord->isAnonymousStructOrUnion()) {
2623 // If both anonymous structs/unions are in a record context, make sure
2624 // they occur in the same location in the context records.
David Blaikie05785d12013-02-20 22:23:23 +00002625 if (Optional<unsigned> Index1
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002626 = findAnonymousStructOrUnionIndex(D)) {
David Blaikie05785d12013-02-20 22:23:23 +00002627 if (Optional<unsigned> Index2 =
2628 findAnonymousStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002629 if (*Index1 != *Index2)
2630 continue;
2631 }
2632 }
2633 }
2634
Douglas Gregor25791052010-02-12 00:09:27 +00002635 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002636 if ((SearchName && !D->isCompleteDefinition())
2637 || (D->isCompleteDefinition() &&
2638 D->isAnonymousStructOrUnion()
2639 == FoundDef->isAnonymousStructOrUnion() &&
2640 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002641 // The record types structurally match, or the "from" translation
2642 // unit only had a forward declaration anyway; call it the same
2643 // function.
2644 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002645 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002646 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002647 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002648 // We have a forward declaration of this type, so adopt that forward
2649 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00002650
2651 // If one or both can be completed from external storage then try one
2652 // last time to complete and compare them before doing this.
2653
2654 if (FoundRecord->hasExternalLexicalStorage() &&
2655 !FoundRecord->isCompleteDefinition())
2656 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2657 if (D->hasExternalLexicalStorage())
2658 D->getASTContext().getExternalSource()->CompleteType(D);
2659
2660 if (FoundRecord->isCompleteDefinition() &&
2661 D->isCompleteDefinition() &&
2662 !IsStructuralMatch(D, FoundRecord))
2663 continue;
2664
Douglas Gregor25791052010-02-12 00:09:27 +00002665 AdoptDecl = FoundRecord;
2666 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002667 } else if (!SearchName) {
2668 continue;
2669 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002670 }
2671
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002672 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002673 }
2674
Douglas Gregordd6006f2012-07-17 21:16:27 +00002675 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002676 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2677 ConflictingDecls.data(),
2678 ConflictingDecls.size());
2679 }
2680 }
2681
2682 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002683 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002684 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002685 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00002686 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00002687 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00002688 D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002689 DC, StartLoc, Loc,
2690 Name.getAsIdentifierInfo());
Douglas Gregor3996e242010-02-15 22:01:00 +00002691 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002692 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002693 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002694 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002695 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002696 }
Douglas Gregor14454802011-02-25 02:25:35 +00002697
2698 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002699 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002700 LexicalDC->addDeclInternal(D2);
Douglas Gregordd6006f2012-07-17 21:16:27 +00002701 if (D->isAnonymousStructOrUnion())
2702 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002703 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002704
Douglas Gregor3996e242010-02-15 22:01:00 +00002705 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002706
Douglas Gregor95d82832012-01-24 18:36:04 +00002707 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00002708 return nullptr;
2709
Douglas Gregor3996e242010-02-15 22:01:00 +00002710 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002711}
2712
Douglas Gregor98c10182010-02-12 22:17:39 +00002713Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2714 // Import the major distinguishing characteristics of this enumerator.
2715 DeclContext *DC, *LexicalDC;
2716 DeclarationName Name;
2717 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002718 NamedDecl *ToD;
2719 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002720 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002721 if (ToD)
2722 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002723
2724 QualType T = Importer.Import(D->getType());
2725 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002726 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002727
Douglas Gregor98c10182010-02-12 22:17:39 +00002728 // Determine whether there are any other declarations with the same name and
2729 // in the same context.
2730 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002731 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002732 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002733 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002734 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002735 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2736 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002737 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002738
2739 if (EnumConstantDecl *FoundEnumConstant
2740 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
2741 if (IsStructuralMatch(D, FoundEnumConstant))
2742 return Importer.Imported(D, FoundEnumConstant);
2743 }
2744
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002745 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002746 }
2747
2748 if (!ConflictingDecls.empty()) {
2749 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2750 ConflictingDecls.data(),
2751 ConflictingDecls.size());
2752 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002753 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002754 }
2755 }
2756
2757 Expr *Init = Importer.Import(D->getInitExpr());
2758 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00002759 return nullptr;
2760
Douglas Gregor98c10182010-02-12 22:17:39 +00002761 EnumConstantDecl *ToEnumerator
2762 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2763 Name.getAsIdentifierInfo(), T,
2764 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002765 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002766 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002767 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002768 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002769 return ToEnumerator;
2770}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002771
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002772Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2773 // Import the major distinguishing characteristics of this function.
2774 DeclContext *DC, *LexicalDC;
2775 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002776 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002777 NamedDecl *ToD;
2778 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002779 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002780 if (ToD)
2781 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002782
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002783 // Try to find a function in our own ("to") context with the same name, same
2784 // type, and in the same context as the function we're importing.
2785 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002786 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002787 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002788 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002789 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002790 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2791 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002792 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002793
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002794 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002795 if (FoundFunction->hasExternalFormalLinkage() &&
2796 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002797 if (Importer.IsStructurallyEquivalent(D->getType(),
2798 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002799 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002800 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002801 }
2802
2803 // FIXME: Check for overloading more carefully, e.g., by boosting
2804 // Sema::IsOverload out to the AST library.
2805
2806 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002807 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002808 continue;
2809
2810 // Complain about inconsistent function types.
2811 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002812 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002813 Importer.ToDiag(FoundFunction->getLocation(),
2814 diag::note_odr_value_here)
2815 << FoundFunction->getType();
2816 }
2817 }
2818
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002819 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002820 }
2821
2822 if (!ConflictingDecls.empty()) {
2823 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2824 ConflictingDecls.data(),
2825 ConflictingDecls.size());
2826 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002827 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002828 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002829 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002830
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002831 DeclarationNameInfo NameInfo(Name, Loc);
2832 // Import additional name location/type info.
2833 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2834
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002835 QualType FromTy = D->getType();
2836 bool usedDifferentExceptionSpec = false;
2837
2838 if (const FunctionProtoType *
2839 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2840 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2841 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2842 // FunctionDecl that we are importing the FunctionProtoType for.
2843 // To avoid an infinite recursion when importing, create the FunctionDecl
2844 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00002845 if (FromEPI.ExceptionSpec.SourceDecl ||
2846 FromEPI.ExceptionSpec.SourceTemplate ||
2847 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002848 FunctionProtoType::ExtProtoInfo DefaultEPI;
2849 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00002850 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002851 usedDifferentExceptionSpec = true;
2852 }
2853 }
2854
Douglas Gregorb4964f72010-02-15 23:54:17 +00002855 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002856 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002857 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002858 return nullptr;
2859
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002860 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002861 SmallVector<ParmVarDecl *, 8> Parameters;
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00002862 for (auto P : D->params()) {
2863 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002864 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00002865 return nullptr;
2866
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002867 Parameters.push_back(ToP);
2868 }
2869
2870 // Create the imported function.
2871 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Craig Topper36250ad2014-05-12 05:36:57 +00002872 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002873 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Douglas Gregor00eace12010-02-21 18:29:16 +00002874 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2875 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2876 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002877 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002878 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002879 FromConstructor->isExplicit(),
2880 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002881 D->isImplicit(),
2882 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002883 } else if (isa<CXXDestructorDecl>(D)) {
2884 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2885 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002886 InnerLocStart,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002887 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002888 D->isInlineSpecified(),
2889 D->isImplicit());
2890 } else if (CXXConversionDecl *FromConversion
2891 = dyn_cast<CXXConversionDecl>(D)) {
2892 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2893 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002894 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002895 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002896 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002897 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002898 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002899 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00002900 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2901 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2902 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002903 InnerLocStart,
Douglas Gregora50ad132010-11-29 16:04:58 +00002904 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002905 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002906 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002907 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002908 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002909 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002910 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Sean Callanan59721b32015-04-28 18:41:46 +00002911 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002912 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002913 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002914 D->hasWrittenPrototype(),
2915 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002916 }
John McCall3e11ebe2010-03-15 10:12:16 +00002917
2918 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002919 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002920 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002921 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002922 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2923 ToFunction->setTrivial(D->isTrivial());
2924 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002925 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002926
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002927 // Set the parameters.
2928 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002929 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00002930 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002931 }
David Blaikie9c70e042011-09-21 18:16:56 +00002932 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002933
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002934 if (usedDifferentExceptionSpec) {
2935 // Update FunctionProtoType::ExtProtoInfo.
2936 QualType T = Importer.Import(D->getType());
2937 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002938 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002939 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002940 }
2941
Sean Callanan59721b32015-04-28 18:41:46 +00002942 // Import the body, if any.
2943 if (Stmt *FromBody = D->getBody()) {
2944 if (Stmt *ToBody = Importer.Import(FromBody)) {
2945 ToFunction->setBody(ToBody);
2946 }
2947 }
2948
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002949 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002950
2951 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00002952 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002953
Douglas Gregor43f54792010-02-17 02:12:47 +00002954 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002955}
2956
Douglas Gregor00eace12010-02-21 18:29:16 +00002957Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2958 return VisitFunctionDecl(D);
2959}
2960
2961Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2962 return VisitCXXMethodDecl(D);
2963}
2964
2965Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2966 return VisitCXXMethodDecl(D);
2967}
2968
2969Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2970 return VisitCXXMethodDecl(D);
2971}
2972
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002973static unsigned getFieldIndex(Decl *F) {
2974 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
2975 if (!Owner)
2976 return 0;
2977
2978 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00002979 for (const auto *D : Owner->noload_decls()) {
2980 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002981 return Index;
2982
2983 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2984 ++Index;
2985 }
2986
2987 return Index;
2988}
2989
Douglas Gregor5c73e912010-02-11 00:48:18 +00002990Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2991 // Import the major distinguishing characteristics of a variable.
2992 DeclContext *DC, *LexicalDC;
2993 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002994 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002995 NamedDecl *ToD;
2996 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002997 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002998 if (ToD)
2999 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003000
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003001 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003002 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003003 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003004 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3005 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003006 // For anonymous fields, match up by index.
3007 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
3008 continue;
3009
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003010 if (Importer.IsStructurallyEquivalent(D->getType(),
3011 FoundField->getType())) {
3012 Importer.Imported(D, FoundField);
3013 return FoundField;
3014 }
3015
3016 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3017 << Name << D->getType() << FoundField->getType();
3018 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3019 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003020 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003021 }
3022 }
3023
Douglas Gregorb4964f72010-02-15 23:54:17 +00003024 // Import the type.
3025 QualType T = Importer.Import(D->getType());
3026 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003027 return nullptr;
3028
Douglas Gregor5c73e912010-02-11 00:48:18 +00003029 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3030 Expr *BitWidth = Importer.Import(D->getBitWidth());
3031 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00003032 return nullptr;
3033
Abramo Bagnaradff19302011-03-08 08:55:46 +00003034 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
3035 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00003036 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00003037 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00003038 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00003039 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003040 ToField->setLexicalDeclContext(LexicalDC);
Sean Callanan3a83ea72016-03-03 02:22:05 +00003041 if (Expr *FromInitializer = D->getInClassInitializer()) {
Sean Callananbb33f582016-03-03 01:21:28 +00003042 Expr *ToInitializer = Importer.Import(FromInitializer);
3043 if (ToInitializer)
3044 ToField->setInClassInitializer(ToInitializer);
3045 else
3046 return nullptr;
3047 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003048 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003049 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00003050 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003051 return ToField;
3052}
3053
Francois Pichet783dd6e2010-11-21 06:08:52 +00003054Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
3055 // Import the major distinguishing characteristics of a variable.
3056 DeclContext *DC, *LexicalDC;
3057 DeclarationName Name;
3058 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003059 NamedDecl *ToD;
3060 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003061 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003062 if (ToD)
3063 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003064
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003065 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003066 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003067 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003068 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003069 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003070 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003071 // For anonymous indirect fields, match up by index.
3072 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
3073 continue;
3074
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003075 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003076 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003077 !Name.isEmpty())) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003078 Importer.Imported(D, FoundField);
3079 return FoundField;
3080 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003081
3082 // If there are more anonymous fields to check, continue.
3083 if (!Name && I < N-1)
3084 continue;
3085
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003086 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3087 << Name << D->getType() << FoundField->getType();
3088 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3089 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003090 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003091 }
3092 }
3093
Francois Pichet783dd6e2010-11-21 06:08:52 +00003094 // Import the type.
3095 QualType T = Importer.Import(D->getType());
3096 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003097 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003098
3099 NamedDecl **NamedChain =
3100 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
3101
3102 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00003103 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00003104 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003105 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00003106 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003107 NamedChain[i++] = cast<NamedDecl>(D);
3108 }
3109
3110 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
Aaron Ballman260995b2014-10-15 16:58:18 +00003111 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
3112 NamedChain, D->getChainingSize());
3113
3114 for (const auto *Attr : D->attrs())
3115 ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
3116
Francois Pichet783dd6e2010-11-21 06:08:52 +00003117 ToIndirectField->setAccess(D->getAccess());
3118 ToIndirectField->setLexicalDeclContext(LexicalDC);
3119 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00003120 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003121 return ToIndirectField;
3122}
3123
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003124Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
3125 // Import the major distinguishing characteristics of an ivar.
3126 DeclContext *DC, *LexicalDC;
3127 DeclarationName Name;
3128 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003129 NamedDecl *ToD;
3130 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003131 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003132 if (ToD)
3133 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003134
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003135 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003136 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003137 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003138 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3139 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003140 if (Importer.IsStructurallyEquivalent(D->getType(),
3141 FoundIvar->getType())) {
3142 Importer.Imported(D, FoundIvar);
3143 return FoundIvar;
3144 }
3145
3146 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
3147 << Name << D->getType() << FoundIvar->getType();
3148 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3149 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003150 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003151 }
3152 }
3153
3154 // Import the type.
3155 QualType T = Importer.Import(D->getType());
3156 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003157 return nullptr;
3158
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003159 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3160 Expr *BitWidth = Importer.Import(D->getBitWidth());
3161 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00003162 return nullptr;
3163
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00003164 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
3165 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003166 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003167 Loc, Name.getAsIdentifierInfo(),
3168 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003169 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003170 ToIvar->setLexicalDeclContext(LexicalDC);
3171 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003172 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003173 return ToIvar;
3174
3175}
3176
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003177Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
3178 // Import the major distinguishing characteristics of a variable.
3179 DeclContext *DC, *LexicalDC;
3180 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003181 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003182 NamedDecl *ToD;
3183 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003184 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003185 if (ToD)
3186 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003187
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003188 // Try to find a variable in our own ("to") context with the same name and
3189 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00003190 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00003191 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003192 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003193 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003194 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003195 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003196 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3197 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003198 continue;
3199
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003200 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003201 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00003202 if (FoundVar->hasExternalFormalLinkage() &&
3203 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003204 if (Importer.IsStructurallyEquivalent(D->getType(),
3205 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003206 MergeWithVar = FoundVar;
3207 break;
3208 }
3209
Douglas Gregor56521c52010-02-12 17:23:39 +00003210 const ArrayType *FoundArray
3211 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3212 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00003213 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00003214 if (FoundArray && TArray) {
3215 if (isa<IncompleteArrayType>(FoundArray) &&
3216 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003217 // Import the type.
3218 QualType T = Importer.Import(D->getType());
3219 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003220 return nullptr;
3221
Douglas Gregor56521c52010-02-12 17:23:39 +00003222 FoundVar->setType(T);
3223 MergeWithVar = FoundVar;
3224 break;
3225 } else if (isa<IncompleteArrayType>(TArray) &&
3226 isa<ConstantArrayType>(FoundArray)) {
3227 MergeWithVar = FoundVar;
3228 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003229 }
3230 }
3231
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003232 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003233 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003234 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3235 << FoundVar->getType();
3236 }
3237 }
3238
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003239 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003240 }
3241
3242 if (MergeWithVar) {
3243 // An equivalent variable with external linkage has been found. Link
3244 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003245 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003246
3247 if (VarDecl *DDef = D->getDefinition()) {
3248 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
3249 Importer.ToDiag(ExistingDef->getLocation(),
3250 diag::err_odr_variable_multiple_def)
3251 << Name;
3252 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
3253 } else {
3254 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00003255 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00003256 if (DDef->isInitKnownICE()) {
3257 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
3258 Eval->CheckedICE = true;
3259 Eval->IsICE = DDef->isInitICE();
3260 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003261 }
3262 }
3263
3264 return MergeWithVar;
3265 }
3266
3267 if (!ConflictingDecls.empty()) {
3268 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3269 ConflictingDecls.data(),
3270 ConflictingDecls.size());
3271 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003272 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003273 }
3274 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003275
Douglas Gregorb4964f72010-02-15 23:54:17 +00003276 // Import the type.
3277 QualType T = Importer.Import(D->getType());
3278 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003279 return nullptr;
3280
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003281 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003282 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00003283 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
3284 Importer.Import(D->getInnerLocStart()),
3285 Loc, Name.getAsIdentifierInfo(),
3286 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003287 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00003288 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003289 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003290 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003291 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003292 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003293
Sean Callanan59721b32015-04-28 18:41:46 +00003294 if (!D->isFileVarDecl() &&
3295 D->isUsed())
3296 ToVar->setIsUsed();
3297
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003298 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00003299 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00003300 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003301
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003302 return ToVar;
3303}
3304
Douglas Gregor8b228d72010-02-17 21:22:52 +00003305Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
3306 // Parameters are created in the translation unit's context, then moved
3307 // into the function declaration's context afterward.
3308 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3309
3310 // Import the name of this declaration.
3311 DeclarationName Name = Importer.Import(D->getDeclName());
3312 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003313 return nullptr;
3314
Douglas Gregor8b228d72010-02-17 21:22:52 +00003315 // Import the location of this declaration.
3316 SourceLocation Loc = Importer.Import(D->getLocation());
3317
3318 // Import the parameter's type.
3319 QualType T = Importer.Import(D->getType());
3320 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003321 return nullptr;
3322
Douglas Gregor8b228d72010-02-17 21:22:52 +00003323 // Create the imported parameter.
3324 ImplicitParamDecl *ToParm
3325 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
3326 Loc, Name.getAsIdentifierInfo(),
3327 T);
3328 return Importer.Imported(D, ToParm);
3329}
3330
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003331Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
3332 // Parameters are created in the translation unit's context, then moved
3333 // into the function declaration's context afterward.
3334 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3335
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003336 // Import the name of this declaration.
3337 DeclarationName Name = Importer.Import(D->getDeclName());
3338 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003339 return nullptr;
3340
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003341 // Import the location of this declaration.
3342 SourceLocation Loc = Importer.Import(D->getLocation());
3343
3344 // Import the parameter's type.
3345 QualType T = Importer.Import(D->getType());
3346 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003347 return nullptr;
3348
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003349 // Create the imported parameter.
3350 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3351 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003352 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003353 Loc, Name.getAsIdentifierInfo(),
3354 T, TInfo, D->getStorageClass(),
Craig Topper36250ad2014-05-12 05:36:57 +00003355 /*FIXME: Default argument*/nullptr);
John McCallf3cd6652010-03-12 18:31:32 +00003356 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Sean Callanan59721b32015-04-28 18:41:46 +00003357
3358 if (D->isUsed())
3359 ToParm->setIsUsed();
3360
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003361 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003362}
3363
Douglas Gregor43f54792010-02-17 02:12:47 +00003364Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3365 // Import the major distinguishing characteristics of a method.
3366 DeclContext *DC, *LexicalDC;
3367 DeclarationName Name;
3368 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003369 NamedDecl *ToD;
3370 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003371 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003372 if (ToD)
3373 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003374
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003375 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003376 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003377 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3378 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003379 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3380 continue;
3381
3382 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003383 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3384 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003385 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003386 << D->isInstanceMethod() << Name << D->getReturnType()
3387 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00003388 Importer.ToDiag(FoundMethod->getLocation(),
3389 diag::note_odr_objc_method_here)
3390 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003391 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003392 }
3393
3394 // Check the number of parameters.
3395 if (D->param_size() != FoundMethod->param_size()) {
3396 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3397 << D->isInstanceMethod() << Name
3398 << D->param_size() << FoundMethod->param_size();
3399 Importer.ToDiag(FoundMethod->getLocation(),
3400 diag::note_odr_objc_method_here)
3401 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003402 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003403 }
3404
3405 // Check parameter types.
3406 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
3407 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3408 P != PEnd; ++P, ++FoundP) {
3409 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3410 (*FoundP)->getType())) {
3411 Importer.FromDiag((*P)->getLocation(),
3412 diag::err_odr_objc_method_param_type_inconsistent)
3413 << D->isInstanceMethod() << Name
3414 << (*P)->getType() << (*FoundP)->getType();
3415 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3416 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003417 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003418 }
3419 }
3420
3421 // Check variadic/non-variadic.
3422 // Check the number of parameters.
3423 if (D->isVariadic() != FoundMethod->isVariadic()) {
3424 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3425 << D->isInstanceMethod() << Name;
3426 Importer.ToDiag(FoundMethod->getLocation(),
3427 diag::note_odr_objc_method_here)
3428 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003429 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003430 }
3431
3432 // FIXME: Any other bits we need to merge?
3433 return Importer.Imported(D, FoundMethod);
3434 }
3435 }
3436
3437 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00003438 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003439 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003440 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003441
Alp Toker314cc812014-01-25 16:55:45 +00003442 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00003443
Alp Toker314cc812014-01-25 16:55:45 +00003444 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
3445 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3446 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3447 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3448 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003449
3450 // FIXME: When we decide to merge method definitions, we'll need to
3451 // deal with implicit parameters.
3452
3453 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003454 SmallVector<ParmVarDecl *, 5> ToParams;
Aaron Ballman43b68be2014-03-07 17:50:17 +00003455 for (auto *FromP : D->params()) {
3456 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00003457 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003458 return nullptr;
3459
Douglas Gregor43f54792010-02-17 02:12:47 +00003460 ToParams.push_back(ToP);
3461 }
3462
3463 // Set the parameters.
3464 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3465 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003466 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003467 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003468 SmallVector<SourceLocation, 12> SelLocs;
3469 D->getSelectorLocs(SelLocs);
3470 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003471
3472 ToMethod->setLexicalDeclContext(LexicalDC);
3473 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003474 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003475 return ToMethod;
3476}
3477
Douglas Gregor85f3f952015-07-07 03:57:15 +00003478Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3479 // Import the major distinguishing characteristics of a category.
3480 DeclContext *DC, *LexicalDC;
3481 DeclarationName Name;
3482 SourceLocation Loc;
3483 NamedDecl *ToD;
3484 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3485 return nullptr;
3486 if (ToD)
3487 return ToD;
3488
3489 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3490 if (!BoundInfo)
3491 return nullptr;
3492
3493 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
3494 Importer.getToContext(), DC,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00003495 D->getVariance(),
3496 Importer.Import(D->getVarianceLoc()),
Douglas Gregore83b9562015-07-07 03:57:53 +00003497 D->getIndex(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003498 Importer.Import(D->getLocation()),
3499 Name.getAsIdentifierInfo(),
3500 Importer.Import(D->getColonLoc()),
3501 BoundInfo);
3502 Importer.Imported(D, Result);
3503 Result->setLexicalDeclContext(LexicalDC);
3504 return Result;
3505}
3506
Douglas Gregor84c51c32010-02-18 01:47:50 +00003507Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3508 // Import the major distinguishing characteristics of a category.
3509 DeclContext *DC, *LexicalDC;
3510 DeclarationName Name;
3511 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003512 NamedDecl *ToD;
3513 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003514 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003515 if (ToD)
3516 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003517
Douglas Gregor84c51c32010-02-18 01:47:50 +00003518 ObjCInterfaceDecl *ToInterface
3519 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3520 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00003521 return nullptr;
3522
Douglas Gregor84c51c32010-02-18 01:47:50 +00003523 // Determine if we've already encountered this category.
3524 ObjCCategoryDecl *MergeWithCategory
3525 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3526 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3527 if (!ToCategory) {
3528 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003529 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003530 Loc,
3531 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003532 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003533 ToInterface,
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003534 /*TypeParamList=*/nullptr,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003535 Importer.Import(D->getIvarLBraceLoc()),
3536 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003537 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003538 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003539 Importer.Imported(D, ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003540 // Import the type parameter list after calling Imported, to avoid
3541 // loops when bringing in their DeclContext.
3542 ToCategory->setTypeParamList(ImportObjCTypeParamList(
3543 D->getTypeParamList()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003544
Douglas Gregor84c51c32010-02-18 01:47:50 +00003545 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003546 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3547 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003548 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3549 = D->protocol_loc_begin();
3550 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3551 FromProtoEnd = D->protocol_end();
3552 FromProto != FromProtoEnd;
3553 ++FromProto, ++FromProtoLoc) {
3554 ObjCProtocolDecl *ToProto
3555 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3556 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00003557 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003558 Protocols.push_back(ToProto);
3559 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3560 }
3561
3562 // FIXME: If we're merging, make sure that the protocol list is the same.
3563 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3564 ProtocolLocs.data(), Importer.getToContext());
3565
3566 } else {
3567 Importer.Imported(D, ToCategory);
3568 }
3569
3570 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003571 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003572
3573 // If we have an implementation, import it as well.
3574 if (D->getImplementation()) {
3575 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003576 = cast_or_null<ObjCCategoryImplDecl>(
3577 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003578 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00003579 return nullptr;
3580
Douglas Gregor84c51c32010-02-18 01:47:50 +00003581 ToCategory->setImplementation(Impl);
3582 }
3583
3584 return ToCategory;
3585}
3586
Douglas Gregor2aa53772012-01-24 17:42:07 +00003587bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3588 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003589 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003590 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003591 if (shouldForceImportDeclContext(Kind))
3592 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003593 return false;
3594 }
3595
3596 // Start the protocol definition
3597 To->startDefinition();
3598
3599 // Import protocols
3600 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3601 SmallVector<SourceLocation, 4> ProtocolLocs;
3602 ObjCProtocolDecl::protocol_loc_iterator
3603 FromProtoLoc = From->protocol_loc_begin();
3604 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3605 FromProtoEnd = From->protocol_end();
3606 FromProto != FromProtoEnd;
3607 ++FromProto, ++FromProtoLoc) {
3608 ObjCProtocolDecl *ToProto
3609 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3610 if (!ToProto)
3611 return true;
3612 Protocols.push_back(ToProto);
3613 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3614 }
3615
3616 // FIXME: If we're merging, make sure that the protocol list is the same.
3617 To->setProtocolList(Protocols.data(), Protocols.size(),
3618 ProtocolLocs.data(), Importer.getToContext());
3619
Douglas Gregor2e15c842012-02-01 21:00:38 +00003620 if (shouldForceImportDeclContext(Kind)) {
3621 // Import all of the members of this protocol.
3622 ImportDeclContext(From, /*ForceImport=*/true);
3623 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003624 return false;
3625}
3626
Douglas Gregor98d156a2010-02-17 16:12:00 +00003627Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003628 // If this protocol has a definition in the translation unit we're coming
3629 // from, but this particular declaration is not that definition, import the
3630 // definition and map to that.
3631 ObjCProtocolDecl *Definition = D->getDefinition();
3632 if (Definition && Definition != D) {
3633 Decl *ImportedDef = Importer.Import(Definition);
3634 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003635 return nullptr;
3636
Douglas Gregor2aa53772012-01-24 17:42:07 +00003637 return Importer.Imported(D, ImportedDef);
3638 }
3639
Douglas Gregor84c51c32010-02-18 01:47:50 +00003640 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003641 DeclContext *DC, *LexicalDC;
3642 DeclarationName Name;
3643 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003644 NamedDecl *ToD;
3645 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003646 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003647 if (ToD)
3648 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00003649
Craig Topper36250ad2014-05-12 05:36:57 +00003650 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003651 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003652 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003653 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3654 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003655 continue;
3656
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003657 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003658 break;
3659 }
3660
3661 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003662 if (!ToProto) {
3663 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3664 Name.getAsIdentifierInfo(), Loc,
3665 Importer.Import(D->getAtStartLoc()),
Craig Topper36250ad2014-05-12 05:36:57 +00003666 /*PrevDecl=*/nullptr);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003667 ToProto->setLexicalDeclContext(LexicalDC);
3668 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003669 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003670
3671 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003672
Douglas Gregor2aa53772012-01-24 17:42:07 +00003673 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00003674 return nullptr;
3675
Douglas Gregor98d156a2010-02-17 16:12:00 +00003676 return ToProto;
3677}
3678
Sean Callanan0aae0412014-12-10 00:00:37 +00003679Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3680 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3681 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3682
3683 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3684 SourceLocation LangLoc = Importer.Import(D->getLocation());
3685
3686 bool HasBraces = D->hasBraces();
3687
Sean Callananb12a8552014-12-10 21:22:20 +00003688 LinkageSpecDecl *ToLinkageSpec =
3689 LinkageSpecDecl::Create(Importer.getToContext(),
3690 DC,
3691 ExternLoc,
3692 LangLoc,
3693 D->getLanguage(),
3694 HasBraces);
Sean Callanan0aae0412014-12-10 00:00:37 +00003695
3696 if (HasBraces) {
3697 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3698 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3699 }
3700
3701 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3702 LexicalDC->addDeclInternal(ToLinkageSpec);
3703
3704 Importer.Imported(D, ToLinkageSpec);
3705
3706 return ToLinkageSpec;
3707}
3708
Douglas Gregor2aa53772012-01-24 17:42:07 +00003709bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3710 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003711 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003712 if (To->getDefinition()) {
3713 // Check consistency of superclass.
3714 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3715 if (FromSuper) {
3716 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3717 if (!FromSuper)
3718 return true;
3719 }
3720
3721 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3722 if ((bool)FromSuper != (bool)ToSuper ||
3723 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3724 Importer.ToDiag(To->getLocation(),
3725 diag::err_odr_objc_superclass_inconsistent)
3726 << To->getDeclName();
3727 if (ToSuper)
3728 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3729 << To->getSuperClass()->getDeclName();
3730 else
3731 Importer.ToDiag(To->getLocation(),
3732 diag::note_odr_objc_missing_superclass);
3733 if (From->getSuperClass())
3734 Importer.FromDiag(From->getSuperClassLoc(),
3735 diag::note_odr_objc_superclass)
3736 << From->getSuperClass()->getDeclName();
3737 else
3738 Importer.FromDiag(From->getLocation(),
3739 diag::note_odr_objc_missing_superclass);
3740 }
3741
Douglas Gregor2e15c842012-02-01 21:00:38 +00003742 if (shouldForceImportDeclContext(Kind))
3743 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003744 return false;
3745 }
3746
3747 // Start the definition.
3748 To->startDefinition();
3749
3750 // If this class has a superclass, import it.
3751 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00003752 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3753 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00003754 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00003755
3756 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003757 }
3758
3759 // Import protocols
3760 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3761 SmallVector<SourceLocation, 4> ProtocolLocs;
3762 ObjCInterfaceDecl::protocol_loc_iterator
3763 FromProtoLoc = From->protocol_loc_begin();
3764
3765 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3766 FromProtoEnd = From->protocol_end();
3767 FromProto != FromProtoEnd;
3768 ++FromProto, ++FromProtoLoc) {
3769 ObjCProtocolDecl *ToProto
3770 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3771 if (!ToProto)
3772 return true;
3773 Protocols.push_back(ToProto);
3774 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3775 }
3776
3777 // FIXME: If we're merging, make sure that the protocol list is the same.
3778 To->setProtocolList(Protocols.data(), Protocols.size(),
3779 ProtocolLocs.data(), Importer.getToContext());
3780
3781 // Import categories. When the categories themselves are imported, they'll
3782 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00003783 for (auto *Cat : From->known_categories())
3784 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003785
Douglas Gregor2aa53772012-01-24 17:42:07 +00003786 // If we have an @implementation, import it as well.
3787 if (From->getImplementation()) {
3788 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3789 Importer.Import(From->getImplementation()));
3790 if (!Impl)
3791 return true;
3792
3793 To->setImplementation(Impl);
3794 }
3795
Douglas Gregor2e15c842012-02-01 21:00:38 +00003796 if (shouldForceImportDeclContext(Kind)) {
3797 // Import all of the members of this class.
3798 ImportDeclContext(From, /*ForceImport=*/true);
3799 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003800 return false;
3801}
3802
Douglas Gregor85f3f952015-07-07 03:57:15 +00003803ObjCTypeParamList *
3804ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3805 if (!list)
3806 return nullptr;
3807
3808 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
3809 for (auto fromTypeParam : *list) {
3810 auto toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3811 Importer.Import(fromTypeParam));
3812 if (!toTypeParam)
3813 return nullptr;
3814
3815 toTypeParams.push_back(toTypeParam);
3816 }
3817
3818 return ObjCTypeParamList::create(Importer.getToContext(),
3819 Importer.Import(list->getLAngleLoc()),
3820 toTypeParams,
3821 Importer.Import(list->getRAngleLoc()));
3822}
3823
Douglas Gregor45635322010-02-16 01:20:57 +00003824Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003825 // If this class has a definition in the translation unit we're coming from,
3826 // but this particular declaration is not that definition, import the
3827 // definition and map to that.
3828 ObjCInterfaceDecl *Definition = D->getDefinition();
3829 if (Definition && Definition != D) {
3830 Decl *ImportedDef = Importer.Import(Definition);
3831 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003832 return nullptr;
3833
Douglas Gregor2aa53772012-01-24 17:42:07 +00003834 return Importer.Imported(D, ImportedDef);
3835 }
3836
Douglas Gregor45635322010-02-16 01:20:57 +00003837 // Import the major distinguishing characteristics of an @interface.
3838 DeclContext *DC, *LexicalDC;
3839 DeclarationName Name;
3840 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003841 NamedDecl *ToD;
3842 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003843 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003844 if (ToD)
3845 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00003846
Douglas Gregor2aa53772012-01-24 17:42:07 +00003847 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00003848 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003849 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003850 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003851 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3852 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003853 continue;
3854
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003855 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00003856 break;
3857 }
3858
Douglas Gregor2aa53772012-01-24 17:42:07 +00003859 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003860 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003861 if (!ToIface) {
3862 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3863 Importer.Import(D->getAtStartLoc()),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003864 Name.getAsIdentifierInfo(),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003865 /*TypeParamList=*/nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00003866 /*PrevDecl=*/nullptr, Loc,
Douglas Gregor2aa53772012-01-24 17:42:07 +00003867 D->isImplicitInterfaceDecl());
3868 ToIface->setLexicalDeclContext(LexicalDC);
3869 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003870 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003871 Importer.Imported(D, ToIface);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003872 // Import the type parameter list after calling Imported, to avoid
3873 // loops when bringing in their DeclContext.
3874 ToIface->setTypeParamList(ImportObjCTypeParamList(
3875 D->getTypeParamListAsWritten()));
Douglas Gregor45635322010-02-16 01:20:57 +00003876
Douglas Gregor2aa53772012-01-24 17:42:07 +00003877 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00003878 return nullptr;
3879
Douglas Gregor98d156a2010-02-17 16:12:00 +00003880 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003881}
3882
Douglas Gregor4da9d682010-12-07 15:32:12 +00003883Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3884 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3885 Importer.Import(D->getCategoryDecl()));
3886 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00003887 return nullptr;
3888
Douglas Gregor4da9d682010-12-07 15:32:12 +00003889 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3890 if (!ToImpl) {
3891 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3892 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003893 return nullptr;
3894
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003895 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003896 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003897 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003898 Category->getClassInterface(),
3899 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003900 Importer.Import(D->getAtStartLoc()),
3901 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003902
3903 DeclContext *LexicalDC = DC;
3904 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3905 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3906 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003907 return nullptr;
3908
Douglas Gregor4da9d682010-12-07 15:32:12 +00003909 ToImpl->setLexicalDeclContext(LexicalDC);
3910 }
3911
Sean Callanan95e74be2011-10-21 02:57:43 +00003912 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003913 Category->setImplementation(ToImpl);
3914 }
3915
3916 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003917 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003918 return ToImpl;
3919}
3920
Douglas Gregorda8025c2010-12-07 01:26:03 +00003921Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3922 // Find the corresponding interface.
3923 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3924 Importer.Import(D->getClassInterface()));
3925 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00003926 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003927
3928 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00003929 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003930 if (D->getSuperClass()) {
3931 Super = cast_or_null<ObjCInterfaceDecl>(
3932 Importer.Import(D->getSuperClass()));
3933 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00003934 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003935 }
3936
3937 ObjCImplementationDecl *Impl = Iface->getImplementation();
3938 if (!Impl) {
3939 // We haven't imported an implementation yet. Create a new @implementation
3940 // now.
3941 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3942 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003943 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003944 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003945 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00003946 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003947 Importer.Import(D->getIvarLBraceLoc()),
3948 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003949
3950 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3951 DeclContext *LexicalDC
3952 = Importer.ImportContext(D->getLexicalDeclContext());
3953 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003954 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003955 Impl->setLexicalDeclContext(LexicalDC);
3956 }
3957
3958 // Associate the implementation with the class it implements.
3959 Iface->setImplementation(Impl);
3960 Importer.Imported(D, Iface->getImplementation());
3961 } else {
3962 Importer.Imported(D, Iface->getImplementation());
3963
3964 // Verify that the existing @implementation has the same superclass.
3965 if ((Super && !Impl->getSuperClass()) ||
3966 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00003967 (Super && Impl->getSuperClass() &&
3968 !declaresSameEntity(Super->getCanonicalDecl(),
3969 Impl->getSuperClass()))) {
3970 Importer.ToDiag(Impl->getLocation(),
3971 diag::err_odr_objc_superclass_inconsistent)
3972 << Iface->getDeclName();
3973 // FIXME: It would be nice to have the location of the superclass
3974 // below.
3975 if (Impl->getSuperClass())
3976 Importer.ToDiag(Impl->getLocation(),
3977 diag::note_odr_objc_superclass)
3978 << Impl->getSuperClass()->getDeclName();
3979 else
3980 Importer.ToDiag(Impl->getLocation(),
3981 diag::note_odr_objc_missing_superclass);
3982 if (D->getSuperClass())
3983 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003984 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00003985 << D->getSuperClass()->getDeclName();
3986 else
3987 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003988 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00003989 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003990 }
3991 }
3992
3993 // Import all of the members of this @implementation.
3994 ImportDeclContext(D);
3995
3996 return Impl;
3997}
3998
Douglas Gregora11c4582010-02-17 18:02:10 +00003999Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
4000 // Import the major distinguishing characteristics of an @property.
4001 DeclContext *DC, *LexicalDC;
4002 DeclarationName Name;
4003 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004004 NamedDecl *ToD;
4005 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004006 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004007 if (ToD)
4008 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004009
4010 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004011 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004012 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004013 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004014 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004015 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004016 // Check property types.
4017 if (!Importer.IsStructurallyEquivalent(D->getType(),
4018 FoundProp->getType())) {
4019 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
4020 << Name << D->getType() << FoundProp->getType();
4021 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4022 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00004023 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004024 }
4025
4026 // FIXME: Check property attributes, getters, setters, etc.?
4027
4028 // Consider these properties to be equivalent.
4029 Importer.Imported(D, FoundProp);
4030 return FoundProp;
4031 }
4032 }
4033
4034 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00004035 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
4036 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00004037 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004038
4039 // Create the new property.
4040 ObjCPropertyDecl *ToProperty
4041 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
4042 Name.getAsIdentifierInfo(),
4043 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00004044 Importer.Import(D->getLParenLoc()),
Douglas Gregor813a0662015-06-19 18:14:38 +00004045 Importer.Import(D->getType()),
4046 TSI,
Douglas Gregora11c4582010-02-17 18:02:10 +00004047 D->getPropertyImplementation());
4048 Importer.Imported(D, ToProperty);
4049 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004050 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004051
4052 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004053 ToProperty->setPropertyAttributesAsWritten(
4054 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00004055 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
4056 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
4057 ToProperty->setGetterMethodDecl(
4058 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
4059 ToProperty->setSetterMethodDecl(
4060 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
4061 ToProperty->setPropertyIvarDecl(
4062 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
4063 return ToProperty;
4064}
4065
Douglas Gregor14a49e22010-12-07 18:32:03 +00004066Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4067 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
4068 Importer.Import(D->getPropertyDecl()));
4069 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00004070 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004071
4072 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4073 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004074 return nullptr;
4075
Douglas Gregor14a49e22010-12-07 18:32:03 +00004076 // Import the lexical declaration context.
4077 DeclContext *LexicalDC = DC;
4078 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4079 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4080 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004081 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004082 }
4083
4084 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
4085 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00004086 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004087
4088 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004089 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004090 if (D->getPropertyIvarDecl()) {
4091 Ivar = cast_or_null<ObjCIvarDecl>(
4092 Importer.Import(D->getPropertyIvarDecl()));
4093 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00004094 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004095 }
4096
4097 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004098 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4099 Property->getQueryKind());
Douglas Gregor14a49e22010-12-07 18:32:03 +00004100 if (!ToImpl) {
4101 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
4102 Importer.Import(D->getLocStart()),
4103 Importer.Import(D->getLocation()),
4104 Property,
4105 D->getPropertyImplementation(),
4106 Ivar,
4107 Importer.Import(D->getPropertyIvarDeclLoc()));
4108 ToImpl->setLexicalDeclContext(LexicalDC);
4109 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00004110 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004111 } else {
4112 // Check that we have the same kind of property implementation (@synthesize
4113 // vs. @dynamic).
4114 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
4115 Importer.ToDiag(ToImpl->getLocation(),
4116 diag::err_odr_objc_property_impl_kind_inconsistent)
4117 << Property->getDeclName()
4118 << (ToImpl->getPropertyImplementation()
4119 == ObjCPropertyImplDecl::Dynamic);
4120 Importer.FromDiag(D->getLocation(),
4121 diag::note_odr_objc_property_impl_kind)
4122 << D->getPropertyDecl()->getDeclName()
4123 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00004124 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004125 }
4126
4127 // For @synthesize, check that we have the same
4128 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4129 Ivar != ToImpl->getPropertyIvarDecl()) {
4130 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
4131 diag::err_odr_objc_synthesize_ivar_inconsistent)
4132 << Property->getDeclName()
4133 << ToImpl->getPropertyIvarDecl()->getDeclName()
4134 << Ivar->getDeclName();
4135 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
4136 diag::note_odr_objc_synthesize_ivar_here)
4137 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00004138 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004139 }
4140
4141 // Merge the existing implementation with the new implementation.
4142 Importer.Imported(D, ToImpl);
4143 }
4144
4145 return ToImpl;
4146}
4147
Douglas Gregora082a492010-11-30 19:14:50 +00004148Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
4149 // For template arguments, we adopt the translation unit as our declaration
4150 // context. This context will be fixed when the actual template declaration
4151 // is created.
4152
4153 // FIXME: Import default argument.
4154 return TemplateTypeParmDecl::Create(Importer.getToContext(),
4155 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00004156 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004157 Importer.Import(D->getLocation()),
4158 D->getDepth(),
4159 D->getIndex(),
4160 Importer.Import(D->getIdentifier()),
4161 D->wasDeclaredWithTypename(),
4162 D->isParameterPack());
4163}
4164
4165Decl *
4166ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
4167 // Import the name of this declaration.
4168 DeclarationName Name = Importer.Import(D->getDeclName());
4169 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004170 return nullptr;
4171
Douglas Gregora082a492010-11-30 19:14:50 +00004172 // Import the location of this declaration.
4173 SourceLocation Loc = Importer.Import(D->getLocation());
4174
4175 // Import the type of this declaration.
4176 QualType T = Importer.Import(D->getType());
4177 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004178 return nullptr;
4179
Douglas Gregora082a492010-11-30 19:14:50 +00004180 // Import type-source information.
4181 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4182 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00004183 return nullptr;
4184
Douglas Gregora082a492010-11-30 19:14:50 +00004185 // FIXME: Import default argument.
4186
4187 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
4188 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004189 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004190 Loc, D->getDepth(), D->getPosition(),
4191 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00004192 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00004193}
4194
4195Decl *
4196ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4197 // Import the name of this declaration.
4198 DeclarationName Name = Importer.Import(D->getDeclName());
4199 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004200 return nullptr;
4201
Douglas Gregora082a492010-11-30 19:14:50 +00004202 // Import the location of this declaration.
4203 SourceLocation Loc = Importer.Import(D->getLocation());
4204
4205 // Import template parameters.
4206 TemplateParameterList *TemplateParams
4207 = ImportTemplateParameterList(D->getTemplateParameters());
4208 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004209 return nullptr;
4210
Douglas Gregora082a492010-11-30 19:14:50 +00004211 // FIXME: Import default argument.
4212
4213 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
4214 Importer.getToContext().getTranslationUnitDecl(),
4215 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00004216 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00004217 Name.getAsIdentifierInfo(),
4218 TemplateParams);
4219}
4220
4221Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
4222 // If this record has a definition in the translation unit we're coming from,
4223 // but this particular declaration is not that definition, import the
4224 // definition and map to that.
4225 CXXRecordDecl *Definition
4226 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
4227 if (Definition && Definition != D->getTemplatedDecl()) {
4228 Decl *ImportedDef
4229 = Importer.Import(Definition->getDescribedClassTemplate());
4230 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004231 return nullptr;
4232
Douglas Gregora082a492010-11-30 19:14:50 +00004233 return Importer.Imported(D, ImportedDef);
4234 }
4235
4236 // Import the major distinguishing characteristics of this class template.
4237 DeclContext *DC, *LexicalDC;
4238 DeclarationName Name;
4239 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004240 NamedDecl *ToD;
4241 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004242 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004243 if (ToD)
4244 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004245
Douglas Gregora082a492010-11-30 19:14:50 +00004246 // We may already have a template of the same name; try to find and match it.
4247 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004248 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004249 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004250 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004251 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4252 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00004253 continue;
4254
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004255 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00004256 if (ClassTemplateDecl *FoundTemplate
4257 = dyn_cast<ClassTemplateDecl>(Found)) {
4258 if (IsStructuralMatch(D, FoundTemplate)) {
4259 // The class templates structurally match; call it the same template.
4260 // FIXME: We may be filling in a forward declaration here. Handle
4261 // this case!
4262 Importer.Imported(D->getTemplatedDecl(),
4263 FoundTemplate->getTemplatedDecl());
4264 return Importer.Imported(D, FoundTemplate);
4265 }
4266 }
4267
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004268 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00004269 }
4270
4271 if (!ConflictingDecls.empty()) {
4272 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4273 ConflictingDecls.data(),
4274 ConflictingDecls.size());
4275 }
4276
4277 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004278 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00004279 }
4280
4281 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
4282
4283 // Create the declaration that is being templated.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004284 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4285 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregora082a492010-11-30 19:14:50 +00004286 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
4287 DTemplated->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004288 DC, StartLoc, IdLoc,
4289 Name.getAsIdentifierInfo());
Douglas Gregora082a492010-11-30 19:14:50 +00004290 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregor14454802011-02-25 02:25:35 +00004291 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregora082a492010-11-30 19:14:50 +00004292 D2Templated->setLexicalDeclContext(LexicalDC);
4293
4294 // Create the class template declaration itself.
4295 TemplateParameterList *TemplateParams
4296 = ImportTemplateParameterList(D->getTemplateParameters());
4297 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004298 return nullptr;
4299
Douglas Gregora082a492010-11-30 19:14:50 +00004300 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4301 Loc, Name, TemplateParams,
4302 D2Templated,
Craig Topper36250ad2014-05-12 05:36:57 +00004303 /*PrevDecl=*/nullptr);
Douglas Gregora082a492010-11-30 19:14:50 +00004304 D2Templated->setDescribedClassTemplate(D2);
4305
4306 D2->setAccess(D->getAccess());
4307 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004308 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004309
4310 // Note the relationship between the class templates.
4311 Importer.Imported(D, D2);
4312 Importer.Imported(DTemplated, D2Templated);
4313
John McCallf937c022011-10-07 06:10:15 +00004314 if (DTemplated->isCompleteDefinition() &&
4315 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004316 // FIXME: Import definition!
4317 }
4318
4319 return D2;
4320}
4321
Douglas Gregore2e50d332010-12-01 01:36:18 +00004322Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4323 ClassTemplateSpecializationDecl *D) {
4324 // If this record has a definition in the translation unit we're coming from,
4325 // but this particular declaration is not that definition, import the
4326 // definition and map to that.
4327 TagDecl *Definition = D->getDefinition();
4328 if (Definition && Definition != D) {
4329 Decl *ImportedDef = Importer.Import(Definition);
4330 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004331 return nullptr;
4332
Douglas Gregore2e50d332010-12-01 01:36:18 +00004333 return Importer.Imported(D, ImportedDef);
4334 }
4335
4336 ClassTemplateDecl *ClassTemplate
4337 = cast_or_null<ClassTemplateDecl>(Importer.Import(
4338 D->getSpecializedTemplate()));
4339 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004340 return nullptr;
4341
Douglas Gregore2e50d332010-12-01 01:36:18 +00004342 // Import the context of this declaration.
4343 DeclContext *DC = ClassTemplate->getDeclContext();
4344 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004345 return nullptr;
4346
Douglas Gregore2e50d332010-12-01 01:36:18 +00004347 DeclContext *LexicalDC = DC;
4348 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4349 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4350 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004351 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004352 }
4353
4354 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004355 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4356 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004357
4358 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004359 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004360 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4361 D->getTemplateArgs().size(),
4362 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004363 return nullptr;
4364
Douglas Gregore2e50d332010-12-01 01:36:18 +00004365 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004366 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004367 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004368 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004369 if (D2) {
4370 // We already have a class template specialization with these template
4371 // arguments.
4372
4373 // FIXME: Check for specialization vs. instantiation errors.
4374
4375 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004376 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004377 // The record types structurally match, or the "from" translation
4378 // unit only had a forward declaration anyway; call it the same
4379 // function.
4380 return Importer.Imported(D, FoundDef);
4381 }
4382 }
4383 } else {
4384 // Create a new specialization.
4385 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4386 D->getTagKind(), DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004387 StartLoc, IdLoc,
4388 ClassTemplate,
Douglas Gregore2e50d332010-12-01 01:36:18 +00004389 TemplateArgs.data(),
4390 TemplateArgs.size(),
Craig Topper36250ad2014-05-12 05:36:57 +00004391 /*PrevDecl=*/nullptr);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004392 D2->setSpecializationKind(D->getSpecializationKind());
4393
4394 // Add this specialization to the class template.
4395 ClassTemplate->AddSpecialization(D2, InsertPos);
4396
4397 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004398 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00004399
4400 // Add the specialization to this context.
4401 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004402 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004403 }
4404 Importer.Imported(D, D2);
4405
John McCallf937c022011-10-07 06:10:15 +00004406 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004407 return nullptr;
4408
Douglas Gregore2e50d332010-12-01 01:36:18 +00004409 return D2;
4410}
4411
Larisse Voufo39a1e502013-08-06 01:03:05 +00004412Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4413 // If this variable has a definition in the translation unit we're coming
4414 // from,
4415 // but this particular declaration is not that definition, import the
4416 // definition and map to that.
4417 VarDecl *Definition =
4418 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4419 if (Definition && Definition != D->getTemplatedDecl()) {
4420 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4421 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004422 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004423
4424 return Importer.Imported(D, ImportedDef);
4425 }
4426
4427 // Import the major distinguishing characteristics of this variable template.
4428 DeclContext *DC, *LexicalDC;
4429 DeclarationName Name;
4430 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004431 NamedDecl *ToD;
4432 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004433 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004434 if (ToD)
4435 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004436
4437 // We may already have a template of the same name; try to find and match it.
4438 assert(!DC->isFunctionOrMethod() &&
4439 "Variable templates cannot be declared at function scope");
4440 SmallVector<NamedDecl *, 4> ConflictingDecls;
4441 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004442 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004443 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4444 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4445 continue;
4446
4447 Decl *Found = FoundDecls[I];
4448 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
4449 if (IsStructuralMatch(D, FoundTemplate)) {
4450 // The variable templates structurally match; call it the same template.
4451 Importer.Imported(D->getTemplatedDecl(),
4452 FoundTemplate->getTemplatedDecl());
4453 return Importer.Imported(D, FoundTemplate);
4454 }
4455 }
4456
4457 ConflictingDecls.push_back(FoundDecls[I]);
4458 }
4459
4460 if (!ConflictingDecls.empty()) {
4461 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4462 ConflictingDecls.data(),
4463 ConflictingDecls.size());
4464 }
4465
4466 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004467 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004468
4469 VarDecl *DTemplated = D->getTemplatedDecl();
4470
4471 // Import the type.
4472 QualType T = Importer.Import(DTemplated->getType());
4473 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004474 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004475
4476 // Create the declaration that is being templated.
4477 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4478 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
4479 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo());
4480 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc,
4481 IdLoc, Name.getAsIdentifierInfo(), T,
4482 TInfo, DTemplated->getStorageClass());
4483 D2Templated->setAccess(DTemplated->getAccess());
4484 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
4485 D2Templated->setLexicalDeclContext(LexicalDC);
4486
4487 // Importer.Imported(DTemplated, D2Templated);
4488 // LexicalDC->addDeclInternal(D2Templated);
4489
4490 // Merge the initializer.
4491 if (ImportDefinition(DTemplated, D2Templated))
Craig Topper36250ad2014-05-12 05:36:57 +00004492 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004493
4494 // Create the variable template declaration itself.
4495 TemplateParameterList *TemplateParams =
4496 ImportTemplateParameterList(D->getTemplateParameters());
4497 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004498 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004499
4500 VarTemplateDecl *D2 = VarTemplateDecl::Create(
Richard Smithbeef3452014-01-16 23:39:20 +00004501 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004502 D2Templated->setDescribedVarTemplate(D2);
4503
4504 D2->setAccess(D->getAccess());
4505 D2->setLexicalDeclContext(LexicalDC);
4506 LexicalDC->addDeclInternal(D2);
4507
4508 // Note the relationship between the variable templates.
4509 Importer.Imported(D, D2);
4510 Importer.Imported(DTemplated, D2Templated);
4511
4512 if (DTemplated->isThisDeclarationADefinition() &&
4513 !D2Templated->isThisDeclarationADefinition()) {
4514 // FIXME: Import definition!
4515 }
4516
4517 return D2;
4518}
4519
4520Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4521 VarTemplateSpecializationDecl *D) {
4522 // If this record has a definition in the translation unit we're coming from,
4523 // but this particular declaration is not that definition, import the
4524 // definition and map to that.
4525 VarDecl *Definition = D->getDefinition();
4526 if (Definition && Definition != D) {
4527 Decl *ImportedDef = Importer.Import(Definition);
4528 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004529 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004530
4531 return Importer.Imported(D, ImportedDef);
4532 }
4533
4534 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
4535 Importer.Import(D->getSpecializedTemplate()));
4536 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004537 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004538
4539 // Import the context of this declaration.
4540 DeclContext *DC = VarTemplate->getDeclContext();
4541 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004542 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004543
4544 DeclContext *LexicalDC = DC;
4545 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4546 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4547 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004548 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004549 }
4550
4551 // Import the location of this declaration.
4552 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4553 SourceLocation IdLoc = Importer.Import(D->getLocation());
4554
4555 // Import template arguments.
4556 SmallVector<TemplateArgument, 2> TemplateArgs;
4557 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4558 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004559 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004560
4561 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004562 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004563 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004564 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004565 if (D2) {
4566 // We already have a variable template specialization with these template
4567 // arguments.
4568
4569 // FIXME: Check for specialization vs. instantiation errors.
4570
4571 if (VarDecl *FoundDef = D2->getDefinition()) {
4572 if (!D->isThisDeclarationADefinition() ||
4573 IsStructuralMatch(D, FoundDef)) {
4574 // The record types structurally match, or the "from" translation
4575 // unit only had a forward declaration anyway; call it the same
4576 // variable.
4577 return Importer.Imported(D, FoundDef);
4578 }
4579 }
4580 } else {
4581
4582 // Import the type.
4583 QualType T = Importer.Import(D->getType());
4584 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004585 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004586 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4587
4588 // Create a new specialization.
4589 D2 = VarTemplateSpecializationDecl::Create(
4590 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
4591 D->getStorageClass(), TemplateArgs.data(), TemplateArgs.size());
4592 D2->setSpecializationKind(D->getSpecializationKind());
4593 D2->setTemplateArgsInfo(D->getTemplateArgsInfo());
4594
4595 // Add this specialization to the class template.
4596 VarTemplate->AddSpecialization(D2, InsertPos);
4597
4598 // Import the qualifier, if any.
4599 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4600
4601 // Add the specialization to this context.
4602 D2->setLexicalDeclContext(LexicalDC);
4603 LexicalDC->addDeclInternal(D2);
4604 }
4605 Importer.Imported(D, D2);
4606
4607 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004608 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004609
4610 return D2;
4611}
4612
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004613//----------------------------------------------------------------------------
4614// Import Statements
4615//----------------------------------------------------------------------------
4616
Sean Callanan59721b32015-04-28 18:41:46 +00004617DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4618 if (DG.isNull())
4619 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4620 size_t NumDecls = DG.end() - DG.begin();
4621 SmallVector<Decl *, 1> ToDecls(NumDecls);
4622 auto &_Importer = this->Importer;
4623 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4624 [&_Importer](Decl *D) -> Decl * {
4625 return _Importer.Import(D);
4626 });
4627 return DeclGroupRef::Create(Importer.getToContext(),
4628 ToDecls.begin(),
4629 NumDecls);
4630}
4631
4632 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4633 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4634 << S->getStmtClassName();
4635 return nullptr;
4636 }
4637
4638Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4639 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
4640 for (Decl *ToD : ToDG) {
4641 if (!ToD)
4642 return nullptr;
4643 }
4644 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4645 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4646 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4647}
4648
4649Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4650 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4651 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4652 S->hasLeadingEmptyMacro());
4653}
4654
4655Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
4656 SmallVector<Stmt *, 4> ToStmts(S->size());
4657 auto &_Importer = this->Importer;
4658 std::transform(S->body_begin(), S->body_end(), ToStmts.begin(),
4659 [&_Importer](Stmt *CS) -> Stmt * {
4660 return _Importer.Import(CS);
4661 });
4662 for (Stmt *ToS : ToStmts) {
4663 if (!ToS)
4664 return nullptr;
4665 }
4666 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4667 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
4668 return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(),
4669 ToStmts,
4670 ToLBraceLoc, ToRBraceLoc);
4671}
4672
4673Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4674 Expr *ToLHS = Importer.Import(S->getLHS());
4675 if (!ToLHS)
4676 return nullptr;
4677 Expr *ToRHS = Importer.Import(S->getRHS());
4678 if (!ToRHS && S->getRHS())
4679 return nullptr;
4680 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4681 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4682 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4683 return new (Importer.getToContext()) CaseStmt(ToLHS, ToRHS,
4684 ToCaseLoc, ToEllipsisLoc,
4685 ToColonLoc);
4686}
4687
4688Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4689 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4690 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4691 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4692 if (!ToSubStmt && S->getSubStmt())
4693 return nullptr;
4694 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4695 ToSubStmt);
4696}
4697
4698Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4699 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
4700 LabelDecl *ToLabelDecl =
4701 cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
4702 if (!ToLabelDecl && S->getDecl())
4703 return nullptr;
4704 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4705 if (!ToSubStmt && S->getSubStmt())
4706 return nullptr;
4707 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4708 ToSubStmt);
4709}
4710
4711Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4712 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4713 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4714 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
4715 ASTContext &_ToContext = Importer.getToContext();
4716 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
4717 [&_ToContext](const Attr *A) -> const Attr * {
4718 return A->clone(_ToContext);
4719 });
4720 for (const Attr *ToA : ToAttrs) {
4721 if (!ToA)
4722 return nullptr;
4723 }
4724 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4725 if (!ToSubStmt && S->getSubStmt())
4726 return nullptr;
4727 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4728 ToAttrs, ToSubStmt);
4729}
4730
4731Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4732 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
4733 VarDecl *ToConditionVariable = nullptr;
4734 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4735 ToConditionVariable =
4736 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4737 if (!ToConditionVariable)
4738 return nullptr;
4739 }
4740 Expr *ToCondition = Importer.Import(S->getCond());
4741 if (!ToCondition && S->getCond())
4742 return nullptr;
4743 Stmt *ToThenStmt = Importer.Import(S->getThen());
4744 if (!ToThenStmt && S->getThen())
4745 return nullptr;
4746 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4747 Stmt *ToElseStmt = Importer.Import(S->getElse());
4748 if (!ToElseStmt && S->getElse())
4749 return nullptr;
4750 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
4751 ToIfLoc, ToConditionVariable,
4752 ToCondition, ToThenStmt,
4753 ToElseLoc, ToElseStmt);
4754}
4755
4756Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
4757 VarDecl *ToConditionVariable = nullptr;
4758 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4759 ToConditionVariable =
4760 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4761 if (!ToConditionVariable)
4762 return nullptr;
4763 }
4764 Expr *ToCondition = Importer.Import(S->getCond());
4765 if (!ToCondition && S->getCond())
4766 return nullptr;
4767 SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt(
4768 Importer.getToContext(), ToConditionVariable,
4769 ToCondition);
4770 Stmt *ToBody = Importer.Import(S->getBody());
4771 if (!ToBody && S->getBody())
4772 return nullptr;
4773 ToStmt->setBody(ToBody);
4774 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4775 // Now we have to re-chain the cases.
4776 SwitchCase *LastChainedSwitchCase = nullptr;
4777 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4778 SC = SC->getNextSwitchCase()) {
4779 SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
4780 if (!ToSC)
4781 return nullptr;
4782 if (LastChainedSwitchCase)
4783 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4784 else
4785 ToStmt->setSwitchCaseList(ToSC);
4786 LastChainedSwitchCase = ToSC;
4787 }
4788 return ToStmt;
4789}
4790
4791Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
4792 VarDecl *ToConditionVariable = nullptr;
4793 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4794 ToConditionVariable =
4795 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4796 if (!ToConditionVariable)
4797 return nullptr;
4798 }
4799 Expr *ToCondition = Importer.Import(S->getCond());
4800 if (!ToCondition && S->getCond())
4801 return nullptr;
4802 Stmt *ToBody = Importer.Import(S->getBody());
4803 if (!ToBody && S->getBody())
4804 return nullptr;
4805 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4806 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
4807 ToConditionVariable,
4808 ToCondition, ToBody,
4809 ToWhileLoc);
4810}
4811
4812Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
4813 Stmt *ToBody = Importer.Import(S->getBody());
4814 if (!ToBody && S->getBody())
4815 return nullptr;
4816 Expr *ToCondition = Importer.Import(S->getCond());
4817 if (!ToCondition && S->getCond())
4818 return nullptr;
4819 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
4820 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4821 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4822 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
4823 ToDoLoc, ToWhileLoc,
4824 ToRParenLoc);
4825}
4826
4827Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
4828 Stmt *ToInit = Importer.Import(S->getInit());
4829 if (!ToInit && S->getInit())
4830 return nullptr;
4831 Expr *ToCondition = Importer.Import(S->getCond());
4832 if (!ToCondition && S->getCond())
4833 return nullptr;
4834 VarDecl *ToConditionVariable = nullptr;
4835 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4836 ToConditionVariable =
4837 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4838 if (!ToConditionVariable)
4839 return nullptr;
4840 }
4841 Expr *ToInc = Importer.Import(S->getInc());
4842 if (!ToInc && S->getInc())
4843 return nullptr;
4844 Stmt *ToBody = Importer.Import(S->getBody());
4845 if (!ToBody && S->getBody())
4846 return nullptr;
4847 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4848 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
4849 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4850 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
4851 ToInit, ToCondition,
4852 ToConditionVariable,
4853 ToInc, ToBody,
4854 ToForLoc, ToLParenLoc,
4855 ToRParenLoc);
4856}
4857
4858Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
4859 LabelDecl *ToLabel = nullptr;
4860 if (LabelDecl *FromLabel = S->getLabel()) {
4861 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
4862 if (!ToLabel)
4863 return nullptr;
4864 }
4865 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4866 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
4867 return new (Importer.getToContext()) GotoStmt(ToLabel,
4868 ToGotoLoc, ToLabelLoc);
4869}
4870
4871Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
4872 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4873 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
4874 Expr *ToTarget = Importer.Import(S->getTarget());
4875 if (!ToTarget && S->getTarget())
4876 return nullptr;
4877 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
4878 ToTarget);
4879}
4880
4881Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
4882 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
4883 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
4884}
4885
4886Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
4887 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
4888 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
4889}
4890
4891Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
4892 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
4893 Expr *ToRetExpr = Importer.Import(S->getRetValue());
4894 if (!ToRetExpr && S->getRetValue())
4895 return nullptr;
4896 VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate());
4897 VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
4898 if (!ToNRVOCandidate && NRVOCandidate)
4899 return nullptr;
4900 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
4901 ToNRVOCandidate);
4902}
4903
4904Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
4905 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
4906 VarDecl *ToExceptionDecl = nullptr;
4907 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
4908 ToExceptionDecl =
4909 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4910 if (!ToExceptionDecl)
4911 return nullptr;
4912 }
4913 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
4914 if (!ToHandlerBlock && S->getHandlerBlock())
4915 return nullptr;
4916 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
4917 ToExceptionDecl,
4918 ToHandlerBlock);
4919}
4920
4921Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
4922 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
4923 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
4924 if (!ToTryBlock && S->getTryBlock())
4925 return nullptr;
4926 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
4927 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
4928 CXXCatchStmt *FromHandler = S->getHandler(HI);
4929 if (Stmt *ToHandler = Importer.Import(FromHandler))
4930 ToHandlers[HI] = ToHandler;
4931 else
4932 return nullptr;
4933 }
4934 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
4935 ToHandlers);
4936}
4937
4938Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
4939 DeclStmt *ToRange =
4940 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
4941 if (!ToRange && S->getRangeStmt())
4942 return nullptr;
Richard Smith01694c32016-03-20 10:33:40 +00004943 DeclStmt *ToBegin =
4944 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
4945 if (!ToBegin && S->getBeginStmt())
4946 return nullptr;
4947 DeclStmt *ToEnd =
4948 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
4949 if (!ToEnd && S->getEndStmt())
Sean Callanan59721b32015-04-28 18:41:46 +00004950 return nullptr;
4951 Expr *ToCond = Importer.Import(S->getCond());
4952 if (!ToCond && S->getCond())
4953 return nullptr;
4954 Expr *ToInc = Importer.Import(S->getInc());
4955 if (!ToInc && S->getInc())
4956 return nullptr;
4957 DeclStmt *ToLoopVar =
4958 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
4959 if (!ToLoopVar && S->getLoopVarStmt())
4960 return nullptr;
4961 Stmt *ToBody = Importer.Import(S->getBody());
4962 if (!ToBody && S->getBody())
4963 return nullptr;
4964 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
Richard Smith9f690bd2015-10-27 06:02:45 +00004965 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00004966 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4967 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
Richard Smith01694c32016-03-20 10:33:40 +00004968 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
Sean Callanan59721b32015-04-28 18:41:46 +00004969 ToCond, ToInc,
4970 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00004971 ToForLoc, ToCoawaitLoc,
4972 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00004973}
4974
4975Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
4976 Stmt *ToElem = Importer.Import(S->getElement());
4977 if (!ToElem && S->getElement())
4978 return nullptr;
4979 Expr *ToCollect = Importer.Import(S->getCollection());
4980 if (!ToCollect && S->getCollection())
4981 return nullptr;
4982 Stmt *ToBody = Importer.Import(S->getBody());
4983 if (!ToBody && S->getBody())
4984 return nullptr;
4985 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4986 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4987 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
4988 ToCollect,
4989 ToBody, ToForLoc,
4990 ToRParenLoc);
4991}
4992
4993Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
4994 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
4995 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4996 VarDecl *ToExceptionDecl = nullptr;
4997 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
4998 ToExceptionDecl =
4999 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5000 if (!ToExceptionDecl)
5001 return nullptr;
5002 }
5003 Stmt *ToBody = Importer.Import(S->getCatchBody());
5004 if (!ToBody && S->getCatchBody())
5005 return nullptr;
5006 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
5007 ToRParenLoc,
5008 ToExceptionDecl,
5009 ToBody);
5010}
5011
5012Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5013 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
5014 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
5015 if (!ToAtFinallyStmt && S->getFinallyBody())
5016 return nullptr;
5017 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
5018 ToAtFinallyStmt);
5019}
5020
5021Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5022 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
5023 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
5024 if (!ToAtTryStmt && S->getTryBody())
5025 return nullptr;
5026 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5027 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5028 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
5029 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
5030 ToCatchStmts[CI] = ToCatchStmt;
5031 else
5032 return nullptr;
5033 }
5034 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
5035 if (!ToAtFinallyStmt && S->getFinallyStmt())
5036 return nullptr;
5037 return ObjCAtTryStmt::Create(Importer.getToContext(),
5038 ToAtTryLoc, ToAtTryStmt,
5039 ToCatchStmts.begin(), ToCatchStmts.size(),
5040 ToAtFinallyStmt);
5041}
5042
5043Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
5044 (ObjCAtSynchronizedStmt *S) {
5045 SourceLocation ToAtSynchronizedLoc =
5046 Importer.Import(S->getAtSynchronizedLoc());
5047 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
5048 if (!ToSynchExpr && S->getSynchExpr())
5049 return nullptr;
5050 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
5051 if (!ToSynchBody && S->getSynchBody())
5052 return nullptr;
5053 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5054 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5055}
5056
5057Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5058 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5059 Expr *ToThrow = Importer.Import(S->getThrowExpr());
5060 if (!ToThrow && S->getThrowExpr())
5061 return nullptr;
5062 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5063}
5064
5065Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5066 (ObjCAutoreleasePoolStmt *S) {
5067 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5068 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5069 if (!ToSubStmt && S->getSubStmt())
5070 return nullptr;
5071 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5072 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005073}
5074
5075//----------------------------------------------------------------------------
5076// Import Expressions
5077//----------------------------------------------------------------------------
5078Expr *ASTNodeImporter::VisitExpr(Expr *E) {
5079 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5080 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00005081 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005082}
5083
Douglas Gregor52f820e2010-02-19 01:17:02 +00005084Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00005085 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
5086 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005087 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005088
Craig Topper36250ad2014-05-12 05:36:57 +00005089 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005090 if (E->getDecl() != E->getFoundDecl()) {
5091 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5092 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00005093 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005094 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00005095
5096 QualType T = Importer.Import(E->getType());
5097 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005098 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005099
5100 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5101 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00005102 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005103 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00005104 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005105 Importer.Import(E->getLocation()),
5106 T, E->getValueKind(),
5107 FoundD,
Craig Topper36250ad2014-05-12 05:36:57 +00005108 /*FIXME:TemplateArgs=*/nullptr);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005109 if (E->hadMultipleCandidates())
5110 DRE->setHadMultipleCandidates(true);
5111 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00005112}
5113
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005114Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5115 QualType T = Importer.Import(E->getType());
5116 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005117 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005118
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005119 return IntegerLiteral::Create(Importer.getToContext(),
5120 E->getValue(), T,
5121 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005122}
5123
Douglas Gregor623421d2010-02-18 02:21:22 +00005124Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5125 QualType T = Importer.Import(E->getType());
5126 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005127 return nullptr;
5128
Douglas Gregorfb65e592011-07-27 05:40:30 +00005129 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5130 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005131 Importer.Import(E->getLocation()));
5132}
5133
Douglas Gregorc74247e2010-02-19 01:07:06 +00005134Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5135 Expr *SubExpr = Importer.Import(E->getSubExpr());
5136 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005137 return nullptr;
5138
Douglas Gregorc74247e2010-02-19 01:07:06 +00005139 return new (Importer.getToContext())
5140 ParenExpr(Importer.Import(E->getLParen()),
5141 Importer.Import(E->getRParen()),
5142 SubExpr);
5143}
5144
5145Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5146 QualType T = Importer.Import(E->getType());
5147 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005148 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005149
5150 Expr *SubExpr = Importer.Import(E->getSubExpr());
5151 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005152 return nullptr;
5153
Douglas Gregorc74247e2010-02-19 01:07:06 +00005154 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005155 T, E->getValueKind(),
5156 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00005157 Importer.Import(E->getOperatorLoc()));
5158}
5159
Peter Collingbournee190dee2011-03-11 19:24:49 +00005160Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
5161 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005162 QualType ResultType = Importer.Import(E->getType());
5163
5164 if (E->isArgumentType()) {
5165 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5166 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005167 return nullptr;
5168
Peter Collingbournee190dee2011-03-11 19:24:49 +00005169 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5170 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005171 Importer.Import(E->getOperatorLoc()),
5172 Importer.Import(E->getRParenLoc()));
5173 }
5174
5175 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5176 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005177 return nullptr;
5178
Peter Collingbournee190dee2011-03-11 19:24:49 +00005179 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5180 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005181 Importer.Import(E->getOperatorLoc()),
5182 Importer.Import(E->getRParenLoc()));
5183}
5184
Douglas Gregorc74247e2010-02-19 01:07:06 +00005185Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5186 QualType T = Importer.Import(E->getType());
5187 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005188 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005189
5190 Expr *LHS = Importer.Import(E->getLHS());
5191 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005192 return nullptr;
5193
Douglas Gregorc74247e2010-02-19 01:07:06 +00005194 Expr *RHS = Importer.Import(E->getRHS());
5195 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005196 return nullptr;
5197
Douglas Gregorc74247e2010-02-19 01:07:06 +00005198 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005199 T, E->getValueKind(),
5200 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005201 Importer.Import(E->getOperatorLoc()),
5202 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005203}
5204
5205Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5206 QualType T = Importer.Import(E->getType());
5207 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005208 return nullptr;
5209
Douglas Gregorc74247e2010-02-19 01:07:06 +00005210 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5211 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005212 return nullptr;
5213
Douglas Gregorc74247e2010-02-19 01:07:06 +00005214 QualType CompResultType = Importer.Import(E->getComputationResultType());
5215 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005216 return nullptr;
5217
Douglas Gregorc74247e2010-02-19 01:07:06 +00005218 Expr *LHS = Importer.Import(E->getLHS());
5219 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005220 return nullptr;
5221
Douglas Gregorc74247e2010-02-19 01:07:06 +00005222 Expr *RHS = Importer.Import(E->getRHS());
5223 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005224 return nullptr;
5225
Douglas Gregorc74247e2010-02-19 01:07:06 +00005226 return new (Importer.getToContext())
5227 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005228 T, E->getValueKind(),
5229 E->getObjectKind(),
5230 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005231 Importer.Import(E->getOperatorLoc()),
5232 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005233}
5234
Benjamin Kramer8aef5962011-03-26 12:38:21 +00005235static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallcf142162010-08-07 06:22:56 +00005236 if (E->path_empty()) return false;
5237
5238 // TODO: import cast paths
5239 return true;
5240}
5241
Douglas Gregor98c10182010-02-12 22:17:39 +00005242Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5243 QualType T = Importer.Import(E->getType());
5244 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005245 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005246
5247 Expr *SubExpr = Importer.Import(E->getSubExpr());
5248 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005249 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005250
5251 CXXCastPath BasePath;
5252 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005253 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005254
5255 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005256 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005257}
5258
Douglas Gregor5481d322010-02-19 01:32:14 +00005259Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
5260 QualType T = Importer.Import(E->getType());
5261 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005262 return nullptr;
5263
Douglas Gregor5481d322010-02-19 01:32:14 +00005264 Expr *SubExpr = Importer.Import(E->getSubExpr());
5265 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005266 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00005267
5268 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5269 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00005270 return nullptr;
5271
John McCallcf142162010-08-07 06:22:56 +00005272 CXXCastPath BasePath;
5273 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005274 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005275
John McCall7decc9e2010-11-18 06:31:45 +00005276 return CStyleCastExpr::Create(Importer.getToContext(), T,
5277 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00005278 SubExpr, &BasePath, TInfo,
5279 Importer.Import(E->getLParenLoc()),
5280 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00005281}
5282
Sean Callanan59721b32015-04-28 18:41:46 +00005283Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
5284 QualType T = Importer.Import(E->getType());
5285 if (T.isNull())
5286 return nullptr;
5287
5288 CXXConstructorDecl *ToCCD =
5289 dyn_cast<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
5290 if (!ToCCD && E->getConstructor())
5291 return nullptr;
5292
5293 size_t NumArgs = E->getNumArgs();
5294 SmallVector<Expr *, 1> ToArgs(NumArgs);
5295 ASTImporter &_Importer = Importer;
5296 std::transform(E->arg_begin(), E->arg_end(), ToArgs.begin(),
5297 [&_Importer](Expr *AE) -> Expr * {
5298 return _Importer.Import(AE);
5299 });
5300 for (Expr *ToA : ToArgs) {
5301 if (!ToA)
5302 return nullptr;
5303 }
5304
5305 return CXXConstructExpr::Create(Importer.getToContext(), T,
5306 Importer.Import(E->getLocation()),
5307 ToCCD, E->isElidable(),
5308 ToArgs, E->hadMultipleCandidates(),
5309 E->isListInitialization(),
5310 E->isStdInitListInitialization(),
5311 E->requiresZeroInitialization(),
5312 E->getConstructionKind(),
5313 Importer.Import(E->getParenOrBraceRange()));
5314}
5315
5316Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
5317 QualType T = Importer.Import(E->getType());
5318 if (T.isNull())
5319 return nullptr;
5320
5321 Expr *ToBase = Importer.Import(E->getBase());
5322 if (!ToBase && E->getBase())
5323 return nullptr;
5324
5325 ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
5326 if (!ToMember && E->getMemberDecl())
5327 return nullptr;
5328
5329 DeclAccessPair ToFoundDecl = DeclAccessPair::make(
5330 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
5331 E->getFoundDecl().getAccess());
5332
5333 DeclarationNameInfo ToMemberNameInfo(
5334 Importer.Import(E->getMemberNameInfo().getName()),
5335 Importer.Import(E->getMemberNameInfo().getLoc()));
5336
5337 if (E->hasExplicitTemplateArgs()) {
5338 return nullptr; // FIXME: handle template arguments
5339 }
5340
5341 return MemberExpr::Create(Importer.getToContext(), ToBase,
5342 E->isArrow(),
5343 Importer.Import(E->getOperatorLoc()),
5344 Importer.Import(E->getQualifierLoc()),
5345 Importer.Import(E->getTemplateKeywordLoc()),
5346 ToMember, ToFoundDecl, ToMemberNameInfo,
5347 nullptr, T, E->getValueKind(),
5348 E->getObjectKind());
5349}
5350
5351Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
5352 QualType T = Importer.Import(E->getType());
5353 if (T.isNull())
5354 return nullptr;
5355
5356 Expr *ToCallee = Importer.Import(E->getCallee());
5357 if (!ToCallee && E->getCallee())
5358 return nullptr;
5359
5360 unsigned NumArgs = E->getNumArgs();
5361
5362 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
5363
5364 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) {
5365 Expr *FromArg = E->getArg(ai);
5366 Expr *ToArg = Importer.Import(FromArg);
5367 if (!ToArg)
5368 return nullptr;
5369 ToArgs[ai] = ToArg;
5370 }
5371
5372 Expr **ToArgs_Copied = new (Importer.getToContext())
5373 Expr*[NumArgs];
5374
5375 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
5376 ToArgs_Copied[ai] = ToArgs[ai];
5377
5378 return new (Importer.getToContext())
5379 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00005380 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00005381 Importer.Import(E->getRParenLoc()));
5382}
5383
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005384ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00005385 ASTContext &FromContext, FileManager &FromFileManager,
5386 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00005387 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00005388 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Richard Smith5bb4cdf2012-12-20 02:22:15 +00005389 Minimal(MinimalImport), LastDiagFromFrom(false)
Douglas Gregor0a791672011-01-18 03:11:38 +00005390{
Douglas Gregor62d311f2010-02-09 19:21:46 +00005391 ImportedDecls[FromContext.getTranslationUnitDecl()]
5392 = ToContext.getTranslationUnitDecl();
5393}
5394
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +00005395ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00005396
5397QualType ASTImporter::Import(QualType FromT) {
5398 if (FromT.isNull())
5399 return QualType();
John McCall424cec92011-01-19 06:33:43 +00005400
5401 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00005402
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005403 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00005404 llvm::DenseMap<const Type *, const Type *>::iterator Pos
5405 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005406 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00005407 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00005408
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005409 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00005410 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00005411 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00005412 if (ToT.isNull())
5413 return ToT;
5414
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005415 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00005416 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005417
John McCall424cec92011-01-19 06:33:43 +00005418 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00005419}
5420
Douglas Gregor62d311f2010-02-09 19:21:46 +00005421TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005422 if (!FromTSI)
5423 return FromTSI;
5424
5425 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00005426 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005427 QualType T = Import(FromTSI->getType());
5428 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005429 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005430
5431 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00005432 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00005433}
5434
Sean Callanan59721b32015-04-28 18:41:46 +00005435Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
5436 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
5437 if (Pos != ImportedDecls.end()) {
5438 Decl *ToD = Pos->second;
5439 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
5440 return ToD;
5441 } else {
5442 return nullptr;
5443 }
5444}
5445
Douglas Gregor62d311f2010-02-09 19:21:46 +00005446Decl *ASTImporter::Import(Decl *FromD) {
5447 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00005448 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005449
Douglas Gregord451ea92011-07-29 23:31:30 +00005450 ASTNodeImporter Importer(*this);
5451
Douglas Gregor62d311f2010-02-09 19:21:46 +00005452 // Check whether we've already imported this declaration.
5453 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00005454 if (Pos != ImportedDecls.end()) {
5455 Decl *ToD = Pos->second;
5456 Importer.ImportDefinitionIfNeeded(FromD, ToD);
5457 return ToD;
5458 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00005459
5460 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00005461 Decl *ToD = Importer.Visit(FromD);
5462 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005463 return nullptr;
5464
Douglas Gregor62d311f2010-02-09 19:21:46 +00005465 // Record the imported declaration.
5466 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00005467
5468 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
5469 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00005470 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00005471 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00005472 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00005473 // When we've finished transforming a typedef, see whether it was the
5474 // typedef for an anonymous tag.
Craig Topper2341c0d2013-07-04 03:08:24 +00005475 for (SmallVectorImpl<TagDecl *>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00005476 FromTag = AnonTagsWithPendingTypedefs.begin(),
5477 FromTagEnd = AnonTagsWithPendingTypedefs.end();
5478 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00005479 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00005480 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
5481 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00005482 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00005483 AnonTagsWithPendingTypedefs.erase(FromTag);
5484 break;
5485 }
5486 }
5487 }
5488 }
5489
Douglas Gregor62d311f2010-02-09 19:21:46 +00005490 return ToD;
5491}
5492
5493DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
5494 if (!FromDC)
5495 return FromDC;
5496
Douglas Gregor95d82832012-01-24 18:36:04 +00005497 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00005498 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00005499 return nullptr;
5500
Douglas Gregor2e15c842012-02-01 21:00:38 +00005501 // When we're using a record/enum/Objective-C class/protocol as a context, we
5502 // need it to have a definition.
5503 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00005504 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00005505 if (ToRecord->isCompleteDefinition()) {
5506 // Do nothing.
5507 } else if (FromRecord->isCompleteDefinition()) {
5508 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
5509 ASTNodeImporter::IDK_Basic);
5510 } else {
5511 CompleteDecl(ToRecord);
5512 }
5513 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
5514 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
5515 if (ToEnum->isCompleteDefinition()) {
5516 // Do nothing.
5517 } else if (FromEnum->isCompleteDefinition()) {
5518 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
5519 ASTNodeImporter::IDK_Basic);
5520 } else {
5521 CompleteDecl(ToEnum);
5522 }
5523 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
5524 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
5525 if (ToClass->getDefinition()) {
5526 // Do nothing.
5527 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
5528 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
5529 ASTNodeImporter::IDK_Basic);
5530 } else {
5531 CompleteDecl(ToClass);
5532 }
5533 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
5534 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
5535 if (ToProto->getDefinition()) {
5536 // Do nothing.
5537 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
5538 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
5539 ASTNodeImporter::IDK_Basic);
5540 } else {
5541 CompleteDecl(ToProto);
5542 }
Douglas Gregor95d82832012-01-24 18:36:04 +00005543 }
5544
5545 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005546}
5547
5548Expr *ASTImporter::Import(Expr *FromE) {
5549 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00005550 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005551
5552 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
5553}
5554
5555Stmt *ASTImporter::Import(Stmt *FromS) {
5556 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00005557 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005558
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005559 // Check whether we've already imported this declaration.
5560 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
5561 if (Pos != ImportedStmts.end())
5562 return Pos->second;
5563
5564 // Import the type
5565 ASTNodeImporter Importer(*this);
5566 Stmt *ToS = Importer.Visit(FromS);
5567 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00005568 return nullptr;
5569
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005570 // Record the imported declaration.
5571 ImportedStmts[FromS] = ToS;
5572 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005573}
5574
5575NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
5576 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00005577 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005578
Douglas Gregor90ebf252011-04-27 16:48:40 +00005579 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
5580
5581 switch (FromNNS->getKind()) {
5582 case NestedNameSpecifier::Identifier:
5583 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
5584 return NestedNameSpecifier::Create(ToContext, prefix, II);
5585 }
Craig Topper36250ad2014-05-12 05:36:57 +00005586 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005587
5588 case NestedNameSpecifier::Namespace:
5589 if (NamespaceDecl *NS =
5590 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
5591 return NestedNameSpecifier::Create(ToContext, prefix, NS);
5592 }
Craig Topper36250ad2014-05-12 05:36:57 +00005593 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005594
5595 case NestedNameSpecifier::NamespaceAlias:
5596 if (NamespaceAliasDecl *NSAD =
5597 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
5598 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
5599 }
Craig Topper36250ad2014-05-12 05:36:57 +00005600 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005601
5602 case NestedNameSpecifier::Global:
5603 return NestedNameSpecifier::GlobalSpecifier(ToContext);
5604
Nikola Smiljanic67860242014-09-26 00:28:20 +00005605 case NestedNameSpecifier::Super:
5606 if (CXXRecordDecl *RD =
5607 cast<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
5608 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
5609 }
5610 return nullptr;
5611
Douglas Gregor90ebf252011-04-27 16:48:40 +00005612 case NestedNameSpecifier::TypeSpec:
5613 case NestedNameSpecifier::TypeSpecWithTemplate: {
5614 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
5615 if (!T.isNull()) {
5616 bool bTemplate = FromNNS->getKind() ==
5617 NestedNameSpecifier::TypeSpecWithTemplate;
5618 return NestedNameSpecifier::Create(ToContext, prefix,
5619 bTemplate, T.getTypePtr());
5620 }
5621 }
Craig Topper36250ad2014-05-12 05:36:57 +00005622 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005623 }
5624
5625 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00005626}
5627
Douglas Gregor14454802011-02-25 02:25:35 +00005628NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
5629 // FIXME: Implement!
5630 return NestedNameSpecifierLoc();
5631}
5632
Douglas Gregore2e50d332010-12-01 01:36:18 +00005633TemplateName ASTImporter::Import(TemplateName From) {
5634 switch (From.getKind()) {
5635 case TemplateName::Template:
5636 if (TemplateDecl *ToTemplate
5637 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
5638 return TemplateName(ToTemplate);
5639
5640 return TemplateName();
5641
5642 case TemplateName::OverloadedTemplate: {
5643 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
5644 UnresolvedSet<2> ToTemplates;
5645 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
5646 E = FromStorage->end();
5647 I != E; ++I) {
5648 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
5649 ToTemplates.addDecl(To);
5650 else
5651 return TemplateName();
5652 }
5653 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
5654 ToTemplates.end());
5655 }
5656
5657 case TemplateName::QualifiedTemplate: {
5658 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
5659 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
5660 if (!Qualifier)
5661 return TemplateName();
5662
5663 if (TemplateDecl *ToTemplate
5664 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
5665 return ToContext.getQualifiedTemplateName(Qualifier,
5666 QTN->hasTemplateKeyword(),
5667 ToTemplate);
5668
5669 return TemplateName();
5670 }
5671
5672 case TemplateName::DependentTemplate: {
5673 DependentTemplateName *DTN = From.getAsDependentTemplateName();
5674 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
5675 if (!Qualifier)
5676 return TemplateName();
5677
5678 if (DTN->isIdentifier()) {
5679 return ToContext.getDependentTemplateName(Qualifier,
5680 Import(DTN->getIdentifier()));
5681 }
5682
5683 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
5684 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005685
5686 case TemplateName::SubstTemplateTemplateParm: {
5687 SubstTemplateTemplateParmStorage *subst
5688 = From.getAsSubstTemplateTemplateParm();
5689 TemplateTemplateParmDecl *param
5690 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
5691 if (!param)
5692 return TemplateName();
5693
5694 TemplateName replacement = Import(subst->getReplacement());
5695 if (replacement.isNull()) return TemplateName();
5696
5697 return ToContext.getSubstTemplateTemplateParm(param, replacement);
5698 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005699
5700 case TemplateName::SubstTemplateTemplateParmPack: {
5701 SubstTemplateTemplateParmPackStorage *SubstPack
5702 = From.getAsSubstTemplateTemplateParmPack();
5703 TemplateTemplateParmDecl *Param
5704 = cast_or_null<TemplateTemplateParmDecl>(
5705 Import(SubstPack->getParameterPack()));
5706 if (!Param)
5707 return TemplateName();
5708
5709 ASTNodeImporter Importer(*this);
5710 TemplateArgument ArgPack
5711 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
5712 if (ArgPack.isNull())
5713 return TemplateName();
5714
5715 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
5716 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00005717 }
5718
5719 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00005720}
5721
Douglas Gregor62d311f2010-02-09 19:21:46 +00005722SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
5723 if (FromLoc.isInvalid())
5724 return SourceLocation();
5725
Douglas Gregor811663e2010-02-10 00:15:17 +00005726 SourceManager &FromSM = FromContext.getSourceManager();
5727
5728 // For now, map everything down to its spelling location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00005729 // don't have to import macro expansions.
5730 // FIXME: Import macro expansions!
Douglas Gregor811663e2010-02-10 00:15:17 +00005731 FromLoc = FromSM.getSpellingLoc(FromLoc);
5732 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
5733 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00005734 FileID ToFileID = Import(Decomposed.first);
5735 if (ToFileID.isInvalid())
5736 return SourceLocation();
Sean Callanan59721b32015-04-28 18:41:46 +00005737 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
5738 .getLocWithOffset(Decomposed.second);
5739 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005740}
5741
5742SourceRange ASTImporter::Import(SourceRange FromRange) {
5743 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
5744}
5745
Douglas Gregor811663e2010-02-10 00:15:17 +00005746FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00005747 llvm::DenseMap<FileID, FileID>::iterator Pos
5748 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00005749 if (Pos != ImportedFileIDs.end())
5750 return Pos->second;
5751
5752 SourceManager &FromSM = FromContext.getSourceManager();
5753 SourceManager &ToSM = ToContext.getSourceManager();
5754 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00005755 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00005756
5757 // Include location of this file.
5758 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
5759
5760 // Map the FileID for to the "to" source manager.
5761 FileID ToID;
5762 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan25d34af2015-04-30 00:44:21 +00005763 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
Douglas Gregor811663e2010-02-10 00:15:17 +00005764 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
5765 // disk again
5766 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
5767 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00005768 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Sean Callanan238d8972014-12-10 01:26:39 +00005769 if (!Entry)
5770 return FileID();
Douglas Gregor811663e2010-02-10 00:15:17 +00005771 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
5772 FromSLoc.getFile().getFileCharacteristic());
5773 } else {
5774 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005775 const llvm::MemoryBuffer *
5776 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00005777 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00005778 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00005779 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00005780 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00005781 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00005782 }
5783
5784
Sebastian Redl99219f12010-09-30 01:03:06 +00005785 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00005786 return ToID;
5787}
5788
Douglas Gregor0a791672011-01-18 03:11:38 +00005789void ASTImporter::ImportDefinition(Decl *From) {
5790 Decl *To = Import(From);
5791 if (!To)
5792 return;
5793
5794 if (DeclContext *FromDC = cast<DeclContext>(From)) {
5795 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00005796
5797 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
5798 if (!ToRecord->getDefinition()) {
5799 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00005800 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00005801 return;
5802 }
5803 }
Douglas Gregord451ea92011-07-29 23:31:30 +00005804
5805 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
5806 if (!ToEnum->getDefinition()) {
5807 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00005808 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00005809 return;
5810 }
5811 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00005812
5813 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
5814 if (!ToIFace->getDefinition()) {
5815 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00005816 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00005817 return;
5818 }
5819 }
Douglas Gregord451ea92011-07-29 23:31:30 +00005820
Douglas Gregor2aa53772012-01-24 17:42:07 +00005821 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
5822 if (!ToProto->getDefinition()) {
5823 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00005824 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00005825 return;
5826 }
5827 }
5828
Douglas Gregor0a791672011-01-18 03:11:38 +00005829 Importer.ImportDeclContext(FromDC, true);
5830 }
5831}
5832
Douglas Gregor96e578d2010-02-05 17:54:41 +00005833DeclarationName ASTImporter::Import(DeclarationName FromName) {
5834 if (!FromName)
5835 return DeclarationName();
5836
5837 switch (FromName.getNameKind()) {
5838 case DeclarationName::Identifier:
5839 return Import(FromName.getAsIdentifierInfo());
5840
5841 case DeclarationName::ObjCZeroArgSelector:
5842 case DeclarationName::ObjCOneArgSelector:
5843 case DeclarationName::ObjCMultiArgSelector:
5844 return Import(FromName.getObjCSelector());
5845
5846 case DeclarationName::CXXConstructorName: {
5847 QualType T = Import(FromName.getCXXNameType());
5848 if (T.isNull())
5849 return DeclarationName();
5850
5851 return ToContext.DeclarationNames.getCXXConstructorName(
5852 ToContext.getCanonicalType(T));
5853 }
5854
5855 case DeclarationName::CXXDestructorName: {
5856 QualType T = Import(FromName.getCXXNameType());
5857 if (T.isNull())
5858 return DeclarationName();
5859
5860 return ToContext.DeclarationNames.getCXXDestructorName(
5861 ToContext.getCanonicalType(T));
5862 }
5863
5864 case DeclarationName::CXXConversionFunctionName: {
5865 QualType T = Import(FromName.getCXXNameType());
5866 if (T.isNull())
5867 return DeclarationName();
5868
5869 return ToContext.DeclarationNames.getCXXConversionFunctionName(
5870 ToContext.getCanonicalType(T));
5871 }
5872
5873 case DeclarationName::CXXOperatorName:
5874 return ToContext.DeclarationNames.getCXXOperatorName(
5875 FromName.getCXXOverloadedOperator());
5876
5877 case DeclarationName::CXXLiteralOperatorName:
5878 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
5879 Import(FromName.getCXXLiteralIdentifier()));
5880
5881 case DeclarationName::CXXUsingDirective:
5882 // FIXME: STATICS!
5883 return DeclarationName::getUsingDirectiveName();
5884 }
5885
David Blaikiee4d798f2012-01-20 21:50:17 +00005886 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00005887}
5888
Douglas Gregore2e50d332010-12-01 01:36:18 +00005889IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00005890 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00005891 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00005892
5893 return &ToContext.Idents.get(FromId->getName());
5894}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005895
Douglas Gregor43f54792010-02-17 02:12:47 +00005896Selector ASTImporter::Import(Selector FromSel) {
5897 if (FromSel.isNull())
5898 return Selector();
5899
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005900 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00005901 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
5902 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
5903 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
5904 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
5905}
5906
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005907DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
5908 DeclContext *DC,
5909 unsigned IDNS,
5910 NamedDecl **Decls,
5911 unsigned NumDecls) {
5912 return Name;
5913}
5914
5915DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00005916 if (LastDiagFromFrom)
5917 ToContext.getDiagnostics().notePriorDiagnosticFrom(
5918 FromContext.getDiagnostics());
5919 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005920 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005921}
5922
5923DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00005924 if (!LastDiagFromFrom)
5925 FromContext.getDiagnostics().notePriorDiagnosticFrom(
5926 ToContext.getDiagnostics());
5927 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005928 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005929}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00005930
Douglas Gregor2e15c842012-02-01 21:00:38 +00005931void ASTImporter::CompleteDecl (Decl *D) {
5932 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
5933 if (!ID->getDefinition())
5934 ID->startDefinition();
5935 }
5936 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
5937 if (!PD->getDefinition())
5938 PD->startDefinition();
5939 }
5940 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
5941 if (!TD->getDefinition() && !TD->isBeingDefined()) {
5942 TD->startDefinition();
5943 TD->setCompleteDefinition(true);
5944 }
5945 }
5946 else {
5947 assert (0 && "CompleteDecl called on a Decl that can't be completed");
5948 }
5949}
5950
Douglas Gregor8cdbe642010-02-12 23:44:20 +00005951Decl *ASTImporter::Imported(Decl *From, Decl *To) {
5952 ImportedDecls[From] = To;
5953 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00005954}
Douglas Gregorb4964f72010-02-15 23:54:17 +00005955
Douglas Gregordd6006f2012-07-17 21:16:27 +00005956bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
5957 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00005958 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00005959 = ImportedTypes.find(From.getTypePtr());
5960 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
5961 return true;
5962
Douglas Gregordd6006f2012-07-17 21:16:27 +00005963 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
5964 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00005965 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00005966}