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);
325 //----------------------------------------------------------------------------
327 //----------------------------------------------------------------------------
329 using namespace clang;
331 QualType ASTNodeImporter::VisitType(const Type *T) {
332 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
333 << T->getTypeClassName();
337 QualType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
338 QualType UnderlyingType = Importer.Import(T->getValueType());
339 if(UnderlyingType.isNull())
342 return Importer.getToContext().getAtomicType(UnderlyingType);
345 QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
346 switch (T->getKind()) {
347 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
348 case BuiltinType::Id: \
349 return Importer.getToContext().SingletonId;
350 #include "clang/Basic/OpenCLImageTypes.def"
351 #define SHARED_SINGLETON_TYPE(Expansion)
352 #define BUILTIN_TYPE(Id, SingletonId) \
353 case BuiltinType::Id: return Importer.getToContext().SingletonId;
354 #include "clang/AST/BuiltinTypes.def"
356 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
357 // context supports C++.
359 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
360 // context supports ObjC.
362 case BuiltinType::Char_U:
363 // The context we're importing from has an unsigned 'char'. If we're
364 // importing into a context with a signed 'char', translate to
365 // 'unsigned char' instead.
366 if (Importer.getToContext().getLangOpts().CharIsSigned)
367 return Importer.getToContext().UnsignedCharTy;
369 return Importer.getToContext().CharTy;
371 case BuiltinType::Char_S:
372 // The context we're importing from has an unsigned 'char'. If we're
373 // importing into a context with a signed 'char', translate to
374 // 'unsigned char' instead.
375 if (!Importer.getToContext().getLangOpts().CharIsSigned)
376 return Importer.getToContext().SignedCharTy;
378 return Importer.getToContext().CharTy;
380 case BuiltinType::WChar_S:
381 case BuiltinType::WChar_U:
382 // FIXME: If not in C++, shall we translate to the C equivalent of
384 return Importer.getToContext().WCharTy;
387 llvm_unreachable("Invalid BuiltinType Kind!");
390 QualType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
391 QualType OrigT = Importer.Import(T->getOriginalType());
395 return Importer.getToContext().getDecayedType(OrigT);
398 QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
399 QualType ToElementType = Importer.Import(T->getElementType());
400 if (ToElementType.isNull())
403 return Importer.getToContext().getComplexType(ToElementType);
406 QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
407 QualType ToPointeeType = Importer.Import(T->getPointeeType());
408 if (ToPointeeType.isNull())
411 return Importer.getToContext().getPointerType(ToPointeeType);
414 QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
415 // FIXME: Check for blocks support in "to" context.
416 QualType ToPointeeType = Importer.Import(T->getPointeeType());
417 if (ToPointeeType.isNull())
420 return Importer.getToContext().getBlockPointerType(ToPointeeType);
424 ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
425 // FIXME: Check for C++ support in "to" context.
426 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
427 if (ToPointeeType.isNull())
430 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
434 ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
435 // FIXME: Check for C++0x support in "to" context.
436 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
437 if (ToPointeeType.isNull())
440 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
443 QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
444 // FIXME: Check for C++ support in "to" context.
445 QualType ToPointeeType = Importer.Import(T->getPointeeType());
446 if (ToPointeeType.isNull())
449 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
450 return Importer.getToContext().getMemberPointerType(ToPointeeType,
451 ClassType.getTypePtr());
454 QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
455 QualType ToElementType = Importer.Import(T->getElementType());
456 if (ToElementType.isNull())
459 return Importer.getToContext().getConstantArrayType(ToElementType,
461 T->getSizeModifier(),
462 T->getIndexTypeCVRQualifiers());
466 ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
467 QualType ToElementType = Importer.Import(T->getElementType());
468 if (ToElementType.isNull())
471 return Importer.getToContext().getIncompleteArrayType(ToElementType,
472 T->getSizeModifier(),
473 T->getIndexTypeCVRQualifiers());
476 QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
477 QualType ToElementType = Importer.Import(T->getElementType());
478 if (ToElementType.isNull())
481 Expr *Size = Importer.Import(T->getSizeExpr());
485 SourceRange Brackets = Importer.Import(T->getBracketsRange());
486 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
487 T->getSizeModifier(),
488 T->getIndexTypeCVRQualifiers(),
492 QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
493 QualType ToElementType = Importer.Import(T->getElementType());
494 if (ToElementType.isNull())
497 return Importer.getToContext().getVectorType(ToElementType,
502 QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
503 QualType ToElementType = Importer.Import(T->getElementType());
504 if (ToElementType.isNull())
507 return Importer.getToContext().getExtVectorType(ToElementType,
508 T->getNumElements());
512 ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
513 // FIXME: What happens if we're importing a function without a prototype
514 // into C++? Should we make it variadic?
515 QualType ToResultType = Importer.Import(T->getReturnType());
516 if (ToResultType.isNull())
519 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
523 QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
524 QualType ToResultType = Importer.Import(T->getReturnType());
525 if (ToResultType.isNull())
528 // Import argument types
529 SmallVector<QualType, 4> ArgTypes;
530 for (const auto &A : T->param_types()) {
531 QualType ArgType = Importer.Import(A);
532 if (ArgType.isNull())
534 ArgTypes.push_back(ArgType);
537 // Import exception types
538 SmallVector<QualType, 4> ExceptionTypes;
539 for (const auto &E : T->exceptions()) {
540 QualType ExceptionType = Importer.Import(E);
541 if (ExceptionType.isNull())
543 ExceptionTypes.push_back(ExceptionType);
546 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
547 FunctionProtoType::ExtProtoInfo ToEPI;
549 ToEPI.ExtInfo = FromEPI.ExtInfo;
550 ToEPI.Variadic = FromEPI.Variadic;
551 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
552 ToEPI.TypeQuals = FromEPI.TypeQuals;
553 ToEPI.RefQualifier = FromEPI.RefQualifier;
554 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
555 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
556 ToEPI.ExceptionSpec.NoexceptExpr =
557 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
558 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
559 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
560 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
561 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
563 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
566 QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
567 QualType ToInnerType = Importer.Import(T->getInnerType());
568 if (ToInnerType.isNull())
571 return Importer.getToContext().getParenType(ToInnerType);
574 QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
575 TypedefNameDecl *ToDecl
576 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
580 return Importer.getToContext().getTypeDeclType(ToDecl);
583 QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
584 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
588 return Importer.getToContext().getTypeOfExprType(ToExpr);
591 QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
592 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
593 if (ToUnderlyingType.isNull())
596 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
599 QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
600 // FIXME: Make sure that the "to" context supports C++0x!
601 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
605 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
606 if (UnderlyingType.isNull())
609 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
612 QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
613 QualType ToBaseType = Importer.Import(T->getBaseType());
614 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
615 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
618 return Importer.getToContext().getUnaryTransformType(ToBaseType,
623 QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
624 // FIXME: Make sure that the "to" context supports C++11!
625 QualType FromDeduced = T->getDeducedType();
627 if (!FromDeduced.isNull()) {
628 ToDeduced = Importer.Import(FromDeduced);
629 if (ToDeduced.isNull())
633 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
634 /*IsDependent*/false);
637 QualType ASTNodeImporter::VisitInjectedClassNameType(
638 const InjectedClassNameType *T) {
639 CXXRecordDecl *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl()));
643 QualType InjType = Importer.Import(T->getInjectedSpecializationType());
644 if (InjType.isNull())
647 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
648 // See comments in InjectedClassNameType definition for details
649 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
651 TypeAlignmentInBits = 4,
652 TypeAlignment = 1 << TypeAlignmentInBits
655 return QualType(new (Importer.getToContext(), TypeAlignment)
656 InjectedClassNameType(D, InjType), 0);
659 QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
661 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
665 return Importer.getToContext().getTagDeclType(ToDecl);
668 QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
670 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
674 return Importer.getToContext().getTagDeclType(ToDecl);
677 QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
678 QualType FromModifiedType = T->getModifiedType();
679 QualType FromEquivalentType = T->getEquivalentType();
680 QualType ToModifiedType;
681 QualType ToEquivalentType;
683 if (!FromModifiedType.isNull()) {
684 ToModifiedType = Importer.Import(FromModifiedType);
685 if (ToModifiedType.isNull())
688 if (!FromEquivalentType.isNull()) {
689 ToEquivalentType = Importer.Import(FromEquivalentType);
690 if (ToEquivalentType.isNull())
694 return Importer.getToContext().getAttributedType(T->getAttrKind(),
695 ToModifiedType, ToEquivalentType);
699 QualType ASTNodeImporter::VisitTemplateTypeParmType(
700 const TemplateTypeParmType *T) {
701 TemplateTypeParmDecl *ParmDecl =
702 cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl()));
703 if (!ParmDecl && T->getDecl())
706 return Importer.getToContext().getTemplateTypeParmType(
707 T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl);
710 QualType ASTNodeImporter::VisitSubstTemplateTypeParmType(
711 const SubstTemplateTypeParmType *T) {
712 const TemplateTypeParmType *Replaced =
713 cast_or_null<TemplateTypeParmType>(Importer.Import(
714 QualType(T->getReplacedParameter(), 0)).getTypePtr());
718 QualType Replacement = Importer.Import(T->getReplacementType());
719 if (Replacement.isNull())
721 Replacement = Replacement.getCanonicalType();
723 return Importer.getToContext().getSubstTemplateTypeParmType(
724 Replaced, Replacement);
727 QualType ASTNodeImporter::VisitTemplateSpecializationType(
728 const TemplateSpecializationType *T) {
729 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
730 if (ToTemplate.isNull())
733 SmallVector<TemplateArgument, 2> ToTemplateArgs;
734 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
737 QualType ToCanonType;
738 if (!QualType(T, 0).isCanonical()) {
739 QualType FromCanonType
740 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
741 ToCanonType =Importer.Import(FromCanonType);
742 if (ToCanonType.isNull())
745 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
750 QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
751 NestedNameSpecifier *ToQualifier = nullptr;
752 // Note: the qualifier in an ElaboratedType is optional.
753 if (T->getQualifier()) {
754 ToQualifier = Importer.Import(T->getQualifier());
759 QualType ToNamedType = Importer.Import(T->getNamedType());
760 if (ToNamedType.isNull())
763 return Importer.getToContext().getElaboratedType(T->getKeyword(),
764 ToQualifier, ToNamedType);
767 QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
768 ObjCInterfaceDecl *Class
769 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
773 return Importer.getToContext().getObjCInterfaceType(Class);
776 QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
777 QualType ToBaseType = Importer.Import(T->getBaseType());
778 if (ToBaseType.isNull())
781 SmallVector<QualType, 4> TypeArgs;
782 for (auto TypeArg : T->getTypeArgsAsWritten()) {
783 QualType ImportedTypeArg = Importer.Import(TypeArg);
784 if (ImportedTypeArg.isNull())
787 TypeArgs.push_back(ImportedTypeArg);
790 SmallVector<ObjCProtocolDecl *, 4> Protocols;
791 for (auto *P : T->quals()) {
792 ObjCProtocolDecl *Protocol
793 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
796 Protocols.push_back(Protocol);
799 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
801 T->isKindOfTypeAsWritten());
805 ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
806 QualType ToPointeeType = Importer.Import(T->getPointeeType());
807 if (ToPointeeType.isNull())
810 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
813 //----------------------------------------------------------------------------
814 // Import Declarations
815 //----------------------------------------------------------------------------
816 bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
817 DeclContext *&LexicalDC,
818 DeclarationName &Name,
820 SourceLocation &Loc) {
821 // Import the context of this declaration.
822 DC = Importer.ImportContext(D->getDeclContext());
827 if (D->getDeclContext() != D->getLexicalDeclContext()) {
828 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
833 // Import the name of this declaration.
834 Name = Importer.Import(D->getDeclName());
835 if (D->getDeclName() && !Name)
838 // Import the location of this declaration.
839 Loc = Importer.Import(D->getLocation());
840 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
844 void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
849 ToD = Importer.Import(FromD);
854 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
855 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
856 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
857 ImportDefinition(FromRecord, ToRecord);
863 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
864 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
865 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
866 ImportDefinition(FromEnum, ToEnum);
874 ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
875 DeclarationNameInfo& To) {
876 // NOTE: To.Name and To.Loc are already imported.
877 // We only have to import To.LocInfo.
878 switch (To.getName().getNameKind()) {
879 case DeclarationName::Identifier:
880 case DeclarationName::ObjCZeroArgSelector:
881 case DeclarationName::ObjCOneArgSelector:
882 case DeclarationName::ObjCMultiArgSelector:
883 case DeclarationName::CXXUsingDirective:
884 case DeclarationName::CXXDeductionGuideName:
887 case DeclarationName::CXXOperatorName: {
888 SourceRange Range = From.getCXXOperatorNameRange();
889 To.setCXXOperatorNameRange(Importer.Import(Range));
892 case DeclarationName::CXXLiteralOperatorName: {
893 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
894 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
897 case DeclarationName::CXXConstructorName:
898 case DeclarationName::CXXDestructorName:
899 case DeclarationName::CXXConversionFunctionName: {
900 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
901 To.setNamedTypeInfo(Importer.Import(FromTInfo));
905 llvm_unreachable("Unknown name kind.");
908 void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
909 if (Importer.isMinimalImport() && !ForceImport) {
910 Importer.ImportContext(FromDC);
914 for (auto *From : FromDC->decls())
915 Importer.Import(From);
918 bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
919 ImportDefinitionKind Kind) {
920 if (To->getDefinition() || To->isBeingDefined()) {
921 if (Kind == IDK_Everything)
922 ImportDeclContext(From, /*ForceImport=*/true);
927 To->startDefinition();
930 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
931 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
933 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
934 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
935 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
936 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
937 ToData.Aggregate = FromData.Aggregate;
938 ToData.PlainOldData = FromData.PlainOldData;
939 ToData.Empty = FromData.Empty;
940 ToData.Polymorphic = FromData.Polymorphic;
941 ToData.Abstract = FromData.Abstract;
942 ToData.IsStandardLayout = FromData.IsStandardLayout;
943 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
944 ToData.HasPrivateFields = FromData.HasPrivateFields;
945 ToData.HasProtectedFields = FromData.HasProtectedFields;
946 ToData.HasPublicFields = FromData.HasPublicFields;
947 ToData.HasMutableFields = FromData.HasMutableFields;
948 ToData.HasVariantMembers = FromData.HasVariantMembers;
949 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
950 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
951 ToData.HasUninitializedReferenceMember
952 = FromData.HasUninitializedReferenceMember;
953 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
954 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
955 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
956 ToData.NeedOverloadResolutionForMoveConstructor
957 = FromData.NeedOverloadResolutionForMoveConstructor;
958 ToData.NeedOverloadResolutionForMoveAssignment
959 = FromData.NeedOverloadResolutionForMoveAssignment;
960 ToData.NeedOverloadResolutionForDestructor
961 = FromData.NeedOverloadResolutionForDestructor;
962 ToData.DefaultedMoveConstructorIsDeleted
963 = FromData.DefaultedMoveConstructorIsDeleted;
964 ToData.DefaultedMoveAssignmentIsDeleted
965 = FromData.DefaultedMoveAssignmentIsDeleted;
966 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
967 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
968 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
969 ToData.HasConstexprNonCopyMoveConstructor
970 = FromData.HasConstexprNonCopyMoveConstructor;
971 ToData.HasDefaultedDefaultConstructor
972 = FromData.HasDefaultedDefaultConstructor;
973 ToData.DefaultedDefaultConstructorIsConstexpr
974 = FromData.DefaultedDefaultConstructorIsConstexpr;
975 ToData.HasConstexprDefaultConstructor
976 = FromData.HasConstexprDefaultConstructor;
977 ToData.HasNonLiteralTypeFieldsOrBases
978 = FromData.HasNonLiteralTypeFieldsOrBases;
979 // ComputedVisibleConversions not imported.
980 ToData.UserProvidedDefaultConstructor
981 = FromData.UserProvidedDefaultConstructor;
982 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
983 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
984 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
985 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
986 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
987 ToData.ImplicitCopyAssignmentHasConstParam
988 = FromData.ImplicitCopyAssignmentHasConstParam;
989 ToData.HasDeclaredCopyConstructorWithConstParam
990 = FromData.HasDeclaredCopyConstructorWithConstParam;
991 ToData.HasDeclaredCopyAssignmentWithConstParam
992 = FromData.HasDeclaredCopyAssignmentWithConstParam;
993 ToData.IsLambda = FromData.IsLambda;
995 SmallVector<CXXBaseSpecifier *, 4> Bases;
996 for (const auto &Base1 : FromCXX->bases()) {
997 QualType T = Importer.Import(Base1.getType());
1001 SourceLocation EllipsisLoc;
1002 if (Base1.isPackExpansion())
1003 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
1005 // Ensure that we have a definition for the base.
1006 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
1009 new (Importer.getToContext())
1010 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
1012 Base1.isBaseOfClass(),
1013 Base1.getAccessSpecifierAsWritten(),
1014 Importer.Import(Base1.getTypeSourceInfo()),
1018 ToCXX->setBases(Bases.data(), Bases.size());
1021 if (shouldForceImportDeclContext(Kind))
1022 ImportDeclContext(From, /*ForceImport=*/true);
1024 To->completeDefinition();
1028 bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
1029 ImportDefinitionKind Kind) {
1030 if (To->getAnyInitializer())
1033 // FIXME: Can we really import any initializer? Alternatively, we could force
1034 // ourselves to import every declaration of a variable and then only use
1036 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
1038 // FIXME: Other bits to merge?
1043 bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
1044 ImportDefinitionKind Kind) {
1045 if (To->getDefinition() || To->isBeingDefined()) {
1046 if (Kind == IDK_Everything)
1047 ImportDeclContext(From, /*ForceImport=*/true);
1051 To->startDefinition();
1053 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1057 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1058 if (ToPromotionType.isNull())
1061 if (shouldForceImportDeclContext(Kind))
1062 ImportDeclContext(From, /*ForceImport=*/true);
1064 // FIXME: we might need to merge the number of positive or negative bits
1065 // if the enumerator lists don't match.
1066 To->completeDefinition(T, ToPromotionType,
1067 From->getNumPositiveBits(),
1068 From->getNumNegativeBits());
1072 TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1073 TemplateParameterList *Params) {
1074 SmallVector<NamedDecl *, 4> ToParams(Params->size());
1075 if (ImportContainerChecked(*Params, ToParams))
1078 Expr *ToRequiresClause;
1079 if (Expr *const R = Params->getRequiresClause()) {
1080 ToRequiresClause = Importer.Import(R);
1081 if (!ToRequiresClause)
1084 ToRequiresClause = nullptr;
1087 return TemplateParameterList::Create(Importer.getToContext(),
1088 Importer.Import(Params->getTemplateLoc()),
1089 Importer.Import(Params->getLAngleLoc()),
1091 Importer.Import(Params->getRAngleLoc()),
1096 ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1097 switch (From.getKind()) {
1098 case TemplateArgument::Null:
1099 return TemplateArgument();
1101 case TemplateArgument::Type: {
1102 QualType ToType = Importer.Import(From.getAsType());
1103 if (ToType.isNull())
1104 return TemplateArgument();
1105 return TemplateArgument(ToType);
1108 case TemplateArgument::Integral: {
1109 QualType ToType = Importer.Import(From.getIntegralType());
1110 if (ToType.isNull())
1111 return TemplateArgument();
1112 return TemplateArgument(From, ToType);
1115 case TemplateArgument::Declaration: {
1116 ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
1117 QualType ToType = Importer.Import(From.getParamTypeForDecl());
1118 if (!To || ToType.isNull())
1119 return TemplateArgument();
1120 return TemplateArgument(To, ToType);
1123 case TemplateArgument::NullPtr: {
1124 QualType ToType = Importer.Import(From.getNullPtrType());
1125 if (ToType.isNull())
1126 return TemplateArgument();
1127 return TemplateArgument(ToType, /*isNullPtr*/true);
1130 case TemplateArgument::Template: {
1131 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1132 if (ToTemplate.isNull())
1133 return TemplateArgument();
1135 return TemplateArgument(ToTemplate);
1138 case TemplateArgument::TemplateExpansion: {
1139 TemplateName ToTemplate
1140 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1141 if (ToTemplate.isNull())
1142 return TemplateArgument();
1144 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
1147 case TemplateArgument::Expression:
1148 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1149 return TemplateArgument(ToExpr);
1150 return TemplateArgument();
1152 case TemplateArgument::Pack: {
1153 SmallVector<TemplateArgument, 2> ToPack;
1154 ToPack.reserve(From.pack_size());
1155 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1156 return TemplateArgument();
1158 return TemplateArgument(
1159 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
1163 llvm_unreachable("Invalid template argument kind");
1166 TemplateArgumentLoc ASTNodeImporter::ImportTemplateArgumentLoc(
1167 const TemplateArgumentLoc &TALoc, bool &Error) {
1169 TemplateArgument Arg = ImportTemplateArgument(TALoc.getArgument());
1170 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
1171 TemplateArgumentLocInfo ToInfo;
1172 if (Arg.getKind() == TemplateArgument::Expression) {
1173 Expr *E = Importer.Import(FromInfo.getAsExpr());
1174 ToInfo = TemplateArgumentLocInfo(E);
1177 } else if (Arg.getKind() == TemplateArgument::Type) {
1178 if (TypeSourceInfo *TSI = Importer.Import(FromInfo.getAsTypeSourceInfo()))
1179 ToInfo = TemplateArgumentLocInfo(TSI);
1183 ToInfo = TemplateArgumentLocInfo(
1184 Importer.Import(FromInfo.getTemplateQualifierLoc()),
1185 Importer.Import(FromInfo.getTemplateNameLoc()),
1186 Importer.Import(FromInfo.getTemplateEllipsisLoc()));
1188 return TemplateArgumentLoc(Arg, ToInfo);
1191 bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1192 unsigned NumFromArgs,
1193 SmallVectorImpl<TemplateArgument> &ToArgs) {
1194 for (unsigned I = 0; I != NumFromArgs; ++I) {
1195 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1196 if (To.isNull() && !FromArgs[I].isNull())
1199 ToArgs.push_back(To);
1205 bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
1206 RecordDecl *ToRecord, bool Complain) {
1207 // Eliminate a potential failure point where we attempt to re-import
1208 // something we're trying to import while completing ToRecord.
1209 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1211 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
1213 ToRecord = ToOriginRecord;
1216 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1217 ToRecord->getASTContext(),
1218 Importer.getNonEquivalentDecls(),
1220 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
1223 bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
1225 StructuralEquivalenceContext Ctx(
1226 Importer.getFromContext(), Importer.getToContext(),
1227 Importer.getNonEquivalentDecls(), false, Complain);
1228 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
1231 bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
1232 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1233 Importer.getToContext(),
1234 Importer.getNonEquivalentDecls());
1235 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
1238 bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
1239 EnumConstantDecl *ToEC)
1241 const llvm::APSInt &FromVal = FromEC->getInitVal();
1242 const llvm::APSInt &ToVal = ToEC->getInitVal();
1244 return FromVal.isSigned() == ToVal.isSigned() &&
1245 FromVal.getBitWidth() == ToVal.getBitWidth() &&
1249 bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
1250 ClassTemplateDecl *To) {
1251 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1252 Importer.getToContext(),
1253 Importer.getNonEquivalentDecls());
1254 return Ctx.IsStructurallyEquivalent(From, To);
1257 bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
1258 VarTemplateDecl *To) {
1259 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1260 Importer.getToContext(),
1261 Importer.getNonEquivalentDecls());
1262 return Ctx.IsStructurallyEquivalent(From, To);
1265 Decl *ASTNodeImporter::VisitDecl(Decl *D) {
1266 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1267 << D->getDeclKindName();
1271 Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
1272 TranslationUnitDecl *ToD =
1273 Importer.getToContext().getTranslationUnitDecl();
1275 Importer.Imported(D, ToD);
1280 Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
1282 SourceLocation Loc = Importer.Import(D->getLocation());
1283 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
1285 // Import the context of this declaration.
1286 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1290 AccessSpecDecl *accessSpecDecl
1291 = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(),
1294 if (!accessSpecDecl)
1297 // Lexical DeclContext and Semantic DeclContext
1298 // is always the same for the accessSpec.
1299 accessSpecDecl->setLexicalDeclContext(DC);
1300 DC->addDeclInternal(accessSpecDecl);
1302 return accessSpecDecl;
1305 Decl *ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
1306 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1310 DeclContext *LexicalDC = DC;
1312 // Import the location of this declaration.
1313 SourceLocation Loc = Importer.Import(D->getLocation());
1315 Expr *AssertExpr = Importer.Import(D->getAssertExpr());
1319 StringLiteral *FromMsg = D->getMessage();
1320 StringLiteral *ToMsg = cast_or_null<StringLiteral>(Importer.Import(FromMsg));
1321 if (!ToMsg && FromMsg)
1324 StaticAssertDecl *ToD = StaticAssertDecl::Create(
1325 Importer.getToContext(), DC, Loc, AssertExpr, ToMsg,
1326 Importer.Import(D->getRParenLoc()), D->isFailed());
1328 ToD->setLexicalDeclContext(LexicalDC);
1329 LexicalDC->addDeclInternal(ToD);
1330 Importer.Imported(D, ToD);
1334 Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1335 // Import the major distinguishing characteristics of this namespace.
1336 DeclContext *DC, *LexicalDC;
1337 DeclarationName Name;
1340 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1345 NamespaceDecl *MergeWithNamespace = nullptr;
1347 // This is an anonymous namespace. Adopt an existing anonymous
1348 // namespace if we can.
1349 // FIXME: Not testable.
1350 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1351 MergeWithNamespace = TU->getAnonymousNamespace();
1353 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1355 SmallVector<NamedDecl *, 4> ConflictingDecls;
1356 SmallVector<NamedDecl *, 2> FoundDecls;
1357 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
1358 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1359 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
1362 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
1363 MergeWithNamespace = FoundNS;
1364 ConflictingDecls.clear();
1368 ConflictingDecls.push_back(FoundDecls[I]);
1371 if (!ConflictingDecls.empty()) {
1372 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
1373 ConflictingDecls.data(),
1374 ConflictingDecls.size());
1378 // Create the "to" namespace, if needed.
1379 NamespaceDecl *ToNamespace = MergeWithNamespace;
1381 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
1383 Importer.Import(D->getLocStart()),
1384 Loc, Name.getAsIdentifierInfo(),
1385 /*PrevDecl=*/nullptr);
1386 ToNamespace->setLexicalDeclContext(LexicalDC);
1387 LexicalDC->addDeclInternal(ToNamespace);
1389 // If this is an anonymous namespace, register it as the anonymous
1390 // namespace within its context.
1392 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1393 TU->setAnonymousNamespace(ToNamespace);
1395 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1398 Importer.Imported(D, ToNamespace);
1400 ImportDeclContext(D);
1405 Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
1406 // Import the major distinguishing characteristics of this typedef.
1407 DeclContext *DC, *LexicalDC;
1408 DeclarationName Name;
1411 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1416 // If this typedef is not in block scope, determine whether we've
1417 // seen a typedef with the same name (that we can merge with) or any
1418 // other entity by that name (which name lookup could conflict with).
1419 if (!DC->isFunctionOrMethod()) {
1420 SmallVector<NamedDecl *, 4> ConflictingDecls;
1421 unsigned IDNS = Decl::IDNS_Ordinary;
1422 SmallVector<NamedDecl *, 2> FoundDecls;
1423 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
1424 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1425 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
1427 if (TypedefNameDecl *FoundTypedef =
1428 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
1429 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1430 FoundTypedef->getUnderlyingType()))
1431 return Importer.Imported(D, FoundTypedef);
1434 ConflictingDecls.push_back(FoundDecls[I]);
1437 if (!ConflictingDecls.empty()) {
1438 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1439 ConflictingDecls.data(),
1440 ConflictingDecls.size());
1446 // Import the underlying type of this typedef;
1447 QualType T = Importer.Import(D->getUnderlyingType());
1451 // Create the new typedef node.
1452 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1453 SourceLocation StartL = Importer.Import(D->getLocStart());
1454 TypedefNameDecl *ToTypedef;
1456 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
1458 Name.getAsIdentifierInfo(),
1461 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1463 Name.getAsIdentifierInfo(),
1466 ToTypedef->setAccess(D->getAccess());
1467 ToTypedef->setLexicalDeclContext(LexicalDC);
1468 Importer.Imported(D, ToTypedef);
1469 LexicalDC->addDeclInternal(ToTypedef);
1474 Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1475 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
1478 Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
1479 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
1482 Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
1483 // Import the major distinguishing characteristics of this label.
1484 DeclContext *DC, *LexicalDC;
1485 DeclarationName Name;
1488 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1493 assert(LexicalDC->isFunctionOrMethod());
1495 LabelDecl *ToLabel = D->isGnuLocal()
1496 ? LabelDecl::Create(Importer.getToContext(),
1497 DC, Importer.Import(D->getLocation()),
1498 Name.getAsIdentifierInfo(),
1499 Importer.Import(D->getLocStart()))
1500 : LabelDecl::Create(Importer.getToContext(),
1501 DC, Importer.Import(D->getLocation()),
1502 Name.getAsIdentifierInfo());
1503 Importer.Imported(D, ToLabel);
1505 LabelStmt *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt()));
1509 ToLabel->setStmt(Label);
1510 ToLabel->setLexicalDeclContext(LexicalDC);
1511 LexicalDC->addDeclInternal(ToLabel);
1515 Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1516 // Import the major distinguishing characteristics of this enum.
1517 DeclContext *DC, *LexicalDC;
1518 DeclarationName Name;
1521 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1526 // Figure out what enum name we're looking for.
1527 unsigned IDNS = Decl::IDNS_Tag;
1528 DeclarationName SearchName = Name;
1529 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1530 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
1531 IDNS = Decl::IDNS_Ordinary;
1532 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
1533 IDNS |= Decl::IDNS_Ordinary;
1535 // We may already have an enum of the same name; try to find and match it.
1536 if (!DC->isFunctionOrMethod() && SearchName) {
1537 SmallVector<NamedDecl *, 4> ConflictingDecls;
1538 SmallVector<NamedDecl *, 2> FoundDecls;
1539 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
1540 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1541 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
1544 Decl *Found = FoundDecls[I];
1545 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
1546 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1547 Found = Tag->getDecl();
1550 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
1551 if (IsStructuralMatch(D, FoundEnum))
1552 return Importer.Imported(D, FoundEnum);
1555 ConflictingDecls.push_back(FoundDecls[I]);
1558 if (!ConflictingDecls.empty()) {
1559 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1560 ConflictingDecls.data(),
1561 ConflictingDecls.size());
1565 // Create the enum declaration.
1566 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
1567 Importer.Import(D->getLocStart()),
1568 Loc, Name.getAsIdentifierInfo(), nullptr,
1569 D->isScoped(), D->isScopedUsingClassTag(),
1571 // Import the qualifier, if any.
1572 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
1573 D2->setAccess(D->getAccess());
1574 D2->setLexicalDeclContext(LexicalDC);
1575 Importer.Imported(D, D2);
1576 LexicalDC->addDeclInternal(D2);
1578 // Import the integer type.
1579 QualType ToIntegerType = Importer.Import(D->getIntegerType());
1580 if (ToIntegerType.isNull())
1582 D2->setIntegerType(ToIntegerType);
1584 // Import the definition
1585 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
1591 Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
1592 // If this record has a definition in the translation unit we're coming from,
1593 // but this particular declaration is not that definition, import the
1594 // definition and map to that.
1595 TagDecl *Definition = D->getDefinition();
1596 if (Definition && Definition != D) {
1597 Decl *ImportedDef = Importer.Import(Definition);
1601 return Importer.Imported(D, ImportedDef);
1604 // Import the major distinguishing characteristics of this record.
1605 DeclContext *DC, *LexicalDC;
1606 DeclarationName Name;
1609 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1614 // Figure out what structure name we're looking for.
1615 unsigned IDNS = Decl::IDNS_Tag;
1616 DeclarationName SearchName = Name;
1617 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1618 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
1619 IDNS = Decl::IDNS_Ordinary;
1620 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
1621 IDNS |= Decl::IDNS_Ordinary;
1623 // We may already have a record of the same name; try to find and match it.
1624 RecordDecl *AdoptDecl = nullptr;
1625 if (!DC->isFunctionOrMethod()) {
1626 SmallVector<NamedDecl *, 4> ConflictingDecls;
1627 SmallVector<NamedDecl *, 2> FoundDecls;
1628 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
1629 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1630 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
1633 Decl *Found = FoundDecls[I];
1634 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
1635 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1636 Found = Tag->getDecl();
1639 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
1640 if (D->isAnonymousStructOrUnion() &&
1641 FoundRecord->isAnonymousStructOrUnion()) {
1642 // If both anonymous structs/unions are in a record context, make sure
1643 // they occur in the same location in the context records.
1644 if (Optional<unsigned> Index1 =
1645 StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(
1647 if (Optional<unsigned> Index2 = StructuralEquivalenceContext::
1648 findUntaggedStructOrUnionIndex(FoundRecord)) {
1649 if (*Index1 != *Index2)
1655 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
1656 if ((SearchName && !D->isCompleteDefinition())
1657 || (D->isCompleteDefinition() &&
1658 D->isAnonymousStructOrUnion()
1659 == FoundDef->isAnonymousStructOrUnion() &&
1660 IsStructuralMatch(D, FoundDef))) {
1661 // The record types structurally match, or the "from" translation
1662 // unit only had a forward declaration anyway; call it the same
1664 // FIXME: For C++, we should also merge methods here.
1665 return Importer.Imported(D, FoundDef);
1667 } else if (!D->isCompleteDefinition()) {
1668 // We have a forward declaration of this type, so adopt that forward
1669 // declaration rather than building a new one.
1671 // If one or both can be completed from external storage then try one
1672 // last time to complete and compare them before doing this.
1674 if (FoundRecord->hasExternalLexicalStorage() &&
1675 !FoundRecord->isCompleteDefinition())
1676 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
1677 if (D->hasExternalLexicalStorage())
1678 D->getASTContext().getExternalSource()->CompleteType(D);
1680 if (FoundRecord->isCompleteDefinition() &&
1681 D->isCompleteDefinition() &&
1682 !IsStructuralMatch(D, FoundRecord))
1685 AdoptDecl = FoundRecord;
1687 } else if (!SearchName) {
1692 ConflictingDecls.push_back(FoundDecls[I]);
1695 if (!ConflictingDecls.empty() && SearchName) {
1696 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1697 ConflictingDecls.data(),
1698 ConflictingDecls.size());
1702 // Create the record declaration.
1703 RecordDecl *D2 = AdoptDecl;
1704 SourceLocation StartLoc = Importer.Import(D->getLocStart());
1706 CXXRecordDecl *D2CXX = nullptr;
1707 if (CXXRecordDecl *DCXX = llvm::dyn_cast<CXXRecordDecl>(D)) {
1708 if (DCXX->isLambda()) {
1709 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
1710 D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(),
1712 DCXX->isDependentLambda(),
1713 DCXX->isGenericLambda(),
1714 DCXX->getLambdaCaptureDefault());
1715 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
1716 if (DCXX->getLambdaContextDecl() && !CDecl)
1718 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl);
1719 } else if (DCXX->isInjectedClassName()) {
1720 // We have to be careful to do a similar dance to the one in
1721 // Sema::ActOnStartCXXMemberDeclarations
1722 CXXRecordDecl *const PrevDecl = nullptr;
1723 const bool DelayTypeCreation = true;
1724 D2CXX = CXXRecordDecl::Create(
1725 Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc,
1726 Name.getAsIdentifierInfo(), PrevDecl, DelayTypeCreation);
1727 Importer.getToContext().getTypeDeclType(
1728 D2CXX, llvm::dyn_cast<CXXRecordDecl>(DC));
1730 D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
1733 Name.getAsIdentifierInfo());
1736 D2->setAccess(D->getAccess());
1738 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
1739 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
1742 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
1743 D2->setLexicalDeclContext(LexicalDC);
1744 LexicalDC->addDeclInternal(D2);
1745 if (D->isAnonymousStructOrUnion())
1746 D2->setAnonymousStructOrUnion(true);
1749 Importer.Imported(D, D2);
1751 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
1757 Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
1758 // Import the major distinguishing characteristics of this enumerator.
1759 DeclContext *DC, *LexicalDC;
1760 DeclarationName Name;
1763 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1768 QualType T = Importer.Import(D->getType());
1772 // Determine whether there are any other declarations with the same name and
1773 // in the same context.
1774 if (!LexicalDC->isFunctionOrMethod()) {
1775 SmallVector<NamedDecl *, 4> ConflictingDecls;
1776 unsigned IDNS = Decl::IDNS_Ordinary;
1777 SmallVector<NamedDecl *, 2> FoundDecls;
1778 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
1779 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1780 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
1783 if (EnumConstantDecl *FoundEnumConstant
1784 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
1785 if (IsStructuralMatch(D, FoundEnumConstant))
1786 return Importer.Imported(D, FoundEnumConstant);
1789 ConflictingDecls.push_back(FoundDecls[I]);
1792 if (!ConflictingDecls.empty()) {
1793 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1794 ConflictingDecls.data(),
1795 ConflictingDecls.size());
1801 Expr *Init = Importer.Import(D->getInitExpr());
1802 if (D->getInitExpr() && !Init)
1805 EnumConstantDecl *ToEnumerator
1806 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
1807 Name.getAsIdentifierInfo(), T,
1808 Init, D->getInitVal());
1809 ToEnumerator->setAccess(D->getAccess());
1810 ToEnumerator->setLexicalDeclContext(LexicalDC);
1811 Importer.Imported(D, ToEnumerator);
1812 LexicalDC->addDeclInternal(ToEnumerator);
1813 return ToEnumerator;
1816 Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
1817 // Import the major distinguishing characteristics of this function.
1818 DeclContext *DC, *LexicalDC;
1819 DeclarationName Name;
1822 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1827 // Try to find a function in our own ("to") context with the same name, same
1828 // type, and in the same context as the function we're importing.
1829 if (!LexicalDC->isFunctionOrMethod()) {
1830 SmallVector<NamedDecl *, 4> ConflictingDecls;
1831 unsigned IDNS = Decl::IDNS_Ordinary;
1832 SmallVector<NamedDecl *, 2> FoundDecls;
1833 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
1834 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1835 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
1838 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
1839 if (FoundFunction->hasExternalFormalLinkage() &&
1840 D->hasExternalFormalLinkage()) {
1841 if (Importer.IsStructurallyEquivalent(D->getType(),
1842 FoundFunction->getType())) {
1843 // FIXME: Actually try to merge the body and other attributes.
1844 return Importer.Imported(D, FoundFunction);
1847 // FIXME: Check for overloading more carefully, e.g., by boosting
1848 // Sema::IsOverload out to the AST library.
1850 // Function overloading is okay in C++.
1851 if (Importer.getToContext().getLangOpts().CPlusPlus)
1854 // Complain about inconsistent function types.
1855 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
1856 << Name << D->getType() << FoundFunction->getType();
1857 Importer.ToDiag(FoundFunction->getLocation(),
1858 diag::note_odr_value_here)
1859 << FoundFunction->getType();
1863 ConflictingDecls.push_back(FoundDecls[I]);
1866 if (!ConflictingDecls.empty()) {
1867 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1868 ConflictingDecls.data(),
1869 ConflictingDecls.size());
1875 DeclarationNameInfo NameInfo(Name, Loc);
1876 // Import additional name location/type info.
1877 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
1879 QualType FromTy = D->getType();
1880 bool usedDifferentExceptionSpec = false;
1882 if (const FunctionProtoType *
1883 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
1884 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
1885 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
1886 // FunctionDecl that we are importing the FunctionProtoType for.
1887 // To avoid an infinite recursion when importing, create the FunctionDecl
1888 // with a simplified function type and update it afterwards.
1889 if (FromEPI.ExceptionSpec.SourceDecl ||
1890 FromEPI.ExceptionSpec.SourceTemplate ||
1891 FromEPI.ExceptionSpec.NoexceptExpr) {
1892 FunctionProtoType::ExtProtoInfo DefaultEPI;
1893 FromTy = Importer.getFromContext().getFunctionType(
1894 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
1895 usedDifferentExceptionSpec = true;
1900 QualType T = Importer.Import(FromTy);
1904 // Import the function parameters.
1905 SmallVector<ParmVarDecl *, 8> Parameters;
1906 for (auto P : D->parameters()) {
1907 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
1911 Parameters.push_back(ToP);
1914 // Create the imported function.
1915 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1916 FunctionDecl *ToFunction = nullptr;
1917 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
1918 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
1919 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
1920 cast<CXXRecordDecl>(DC),
1923 FromConstructor->isExplicit(),
1924 D->isInlineSpecified(),
1927 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
1928 SmallVector<CXXCtorInitializer *, 4> CtorInitializers;
1929 for (CXXCtorInitializer *I : FromConstructor->inits()) {
1930 CXXCtorInitializer *ToI =
1931 cast_or_null<CXXCtorInitializer>(Importer.Import(I));
1934 CtorInitializers.push_back(ToI);
1936 CXXCtorInitializer **Memory =
1937 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
1938 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
1939 CXXConstructorDecl *ToCtor = llvm::cast<CXXConstructorDecl>(ToFunction);
1940 ToCtor->setCtorInitializers(Memory);
1941 ToCtor->setNumCtorInitializers(NumInitializers);
1943 } else if (isa<CXXDestructorDecl>(D)) {
1944 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
1945 cast<CXXRecordDecl>(DC),
1948 D->isInlineSpecified(),
1950 } else if (CXXConversionDecl *FromConversion
1951 = dyn_cast<CXXConversionDecl>(D)) {
1952 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
1953 cast<CXXRecordDecl>(DC),
1956 D->isInlineSpecified(),
1957 FromConversion->isExplicit(),
1959 Importer.Import(D->getLocEnd()));
1960 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
1961 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
1962 cast<CXXRecordDecl>(DC),
1965 Method->getStorageClass(),
1966 Method->isInlineSpecified(),
1968 Importer.Import(D->getLocEnd()));
1970 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
1972 NameInfo, T, TInfo, D->getStorageClass(),
1973 D->isInlineSpecified(),
1974 D->hasWrittenPrototype(),
1978 // Import the qualifier, if any.
1979 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
1980 ToFunction->setAccess(D->getAccess());
1981 ToFunction->setLexicalDeclContext(LexicalDC);
1982 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
1983 ToFunction->setTrivial(D->isTrivial());
1984 ToFunction->setPure(D->isPure());
1985 Importer.Imported(D, ToFunction);
1987 // Set the parameters.
1988 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
1989 Parameters[I]->setOwningFunction(ToFunction);
1990 ToFunction->addDeclInternal(Parameters[I]);
1992 ToFunction->setParams(Parameters);
1994 if (usedDifferentExceptionSpec) {
1995 // Update FunctionProtoType::ExtProtoInfo.
1996 QualType T = Importer.Import(D->getType());
1999 ToFunction->setType(T);
2002 // Import the body, if any.
2003 if (Stmt *FromBody = D->getBody()) {
2004 if (Stmt *ToBody = Importer.Import(FromBody)) {
2005 ToFunction->setBody(ToBody);
2009 // FIXME: Other bits to merge?
2011 // Add this function to the lexical context.
2012 LexicalDC->addDeclInternal(ToFunction);
2017 Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2018 return VisitFunctionDecl(D);
2021 Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2022 return VisitCXXMethodDecl(D);
2025 Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2026 return VisitCXXMethodDecl(D);
2029 Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2030 return VisitCXXMethodDecl(D);
2033 static unsigned getFieldIndex(Decl *F) {
2034 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
2039 for (const auto *D : Owner->noload_decls()) {
2043 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2050 Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2051 // Import the major distinguishing characteristics of a variable.
2052 DeclContext *DC, *LexicalDC;
2053 DeclarationName Name;
2056 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2061 // Determine whether we've already imported this field.
2062 SmallVector<NamedDecl *, 2> FoundDecls;
2063 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2064 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2065 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
2066 // For anonymous fields, match up by index.
2067 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2070 if (Importer.IsStructurallyEquivalent(D->getType(),
2071 FoundField->getType())) {
2072 Importer.Imported(D, FoundField);
2076 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2077 << Name << D->getType() << FoundField->getType();
2078 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2079 << FoundField->getType();
2085 QualType T = Importer.Import(D->getType());
2089 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2090 Expr *BitWidth = Importer.Import(D->getBitWidth());
2091 if (!BitWidth && D->getBitWidth())
2094 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2095 Importer.Import(D->getInnerLocStart()),
2096 Loc, Name.getAsIdentifierInfo(),
2097 T, TInfo, BitWidth, D->isMutable(),
2098 D->getInClassInitStyle());
2099 ToField->setAccess(D->getAccess());
2100 ToField->setLexicalDeclContext(LexicalDC);
2101 if (Expr *FromInitializer = D->getInClassInitializer()) {
2102 Expr *ToInitializer = Importer.Import(FromInitializer);
2104 ToField->setInClassInitializer(ToInitializer);
2108 ToField->setImplicit(D->isImplicit());
2109 Importer.Imported(D, ToField);
2110 LexicalDC->addDeclInternal(ToField);
2114 Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2115 // Import the major distinguishing characteristics of a variable.
2116 DeclContext *DC, *LexicalDC;
2117 DeclarationName Name;
2120 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2125 // Determine whether we've already imported this field.
2126 SmallVector<NamedDecl *, 2> FoundDecls;
2127 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2128 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2129 if (IndirectFieldDecl *FoundField
2130 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
2131 // For anonymous indirect fields, match up by index.
2132 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2135 if (Importer.IsStructurallyEquivalent(D->getType(),
2136 FoundField->getType(),
2138 Importer.Imported(D, FoundField);
2142 // If there are more anonymous fields to check, continue.
2143 if (!Name && I < N-1)
2146 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2147 << Name << D->getType() << FoundField->getType();
2148 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2149 << FoundField->getType();
2155 QualType T = Importer.Import(D->getType());
2159 NamedDecl **NamedChain =
2160 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2163 for (auto *PI : D->chain()) {
2164 Decl *D = Importer.Import(PI);
2167 NamedChain[i++] = cast<NamedDecl>(D);
2170 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2171 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
2172 {NamedChain, D->getChainingSize()});
2174 for (const auto *Attr : D->attrs())
2175 ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
2177 ToIndirectField->setAccess(D->getAccess());
2178 ToIndirectField->setLexicalDeclContext(LexicalDC);
2179 Importer.Imported(D, ToIndirectField);
2180 LexicalDC->addDeclInternal(ToIndirectField);
2181 return ToIndirectField;
2184 Decl *ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
2185 // Import the major distinguishing characteristics of a declaration.
2186 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2187 DeclContext *LexicalDC = D->getDeclContext() == D->getLexicalDeclContext()
2188 ? DC : Importer.ImportContext(D->getLexicalDeclContext());
2189 if (!DC || !LexicalDC)
2192 // Determine whether we've already imported this decl.
2193 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
2194 auto *RD = cast<CXXRecordDecl>(DC);
2195 FriendDecl *ImportedFriend = RD->getFirstFriend();
2196 StructuralEquivalenceContext Context(
2197 Importer.getFromContext(), Importer.getToContext(),
2198 Importer.getNonEquivalentDecls(), false, false);
2200 while (ImportedFriend) {
2201 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
2202 if (Context.IsStructurallyEquivalent(D->getFriendDecl(),
2203 ImportedFriend->getFriendDecl()))
2204 return Importer.Imported(D, ImportedFriend);
2206 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
2207 if (Importer.IsStructurallyEquivalent(
2208 D->getFriendType()->getType(),
2209 ImportedFriend->getFriendType()->getType(), true))
2210 return Importer.Imported(D, ImportedFriend);
2212 ImportedFriend = ImportedFriend->getNextFriend();
2215 // Not found. Create it.
2216 FriendDecl::FriendUnion ToFU;
2217 if (NamedDecl *FriendD = D->getFriendDecl())
2218 ToFU = cast_or_null<NamedDecl>(Importer.Import(FriendD));
2220 ToFU = Importer.Import(D->getFriendType());
2224 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
2225 TemplateParameterList **FromTPLists =
2226 D->getTrailingObjects<TemplateParameterList *>();
2227 for (unsigned I = 0; I < D->NumTPLists; I++) {
2228 TemplateParameterList *List = ImportTemplateParameterList(FromTPLists[I]);
2231 ToTPLists[I] = List;
2234 FriendDecl *FrD = FriendDecl::Create(Importer.getToContext(), DC,
2235 Importer.Import(D->getLocation()),
2236 ToFU, Importer.Import(D->getFriendLoc()),
2239 Importer.Imported(D, FrD);
2240 RD->pushFriendDecl(FrD);
2242 FrD->setAccess(D->getAccess());
2243 FrD->setLexicalDeclContext(LexicalDC);
2244 LexicalDC->addDeclInternal(FrD);
2248 Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2249 // Import the major distinguishing characteristics of an ivar.
2250 DeclContext *DC, *LexicalDC;
2251 DeclarationName Name;
2254 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2259 // Determine whether we've already imported this ivar
2260 SmallVector<NamedDecl *, 2> FoundDecls;
2261 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2262 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2263 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
2264 if (Importer.IsStructurallyEquivalent(D->getType(),
2265 FoundIvar->getType())) {
2266 Importer.Imported(D, FoundIvar);
2270 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2271 << Name << D->getType() << FoundIvar->getType();
2272 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2273 << FoundIvar->getType();
2279 QualType T = Importer.Import(D->getType());
2283 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2284 Expr *BitWidth = Importer.Import(D->getBitWidth());
2285 if (!BitWidth && D->getBitWidth())
2288 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2289 cast<ObjCContainerDecl>(DC),
2290 Importer.Import(D->getInnerLocStart()),
2291 Loc, Name.getAsIdentifierInfo(),
2292 T, TInfo, D->getAccessControl(),
2293 BitWidth, D->getSynthesize());
2294 ToIvar->setLexicalDeclContext(LexicalDC);
2295 Importer.Imported(D, ToIvar);
2296 LexicalDC->addDeclInternal(ToIvar);
2301 Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2302 // Import the major distinguishing characteristics of a variable.
2303 DeclContext *DC, *LexicalDC;
2304 DeclarationName Name;
2307 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2312 // Try to find a variable in our own ("to") context with the same name and
2313 // in the same context as the variable we're importing.
2314 if (D->isFileVarDecl()) {
2315 VarDecl *MergeWithVar = nullptr;
2316 SmallVector<NamedDecl *, 4> ConflictingDecls;
2317 unsigned IDNS = Decl::IDNS_Ordinary;
2318 SmallVector<NamedDecl *, 2> FoundDecls;
2319 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2320 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2321 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
2324 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
2325 // We have found a variable that we may need to merge with. Check it.
2326 if (FoundVar->hasExternalFormalLinkage() &&
2327 D->hasExternalFormalLinkage()) {
2328 if (Importer.IsStructurallyEquivalent(D->getType(),
2329 FoundVar->getType())) {
2330 MergeWithVar = FoundVar;
2334 const ArrayType *FoundArray
2335 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2336 const ArrayType *TArray
2337 = Importer.getToContext().getAsArrayType(D->getType());
2338 if (FoundArray && TArray) {
2339 if (isa<IncompleteArrayType>(FoundArray) &&
2340 isa<ConstantArrayType>(TArray)) {
2342 QualType T = Importer.Import(D->getType());
2346 FoundVar->setType(T);
2347 MergeWithVar = FoundVar;
2349 } else if (isa<IncompleteArrayType>(TArray) &&
2350 isa<ConstantArrayType>(FoundArray)) {
2351 MergeWithVar = FoundVar;
2356 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
2357 << Name << D->getType() << FoundVar->getType();
2358 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2359 << FoundVar->getType();
2363 ConflictingDecls.push_back(FoundDecls[I]);
2367 // An equivalent variable with external linkage has been found. Link
2368 // the two declarations, then merge them.
2369 Importer.Imported(D, MergeWithVar);
2371 if (VarDecl *DDef = D->getDefinition()) {
2372 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2373 Importer.ToDiag(ExistingDef->getLocation(),
2374 diag::err_odr_variable_multiple_def)
2376 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2378 Expr *Init = Importer.Import(DDef->getInit());
2379 MergeWithVar->setInit(Init);
2380 if (DDef->isInitKnownICE()) {
2381 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2382 Eval->CheckedICE = true;
2383 Eval->IsICE = DDef->isInitICE();
2388 return MergeWithVar;
2391 if (!ConflictingDecls.empty()) {
2392 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2393 ConflictingDecls.data(),
2394 ConflictingDecls.size());
2401 QualType T = Importer.Import(D->getType());
2405 // Create the imported variable.
2406 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2407 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2408 Importer.Import(D->getInnerLocStart()),
2409 Loc, Name.getAsIdentifierInfo(),
2411 D->getStorageClass());
2412 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
2413 ToVar->setAccess(D->getAccess());
2414 ToVar->setLexicalDeclContext(LexicalDC);
2415 Importer.Imported(D, ToVar);
2416 LexicalDC->addDeclInternal(ToVar);
2418 if (!D->isFileVarDecl() &&
2422 // Merge the initializer.
2423 if (ImportDefinition(D, ToVar))
2426 if (D->isConstexpr())
2427 ToVar->setConstexpr(true);
2432 Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2433 // Parameters are created in the translation unit's context, then moved
2434 // into the function declaration's context afterward.
2435 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2437 // Import the name of this declaration.
2438 DeclarationName Name = Importer.Import(D->getDeclName());
2439 if (D->getDeclName() && !Name)
2442 // Import the location of this declaration.
2443 SourceLocation Loc = Importer.Import(D->getLocation());
2445 // Import the parameter's type.
2446 QualType T = Importer.Import(D->getType());
2450 // Create the imported parameter.
2451 ImplicitParamDecl *ToParm
2452 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2453 Loc, Name.getAsIdentifierInfo(),
2455 return Importer.Imported(D, ToParm);
2458 Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2459 // Parameters are created in the translation unit's context, then moved
2460 // into the function declaration's context afterward.
2461 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2463 // Import the name of this declaration.
2464 DeclarationName Name = Importer.Import(D->getDeclName());
2465 if (D->getDeclName() && !Name)
2468 // Import the location of this declaration.
2469 SourceLocation Loc = Importer.Import(D->getLocation());
2471 // Import the parameter's type.
2472 QualType T = Importer.Import(D->getType());
2476 // Create the imported parameter.
2477 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2478 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2479 Importer.Import(D->getInnerLocStart()),
2480 Loc, Name.getAsIdentifierInfo(),
2481 T, TInfo, D->getStorageClass(),
2482 /*DefaultArg*/ nullptr);
2484 // Set the default argument.
2485 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
2486 ToParm->setKNRPromoted(D->isKNRPromoted());
2488 Expr *ToDefArg = nullptr;
2489 Expr *FromDefArg = nullptr;
2490 if (D->hasUninstantiatedDefaultArg()) {
2491 FromDefArg = D->getUninstantiatedDefaultArg();
2492 ToDefArg = Importer.Import(FromDefArg);
2493 ToParm->setUninstantiatedDefaultArg(ToDefArg);
2494 } else if (D->hasUnparsedDefaultArg()) {
2495 ToParm->setUnparsedDefaultArg();
2496 } else if (D->hasDefaultArg()) {
2497 FromDefArg = D->getDefaultArg();
2498 ToDefArg = Importer.Import(FromDefArg);
2499 ToParm->setDefaultArg(ToDefArg);
2501 if (FromDefArg && !ToDefArg)
2505 ToParm->setIsUsed();
2507 return Importer.Imported(D, ToParm);
2510 Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2511 // Import the major distinguishing characteristics of a method.
2512 DeclContext *DC, *LexicalDC;
2513 DeclarationName Name;
2516 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2521 SmallVector<NamedDecl *, 2> FoundDecls;
2522 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2523 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2524 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
2525 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2528 // Check return types.
2529 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
2530 FoundMethod->getReturnType())) {
2531 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2532 << D->isInstanceMethod() << Name << D->getReturnType()
2533 << FoundMethod->getReturnType();
2534 Importer.ToDiag(FoundMethod->getLocation(),
2535 diag::note_odr_objc_method_here)
2536 << D->isInstanceMethod() << Name;
2540 // Check the number of parameters.
2541 if (D->param_size() != FoundMethod->param_size()) {
2542 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2543 << D->isInstanceMethod() << Name
2544 << D->param_size() << FoundMethod->param_size();
2545 Importer.ToDiag(FoundMethod->getLocation(),
2546 diag::note_odr_objc_method_here)
2547 << D->isInstanceMethod() << Name;
2551 // Check parameter types.
2552 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2553 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2554 P != PEnd; ++P, ++FoundP) {
2555 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2556 (*FoundP)->getType())) {
2557 Importer.FromDiag((*P)->getLocation(),
2558 diag::err_odr_objc_method_param_type_inconsistent)
2559 << D->isInstanceMethod() << Name
2560 << (*P)->getType() << (*FoundP)->getType();
2561 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2562 << (*FoundP)->getType();
2567 // Check variadic/non-variadic.
2568 // Check the number of parameters.
2569 if (D->isVariadic() != FoundMethod->isVariadic()) {
2570 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2571 << D->isInstanceMethod() << Name;
2572 Importer.ToDiag(FoundMethod->getLocation(),
2573 diag::note_odr_objc_method_here)
2574 << D->isInstanceMethod() << Name;
2578 // FIXME: Any other bits we need to merge?
2579 return Importer.Imported(D, FoundMethod);
2583 // Import the result type.
2584 QualType ResultTy = Importer.Import(D->getReturnType());
2585 if (ResultTy.isNull())
2588 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
2590 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
2591 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
2592 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
2593 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
2594 D->getImplementationControl(), D->hasRelatedResultType());
2596 // FIXME: When we decide to merge method definitions, we'll need to
2597 // deal with implicit parameters.
2599 // Import the parameters
2600 SmallVector<ParmVarDecl *, 5> ToParams;
2601 for (auto *FromP : D->parameters()) {
2602 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
2606 ToParams.push_back(ToP);
2609 // Set the parameters.
2610 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2611 ToParams[I]->setOwningFunction(ToMethod);
2612 ToMethod->addDeclInternal(ToParams[I]);
2614 SmallVector<SourceLocation, 12> SelLocs;
2615 D->getSelectorLocs(SelLocs);
2616 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
2618 ToMethod->setLexicalDeclContext(LexicalDC);
2619 Importer.Imported(D, ToMethod);
2620 LexicalDC->addDeclInternal(ToMethod);
2624 Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
2625 // Import the major distinguishing characteristics of a category.
2626 DeclContext *DC, *LexicalDC;
2627 DeclarationName Name;
2630 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2635 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
2639 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
2640 Importer.getToContext(), DC,
2642 Importer.Import(D->getVarianceLoc()),
2644 Importer.Import(D->getLocation()),
2645 Name.getAsIdentifierInfo(),
2646 Importer.Import(D->getColonLoc()),
2648 Importer.Imported(D, Result);
2649 Result->setLexicalDeclContext(LexicalDC);
2653 Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2654 // Import the major distinguishing characteristics of a category.
2655 DeclContext *DC, *LexicalDC;
2656 DeclarationName Name;
2659 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2664 ObjCInterfaceDecl *ToInterface
2665 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2669 // Determine if we've already encountered this category.
2670 ObjCCategoryDecl *MergeWithCategory
2671 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2672 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2674 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2675 Importer.Import(D->getAtStartLoc()),
2677 Importer.Import(D->getCategoryNameLoc()),
2678 Name.getAsIdentifierInfo(),
2680 /*TypeParamList=*/nullptr,
2681 Importer.Import(D->getIvarLBraceLoc()),
2682 Importer.Import(D->getIvarRBraceLoc()));
2683 ToCategory->setLexicalDeclContext(LexicalDC);
2684 LexicalDC->addDeclInternal(ToCategory);
2685 Importer.Imported(D, ToCategory);
2686 // Import the type parameter list after calling Imported, to avoid
2687 // loops when bringing in their DeclContext.
2688 ToCategory->setTypeParamList(ImportObjCTypeParamList(
2689 D->getTypeParamList()));
2692 SmallVector<ObjCProtocolDecl *, 4> Protocols;
2693 SmallVector<SourceLocation, 4> ProtocolLocs;
2694 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2695 = D->protocol_loc_begin();
2696 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2697 FromProtoEnd = D->protocol_end();
2698 FromProto != FromProtoEnd;
2699 ++FromProto, ++FromProtoLoc) {
2700 ObjCProtocolDecl *ToProto
2701 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2704 Protocols.push_back(ToProto);
2705 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2708 // FIXME: If we're merging, make sure that the protocol list is the same.
2709 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2710 ProtocolLocs.data(), Importer.getToContext());
2713 Importer.Imported(D, ToCategory);
2716 // Import all of the members of this category.
2717 ImportDeclContext(D);
2719 // If we have an implementation, import it as well.
2720 if (D->getImplementation()) {
2721 ObjCCategoryImplDecl *Impl
2722 = cast_or_null<ObjCCategoryImplDecl>(
2723 Importer.Import(D->getImplementation()));
2727 ToCategory->setImplementation(Impl);
2733 bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
2734 ObjCProtocolDecl *To,
2735 ImportDefinitionKind Kind) {
2736 if (To->getDefinition()) {
2737 if (shouldForceImportDeclContext(Kind))
2738 ImportDeclContext(From);
2742 // Start the protocol definition
2743 To->startDefinition();
2746 SmallVector<ObjCProtocolDecl *, 4> Protocols;
2747 SmallVector<SourceLocation, 4> ProtocolLocs;
2748 ObjCProtocolDecl::protocol_loc_iterator
2749 FromProtoLoc = From->protocol_loc_begin();
2750 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
2751 FromProtoEnd = From->protocol_end();
2752 FromProto != FromProtoEnd;
2753 ++FromProto, ++FromProtoLoc) {
2754 ObjCProtocolDecl *ToProto
2755 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2758 Protocols.push_back(ToProto);
2759 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2762 // FIXME: If we're merging, make sure that the protocol list is the same.
2763 To->setProtocolList(Protocols.data(), Protocols.size(),
2764 ProtocolLocs.data(), Importer.getToContext());
2766 if (shouldForceImportDeclContext(Kind)) {
2767 // Import all of the members of this protocol.
2768 ImportDeclContext(From, /*ForceImport=*/true);
2773 Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
2774 // If this protocol has a definition in the translation unit we're coming
2775 // from, but this particular declaration is not that definition, import the
2776 // definition and map to that.
2777 ObjCProtocolDecl *Definition = D->getDefinition();
2778 if (Definition && Definition != D) {
2779 Decl *ImportedDef = Importer.Import(Definition);
2783 return Importer.Imported(D, ImportedDef);
2786 // Import the major distinguishing characteristics of a protocol.
2787 DeclContext *DC, *LexicalDC;
2788 DeclarationName Name;
2791 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2796 ObjCProtocolDecl *MergeWithProtocol = nullptr;
2797 SmallVector<NamedDecl *, 2> FoundDecls;
2798 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2799 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2800 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2803 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
2807 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2809 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
2810 Name.getAsIdentifierInfo(), Loc,
2811 Importer.Import(D->getAtStartLoc()),
2812 /*PrevDecl=*/nullptr);
2813 ToProto->setLexicalDeclContext(LexicalDC);
2814 LexicalDC->addDeclInternal(ToProto);
2817 Importer.Imported(D, ToProto);
2819 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
2825 Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
2826 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2827 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
2829 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
2830 SourceLocation LangLoc = Importer.Import(D->getLocation());
2832 bool HasBraces = D->hasBraces();
2834 LinkageSpecDecl *ToLinkageSpec =
2835 LinkageSpecDecl::Create(Importer.getToContext(),
2843 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
2844 ToLinkageSpec->setRBraceLoc(RBraceLoc);
2847 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
2848 LexicalDC->addDeclInternal(ToLinkageSpec);
2850 Importer.Imported(D, ToLinkageSpec);
2852 return ToLinkageSpec;
2855 bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
2856 ObjCInterfaceDecl *To,
2857 ImportDefinitionKind Kind) {
2858 if (To->getDefinition()) {
2859 // Check consistency of superclass.
2860 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
2862 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
2867 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
2868 if ((bool)FromSuper != (bool)ToSuper ||
2869 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
2870 Importer.ToDiag(To->getLocation(),
2871 diag::err_odr_objc_superclass_inconsistent)
2872 << To->getDeclName();
2874 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
2875 << To->getSuperClass()->getDeclName();
2877 Importer.ToDiag(To->getLocation(),
2878 diag::note_odr_objc_missing_superclass);
2879 if (From->getSuperClass())
2880 Importer.FromDiag(From->getSuperClassLoc(),
2881 diag::note_odr_objc_superclass)
2882 << From->getSuperClass()->getDeclName();
2884 Importer.FromDiag(From->getLocation(),
2885 diag::note_odr_objc_missing_superclass);
2888 if (shouldForceImportDeclContext(Kind))
2889 ImportDeclContext(From);
2893 // Start the definition.
2894 To->startDefinition();
2896 // If this class has a superclass, import it.
2897 if (From->getSuperClass()) {
2898 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
2902 To->setSuperClass(SuperTInfo);
2906 SmallVector<ObjCProtocolDecl *, 4> Protocols;
2907 SmallVector<SourceLocation, 4> ProtocolLocs;
2908 ObjCInterfaceDecl::protocol_loc_iterator
2909 FromProtoLoc = From->protocol_loc_begin();
2911 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
2912 FromProtoEnd = From->protocol_end();
2913 FromProto != FromProtoEnd;
2914 ++FromProto, ++FromProtoLoc) {
2915 ObjCProtocolDecl *ToProto
2916 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2919 Protocols.push_back(ToProto);
2920 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2923 // FIXME: If we're merging, make sure that the protocol list is the same.
2924 To->setProtocolList(Protocols.data(), Protocols.size(),
2925 ProtocolLocs.data(), Importer.getToContext());
2927 // Import categories. When the categories themselves are imported, they'll
2928 // hook themselves into this interface.
2929 for (auto *Cat : From->known_categories())
2930 Importer.Import(Cat);
2932 // If we have an @implementation, import it as well.
2933 if (From->getImplementation()) {
2934 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
2935 Importer.Import(From->getImplementation()));
2939 To->setImplementation(Impl);
2942 if (shouldForceImportDeclContext(Kind)) {
2943 // Import all of the members of this class.
2944 ImportDeclContext(From, /*ForceImport=*/true);
2950 ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
2954 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
2955 for (auto fromTypeParam : *list) {
2956 auto toTypeParam = cast_or_null<ObjCTypeParamDecl>(
2957 Importer.Import(fromTypeParam));
2961 toTypeParams.push_back(toTypeParam);
2964 return ObjCTypeParamList::create(Importer.getToContext(),
2965 Importer.Import(list->getLAngleLoc()),
2967 Importer.Import(list->getRAngleLoc()));
2970 Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2971 // If this class has a definition in the translation unit we're coming from,
2972 // but this particular declaration is not that definition, import the
2973 // definition and map to that.
2974 ObjCInterfaceDecl *Definition = D->getDefinition();
2975 if (Definition && Definition != D) {
2976 Decl *ImportedDef = Importer.Import(Definition);
2980 return Importer.Imported(D, ImportedDef);
2983 // Import the major distinguishing characteristics of an @interface.
2984 DeclContext *DC, *LexicalDC;
2985 DeclarationName Name;
2988 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2993 // Look for an existing interface with the same name.
2994 ObjCInterfaceDecl *MergeWithIface = nullptr;
2995 SmallVector<NamedDecl *, 2> FoundDecls;
2996 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2997 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2998 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3001 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
3005 // Create an interface declaration, if one does not already exist.
3006 ObjCInterfaceDecl *ToIface = MergeWithIface;
3008 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3009 Importer.Import(D->getAtStartLoc()),
3010 Name.getAsIdentifierInfo(),
3011 /*TypeParamList=*/nullptr,
3012 /*PrevDecl=*/nullptr, Loc,
3013 D->isImplicitInterfaceDecl());
3014 ToIface->setLexicalDeclContext(LexicalDC);
3015 LexicalDC->addDeclInternal(ToIface);
3017 Importer.Imported(D, ToIface);
3018 // Import the type parameter list after calling Imported, to avoid
3019 // loops when bringing in their DeclContext.
3020 ToIface->setTypeParamList(ImportObjCTypeParamList(
3021 D->getTypeParamListAsWritten()));
3023 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
3029 Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3030 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3031 Importer.Import(D->getCategoryDecl()));
3035 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3037 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3041 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
3042 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
3043 Importer.Import(D->getIdentifier()),
3044 Category->getClassInterface(),
3045 Importer.Import(D->getLocation()),
3046 Importer.Import(D->getAtStartLoc()),
3049 DeclContext *LexicalDC = DC;
3050 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3051 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3055 ToImpl->setLexicalDeclContext(LexicalDC);
3058 LexicalDC->addDeclInternal(ToImpl);
3059 Category->setImplementation(ToImpl);
3062 Importer.Imported(D, ToImpl);
3063 ImportDeclContext(D);
3067 Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3068 // Find the corresponding interface.
3069 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3070 Importer.Import(D->getClassInterface()));
3074 // Import the superclass, if any.
3075 ObjCInterfaceDecl *Super = nullptr;
3076 if (D->getSuperClass()) {
3077 Super = cast_or_null<ObjCInterfaceDecl>(
3078 Importer.Import(D->getSuperClass()));
3083 ObjCImplementationDecl *Impl = Iface->getImplementation();
3085 // We haven't imported an implementation yet. Create a new @implementation
3087 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3088 Importer.ImportContext(D->getDeclContext()),
3090 Importer.Import(D->getLocation()),
3091 Importer.Import(D->getAtStartLoc()),
3092 Importer.Import(D->getSuperClassLoc()),
3093 Importer.Import(D->getIvarLBraceLoc()),
3094 Importer.Import(D->getIvarRBraceLoc()));
3096 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3097 DeclContext *LexicalDC
3098 = Importer.ImportContext(D->getLexicalDeclContext());
3101 Impl->setLexicalDeclContext(LexicalDC);
3104 // Associate the implementation with the class it implements.
3105 Iface->setImplementation(Impl);
3106 Importer.Imported(D, Iface->getImplementation());
3108 Importer.Imported(D, Iface->getImplementation());
3110 // Verify that the existing @implementation has the same superclass.
3111 if ((Super && !Impl->getSuperClass()) ||
3112 (!Super && Impl->getSuperClass()) ||
3113 (Super && Impl->getSuperClass() &&
3114 !declaresSameEntity(Super->getCanonicalDecl(),
3115 Impl->getSuperClass()))) {
3116 Importer.ToDiag(Impl->getLocation(),
3117 diag::err_odr_objc_superclass_inconsistent)
3118 << Iface->getDeclName();
3119 // FIXME: It would be nice to have the location of the superclass
3121 if (Impl->getSuperClass())
3122 Importer.ToDiag(Impl->getLocation(),
3123 diag::note_odr_objc_superclass)
3124 << Impl->getSuperClass()->getDeclName();
3126 Importer.ToDiag(Impl->getLocation(),
3127 diag::note_odr_objc_missing_superclass);
3128 if (D->getSuperClass())
3129 Importer.FromDiag(D->getLocation(),
3130 diag::note_odr_objc_superclass)
3131 << D->getSuperClass()->getDeclName();
3133 Importer.FromDiag(D->getLocation(),
3134 diag::note_odr_objc_missing_superclass);
3139 // Import all of the members of this @implementation.
3140 ImportDeclContext(D);
3145 Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3146 // Import the major distinguishing characteristics of an @property.
3147 DeclContext *DC, *LexicalDC;
3148 DeclarationName Name;
3151 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3156 // Check whether we have already imported this property.
3157 SmallVector<NamedDecl *, 2> FoundDecls;
3158 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3159 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3160 if (ObjCPropertyDecl *FoundProp
3161 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
3162 // Check property types.
3163 if (!Importer.IsStructurallyEquivalent(D->getType(),
3164 FoundProp->getType())) {
3165 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3166 << Name << D->getType() << FoundProp->getType();
3167 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3168 << FoundProp->getType();
3172 // FIXME: Check property attributes, getters, setters, etc.?
3174 // Consider these properties to be equivalent.
3175 Importer.Imported(D, FoundProp);
3181 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
3185 // Create the new property.
3186 ObjCPropertyDecl *ToProperty
3187 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3188 Name.getAsIdentifierInfo(),
3189 Importer.Import(D->getAtLoc()),
3190 Importer.Import(D->getLParenLoc()),
3191 Importer.Import(D->getType()),
3193 D->getPropertyImplementation());
3194 Importer.Imported(D, ToProperty);
3195 ToProperty->setLexicalDeclContext(LexicalDC);
3196 LexicalDC->addDeclInternal(ToProperty);
3198 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
3199 ToProperty->setPropertyAttributesAsWritten(
3200 D->getPropertyAttributesAsWritten());
3201 ToProperty->setGetterName(Importer.Import(D->getGetterName()),
3202 Importer.Import(D->getGetterNameLoc()));
3203 ToProperty->setSetterName(Importer.Import(D->getSetterName()),
3204 Importer.Import(D->getSetterNameLoc()));
3205 ToProperty->setGetterMethodDecl(
3206 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3207 ToProperty->setSetterMethodDecl(
3208 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3209 ToProperty->setPropertyIvarDecl(
3210 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3214 Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3215 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3216 Importer.Import(D->getPropertyDecl()));
3220 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3224 // Import the lexical declaration context.
3225 DeclContext *LexicalDC = DC;
3226 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3227 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3232 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3236 // Import the ivar (for an @synthesize).
3237 ObjCIvarDecl *Ivar = nullptr;
3238 if (D->getPropertyIvarDecl()) {
3239 Ivar = cast_or_null<ObjCIvarDecl>(
3240 Importer.Import(D->getPropertyIvarDecl()));
3245 ObjCPropertyImplDecl *ToImpl
3246 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
3247 Property->getQueryKind());
3249 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3250 Importer.Import(D->getLocStart()),
3251 Importer.Import(D->getLocation()),
3253 D->getPropertyImplementation(),
3255 Importer.Import(D->getPropertyIvarDeclLoc()));
3256 ToImpl->setLexicalDeclContext(LexicalDC);
3257 Importer.Imported(D, ToImpl);
3258 LexicalDC->addDeclInternal(ToImpl);
3260 // Check that we have the same kind of property implementation (@synthesize
3262 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3263 Importer.ToDiag(ToImpl->getLocation(),
3264 diag::err_odr_objc_property_impl_kind_inconsistent)
3265 << Property->getDeclName()
3266 << (ToImpl->getPropertyImplementation()
3267 == ObjCPropertyImplDecl::Dynamic);
3268 Importer.FromDiag(D->getLocation(),
3269 diag::note_odr_objc_property_impl_kind)
3270 << D->getPropertyDecl()->getDeclName()
3271 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3275 // For @synthesize, check that we have the same
3276 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3277 Ivar != ToImpl->getPropertyIvarDecl()) {
3278 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3279 diag::err_odr_objc_synthesize_ivar_inconsistent)
3280 << Property->getDeclName()
3281 << ToImpl->getPropertyIvarDecl()->getDeclName()
3282 << Ivar->getDeclName();
3283 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3284 diag::note_odr_objc_synthesize_ivar_here)
3285 << D->getPropertyIvarDecl()->getDeclName();
3289 // Merge the existing implementation with the new implementation.
3290 Importer.Imported(D, ToImpl);
3296 Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3297 // For template arguments, we adopt the translation unit as our declaration
3298 // context. This context will be fixed when the actual template declaration
3301 // FIXME: Import default argument.
3302 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3303 Importer.getToContext().getTranslationUnitDecl(),
3304 Importer.Import(D->getLocStart()),
3305 Importer.Import(D->getLocation()),
3308 Importer.Import(D->getIdentifier()),
3309 D->wasDeclaredWithTypename(),
3310 D->isParameterPack());
3314 ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3315 // Import the name of this declaration.
3316 DeclarationName Name = Importer.Import(D->getDeclName());
3317 if (D->getDeclName() && !Name)
3320 // Import the location of this declaration.
3321 SourceLocation Loc = Importer.Import(D->getLocation());
3323 // Import the type of this declaration.
3324 QualType T = Importer.Import(D->getType());
3328 // Import type-source information.
3329 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3330 if (D->getTypeSourceInfo() && !TInfo)
3333 // FIXME: Import default argument.
3335 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3336 Importer.getToContext().getTranslationUnitDecl(),
3337 Importer.Import(D->getInnerLocStart()),
3338 Loc, D->getDepth(), D->getPosition(),
3339 Name.getAsIdentifierInfo(),
3340 T, D->isParameterPack(), TInfo);
3344 ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3345 // Import the name of this declaration.
3346 DeclarationName Name = Importer.Import(D->getDeclName());
3347 if (D->getDeclName() && !Name)
3350 // Import the location of this declaration.
3351 SourceLocation Loc = Importer.Import(D->getLocation());
3353 // Import template parameters.
3354 TemplateParameterList *TemplateParams
3355 = ImportTemplateParameterList(D->getTemplateParameters());
3356 if (!TemplateParams)
3359 // FIXME: Import default argument.
3361 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3362 Importer.getToContext().getTranslationUnitDecl(),
3363 Loc, D->getDepth(), D->getPosition(),
3364 D->isParameterPack(),
3365 Name.getAsIdentifierInfo(),
3369 Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3370 // If this record has a definition in the translation unit we're coming from,
3371 // but this particular declaration is not that definition, import the
3372 // definition and map to that.
3373 CXXRecordDecl *Definition
3374 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3375 if (Definition && Definition != D->getTemplatedDecl()) {
3377 = Importer.Import(Definition->getDescribedClassTemplate());
3381 return Importer.Imported(D, ImportedDef);
3384 // Import the major distinguishing characteristics of this class template.
3385 DeclContext *DC, *LexicalDC;
3386 DeclarationName Name;
3389 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3394 // We may already have a template of the same name; try to find and match it.
3395 if (!DC->isFunctionOrMethod()) {
3396 SmallVector<NamedDecl *, 4> ConflictingDecls;
3397 SmallVector<NamedDecl *, 2> FoundDecls;
3398 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3399 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3400 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3403 Decl *Found = FoundDecls[I];
3404 if (ClassTemplateDecl *FoundTemplate
3405 = dyn_cast<ClassTemplateDecl>(Found)) {
3406 if (IsStructuralMatch(D, FoundTemplate)) {
3407 // The class templates structurally match; call it the same template.
3408 // FIXME: We may be filling in a forward declaration here. Handle
3410 Importer.Imported(D->getTemplatedDecl(),
3411 FoundTemplate->getTemplatedDecl());
3412 return Importer.Imported(D, FoundTemplate);
3416 ConflictingDecls.push_back(FoundDecls[I]);
3419 if (!ConflictingDecls.empty()) {
3420 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3421 ConflictingDecls.data(),
3422 ConflictingDecls.size());
3429 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3431 // Create the declaration that is being templated.
3432 // Create the declaration that is being templated.
3433 CXXRecordDecl *D2Templated = cast_or_null<CXXRecordDecl>(
3434 Importer.Import(DTemplated));
3438 // Resolve possible cyclic import.
3439 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
3440 return AlreadyImported;
3442 // Create the class template declaration itself.
3443 TemplateParameterList *TemplateParams
3444 = ImportTemplateParameterList(D->getTemplateParameters());
3445 if (!TemplateParams)
3448 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3449 Loc, Name, TemplateParams,
3451 D2Templated->setDescribedClassTemplate(D2);
3453 D2->setAccess(D->getAccess());
3454 D2->setLexicalDeclContext(LexicalDC);
3455 LexicalDC->addDeclInternal(D2);
3457 // Note the relationship between the class templates.
3458 Importer.Imported(D, D2);
3459 Importer.Imported(DTemplated, D2Templated);
3461 if (DTemplated->isCompleteDefinition() &&
3462 !D2Templated->isCompleteDefinition()) {
3463 // FIXME: Import definition!
3469 Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3470 ClassTemplateSpecializationDecl *D) {
3471 // If this record has a definition in the translation unit we're coming from,
3472 // but this particular declaration is not that definition, import the
3473 // definition and map to that.
3474 TagDecl *Definition = D->getDefinition();
3475 if (Definition && Definition != D) {
3476 Decl *ImportedDef = Importer.Import(Definition);
3480 return Importer.Imported(D, ImportedDef);
3483 ClassTemplateDecl *ClassTemplate
3484 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3485 D->getSpecializedTemplate()));
3489 // Import the context of this declaration.
3490 DeclContext *DC = ClassTemplate->getDeclContext();
3494 DeclContext *LexicalDC = DC;
3495 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3496 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3501 // Import the location of this declaration.
3502 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3503 SourceLocation IdLoc = Importer.Import(D->getLocation());
3505 // Import template arguments.
3506 SmallVector<TemplateArgument, 2> TemplateArgs;
3507 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3508 D->getTemplateArgs().size(),
3512 // Try to find an existing specialization with these template arguments.
3513 void *InsertPos = nullptr;
3514 ClassTemplateSpecializationDecl *D2
3515 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
3517 // We already have a class template specialization with these template
3520 // FIXME: Check for specialization vs. instantiation errors.
3522 if (RecordDecl *FoundDef = D2->getDefinition()) {
3523 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
3524 // The record types structurally match, or the "from" translation
3525 // unit only had a forward declaration anyway; call it the same
3527 return Importer.Imported(D, FoundDef);
3531 // Create a new specialization.
3532 if (ClassTemplatePartialSpecializationDecl *PartialSpec =
3533 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
3535 // Import TemplateArgumentListInfo
3536 TemplateArgumentListInfo ToTAInfo;
3537 auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
3538 for (unsigned I = 0, E = ASTTemplateArgs.NumTemplateArgs; I < E; ++I) {
3540 auto ToLoc = ImportTemplateArgumentLoc(ASTTemplateArgs[I], Error);
3543 ToTAInfo.addArgument(ToLoc);
3546 QualType CanonInjType = Importer.Import(
3547 PartialSpec->getInjectedSpecializationType());
3548 if (CanonInjType.isNull())
3550 CanonInjType = CanonInjType.getCanonicalType();
3552 TemplateParameterList *ToTPList = ImportTemplateParameterList(
3553 PartialSpec->getTemplateParameters());
3554 if (!ToTPList && PartialSpec->getTemplateParameters())
3557 D2 = ClassTemplatePartialSpecializationDecl::Create(
3558 Importer.getToContext(), D->getTagKind(), DC, StartLoc, IdLoc,
3559 ToTPList, ClassTemplate,
3560 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
3561 ToTAInfo, CanonInjType, nullptr);
3564 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3565 D->getTagKind(), DC,
3569 /*PrevDecl=*/nullptr);
3572 D2->setSpecializationKind(D->getSpecializationKind());
3574 // Add this specialization to the class template.
3575 ClassTemplate->AddSpecialization(D2, InsertPos);
3577 // Import the qualifier, if any.
3578 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
3580 Importer.Imported(D, D2);
3582 if (auto *TSI = D->getTypeAsWritten()) {
3583 TypeSourceInfo *TInfo = Importer.Import(TSI);
3586 D2->setTypeAsWritten(TInfo);
3587 D2->setTemplateKeywordLoc(Importer.Import(D->getTemplateKeywordLoc()));
3588 D2->setExternLoc(Importer.Import(D->getExternLoc()));
3591 SourceLocation POI = Importer.Import(D->getPointOfInstantiation());
3593 D2->setPointOfInstantiation(POI);
3594 else if (D->getPointOfInstantiation().isValid())
3597 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
3599 // Add the specialization to this context.
3600 D2->setLexicalDeclContext(LexicalDC);
3601 LexicalDC->addDeclInternal(D2);
3603 Importer.Imported(D, D2);
3604 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
3610 Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
3611 // If this variable has a definition in the translation unit we're coming
3613 // but this particular declaration is not that definition, import the
3614 // definition and map to that.
3615 VarDecl *Definition =
3616 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
3617 if (Definition && Definition != D->getTemplatedDecl()) {
3618 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
3622 return Importer.Imported(D, ImportedDef);
3625 // Import the major distinguishing characteristics of this variable template.
3626 DeclContext *DC, *LexicalDC;
3627 DeclarationName Name;
3630 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3635 // We may already have a template of the same name; try to find and match it.
3636 assert(!DC->isFunctionOrMethod() &&
3637 "Variable templates cannot be declared at function scope");
3638 SmallVector<NamedDecl *, 4> ConflictingDecls;
3639 SmallVector<NamedDecl *, 2> FoundDecls;
3640 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3641 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3642 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3645 Decl *Found = FoundDecls[I];
3646 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
3647 if (IsStructuralMatch(D, FoundTemplate)) {
3648 // The variable templates structurally match; call it the same template.
3649 Importer.Imported(D->getTemplatedDecl(),
3650 FoundTemplate->getTemplatedDecl());
3651 return Importer.Imported(D, FoundTemplate);
3655 ConflictingDecls.push_back(FoundDecls[I]);
3658 if (!ConflictingDecls.empty()) {
3659 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3660 ConflictingDecls.data(),
3661 ConflictingDecls.size());
3667 VarDecl *DTemplated = D->getTemplatedDecl();
3670 QualType T = Importer.Import(DTemplated->getType());
3674 // Create the declaration that is being templated.
3675 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3676 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
3677 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo());
3678 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc,
3679 IdLoc, Name.getAsIdentifierInfo(), T,
3680 TInfo, DTemplated->getStorageClass());
3681 D2Templated->setAccess(DTemplated->getAccess());
3682 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
3683 D2Templated->setLexicalDeclContext(LexicalDC);
3685 // Importer.Imported(DTemplated, D2Templated);
3686 // LexicalDC->addDeclInternal(D2Templated);
3688 // Merge the initializer.
3689 if (ImportDefinition(DTemplated, D2Templated))
3692 // Create the variable template declaration itself.
3693 TemplateParameterList *TemplateParams =
3694 ImportTemplateParameterList(D->getTemplateParameters());
3695 if (!TemplateParams)
3698 VarTemplateDecl *D2 = VarTemplateDecl::Create(
3699 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated);
3700 D2Templated->setDescribedVarTemplate(D2);
3702 D2->setAccess(D->getAccess());
3703 D2->setLexicalDeclContext(LexicalDC);
3704 LexicalDC->addDeclInternal(D2);
3706 // Note the relationship between the variable templates.
3707 Importer.Imported(D, D2);
3708 Importer.Imported(DTemplated, D2Templated);
3710 if (DTemplated->isThisDeclarationADefinition() &&
3711 !D2Templated->isThisDeclarationADefinition()) {
3712 // FIXME: Import definition!
3718 Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
3719 VarTemplateSpecializationDecl *D) {
3720 // If this record has a definition in the translation unit we're coming from,
3721 // but this particular declaration is not that definition, import the
3722 // definition and map to that.
3723 VarDecl *Definition = D->getDefinition();
3724 if (Definition && Definition != D) {
3725 Decl *ImportedDef = Importer.Import(Definition);
3729 return Importer.Imported(D, ImportedDef);
3732 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
3733 Importer.Import(D->getSpecializedTemplate()));
3737 // Import the context of this declaration.
3738 DeclContext *DC = VarTemplate->getDeclContext();
3742 DeclContext *LexicalDC = DC;
3743 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3744 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3749 // Import the location of this declaration.
3750 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3751 SourceLocation IdLoc = Importer.Import(D->getLocation());
3753 // Import template arguments.
3754 SmallVector<TemplateArgument, 2> TemplateArgs;
3755 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3756 D->getTemplateArgs().size(), TemplateArgs))
3759 // Try to find an existing specialization with these template arguments.
3760 void *InsertPos = nullptr;
3761 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
3762 TemplateArgs, InsertPos);
3764 // We already have a variable template specialization with these template
3767 // FIXME: Check for specialization vs. instantiation errors.
3769 if (VarDecl *FoundDef = D2->getDefinition()) {
3770 if (!D->isThisDeclarationADefinition() ||
3771 IsStructuralMatch(D, FoundDef)) {
3772 // The record types structurally match, or the "from" translation
3773 // unit only had a forward declaration anyway; call it the same
3775 return Importer.Imported(D, FoundDef);
3781 QualType T = Importer.Import(D->getType());
3784 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3786 // Create a new specialization.
3787 D2 = VarTemplateSpecializationDecl::Create(
3788 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
3789 D->getStorageClass(), TemplateArgs);
3790 D2->setSpecializationKind(D->getSpecializationKind());
3791 D2->setTemplateArgsInfo(D->getTemplateArgsInfo());
3793 // Add this specialization to the class template.
3794 VarTemplate->AddSpecialization(D2, InsertPos);
3796 // Import the qualifier, if any.
3797 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
3799 // Add the specialization to this context.
3800 D2->setLexicalDeclContext(LexicalDC);
3801 LexicalDC->addDeclInternal(D2);
3803 Importer.Imported(D, D2);
3805 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2))
3811 //----------------------------------------------------------------------------
3812 // Import Statements
3813 //----------------------------------------------------------------------------
3815 DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
3817 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
3818 size_t NumDecls = DG.end() - DG.begin();
3819 SmallVector<Decl *, 1> ToDecls(NumDecls);
3820 auto &_Importer = this->Importer;
3821 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
3822 [&_Importer](Decl *D) -> Decl * {
3823 return _Importer.Import(D);
3825 return DeclGroupRef::Create(Importer.getToContext(),
3830 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3831 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3832 << S->getStmtClassName();
3837 Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
3838 SmallVector<IdentifierInfo *, 4> Names;
3839 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
3840 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
3841 // ToII is nullptr when no symbolic name is given for output operand
3842 // see ParseStmtAsm::ParseAsmOperandsOpt
3843 if (!ToII && S->getOutputIdentifier(I))
3845 Names.push_back(ToII);
3847 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
3848 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
3849 // ToII is nullptr when no symbolic name is given for input operand
3850 // see ParseStmtAsm::ParseAsmOperandsOpt
3851 if (!ToII && S->getInputIdentifier(I))
3853 Names.push_back(ToII);
3856 SmallVector<StringLiteral *, 4> Clobbers;
3857 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
3858 StringLiteral *Clobber = cast_or_null<StringLiteral>(
3859 Importer.Import(S->getClobberStringLiteral(I)));
3862 Clobbers.push_back(Clobber);
3865 SmallVector<StringLiteral *, 4> Constraints;
3866 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
3867 StringLiteral *Output = cast_or_null<StringLiteral>(
3868 Importer.Import(S->getOutputConstraintLiteral(I)));
3871 Constraints.push_back(Output);
3874 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
3875 StringLiteral *Input = cast_or_null<StringLiteral>(
3876 Importer.Import(S->getInputConstraintLiteral(I)));
3879 Constraints.push_back(Input);
3882 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
3883 if (ImportContainerChecked(S->outputs(), Exprs))
3886 if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
3889 StringLiteral *AsmStr = cast_or_null<StringLiteral>(
3890 Importer.Import(S->getAsmString()));
3894 return new (Importer.getToContext()) GCCAsmStmt(
3895 Importer.getToContext(),
3896 Importer.Import(S->getAsmLoc()),
3905 S->getNumClobbers(),
3907 Importer.Import(S->getRParenLoc()));
3910 Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
3911 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
3912 for (Decl *ToD : ToDG) {
3916 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
3917 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
3918 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
3921 Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
3922 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
3923 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
3924 S->hasLeadingEmptyMacro());
3927 Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
3928 llvm::SmallVector<Stmt *, 8> ToStmts(S->size());
3930 if (ImportContainerChecked(S->body(), ToStmts))
3933 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
3934 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
3935 return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(),
3937 ToLBraceLoc, ToRBraceLoc);
3940 Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
3941 Expr *ToLHS = Importer.Import(S->getLHS());
3944 Expr *ToRHS = Importer.Import(S->getRHS());
3945 if (!ToRHS && S->getRHS())
3947 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
3948 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
3949 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
3950 return new (Importer.getToContext()) CaseStmt(ToLHS, ToRHS,
3951 ToCaseLoc, ToEllipsisLoc,
3955 Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
3956 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
3957 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
3958 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
3959 if (!ToSubStmt && S->getSubStmt())
3961 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
3965 Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
3966 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
3967 LabelDecl *ToLabelDecl =
3968 cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
3969 if (!ToLabelDecl && S->getDecl())
3971 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
3972 if (!ToSubStmt && S->getSubStmt())
3974 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
3978 Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
3979 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
3980 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
3981 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
3982 ASTContext &_ToContext = Importer.getToContext();
3983 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
3984 [&_ToContext](const Attr *A) -> const Attr * {
3985 return A->clone(_ToContext);
3987 for (const Attr *ToA : ToAttrs) {
3991 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
3992 if (!ToSubStmt && S->getSubStmt())
3994 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
3995 ToAttrs, ToSubStmt);
3998 Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
3999 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
4000 Stmt *ToInit = Importer.Import(S->getInit());
4001 if (!ToInit && S->getInit())
4003 VarDecl *ToConditionVariable = nullptr;
4004 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4005 ToConditionVariable =
4006 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4007 if (!ToConditionVariable)
4010 Expr *ToCondition = Importer.Import(S->getCond());
4011 if (!ToCondition && S->getCond())
4013 Stmt *ToThenStmt = Importer.Import(S->getThen());
4014 if (!ToThenStmt && S->getThen())
4016 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4017 Stmt *ToElseStmt = Importer.Import(S->getElse());
4018 if (!ToElseStmt && S->getElse())
4020 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
4021 ToIfLoc, S->isConstexpr(),
4023 ToConditionVariable,
4024 ToCondition, ToThenStmt,
4025 ToElseLoc, ToElseStmt);
4028 Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
4029 Stmt *ToInit = Importer.Import(S->getInit());
4030 if (!ToInit && S->getInit())
4032 VarDecl *ToConditionVariable = nullptr;
4033 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4034 ToConditionVariable =
4035 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4036 if (!ToConditionVariable)
4039 Expr *ToCondition = Importer.Import(S->getCond());
4040 if (!ToCondition && S->getCond())
4042 SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt(
4043 Importer.getToContext(), ToInit,
4044 ToConditionVariable, ToCondition);
4045 Stmt *ToBody = Importer.Import(S->getBody());
4046 if (!ToBody && S->getBody())
4048 ToStmt->setBody(ToBody);
4049 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4050 // Now we have to re-chain the cases.
4051 SwitchCase *LastChainedSwitchCase = nullptr;
4052 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4053 SC = SC->getNextSwitchCase()) {
4054 SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
4057 if (LastChainedSwitchCase)
4058 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4060 ToStmt->setSwitchCaseList(ToSC);
4061 LastChainedSwitchCase = ToSC;
4066 Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
4067 VarDecl *ToConditionVariable = nullptr;
4068 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4069 ToConditionVariable =
4070 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4071 if (!ToConditionVariable)
4074 Expr *ToCondition = Importer.Import(S->getCond());
4075 if (!ToCondition && S->getCond())
4077 Stmt *ToBody = Importer.Import(S->getBody());
4078 if (!ToBody && S->getBody())
4080 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4081 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
4082 ToConditionVariable,
4083 ToCondition, ToBody,
4087 Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
4088 Stmt *ToBody = Importer.Import(S->getBody());
4089 if (!ToBody && S->getBody())
4091 Expr *ToCondition = Importer.Import(S->getCond());
4092 if (!ToCondition && S->getCond())
4094 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
4095 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4096 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4097 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
4098 ToDoLoc, ToWhileLoc,
4102 Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
4103 Stmt *ToInit = Importer.Import(S->getInit());
4104 if (!ToInit && S->getInit())
4106 Expr *ToCondition = Importer.Import(S->getCond());
4107 if (!ToCondition && S->getCond())
4109 VarDecl *ToConditionVariable = nullptr;
4110 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4111 ToConditionVariable =
4112 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4113 if (!ToConditionVariable)
4116 Expr *ToInc = Importer.Import(S->getInc());
4117 if (!ToInc && S->getInc())
4119 Stmt *ToBody = Importer.Import(S->getBody());
4120 if (!ToBody && S->getBody())
4122 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4123 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
4124 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4125 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
4126 ToInit, ToCondition,
4127 ToConditionVariable,
4129 ToForLoc, ToLParenLoc,
4133 Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
4134 LabelDecl *ToLabel = nullptr;
4135 if (LabelDecl *FromLabel = S->getLabel()) {
4136 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
4140 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4141 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
4142 return new (Importer.getToContext()) GotoStmt(ToLabel,
4143 ToGotoLoc, ToLabelLoc);
4146 Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
4147 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4148 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
4149 Expr *ToTarget = Importer.Import(S->getTarget());
4150 if (!ToTarget && S->getTarget())
4152 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
4156 Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
4157 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
4158 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
4161 Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
4162 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
4163 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
4166 Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
4167 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
4168 Expr *ToRetExpr = Importer.Import(S->getRetValue());
4169 if (!ToRetExpr && S->getRetValue())
4171 VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate());
4172 VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
4173 if (!ToNRVOCandidate && NRVOCandidate)
4175 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
4179 Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
4180 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
4181 VarDecl *ToExceptionDecl = nullptr;
4182 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
4184 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4185 if (!ToExceptionDecl)
4188 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
4189 if (!ToHandlerBlock && S->getHandlerBlock())
4191 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
4196 Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
4197 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
4198 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
4199 if (!ToTryBlock && S->getTryBlock())
4201 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
4202 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
4203 CXXCatchStmt *FromHandler = S->getHandler(HI);
4204 if (Stmt *ToHandler = Importer.Import(FromHandler))
4205 ToHandlers[HI] = ToHandler;
4209 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
4213 Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
4215 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
4216 if (!ToRange && S->getRangeStmt())
4219 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
4220 if (!ToBegin && S->getBeginStmt())
4223 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
4224 if (!ToEnd && S->getEndStmt())
4226 Expr *ToCond = Importer.Import(S->getCond());
4227 if (!ToCond && S->getCond())
4229 Expr *ToInc = Importer.Import(S->getInc());
4230 if (!ToInc && S->getInc())
4232 DeclStmt *ToLoopVar =
4233 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
4234 if (!ToLoopVar && S->getLoopVarStmt())
4236 Stmt *ToBody = Importer.Import(S->getBody());
4237 if (!ToBody && S->getBody())
4239 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4240 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
4241 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4242 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4243 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
4246 ToForLoc, ToCoawaitLoc,
4247 ToColonLoc, ToRParenLoc);
4250 Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
4251 Stmt *ToElem = Importer.Import(S->getElement());
4252 if (!ToElem && S->getElement())
4254 Expr *ToCollect = Importer.Import(S->getCollection());
4255 if (!ToCollect && S->getCollection())
4257 Stmt *ToBody = Importer.Import(S->getBody());
4258 if (!ToBody && S->getBody())
4260 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4261 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4262 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
4268 Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
4269 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
4270 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4271 VarDecl *ToExceptionDecl = nullptr;
4272 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
4274 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4275 if (!ToExceptionDecl)
4278 Stmt *ToBody = Importer.Import(S->getCatchBody());
4279 if (!ToBody && S->getCatchBody())
4281 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
4287 Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
4288 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
4289 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
4290 if (!ToAtFinallyStmt && S->getFinallyBody())
4292 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
4296 Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
4297 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
4298 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
4299 if (!ToAtTryStmt && S->getTryBody())
4301 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
4302 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
4303 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
4304 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
4305 ToCatchStmts[CI] = ToCatchStmt;
4309 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
4310 if (!ToAtFinallyStmt && S->getFinallyStmt())
4312 return ObjCAtTryStmt::Create(Importer.getToContext(),
4313 ToAtTryLoc, ToAtTryStmt,
4314 ToCatchStmts.begin(), ToCatchStmts.size(),
4318 Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
4319 (ObjCAtSynchronizedStmt *S) {
4320 SourceLocation ToAtSynchronizedLoc =
4321 Importer.Import(S->getAtSynchronizedLoc());
4322 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
4323 if (!ToSynchExpr && S->getSynchExpr())
4325 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
4326 if (!ToSynchBody && S->getSynchBody())
4328 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
4329 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
4332 Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
4333 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
4334 Expr *ToThrow = Importer.Import(S->getThrowExpr());
4335 if (!ToThrow && S->getThrowExpr())
4337 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
4340 Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
4341 (ObjCAutoreleasePoolStmt *S) {
4342 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
4343 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4344 if (!ToSubStmt && S->getSubStmt())
4346 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
4350 //----------------------------------------------------------------------------
4351 // Import Expressions
4352 //----------------------------------------------------------------------------
4353 Expr *ASTNodeImporter::VisitExpr(Expr *E) {
4354 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
4355 << E->getStmtClassName();
4359 Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
4360 QualType T = Importer.Import(E->getType());
4364 Expr *SubExpr = Importer.Import(E->getSubExpr());
4365 if (!SubExpr && E->getSubExpr())
4368 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
4372 return new (Importer.getToContext()) VAArgExpr(
4373 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
4374 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
4378 Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
4379 QualType T = Importer.Import(E->getType());
4383 return new (Importer.getToContext()) GNUNullExpr(
4384 T, Importer.Import(E->getLocStart()));
4387 Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
4388 QualType T = Importer.Import(E->getType());
4392 StringLiteral *SL = cast_or_null<StringLiteral>(
4393 Importer.Import(E->getFunctionName()));
4394 if (!SL && E->getFunctionName())
4397 return new (Importer.getToContext()) PredefinedExpr(
4398 Importer.Import(E->getLocStart()), T, E->getIdentType(), SL);
4401 Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
4402 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
4406 NamedDecl *FoundD = nullptr;
4407 if (E->getDecl() != E->getFoundDecl()) {
4408 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
4413 QualType T = Importer.Import(E->getType());
4418 TemplateArgumentListInfo ToTAInfo;
4419 TemplateArgumentListInfo *ResInfo = nullptr;
4420 if (E->hasExplicitTemplateArgs()) {
4421 for (const auto &FromLoc : E->template_arguments()) {
4423 TemplateArgumentLoc ToTALoc = ImportTemplateArgumentLoc(FromLoc, Error);
4426 ToTAInfo.addArgument(ToTALoc);
4428 ResInfo = &ToTAInfo;
4431 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
4432 Importer.Import(E->getQualifierLoc()),
4433 Importer.Import(E->getTemplateKeywordLoc()),
4435 E->refersToEnclosingVariableOrCapture(),
4436 Importer.Import(E->getLocation()),
4437 T, E->getValueKind(),
4439 if (E->hadMultipleCandidates())
4440 DRE->setHadMultipleCandidates(true);
4444 Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
4445 QualType T = Importer.Import(E->getType());
4449 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
4452 ASTNodeImporter::Designator
4453 ASTNodeImporter::ImportDesignator(const Designator &D) {
4454 if (D.isFieldDesignator()) {
4455 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
4456 // Caller checks for import error
4457 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
4458 Importer.Import(D.getFieldLoc()));
4460 if (D.isArrayDesignator())
4461 return Designator(D.getFirstExprIndex(),
4462 Importer.Import(D.getLBracketLoc()),
4463 Importer.Import(D.getRBracketLoc()));
4465 assert(D.isArrayRangeDesignator());
4466 return Designator(D.getFirstExprIndex(),
4467 Importer.Import(D.getLBracketLoc()),
4468 Importer.Import(D.getEllipsisLoc()),
4469 Importer.Import(D.getRBracketLoc()));
4473 Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
4474 Expr *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
4478 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
4479 // List elements from the second, the first is Init itself
4480 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
4481 if (Expr *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
4482 IndexExprs[I - 1] = Arg;
4487 SmallVector<Designator, 4> Designators(DIE->size());
4488 llvm::transform(DIE->designators(), Designators.begin(),
4489 [this](const Designator &D) -> Designator {
4490 return ImportDesignator(D);
4493 for (const Designator &D : DIE->designators())
4494 if (D.isFieldDesignator() && !D.getFieldName())
4497 return DesignatedInitExpr::Create(
4498 Importer.getToContext(), Designators,
4499 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
4500 DIE->usesGNUSyntax(), Init);
4503 Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
4504 QualType T = Importer.Import(E->getType());
4508 return new (Importer.getToContext())
4509 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
4512 Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
4513 QualType T = Importer.Import(E->getType());
4517 return IntegerLiteral::Create(Importer.getToContext(),
4519 Importer.Import(E->getLocation()));
4522 Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
4523 QualType T = Importer.Import(E->getType());
4527 return FloatingLiteral::Create(Importer.getToContext(),
4528 E->getValue(), E->isExact(), T,
4529 Importer.Import(E->getLocation()));
4532 Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
4533 QualType T = Importer.Import(E->getType());
4537 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
4539 Importer.Import(E->getLocation()));
4542 Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
4543 QualType T = Importer.Import(E->getType());
4547 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
4548 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
4550 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
4551 E->getKind(), E->isPascal(), T,
4552 Locations.data(), Locations.size());
4555 Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
4556 QualType T = Importer.Import(E->getType());
4560 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
4564 Expr *Init = Importer.Import(E->getInitializer());
4568 return new (Importer.getToContext()) CompoundLiteralExpr(
4569 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
4570 Init, E->isFileScope());
4573 Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
4574 QualType T = Importer.Import(E->getType());
4578 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
4579 if (ImportArrayChecked(
4580 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
4584 return new (Importer.getToContext()) AtomicExpr(
4585 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
4586 Importer.Import(E->getRParenLoc()));
4589 Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
4590 QualType T = Importer.Import(E->getType());
4594 LabelDecl *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
4598 return new (Importer.getToContext()) AddrLabelExpr(
4599 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
4603 Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
4604 Expr *SubExpr = Importer.Import(E->getSubExpr());
4608 return new (Importer.getToContext())
4609 ParenExpr(Importer.Import(E->getLParen()),
4610 Importer.Import(E->getRParen()),
4614 Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
4615 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
4616 if (ImportContainerChecked(E->exprs(), Exprs))
4619 return new (Importer.getToContext()) ParenListExpr(
4620 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
4621 Exprs, Importer.Import(E->getLParenLoc()));
4624 Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
4625 QualType T = Importer.Import(E->getType());
4629 CompoundStmt *ToSubStmt = cast_or_null<CompoundStmt>(
4630 Importer.Import(E->getSubStmt()));
4631 if (!ToSubStmt && E->getSubStmt())
4634 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
4635 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
4638 Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
4639 QualType T = Importer.Import(E->getType());
4643 Expr *SubExpr = Importer.Import(E->getSubExpr());
4647 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
4648 T, E->getValueKind(),
4650 Importer.Import(E->getOperatorLoc()));
4653 Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
4654 UnaryExprOrTypeTraitExpr *E) {
4655 QualType ResultType = Importer.Import(E->getType());
4657 if (E->isArgumentType()) {
4658 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
4662 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4664 Importer.Import(E->getOperatorLoc()),
4665 Importer.Import(E->getRParenLoc()));
4668 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
4672 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4673 SubExpr, ResultType,
4674 Importer.Import(E->getOperatorLoc()),
4675 Importer.Import(E->getRParenLoc()));
4678 Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
4679 QualType T = Importer.Import(E->getType());
4683 Expr *LHS = Importer.Import(E->getLHS());
4687 Expr *RHS = Importer.Import(E->getRHS());
4691 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
4692 T, E->getValueKind(),
4694 Importer.Import(E->getOperatorLoc()),
4695 E->getFPFeatures());
4698 Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
4699 QualType T = Importer.Import(E->getType());
4703 Expr *ToLHS = Importer.Import(E->getLHS());
4707 Expr *ToRHS = Importer.Import(E->getRHS());
4711 Expr *ToCond = Importer.Import(E->getCond());
4715 return new (Importer.getToContext()) ConditionalOperator(
4716 ToCond, Importer.Import(E->getQuestionLoc()),
4717 ToLHS, Importer.Import(E->getColonLoc()),
4718 ToRHS, T, E->getValueKind(), E->getObjectKind());
4721 Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
4722 BinaryConditionalOperator *E) {
4723 QualType T = Importer.Import(E->getType());
4727 Expr *Common = Importer.Import(E->getCommon());
4731 Expr *Cond = Importer.Import(E->getCond());
4735 OpaqueValueExpr *OpaqueValue = cast_or_null<OpaqueValueExpr>(
4736 Importer.Import(E->getOpaqueValue()));
4740 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
4744 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
4748 return new (Importer.getToContext()) BinaryConditionalOperator(
4749 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
4750 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
4751 T, E->getValueKind(), E->getObjectKind());
4754 Expr *ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
4755 QualType T = Importer.Import(E->getType());
4759 TypeSourceInfo *ToQueried = Importer.Import(E->getQueriedTypeSourceInfo());
4763 Expr *Dim = Importer.Import(E->getDimensionExpression());
4764 if (!Dim && E->getDimensionExpression())
4767 return new (Importer.getToContext()) ArrayTypeTraitExpr(
4768 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
4769 E->getValue(), Dim, Importer.Import(E->getLocEnd()), T);
4772 Expr *ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
4773 QualType T = Importer.Import(E->getType());
4777 Expr *ToQueried = Importer.Import(E->getQueriedExpression());
4781 return new (Importer.getToContext()) ExpressionTraitExpr(
4782 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
4783 E->getValue(), Importer.Import(E->getLocEnd()), T);
4786 Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
4787 QualType T = Importer.Import(E->getType());
4791 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
4792 if (!SourceExpr && E->getSourceExpr())
4795 return new (Importer.getToContext()) OpaqueValueExpr(
4796 Importer.Import(E->getLocation()), T, E->getValueKind(),
4797 E->getObjectKind(), SourceExpr);
4800 Expr *ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
4801 QualType T = Importer.Import(E->getType());
4805 Expr *ToLHS = Importer.Import(E->getLHS());
4809 Expr *ToRHS = Importer.Import(E->getRHS());
4813 return new (Importer.getToContext()) ArraySubscriptExpr(
4814 ToLHS, ToRHS, T, E->getValueKind(), E->getObjectKind(),
4815 Importer.Import(E->getRBracketLoc()));
4818 Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
4819 QualType T = Importer.Import(E->getType());
4823 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
4824 if (CompLHSType.isNull())
4827 QualType CompResultType = Importer.Import(E->getComputationResultType());
4828 if (CompResultType.isNull())
4831 Expr *LHS = Importer.Import(E->getLHS());
4835 Expr *RHS = Importer.Import(E->getRHS());
4839 return new (Importer.getToContext())
4840 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
4841 T, E->getValueKind(),
4843 CompLHSType, CompResultType,
4844 Importer.Import(E->getOperatorLoc()),
4845 E->getFPFeatures());
4848 bool ASTNodeImporter::ImportCastPath(CastExpr *CE, CXXCastPath &Path) {
4849 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
4850 if (CXXBaseSpecifier *Spec = Importer.Import(*I))
4851 Path.push_back(Spec);
4858 Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4859 QualType T = Importer.Import(E->getType());
4863 Expr *SubExpr = Importer.Import(E->getSubExpr());
4867 CXXCastPath BasePath;
4868 if (ImportCastPath(E, BasePath))
4871 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
4872 SubExpr, &BasePath, E->getValueKind());
4875 Expr *ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
4876 QualType T = Importer.Import(E->getType());
4880 Expr *SubExpr = Importer.Import(E->getSubExpr());
4884 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4885 if (!TInfo && E->getTypeInfoAsWritten())
4888 CXXCastPath BasePath;
4889 if (ImportCastPath(E, BasePath))
4892 switch (E->getStmtClass()) {
4893 case Stmt::CStyleCastExprClass: {
4894 CStyleCastExpr *CCE = cast<CStyleCastExpr>(E);
4895 return CStyleCastExpr::Create(Importer.getToContext(), T,
4896 E->getValueKind(), E->getCastKind(),
4897 SubExpr, &BasePath, TInfo,
4898 Importer.Import(CCE->getLParenLoc()),
4899 Importer.Import(CCE->getRParenLoc()));
4902 case Stmt::CXXFunctionalCastExprClass: {
4903 CXXFunctionalCastExpr *FCE = cast<CXXFunctionalCastExpr>(E);
4904 return CXXFunctionalCastExpr::Create(Importer.getToContext(), T,
4905 E->getValueKind(), TInfo,
4906 E->getCastKind(), SubExpr, &BasePath,
4907 Importer.Import(FCE->getLParenLoc()),
4908 Importer.Import(FCE->getRParenLoc()));
4911 case Stmt::ObjCBridgedCastExprClass: {
4912 ObjCBridgedCastExpr *OCE = cast<ObjCBridgedCastExpr>(E);
4913 return new (Importer.getToContext()) ObjCBridgedCastExpr(
4914 Importer.Import(OCE->getLParenLoc()), OCE->getBridgeKind(),
4915 E->getCastKind(), Importer.Import(OCE->getBridgeKeywordLoc()),
4919 break; // just fall through
4922 CXXNamedCastExpr *Named = cast<CXXNamedCastExpr>(E);
4923 SourceLocation ExprLoc = Importer.Import(Named->getOperatorLoc()),
4924 RParenLoc = Importer.Import(Named->getRParenLoc());
4925 SourceRange Brackets = Importer.Import(Named->getAngleBrackets());
4927 switch (E->getStmtClass()) {
4928 case Stmt::CXXStaticCastExprClass:
4929 return CXXStaticCastExpr::Create(Importer.getToContext(), T,
4930 E->getValueKind(), E->getCastKind(),
4931 SubExpr, &BasePath, TInfo,
4932 ExprLoc, RParenLoc, Brackets);
4934 case Stmt::CXXDynamicCastExprClass:
4935 return CXXDynamicCastExpr::Create(Importer.getToContext(), T,
4936 E->getValueKind(), E->getCastKind(),
4937 SubExpr, &BasePath, TInfo,
4938 ExprLoc, RParenLoc, Brackets);
4940 case Stmt::CXXReinterpretCastExprClass:
4941 return CXXReinterpretCastExpr::Create(Importer.getToContext(), T,
4942 E->getValueKind(), E->getCastKind(),
4943 SubExpr, &BasePath, TInfo,
4944 ExprLoc, RParenLoc, Brackets);
4946 case Stmt::CXXConstCastExprClass:
4947 return CXXConstCastExpr::Create(Importer.getToContext(), T,
4948 E->getValueKind(), SubExpr, TInfo, ExprLoc,
4949 RParenLoc, Brackets);
4951 llvm_unreachable("Cast expression of unsupported type!");
4956 Expr *ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *OE) {
4957 QualType T = Importer.Import(OE->getType());
4961 SmallVector<OffsetOfNode, 4> Nodes;
4962 for (int I = 0, E = OE->getNumComponents(); I < E; ++I) {
4963 const OffsetOfNode &Node = OE->getComponent(I);
4965 switch (Node.getKind()) {
4966 case OffsetOfNode::Array:
4967 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()),
4968 Node.getArrayExprIndex(),
4969 Importer.Import(Node.getLocEnd())));
4972 case OffsetOfNode::Base: {
4973 CXXBaseSpecifier *BS = Importer.Import(Node.getBase());
4974 if (!BS && Node.getBase())
4976 Nodes.push_back(OffsetOfNode(BS));
4979 case OffsetOfNode::Field: {
4980 FieldDecl *FD = cast_or_null<FieldDecl>(Importer.Import(Node.getField()));
4983 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), FD,
4984 Importer.Import(Node.getLocEnd())));
4987 case OffsetOfNode::Identifier: {
4988 IdentifierInfo *ToII = Importer.Import(Node.getFieldName());
4991 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), ToII,
4992 Importer.Import(Node.getLocEnd())));
4998 SmallVector<Expr *, 4> Exprs(OE->getNumExpressions());
4999 for (int I = 0, E = OE->getNumExpressions(); I < E; ++I) {
5000 Expr *ToIndexExpr = Importer.Import(OE->getIndexExpr(I));
5003 Exprs[I] = ToIndexExpr;
5006 TypeSourceInfo *TInfo = Importer.Import(OE->getTypeSourceInfo());
5007 if (!TInfo && OE->getTypeSourceInfo())
5010 return OffsetOfExpr::Create(Importer.getToContext(), T,
5011 Importer.Import(OE->getOperatorLoc()),
5012 TInfo, Nodes, Exprs,
5013 Importer.Import(OE->getRParenLoc()));
5016 Expr *ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
5017 QualType T = Importer.Import(E->getType());
5021 Expr *Operand = Importer.Import(E->getOperand());
5025 CanThrowResult CanThrow;
5026 if (E->isValueDependent())
5027 CanThrow = CT_Dependent;
5029 CanThrow = E->getValue() ? CT_Can : CT_Cannot;
5031 return new (Importer.getToContext()) CXXNoexceptExpr(
5032 T, Operand, CanThrow,
5033 Importer.Import(E->getLocStart()), Importer.Import(E->getLocEnd()));
5036 Expr *ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
5037 QualType T = Importer.Import(E->getType());
5041 Expr *SubExpr = Importer.Import(E->getSubExpr());
5042 if (!SubExpr && E->getSubExpr())
5045 return new (Importer.getToContext()) CXXThrowExpr(
5046 SubExpr, T, Importer.Import(E->getThrowLoc()),
5047 E->isThrownVariableInScope());
5050 Expr *ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
5051 ParmVarDecl *Param = cast_or_null<ParmVarDecl>(
5052 Importer.Import(E->getParam()));
5056 return CXXDefaultArgExpr::Create(
5057 Importer.getToContext(), Importer.Import(E->getUsedLocation()), Param);
5060 Expr *ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
5061 QualType T = Importer.Import(E->getType());
5065 TypeSourceInfo *TypeInfo = Importer.Import(E->getTypeSourceInfo());
5069 return new (Importer.getToContext()) CXXScalarValueInitExpr(
5070 T, TypeInfo, Importer.Import(E->getRParenLoc()));
5073 Expr *ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
5074 Expr *SubExpr = Importer.Import(E->getSubExpr());
5078 auto *Dtor = cast_or_null<CXXDestructorDecl>(
5079 Importer.Import(const_cast<CXXDestructorDecl *>(
5080 E->getTemporary()->getDestructor())));
5084 ASTContext &ToCtx = Importer.getToContext();
5085 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, Dtor);
5086 return CXXBindTemporaryExpr::Create(ToCtx, Temp, SubExpr);
5089 Expr *ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE) {
5090 QualType T = Importer.Import(CE->getType());
5094 SmallVector<Expr *, 8> Args(CE->getNumArgs());
5095 if (ImportContainerChecked(CE->arguments(), Args))
5098 auto *Ctor = cast_or_null<CXXConstructorDecl>(
5099 Importer.Import(CE->getConstructor()));
5103 return CXXTemporaryObjectExpr::Create(
5104 Importer.getToContext(), T,
5105 Importer.Import(CE->getLocStart()),
5109 CE->hadMultipleCandidates(),
5110 CE->isListInitialization(),
5111 CE->isStdInitListInitialization(),
5112 CE->requiresZeroInitialization(),
5113 CE->getConstructionKind(),
5114 Importer.Import(CE->getParenOrBraceRange()));
5118 ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
5119 QualType T = Importer.Import(E->getType());
5123 Expr *TempE = Importer.Import(E->GetTemporaryExpr());
5127 ValueDecl *ExtendedBy = cast_or_null<ValueDecl>(
5128 Importer.Import(const_cast<ValueDecl *>(E->getExtendingDecl())));
5129 if (!ExtendedBy && E->getExtendingDecl())
5132 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
5133 T, TempE, E->isBoundToLvalueReference());
5135 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
5136 ToMTE->setExtendingDecl(ExtendedBy, E->getManglingNumber());
5140 Expr *ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *CE) {
5141 QualType T = Importer.Import(CE->getType());
5145 SmallVector<Expr *, 4> PlacementArgs(CE->getNumPlacementArgs());
5146 if (ImportContainerChecked(CE->placement_arguments(), PlacementArgs))
5149 FunctionDecl *OperatorNewDecl = cast_or_null<FunctionDecl>(
5150 Importer.Import(CE->getOperatorNew()));
5151 if (!OperatorNewDecl && CE->getOperatorNew())
5154 FunctionDecl *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
5155 Importer.Import(CE->getOperatorDelete()));
5156 if (!OperatorDeleteDecl && CE->getOperatorDelete())
5159 Expr *ToInit = Importer.Import(CE->getInitializer());
5160 if (!ToInit && CE->getInitializer())
5163 TypeSourceInfo *TInfo = Importer.Import(CE->getAllocatedTypeSourceInfo());
5167 Expr *ToArrSize = Importer.Import(CE->getArraySize());
5168 if (!ToArrSize && CE->getArraySize())
5171 return new (Importer.getToContext()) CXXNewExpr(
5172 Importer.getToContext(),
5174 OperatorNewDecl, OperatorDeleteDecl,
5175 CE->passAlignment(),
5176 CE->doesUsualArrayDeleteWantSize(),
5178 Importer.Import(CE->getTypeIdParens()),
5179 ToArrSize, CE->getInitializationStyle(), ToInit, T, TInfo,
5180 Importer.Import(CE->getSourceRange()),
5181 Importer.Import(CE->getDirectInitRange()));
5184 Expr *ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
5185 QualType T = Importer.Import(E->getType());
5189 FunctionDecl *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
5190 Importer.Import(E->getOperatorDelete()));
5191 if (!OperatorDeleteDecl && E->getOperatorDelete())
5194 Expr *ToArg = Importer.Import(E->getArgument());
5195 if (!ToArg && E->getArgument())
5198 return new (Importer.getToContext()) CXXDeleteExpr(
5199 T, E->isGlobalDelete(),
5201 E->isArrayFormAsWritten(),
5202 E->doesUsualArrayDeleteWantSize(),
5205 Importer.Import(E->getLocStart()));
5208 Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
5209 QualType T = Importer.Import(E->getType());
5213 CXXConstructorDecl *ToCCD =
5214 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
5218 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
5219 if (ImportContainerChecked(E->arguments(), ToArgs))
5222 return CXXConstructExpr::Create(Importer.getToContext(), T,
5223 Importer.Import(E->getLocation()),
5224 ToCCD, E->isElidable(),
5225 ToArgs, E->hadMultipleCandidates(),
5226 E->isListInitialization(),
5227 E->isStdInitListInitialization(),
5228 E->requiresZeroInitialization(),
5229 E->getConstructionKind(),
5230 Importer.Import(E->getParenOrBraceRange()));
5233 Expr *ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *EWC) {
5234 Expr *SubExpr = Importer.Import(EWC->getSubExpr());
5235 if (!SubExpr && EWC->getSubExpr())
5238 SmallVector<ExprWithCleanups::CleanupObject, 8> Objs(EWC->getNumObjects());
5239 for (unsigned I = 0, E = EWC->getNumObjects(); I < E; I++)
5240 if (ExprWithCleanups::CleanupObject Obj =
5241 cast_or_null<BlockDecl>(Importer.Import(EWC->getObject(I))))
5246 return ExprWithCleanups::Create(Importer.getToContext(),
5247 SubExpr, EWC->cleanupsHaveSideEffects(),
5251 Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
5252 QualType T = Importer.Import(E->getType());
5256 Expr *ToFn = Importer.Import(E->getCallee());
5260 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
5261 if (ImportContainerChecked(E->arguments(), ToArgs))
5264 return new (Importer.getToContext()) CXXMemberCallExpr(
5265 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
5266 Importer.Import(E->getRParenLoc()));
5269 Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
5270 QualType T = Importer.Import(E->getType());
5274 return new (Importer.getToContext())
5275 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
5278 Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
5279 QualType T = Importer.Import(E->getType());
5283 return new (Importer.getToContext())
5284 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
5288 Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
5289 QualType T = Importer.Import(E->getType());
5293 Expr *ToBase = Importer.Import(E->getBase());
5294 if (!ToBase && E->getBase())
5297 ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
5298 if (!ToMember && E->getMemberDecl())
5301 DeclAccessPair ToFoundDecl = DeclAccessPair::make(
5302 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
5303 E->getFoundDecl().getAccess());
5305 DeclarationNameInfo ToMemberNameInfo(
5306 Importer.Import(E->getMemberNameInfo().getName()),
5307 Importer.Import(E->getMemberNameInfo().getLoc()));
5309 if (E->hasExplicitTemplateArgs()) {
5310 return nullptr; // FIXME: handle template arguments
5313 return MemberExpr::Create(Importer.getToContext(), ToBase,
5315 Importer.Import(E->getOperatorLoc()),
5316 Importer.Import(E->getQualifierLoc()),
5317 Importer.Import(E->getTemplateKeywordLoc()),
5318 ToMember, ToFoundDecl, ToMemberNameInfo,
5319 nullptr, T, E->getValueKind(),
5320 E->getObjectKind());
5323 Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
5324 QualType T = Importer.Import(E->getType());
5328 Expr *ToCallee = Importer.Import(E->getCallee());
5329 if (!ToCallee && E->getCallee())
5332 unsigned NumArgs = E->getNumArgs();
5334 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
5336 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) {
5337 Expr *FromArg = E->getArg(ai);
5338 Expr *ToArg = Importer.Import(FromArg);
5344 Expr **ToArgs_Copied = new (Importer.getToContext())
5347 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
5348 ToArgs_Copied[ai] = ToArgs[ai];
5350 return new (Importer.getToContext())
5351 CallExpr(Importer.getToContext(), ToCallee,
5352 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
5353 Importer.Import(E->getRParenLoc()));
5356 Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
5357 QualType T = Importer.Import(ILE->getType());
5361 llvm::SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
5362 if (ImportContainerChecked(ILE->inits(), Exprs))
5365 ASTContext &ToCtx = Importer.getToContext();
5366 InitListExpr *To = new (ToCtx) InitListExpr(
5367 ToCtx, Importer.Import(ILE->getLBraceLoc()),
5368 Exprs, Importer.Import(ILE->getLBraceLoc()));
5371 if (ILE->hasArrayFiller()) {
5372 Expr *Filler = Importer.Import(ILE->getArrayFiller());
5375 To->setArrayFiller(Filler);
5378 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
5379 FieldDecl *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
5382 To->setInitializedFieldInUnion(ToFD);
5385 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
5386 InitListExpr *ToSyntForm = cast_or_null<InitListExpr>(
5387 Importer.Import(SyntForm));
5390 To->setSyntacticForm(ToSyntForm);
5393 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
5394 To->setValueDependent(ILE->isValueDependent());
5395 To->setInstantiationDependent(ILE->isInstantiationDependent());
5400 Expr *ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
5401 QualType ToType = Importer.Import(E->getType());
5402 if (ToType.isNull())
5405 Expr *ToCommon = Importer.Import(E->getCommonExpr());
5406 if (!ToCommon && E->getCommonExpr())
5409 Expr *ToSubExpr = Importer.Import(E->getSubExpr());
5410 if (!ToSubExpr && E->getSubExpr())
5413 return new (Importer.getToContext())
5414 ArrayInitLoopExpr(ToType, ToCommon, ToSubExpr);
5417 Expr *ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
5418 QualType ToType = Importer.Import(E->getType());
5419 if (ToType.isNull())
5421 return new (Importer.getToContext()) ArrayInitIndexExpr(ToType);
5424 Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
5425 FieldDecl *ToField = llvm::dyn_cast_or_null<FieldDecl>(
5426 Importer.Import(DIE->getField()));
5427 if (!ToField && DIE->getField())
5430 return CXXDefaultInitExpr::Create(
5431 Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField);
5434 Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
5435 QualType ToType = Importer.Import(E->getType());
5436 if (ToType.isNull() && !E->getType().isNull())
5438 ExprValueKind VK = E->getValueKind();
5439 CastKind CK = E->getCastKind();
5440 Expr *ToOp = Importer.Import(E->getSubExpr());
5441 if (!ToOp && E->getSubExpr())
5443 CXXCastPath BasePath;
5444 if (ImportCastPath(E, BasePath))
5446 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten());
5447 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc());
5448 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc());
5449 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets());
5451 if (isa<CXXStaticCastExpr>(E)) {
5452 return CXXStaticCastExpr::Create(
5453 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
5454 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
5455 } else if (isa<CXXDynamicCastExpr>(E)) {
5456 return CXXDynamicCastExpr::Create(
5457 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
5458 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
5459 } else if (isa<CXXReinterpretCastExpr>(E)) {
5460 return CXXReinterpretCastExpr::Create(
5461 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
5462 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
5469 Expr *ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
5470 SubstNonTypeTemplateParmExpr *E) {
5471 QualType T = Importer.Import(E->getType());
5475 NonTypeTemplateParmDecl *Param = cast_or_null<NonTypeTemplateParmDecl>(
5476 Importer.Import(E->getParameter()));
5480 Expr *Replacement = Importer.Import(E->getReplacement());
5484 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
5485 T, E->getValueKind(), Importer.Import(E->getExprLoc()), Param,
5489 ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
5490 ASTContext &FromContext, FileManager &FromFileManager,
5492 : ToContext(ToContext), FromContext(FromContext),
5493 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
5494 Minimal(MinimalImport), LastDiagFromFrom(false)
5496 ImportedDecls[FromContext.getTranslationUnitDecl()]
5497 = ToContext.getTranslationUnitDecl();
5500 ASTImporter::~ASTImporter() { }
5502 QualType ASTImporter::Import(QualType FromT) {
5506 const Type *fromTy = FromT.getTypePtr();
5508 // Check whether we've already imported this type.
5509 llvm::DenseMap<const Type *, const Type *>::iterator Pos
5510 = ImportedTypes.find(fromTy);
5511 if (Pos != ImportedTypes.end())
5512 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
5515 ASTNodeImporter Importer(*this);
5516 QualType ToT = Importer.Visit(fromTy);
5520 // Record the imported type.
5521 ImportedTypes[fromTy] = ToT.getTypePtr();
5523 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
5526 TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
5530 // FIXME: For now we just create a "trivial" type source info based
5531 // on the type and a single location. Implement a real version of this.
5532 QualType T = Import(FromTSI->getType());
5536 return ToContext.getTrivialTypeSourceInfo(T,
5537 Import(FromTSI->getTypeLoc().getLocStart()));
5540 Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
5541 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
5542 if (Pos != ImportedDecls.end()) {
5543 Decl *ToD = Pos->second;
5544 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
5551 Decl *ASTImporter::Import(Decl *FromD) {
5555 ASTNodeImporter Importer(*this);
5557 // Check whether we've already imported this declaration.
5558 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
5559 if (Pos != ImportedDecls.end()) {
5560 Decl *ToD = Pos->second;
5561 Importer.ImportDefinitionIfNeeded(FromD, ToD);
5566 Decl *ToD = Importer.Visit(FromD);
5570 // Record the imported declaration.
5571 ImportedDecls[FromD] = ToD;
5573 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
5574 // Keep track of anonymous tags that have an associated typedef.
5575 if (FromTag->getTypedefNameForAnonDecl())
5576 AnonTagsWithPendingTypedefs.push_back(FromTag);
5577 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
5578 // When we've finished transforming a typedef, see whether it was the
5579 // typedef for an anonymous tag.
5580 for (SmallVectorImpl<TagDecl *>::iterator
5581 FromTag = AnonTagsWithPendingTypedefs.begin(),
5582 FromTagEnd = AnonTagsWithPendingTypedefs.end();
5583 FromTag != FromTagEnd; ++FromTag) {
5584 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
5585 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
5586 // We found the typedef for an anonymous tag; link them.
5587 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
5588 AnonTagsWithPendingTypedefs.erase(FromTag);
5598 DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
5602 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
5606 // When we're using a record/enum/Objective-C class/protocol as a context, we
5607 // need it to have a definition.
5608 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
5609 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
5610 if (ToRecord->isCompleteDefinition()) {
5612 } else if (FromRecord->isCompleteDefinition()) {
5613 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
5614 ASTNodeImporter::IDK_Basic);
5616 CompleteDecl(ToRecord);
5618 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
5619 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
5620 if (ToEnum->isCompleteDefinition()) {
5622 } else if (FromEnum->isCompleteDefinition()) {
5623 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
5624 ASTNodeImporter::IDK_Basic);
5626 CompleteDecl(ToEnum);
5628 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
5629 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
5630 if (ToClass->getDefinition()) {
5632 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
5633 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
5634 ASTNodeImporter::IDK_Basic);
5636 CompleteDecl(ToClass);
5638 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
5639 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
5640 if (ToProto->getDefinition()) {
5642 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
5643 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
5644 ASTNodeImporter::IDK_Basic);
5646 CompleteDecl(ToProto);
5653 Expr *ASTImporter::Import(Expr *FromE) {
5657 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
5660 Stmt *ASTImporter::Import(Stmt *FromS) {
5664 // Check whether we've already imported this declaration.
5665 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
5666 if (Pos != ImportedStmts.end())
5670 ASTNodeImporter Importer(*this);
5671 Stmt *ToS = Importer.Visit(FromS);
5675 // Record the imported declaration.
5676 ImportedStmts[FromS] = ToS;
5680 NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
5684 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
5686 switch (FromNNS->getKind()) {
5687 case NestedNameSpecifier::Identifier:
5688 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
5689 return NestedNameSpecifier::Create(ToContext, prefix, II);
5693 case NestedNameSpecifier::Namespace:
5694 if (NamespaceDecl *NS =
5695 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
5696 return NestedNameSpecifier::Create(ToContext, prefix, NS);
5700 case NestedNameSpecifier::NamespaceAlias:
5701 if (NamespaceAliasDecl *NSAD =
5702 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
5703 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
5707 case NestedNameSpecifier::Global:
5708 return NestedNameSpecifier::GlobalSpecifier(ToContext);
5710 case NestedNameSpecifier::Super:
5711 if (CXXRecordDecl *RD =
5712 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
5713 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
5717 case NestedNameSpecifier::TypeSpec:
5718 case NestedNameSpecifier::TypeSpecWithTemplate: {
5719 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
5721 bool bTemplate = FromNNS->getKind() ==
5722 NestedNameSpecifier::TypeSpecWithTemplate;
5723 return NestedNameSpecifier::Create(ToContext, prefix,
5724 bTemplate, T.getTypePtr());
5730 llvm_unreachable("Invalid nested name specifier kind");
5733 NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
5734 // Copied from NestedNameSpecifier mostly.
5735 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
5736 NestedNameSpecifierLoc NNS = FromNNS;
5738 // Push each of the nested-name-specifiers's onto a stack for
5739 // serialization in reverse order.
5741 NestedNames.push_back(NNS);
5742 NNS = NNS.getPrefix();
5745 NestedNameSpecifierLocBuilder Builder;
5747 while (!NestedNames.empty()) {
5748 NNS = NestedNames.pop_back_val();
5749 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
5751 return NestedNameSpecifierLoc();
5753 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
5755 case NestedNameSpecifier::Identifier:
5756 Builder.Extend(getToContext(),
5757 Spec->getAsIdentifier(),
5758 Import(NNS.getLocalBeginLoc()),
5759 Import(NNS.getLocalEndLoc()));
5762 case NestedNameSpecifier::Namespace:
5763 Builder.Extend(getToContext(),
5764 Spec->getAsNamespace(),
5765 Import(NNS.getLocalBeginLoc()),
5766 Import(NNS.getLocalEndLoc()));
5769 case NestedNameSpecifier::NamespaceAlias:
5770 Builder.Extend(getToContext(),
5771 Spec->getAsNamespaceAlias(),
5772 Import(NNS.getLocalBeginLoc()),
5773 Import(NNS.getLocalEndLoc()));
5776 case NestedNameSpecifier::TypeSpec:
5777 case NestedNameSpecifier::TypeSpecWithTemplate: {
5778 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
5779 QualType(Spec->getAsType(), 0));
5780 Builder.Extend(getToContext(),
5781 Import(NNS.getLocalBeginLoc()),
5783 Import(NNS.getLocalEndLoc()));
5787 case NestedNameSpecifier::Global:
5788 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
5791 case NestedNameSpecifier::Super: {
5792 SourceRange ToRange = Import(NNS.getSourceRange());
5793 Builder.MakeSuper(getToContext(),
5794 Spec->getAsRecordDecl(),
5801 return Builder.getWithLocInContext(getToContext());
5804 TemplateName ASTImporter::Import(TemplateName From) {
5805 switch (From.getKind()) {
5806 case TemplateName::Template:
5807 if (TemplateDecl *ToTemplate
5808 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
5809 return TemplateName(ToTemplate);
5811 return TemplateName();
5813 case TemplateName::OverloadedTemplate: {
5814 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
5815 UnresolvedSet<2> ToTemplates;
5816 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
5817 E = FromStorage->end();
5819 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
5820 ToTemplates.addDecl(To);
5822 return TemplateName();
5824 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
5828 case TemplateName::QualifiedTemplate: {
5829 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
5830 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
5832 return TemplateName();
5834 if (TemplateDecl *ToTemplate
5835 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
5836 return ToContext.getQualifiedTemplateName(Qualifier,
5837 QTN->hasTemplateKeyword(),
5840 return TemplateName();
5843 case TemplateName::DependentTemplate: {
5844 DependentTemplateName *DTN = From.getAsDependentTemplateName();
5845 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
5847 return TemplateName();
5849 if (DTN->isIdentifier()) {
5850 return ToContext.getDependentTemplateName(Qualifier,
5851 Import(DTN->getIdentifier()));
5854 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
5857 case TemplateName::SubstTemplateTemplateParm: {
5858 SubstTemplateTemplateParmStorage *subst
5859 = From.getAsSubstTemplateTemplateParm();
5860 TemplateTemplateParmDecl *param
5861 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
5863 return TemplateName();
5865 TemplateName replacement = Import(subst->getReplacement());
5866 if (replacement.isNull()) return TemplateName();
5868 return ToContext.getSubstTemplateTemplateParm(param, replacement);
5871 case TemplateName::SubstTemplateTemplateParmPack: {
5872 SubstTemplateTemplateParmPackStorage *SubstPack
5873 = From.getAsSubstTemplateTemplateParmPack();
5874 TemplateTemplateParmDecl *Param
5875 = cast_or_null<TemplateTemplateParmDecl>(
5876 Import(SubstPack->getParameterPack()));
5878 return TemplateName();
5880 ASTNodeImporter Importer(*this);
5881 TemplateArgument ArgPack
5882 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
5883 if (ArgPack.isNull())
5884 return TemplateName();
5886 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
5890 llvm_unreachable("Invalid template name kind");
5893 SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
5894 if (FromLoc.isInvalid())
5895 return SourceLocation();
5897 SourceManager &FromSM = FromContext.getSourceManager();
5899 // For now, map everything down to its file location, so that we
5900 // don't have to import macro expansions.
5901 // FIXME: Import macro expansions!
5902 FromLoc = FromSM.getFileLoc(FromLoc);
5903 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
5904 SourceManager &ToSM = ToContext.getSourceManager();
5905 FileID ToFileID = Import(Decomposed.first);
5906 if (ToFileID.isInvalid())
5907 return SourceLocation();
5908 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
5909 .getLocWithOffset(Decomposed.second);
5913 SourceRange ASTImporter::Import(SourceRange FromRange) {
5914 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
5917 FileID ASTImporter::Import(FileID FromID) {
5918 llvm::DenseMap<FileID, FileID>::iterator Pos
5919 = ImportedFileIDs.find(FromID);
5920 if (Pos != ImportedFileIDs.end())
5923 SourceManager &FromSM = FromContext.getSourceManager();
5924 SourceManager &ToSM = ToContext.getSourceManager();
5925 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
5926 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
5928 // Include location of this file.
5929 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
5931 // Map the FileID for to the "to" source manager.
5933 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
5934 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
5935 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
5937 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
5938 // than mmap the files several times.
5939 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
5942 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
5943 FromSLoc.getFile().getFileCharacteristic());
5945 // FIXME: We want to re-use the existing MemoryBuffer!
5946 const llvm::MemoryBuffer *
5947 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
5948 std::unique_ptr<llvm::MemoryBuffer> ToBuf
5949 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
5950 FromBuf->getBufferIdentifier());
5951 ToID = ToSM.createFileID(std::move(ToBuf),
5952 FromSLoc.getFile().getFileCharacteristic());
5956 ImportedFileIDs[FromID] = ToID;
5960 CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
5961 Expr *ToExpr = Import(From->getInit());
5962 if (!ToExpr && From->getInit())
5965 if (From->isBaseInitializer()) {
5966 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
5967 if (!ToTInfo && From->getTypeSourceInfo())
5970 return new (ToContext) CXXCtorInitializer(
5971 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
5972 ToExpr, Import(From->getRParenLoc()),
5973 From->isPackExpansion() ? Import(From->getEllipsisLoc())
5974 : SourceLocation());
5975 } else if (From->isMemberInitializer()) {
5976 FieldDecl *ToField =
5977 llvm::cast_or_null<FieldDecl>(Import(From->getMember()));
5978 if (!ToField && From->getMember())
5981 return new (ToContext) CXXCtorInitializer(
5982 ToContext, ToField, Import(From->getMemberLocation()),
5983 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
5984 } else if (From->isIndirectMemberInitializer()) {
5985 IndirectFieldDecl *ToIField = llvm::cast_or_null<IndirectFieldDecl>(
5986 Import(From->getIndirectMember()));
5987 if (!ToIField && From->getIndirectMember())
5990 return new (ToContext) CXXCtorInitializer(
5991 ToContext, ToIField, Import(From->getMemberLocation()),
5992 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
5993 } else if (From->isDelegatingInitializer()) {
5994 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
5995 if (!ToTInfo && From->getTypeSourceInfo())
5998 return new (ToContext)
5999 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
6000 ToExpr, Import(From->getRParenLoc()));
6007 CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
6008 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
6009 if (Pos != ImportedCXXBaseSpecifiers.end())
6012 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
6013 Import(BaseSpec->getSourceRange()),
6014 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
6015 BaseSpec->getAccessSpecifierAsWritten(),
6016 Import(BaseSpec->getTypeSourceInfo()),
6017 Import(BaseSpec->getEllipsisLoc()));
6018 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
6022 void ASTImporter::ImportDefinition(Decl *From) {
6023 Decl *To = Import(From);
6027 if (DeclContext *FromDC = cast<DeclContext>(From)) {
6028 ASTNodeImporter Importer(*this);
6030 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
6031 if (!ToRecord->getDefinition()) {
6032 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
6033 ASTNodeImporter::IDK_Everything);
6038 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
6039 if (!ToEnum->getDefinition()) {
6040 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
6041 ASTNodeImporter::IDK_Everything);
6046 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
6047 if (!ToIFace->getDefinition()) {
6048 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
6049 ASTNodeImporter::IDK_Everything);
6054 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
6055 if (!ToProto->getDefinition()) {
6056 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
6057 ASTNodeImporter::IDK_Everything);
6062 Importer.ImportDeclContext(FromDC, true);
6066 DeclarationName ASTImporter::Import(DeclarationName FromName) {
6068 return DeclarationName();
6070 switch (FromName.getNameKind()) {
6071 case DeclarationName::Identifier:
6072 return Import(FromName.getAsIdentifierInfo());
6074 case DeclarationName::ObjCZeroArgSelector:
6075 case DeclarationName::ObjCOneArgSelector:
6076 case DeclarationName::ObjCMultiArgSelector:
6077 return Import(FromName.getObjCSelector());
6079 case DeclarationName::CXXConstructorName: {
6080 QualType T = Import(FromName.getCXXNameType());
6082 return DeclarationName();
6084 return ToContext.DeclarationNames.getCXXConstructorName(
6085 ToContext.getCanonicalType(T));
6088 case DeclarationName::CXXDestructorName: {
6089 QualType T = Import(FromName.getCXXNameType());
6091 return DeclarationName();
6093 return ToContext.DeclarationNames.getCXXDestructorName(
6094 ToContext.getCanonicalType(T));
6097 case DeclarationName::CXXDeductionGuideName: {
6098 TemplateDecl *Template = cast_or_null<TemplateDecl>(
6099 Import(FromName.getCXXDeductionGuideTemplate()));
6101 return DeclarationName();
6102 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
6105 case DeclarationName::CXXConversionFunctionName: {
6106 QualType T = Import(FromName.getCXXNameType());
6108 return DeclarationName();
6110 return ToContext.DeclarationNames.getCXXConversionFunctionName(
6111 ToContext.getCanonicalType(T));
6114 case DeclarationName::CXXOperatorName:
6115 return ToContext.DeclarationNames.getCXXOperatorName(
6116 FromName.getCXXOverloadedOperator());
6118 case DeclarationName::CXXLiteralOperatorName:
6119 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
6120 Import(FromName.getCXXLiteralIdentifier()));
6122 case DeclarationName::CXXUsingDirective:
6124 return DeclarationName::getUsingDirectiveName();
6127 llvm_unreachable("Invalid DeclarationName Kind!");
6130 IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
6134 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
6136 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
6137 ToId->setBuiltinID(FromId->getBuiltinID());
6142 Selector ASTImporter::Import(Selector FromSel) {
6143 if (FromSel.isNull())
6146 SmallVector<IdentifierInfo *, 4> Idents;
6147 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
6148 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
6149 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
6150 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
6153 DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
6157 unsigned NumDecls) {
6161 DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
6162 if (LastDiagFromFrom)
6163 ToContext.getDiagnostics().notePriorDiagnosticFrom(
6164 FromContext.getDiagnostics());
6165 LastDiagFromFrom = false;
6166 return ToContext.getDiagnostics().Report(Loc, DiagID);
6169 DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
6170 if (!LastDiagFromFrom)
6171 FromContext.getDiagnostics().notePriorDiagnosticFrom(
6172 ToContext.getDiagnostics());
6173 LastDiagFromFrom = true;
6174 return FromContext.getDiagnostics().Report(Loc, DiagID);
6177 void ASTImporter::CompleteDecl (Decl *D) {
6178 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
6179 if (!ID->getDefinition())
6180 ID->startDefinition();
6182 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
6183 if (!PD->getDefinition())
6184 PD->startDefinition();
6186 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
6187 if (!TD->getDefinition() && !TD->isBeingDefined()) {
6188 TD->startDefinition();
6189 TD->setCompleteDefinition(true);
6193 assert (0 && "CompleteDecl called on a Decl that can't be completed");
6197 Decl *ASTImporter::Imported(Decl *From, Decl *To) {
6198 if (From->hasAttrs()) {
6199 for (Attr *FromAttr : From->getAttrs())
6200 To->addAttr(FromAttr->clone(To->getASTContext()));
6202 if (From->isUsed()) {
6205 if (From->isImplicit()) {
6208 ImportedDecls[From] = To;
6212 bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
6214 llvm::DenseMap<const Type *, const Type *>::iterator Pos
6215 = ImportedTypes.find(From.getTypePtr());
6216 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
6219 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
6221 return Ctx.IsStructurallyEquivalent(From, To);