1 //===--- ASTImporter.cpp - Importing ASTs from other Contexts ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ASTImporter class which imports AST nodes from one
11 // context into another context.
13 //===----------------------------------------------------------------------===//
14 #include "clang/AST/ASTImporter.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/ASTDiagnostic.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/DeclObjC.h"
19 #include "clang/AST/DeclVisitor.h"
20 #include "clang/AST/StmtVisitor.h"
21 #include "clang/AST/TypeVisitor.h"
22 #include "clang/Basic/FileManager.h"
23 #include "clang/Basic/SourceManager.h"
24 #include "llvm/Support/MemoryBuffer.h"
28 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
29 public DeclVisitor<ASTNodeImporter, Decl *>,
30 public StmtVisitor<ASTNodeImporter, Stmt *> {
31 ASTImporter &Importer;
34 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
36 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
37 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
38 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
41 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);
52 // FIXME: DependentSizedArrayType
53 // FIXME: DependentSizedExtVectorType
54 QualType VisitVectorType(const VectorType *T);
55 QualType VisitExtVectorType(const ExtVectorType *T);
56 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
57 QualType VisitFunctionProtoType(const FunctionProtoType *T);
58 // FIXME: UnresolvedUsingType
59 QualType VisitParenType(const ParenType *T);
60 QualType VisitTypedefType(const TypedefType *T);
61 QualType VisitTypeOfExprType(const TypeOfExprType *T);
62 // FIXME: DependentTypeOfExprType
63 QualType VisitTypeOfType(const TypeOfType *T);
64 QualType VisitDecltypeType(const DecltypeType *T);
65 QualType VisitUnaryTransformType(const UnaryTransformType *T);
66 QualType VisitAutoType(const AutoType *T);
67 // FIXME: DependentDecltypeType
68 QualType VisitRecordType(const RecordType *T);
69 QualType VisitEnumType(const EnumType *T);
70 QualType VisitAttributedType(const AttributedType *T);
71 // FIXME: TemplateTypeParmType
72 // FIXME: SubstTemplateTypeParmType
73 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
74 QualType VisitElaboratedType(const ElaboratedType *T);
75 // FIXME: DependentNameType
76 // FIXME: DependentTemplateSpecializationType
77 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
78 QualType VisitObjCObjectType(const ObjCObjectType *T);
79 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
81 // Importing declarations
82 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
83 DeclContext *&LexicalDC, DeclarationName &Name,
84 NamedDecl *&ToD, SourceLocation &Loc);
85 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
86 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
87 DeclarationNameInfo& To);
88 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
90 /// \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).
96 /// \brief Import everything.
98 /// \brief Import only the bare bones needed to establish a valid
103 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
104 return IDK == IDK_Everything ||
105 (IDK == IDK_Default && !Importer.isMinimalImport());
108 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
109 ImportDefinitionKind Kind = IDK_Default);
110 bool ImportDefinition(VarDecl *From, VarDecl *To,
111 ImportDefinitionKind Kind = IDK_Default);
112 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
113 ImportDefinitionKind Kind = IDK_Default);
114 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
115 ImportDefinitionKind Kind = IDK_Default);
116 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
117 ImportDefinitionKind Kind = IDK_Default);
118 TemplateParameterList *ImportTemplateParameterList(
119 TemplateParameterList *Params);
120 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
121 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
122 unsigned NumFromArgs,
123 SmallVectorImpl<TemplateArgument> &ToArgs);
124 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
125 bool Complain = true);
126 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
127 bool Complain = true);
128 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
129 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
130 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
131 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
132 Decl *VisitDecl(Decl *D);
133 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
134 Decl *VisitNamespaceDecl(NamespaceDecl *D);
135 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
136 Decl *VisitTypedefDecl(TypedefDecl *D);
137 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
138 Decl *VisitEnumDecl(EnumDecl *D);
139 Decl *VisitRecordDecl(RecordDecl *D);
140 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
141 Decl *VisitFunctionDecl(FunctionDecl *D);
142 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
143 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
144 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
145 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
146 Decl *VisitFieldDecl(FieldDecl *D);
147 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
148 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
149 Decl *VisitVarDecl(VarDecl *D);
150 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
151 Decl *VisitParmVarDecl(ParmVarDecl *D);
152 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
153 Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
154 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
155 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
156 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D);
158 ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list);
159 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
160 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
161 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
162 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
163 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
164 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
165 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
166 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
167 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
168 Decl *VisitClassTemplateSpecializationDecl(
169 ClassTemplateSpecializationDecl *D);
170 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
171 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
173 // Importing statements
174 DeclGroupRef ImportDeclGroup(DeclGroupRef DG);
176 Stmt *VisitStmt(Stmt *S);
177 Stmt *VisitDeclStmt(DeclStmt *S);
178 Stmt *VisitNullStmt(NullStmt *S);
179 Stmt *VisitCompoundStmt(CompoundStmt *S);
180 Stmt *VisitCaseStmt(CaseStmt *S);
181 Stmt *VisitDefaultStmt(DefaultStmt *S);
182 Stmt *VisitLabelStmt(LabelStmt *S);
183 Stmt *VisitAttributedStmt(AttributedStmt *S);
184 Stmt *VisitIfStmt(IfStmt *S);
185 Stmt *VisitSwitchStmt(SwitchStmt *S);
186 Stmt *VisitWhileStmt(WhileStmt *S);
187 Stmt *VisitDoStmt(DoStmt *S);
188 Stmt *VisitForStmt(ForStmt *S);
189 Stmt *VisitGotoStmt(GotoStmt *S);
190 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S);
191 Stmt *VisitContinueStmt(ContinueStmt *S);
192 Stmt *VisitBreakStmt(BreakStmt *S);
193 Stmt *VisitReturnStmt(ReturnStmt *S);
196 // FIXME: SEHExceptStmt
197 // FIXME: SEHFinallyStmt
199 // FIXME: SEHLeaveStmt
200 // FIXME: CapturedStmt
201 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S);
202 Stmt *VisitCXXTryStmt(CXXTryStmt *S);
203 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S);
204 // FIXME: MSDependentExistsStmt
205 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
206 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
207 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
208 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
209 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
210 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
211 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
213 // Importing expressions
214 Expr *VisitExpr(Expr *E);
215 Expr *VisitDeclRefExpr(DeclRefExpr *E);
216 Expr *VisitIntegerLiteral(IntegerLiteral *E);
217 Expr *VisitCharacterLiteral(CharacterLiteral *E);
218 Expr *VisitParenExpr(ParenExpr *E);
219 Expr *VisitUnaryOperator(UnaryOperator *E);
220 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
221 Expr *VisitBinaryOperator(BinaryOperator *E);
222 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
223 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
224 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
225 Expr *VisitCXXConstructExpr(CXXConstructExpr *E);
226 Expr *VisitMemberExpr(MemberExpr *E);
227 Expr *VisitCallExpr(CallExpr *E);
230 using namespace clang;
232 //----------------------------------------------------------------------------
233 // Structural Equivalence
234 //----------------------------------------------------------------------------
237 struct StructuralEquivalenceContext {
238 /// \brief AST contexts for which we are checking structural equivalence.
241 /// \brief The set of "tentative" equivalences between two canonical
242 /// declarations, mapping from a declaration in the first context to the
243 /// declaration in the second context that we believe to be equivalent.
244 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
246 /// \brief Queue of declarations in the first context whose equivalence
247 /// with a declaration in the second context still needs to be verified.
248 std::deque<Decl *> DeclsToCheck;
250 /// \brief Declaration (from, to) pairs that are known not to be equivalent
251 /// (which we have already complained about).
252 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
254 /// \brief Whether we're being strict about the spelling of types when
255 /// unifying two types.
256 bool StrictTypeSpelling;
258 /// \brief Whether to complain about failures.
261 /// \brief \c true if the last diagnostic came from C2.
264 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
265 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
266 bool StrictTypeSpelling = false,
267 bool Complain = true)
268 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
269 StrictTypeSpelling(StrictTypeSpelling), Complain(Complain),
270 LastDiagFromC2(false) {}
272 /// \brief Determine whether the two declarations are structurally
274 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
276 /// \brief Determine whether the two types are structurally equivalent.
277 bool IsStructurallyEquivalent(QualType T1, QualType T2);
280 /// \brief Finish checking all of the structural equivalences.
282 /// \returns true if an error occurred, false otherwise.
286 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
287 assert(Complain && "Not allowed to complain");
289 C1.getDiagnostics().notePriorDiagnosticFrom(C2.getDiagnostics());
290 LastDiagFromC2 = false;
291 return C1.getDiagnostics().Report(Loc, DiagID);
294 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
295 assert(Complain && "Not allowed to complain");
297 C2.getDiagnostics().notePriorDiagnosticFrom(C1.getDiagnostics());
298 LastDiagFromC2 = true;
299 return C2.getDiagnostics().Report(Loc, DiagID);
304 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
305 QualType T1, QualType T2);
306 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
309 /// \brief Determine structural equivalence of two expressions.
310 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
311 Expr *E1, Expr *E2) {
315 // FIXME: Actually perform a structural comparison!
319 /// \brief Determine whether two identifiers are equivalent.
320 static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
321 const IdentifierInfo *Name2) {
322 if (!Name1 || !Name2)
323 return Name1 == Name2;
325 return Name1->getName() == Name2->getName();
328 /// \brief Determine whether two nested-name-specifiers are equivalent.
329 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
330 NestedNameSpecifier *NNS1,
331 NestedNameSpecifier *NNS2) {
336 /// \brief Determine whether two template arguments are equivalent.
337 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
338 const TemplateArgument &Arg1,
339 const TemplateArgument &Arg2) {
340 if (Arg1.getKind() != Arg2.getKind())
343 switch (Arg1.getKind()) {
344 case TemplateArgument::Null:
347 case TemplateArgument::Type:
348 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
350 case TemplateArgument::Integral:
351 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
352 Arg2.getIntegralType()))
355 return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral());
357 case TemplateArgument::Declaration:
358 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
360 case TemplateArgument::NullPtr:
361 return true; // FIXME: Is this correct?
363 case TemplateArgument::Template:
364 return IsStructurallyEquivalent(Context,
365 Arg1.getAsTemplate(),
366 Arg2.getAsTemplate());
368 case TemplateArgument::TemplateExpansion:
369 return IsStructurallyEquivalent(Context,
370 Arg1.getAsTemplateOrTemplatePattern(),
371 Arg2.getAsTemplateOrTemplatePattern());
373 case TemplateArgument::Expression:
374 return IsStructurallyEquivalent(Context,
375 Arg1.getAsExpr(), Arg2.getAsExpr());
377 case TemplateArgument::Pack:
378 if (Arg1.pack_size() != Arg2.pack_size())
381 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
382 if (!IsStructurallyEquivalent(Context,
383 Arg1.pack_begin()[I],
384 Arg2.pack_begin()[I]))
390 llvm_unreachable("Invalid template argument kind");
393 /// \brief Determine structural equivalence for the common part of array
395 static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
396 const ArrayType *Array1,
397 const ArrayType *Array2) {
398 if (!IsStructurallyEquivalent(Context,
399 Array1->getElementType(),
400 Array2->getElementType()))
402 if (Array1->getSizeModifier() != Array2->getSizeModifier())
404 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
410 /// \brief Determine structural equivalence of two types.
411 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
412 QualType T1, QualType T2) {
413 if (T1.isNull() || T2.isNull())
414 return T1.isNull() && T2.isNull();
416 if (!Context.StrictTypeSpelling) {
417 // We aren't being strict about token-to-token equivalence of types,
418 // so map down to the canonical type.
419 T1 = Context.C1.getCanonicalType(T1);
420 T2 = Context.C2.getCanonicalType(T2);
423 if (T1.getQualifiers() != T2.getQualifiers())
426 Type::TypeClass TC = T1->getTypeClass();
428 if (T1->getTypeClass() != T2->getTypeClass()) {
429 // Compare function types with prototypes vs. without prototypes as if
430 // both did not have prototypes.
431 if (T1->getTypeClass() == Type::FunctionProto &&
432 T2->getTypeClass() == Type::FunctionNoProto)
433 TC = Type::FunctionNoProto;
434 else if (T1->getTypeClass() == Type::FunctionNoProto &&
435 T2->getTypeClass() == Type::FunctionProto)
436 TC = Type::FunctionNoProto;
443 // FIXME: Deal with Char_S/Char_U.
444 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
449 if (!IsStructurallyEquivalent(Context,
450 cast<ComplexType>(T1)->getElementType(),
451 cast<ComplexType>(T2)->getElementType()))
457 if (!IsStructurallyEquivalent(Context,
458 cast<AdjustedType>(T1)->getOriginalType(),
459 cast<AdjustedType>(T2)->getOriginalType()))
464 if (!IsStructurallyEquivalent(Context,
465 cast<PointerType>(T1)->getPointeeType(),
466 cast<PointerType>(T2)->getPointeeType()))
470 case Type::BlockPointer:
471 if (!IsStructurallyEquivalent(Context,
472 cast<BlockPointerType>(T1)->getPointeeType(),
473 cast<BlockPointerType>(T2)->getPointeeType()))
477 case Type::LValueReference:
478 case Type::RValueReference: {
479 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
480 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
481 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
483 if (Ref1->isInnerRef() != Ref2->isInnerRef())
485 if (!IsStructurallyEquivalent(Context,
486 Ref1->getPointeeTypeAsWritten(),
487 Ref2->getPointeeTypeAsWritten()))
492 case Type::MemberPointer: {
493 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
494 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
495 if (!IsStructurallyEquivalent(Context,
496 MemPtr1->getPointeeType(),
497 MemPtr2->getPointeeType()))
499 if (!IsStructurallyEquivalent(Context,
500 QualType(MemPtr1->getClass(), 0),
501 QualType(MemPtr2->getClass(), 0)))
506 case Type::ConstantArray: {
507 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
508 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
509 if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
512 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
517 case Type::IncompleteArray:
518 if (!IsArrayStructurallyEquivalent(Context,
520 cast<ArrayType>(T2)))
524 case Type::VariableArray: {
525 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
526 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
527 if (!IsStructurallyEquivalent(Context,
528 Array1->getSizeExpr(), Array2->getSizeExpr()))
531 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
537 case Type::DependentSizedArray: {
538 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
539 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
540 if (!IsStructurallyEquivalent(Context,
541 Array1->getSizeExpr(), Array2->getSizeExpr()))
544 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
550 case Type::DependentSizedExtVector: {
551 const DependentSizedExtVectorType *Vec1
552 = cast<DependentSizedExtVectorType>(T1);
553 const DependentSizedExtVectorType *Vec2
554 = cast<DependentSizedExtVectorType>(T2);
555 if (!IsStructurallyEquivalent(Context,
556 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
558 if (!IsStructurallyEquivalent(Context,
559 Vec1->getElementType(),
560 Vec2->getElementType()))
566 case Type::ExtVector: {
567 const VectorType *Vec1 = cast<VectorType>(T1);
568 const VectorType *Vec2 = cast<VectorType>(T2);
569 if (!IsStructurallyEquivalent(Context,
570 Vec1->getElementType(),
571 Vec2->getElementType()))
573 if (Vec1->getNumElements() != Vec2->getNumElements())
575 if (Vec1->getVectorKind() != Vec2->getVectorKind())
580 case Type::FunctionProto: {
581 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
582 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
583 if (Proto1->getNumParams() != Proto2->getNumParams())
585 for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) {
586 if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I),
587 Proto2->getParamType(I)))
590 if (Proto1->isVariadic() != Proto2->isVariadic())
592 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
594 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
595 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
597 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
598 if (!IsStructurallyEquivalent(Context,
599 Proto1->getExceptionType(I),
600 Proto2->getExceptionType(I)))
603 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
604 if (!IsStructurallyEquivalent(Context,
605 Proto1->getNoexceptExpr(),
606 Proto2->getNoexceptExpr()))
609 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
612 // Fall through to check the bits common with FunctionNoProtoType.
615 case Type::FunctionNoProto: {
616 const FunctionType *Function1 = cast<FunctionType>(T1);
617 const FunctionType *Function2 = cast<FunctionType>(T2);
618 if (!IsStructurallyEquivalent(Context, Function1->getReturnType(),
619 Function2->getReturnType()))
621 if (Function1->getExtInfo() != Function2->getExtInfo())
626 case Type::UnresolvedUsing:
627 if (!IsStructurallyEquivalent(Context,
628 cast<UnresolvedUsingType>(T1)->getDecl(),
629 cast<UnresolvedUsingType>(T2)->getDecl()))
634 case Type::Attributed:
635 if (!IsStructurallyEquivalent(Context,
636 cast<AttributedType>(T1)->getModifiedType(),
637 cast<AttributedType>(T2)->getModifiedType()))
639 if (!IsStructurallyEquivalent(Context,
640 cast<AttributedType>(T1)->getEquivalentType(),
641 cast<AttributedType>(T2)->getEquivalentType()))
646 if (!IsStructurallyEquivalent(Context,
647 cast<ParenType>(T1)->getInnerType(),
648 cast<ParenType>(T2)->getInnerType()))
653 if (!IsStructurallyEquivalent(Context,
654 cast<TypedefType>(T1)->getDecl(),
655 cast<TypedefType>(T2)->getDecl()))
659 case Type::TypeOfExpr:
660 if (!IsStructurallyEquivalent(Context,
661 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
662 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
667 if (!IsStructurallyEquivalent(Context,
668 cast<TypeOfType>(T1)->getUnderlyingType(),
669 cast<TypeOfType>(T2)->getUnderlyingType()))
673 case Type::UnaryTransform:
674 if (!IsStructurallyEquivalent(Context,
675 cast<UnaryTransformType>(T1)->getUnderlyingType(),
676 cast<UnaryTransformType>(T1)->getUnderlyingType()))
681 if (!IsStructurallyEquivalent(Context,
682 cast<DecltypeType>(T1)->getUnderlyingExpr(),
683 cast<DecltypeType>(T2)->getUnderlyingExpr()))
688 if (!IsStructurallyEquivalent(Context,
689 cast<AutoType>(T1)->getDeducedType(),
690 cast<AutoType>(T2)->getDeducedType()))
696 if (!IsStructurallyEquivalent(Context,
697 cast<TagType>(T1)->getDecl(),
698 cast<TagType>(T2)->getDecl()))
702 case Type::TemplateTypeParm: {
703 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
704 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
705 if (Parm1->getDepth() != Parm2->getDepth())
707 if (Parm1->getIndex() != Parm2->getIndex())
709 if (Parm1->isParameterPack() != Parm2->isParameterPack())
712 // Names of template type parameters are never significant.
716 case Type::SubstTemplateTypeParm: {
717 const SubstTemplateTypeParmType *Subst1
718 = cast<SubstTemplateTypeParmType>(T1);
719 const SubstTemplateTypeParmType *Subst2
720 = cast<SubstTemplateTypeParmType>(T2);
721 if (!IsStructurallyEquivalent(Context,
722 QualType(Subst1->getReplacedParameter(), 0),
723 QualType(Subst2->getReplacedParameter(), 0)))
725 if (!IsStructurallyEquivalent(Context,
726 Subst1->getReplacementType(),
727 Subst2->getReplacementType()))
732 case Type::SubstTemplateTypeParmPack: {
733 const SubstTemplateTypeParmPackType *Subst1
734 = cast<SubstTemplateTypeParmPackType>(T1);
735 const SubstTemplateTypeParmPackType *Subst2
736 = cast<SubstTemplateTypeParmPackType>(T2);
737 if (!IsStructurallyEquivalent(Context,
738 QualType(Subst1->getReplacedParameter(), 0),
739 QualType(Subst2->getReplacedParameter(), 0)))
741 if (!IsStructurallyEquivalent(Context,
742 Subst1->getArgumentPack(),
743 Subst2->getArgumentPack()))
747 case Type::TemplateSpecialization: {
748 const TemplateSpecializationType *Spec1
749 = cast<TemplateSpecializationType>(T1);
750 const TemplateSpecializationType *Spec2
751 = cast<TemplateSpecializationType>(T2);
752 if (!IsStructurallyEquivalent(Context,
753 Spec1->getTemplateName(),
754 Spec2->getTemplateName()))
756 if (Spec1->getNumArgs() != Spec2->getNumArgs())
758 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
759 if (!IsStructurallyEquivalent(Context,
760 Spec1->getArg(I), Spec2->getArg(I)))
766 case Type::Elaborated: {
767 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
768 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
769 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
770 if (Elab1->getKeyword() != Elab2->getKeyword())
772 if (!IsStructurallyEquivalent(Context,
773 Elab1->getQualifier(),
774 Elab2->getQualifier()))
776 if (!IsStructurallyEquivalent(Context,
777 Elab1->getNamedType(),
778 Elab2->getNamedType()))
783 case Type::InjectedClassName: {
784 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
785 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
786 if (!IsStructurallyEquivalent(Context,
787 Inj1->getInjectedSpecializationType(),
788 Inj2->getInjectedSpecializationType()))
793 case Type::DependentName: {
794 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
795 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
796 if (!IsStructurallyEquivalent(Context,
797 Typename1->getQualifier(),
798 Typename2->getQualifier()))
800 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
801 Typename2->getIdentifier()))
807 case Type::DependentTemplateSpecialization: {
808 const DependentTemplateSpecializationType *Spec1 =
809 cast<DependentTemplateSpecializationType>(T1);
810 const DependentTemplateSpecializationType *Spec2 =
811 cast<DependentTemplateSpecializationType>(T2);
812 if (!IsStructurallyEquivalent(Context,
813 Spec1->getQualifier(),
814 Spec2->getQualifier()))
816 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
817 Spec2->getIdentifier()))
819 if (Spec1->getNumArgs() != Spec2->getNumArgs())
821 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
822 if (!IsStructurallyEquivalent(Context,
823 Spec1->getArg(I), Spec2->getArg(I)))
829 case Type::PackExpansion:
830 if (!IsStructurallyEquivalent(Context,
831 cast<PackExpansionType>(T1)->getPattern(),
832 cast<PackExpansionType>(T2)->getPattern()))
836 case Type::ObjCInterface: {
837 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
838 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
839 if (!IsStructurallyEquivalent(Context,
840 Iface1->getDecl(), Iface2->getDecl()))
845 case Type::ObjCObject: {
846 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
847 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
848 if (!IsStructurallyEquivalent(Context,
850 Obj2->getBaseType()))
852 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
854 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
855 if (!IsStructurallyEquivalent(Context,
856 Obj1->getProtocol(I),
857 Obj2->getProtocol(I)))
863 case Type::ObjCObjectPointer: {
864 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
865 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
866 if (!IsStructurallyEquivalent(Context,
867 Ptr1->getPointeeType(),
868 Ptr2->getPointeeType()))
874 if (!IsStructurallyEquivalent(Context,
875 cast<AtomicType>(T1)->getValueType(),
876 cast<AtomicType>(T2)->getValueType()))
882 if (!IsStructurallyEquivalent(Context,
883 cast<PipeType>(T1)->getElementType(),
884 cast<PipeType>(T2)->getElementType()))
894 /// \brief Determine structural equivalence of two fields.
895 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
896 FieldDecl *Field1, FieldDecl *Field2) {
897 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
899 // For anonymous structs/unions, match up the anonymous struct/union type
900 // declarations directly, so that we don't go off searching for anonymous
902 if (Field1->isAnonymousStructOrUnion() &&
903 Field2->isAnonymousStructOrUnion()) {
904 RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl();
905 RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl();
906 return IsStructurallyEquivalent(Context, D1, D2);
909 // Check for equivalent field names.
910 IdentifierInfo *Name1 = Field1->getIdentifier();
911 IdentifierInfo *Name2 = Field2->getIdentifier();
912 if (!::IsStructurallyEquivalent(Name1, Name2))
915 if (!IsStructurallyEquivalent(Context,
916 Field1->getType(), Field2->getType())) {
917 if (Context.Complain) {
918 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
919 << Context.C2.getTypeDeclType(Owner2);
920 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
921 << Field2->getDeclName() << Field2->getType();
922 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
923 << Field1->getDeclName() << Field1->getType();
928 if (Field1->isBitField() != Field2->isBitField()) {
929 if (Context.Complain) {
930 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
931 << Context.C2.getTypeDeclType(Owner2);
932 if (Field1->isBitField()) {
933 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
934 << Field1->getDeclName() << Field1->getType()
935 << Field1->getBitWidthValue(Context.C1);
936 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
937 << Field2->getDeclName();
939 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
940 << Field2->getDeclName() << Field2->getType()
941 << Field2->getBitWidthValue(Context.C2);
942 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
943 << Field1->getDeclName();
949 if (Field1->isBitField()) {
950 // Make sure that the bit-fields are the same length.
951 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
952 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
954 if (Bits1 != Bits2) {
955 if (Context.Complain) {
956 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
957 << Context.C2.getTypeDeclType(Owner2);
958 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
959 << Field2->getDeclName() << Field2->getType() << Bits2;
960 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
961 << Field1->getDeclName() << Field1->getType() << Bits1;
970 /// \brief Find the index of the given anonymous struct/union within its
973 /// \returns Returns the index of this anonymous struct/union in its context,
974 /// including the next assigned index (if none of them match). Returns an
975 /// empty option if the context is not a record, i.e.. if the anonymous
976 /// struct/union is at namespace or block scope.
977 static Optional<unsigned> findAnonymousStructOrUnionIndex(RecordDecl *Anon) {
978 ASTContext &Context = Anon->getASTContext();
979 QualType AnonTy = Context.getRecordType(Anon);
981 RecordDecl *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext());
986 for (const auto *D : Owner->noload_decls()) {
987 const auto *F = dyn_cast<FieldDecl>(D);
988 if (!F || !F->isAnonymousStructOrUnion())
991 if (Context.hasSameType(F->getType(), AnonTy))
1000 /// \brief Determine structural equivalence of two records.
1001 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1002 RecordDecl *D1, RecordDecl *D2) {
1003 if (D1->isUnion() != D2->isUnion()) {
1004 if (Context.Complain) {
1005 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1006 << Context.C2.getTypeDeclType(D2);
1007 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
1008 << D1->getDeclName() << (unsigned)D1->getTagKind();
1013 if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) {
1014 // If both anonymous structs/unions are in a record context, make sure
1015 // they occur in the same location in the context records.
1016 if (Optional<unsigned> Index1 = findAnonymousStructOrUnionIndex(D1)) {
1017 if (Optional<unsigned> Index2 = findAnonymousStructOrUnionIndex(D2)) {
1018 if (*Index1 != *Index2)
1024 // If both declarations are class template specializations, we know
1025 // the ODR applies, so check the template and template arguments.
1026 ClassTemplateSpecializationDecl *Spec1
1027 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
1028 ClassTemplateSpecializationDecl *Spec2
1029 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
1030 if (Spec1 && Spec2) {
1031 // Check that the specialized templates are the same.
1032 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
1033 Spec2->getSpecializedTemplate()))
1036 // Check that the template arguments are the same.
1037 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
1040 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
1041 if (!IsStructurallyEquivalent(Context,
1042 Spec1->getTemplateArgs().get(I),
1043 Spec2->getTemplateArgs().get(I)))
1046 // If one is a class template specialization and the other is not, these
1047 // structures are different.
1048 else if (Spec1 || Spec2)
1051 // Compare the definitions of these two records. If either or both are
1052 // incomplete, we assume that they are equivalent.
1053 D1 = D1->getDefinition();
1054 D2 = D2->getDefinition();
1058 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
1059 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
1060 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
1061 if (Context.Complain) {
1062 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1063 << Context.C2.getTypeDeclType(D2);
1064 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
1065 << D2CXX->getNumBases();
1066 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
1067 << D1CXX->getNumBases();
1072 // Check the base classes.
1073 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
1074 BaseEnd1 = D1CXX->bases_end(),
1075 Base2 = D2CXX->bases_begin();
1078 if (!IsStructurallyEquivalent(Context,
1079 Base1->getType(), Base2->getType())) {
1080 if (Context.Complain) {
1081 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1082 << Context.C2.getTypeDeclType(D2);
1083 Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
1085 << Base2->getSourceRange();
1086 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1088 << Base1->getSourceRange();
1093 // Check virtual vs. non-virtual inheritance mismatch.
1094 if (Base1->isVirtual() != Base2->isVirtual()) {
1095 if (Context.Complain) {
1096 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1097 << Context.C2.getTypeDeclType(D2);
1098 Context.Diag2(Base2->getLocStart(),
1099 diag::note_odr_virtual_base)
1100 << Base2->isVirtual() << Base2->getSourceRange();
1101 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1102 << Base1->isVirtual()
1103 << Base1->getSourceRange();
1108 } else if (D1CXX->getNumBases() > 0) {
1109 if (Context.Complain) {
1110 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1111 << Context.C2.getTypeDeclType(D2);
1112 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
1113 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1115 << Base1->getSourceRange();
1116 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
1122 // Check the fields for consistency.
1123 RecordDecl::field_iterator Field2 = D2->field_begin(),
1124 Field2End = D2->field_end();
1125 for (RecordDecl::field_iterator Field1 = D1->field_begin(),
1126 Field1End = D1->field_end();
1127 Field1 != Field1End;
1128 ++Field1, ++Field2) {
1129 if (Field2 == Field2End) {
1130 if (Context.Complain) {
1131 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1132 << Context.C2.getTypeDeclType(D2);
1133 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
1134 << Field1->getDeclName() << Field1->getType();
1135 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
1140 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
1144 if (Field2 != Field2End) {
1145 if (Context.Complain) {
1146 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1147 << Context.C2.getTypeDeclType(D2);
1148 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1149 << Field2->getDeclName() << Field2->getType();
1150 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1158 /// \brief Determine structural equivalence of two enums.
1159 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1160 EnumDecl *D1, EnumDecl *D2) {
1161 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1162 EC2End = D2->enumerator_end();
1163 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1164 EC1End = D1->enumerator_end();
1165 EC1 != EC1End; ++EC1, ++EC2) {
1166 if (EC2 == EC2End) {
1167 if (Context.Complain) {
1168 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1169 << Context.C2.getTypeDeclType(D2);
1170 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1171 << EC1->getDeclName()
1172 << EC1->getInitVal().toString(10);
1173 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1178 llvm::APSInt Val1 = EC1->getInitVal();
1179 llvm::APSInt Val2 = EC2->getInitVal();
1180 if (!llvm::APSInt::isSameValue(Val1, Val2) ||
1181 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
1182 if (Context.Complain) {
1183 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1184 << Context.C2.getTypeDeclType(D2);
1185 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1186 << EC2->getDeclName()
1187 << EC2->getInitVal().toString(10);
1188 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1189 << EC1->getDeclName()
1190 << EC1->getInitVal().toString(10);
1196 if (EC2 != EC2End) {
1197 if (Context.Complain) {
1198 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1199 << Context.C2.getTypeDeclType(D2);
1200 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1201 << EC2->getDeclName()
1202 << EC2->getInitVal().toString(10);
1203 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1211 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1212 TemplateParameterList *Params1,
1213 TemplateParameterList *Params2) {
1214 if (Params1->size() != Params2->size()) {
1215 if (Context.Complain) {
1216 Context.Diag2(Params2->getTemplateLoc(),
1217 diag::err_odr_different_num_template_parameters)
1218 << Params1->size() << Params2->size();
1219 Context.Diag1(Params1->getTemplateLoc(),
1220 diag::note_odr_template_parameter_list);
1225 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1226 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1227 if (Context.Complain) {
1228 Context.Diag2(Params2->getParam(I)->getLocation(),
1229 diag::err_odr_different_template_parameter_kind);
1230 Context.Diag1(Params1->getParam(I)->getLocation(),
1231 diag::note_odr_template_parameter_here);
1236 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1237 Params2->getParam(I))) {
1246 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1247 TemplateTypeParmDecl *D1,
1248 TemplateTypeParmDecl *D2) {
1249 if (D1->isParameterPack() != D2->isParameterPack()) {
1250 if (Context.Complain) {
1251 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1252 << D2->isParameterPack();
1253 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1254 << D1->isParameterPack();
1262 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1263 NonTypeTemplateParmDecl *D1,
1264 NonTypeTemplateParmDecl *D2) {
1265 if (D1->isParameterPack() != D2->isParameterPack()) {
1266 if (Context.Complain) {
1267 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1268 << D2->isParameterPack();
1269 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1270 << D1->isParameterPack();
1276 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1277 if (Context.Complain) {
1278 Context.Diag2(D2->getLocation(),
1279 diag::err_odr_non_type_parameter_type_inconsistent)
1280 << D2->getType() << D1->getType();
1281 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1290 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1291 TemplateTemplateParmDecl *D1,
1292 TemplateTemplateParmDecl *D2) {
1293 if (D1->isParameterPack() != D2->isParameterPack()) {
1294 if (Context.Complain) {
1295 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1296 << D2->isParameterPack();
1297 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1298 << D1->isParameterPack();
1303 // Check template parameter lists.
1304 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1305 D2->getTemplateParameters());
1308 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1309 ClassTemplateDecl *D1,
1310 ClassTemplateDecl *D2) {
1311 // Check template parameters.
1312 if (!IsStructurallyEquivalent(Context,
1313 D1->getTemplateParameters(),
1314 D2->getTemplateParameters()))
1317 // Check the templated declaration.
1318 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1319 D2->getTemplatedDecl());
1322 /// \brief Determine structural equivalence of two declarations.
1323 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1324 Decl *D1, Decl *D2) {
1325 // FIXME: Check for known structural equivalences via a callback of some sort.
1327 // Check whether we already know that these two declarations are not
1328 // structurally equivalent.
1329 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1330 D2->getCanonicalDecl())))
1333 // Determine whether we've already produced a tentative equivalence for D1.
1334 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1336 return EquivToD1 == D2->getCanonicalDecl();
1338 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1339 EquivToD1 = D2->getCanonicalDecl();
1340 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1344 bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1346 if (!::IsStructurallyEquivalent(*this, D1, D2))
1352 bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1354 if (!::IsStructurallyEquivalent(*this, T1, T2))
1360 bool StructuralEquivalenceContext::Finish() {
1361 while (!DeclsToCheck.empty()) {
1362 // Check the next declaration.
1363 Decl *D1 = DeclsToCheck.front();
1364 DeclsToCheck.pop_front();
1366 Decl *D2 = TentativeEquivalences[D1];
1367 assert(D2 && "Unrecorded tentative equivalence?");
1369 bool Equivalent = true;
1371 // FIXME: Switch on all declaration kinds. For now, we're just going to
1372 // check the obvious ones.
1373 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1374 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1375 // Check for equivalent structure names.
1376 IdentifierInfo *Name1 = Record1->getIdentifier();
1377 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1378 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
1379 IdentifierInfo *Name2 = Record2->getIdentifier();
1380 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1381 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
1382 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1383 !::IsStructurallyEquivalent(*this, Record1, Record2))
1386 // Record/non-record mismatch.
1389 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
1390 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1391 // Check for equivalent enum names.
1392 IdentifierInfo *Name1 = Enum1->getIdentifier();
1393 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1394 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
1395 IdentifierInfo *Name2 = Enum2->getIdentifier();
1396 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1397 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
1398 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1399 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1402 // Enum/non-enum mismatch
1405 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1406 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
1407 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
1408 Typedef2->getIdentifier()) ||
1409 !::IsStructurallyEquivalent(*this,
1410 Typedef1->getUnderlyingType(),
1411 Typedef2->getUnderlyingType()))
1414 // Typedef/non-typedef mismatch.
1417 } else if (ClassTemplateDecl *ClassTemplate1
1418 = dyn_cast<ClassTemplateDecl>(D1)) {
1419 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1420 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1421 ClassTemplate2->getIdentifier()) ||
1422 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1425 // Class template/non-class-template mismatch.
1428 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1429 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1430 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1436 } else if (NonTypeTemplateParmDecl *NTTP1
1437 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1438 if (NonTypeTemplateParmDecl *NTTP2
1439 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1440 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1446 } else if (TemplateTemplateParmDecl *TTP1
1447 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1448 if (TemplateTemplateParmDecl *TTP2
1449 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1450 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1459 // Note that these two declarations are not equivalent (and we already
1461 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1462 D2->getCanonicalDecl()));
1465 // FIXME: Check other declaration kinds!
1471 //----------------------------------------------------------------------------
1473 //----------------------------------------------------------------------------
1475 QualType ASTNodeImporter::VisitType(const Type *T) {
1476 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1477 << T->getTypeClassName();
1481 QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
1482 switch (T->getKind()) {
1483 #define SHARED_SINGLETON_TYPE(Expansion)
1484 #define BUILTIN_TYPE(Id, SingletonId) \
1485 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1486 #include "clang/AST/BuiltinTypes.def"
1488 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1489 // context supports C++.
1491 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1492 // context supports ObjC.
1494 case BuiltinType::Char_U:
1495 // The context we're importing from has an unsigned 'char'. If we're
1496 // importing into a context with a signed 'char', translate to
1497 // 'unsigned char' instead.
1498 if (Importer.getToContext().getLangOpts().CharIsSigned)
1499 return Importer.getToContext().UnsignedCharTy;
1501 return Importer.getToContext().CharTy;
1503 case BuiltinType::Char_S:
1504 // The context we're importing from has an unsigned 'char'. If we're
1505 // importing into a context with a signed 'char', translate to
1506 // 'unsigned char' instead.
1507 if (!Importer.getToContext().getLangOpts().CharIsSigned)
1508 return Importer.getToContext().SignedCharTy;
1510 return Importer.getToContext().CharTy;
1512 case BuiltinType::WChar_S:
1513 case BuiltinType::WChar_U:
1514 // FIXME: If not in C++, shall we translate to the C equivalent of
1516 return Importer.getToContext().WCharTy;
1519 llvm_unreachable("Invalid BuiltinType Kind!");
1522 QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
1523 QualType ToElementType = Importer.Import(T->getElementType());
1524 if (ToElementType.isNull())
1527 return Importer.getToContext().getComplexType(ToElementType);
1530 QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
1531 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1532 if (ToPointeeType.isNull())
1535 return Importer.getToContext().getPointerType(ToPointeeType);
1538 QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
1539 // FIXME: Check for blocks support in "to" context.
1540 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1541 if (ToPointeeType.isNull())
1544 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1548 ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
1549 // FIXME: Check for C++ support in "to" context.
1550 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1551 if (ToPointeeType.isNull())
1554 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1558 ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
1559 // FIXME: Check for C++0x support in "to" context.
1560 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1561 if (ToPointeeType.isNull())
1564 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1567 QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
1568 // FIXME: Check for C++ support in "to" context.
1569 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1570 if (ToPointeeType.isNull())
1573 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1574 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1575 ClassType.getTypePtr());
1578 QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
1579 QualType ToElementType = Importer.Import(T->getElementType());
1580 if (ToElementType.isNull())
1583 return Importer.getToContext().getConstantArrayType(ToElementType,
1585 T->getSizeModifier(),
1586 T->getIndexTypeCVRQualifiers());
1590 ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
1591 QualType ToElementType = Importer.Import(T->getElementType());
1592 if (ToElementType.isNull())
1595 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1596 T->getSizeModifier(),
1597 T->getIndexTypeCVRQualifiers());
1600 QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
1601 QualType ToElementType = Importer.Import(T->getElementType());
1602 if (ToElementType.isNull())
1605 Expr *Size = Importer.Import(T->getSizeExpr());
1609 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1610 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1611 T->getSizeModifier(),
1612 T->getIndexTypeCVRQualifiers(),
1616 QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
1617 QualType ToElementType = Importer.Import(T->getElementType());
1618 if (ToElementType.isNull())
1621 return Importer.getToContext().getVectorType(ToElementType,
1622 T->getNumElements(),
1623 T->getVectorKind());
1626 QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
1627 QualType ToElementType = Importer.Import(T->getElementType());
1628 if (ToElementType.isNull())
1631 return Importer.getToContext().getExtVectorType(ToElementType,
1632 T->getNumElements());
1636 ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
1637 // FIXME: What happens if we're importing a function without a prototype
1638 // into C++? Should we make it variadic?
1639 QualType ToResultType = Importer.Import(T->getReturnType());
1640 if (ToResultType.isNull())
1643 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
1647 QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
1648 QualType ToResultType = Importer.Import(T->getReturnType());
1649 if (ToResultType.isNull())
1652 // Import argument types
1653 SmallVector<QualType, 4> ArgTypes;
1654 for (const auto &A : T->param_types()) {
1655 QualType ArgType = Importer.Import(A);
1656 if (ArgType.isNull())
1658 ArgTypes.push_back(ArgType);
1661 // Import exception types
1662 SmallVector<QualType, 4> ExceptionTypes;
1663 for (const auto &E : T->exceptions()) {
1664 QualType ExceptionType = Importer.Import(E);
1665 if (ExceptionType.isNull())
1667 ExceptionTypes.push_back(ExceptionType);
1670 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1671 FunctionProtoType::ExtProtoInfo ToEPI;
1673 ToEPI.ExtInfo = FromEPI.ExtInfo;
1674 ToEPI.Variadic = FromEPI.Variadic;
1675 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1676 ToEPI.TypeQuals = FromEPI.TypeQuals;
1677 ToEPI.RefQualifier = FromEPI.RefQualifier;
1678 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1679 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
1680 ToEPI.ExceptionSpec.NoexceptExpr =
1681 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
1682 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
1683 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
1684 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
1685 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
1687 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
1690 QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1691 QualType ToInnerType = Importer.Import(T->getInnerType());
1692 if (ToInnerType.isNull())
1695 return Importer.getToContext().getParenType(ToInnerType);
1698 QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
1699 TypedefNameDecl *ToDecl
1700 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
1704 return Importer.getToContext().getTypeDeclType(ToDecl);
1707 QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
1708 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1712 return Importer.getToContext().getTypeOfExprType(ToExpr);
1715 QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
1716 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1717 if (ToUnderlyingType.isNull())
1720 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1723 QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
1724 // FIXME: Make sure that the "to" context supports C++0x!
1725 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1729 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1730 if (UnderlyingType.isNull())
1733 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
1736 QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1737 QualType ToBaseType = Importer.Import(T->getBaseType());
1738 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1739 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1742 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1747 QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1748 // FIXME: Make sure that the "to" context supports C++11!
1749 QualType FromDeduced = T->getDeducedType();
1751 if (!FromDeduced.isNull()) {
1752 ToDeduced = Importer.Import(FromDeduced);
1753 if (ToDeduced.isNull())
1757 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
1758 /*IsDependent*/false);
1761 QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
1763 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1767 return Importer.getToContext().getTagDeclType(ToDecl);
1770 QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
1772 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1776 return Importer.getToContext().getTagDeclType(ToDecl);
1779 QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
1780 QualType FromModifiedType = T->getModifiedType();
1781 QualType FromEquivalentType = T->getEquivalentType();
1782 QualType ToModifiedType;
1783 QualType ToEquivalentType;
1785 if (!FromModifiedType.isNull()) {
1786 ToModifiedType = Importer.Import(FromModifiedType);
1787 if (ToModifiedType.isNull())
1790 if (!FromEquivalentType.isNull()) {
1791 ToEquivalentType = Importer.Import(FromEquivalentType);
1792 if (ToEquivalentType.isNull())
1796 return Importer.getToContext().getAttributedType(T->getAttrKind(),
1797 ToModifiedType, ToEquivalentType);
1800 QualType ASTNodeImporter::VisitTemplateSpecializationType(
1801 const TemplateSpecializationType *T) {
1802 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1803 if (ToTemplate.isNull())
1806 SmallVector<TemplateArgument, 2> ToTemplateArgs;
1807 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1810 QualType ToCanonType;
1811 if (!QualType(T, 0).isCanonical()) {
1812 QualType FromCanonType
1813 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1814 ToCanonType =Importer.Import(FromCanonType);
1815 if (ToCanonType.isNull())
1818 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1819 ToTemplateArgs.data(),
1820 ToTemplateArgs.size(),
1824 QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
1825 NestedNameSpecifier *ToQualifier = nullptr;
1826 // Note: the qualifier in an ElaboratedType is optional.
1827 if (T->getQualifier()) {
1828 ToQualifier = Importer.Import(T->getQualifier());
1833 QualType ToNamedType = Importer.Import(T->getNamedType());
1834 if (ToNamedType.isNull())
1837 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1838 ToQualifier, ToNamedType);
1841 QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
1842 ObjCInterfaceDecl *Class
1843 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1847 return Importer.getToContext().getObjCInterfaceType(Class);
1850 QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
1851 QualType ToBaseType = Importer.Import(T->getBaseType());
1852 if (ToBaseType.isNull())
1855 SmallVector<QualType, 4> TypeArgs;
1856 for (auto TypeArg : T->getTypeArgsAsWritten()) {
1857 QualType ImportedTypeArg = Importer.Import(TypeArg);
1858 if (ImportedTypeArg.isNull())
1861 TypeArgs.push_back(ImportedTypeArg);
1864 SmallVector<ObjCProtocolDecl *, 4> Protocols;
1865 for (auto *P : T->quals()) {
1866 ObjCProtocolDecl *Protocol
1867 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
1870 Protocols.push_back(Protocol);
1873 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
1875 T->isKindOfTypeAsWritten());
1879 ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
1880 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1881 if (ToPointeeType.isNull())
1884 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
1887 //----------------------------------------------------------------------------
1888 // Import Declarations
1889 //----------------------------------------------------------------------------
1890 bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1891 DeclContext *&LexicalDC,
1892 DeclarationName &Name,
1894 SourceLocation &Loc) {
1895 // Import the context of this declaration.
1896 DC = Importer.ImportContext(D->getDeclContext());
1901 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1902 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1907 // Import the name of this declaration.
1908 Name = Importer.Import(D->getDeclName());
1909 if (D->getDeclName() && !Name)
1912 // Import the location of this declaration.
1913 Loc = Importer.Import(D->getLocation());
1914 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
1918 void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1923 ToD = Importer.Import(FromD);
1928 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1929 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
1930 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
1931 ImportDefinition(FromRecord, ToRecord);
1937 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1938 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1939 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1940 ImportDefinition(FromEnum, ToEnum);
1948 ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1949 DeclarationNameInfo& To) {
1950 // NOTE: To.Name and To.Loc are already imported.
1951 // We only have to import To.LocInfo.
1952 switch (To.getName().getNameKind()) {
1953 case DeclarationName::Identifier:
1954 case DeclarationName::ObjCZeroArgSelector:
1955 case DeclarationName::ObjCOneArgSelector:
1956 case DeclarationName::ObjCMultiArgSelector:
1957 case DeclarationName::CXXUsingDirective:
1960 case DeclarationName::CXXOperatorName: {
1961 SourceRange Range = From.getCXXOperatorNameRange();
1962 To.setCXXOperatorNameRange(Importer.Import(Range));
1965 case DeclarationName::CXXLiteralOperatorName: {
1966 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1967 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1970 case DeclarationName::CXXConstructorName:
1971 case DeclarationName::CXXDestructorName:
1972 case DeclarationName::CXXConversionFunctionName: {
1973 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1974 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1978 llvm_unreachable("Unknown name kind.");
1981 void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
1982 if (Importer.isMinimalImport() && !ForceImport) {
1983 Importer.ImportContext(FromDC);
1987 for (auto *From : FromDC->decls())
1988 Importer.Import(From);
1991 bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
1992 ImportDefinitionKind Kind) {
1993 if (To->getDefinition() || To->isBeingDefined()) {
1994 if (Kind == IDK_Everything)
1995 ImportDeclContext(From, /*ForceImport=*/true);
2000 To->startDefinition();
2002 // Add base classes.
2003 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
2004 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
2006 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
2007 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
2008 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
2009 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
2010 ToData.Aggregate = FromData.Aggregate;
2011 ToData.PlainOldData = FromData.PlainOldData;
2012 ToData.Empty = FromData.Empty;
2013 ToData.Polymorphic = FromData.Polymorphic;
2014 ToData.Abstract = FromData.Abstract;
2015 ToData.IsStandardLayout = FromData.IsStandardLayout;
2016 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
2017 ToData.HasPrivateFields = FromData.HasPrivateFields;
2018 ToData.HasProtectedFields = FromData.HasProtectedFields;
2019 ToData.HasPublicFields = FromData.HasPublicFields;
2020 ToData.HasMutableFields = FromData.HasMutableFields;
2021 ToData.HasVariantMembers = FromData.HasVariantMembers;
2022 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
2023 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
2024 ToData.HasUninitializedReferenceMember
2025 = FromData.HasUninitializedReferenceMember;
2026 ToData.NeedOverloadResolutionForMoveConstructor
2027 = FromData.NeedOverloadResolutionForMoveConstructor;
2028 ToData.NeedOverloadResolutionForMoveAssignment
2029 = FromData.NeedOverloadResolutionForMoveAssignment;
2030 ToData.NeedOverloadResolutionForDestructor
2031 = FromData.NeedOverloadResolutionForDestructor;
2032 ToData.DefaultedMoveConstructorIsDeleted
2033 = FromData.DefaultedMoveConstructorIsDeleted;
2034 ToData.DefaultedMoveAssignmentIsDeleted
2035 = FromData.DefaultedMoveAssignmentIsDeleted;
2036 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
2037 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
2038 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
2039 ToData.HasConstexprNonCopyMoveConstructor
2040 = FromData.HasConstexprNonCopyMoveConstructor;
2041 ToData.DefaultedDefaultConstructorIsConstexpr
2042 = FromData.DefaultedDefaultConstructorIsConstexpr;
2043 ToData.HasConstexprDefaultConstructor
2044 = FromData.HasConstexprDefaultConstructor;
2045 ToData.HasNonLiteralTypeFieldsOrBases
2046 = FromData.HasNonLiteralTypeFieldsOrBases;
2047 // ComputedVisibleConversions not imported.
2048 ToData.UserProvidedDefaultConstructor
2049 = FromData.UserProvidedDefaultConstructor;
2050 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
2051 ToData.ImplicitCopyConstructorHasConstParam
2052 = FromData.ImplicitCopyConstructorHasConstParam;
2053 ToData.ImplicitCopyAssignmentHasConstParam
2054 = FromData.ImplicitCopyAssignmentHasConstParam;
2055 ToData.HasDeclaredCopyConstructorWithConstParam
2056 = FromData.HasDeclaredCopyConstructorWithConstParam;
2057 ToData.HasDeclaredCopyAssignmentWithConstParam
2058 = FromData.HasDeclaredCopyAssignmentWithConstParam;
2059 ToData.IsLambda = FromData.IsLambda;
2061 SmallVector<CXXBaseSpecifier *, 4> Bases;
2062 for (const auto &Base1 : FromCXX->bases()) {
2063 QualType T = Importer.Import(Base1.getType());
2067 SourceLocation EllipsisLoc;
2068 if (Base1.isPackExpansion())
2069 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
2071 // Ensure that we have a definition for the base.
2072 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
2075 new (Importer.getToContext())
2076 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
2078 Base1.isBaseOfClass(),
2079 Base1.getAccessSpecifierAsWritten(),
2080 Importer.Import(Base1.getTypeSourceInfo()),
2084 ToCXX->setBases(Bases.data(), Bases.size());
2087 if (shouldForceImportDeclContext(Kind))
2088 ImportDeclContext(From, /*ForceImport=*/true);
2090 To->completeDefinition();
2094 bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
2095 ImportDefinitionKind Kind) {
2096 if (To->getAnyInitializer())
2099 // FIXME: Can we really import any initializer? Alternatively, we could force
2100 // ourselves to import every declaration of a variable and then only use
2102 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
2104 // FIXME: Other bits to merge?
2109 bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
2110 ImportDefinitionKind Kind) {
2111 if (To->getDefinition() || To->isBeingDefined()) {
2112 if (Kind == IDK_Everything)
2113 ImportDeclContext(From, /*ForceImport=*/true);
2117 To->startDefinition();
2119 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
2123 QualType ToPromotionType = Importer.Import(From->getPromotionType());
2124 if (ToPromotionType.isNull())
2127 if (shouldForceImportDeclContext(Kind))
2128 ImportDeclContext(From, /*ForceImport=*/true);
2130 // FIXME: we might need to merge the number of positive or negative bits
2131 // if the enumerator lists don't match.
2132 To->completeDefinition(T, ToPromotionType,
2133 From->getNumPositiveBits(),
2134 From->getNumNegativeBits());
2138 TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
2139 TemplateParameterList *Params) {
2140 SmallVector<NamedDecl *, 4> ToParams;
2141 ToParams.reserve(Params->size());
2142 for (TemplateParameterList::iterator P = Params->begin(),
2143 PEnd = Params->end();
2145 Decl *To = Importer.Import(*P);
2149 ToParams.push_back(cast<NamedDecl>(To));
2152 return TemplateParameterList::Create(Importer.getToContext(),
2153 Importer.Import(Params->getTemplateLoc()),
2154 Importer.Import(Params->getLAngleLoc()),
2156 Importer.Import(Params->getRAngleLoc()));
2160 ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
2161 switch (From.getKind()) {
2162 case TemplateArgument::Null:
2163 return TemplateArgument();
2165 case TemplateArgument::Type: {
2166 QualType ToType = Importer.Import(From.getAsType());
2167 if (ToType.isNull())
2168 return TemplateArgument();
2169 return TemplateArgument(ToType);
2172 case TemplateArgument::Integral: {
2173 QualType ToType = Importer.Import(From.getIntegralType());
2174 if (ToType.isNull())
2175 return TemplateArgument();
2176 return TemplateArgument(From, ToType);
2179 case TemplateArgument::Declaration: {
2180 ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
2181 QualType ToType = Importer.Import(From.getParamTypeForDecl());
2182 if (!To || ToType.isNull())
2183 return TemplateArgument();
2184 return TemplateArgument(To, ToType);
2187 case TemplateArgument::NullPtr: {
2188 QualType ToType = Importer.Import(From.getNullPtrType());
2189 if (ToType.isNull())
2190 return TemplateArgument();
2191 return TemplateArgument(ToType, /*isNullPtr*/true);
2194 case TemplateArgument::Template: {
2195 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
2196 if (ToTemplate.isNull())
2197 return TemplateArgument();
2199 return TemplateArgument(ToTemplate);
2202 case TemplateArgument::TemplateExpansion: {
2203 TemplateName ToTemplate
2204 = Importer.Import(From.getAsTemplateOrTemplatePattern());
2205 if (ToTemplate.isNull())
2206 return TemplateArgument();
2208 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
2211 case TemplateArgument::Expression:
2212 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2213 return TemplateArgument(ToExpr);
2214 return TemplateArgument();
2216 case TemplateArgument::Pack: {
2217 SmallVector<TemplateArgument, 2> ToPack;
2218 ToPack.reserve(From.pack_size());
2219 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2220 return TemplateArgument();
2222 return TemplateArgument(
2223 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
2227 llvm_unreachable("Invalid template argument kind");
2230 bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2231 unsigned NumFromArgs,
2232 SmallVectorImpl<TemplateArgument> &ToArgs) {
2233 for (unsigned I = 0; I != NumFromArgs; ++I) {
2234 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2235 if (To.isNull() && !FromArgs[I].isNull())
2238 ToArgs.push_back(To);
2244 bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
2245 RecordDecl *ToRecord, bool Complain) {
2246 // Eliminate a potential failure point where we attempt to re-import
2247 // something we're trying to import while completing ToRecord.
2248 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
2250 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
2252 ToRecord = ToOriginRecord;
2255 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2256 ToRecord->getASTContext(),
2257 Importer.getNonEquivalentDecls(),
2259 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
2262 bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
2264 StructuralEquivalenceContext Ctx(
2265 Importer.getFromContext(), Importer.getToContext(),
2266 Importer.getNonEquivalentDecls(), false, Complain);
2267 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
2270 bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
2271 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2272 Importer.getToContext(),
2273 Importer.getNonEquivalentDecls());
2274 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
2277 bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
2278 EnumConstantDecl *ToEC)
2280 const llvm::APSInt &FromVal = FromEC->getInitVal();
2281 const llvm::APSInt &ToVal = ToEC->getInitVal();
2283 return FromVal.isSigned() == ToVal.isSigned() &&
2284 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2288 bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
2289 ClassTemplateDecl *To) {
2290 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2291 Importer.getToContext(),
2292 Importer.getNonEquivalentDecls());
2293 return Ctx.IsStructurallyEquivalent(From, To);
2296 bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2297 VarTemplateDecl *To) {
2298 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2299 Importer.getToContext(),
2300 Importer.getNonEquivalentDecls());
2301 return Ctx.IsStructurallyEquivalent(From, To);
2304 Decl *ASTNodeImporter::VisitDecl(Decl *D) {
2305 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
2306 << D->getDeclKindName();
2310 Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2311 TranslationUnitDecl *ToD =
2312 Importer.getToContext().getTranslationUnitDecl();
2314 Importer.Imported(D, ToD);
2319 Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2320 // Import the major distinguishing characteristics of this namespace.
2321 DeclContext *DC, *LexicalDC;
2322 DeclarationName Name;
2325 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2330 NamespaceDecl *MergeWithNamespace = nullptr;
2332 // This is an anonymous namespace. Adopt an existing anonymous
2333 // namespace if we can.
2334 // FIXME: Not testable.
2335 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2336 MergeWithNamespace = TU->getAnonymousNamespace();
2338 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2340 SmallVector<NamedDecl *, 4> ConflictingDecls;
2341 SmallVector<NamedDecl *, 2> FoundDecls;
2342 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2343 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2344 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
2347 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
2348 MergeWithNamespace = FoundNS;
2349 ConflictingDecls.clear();
2353 ConflictingDecls.push_back(FoundDecls[I]);
2356 if (!ConflictingDecls.empty()) {
2357 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
2358 ConflictingDecls.data(),
2359 ConflictingDecls.size());
2363 // Create the "to" namespace, if needed.
2364 NamespaceDecl *ToNamespace = MergeWithNamespace;
2366 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
2368 Importer.Import(D->getLocStart()),
2369 Loc, Name.getAsIdentifierInfo(),
2370 /*PrevDecl=*/nullptr);
2371 ToNamespace->setLexicalDeclContext(LexicalDC);
2372 LexicalDC->addDeclInternal(ToNamespace);
2374 // If this is an anonymous namespace, register it as the anonymous
2375 // namespace within its context.
2377 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2378 TU->setAnonymousNamespace(ToNamespace);
2380 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2383 Importer.Imported(D, ToNamespace);
2385 ImportDeclContext(D);
2390 Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
2391 // Import the major distinguishing characteristics of this typedef.
2392 DeclContext *DC, *LexicalDC;
2393 DeclarationName Name;
2396 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2401 // If this typedef is not in block scope, determine whether we've
2402 // seen a typedef with the same name (that we can merge with) or any
2403 // other entity by that name (which name lookup could conflict with).
2404 if (!DC->isFunctionOrMethod()) {
2405 SmallVector<NamedDecl *, 4> ConflictingDecls;
2406 unsigned IDNS = Decl::IDNS_Ordinary;
2407 SmallVector<NamedDecl *, 2> FoundDecls;
2408 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2409 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2410 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
2412 if (TypedefNameDecl *FoundTypedef =
2413 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
2414 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2415 FoundTypedef->getUnderlyingType()))
2416 return Importer.Imported(D, FoundTypedef);
2419 ConflictingDecls.push_back(FoundDecls[I]);
2422 if (!ConflictingDecls.empty()) {
2423 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2424 ConflictingDecls.data(),
2425 ConflictingDecls.size());
2431 // Import the underlying type of this typedef;
2432 QualType T = Importer.Import(D->getUnderlyingType());
2436 // Create the new typedef node.
2437 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2438 SourceLocation StartL = Importer.Import(D->getLocStart());
2439 TypedefNameDecl *ToTypedef;
2441 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2443 Name.getAsIdentifierInfo(),
2446 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2448 Name.getAsIdentifierInfo(),
2451 ToTypedef->setAccess(D->getAccess());
2452 ToTypedef->setLexicalDeclContext(LexicalDC);
2453 Importer.Imported(D, ToTypedef);
2454 LexicalDC->addDeclInternal(ToTypedef);
2459 Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2460 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2463 Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2464 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2467 Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2468 // Import the major distinguishing characteristics of this enum.
2469 DeclContext *DC, *LexicalDC;
2470 DeclarationName Name;
2473 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2478 // Figure out what enum name we're looking for.
2479 unsigned IDNS = Decl::IDNS_Tag;
2480 DeclarationName SearchName = Name;
2481 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2482 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
2483 IDNS = Decl::IDNS_Ordinary;
2484 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
2485 IDNS |= Decl::IDNS_Ordinary;
2487 // We may already have an enum of the same name; try to find and match it.
2488 if (!DC->isFunctionOrMethod() && SearchName) {
2489 SmallVector<NamedDecl *, 4> ConflictingDecls;
2490 SmallVector<NamedDecl *, 2> FoundDecls;
2491 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2492 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2493 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
2496 Decl *Found = FoundDecls[I];
2497 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2498 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2499 Found = Tag->getDecl();
2502 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
2503 if (IsStructuralMatch(D, FoundEnum))
2504 return Importer.Imported(D, FoundEnum);
2507 ConflictingDecls.push_back(FoundDecls[I]);
2510 if (!ConflictingDecls.empty()) {
2511 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2512 ConflictingDecls.data(),
2513 ConflictingDecls.size());
2517 // Create the enum declaration.
2518 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2519 Importer.Import(D->getLocStart()),
2520 Loc, Name.getAsIdentifierInfo(), nullptr,
2521 D->isScoped(), D->isScopedUsingClassTag(),
2523 // Import the qualifier, if any.
2524 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
2525 D2->setAccess(D->getAccess());
2526 D2->setLexicalDeclContext(LexicalDC);
2527 Importer.Imported(D, D2);
2528 LexicalDC->addDeclInternal(D2);
2530 // Import the integer type.
2531 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2532 if (ToIntegerType.isNull())
2534 D2->setIntegerType(ToIntegerType);
2536 // Import the definition
2537 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
2543 Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2544 // If this record has a definition in the translation unit we're coming from,
2545 // but this particular declaration is not that definition, import the
2546 // definition and map to that.
2547 TagDecl *Definition = D->getDefinition();
2548 if (Definition && Definition != D) {
2549 Decl *ImportedDef = Importer.Import(Definition);
2553 return Importer.Imported(D, ImportedDef);
2556 // Import the major distinguishing characteristics of this record.
2557 DeclContext *DC, *LexicalDC;
2558 DeclarationName Name;
2561 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2566 // Figure out what structure name we're looking for.
2567 unsigned IDNS = Decl::IDNS_Tag;
2568 DeclarationName SearchName = Name;
2569 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2570 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
2571 IDNS = Decl::IDNS_Ordinary;
2572 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
2573 IDNS |= Decl::IDNS_Ordinary;
2575 // We may already have a record of the same name; try to find and match it.
2576 RecordDecl *AdoptDecl = nullptr;
2577 if (!DC->isFunctionOrMethod()) {
2578 SmallVector<NamedDecl *, 4> ConflictingDecls;
2579 SmallVector<NamedDecl *, 2> FoundDecls;
2580 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2581 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2582 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
2585 Decl *Found = FoundDecls[I];
2586 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2587 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2588 Found = Tag->getDecl();
2591 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
2592 if (D->isAnonymousStructOrUnion() &&
2593 FoundRecord->isAnonymousStructOrUnion()) {
2594 // If both anonymous structs/unions are in a record context, make sure
2595 // they occur in the same location in the context records.
2596 if (Optional<unsigned> Index1
2597 = findAnonymousStructOrUnionIndex(D)) {
2598 if (Optional<unsigned> Index2 =
2599 findAnonymousStructOrUnionIndex(FoundRecord)) {
2600 if (*Index1 != *Index2)
2606 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
2607 if ((SearchName && !D->isCompleteDefinition())
2608 || (D->isCompleteDefinition() &&
2609 D->isAnonymousStructOrUnion()
2610 == FoundDef->isAnonymousStructOrUnion() &&
2611 IsStructuralMatch(D, FoundDef))) {
2612 // The record types structurally match, or the "from" translation
2613 // unit only had a forward declaration anyway; call it the same
2615 // FIXME: For C++, we should also merge methods here.
2616 return Importer.Imported(D, FoundDef);
2618 } else if (!D->isCompleteDefinition()) {
2619 // We have a forward declaration of this type, so adopt that forward
2620 // declaration rather than building a new one.
2622 // If one or both can be completed from external storage then try one
2623 // last time to complete and compare them before doing this.
2625 if (FoundRecord->hasExternalLexicalStorage() &&
2626 !FoundRecord->isCompleteDefinition())
2627 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2628 if (D->hasExternalLexicalStorage())
2629 D->getASTContext().getExternalSource()->CompleteType(D);
2631 if (FoundRecord->isCompleteDefinition() &&
2632 D->isCompleteDefinition() &&
2633 !IsStructuralMatch(D, FoundRecord))
2636 AdoptDecl = FoundRecord;
2638 } else if (!SearchName) {
2643 ConflictingDecls.push_back(FoundDecls[I]);
2646 if (!ConflictingDecls.empty() && SearchName) {
2647 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2648 ConflictingDecls.data(),
2649 ConflictingDecls.size());
2653 // Create the record declaration.
2654 RecordDecl *D2 = AdoptDecl;
2655 SourceLocation StartLoc = Importer.Import(D->getLocStart());
2657 if (isa<CXXRecordDecl>(D)) {
2658 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
2661 Name.getAsIdentifierInfo());
2663 D2->setAccess(D->getAccess());
2665 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
2666 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
2669 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
2670 D2->setLexicalDeclContext(LexicalDC);
2671 LexicalDC->addDeclInternal(D2);
2672 if (D->isAnonymousStructOrUnion())
2673 D2->setAnonymousStructOrUnion(true);
2676 Importer.Imported(D, D2);
2678 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
2684 Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2685 // Import the major distinguishing characteristics of this enumerator.
2686 DeclContext *DC, *LexicalDC;
2687 DeclarationName Name;
2690 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2695 QualType T = Importer.Import(D->getType());
2699 // Determine whether there are any other declarations with the same name and
2700 // in the same context.
2701 if (!LexicalDC->isFunctionOrMethod()) {
2702 SmallVector<NamedDecl *, 4> ConflictingDecls;
2703 unsigned IDNS = Decl::IDNS_Ordinary;
2704 SmallVector<NamedDecl *, 2> FoundDecls;
2705 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2706 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2707 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
2710 if (EnumConstantDecl *FoundEnumConstant
2711 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
2712 if (IsStructuralMatch(D, FoundEnumConstant))
2713 return Importer.Imported(D, FoundEnumConstant);
2716 ConflictingDecls.push_back(FoundDecls[I]);
2719 if (!ConflictingDecls.empty()) {
2720 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2721 ConflictingDecls.data(),
2722 ConflictingDecls.size());
2728 Expr *Init = Importer.Import(D->getInitExpr());
2729 if (D->getInitExpr() && !Init)
2732 EnumConstantDecl *ToEnumerator
2733 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2734 Name.getAsIdentifierInfo(), T,
2735 Init, D->getInitVal());
2736 ToEnumerator->setAccess(D->getAccess());
2737 ToEnumerator->setLexicalDeclContext(LexicalDC);
2738 Importer.Imported(D, ToEnumerator);
2739 LexicalDC->addDeclInternal(ToEnumerator);
2740 return ToEnumerator;
2743 Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2744 // Import the major distinguishing characteristics of this function.
2745 DeclContext *DC, *LexicalDC;
2746 DeclarationName Name;
2749 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2754 // Try to find a function in our own ("to") context with the same name, same
2755 // type, and in the same context as the function we're importing.
2756 if (!LexicalDC->isFunctionOrMethod()) {
2757 SmallVector<NamedDecl *, 4> ConflictingDecls;
2758 unsigned IDNS = Decl::IDNS_Ordinary;
2759 SmallVector<NamedDecl *, 2> FoundDecls;
2760 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2761 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2762 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
2765 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
2766 if (FoundFunction->hasExternalFormalLinkage() &&
2767 D->hasExternalFormalLinkage()) {
2768 if (Importer.IsStructurallyEquivalent(D->getType(),
2769 FoundFunction->getType())) {
2770 // FIXME: Actually try to merge the body and other attributes.
2771 return Importer.Imported(D, FoundFunction);
2774 // FIXME: Check for overloading more carefully, e.g., by boosting
2775 // Sema::IsOverload out to the AST library.
2777 // Function overloading is okay in C++.
2778 if (Importer.getToContext().getLangOpts().CPlusPlus)
2781 // Complain about inconsistent function types.
2782 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
2783 << Name << D->getType() << FoundFunction->getType();
2784 Importer.ToDiag(FoundFunction->getLocation(),
2785 diag::note_odr_value_here)
2786 << FoundFunction->getType();
2790 ConflictingDecls.push_back(FoundDecls[I]);
2793 if (!ConflictingDecls.empty()) {
2794 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2795 ConflictingDecls.data(),
2796 ConflictingDecls.size());
2802 DeclarationNameInfo NameInfo(Name, Loc);
2803 // Import additional name location/type info.
2804 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2806 QualType FromTy = D->getType();
2807 bool usedDifferentExceptionSpec = false;
2809 if (const FunctionProtoType *
2810 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2811 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2812 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2813 // FunctionDecl that we are importing the FunctionProtoType for.
2814 // To avoid an infinite recursion when importing, create the FunctionDecl
2815 // with a simplified function type and update it afterwards.
2816 if (FromEPI.ExceptionSpec.SourceDecl ||
2817 FromEPI.ExceptionSpec.SourceTemplate ||
2818 FromEPI.ExceptionSpec.NoexceptExpr) {
2819 FunctionProtoType::ExtProtoInfo DefaultEPI;
2820 FromTy = Importer.getFromContext().getFunctionType(
2821 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
2822 usedDifferentExceptionSpec = true;
2827 QualType T = Importer.Import(FromTy);
2831 // Import the function parameters.
2832 SmallVector<ParmVarDecl *, 8> Parameters;
2833 for (auto P : D->params()) {
2834 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
2838 Parameters.push_back(ToP);
2841 // Create the imported function.
2842 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2843 FunctionDecl *ToFunction = nullptr;
2844 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
2845 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2846 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2847 cast<CXXRecordDecl>(DC),
2850 FromConstructor->isExplicit(),
2851 D->isInlineSpecified(),
2854 } else if (isa<CXXDestructorDecl>(D)) {
2855 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2856 cast<CXXRecordDecl>(DC),
2859 D->isInlineSpecified(),
2861 } else if (CXXConversionDecl *FromConversion
2862 = dyn_cast<CXXConversionDecl>(D)) {
2863 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2864 cast<CXXRecordDecl>(DC),
2867 D->isInlineSpecified(),
2868 FromConversion->isExplicit(),
2870 Importer.Import(D->getLocEnd()));
2871 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2872 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2873 cast<CXXRecordDecl>(DC),
2876 Method->getStorageClass(),
2877 Method->isInlineSpecified(),
2879 Importer.Import(D->getLocEnd()));
2881 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
2883 NameInfo, T, TInfo, D->getStorageClass(),
2884 D->isInlineSpecified(),
2885 D->hasWrittenPrototype(),
2889 // Import the qualifier, if any.
2890 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
2891 ToFunction->setAccess(D->getAccess());
2892 ToFunction->setLexicalDeclContext(LexicalDC);
2893 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2894 ToFunction->setTrivial(D->isTrivial());
2895 ToFunction->setPure(D->isPure());
2896 Importer.Imported(D, ToFunction);
2898 // Set the parameters.
2899 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
2900 Parameters[I]->setOwningFunction(ToFunction);
2901 ToFunction->addDeclInternal(Parameters[I]);
2903 ToFunction->setParams(Parameters);
2905 if (usedDifferentExceptionSpec) {
2906 // Update FunctionProtoType::ExtProtoInfo.
2907 QualType T = Importer.Import(D->getType());
2910 ToFunction->setType(T);
2913 // Import the body, if any.
2914 if (Stmt *FromBody = D->getBody()) {
2915 if (Stmt *ToBody = Importer.Import(FromBody)) {
2916 ToFunction->setBody(ToBody);
2920 // FIXME: Other bits to merge?
2922 // Add this function to the lexical context.
2923 LexicalDC->addDeclInternal(ToFunction);
2928 Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2929 return VisitFunctionDecl(D);
2932 Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2933 return VisitCXXMethodDecl(D);
2936 Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2937 return VisitCXXMethodDecl(D);
2940 Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2941 return VisitCXXMethodDecl(D);
2944 static unsigned getFieldIndex(Decl *F) {
2945 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
2950 for (const auto *D : Owner->noload_decls()) {
2954 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2961 Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2962 // Import the major distinguishing characteristics of a variable.
2963 DeclContext *DC, *LexicalDC;
2964 DeclarationName Name;
2967 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2972 // Determine whether we've already imported this field.
2973 SmallVector<NamedDecl *, 2> FoundDecls;
2974 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2975 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2976 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
2977 // For anonymous fields, match up by index.
2978 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2981 if (Importer.IsStructurallyEquivalent(D->getType(),
2982 FoundField->getType())) {
2983 Importer.Imported(D, FoundField);
2987 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2988 << Name << D->getType() << FoundField->getType();
2989 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2990 << FoundField->getType();
2996 QualType T = Importer.Import(D->getType());
3000 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3001 Expr *BitWidth = Importer.Import(D->getBitWidth());
3002 if (!BitWidth && D->getBitWidth())
3005 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
3006 Importer.Import(D->getInnerLocStart()),
3007 Loc, Name.getAsIdentifierInfo(),
3008 T, TInfo, BitWidth, D->isMutable(),
3009 D->getInClassInitStyle());
3010 ToField->setAccess(D->getAccess());
3011 ToField->setLexicalDeclContext(LexicalDC);
3012 if (ToField->hasInClassInitializer())
3013 ToField->setInClassInitializer(D->getInClassInitializer());
3014 ToField->setImplicit(D->isImplicit());
3015 Importer.Imported(D, ToField);
3016 LexicalDC->addDeclInternal(ToField);
3020 Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
3021 // Import the major distinguishing characteristics of a variable.
3022 DeclContext *DC, *LexicalDC;
3023 DeclarationName Name;
3026 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3031 // Determine whether we've already imported this field.
3032 SmallVector<NamedDecl *, 2> FoundDecls;
3033 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3034 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3035 if (IndirectFieldDecl *FoundField
3036 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
3037 // For anonymous indirect fields, match up by index.
3038 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
3041 if (Importer.IsStructurallyEquivalent(D->getType(),
3042 FoundField->getType(),
3044 Importer.Imported(D, FoundField);
3048 // If there are more anonymous fields to check, continue.
3049 if (!Name && I < N-1)
3052 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3053 << Name << D->getType() << FoundField->getType();
3054 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3055 << FoundField->getType();
3061 QualType T = Importer.Import(D->getType());
3065 NamedDecl **NamedChain =
3066 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
3069 for (auto *PI : D->chain()) {
3070 Decl *D = Importer.Import(PI);
3073 NamedChain[i++] = cast<NamedDecl>(D);
3076 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
3077 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
3078 NamedChain, D->getChainingSize());
3080 for (const auto *Attr : D->attrs())
3081 ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
3083 ToIndirectField->setAccess(D->getAccess());
3084 ToIndirectField->setLexicalDeclContext(LexicalDC);
3085 Importer.Imported(D, ToIndirectField);
3086 LexicalDC->addDeclInternal(ToIndirectField);
3087 return ToIndirectField;
3090 Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
3091 // Import the major distinguishing characteristics of an ivar.
3092 DeclContext *DC, *LexicalDC;
3093 DeclarationName Name;
3096 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3101 // Determine whether we've already imported this ivar
3102 SmallVector<NamedDecl *, 2> FoundDecls;
3103 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3104 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3105 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
3106 if (Importer.IsStructurallyEquivalent(D->getType(),
3107 FoundIvar->getType())) {
3108 Importer.Imported(D, FoundIvar);
3112 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
3113 << Name << D->getType() << FoundIvar->getType();
3114 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3115 << FoundIvar->getType();
3121 QualType T = Importer.Import(D->getType());
3125 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3126 Expr *BitWidth = Importer.Import(D->getBitWidth());
3127 if (!BitWidth && D->getBitWidth())
3130 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
3131 cast<ObjCContainerDecl>(DC),
3132 Importer.Import(D->getInnerLocStart()),
3133 Loc, Name.getAsIdentifierInfo(),
3134 T, TInfo, D->getAccessControl(),
3135 BitWidth, D->getSynthesize());
3136 ToIvar->setLexicalDeclContext(LexicalDC);
3137 Importer.Imported(D, ToIvar);
3138 LexicalDC->addDeclInternal(ToIvar);
3143 Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
3144 // Import the major distinguishing characteristics of a variable.
3145 DeclContext *DC, *LexicalDC;
3146 DeclarationName Name;
3149 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3154 // Try to find a variable in our own ("to") context with the same name and
3155 // in the same context as the variable we're importing.
3156 if (D->isFileVarDecl()) {
3157 VarDecl *MergeWithVar = nullptr;
3158 SmallVector<NamedDecl *, 4> ConflictingDecls;
3159 unsigned IDNS = Decl::IDNS_Ordinary;
3160 SmallVector<NamedDecl *, 2> FoundDecls;
3161 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3162 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3163 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
3166 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
3167 // We have found a variable that we may need to merge with. Check it.
3168 if (FoundVar->hasExternalFormalLinkage() &&
3169 D->hasExternalFormalLinkage()) {
3170 if (Importer.IsStructurallyEquivalent(D->getType(),
3171 FoundVar->getType())) {
3172 MergeWithVar = FoundVar;
3176 const ArrayType *FoundArray
3177 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3178 const ArrayType *TArray
3179 = Importer.getToContext().getAsArrayType(D->getType());
3180 if (FoundArray && TArray) {
3181 if (isa<IncompleteArrayType>(FoundArray) &&
3182 isa<ConstantArrayType>(TArray)) {
3184 QualType T = Importer.Import(D->getType());
3188 FoundVar->setType(T);
3189 MergeWithVar = FoundVar;
3191 } else if (isa<IncompleteArrayType>(TArray) &&
3192 isa<ConstantArrayType>(FoundArray)) {
3193 MergeWithVar = FoundVar;
3198 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
3199 << Name << D->getType() << FoundVar->getType();
3200 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3201 << FoundVar->getType();
3205 ConflictingDecls.push_back(FoundDecls[I]);
3209 // An equivalent variable with external linkage has been found. Link
3210 // the two declarations, then merge them.
3211 Importer.Imported(D, MergeWithVar);
3213 if (VarDecl *DDef = D->getDefinition()) {
3214 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
3215 Importer.ToDiag(ExistingDef->getLocation(),
3216 diag::err_odr_variable_multiple_def)
3218 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
3220 Expr *Init = Importer.Import(DDef->getInit());
3221 MergeWithVar->setInit(Init);
3222 if (DDef->isInitKnownICE()) {
3223 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
3224 Eval->CheckedICE = true;
3225 Eval->IsICE = DDef->isInitICE();
3230 return MergeWithVar;
3233 if (!ConflictingDecls.empty()) {
3234 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3235 ConflictingDecls.data(),
3236 ConflictingDecls.size());
3243 QualType T = Importer.Import(D->getType());
3247 // Create the imported variable.
3248 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3249 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
3250 Importer.Import(D->getInnerLocStart()),
3251 Loc, Name.getAsIdentifierInfo(),
3253 D->getStorageClass());
3254 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
3255 ToVar->setAccess(D->getAccess());
3256 ToVar->setLexicalDeclContext(LexicalDC);
3257 Importer.Imported(D, ToVar);
3258 LexicalDC->addDeclInternal(ToVar);
3260 if (!D->isFileVarDecl() &&
3264 // Merge the initializer.
3265 if (ImportDefinition(D, ToVar))
3271 Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
3272 // Parameters are created in the translation unit's context, then moved
3273 // into the function declaration's context afterward.
3274 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3276 // Import the name of this declaration.
3277 DeclarationName Name = Importer.Import(D->getDeclName());
3278 if (D->getDeclName() && !Name)
3281 // Import the location of this declaration.
3282 SourceLocation Loc = Importer.Import(D->getLocation());
3284 // Import the parameter's type.
3285 QualType T = Importer.Import(D->getType());
3289 // Create the imported parameter.
3290 ImplicitParamDecl *ToParm
3291 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
3292 Loc, Name.getAsIdentifierInfo(),
3294 return Importer.Imported(D, ToParm);
3297 Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
3298 // Parameters are created in the translation unit's context, then moved
3299 // into the function declaration's context afterward.
3300 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3302 // Import the name of this declaration.
3303 DeclarationName Name = Importer.Import(D->getDeclName());
3304 if (D->getDeclName() && !Name)
3307 // Import the location of this declaration.
3308 SourceLocation Loc = Importer.Import(D->getLocation());
3310 // Import the parameter's type.
3311 QualType T = Importer.Import(D->getType());
3315 // Create the imported parameter.
3316 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3317 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
3318 Importer.Import(D->getInnerLocStart()),
3319 Loc, Name.getAsIdentifierInfo(),
3320 T, TInfo, D->getStorageClass(),
3321 /*FIXME: Default argument*/nullptr);
3322 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
3325 ToParm->setIsUsed();
3327 return Importer.Imported(D, ToParm);
3330 Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3331 // Import the major distinguishing characteristics of a method.
3332 DeclContext *DC, *LexicalDC;
3333 DeclarationName Name;
3336 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3341 SmallVector<NamedDecl *, 2> FoundDecls;
3342 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3343 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3344 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
3345 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3348 // Check return types.
3349 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3350 FoundMethod->getReturnType())) {
3351 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
3352 << D->isInstanceMethod() << Name << D->getReturnType()
3353 << FoundMethod->getReturnType();
3354 Importer.ToDiag(FoundMethod->getLocation(),
3355 diag::note_odr_objc_method_here)
3356 << D->isInstanceMethod() << Name;
3360 // Check the number of parameters.
3361 if (D->param_size() != FoundMethod->param_size()) {
3362 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3363 << D->isInstanceMethod() << Name
3364 << D->param_size() << FoundMethod->param_size();
3365 Importer.ToDiag(FoundMethod->getLocation(),
3366 diag::note_odr_objc_method_here)
3367 << D->isInstanceMethod() << Name;
3371 // Check parameter types.
3372 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
3373 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3374 P != PEnd; ++P, ++FoundP) {
3375 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3376 (*FoundP)->getType())) {
3377 Importer.FromDiag((*P)->getLocation(),
3378 diag::err_odr_objc_method_param_type_inconsistent)
3379 << D->isInstanceMethod() << Name
3380 << (*P)->getType() << (*FoundP)->getType();
3381 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3382 << (*FoundP)->getType();
3387 // Check variadic/non-variadic.
3388 // Check the number of parameters.
3389 if (D->isVariadic() != FoundMethod->isVariadic()) {
3390 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3391 << D->isInstanceMethod() << Name;
3392 Importer.ToDiag(FoundMethod->getLocation(),
3393 diag::note_odr_objc_method_here)
3394 << D->isInstanceMethod() << Name;
3398 // FIXME: Any other bits we need to merge?
3399 return Importer.Imported(D, FoundMethod);
3403 // Import the result type.
3404 QualType ResultTy = Importer.Import(D->getReturnType());
3405 if (ResultTy.isNull())
3408 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
3410 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
3411 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3412 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3413 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3414 D->getImplementationControl(), D->hasRelatedResultType());
3416 // FIXME: When we decide to merge method definitions, we'll need to
3417 // deal with implicit parameters.
3419 // Import the parameters
3420 SmallVector<ParmVarDecl *, 5> ToParams;
3421 for (auto *FromP : D->params()) {
3422 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
3426 ToParams.push_back(ToP);
3429 // Set the parameters.
3430 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3431 ToParams[I]->setOwningFunction(ToMethod);
3432 ToMethod->addDeclInternal(ToParams[I]);
3434 SmallVector<SourceLocation, 12> SelLocs;
3435 D->getSelectorLocs(SelLocs);
3436 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
3438 ToMethod->setLexicalDeclContext(LexicalDC);
3439 Importer.Imported(D, ToMethod);
3440 LexicalDC->addDeclInternal(ToMethod);
3444 Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3445 // Import the major distinguishing characteristics of a category.
3446 DeclContext *DC, *LexicalDC;
3447 DeclarationName Name;
3450 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3455 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3459 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
3460 Importer.getToContext(), DC,
3462 Importer.Import(D->getVarianceLoc()),
3464 Importer.Import(D->getLocation()),
3465 Name.getAsIdentifierInfo(),
3466 Importer.Import(D->getColonLoc()),
3468 Importer.Imported(D, Result);
3469 Result->setLexicalDeclContext(LexicalDC);
3473 Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3474 // Import the major distinguishing characteristics of a category.
3475 DeclContext *DC, *LexicalDC;
3476 DeclarationName Name;
3479 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3484 ObjCInterfaceDecl *ToInterface
3485 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3489 // Determine if we've already encountered this category.
3490 ObjCCategoryDecl *MergeWithCategory
3491 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3492 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3494 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
3495 Importer.Import(D->getAtStartLoc()),
3497 Importer.Import(D->getCategoryNameLoc()),
3498 Name.getAsIdentifierInfo(),
3500 /*TypeParamList=*/nullptr,
3501 Importer.Import(D->getIvarLBraceLoc()),
3502 Importer.Import(D->getIvarRBraceLoc()));
3503 ToCategory->setLexicalDeclContext(LexicalDC);
3504 LexicalDC->addDeclInternal(ToCategory);
3505 Importer.Imported(D, ToCategory);
3506 // Import the type parameter list after calling Imported, to avoid
3507 // loops when bringing in their DeclContext.
3508 ToCategory->setTypeParamList(ImportObjCTypeParamList(
3509 D->getTypeParamList()));
3512 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3513 SmallVector<SourceLocation, 4> ProtocolLocs;
3514 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3515 = D->protocol_loc_begin();
3516 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3517 FromProtoEnd = D->protocol_end();
3518 FromProto != FromProtoEnd;
3519 ++FromProto, ++FromProtoLoc) {
3520 ObjCProtocolDecl *ToProto
3521 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3524 Protocols.push_back(ToProto);
3525 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3528 // FIXME: If we're merging, make sure that the protocol list is the same.
3529 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3530 ProtocolLocs.data(), Importer.getToContext());
3533 Importer.Imported(D, ToCategory);
3536 // Import all of the members of this category.
3537 ImportDeclContext(D);
3539 // If we have an implementation, import it as well.
3540 if (D->getImplementation()) {
3541 ObjCCategoryImplDecl *Impl
3542 = cast_or_null<ObjCCategoryImplDecl>(
3543 Importer.Import(D->getImplementation()));
3547 ToCategory->setImplementation(Impl);
3553 bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3554 ObjCProtocolDecl *To,
3555 ImportDefinitionKind Kind) {
3556 if (To->getDefinition()) {
3557 if (shouldForceImportDeclContext(Kind))
3558 ImportDeclContext(From);
3562 // Start the protocol definition
3563 To->startDefinition();
3566 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3567 SmallVector<SourceLocation, 4> ProtocolLocs;
3568 ObjCProtocolDecl::protocol_loc_iterator
3569 FromProtoLoc = From->protocol_loc_begin();
3570 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3571 FromProtoEnd = From->protocol_end();
3572 FromProto != FromProtoEnd;
3573 ++FromProto, ++FromProtoLoc) {
3574 ObjCProtocolDecl *ToProto
3575 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3578 Protocols.push_back(ToProto);
3579 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3582 // FIXME: If we're merging, make sure that the protocol list is the same.
3583 To->setProtocolList(Protocols.data(), Protocols.size(),
3584 ProtocolLocs.data(), Importer.getToContext());
3586 if (shouldForceImportDeclContext(Kind)) {
3587 // Import all of the members of this protocol.
3588 ImportDeclContext(From, /*ForceImport=*/true);
3593 Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
3594 // If this protocol has a definition in the translation unit we're coming
3595 // from, but this particular declaration is not that definition, import the
3596 // definition and map to that.
3597 ObjCProtocolDecl *Definition = D->getDefinition();
3598 if (Definition && Definition != D) {
3599 Decl *ImportedDef = Importer.Import(Definition);
3603 return Importer.Imported(D, ImportedDef);
3606 // Import the major distinguishing characteristics of a protocol.
3607 DeclContext *DC, *LexicalDC;
3608 DeclarationName Name;
3611 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3616 ObjCProtocolDecl *MergeWithProtocol = nullptr;
3617 SmallVector<NamedDecl *, 2> FoundDecls;
3618 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3619 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3620 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
3623 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
3627 ObjCProtocolDecl *ToProto = MergeWithProtocol;
3629 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3630 Name.getAsIdentifierInfo(), Loc,
3631 Importer.Import(D->getAtStartLoc()),
3632 /*PrevDecl=*/nullptr);
3633 ToProto->setLexicalDeclContext(LexicalDC);
3634 LexicalDC->addDeclInternal(ToProto);
3637 Importer.Imported(D, ToProto);
3639 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
3645 Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3646 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3647 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3649 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3650 SourceLocation LangLoc = Importer.Import(D->getLocation());
3652 bool HasBraces = D->hasBraces();
3654 LinkageSpecDecl *ToLinkageSpec =
3655 LinkageSpecDecl::Create(Importer.getToContext(),
3663 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3664 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3667 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3668 LexicalDC->addDeclInternal(ToLinkageSpec);
3670 Importer.Imported(D, ToLinkageSpec);
3672 return ToLinkageSpec;
3675 bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3676 ObjCInterfaceDecl *To,
3677 ImportDefinitionKind Kind) {
3678 if (To->getDefinition()) {
3679 // Check consistency of superclass.
3680 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3682 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3687 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3688 if ((bool)FromSuper != (bool)ToSuper ||
3689 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3690 Importer.ToDiag(To->getLocation(),
3691 diag::err_odr_objc_superclass_inconsistent)
3692 << To->getDeclName();
3694 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3695 << To->getSuperClass()->getDeclName();
3697 Importer.ToDiag(To->getLocation(),
3698 diag::note_odr_objc_missing_superclass);
3699 if (From->getSuperClass())
3700 Importer.FromDiag(From->getSuperClassLoc(),
3701 diag::note_odr_objc_superclass)
3702 << From->getSuperClass()->getDeclName();
3704 Importer.FromDiag(From->getLocation(),
3705 diag::note_odr_objc_missing_superclass);
3708 if (shouldForceImportDeclContext(Kind))
3709 ImportDeclContext(From);
3713 // Start the definition.
3714 To->startDefinition();
3716 // If this class has a superclass, import it.
3717 if (From->getSuperClass()) {
3718 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3722 To->setSuperClass(SuperTInfo);
3726 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3727 SmallVector<SourceLocation, 4> ProtocolLocs;
3728 ObjCInterfaceDecl::protocol_loc_iterator
3729 FromProtoLoc = From->protocol_loc_begin();
3731 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3732 FromProtoEnd = From->protocol_end();
3733 FromProto != FromProtoEnd;
3734 ++FromProto, ++FromProtoLoc) {
3735 ObjCProtocolDecl *ToProto
3736 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3739 Protocols.push_back(ToProto);
3740 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3743 // FIXME: If we're merging, make sure that the protocol list is the same.
3744 To->setProtocolList(Protocols.data(), Protocols.size(),
3745 ProtocolLocs.data(), Importer.getToContext());
3747 // Import categories. When the categories themselves are imported, they'll
3748 // hook themselves into this interface.
3749 for (auto *Cat : From->known_categories())
3750 Importer.Import(Cat);
3752 // If we have an @implementation, import it as well.
3753 if (From->getImplementation()) {
3754 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3755 Importer.Import(From->getImplementation()));
3759 To->setImplementation(Impl);
3762 if (shouldForceImportDeclContext(Kind)) {
3763 // Import all of the members of this class.
3764 ImportDeclContext(From, /*ForceImport=*/true);
3770 ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3774 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
3775 for (auto fromTypeParam : *list) {
3776 auto toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3777 Importer.Import(fromTypeParam));
3781 toTypeParams.push_back(toTypeParam);
3784 return ObjCTypeParamList::create(Importer.getToContext(),
3785 Importer.Import(list->getLAngleLoc()),
3787 Importer.Import(list->getRAngleLoc()));
3790 Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
3791 // If this class has a definition in the translation unit we're coming from,
3792 // but this particular declaration is not that definition, import the
3793 // definition and map to that.
3794 ObjCInterfaceDecl *Definition = D->getDefinition();
3795 if (Definition && Definition != D) {
3796 Decl *ImportedDef = Importer.Import(Definition);
3800 return Importer.Imported(D, ImportedDef);
3803 // Import the major distinguishing characteristics of an @interface.
3804 DeclContext *DC, *LexicalDC;
3805 DeclarationName Name;
3808 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3813 // Look for an existing interface with the same name.
3814 ObjCInterfaceDecl *MergeWithIface = nullptr;
3815 SmallVector<NamedDecl *, 2> FoundDecls;
3816 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3817 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3818 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3821 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
3825 // Create an interface declaration, if one does not already exist.
3826 ObjCInterfaceDecl *ToIface = MergeWithIface;
3828 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3829 Importer.Import(D->getAtStartLoc()),
3830 Name.getAsIdentifierInfo(),
3831 /*TypeParamList=*/nullptr,
3832 /*PrevDecl=*/nullptr, Loc,
3833 D->isImplicitInterfaceDecl());
3834 ToIface->setLexicalDeclContext(LexicalDC);
3835 LexicalDC->addDeclInternal(ToIface);
3837 Importer.Imported(D, ToIface);
3838 // Import the type parameter list after calling Imported, to avoid
3839 // loops when bringing in their DeclContext.
3840 ToIface->setTypeParamList(ImportObjCTypeParamList(
3841 D->getTypeParamListAsWritten()));
3843 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
3849 Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3850 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3851 Importer.Import(D->getCategoryDecl()));
3855 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3857 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3861 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
3862 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
3863 Importer.Import(D->getIdentifier()),
3864 Category->getClassInterface(),
3865 Importer.Import(D->getLocation()),
3866 Importer.Import(D->getAtStartLoc()),
3869 DeclContext *LexicalDC = DC;
3870 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3871 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3875 ToImpl->setLexicalDeclContext(LexicalDC);
3878 LexicalDC->addDeclInternal(ToImpl);
3879 Category->setImplementation(ToImpl);
3882 Importer.Imported(D, ToImpl);
3883 ImportDeclContext(D);
3887 Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3888 // Find the corresponding interface.
3889 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3890 Importer.Import(D->getClassInterface()));
3894 // Import the superclass, if any.
3895 ObjCInterfaceDecl *Super = nullptr;
3896 if (D->getSuperClass()) {
3897 Super = cast_or_null<ObjCInterfaceDecl>(
3898 Importer.Import(D->getSuperClass()));
3903 ObjCImplementationDecl *Impl = Iface->getImplementation();
3905 // We haven't imported an implementation yet. Create a new @implementation
3907 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3908 Importer.ImportContext(D->getDeclContext()),
3910 Importer.Import(D->getLocation()),
3911 Importer.Import(D->getAtStartLoc()),
3912 Importer.Import(D->getSuperClassLoc()),
3913 Importer.Import(D->getIvarLBraceLoc()),
3914 Importer.Import(D->getIvarRBraceLoc()));
3916 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3917 DeclContext *LexicalDC
3918 = Importer.ImportContext(D->getLexicalDeclContext());
3921 Impl->setLexicalDeclContext(LexicalDC);
3924 // Associate the implementation with the class it implements.
3925 Iface->setImplementation(Impl);
3926 Importer.Imported(D, Iface->getImplementation());
3928 Importer.Imported(D, Iface->getImplementation());
3930 // Verify that the existing @implementation has the same superclass.
3931 if ((Super && !Impl->getSuperClass()) ||
3932 (!Super && Impl->getSuperClass()) ||
3933 (Super && Impl->getSuperClass() &&
3934 !declaresSameEntity(Super->getCanonicalDecl(),
3935 Impl->getSuperClass()))) {
3936 Importer.ToDiag(Impl->getLocation(),
3937 diag::err_odr_objc_superclass_inconsistent)
3938 << Iface->getDeclName();
3939 // FIXME: It would be nice to have the location of the superclass
3941 if (Impl->getSuperClass())
3942 Importer.ToDiag(Impl->getLocation(),
3943 diag::note_odr_objc_superclass)
3944 << Impl->getSuperClass()->getDeclName();
3946 Importer.ToDiag(Impl->getLocation(),
3947 diag::note_odr_objc_missing_superclass);
3948 if (D->getSuperClass())
3949 Importer.FromDiag(D->getLocation(),
3950 diag::note_odr_objc_superclass)
3951 << D->getSuperClass()->getDeclName();
3953 Importer.FromDiag(D->getLocation(),
3954 diag::note_odr_objc_missing_superclass);
3959 // Import all of the members of this @implementation.
3960 ImportDeclContext(D);
3965 Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3966 // Import the major distinguishing characteristics of an @property.
3967 DeclContext *DC, *LexicalDC;
3968 DeclarationName Name;
3971 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3976 // Check whether we have already imported this property.
3977 SmallVector<NamedDecl *, 2> FoundDecls;
3978 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3979 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3980 if (ObjCPropertyDecl *FoundProp
3981 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
3982 // Check property types.
3983 if (!Importer.IsStructurallyEquivalent(D->getType(),
3984 FoundProp->getType())) {
3985 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3986 << Name << D->getType() << FoundProp->getType();
3987 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3988 << FoundProp->getType();
3992 // FIXME: Check property attributes, getters, setters, etc.?
3994 // Consider these properties to be equivalent.
3995 Importer.Imported(D, FoundProp);
4001 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
4005 // Create the new property.
4006 ObjCPropertyDecl *ToProperty
4007 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
4008 Name.getAsIdentifierInfo(),
4009 Importer.Import(D->getAtLoc()),
4010 Importer.Import(D->getLParenLoc()),
4011 Importer.Import(D->getType()),
4013 D->getPropertyImplementation());
4014 Importer.Imported(D, ToProperty);
4015 ToProperty->setLexicalDeclContext(LexicalDC);
4016 LexicalDC->addDeclInternal(ToProperty);
4018 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
4019 ToProperty->setPropertyAttributesAsWritten(
4020 D->getPropertyAttributesAsWritten());
4021 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
4022 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
4023 ToProperty->setGetterMethodDecl(
4024 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
4025 ToProperty->setSetterMethodDecl(
4026 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
4027 ToProperty->setPropertyIvarDecl(
4028 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
4032 Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4033 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
4034 Importer.Import(D->getPropertyDecl()));
4038 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4042 // Import the lexical declaration context.
4043 DeclContext *LexicalDC = DC;
4044 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4045 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4050 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
4054 // Import the ivar (for an @synthesize).
4055 ObjCIvarDecl *Ivar = nullptr;
4056 if (D->getPropertyIvarDecl()) {
4057 Ivar = cast_or_null<ObjCIvarDecl>(
4058 Importer.Import(D->getPropertyIvarDecl()));
4063 ObjCPropertyImplDecl *ToImpl
4064 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4065 Property->getQueryKind());
4067 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
4068 Importer.Import(D->getLocStart()),
4069 Importer.Import(D->getLocation()),
4071 D->getPropertyImplementation(),
4073 Importer.Import(D->getPropertyIvarDeclLoc()));
4074 ToImpl->setLexicalDeclContext(LexicalDC);
4075 Importer.Imported(D, ToImpl);
4076 LexicalDC->addDeclInternal(ToImpl);
4078 // Check that we have the same kind of property implementation (@synthesize
4080 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
4081 Importer.ToDiag(ToImpl->getLocation(),
4082 diag::err_odr_objc_property_impl_kind_inconsistent)
4083 << Property->getDeclName()
4084 << (ToImpl->getPropertyImplementation()
4085 == ObjCPropertyImplDecl::Dynamic);
4086 Importer.FromDiag(D->getLocation(),
4087 diag::note_odr_objc_property_impl_kind)
4088 << D->getPropertyDecl()->getDeclName()
4089 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
4093 // For @synthesize, check that we have the same
4094 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4095 Ivar != ToImpl->getPropertyIvarDecl()) {
4096 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
4097 diag::err_odr_objc_synthesize_ivar_inconsistent)
4098 << Property->getDeclName()
4099 << ToImpl->getPropertyIvarDecl()->getDeclName()
4100 << Ivar->getDeclName();
4101 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
4102 diag::note_odr_objc_synthesize_ivar_here)
4103 << D->getPropertyIvarDecl()->getDeclName();
4107 // Merge the existing implementation with the new implementation.
4108 Importer.Imported(D, ToImpl);
4114 Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
4115 // For template arguments, we adopt the translation unit as our declaration
4116 // context. This context will be fixed when the actual template declaration
4119 // FIXME: Import default argument.
4120 return TemplateTypeParmDecl::Create(Importer.getToContext(),
4121 Importer.getToContext().getTranslationUnitDecl(),
4122 Importer.Import(D->getLocStart()),
4123 Importer.Import(D->getLocation()),
4126 Importer.Import(D->getIdentifier()),
4127 D->wasDeclaredWithTypename(),
4128 D->isParameterPack());
4132 ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
4133 // Import the name of this declaration.
4134 DeclarationName Name = Importer.Import(D->getDeclName());
4135 if (D->getDeclName() && !Name)
4138 // Import the location of this declaration.
4139 SourceLocation Loc = Importer.Import(D->getLocation());
4141 // Import the type of this declaration.
4142 QualType T = Importer.Import(D->getType());
4146 // Import type-source information.
4147 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4148 if (D->getTypeSourceInfo() && !TInfo)
4151 // FIXME: Import default argument.
4153 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
4154 Importer.getToContext().getTranslationUnitDecl(),
4155 Importer.Import(D->getInnerLocStart()),
4156 Loc, D->getDepth(), D->getPosition(),
4157 Name.getAsIdentifierInfo(),
4158 T, D->isParameterPack(), TInfo);
4162 ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4163 // Import the name of this declaration.
4164 DeclarationName Name = Importer.Import(D->getDeclName());
4165 if (D->getDeclName() && !Name)
4168 // Import the location of this declaration.
4169 SourceLocation Loc = Importer.Import(D->getLocation());
4171 // Import template parameters.
4172 TemplateParameterList *TemplateParams
4173 = ImportTemplateParameterList(D->getTemplateParameters());
4174 if (!TemplateParams)
4177 // FIXME: Import default argument.
4179 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
4180 Importer.getToContext().getTranslationUnitDecl(),
4181 Loc, D->getDepth(), D->getPosition(),
4182 D->isParameterPack(),
4183 Name.getAsIdentifierInfo(),
4187 Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
4188 // If this record has a definition in the translation unit we're coming from,
4189 // but this particular declaration is not that definition, import the
4190 // definition and map to that.
4191 CXXRecordDecl *Definition
4192 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
4193 if (Definition && Definition != D->getTemplatedDecl()) {
4195 = Importer.Import(Definition->getDescribedClassTemplate());
4199 return Importer.Imported(D, ImportedDef);
4202 // Import the major distinguishing characteristics of this class template.
4203 DeclContext *DC, *LexicalDC;
4204 DeclarationName Name;
4207 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4212 // We may already have a template of the same name; try to find and match it.
4213 if (!DC->isFunctionOrMethod()) {
4214 SmallVector<NamedDecl *, 4> ConflictingDecls;
4215 SmallVector<NamedDecl *, 2> FoundDecls;
4216 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
4217 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4218 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4221 Decl *Found = FoundDecls[I];
4222 if (ClassTemplateDecl *FoundTemplate
4223 = dyn_cast<ClassTemplateDecl>(Found)) {
4224 if (IsStructuralMatch(D, FoundTemplate)) {
4225 // The class templates structurally match; call it the same template.
4226 // FIXME: We may be filling in a forward declaration here. Handle
4228 Importer.Imported(D->getTemplatedDecl(),
4229 FoundTemplate->getTemplatedDecl());
4230 return Importer.Imported(D, FoundTemplate);
4234 ConflictingDecls.push_back(FoundDecls[I]);
4237 if (!ConflictingDecls.empty()) {
4238 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4239 ConflictingDecls.data(),
4240 ConflictingDecls.size());
4247 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
4249 // Create the declaration that is being templated.
4250 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4251 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
4252 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
4253 DTemplated->getTagKind(),
4254 DC, StartLoc, IdLoc,
4255 Name.getAsIdentifierInfo());
4256 D2Templated->setAccess(DTemplated->getAccess());
4257 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
4258 D2Templated->setLexicalDeclContext(LexicalDC);
4260 // Create the class template declaration itself.
4261 TemplateParameterList *TemplateParams
4262 = ImportTemplateParameterList(D->getTemplateParameters());
4263 if (!TemplateParams)
4266 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4267 Loc, Name, TemplateParams,
4269 /*PrevDecl=*/nullptr);
4270 D2Templated->setDescribedClassTemplate(D2);
4272 D2->setAccess(D->getAccess());
4273 D2->setLexicalDeclContext(LexicalDC);
4274 LexicalDC->addDeclInternal(D2);
4276 // Note the relationship between the class templates.
4277 Importer.Imported(D, D2);
4278 Importer.Imported(DTemplated, D2Templated);
4280 if (DTemplated->isCompleteDefinition() &&
4281 !D2Templated->isCompleteDefinition()) {
4282 // FIXME: Import definition!
4288 Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4289 ClassTemplateSpecializationDecl *D) {
4290 // If this record has a definition in the translation unit we're coming from,
4291 // but this particular declaration is not that definition, import the
4292 // definition and map to that.
4293 TagDecl *Definition = D->getDefinition();
4294 if (Definition && Definition != D) {
4295 Decl *ImportedDef = Importer.Import(Definition);
4299 return Importer.Imported(D, ImportedDef);
4302 ClassTemplateDecl *ClassTemplate
4303 = cast_or_null<ClassTemplateDecl>(Importer.Import(
4304 D->getSpecializedTemplate()));
4308 // Import the context of this declaration.
4309 DeclContext *DC = ClassTemplate->getDeclContext();
4313 DeclContext *LexicalDC = DC;
4314 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4315 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4320 // Import the location of this declaration.
4321 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4322 SourceLocation IdLoc = Importer.Import(D->getLocation());
4324 // Import template arguments.
4325 SmallVector<TemplateArgument, 2> TemplateArgs;
4326 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4327 D->getTemplateArgs().size(),
4331 // Try to find an existing specialization with these template arguments.
4332 void *InsertPos = nullptr;
4333 ClassTemplateSpecializationDecl *D2
4334 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
4336 // We already have a class template specialization with these template
4339 // FIXME: Check for specialization vs. instantiation errors.
4341 if (RecordDecl *FoundDef = D2->getDefinition()) {
4342 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
4343 // The record types structurally match, or the "from" translation
4344 // unit only had a forward declaration anyway; call it the same
4346 return Importer.Imported(D, FoundDef);
4350 // Create a new specialization.
4351 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4352 D->getTagKind(), DC,
4355 TemplateArgs.data(),
4356 TemplateArgs.size(),
4357 /*PrevDecl=*/nullptr);
4358 D2->setSpecializationKind(D->getSpecializationKind());
4360 // Add this specialization to the class template.
4361 ClassTemplate->AddSpecialization(D2, InsertPos);
4363 // Import the qualifier, if any.
4364 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4366 // Add the specialization to this context.
4367 D2->setLexicalDeclContext(LexicalDC);
4368 LexicalDC->addDeclInternal(D2);
4370 Importer.Imported(D, D2);
4372 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
4378 Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4379 // If this variable has a definition in the translation unit we're coming
4381 // but this particular declaration is not that definition, import the
4382 // definition and map to that.
4383 VarDecl *Definition =
4384 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4385 if (Definition && Definition != D->getTemplatedDecl()) {
4386 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4390 return Importer.Imported(D, ImportedDef);
4393 // Import the major distinguishing characteristics of this variable template.
4394 DeclContext *DC, *LexicalDC;
4395 DeclarationName Name;
4398 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4403 // We may already have a template of the same name; try to find and match it.
4404 assert(!DC->isFunctionOrMethod() &&
4405 "Variable templates cannot be declared at function scope");
4406 SmallVector<NamedDecl *, 4> ConflictingDecls;
4407 SmallVector<NamedDecl *, 2> FoundDecls;
4408 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
4409 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4410 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4413 Decl *Found = FoundDecls[I];
4414 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
4415 if (IsStructuralMatch(D, FoundTemplate)) {
4416 // The variable templates structurally match; call it the same template.
4417 Importer.Imported(D->getTemplatedDecl(),
4418 FoundTemplate->getTemplatedDecl());
4419 return Importer.Imported(D, FoundTemplate);
4423 ConflictingDecls.push_back(FoundDecls[I]);
4426 if (!ConflictingDecls.empty()) {
4427 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4428 ConflictingDecls.data(),
4429 ConflictingDecls.size());
4435 VarDecl *DTemplated = D->getTemplatedDecl();
4438 QualType T = Importer.Import(DTemplated->getType());
4442 // Create the declaration that is being templated.
4443 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4444 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
4445 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo());
4446 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc,
4447 IdLoc, Name.getAsIdentifierInfo(), T,
4448 TInfo, DTemplated->getStorageClass());
4449 D2Templated->setAccess(DTemplated->getAccess());
4450 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
4451 D2Templated->setLexicalDeclContext(LexicalDC);
4453 // Importer.Imported(DTemplated, D2Templated);
4454 // LexicalDC->addDeclInternal(D2Templated);
4456 // Merge the initializer.
4457 if (ImportDefinition(DTemplated, D2Templated))
4460 // Create the variable template declaration itself.
4461 TemplateParameterList *TemplateParams =
4462 ImportTemplateParameterList(D->getTemplateParameters());
4463 if (!TemplateParams)
4466 VarTemplateDecl *D2 = VarTemplateDecl::Create(
4467 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated);
4468 D2Templated->setDescribedVarTemplate(D2);
4470 D2->setAccess(D->getAccess());
4471 D2->setLexicalDeclContext(LexicalDC);
4472 LexicalDC->addDeclInternal(D2);
4474 // Note the relationship between the variable templates.
4475 Importer.Imported(D, D2);
4476 Importer.Imported(DTemplated, D2Templated);
4478 if (DTemplated->isThisDeclarationADefinition() &&
4479 !D2Templated->isThisDeclarationADefinition()) {
4480 // FIXME: Import definition!
4486 Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4487 VarTemplateSpecializationDecl *D) {
4488 // If this record has a definition in the translation unit we're coming from,
4489 // but this particular declaration is not that definition, import the
4490 // definition and map to that.
4491 VarDecl *Definition = D->getDefinition();
4492 if (Definition && Definition != D) {
4493 Decl *ImportedDef = Importer.Import(Definition);
4497 return Importer.Imported(D, ImportedDef);
4500 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
4501 Importer.Import(D->getSpecializedTemplate()));
4505 // Import the context of this declaration.
4506 DeclContext *DC = VarTemplate->getDeclContext();
4510 DeclContext *LexicalDC = DC;
4511 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4512 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4517 // Import the location of this declaration.
4518 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4519 SourceLocation IdLoc = Importer.Import(D->getLocation());
4521 // Import template arguments.
4522 SmallVector<TemplateArgument, 2> TemplateArgs;
4523 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4524 D->getTemplateArgs().size(), TemplateArgs))
4527 // Try to find an existing specialization with these template arguments.
4528 void *InsertPos = nullptr;
4529 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
4530 TemplateArgs, InsertPos);
4532 // We already have a variable template specialization with these template
4535 // FIXME: Check for specialization vs. instantiation errors.
4537 if (VarDecl *FoundDef = D2->getDefinition()) {
4538 if (!D->isThisDeclarationADefinition() ||
4539 IsStructuralMatch(D, FoundDef)) {
4540 // The record types structurally match, or the "from" translation
4541 // unit only had a forward declaration anyway; call it the same
4543 return Importer.Imported(D, FoundDef);
4549 QualType T = Importer.Import(D->getType());
4552 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4554 // Create a new specialization.
4555 D2 = VarTemplateSpecializationDecl::Create(
4556 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
4557 D->getStorageClass(), TemplateArgs.data(), TemplateArgs.size());
4558 D2->setSpecializationKind(D->getSpecializationKind());
4559 D2->setTemplateArgsInfo(D->getTemplateArgsInfo());
4561 // Add this specialization to the class template.
4562 VarTemplate->AddSpecialization(D2, InsertPos);
4564 // Import the qualifier, if any.
4565 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4567 // Add the specialization to this context.
4568 D2->setLexicalDeclContext(LexicalDC);
4569 LexicalDC->addDeclInternal(D2);
4571 Importer.Imported(D, D2);
4573 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2))
4579 //----------------------------------------------------------------------------
4580 // Import Statements
4581 //----------------------------------------------------------------------------
4583 DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4585 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4586 size_t NumDecls = DG.end() - DG.begin();
4587 SmallVector<Decl *, 1> ToDecls(NumDecls);
4588 auto &_Importer = this->Importer;
4589 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4590 [&_Importer](Decl *D) -> Decl * {
4591 return _Importer.Import(D);
4593 return DeclGroupRef::Create(Importer.getToContext(),
4598 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4599 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4600 << S->getStmtClassName();
4604 Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4605 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
4606 for (Decl *ToD : ToDG) {
4610 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4611 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4612 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4615 Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4616 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4617 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4618 S->hasLeadingEmptyMacro());
4621 Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
4622 SmallVector<Stmt *, 4> ToStmts(S->size());
4623 auto &_Importer = this->Importer;
4624 std::transform(S->body_begin(), S->body_end(), ToStmts.begin(),
4625 [&_Importer](Stmt *CS) -> Stmt * {
4626 return _Importer.Import(CS);
4628 for (Stmt *ToS : ToStmts) {
4632 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4633 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
4634 return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(),
4636 ToLBraceLoc, ToRBraceLoc);
4639 Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4640 Expr *ToLHS = Importer.Import(S->getLHS());
4643 Expr *ToRHS = Importer.Import(S->getRHS());
4644 if (!ToRHS && S->getRHS())
4646 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4647 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4648 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4649 return new (Importer.getToContext()) CaseStmt(ToLHS, ToRHS,
4650 ToCaseLoc, ToEllipsisLoc,
4654 Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4655 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4656 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4657 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4658 if (!ToSubStmt && S->getSubStmt())
4660 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4664 Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4665 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
4666 LabelDecl *ToLabelDecl =
4667 cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
4668 if (!ToLabelDecl && S->getDecl())
4670 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4671 if (!ToSubStmt && S->getSubStmt())
4673 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4677 Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4678 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4679 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4680 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
4681 ASTContext &_ToContext = Importer.getToContext();
4682 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
4683 [&_ToContext](const Attr *A) -> const Attr * {
4684 return A->clone(_ToContext);
4686 for (const Attr *ToA : ToAttrs) {
4690 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4691 if (!ToSubStmt && S->getSubStmt())
4693 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4694 ToAttrs, ToSubStmt);
4697 Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4698 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
4699 VarDecl *ToConditionVariable = nullptr;
4700 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4701 ToConditionVariable =
4702 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4703 if (!ToConditionVariable)
4706 Expr *ToCondition = Importer.Import(S->getCond());
4707 if (!ToCondition && S->getCond())
4709 Stmt *ToThenStmt = Importer.Import(S->getThen());
4710 if (!ToThenStmt && S->getThen())
4712 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4713 Stmt *ToElseStmt = Importer.Import(S->getElse());
4714 if (!ToElseStmt && S->getElse())
4716 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
4717 ToIfLoc, ToConditionVariable,
4718 ToCondition, ToThenStmt,
4719 ToElseLoc, ToElseStmt);
4722 Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
4723 VarDecl *ToConditionVariable = nullptr;
4724 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4725 ToConditionVariable =
4726 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4727 if (!ToConditionVariable)
4730 Expr *ToCondition = Importer.Import(S->getCond());
4731 if (!ToCondition && S->getCond())
4733 SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt(
4734 Importer.getToContext(), ToConditionVariable,
4736 Stmt *ToBody = Importer.Import(S->getBody());
4737 if (!ToBody && S->getBody())
4739 ToStmt->setBody(ToBody);
4740 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4741 // Now we have to re-chain the cases.
4742 SwitchCase *LastChainedSwitchCase = nullptr;
4743 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4744 SC = SC->getNextSwitchCase()) {
4745 SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
4748 if (LastChainedSwitchCase)
4749 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4751 ToStmt->setSwitchCaseList(ToSC);
4752 LastChainedSwitchCase = ToSC;
4757 Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
4758 VarDecl *ToConditionVariable = nullptr;
4759 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4760 ToConditionVariable =
4761 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4762 if (!ToConditionVariable)
4765 Expr *ToCondition = Importer.Import(S->getCond());
4766 if (!ToCondition && S->getCond())
4768 Stmt *ToBody = Importer.Import(S->getBody());
4769 if (!ToBody && S->getBody())
4771 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4772 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
4773 ToConditionVariable,
4774 ToCondition, ToBody,
4778 Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
4779 Stmt *ToBody = Importer.Import(S->getBody());
4780 if (!ToBody && S->getBody())
4782 Expr *ToCondition = Importer.Import(S->getCond());
4783 if (!ToCondition && S->getCond())
4785 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
4786 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4787 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4788 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
4789 ToDoLoc, ToWhileLoc,
4793 Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
4794 Stmt *ToInit = Importer.Import(S->getInit());
4795 if (!ToInit && S->getInit())
4797 Expr *ToCondition = Importer.Import(S->getCond());
4798 if (!ToCondition && S->getCond())
4800 VarDecl *ToConditionVariable = nullptr;
4801 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4802 ToConditionVariable =
4803 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4804 if (!ToConditionVariable)
4807 Expr *ToInc = Importer.Import(S->getInc());
4808 if (!ToInc && S->getInc())
4810 Stmt *ToBody = Importer.Import(S->getBody());
4811 if (!ToBody && S->getBody())
4813 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4814 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
4815 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4816 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
4817 ToInit, ToCondition,
4818 ToConditionVariable,
4820 ToForLoc, ToLParenLoc,
4824 Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
4825 LabelDecl *ToLabel = nullptr;
4826 if (LabelDecl *FromLabel = S->getLabel()) {
4827 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
4831 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4832 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
4833 return new (Importer.getToContext()) GotoStmt(ToLabel,
4834 ToGotoLoc, ToLabelLoc);
4837 Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
4838 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4839 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
4840 Expr *ToTarget = Importer.Import(S->getTarget());
4841 if (!ToTarget && S->getTarget())
4843 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
4847 Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
4848 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
4849 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
4852 Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
4853 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
4854 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
4857 Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
4858 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
4859 Expr *ToRetExpr = Importer.Import(S->getRetValue());
4860 if (!ToRetExpr && S->getRetValue())
4862 VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate());
4863 VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
4864 if (!ToNRVOCandidate && NRVOCandidate)
4866 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
4870 Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
4871 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
4872 VarDecl *ToExceptionDecl = nullptr;
4873 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
4875 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4876 if (!ToExceptionDecl)
4879 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
4880 if (!ToHandlerBlock && S->getHandlerBlock())
4882 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
4887 Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
4888 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
4889 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
4890 if (!ToTryBlock && S->getTryBlock())
4892 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
4893 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
4894 CXXCatchStmt *FromHandler = S->getHandler(HI);
4895 if (Stmt *ToHandler = Importer.Import(FromHandler))
4896 ToHandlers[HI] = ToHandler;
4900 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
4904 Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
4906 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
4907 if (!ToRange && S->getRangeStmt())
4909 DeclStmt *ToBeginEnd =
4910 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginEndStmt()));
4911 if (!ToBeginEnd && S->getBeginEndStmt())
4913 Expr *ToCond = Importer.Import(S->getCond());
4914 if (!ToCond && S->getCond())
4916 Expr *ToInc = Importer.Import(S->getInc());
4917 if (!ToInc && S->getInc())
4919 DeclStmt *ToLoopVar =
4920 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
4921 if (!ToLoopVar && S->getLoopVarStmt())
4923 Stmt *ToBody = Importer.Import(S->getBody());
4924 if (!ToBody && S->getBody())
4926 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4927 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
4928 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4929 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4930 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBeginEnd,
4933 ToForLoc, ToCoawaitLoc,
4934 ToColonLoc, ToRParenLoc);
4937 Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
4938 Stmt *ToElem = Importer.Import(S->getElement());
4939 if (!ToElem && S->getElement())
4941 Expr *ToCollect = Importer.Import(S->getCollection());
4942 if (!ToCollect && S->getCollection())
4944 Stmt *ToBody = Importer.Import(S->getBody());
4945 if (!ToBody && S->getBody())
4947 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4948 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4949 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
4955 Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
4956 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
4957 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4958 VarDecl *ToExceptionDecl = nullptr;
4959 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
4961 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4962 if (!ToExceptionDecl)
4965 Stmt *ToBody = Importer.Import(S->getCatchBody());
4966 if (!ToBody && S->getCatchBody())
4968 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
4974 Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
4975 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
4976 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
4977 if (!ToAtFinallyStmt && S->getFinallyBody())
4979 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
4983 Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
4984 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
4985 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
4986 if (!ToAtTryStmt && S->getTryBody())
4988 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
4989 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
4990 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
4991 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
4992 ToCatchStmts[CI] = ToCatchStmt;
4996 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
4997 if (!ToAtFinallyStmt && S->getFinallyStmt())
4999 return ObjCAtTryStmt::Create(Importer.getToContext(),
5000 ToAtTryLoc, ToAtTryStmt,
5001 ToCatchStmts.begin(), ToCatchStmts.size(),
5005 Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
5006 (ObjCAtSynchronizedStmt *S) {
5007 SourceLocation ToAtSynchronizedLoc =
5008 Importer.Import(S->getAtSynchronizedLoc());
5009 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
5010 if (!ToSynchExpr && S->getSynchExpr())
5012 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
5013 if (!ToSynchBody && S->getSynchBody())
5015 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5016 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5019 Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5020 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5021 Expr *ToThrow = Importer.Import(S->getThrowExpr());
5022 if (!ToThrow && S->getThrowExpr())
5024 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5027 Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5028 (ObjCAutoreleasePoolStmt *S) {
5029 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5030 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5031 if (!ToSubStmt && S->getSubStmt())
5033 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5037 //----------------------------------------------------------------------------
5038 // Import Expressions
5039 //----------------------------------------------------------------------------
5040 Expr *ASTNodeImporter::VisitExpr(Expr *E) {
5041 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5042 << E->getStmtClassName();
5046 Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
5047 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
5051 NamedDecl *FoundD = nullptr;
5052 if (E->getDecl() != E->getFoundDecl()) {
5053 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5058 QualType T = Importer.Import(E->getType());
5062 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5063 Importer.Import(E->getQualifierLoc()),
5064 Importer.Import(E->getTemplateKeywordLoc()),
5066 E->refersToEnclosingVariableOrCapture(),
5067 Importer.Import(E->getLocation()),
5068 T, E->getValueKind(),
5070 /*FIXME:TemplateArgs=*/nullptr);
5071 if (E->hadMultipleCandidates())
5072 DRE->setHadMultipleCandidates(true);
5076 Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5077 QualType T = Importer.Import(E->getType());
5081 return IntegerLiteral::Create(Importer.getToContext(),
5083 Importer.Import(E->getLocation()));
5086 Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5087 QualType T = Importer.Import(E->getType());
5091 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5093 Importer.Import(E->getLocation()));
5096 Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5097 Expr *SubExpr = Importer.Import(E->getSubExpr());
5101 return new (Importer.getToContext())
5102 ParenExpr(Importer.Import(E->getLParen()),
5103 Importer.Import(E->getRParen()),
5107 Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5108 QualType T = Importer.Import(E->getType());
5112 Expr *SubExpr = Importer.Import(E->getSubExpr());
5116 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
5117 T, E->getValueKind(),
5119 Importer.Import(E->getOperatorLoc()));
5122 Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
5123 UnaryExprOrTypeTraitExpr *E) {
5124 QualType ResultType = Importer.Import(E->getType());
5126 if (E->isArgumentType()) {
5127 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5131 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5133 Importer.Import(E->getOperatorLoc()),
5134 Importer.Import(E->getRParenLoc()));
5137 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5141 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5142 SubExpr, ResultType,
5143 Importer.Import(E->getOperatorLoc()),
5144 Importer.Import(E->getRParenLoc()));
5147 Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5148 QualType T = Importer.Import(E->getType());
5152 Expr *LHS = Importer.Import(E->getLHS());
5156 Expr *RHS = Importer.Import(E->getRHS());
5160 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
5161 T, E->getValueKind(),
5163 Importer.Import(E->getOperatorLoc()),
5164 E->isFPContractable());
5167 Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5168 QualType T = Importer.Import(E->getType());
5172 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5173 if (CompLHSType.isNull())
5176 QualType CompResultType = Importer.Import(E->getComputationResultType());
5177 if (CompResultType.isNull())
5180 Expr *LHS = Importer.Import(E->getLHS());
5184 Expr *RHS = Importer.Import(E->getRHS());
5188 return new (Importer.getToContext())
5189 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
5190 T, E->getValueKind(),
5192 CompLHSType, CompResultType,
5193 Importer.Import(E->getOperatorLoc()),
5194 E->isFPContractable());
5197 static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
5198 if (E->path_empty()) return false;
5200 // TODO: import cast paths
5204 Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5205 QualType T = Importer.Import(E->getType());
5209 Expr *SubExpr = Importer.Import(E->getSubExpr());
5213 CXXCastPath BasePath;
5214 if (ImportCastPath(E, BasePath))
5217 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
5218 SubExpr, &BasePath, E->getValueKind());
5221 Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
5222 QualType T = Importer.Import(E->getType());
5226 Expr *SubExpr = Importer.Import(E->getSubExpr());
5230 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5231 if (!TInfo && E->getTypeInfoAsWritten())
5234 CXXCastPath BasePath;
5235 if (ImportCastPath(E, BasePath))
5238 return CStyleCastExpr::Create(Importer.getToContext(), T,
5239 E->getValueKind(), E->getCastKind(),
5240 SubExpr, &BasePath, TInfo,
5241 Importer.Import(E->getLParenLoc()),
5242 Importer.Import(E->getRParenLoc()));
5245 Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
5246 QualType T = Importer.Import(E->getType());
5250 CXXConstructorDecl *ToCCD =
5251 dyn_cast<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
5252 if (!ToCCD && E->getConstructor())
5255 size_t NumArgs = E->getNumArgs();
5256 SmallVector<Expr *, 1> ToArgs(NumArgs);
5257 ASTImporter &_Importer = Importer;
5258 std::transform(E->arg_begin(), E->arg_end(), ToArgs.begin(),
5259 [&_Importer](Expr *AE) -> Expr * {
5260 return _Importer.Import(AE);
5262 for (Expr *ToA : ToArgs) {
5267 return CXXConstructExpr::Create(Importer.getToContext(), T,
5268 Importer.Import(E->getLocation()),
5269 ToCCD, E->isElidable(),
5270 ToArgs, E->hadMultipleCandidates(),
5271 E->isListInitialization(),
5272 E->isStdInitListInitialization(),
5273 E->requiresZeroInitialization(),
5274 E->getConstructionKind(),
5275 Importer.Import(E->getParenOrBraceRange()));
5278 Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
5279 QualType T = Importer.Import(E->getType());
5283 Expr *ToBase = Importer.Import(E->getBase());
5284 if (!ToBase && E->getBase())
5287 ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
5288 if (!ToMember && E->getMemberDecl())
5291 DeclAccessPair ToFoundDecl = DeclAccessPair::make(
5292 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
5293 E->getFoundDecl().getAccess());
5295 DeclarationNameInfo ToMemberNameInfo(
5296 Importer.Import(E->getMemberNameInfo().getName()),
5297 Importer.Import(E->getMemberNameInfo().getLoc()));
5299 if (E->hasExplicitTemplateArgs()) {
5300 return nullptr; // FIXME: handle template arguments
5303 return MemberExpr::Create(Importer.getToContext(), ToBase,
5305 Importer.Import(E->getOperatorLoc()),
5306 Importer.Import(E->getQualifierLoc()),
5307 Importer.Import(E->getTemplateKeywordLoc()),
5308 ToMember, ToFoundDecl, ToMemberNameInfo,
5309 nullptr, T, E->getValueKind(),
5310 E->getObjectKind());
5313 Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
5314 QualType T = Importer.Import(E->getType());
5318 Expr *ToCallee = Importer.Import(E->getCallee());
5319 if (!ToCallee && E->getCallee())
5322 unsigned NumArgs = E->getNumArgs();
5324 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
5326 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) {
5327 Expr *FromArg = E->getArg(ai);
5328 Expr *ToArg = Importer.Import(FromArg);
5334 Expr **ToArgs_Copied = new (Importer.getToContext())
5337 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
5338 ToArgs_Copied[ai] = ToArgs[ai];
5340 return new (Importer.getToContext())
5341 CallExpr(Importer.getToContext(), ToCallee,
5342 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
5343 Importer.Import(E->getRParenLoc()));
5346 ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
5347 ASTContext &FromContext, FileManager &FromFileManager,
5349 : ToContext(ToContext), FromContext(FromContext),
5350 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
5351 Minimal(MinimalImport), LastDiagFromFrom(false)
5353 ImportedDecls[FromContext.getTranslationUnitDecl()]
5354 = ToContext.getTranslationUnitDecl();
5357 ASTImporter::~ASTImporter() { }
5359 QualType ASTImporter::Import(QualType FromT) {
5363 const Type *fromTy = FromT.getTypePtr();
5365 // Check whether we've already imported this type.
5366 llvm::DenseMap<const Type *, const Type *>::iterator Pos
5367 = ImportedTypes.find(fromTy);
5368 if (Pos != ImportedTypes.end())
5369 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
5372 ASTNodeImporter Importer(*this);
5373 QualType ToT = Importer.Visit(fromTy);
5377 // Record the imported type.
5378 ImportedTypes[fromTy] = ToT.getTypePtr();
5380 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
5383 TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
5387 // FIXME: For now we just create a "trivial" type source info based
5388 // on the type and a single location. Implement a real version of this.
5389 QualType T = Import(FromTSI->getType());
5393 return ToContext.getTrivialTypeSourceInfo(T,
5394 Import(FromTSI->getTypeLoc().getLocStart()));
5397 Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
5398 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
5399 if (Pos != ImportedDecls.end()) {
5400 Decl *ToD = Pos->second;
5401 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
5408 Decl *ASTImporter::Import(Decl *FromD) {
5412 ASTNodeImporter Importer(*this);
5414 // Check whether we've already imported this declaration.
5415 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
5416 if (Pos != ImportedDecls.end()) {
5417 Decl *ToD = Pos->second;
5418 Importer.ImportDefinitionIfNeeded(FromD, ToD);
5423 Decl *ToD = Importer.Visit(FromD);
5427 // Record the imported declaration.
5428 ImportedDecls[FromD] = ToD;
5430 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
5431 // Keep track of anonymous tags that have an associated typedef.
5432 if (FromTag->getTypedefNameForAnonDecl())
5433 AnonTagsWithPendingTypedefs.push_back(FromTag);
5434 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
5435 // When we've finished transforming a typedef, see whether it was the
5436 // typedef for an anonymous tag.
5437 for (SmallVectorImpl<TagDecl *>::iterator
5438 FromTag = AnonTagsWithPendingTypedefs.begin(),
5439 FromTagEnd = AnonTagsWithPendingTypedefs.end();
5440 FromTag != FromTagEnd; ++FromTag) {
5441 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
5442 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
5443 // We found the typedef for an anonymous tag; link them.
5444 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
5445 AnonTagsWithPendingTypedefs.erase(FromTag);
5455 DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
5459 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
5463 // When we're using a record/enum/Objective-C class/protocol as a context, we
5464 // need it to have a definition.
5465 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
5466 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
5467 if (ToRecord->isCompleteDefinition()) {
5469 } else if (FromRecord->isCompleteDefinition()) {
5470 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
5471 ASTNodeImporter::IDK_Basic);
5473 CompleteDecl(ToRecord);
5475 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
5476 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
5477 if (ToEnum->isCompleteDefinition()) {
5479 } else if (FromEnum->isCompleteDefinition()) {
5480 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
5481 ASTNodeImporter::IDK_Basic);
5483 CompleteDecl(ToEnum);
5485 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
5486 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
5487 if (ToClass->getDefinition()) {
5489 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
5490 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
5491 ASTNodeImporter::IDK_Basic);
5493 CompleteDecl(ToClass);
5495 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
5496 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
5497 if (ToProto->getDefinition()) {
5499 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
5500 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
5501 ASTNodeImporter::IDK_Basic);
5503 CompleteDecl(ToProto);
5510 Expr *ASTImporter::Import(Expr *FromE) {
5514 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
5517 Stmt *ASTImporter::Import(Stmt *FromS) {
5521 // Check whether we've already imported this declaration.
5522 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
5523 if (Pos != ImportedStmts.end())
5527 ASTNodeImporter Importer(*this);
5528 Stmt *ToS = Importer.Visit(FromS);
5532 // Record the imported declaration.
5533 ImportedStmts[FromS] = ToS;
5537 NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
5541 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
5543 switch (FromNNS->getKind()) {
5544 case NestedNameSpecifier::Identifier:
5545 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
5546 return NestedNameSpecifier::Create(ToContext, prefix, II);
5550 case NestedNameSpecifier::Namespace:
5551 if (NamespaceDecl *NS =
5552 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
5553 return NestedNameSpecifier::Create(ToContext, prefix, NS);
5557 case NestedNameSpecifier::NamespaceAlias:
5558 if (NamespaceAliasDecl *NSAD =
5559 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
5560 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
5564 case NestedNameSpecifier::Global:
5565 return NestedNameSpecifier::GlobalSpecifier(ToContext);
5567 case NestedNameSpecifier::Super:
5568 if (CXXRecordDecl *RD =
5569 cast<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
5570 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
5574 case NestedNameSpecifier::TypeSpec:
5575 case NestedNameSpecifier::TypeSpecWithTemplate: {
5576 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
5578 bool bTemplate = FromNNS->getKind() ==
5579 NestedNameSpecifier::TypeSpecWithTemplate;
5580 return NestedNameSpecifier::Create(ToContext, prefix,
5581 bTemplate, T.getTypePtr());
5587 llvm_unreachable("Invalid nested name specifier kind");
5590 NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
5591 // FIXME: Implement!
5592 return NestedNameSpecifierLoc();
5595 TemplateName ASTImporter::Import(TemplateName From) {
5596 switch (From.getKind()) {
5597 case TemplateName::Template:
5598 if (TemplateDecl *ToTemplate
5599 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
5600 return TemplateName(ToTemplate);
5602 return TemplateName();
5604 case TemplateName::OverloadedTemplate: {
5605 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
5606 UnresolvedSet<2> ToTemplates;
5607 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
5608 E = FromStorage->end();
5610 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
5611 ToTemplates.addDecl(To);
5613 return TemplateName();
5615 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
5619 case TemplateName::QualifiedTemplate: {
5620 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
5621 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
5623 return TemplateName();
5625 if (TemplateDecl *ToTemplate
5626 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
5627 return ToContext.getQualifiedTemplateName(Qualifier,
5628 QTN->hasTemplateKeyword(),
5631 return TemplateName();
5634 case TemplateName::DependentTemplate: {
5635 DependentTemplateName *DTN = From.getAsDependentTemplateName();
5636 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
5638 return TemplateName();
5640 if (DTN->isIdentifier()) {
5641 return ToContext.getDependentTemplateName(Qualifier,
5642 Import(DTN->getIdentifier()));
5645 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
5648 case TemplateName::SubstTemplateTemplateParm: {
5649 SubstTemplateTemplateParmStorage *subst
5650 = From.getAsSubstTemplateTemplateParm();
5651 TemplateTemplateParmDecl *param
5652 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
5654 return TemplateName();
5656 TemplateName replacement = Import(subst->getReplacement());
5657 if (replacement.isNull()) return TemplateName();
5659 return ToContext.getSubstTemplateTemplateParm(param, replacement);
5662 case TemplateName::SubstTemplateTemplateParmPack: {
5663 SubstTemplateTemplateParmPackStorage *SubstPack
5664 = From.getAsSubstTemplateTemplateParmPack();
5665 TemplateTemplateParmDecl *Param
5666 = cast_or_null<TemplateTemplateParmDecl>(
5667 Import(SubstPack->getParameterPack()));
5669 return TemplateName();
5671 ASTNodeImporter Importer(*this);
5672 TemplateArgument ArgPack
5673 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
5674 if (ArgPack.isNull())
5675 return TemplateName();
5677 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
5681 llvm_unreachable("Invalid template name kind");
5684 SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
5685 if (FromLoc.isInvalid())
5686 return SourceLocation();
5688 SourceManager &FromSM = FromContext.getSourceManager();
5690 // For now, map everything down to its spelling location, so that we
5691 // don't have to import macro expansions.
5692 // FIXME: Import macro expansions!
5693 FromLoc = FromSM.getSpellingLoc(FromLoc);
5694 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
5695 SourceManager &ToSM = ToContext.getSourceManager();
5696 FileID ToFileID = Import(Decomposed.first);
5697 if (ToFileID.isInvalid())
5698 return SourceLocation();
5699 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
5700 .getLocWithOffset(Decomposed.second);
5704 SourceRange ASTImporter::Import(SourceRange FromRange) {
5705 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
5708 FileID ASTImporter::Import(FileID FromID) {
5709 llvm::DenseMap<FileID, FileID>::iterator Pos
5710 = ImportedFileIDs.find(FromID);
5711 if (Pos != ImportedFileIDs.end())
5714 SourceManager &FromSM = FromContext.getSourceManager();
5715 SourceManager &ToSM = ToContext.getSourceManager();
5716 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
5717 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
5719 // Include location of this file.
5720 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
5722 // Map the FileID for to the "to" source manager.
5724 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
5725 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
5726 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
5728 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
5729 // than mmap the files several times.
5730 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
5733 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
5734 FromSLoc.getFile().getFileCharacteristic());
5736 // FIXME: We want to re-use the existing MemoryBuffer!
5737 const llvm::MemoryBuffer *
5738 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
5739 std::unique_ptr<llvm::MemoryBuffer> ToBuf
5740 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
5741 FromBuf->getBufferIdentifier());
5742 ToID = ToSM.createFileID(std::move(ToBuf),
5743 FromSLoc.getFile().getFileCharacteristic());
5747 ImportedFileIDs[FromID] = ToID;
5751 void ASTImporter::ImportDefinition(Decl *From) {
5752 Decl *To = Import(From);
5756 if (DeclContext *FromDC = cast<DeclContext>(From)) {
5757 ASTNodeImporter Importer(*this);
5759 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
5760 if (!ToRecord->getDefinition()) {
5761 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
5762 ASTNodeImporter::IDK_Everything);
5767 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
5768 if (!ToEnum->getDefinition()) {
5769 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
5770 ASTNodeImporter::IDK_Everything);
5775 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
5776 if (!ToIFace->getDefinition()) {
5777 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
5778 ASTNodeImporter::IDK_Everything);
5783 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
5784 if (!ToProto->getDefinition()) {
5785 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
5786 ASTNodeImporter::IDK_Everything);
5791 Importer.ImportDeclContext(FromDC, true);
5795 DeclarationName ASTImporter::Import(DeclarationName FromName) {
5797 return DeclarationName();
5799 switch (FromName.getNameKind()) {
5800 case DeclarationName::Identifier:
5801 return Import(FromName.getAsIdentifierInfo());
5803 case DeclarationName::ObjCZeroArgSelector:
5804 case DeclarationName::ObjCOneArgSelector:
5805 case DeclarationName::ObjCMultiArgSelector:
5806 return Import(FromName.getObjCSelector());
5808 case DeclarationName::CXXConstructorName: {
5809 QualType T = Import(FromName.getCXXNameType());
5811 return DeclarationName();
5813 return ToContext.DeclarationNames.getCXXConstructorName(
5814 ToContext.getCanonicalType(T));
5817 case DeclarationName::CXXDestructorName: {
5818 QualType T = Import(FromName.getCXXNameType());
5820 return DeclarationName();
5822 return ToContext.DeclarationNames.getCXXDestructorName(
5823 ToContext.getCanonicalType(T));
5826 case DeclarationName::CXXConversionFunctionName: {
5827 QualType T = Import(FromName.getCXXNameType());
5829 return DeclarationName();
5831 return ToContext.DeclarationNames.getCXXConversionFunctionName(
5832 ToContext.getCanonicalType(T));
5835 case DeclarationName::CXXOperatorName:
5836 return ToContext.DeclarationNames.getCXXOperatorName(
5837 FromName.getCXXOverloadedOperator());
5839 case DeclarationName::CXXLiteralOperatorName:
5840 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
5841 Import(FromName.getCXXLiteralIdentifier()));
5843 case DeclarationName::CXXUsingDirective:
5845 return DeclarationName::getUsingDirectiveName();
5848 llvm_unreachable("Invalid DeclarationName Kind!");
5851 IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
5855 return &ToContext.Idents.get(FromId->getName());
5858 Selector ASTImporter::Import(Selector FromSel) {
5859 if (FromSel.isNull())
5862 SmallVector<IdentifierInfo *, 4> Idents;
5863 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
5864 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
5865 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
5866 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
5869 DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
5873 unsigned NumDecls) {
5877 DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
5878 if (LastDiagFromFrom)
5879 ToContext.getDiagnostics().notePriorDiagnosticFrom(
5880 FromContext.getDiagnostics());
5881 LastDiagFromFrom = false;
5882 return ToContext.getDiagnostics().Report(Loc, DiagID);
5885 DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
5886 if (!LastDiagFromFrom)
5887 FromContext.getDiagnostics().notePriorDiagnosticFrom(
5888 ToContext.getDiagnostics());
5889 LastDiagFromFrom = true;
5890 return FromContext.getDiagnostics().Report(Loc, DiagID);
5893 void ASTImporter::CompleteDecl (Decl *D) {
5894 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
5895 if (!ID->getDefinition())
5896 ID->startDefinition();
5898 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
5899 if (!PD->getDefinition())
5900 PD->startDefinition();
5902 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
5903 if (!TD->getDefinition() && !TD->isBeingDefined()) {
5904 TD->startDefinition();
5905 TD->setCompleteDefinition(true);
5909 assert (0 && "CompleteDecl called on a Decl that can't be completed");
5913 Decl *ASTImporter::Imported(Decl *From, Decl *To) {
5914 ImportedDecls[From] = To;
5918 bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
5920 llvm::DenseMap<const Type *, const Type *>::iterator Pos
5921 = ImportedTypes.find(From.getTypePtr());
5922 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
5925 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
5927 return Ctx.IsStructurallyEquivalent(From, To);