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/ASTStructuralEquivalence.h"
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/DeclObjC.h"
20 #include "clang/AST/DeclVisitor.h"
21 #include "clang/AST/StmtVisitor.h"
22 #include "clang/AST/TypeVisitor.h"
23 #include "clang/Basic/FileManager.h"
24 #include "clang/Basic/SourceManager.h"
25 #include "llvm/Support/MemoryBuffer.h"
29 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
30 public DeclVisitor<ASTNodeImporter, Decl *>,
31 public StmtVisitor<ASTNodeImporter, Stmt *> {
32 ASTImporter &Importer;
35 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
37 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
38 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
39 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
42 QualType VisitType(const Type *T);
43 QualType VisitAtomicType(const AtomicType *T);
44 QualType VisitBuiltinType(const BuiltinType *T);
45 QualType VisitDecayedType(const DecayedType *T);
46 QualType VisitComplexType(const ComplexType *T);
47 QualType VisitPointerType(const PointerType *T);
48 QualType VisitBlockPointerType(const BlockPointerType *T);
49 QualType VisitLValueReferenceType(const LValueReferenceType *T);
50 QualType VisitRValueReferenceType(const RValueReferenceType *T);
51 QualType VisitMemberPointerType(const MemberPointerType *T);
52 QualType VisitConstantArrayType(const ConstantArrayType *T);
53 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
54 QualType VisitVariableArrayType(const VariableArrayType *T);
55 // FIXME: DependentSizedArrayType
56 // FIXME: DependentSizedExtVectorType
57 QualType VisitVectorType(const VectorType *T);
58 QualType VisitExtVectorType(const ExtVectorType *T);
59 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
60 QualType VisitFunctionProtoType(const FunctionProtoType *T);
61 // FIXME: UnresolvedUsingType
62 QualType VisitParenType(const ParenType *T);
63 QualType VisitTypedefType(const TypedefType *T);
64 QualType VisitTypeOfExprType(const TypeOfExprType *T);
65 // FIXME: DependentTypeOfExprType
66 QualType VisitTypeOfType(const TypeOfType *T);
67 QualType VisitDecltypeType(const DecltypeType *T);
68 QualType VisitUnaryTransformType(const UnaryTransformType *T);
69 QualType VisitAutoType(const AutoType *T);
70 QualType VisitInjectedClassNameType(const InjectedClassNameType *T);
71 // FIXME: DependentDecltypeType
72 QualType VisitRecordType(const RecordType *T);
73 QualType VisitEnumType(const EnumType *T);
74 QualType VisitAttributedType(const AttributedType *T);
75 QualType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
76 QualType VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T);
77 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
78 QualType VisitElaboratedType(const ElaboratedType *T);
79 // FIXME: DependentNameType
80 // FIXME: DependentTemplateSpecializationType
81 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
82 QualType VisitObjCObjectType(const ObjCObjectType *T);
83 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
85 // Importing declarations
86 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
87 DeclContext *&LexicalDC, DeclarationName &Name,
88 NamedDecl *&ToD, SourceLocation &Loc);
89 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
90 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
91 DeclarationNameInfo& To);
92 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
94 bool ImportCastPath(CastExpr *E, CXXCastPath &Path);
96 typedef DesignatedInitExpr::Designator Designator;
97 Designator ImportDesignator(const Designator &D);
100 /// \brief What we should import from the definition.
101 enum ImportDefinitionKind {
102 /// \brief Import the default subset of the definition, which might be
103 /// nothing (if minimal import is set) or might be everything (if minimal
104 /// import is not set).
106 /// \brief Import everything.
108 /// \brief Import only the bare bones needed to establish a valid
113 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
114 return IDK == IDK_Everything ||
115 (IDK == IDK_Default && !Importer.isMinimalImport());
118 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
119 ImportDefinitionKind Kind = IDK_Default);
120 bool ImportDefinition(VarDecl *From, VarDecl *To,
121 ImportDefinitionKind Kind = IDK_Default);
122 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
123 ImportDefinitionKind Kind = IDK_Default);
124 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
125 ImportDefinitionKind Kind = IDK_Default);
126 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
127 ImportDefinitionKind Kind = IDK_Default);
128 TemplateParameterList *ImportTemplateParameterList(
129 TemplateParameterList *Params);
130 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
131 TemplateArgumentLoc ImportTemplateArgumentLoc(
132 const TemplateArgumentLoc &TALoc, bool &Error);
133 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
134 unsigned NumFromArgs,
135 SmallVectorImpl<TemplateArgument> &ToArgs);
136 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
137 bool Complain = true);
138 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
139 bool Complain = true);
140 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
141 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
142 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
143 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
144 Decl *VisitDecl(Decl *D);
145 Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
146 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
147 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
148 Decl *VisitNamespaceDecl(NamespaceDecl *D);
149 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
150 Decl *VisitTypedefDecl(TypedefDecl *D);
151 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
152 Decl *VisitLabelDecl(LabelDecl *D);
153 Decl *VisitEnumDecl(EnumDecl *D);
154 Decl *VisitRecordDecl(RecordDecl *D);
155 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
156 Decl *VisitFunctionDecl(FunctionDecl *D);
157 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
158 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
159 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
160 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
161 Decl *VisitFieldDecl(FieldDecl *D);
162 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
163 Decl *VisitFriendDecl(FriendDecl *D);
164 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
165 Decl *VisitVarDecl(VarDecl *D);
166 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
167 Decl *VisitParmVarDecl(ParmVarDecl *D);
168 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
169 Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
170 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
171 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
172 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D);
174 ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list);
175 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
176 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
177 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
178 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
179 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
180 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
181 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
182 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
183 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
184 Decl *VisitClassTemplateSpecializationDecl(
185 ClassTemplateSpecializationDecl *D);
186 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
187 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
189 // Importing statements
190 DeclGroupRef ImportDeclGroup(DeclGroupRef DG);
192 Stmt *VisitStmt(Stmt *S);
193 Stmt *VisitGCCAsmStmt(GCCAsmStmt *S);
194 Stmt *VisitDeclStmt(DeclStmt *S);
195 Stmt *VisitNullStmt(NullStmt *S);
196 Stmt *VisitCompoundStmt(CompoundStmt *S);
197 Stmt *VisitCaseStmt(CaseStmt *S);
198 Stmt *VisitDefaultStmt(DefaultStmt *S);
199 Stmt *VisitLabelStmt(LabelStmt *S);
200 Stmt *VisitAttributedStmt(AttributedStmt *S);
201 Stmt *VisitIfStmt(IfStmt *S);
202 Stmt *VisitSwitchStmt(SwitchStmt *S);
203 Stmt *VisitWhileStmt(WhileStmt *S);
204 Stmt *VisitDoStmt(DoStmt *S);
205 Stmt *VisitForStmt(ForStmt *S);
206 Stmt *VisitGotoStmt(GotoStmt *S);
207 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S);
208 Stmt *VisitContinueStmt(ContinueStmt *S);
209 Stmt *VisitBreakStmt(BreakStmt *S);
210 Stmt *VisitReturnStmt(ReturnStmt *S);
212 // FIXME: SEHExceptStmt
213 // FIXME: SEHFinallyStmt
215 // FIXME: SEHLeaveStmt
216 // FIXME: CapturedStmt
217 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S);
218 Stmt *VisitCXXTryStmt(CXXTryStmt *S);
219 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S);
220 // FIXME: MSDependentExistsStmt
221 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
222 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
223 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
224 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
225 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
226 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
227 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
229 // Importing expressions
230 Expr *VisitExpr(Expr *E);
231 Expr *VisitVAArgExpr(VAArgExpr *E);
232 Expr *VisitGNUNullExpr(GNUNullExpr *E);
233 Expr *VisitPredefinedExpr(PredefinedExpr *E);
234 Expr *VisitDeclRefExpr(DeclRefExpr *E);
235 Expr *VisitImplicitValueInitExpr(ImplicitValueInitExpr *ILE);
236 Expr *VisitDesignatedInitExpr(DesignatedInitExpr *E);
237 Expr *VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
238 Expr *VisitIntegerLiteral(IntegerLiteral *E);
239 Expr *VisitFloatingLiteral(FloatingLiteral *E);
240 Expr *VisitCharacterLiteral(CharacterLiteral *E);
241 Expr *VisitStringLiteral(StringLiteral *E);
242 Expr *VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
243 Expr *VisitAtomicExpr(AtomicExpr *E);
244 Expr *VisitAddrLabelExpr(AddrLabelExpr *E);
245 Expr *VisitParenExpr(ParenExpr *E);
246 Expr *VisitParenListExpr(ParenListExpr *E);
247 Expr *VisitStmtExpr(StmtExpr *E);
248 Expr *VisitUnaryOperator(UnaryOperator *E);
249 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
250 Expr *VisitBinaryOperator(BinaryOperator *E);
251 Expr *VisitConditionalOperator(ConditionalOperator *E);
252 Expr *VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
253 Expr *VisitOpaqueValueExpr(OpaqueValueExpr *E);
254 Expr *VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
255 Expr *VisitExpressionTraitExpr(ExpressionTraitExpr *E);
256 Expr *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
257 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
258 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
259 Expr *VisitExplicitCastExpr(ExplicitCastExpr *E);
260 Expr *VisitOffsetOfExpr(OffsetOfExpr *OE);
261 Expr *VisitCXXThrowExpr(CXXThrowExpr *E);
262 Expr *VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
263 Expr *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
264 Expr *VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
265 Expr *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
266 Expr *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE);
267 Expr *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
268 Expr *VisitCXXNewExpr(CXXNewExpr *CE);
269 Expr *VisitCXXDeleteExpr(CXXDeleteExpr *E);
270 Expr *VisitCXXConstructExpr(CXXConstructExpr *E);
271 Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
272 Expr *VisitExprWithCleanups(ExprWithCleanups *EWC);
273 Expr *VisitCXXThisExpr(CXXThisExpr *E);
274 Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
275 Expr *VisitMemberExpr(MemberExpr *E);
276 Expr *VisitCallExpr(CallExpr *E);
277 Expr *VisitInitListExpr(InitListExpr *E);
278 Expr *VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
279 Expr *VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
280 Expr *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
281 Expr *VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
282 Expr *VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
285 template<typename IIter, typename OIter>
286 void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) {
287 typedef typename std::remove_reference<decltype(*Obegin)>::type ItemT;
288 ASTImporter &ImporterRef = Importer;
289 std::transform(Ibegin, Iend, Obegin,
290 [&ImporterRef](ItemT From) -> ItemT {
291 return ImporterRef.Import(From);
295 template<typename IIter, typename OIter>
296 bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
297 typedef typename std::remove_reference<decltype(**Obegin)>::type ItemT;
298 ASTImporter &ImporterRef = Importer;
300 std::transform(Ibegin, Iend, Obegin,
301 [&ImporterRef, &Failed](ItemT *From) -> ItemT * {
302 ItemT *To = cast_or_null<ItemT>(
303 ImporterRef.Import(From));
311 template<typename InContainerTy, typename OutContainerTy>
312 bool ImportContainerChecked(const InContainerTy &InContainer,
313 OutContainerTy &OutContainer) {
314 return ImportArrayChecked(InContainer.begin(), InContainer.end(),
315 OutContainer.begin());
318 template<typename InContainerTy, typename OIter>
319 bool ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
320 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
323 // Importing overrides.
324 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
328 //----------------------------------------------------------------------------
330 //----------------------------------------------------------------------------
332 using namespace clang;
334 QualType ASTNodeImporter::VisitType(const Type *T) {
335 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
336 << T->getTypeClassName();
340 QualType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
341 QualType UnderlyingType = Importer.Import(T->getValueType());
342 if(UnderlyingType.isNull())
345 return Importer.getToContext().getAtomicType(UnderlyingType);
348 QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
349 switch (T->getKind()) {
350 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
351 case BuiltinType::Id: \
352 return Importer.getToContext().SingletonId;
353 #include "clang/Basic/OpenCLImageTypes.def"
354 #define SHARED_SINGLETON_TYPE(Expansion)
355 #define BUILTIN_TYPE(Id, SingletonId) \
356 case BuiltinType::Id: return Importer.getToContext().SingletonId;
357 #include "clang/AST/BuiltinTypes.def"
359 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
360 // context supports C++.
362 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
363 // context supports ObjC.
365 case BuiltinType::Char_U:
366 // The context we're importing from has an unsigned 'char'. If we're
367 // importing into a context with a signed 'char', translate to
368 // 'unsigned char' instead.
369 if (Importer.getToContext().getLangOpts().CharIsSigned)
370 return Importer.getToContext().UnsignedCharTy;
372 return Importer.getToContext().CharTy;
374 case BuiltinType::Char_S:
375 // The context we're importing from has an unsigned 'char'. If we're
376 // importing into a context with a signed 'char', translate to
377 // 'unsigned char' instead.
378 if (!Importer.getToContext().getLangOpts().CharIsSigned)
379 return Importer.getToContext().SignedCharTy;
381 return Importer.getToContext().CharTy;
383 case BuiltinType::WChar_S:
384 case BuiltinType::WChar_U:
385 // FIXME: If not in C++, shall we translate to the C equivalent of
387 return Importer.getToContext().WCharTy;
390 llvm_unreachable("Invalid BuiltinType Kind!");
393 QualType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
394 QualType OrigT = Importer.Import(T->getOriginalType());
398 return Importer.getToContext().getDecayedType(OrigT);
401 QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
402 QualType ToElementType = Importer.Import(T->getElementType());
403 if (ToElementType.isNull())
406 return Importer.getToContext().getComplexType(ToElementType);
409 QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
410 QualType ToPointeeType = Importer.Import(T->getPointeeType());
411 if (ToPointeeType.isNull())
414 return Importer.getToContext().getPointerType(ToPointeeType);
417 QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
418 // FIXME: Check for blocks support in "to" context.
419 QualType ToPointeeType = Importer.Import(T->getPointeeType());
420 if (ToPointeeType.isNull())
423 return Importer.getToContext().getBlockPointerType(ToPointeeType);
427 ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
428 // FIXME: Check for C++ support in "to" context.
429 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
430 if (ToPointeeType.isNull())
433 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
437 ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
438 // FIXME: Check for C++0x support in "to" context.
439 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
440 if (ToPointeeType.isNull())
443 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
446 QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
447 // FIXME: Check for C++ support in "to" context.
448 QualType ToPointeeType = Importer.Import(T->getPointeeType());
449 if (ToPointeeType.isNull())
452 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
453 return Importer.getToContext().getMemberPointerType(ToPointeeType,
454 ClassType.getTypePtr());
457 QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
458 QualType ToElementType = Importer.Import(T->getElementType());
459 if (ToElementType.isNull())
462 return Importer.getToContext().getConstantArrayType(ToElementType,
464 T->getSizeModifier(),
465 T->getIndexTypeCVRQualifiers());
469 ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
470 QualType ToElementType = Importer.Import(T->getElementType());
471 if (ToElementType.isNull())
474 return Importer.getToContext().getIncompleteArrayType(ToElementType,
475 T->getSizeModifier(),
476 T->getIndexTypeCVRQualifiers());
479 QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
480 QualType ToElementType = Importer.Import(T->getElementType());
481 if (ToElementType.isNull())
484 Expr *Size = Importer.Import(T->getSizeExpr());
488 SourceRange Brackets = Importer.Import(T->getBracketsRange());
489 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
490 T->getSizeModifier(),
491 T->getIndexTypeCVRQualifiers(),
495 QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
496 QualType ToElementType = Importer.Import(T->getElementType());
497 if (ToElementType.isNull())
500 return Importer.getToContext().getVectorType(ToElementType,
505 QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
506 QualType ToElementType = Importer.Import(T->getElementType());
507 if (ToElementType.isNull())
510 return Importer.getToContext().getExtVectorType(ToElementType,
511 T->getNumElements());
515 ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
516 // FIXME: What happens if we're importing a function without a prototype
517 // into C++? Should we make it variadic?
518 QualType ToResultType = Importer.Import(T->getReturnType());
519 if (ToResultType.isNull())
522 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
526 QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
527 QualType ToResultType = Importer.Import(T->getReturnType());
528 if (ToResultType.isNull())
531 // Import argument types
532 SmallVector<QualType, 4> ArgTypes;
533 for (const auto &A : T->param_types()) {
534 QualType ArgType = Importer.Import(A);
535 if (ArgType.isNull())
537 ArgTypes.push_back(ArgType);
540 // Import exception types
541 SmallVector<QualType, 4> ExceptionTypes;
542 for (const auto &E : T->exceptions()) {
543 QualType ExceptionType = Importer.Import(E);
544 if (ExceptionType.isNull())
546 ExceptionTypes.push_back(ExceptionType);
549 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
550 FunctionProtoType::ExtProtoInfo ToEPI;
552 ToEPI.ExtInfo = FromEPI.ExtInfo;
553 ToEPI.Variadic = FromEPI.Variadic;
554 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
555 ToEPI.TypeQuals = FromEPI.TypeQuals;
556 ToEPI.RefQualifier = FromEPI.RefQualifier;
557 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
558 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
559 ToEPI.ExceptionSpec.NoexceptExpr =
560 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
561 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
562 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
563 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
564 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
566 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
569 QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
570 QualType ToInnerType = Importer.Import(T->getInnerType());
571 if (ToInnerType.isNull())
574 return Importer.getToContext().getParenType(ToInnerType);
577 QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
578 TypedefNameDecl *ToDecl
579 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
583 return Importer.getToContext().getTypeDeclType(ToDecl);
586 QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
587 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
591 return Importer.getToContext().getTypeOfExprType(ToExpr);
594 QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
595 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
596 if (ToUnderlyingType.isNull())
599 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
602 QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
603 // FIXME: Make sure that the "to" context supports C++0x!
604 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
608 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
609 if (UnderlyingType.isNull())
612 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
615 QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
616 QualType ToBaseType = Importer.Import(T->getBaseType());
617 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
618 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
621 return Importer.getToContext().getUnaryTransformType(ToBaseType,
626 QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
627 // FIXME: Make sure that the "to" context supports C++11!
628 QualType FromDeduced = T->getDeducedType();
630 if (!FromDeduced.isNull()) {
631 ToDeduced = Importer.Import(FromDeduced);
632 if (ToDeduced.isNull())
636 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
637 /*IsDependent*/false);
640 QualType ASTNodeImporter::VisitInjectedClassNameType(
641 const InjectedClassNameType *T) {
642 CXXRecordDecl *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl()));
646 QualType InjType = Importer.Import(T->getInjectedSpecializationType());
647 if (InjType.isNull())
650 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
651 // See comments in InjectedClassNameType definition for details
652 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
654 TypeAlignmentInBits = 4,
655 TypeAlignment = 1 << TypeAlignmentInBits
658 return QualType(new (Importer.getToContext(), TypeAlignment)
659 InjectedClassNameType(D, InjType), 0);
662 QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
664 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
668 return Importer.getToContext().getTagDeclType(ToDecl);
671 QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
673 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
677 return Importer.getToContext().getTagDeclType(ToDecl);
680 QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
681 QualType FromModifiedType = T->getModifiedType();
682 QualType FromEquivalentType = T->getEquivalentType();
683 QualType ToModifiedType;
684 QualType ToEquivalentType;
686 if (!FromModifiedType.isNull()) {
687 ToModifiedType = Importer.Import(FromModifiedType);
688 if (ToModifiedType.isNull())
691 if (!FromEquivalentType.isNull()) {
692 ToEquivalentType = Importer.Import(FromEquivalentType);
693 if (ToEquivalentType.isNull())
697 return Importer.getToContext().getAttributedType(T->getAttrKind(),
698 ToModifiedType, ToEquivalentType);
702 QualType ASTNodeImporter::VisitTemplateTypeParmType(
703 const TemplateTypeParmType *T) {
704 TemplateTypeParmDecl *ParmDecl =
705 cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl()));
706 if (!ParmDecl && T->getDecl())
709 return Importer.getToContext().getTemplateTypeParmType(
710 T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl);
713 QualType ASTNodeImporter::VisitSubstTemplateTypeParmType(
714 const SubstTemplateTypeParmType *T) {
715 const TemplateTypeParmType *Replaced =
716 cast_or_null<TemplateTypeParmType>(Importer.Import(
717 QualType(T->getReplacedParameter(), 0)).getTypePtr());
721 QualType Replacement = Importer.Import(T->getReplacementType());
722 if (Replacement.isNull())
724 Replacement = Replacement.getCanonicalType();
726 return Importer.getToContext().getSubstTemplateTypeParmType(
727 Replaced, Replacement);
730 QualType ASTNodeImporter::VisitTemplateSpecializationType(
731 const TemplateSpecializationType *T) {
732 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
733 if (ToTemplate.isNull())
736 SmallVector<TemplateArgument, 2> ToTemplateArgs;
737 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
740 QualType ToCanonType;
741 if (!QualType(T, 0).isCanonical()) {
742 QualType FromCanonType
743 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
744 ToCanonType =Importer.Import(FromCanonType);
745 if (ToCanonType.isNull())
748 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
753 QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
754 NestedNameSpecifier *ToQualifier = nullptr;
755 // Note: the qualifier in an ElaboratedType is optional.
756 if (T->getQualifier()) {
757 ToQualifier = Importer.Import(T->getQualifier());
762 QualType ToNamedType = Importer.Import(T->getNamedType());
763 if (ToNamedType.isNull())
766 return Importer.getToContext().getElaboratedType(T->getKeyword(),
767 ToQualifier, ToNamedType);
770 QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
771 ObjCInterfaceDecl *Class
772 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
776 return Importer.getToContext().getObjCInterfaceType(Class);
779 QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
780 QualType ToBaseType = Importer.Import(T->getBaseType());
781 if (ToBaseType.isNull())
784 SmallVector<QualType, 4> TypeArgs;
785 for (auto TypeArg : T->getTypeArgsAsWritten()) {
786 QualType ImportedTypeArg = Importer.Import(TypeArg);
787 if (ImportedTypeArg.isNull())
790 TypeArgs.push_back(ImportedTypeArg);
793 SmallVector<ObjCProtocolDecl *, 4> Protocols;
794 for (auto *P : T->quals()) {
795 ObjCProtocolDecl *Protocol
796 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
799 Protocols.push_back(Protocol);
802 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
804 T->isKindOfTypeAsWritten());
808 ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
809 QualType ToPointeeType = Importer.Import(T->getPointeeType());
810 if (ToPointeeType.isNull())
813 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
816 //----------------------------------------------------------------------------
817 // Import Declarations
818 //----------------------------------------------------------------------------
819 bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
820 DeclContext *&LexicalDC,
821 DeclarationName &Name,
823 SourceLocation &Loc) {
824 // Import the context of this declaration.
825 DC = Importer.ImportContext(D->getDeclContext());
830 if (D->getDeclContext() != D->getLexicalDeclContext()) {
831 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
836 // Import the name of this declaration.
837 Name = Importer.Import(D->getDeclName());
838 if (D->getDeclName() && !Name)
841 // Import the location of this declaration.
842 Loc = Importer.Import(D->getLocation());
843 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
847 void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
852 ToD = Importer.Import(FromD);
857 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
858 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
859 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
860 ImportDefinition(FromRecord, ToRecord);
866 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
867 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
868 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
869 ImportDefinition(FromEnum, ToEnum);
877 ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
878 DeclarationNameInfo& To) {
879 // NOTE: To.Name and To.Loc are already imported.
880 // We only have to import To.LocInfo.
881 switch (To.getName().getNameKind()) {
882 case DeclarationName::Identifier:
883 case DeclarationName::ObjCZeroArgSelector:
884 case DeclarationName::ObjCOneArgSelector:
885 case DeclarationName::ObjCMultiArgSelector:
886 case DeclarationName::CXXUsingDirective:
887 case DeclarationName::CXXDeductionGuideName:
890 case DeclarationName::CXXOperatorName: {
891 SourceRange Range = From.getCXXOperatorNameRange();
892 To.setCXXOperatorNameRange(Importer.Import(Range));
895 case DeclarationName::CXXLiteralOperatorName: {
896 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
897 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
900 case DeclarationName::CXXConstructorName:
901 case DeclarationName::CXXDestructorName:
902 case DeclarationName::CXXConversionFunctionName: {
903 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
904 To.setNamedTypeInfo(Importer.Import(FromTInfo));
908 llvm_unreachable("Unknown name kind.");
911 void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
912 if (Importer.isMinimalImport() && !ForceImport) {
913 Importer.ImportContext(FromDC);
917 for (auto *From : FromDC->decls())
918 Importer.Import(From);
921 bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
922 ImportDefinitionKind Kind) {
923 if (To->getDefinition() || To->isBeingDefined()) {
924 if (Kind == IDK_Everything)
925 ImportDeclContext(From, /*ForceImport=*/true);
930 To->startDefinition();
933 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
934 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
936 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
937 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
938 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
939 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
940 ToData.Aggregate = FromData.Aggregate;
941 ToData.PlainOldData = FromData.PlainOldData;
942 ToData.Empty = FromData.Empty;
943 ToData.Polymorphic = FromData.Polymorphic;
944 ToData.Abstract = FromData.Abstract;
945 ToData.IsStandardLayout = FromData.IsStandardLayout;
946 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
947 ToData.HasPrivateFields = FromData.HasPrivateFields;
948 ToData.HasProtectedFields = FromData.HasProtectedFields;
949 ToData.HasPublicFields = FromData.HasPublicFields;
950 ToData.HasMutableFields = FromData.HasMutableFields;
951 ToData.HasVariantMembers = FromData.HasVariantMembers;
952 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
953 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
954 ToData.HasUninitializedReferenceMember
955 = FromData.HasUninitializedReferenceMember;
956 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
957 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
958 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
959 ToData.NeedOverloadResolutionForCopyConstructor
960 = FromData.NeedOverloadResolutionForCopyConstructor;
961 ToData.NeedOverloadResolutionForMoveConstructor
962 = FromData.NeedOverloadResolutionForMoveConstructor;
963 ToData.NeedOverloadResolutionForMoveAssignment
964 = FromData.NeedOverloadResolutionForMoveAssignment;
965 ToData.NeedOverloadResolutionForDestructor
966 = FromData.NeedOverloadResolutionForDestructor;
967 ToData.DefaultedCopyConstructorIsDeleted
968 = FromData.DefaultedCopyConstructorIsDeleted;
969 ToData.DefaultedMoveConstructorIsDeleted
970 = FromData.DefaultedMoveConstructorIsDeleted;
971 ToData.DefaultedMoveAssignmentIsDeleted
972 = FromData.DefaultedMoveAssignmentIsDeleted;
973 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
974 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
975 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
976 ToData.HasConstexprNonCopyMoveConstructor
977 = FromData.HasConstexprNonCopyMoveConstructor;
978 ToData.HasDefaultedDefaultConstructor
979 = FromData.HasDefaultedDefaultConstructor;
980 ToData.CanPassInRegisters = FromData.CanPassInRegisters;
981 ToData.DefaultedDefaultConstructorIsConstexpr
982 = FromData.DefaultedDefaultConstructorIsConstexpr;
983 ToData.HasConstexprDefaultConstructor
984 = FromData.HasConstexprDefaultConstructor;
985 ToData.HasNonLiteralTypeFieldsOrBases
986 = FromData.HasNonLiteralTypeFieldsOrBases;
987 // ComputedVisibleConversions not imported.
988 ToData.UserProvidedDefaultConstructor
989 = FromData.UserProvidedDefaultConstructor;
990 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
991 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
992 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
993 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
994 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
995 ToData.ImplicitCopyAssignmentHasConstParam
996 = FromData.ImplicitCopyAssignmentHasConstParam;
997 ToData.HasDeclaredCopyConstructorWithConstParam
998 = FromData.HasDeclaredCopyConstructorWithConstParam;
999 ToData.HasDeclaredCopyAssignmentWithConstParam
1000 = FromData.HasDeclaredCopyAssignmentWithConstParam;
1001 ToData.IsLambda = FromData.IsLambda;
1003 SmallVector<CXXBaseSpecifier *, 4> Bases;
1004 for (const auto &Base1 : FromCXX->bases()) {
1005 QualType T = Importer.Import(Base1.getType());
1009 SourceLocation EllipsisLoc;
1010 if (Base1.isPackExpansion())
1011 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
1013 // Ensure that we have a definition for the base.
1014 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
1017 new (Importer.getToContext())
1018 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
1020 Base1.isBaseOfClass(),
1021 Base1.getAccessSpecifierAsWritten(),
1022 Importer.Import(Base1.getTypeSourceInfo()),
1026 ToCXX->setBases(Bases.data(), Bases.size());
1029 if (shouldForceImportDeclContext(Kind))
1030 ImportDeclContext(From, /*ForceImport=*/true);
1032 To->completeDefinition();
1036 bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
1037 ImportDefinitionKind Kind) {
1038 if (To->getAnyInitializer())
1041 // FIXME: Can we really import any initializer? Alternatively, we could force
1042 // ourselves to import every declaration of a variable and then only use
1044 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
1046 // FIXME: Other bits to merge?
1051 bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
1052 ImportDefinitionKind Kind) {
1053 if (To->getDefinition() || To->isBeingDefined()) {
1054 if (Kind == IDK_Everything)
1055 ImportDeclContext(From, /*ForceImport=*/true);
1059 To->startDefinition();
1061 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1065 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1066 if (ToPromotionType.isNull())
1069 if (shouldForceImportDeclContext(Kind))
1070 ImportDeclContext(From, /*ForceImport=*/true);
1072 // FIXME: we might need to merge the number of positive or negative bits
1073 // if the enumerator lists don't match.
1074 To->completeDefinition(T, ToPromotionType,
1075 From->getNumPositiveBits(),
1076 From->getNumNegativeBits());
1080 TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1081 TemplateParameterList *Params) {
1082 SmallVector<NamedDecl *, 4> ToParams(Params->size());
1083 if (ImportContainerChecked(*Params, ToParams))
1086 Expr *ToRequiresClause;
1087 if (Expr *const R = Params->getRequiresClause()) {
1088 ToRequiresClause = Importer.Import(R);
1089 if (!ToRequiresClause)
1092 ToRequiresClause = nullptr;
1095 return TemplateParameterList::Create(Importer.getToContext(),
1096 Importer.Import(Params->getTemplateLoc()),
1097 Importer.Import(Params->getLAngleLoc()),
1099 Importer.Import(Params->getRAngleLoc()),
1104 ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1105 switch (From.getKind()) {
1106 case TemplateArgument::Null:
1107 return TemplateArgument();
1109 case TemplateArgument::Type: {
1110 QualType ToType = Importer.Import(From.getAsType());
1111 if (ToType.isNull())
1112 return TemplateArgument();
1113 return TemplateArgument(ToType);
1116 case TemplateArgument::Integral: {
1117 QualType ToType = Importer.Import(From.getIntegralType());
1118 if (ToType.isNull())
1119 return TemplateArgument();
1120 return TemplateArgument(From, ToType);
1123 case TemplateArgument::Declaration: {
1124 ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
1125 QualType ToType = Importer.Import(From.getParamTypeForDecl());
1126 if (!To || ToType.isNull())
1127 return TemplateArgument();
1128 return TemplateArgument(To, ToType);
1131 case TemplateArgument::NullPtr: {
1132 QualType ToType = Importer.Import(From.getNullPtrType());
1133 if (ToType.isNull())
1134 return TemplateArgument();
1135 return TemplateArgument(ToType, /*isNullPtr*/true);
1138 case TemplateArgument::Template: {
1139 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1140 if (ToTemplate.isNull())
1141 return TemplateArgument();
1143 return TemplateArgument(ToTemplate);
1146 case TemplateArgument::TemplateExpansion: {
1147 TemplateName ToTemplate
1148 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1149 if (ToTemplate.isNull())
1150 return TemplateArgument();
1152 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
1155 case TemplateArgument::Expression:
1156 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1157 return TemplateArgument(ToExpr);
1158 return TemplateArgument();
1160 case TemplateArgument::Pack: {
1161 SmallVector<TemplateArgument, 2> ToPack;
1162 ToPack.reserve(From.pack_size());
1163 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1164 return TemplateArgument();
1166 return TemplateArgument(
1167 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
1171 llvm_unreachable("Invalid template argument kind");
1174 TemplateArgumentLoc ASTNodeImporter::ImportTemplateArgumentLoc(
1175 const TemplateArgumentLoc &TALoc, bool &Error) {
1177 TemplateArgument Arg = ImportTemplateArgument(TALoc.getArgument());
1178 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
1179 TemplateArgumentLocInfo ToInfo;
1180 if (Arg.getKind() == TemplateArgument::Expression) {
1181 Expr *E = Importer.Import(FromInfo.getAsExpr());
1182 ToInfo = TemplateArgumentLocInfo(E);
1185 } else if (Arg.getKind() == TemplateArgument::Type) {
1186 if (TypeSourceInfo *TSI = Importer.Import(FromInfo.getAsTypeSourceInfo()))
1187 ToInfo = TemplateArgumentLocInfo(TSI);
1191 ToInfo = TemplateArgumentLocInfo(
1192 Importer.Import(FromInfo.getTemplateQualifierLoc()),
1193 Importer.Import(FromInfo.getTemplateNameLoc()),
1194 Importer.Import(FromInfo.getTemplateEllipsisLoc()));
1196 return TemplateArgumentLoc(Arg, ToInfo);
1199 bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1200 unsigned NumFromArgs,
1201 SmallVectorImpl<TemplateArgument> &ToArgs) {
1202 for (unsigned I = 0; I != NumFromArgs; ++I) {
1203 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1204 if (To.isNull() && !FromArgs[I].isNull())
1207 ToArgs.push_back(To);
1213 bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
1214 RecordDecl *ToRecord, bool Complain) {
1215 // Eliminate a potential failure point where we attempt to re-import
1216 // something we're trying to import while completing ToRecord.
1217 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1219 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
1221 ToRecord = ToOriginRecord;
1224 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1225 ToRecord->getASTContext(),
1226 Importer.getNonEquivalentDecls(),
1228 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
1231 bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
1233 StructuralEquivalenceContext Ctx(
1234 Importer.getFromContext(), Importer.getToContext(),
1235 Importer.getNonEquivalentDecls(), false, Complain);
1236 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
1239 bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
1240 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1241 Importer.getToContext(),
1242 Importer.getNonEquivalentDecls());
1243 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
1246 bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
1247 EnumConstantDecl *ToEC)
1249 const llvm::APSInt &FromVal = FromEC->getInitVal();
1250 const llvm::APSInt &ToVal = ToEC->getInitVal();
1252 return FromVal.isSigned() == ToVal.isSigned() &&
1253 FromVal.getBitWidth() == ToVal.getBitWidth() &&
1257 bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
1258 ClassTemplateDecl *To) {
1259 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1260 Importer.getToContext(),
1261 Importer.getNonEquivalentDecls());
1262 return Ctx.IsStructurallyEquivalent(From, To);
1265 bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
1266 VarTemplateDecl *To) {
1267 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1268 Importer.getToContext(),
1269 Importer.getNonEquivalentDecls());
1270 return Ctx.IsStructurallyEquivalent(From, To);
1273 Decl *ASTNodeImporter::VisitDecl(Decl *D) {
1274 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1275 << D->getDeclKindName();
1279 Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
1280 TranslationUnitDecl *ToD =
1281 Importer.getToContext().getTranslationUnitDecl();
1283 Importer.Imported(D, ToD);
1288 Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
1290 SourceLocation Loc = Importer.Import(D->getLocation());
1291 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
1293 // Import the context of this declaration.
1294 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1298 AccessSpecDecl *accessSpecDecl
1299 = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(),
1302 if (!accessSpecDecl)
1305 // Lexical DeclContext and Semantic DeclContext
1306 // is always the same for the accessSpec.
1307 accessSpecDecl->setLexicalDeclContext(DC);
1308 DC->addDeclInternal(accessSpecDecl);
1310 return accessSpecDecl;
1313 Decl *ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
1314 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1318 DeclContext *LexicalDC = DC;
1320 // Import the location of this declaration.
1321 SourceLocation Loc = Importer.Import(D->getLocation());
1323 Expr *AssertExpr = Importer.Import(D->getAssertExpr());
1327 StringLiteral *FromMsg = D->getMessage();
1328 StringLiteral *ToMsg = cast_or_null<StringLiteral>(Importer.Import(FromMsg));
1329 if (!ToMsg && FromMsg)
1332 StaticAssertDecl *ToD = StaticAssertDecl::Create(
1333 Importer.getToContext(), DC, Loc, AssertExpr, ToMsg,
1334 Importer.Import(D->getRParenLoc()), D->isFailed());
1336 ToD->setLexicalDeclContext(LexicalDC);
1337 LexicalDC->addDeclInternal(ToD);
1338 Importer.Imported(D, ToD);
1342 Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1343 // Import the major distinguishing characteristics of this namespace.
1344 DeclContext *DC, *LexicalDC;
1345 DeclarationName Name;
1348 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1353 NamespaceDecl *MergeWithNamespace = nullptr;
1355 // This is an anonymous namespace. Adopt an existing anonymous
1356 // namespace if we can.
1357 // FIXME: Not testable.
1358 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1359 MergeWithNamespace = TU->getAnonymousNamespace();
1361 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1363 SmallVector<NamedDecl *, 4> ConflictingDecls;
1364 SmallVector<NamedDecl *, 2> FoundDecls;
1365 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
1366 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1367 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
1370 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
1371 MergeWithNamespace = FoundNS;
1372 ConflictingDecls.clear();
1376 ConflictingDecls.push_back(FoundDecls[I]);
1379 if (!ConflictingDecls.empty()) {
1380 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
1381 ConflictingDecls.data(),
1382 ConflictingDecls.size());
1386 // Create the "to" namespace, if needed.
1387 NamespaceDecl *ToNamespace = MergeWithNamespace;
1389 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
1391 Importer.Import(D->getLocStart()),
1392 Loc, Name.getAsIdentifierInfo(),
1393 /*PrevDecl=*/nullptr);
1394 ToNamespace->setLexicalDeclContext(LexicalDC);
1395 LexicalDC->addDeclInternal(ToNamespace);
1397 // If this is an anonymous namespace, register it as the anonymous
1398 // namespace within its context.
1400 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1401 TU->setAnonymousNamespace(ToNamespace);
1403 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1406 Importer.Imported(D, ToNamespace);
1408 ImportDeclContext(D);
1413 Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
1414 // Import the major distinguishing characteristics of this typedef.
1415 DeclContext *DC, *LexicalDC;
1416 DeclarationName Name;
1419 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1424 // If this typedef is not in block scope, determine whether we've
1425 // seen a typedef with the same name (that we can merge with) or any
1426 // other entity by that name (which name lookup could conflict with).
1427 if (!DC->isFunctionOrMethod()) {
1428 SmallVector<NamedDecl *, 4> ConflictingDecls;
1429 unsigned IDNS = Decl::IDNS_Ordinary;
1430 SmallVector<NamedDecl *, 2> FoundDecls;
1431 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
1432 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1433 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
1435 if (TypedefNameDecl *FoundTypedef =
1436 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
1437 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1438 FoundTypedef->getUnderlyingType()))
1439 return Importer.Imported(D, FoundTypedef);
1442 ConflictingDecls.push_back(FoundDecls[I]);
1445 if (!ConflictingDecls.empty()) {
1446 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1447 ConflictingDecls.data(),
1448 ConflictingDecls.size());
1454 // Import the underlying type of this typedef;
1455 QualType T = Importer.Import(D->getUnderlyingType());
1459 // Create the new typedef node.
1460 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1461 SourceLocation StartL = Importer.Import(D->getLocStart());
1462 TypedefNameDecl *ToTypedef;
1464 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
1466 Name.getAsIdentifierInfo(),
1469 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1471 Name.getAsIdentifierInfo(),
1474 ToTypedef->setAccess(D->getAccess());
1475 ToTypedef->setLexicalDeclContext(LexicalDC);
1476 Importer.Imported(D, ToTypedef);
1477 LexicalDC->addDeclInternal(ToTypedef);
1482 Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1483 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
1486 Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
1487 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
1490 Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
1491 // Import the major distinguishing characteristics of this label.
1492 DeclContext *DC, *LexicalDC;
1493 DeclarationName Name;
1496 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1501 assert(LexicalDC->isFunctionOrMethod());
1503 LabelDecl *ToLabel = D->isGnuLocal()
1504 ? LabelDecl::Create(Importer.getToContext(),
1505 DC, Importer.Import(D->getLocation()),
1506 Name.getAsIdentifierInfo(),
1507 Importer.Import(D->getLocStart()))
1508 : LabelDecl::Create(Importer.getToContext(),
1509 DC, Importer.Import(D->getLocation()),
1510 Name.getAsIdentifierInfo());
1511 Importer.Imported(D, ToLabel);
1513 LabelStmt *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt()));
1517 ToLabel->setStmt(Label);
1518 ToLabel->setLexicalDeclContext(LexicalDC);
1519 LexicalDC->addDeclInternal(ToLabel);
1523 Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1524 // Import the major distinguishing characteristics of this enum.
1525 DeclContext *DC, *LexicalDC;
1526 DeclarationName Name;
1529 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1534 // Figure out what enum name we're looking for.
1535 unsigned IDNS = Decl::IDNS_Tag;
1536 DeclarationName SearchName = Name;
1537 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1538 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
1539 IDNS = Decl::IDNS_Ordinary;
1540 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
1541 IDNS |= Decl::IDNS_Ordinary;
1543 // We may already have an enum of the same name; try to find and match it.
1544 if (!DC->isFunctionOrMethod() && SearchName) {
1545 SmallVector<NamedDecl *, 4> ConflictingDecls;
1546 SmallVector<NamedDecl *, 2> FoundDecls;
1547 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
1548 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1549 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
1552 Decl *Found = FoundDecls[I];
1553 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
1554 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1555 Found = Tag->getDecl();
1558 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
1559 if (IsStructuralMatch(D, FoundEnum))
1560 return Importer.Imported(D, FoundEnum);
1563 ConflictingDecls.push_back(FoundDecls[I]);
1566 if (!ConflictingDecls.empty()) {
1567 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1568 ConflictingDecls.data(),
1569 ConflictingDecls.size());
1573 // Create the enum declaration.
1574 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
1575 Importer.Import(D->getLocStart()),
1576 Loc, Name.getAsIdentifierInfo(), nullptr,
1577 D->isScoped(), D->isScopedUsingClassTag(),
1579 // Import the qualifier, if any.
1580 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
1581 D2->setAccess(D->getAccess());
1582 D2->setLexicalDeclContext(LexicalDC);
1583 Importer.Imported(D, D2);
1584 LexicalDC->addDeclInternal(D2);
1586 // Import the integer type.
1587 QualType ToIntegerType = Importer.Import(D->getIntegerType());
1588 if (ToIntegerType.isNull())
1590 D2->setIntegerType(ToIntegerType);
1592 // Import the definition
1593 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
1599 Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
1600 // If this record has a definition in the translation unit we're coming from,
1601 // but this particular declaration is not that definition, import the
1602 // definition and map to that.
1603 TagDecl *Definition = D->getDefinition();
1604 if (Definition && Definition != D) {
1605 Decl *ImportedDef = Importer.Import(Definition);
1609 return Importer.Imported(D, ImportedDef);
1612 // Import the major distinguishing characteristics of this record.
1613 DeclContext *DC, *LexicalDC;
1614 DeclarationName Name;
1617 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1622 // Figure out what structure name we're looking for.
1623 unsigned IDNS = Decl::IDNS_Tag;
1624 DeclarationName SearchName = Name;
1625 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1626 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
1627 IDNS = Decl::IDNS_Ordinary;
1628 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
1629 IDNS |= Decl::IDNS_Ordinary;
1631 // We may already have a record of the same name; try to find and match it.
1632 RecordDecl *AdoptDecl = nullptr;
1633 RecordDecl *PrevDecl = nullptr;
1634 if (!DC->isFunctionOrMethod()) {
1635 SmallVector<NamedDecl *, 4> ConflictingDecls;
1636 SmallVector<NamedDecl *, 2> FoundDecls;
1637 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
1639 if (!FoundDecls.empty()) {
1640 // We're going to have to compare D against potentially conflicting Decls, so complete it.
1641 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
1642 D->getASTContext().getExternalSource()->CompleteType(D);
1645 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1646 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
1649 Decl *Found = FoundDecls[I];
1650 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
1651 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1652 Found = Tag->getDecl();
1655 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
1656 if (D->isAnonymousStructOrUnion() &&
1657 FoundRecord->isAnonymousStructOrUnion()) {
1658 // If both anonymous structs/unions are in a record context, make sure
1659 // they occur in the same location in the context records.
1660 if (Optional<unsigned> Index1 =
1661 StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(
1663 if (Optional<unsigned> Index2 = StructuralEquivalenceContext::
1664 findUntaggedStructOrUnionIndex(FoundRecord)) {
1665 if (*Index1 != *Index2)
1671 PrevDecl = FoundRecord;
1673 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
1674 if ((SearchName && !D->isCompleteDefinition())
1675 || (D->isCompleteDefinition() &&
1676 D->isAnonymousStructOrUnion()
1677 == FoundDef->isAnonymousStructOrUnion() &&
1678 IsStructuralMatch(D, FoundDef))) {
1679 // The record types structurally match, or the "from" translation
1680 // unit only had a forward declaration anyway; call it the same
1682 // FIXME: For C++, we should also merge methods here.
1683 return Importer.Imported(D, FoundDef);
1685 } else if (!D->isCompleteDefinition()) {
1686 // We have a forward declaration of this type, so adopt that forward
1687 // declaration rather than building a new one.
1689 // If one or both can be completed from external storage then try one
1690 // last time to complete and compare them before doing this.
1692 if (FoundRecord->hasExternalLexicalStorage() &&
1693 !FoundRecord->isCompleteDefinition())
1694 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
1695 if (D->hasExternalLexicalStorage())
1696 D->getASTContext().getExternalSource()->CompleteType(D);
1698 if (FoundRecord->isCompleteDefinition() &&
1699 D->isCompleteDefinition() &&
1700 !IsStructuralMatch(D, FoundRecord))
1703 AdoptDecl = FoundRecord;
1705 } else if (!SearchName) {
1710 ConflictingDecls.push_back(FoundDecls[I]);
1713 if (!ConflictingDecls.empty() && SearchName) {
1714 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1715 ConflictingDecls.data(),
1716 ConflictingDecls.size());
1720 // Create the record declaration.
1721 RecordDecl *D2 = AdoptDecl;
1722 SourceLocation StartLoc = Importer.Import(D->getLocStart());
1724 CXXRecordDecl *D2CXX = nullptr;
1725 if (CXXRecordDecl *DCXX = llvm::dyn_cast<CXXRecordDecl>(D)) {
1726 if (DCXX->isLambda()) {
1727 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
1728 D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(),
1730 DCXX->isDependentLambda(),
1731 DCXX->isGenericLambda(),
1732 DCXX->getLambdaCaptureDefault());
1733 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
1734 if (DCXX->getLambdaContextDecl() && !CDecl)
1736 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl);
1737 } else if (DCXX->isInjectedClassName()) {
1738 // We have to be careful to do a similar dance to the one in
1739 // Sema::ActOnStartCXXMemberDeclarations
1740 CXXRecordDecl *const PrevDecl = nullptr;
1741 const bool DelayTypeCreation = true;
1742 D2CXX = CXXRecordDecl::Create(
1743 Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc,
1744 Name.getAsIdentifierInfo(), PrevDecl, DelayTypeCreation);
1745 Importer.getToContext().getTypeDeclType(
1746 D2CXX, llvm::dyn_cast<CXXRecordDecl>(DC));
1748 D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
1751 Name.getAsIdentifierInfo());
1754 D2->setAccess(D->getAccess());
1756 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
1757 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
1760 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
1761 D2->setLexicalDeclContext(LexicalDC);
1762 LexicalDC->addDeclInternal(D2);
1763 if (D->isAnonymousStructOrUnion())
1764 D2->setAnonymousStructOrUnion(true);
1766 // FIXME: do this for all Redeclarables, not just RecordDecls.
1767 D2->setPreviousDecl(PrevDecl);
1771 Importer.Imported(D, D2);
1773 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
1779 Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
1780 // Import the major distinguishing characteristics of this enumerator.
1781 DeclContext *DC, *LexicalDC;
1782 DeclarationName Name;
1785 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1790 QualType T = Importer.Import(D->getType());
1794 // Determine whether there are any other declarations with the same name and
1795 // in the same context.
1796 if (!LexicalDC->isFunctionOrMethod()) {
1797 SmallVector<NamedDecl *, 4> ConflictingDecls;
1798 unsigned IDNS = Decl::IDNS_Ordinary;
1799 SmallVector<NamedDecl *, 2> FoundDecls;
1800 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
1801 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1802 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
1805 if (EnumConstantDecl *FoundEnumConstant
1806 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
1807 if (IsStructuralMatch(D, FoundEnumConstant))
1808 return Importer.Imported(D, FoundEnumConstant);
1811 ConflictingDecls.push_back(FoundDecls[I]);
1814 if (!ConflictingDecls.empty()) {
1815 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1816 ConflictingDecls.data(),
1817 ConflictingDecls.size());
1823 Expr *Init = Importer.Import(D->getInitExpr());
1824 if (D->getInitExpr() && !Init)
1827 EnumConstantDecl *ToEnumerator
1828 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
1829 Name.getAsIdentifierInfo(), T,
1830 Init, D->getInitVal());
1831 ToEnumerator->setAccess(D->getAccess());
1832 ToEnumerator->setLexicalDeclContext(LexicalDC);
1833 Importer.Imported(D, ToEnumerator);
1834 LexicalDC->addDeclInternal(ToEnumerator);
1835 return ToEnumerator;
1838 Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
1839 // Import the major distinguishing characteristics of this function.
1840 DeclContext *DC, *LexicalDC;
1841 DeclarationName Name;
1844 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1849 // Try to find a function in our own ("to") context with the same name, same
1850 // type, and in the same context as the function we're importing.
1851 if (!LexicalDC->isFunctionOrMethod()) {
1852 SmallVector<NamedDecl *, 4> ConflictingDecls;
1853 unsigned IDNS = Decl::IDNS_Ordinary;
1854 SmallVector<NamedDecl *, 2> FoundDecls;
1855 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
1856 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1857 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
1860 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
1861 if (FoundFunction->hasExternalFormalLinkage() &&
1862 D->hasExternalFormalLinkage()) {
1863 if (Importer.IsStructurallyEquivalent(D->getType(),
1864 FoundFunction->getType())) {
1865 // FIXME: Actually try to merge the body and other attributes.
1866 return Importer.Imported(D, FoundFunction);
1869 // FIXME: Check for overloading more carefully, e.g., by boosting
1870 // Sema::IsOverload out to the AST library.
1872 // Function overloading is okay in C++.
1873 if (Importer.getToContext().getLangOpts().CPlusPlus)
1876 // Complain about inconsistent function types.
1877 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
1878 << Name << D->getType() << FoundFunction->getType();
1879 Importer.ToDiag(FoundFunction->getLocation(),
1880 diag::note_odr_value_here)
1881 << FoundFunction->getType();
1885 ConflictingDecls.push_back(FoundDecls[I]);
1888 if (!ConflictingDecls.empty()) {
1889 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1890 ConflictingDecls.data(),
1891 ConflictingDecls.size());
1897 DeclarationNameInfo NameInfo(Name, Loc);
1898 // Import additional name location/type info.
1899 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
1901 QualType FromTy = D->getType();
1902 bool usedDifferentExceptionSpec = false;
1904 if (const FunctionProtoType *
1905 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
1906 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
1907 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
1908 // FunctionDecl that we are importing the FunctionProtoType for.
1909 // To avoid an infinite recursion when importing, create the FunctionDecl
1910 // with a simplified function type and update it afterwards.
1911 if (FromEPI.ExceptionSpec.SourceDecl ||
1912 FromEPI.ExceptionSpec.SourceTemplate ||
1913 FromEPI.ExceptionSpec.NoexceptExpr) {
1914 FunctionProtoType::ExtProtoInfo DefaultEPI;
1915 FromTy = Importer.getFromContext().getFunctionType(
1916 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
1917 usedDifferentExceptionSpec = true;
1922 QualType T = Importer.Import(FromTy);
1926 // Import the function parameters.
1927 SmallVector<ParmVarDecl *, 8> Parameters;
1928 for (auto P : D->parameters()) {
1929 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
1933 Parameters.push_back(ToP);
1936 // Create the imported function.
1937 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1938 FunctionDecl *ToFunction = nullptr;
1939 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
1940 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
1941 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
1942 cast<CXXRecordDecl>(DC),
1945 FromConstructor->isExplicit(),
1946 D->isInlineSpecified(),
1949 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
1950 SmallVector<CXXCtorInitializer *, 4> CtorInitializers;
1951 for (CXXCtorInitializer *I : FromConstructor->inits()) {
1952 CXXCtorInitializer *ToI =
1953 cast_or_null<CXXCtorInitializer>(Importer.Import(I));
1956 CtorInitializers.push_back(ToI);
1958 CXXCtorInitializer **Memory =
1959 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
1960 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
1961 CXXConstructorDecl *ToCtor = llvm::cast<CXXConstructorDecl>(ToFunction);
1962 ToCtor->setCtorInitializers(Memory);
1963 ToCtor->setNumCtorInitializers(NumInitializers);
1965 } else if (isa<CXXDestructorDecl>(D)) {
1966 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
1967 cast<CXXRecordDecl>(DC),
1970 D->isInlineSpecified(),
1972 } else if (CXXConversionDecl *FromConversion
1973 = dyn_cast<CXXConversionDecl>(D)) {
1974 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
1975 cast<CXXRecordDecl>(DC),
1978 D->isInlineSpecified(),
1979 FromConversion->isExplicit(),
1981 Importer.Import(D->getLocEnd()));
1982 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
1983 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
1984 cast<CXXRecordDecl>(DC),
1987 Method->getStorageClass(),
1988 Method->isInlineSpecified(),
1990 Importer.Import(D->getLocEnd()));
1992 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
1994 NameInfo, T, TInfo, D->getStorageClass(),
1995 D->isInlineSpecified(),
1996 D->hasWrittenPrototype(),
2000 // Import the qualifier, if any.
2001 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
2002 ToFunction->setAccess(D->getAccess());
2003 ToFunction->setLexicalDeclContext(LexicalDC);
2004 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2005 ToFunction->setTrivial(D->isTrivial());
2006 ToFunction->setPure(D->isPure());
2007 Importer.Imported(D, ToFunction);
2009 // Set the parameters.
2010 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
2011 Parameters[I]->setOwningFunction(ToFunction);
2012 ToFunction->addDeclInternal(Parameters[I]);
2014 ToFunction->setParams(Parameters);
2016 if (usedDifferentExceptionSpec) {
2017 // Update FunctionProtoType::ExtProtoInfo.
2018 QualType T = Importer.Import(D->getType());
2021 ToFunction->setType(T);
2024 // Import the body, if any.
2025 if (Stmt *FromBody = D->getBody()) {
2026 if (Stmt *ToBody = Importer.Import(FromBody)) {
2027 ToFunction->setBody(ToBody);
2031 // FIXME: Other bits to merge?
2033 // Add this function to the lexical context.
2034 LexicalDC->addDeclInternal(ToFunction);
2036 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
2037 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
2042 Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2043 return VisitFunctionDecl(D);
2046 Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2047 return VisitCXXMethodDecl(D);
2050 Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2051 return VisitCXXMethodDecl(D);
2054 Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2055 return VisitCXXMethodDecl(D);
2058 static unsigned getFieldIndex(Decl *F) {
2059 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
2064 for (const auto *D : Owner->noload_decls()) {
2068 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2075 Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2076 // Import the major distinguishing characteristics of a variable.
2077 DeclContext *DC, *LexicalDC;
2078 DeclarationName Name;
2081 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2086 // Determine whether we've already imported this field.
2087 SmallVector<NamedDecl *, 2> FoundDecls;
2088 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2089 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2090 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
2091 // For anonymous fields, match up by index.
2092 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2095 if (Importer.IsStructurallyEquivalent(D->getType(),
2096 FoundField->getType())) {
2097 Importer.Imported(D, FoundField);
2101 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2102 << Name << D->getType() << FoundField->getType();
2103 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2104 << FoundField->getType();
2110 QualType T = Importer.Import(D->getType());
2114 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2115 Expr *BitWidth = Importer.Import(D->getBitWidth());
2116 if (!BitWidth && D->getBitWidth())
2119 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2120 Importer.Import(D->getInnerLocStart()),
2121 Loc, Name.getAsIdentifierInfo(),
2122 T, TInfo, BitWidth, D->isMutable(),
2123 D->getInClassInitStyle());
2124 ToField->setAccess(D->getAccess());
2125 ToField->setLexicalDeclContext(LexicalDC);
2126 if (Expr *FromInitializer = D->getInClassInitializer()) {
2127 Expr *ToInitializer = Importer.Import(FromInitializer);
2129 ToField->setInClassInitializer(ToInitializer);
2133 ToField->setImplicit(D->isImplicit());
2134 Importer.Imported(D, ToField);
2135 LexicalDC->addDeclInternal(ToField);
2139 Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2140 // Import the major distinguishing characteristics of a variable.
2141 DeclContext *DC, *LexicalDC;
2142 DeclarationName Name;
2145 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2150 // Determine whether we've already imported this field.
2151 SmallVector<NamedDecl *, 2> FoundDecls;
2152 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2153 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2154 if (IndirectFieldDecl *FoundField
2155 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
2156 // For anonymous indirect fields, match up by index.
2157 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2160 if (Importer.IsStructurallyEquivalent(D->getType(),
2161 FoundField->getType(),
2163 Importer.Imported(D, FoundField);
2167 // If there are more anonymous fields to check, continue.
2168 if (!Name && I < N-1)
2171 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2172 << Name << D->getType() << FoundField->getType();
2173 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2174 << FoundField->getType();
2180 QualType T = Importer.Import(D->getType());
2184 NamedDecl **NamedChain =
2185 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2188 for (auto *PI : D->chain()) {
2189 Decl *D = Importer.Import(PI);
2192 NamedChain[i++] = cast<NamedDecl>(D);
2195 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2196 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
2197 {NamedChain, D->getChainingSize()});
2199 for (const auto *Attr : D->attrs())
2200 ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
2202 ToIndirectField->setAccess(D->getAccess());
2203 ToIndirectField->setLexicalDeclContext(LexicalDC);
2204 Importer.Imported(D, ToIndirectField);
2205 LexicalDC->addDeclInternal(ToIndirectField);
2206 return ToIndirectField;
2209 Decl *ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
2210 // Import the major distinguishing characteristics of a declaration.
2211 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2212 DeclContext *LexicalDC = D->getDeclContext() == D->getLexicalDeclContext()
2213 ? DC : Importer.ImportContext(D->getLexicalDeclContext());
2214 if (!DC || !LexicalDC)
2217 // Determine whether we've already imported this decl.
2218 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
2219 auto *RD = cast<CXXRecordDecl>(DC);
2220 FriendDecl *ImportedFriend = RD->getFirstFriend();
2221 StructuralEquivalenceContext Context(
2222 Importer.getFromContext(), Importer.getToContext(),
2223 Importer.getNonEquivalentDecls(), false, false);
2225 while (ImportedFriend) {
2226 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
2227 if (Context.IsStructurallyEquivalent(D->getFriendDecl(),
2228 ImportedFriend->getFriendDecl()))
2229 return Importer.Imported(D, ImportedFriend);
2231 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
2232 if (Importer.IsStructurallyEquivalent(
2233 D->getFriendType()->getType(),
2234 ImportedFriend->getFriendType()->getType(), true))
2235 return Importer.Imported(D, ImportedFriend);
2237 ImportedFriend = ImportedFriend->getNextFriend();
2240 // Not found. Create it.
2241 FriendDecl::FriendUnion ToFU;
2242 if (NamedDecl *FriendD = D->getFriendDecl())
2243 ToFU = cast_or_null<NamedDecl>(Importer.Import(FriendD));
2245 ToFU = Importer.Import(D->getFriendType());
2249 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
2250 TemplateParameterList **FromTPLists =
2251 D->getTrailingObjects<TemplateParameterList *>();
2252 for (unsigned I = 0; I < D->NumTPLists; I++) {
2253 TemplateParameterList *List = ImportTemplateParameterList(FromTPLists[I]);
2256 ToTPLists[I] = List;
2259 FriendDecl *FrD = FriendDecl::Create(Importer.getToContext(), DC,
2260 Importer.Import(D->getLocation()),
2261 ToFU, Importer.Import(D->getFriendLoc()),
2264 Importer.Imported(D, FrD);
2265 RD->pushFriendDecl(FrD);
2267 FrD->setAccess(D->getAccess());
2268 FrD->setLexicalDeclContext(LexicalDC);
2269 LexicalDC->addDeclInternal(FrD);
2273 Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2274 // Import the major distinguishing characteristics of an ivar.
2275 DeclContext *DC, *LexicalDC;
2276 DeclarationName Name;
2279 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2284 // Determine whether we've already imported this ivar
2285 SmallVector<NamedDecl *, 2> FoundDecls;
2286 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2287 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2288 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
2289 if (Importer.IsStructurallyEquivalent(D->getType(),
2290 FoundIvar->getType())) {
2291 Importer.Imported(D, FoundIvar);
2295 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2296 << Name << D->getType() << FoundIvar->getType();
2297 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2298 << FoundIvar->getType();
2304 QualType T = Importer.Import(D->getType());
2308 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2309 Expr *BitWidth = Importer.Import(D->getBitWidth());
2310 if (!BitWidth && D->getBitWidth())
2313 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2314 cast<ObjCContainerDecl>(DC),
2315 Importer.Import(D->getInnerLocStart()),
2316 Loc, Name.getAsIdentifierInfo(),
2317 T, TInfo, D->getAccessControl(),
2318 BitWidth, D->getSynthesize());
2319 ToIvar->setLexicalDeclContext(LexicalDC);
2320 Importer.Imported(D, ToIvar);
2321 LexicalDC->addDeclInternal(ToIvar);
2326 Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2327 // Import the major distinguishing characteristics of a variable.
2328 DeclContext *DC, *LexicalDC;
2329 DeclarationName Name;
2332 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2337 // Try to find a variable in our own ("to") context with the same name and
2338 // in the same context as the variable we're importing.
2339 if (D->isFileVarDecl()) {
2340 VarDecl *MergeWithVar = nullptr;
2341 SmallVector<NamedDecl *, 4> ConflictingDecls;
2342 unsigned IDNS = Decl::IDNS_Ordinary;
2343 SmallVector<NamedDecl *, 2> FoundDecls;
2344 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2345 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2346 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
2349 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
2350 // We have found a variable that we may need to merge with. Check it.
2351 if (FoundVar->hasExternalFormalLinkage() &&
2352 D->hasExternalFormalLinkage()) {
2353 if (Importer.IsStructurallyEquivalent(D->getType(),
2354 FoundVar->getType())) {
2355 MergeWithVar = FoundVar;
2359 const ArrayType *FoundArray
2360 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2361 const ArrayType *TArray
2362 = Importer.getToContext().getAsArrayType(D->getType());
2363 if (FoundArray && TArray) {
2364 if (isa<IncompleteArrayType>(FoundArray) &&
2365 isa<ConstantArrayType>(TArray)) {
2367 QualType T = Importer.Import(D->getType());
2371 FoundVar->setType(T);
2372 MergeWithVar = FoundVar;
2374 } else if (isa<IncompleteArrayType>(TArray) &&
2375 isa<ConstantArrayType>(FoundArray)) {
2376 MergeWithVar = FoundVar;
2381 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
2382 << Name << D->getType() << FoundVar->getType();
2383 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2384 << FoundVar->getType();
2388 ConflictingDecls.push_back(FoundDecls[I]);
2392 // An equivalent variable with external linkage has been found. Link
2393 // the two declarations, then merge them.
2394 Importer.Imported(D, MergeWithVar);
2396 if (VarDecl *DDef = D->getDefinition()) {
2397 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2398 Importer.ToDiag(ExistingDef->getLocation(),
2399 diag::err_odr_variable_multiple_def)
2401 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2403 Expr *Init = Importer.Import(DDef->getInit());
2404 MergeWithVar->setInit(Init);
2405 if (DDef->isInitKnownICE()) {
2406 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2407 Eval->CheckedICE = true;
2408 Eval->IsICE = DDef->isInitICE();
2413 return MergeWithVar;
2416 if (!ConflictingDecls.empty()) {
2417 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2418 ConflictingDecls.data(),
2419 ConflictingDecls.size());
2426 QualType T = Importer.Import(D->getType());
2430 // Create the imported variable.
2431 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2432 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2433 Importer.Import(D->getInnerLocStart()),
2434 Loc, Name.getAsIdentifierInfo(),
2436 D->getStorageClass());
2437 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
2438 ToVar->setAccess(D->getAccess());
2439 ToVar->setLexicalDeclContext(LexicalDC);
2440 Importer.Imported(D, ToVar);
2441 LexicalDC->addDeclInternal(ToVar);
2443 if (!D->isFileVarDecl() &&
2447 // Merge the initializer.
2448 if (ImportDefinition(D, ToVar))
2451 if (D->isConstexpr())
2452 ToVar->setConstexpr(true);
2457 Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2458 // Parameters are created in the translation unit's context, then moved
2459 // into the function declaration's context afterward.
2460 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2462 // Import the name of this declaration.
2463 DeclarationName Name = Importer.Import(D->getDeclName());
2464 if (D->getDeclName() && !Name)
2467 // Import the location of this declaration.
2468 SourceLocation Loc = Importer.Import(D->getLocation());
2470 // Import the parameter's type.
2471 QualType T = Importer.Import(D->getType());
2475 // Create the imported parameter.
2476 auto *ToParm = ImplicitParamDecl::Create(Importer.getToContext(), DC, Loc,
2477 Name.getAsIdentifierInfo(), T,
2478 D->getParameterKind());
2479 return Importer.Imported(D, ToParm);
2482 Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2483 // Parameters are created in the translation unit's context, then moved
2484 // into the function declaration's context afterward.
2485 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2487 // Import the name of this declaration.
2488 DeclarationName Name = Importer.Import(D->getDeclName());
2489 if (D->getDeclName() && !Name)
2492 // Import the location of this declaration.
2493 SourceLocation Loc = Importer.Import(D->getLocation());
2495 // Import the parameter's type.
2496 QualType T = Importer.Import(D->getType());
2500 // Create the imported parameter.
2501 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2502 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2503 Importer.Import(D->getInnerLocStart()),
2504 Loc, Name.getAsIdentifierInfo(),
2505 T, TInfo, D->getStorageClass(),
2506 /*DefaultArg*/ nullptr);
2508 // Set the default argument.
2509 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
2510 ToParm->setKNRPromoted(D->isKNRPromoted());
2512 Expr *ToDefArg = nullptr;
2513 Expr *FromDefArg = nullptr;
2514 if (D->hasUninstantiatedDefaultArg()) {
2515 FromDefArg = D->getUninstantiatedDefaultArg();
2516 ToDefArg = Importer.Import(FromDefArg);
2517 ToParm->setUninstantiatedDefaultArg(ToDefArg);
2518 } else if (D->hasUnparsedDefaultArg()) {
2519 ToParm->setUnparsedDefaultArg();
2520 } else if (D->hasDefaultArg()) {
2521 FromDefArg = D->getDefaultArg();
2522 ToDefArg = Importer.Import(FromDefArg);
2523 ToParm->setDefaultArg(ToDefArg);
2525 if (FromDefArg && !ToDefArg)
2529 ToParm->setIsUsed();
2531 return Importer.Imported(D, ToParm);
2534 Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2535 // Import the major distinguishing characteristics of a method.
2536 DeclContext *DC, *LexicalDC;
2537 DeclarationName Name;
2540 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2545 SmallVector<NamedDecl *, 2> FoundDecls;
2546 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2547 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2548 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
2549 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2552 // Check return types.
2553 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
2554 FoundMethod->getReturnType())) {
2555 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2556 << D->isInstanceMethod() << Name << D->getReturnType()
2557 << FoundMethod->getReturnType();
2558 Importer.ToDiag(FoundMethod->getLocation(),
2559 diag::note_odr_objc_method_here)
2560 << D->isInstanceMethod() << Name;
2564 // Check the number of parameters.
2565 if (D->param_size() != FoundMethod->param_size()) {
2566 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2567 << D->isInstanceMethod() << Name
2568 << D->param_size() << FoundMethod->param_size();
2569 Importer.ToDiag(FoundMethod->getLocation(),
2570 diag::note_odr_objc_method_here)
2571 << D->isInstanceMethod() << Name;
2575 // Check parameter types.
2576 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2577 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2578 P != PEnd; ++P, ++FoundP) {
2579 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2580 (*FoundP)->getType())) {
2581 Importer.FromDiag((*P)->getLocation(),
2582 diag::err_odr_objc_method_param_type_inconsistent)
2583 << D->isInstanceMethod() << Name
2584 << (*P)->getType() << (*FoundP)->getType();
2585 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2586 << (*FoundP)->getType();
2591 // Check variadic/non-variadic.
2592 // Check the number of parameters.
2593 if (D->isVariadic() != FoundMethod->isVariadic()) {
2594 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2595 << D->isInstanceMethod() << Name;
2596 Importer.ToDiag(FoundMethod->getLocation(),
2597 diag::note_odr_objc_method_here)
2598 << D->isInstanceMethod() << Name;
2602 // FIXME: Any other bits we need to merge?
2603 return Importer.Imported(D, FoundMethod);
2607 // Import the result type.
2608 QualType ResultTy = Importer.Import(D->getReturnType());
2609 if (ResultTy.isNull())
2612 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
2614 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
2615 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
2616 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
2617 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
2618 D->getImplementationControl(), D->hasRelatedResultType());
2620 // FIXME: When we decide to merge method definitions, we'll need to
2621 // deal with implicit parameters.
2623 // Import the parameters
2624 SmallVector<ParmVarDecl *, 5> ToParams;
2625 for (auto *FromP : D->parameters()) {
2626 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
2630 ToParams.push_back(ToP);
2633 // Set the parameters.
2634 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2635 ToParams[I]->setOwningFunction(ToMethod);
2636 ToMethod->addDeclInternal(ToParams[I]);
2638 SmallVector<SourceLocation, 12> SelLocs;
2639 D->getSelectorLocs(SelLocs);
2640 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
2642 ToMethod->setLexicalDeclContext(LexicalDC);
2643 Importer.Imported(D, ToMethod);
2644 LexicalDC->addDeclInternal(ToMethod);
2648 Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
2649 // Import the major distinguishing characteristics of a category.
2650 DeclContext *DC, *LexicalDC;
2651 DeclarationName Name;
2654 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2659 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
2663 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
2664 Importer.getToContext(), DC,
2666 Importer.Import(D->getVarianceLoc()),
2668 Importer.Import(D->getLocation()),
2669 Name.getAsIdentifierInfo(),
2670 Importer.Import(D->getColonLoc()),
2672 Importer.Imported(D, Result);
2673 Result->setLexicalDeclContext(LexicalDC);
2677 Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2678 // Import the major distinguishing characteristics of a category.
2679 DeclContext *DC, *LexicalDC;
2680 DeclarationName Name;
2683 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2688 ObjCInterfaceDecl *ToInterface
2689 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2693 // Determine if we've already encountered this category.
2694 ObjCCategoryDecl *MergeWithCategory
2695 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2696 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2698 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2699 Importer.Import(D->getAtStartLoc()),
2701 Importer.Import(D->getCategoryNameLoc()),
2702 Name.getAsIdentifierInfo(),
2704 /*TypeParamList=*/nullptr,
2705 Importer.Import(D->getIvarLBraceLoc()),
2706 Importer.Import(D->getIvarRBraceLoc()));
2707 ToCategory->setLexicalDeclContext(LexicalDC);
2708 LexicalDC->addDeclInternal(ToCategory);
2709 Importer.Imported(D, ToCategory);
2710 // Import the type parameter list after calling Imported, to avoid
2711 // loops when bringing in their DeclContext.
2712 ToCategory->setTypeParamList(ImportObjCTypeParamList(
2713 D->getTypeParamList()));
2716 SmallVector<ObjCProtocolDecl *, 4> Protocols;
2717 SmallVector<SourceLocation, 4> ProtocolLocs;
2718 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2719 = D->protocol_loc_begin();
2720 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2721 FromProtoEnd = D->protocol_end();
2722 FromProto != FromProtoEnd;
2723 ++FromProto, ++FromProtoLoc) {
2724 ObjCProtocolDecl *ToProto
2725 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2728 Protocols.push_back(ToProto);
2729 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2732 // FIXME: If we're merging, make sure that the protocol list is the same.
2733 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2734 ProtocolLocs.data(), Importer.getToContext());
2737 Importer.Imported(D, ToCategory);
2740 // Import all of the members of this category.
2741 ImportDeclContext(D);
2743 // If we have an implementation, import it as well.
2744 if (D->getImplementation()) {
2745 ObjCCategoryImplDecl *Impl
2746 = cast_or_null<ObjCCategoryImplDecl>(
2747 Importer.Import(D->getImplementation()));
2751 ToCategory->setImplementation(Impl);
2757 bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
2758 ObjCProtocolDecl *To,
2759 ImportDefinitionKind Kind) {
2760 if (To->getDefinition()) {
2761 if (shouldForceImportDeclContext(Kind))
2762 ImportDeclContext(From);
2766 // Start the protocol definition
2767 To->startDefinition();
2770 SmallVector<ObjCProtocolDecl *, 4> Protocols;
2771 SmallVector<SourceLocation, 4> ProtocolLocs;
2772 ObjCProtocolDecl::protocol_loc_iterator
2773 FromProtoLoc = From->protocol_loc_begin();
2774 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
2775 FromProtoEnd = From->protocol_end();
2776 FromProto != FromProtoEnd;
2777 ++FromProto, ++FromProtoLoc) {
2778 ObjCProtocolDecl *ToProto
2779 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2782 Protocols.push_back(ToProto);
2783 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2786 // FIXME: If we're merging, make sure that the protocol list is the same.
2787 To->setProtocolList(Protocols.data(), Protocols.size(),
2788 ProtocolLocs.data(), Importer.getToContext());
2790 if (shouldForceImportDeclContext(Kind)) {
2791 // Import all of the members of this protocol.
2792 ImportDeclContext(From, /*ForceImport=*/true);
2797 Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
2798 // If this protocol has a definition in the translation unit we're coming
2799 // from, but this particular declaration is not that definition, import the
2800 // definition and map to that.
2801 ObjCProtocolDecl *Definition = D->getDefinition();
2802 if (Definition && Definition != D) {
2803 Decl *ImportedDef = Importer.Import(Definition);
2807 return Importer.Imported(D, ImportedDef);
2810 // Import the major distinguishing characteristics of a protocol.
2811 DeclContext *DC, *LexicalDC;
2812 DeclarationName Name;
2815 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2820 ObjCProtocolDecl *MergeWithProtocol = nullptr;
2821 SmallVector<NamedDecl *, 2> FoundDecls;
2822 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2823 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2824 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2827 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
2831 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2833 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
2834 Name.getAsIdentifierInfo(), Loc,
2835 Importer.Import(D->getAtStartLoc()),
2836 /*PrevDecl=*/nullptr);
2837 ToProto->setLexicalDeclContext(LexicalDC);
2838 LexicalDC->addDeclInternal(ToProto);
2841 Importer.Imported(D, ToProto);
2843 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
2849 Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
2850 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2851 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
2853 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
2854 SourceLocation LangLoc = Importer.Import(D->getLocation());
2856 bool HasBraces = D->hasBraces();
2858 LinkageSpecDecl *ToLinkageSpec =
2859 LinkageSpecDecl::Create(Importer.getToContext(),
2867 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
2868 ToLinkageSpec->setRBraceLoc(RBraceLoc);
2871 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
2872 LexicalDC->addDeclInternal(ToLinkageSpec);
2874 Importer.Imported(D, ToLinkageSpec);
2876 return ToLinkageSpec;
2879 bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
2880 ObjCInterfaceDecl *To,
2881 ImportDefinitionKind Kind) {
2882 if (To->getDefinition()) {
2883 // Check consistency of superclass.
2884 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
2886 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
2891 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
2892 if ((bool)FromSuper != (bool)ToSuper ||
2893 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
2894 Importer.ToDiag(To->getLocation(),
2895 diag::err_odr_objc_superclass_inconsistent)
2896 << To->getDeclName();
2898 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
2899 << To->getSuperClass()->getDeclName();
2901 Importer.ToDiag(To->getLocation(),
2902 diag::note_odr_objc_missing_superclass);
2903 if (From->getSuperClass())
2904 Importer.FromDiag(From->getSuperClassLoc(),
2905 diag::note_odr_objc_superclass)
2906 << From->getSuperClass()->getDeclName();
2908 Importer.FromDiag(From->getLocation(),
2909 diag::note_odr_objc_missing_superclass);
2912 if (shouldForceImportDeclContext(Kind))
2913 ImportDeclContext(From);
2917 // Start the definition.
2918 To->startDefinition();
2920 // If this class has a superclass, import it.
2921 if (From->getSuperClass()) {
2922 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
2926 To->setSuperClass(SuperTInfo);
2930 SmallVector<ObjCProtocolDecl *, 4> Protocols;
2931 SmallVector<SourceLocation, 4> ProtocolLocs;
2932 ObjCInterfaceDecl::protocol_loc_iterator
2933 FromProtoLoc = From->protocol_loc_begin();
2935 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
2936 FromProtoEnd = From->protocol_end();
2937 FromProto != FromProtoEnd;
2938 ++FromProto, ++FromProtoLoc) {
2939 ObjCProtocolDecl *ToProto
2940 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2943 Protocols.push_back(ToProto);
2944 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2947 // FIXME: If we're merging, make sure that the protocol list is the same.
2948 To->setProtocolList(Protocols.data(), Protocols.size(),
2949 ProtocolLocs.data(), Importer.getToContext());
2951 // Import categories. When the categories themselves are imported, they'll
2952 // hook themselves into this interface.
2953 for (auto *Cat : From->known_categories())
2954 Importer.Import(Cat);
2956 // If we have an @implementation, import it as well.
2957 if (From->getImplementation()) {
2958 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
2959 Importer.Import(From->getImplementation()));
2963 To->setImplementation(Impl);
2966 if (shouldForceImportDeclContext(Kind)) {
2967 // Import all of the members of this class.
2968 ImportDeclContext(From, /*ForceImport=*/true);
2974 ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
2978 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
2979 for (auto fromTypeParam : *list) {
2980 auto toTypeParam = cast_or_null<ObjCTypeParamDecl>(
2981 Importer.Import(fromTypeParam));
2985 toTypeParams.push_back(toTypeParam);
2988 return ObjCTypeParamList::create(Importer.getToContext(),
2989 Importer.Import(list->getLAngleLoc()),
2991 Importer.Import(list->getRAngleLoc()));
2994 Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2995 // If this class has a definition in the translation unit we're coming from,
2996 // but this particular declaration is not that definition, import the
2997 // definition and map to that.
2998 ObjCInterfaceDecl *Definition = D->getDefinition();
2999 if (Definition && Definition != D) {
3000 Decl *ImportedDef = Importer.Import(Definition);
3004 return Importer.Imported(D, ImportedDef);
3007 // Import the major distinguishing characteristics of an @interface.
3008 DeclContext *DC, *LexicalDC;
3009 DeclarationName Name;
3012 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3017 // Look for an existing interface with the same name.
3018 ObjCInterfaceDecl *MergeWithIface = nullptr;
3019 SmallVector<NamedDecl *, 2> FoundDecls;
3020 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3021 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3022 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3025 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
3029 // Create an interface declaration, if one does not already exist.
3030 ObjCInterfaceDecl *ToIface = MergeWithIface;
3032 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3033 Importer.Import(D->getAtStartLoc()),
3034 Name.getAsIdentifierInfo(),
3035 /*TypeParamList=*/nullptr,
3036 /*PrevDecl=*/nullptr, Loc,
3037 D->isImplicitInterfaceDecl());
3038 ToIface->setLexicalDeclContext(LexicalDC);
3039 LexicalDC->addDeclInternal(ToIface);
3041 Importer.Imported(D, ToIface);
3042 // Import the type parameter list after calling Imported, to avoid
3043 // loops when bringing in their DeclContext.
3044 ToIface->setTypeParamList(ImportObjCTypeParamList(
3045 D->getTypeParamListAsWritten()));
3047 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
3053 Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3054 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3055 Importer.Import(D->getCategoryDecl()));
3059 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3061 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3065 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
3066 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
3067 Importer.Import(D->getIdentifier()),
3068 Category->getClassInterface(),
3069 Importer.Import(D->getLocation()),
3070 Importer.Import(D->getAtStartLoc()),
3073 DeclContext *LexicalDC = DC;
3074 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3075 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3079 ToImpl->setLexicalDeclContext(LexicalDC);
3082 LexicalDC->addDeclInternal(ToImpl);
3083 Category->setImplementation(ToImpl);
3086 Importer.Imported(D, ToImpl);
3087 ImportDeclContext(D);
3091 Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3092 // Find the corresponding interface.
3093 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3094 Importer.Import(D->getClassInterface()));
3098 // Import the superclass, if any.
3099 ObjCInterfaceDecl *Super = nullptr;
3100 if (D->getSuperClass()) {
3101 Super = cast_or_null<ObjCInterfaceDecl>(
3102 Importer.Import(D->getSuperClass()));
3107 ObjCImplementationDecl *Impl = Iface->getImplementation();
3109 // We haven't imported an implementation yet. Create a new @implementation
3111 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3112 Importer.ImportContext(D->getDeclContext()),
3114 Importer.Import(D->getLocation()),
3115 Importer.Import(D->getAtStartLoc()),
3116 Importer.Import(D->getSuperClassLoc()),
3117 Importer.Import(D->getIvarLBraceLoc()),
3118 Importer.Import(D->getIvarRBraceLoc()));
3120 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3121 DeclContext *LexicalDC
3122 = Importer.ImportContext(D->getLexicalDeclContext());
3125 Impl->setLexicalDeclContext(LexicalDC);
3128 // Associate the implementation with the class it implements.
3129 Iface->setImplementation(Impl);
3130 Importer.Imported(D, Iface->getImplementation());
3132 Importer.Imported(D, Iface->getImplementation());
3134 // Verify that the existing @implementation has the same superclass.
3135 if ((Super && !Impl->getSuperClass()) ||
3136 (!Super && Impl->getSuperClass()) ||
3137 (Super && Impl->getSuperClass() &&
3138 !declaresSameEntity(Super->getCanonicalDecl(),
3139 Impl->getSuperClass()))) {
3140 Importer.ToDiag(Impl->getLocation(),
3141 diag::err_odr_objc_superclass_inconsistent)
3142 << Iface->getDeclName();
3143 // FIXME: It would be nice to have the location of the superclass
3145 if (Impl->getSuperClass())
3146 Importer.ToDiag(Impl->getLocation(),
3147 diag::note_odr_objc_superclass)
3148 << Impl->getSuperClass()->getDeclName();
3150 Importer.ToDiag(Impl->getLocation(),
3151 diag::note_odr_objc_missing_superclass);
3152 if (D->getSuperClass())
3153 Importer.FromDiag(D->getLocation(),
3154 diag::note_odr_objc_superclass)
3155 << D->getSuperClass()->getDeclName();
3157 Importer.FromDiag(D->getLocation(),
3158 diag::note_odr_objc_missing_superclass);
3163 // Import all of the members of this @implementation.
3164 ImportDeclContext(D);
3169 Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3170 // Import the major distinguishing characteristics of an @property.
3171 DeclContext *DC, *LexicalDC;
3172 DeclarationName Name;
3175 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3180 // Check whether we have already imported this property.
3181 SmallVector<NamedDecl *, 2> FoundDecls;
3182 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3183 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3184 if (ObjCPropertyDecl *FoundProp
3185 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
3186 // Check property types.
3187 if (!Importer.IsStructurallyEquivalent(D->getType(),
3188 FoundProp->getType())) {
3189 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3190 << Name << D->getType() << FoundProp->getType();
3191 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3192 << FoundProp->getType();
3196 // FIXME: Check property attributes, getters, setters, etc.?
3198 // Consider these properties to be equivalent.
3199 Importer.Imported(D, FoundProp);
3205 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
3209 // Create the new property.
3210 ObjCPropertyDecl *ToProperty
3211 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3212 Name.getAsIdentifierInfo(),
3213 Importer.Import(D->getAtLoc()),
3214 Importer.Import(D->getLParenLoc()),
3215 Importer.Import(D->getType()),
3217 D->getPropertyImplementation());
3218 Importer.Imported(D, ToProperty);
3219 ToProperty->setLexicalDeclContext(LexicalDC);
3220 LexicalDC->addDeclInternal(ToProperty);
3222 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
3223 ToProperty->setPropertyAttributesAsWritten(
3224 D->getPropertyAttributesAsWritten());
3225 ToProperty->setGetterName(Importer.Import(D->getGetterName()),
3226 Importer.Import(D->getGetterNameLoc()));
3227 ToProperty->setSetterName(Importer.Import(D->getSetterName()),
3228 Importer.Import(D->getSetterNameLoc()));
3229 ToProperty->setGetterMethodDecl(
3230 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3231 ToProperty->setSetterMethodDecl(
3232 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3233 ToProperty->setPropertyIvarDecl(
3234 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3238 Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3239 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3240 Importer.Import(D->getPropertyDecl()));
3244 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3248 // Import the lexical declaration context.
3249 DeclContext *LexicalDC = DC;
3250 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3251 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3256 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3260 // Import the ivar (for an @synthesize).
3261 ObjCIvarDecl *Ivar = nullptr;
3262 if (D->getPropertyIvarDecl()) {
3263 Ivar = cast_or_null<ObjCIvarDecl>(
3264 Importer.Import(D->getPropertyIvarDecl()));
3269 ObjCPropertyImplDecl *ToImpl
3270 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
3271 Property->getQueryKind());
3273 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3274 Importer.Import(D->getLocStart()),
3275 Importer.Import(D->getLocation()),
3277 D->getPropertyImplementation(),
3279 Importer.Import(D->getPropertyIvarDeclLoc()));
3280 ToImpl->setLexicalDeclContext(LexicalDC);
3281 Importer.Imported(D, ToImpl);
3282 LexicalDC->addDeclInternal(ToImpl);
3284 // Check that we have the same kind of property implementation (@synthesize
3286 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3287 Importer.ToDiag(ToImpl->getLocation(),
3288 diag::err_odr_objc_property_impl_kind_inconsistent)
3289 << Property->getDeclName()
3290 << (ToImpl->getPropertyImplementation()
3291 == ObjCPropertyImplDecl::Dynamic);
3292 Importer.FromDiag(D->getLocation(),
3293 diag::note_odr_objc_property_impl_kind)
3294 << D->getPropertyDecl()->getDeclName()
3295 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3299 // For @synthesize, check that we have the same
3300 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3301 Ivar != ToImpl->getPropertyIvarDecl()) {
3302 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3303 diag::err_odr_objc_synthesize_ivar_inconsistent)
3304 << Property->getDeclName()
3305 << ToImpl->getPropertyIvarDecl()->getDeclName()
3306 << Ivar->getDeclName();
3307 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3308 diag::note_odr_objc_synthesize_ivar_here)
3309 << D->getPropertyIvarDecl()->getDeclName();
3313 // Merge the existing implementation with the new implementation.
3314 Importer.Imported(D, ToImpl);
3320 Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3321 // For template arguments, we adopt the translation unit as our declaration
3322 // context. This context will be fixed when the actual template declaration
3325 // FIXME: Import default argument.
3326 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3327 Importer.getToContext().getTranslationUnitDecl(),
3328 Importer.Import(D->getLocStart()),
3329 Importer.Import(D->getLocation()),
3332 Importer.Import(D->getIdentifier()),
3333 D->wasDeclaredWithTypename(),
3334 D->isParameterPack());
3338 ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3339 // Import the name of this declaration.
3340 DeclarationName Name = Importer.Import(D->getDeclName());
3341 if (D->getDeclName() && !Name)
3344 // Import the location of this declaration.
3345 SourceLocation Loc = Importer.Import(D->getLocation());
3347 // Import the type of this declaration.
3348 QualType T = Importer.Import(D->getType());
3352 // Import type-source information.
3353 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3354 if (D->getTypeSourceInfo() && !TInfo)
3357 // FIXME: Import default argument.
3359 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3360 Importer.getToContext().getTranslationUnitDecl(),
3361 Importer.Import(D->getInnerLocStart()),
3362 Loc, D->getDepth(), D->getPosition(),
3363 Name.getAsIdentifierInfo(),
3364 T, D->isParameterPack(), TInfo);
3368 ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3369 // Import the name of this declaration.
3370 DeclarationName Name = Importer.Import(D->getDeclName());
3371 if (D->getDeclName() && !Name)
3374 // Import the location of this declaration.
3375 SourceLocation Loc = Importer.Import(D->getLocation());
3377 // Import template parameters.
3378 TemplateParameterList *TemplateParams
3379 = ImportTemplateParameterList(D->getTemplateParameters());
3380 if (!TemplateParams)
3383 // FIXME: Import default argument.
3385 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3386 Importer.getToContext().getTranslationUnitDecl(),
3387 Loc, D->getDepth(), D->getPosition(),
3388 D->isParameterPack(),
3389 Name.getAsIdentifierInfo(),
3393 Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3394 // If this record has a definition in the translation unit we're coming from,
3395 // but this particular declaration is not that definition, import the
3396 // definition and map to that.
3397 CXXRecordDecl *Definition
3398 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3399 if (Definition && Definition != D->getTemplatedDecl()) {
3401 = Importer.Import(Definition->getDescribedClassTemplate());
3405 return Importer.Imported(D, ImportedDef);
3408 // Import the major distinguishing characteristics of this class template.
3409 DeclContext *DC, *LexicalDC;
3410 DeclarationName Name;
3413 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3418 // We may already have a template of the same name; try to find and match it.
3419 if (!DC->isFunctionOrMethod()) {
3420 SmallVector<NamedDecl *, 4> ConflictingDecls;
3421 SmallVector<NamedDecl *, 2> FoundDecls;
3422 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3423 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3424 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3427 Decl *Found = FoundDecls[I];
3428 if (ClassTemplateDecl *FoundTemplate
3429 = dyn_cast<ClassTemplateDecl>(Found)) {
3430 if (IsStructuralMatch(D, FoundTemplate)) {
3431 // The class templates structurally match; call it the same template.
3432 // FIXME: We may be filling in a forward declaration here. Handle
3434 Importer.Imported(D->getTemplatedDecl(),
3435 FoundTemplate->getTemplatedDecl());
3436 return Importer.Imported(D, FoundTemplate);
3440 ConflictingDecls.push_back(FoundDecls[I]);
3443 if (!ConflictingDecls.empty()) {
3444 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3445 ConflictingDecls.data(),
3446 ConflictingDecls.size());
3453 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3455 // Create the declaration that is being templated.
3456 // Create the declaration that is being templated.
3457 CXXRecordDecl *D2Templated = cast_or_null<CXXRecordDecl>(
3458 Importer.Import(DTemplated));
3462 // Resolve possible cyclic import.
3463 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
3464 return AlreadyImported;
3466 // Create the class template declaration itself.
3467 TemplateParameterList *TemplateParams
3468 = ImportTemplateParameterList(D->getTemplateParameters());
3469 if (!TemplateParams)
3472 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3473 Loc, Name, TemplateParams,
3475 D2Templated->setDescribedClassTemplate(D2);
3477 D2->setAccess(D->getAccess());
3478 D2->setLexicalDeclContext(LexicalDC);
3479 LexicalDC->addDeclInternal(D2);
3481 // Note the relationship between the class templates.
3482 Importer.Imported(D, D2);
3483 Importer.Imported(DTemplated, D2Templated);
3485 if (DTemplated->isCompleteDefinition() &&
3486 !D2Templated->isCompleteDefinition()) {
3487 // FIXME: Import definition!
3493 Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3494 ClassTemplateSpecializationDecl *D) {
3495 // If this record has a definition in the translation unit we're coming from,
3496 // but this particular declaration is not that definition, import the
3497 // definition and map to that.
3498 TagDecl *Definition = D->getDefinition();
3499 if (Definition && Definition != D) {
3500 Decl *ImportedDef = Importer.Import(Definition);
3504 return Importer.Imported(D, ImportedDef);
3507 ClassTemplateDecl *ClassTemplate
3508 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3509 D->getSpecializedTemplate()));
3513 // Import the context of this declaration.
3514 DeclContext *DC = ClassTemplate->getDeclContext();
3518 DeclContext *LexicalDC = DC;
3519 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3520 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3525 // Import the location of this declaration.
3526 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3527 SourceLocation IdLoc = Importer.Import(D->getLocation());
3529 // Import template arguments.
3530 SmallVector<TemplateArgument, 2> TemplateArgs;
3531 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3532 D->getTemplateArgs().size(),
3536 // Try to find an existing specialization with these template arguments.
3537 void *InsertPos = nullptr;
3538 ClassTemplateSpecializationDecl *D2
3539 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
3541 // We already have a class template specialization with these template
3544 // FIXME: Check for specialization vs. instantiation errors.
3546 if (RecordDecl *FoundDef = D2->getDefinition()) {
3547 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
3548 // The record types structurally match, or the "from" translation
3549 // unit only had a forward declaration anyway; call it the same
3551 return Importer.Imported(D, FoundDef);
3555 // Create a new specialization.
3556 if (ClassTemplatePartialSpecializationDecl *PartialSpec =
3557 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
3559 // Import TemplateArgumentListInfo
3560 TemplateArgumentListInfo ToTAInfo;
3561 auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
3562 for (unsigned I = 0, E = ASTTemplateArgs.NumTemplateArgs; I < E; ++I) {
3564 auto ToLoc = ImportTemplateArgumentLoc(ASTTemplateArgs[I], Error);
3567 ToTAInfo.addArgument(ToLoc);
3570 QualType CanonInjType = Importer.Import(
3571 PartialSpec->getInjectedSpecializationType());
3572 if (CanonInjType.isNull())
3574 CanonInjType = CanonInjType.getCanonicalType();
3576 TemplateParameterList *ToTPList = ImportTemplateParameterList(
3577 PartialSpec->getTemplateParameters());
3578 if (!ToTPList && PartialSpec->getTemplateParameters())
3581 D2 = ClassTemplatePartialSpecializationDecl::Create(
3582 Importer.getToContext(), D->getTagKind(), DC, StartLoc, IdLoc,
3583 ToTPList, ClassTemplate,
3584 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
3585 ToTAInfo, CanonInjType, nullptr);
3588 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3589 D->getTagKind(), DC,
3593 /*PrevDecl=*/nullptr);
3596 D2->setSpecializationKind(D->getSpecializationKind());
3598 // Add this specialization to the class template.
3599 ClassTemplate->AddSpecialization(D2, InsertPos);
3601 // Import the qualifier, if any.
3602 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
3604 Importer.Imported(D, D2);
3606 if (auto *TSI = D->getTypeAsWritten()) {
3607 TypeSourceInfo *TInfo = Importer.Import(TSI);
3610 D2->setTypeAsWritten(TInfo);
3611 D2->setTemplateKeywordLoc(Importer.Import(D->getTemplateKeywordLoc()));
3612 D2->setExternLoc(Importer.Import(D->getExternLoc()));
3615 SourceLocation POI = Importer.Import(D->getPointOfInstantiation());
3617 D2->setPointOfInstantiation(POI);
3618 else if (D->getPointOfInstantiation().isValid())
3621 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
3623 // Add the specialization to this context.
3624 D2->setLexicalDeclContext(LexicalDC);
3625 LexicalDC->addDeclInternal(D2);
3627 Importer.Imported(D, D2);
3628 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
3634 Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
3635 // If this variable has a definition in the translation unit we're coming
3637 // but this particular declaration is not that definition, import the
3638 // definition and map to that.
3639 VarDecl *Definition =
3640 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
3641 if (Definition && Definition != D->getTemplatedDecl()) {
3642 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
3646 return Importer.Imported(D, ImportedDef);
3649 // Import the major distinguishing characteristics of this variable template.
3650 DeclContext *DC, *LexicalDC;
3651 DeclarationName Name;
3654 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3659 // We may already have a template of the same name; try to find and match it.
3660 assert(!DC->isFunctionOrMethod() &&
3661 "Variable templates cannot be declared at function scope");
3662 SmallVector<NamedDecl *, 4> ConflictingDecls;
3663 SmallVector<NamedDecl *, 2> FoundDecls;
3664 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3665 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3666 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3669 Decl *Found = FoundDecls[I];
3670 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
3671 if (IsStructuralMatch(D, FoundTemplate)) {
3672 // The variable templates structurally match; call it the same template.
3673 Importer.Imported(D->getTemplatedDecl(),
3674 FoundTemplate->getTemplatedDecl());
3675 return Importer.Imported(D, FoundTemplate);
3679 ConflictingDecls.push_back(FoundDecls[I]);
3682 if (!ConflictingDecls.empty()) {
3683 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3684 ConflictingDecls.data(),
3685 ConflictingDecls.size());
3691 VarDecl *DTemplated = D->getTemplatedDecl();
3694 QualType T = Importer.Import(DTemplated->getType());
3698 // Create the declaration that is being templated.
3699 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3700 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
3701 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo());
3702 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc,
3703 IdLoc, Name.getAsIdentifierInfo(), T,
3704 TInfo, DTemplated->getStorageClass());
3705 D2Templated->setAccess(DTemplated->getAccess());
3706 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
3707 D2Templated->setLexicalDeclContext(LexicalDC);
3709 // Importer.Imported(DTemplated, D2Templated);
3710 // LexicalDC->addDeclInternal(D2Templated);
3712 // Merge the initializer.
3713 if (ImportDefinition(DTemplated, D2Templated))
3716 // Create the variable template declaration itself.
3717 TemplateParameterList *TemplateParams =
3718 ImportTemplateParameterList(D->getTemplateParameters());
3719 if (!TemplateParams)
3722 VarTemplateDecl *D2 = VarTemplateDecl::Create(
3723 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated);
3724 D2Templated->setDescribedVarTemplate(D2);
3726 D2->setAccess(D->getAccess());
3727 D2->setLexicalDeclContext(LexicalDC);
3728 LexicalDC->addDeclInternal(D2);
3730 // Note the relationship between the variable templates.
3731 Importer.Imported(D, D2);
3732 Importer.Imported(DTemplated, D2Templated);
3734 if (DTemplated->isThisDeclarationADefinition() &&
3735 !D2Templated->isThisDeclarationADefinition()) {
3736 // FIXME: Import definition!
3742 Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
3743 VarTemplateSpecializationDecl *D) {
3744 // If this record has a definition in the translation unit we're coming from,
3745 // but this particular declaration is not that definition, import the
3746 // definition and map to that.
3747 VarDecl *Definition = D->getDefinition();
3748 if (Definition && Definition != D) {
3749 Decl *ImportedDef = Importer.Import(Definition);
3753 return Importer.Imported(D, ImportedDef);
3756 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
3757 Importer.Import(D->getSpecializedTemplate()));
3761 // Import the context of this declaration.
3762 DeclContext *DC = VarTemplate->getDeclContext();
3766 DeclContext *LexicalDC = DC;
3767 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3768 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3773 // Import the location of this declaration.
3774 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3775 SourceLocation IdLoc = Importer.Import(D->getLocation());
3777 // Import template arguments.
3778 SmallVector<TemplateArgument, 2> TemplateArgs;
3779 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3780 D->getTemplateArgs().size(), TemplateArgs))
3783 // Try to find an existing specialization with these template arguments.
3784 void *InsertPos = nullptr;
3785 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
3786 TemplateArgs, InsertPos);
3788 // We already have a variable template specialization with these template
3791 // FIXME: Check for specialization vs. instantiation errors.
3793 if (VarDecl *FoundDef = D2->getDefinition()) {
3794 if (!D->isThisDeclarationADefinition() ||
3795 IsStructuralMatch(D, FoundDef)) {
3796 // The record types structurally match, or the "from" translation
3797 // unit only had a forward declaration anyway; call it the same
3799 return Importer.Imported(D, FoundDef);
3805 QualType T = Importer.Import(D->getType());
3808 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3810 // Create a new specialization.
3811 D2 = VarTemplateSpecializationDecl::Create(
3812 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
3813 D->getStorageClass(), TemplateArgs);
3814 D2->setSpecializationKind(D->getSpecializationKind());
3815 D2->setTemplateArgsInfo(D->getTemplateArgsInfo());
3817 // Add this specialization to the class template.
3818 VarTemplate->AddSpecialization(D2, InsertPos);
3820 // Import the qualifier, if any.
3821 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
3823 // Add the specialization to this context.
3824 D2->setLexicalDeclContext(LexicalDC);
3825 LexicalDC->addDeclInternal(D2);
3827 Importer.Imported(D, D2);
3829 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2))
3835 //----------------------------------------------------------------------------
3836 // Import Statements
3837 //----------------------------------------------------------------------------
3839 DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
3841 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
3842 size_t NumDecls = DG.end() - DG.begin();
3843 SmallVector<Decl *, 1> ToDecls(NumDecls);
3844 auto &_Importer = this->Importer;
3845 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
3846 [&_Importer](Decl *D) -> Decl * {
3847 return _Importer.Import(D);
3849 return DeclGroupRef::Create(Importer.getToContext(),
3854 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3855 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3856 << S->getStmtClassName();
3861 Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
3862 SmallVector<IdentifierInfo *, 4> Names;
3863 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
3864 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
3865 // ToII is nullptr when no symbolic name is given for output operand
3866 // see ParseStmtAsm::ParseAsmOperandsOpt
3867 if (!ToII && S->getOutputIdentifier(I))
3869 Names.push_back(ToII);
3871 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
3872 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
3873 // ToII is nullptr when no symbolic name is given for input operand
3874 // see ParseStmtAsm::ParseAsmOperandsOpt
3875 if (!ToII && S->getInputIdentifier(I))
3877 Names.push_back(ToII);
3880 SmallVector<StringLiteral *, 4> Clobbers;
3881 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
3882 StringLiteral *Clobber = cast_or_null<StringLiteral>(
3883 Importer.Import(S->getClobberStringLiteral(I)));
3886 Clobbers.push_back(Clobber);
3889 SmallVector<StringLiteral *, 4> Constraints;
3890 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
3891 StringLiteral *Output = cast_or_null<StringLiteral>(
3892 Importer.Import(S->getOutputConstraintLiteral(I)));
3895 Constraints.push_back(Output);
3898 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
3899 StringLiteral *Input = cast_or_null<StringLiteral>(
3900 Importer.Import(S->getInputConstraintLiteral(I)));
3903 Constraints.push_back(Input);
3906 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
3907 if (ImportContainerChecked(S->outputs(), Exprs))
3910 if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
3913 StringLiteral *AsmStr = cast_or_null<StringLiteral>(
3914 Importer.Import(S->getAsmString()));
3918 return new (Importer.getToContext()) GCCAsmStmt(
3919 Importer.getToContext(),
3920 Importer.Import(S->getAsmLoc()),
3929 S->getNumClobbers(),
3931 Importer.Import(S->getRParenLoc()));
3934 Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
3935 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
3936 for (Decl *ToD : ToDG) {
3940 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
3941 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
3942 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
3945 Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
3946 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
3947 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
3948 S->hasLeadingEmptyMacro());
3951 Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
3952 llvm::SmallVector<Stmt *, 8> ToStmts(S->size());
3954 if (ImportContainerChecked(S->body(), ToStmts))
3957 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
3958 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
3959 return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(),
3961 ToLBraceLoc, ToRBraceLoc);
3964 Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
3965 Expr *ToLHS = Importer.Import(S->getLHS());
3968 Expr *ToRHS = Importer.Import(S->getRHS());
3969 if (!ToRHS && S->getRHS())
3971 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
3972 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
3973 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
3974 return new (Importer.getToContext()) CaseStmt(ToLHS, ToRHS,
3975 ToCaseLoc, ToEllipsisLoc,
3979 Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
3980 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
3981 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
3982 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
3983 if (!ToSubStmt && S->getSubStmt())
3985 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
3989 Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
3990 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
3991 LabelDecl *ToLabelDecl =
3992 cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
3993 if (!ToLabelDecl && S->getDecl())
3995 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
3996 if (!ToSubStmt && S->getSubStmt())
3998 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4002 Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4003 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4004 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4005 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
4006 ASTContext &_ToContext = Importer.getToContext();
4007 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
4008 [&_ToContext](const Attr *A) -> const Attr * {
4009 return A->clone(_ToContext);
4011 for (const Attr *ToA : ToAttrs) {
4015 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4016 if (!ToSubStmt && S->getSubStmt())
4018 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4019 ToAttrs, ToSubStmt);
4022 Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4023 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
4024 Stmt *ToInit = Importer.Import(S->getInit());
4025 if (!ToInit && S->getInit())
4027 VarDecl *ToConditionVariable = nullptr;
4028 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4029 ToConditionVariable =
4030 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4031 if (!ToConditionVariable)
4034 Expr *ToCondition = Importer.Import(S->getCond());
4035 if (!ToCondition && S->getCond())
4037 Stmt *ToThenStmt = Importer.Import(S->getThen());
4038 if (!ToThenStmt && S->getThen())
4040 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4041 Stmt *ToElseStmt = Importer.Import(S->getElse());
4042 if (!ToElseStmt && S->getElse())
4044 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
4045 ToIfLoc, S->isConstexpr(),
4047 ToConditionVariable,
4048 ToCondition, ToThenStmt,
4049 ToElseLoc, ToElseStmt);
4052 Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
4053 Stmt *ToInit = Importer.Import(S->getInit());
4054 if (!ToInit && S->getInit())
4056 VarDecl *ToConditionVariable = nullptr;
4057 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4058 ToConditionVariable =
4059 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4060 if (!ToConditionVariable)
4063 Expr *ToCondition = Importer.Import(S->getCond());
4064 if (!ToCondition && S->getCond())
4066 SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt(
4067 Importer.getToContext(), ToInit,
4068 ToConditionVariable, ToCondition);
4069 Stmt *ToBody = Importer.Import(S->getBody());
4070 if (!ToBody && S->getBody())
4072 ToStmt->setBody(ToBody);
4073 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4074 // Now we have to re-chain the cases.
4075 SwitchCase *LastChainedSwitchCase = nullptr;
4076 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4077 SC = SC->getNextSwitchCase()) {
4078 SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
4081 if (LastChainedSwitchCase)
4082 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4084 ToStmt->setSwitchCaseList(ToSC);
4085 LastChainedSwitchCase = ToSC;
4090 Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
4091 VarDecl *ToConditionVariable = nullptr;
4092 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4093 ToConditionVariable =
4094 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4095 if (!ToConditionVariable)
4098 Expr *ToCondition = Importer.Import(S->getCond());
4099 if (!ToCondition && S->getCond())
4101 Stmt *ToBody = Importer.Import(S->getBody());
4102 if (!ToBody && S->getBody())
4104 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4105 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
4106 ToConditionVariable,
4107 ToCondition, ToBody,
4111 Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
4112 Stmt *ToBody = Importer.Import(S->getBody());
4113 if (!ToBody && S->getBody())
4115 Expr *ToCondition = Importer.Import(S->getCond());
4116 if (!ToCondition && S->getCond())
4118 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
4119 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4120 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4121 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
4122 ToDoLoc, ToWhileLoc,
4126 Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
4127 Stmt *ToInit = Importer.Import(S->getInit());
4128 if (!ToInit && S->getInit())
4130 Expr *ToCondition = Importer.Import(S->getCond());
4131 if (!ToCondition && S->getCond())
4133 VarDecl *ToConditionVariable = nullptr;
4134 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4135 ToConditionVariable =
4136 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4137 if (!ToConditionVariable)
4140 Expr *ToInc = Importer.Import(S->getInc());
4141 if (!ToInc && S->getInc())
4143 Stmt *ToBody = Importer.Import(S->getBody());
4144 if (!ToBody && S->getBody())
4146 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4147 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
4148 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4149 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
4150 ToInit, ToCondition,
4151 ToConditionVariable,
4153 ToForLoc, ToLParenLoc,
4157 Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
4158 LabelDecl *ToLabel = nullptr;
4159 if (LabelDecl *FromLabel = S->getLabel()) {
4160 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
4164 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4165 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
4166 return new (Importer.getToContext()) GotoStmt(ToLabel,
4167 ToGotoLoc, ToLabelLoc);
4170 Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
4171 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4172 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
4173 Expr *ToTarget = Importer.Import(S->getTarget());
4174 if (!ToTarget && S->getTarget())
4176 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
4180 Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
4181 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
4182 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
4185 Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
4186 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
4187 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
4190 Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
4191 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
4192 Expr *ToRetExpr = Importer.Import(S->getRetValue());
4193 if (!ToRetExpr && S->getRetValue())
4195 VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate());
4196 VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
4197 if (!ToNRVOCandidate && NRVOCandidate)
4199 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
4203 Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
4204 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
4205 VarDecl *ToExceptionDecl = nullptr;
4206 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
4208 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4209 if (!ToExceptionDecl)
4212 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
4213 if (!ToHandlerBlock && S->getHandlerBlock())
4215 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
4220 Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
4221 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
4222 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
4223 if (!ToTryBlock && S->getTryBlock())
4225 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
4226 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
4227 CXXCatchStmt *FromHandler = S->getHandler(HI);
4228 if (Stmt *ToHandler = Importer.Import(FromHandler))
4229 ToHandlers[HI] = ToHandler;
4233 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
4237 Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
4239 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
4240 if (!ToRange && S->getRangeStmt())
4243 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
4244 if (!ToBegin && S->getBeginStmt())
4247 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
4248 if (!ToEnd && S->getEndStmt())
4250 Expr *ToCond = Importer.Import(S->getCond());
4251 if (!ToCond && S->getCond())
4253 Expr *ToInc = Importer.Import(S->getInc());
4254 if (!ToInc && S->getInc())
4256 DeclStmt *ToLoopVar =
4257 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
4258 if (!ToLoopVar && S->getLoopVarStmt())
4260 Stmt *ToBody = Importer.Import(S->getBody());
4261 if (!ToBody && S->getBody())
4263 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4264 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
4265 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4266 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4267 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
4270 ToForLoc, ToCoawaitLoc,
4271 ToColonLoc, ToRParenLoc);
4274 Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
4275 Stmt *ToElem = Importer.Import(S->getElement());
4276 if (!ToElem && S->getElement())
4278 Expr *ToCollect = Importer.Import(S->getCollection());
4279 if (!ToCollect && S->getCollection())
4281 Stmt *ToBody = Importer.Import(S->getBody());
4282 if (!ToBody && S->getBody())
4284 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4285 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4286 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
4292 Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
4293 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
4294 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4295 VarDecl *ToExceptionDecl = nullptr;
4296 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
4298 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4299 if (!ToExceptionDecl)
4302 Stmt *ToBody = Importer.Import(S->getCatchBody());
4303 if (!ToBody && S->getCatchBody())
4305 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
4311 Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
4312 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
4313 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
4314 if (!ToAtFinallyStmt && S->getFinallyBody())
4316 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
4320 Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
4321 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
4322 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
4323 if (!ToAtTryStmt && S->getTryBody())
4325 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
4326 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
4327 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
4328 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
4329 ToCatchStmts[CI] = ToCatchStmt;
4333 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
4334 if (!ToAtFinallyStmt && S->getFinallyStmt())
4336 return ObjCAtTryStmt::Create(Importer.getToContext(),
4337 ToAtTryLoc, ToAtTryStmt,
4338 ToCatchStmts.begin(), ToCatchStmts.size(),
4342 Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
4343 (ObjCAtSynchronizedStmt *S) {
4344 SourceLocation ToAtSynchronizedLoc =
4345 Importer.Import(S->getAtSynchronizedLoc());
4346 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
4347 if (!ToSynchExpr && S->getSynchExpr())
4349 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
4350 if (!ToSynchBody && S->getSynchBody())
4352 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
4353 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
4356 Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
4357 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
4358 Expr *ToThrow = Importer.Import(S->getThrowExpr());
4359 if (!ToThrow && S->getThrowExpr())
4361 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
4364 Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
4365 (ObjCAutoreleasePoolStmt *S) {
4366 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
4367 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4368 if (!ToSubStmt && S->getSubStmt())
4370 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
4374 //----------------------------------------------------------------------------
4375 // Import Expressions
4376 //----------------------------------------------------------------------------
4377 Expr *ASTNodeImporter::VisitExpr(Expr *E) {
4378 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
4379 << E->getStmtClassName();
4383 Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
4384 QualType T = Importer.Import(E->getType());
4388 Expr *SubExpr = Importer.Import(E->getSubExpr());
4389 if (!SubExpr && E->getSubExpr())
4392 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
4396 return new (Importer.getToContext()) VAArgExpr(
4397 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
4398 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
4402 Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
4403 QualType T = Importer.Import(E->getType());
4407 return new (Importer.getToContext()) GNUNullExpr(
4408 T, Importer.Import(E->getLocStart()));
4411 Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
4412 QualType T = Importer.Import(E->getType());
4416 StringLiteral *SL = cast_or_null<StringLiteral>(
4417 Importer.Import(E->getFunctionName()));
4418 if (!SL && E->getFunctionName())
4421 return new (Importer.getToContext()) PredefinedExpr(
4422 Importer.Import(E->getLocStart()), T, E->getIdentType(), SL);
4425 Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
4426 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
4430 NamedDecl *FoundD = nullptr;
4431 if (E->getDecl() != E->getFoundDecl()) {
4432 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
4437 QualType T = Importer.Import(E->getType());
4442 TemplateArgumentListInfo ToTAInfo;
4443 TemplateArgumentListInfo *ResInfo = nullptr;
4444 if (E->hasExplicitTemplateArgs()) {
4445 for (const auto &FromLoc : E->template_arguments()) {
4447 TemplateArgumentLoc ToTALoc = ImportTemplateArgumentLoc(FromLoc, Error);
4450 ToTAInfo.addArgument(ToTALoc);
4452 ResInfo = &ToTAInfo;
4455 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
4456 Importer.Import(E->getQualifierLoc()),
4457 Importer.Import(E->getTemplateKeywordLoc()),
4459 E->refersToEnclosingVariableOrCapture(),
4460 Importer.Import(E->getLocation()),
4461 T, E->getValueKind(),
4463 if (E->hadMultipleCandidates())
4464 DRE->setHadMultipleCandidates(true);
4468 Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
4469 QualType T = Importer.Import(E->getType());
4473 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
4476 ASTNodeImporter::Designator
4477 ASTNodeImporter::ImportDesignator(const Designator &D) {
4478 if (D.isFieldDesignator()) {
4479 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
4480 // Caller checks for import error
4481 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
4482 Importer.Import(D.getFieldLoc()));
4484 if (D.isArrayDesignator())
4485 return Designator(D.getFirstExprIndex(),
4486 Importer.Import(D.getLBracketLoc()),
4487 Importer.Import(D.getRBracketLoc()));
4489 assert(D.isArrayRangeDesignator());
4490 return Designator(D.getFirstExprIndex(),
4491 Importer.Import(D.getLBracketLoc()),
4492 Importer.Import(D.getEllipsisLoc()),
4493 Importer.Import(D.getRBracketLoc()));
4497 Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
4498 Expr *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
4502 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
4503 // List elements from the second, the first is Init itself
4504 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
4505 if (Expr *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
4506 IndexExprs[I - 1] = Arg;
4511 SmallVector<Designator, 4> Designators(DIE->size());
4512 llvm::transform(DIE->designators(), Designators.begin(),
4513 [this](const Designator &D) -> Designator {
4514 return ImportDesignator(D);
4517 for (const Designator &D : DIE->designators())
4518 if (D.isFieldDesignator() && !D.getFieldName())
4521 return DesignatedInitExpr::Create(
4522 Importer.getToContext(), Designators,
4523 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
4524 DIE->usesGNUSyntax(), Init);
4527 Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
4528 QualType T = Importer.Import(E->getType());
4532 return new (Importer.getToContext())
4533 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
4536 Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
4537 QualType T = Importer.Import(E->getType());
4541 return IntegerLiteral::Create(Importer.getToContext(),
4543 Importer.Import(E->getLocation()));
4546 Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
4547 QualType T = Importer.Import(E->getType());
4551 return FloatingLiteral::Create(Importer.getToContext(),
4552 E->getValue(), E->isExact(), T,
4553 Importer.Import(E->getLocation()));
4556 Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
4557 QualType T = Importer.Import(E->getType());
4561 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
4563 Importer.Import(E->getLocation()));
4566 Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
4567 QualType T = Importer.Import(E->getType());
4571 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
4572 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
4574 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
4575 E->getKind(), E->isPascal(), T,
4576 Locations.data(), Locations.size());
4579 Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
4580 QualType T = Importer.Import(E->getType());
4584 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
4588 Expr *Init = Importer.Import(E->getInitializer());
4592 return new (Importer.getToContext()) CompoundLiteralExpr(
4593 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
4594 Init, E->isFileScope());
4597 Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
4598 QualType T = Importer.Import(E->getType());
4602 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
4603 if (ImportArrayChecked(
4604 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
4608 return new (Importer.getToContext()) AtomicExpr(
4609 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
4610 Importer.Import(E->getRParenLoc()));
4613 Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
4614 QualType T = Importer.Import(E->getType());
4618 LabelDecl *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
4622 return new (Importer.getToContext()) AddrLabelExpr(
4623 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
4627 Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
4628 Expr *SubExpr = Importer.Import(E->getSubExpr());
4632 return new (Importer.getToContext())
4633 ParenExpr(Importer.Import(E->getLParen()),
4634 Importer.Import(E->getRParen()),
4638 Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
4639 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
4640 if (ImportContainerChecked(E->exprs(), Exprs))
4643 return new (Importer.getToContext()) ParenListExpr(
4644 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
4645 Exprs, Importer.Import(E->getLParenLoc()));
4648 Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
4649 QualType T = Importer.Import(E->getType());
4653 CompoundStmt *ToSubStmt = cast_or_null<CompoundStmt>(
4654 Importer.Import(E->getSubStmt()));
4655 if (!ToSubStmt && E->getSubStmt())
4658 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
4659 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
4662 Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
4663 QualType T = Importer.Import(E->getType());
4667 Expr *SubExpr = Importer.Import(E->getSubExpr());
4671 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
4672 T, E->getValueKind(),
4674 Importer.Import(E->getOperatorLoc()));
4677 Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
4678 UnaryExprOrTypeTraitExpr *E) {
4679 QualType ResultType = Importer.Import(E->getType());
4681 if (E->isArgumentType()) {
4682 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
4686 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4688 Importer.Import(E->getOperatorLoc()),
4689 Importer.Import(E->getRParenLoc()));
4692 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
4696 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4697 SubExpr, ResultType,
4698 Importer.Import(E->getOperatorLoc()),
4699 Importer.Import(E->getRParenLoc()));
4702 Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
4703 QualType T = Importer.Import(E->getType());
4707 Expr *LHS = Importer.Import(E->getLHS());
4711 Expr *RHS = Importer.Import(E->getRHS());
4715 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
4716 T, E->getValueKind(),
4718 Importer.Import(E->getOperatorLoc()),
4719 E->getFPFeatures());
4722 Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
4723 QualType T = Importer.Import(E->getType());
4727 Expr *ToLHS = Importer.Import(E->getLHS());
4731 Expr *ToRHS = Importer.Import(E->getRHS());
4735 Expr *ToCond = Importer.Import(E->getCond());
4739 return new (Importer.getToContext()) ConditionalOperator(
4740 ToCond, Importer.Import(E->getQuestionLoc()),
4741 ToLHS, Importer.Import(E->getColonLoc()),
4742 ToRHS, T, E->getValueKind(), E->getObjectKind());
4745 Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
4746 BinaryConditionalOperator *E) {
4747 QualType T = Importer.Import(E->getType());
4751 Expr *Common = Importer.Import(E->getCommon());
4755 Expr *Cond = Importer.Import(E->getCond());
4759 OpaqueValueExpr *OpaqueValue = cast_or_null<OpaqueValueExpr>(
4760 Importer.Import(E->getOpaqueValue()));
4764 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
4768 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
4772 return new (Importer.getToContext()) BinaryConditionalOperator(
4773 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
4774 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
4775 T, E->getValueKind(), E->getObjectKind());
4778 Expr *ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
4779 QualType T = Importer.Import(E->getType());
4783 TypeSourceInfo *ToQueried = Importer.Import(E->getQueriedTypeSourceInfo());
4787 Expr *Dim = Importer.Import(E->getDimensionExpression());
4788 if (!Dim && E->getDimensionExpression())
4791 return new (Importer.getToContext()) ArrayTypeTraitExpr(
4792 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
4793 E->getValue(), Dim, Importer.Import(E->getLocEnd()), T);
4796 Expr *ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
4797 QualType T = Importer.Import(E->getType());
4801 Expr *ToQueried = Importer.Import(E->getQueriedExpression());
4805 return new (Importer.getToContext()) ExpressionTraitExpr(
4806 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
4807 E->getValue(), Importer.Import(E->getLocEnd()), T);
4810 Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
4811 QualType T = Importer.Import(E->getType());
4815 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
4816 if (!SourceExpr && E->getSourceExpr())
4819 return new (Importer.getToContext()) OpaqueValueExpr(
4820 Importer.Import(E->getLocation()), T, E->getValueKind(),
4821 E->getObjectKind(), SourceExpr);
4824 Expr *ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
4825 QualType T = Importer.Import(E->getType());
4829 Expr *ToLHS = Importer.Import(E->getLHS());
4833 Expr *ToRHS = Importer.Import(E->getRHS());
4837 return new (Importer.getToContext()) ArraySubscriptExpr(
4838 ToLHS, ToRHS, T, E->getValueKind(), E->getObjectKind(),
4839 Importer.Import(E->getRBracketLoc()));
4842 Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
4843 QualType T = Importer.Import(E->getType());
4847 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
4848 if (CompLHSType.isNull())
4851 QualType CompResultType = Importer.Import(E->getComputationResultType());
4852 if (CompResultType.isNull())
4855 Expr *LHS = Importer.Import(E->getLHS());
4859 Expr *RHS = Importer.Import(E->getRHS());
4863 return new (Importer.getToContext())
4864 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
4865 T, E->getValueKind(),
4867 CompLHSType, CompResultType,
4868 Importer.Import(E->getOperatorLoc()),
4869 E->getFPFeatures());
4872 bool ASTNodeImporter::ImportCastPath(CastExpr *CE, CXXCastPath &Path) {
4873 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
4874 if (CXXBaseSpecifier *Spec = Importer.Import(*I))
4875 Path.push_back(Spec);
4882 Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4883 QualType T = Importer.Import(E->getType());
4887 Expr *SubExpr = Importer.Import(E->getSubExpr());
4891 CXXCastPath BasePath;
4892 if (ImportCastPath(E, BasePath))
4895 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
4896 SubExpr, &BasePath, E->getValueKind());
4899 Expr *ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
4900 QualType T = Importer.Import(E->getType());
4904 Expr *SubExpr = Importer.Import(E->getSubExpr());
4908 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4909 if (!TInfo && E->getTypeInfoAsWritten())
4912 CXXCastPath BasePath;
4913 if (ImportCastPath(E, BasePath))
4916 switch (E->getStmtClass()) {
4917 case Stmt::CStyleCastExprClass: {
4918 CStyleCastExpr *CCE = cast<CStyleCastExpr>(E);
4919 return CStyleCastExpr::Create(Importer.getToContext(), T,
4920 E->getValueKind(), E->getCastKind(),
4921 SubExpr, &BasePath, TInfo,
4922 Importer.Import(CCE->getLParenLoc()),
4923 Importer.Import(CCE->getRParenLoc()));
4926 case Stmt::CXXFunctionalCastExprClass: {
4927 CXXFunctionalCastExpr *FCE = cast<CXXFunctionalCastExpr>(E);
4928 return CXXFunctionalCastExpr::Create(Importer.getToContext(), T,
4929 E->getValueKind(), TInfo,
4930 E->getCastKind(), SubExpr, &BasePath,
4931 Importer.Import(FCE->getLParenLoc()),
4932 Importer.Import(FCE->getRParenLoc()));
4935 case Stmt::ObjCBridgedCastExprClass: {
4936 ObjCBridgedCastExpr *OCE = cast<ObjCBridgedCastExpr>(E);
4937 return new (Importer.getToContext()) ObjCBridgedCastExpr(
4938 Importer.Import(OCE->getLParenLoc()), OCE->getBridgeKind(),
4939 E->getCastKind(), Importer.Import(OCE->getBridgeKeywordLoc()),
4943 break; // just fall through
4946 CXXNamedCastExpr *Named = cast<CXXNamedCastExpr>(E);
4947 SourceLocation ExprLoc = Importer.Import(Named->getOperatorLoc()),
4948 RParenLoc = Importer.Import(Named->getRParenLoc());
4949 SourceRange Brackets = Importer.Import(Named->getAngleBrackets());
4951 switch (E->getStmtClass()) {
4952 case Stmt::CXXStaticCastExprClass:
4953 return CXXStaticCastExpr::Create(Importer.getToContext(), T,
4954 E->getValueKind(), E->getCastKind(),
4955 SubExpr, &BasePath, TInfo,
4956 ExprLoc, RParenLoc, Brackets);
4958 case Stmt::CXXDynamicCastExprClass:
4959 return CXXDynamicCastExpr::Create(Importer.getToContext(), T,
4960 E->getValueKind(), E->getCastKind(),
4961 SubExpr, &BasePath, TInfo,
4962 ExprLoc, RParenLoc, Brackets);
4964 case Stmt::CXXReinterpretCastExprClass:
4965 return CXXReinterpretCastExpr::Create(Importer.getToContext(), T,
4966 E->getValueKind(), E->getCastKind(),
4967 SubExpr, &BasePath, TInfo,
4968 ExprLoc, RParenLoc, Brackets);
4970 case Stmt::CXXConstCastExprClass:
4971 return CXXConstCastExpr::Create(Importer.getToContext(), T,
4972 E->getValueKind(), SubExpr, TInfo, ExprLoc,
4973 RParenLoc, Brackets);
4975 llvm_unreachable("Cast expression of unsupported type!");
4980 Expr *ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *OE) {
4981 QualType T = Importer.Import(OE->getType());
4985 SmallVector<OffsetOfNode, 4> Nodes;
4986 for (int I = 0, E = OE->getNumComponents(); I < E; ++I) {
4987 const OffsetOfNode &Node = OE->getComponent(I);
4989 switch (Node.getKind()) {
4990 case OffsetOfNode::Array:
4991 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()),
4992 Node.getArrayExprIndex(),
4993 Importer.Import(Node.getLocEnd())));
4996 case OffsetOfNode::Base: {
4997 CXXBaseSpecifier *BS = Importer.Import(Node.getBase());
4998 if (!BS && Node.getBase())
5000 Nodes.push_back(OffsetOfNode(BS));
5003 case OffsetOfNode::Field: {
5004 FieldDecl *FD = cast_or_null<FieldDecl>(Importer.Import(Node.getField()));
5007 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), FD,
5008 Importer.Import(Node.getLocEnd())));
5011 case OffsetOfNode::Identifier: {
5012 IdentifierInfo *ToII = Importer.Import(Node.getFieldName());
5015 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), ToII,
5016 Importer.Import(Node.getLocEnd())));
5022 SmallVector<Expr *, 4> Exprs(OE->getNumExpressions());
5023 for (int I = 0, E = OE->getNumExpressions(); I < E; ++I) {
5024 Expr *ToIndexExpr = Importer.Import(OE->getIndexExpr(I));
5027 Exprs[I] = ToIndexExpr;
5030 TypeSourceInfo *TInfo = Importer.Import(OE->getTypeSourceInfo());
5031 if (!TInfo && OE->getTypeSourceInfo())
5034 return OffsetOfExpr::Create(Importer.getToContext(), T,
5035 Importer.Import(OE->getOperatorLoc()),
5036 TInfo, Nodes, Exprs,
5037 Importer.Import(OE->getRParenLoc()));
5040 Expr *ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
5041 QualType T = Importer.Import(E->getType());
5045 Expr *Operand = Importer.Import(E->getOperand());
5049 CanThrowResult CanThrow;
5050 if (E->isValueDependent())
5051 CanThrow = CT_Dependent;
5053 CanThrow = E->getValue() ? CT_Can : CT_Cannot;
5055 return new (Importer.getToContext()) CXXNoexceptExpr(
5056 T, Operand, CanThrow,
5057 Importer.Import(E->getLocStart()), Importer.Import(E->getLocEnd()));
5060 Expr *ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
5061 QualType T = Importer.Import(E->getType());
5065 Expr *SubExpr = Importer.Import(E->getSubExpr());
5066 if (!SubExpr && E->getSubExpr())
5069 return new (Importer.getToContext()) CXXThrowExpr(
5070 SubExpr, T, Importer.Import(E->getThrowLoc()),
5071 E->isThrownVariableInScope());
5074 Expr *ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
5075 ParmVarDecl *Param = cast_or_null<ParmVarDecl>(
5076 Importer.Import(E->getParam()));
5080 return CXXDefaultArgExpr::Create(
5081 Importer.getToContext(), Importer.Import(E->getUsedLocation()), Param);
5084 Expr *ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
5085 QualType T = Importer.Import(E->getType());
5089 TypeSourceInfo *TypeInfo = Importer.Import(E->getTypeSourceInfo());
5093 return new (Importer.getToContext()) CXXScalarValueInitExpr(
5094 T, TypeInfo, Importer.Import(E->getRParenLoc()));
5097 Expr *ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
5098 Expr *SubExpr = Importer.Import(E->getSubExpr());
5102 auto *Dtor = cast_or_null<CXXDestructorDecl>(
5103 Importer.Import(const_cast<CXXDestructorDecl *>(
5104 E->getTemporary()->getDestructor())));
5108 ASTContext &ToCtx = Importer.getToContext();
5109 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, Dtor);
5110 return CXXBindTemporaryExpr::Create(ToCtx, Temp, SubExpr);
5113 Expr *ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE) {
5114 QualType T = Importer.Import(CE->getType());
5118 SmallVector<Expr *, 8> Args(CE->getNumArgs());
5119 if (ImportContainerChecked(CE->arguments(), Args))
5122 auto *Ctor = cast_or_null<CXXConstructorDecl>(
5123 Importer.Import(CE->getConstructor()));
5127 return CXXTemporaryObjectExpr::Create(
5128 Importer.getToContext(), T,
5129 Importer.Import(CE->getLocStart()),
5133 CE->hadMultipleCandidates(),
5134 CE->isListInitialization(),
5135 CE->isStdInitListInitialization(),
5136 CE->requiresZeroInitialization(),
5137 CE->getConstructionKind(),
5138 Importer.Import(CE->getParenOrBraceRange()));
5142 ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
5143 QualType T = Importer.Import(E->getType());
5147 Expr *TempE = Importer.Import(E->GetTemporaryExpr());
5151 ValueDecl *ExtendedBy = cast_or_null<ValueDecl>(
5152 Importer.Import(const_cast<ValueDecl *>(E->getExtendingDecl())));
5153 if (!ExtendedBy && E->getExtendingDecl())
5156 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
5157 T, TempE, E->isBoundToLvalueReference());
5159 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
5160 ToMTE->setExtendingDecl(ExtendedBy, E->getManglingNumber());
5164 Expr *ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *CE) {
5165 QualType T = Importer.Import(CE->getType());
5169 SmallVector<Expr *, 4> PlacementArgs(CE->getNumPlacementArgs());
5170 if (ImportContainerChecked(CE->placement_arguments(), PlacementArgs))
5173 FunctionDecl *OperatorNewDecl = cast_or_null<FunctionDecl>(
5174 Importer.Import(CE->getOperatorNew()));
5175 if (!OperatorNewDecl && CE->getOperatorNew())
5178 FunctionDecl *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
5179 Importer.Import(CE->getOperatorDelete()));
5180 if (!OperatorDeleteDecl && CE->getOperatorDelete())
5183 Expr *ToInit = Importer.Import(CE->getInitializer());
5184 if (!ToInit && CE->getInitializer())
5187 TypeSourceInfo *TInfo = Importer.Import(CE->getAllocatedTypeSourceInfo());
5191 Expr *ToArrSize = Importer.Import(CE->getArraySize());
5192 if (!ToArrSize && CE->getArraySize())
5195 return new (Importer.getToContext()) CXXNewExpr(
5196 Importer.getToContext(),
5198 OperatorNewDecl, OperatorDeleteDecl,
5199 CE->passAlignment(),
5200 CE->doesUsualArrayDeleteWantSize(),
5202 Importer.Import(CE->getTypeIdParens()),
5203 ToArrSize, CE->getInitializationStyle(), ToInit, T, TInfo,
5204 Importer.Import(CE->getSourceRange()),
5205 Importer.Import(CE->getDirectInitRange()));
5208 Expr *ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
5209 QualType T = Importer.Import(E->getType());
5213 FunctionDecl *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
5214 Importer.Import(E->getOperatorDelete()));
5215 if (!OperatorDeleteDecl && E->getOperatorDelete())
5218 Expr *ToArg = Importer.Import(E->getArgument());
5219 if (!ToArg && E->getArgument())
5222 return new (Importer.getToContext()) CXXDeleteExpr(
5223 T, E->isGlobalDelete(),
5225 E->isArrayFormAsWritten(),
5226 E->doesUsualArrayDeleteWantSize(),
5229 Importer.Import(E->getLocStart()));
5232 Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
5233 QualType T = Importer.Import(E->getType());
5237 CXXConstructorDecl *ToCCD =
5238 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
5242 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
5243 if (ImportContainerChecked(E->arguments(), ToArgs))
5246 return CXXConstructExpr::Create(Importer.getToContext(), T,
5247 Importer.Import(E->getLocation()),
5248 ToCCD, E->isElidable(),
5249 ToArgs, E->hadMultipleCandidates(),
5250 E->isListInitialization(),
5251 E->isStdInitListInitialization(),
5252 E->requiresZeroInitialization(),
5253 E->getConstructionKind(),
5254 Importer.Import(E->getParenOrBraceRange()));
5257 Expr *ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *EWC) {
5258 Expr *SubExpr = Importer.Import(EWC->getSubExpr());
5259 if (!SubExpr && EWC->getSubExpr())
5262 SmallVector<ExprWithCleanups::CleanupObject, 8> Objs(EWC->getNumObjects());
5263 for (unsigned I = 0, E = EWC->getNumObjects(); I < E; I++)
5264 if (ExprWithCleanups::CleanupObject Obj =
5265 cast_or_null<BlockDecl>(Importer.Import(EWC->getObject(I))))
5270 return ExprWithCleanups::Create(Importer.getToContext(),
5271 SubExpr, EWC->cleanupsHaveSideEffects(),
5275 Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
5276 QualType T = Importer.Import(E->getType());
5280 Expr *ToFn = Importer.Import(E->getCallee());
5284 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
5285 if (ImportContainerChecked(E->arguments(), ToArgs))
5288 return new (Importer.getToContext()) CXXMemberCallExpr(
5289 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
5290 Importer.Import(E->getRParenLoc()));
5293 Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
5294 QualType T = Importer.Import(E->getType());
5298 return new (Importer.getToContext())
5299 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
5302 Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
5303 QualType T = Importer.Import(E->getType());
5307 return new (Importer.getToContext())
5308 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
5312 Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
5313 QualType T = Importer.Import(E->getType());
5317 Expr *ToBase = Importer.Import(E->getBase());
5318 if (!ToBase && E->getBase())
5321 ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
5322 if (!ToMember && E->getMemberDecl())
5325 DeclAccessPair ToFoundDecl = DeclAccessPair::make(
5326 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
5327 E->getFoundDecl().getAccess());
5329 DeclarationNameInfo ToMemberNameInfo(
5330 Importer.Import(E->getMemberNameInfo().getName()),
5331 Importer.Import(E->getMemberNameInfo().getLoc()));
5333 if (E->hasExplicitTemplateArgs()) {
5334 return nullptr; // FIXME: handle template arguments
5337 return MemberExpr::Create(Importer.getToContext(), ToBase,
5339 Importer.Import(E->getOperatorLoc()),
5340 Importer.Import(E->getQualifierLoc()),
5341 Importer.Import(E->getTemplateKeywordLoc()),
5342 ToMember, ToFoundDecl, ToMemberNameInfo,
5343 nullptr, T, E->getValueKind(),
5344 E->getObjectKind());
5347 Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
5348 QualType T = Importer.Import(E->getType());
5352 Expr *ToCallee = Importer.Import(E->getCallee());
5353 if (!ToCallee && E->getCallee())
5356 unsigned NumArgs = E->getNumArgs();
5358 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
5360 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) {
5361 Expr *FromArg = E->getArg(ai);
5362 Expr *ToArg = Importer.Import(FromArg);
5368 Expr **ToArgs_Copied = new (Importer.getToContext())
5371 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
5372 ToArgs_Copied[ai] = ToArgs[ai];
5374 return new (Importer.getToContext())
5375 CallExpr(Importer.getToContext(), ToCallee,
5376 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
5377 Importer.Import(E->getRParenLoc()));
5380 Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
5381 QualType T = Importer.Import(ILE->getType());
5385 llvm::SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
5386 if (ImportContainerChecked(ILE->inits(), Exprs))
5389 ASTContext &ToCtx = Importer.getToContext();
5390 InitListExpr *To = new (ToCtx) InitListExpr(
5391 ToCtx, Importer.Import(ILE->getLBraceLoc()),
5392 Exprs, Importer.Import(ILE->getLBraceLoc()));
5395 if (ILE->hasArrayFiller()) {
5396 Expr *Filler = Importer.Import(ILE->getArrayFiller());
5399 To->setArrayFiller(Filler);
5402 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
5403 FieldDecl *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
5406 To->setInitializedFieldInUnion(ToFD);
5409 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
5410 InitListExpr *ToSyntForm = cast_or_null<InitListExpr>(
5411 Importer.Import(SyntForm));
5414 To->setSyntacticForm(ToSyntForm);
5417 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
5418 To->setValueDependent(ILE->isValueDependent());
5419 To->setInstantiationDependent(ILE->isInstantiationDependent());
5424 Expr *ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
5425 QualType ToType = Importer.Import(E->getType());
5426 if (ToType.isNull())
5429 Expr *ToCommon = Importer.Import(E->getCommonExpr());
5430 if (!ToCommon && E->getCommonExpr())
5433 Expr *ToSubExpr = Importer.Import(E->getSubExpr());
5434 if (!ToSubExpr && E->getSubExpr())
5437 return new (Importer.getToContext())
5438 ArrayInitLoopExpr(ToType, ToCommon, ToSubExpr);
5441 Expr *ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
5442 QualType ToType = Importer.Import(E->getType());
5443 if (ToType.isNull())
5445 return new (Importer.getToContext()) ArrayInitIndexExpr(ToType);
5448 Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
5449 FieldDecl *ToField = llvm::dyn_cast_or_null<FieldDecl>(
5450 Importer.Import(DIE->getField()));
5451 if (!ToField && DIE->getField())
5454 return CXXDefaultInitExpr::Create(
5455 Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField);
5458 Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
5459 QualType ToType = Importer.Import(E->getType());
5460 if (ToType.isNull() && !E->getType().isNull())
5462 ExprValueKind VK = E->getValueKind();
5463 CastKind CK = E->getCastKind();
5464 Expr *ToOp = Importer.Import(E->getSubExpr());
5465 if (!ToOp && E->getSubExpr())
5467 CXXCastPath BasePath;
5468 if (ImportCastPath(E, BasePath))
5470 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten());
5471 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc());
5472 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc());
5473 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets());
5475 if (isa<CXXStaticCastExpr>(E)) {
5476 return CXXStaticCastExpr::Create(
5477 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
5478 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
5479 } else if (isa<CXXDynamicCastExpr>(E)) {
5480 return CXXDynamicCastExpr::Create(
5481 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
5482 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
5483 } else if (isa<CXXReinterpretCastExpr>(E)) {
5484 return CXXReinterpretCastExpr::Create(
5485 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
5486 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
5493 Expr *ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
5494 SubstNonTypeTemplateParmExpr *E) {
5495 QualType T = Importer.Import(E->getType());
5499 NonTypeTemplateParmDecl *Param = cast_or_null<NonTypeTemplateParmDecl>(
5500 Importer.Import(E->getParameter()));
5504 Expr *Replacement = Importer.Import(E->getReplacement());
5508 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
5509 T, E->getValueKind(), Importer.Import(E->getExprLoc()), Param,
5513 void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
5514 CXXMethodDecl *FromMethod) {
5515 for (auto *FromOverriddenMethod : FromMethod->overridden_methods())
5516 ToMethod->addOverriddenMethod(
5517 cast<CXXMethodDecl>(Importer.Import(const_cast<CXXMethodDecl*>(
5518 FromOverriddenMethod))));
5521 ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
5522 ASTContext &FromContext, FileManager &FromFileManager,
5524 : ToContext(ToContext), FromContext(FromContext),
5525 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
5526 Minimal(MinimalImport), LastDiagFromFrom(false)
5528 ImportedDecls[FromContext.getTranslationUnitDecl()]
5529 = ToContext.getTranslationUnitDecl();
5532 ASTImporter::~ASTImporter() { }
5534 QualType ASTImporter::Import(QualType FromT) {
5538 const Type *fromTy = FromT.getTypePtr();
5540 // Check whether we've already imported this type.
5541 llvm::DenseMap<const Type *, const Type *>::iterator Pos
5542 = ImportedTypes.find(fromTy);
5543 if (Pos != ImportedTypes.end())
5544 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
5547 ASTNodeImporter Importer(*this);
5548 QualType ToT = Importer.Visit(fromTy);
5552 // Record the imported type.
5553 ImportedTypes[fromTy] = ToT.getTypePtr();
5555 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
5558 TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
5562 // FIXME: For now we just create a "trivial" type source info based
5563 // on the type and a single location. Implement a real version of this.
5564 QualType T = Import(FromTSI->getType());
5568 return ToContext.getTrivialTypeSourceInfo(T,
5569 Import(FromTSI->getTypeLoc().getLocStart()));
5572 Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
5573 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
5574 if (Pos != ImportedDecls.end()) {
5575 Decl *ToD = Pos->second;
5576 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
5583 Decl *ASTImporter::Import(Decl *FromD) {
5587 ASTNodeImporter Importer(*this);
5589 // Check whether we've already imported this declaration.
5590 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
5591 if (Pos != ImportedDecls.end()) {
5592 Decl *ToD = Pos->second;
5593 Importer.ImportDefinitionIfNeeded(FromD, ToD);
5598 Decl *ToD = Importer.Visit(FromD);
5602 // Record the imported declaration.
5603 ImportedDecls[FromD] = ToD;
5605 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
5606 // Keep track of anonymous tags that have an associated typedef.
5607 if (FromTag->getTypedefNameForAnonDecl())
5608 AnonTagsWithPendingTypedefs.push_back(FromTag);
5609 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
5610 // When we've finished transforming a typedef, see whether it was the
5611 // typedef for an anonymous tag.
5612 for (SmallVectorImpl<TagDecl *>::iterator
5613 FromTag = AnonTagsWithPendingTypedefs.begin(),
5614 FromTagEnd = AnonTagsWithPendingTypedefs.end();
5615 FromTag != FromTagEnd; ++FromTag) {
5616 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
5617 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
5618 // We found the typedef for an anonymous tag; link them.
5619 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
5620 AnonTagsWithPendingTypedefs.erase(FromTag);
5630 DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
5634 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
5638 // When we're using a record/enum/Objective-C class/protocol as a context, we
5639 // need it to have a definition.
5640 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
5641 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
5642 if (ToRecord->isCompleteDefinition()) {
5644 } else if (FromRecord->isCompleteDefinition()) {
5645 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
5646 ASTNodeImporter::IDK_Basic);
5648 CompleteDecl(ToRecord);
5650 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
5651 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
5652 if (ToEnum->isCompleteDefinition()) {
5654 } else if (FromEnum->isCompleteDefinition()) {
5655 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
5656 ASTNodeImporter::IDK_Basic);
5658 CompleteDecl(ToEnum);
5660 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
5661 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
5662 if (ToClass->getDefinition()) {
5664 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
5665 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
5666 ASTNodeImporter::IDK_Basic);
5668 CompleteDecl(ToClass);
5670 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
5671 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
5672 if (ToProto->getDefinition()) {
5674 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
5675 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
5676 ASTNodeImporter::IDK_Basic);
5678 CompleteDecl(ToProto);
5685 Expr *ASTImporter::Import(Expr *FromE) {
5689 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
5692 Stmt *ASTImporter::Import(Stmt *FromS) {
5696 // Check whether we've already imported this declaration.
5697 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
5698 if (Pos != ImportedStmts.end())
5702 ASTNodeImporter Importer(*this);
5703 Stmt *ToS = Importer.Visit(FromS);
5707 // Record the imported declaration.
5708 ImportedStmts[FromS] = ToS;
5712 NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
5716 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
5718 switch (FromNNS->getKind()) {
5719 case NestedNameSpecifier::Identifier:
5720 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
5721 return NestedNameSpecifier::Create(ToContext, prefix, II);
5725 case NestedNameSpecifier::Namespace:
5726 if (NamespaceDecl *NS =
5727 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
5728 return NestedNameSpecifier::Create(ToContext, prefix, NS);
5732 case NestedNameSpecifier::NamespaceAlias:
5733 if (NamespaceAliasDecl *NSAD =
5734 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
5735 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
5739 case NestedNameSpecifier::Global:
5740 return NestedNameSpecifier::GlobalSpecifier(ToContext);
5742 case NestedNameSpecifier::Super:
5743 if (CXXRecordDecl *RD =
5744 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
5745 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
5749 case NestedNameSpecifier::TypeSpec:
5750 case NestedNameSpecifier::TypeSpecWithTemplate: {
5751 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
5753 bool bTemplate = FromNNS->getKind() ==
5754 NestedNameSpecifier::TypeSpecWithTemplate;
5755 return NestedNameSpecifier::Create(ToContext, prefix,
5756 bTemplate, T.getTypePtr());
5762 llvm_unreachable("Invalid nested name specifier kind");
5765 NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
5766 // Copied from NestedNameSpecifier mostly.
5767 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
5768 NestedNameSpecifierLoc NNS = FromNNS;
5770 // Push each of the nested-name-specifiers's onto a stack for
5771 // serialization in reverse order.
5773 NestedNames.push_back(NNS);
5774 NNS = NNS.getPrefix();
5777 NestedNameSpecifierLocBuilder Builder;
5779 while (!NestedNames.empty()) {
5780 NNS = NestedNames.pop_back_val();
5781 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
5783 return NestedNameSpecifierLoc();
5785 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
5787 case NestedNameSpecifier::Identifier:
5788 Builder.Extend(getToContext(),
5789 Spec->getAsIdentifier(),
5790 Import(NNS.getLocalBeginLoc()),
5791 Import(NNS.getLocalEndLoc()));
5794 case NestedNameSpecifier::Namespace:
5795 Builder.Extend(getToContext(),
5796 Spec->getAsNamespace(),
5797 Import(NNS.getLocalBeginLoc()),
5798 Import(NNS.getLocalEndLoc()));
5801 case NestedNameSpecifier::NamespaceAlias:
5802 Builder.Extend(getToContext(),
5803 Spec->getAsNamespaceAlias(),
5804 Import(NNS.getLocalBeginLoc()),
5805 Import(NNS.getLocalEndLoc()));
5808 case NestedNameSpecifier::TypeSpec:
5809 case NestedNameSpecifier::TypeSpecWithTemplate: {
5810 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
5811 QualType(Spec->getAsType(), 0));
5812 Builder.Extend(getToContext(),
5813 Import(NNS.getLocalBeginLoc()),
5815 Import(NNS.getLocalEndLoc()));
5819 case NestedNameSpecifier::Global:
5820 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
5823 case NestedNameSpecifier::Super: {
5824 SourceRange ToRange = Import(NNS.getSourceRange());
5825 Builder.MakeSuper(getToContext(),
5826 Spec->getAsRecordDecl(),
5833 return Builder.getWithLocInContext(getToContext());
5836 TemplateName ASTImporter::Import(TemplateName From) {
5837 switch (From.getKind()) {
5838 case TemplateName::Template:
5839 if (TemplateDecl *ToTemplate
5840 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
5841 return TemplateName(ToTemplate);
5843 return TemplateName();
5845 case TemplateName::OverloadedTemplate: {
5846 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
5847 UnresolvedSet<2> ToTemplates;
5848 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
5849 E = FromStorage->end();
5851 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
5852 ToTemplates.addDecl(To);
5854 return TemplateName();
5856 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
5860 case TemplateName::QualifiedTemplate: {
5861 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
5862 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
5864 return TemplateName();
5866 if (TemplateDecl *ToTemplate
5867 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
5868 return ToContext.getQualifiedTemplateName(Qualifier,
5869 QTN->hasTemplateKeyword(),
5872 return TemplateName();
5875 case TemplateName::DependentTemplate: {
5876 DependentTemplateName *DTN = From.getAsDependentTemplateName();
5877 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
5879 return TemplateName();
5881 if (DTN->isIdentifier()) {
5882 return ToContext.getDependentTemplateName(Qualifier,
5883 Import(DTN->getIdentifier()));
5886 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
5889 case TemplateName::SubstTemplateTemplateParm: {
5890 SubstTemplateTemplateParmStorage *subst
5891 = From.getAsSubstTemplateTemplateParm();
5892 TemplateTemplateParmDecl *param
5893 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
5895 return TemplateName();
5897 TemplateName replacement = Import(subst->getReplacement());
5898 if (replacement.isNull()) return TemplateName();
5900 return ToContext.getSubstTemplateTemplateParm(param, replacement);
5903 case TemplateName::SubstTemplateTemplateParmPack: {
5904 SubstTemplateTemplateParmPackStorage *SubstPack
5905 = From.getAsSubstTemplateTemplateParmPack();
5906 TemplateTemplateParmDecl *Param
5907 = cast_or_null<TemplateTemplateParmDecl>(
5908 Import(SubstPack->getParameterPack()));
5910 return TemplateName();
5912 ASTNodeImporter Importer(*this);
5913 TemplateArgument ArgPack
5914 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
5915 if (ArgPack.isNull())
5916 return TemplateName();
5918 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
5922 llvm_unreachable("Invalid template name kind");
5925 SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
5926 if (FromLoc.isInvalid())
5927 return SourceLocation();
5929 SourceManager &FromSM = FromContext.getSourceManager();
5931 // For now, map everything down to its file location, so that we
5932 // don't have to import macro expansions.
5933 // FIXME: Import macro expansions!
5934 FromLoc = FromSM.getFileLoc(FromLoc);
5935 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
5936 SourceManager &ToSM = ToContext.getSourceManager();
5937 FileID ToFileID = Import(Decomposed.first);
5938 if (ToFileID.isInvalid())
5939 return SourceLocation();
5940 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
5941 .getLocWithOffset(Decomposed.second);
5945 SourceRange ASTImporter::Import(SourceRange FromRange) {
5946 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
5949 FileID ASTImporter::Import(FileID FromID) {
5950 llvm::DenseMap<FileID, FileID>::iterator Pos
5951 = ImportedFileIDs.find(FromID);
5952 if (Pos != ImportedFileIDs.end())
5955 SourceManager &FromSM = FromContext.getSourceManager();
5956 SourceManager &ToSM = ToContext.getSourceManager();
5957 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
5958 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
5960 // Include location of this file.
5961 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
5963 // Map the FileID for to the "to" source manager.
5965 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
5966 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
5967 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
5969 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
5970 // than mmap the files several times.
5971 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
5974 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
5975 FromSLoc.getFile().getFileCharacteristic());
5977 // FIXME: We want to re-use the existing MemoryBuffer!
5978 const llvm::MemoryBuffer *
5979 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
5980 std::unique_ptr<llvm::MemoryBuffer> ToBuf
5981 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
5982 FromBuf->getBufferIdentifier());
5983 ToID = ToSM.createFileID(std::move(ToBuf),
5984 FromSLoc.getFile().getFileCharacteristic());
5988 ImportedFileIDs[FromID] = ToID;
5992 CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
5993 Expr *ToExpr = Import(From->getInit());
5994 if (!ToExpr && From->getInit())
5997 if (From->isBaseInitializer()) {
5998 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
5999 if (!ToTInfo && From->getTypeSourceInfo())
6002 return new (ToContext) CXXCtorInitializer(
6003 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
6004 ToExpr, Import(From->getRParenLoc()),
6005 From->isPackExpansion() ? Import(From->getEllipsisLoc())
6006 : SourceLocation());
6007 } else if (From->isMemberInitializer()) {
6008 FieldDecl *ToField =
6009 llvm::cast_or_null<FieldDecl>(Import(From->getMember()));
6010 if (!ToField && From->getMember())
6013 return new (ToContext) CXXCtorInitializer(
6014 ToContext, ToField, Import(From->getMemberLocation()),
6015 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
6016 } else if (From->isIndirectMemberInitializer()) {
6017 IndirectFieldDecl *ToIField = llvm::cast_or_null<IndirectFieldDecl>(
6018 Import(From->getIndirectMember()));
6019 if (!ToIField && From->getIndirectMember())
6022 return new (ToContext) CXXCtorInitializer(
6023 ToContext, ToIField, Import(From->getMemberLocation()),
6024 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
6025 } else if (From->isDelegatingInitializer()) {
6026 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
6027 if (!ToTInfo && From->getTypeSourceInfo())
6030 return new (ToContext)
6031 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
6032 ToExpr, Import(From->getRParenLoc()));
6039 CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
6040 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
6041 if (Pos != ImportedCXXBaseSpecifiers.end())
6044 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
6045 Import(BaseSpec->getSourceRange()),
6046 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
6047 BaseSpec->getAccessSpecifierAsWritten(),
6048 Import(BaseSpec->getTypeSourceInfo()),
6049 Import(BaseSpec->getEllipsisLoc()));
6050 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
6054 void ASTImporter::ImportDefinition(Decl *From) {
6055 Decl *To = Import(From);
6059 if (DeclContext *FromDC = cast<DeclContext>(From)) {
6060 ASTNodeImporter Importer(*this);
6062 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
6063 if (!ToRecord->getDefinition()) {
6064 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
6065 ASTNodeImporter::IDK_Everything);
6070 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
6071 if (!ToEnum->getDefinition()) {
6072 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
6073 ASTNodeImporter::IDK_Everything);
6078 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
6079 if (!ToIFace->getDefinition()) {
6080 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
6081 ASTNodeImporter::IDK_Everything);
6086 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
6087 if (!ToProto->getDefinition()) {
6088 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
6089 ASTNodeImporter::IDK_Everything);
6094 Importer.ImportDeclContext(FromDC, true);
6098 DeclarationName ASTImporter::Import(DeclarationName FromName) {
6100 return DeclarationName();
6102 switch (FromName.getNameKind()) {
6103 case DeclarationName::Identifier:
6104 return Import(FromName.getAsIdentifierInfo());
6106 case DeclarationName::ObjCZeroArgSelector:
6107 case DeclarationName::ObjCOneArgSelector:
6108 case DeclarationName::ObjCMultiArgSelector:
6109 return Import(FromName.getObjCSelector());
6111 case DeclarationName::CXXConstructorName: {
6112 QualType T = Import(FromName.getCXXNameType());
6114 return DeclarationName();
6116 return ToContext.DeclarationNames.getCXXConstructorName(
6117 ToContext.getCanonicalType(T));
6120 case DeclarationName::CXXDestructorName: {
6121 QualType T = Import(FromName.getCXXNameType());
6123 return DeclarationName();
6125 return ToContext.DeclarationNames.getCXXDestructorName(
6126 ToContext.getCanonicalType(T));
6129 case DeclarationName::CXXDeductionGuideName: {
6130 TemplateDecl *Template = cast_or_null<TemplateDecl>(
6131 Import(FromName.getCXXDeductionGuideTemplate()));
6133 return DeclarationName();
6134 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
6137 case DeclarationName::CXXConversionFunctionName: {
6138 QualType T = Import(FromName.getCXXNameType());
6140 return DeclarationName();
6142 return ToContext.DeclarationNames.getCXXConversionFunctionName(
6143 ToContext.getCanonicalType(T));
6146 case DeclarationName::CXXOperatorName:
6147 return ToContext.DeclarationNames.getCXXOperatorName(
6148 FromName.getCXXOverloadedOperator());
6150 case DeclarationName::CXXLiteralOperatorName:
6151 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
6152 Import(FromName.getCXXLiteralIdentifier()));
6154 case DeclarationName::CXXUsingDirective:
6156 return DeclarationName::getUsingDirectiveName();
6159 llvm_unreachable("Invalid DeclarationName Kind!");
6162 IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
6166 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
6168 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
6169 ToId->setBuiltinID(FromId->getBuiltinID());
6174 Selector ASTImporter::Import(Selector FromSel) {
6175 if (FromSel.isNull())
6178 SmallVector<IdentifierInfo *, 4> Idents;
6179 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
6180 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
6181 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
6182 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
6185 DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
6189 unsigned NumDecls) {
6193 DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
6194 if (LastDiagFromFrom)
6195 ToContext.getDiagnostics().notePriorDiagnosticFrom(
6196 FromContext.getDiagnostics());
6197 LastDiagFromFrom = false;
6198 return ToContext.getDiagnostics().Report(Loc, DiagID);
6201 DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
6202 if (!LastDiagFromFrom)
6203 FromContext.getDiagnostics().notePriorDiagnosticFrom(
6204 ToContext.getDiagnostics());
6205 LastDiagFromFrom = true;
6206 return FromContext.getDiagnostics().Report(Loc, DiagID);
6209 void ASTImporter::CompleteDecl (Decl *D) {
6210 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
6211 if (!ID->getDefinition())
6212 ID->startDefinition();
6214 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
6215 if (!PD->getDefinition())
6216 PD->startDefinition();
6218 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
6219 if (!TD->getDefinition() && !TD->isBeingDefined()) {
6220 TD->startDefinition();
6221 TD->setCompleteDefinition(true);
6225 assert (0 && "CompleteDecl called on a Decl that can't be completed");
6229 Decl *ASTImporter::Imported(Decl *From, Decl *To) {
6230 if (From->hasAttrs()) {
6231 for (Attr *FromAttr : From->getAttrs())
6232 To->addAttr(FromAttr->clone(To->getASTContext()));
6234 if (From->isUsed()) {
6237 if (From->isImplicit()) {
6240 ImportedDecls[From] = To;
6244 bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
6246 llvm::DenseMap<const Type *, const Type *>::iterator Pos
6247 = ImportedTypes.find(From.getTypePtr());
6248 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
6251 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
6253 return Ctx.IsStructurallyEquivalent(From, To);