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()))
886 /// \brief Determine structural equivalence of two fields.
887 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
888 FieldDecl *Field1, FieldDecl *Field2) {
889 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
891 // For anonymous structs/unions, match up the anonymous struct/union type
892 // declarations directly, so that we don't go off searching for anonymous
894 if (Field1->isAnonymousStructOrUnion() &&
895 Field2->isAnonymousStructOrUnion()) {
896 RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl();
897 RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl();
898 return IsStructurallyEquivalent(Context, D1, D2);
901 // Check for equivalent field names.
902 IdentifierInfo *Name1 = Field1->getIdentifier();
903 IdentifierInfo *Name2 = Field2->getIdentifier();
904 if (!::IsStructurallyEquivalent(Name1, Name2))
907 if (!IsStructurallyEquivalent(Context,
908 Field1->getType(), Field2->getType())) {
909 if (Context.Complain) {
910 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
911 << Context.C2.getTypeDeclType(Owner2);
912 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
913 << Field2->getDeclName() << Field2->getType();
914 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
915 << Field1->getDeclName() << Field1->getType();
920 if (Field1->isBitField() != Field2->isBitField()) {
921 if (Context.Complain) {
922 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
923 << Context.C2.getTypeDeclType(Owner2);
924 if (Field1->isBitField()) {
925 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
926 << Field1->getDeclName() << Field1->getType()
927 << Field1->getBitWidthValue(Context.C1);
928 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
929 << Field2->getDeclName();
931 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
932 << Field2->getDeclName() << Field2->getType()
933 << Field2->getBitWidthValue(Context.C2);
934 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
935 << Field1->getDeclName();
941 if (Field1->isBitField()) {
942 // Make sure that the bit-fields are the same length.
943 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
944 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
946 if (Bits1 != Bits2) {
947 if (Context.Complain) {
948 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
949 << Context.C2.getTypeDeclType(Owner2);
950 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
951 << Field2->getDeclName() << Field2->getType() << Bits2;
952 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
953 << Field1->getDeclName() << Field1->getType() << Bits1;
962 /// \brief Find the index of the given anonymous struct/union within its
965 /// \returns Returns the index of this anonymous struct/union in its context,
966 /// including the next assigned index (if none of them match). Returns an
967 /// empty option if the context is not a record, i.e.. if the anonymous
968 /// struct/union is at namespace or block scope.
969 static Optional<unsigned> findAnonymousStructOrUnionIndex(RecordDecl *Anon) {
970 ASTContext &Context = Anon->getASTContext();
971 QualType AnonTy = Context.getRecordType(Anon);
973 RecordDecl *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext());
978 for (const auto *D : Owner->noload_decls()) {
979 const auto *F = dyn_cast<FieldDecl>(D);
980 if (!F || !F->isAnonymousStructOrUnion())
983 if (Context.hasSameType(F->getType(), AnonTy))
992 /// \brief Determine structural equivalence of two records.
993 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
994 RecordDecl *D1, RecordDecl *D2) {
995 if (D1->isUnion() != D2->isUnion()) {
996 if (Context.Complain) {
997 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
998 << Context.C2.getTypeDeclType(D2);
999 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
1000 << D1->getDeclName() << (unsigned)D1->getTagKind();
1005 if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) {
1006 // If both anonymous structs/unions are in a record context, make sure
1007 // they occur in the same location in the context records.
1008 if (Optional<unsigned> Index1 = findAnonymousStructOrUnionIndex(D1)) {
1009 if (Optional<unsigned> Index2 = findAnonymousStructOrUnionIndex(D2)) {
1010 if (*Index1 != *Index2)
1016 // If both declarations are class template specializations, we know
1017 // the ODR applies, so check the template and template arguments.
1018 ClassTemplateSpecializationDecl *Spec1
1019 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
1020 ClassTemplateSpecializationDecl *Spec2
1021 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
1022 if (Spec1 && Spec2) {
1023 // Check that the specialized templates are the same.
1024 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
1025 Spec2->getSpecializedTemplate()))
1028 // Check that the template arguments are the same.
1029 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
1032 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
1033 if (!IsStructurallyEquivalent(Context,
1034 Spec1->getTemplateArgs().get(I),
1035 Spec2->getTemplateArgs().get(I)))
1038 // If one is a class template specialization and the other is not, these
1039 // structures are different.
1040 else if (Spec1 || Spec2)
1043 // Compare the definitions of these two records. If either or both are
1044 // incomplete, we assume that they are equivalent.
1045 D1 = D1->getDefinition();
1046 D2 = D2->getDefinition();
1050 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
1051 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
1052 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
1053 if (Context.Complain) {
1054 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1055 << Context.C2.getTypeDeclType(D2);
1056 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
1057 << D2CXX->getNumBases();
1058 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
1059 << D1CXX->getNumBases();
1064 // Check the base classes.
1065 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
1066 BaseEnd1 = D1CXX->bases_end(),
1067 Base2 = D2CXX->bases_begin();
1070 if (!IsStructurallyEquivalent(Context,
1071 Base1->getType(), Base2->getType())) {
1072 if (Context.Complain) {
1073 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1074 << Context.C2.getTypeDeclType(D2);
1075 Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
1077 << Base2->getSourceRange();
1078 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1080 << Base1->getSourceRange();
1085 // Check virtual vs. non-virtual inheritance mismatch.
1086 if (Base1->isVirtual() != Base2->isVirtual()) {
1087 if (Context.Complain) {
1088 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1089 << Context.C2.getTypeDeclType(D2);
1090 Context.Diag2(Base2->getLocStart(),
1091 diag::note_odr_virtual_base)
1092 << Base2->isVirtual() << Base2->getSourceRange();
1093 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1094 << Base1->isVirtual()
1095 << Base1->getSourceRange();
1100 } else if (D1CXX->getNumBases() > 0) {
1101 if (Context.Complain) {
1102 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1103 << Context.C2.getTypeDeclType(D2);
1104 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
1105 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1107 << Base1->getSourceRange();
1108 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
1114 // Check the fields for consistency.
1115 RecordDecl::field_iterator Field2 = D2->field_begin(),
1116 Field2End = D2->field_end();
1117 for (RecordDecl::field_iterator Field1 = D1->field_begin(),
1118 Field1End = D1->field_end();
1119 Field1 != Field1End;
1120 ++Field1, ++Field2) {
1121 if (Field2 == Field2End) {
1122 if (Context.Complain) {
1123 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1124 << Context.C2.getTypeDeclType(D2);
1125 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
1126 << Field1->getDeclName() << Field1->getType();
1127 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
1132 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
1136 if (Field2 != Field2End) {
1137 if (Context.Complain) {
1138 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1139 << Context.C2.getTypeDeclType(D2);
1140 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1141 << Field2->getDeclName() << Field2->getType();
1142 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1150 /// \brief Determine structural equivalence of two enums.
1151 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1152 EnumDecl *D1, EnumDecl *D2) {
1153 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1154 EC2End = D2->enumerator_end();
1155 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1156 EC1End = D1->enumerator_end();
1157 EC1 != EC1End; ++EC1, ++EC2) {
1158 if (EC2 == EC2End) {
1159 if (Context.Complain) {
1160 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1161 << Context.C2.getTypeDeclType(D2);
1162 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1163 << EC1->getDeclName()
1164 << EC1->getInitVal().toString(10);
1165 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1170 llvm::APSInt Val1 = EC1->getInitVal();
1171 llvm::APSInt Val2 = EC2->getInitVal();
1172 if (!llvm::APSInt::isSameValue(Val1, Val2) ||
1173 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
1174 if (Context.Complain) {
1175 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1176 << Context.C2.getTypeDeclType(D2);
1177 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1178 << EC2->getDeclName()
1179 << EC2->getInitVal().toString(10);
1180 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1181 << EC1->getDeclName()
1182 << EC1->getInitVal().toString(10);
1188 if (EC2 != EC2End) {
1189 if (Context.Complain) {
1190 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1191 << Context.C2.getTypeDeclType(D2);
1192 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1193 << EC2->getDeclName()
1194 << EC2->getInitVal().toString(10);
1195 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1203 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1204 TemplateParameterList *Params1,
1205 TemplateParameterList *Params2) {
1206 if (Params1->size() != Params2->size()) {
1207 if (Context.Complain) {
1208 Context.Diag2(Params2->getTemplateLoc(),
1209 diag::err_odr_different_num_template_parameters)
1210 << Params1->size() << Params2->size();
1211 Context.Diag1(Params1->getTemplateLoc(),
1212 diag::note_odr_template_parameter_list);
1217 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1218 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1219 if (Context.Complain) {
1220 Context.Diag2(Params2->getParam(I)->getLocation(),
1221 diag::err_odr_different_template_parameter_kind);
1222 Context.Diag1(Params1->getParam(I)->getLocation(),
1223 diag::note_odr_template_parameter_here);
1228 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1229 Params2->getParam(I))) {
1238 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1239 TemplateTypeParmDecl *D1,
1240 TemplateTypeParmDecl *D2) {
1241 if (D1->isParameterPack() != D2->isParameterPack()) {
1242 if (Context.Complain) {
1243 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1244 << D2->isParameterPack();
1245 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1246 << D1->isParameterPack();
1254 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1255 NonTypeTemplateParmDecl *D1,
1256 NonTypeTemplateParmDecl *D2) {
1257 if (D1->isParameterPack() != D2->isParameterPack()) {
1258 if (Context.Complain) {
1259 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1260 << D2->isParameterPack();
1261 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1262 << D1->isParameterPack();
1268 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1269 if (Context.Complain) {
1270 Context.Diag2(D2->getLocation(),
1271 diag::err_odr_non_type_parameter_type_inconsistent)
1272 << D2->getType() << D1->getType();
1273 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1282 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1283 TemplateTemplateParmDecl *D1,
1284 TemplateTemplateParmDecl *D2) {
1285 if (D1->isParameterPack() != D2->isParameterPack()) {
1286 if (Context.Complain) {
1287 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1288 << D2->isParameterPack();
1289 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1290 << D1->isParameterPack();
1295 // Check template parameter lists.
1296 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1297 D2->getTemplateParameters());
1300 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1301 ClassTemplateDecl *D1,
1302 ClassTemplateDecl *D2) {
1303 // Check template parameters.
1304 if (!IsStructurallyEquivalent(Context,
1305 D1->getTemplateParameters(),
1306 D2->getTemplateParameters()))
1309 // Check the templated declaration.
1310 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1311 D2->getTemplatedDecl());
1314 /// \brief Determine structural equivalence of two declarations.
1315 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1316 Decl *D1, Decl *D2) {
1317 // FIXME: Check for known structural equivalences via a callback of some sort.
1319 // Check whether we already know that these two declarations are not
1320 // structurally equivalent.
1321 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1322 D2->getCanonicalDecl())))
1325 // Determine whether we've already produced a tentative equivalence for D1.
1326 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1328 return EquivToD1 == D2->getCanonicalDecl();
1330 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1331 EquivToD1 = D2->getCanonicalDecl();
1332 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1336 bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1338 if (!::IsStructurallyEquivalent(*this, D1, D2))
1344 bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1346 if (!::IsStructurallyEquivalent(*this, T1, T2))
1352 bool StructuralEquivalenceContext::Finish() {
1353 while (!DeclsToCheck.empty()) {
1354 // Check the next declaration.
1355 Decl *D1 = DeclsToCheck.front();
1356 DeclsToCheck.pop_front();
1358 Decl *D2 = TentativeEquivalences[D1];
1359 assert(D2 && "Unrecorded tentative equivalence?");
1361 bool Equivalent = true;
1363 // FIXME: Switch on all declaration kinds. For now, we're just going to
1364 // check the obvious ones.
1365 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1366 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1367 // Check for equivalent structure names.
1368 IdentifierInfo *Name1 = Record1->getIdentifier();
1369 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1370 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
1371 IdentifierInfo *Name2 = Record2->getIdentifier();
1372 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1373 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
1374 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1375 !::IsStructurallyEquivalent(*this, Record1, Record2))
1378 // Record/non-record mismatch.
1381 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
1382 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1383 // Check for equivalent enum names.
1384 IdentifierInfo *Name1 = Enum1->getIdentifier();
1385 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1386 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
1387 IdentifierInfo *Name2 = Enum2->getIdentifier();
1388 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1389 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
1390 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1391 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1394 // Enum/non-enum mismatch
1397 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1398 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
1399 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
1400 Typedef2->getIdentifier()) ||
1401 !::IsStructurallyEquivalent(*this,
1402 Typedef1->getUnderlyingType(),
1403 Typedef2->getUnderlyingType()))
1406 // Typedef/non-typedef mismatch.
1409 } else if (ClassTemplateDecl *ClassTemplate1
1410 = dyn_cast<ClassTemplateDecl>(D1)) {
1411 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1412 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1413 ClassTemplate2->getIdentifier()) ||
1414 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1417 // Class template/non-class-template mismatch.
1420 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1421 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1422 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1428 } else if (NonTypeTemplateParmDecl *NTTP1
1429 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1430 if (NonTypeTemplateParmDecl *NTTP2
1431 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1432 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1438 } else if (TemplateTemplateParmDecl *TTP1
1439 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1440 if (TemplateTemplateParmDecl *TTP2
1441 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1442 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1451 // Note that these two declarations are not equivalent (and we already
1453 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1454 D2->getCanonicalDecl()));
1457 // FIXME: Check other declaration kinds!
1463 //----------------------------------------------------------------------------
1465 //----------------------------------------------------------------------------
1467 QualType ASTNodeImporter::VisitType(const Type *T) {
1468 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1469 << T->getTypeClassName();
1473 QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
1474 switch (T->getKind()) {
1475 #define SHARED_SINGLETON_TYPE(Expansion)
1476 #define BUILTIN_TYPE(Id, SingletonId) \
1477 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1478 #include "clang/AST/BuiltinTypes.def"
1480 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1481 // context supports C++.
1483 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1484 // context supports ObjC.
1486 case BuiltinType::Char_U:
1487 // The context we're importing from has an unsigned 'char'. If we're
1488 // importing into a context with a signed 'char', translate to
1489 // 'unsigned char' instead.
1490 if (Importer.getToContext().getLangOpts().CharIsSigned)
1491 return Importer.getToContext().UnsignedCharTy;
1493 return Importer.getToContext().CharTy;
1495 case BuiltinType::Char_S:
1496 // The context we're importing from has an unsigned 'char'. If we're
1497 // importing into a context with a signed 'char', translate to
1498 // 'unsigned char' instead.
1499 if (!Importer.getToContext().getLangOpts().CharIsSigned)
1500 return Importer.getToContext().SignedCharTy;
1502 return Importer.getToContext().CharTy;
1504 case BuiltinType::WChar_S:
1505 case BuiltinType::WChar_U:
1506 // FIXME: If not in C++, shall we translate to the C equivalent of
1508 return Importer.getToContext().WCharTy;
1511 llvm_unreachable("Invalid BuiltinType Kind!");
1514 QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
1515 QualType ToElementType = Importer.Import(T->getElementType());
1516 if (ToElementType.isNull())
1519 return Importer.getToContext().getComplexType(ToElementType);
1522 QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
1523 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1524 if (ToPointeeType.isNull())
1527 return Importer.getToContext().getPointerType(ToPointeeType);
1530 QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
1531 // FIXME: Check for blocks support in "to" context.
1532 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1533 if (ToPointeeType.isNull())
1536 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1540 ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
1541 // FIXME: Check for C++ support in "to" context.
1542 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1543 if (ToPointeeType.isNull())
1546 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1550 ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
1551 // FIXME: Check for C++0x support in "to" context.
1552 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1553 if (ToPointeeType.isNull())
1556 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1559 QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
1560 // FIXME: Check for C++ support in "to" context.
1561 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1562 if (ToPointeeType.isNull())
1565 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1566 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1567 ClassType.getTypePtr());
1570 QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
1571 QualType ToElementType = Importer.Import(T->getElementType());
1572 if (ToElementType.isNull())
1575 return Importer.getToContext().getConstantArrayType(ToElementType,
1577 T->getSizeModifier(),
1578 T->getIndexTypeCVRQualifiers());
1582 ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
1583 QualType ToElementType = Importer.Import(T->getElementType());
1584 if (ToElementType.isNull())
1587 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1588 T->getSizeModifier(),
1589 T->getIndexTypeCVRQualifiers());
1592 QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
1593 QualType ToElementType = Importer.Import(T->getElementType());
1594 if (ToElementType.isNull())
1597 Expr *Size = Importer.Import(T->getSizeExpr());
1601 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1602 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1603 T->getSizeModifier(),
1604 T->getIndexTypeCVRQualifiers(),
1608 QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
1609 QualType ToElementType = Importer.Import(T->getElementType());
1610 if (ToElementType.isNull())
1613 return Importer.getToContext().getVectorType(ToElementType,
1614 T->getNumElements(),
1615 T->getVectorKind());
1618 QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
1619 QualType ToElementType = Importer.Import(T->getElementType());
1620 if (ToElementType.isNull())
1623 return Importer.getToContext().getExtVectorType(ToElementType,
1624 T->getNumElements());
1628 ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
1629 // FIXME: What happens if we're importing a function without a prototype
1630 // into C++? Should we make it variadic?
1631 QualType ToResultType = Importer.Import(T->getReturnType());
1632 if (ToResultType.isNull())
1635 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
1639 QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
1640 QualType ToResultType = Importer.Import(T->getReturnType());
1641 if (ToResultType.isNull())
1644 // Import argument types
1645 SmallVector<QualType, 4> ArgTypes;
1646 for (const auto &A : T->param_types()) {
1647 QualType ArgType = Importer.Import(A);
1648 if (ArgType.isNull())
1650 ArgTypes.push_back(ArgType);
1653 // Import exception types
1654 SmallVector<QualType, 4> ExceptionTypes;
1655 for (const auto &E : T->exceptions()) {
1656 QualType ExceptionType = Importer.Import(E);
1657 if (ExceptionType.isNull())
1659 ExceptionTypes.push_back(ExceptionType);
1662 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1663 FunctionProtoType::ExtProtoInfo ToEPI;
1665 ToEPI.ExtInfo = FromEPI.ExtInfo;
1666 ToEPI.Variadic = FromEPI.Variadic;
1667 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1668 ToEPI.TypeQuals = FromEPI.TypeQuals;
1669 ToEPI.RefQualifier = FromEPI.RefQualifier;
1670 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1671 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
1672 ToEPI.ExceptionSpec.NoexceptExpr =
1673 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
1674 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
1675 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
1676 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
1677 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
1679 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
1682 QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1683 QualType ToInnerType = Importer.Import(T->getInnerType());
1684 if (ToInnerType.isNull())
1687 return Importer.getToContext().getParenType(ToInnerType);
1690 QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
1691 TypedefNameDecl *ToDecl
1692 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
1696 return Importer.getToContext().getTypeDeclType(ToDecl);
1699 QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
1700 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1704 return Importer.getToContext().getTypeOfExprType(ToExpr);
1707 QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
1708 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1709 if (ToUnderlyingType.isNull())
1712 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1715 QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
1716 // FIXME: Make sure that the "to" context supports C++0x!
1717 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1721 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1722 if (UnderlyingType.isNull())
1725 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
1728 QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1729 QualType ToBaseType = Importer.Import(T->getBaseType());
1730 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1731 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1734 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1739 QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1740 // FIXME: Make sure that the "to" context supports C++11!
1741 QualType FromDeduced = T->getDeducedType();
1743 if (!FromDeduced.isNull()) {
1744 ToDeduced = Importer.Import(FromDeduced);
1745 if (ToDeduced.isNull())
1749 return Importer.getToContext().getAutoType(ToDeduced, T->isDecltypeAuto(),
1750 /*IsDependent*/false);
1753 QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
1755 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1759 return Importer.getToContext().getTagDeclType(ToDecl);
1762 QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
1764 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1768 return Importer.getToContext().getTagDeclType(ToDecl);
1771 QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
1772 QualType FromModifiedType = T->getModifiedType();
1773 QualType FromEquivalentType = T->getEquivalentType();
1774 QualType ToModifiedType;
1775 QualType ToEquivalentType;
1777 if (!FromModifiedType.isNull()) {
1778 ToModifiedType = Importer.Import(FromModifiedType);
1779 if (ToModifiedType.isNull())
1782 if (!FromEquivalentType.isNull()) {
1783 ToEquivalentType = Importer.Import(FromEquivalentType);
1784 if (ToEquivalentType.isNull())
1788 return Importer.getToContext().getAttributedType(T->getAttrKind(),
1789 ToModifiedType, ToEquivalentType);
1792 QualType ASTNodeImporter::VisitTemplateSpecializationType(
1793 const TemplateSpecializationType *T) {
1794 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1795 if (ToTemplate.isNull())
1798 SmallVector<TemplateArgument, 2> ToTemplateArgs;
1799 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1802 QualType ToCanonType;
1803 if (!QualType(T, 0).isCanonical()) {
1804 QualType FromCanonType
1805 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1806 ToCanonType =Importer.Import(FromCanonType);
1807 if (ToCanonType.isNull())
1810 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1811 ToTemplateArgs.data(),
1812 ToTemplateArgs.size(),
1816 QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
1817 NestedNameSpecifier *ToQualifier = nullptr;
1818 // Note: the qualifier in an ElaboratedType is optional.
1819 if (T->getQualifier()) {
1820 ToQualifier = Importer.Import(T->getQualifier());
1825 QualType ToNamedType = Importer.Import(T->getNamedType());
1826 if (ToNamedType.isNull())
1829 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1830 ToQualifier, ToNamedType);
1833 QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
1834 ObjCInterfaceDecl *Class
1835 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1839 return Importer.getToContext().getObjCInterfaceType(Class);
1842 QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
1843 QualType ToBaseType = Importer.Import(T->getBaseType());
1844 if (ToBaseType.isNull())
1847 SmallVector<QualType, 4> TypeArgs;
1848 for (auto TypeArg : T->getTypeArgs()) {
1849 QualType ImportedTypeArg = Importer.Import(TypeArg);
1850 if (ImportedTypeArg.isNull())
1853 TypeArgs.push_back(ImportedTypeArg);
1857 SmallVector<ObjCProtocolDecl *, 4> Protocols;
1858 for (auto *P : T->quals()) {
1859 ObjCProtocolDecl *Protocol
1860 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
1863 Protocols.push_back(Protocol);
1866 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
1871 ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
1872 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1873 if (ToPointeeType.isNull())
1876 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
1879 //----------------------------------------------------------------------------
1880 // Import Declarations
1881 //----------------------------------------------------------------------------
1882 bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1883 DeclContext *&LexicalDC,
1884 DeclarationName &Name,
1886 SourceLocation &Loc) {
1887 // Import the context of this declaration.
1888 DC = Importer.ImportContext(D->getDeclContext());
1893 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1894 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1899 // Import the name of this declaration.
1900 Name = Importer.Import(D->getDeclName());
1901 if (D->getDeclName() && !Name)
1904 // Import the location of this declaration.
1905 Loc = Importer.Import(D->getLocation());
1906 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
1910 void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1915 ToD = Importer.Import(FromD);
1920 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1921 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
1922 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
1923 ImportDefinition(FromRecord, ToRecord);
1929 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1930 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1931 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1932 ImportDefinition(FromEnum, ToEnum);
1940 ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1941 DeclarationNameInfo& To) {
1942 // NOTE: To.Name and To.Loc are already imported.
1943 // We only have to import To.LocInfo.
1944 switch (To.getName().getNameKind()) {
1945 case DeclarationName::Identifier:
1946 case DeclarationName::ObjCZeroArgSelector:
1947 case DeclarationName::ObjCOneArgSelector:
1948 case DeclarationName::ObjCMultiArgSelector:
1949 case DeclarationName::CXXUsingDirective:
1952 case DeclarationName::CXXOperatorName: {
1953 SourceRange Range = From.getCXXOperatorNameRange();
1954 To.setCXXOperatorNameRange(Importer.Import(Range));
1957 case DeclarationName::CXXLiteralOperatorName: {
1958 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1959 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1962 case DeclarationName::CXXConstructorName:
1963 case DeclarationName::CXXDestructorName:
1964 case DeclarationName::CXXConversionFunctionName: {
1965 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1966 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1970 llvm_unreachable("Unknown name kind.");
1973 void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
1974 if (Importer.isMinimalImport() && !ForceImport) {
1975 Importer.ImportContext(FromDC);
1979 for (auto *From : FromDC->decls())
1980 Importer.Import(From);
1983 bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
1984 ImportDefinitionKind Kind) {
1985 if (To->getDefinition() || To->isBeingDefined()) {
1986 if (Kind == IDK_Everything)
1987 ImportDeclContext(From, /*ForceImport=*/true);
1992 To->startDefinition();
1994 // Add base classes.
1995 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1996 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1998 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1999 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
2000 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
2001 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
2002 ToData.Aggregate = FromData.Aggregate;
2003 ToData.PlainOldData = FromData.PlainOldData;
2004 ToData.Empty = FromData.Empty;
2005 ToData.Polymorphic = FromData.Polymorphic;
2006 ToData.Abstract = FromData.Abstract;
2007 ToData.IsStandardLayout = FromData.IsStandardLayout;
2008 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
2009 ToData.HasPrivateFields = FromData.HasPrivateFields;
2010 ToData.HasProtectedFields = FromData.HasProtectedFields;
2011 ToData.HasPublicFields = FromData.HasPublicFields;
2012 ToData.HasMutableFields = FromData.HasMutableFields;
2013 ToData.HasVariantMembers = FromData.HasVariantMembers;
2014 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
2015 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
2016 ToData.HasUninitializedReferenceMember
2017 = FromData.HasUninitializedReferenceMember;
2018 ToData.NeedOverloadResolutionForMoveConstructor
2019 = FromData.NeedOverloadResolutionForMoveConstructor;
2020 ToData.NeedOverloadResolutionForMoveAssignment
2021 = FromData.NeedOverloadResolutionForMoveAssignment;
2022 ToData.NeedOverloadResolutionForDestructor
2023 = FromData.NeedOverloadResolutionForDestructor;
2024 ToData.DefaultedMoveConstructorIsDeleted
2025 = FromData.DefaultedMoveConstructorIsDeleted;
2026 ToData.DefaultedMoveAssignmentIsDeleted
2027 = FromData.DefaultedMoveAssignmentIsDeleted;
2028 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
2029 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
2030 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
2031 ToData.HasConstexprNonCopyMoveConstructor
2032 = FromData.HasConstexprNonCopyMoveConstructor;
2033 ToData.DefaultedDefaultConstructorIsConstexpr
2034 = FromData.DefaultedDefaultConstructorIsConstexpr;
2035 ToData.HasConstexprDefaultConstructor
2036 = FromData.HasConstexprDefaultConstructor;
2037 ToData.HasNonLiteralTypeFieldsOrBases
2038 = FromData.HasNonLiteralTypeFieldsOrBases;
2039 // ComputedVisibleConversions not imported.
2040 ToData.UserProvidedDefaultConstructor
2041 = FromData.UserProvidedDefaultConstructor;
2042 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
2043 ToData.ImplicitCopyConstructorHasConstParam
2044 = FromData.ImplicitCopyConstructorHasConstParam;
2045 ToData.ImplicitCopyAssignmentHasConstParam
2046 = FromData.ImplicitCopyAssignmentHasConstParam;
2047 ToData.HasDeclaredCopyConstructorWithConstParam
2048 = FromData.HasDeclaredCopyConstructorWithConstParam;
2049 ToData.HasDeclaredCopyAssignmentWithConstParam
2050 = FromData.HasDeclaredCopyAssignmentWithConstParam;
2051 ToData.IsLambda = FromData.IsLambda;
2053 SmallVector<CXXBaseSpecifier *, 4> Bases;
2054 for (const auto &Base1 : FromCXX->bases()) {
2055 QualType T = Importer.Import(Base1.getType());
2059 SourceLocation EllipsisLoc;
2060 if (Base1.isPackExpansion())
2061 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
2063 // Ensure that we have a definition for the base.
2064 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
2067 new (Importer.getToContext())
2068 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
2070 Base1.isBaseOfClass(),
2071 Base1.getAccessSpecifierAsWritten(),
2072 Importer.Import(Base1.getTypeSourceInfo()),
2076 ToCXX->setBases(Bases.data(), Bases.size());
2079 if (shouldForceImportDeclContext(Kind))
2080 ImportDeclContext(From, /*ForceImport=*/true);
2082 To->completeDefinition();
2086 bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
2087 ImportDefinitionKind Kind) {
2088 if (To->getAnyInitializer())
2091 // FIXME: Can we really import any initializer? Alternatively, we could force
2092 // ourselves to import every declaration of a variable and then only use
2094 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
2096 // FIXME: Other bits to merge?
2101 bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
2102 ImportDefinitionKind Kind) {
2103 if (To->getDefinition() || To->isBeingDefined()) {
2104 if (Kind == IDK_Everything)
2105 ImportDeclContext(From, /*ForceImport=*/true);
2109 To->startDefinition();
2111 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
2115 QualType ToPromotionType = Importer.Import(From->getPromotionType());
2116 if (ToPromotionType.isNull())
2119 if (shouldForceImportDeclContext(Kind))
2120 ImportDeclContext(From, /*ForceImport=*/true);
2122 // FIXME: we might need to merge the number of positive or negative bits
2123 // if the enumerator lists don't match.
2124 To->completeDefinition(T, ToPromotionType,
2125 From->getNumPositiveBits(),
2126 From->getNumNegativeBits());
2130 TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
2131 TemplateParameterList *Params) {
2132 SmallVector<NamedDecl *, 4> ToParams;
2133 ToParams.reserve(Params->size());
2134 for (TemplateParameterList::iterator P = Params->begin(),
2135 PEnd = Params->end();
2137 Decl *To = Importer.Import(*P);
2141 ToParams.push_back(cast<NamedDecl>(To));
2144 return TemplateParameterList::Create(Importer.getToContext(),
2145 Importer.Import(Params->getTemplateLoc()),
2146 Importer.Import(Params->getLAngleLoc()),
2147 ToParams.data(), ToParams.size(),
2148 Importer.Import(Params->getRAngleLoc()));
2152 ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
2153 switch (From.getKind()) {
2154 case TemplateArgument::Null:
2155 return TemplateArgument();
2157 case TemplateArgument::Type: {
2158 QualType ToType = Importer.Import(From.getAsType());
2159 if (ToType.isNull())
2160 return TemplateArgument();
2161 return TemplateArgument(ToType);
2164 case TemplateArgument::Integral: {
2165 QualType ToType = Importer.Import(From.getIntegralType());
2166 if (ToType.isNull())
2167 return TemplateArgument();
2168 return TemplateArgument(From, ToType);
2171 case TemplateArgument::Declaration: {
2172 ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
2173 QualType ToType = Importer.Import(From.getParamTypeForDecl());
2174 if (!To || ToType.isNull())
2175 return TemplateArgument();
2176 return TemplateArgument(To, ToType);
2179 case TemplateArgument::NullPtr: {
2180 QualType ToType = Importer.Import(From.getNullPtrType());
2181 if (ToType.isNull())
2182 return TemplateArgument();
2183 return TemplateArgument(ToType, /*isNullPtr*/true);
2186 case TemplateArgument::Template: {
2187 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
2188 if (ToTemplate.isNull())
2189 return TemplateArgument();
2191 return TemplateArgument(ToTemplate);
2194 case TemplateArgument::TemplateExpansion: {
2195 TemplateName ToTemplate
2196 = Importer.Import(From.getAsTemplateOrTemplatePattern());
2197 if (ToTemplate.isNull())
2198 return TemplateArgument();
2200 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
2203 case TemplateArgument::Expression:
2204 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2205 return TemplateArgument(ToExpr);
2206 return TemplateArgument();
2208 case TemplateArgument::Pack: {
2209 SmallVector<TemplateArgument, 2> ToPack;
2210 ToPack.reserve(From.pack_size());
2211 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2212 return TemplateArgument();
2214 TemplateArgument *ToArgs
2215 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
2216 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
2217 return TemplateArgument(ToArgs, ToPack.size());
2221 llvm_unreachable("Invalid template argument kind");
2224 bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2225 unsigned NumFromArgs,
2226 SmallVectorImpl<TemplateArgument> &ToArgs) {
2227 for (unsigned I = 0; I != NumFromArgs; ++I) {
2228 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2229 if (To.isNull() && !FromArgs[I].isNull())
2232 ToArgs.push_back(To);
2238 bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
2239 RecordDecl *ToRecord, bool Complain) {
2240 // Eliminate a potential failure point where we attempt to re-import
2241 // something we're trying to import while completing ToRecord.
2242 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
2244 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
2246 ToRecord = ToOriginRecord;
2249 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2250 ToRecord->getASTContext(),
2251 Importer.getNonEquivalentDecls(),
2253 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
2256 bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
2258 StructuralEquivalenceContext Ctx(
2259 Importer.getFromContext(), Importer.getToContext(),
2260 Importer.getNonEquivalentDecls(), false, Complain);
2261 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
2264 bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
2265 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2266 Importer.getToContext(),
2267 Importer.getNonEquivalentDecls());
2268 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
2271 bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
2272 EnumConstantDecl *ToEC)
2274 const llvm::APSInt &FromVal = FromEC->getInitVal();
2275 const llvm::APSInt &ToVal = ToEC->getInitVal();
2277 return FromVal.isSigned() == ToVal.isSigned() &&
2278 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2282 bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
2283 ClassTemplateDecl *To) {
2284 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2285 Importer.getToContext(),
2286 Importer.getNonEquivalentDecls());
2287 return Ctx.IsStructurallyEquivalent(From, To);
2290 bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2291 VarTemplateDecl *To) {
2292 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2293 Importer.getToContext(),
2294 Importer.getNonEquivalentDecls());
2295 return Ctx.IsStructurallyEquivalent(From, To);
2298 Decl *ASTNodeImporter::VisitDecl(Decl *D) {
2299 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
2300 << D->getDeclKindName();
2304 Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2305 TranslationUnitDecl *ToD =
2306 Importer.getToContext().getTranslationUnitDecl();
2308 Importer.Imported(D, ToD);
2313 Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2314 // Import the major distinguishing characteristics of this namespace.
2315 DeclContext *DC, *LexicalDC;
2316 DeclarationName Name;
2319 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2324 NamespaceDecl *MergeWithNamespace = nullptr;
2326 // This is an anonymous namespace. Adopt an existing anonymous
2327 // namespace if we can.
2328 // FIXME: Not testable.
2329 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2330 MergeWithNamespace = TU->getAnonymousNamespace();
2332 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2334 SmallVector<NamedDecl *, 4> ConflictingDecls;
2335 SmallVector<NamedDecl *, 2> FoundDecls;
2336 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2337 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2338 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
2341 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
2342 MergeWithNamespace = FoundNS;
2343 ConflictingDecls.clear();
2347 ConflictingDecls.push_back(FoundDecls[I]);
2350 if (!ConflictingDecls.empty()) {
2351 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
2352 ConflictingDecls.data(),
2353 ConflictingDecls.size());
2357 // Create the "to" namespace, if needed.
2358 NamespaceDecl *ToNamespace = MergeWithNamespace;
2360 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
2362 Importer.Import(D->getLocStart()),
2363 Loc, Name.getAsIdentifierInfo(),
2364 /*PrevDecl=*/nullptr);
2365 ToNamespace->setLexicalDeclContext(LexicalDC);
2366 LexicalDC->addDeclInternal(ToNamespace);
2368 // If this is an anonymous namespace, register it as the anonymous
2369 // namespace within its context.
2371 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2372 TU->setAnonymousNamespace(ToNamespace);
2374 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2377 Importer.Imported(D, ToNamespace);
2379 ImportDeclContext(D);
2384 Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
2385 // Import the major distinguishing characteristics of this typedef.
2386 DeclContext *DC, *LexicalDC;
2387 DeclarationName Name;
2390 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2395 // If this typedef is not in block scope, determine whether we've
2396 // seen a typedef with the same name (that we can merge with) or any
2397 // other entity by that name (which name lookup could conflict with).
2398 if (!DC->isFunctionOrMethod()) {
2399 SmallVector<NamedDecl *, 4> ConflictingDecls;
2400 unsigned IDNS = Decl::IDNS_Ordinary;
2401 SmallVector<NamedDecl *, 2> FoundDecls;
2402 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2403 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2404 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
2406 if (TypedefNameDecl *FoundTypedef =
2407 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
2408 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2409 FoundTypedef->getUnderlyingType()))
2410 return Importer.Imported(D, FoundTypedef);
2413 ConflictingDecls.push_back(FoundDecls[I]);
2416 if (!ConflictingDecls.empty()) {
2417 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2418 ConflictingDecls.data(),
2419 ConflictingDecls.size());
2425 // Import the underlying type of this typedef;
2426 QualType T = Importer.Import(D->getUnderlyingType());
2430 // Create the new typedef node.
2431 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2432 SourceLocation StartL = Importer.Import(D->getLocStart());
2433 TypedefNameDecl *ToTypedef;
2435 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2437 Name.getAsIdentifierInfo(),
2440 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2442 Name.getAsIdentifierInfo(),
2445 ToTypedef->setAccess(D->getAccess());
2446 ToTypedef->setLexicalDeclContext(LexicalDC);
2447 Importer.Imported(D, ToTypedef);
2448 LexicalDC->addDeclInternal(ToTypedef);
2453 Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2454 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2457 Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2458 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2461 Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2462 // Import the major distinguishing characteristics of this enum.
2463 DeclContext *DC, *LexicalDC;
2464 DeclarationName Name;
2467 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2472 // Figure out what enum name we're looking for.
2473 unsigned IDNS = Decl::IDNS_Tag;
2474 DeclarationName SearchName = Name;
2475 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2476 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
2477 IDNS = Decl::IDNS_Ordinary;
2478 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
2479 IDNS |= Decl::IDNS_Ordinary;
2481 // We may already have an enum of the same name; try to find and match it.
2482 if (!DC->isFunctionOrMethod() && SearchName) {
2483 SmallVector<NamedDecl *, 4> ConflictingDecls;
2484 SmallVector<NamedDecl *, 2> FoundDecls;
2485 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2486 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2487 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
2490 Decl *Found = FoundDecls[I];
2491 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2492 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2493 Found = Tag->getDecl();
2496 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
2497 if (IsStructuralMatch(D, FoundEnum))
2498 return Importer.Imported(D, FoundEnum);
2501 ConflictingDecls.push_back(FoundDecls[I]);
2504 if (!ConflictingDecls.empty()) {
2505 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2506 ConflictingDecls.data(),
2507 ConflictingDecls.size());
2511 // Create the enum declaration.
2512 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2513 Importer.Import(D->getLocStart()),
2514 Loc, Name.getAsIdentifierInfo(), nullptr,
2515 D->isScoped(), D->isScopedUsingClassTag(),
2517 // Import the qualifier, if any.
2518 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
2519 D2->setAccess(D->getAccess());
2520 D2->setLexicalDeclContext(LexicalDC);
2521 Importer.Imported(D, D2);
2522 LexicalDC->addDeclInternal(D2);
2524 // Import the integer type.
2525 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2526 if (ToIntegerType.isNull())
2528 D2->setIntegerType(ToIntegerType);
2530 // Import the definition
2531 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
2537 Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2538 // If this record has a definition in the translation unit we're coming from,
2539 // but this particular declaration is not that definition, import the
2540 // definition and map to that.
2541 TagDecl *Definition = D->getDefinition();
2542 if (Definition && Definition != D) {
2543 Decl *ImportedDef = Importer.Import(Definition);
2547 return Importer.Imported(D, ImportedDef);
2550 // Import the major distinguishing characteristics of this record.
2551 DeclContext *DC, *LexicalDC;
2552 DeclarationName Name;
2555 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2560 // Figure out what structure name we're looking for.
2561 unsigned IDNS = Decl::IDNS_Tag;
2562 DeclarationName SearchName = Name;
2563 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2564 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
2565 IDNS = Decl::IDNS_Ordinary;
2566 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
2567 IDNS |= Decl::IDNS_Ordinary;
2569 // We may already have a record of the same name; try to find and match it.
2570 RecordDecl *AdoptDecl = nullptr;
2571 if (!DC->isFunctionOrMethod()) {
2572 SmallVector<NamedDecl *, 4> ConflictingDecls;
2573 SmallVector<NamedDecl *, 2> FoundDecls;
2574 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2575 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2576 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
2579 Decl *Found = FoundDecls[I];
2580 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2581 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2582 Found = Tag->getDecl();
2585 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
2586 if (D->isAnonymousStructOrUnion() &&
2587 FoundRecord->isAnonymousStructOrUnion()) {
2588 // If both anonymous structs/unions are in a record context, make sure
2589 // they occur in the same location in the context records.
2590 if (Optional<unsigned> Index1
2591 = findAnonymousStructOrUnionIndex(D)) {
2592 if (Optional<unsigned> Index2 =
2593 findAnonymousStructOrUnionIndex(FoundRecord)) {
2594 if (*Index1 != *Index2)
2600 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
2601 if ((SearchName && !D->isCompleteDefinition())
2602 || (D->isCompleteDefinition() &&
2603 D->isAnonymousStructOrUnion()
2604 == FoundDef->isAnonymousStructOrUnion() &&
2605 IsStructuralMatch(D, FoundDef))) {
2606 // The record types structurally match, or the "from" translation
2607 // unit only had a forward declaration anyway; call it the same
2609 // FIXME: For C++, we should also merge methods here.
2610 return Importer.Imported(D, FoundDef);
2612 } else if (!D->isCompleteDefinition()) {
2613 // We have a forward declaration of this type, so adopt that forward
2614 // declaration rather than building a new one.
2616 // If one or both can be completed from external storage then try one
2617 // last time to complete and compare them before doing this.
2619 if (FoundRecord->hasExternalLexicalStorage() &&
2620 !FoundRecord->isCompleteDefinition())
2621 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2622 if (D->hasExternalLexicalStorage())
2623 D->getASTContext().getExternalSource()->CompleteType(D);
2625 if (FoundRecord->isCompleteDefinition() &&
2626 D->isCompleteDefinition() &&
2627 !IsStructuralMatch(D, FoundRecord))
2630 AdoptDecl = FoundRecord;
2632 } else if (!SearchName) {
2637 ConflictingDecls.push_back(FoundDecls[I]);
2640 if (!ConflictingDecls.empty() && SearchName) {
2641 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2642 ConflictingDecls.data(),
2643 ConflictingDecls.size());
2647 // Create the record declaration.
2648 RecordDecl *D2 = AdoptDecl;
2649 SourceLocation StartLoc = Importer.Import(D->getLocStart());
2651 if (isa<CXXRecordDecl>(D)) {
2652 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
2655 Name.getAsIdentifierInfo());
2657 D2->setAccess(D->getAccess());
2659 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
2660 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
2663 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
2664 D2->setLexicalDeclContext(LexicalDC);
2665 LexicalDC->addDeclInternal(D2);
2666 if (D->isAnonymousStructOrUnion())
2667 D2->setAnonymousStructOrUnion(true);
2670 Importer.Imported(D, D2);
2672 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
2678 Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2679 // Import the major distinguishing characteristics of this enumerator.
2680 DeclContext *DC, *LexicalDC;
2681 DeclarationName Name;
2684 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2689 QualType T = Importer.Import(D->getType());
2693 // Determine whether there are any other declarations with the same name and
2694 // in the same context.
2695 if (!LexicalDC->isFunctionOrMethod()) {
2696 SmallVector<NamedDecl *, 4> ConflictingDecls;
2697 unsigned IDNS = Decl::IDNS_Ordinary;
2698 SmallVector<NamedDecl *, 2> FoundDecls;
2699 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2700 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2701 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
2704 if (EnumConstantDecl *FoundEnumConstant
2705 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
2706 if (IsStructuralMatch(D, FoundEnumConstant))
2707 return Importer.Imported(D, FoundEnumConstant);
2710 ConflictingDecls.push_back(FoundDecls[I]);
2713 if (!ConflictingDecls.empty()) {
2714 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2715 ConflictingDecls.data(),
2716 ConflictingDecls.size());
2722 Expr *Init = Importer.Import(D->getInitExpr());
2723 if (D->getInitExpr() && !Init)
2726 EnumConstantDecl *ToEnumerator
2727 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2728 Name.getAsIdentifierInfo(), T,
2729 Init, D->getInitVal());
2730 ToEnumerator->setAccess(D->getAccess());
2731 ToEnumerator->setLexicalDeclContext(LexicalDC);
2732 Importer.Imported(D, ToEnumerator);
2733 LexicalDC->addDeclInternal(ToEnumerator);
2734 return ToEnumerator;
2737 Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2738 // Import the major distinguishing characteristics of this function.
2739 DeclContext *DC, *LexicalDC;
2740 DeclarationName Name;
2743 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2748 // Try to find a function in our own ("to") context with the same name, same
2749 // type, and in the same context as the function we're importing.
2750 if (!LexicalDC->isFunctionOrMethod()) {
2751 SmallVector<NamedDecl *, 4> ConflictingDecls;
2752 unsigned IDNS = Decl::IDNS_Ordinary;
2753 SmallVector<NamedDecl *, 2> FoundDecls;
2754 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2755 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2756 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
2759 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
2760 if (FoundFunction->hasExternalFormalLinkage() &&
2761 D->hasExternalFormalLinkage()) {
2762 if (Importer.IsStructurallyEquivalent(D->getType(),
2763 FoundFunction->getType())) {
2764 // FIXME: Actually try to merge the body and other attributes.
2765 return Importer.Imported(D, FoundFunction);
2768 // FIXME: Check for overloading more carefully, e.g., by boosting
2769 // Sema::IsOverload out to the AST library.
2771 // Function overloading is okay in C++.
2772 if (Importer.getToContext().getLangOpts().CPlusPlus)
2775 // Complain about inconsistent function types.
2776 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
2777 << Name << D->getType() << FoundFunction->getType();
2778 Importer.ToDiag(FoundFunction->getLocation(),
2779 diag::note_odr_value_here)
2780 << FoundFunction->getType();
2784 ConflictingDecls.push_back(FoundDecls[I]);
2787 if (!ConflictingDecls.empty()) {
2788 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2789 ConflictingDecls.data(),
2790 ConflictingDecls.size());
2796 DeclarationNameInfo NameInfo(Name, Loc);
2797 // Import additional name location/type info.
2798 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2800 QualType FromTy = D->getType();
2801 bool usedDifferentExceptionSpec = false;
2803 if (const FunctionProtoType *
2804 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2805 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2806 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2807 // FunctionDecl that we are importing the FunctionProtoType for.
2808 // To avoid an infinite recursion when importing, create the FunctionDecl
2809 // with a simplified function type and update it afterwards.
2810 if (FromEPI.ExceptionSpec.SourceDecl ||
2811 FromEPI.ExceptionSpec.SourceTemplate ||
2812 FromEPI.ExceptionSpec.NoexceptExpr) {
2813 FunctionProtoType::ExtProtoInfo DefaultEPI;
2814 FromTy = Importer.getFromContext().getFunctionType(
2815 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
2816 usedDifferentExceptionSpec = true;
2821 QualType T = Importer.Import(FromTy);
2825 // Import the function parameters.
2826 SmallVector<ParmVarDecl *, 8> Parameters;
2827 for (auto P : D->params()) {
2828 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
2832 Parameters.push_back(ToP);
2835 // Create the imported function.
2836 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2837 FunctionDecl *ToFunction = nullptr;
2838 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
2839 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2840 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2841 cast<CXXRecordDecl>(DC),
2844 FromConstructor->isExplicit(),
2845 D->isInlineSpecified(),
2848 } else if (isa<CXXDestructorDecl>(D)) {
2849 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2850 cast<CXXRecordDecl>(DC),
2853 D->isInlineSpecified(),
2855 } else if (CXXConversionDecl *FromConversion
2856 = dyn_cast<CXXConversionDecl>(D)) {
2857 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2858 cast<CXXRecordDecl>(DC),
2861 D->isInlineSpecified(),
2862 FromConversion->isExplicit(),
2864 Importer.Import(D->getLocEnd()));
2865 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2866 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2867 cast<CXXRecordDecl>(DC),
2870 Method->getStorageClass(),
2871 Method->isInlineSpecified(),
2873 Importer.Import(D->getLocEnd()));
2875 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
2877 NameInfo, T, TInfo, D->getStorageClass(),
2878 D->isInlineSpecified(),
2879 D->hasWrittenPrototype(),
2883 // Import the qualifier, if any.
2884 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
2885 ToFunction->setAccess(D->getAccess());
2886 ToFunction->setLexicalDeclContext(LexicalDC);
2887 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2888 ToFunction->setTrivial(D->isTrivial());
2889 ToFunction->setPure(D->isPure());
2890 Importer.Imported(D, ToFunction);
2892 // Set the parameters.
2893 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
2894 Parameters[I]->setOwningFunction(ToFunction);
2895 ToFunction->addDeclInternal(Parameters[I]);
2897 ToFunction->setParams(Parameters);
2899 if (usedDifferentExceptionSpec) {
2900 // Update FunctionProtoType::ExtProtoInfo.
2901 QualType T = Importer.Import(D->getType());
2904 ToFunction->setType(T);
2907 // Import the body, if any.
2908 if (Stmt *FromBody = D->getBody()) {
2909 if (Stmt *ToBody = Importer.Import(FromBody)) {
2910 ToFunction->setBody(ToBody);
2914 // FIXME: Other bits to merge?
2916 // Add this function to the lexical context.
2917 LexicalDC->addDeclInternal(ToFunction);
2922 Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2923 return VisitFunctionDecl(D);
2926 Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2927 return VisitCXXMethodDecl(D);
2930 Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2931 return VisitCXXMethodDecl(D);
2934 Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2935 return VisitCXXMethodDecl(D);
2938 static unsigned getFieldIndex(Decl *F) {
2939 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
2944 for (const auto *D : Owner->noload_decls()) {
2948 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2955 Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2956 // Import the major distinguishing characteristics of a variable.
2957 DeclContext *DC, *LexicalDC;
2958 DeclarationName Name;
2961 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2966 // Determine whether we've already imported this field.
2967 SmallVector<NamedDecl *, 2> FoundDecls;
2968 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2969 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2970 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
2971 // For anonymous fields, match up by index.
2972 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2975 if (Importer.IsStructurallyEquivalent(D->getType(),
2976 FoundField->getType())) {
2977 Importer.Imported(D, FoundField);
2981 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2982 << Name << D->getType() << FoundField->getType();
2983 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2984 << FoundField->getType();
2990 QualType T = Importer.Import(D->getType());
2994 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2995 Expr *BitWidth = Importer.Import(D->getBitWidth());
2996 if (!BitWidth && D->getBitWidth())
2999 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
3000 Importer.Import(D->getInnerLocStart()),
3001 Loc, Name.getAsIdentifierInfo(),
3002 T, TInfo, BitWidth, D->isMutable(),
3003 D->getInClassInitStyle());
3004 ToField->setAccess(D->getAccess());
3005 ToField->setLexicalDeclContext(LexicalDC);
3006 if (ToField->hasInClassInitializer())
3007 ToField->setInClassInitializer(D->getInClassInitializer());
3008 ToField->setImplicit(D->isImplicit());
3009 Importer.Imported(D, ToField);
3010 LexicalDC->addDeclInternal(ToField);
3014 Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
3015 // Import the major distinguishing characteristics of a variable.
3016 DeclContext *DC, *LexicalDC;
3017 DeclarationName Name;
3020 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3025 // Determine whether we've already imported this field.
3026 SmallVector<NamedDecl *, 2> FoundDecls;
3027 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3028 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3029 if (IndirectFieldDecl *FoundField
3030 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
3031 // For anonymous indirect fields, match up by index.
3032 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
3035 if (Importer.IsStructurallyEquivalent(D->getType(),
3036 FoundField->getType(),
3038 Importer.Imported(D, FoundField);
3042 // If there are more anonymous fields to check, continue.
3043 if (!Name && I < N-1)
3046 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3047 << Name << D->getType() << FoundField->getType();
3048 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3049 << FoundField->getType();
3055 QualType T = Importer.Import(D->getType());
3059 NamedDecl **NamedChain =
3060 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
3063 for (auto *PI : D->chain()) {
3064 Decl *D = Importer.Import(PI);
3067 NamedChain[i++] = cast<NamedDecl>(D);
3070 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
3071 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
3072 NamedChain, D->getChainingSize());
3074 for (const auto *Attr : D->attrs())
3075 ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
3077 ToIndirectField->setAccess(D->getAccess());
3078 ToIndirectField->setLexicalDeclContext(LexicalDC);
3079 Importer.Imported(D, ToIndirectField);
3080 LexicalDC->addDeclInternal(ToIndirectField);
3081 return ToIndirectField;
3084 Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
3085 // Import the major distinguishing characteristics of an ivar.
3086 DeclContext *DC, *LexicalDC;
3087 DeclarationName Name;
3090 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3095 // Determine whether we've already imported this ivar
3096 SmallVector<NamedDecl *, 2> FoundDecls;
3097 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3098 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3099 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
3100 if (Importer.IsStructurallyEquivalent(D->getType(),
3101 FoundIvar->getType())) {
3102 Importer.Imported(D, FoundIvar);
3106 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
3107 << Name << D->getType() << FoundIvar->getType();
3108 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3109 << FoundIvar->getType();
3115 QualType T = Importer.Import(D->getType());
3119 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3120 Expr *BitWidth = Importer.Import(D->getBitWidth());
3121 if (!BitWidth && D->getBitWidth())
3124 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
3125 cast<ObjCContainerDecl>(DC),
3126 Importer.Import(D->getInnerLocStart()),
3127 Loc, Name.getAsIdentifierInfo(),
3128 T, TInfo, D->getAccessControl(),
3129 BitWidth, D->getSynthesize());
3130 ToIvar->setLexicalDeclContext(LexicalDC);
3131 Importer.Imported(D, ToIvar);
3132 LexicalDC->addDeclInternal(ToIvar);
3137 Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
3138 // Import the major distinguishing characteristics of a variable.
3139 DeclContext *DC, *LexicalDC;
3140 DeclarationName Name;
3143 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3148 // Try to find a variable in our own ("to") context with the same name and
3149 // in the same context as the variable we're importing.
3150 if (D->isFileVarDecl()) {
3151 VarDecl *MergeWithVar = nullptr;
3152 SmallVector<NamedDecl *, 4> ConflictingDecls;
3153 unsigned IDNS = Decl::IDNS_Ordinary;
3154 SmallVector<NamedDecl *, 2> FoundDecls;
3155 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3156 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3157 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
3160 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
3161 // We have found a variable that we may need to merge with. Check it.
3162 if (FoundVar->hasExternalFormalLinkage() &&
3163 D->hasExternalFormalLinkage()) {
3164 if (Importer.IsStructurallyEquivalent(D->getType(),
3165 FoundVar->getType())) {
3166 MergeWithVar = FoundVar;
3170 const ArrayType *FoundArray
3171 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3172 const ArrayType *TArray
3173 = Importer.getToContext().getAsArrayType(D->getType());
3174 if (FoundArray && TArray) {
3175 if (isa<IncompleteArrayType>(FoundArray) &&
3176 isa<ConstantArrayType>(TArray)) {
3178 QualType T = Importer.Import(D->getType());
3182 FoundVar->setType(T);
3183 MergeWithVar = FoundVar;
3185 } else if (isa<IncompleteArrayType>(TArray) &&
3186 isa<ConstantArrayType>(FoundArray)) {
3187 MergeWithVar = FoundVar;
3192 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
3193 << Name << D->getType() << FoundVar->getType();
3194 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3195 << FoundVar->getType();
3199 ConflictingDecls.push_back(FoundDecls[I]);
3203 // An equivalent variable with external linkage has been found. Link
3204 // the two declarations, then merge them.
3205 Importer.Imported(D, MergeWithVar);
3207 if (VarDecl *DDef = D->getDefinition()) {
3208 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
3209 Importer.ToDiag(ExistingDef->getLocation(),
3210 diag::err_odr_variable_multiple_def)
3212 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
3214 Expr *Init = Importer.Import(DDef->getInit());
3215 MergeWithVar->setInit(Init);
3216 if (DDef->isInitKnownICE()) {
3217 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
3218 Eval->CheckedICE = true;
3219 Eval->IsICE = DDef->isInitICE();
3224 return MergeWithVar;
3227 if (!ConflictingDecls.empty()) {
3228 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3229 ConflictingDecls.data(),
3230 ConflictingDecls.size());
3237 QualType T = Importer.Import(D->getType());
3241 // Create the imported variable.
3242 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3243 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
3244 Importer.Import(D->getInnerLocStart()),
3245 Loc, Name.getAsIdentifierInfo(),
3247 D->getStorageClass());
3248 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
3249 ToVar->setAccess(D->getAccess());
3250 ToVar->setLexicalDeclContext(LexicalDC);
3251 Importer.Imported(D, ToVar);
3252 LexicalDC->addDeclInternal(ToVar);
3254 if (!D->isFileVarDecl() &&
3258 // Merge the initializer.
3259 if (ImportDefinition(D, ToVar))
3265 Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
3266 // Parameters are created in the translation unit's context, then moved
3267 // into the function declaration's context afterward.
3268 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3270 // Import the name of this declaration.
3271 DeclarationName Name = Importer.Import(D->getDeclName());
3272 if (D->getDeclName() && !Name)
3275 // Import the location of this declaration.
3276 SourceLocation Loc = Importer.Import(D->getLocation());
3278 // Import the parameter's type.
3279 QualType T = Importer.Import(D->getType());
3283 // Create the imported parameter.
3284 ImplicitParamDecl *ToParm
3285 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
3286 Loc, Name.getAsIdentifierInfo(),
3288 return Importer.Imported(D, ToParm);
3291 Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
3292 // Parameters are created in the translation unit's context, then moved
3293 // into the function declaration's context afterward.
3294 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3296 // Import the name of this declaration.
3297 DeclarationName Name = Importer.Import(D->getDeclName());
3298 if (D->getDeclName() && !Name)
3301 // Import the location of this declaration.
3302 SourceLocation Loc = Importer.Import(D->getLocation());
3304 // Import the parameter's type.
3305 QualType T = Importer.Import(D->getType());
3309 // Create the imported parameter.
3310 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3311 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
3312 Importer.Import(D->getInnerLocStart()),
3313 Loc, Name.getAsIdentifierInfo(),
3314 T, TInfo, D->getStorageClass(),
3315 /*FIXME: Default argument*/nullptr);
3316 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
3319 ToParm->setIsUsed();
3321 return Importer.Imported(D, ToParm);
3324 Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3325 // Import the major distinguishing characteristics of a method.
3326 DeclContext *DC, *LexicalDC;
3327 DeclarationName Name;
3330 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3335 SmallVector<NamedDecl *, 2> FoundDecls;
3336 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3337 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3338 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
3339 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3342 // Check return types.
3343 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3344 FoundMethod->getReturnType())) {
3345 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
3346 << D->isInstanceMethod() << Name << D->getReturnType()
3347 << FoundMethod->getReturnType();
3348 Importer.ToDiag(FoundMethod->getLocation(),
3349 diag::note_odr_objc_method_here)
3350 << D->isInstanceMethod() << Name;
3354 // Check the number of parameters.
3355 if (D->param_size() != FoundMethod->param_size()) {
3356 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3357 << D->isInstanceMethod() << Name
3358 << D->param_size() << FoundMethod->param_size();
3359 Importer.ToDiag(FoundMethod->getLocation(),
3360 diag::note_odr_objc_method_here)
3361 << D->isInstanceMethod() << Name;
3365 // Check parameter types.
3366 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
3367 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3368 P != PEnd; ++P, ++FoundP) {
3369 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3370 (*FoundP)->getType())) {
3371 Importer.FromDiag((*P)->getLocation(),
3372 diag::err_odr_objc_method_param_type_inconsistent)
3373 << D->isInstanceMethod() << Name
3374 << (*P)->getType() << (*FoundP)->getType();
3375 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3376 << (*FoundP)->getType();
3381 // Check variadic/non-variadic.
3382 // Check the number of parameters.
3383 if (D->isVariadic() != FoundMethod->isVariadic()) {
3384 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3385 << D->isInstanceMethod() << Name;
3386 Importer.ToDiag(FoundMethod->getLocation(),
3387 diag::note_odr_objc_method_here)
3388 << D->isInstanceMethod() << Name;
3392 // FIXME: Any other bits we need to merge?
3393 return Importer.Imported(D, FoundMethod);
3397 // Import the result type.
3398 QualType ResultTy = Importer.Import(D->getReturnType());
3399 if (ResultTy.isNull())
3402 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
3404 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
3405 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3406 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3407 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3408 D->getImplementationControl(), D->hasRelatedResultType());
3410 // FIXME: When we decide to merge method definitions, we'll need to
3411 // deal with implicit parameters.
3413 // Import the parameters
3414 SmallVector<ParmVarDecl *, 5> ToParams;
3415 for (auto *FromP : D->params()) {
3416 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
3420 ToParams.push_back(ToP);
3423 // Set the parameters.
3424 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3425 ToParams[I]->setOwningFunction(ToMethod);
3426 ToMethod->addDeclInternal(ToParams[I]);
3428 SmallVector<SourceLocation, 12> SelLocs;
3429 D->getSelectorLocs(SelLocs);
3430 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
3432 ToMethod->setLexicalDeclContext(LexicalDC);
3433 Importer.Imported(D, ToMethod);
3434 LexicalDC->addDeclInternal(ToMethod);
3438 Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3439 // Import the major distinguishing characteristics of a category.
3440 DeclContext *DC, *LexicalDC;
3441 DeclarationName Name;
3444 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3449 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3453 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
3454 Importer.getToContext(), DC,
3455 Importer.Import(D->getLocation()),
3456 Name.getAsIdentifierInfo(),
3457 Importer.Import(D->getColonLoc()),
3459 Importer.Imported(D, Result);
3460 Result->setLexicalDeclContext(LexicalDC);
3464 Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3465 // Import the major distinguishing characteristics of a category.
3466 DeclContext *DC, *LexicalDC;
3467 DeclarationName Name;
3470 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3475 ObjCInterfaceDecl *ToInterface
3476 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3480 // Determine if we've already encountered this category.
3481 ObjCCategoryDecl *MergeWithCategory
3482 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3483 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3485 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
3486 Importer.Import(D->getAtStartLoc()),
3488 Importer.Import(D->getCategoryNameLoc()),
3489 Name.getAsIdentifierInfo(),
3491 ImportObjCTypeParamList(
3492 D->getTypeParamList()),
3493 Importer.Import(D->getIvarLBraceLoc()),
3494 Importer.Import(D->getIvarRBraceLoc()));
3495 ToCategory->setLexicalDeclContext(LexicalDC);
3496 LexicalDC->addDeclInternal(ToCategory);
3497 Importer.Imported(D, ToCategory);
3500 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3501 SmallVector<SourceLocation, 4> ProtocolLocs;
3502 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3503 = D->protocol_loc_begin();
3504 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3505 FromProtoEnd = D->protocol_end();
3506 FromProto != FromProtoEnd;
3507 ++FromProto, ++FromProtoLoc) {
3508 ObjCProtocolDecl *ToProto
3509 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3512 Protocols.push_back(ToProto);
3513 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3516 // FIXME: If we're merging, make sure that the protocol list is the same.
3517 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3518 ProtocolLocs.data(), Importer.getToContext());
3521 Importer.Imported(D, ToCategory);
3524 // Import all of the members of this category.
3525 ImportDeclContext(D);
3527 // If we have an implementation, import it as well.
3528 if (D->getImplementation()) {
3529 ObjCCategoryImplDecl *Impl
3530 = cast_or_null<ObjCCategoryImplDecl>(
3531 Importer.Import(D->getImplementation()));
3535 ToCategory->setImplementation(Impl);
3541 bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3542 ObjCProtocolDecl *To,
3543 ImportDefinitionKind Kind) {
3544 if (To->getDefinition()) {
3545 if (shouldForceImportDeclContext(Kind))
3546 ImportDeclContext(From);
3550 // Start the protocol definition
3551 To->startDefinition();
3554 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3555 SmallVector<SourceLocation, 4> ProtocolLocs;
3556 ObjCProtocolDecl::protocol_loc_iterator
3557 FromProtoLoc = From->protocol_loc_begin();
3558 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3559 FromProtoEnd = From->protocol_end();
3560 FromProto != FromProtoEnd;
3561 ++FromProto, ++FromProtoLoc) {
3562 ObjCProtocolDecl *ToProto
3563 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3566 Protocols.push_back(ToProto);
3567 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3570 // FIXME: If we're merging, make sure that the protocol list is the same.
3571 To->setProtocolList(Protocols.data(), Protocols.size(),
3572 ProtocolLocs.data(), Importer.getToContext());
3574 if (shouldForceImportDeclContext(Kind)) {
3575 // Import all of the members of this protocol.
3576 ImportDeclContext(From, /*ForceImport=*/true);
3581 Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
3582 // If this protocol has a definition in the translation unit we're coming
3583 // from, but this particular declaration is not that definition, import the
3584 // definition and map to that.
3585 ObjCProtocolDecl *Definition = D->getDefinition();
3586 if (Definition && Definition != D) {
3587 Decl *ImportedDef = Importer.Import(Definition);
3591 return Importer.Imported(D, ImportedDef);
3594 // Import the major distinguishing characteristics of a protocol.
3595 DeclContext *DC, *LexicalDC;
3596 DeclarationName Name;
3599 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3604 ObjCProtocolDecl *MergeWithProtocol = nullptr;
3605 SmallVector<NamedDecl *, 2> FoundDecls;
3606 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3607 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3608 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
3611 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
3615 ObjCProtocolDecl *ToProto = MergeWithProtocol;
3617 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3618 Name.getAsIdentifierInfo(), Loc,
3619 Importer.Import(D->getAtStartLoc()),
3620 /*PrevDecl=*/nullptr);
3621 ToProto->setLexicalDeclContext(LexicalDC);
3622 LexicalDC->addDeclInternal(ToProto);
3625 Importer.Imported(D, ToProto);
3627 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
3633 Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3634 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3635 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3637 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3638 SourceLocation LangLoc = Importer.Import(D->getLocation());
3640 bool HasBraces = D->hasBraces();
3642 LinkageSpecDecl *ToLinkageSpec =
3643 LinkageSpecDecl::Create(Importer.getToContext(),
3651 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3652 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3655 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3656 LexicalDC->addDeclInternal(ToLinkageSpec);
3658 Importer.Imported(D, ToLinkageSpec);
3660 return ToLinkageSpec;
3663 bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3664 ObjCInterfaceDecl *To,
3665 ImportDefinitionKind Kind) {
3666 if (To->getDefinition()) {
3667 // Check consistency of superclass.
3668 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3670 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3675 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3676 if ((bool)FromSuper != (bool)ToSuper ||
3677 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3678 Importer.ToDiag(To->getLocation(),
3679 diag::err_odr_objc_superclass_inconsistent)
3680 << To->getDeclName();
3682 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3683 << To->getSuperClass()->getDeclName();
3685 Importer.ToDiag(To->getLocation(),
3686 diag::note_odr_objc_missing_superclass);
3687 if (From->getSuperClass())
3688 Importer.FromDiag(From->getSuperClassLoc(),
3689 diag::note_odr_objc_superclass)
3690 << From->getSuperClass()->getDeclName();
3692 Importer.FromDiag(From->getLocation(),
3693 diag::note_odr_objc_missing_superclass);
3696 if (shouldForceImportDeclContext(Kind))
3697 ImportDeclContext(From);
3701 // Start the definition.
3702 To->startDefinition();
3704 // If this class has a superclass, import it.
3705 if (From->getSuperClass()) {
3706 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3710 To->setSuperClass(SuperTInfo);
3714 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3715 SmallVector<SourceLocation, 4> ProtocolLocs;
3716 ObjCInterfaceDecl::protocol_loc_iterator
3717 FromProtoLoc = From->protocol_loc_begin();
3719 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3720 FromProtoEnd = From->protocol_end();
3721 FromProto != FromProtoEnd;
3722 ++FromProto, ++FromProtoLoc) {
3723 ObjCProtocolDecl *ToProto
3724 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3727 Protocols.push_back(ToProto);
3728 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3731 // FIXME: If we're merging, make sure that the protocol list is the same.
3732 To->setProtocolList(Protocols.data(), Protocols.size(),
3733 ProtocolLocs.data(), Importer.getToContext());
3735 // Import categories. When the categories themselves are imported, they'll
3736 // hook themselves into this interface.
3737 for (auto *Cat : From->known_categories())
3738 Importer.Import(Cat);
3740 // If we have an @implementation, import it as well.
3741 if (From->getImplementation()) {
3742 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3743 Importer.Import(From->getImplementation()));
3747 To->setImplementation(Impl);
3750 if (shouldForceImportDeclContext(Kind)) {
3751 // Import all of the members of this class.
3752 ImportDeclContext(From, /*ForceImport=*/true);
3758 ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3762 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
3763 for (auto fromTypeParam : *list) {
3764 auto toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3765 Importer.Import(fromTypeParam));
3769 toTypeParams.push_back(toTypeParam);
3772 return ObjCTypeParamList::create(Importer.getToContext(),
3773 Importer.Import(list->getLAngleLoc()),
3775 Importer.Import(list->getRAngleLoc()));
3778 Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
3779 // If this class has a definition in the translation unit we're coming from,
3780 // but this particular declaration is not that definition, import the
3781 // definition and map to that.
3782 ObjCInterfaceDecl *Definition = D->getDefinition();
3783 if (Definition && Definition != D) {
3784 Decl *ImportedDef = Importer.Import(Definition);
3788 return Importer.Imported(D, ImportedDef);
3791 // Import the major distinguishing characteristics of an @interface.
3792 DeclContext *DC, *LexicalDC;
3793 DeclarationName Name;
3796 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3801 // Look for an existing interface with the same name.
3802 ObjCInterfaceDecl *MergeWithIface = nullptr;
3803 SmallVector<NamedDecl *, 2> FoundDecls;
3804 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3805 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3806 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3809 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
3813 // Create an interface declaration, if one does not already exist.
3814 ObjCInterfaceDecl *ToIface = MergeWithIface;
3816 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3817 Importer.Import(D->getAtStartLoc()),
3818 Name.getAsIdentifierInfo(),
3819 ImportObjCTypeParamList(
3820 D->getTypeParamListAsWritten()),
3821 /*PrevDecl=*/nullptr, Loc,
3822 D->isImplicitInterfaceDecl());
3823 ToIface->setLexicalDeclContext(LexicalDC);
3824 LexicalDC->addDeclInternal(ToIface);
3826 Importer.Imported(D, ToIface);
3828 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
3834 Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3835 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3836 Importer.Import(D->getCategoryDecl()));
3840 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3842 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3846 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
3847 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
3848 Importer.Import(D->getIdentifier()),
3849 Category->getClassInterface(),
3850 Importer.Import(D->getLocation()),
3851 Importer.Import(D->getAtStartLoc()),
3854 DeclContext *LexicalDC = DC;
3855 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3856 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3860 ToImpl->setLexicalDeclContext(LexicalDC);
3863 LexicalDC->addDeclInternal(ToImpl);
3864 Category->setImplementation(ToImpl);
3867 Importer.Imported(D, ToImpl);
3868 ImportDeclContext(D);
3872 Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3873 // Find the corresponding interface.
3874 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3875 Importer.Import(D->getClassInterface()));
3879 // Import the superclass, if any.
3880 ObjCInterfaceDecl *Super = nullptr;
3881 if (D->getSuperClass()) {
3882 Super = cast_or_null<ObjCInterfaceDecl>(
3883 Importer.Import(D->getSuperClass()));
3888 ObjCImplementationDecl *Impl = Iface->getImplementation();
3890 // We haven't imported an implementation yet. Create a new @implementation
3892 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3893 Importer.ImportContext(D->getDeclContext()),
3895 Importer.Import(D->getLocation()),
3896 Importer.Import(D->getAtStartLoc()),
3897 Importer.Import(D->getSuperClassLoc()),
3898 Importer.Import(D->getIvarLBraceLoc()),
3899 Importer.Import(D->getIvarRBraceLoc()));
3901 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3902 DeclContext *LexicalDC
3903 = Importer.ImportContext(D->getLexicalDeclContext());
3906 Impl->setLexicalDeclContext(LexicalDC);
3909 // Associate the implementation with the class it implements.
3910 Iface->setImplementation(Impl);
3911 Importer.Imported(D, Iface->getImplementation());
3913 Importer.Imported(D, Iface->getImplementation());
3915 // Verify that the existing @implementation has the same superclass.
3916 if ((Super && !Impl->getSuperClass()) ||
3917 (!Super && Impl->getSuperClass()) ||
3918 (Super && Impl->getSuperClass() &&
3919 !declaresSameEntity(Super->getCanonicalDecl(),
3920 Impl->getSuperClass()))) {
3921 Importer.ToDiag(Impl->getLocation(),
3922 diag::err_odr_objc_superclass_inconsistent)
3923 << Iface->getDeclName();
3924 // FIXME: It would be nice to have the location of the superclass
3926 if (Impl->getSuperClass())
3927 Importer.ToDiag(Impl->getLocation(),
3928 diag::note_odr_objc_superclass)
3929 << Impl->getSuperClass()->getDeclName();
3931 Importer.ToDiag(Impl->getLocation(),
3932 diag::note_odr_objc_missing_superclass);
3933 if (D->getSuperClass())
3934 Importer.FromDiag(D->getLocation(),
3935 diag::note_odr_objc_superclass)
3936 << D->getSuperClass()->getDeclName();
3938 Importer.FromDiag(D->getLocation(),
3939 diag::note_odr_objc_missing_superclass);
3944 // Import all of the members of this @implementation.
3945 ImportDeclContext(D);
3950 Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3951 // Import the major distinguishing characteristics of an @property.
3952 DeclContext *DC, *LexicalDC;
3953 DeclarationName Name;
3956 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3961 // Check whether we have already imported this property.
3962 SmallVector<NamedDecl *, 2> FoundDecls;
3963 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3964 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3965 if (ObjCPropertyDecl *FoundProp
3966 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
3967 // Check property types.
3968 if (!Importer.IsStructurallyEquivalent(D->getType(),
3969 FoundProp->getType())) {
3970 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3971 << Name << D->getType() << FoundProp->getType();
3972 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3973 << FoundProp->getType();
3977 // FIXME: Check property attributes, getters, setters, etc.?
3979 // Consider these properties to be equivalent.
3980 Importer.Imported(D, FoundProp);
3986 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
3990 // Create the new property.
3991 ObjCPropertyDecl *ToProperty
3992 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3993 Name.getAsIdentifierInfo(),
3994 Importer.Import(D->getAtLoc()),
3995 Importer.Import(D->getLParenLoc()),
3996 Importer.Import(D->getType()),
3998 D->getPropertyImplementation());
3999 Importer.Imported(D, ToProperty);
4000 ToProperty->setLexicalDeclContext(LexicalDC);
4001 LexicalDC->addDeclInternal(ToProperty);
4003 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
4004 ToProperty->setPropertyAttributesAsWritten(
4005 D->getPropertyAttributesAsWritten());
4006 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
4007 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
4008 ToProperty->setGetterMethodDecl(
4009 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
4010 ToProperty->setSetterMethodDecl(
4011 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
4012 ToProperty->setPropertyIvarDecl(
4013 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
4017 Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4018 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
4019 Importer.Import(D->getPropertyDecl()));
4023 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4027 // Import the lexical declaration context.
4028 DeclContext *LexicalDC = DC;
4029 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4030 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4035 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
4039 // Import the ivar (for an @synthesize).
4040 ObjCIvarDecl *Ivar = nullptr;
4041 if (D->getPropertyIvarDecl()) {
4042 Ivar = cast_or_null<ObjCIvarDecl>(
4043 Importer.Import(D->getPropertyIvarDecl()));
4048 ObjCPropertyImplDecl *ToImpl
4049 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
4051 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
4052 Importer.Import(D->getLocStart()),
4053 Importer.Import(D->getLocation()),
4055 D->getPropertyImplementation(),
4057 Importer.Import(D->getPropertyIvarDeclLoc()));
4058 ToImpl->setLexicalDeclContext(LexicalDC);
4059 Importer.Imported(D, ToImpl);
4060 LexicalDC->addDeclInternal(ToImpl);
4062 // Check that we have the same kind of property implementation (@synthesize
4064 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
4065 Importer.ToDiag(ToImpl->getLocation(),
4066 diag::err_odr_objc_property_impl_kind_inconsistent)
4067 << Property->getDeclName()
4068 << (ToImpl->getPropertyImplementation()
4069 == ObjCPropertyImplDecl::Dynamic);
4070 Importer.FromDiag(D->getLocation(),
4071 diag::note_odr_objc_property_impl_kind)
4072 << D->getPropertyDecl()->getDeclName()
4073 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
4077 // For @synthesize, check that we have the same
4078 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4079 Ivar != ToImpl->getPropertyIvarDecl()) {
4080 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
4081 diag::err_odr_objc_synthesize_ivar_inconsistent)
4082 << Property->getDeclName()
4083 << ToImpl->getPropertyIvarDecl()->getDeclName()
4084 << Ivar->getDeclName();
4085 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
4086 diag::note_odr_objc_synthesize_ivar_here)
4087 << D->getPropertyIvarDecl()->getDeclName();
4091 // Merge the existing implementation with the new implementation.
4092 Importer.Imported(D, ToImpl);
4098 Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
4099 // For template arguments, we adopt the translation unit as our declaration
4100 // context. This context will be fixed when the actual template declaration
4103 // FIXME: Import default argument.
4104 return TemplateTypeParmDecl::Create(Importer.getToContext(),
4105 Importer.getToContext().getTranslationUnitDecl(),
4106 Importer.Import(D->getLocStart()),
4107 Importer.Import(D->getLocation()),
4110 Importer.Import(D->getIdentifier()),
4111 D->wasDeclaredWithTypename(),
4112 D->isParameterPack());
4116 ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
4117 // Import the name of this declaration.
4118 DeclarationName Name = Importer.Import(D->getDeclName());
4119 if (D->getDeclName() && !Name)
4122 // Import the location of this declaration.
4123 SourceLocation Loc = Importer.Import(D->getLocation());
4125 // Import the type of this declaration.
4126 QualType T = Importer.Import(D->getType());
4130 // Import type-source information.
4131 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4132 if (D->getTypeSourceInfo() && !TInfo)
4135 // FIXME: Import default argument.
4137 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
4138 Importer.getToContext().getTranslationUnitDecl(),
4139 Importer.Import(D->getInnerLocStart()),
4140 Loc, D->getDepth(), D->getPosition(),
4141 Name.getAsIdentifierInfo(),
4142 T, D->isParameterPack(), TInfo);
4146 ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4147 // Import the name of this declaration.
4148 DeclarationName Name = Importer.Import(D->getDeclName());
4149 if (D->getDeclName() && !Name)
4152 // Import the location of this declaration.
4153 SourceLocation Loc = Importer.Import(D->getLocation());
4155 // Import template parameters.
4156 TemplateParameterList *TemplateParams
4157 = ImportTemplateParameterList(D->getTemplateParameters());
4158 if (!TemplateParams)
4161 // FIXME: Import default argument.
4163 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
4164 Importer.getToContext().getTranslationUnitDecl(),
4165 Loc, D->getDepth(), D->getPosition(),
4166 D->isParameterPack(),
4167 Name.getAsIdentifierInfo(),
4171 Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
4172 // If this record has a definition in the translation unit we're coming from,
4173 // but this particular declaration is not that definition, import the
4174 // definition and map to that.
4175 CXXRecordDecl *Definition
4176 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
4177 if (Definition && Definition != D->getTemplatedDecl()) {
4179 = Importer.Import(Definition->getDescribedClassTemplate());
4183 return Importer.Imported(D, ImportedDef);
4186 // Import the major distinguishing characteristics of this class template.
4187 DeclContext *DC, *LexicalDC;
4188 DeclarationName Name;
4191 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4196 // We may already have a template of the same name; try to find and match it.
4197 if (!DC->isFunctionOrMethod()) {
4198 SmallVector<NamedDecl *, 4> ConflictingDecls;
4199 SmallVector<NamedDecl *, 2> FoundDecls;
4200 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
4201 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4202 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4205 Decl *Found = FoundDecls[I];
4206 if (ClassTemplateDecl *FoundTemplate
4207 = dyn_cast<ClassTemplateDecl>(Found)) {
4208 if (IsStructuralMatch(D, FoundTemplate)) {
4209 // The class templates structurally match; call it the same template.
4210 // FIXME: We may be filling in a forward declaration here. Handle
4212 Importer.Imported(D->getTemplatedDecl(),
4213 FoundTemplate->getTemplatedDecl());
4214 return Importer.Imported(D, FoundTemplate);
4218 ConflictingDecls.push_back(FoundDecls[I]);
4221 if (!ConflictingDecls.empty()) {
4222 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4223 ConflictingDecls.data(),
4224 ConflictingDecls.size());
4231 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
4233 // Create the declaration that is being templated.
4234 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4235 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
4236 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
4237 DTemplated->getTagKind(),
4238 DC, StartLoc, IdLoc,
4239 Name.getAsIdentifierInfo());
4240 D2Templated->setAccess(DTemplated->getAccess());
4241 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
4242 D2Templated->setLexicalDeclContext(LexicalDC);
4244 // Create the class template declaration itself.
4245 TemplateParameterList *TemplateParams
4246 = ImportTemplateParameterList(D->getTemplateParameters());
4247 if (!TemplateParams)
4250 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4251 Loc, Name, TemplateParams,
4253 /*PrevDecl=*/nullptr);
4254 D2Templated->setDescribedClassTemplate(D2);
4256 D2->setAccess(D->getAccess());
4257 D2->setLexicalDeclContext(LexicalDC);
4258 LexicalDC->addDeclInternal(D2);
4260 // Note the relationship between the class templates.
4261 Importer.Imported(D, D2);
4262 Importer.Imported(DTemplated, D2Templated);
4264 if (DTemplated->isCompleteDefinition() &&
4265 !D2Templated->isCompleteDefinition()) {
4266 // FIXME: Import definition!
4272 Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4273 ClassTemplateSpecializationDecl *D) {
4274 // If this record has a definition in the translation unit we're coming from,
4275 // but this particular declaration is not that definition, import the
4276 // definition and map to that.
4277 TagDecl *Definition = D->getDefinition();
4278 if (Definition && Definition != D) {
4279 Decl *ImportedDef = Importer.Import(Definition);
4283 return Importer.Imported(D, ImportedDef);
4286 ClassTemplateDecl *ClassTemplate
4287 = cast_or_null<ClassTemplateDecl>(Importer.Import(
4288 D->getSpecializedTemplate()));
4292 // Import the context of this declaration.
4293 DeclContext *DC = ClassTemplate->getDeclContext();
4297 DeclContext *LexicalDC = DC;
4298 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4299 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4304 // Import the location of this declaration.
4305 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4306 SourceLocation IdLoc = Importer.Import(D->getLocation());
4308 // Import template arguments.
4309 SmallVector<TemplateArgument, 2> TemplateArgs;
4310 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4311 D->getTemplateArgs().size(),
4315 // Try to find an existing specialization with these template arguments.
4316 void *InsertPos = nullptr;
4317 ClassTemplateSpecializationDecl *D2
4318 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
4320 // We already have a class template specialization with these template
4323 // FIXME: Check for specialization vs. instantiation errors.
4325 if (RecordDecl *FoundDef = D2->getDefinition()) {
4326 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
4327 // The record types structurally match, or the "from" translation
4328 // unit only had a forward declaration anyway; call it the same
4330 return Importer.Imported(D, FoundDef);
4334 // Create a new specialization.
4335 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4336 D->getTagKind(), DC,
4339 TemplateArgs.data(),
4340 TemplateArgs.size(),
4341 /*PrevDecl=*/nullptr);
4342 D2->setSpecializationKind(D->getSpecializationKind());
4344 // Add this specialization to the class template.
4345 ClassTemplate->AddSpecialization(D2, InsertPos);
4347 // Import the qualifier, if any.
4348 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4350 // Add the specialization to this context.
4351 D2->setLexicalDeclContext(LexicalDC);
4352 LexicalDC->addDeclInternal(D2);
4354 Importer.Imported(D, D2);
4356 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
4362 Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4363 // If this variable has a definition in the translation unit we're coming
4365 // but this particular declaration is not that definition, import the
4366 // definition and map to that.
4367 VarDecl *Definition =
4368 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4369 if (Definition && Definition != D->getTemplatedDecl()) {
4370 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4374 return Importer.Imported(D, ImportedDef);
4377 // Import the major distinguishing characteristics of this variable template.
4378 DeclContext *DC, *LexicalDC;
4379 DeclarationName Name;
4382 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4387 // We may already have a template of the same name; try to find and match it.
4388 assert(!DC->isFunctionOrMethod() &&
4389 "Variable templates cannot be declared at function scope");
4390 SmallVector<NamedDecl *, 4> ConflictingDecls;
4391 SmallVector<NamedDecl *, 2> FoundDecls;
4392 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
4393 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4394 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4397 Decl *Found = FoundDecls[I];
4398 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
4399 if (IsStructuralMatch(D, FoundTemplate)) {
4400 // The variable templates structurally match; call it the same template.
4401 Importer.Imported(D->getTemplatedDecl(),
4402 FoundTemplate->getTemplatedDecl());
4403 return Importer.Imported(D, FoundTemplate);
4407 ConflictingDecls.push_back(FoundDecls[I]);
4410 if (!ConflictingDecls.empty()) {
4411 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4412 ConflictingDecls.data(),
4413 ConflictingDecls.size());
4419 VarDecl *DTemplated = D->getTemplatedDecl();
4422 QualType T = Importer.Import(DTemplated->getType());
4426 // Create the declaration that is being templated.
4427 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4428 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
4429 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo());
4430 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc,
4431 IdLoc, Name.getAsIdentifierInfo(), T,
4432 TInfo, DTemplated->getStorageClass());
4433 D2Templated->setAccess(DTemplated->getAccess());
4434 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
4435 D2Templated->setLexicalDeclContext(LexicalDC);
4437 // Importer.Imported(DTemplated, D2Templated);
4438 // LexicalDC->addDeclInternal(D2Templated);
4440 // Merge the initializer.
4441 if (ImportDefinition(DTemplated, D2Templated))
4444 // Create the variable template declaration itself.
4445 TemplateParameterList *TemplateParams =
4446 ImportTemplateParameterList(D->getTemplateParameters());
4447 if (!TemplateParams)
4450 VarTemplateDecl *D2 = VarTemplateDecl::Create(
4451 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated);
4452 D2Templated->setDescribedVarTemplate(D2);
4454 D2->setAccess(D->getAccess());
4455 D2->setLexicalDeclContext(LexicalDC);
4456 LexicalDC->addDeclInternal(D2);
4458 // Note the relationship between the variable templates.
4459 Importer.Imported(D, D2);
4460 Importer.Imported(DTemplated, D2Templated);
4462 if (DTemplated->isThisDeclarationADefinition() &&
4463 !D2Templated->isThisDeclarationADefinition()) {
4464 // FIXME: Import definition!
4470 Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4471 VarTemplateSpecializationDecl *D) {
4472 // If this record has a definition in the translation unit we're coming from,
4473 // but this particular declaration is not that definition, import the
4474 // definition and map to that.
4475 VarDecl *Definition = D->getDefinition();
4476 if (Definition && Definition != D) {
4477 Decl *ImportedDef = Importer.Import(Definition);
4481 return Importer.Imported(D, ImportedDef);
4484 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
4485 Importer.Import(D->getSpecializedTemplate()));
4489 // Import the context of this declaration.
4490 DeclContext *DC = VarTemplate->getDeclContext();
4494 DeclContext *LexicalDC = DC;
4495 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4496 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4501 // Import the location of this declaration.
4502 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4503 SourceLocation IdLoc = Importer.Import(D->getLocation());
4505 // Import template arguments.
4506 SmallVector<TemplateArgument, 2> TemplateArgs;
4507 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4508 D->getTemplateArgs().size(), TemplateArgs))
4511 // Try to find an existing specialization with these template arguments.
4512 void *InsertPos = nullptr;
4513 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
4514 TemplateArgs, InsertPos);
4516 // We already have a variable template specialization with these template
4519 // FIXME: Check for specialization vs. instantiation errors.
4521 if (VarDecl *FoundDef = D2->getDefinition()) {
4522 if (!D->isThisDeclarationADefinition() ||
4523 IsStructuralMatch(D, FoundDef)) {
4524 // The record types structurally match, or the "from" translation
4525 // unit only had a forward declaration anyway; call it the same
4527 return Importer.Imported(D, FoundDef);
4533 QualType T = Importer.Import(D->getType());
4536 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4538 // Create a new specialization.
4539 D2 = VarTemplateSpecializationDecl::Create(
4540 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
4541 D->getStorageClass(), TemplateArgs.data(), TemplateArgs.size());
4542 D2->setSpecializationKind(D->getSpecializationKind());
4543 D2->setTemplateArgsInfo(D->getTemplateArgsInfo());
4545 // Add this specialization to the class template.
4546 VarTemplate->AddSpecialization(D2, InsertPos);
4548 // Import the qualifier, if any.
4549 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4551 // Add the specialization to this context.
4552 D2->setLexicalDeclContext(LexicalDC);
4553 LexicalDC->addDeclInternal(D2);
4555 Importer.Imported(D, D2);
4557 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2))
4563 //----------------------------------------------------------------------------
4564 // Import Statements
4565 //----------------------------------------------------------------------------
4567 DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4569 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4570 size_t NumDecls = DG.end() - DG.begin();
4571 SmallVector<Decl *, 1> ToDecls(NumDecls);
4572 auto &_Importer = this->Importer;
4573 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4574 [&_Importer](Decl *D) -> Decl * {
4575 return _Importer.Import(D);
4577 return DeclGroupRef::Create(Importer.getToContext(),
4582 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4583 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4584 << S->getStmtClassName();
4588 Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4589 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
4590 for (Decl *ToD : ToDG) {
4594 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4595 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4596 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4599 Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4600 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4601 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4602 S->hasLeadingEmptyMacro());
4605 Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
4606 SmallVector<Stmt *, 4> ToStmts(S->size());
4607 auto &_Importer = this->Importer;
4608 std::transform(S->body_begin(), S->body_end(), ToStmts.begin(),
4609 [&_Importer](Stmt *CS) -> Stmt * {
4610 return _Importer.Import(CS);
4612 for (Stmt *ToS : ToStmts) {
4616 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4617 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
4618 return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(),
4620 ToLBraceLoc, ToRBraceLoc);
4623 Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4624 Expr *ToLHS = Importer.Import(S->getLHS());
4627 Expr *ToRHS = Importer.Import(S->getRHS());
4628 if (!ToRHS && S->getRHS())
4630 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4631 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4632 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4633 return new (Importer.getToContext()) CaseStmt(ToLHS, ToRHS,
4634 ToCaseLoc, ToEllipsisLoc,
4638 Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4639 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4640 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4641 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4642 if (!ToSubStmt && S->getSubStmt())
4644 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4648 Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4649 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
4650 LabelDecl *ToLabelDecl =
4651 cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
4652 if (!ToLabelDecl && S->getDecl())
4654 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4655 if (!ToSubStmt && S->getSubStmt())
4657 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4661 Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4662 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4663 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4664 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
4665 ASTContext &_ToContext = Importer.getToContext();
4666 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
4667 [&_ToContext](const Attr *A) -> const Attr * {
4668 return A->clone(_ToContext);
4670 for (const Attr *ToA : ToAttrs) {
4674 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4675 if (!ToSubStmt && S->getSubStmt())
4677 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4678 ToAttrs, ToSubStmt);
4681 Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4682 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
4683 VarDecl *ToConditionVariable = nullptr;
4684 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4685 ToConditionVariable =
4686 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4687 if (!ToConditionVariable)
4690 Expr *ToCondition = Importer.Import(S->getCond());
4691 if (!ToCondition && S->getCond())
4693 Stmt *ToThenStmt = Importer.Import(S->getThen());
4694 if (!ToThenStmt && S->getThen())
4696 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4697 Stmt *ToElseStmt = Importer.Import(S->getElse());
4698 if (!ToElseStmt && S->getElse())
4700 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
4701 ToIfLoc, ToConditionVariable,
4702 ToCondition, ToThenStmt,
4703 ToElseLoc, ToElseStmt);
4706 Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
4707 VarDecl *ToConditionVariable = nullptr;
4708 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4709 ToConditionVariable =
4710 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4711 if (!ToConditionVariable)
4714 Expr *ToCondition = Importer.Import(S->getCond());
4715 if (!ToCondition && S->getCond())
4717 SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt(
4718 Importer.getToContext(), ToConditionVariable,
4720 Stmt *ToBody = Importer.Import(S->getBody());
4721 if (!ToBody && S->getBody())
4723 ToStmt->setBody(ToBody);
4724 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4725 // Now we have to re-chain the cases.
4726 SwitchCase *LastChainedSwitchCase = nullptr;
4727 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4728 SC = SC->getNextSwitchCase()) {
4729 SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
4732 if (LastChainedSwitchCase)
4733 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4735 ToStmt->setSwitchCaseList(ToSC);
4736 LastChainedSwitchCase = ToSC;
4741 Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
4742 VarDecl *ToConditionVariable = nullptr;
4743 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4744 ToConditionVariable =
4745 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4746 if (!ToConditionVariable)
4749 Expr *ToCondition = Importer.Import(S->getCond());
4750 if (!ToCondition && S->getCond())
4752 Stmt *ToBody = Importer.Import(S->getBody());
4753 if (!ToBody && S->getBody())
4755 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4756 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
4757 ToConditionVariable,
4758 ToCondition, ToBody,
4762 Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
4763 Stmt *ToBody = Importer.Import(S->getBody());
4764 if (!ToBody && S->getBody())
4766 Expr *ToCondition = Importer.Import(S->getCond());
4767 if (!ToCondition && S->getCond())
4769 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
4770 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4771 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4772 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
4773 ToDoLoc, ToWhileLoc,
4777 Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
4778 Stmt *ToInit = Importer.Import(S->getInit());
4779 if (!ToInit && S->getInit())
4781 Expr *ToCondition = Importer.Import(S->getCond());
4782 if (!ToCondition && S->getCond())
4784 VarDecl *ToConditionVariable = nullptr;
4785 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4786 ToConditionVariable =
4787 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4788 if (!ToConditionVariable)
4791 Expr *ToInc = Importer.Import(S->getInc());
4792 if (!ToInc && S->getInc())
4794 Stmt *ToBody = Importer.Import(S->getBody());
4795 if (!ToBody && S->getBody())
4797 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4798 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
4799 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4800 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
4801 ToInit, ToCondition,
4802 ToConditionVariable,
4804 ToForLoc, ToLParenLoc,
4808 Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
4809 LabelDecl *ToLabel = nullptr;
4810 if (LabelDecl *FromLabel = S->getLabel()) {
4811 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
4815 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4816 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
4817 return new (Importer.getToContext()) GotoStmt(ToLabel,
4818 ToGotoLoc, ToLabelLoc);
4821 Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
4822 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4823 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
4824 Expr *ToTarget = Importer.Import(S->getTarget());
4825 if (!ToTarget && S->getTarget())
4827 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
4831 Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
4832 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
4833 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
4836 Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
4837 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
4838 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
4841 Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
4842 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
4843 Expr *ToRetExpr = Importer.Import(S->getRetValue());
4844 if (!ToRetExpr && S->getRetValue())
4846 VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate());
4847 VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
4848 if (!ToNRVOCandidate && NRVOCandidate)
4850 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
4854 Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
4855 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
4856 VarDecl *ToExceptionDecl = nullptr;
4857 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
4859 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4860 if (!ToExceptionDecl)
4863 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
4864 if (!ToHandlerBlock && S->getHandlerBlock())
4866 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
4871 Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
4872 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
4873 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
4874 if (!ToTryBlock && S->getTryBlock())
4876 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
4877 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
4878 CXXCatchStmt *FromHandler = S->getHandler(HI);
4879 if (Stmt *ToHandler = Importer.Import(FromHandler))
4880 ToHandlers[HI] = ToHandler;
4884 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
4888 Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
4890 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
4891 if (!ToRange && S->getRangeStmt())
4893 DeclStmt *ToBeginEnd =
4894 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginEndStmt()));
4895 if (!ToBeginEnd && S->getBeginEndStmt())
4897 Expr *ToCond = Importer.Import(S->getCond());
4898 if (!ToCond && S->getCond())
4900 Expr *ToInc = Importer.Import(S->getInc());
4901 if (!ToInc && S->getInc())
4903 DeclStmt *ToLoopVar =
4904 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
4905 if (!ToLoopVar && S->getLoopVarStmt())
4907 Stmt *ToBody = Importer.Import(S->getBody());
4908 if (!ToBody && S->getBody())
4910 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4911 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4912 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4913 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBeginEnd,
4916 ToForLoc, ToColonLoc,
4920 Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
4921 Stmt *ToElem = Importer.Import(S->getElement());
4922 if (!ToElem && S->getElement())
4924 Expr *ToCollect = Importer.Import(S->getCollection());
4925 if (!ToCollect && S->getCollection())
4927 Stmt *ToBody = Importer.Import(S->getBody());
4928 if (!ToBody && S->getBody())
4930 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4931 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4932 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
4938 Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
4939 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
4940 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4941 VarDecl *ToExceptionDecl = nullptr;
4942 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
4944 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4945 if (!ToExceptionDecl)
4948 Stmt *ToBody = Importer.Import(S->getCatchBody());
4949 if (!ToBody && S->getCatchBody())
4951 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
4957 Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
4958 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
4959 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
4960 if (!ToAtFinallyStmt && S->getFinallyBody())
4962 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
4966 Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
4967 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
4968 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
4969 if (!ToAtTryStmt && S->getTryBody())
4971 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
4972 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
4973 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
4974 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
4975 ToCatchStmts[CI] = ToCatchStmt;
4979 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
4980 if (!ToAtFinallyStmt && S->getFinallyStmt())
4982 return ObjCAtTryStmt::Create(Importer.getToContext(),
4983 ToAtTryLoc, ToAtTryStmt,
4984 ToCatchStmts.begin(), ToCatchStmts.size(),
4988 Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
4989 (ObjCAtSynchronizedStmt *S) {
4990 SourceLocation ToAtSynchronizedLoc =
4991 Importer.Import(S->getAtSynchronizedLoc());
4992 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
4993 if (!ToSynchExpr && S->getSynchExpr())
4995 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
4996 if (!ToSynchBody && S->getSynchBody())
4998 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
4999 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5002 Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5003 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5004 Expr *ToThrow = Importer.Import(S->getThrowExpr());
5005 if (!ToThrow && S->getThrowExpr())
5007 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5010 Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5011 (ObjCAutoreleasePoolStmt *S) {
5012 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5013 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5014 if (!ToSubStmt && S->getSubStmt())
5016 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5020 //----------------------------------------------------------------------------
5021 // Import Expressions
5022 //----------------------------------------------------------------------------
5023 Expr *ASTNodeImporter::VisitExpr(Expr *E) {
5024 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5025 << E->getStmtClassName();
5029 Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
5030 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
5034 NamedDecl *FoundD = nullptr;
5035 if (E->getDecl() != E->getFoundDecl()) {
5036 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5041 QualType T = Importer.Import(E->getType());
5045 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5046 Importer.Import(E->getQualifierLoc()),
5047 Importer.Import(E->getTemplateKeywordLoc()),
5049 E->refersToEnclosingVariableOrCapture(),
5050 Importer.Import(E->getLocation()),
5051 T, E->getValueKind(),
5053 /*FIXME:TemplateArgs=*/nullptr);
5054 if (E->hadMultipleCandidates())
5055 DRE->setHadMultipleCandidates(true);
5059 Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5060 QualType T = Importer.Import(E->getType());
5064 return IntegerLiteral::Create(Importer.getToContext(),
5066 Importer.Import(E->getLocation()));
5069 Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5070 QualType T = Importer.Import(E->getType());
5074 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5076 Importer.Import(E->getLocation()));
5079 Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5080 Expr *SubExpr = Importer.Import(E->getSubExpr());
5084 return new (Importer.getToContext())
5085 ParenExpr(Importer.Import(E->getLParen()),
5086 Importer.Import(E->getRParen()),
5090 Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5091 QualType T = Importer.Import(E->getType());
5095 Expr *SubExpr = Importer.Import(E->getSubExpr());
5099 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
5100 T, E->getValueKind(),
5102 Importer.Import(E->getOperatorLoc()));
5105 Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
5106 UnaryExprOrTypeTraitExpr *E) {
5107 QualType ResultType = Importer.Import(E->getType());
5109 if (E->isArgumentType()) {
5110 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5114 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5116 Importer.Import(E->getOperatorLoc()),
5117 Importer.Import(E->getRParenLoc()));
5120 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5124 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5125 SubExpr, ResultType,
5126 Importer.Import(E->getOperatorLoc()),
5127 Importer.Import(E->getRParenLoc()));
5130 Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5131 QualType T = Importer.Import(E->getType());
5135 Expr *LHS = Importer.Import(E->getLHS());
5139 Expr *RHS = Importer.Import(E->getRHS());
5143 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
5144 T, E->getValueKind(),
5146 Importer.Import(E->getOperatorLoc()),
5147 E->isFPContractable());
5150 Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5151 QualType T = Importer.Import(E->getType());
5155 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5156 if (CompLHSType.isNull())
5159 QualType CompResultType = Importer.Import(E->getComputationResultType());
5160 if (CompResultType.isNull())
5163 Expr *LHS = Importer.Import(E->getLHS());
5167 Expr *RHS = Importer.Import(E->getRHS());
5171 return new (Importer.getToContext())
5172 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
5173 T, E->getValueKind(),
5175 CompLHSType, CompResultType,
5176 Importer.Import(E->getOperatorLoc()),
5177 E->isFPContractable());
5180 static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
5181 if (E->path_empty()) return false;
5183 // TODO: import cast paths
5187 Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5188 QualType T = Importer.Import(E->getType());
5192 Expr *SubExpr = Importer.Import(E->getSubExpr());
5196 CXXCastPath BasePath;
5197 if (ImportCastPath(E, BasePath))
5200 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
5201 SubExpr, &BasePath, E->getValueKind());
5204 Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
5205 QualType T = Importer.Import(E->getType());
5209 Expr *SubExpr = Importer.Import(E->getSubExpr());
5213 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5214 if (!TInfo && E->getTypeInfoAsWritten())
5217 CXXCastPath BasePath;
5218 if (ImportCastPath(E, BasePath))
5221 return CStyleCastExpr::Create(Importer.getToContext(), T,
5222 E->getValueKind(), E->getCastKind(),
5223 SubExpr, &BasePath, TInfo,
5224 Importer.Import(E->getLParenLoc()),
5225 Importer.Import(E->getRParenLoc()));
5228 Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
5229 QualType T = Importer.Import(E->getType());
5233 CXXConstructorDecl *ToCCD =
5234 dyn_cast<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
5235 if (!ToCCD && E->getConstructor())
5238 size_t NumArgs = E->getNumArgs();
5239 SmallVector<Expr *, 1> ToArgs(NumArgs);
5240 ASTImporter &_Importer = Importer;
5241 std::transform(E->arg_begin(), E->arg_end(), ToArgs.begin(),
5242 [&_Importer](Expr *AE) -> Expr * {
5243 return _Importer.Import(AE);
5245 for (Expr *ToA : ToArgs) {
5250 return CXXConstructExpr::Create(Importer.getToContext(), T,
5251 Importer.Import(E->getLocation()),
5252 ToCCD, E->isElidable(),
5253 ToArgs, E->hadMultipleCandidates(),
5254 E->isListInitialization(),
5255 E->isStdInitListInitialization(),
5256 E->requiresZeroInitialization(),
5257 E->getConstructionKind(),
5258 Importer.Import(E->getParenOrBraceRange()));
5261 Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
5262 QualType T = Importer.Import(E->getType());
5266 Expr *ToBase = Importer.Import(E->getBase());
5267 if (!ToBase && E->getBase())
5270 ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
5271 if (!ToMember && E->getMemberDecl())
5274 DeclAccessPair ToFoundDecl = DeclAccessPair::make(
5275 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
5276 E->getFoundDecl().getAccess());
5278 DeclarationNameInfo ToMemberNameInfo(
5279 Importer.Import(E->getMemberNameInfo().getName()),
5280 Importer.Import(E->getMemberNameInfo().getLoc()));
5282 if (E->hasExplicitTemplateArgs()) {
5283 return nullptr; // FIXME: handle template arguments
5286 return MemberExpr::Create(Importer.getToContext(), ToBase,
5288 Importer.Import(E->getOperatorLoc()),
5289 Importer.Import(E->getQualifierLoc()),
5290 Importer.Import(E->getTemplateKeywordLoc()),
5291 ToMember, ToFoundDecl, ToMemberNameInfo,
5292 nullptr, T, E->getValueKind(),
5293 E->getObjectKind());
5296 Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
5297 QualType T = Importer.Import(E->getType());
5301 Expr *ToCallee = Importer.Import(E->getCallee());
5302 if (!ToCallee && E->getCallee())
5305 unsigned NumArgs = E->getNumArgs();
5307 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
5309 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) {
5310 Expr *FromArg = E->getArg(ai);
5311 Expr *ToArg = Importer.Import(FromArg);
5317 Expr **ToArgs_Copied = new (Importer.getToContext())
5320 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
5321 ToArgs_Copied[ai] = ToArgs[ai];
5323 return new (Importer.getToContext())
5324 CallExpr(Importer.getToContext(), ToCallee,
5325 ArrayRef<Expr*>(ToArgs_Copied, NumArgs), T, E->getValueKind(),
5326 Importer.Import(E->getRParenLoc()));
5329 ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
5330 ASTContext &FromContext, FileManager &FromFileManager,
5332 : ToContext(ToContext), FromContext(FromContext),
5333 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
5334 Minimal(MinimalImport), LastDiagFromFrom(false)
5336 ImportedDecls[FromContext.getTranslationUnitDecl()]
5337 = ToContext.getTranslationUnitDecl();
5340 ASTImporter::~ASTImporter() { }
5342 QualType ASTImporter::Import(QualType FromT) {
5346 const Type *fromTy = FromT.getTypePtr();
5348 // Check whether we've already imported this type.
5349 llvm::DenseMap<const Type *, const Type *>::iterator Pos
5350 = ImportedTypes.find(fromTy);
5351 if (Pos != ImportedTypes.end())
5352 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
5355 ASTNodeImporter Importer(*this);
5356 QualType ToT = Importer.Visit(fromTy);
5360 // Record the imported type.
5361 ImportedTypes[fromTy] = ToT.getTypePtr();
5363 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
5366 TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
5370 // FIXME: For now we just create a "trivial" type source info based
5371 // on the type and a single location. Implement a real version of this.
5372 QualType T = Import(FromTSI->getType());
5376 return ToContext.getTrivialTypeSourceInfo(T,
5377 Import(FromTSI->getTypeLoc().getLocStart()));
5380 Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
5381 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
5382 if (Pos != ImportedDecls.end()) {
5383 Decl *ToD = Pos->second;
5384 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
5391 Decl *ASTImporter::Import(Decl *FromD) {
5395 ASTNodeImporter Importer(*this);
5397 // Check whether we've already imported this declaration.
5398 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
5399 if (Pos != ImportedDecls.end()) {
5400 Decl *ToD = Pos->second;
5401 Importer.ImportDefinitionIfNeeded(FromD, ToD);
5406 Decl *ToD = Importer.Visit(FromD);
5410 // Record the imported declaration.
5411 ImportedDecls[FromD] = ToD;
5413 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
5414 // Keep track of anonymous tags that have an associated typedef.
5415 if (FromTag->getTypedefNameForAnonDecl())
5416 AnonTagsWithPendingTypedefs.push_back(FromTag);
5417 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
5418 // When we've finished transforming a typedef, see whether it was the
5419 // typedef for an anonymous tag.
5420 for (SmallVectorImpl<TagDecl *>::iterator
5421 FromTag = AnonTagsWithPendingTypedefs.begin(),
5422 FromTagEnd = AnonTagsWithPendingTypedefs.end();
5423 FromTag != FromTagEnd; ++FromTag) {
5424 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
5425 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
5426 // We found the typedef for an anonymous tag; link them.
5427 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
5428 AnonTagsWithPendingTypedefs.erase(FromTag);
5438 DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
5442 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
5446 // When we're using a record/enum/Objective-C class/protocol as a context, we
5447 // need it to have a definition.
5448 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
5449 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
5450 if (ToRecord->isCompleteDefinition()) {
5452 } else if (FromRecord->isCompleteDefinition()) {
5453 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
5454 ASTNodeImporter::IDK_Basic);
5456 CompleteDecl(ToRecord);
5458 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
5459 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
5460 if (ToEnum->isCompleteDefinition()) {
5462 } else if (FromEnum->isCompleteDefinition()) {
5463 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
5464 ASTNodeImporter::IDK_Basic);
5466 CompleteDecl(ToEnum);
5468 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
5469 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
5470 if (ToClass->getDefinition()) {
5472 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
5473 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
5474 ASTNodeImporter::IDK_Basic);
5476 CompleteDecl(ToClass);
5478 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
5479 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
5480 if (ToProto->getDefinition()) {
5482 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
5483 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
5484 ASTNodeImporter::IDK_Basic);
5486 CompleteDecl(ToProto);
5493 Expr *ASTImporter::Import(Expr *FromE) {
5497 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
5500 Stmt *ASTImporter::Import(Stmt *FromS) {
5504 // Check whether we've already imported this declaration.
5505 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
5506 if (Pos != ImportedStmts.end())
5510 ASTNodeImporter Importer(*this);
5511 Stmt *ToS = Importer.Visit(FromS);
5515 // Record the imported declaration.
5516 ImportedStmts[FromS] = ToS;
5520 NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
5524 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
5526 switch (FromNNS->getKind()) {
5527 case NestedNameSpecifier::Identifier:
5528 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
5529 return NestedNameSpecifier::Create(ToContext, prefix, II);
5533 case NestedNameSpecifier::Namespace:
5534 if (NamespaceDecl *NS =
5535 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
5536 return NestedNameSpecifier::Create(ToContext, prefix, NS);
5540 case NestedNameSpecifier::NamespaceAlias:
5541 if (NamespaceAliasDecl *NSAD =
5542 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
5543 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
5547 case NestedNameSpecifier::Global:
5548 return NestedNameSpecifier::GlobalSpecifier(ToContext);
5550 case NestedNameSpecifier::Super:
5551 if (CXXRecordDecl *RD =
5552 cast<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
5553 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
5557 case NestedNameSpecifier::TypeSpec:
5558 case NestedNameSpecifier::TypeSpecWithTemplate: {
5559 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
5561 bool bTemplate = FromNNS->getKind() ==
5562 NestedNameSpecifier::TypeSpecWithTemplate;
5563 return NestedNameSpecifier::Create(ToContext, prefix,
5564 bTemplate, T.getTypePtr());
5570 llvm_unreachable("Invalid nested name specifier kind");
5573 NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
5574 // FIXME: Implement!
5575 return NestedNameSpecifierLoc();
5578 TemplateName ASTImporter::Import(TemplateName From) {
5579 switch (From.getKind()) {
5580 case TemplateName::Template:
5581 if (TemplateDecl *ToTemplate
5582 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
5583 return TemplateName(ToTemplate);
5585 return TemplateName();
5587 case TemplateName::OverloadedTemplate: {
5588 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
5589 UnresolvedSet<2> ToTemplates;
5590 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
5591 E = FromStorage->end();
5593 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
5594 ToTemplates.addDecl(To);
5596 return TemplateName();
5598 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
5602 case TemplateName::QualifiedTemplate: {
5603 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
5604 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
5606 return TemplateName();
5608 if (TemplateDecl *ToTemplate
5609 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
5610 return ToContext.getQualifiedTemplateName(Qualifier,
5611 QTN->hasTemplateKeyword(),
5614 return TemplateName();
5617 case TemplateName::DependentTemplate: {
5618 DependentTemplateName *DTN = From.getAsDependentTemplateName();
5619 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
5621 return TemplateName();
5623 if (DTN->isIdentifier()) {
5624 return ToContext.getDependentTemplateName(Qualifier,
5625 Import(DTN->getIdentifier()));
5628 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
5631 case TemplateName::SubstTemplateTemplateParm: {
5632 SubstTemplateTemplateParmStorage *subst
5633 = From.getAsSubstTemplateTemplateParm();
5634 TemplateTemplateParmDecl *param
5635 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
5637 return TemplateName();
5639 TemplateName replacement = Import(subst->getReplacement());
5640 if (replacement.isNull()) return TemplateName();
5642 return ToContext.getSubstTemplateTemplateParm(param, replacement);
5645 case TemplateName::SubstTemplateTemplateParmPack: {
5646 SubstTemplateTemplateParmPackStorage *SubstPack
5647 = From.getAsSubstTemplateTemplateParmPack();
5648 TemplateTemplateParmDecl *Param
5649 = cast_or_null<TemplateTemplateParmDecl>(
5650 Import(SubstPack->getParameterPack()));
5652 return TemplateName();
5654 ASTNodeImporter Importer(*this);
5655 TemplateArgument ArgPack
5656 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
5657 if (ArgPack.isNull())
5658 return TemplateName();
5660 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
5664 llvm_unreachable("Invalid template name kind");
5667 SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
5668 if (FromLoc.isInvalid())
5669 return SourceLocation();
5671 SourceManager &FromSM = FromContext.getSourceManager();
5673 // For now, map everything down to its spelling location, so that we
5674 // don't have to import macro expansions.
5675 // FIXME: Import macro expansions!
5676 FromLoc = FromSM.getSpellingLoc(FromLoc);
5677 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
5678 SourceManager &ToSM = ToContext.getSourceManager();
5679 FileID ToFileID = Import(Decomposed.first);
5680 if (ToFileID.isInvalid())
5681 return SourceLocation();
5682 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
5683 .getLocWithOffset(Decomposed.second);
5687 SourceRange ASTImporter::Import(SourceRange FromRange) {
5688 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
5691 FileID ASTImporter::Import(FileID FromID) {
5692 llvm::DenseMap<FileID, FileID>::iterator Pos
5693 = ImportedFileIDs.find(FromID);
5694 if (Pos != ImportedFileIDs.end())
5697 SourceManager &FromSM = FromContext.getSourceManager();
5698 SourceManager &ToSM = ToContext.getSourceManager();
5699 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
5700 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
5702 // Include location of this file.
5703 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
5705 // Map the FileID for to the "to" source manager.
5707 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
5708 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
5709 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
5711 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
5712 // than mmap the files several times.
5713 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
5716 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
5717 FromSLoc.getFile().getFileCharacteristic());
5719 // FIXME: We want to re-use the existing MemoryBuffer!
5720 const llvm::MemoryBuffer *
5721 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
5722 std::unique_ptr<llvm::MemoryBuffer> ToBuf
5723 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
5724 FromBuf->getBufferIdentifier());
5725 ToID = ToSM.createFileID(std::move(ToBuf),
5726 FromSLoc.getFile().getFileCharacteristic());
5730 ImportedFileIDs[FromID] = ToID;
5734 void ASTImporter::ImportDefinition(Decl *From) {
5735 Decl *To = Import(From);
5739 if (DeclContext *FromDC = cast<DeclContext>(From)) {
5740 ASTNodeImporter Importer(*this);
5742 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
5743 if (!ToRecord->getDefinition()) {
5744 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
5745 ASTNodeImporter::IDK_Everything);
5750 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
5751 if (!ToEnum->getDefinition()) {
5752 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
5753 ASTNodeImporter::IDK_Everything);
5758 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
5759 if (!ToIFace->getDefinition()) {
5760 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
5761 ASTNodeImporter::IDK_Everything);
5766 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
5767 if (!ToProto->getDefinition()) {
5768 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
5769 ASTNodeImporter::IDK_Everything);
5774 Importer.ImportDeclContext(FromDC, true);
5778 DeclarationName ASTImporter::Import(DeclarationName FromName) {
5780 return DeclarationName();
5782 switch (FromName.getNameKind()) {
5783 case DeclarationName::Identifier:
5784 return Import(FromName.getAsIdentifierInfo());
5786 case DeclarationName::ObjCZeroArgSelector:
5787 case DeclarationName::ObjCOneArgSelector:
5788 case DeclarationName::ObjCMultiArgSelector:
5789 return Import(FromName.getObjCSelector());
5791 case DeclarationName::CXXConstructorName: {
5792 QualType T = Import(FromName.getCXXNameType());
5794 return DeclarationName();
5796 return ToContext.DeclarationNames.getCXXConstructorName(
5797 ToContext.getCanonicalType(T));
5800 case DeclarationName::CXXDestructorName: {
5801 QualType T = Import(FromName.getCXXNameType());
5803 return DeclarationName();
5805 return ToContext.DeclarationNames.getCXXDestructorName(
5806 ToContext.getCanonicalType(T));
5809 case DeclarationName::CXXConversionFunctionName: {
5810 QualType T = Import(FromName.getCXXNameType());
5812 return DeclarationName();
5814 return ToContext.DeclarationNames.getCXXConversionFunctionName(
5815 ToContext.getCanonicalType(T));
5818 case DeclarationName::CXXOperatorName:
5819 return ToContext.DeclarationNames.getCXXOperatorName(
5820 FromName.getCXXOverloadedOperator());
5822 case DeclarationName::CXXLiteralOperatorName:
5823 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
5824 Import(FromName.getCXXLiteralIdentifier()));
5826 case DeclarationName::CXXUsingDirective:
5828 return DeclarationName::getUsingDirectiveName();
5831 llvm_unreachable("Invalid DeclarationName Kind!");
5834 IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
5838 return &ToContext.Idents.get(FromId->getName());
5841 Selector ASTImporter::Import(Selector FromSel) {
5842 if (FromSel.isNull())
5845 SmallVector<IdentifierInfo *, 4> Idents;
5846 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
5847 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
5848 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
5849 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
5852 DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
5856 unsigned NumDecls) {
5860 DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
5861 if (LastDiagFromFrom)
5862 ToContext.getDiagnostics().notePriorDiagnosticFrom(
5863 FromContext.getDiagnostics());
5864 LastDiagFromFrom = false;
5865 return ToContext.getDiagnostics().Report(Loc, DiagID);
5868 DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
5869 if (!LastDiagFromFrom)
5870 FromContext.getDiagnostics().notePriorDiagnosticFrom(
5871 ToContext.getDiagnostics());
5872 LastDiagFromFrom = true;
5873 return FromContext.getDiagnostics().Report(Loc, DiagID);
5876 void ASTImporter::CompleteDecl (Decl *D) {
5877 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
5878 if (!ID->getDefinition())
5879 ID->startDefinition();
5881 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
5882 if (!PD->getDefinition())
5883 PD->startDefinition();
5885 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
5886 if (!TD->getDefinition() && !TD->isBeingDefined()) {
5887 TD->startDefinition();
5888 TD->setCompleteDefinition(true);
5892 assert (0 && "CompleteDecl called on a Decl that can't be completed");
5896 Decl *ASTImporter::Imported(Decl *From, Decl *To) {
5897 ImportedDecls[From] = To;
5901 bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
5903 llvm::DenseMap<const Type *, const Type *>::iterator Pos
5904 = ImportedTypes.find(From.getTypePtr());
5905 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
5908 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
5910 return Ctx.IsStructurallyEquivalent(From, To);