Bug Summary

File:tools/clang/lib/AST/ASTImporter.cpp
Warning:line 3935, column 12
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name ASTImporter.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -relaxed-aliasing -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/clang/lib/AST -I /build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/AST -I /build/llvm-toolchain-snapshot-7~svn329677/tools/clang/include -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn329677/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/clang/lib/AST -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fno-common -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-04-11-031539-24776-1 -x c++ /build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/AST/ASTImporter.cpp

/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/AST/ASTImporter.cpp

1//===- ASTImporter.cpp - Importing ASTs from other Contexts ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ASTImporter class which imports AST nodes from one
11// context into another context.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/ASTImporter.h"
16#include "clang/AST/ASTContext.h"
17#include "clang/AST/ASTDiagnostic.h"
18#include "clang/AST/ASTStructuralEquivalence.h"
19#include "clang/AST/Attr.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclAccessPair.h"
22#include "clang/AST/DeclBase.h"
23#include "clang/AST/DeclCXX.h"
24#include "clang/AST/DeclFriend.h"
25#include "clang/AST/DeclGroup.h"
26#include "clang/AST/DeclObjC.h"
27#include "clang/AST/DeclTemplate.h"
28#include "clang/AST/DeclVisitor.h"
29#include "clang/AST/DeclarationName.h"
30#include "clang/AST/Expr.h"
31#include "clang/AST/ExprCXX.h"
32#include "clang/AST/ExprObjC.h"
33#include "clang/AST/ExternalASTSource.h"
34#include "clang/AST/LambdaCapture.h"
35#include "clang/AST/NestedNameSpecifier.h"
36#include "clang/AST/OperationKinds.h"
37#include "clang/AST/Stmt.h"
38#include "clang/AST/StmtCXX.h"
39#include "clang/AST/StmtObjC.h"
40#include "clang/AST/StmtVisitor.h"
41#include "clang/AST/TemplateBase.h"
42#include "clang/AST/TemplateName.h"
43#include "clang/AST/Type.h"
44#include "clang/AST/TypeLoc.h"
45#include "clang/AST/TypeVisitor.h"
46#include "clang/AST/UnresolvedSet.h"
47#include "clang/Basic/ExceptionSpecificationType.h"
48#include "clang/Basic/FileManager.h"
49#include "clang/Basic/IdentifierTable.h"
50#include "clang/Basic/LLVM.h"
51#include "clang/Basic/LangOptions.h"
52#include "clang/Basic/SourceLocation.h"
53#include "clang/Basic/SourceManager.h"
54#include "clang/Basic/Specifiers.h"
55#include "llvm/ADT/APSInt.h"
56#include "llvm/ADT/ArrayRef.h"
57#include "llvm/ADT/DenseMap.h"
58#include "llvm/ADT/None.h"
59#include "llvm/ADT/Optional.h"
60#include "llvm/ADT/STLExtras.h"
61#include "llvm/ADT/SmallVector.h"
62#include "llvm/Support/Casting.h"
63#include "llvm/Support/ErrorHandling.h"
64#include "llvm/Support/MemoryBuffer.h"
65#include <algorithm>
66#include <cassert>
67#include <cstddef>
68#include <memory>
69#include <type_traits>
70#include <utility>
71
72namespace clang {
73
74 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
75 public DeclVisitor<ASTNodeImporter, Decl *>,
76 public StmtVisitor<ASTNodeImporter, Stmt *> {
77 ASTImporter &Importer;
78
79 public:
80 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) {}
81
82 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
83 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
84 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
85
86 // Importing types
87 QualType VisitType(const Type *T);
88 QualType VisitAtomicType(const AtomicType *T);
89 QualType VisitBuiltinType(const BuiltinType *T);
90 QualType VisitDecayedType(const DecayedType *T);
91 QualType VisitComplexType(const ComplexType *T);
92 QualType VisitPointerType(const PointerType *T);
93 QualType VisitBlockPointerType(const BlockPointerType *T);
94 QualType VisitLValueReferenceType(const LValueReferenceType *T);
95 QualType VisitRValueReferenceType(const RValueReferenceType *T);
96 QualType VisitMemberPointerType(const MemberPointerType *T);
97 QualType VisitConstantArrayType(const ConstantArrayType *T);
98 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
99 QualType VisitVariableArrayType(const VariableArrayType *T);
100 QualType VisitDependentSizedArrayType(const DependentSizedArrayType *T);
101 // FIXME: DependentSizedExtVectorType
102 QualType VisitVectorType(const VectorType *T);
103 QualType VisitExtVectorType(const ExtVectorType *T);
104 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
105 QualType VisitFunctionProtoType(const FunctionProtoType *T);
106 QualType VisitUnresolvedUsingType(const UnresolvedUsingType *T);
107 QualType VisitParenType(const ParenType *T);
108 QualType VisitTypedefType(const TypedefType *T);
109 QualType VisitTypeOfExprType(const TypeOfExprType *T);
110 // FIXME: DependentTypeOfExprType
111 QualType VisitTypeOfType(const TypeOfType *T);
112 QualType VisitDecltypeType(const DecltypeType *T);
113 QualType VisitUnaryTransformType(const UnaryTransformType *T);
114 QualType VisitAutoType(const AutoType *T);
115 QualType VisitInjectedClassNameType(const InjectedClassNameType *T);
116 // FIXME: DependentDecltypeType
117 QualType VisitRecordType(const RecordType *T);
118 QualType VisitEnumType(const EnumType *T);
119 QualType VisitAttributedType(const AttributedType *T);
120 QualType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
121 QualType VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T);
122 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
123 QualType VisitElaboratedType(const ElaboratedType *T);
124 // FIXME: DependentNameType
125 QualType VisitPackExpansionType(const PackExpansionType *T);
126 QualType VisitDependentTemplateSpecializationType(
127 const DependentTemplateSpecializationType *T);
128 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
129 QualType VisitObjCObjectType(const ObjCObjectType *T);
130 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
131
132 // Importing declarations
133 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
134 DeclContext *&LexicalDC, DeclarationName &Name,
135 NamedDecl *&ToD, SourceLocation &Loc);
136 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
137 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
138 DeclarationNameInfo& To);
139 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
140
141 bool ImportCastPath(CastExpr *E, CXXCastPath &Path);
142
143 using Designator = DesignatedInitExpr::Designator;
144
145 Designator ImportDesignator(const Designator &D);
146
147 Optional<LambdaCapture> ImportLambdaCapture(const LambdaCapture &From);
148
149 /// \brief What we should import from the definition.
150 enum ImportDefinitionKind {
151 /// \brief Import the default subset of the definition, which might be
152 /// nothing (if minimal import is set) or might be everything (if minimal
153 /// import is not set).
154 IDK_Default,
155
156 /// \brief Import everything.
157 IDK_Everything,
158
159 /// \brief Import only the bare bones needed to establish a valid
160 /// DeclContext.
161 IDK_Basic
162 };
163
164 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
165 return IDK == IDK_Everything ||
166 (IDK == IDK_Default && !Importer.isMinimalImport());
167 }
168
169 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
170 ImportDefinitionKind Kind = IDK_Default);
171 bool ImportDefinition(VarDecl *From, VarDecl *To,
172 ImportDefinitionKind Kind = IDK_Default);
173 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
174 ImportDefinitionKind Kind = IDK_Default);
175 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
176 ImportDefinitionKind Kind = IDK_Default);
177 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
178 ImportDefinitionKind Kind = IDK_Default);
179 TemplateParameterList *ImportTemplateParameterList(
180 TemplateParameterList *Params);
181 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
182 Optional<TemplateArgumentLoc> ImportTemplateArgumentLoc(
183 const TemplateArgumentLoc &TALoc);
184 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
185 unsigned NumFromArgs,
186 SmallVectorImpl<TemplateArgument> &ToArgs);
187
188 template <typename InContainerTy>
189 bool ImportTemplateArgumentListInfo(const InContainerTy &Container,
190 TemplateArgumentListInfo &ToTAInfo);
191
192 template<typename InContainerTy>
193 bool ImportTemplateArgumentListInfo(SourceLocation FromLAngleLoc,
194 SourceLocation FromRAngleLoc,
195 const InContainerTy &Container,
196 TemplateArgumentListInfo &Result);
197
198 bool ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD);
199
200 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
201 bool Complain = true);
202 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
203 bool Complain = true);
204 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
205 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
206 bool IsStructuralMatch(FunctionTemplateDecl *From,
207 FunctionTemplateDecl *To);
208 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
209 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
210 Decl *VisitDecl(Decl *D);
211 Decl *VisitEmptyDecl(EmptyDecl *D);
212 Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
213 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
214 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
215 Decl *VisitNamespaceDecl(NamespaceDecl *D);
216 Decl *VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
217 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
218 Decl *VisitTypedefDecl(TypedefDecl *D);
219 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
220 Decl *VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
221 Decl *VisitLabelDecl(LabelDecl *D);
222 Decl *VisitEnumDecl(EnumDecl *D);
223 Decl *VisitRecordDecl(RecordDecl *D);
224 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
225 Decl *VisitFunctionDecl(FunctionDecl *D);
226 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
227 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
228 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
229 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
230 Decl *VisitFieldDecl(FieldDecl *D);
231 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
232 Decl *VisitFriendDecl(FriendDecl *D);
233 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
234 Decl *VisitVarDecl(VarDecl *D);
235 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
236 Decl *VisitParmVarDecl(ParmVarDecl *D);
237 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
238 Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
239 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
240 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
241 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D);
242 Decl *VisitUsingDecl(UsingDecl *D);
243 Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
244 Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
245 Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
246 Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
247
248 ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list);
249 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
250 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
251 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
252 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
253 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
254 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
255 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
256 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
257 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
258 Decl *VisitClassTemplateSpecializationDecl(
259 ClassTemplateSpecializationDecl *D);
260 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
261 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
262 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
263
264 // Importing statements
265 DeclGroupRef ImportDeclGroup(DeclGroupRef DG);
266
267 Stmt *VisitStmt(Stmt *S);
268 Stmt *VisitGCCAsmStmt(GCCAsmStmt *S);
269 Stmt *VisitDeclStmt(DeclStmt *S);
270 Stmt *VisitNullStmt(NullStmt *S);
271 Stmt *VisitCompoundStmt(CompoundStmt *S);
272 Stmt *VisitCaseStmt(CaseStmt *S);
273 Stmt *VisitDefaultStmt(DefaultStmt *S);
274 Stmt *VisitLabelStmt(LabelStmt *S);
275 Stmt *VisitAttributedStmt(AttributedStmt *S);
276 Stmt *VisitIfStmt(IfStmt *S);
277 Stmt *VisitSwitchStmt(SwitchStmt *S);
278 Stmt *VisitWhileStmt(WhileStmt *S);
279 Stmt *VisitDoStmt(DoStmt *S);
280 Stmt *VisitForStmt(ForStmt *S);
281 Stmt *VisitGotoStmt(GotoStmt *S);
282 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S);
283 Stmt *VisitContinueStmt(ContinueStmt *S);
284 Stmt *VisitBreakStmt(BreakStmt *S);
285 Stmt *VisitReturnStmt(ReturnStmt *S);
286 // FIXME: MSAsmStmt
287 // FIXME: SEHExceptStmt
288 // FIXME: SEHFinallyStmt
289 // FIXME: SEHTryStmt
290 // FIXME: SEHLeaveStmt
291 // FIXME: CapturedStmt
292 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S);
293 Stmt *VisitCXXTryStmt(CXXTryStmt *S);
294 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S);
295 // FIXME: MSDependentExistsStmt
296 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
297 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
298 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
299 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
300 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
301 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
302 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
303
304 // Importing expressions
305 Expr *VisitExpr(Expr *E);
306 Expr *VisitVAArgExpr(VAArgExpr *E);
307 Expr *VisitGNUNullExpr(GNUNullExpr *E);
308 Expr *VisitPredefinedExpr(PredefinedExpr *E);
309 Expr *VisitDeclRefExpr(DeclRefExpr *E);
310 Expr *VisitImplicitValueInitExpr(ImplicitValueInitExpr *ILE);
311 Expr *VisitDesignatedInitExpr(DesignatedInitExpr *E);
312 Expr *VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
313 Expr *VisitIntegerLiteral(IntegerLiteral *E);
314 Expr *VisitFloatingLiteral(FloatingLiteral *E);
315 Expr *VisitCharacterLiteral(CharacterLiteral *E);
316 Expr *VisitStringLiteral(StringLiteral *E);
317 Expr *VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
318 Expr *VisitAtomicExpr(AtomicExpr *E);
319 Expr *VisitAddrLabelExpr(AddrLabelExpr *E);
320 Expr *VisitParenExpr(ParenExpr *E);
321 Expr *VisitParenListExpr(ParenListExpr *E);
322 Expr *VisitStmtExpr(StmtExpr *E);
323 Expr *VisitUnaryOperator(UnaryOperator *E);
324 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
325 Expr *VisitBinaryOperator(BinaryOperator *E);
326 Expr *VisitConditionalOperator(ConditionalOperator *E);
327 Expr *VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
328 Expr *VisitOpaqueValueExpr(OpaqueValueExpr *E);
329 Expr *VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
330 Expr *VisitExpressionTraitExpr(ExpressionTraitExpr *E);
331 Expr *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
332 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
333 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
334 Expr *VisitExplicitCastExpr(ExplicitCastExpr *E);
335 Expr *VisitOffsetOfExpr(OffsetOfExpr *OE);
336 Expr *VisitCXXThrowExpr(CXXThrowExpr *E);
337 Expr *VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
338 Expr *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
339 Expr *VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
340 Expr *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
341 Expr *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE);
342 Expr *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
343 Expr *VisitPackExpansionExpr(PackExpansionExpr *E);
344 Expr *VisitSizeOfPackExpr(SizeOfPackExpr *E);
345 Expr *VisitCXXNewExpr(CXXNewExpr *CE);
346 Expr *VisitCXXDeleteExpr(CXXDeleteExpr *E);
347 Expr *VisitCXXConstructExpr(CXXConstructExpr *E);
348 Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
349 Expr *VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
350 Expr *VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *CE);
351 Expr *VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E);
352 Expr *VisitExprWithCleanups(ExprWithCleanups *EWC);
353 Expr *VisitCXXThisExpr(CXXThisExpr *E);
354 Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
355 Expr *VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
356 Expr *VisitMemberExpr(MemberExpr *E);
357 Expr *VisitCallExpr(CallExpr *E);
358 Expr *VisitLambdaExpr(LambdaExpr *LE);
359 Expr *VisitInitListExpr(InitListExpr *E);
360 Expr *VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
361 Expr *VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
362 Expr *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
363 Expr *VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
364 Expr *VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
365 Expr *VisitTypeTraitExpr(TypeTraitExpr *E);
366 Expr *VisitCXXTypeidExpr(CXXTypeidExpr *E);
367
368 template<typename IIter, typename OIter>
369 void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) {
370 using ItemT = typename std::remove_reference<decltype(*Obegin)>::type;
371
372 ASTImporter &ImporterRef = Importer;
373 std::transform(Ibegin, Iend, Obegin,
374 [&ImporterRef](ItemT From) -> ItemT {
375 return ImporterRef.Import(From);
376 });
377 }
378
379 template<typename IIter, typename OIter>
380 bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
381 using ItemT = typename std::remove_reference<decltype(**Obegin)>::type;
382
383 ASTImporter &ImporterRef = Importer;
384 bool Failed = false;
385 std::transform(Ibegin, Iend, Obegin,
386 [&ImporterRef, &Failed](ItemT *From) -> ItemT * {
387 auto *To = cast_or_null<ItemT>(ImporterRef.Import(From));
388 if (!To && From)
389 Failed = true;
390 return To;
391 });
392 return Failed;
393 }
394
395 template<typename InContainerTy, typename OutContainerTy>
396 bool ImportContainerChecked(const InContainerTy &InContainer,
397 OutContainerTy &OutContainer) {
398 return ImportArrayChecked(InContainer.begin(), InContainer.end(),
399 OutContainer.begin());
400 }
401
402 template<typename InContainerTy, typename OIter>
403 bool ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
404 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
405 }
406
407 // Importing overrides.
408 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
409 };
410
411template <typename InContainerTy>
412bool ASTNodeImporter::ImportTemplateArgumentListInfo(
413 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
414 const InContainerTy &Container, TemplateArgumentListInfo &Result) {
415 TemplateArgumentListInfo ToTAInfo(Importer.Import(FromLAngleLoc),
416 Importer.Import(FromRAngleLoc));
417 if (ImportTemplateArgumentListInfo(Container, ToTAInfo))
418 return true;
419 Result = ToTAInfo;
420 return false;
421}
422
423template <>
424bool ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>(
425 const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) {
426 return ImportTemplateArgumentListInfo(
427 From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result);
428}
429
430template <>
431bool ASTNodeImporter::ImportTemplateArgumentListInfo<
432 ASTTemplateArgumentListInfo>(const ASTTemplateArgumentListInfo &From,
433 TemplateArgumentListInfo &Result) {
434 return ImportTemplateArgumentListInfo(From.LAngleLoc, From.RAngleLoc,
435 From.arguments(), Result);
436}
437
438} // namespace clang
439
440//----------------------------------------------------------------------------
441// Import Types
442//----------------------------------------------------------------------------
443
444using namespace clang;
445
446QualType ASTNodeImporter::VisitType(const Type *T) {
447 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
448 << T->getTypeClassName();
449 return {};
450}
451
452QualType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
453 QualType UnderlyingType = Importer.Import(T->getValueType());
454 if(UnderlyingType.isNull())
455 return {};
456
457 return Importer.getToContext().getAtomicType(UnderlyingType);
458}
459
460QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
461 switch (T->getKind()) {
462#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
463 case BuiltinType::Id: \
464 return Importer.getToContext().SingletonId;
465#include "clang/Basic/OpenCLImageTypes.def"
466#define SHARED_SINGLETON_TYPE(Expansion)
467#define BUILTIN_TYPE(Id, SingletonId) \
468 case BuiltinType::Id: return Importer.getToContext().SingletonId;
469#include "clang/AST/BuiltinTypes.def"
470
471 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
472 // context supports C++.
473
474 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
475 // context supports ObjC.
476
477 case BuiltinType::Char_U:
478 // The context we're importing from has an unsigned 'char'. If we're
479 // importing into a context with a signed 'char', translate to
480 // 'unsigned char' instead.
481 if (Importer.getToContext().getLangOpts().CharIsSigned)
482 return Importer.getToContext().UnsignedCharTy;
483
484 return Importer.getToContext().CharTy;
485
486 case BuiltinType::Char_S:
487 // The context we're importing from has an unsigned 'char'. If we're
488 // importing into a context with a signed 'char', translate to
489 // 'unsigned char' instead.
490 if (!Importer.getToContext().getLangOpts().CharIsSigned)
491 return Importer.getToContext().SignedCharTy;
492
493 return Importer.getToContext().CharTy;
494
495 case BuiltinType::WChar_S:
496 case BuiltinType::WChar_U:
497 // FIXME: If not in C++, shall we translate to the C equivalent of
498 // wchar_t?
499 return Importer.getToContext().WCharTy;
500 }
501
502 llvm_unreachable("Invalid BuiltinType Kind!")::llvm::llvm_unreachable_internal("Invalid BuiltinType Kind!"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/AST/ASTImporter.cpp"
, 502)
;
503}
504
505QualType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
506 QualType OrigT = Importer.Import(T->getOriginalType());
507 if (OrigT.isNull())
508 return {};
509
510 return Importer.getToContext().getDecayedType(OrigT);
511}
512
513QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
514 QualType ToElementType = Importer.Import(T->getElementType());
515 if (ToElementType.isNull())
516 return {};
517
518 return Importer.getToContext().getComplexType(ToElementType);
519}
520
521QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
522 QualType ToPointeeType = Importer.Import(T->getPointeeType());
523 if (ToPointeeType.isNull())
524 return {};
525
526 return Importer.getToContext().getPointerType(ToPointeeType);
527}
528
529QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
530 // FIXME: Check for blocks support in "to" context.
531 QualType ToPointeeType = Importer.Import(T->getPointeeType());
532 if (ToPointeeType.isNull())
533 return {};
534
535 return Importer.getToContext().getBlockPointerType(ToPointeeType);
536}
537
538QualType
539ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
540 // FIXME: Check for C++ support in "to" context.
541 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
542 if (ToPointeeType.isNull())
543 return {};
544
545 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
546}
547
548QualType
549ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
550 // FIXME: Check for C++0x support in "to" context.
551 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
552 if (ToPointeeType.isNull())
553 return {};
554
555 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
556}
557
558QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
559 // FIXME: Check for C++ support in "to" context.
560 QualType ToPointeeType = Importer.Import(T->getPointeeType());
561 if (ToPointeeType.isNull())
562 return {};
563
564 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
565 return Importer.getToContext().getMemberPointerType(ToPointeeType,
566 ClassType.getTypePtr());
567}
568
569QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
570 QualType ToElementType = Importer.Import(T->getElementType());
571 if (ToElementType.isNull())
572 return {};
573
574 return Importer.getToContext().getConstantArrayType(ToElementType,
575 T->getSize(),
576 T->getSizeModifier(),
577 T->getIndexTypeCVRQualifiers());
578}
579
580QualType
581ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
582 QualType ToElementType = Importer.Import(T->getElementType());
583 if (ToElementType.isNull())
584 return {};
585
586 return Importer.getToContext().getIncompleteArrayType(ToElementType,
587 T->getSizeModifier(),
588 T->getIndexTypeCVRQualifiers());
589}
590
591QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
592 QualType ToElementType = Importer.Import(T->getElementType());
593 if (ToElementType.isNull())
594 return {};
595
596 Expr *Size = Importer.Import(T->getSizeExpr());
597 if (!Size)
598 return {};
599
600 SourceRange Brackets = Importer.Import(T->getBracketsRange());
601 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
602 T->getSizeModifier(),
603 T->getIndexTypeCVRQualifiers(),
604 Brackets);
605}
606
607QualType ASTNodeImporter::VisitDependentSizedArrayType(
608 const DependentSizedArrayType *T) {
609 QualType ToElementType = Importer.Import(T->getElementType());
610 if (ToElementType.isNull())
611 return {};
612
613 // SizeExpr may be null if size is not specified directly.
614 // For example, 'int a[]'.
615 Expr *Size = Importer.Import(T->getSizeExpr());
616 if (!Size && T->getSizeExpr())
617 return {};
618
619 SourceRange Brackets = Importer.Import(T->getBracketsRange());
620 return Importer.getToContext().getDependentSizedArrayType(
621 ToElementType, Size, T->getSizeModifier(), T->getIndexTypeCVRQualifiers(),
622 Brackets);
623}
624
625QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
626 QualType ToElementType = Importer.Import(T->getElementType());
627 if (ToElementType.isNull())
628 return {};
629
630 return Importer.getToContext().getVectorType(ToElementType,
631 T->getNumElements(),
632 T->getVectorKind());
633}
634
635QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
636 QualType ToElementType = Importer.Import(T->getElementType());
637 if (ToElementType.isNull())
638 return {};
639
640 return Importer.getToContext().getExtVectorType(ToElementType,
641 T->getNumElements());
642}
643
644QualType
645ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
646 // FIXME: What happens if we're importing a function without a prototype
647 // into C++? Should we make it variadic?
648 QualType ToResultType = Importer.Import(T->getReturnType());
649 if (ToResultType.isNull())
650 return {};
651
652 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
653 T->getExtInfo());
654}
655
656QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
657 QualType ToResultType = Importer.Import(T->getReturnType());
658 if (ToResultType.isNull())
659 return {};
660
661 // Import argument types
662 SmallVector<QualType, 4> ArgTypes;
663 for (const auto &A : T->param_types()) {
664 QualType ArgType = Importer.Import(A);
665 if (ArgType.isNull())
666 return {};
667 ArgTypes.push_back(ArgType);
668 }
669
670 // Import exception types
671 SmallVector<QualType, 4> ExceptionTypes;
672 for (const auto &E : T->exceptions()) {
673 QualType ExceptionType = Importer.Import(E);
674 if (ExceptionType.isNull())
675 return {};
676 ExceptionTypes.push_back(ExceptionType);
677 }
678
679 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
680 FunctionProtoType::ExtProtoInfo ToEPI;
681
682 ToEPI.ExtInfo = FromEPI.ExtInfo;
683 ToEPI.Variadic = FromEPI.Variadic;
684 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
685 ToEPI.TypeQuals = FromEPI.TypeQuals;
686 ToEPI.RefQualifier = FromEPI.RefQualifier;
687 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
688 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
689 ToEPI.ExceptionSpec.NoexceptExpr =
690 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
691 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
692 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
693 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
694 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
695
696 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
697}
698
699QualType ASTNodeImporter::VisitUnresolvedUsingType(
700 const UnresolvedUsingType *T) {
701 const auto *ToD =
702 cast_or_null<UnresolvedUsingTypenameDecl>(Importer.Import(T->getDecl()));
703 if (!ToD)
704 return {};
705
706 auto *ToPrevD =
707 cast_or_null<UnresolvedUsingTypenameDecl>(
708 Importer.Import(T->getDecl()->getPreviousDecl()));
709 if (!ToPrevD && T->getDecl()->getPreviousDecl())
710 return {};
711
712 return Importer.getToContext().getTypeDeclType(ToD, ToPrevD);
713}
714
715QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
716 QualType ToInnerType = Importer.Import(T->getInnerType());
717 if (ToInnerType.isNull())
718 return {};
719
720 return Importer.getToContext().getParenType(ToInnerType);
721}
722
723QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
724 auto *ToDecl =
725 dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
726 if (!ToDecl)
727 return {};
728
729 return Importer.getToContext().getTypeDeclType(ToDecl);
730}
731
732QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
733 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
734 if (!ToExpr)
735 return {};
736
737 return Importer.getToContext().getTypeOfExprType(ToExpr);
738}
739
740QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
741 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
742 if (ToUnderlyingType.isNull())
743 return {};
744
745 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
746}
747
748QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
749 // FIXME: Make sure that the "to" context supports C++0x!
750 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
751 if (!ToExpr)
752 return {};
753
754 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
755 if (UnderlyingType.isNull())
756 return {};
757
758 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
759}
760
761QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
762 QualType ToBaseType = Importer.Import(T->getBaseType());
763 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
764 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
765 return {};
766
767 return Importer.getToContext().getUnaryTransformType(ToBaseType,
768 ToUnderlyingType,
769 T->getUTTKind());
770}
771
772QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
773 // FIXME: Make sure that the "to" context supports C++11!
774 QualType FromDeduced = T->getDeducedType();
775 QualType ToDeduced;
776 if (!FromDeduced.isNull()) {
777 ToDeduced = Importer.Import(FromDeduced);
778 if (ToDeduced.isNull())
779 return {};
780 }
781
782 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
783 /*IsDependent*/false);
784}
785
786QualType ASTNodeImporter::VisitInjectedClassNameType(
787 const InjectedClassNameType *T) {
788 auto *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl()));
789 if (!D)
790 return {};
791
792 QualType InjType = Importer.Import(T->getInjectedSpecializationType());
793 if (InjType.isNull())
794 return {};
795
796 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
797 // See comments in InjectedClassNameType definition for details
798 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
799 enum {
800 TypeAlignmentInBits = 4,
801 TypeAlignment = 1 << TypeAlignmentInBits
802 };
803
804 return QualType(new (Importer.getToContext(), TypeAlignment)
805 InjectedClassNameType(D, InjType), 0);
806}
807
808QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
809 auto *ToDecl = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
810 if (!ToDecl)
811 return {};
812
813 return Importer.getToContext().getTagDeclType(ToDecl);
814}
815
816QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
817 auto *ToDecl = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
818 if (!ToDecl)
819 return {};
820
821 return Importer.getToContext().getTagDeclType(ToDecl);
822}
823
824QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
825 QualType FromModifiedType = T->getModifiedType();
826 QualType FromEquivalentType = T->getEquivalentType();
827 QualType ToModifiedType;
828 QualType ToEquivalentType;
829
830 if (!FromModifiedType.isNull()) {
831 ToModifiedType = Importer.Import(FromModifiedType);
832 if (ToModifiedType.isNull())
833 return {};
834 }
835 if (!FromEquivalentType.isNull()) {
836 ToEquivalentType = Importer.Import(FromEquivalentType);
837 if (ToEquivalentType.isNull())
838 return {};
839 }
840
841 return Importer.getToContext().getAttributedType(T->getAttrKind(),
842 ToModifiedType, ToEquivalentType);
843}
844
845QualType ASTNodeImporter::VisitTemplateTypeParmType(
846 const TemplateTypeParmType *T) {
847 auto *ParmDecl =
848 cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl()));
849 if (!ParmDecl && T->getDecl())
850 return {};
851
852 return Importer.getToContext().getTemplateTypeParmType(
853 T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl);
854}
855
856QualType ASTNodeImporter::VisitSubstTemplateTypeParmType(
857 const SubstTemplateTypeParmType *T) {
858 const auto *Replaced =
859 cast_or_null<TemplateTypeParmType>(Importer.Import(
860 QualType(T->getReplacedParameter(), 0)).getTypePtr());
861 if (!Replaced)
862 return {};
863
864 QualType Replacement = Importer.Import(T->getReplacementType());
865 if (Replacement.isNull())
866 return {};
867 Replacement = Replacement.getCanonicalType();
868
869 return Importer.getToContext().getSubstTemplateTypeParmType(
870 Replaced, Replacement);
871}
872
873QualType ASTNodeImporter::VisitTemplateSpecializationType(
874 const TemplateSpecializationType *T) {
875 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
876 if (ToTemplate.isNull())
877 return {};
878
879 SmallVector<TemplateArgument, 2> ToTemplateArgs;
880 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
881 return {};
882
883 QualType ToCanonType;
884 if (!QualType(T, 0).isCanonical()) {
885 QualType FromCanonType
886 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
887 ToCanonType =Importer.Import(FromCanonType);
888 if (ToCanonType.isNull())
889 return {};
890 }
891 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
892 ToTemplateArgs,
893 ToCanonType);
894}
895
896QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
897 NestedNameSpecifier *ToQualifier = nullptr;
898 // Note: the qualifier in an ElaboratedType is optional.
899 if (T->getQualifier()) {
900 ToQualifier = Importer.Import(T->getQualifier());
901 if (!ToQualifier)
902 return {};
903 }
904
905 QualType ToNamedType = Importer.Import(T->getNamedType());
906 if (ToNamedType.isNull())
907 return {};
908
909 return Importer.getToContext().getElaboratedType(T->getKeyword(),
910 ToQualifier, ToNamedType);
911}
912
913QualType ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
914 QualType Pattern = Importer.Import(T->getPattern());
915 if (Pattern.isNull())
916 return {};
917
918 return Importer.getToContext().getPackExpansionType(Pattern,
919 T->getNumExpansions());
920}
921
922QualType ASTNodeImporter::VisitDependentTemplateSpecializationType(
923 const DependentTemplateSpecializationType *T) {
924 NestedNameSpecifier *Qualifier = Importer.Import(T->getQualifier());
925 if (!Qualifier && T->getQualifier())
926 return {};
927
928 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
929 if (!Name && T->getIdentifier())
930 return {};
931
932 SmallVector<TemplateArgument, 2> ToPack;
933 ToPack.reserve(T->getNumArgs());
934 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToPack))
935 return {};
936
937 return Importer.getToContext().getDependentTemplateSpecializationType(
938 T->getKeyword(), Qualifier, Name, ToPack);
939}
940
941QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
942 auto *Class =
943 dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
944 if (!Class)
945 return {};
946
947 return Importer.getToContext().getObjCInterfaceType(Class);
948}
949
950QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
951 QualType ToBaseType = Importer.Import(T->getBaseType());
952 if (ToBaseType.isNull())
953 return {};
954
955 SmallVector<QualType, 4> TypeArgs;
956 for (auto TypeArg : T->getTypeArgsAsWritten()) {
957 QualType ImportedTypeArg = Importer.Import(TypeArg);
958 if (ImportedTypeArg.isNull())
959 return {};
960
961 TypeArgs.push_back(ImportedTypeArg);
962 }
963
964 SmallVector<ObjCProtocolDecl *, 4> Protocols;
965 for (auto *P : T->quals()) {
966 auto *Protocol = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
967 if (!Protocol)
968 return {};
969 Protocols.push_back(Protocol);
970 }
971
972 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
973 Protocols,
974 T->isKindOfTypeAsWritten());
975}
976
977QualType
978ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
979 QualType ToPointeeType = Importer.Import(T->getPointeeType());
980 if (ToPointeeType.isNull())
981 return {};
982
983 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
984}
985
986//----------------------------------------------------------------------------
987// Import Declarations
988//----------------------------------------------------------------------------
989bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
990 DeclContext *&LexicalDC,
991 DeclarationName &Name,
992 NamedDecl *&ToD,
993 SourceLocation &Loc) {
994 // Import the context of this declaration.
995 DC = Importer.ImportContext(D->getDeclContext());
996 if (!DC)
997 return true;
998
999 LexicalDC = DC;
1000 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1001 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1002 if (!LexicalDC)
1003 return true;
1004 }
1005
1006 // Import the name of this declaration.
1007 Name = Importer.Import(D->getDeclName());
1008 if (D->getDeclName() && !Name)
1009 return true;
1010
1011 // Import the location of this declaration.
1012 Loc = Importer.Import(D->getLocation());
1013 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
1014 return false;
1015}
1016
1017void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1018 if (!FromD)
1019 return;
1020
1021 if (!ToD) {
1022 ToD = Importer.Import(FromD);
1023 if (!ToD)
1024 return;
1025 }
1026
1027 if (auto *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1028 if (auto *ToRecord = cast_or_null<RecordDecl>(ToD)) {
1029 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
1030 ImportDefinition(FromRecord, ToRecord);
1031 }
1032 }
1033 return;
1034 }
1035
1036 if (auto *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1037 if (auto *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1038 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1039 ImportDefinition(FromEnum, ToEnum);
1040 }
1041 }
1042 return;
1043 }
1044}
1045
1046void
1047ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1048 DeclarationNameInfo& To) {
1049 // NOTE: To.Name and To.Loc are already imported.
1050 // We only have to import To.LocInfo.
1051 switch (To.getName().getNameKind()) {
1052 case DeclarationName::Identifier:
1053 case DeclarationName::ObjCZeroArgSelector:
1054 case DeclarationName::ObjCOneArgSelector:
1055 case DeclarationName::ObjCMultiArgSelector:
1056 case DeclarationName::CXXUsingDirective:
1057 case DeclarationName::CXXDeductionGuideName:
1058 return;
1059
1060 case DeclarationName::CXXOperatorName: {
1061 SourceRange Range = From.getCXXOperatorNameRange();
1062 To.setCXXOperatorNameRange(Importer.Import(Range));
1063 return;
1064 }
1065 case DeclarationName::CXXLiteralOperatorName: {
1066 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1067 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1068 return;
1069 }
1070 case DeclarationName::CXXConstructorName:
1071 case DeclarationName::CXXDestructorName:
1072 case DeclarationName::CXXConversionFunctionName: {
1073 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1074 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1075 return;
1076 }
1077 }
1078 llvm_unreachable("Unknown name kind.")::llvm::llvm_unreachable_internal("Unknown name kind.", "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/AST/ASTImporter.cpp"
, 1078)
;
1079}
1080
1081void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
1082 if (Importer.isMinimalImport() && !ForceImport) {
9
Assuming the condition is false
1083 Importer.ImportContext(FromDC);
1084 return;
1085 }
1086
1087 for (auto *From : FromDC->decls())
1088 Importer.Import(From);
10
Calling 'ASTImporter::Import'
1089}
1090
1091bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
1092 ImportDefinitionKind Kind) {
1093 if (To->getDefinition() || To->isBeingDefined()) {
1094 if (Kind == IDK_Everything)
1095 ImportDeclContext(From, /*ForceImport=*/true);
1096
1097 return false;
1098 }
1099
1100 To->startDefinition();
1101
1102 // Add base classes.
1103 if (auto *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1104 auto *FromCXX = cast<CXXRecordDecl>(From);
1105
1106 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1107 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1108 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
1109 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
1110 ToData.Aggregate = FromData.Aggregate;
1111 ToData.PlainOldData = FromData.PlainOldData;
1112 ToData.Empty = FromData.Empty;
1113 ToData.Polymorphic = FromData.Polymorphic;
1114 ToData.Abstract = FromData.Abstract;
1115 ToData.IsStandardLayout = FromData.IsStandardLayout;
1116 ToData.IsCXX11StandardLayout = FromData.IsCXX11StandardLayout;
1117 ToData.HasBasesWithFields = FromData.HasBasesWithFields;
1118 ToData.HasBasesWithNonStaticDataMembers =
1119 FromData.HasBasesWithNonStaticDataMembers;
1120 ToData.HasPrivateFields = FromData.HasPrivateFields;
1121 ToData.HasProtectedFields = FromData.HasProtectedFields;
1122 ToData.HasPublicFields = FromData.HasPublicFields;
1123 ToData.HasMutableFields = FromData.HasMutableFields;
1124 ToData.HasVariantMembers = FromData.HasVariantMembers;
1125 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
1126 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
1127 ToData.HasUninitializedReferenceMember
1128 = FromData.HasUninitializedReferenceMember;
1129 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
1130 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1131 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
1132 ToData.NeedOverloadResolutionForCopyConstructor
1133 = FromData.NeedOverloadResolutionForCopyConstructor;
1134 ToData.NeedOverloadResolutionForMoveConstructor
1135 = FromData.NeedOverloadResolutionForMoveConstructor;
1136 ToData.NeedOverloadResolutionForMoveAssignment
1137 = FromData.NeedOverloadResolutionForMoveAssignment;
1138 ToData.NeedOverloadResolutionForDestructor
1139 = FromData.NeedOverloadResolutionForDestructor;
1140 ToData.DefaultedCopyConstructorIsDeleted
1141 = FromData.DefaultedCopyConstructorIsDeleted;
1142 ToData.DefaultedMoveConstructorIsDeleted
1143 = FromData.DefaultedMoveConstructorIsDeleted;
1144 ToData.DefaultedMoveAssignmentIsDeleted
1145 = FromData.DefaultedMoveAssignmentIsDeleted;
1146 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
1147 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1148 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
1149 ToData.HasConstexprNonCopyMoveConstructor
1150 = FromData.HasConstexprNonCopyMoveConstructor;
1151 ToData.HasDefaultedDefaultConstructor
1152 = FromData.HasDefaultedDefaultConstructor;
1153 ToData.DefaultedDefaultConstructorIsConstexpr
1154 = FromData.DefaultedDefaultConstructorIsConstexpr;
1155 ToData.HasConstexprDefaultConstructor
1156 = FromData.HasConstexprDefaultConstructor;
1157 ToData.HasNonLiteralTypeFieldsOrBases
1158 = FromData.HasNonLiteralTypeFieldsOrBases;
1159 // ComputedVisibleConversions not imported.
1160 ToData.UserProvidedDefaultConstructor
1161 = FromData.UserProvidedDefaultConstructor;
1162 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
1163 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1164 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1165 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1166 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
1167 ToData.ImplicitCopyAssignmentHasConstParam
1168 = FromData.ImplicitCopyAssignmentHasConstParam;
1169 ToData.HasDeclaredCopyConstructorWithConstParam
1170 = FromData.HasDeclaredCopyConstructorWithConstParam;
1171 ToData.HasDeclaredCopyAssignmentWithConstParam
1172 = FromData.HasDeclaredCopyAssignmentWithConstParam;
1173
1174 SmallVector<CXXBaseSpecifier *, 4> Bases;
1175 for (const auto &Base1 : FromCXX->bases()) {
1176 QualType T = Importer.Import(Base1.getType());
1177 if (T.isNull())
1178 return true;
1179
1180 SourceLocation EllipsisLoc;
1181 if (Base1.isPackExpansion())
1182 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
1183
1184 // Ensure that we have a definition for the base.
1185 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
1186
1187 Bases.push_back(
1188 new (Importer.getToContext())
1189 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
1190 Base1.isVirtual(),
1191 Base1.isBaseOfClass(),
1192 Base1.getAccessSpecifierAsWritten(),
1193 Importer.Import(Base1.getTypeSourceInfo()),
1194 EllipsisLoc));
1195 }
1196 if (!Bases.empty())
1197 ToCXX->setBases(Bases.data(), Bases.size());
1198 }
1199
1200 if (shouldForceImportDeclContext(Kind))
1201 ImportDeclContext(From, /*ForceImport=*/true);
1202
1203 To->completeDefinition();
1204 return false;
1205}
1206
1207bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
1208 ImportDefinitionKind Kind) {
1209 if (To->getAnyInitializer())
1210 return false;
1211
1212 // FIXME: Can we really import any initializer? Alternatively, we could force
1213 // ourselves to import every declaration of a variable and then only use
1214 // getInit() here.
1215 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
1216
1217 // FIXME: Other bits to merge?
1218
1219 return false;
1220}
1221
1222bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
1223 ImportDefinitionKind Kind) {
1224 if (To->getDefinition() || To->isBeingDefined()) {
1225 if (Kind == IDK_Everything)
1226 ImportDeclContext(From, /*ForceImport=*/true);
1227 return false;
1228 }
1229
1230 To->startDefinition();
1231
1232 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1233 if (T.isNull())
1234 return true;
1235
1236 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1237 if (ToPromotionType.isNull())
1238 return true;
1239
1240 if (shouldForceImportDeclContext(Kind))
1241 ImportDeclContext(From, /*ForceImport=*/true);
1242
1243 // FIXME: we might need to merge the number of positive or negative bits
1244 // if the enumerator lists don't match.
1245 To->completeDefinition(T, ToPromotionType,
1246 From->getNumPositiveBits(),
1247 From->getNumNegativeBits());
1248 return false;
1249}
1250
1251TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1252 TemplateParameterList *Params) {
1253 SmallVector<NamedDecl *, 4> ToParams(Params->size());
1254 if (ImportContainerChecked(*Params, ToParams))
1255 return nullptr;
1256
1257 Expr *ToRequiresClause;
1258 if (Expr *const R = Params->getRequiresClause()) {
1259 ToRequiresClause = Importer.Import(R);
1260 if (!ToRequiresClause)
1261 return nullptr;
1262 } else {
1263 ToRequiresClause = nullptr;
1264 }
1265
1266 return TemplateParameterList::Create(Importer.getToContext(),
1267 Importer.Import(Params->getTemplateLoc()),
1268 Importer.Import(Params->getLAngleLoc()),
1269 ToParams,
1270 Importer.Import(Params->getRAngleLoc()),
1271 ToRequiresClause);
1272}
1273
1274TemplateArgument
1275ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1276 switch (From.getKind()) {
1277 case TemplateArgument::Null:
1278 return TemplateArgument();
1279
1280 case TemplateArgument::Type: {
1281 QualType ToType = Importer.Import(From.getAsType());
1282 if (ToType.isNull())
1283 return {};
1284 return TemplateArgument(ToType);
1285 }
1286
1287 case TemplateArgument::Integral: {
1288 QualType ToType = Importer.Import(From.getIntegralType());
1289 if (ToType.isNull())
1290 return {};
1291 return TemplateArgument(From, ToType);
1292 }
1293
1294 case TemplateArgument::Declaration: {
1295 auto *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
1296 QualType ToType = Importer.Import(From.getParamTypeForDecl());
1297 if (!To || ToType.isNull())
1298 return {};
1299 return TemplateArgument(To, ToType);
1300 }
1301
1302 case TemplateArgument::NullPtr: {
1303 QualType ToType = Importer.Import(From.getNullPtrType());
1304 if (ToType.isNull())
1305 return {};
1306 return TemplateArgument(ToType, /*isNullPtr*/true);
1307 }
1308
1309 case TemplateArgument::Template: {
1310 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1311 if (ToTemplate.isNull())
1312 return {};
1313
1314 return TemplateArgument(ToTemplate);
1315 }
1316
1317 case TemplateArgument::TemplateExpansion: {
1318 TemplateName ToTemplate
1319 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1320 if (ToTemplate.isNull())
1321 return {};
1322
1323 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
1324 }
1325
1326 case TemplateArgument::Expression:
1327 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1328 return TemplateArgument(ToExpr);
1329 return TemplateArgument();
1330
1331 case TemplateArgument::Pack: {
1332 SmallVector<TemplateArgument, 2> ToPack;
1333 ToPack.reserve(From.pack_size());
1334 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1335 return {};
1336
1337 return TemplateArgument(
1338 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
1339 }
1340 }
1341
1342 llvm_unreachable("Invalid template argument kind")::llvm::llvm_unreachable_internal("Invalid template argument kind"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/AST/ASTImporter.cpp"
, 1342)
;
1343}
1344
1345Optional<TemplateArgumentLoc>
1346ASTNodeImporter::ImportTemplateArgumentLoc(const TemplateArgumentLoc &TALoc) {
1347 TemplateArgument Arg = ImportTemplateArgument(TALoc.getArgument());
1348 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
1349 TemplateArgumentLocInfo ToInfo;
1350 if (Arg.getKind() == TemplateArgument::Expression) {
1351 Expr *E = Importer.Import(FromInfo.getAsExpr());
1352 ToInfo = TemplateArgumentLocInfo(E);
1353 if (!E)
1354 return None;
1355 } else if (Arg.getKind() == TemplateArgument::Type) {
1356 if (TypeSourceInfo *TSI = Importer.Import(FromInfo.getAsTypeSourceInfo()))
1357 ToInfo = TemplateArgumentLocInfo(TSI);
1358 else
1359 return None;
1360 } else {
1361 ToInfo = TemplateArgumentLocInfo(
1362 Importer.Import(FromInfo.getTemplateQualifierLoc()),
1363 Importer.Import(FromInfo.getTemplateNameLoc()),
1364 Importer.Import(FromInfo.getTemplateEllipsisLoc()));
1365 }
1366 return TemplateArgumentLoc(Arg, ToInfo);
1367}
1368
1369bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1370 unsigned NumFromArgs,
1371 SmallVectorImpl<TemplateArgument> &ToArgs) {
1372 for (unsigned I = 0; I != NumFromArgs; ++I) {
1373 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1374 if (To.isNull() && !FromArgs[I].isNull())
1375 return true;
1376
1377 ToArgs.push_back(To);
1378 }
1379
1380 return false;
1381}
1382
1383// We cannot use Optional<> pattern here and below because
1384// TemplateArgumentListInfo's operator new is declared as deleted so it cannot
1385// be stored in Optional.
1386template <typename InContainerTy>
1387bool ASTNodeImporter::ImportTemplateArgumentListInfo(
1388 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1389 for (const auto &FromLoc : Container) {
1390 if (auto ToLoc = ImportTemplateArgumentLoc(FromLoc))
1391 ToTAInfo.addArgument(*ToLoc);
1392 else
1393 return true;
1394 }
1395 return false;
1396}
1397
1398bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
1399 RecordDecl *ToRecord, bool Complain) {
1400 // Eliminate a potential failure point where we attempt to re-import
1401 // something we're trying to import while completing ToRecord.
1402 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1403 if (ToOrigin) {
1404 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
1405 if (ToOriginRecord)
1406 ToRecord = ToOriginRecord;
1407 }
1408
1409 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1410 ToRecord->getASTContext(),
1411 Importer.getNonEquivalentDecls(),
1412 false, Complain);
1413 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
1414}
1415
1416bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
1417 bool Complain) {
1418 StructuralEquivalenceContext Ctx(
1419 Importer.getFromContext(), Importer.getToContext(),
1420 Importer.getNonEquivalentDecls(), false, Complain);
1421 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
1422}
1423
1424bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
1425 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1426 Importer.getToContext(),
1427 Importer.getNonEquivalentDecls());
1428 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
1429}
1430
1431bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
1432 FunctionTemplateDecl *To) {
1433 StructuralEquivalenceContext Ctx(
1434 Importer.getFromContext(), Importer.getToContext(),
1435 Importer.getNonEquivalentDecls(), false, false);
1436 return Ctx.IsStructurallyEquivalent(From, To);
1437}
1438
1439bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
1440 EnumConstantDecl *ToEC) {
1441 const llvm::APSInt &FromVal = FromEC->getInitVal();
1442 const llvm::APSInt &ToVal = ToEC->getInitVal();
1443
1444 return FromVal.isSigned() == ToVal.isSigned() &&
1445 FromVal.getBitWidth() == ToVal.getBitWidth() &&
1446 FromVal == ToVal;
1447}
1448
1449bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
1450 ClassTemplateDecl *To) {
1451 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1452 Importer.getToContext(),
1453 Importer.getNonEquivalentDecls());
1454 return Ctx.IsStructurallyEquivalent(From, To);
1455}
1456
1457bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
1458 VarTemplateDecl *To) {
1459 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1460 Importer.getToContext(),
1461 Importer.getNonEquivalentDecls());
1462 return Ctx.IsStructurallyEquivalent(From, To);
1463}
1464
1465Decl *ASTNodeImporter::VisitDecl(Decl *D) {
1466 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1467 << D->getDeclKindName();
1468 return nullptr;
1469}
1470
1471Decl *ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
1472 // Import the context of this declaration.
1473 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1474 if (!DC)
1475 return nullptr;
1476
1477 DeclContext *LexicalDC = DC;
1478 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1479 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1480 if (!LexicalDC)
1481 return nullptr;
1482 }
1483
1484 // Import the location of this declaration.
1485 SourceLocation Loc = Importer.Import(D->getLocation());
1486
1487 EmptyDecl *ToD = EmptyDecl::Create(Importer.getToContext(), DC, Loc);
1488 ToD->setLexicalDeclContext(LexicalDC);
1489 Importer.Imported(D, ToD);
1490 LexicalDC->addDeclInternal(ToD);
1491 return ToD;
1492}
1493
1494Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
1495 TranslationUnitDecl *ToD =
1496 Importer.getToContext().getTranslationUnitDecl();
1497
1498 Importer.Imported(D, ToD);
1499
1500 return ToD;
1501}
1502
1503Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
1504 SourceLocation Loc = Importer.Import(D->getLocation());
1505 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
1506
1507 // Import the context of this declaration.
1508 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1509 if (!DC)
1510 return nullptr;
1511
1512 AccessSpecDecl *accessSpecDecl
1513 = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(),
1514 DC, Loc, ColonLoc);
1515
1516 if (!accessSpecDecl)
1517 return nullptr;
1518
1519 // Lexical DeclContext and Semantic DeclContext
1520 // is always the same for the accessSpec.
1521 accessSpecDecl->setLexicalDeclContext(DC);
1522 DC->addDeclInternal(accessSpecDecl);
1523
1524 return accessSpecDecl;
1525}
1526
1527Decl *ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
1528 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1529 if (!DC)
1530 return nullptr;
1531
1532 DeclContext *LexicalDC = DC;
1533
1534 // Import the location of this declaration.
1535 SourceLocation Loc = Importer.Import(D->getLocation());
1536
1537 Expr *AssertExpr = Importer.Import(D->getAssertExpr());
1538 if (!AssertExpr)
1539 return nullptr;
1540
1541 StringLiteral *FromMsg = D->getMessage();
1542 auto *ToMsg = cast_or_null<StringLiteral>(Importer.Import(FromMsg));
1543 if (!ToMsg && FromMsg)
1544 return nullptr;
1545
1546 StaticAssertDecl *ToD = StaticAssertDecl::Create(
1547 Importer.getToContext(), DC, Loc, AssertExpr, ToMsg,
1548 Importer.Import(D->getRParenLoc()), D->isFailed());
1549
1550 ToD->setLexicalDeclContext(LexicalDC);
1551 LexicalDC->addDeclInternal(ToD);
1552 Importer.Imported(D, ToD);
1553 return ToD;
1554}
1555
1556Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1557 // Import the major distinguishing characteristics of this namespace.
1558 DeclContext *DC, *LexicalDC;
1559 DeclarationName Name;
1560 SourceLocation Loc;
1561 NamedDecl *ToD;
1562 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1563 return nullptr;
1564 if (ToD)
1565 return ToD;
1566
1567 NamespaceDecl *MergeWithNamespace = nullptr;
1568 if (!Name) {
1569 // This is an anonymous namespace. Adopt an existing anonymous
1570 // namespace if we can.
1571 // FIXME: Not testable.
1572 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
1573 MergeWithNamespace = TU->getAnonymousNamespace();
1574 else
1575 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1576 } else {
1577 SmallVector<NamedDecl *, 4> ConflictingDecls;
1578 SmallVector<NamedDecl *, 2> FoundDecls;
1579 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
1580 for (auto *FoundDecl : FoundDecls) {
1581 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace))
1582 continue;
1583
1584 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
1585 MergeWithNamespace = FoundNS;
1586 ConflictingDecls.clear();
1587 break;
1588 }
1589
1590 ConflictingDecls.push_back(FoundDecl);
1591 }
1592
1593 if (!ConflictingDecls.empty()) {
1594 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
1595 ConflictingDecls.data(),
1596 ConflictingDecls.size());
1597 }
1598 }
1599
1600 // Create the "to" namespace, if needed.
1601 NamespaceDecl *ToNamespace = MergeWithNamespace;
1602 if (!ToNamespace) {
1603 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
1604 D->isInline(),
1605 Importer.Import(D->getLocStart()),
1606 Loc, Name.getAsIdentifierInfo(),
1607 /*PrevDecl=*/nullptr);
1608 ToNamespace->setLexicalDeclContext(LexicalDC);
1609 LexicalDC->addDeclInternal(ToNamespace);
1610
1611 // If this is an anonymous namespace, register it as the anonymous
1612 // namespace within its context.
1613 if (!Name) {
1614 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
1615 TU->setAnonymousNamespace(ToNamespace);
1616 else
1617 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1618 }
1619 }
1620 Importer.Imported(D, ToNamespace);
1621
1622 ImportDeclContext(D);
1623
1624 return ToNamespace;
1625}
1626
1627Decl *ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1628 // Import the major distinguishing characteristics of this namespace.
1629 DeclContext *DC, *LexicalDC;
1630 DeclarationName Name;
1631 SourceLocation Loc;
1632 NamedDecl *LookupD;
1633 if (ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
1634 return nullptr;
1635 if (LookupD)
1636 return LookupD;
1637
1638 // NOTE: No conflict resolution is done for namespace aliases now.
1639
1640 auto *TargetDecl = cast_or_null<NamespaceDecl>(
1641 Importer.Import(D->getNamespace()));
1642 if (!TargetDecl)
1643 return nullptr;
1644
1645 IdentifierInfo *ToII = Importer.Import(D->getIdentifier());
1646 if (!ToII)
1647 return nullptr;
1648
1649 NestedNameSpecifierLoc ToQLoc = Importer.Import(D->getQualifierLoc());
1650 if (D->getQualifierLoc() && !ToQLoc)
1651 return nullptr;
1652
1653 NamespaceAliasDecl *ToD = NamespaceAliasDecl::Create(
1654 Importer.getToContext(), DC, Importer.Import(D->getNamespaceLoc()),
1655 Importer.Import(D->getAliasLoc()), ToII, ToQLoc,
1656 Importer.Import(D->getTargetNameLoc()), TargetDecl);
1657
1658 ToD->setLexicalDeclContext(LexicalDC);
1659 Importer.Imported(D, ToD);
1660 LexicalDC->addDeclInternal(ToD);
1661
1662 return ToD;
1663}
1664
1665Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
1666 // Import the major distinguishing characteristics of this typedef.
1667 DeclContext *DC, *LexicalDC;
1668 DeclarationName Name;
1669 SourceLocation Loc;
1670 NamedDecl *ToD;
1671 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1672 return nullptr;
1673 if (ToD)
1674 return ToD;
1675
1676 // If this typedef is not in block scope, determine whether we've
1677 // seen a typedef with the same name (that we can merge with) or any
1678 // other entity by that name (which name lookup could conflict with).
1679 if (!DC->isFunctionOrMethod()) {
1680 SmallVector<NamedDecl *, 4> ConflictingDecls;
1681 unsigned IDNS = Decl::IDNS_Ordinary;
1682 SmallVector<NamedDecl *, 2> FoundDecls;
1683 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
1684 for (auto *FoundDecl : FoundDecls) {
1685 if (!FoundDecl->isInIdentifierNamespace(IDNS))
1686 continue;
1687 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
1688 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1689 FoundTypedef->getUnderlyingType()))
1690 return Importer.Imported(D, FoundTypedef);
1691 }
1692
1693 ConflictingDecls.push_back(FoundDecl);
1694 }
1695
1696 if (!ConflictingDecls.empty()) {
1697 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1698 ConflictingDecls.data(),
1699 ConflictingDecls.size());
1700 if (!Name)
1701 return nullptr;
1702 }
1703 }
1704
1705 // Import the underlying type of this typedef;
1706 QualType T = Importer.Import(D->getUnderlyingType());
1707 if (T.isNull())
1708 return nullptr;
1709
1710 // Create the new typedef node.
1711 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1712 SourceLocation StartL = Importer.Import(D->getLocStart());
1713 TypedefNameDecl *ToTypedef;
1714 if (IsAlias)
1715 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC, StartL, Loc,
1716 Name.getAsIdentifierInfo(), TInfo);
1717 else
1718 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1719 StartL, Loc,
1720 Name.getAsIdentifierInfo(),
1721 TInfo);
1722
1723 ToTypedef->setAccess(D->getAccess());
1724 ToTypedef->setLexicalDeclContext(LexicalDC);
1725 Importer.Imported(D, ToTypedef);
1726
1727 // Templated declarations should not appear in DeclContext.
1728 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
1729 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
1730 LexicalDC->addDeclInternal(ToTypedef);
1731
1732 return ToTypedef;
1733}
1734
1735Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1736 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
1737}
1738
1739Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
1740 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
1741}
1742
1743Decl *ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
1744 // Import the major distinguishing characteristics of this typedef.
1745 DeclContext *DC, *LexicalDC;
1746 DeclarationName Name;
1747 SourceLocation Loc;
1748 NamedDecl *FoundD;
1749 if (ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
1750 return nullptr;
1751 if (FoundD)
1752 return FoundD;
1753
1754 // If this typedef is not in block scope, determine whether we've
1755 // seen a typedef with the same name (that we can merge with) or any
1756 // other entity by that name (which name lookup could conflict with).
1757 if (!DC->isFunctionOrMethod()) {
1758 SmallVector<NamedDecl *, 4> ConflictingDecls;
1759 unsigned IDNS = Decl::IDNS_Ordinary;
1760 SmallVector<NamedDecl *, 2> FoundDecls;
1761 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
1762 for (auto *FoundDecl : FoundDecls) {
1763 if (!FoundDecl->isInIdentifierNamespace(IDNS))
1764 continue;
1765 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
1766 return Importer.Imported(D, FoundAlias);
1767 ConflictingDecls.push_back(FoundDecl);
1768 }
1769
1770 if (!ConflictingDecls.empty()) {
1771 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1772 ConflictingDecls.data(),
1773 ConflictingDecls.size());
1774 if (!Name)
1775 return nullptr;
1776 }
1777 }
1778
1779 TemplateParameterList *Params = ImportTemplateParameterList(
1780 D->getTemplateParameters());
1781 if (!Params)
1782 return nullptr;
1783
1784 auto *TemplDecl = cast_or_null<TypeAliasDecl>(
1785 Importer.Import(D->getTemplatedDecl()));
1786 if (!TemplDecl)
1787 return nullptr;
1788
1789 TypeAliasTemplateDecl *ToAlias = TypeAliasTemplateDecl::Create(
1790 Importer.getToContext(), DC, Loc, Name, Params, TemplDecl);
1791
1792 TemplDecl->setDescribedAliasTemplate(ToAlias);
1793
1794 ToAlias->setAccess(D->getAccess());
1795 ToAlias->setLexicalDeclContext(LexicalDC);
1796 Importer.Imported(D, ToAlias);
1797 LexicalDC->addDeclInternal(ToAlias);
1798 return ToAlias;
1799}
1800
1801Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
1802 // Import the major distinguishing characteristics of this label.
1803 DeclContext *DC, *LexicalDC;
1804 DeclarationName Name;
1805 SourceLocation Loc;
1806 NamedDecl *ToD;
1807 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1808 return nullptr;
1809 if (ToD)
1810 return ToD;
1811
1812 assert(LexicalDC->isFunctionOrMethod())(static_cast <bool> (LexicalDC->isFunctionOrMethod()
) ? void (0) : __assert_fail ("LexicalDC->isFunctionOrMethod()"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/AST/ASTImporter.cpp"
, 1812, __extension__ __PRETTY_FUNCTION__))
;
1813
1814 LabelDecl *ToLabel = D->isGnuLocal()
1815 ? LabelDecl::Create(Importer.getToContext(),
1816 DC, Importer.Import(D->getLocation()),
1817 Name.getAsIdentifierInfo(),
1818 Importer.Import(D->getLocStart()))
1819 : LabelDecl::Create(Importer.getToContext(),
1820 DC, Importer.Import(D->getLocation()),
1821 Name.getAsIdentifierInfo());
1822 Importer.Imported(D, ToLabel);
1823
1824 auto *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt()));
1825 if (!Label)
1826 return nullptr;
1827
1828 ToLabel->setStmt(Label);
1829 ToLabel->setLexicalDeclContext(LexicalDC);
1830 LexicalDC->addDeclInternal(ToLabel);
1831 return ToLabel;
1832}
1833
1834Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1835 // Import the major distinguishing characteristics of this enum.
1836 DeclContext *DC, *LexicalDC;
1837 DeclarationName Name;
1838 SourceLocation Loc;
1839 NamedDecl *ToD;
1840 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1841 return nullptr;
1842 if (ToD)
1843 return ToD;
1844
1845 // Figure out what enum name we're looking for.
1846 unsigned IDNS = Decl::IDNS_Tag;
1847 DeclarationName SearchName = Name;
1848 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1849 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
1850 IDNS = Decl::IDNS_Ordinary;
1851 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
1852 IDNS |= Decl::IDNS_Ordinary;
1853
1854 // We may already have an enum of the same name; try to find and match it.
1855 if (!DC->isFunctionOrMethod() && SearchName) {
1856 SmallVector<NamedDecl *, 4> ConflictingDecls;
1857 SmallVector<NamedDecl *, 2> FoundDecls;
1858 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
1859 for (auto *FoundDecl : FoundDecls) {
1860 if (!FoundDecl->isInIdentifierNamespace(IDNS))
1861 continue;
1862
1863 Decl *Found = FoundDecl;
1864 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
1865 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1866 Found = Tag->getDecl();
1867 }
1868
1869 if (auto *FoundEnum = dyn_cast<EnumDecl>(Found)) {
1870 if (IsStructuralMatch(D, FoundEnum))
1871 return Importer.Imported(D, FoundEnum);
1872 }
1873
1874 ConflictingDecls.push_back(FoundDecl);
1875 }
1876
1877 if (!ConflictingDecls.empty()) {
1878 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1879 ConflictingDecls.data(),
1880 ConflictingDecls.size());
1881 }
1882 }
1883
1884 // Create the enum declaration.
1885 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
1886 Importer.Import(D->getLocStart()),
1887 Loc, Name.getAsIdentifierInfo(), nullptr,
1888 D->isScoped(), D->isScopedUsingClassTag(),
1889 D->isFixed());
1890 // Import the qualifier, if any.
1891 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
1892 D2->setAccess(D->getAccess());
1893 D2->setLexicalDeclContext(LexicalDC);
1894 Importer.Imported(D, D2);
1895 LexicalDC->addDeclInternal(D2);
1896
1897 // Import the integer type.
1898 QualType ToIntegerType = Importer.Import(D->getIntegerType());
1899 if (ToIntegerType.isNull())
1900 return nullptr;
1901 D2->setIntegerType(ToIntegerType);
1902
1903 // Import the definition
1904 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
1905 return nullptr;
1906
1907 return D2;
1908}
1909
1910Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
1911 // If this record has a definition in the translation unit we're coming from,
1912 // but this particular declaration is not that definition, import the
1913 // definition and map to that.
1914 TagDecl *Definition = D->getDefinition();
1915 if (Definition && Definition != D) {
1916 Decl *ImportedDef = Importer.Import(Definition);
1917 if (!ImportedDef)
1918 return nullptr;
1919
1920 return Importer.Imported(D, ImportedDef);
1921 }
1922
1923 // Import the major distinguishing characteristics of this record.
1924 DeclContext *DC, *LexicalDC;
1925 DeclarationName Name;
1926 SourceLocation Loc;
1927 NamedDecl *ToD;
1928 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1929 return nullptr;
1930 if (ToD)
1931 return ToD;
1932
1933 // Figure out what structure name we're looking for.
1934 unsigned IDNS = Decl::IDNS_Tag;
1935 DeclarationName SearchName = Name;
1936 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1937 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
1938 IDNS = Decl::IDNS_Ordinary;
1939 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
1940 IDNS |= Decl::IDNS_Ordinary;
1941
1942 // We may already have a record of the same name; try to find and match it.
1943 RecordDecl *AdoptDecl = nullptr;
1944 RecordDecl *PrevDecl = nullptr;
1945 if (!DC->isFunctionOrMethod()) {
1946 SmallVector<NamedDecl *, 4> ConflictingDecls;
1947 SmallVector<NamedDecl *, 2> FoundDecls;
1948 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
1949
1950 if (!FoundDecls.empty()) {
1951 // We're going to have to compare D against potentially conflicting Decls, so complete it.
1952 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
1953 D->getASTContext().getExternalSource()->CompleteType(D);
1954 }
1955
1956 for (auto *FoundDecl : FoundDecls) {
1957 if (!FoundDecl->isInIdentifierNamespace(IDNS))
1958 continue;
1959
1960 Decl *Found = FoundDecl;
1961 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
1962 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1963 Found = Tag->getDecl();
1964 }
1965
1966 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
1967 if (!SearchName) {
1968 // If both unnamed structs/unions are in a record context, make sure
1969 // they occur in the same location in the context records.
1970 if (Optional<unsigned> Index1 =
1971 StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(
1972 D)) {
1973 if (Optional<unsigned> Index2 = StructuralEquivalenceContext::
1974 findUntaggedStructOrUnionIndex(FoundRecord)) {
1975 if (*Index1 != *Index2)
1976 continue;
1977 }
1978 }
1979 }
1980
1981 PrevDecl = FoundRecord;
1982
1983 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
1984 if ((SearchName && !D->isCompleteDefinition())
1985 || (D->isCompleteDefinition() &&
1986 D->isAnonymousStructOrUnion()
1987 == FoundDef->isAnonymousStructOrUnion() &&
1988 IsStructuralMatch(D, FoundDef))) {
1989 // The record types structurally match, or the "from" translation
1990 // unit only had a forward declaration anyway; call it the same
1991 // function.
1992 // FIXME: For C++, we should also merge methods here.
1993 return Importer.Imported(D, FoundDef);
1994 }
1995 } else if (!D->isCompleteDefinition()) {
1996 // We have a forward declaration of this type, so adopt that forward
1997 // declaration rather than building a new one.
1998
1999 // If one or both can be completed from external storage then try one
2000 // last time to complete and compare them before doing this.
2001
2002 if (FoundRecord->hasExternalLexicalStorage() &&
2003 !FoundRecord->isCompleteDefinition())
2004 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2005 if (D->hasExternalLexicalStorage())
2006 D->getASTContext().getExternalSource()->CompleteType(D);
2007
2008 if (FoundRecord->isCompleteDefinition() &&
2009 D->isCompleteDefinition() &&
2010 !IsStructuralMatch(D, FoundRecord))
2011 continue;
2012
2013 AdoptDecl = FoundRecord;
2014 continue;
2015 } else if (!SearchName) {
2016 continue;
2017 }
2018 }
2019
2020 ConflictingDecls.push_back(FoundDecl);
2021 }
2022
2023 if (!ConflictingDecls.empty() && SearchName) {
2024 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2025 ConflictingDecls.data(),
2026 ConflictingDecls.size());
2027 }
2028 }
2029
2030 // Create the record declaration.
2031 RecordDecl *D2 = AdoptDecl;
2032 SourceLocation StartLoc = Importer.Import(D->getLocStart());
2033 if (!D2) {
2034 CXXRecordDecl *D2CXX = nullptr;
2035 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2036 if (DCXX->isLambda()) {
2037 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
2038 D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(),
2039 DC, TInfo, Loc,
2040 DCXX->isDependentLambda(),
2041 DCXX->isGenericLambda(),
2042 DCXX->getLambdaCaptureDefault());
2043 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
2044 if (DCXX->getLambdaContextDecl() && !CDecl)
2045 return nullptr;
2046 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl);
2047 } else if (DCXX->isInjectedClassName()) {
2048 // We have to be careful to do a similar dance to the one in
2049 // Sema::ActOnStartCXXMemberDeclarations
2050 CXXRecordDecl *const PrevDecl = nullptr;
2051 const bool DelayTypeCreation = true;
2052 D2CXX = CXXRecordDecl::Create(
2053 Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc,
2054 Name.getAsIdentifierInfo(), PrevDecl, DelayTypeCreation);
2055 Importer.getToContext().getTypeDeclType(
2056 D2CXX, dyn_cast<CXXRecordDecl>(DC));
2057 } else {
2058 D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
2059 D->getTagKind(),
2060 DC, StartLoc, Loc,
2061 Name.getAsIdentifierInfo());
2062 }
2063 D2 = D2CXX;
2064 D2->setAccess(D->getAccess());
2065 D2->setLexicalDeclContext(LexicalDC);
2066 if (!DCXX->getDescribedClassTemplate())
2067 LexicalDC->addDeclInternal(D2);
2068
2069 Importer.Imported(D, D2);
2070
2071 if (ClassTemplateDecl *FromDescribed =
2072 DCXX->getDescribedClassTemplate()) {
2073 auto *ToDescribed = cast_or_null<ClassTemplateDecl>(
2074 Importer.Import(FromDescribed));
2075 if (!ToDescribed)
2076 return nullptr;
2077 D2CXX->setDescribedClassTemplate(ToDescribed);
2078 } else if (MemberSpecializationInfo *MemberInfo =
2079 DCXX->getMemberSpecializationInfo()) {
2080 TemplateSpecializationKind SK =
2081 MemberInfo->getTemplateSpecializationKind();
2082 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
2083 auto *ToInst =
2084 cast_or_null<CXXRecordDecl>(Importer.Import(FromInst));
2085 if (FromInst && !ToInst)
2086 return nullptr;
2087 D2CXX->setInstantiationOfMemberClass(ToInst, SK);
2088 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2089 Importer.Import(MemberInfo->getPointOfInstantiation()));
2090 }
2091 } else {
2092 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
2093 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
2094 D2->setLexicalDeclContext(LexicalDC);
2095 LexicalDC->addDeclInternal(D2);
2096 }
2097
2098 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
2099 if (D->isAnonymousStructOrUnion())
2100 D2->setAnonymousStructOrUnion(true);
2101 if (PrevDecl) {
2102 // FIXME: do this for all Redeclarables, not just RecordDecls.
2103 D2->setPreviousDecl(PrevDecl);
2104 }
2105 }
2106
2107 Importer.Imported(D, D2);
2108
2109 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
2110 return nullptr;
2111
2112 return D2;
2113}
2114
2115Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2116 // Import the major distinguishing characteristics of this enumerator.
2117 DeclContext *DC, *LexicalDC;
2118 DeclarationName Name;
2119 SourceLocation Loc;
2120 NamedDecl *ToD;
2121 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2122 return nullptr;
2123 if (ToD)
2124 return ToD;
2125
2126 QualType T = Importer.Import(D->getType());
2127 if (T.isNull())
2128 return nullptr;
2129
2130 // Determine whether there are any other declarations with the same name and
2131 // in the same context.
2132 if (!LexicalDC->isFunctionOrMethod()) {
2133 SmallVector<NamedDecl *, 4> ConflictingDecls;
2134 unsigned IDNS = Decl::IDNS_Ordinary;
2135 SmallVector<NamedDecl *, 2> FoundDecls;
2136 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2137 for (auto *FoundDecl : FoundDecls) {
2138 if (!FoundDecl->isInIdentifierNamespace(IDNS))
2139 continue;
2140
2141 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
2142 if (IsStructuralMatch(D, FoundEnumConstant))
2143 return Importer.Imported(D, FoundEnumConstant);
2144 }
2145
2146 ConflictingDecls.push_back(FoundDecl);
2147 }
2148
2149 if (!ConflictingDecls.empty()) {
2150 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2151 ConflictingDecls.data(),
2152 ConflictingDecls.size());
2153 if (!Name)
2154 return nullptr;
2155 }
2156 }
2157
2158 Expr *Init = Importer.Import(D->getInitExpr());
2159 if (D->getInitExpr() && !Init)
2160 return nullptr;
2161
2162 EnumConstantDecl *ToEnumerator
2163 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2164 Name.getAsIdentifierInfo(), T,
2165 Init, D->getInitVal());
2166 ToEnumerator->setAccess(D->getAccess());
2167 ToEnumerator->setLexicalDeclContext(LexicalDC);
2168 Importer.Imported(D, ToEnumerator);
2169 LexicalDC->addDeclInternal(ToEnumerator);
2170 return ToEnumerator;
2171}
2172
2173bool ASTNodeImporter::ImportTemplateInformation(FunctionDecl *FromFD,
2174 FunctionDecl *ToFD) {
2175 switch (FromFD->getTemplatedKind()) {
2176 case FunctionDecl::TK_NonTemplate:
2177 case FunctionDecl::TK_FunctionTemplate:
2178 return false;
2179
2180 case FunctionDecl::TK_MemberSpecialization: {
2181 auto *InstFD = cast_or_null<FunctionDecl>(
2182 Importer.Import(FromFD->getInstantiatedFromMemberFunction()));
2183 if (!InstFD)
2184 return true;
2185
2186 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
2187 SourceLocation POI = Importer.Import(
2188 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation());
2189 ToFD->setInstantiationOfMemberFunction(InstFD, TSK);
2190 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
2191 return false;
2192 }
2193
2194 case FunctionDecl::TK_FunctionTemplateSpecialization: {
2195 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
2196 auto *Template = cast_or_null<FunctionTemplateDecl>(
2197 Importer.Import(FTSInfo->getTemplate()));
2198 if (!Template)
2199 return true;
2200 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
2201
2202 // Import template arguments.
2203 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
2204 SmallVector<TemplateArgument, 8> ToTemplArgs;
2205 if (ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
2206 ToTemplArgs))
2207 return true;
2208
2209 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
2210 Importer.getToContext(), ToTemplArgs);
2211
2212 TemplateArgumentListInfo ToTAInfo;
2213 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
2214 if (FromTAArgsAsWritten)
2215 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ToTAInfo))
2216 return true;
2217
2218 SourceLocation POI = Importer.Import(FTSInfo->getPointOfInstantiation());
2219
2220 ToFD->setFunctionTemplateSpecialization(
2221 Template, ToTAList, /* InsertPos= */ nullptr,
2222 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, POI);
2223 return false;
2224 }
2225
2226 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2227 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2228 UnresolvedSet<8> TemplDecls;
2229 unsigned NumTemplates = FromInfo->getNumTemplates();
2230 for (unsigned I = 0; I < NumTemplates; I++) {
2231 if (auto *ToFTD = cast_or_null<FunctionTemplateDecl>(
2232 Importer.Import(FromInfo->getTemplate(I))))
2233 TemplDecls.addDecl(ToFTD);
2234 else
2235 return true;
2236 }
2237
2238 // Import TemplateArgumentListInfo.
2239 TemplateArgumentListInfo ToTAInfo;
2240 if (ImportTemplateArgumentListInfo(
2241 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2242 llvm::makeArrayRef(FromInfo->getTemplateArgs(),
2243 FromInfo->getNumTemplateArgs()),
2244 ToTAInfo))
2245 return true;
2246
2247 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2248 TemplDecls, ToTAInfo);
2249 return false;
2250 }
2251 }
2252 llvm_unreachable("All cases should be covered!")::llvm::llvm_unreachable_internal("All cases should be covered!"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/AST/ASTImporter.cpp"
, 2252)
;
2253}
2254
2255Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2256 // Import the major distinguishing characteristics of this function.
2257 DeclContext *DC, *LexicalDC;
2258 DeclarationName Name;
2259 SourceLocation Loc;
2260 NamedDecl *ToD;
2261 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2262 return nullptr;
2263 if (ToD)
2264 return ToD;
2265
2266 const FunctionDecl *FoundWithoutBody = nullptr;
2267
2268 // Try to find a function in our own ("to") context with the same name, same
2269 // type, and in the same context as the function we're importing.
2270 if (!LexicalDC->isFunctionOrMethod()) {
2271 SmallVector<NamedDecl *, 4> ConflictingDecls;
2272 unsigned IDNS = Decl::IDNS_Ordinary;
2273 SmallVector<NamedDecl *, 2> FoundDecls;
2274 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2275 for (auto *FoundDecl : FoundDecls) {
2276 if (!FoundDecl->isInIdentifierNamespace(IDNS))
2277 continue;
2278
2279 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
2280 if (FoundFunction->hasExternalFormalLinkage() &&
2281 D->hasExternalFormalLinkage()) {
2282 if (Importer.IsStructurallyEquivalent(D->getType(),
2283 FoundFunction->getType())) {
2284 // FIXME: Actually try to merge the body and other attributes.
2285 const FunctionDecl *FromBodyDecl = nullptr;
2286 D->hasBody(FromBodyDecl);
2287 if (D == FromBodyDecl && !FoundFunction->hasBody()) {
2288 // This function is needed to merge completely.
2289 FoundWithoutBody = FoundFunction;
2290 break;
2291 }
2292 return Importer.Imported(D, FoundFunction);
2293 }
2294
2295 // FIXME: Check for overloading more carefully, e.g., by boosting
2296 // Sema::IsOverload out to the AST library.
2297
2298 // Function overloading is okay in C++.
2299 if (Importer.getToContext().getLangOpts().CPlusPlus)
2300 continue;
2301
2302 // Complain about inconsistent function types.
2303 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
2304 << Name << D->getType() << FoundFunction->getType();
2305 Importer.ToDiag(FoundFunction->getLocation(),
2306 diag::note_odr_value_here)
2307 << FoundFunction->getType();
2308 }
2309 }
2310
2311 ConflictingDecls.push_back(FoundDecl);
2312 }
2313
2314 if (!ConflictingDecls.empty()) {
2315 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2316 ConflictingDecls.data(),
2317 ConflictingDecls.size());
2318 if (!Name)
2319 return nullptr;
2320 }
2321 }
2322
2323 DeclarationNameInfo NameInfo(Name, Loc);
2324 // Import additional name location/type info.
2325 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2326
2327 QualType FromTy = D->getType();
2328 bool usedDifferentExceptionSpec = false;
2329
2330 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2331 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2332 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2333 // FunctionDecl that we are importing the FunctionProtoType for.
2334 // To avoid an infinite recursion when importing, create the FunctionDecl
2335 // with a simplified function type and update it afterwards.
2336 if (FromEPI.ExceptionSpec.SourceDecl ||
2337 FromEPI.ExceptionSpec.SourceTemplate ||
2338 FromEPI.ExceptionSpec.NoexceptExpr) {
2339 FunctionProtoType::ExtProtoInfo DefaultEPI;
2340 FromTy = Importer.getFromContext().getFunctionType(
2341 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
2342 usedDifferentExceptionSpec = true;
2343 }
2344 }
2345
2346 // Import the type.
2347 QualType T = Importer.Import(FromTy);
2348 if (T.isNull())
2349 return nullptr;
2350
2351 // Import the function parameters.
2352 SmallVector<ParmVarDecl *, 8> Parameters;
2353 for (auto P : D->parameters()) {
2354 auto *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
2355 if (!ToP)
2356 return nullptr;
2357
2358 Parameters.push_back(ToP);
2359 }
2360
2361 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2362 if (D->getTypeSourceInfo() && !TInfo)
2363 return nullptr;
2364
2365 // Create the imported function.
2366 FunctionDecl *ToFunction = nullptr;
2367 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
2368 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2369 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2370 cast<CXXRecordDecl>(DC),
2371 InnerLocStart,
2372 NameInfo, T, TInfo,
2373 FromConstructor->isExplicit(),
2374 D->isInlineSpecified(),
2375 D->isImplicit(),
2376 D->isConstexpr());
2377 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
2378 SmallVector<CXXCtorInitializer *, 4> CtorInitializers;
2379 for (auto *I : FromConstructor->inits()) {
2380 auto *ToI = cast_or_null<CXXCtorInitializer>(Importer.Import(I));
2381 if (!ToI && I)
2382 return nullptr;
2383 CtorInitializers.push_back(ToI);
2384 }
2385 auto **Memory =
2386 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
2387 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
2388 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
2389 ToCtor->setCtorInitializers(Memory);
2390 ToCtor->setNumCtorInitializers(NumInitializers);
2391 }
2392 } else if (isa<CXXDestructorDecl>(D)) {
2393 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2394 cast<CXXRecordDecl>(DC),
2395 InnerLocStart,
2396 NameInfo, T, TInfo,
2397 D->isInlineSpecified(),
2398 D->isImplicit());
2399 } else if (auto *FromConversion = dyn_cast<CXXConversionDecl>(D)) {
2400 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2401 cast<CXXRecordDecl>(DC),
2402 InnerLocStart,
2403 NameInfo, T, TInfo,
2404 D->isInlineSpecified(),
2405 FromConversion->isExplicit(),
2406 D->isConstexpr(),
2407 Importer.Import(D->getLocEnd()));
2408 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
2409 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2410 cast<CXXRecordDecl>(DC),
2411 InnerLocStart,
2412 NameInfo, T, TInfo,
2413 Method->getStorageClass(),
2414 Method->isInlineSpecified(),
2415 D->isConstexpr(),
2416 Importer.Import(D->getLocEnd()));
2417 } else {
2418 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
2419 InnerLocStart,
2420 NameInfo, T, TInfo, D->getStorageClass(),
2421 D->isInlineSpecified(),
2422 D->hasWrittenPrototype(),
2423 D->isConstexpr());
2424 }
2425
2426 // Import the qualifier, if any.
2427 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
2428 ToFunction->setAccess(D->getAccess());
2429 ToFunction->setLexicalDeclContext(LexicalDC);
2430 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2431 ToFunction->setTrivial(D->isTrivial());
2432 ToFunction->setPure(D->isPure());
2433 Importer.Imported(D, ToFunction);
2434
2435 // Set the parameters.
2436 for (auto *Param : Parameters) {
2437 Param->setOwningFunction(ToFunction);
2438 ToFunction->addDeclInternal(Param);
2439 }
2440 ToFunction->setParams(Parameters);
2441
2442 if (FoundWithoutBody) {
2443 auto *Recent = const_cast<FunctionDecl *>(
2444 FoundWithoutBody->getMostRecentDecl());
2445 ToFunction->setPreviousDecl(Recent);
2446 }
2447
2448 // We need to complete creation of FunctionProtoTypeLoc manually with setting
2449 // params it refers to.
2450 if (TInfo) {
2451 if (auto ProtoLoc =
2452 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
2453 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
2454 ProtoLoc.setParam(I, Parameters[I]);
2455 }
2456 }
2457
2458 if (usedDifferentExceptionSpec) {
2459 // Update FunctionProtoType::ExtProtoInfo.
2460 QualType T = Importer.Import(D->getType());
2461 if (T.isNull())
2462 return nullptr;
2463 ToFunction->setType(T);
2464 }
2465
2466 // Import the body, if any.
2467 if (Stmt *FromBody = D->getBody()) {
2468 if (Stmt *ToBody = Importer.Import(FromBody)) {
2469 ToFunction->setBody(ToBody);
2470 }
2471 }
2472
2473 // FIXME: Other bits to merge?
2474
2475 // If it is a template, import all related things.
2476 if (ImportTemplateInformation(D, ToFunction))
2477 return nullptr;
2478
2479 // Add this function to the lexical context.
2480 // NOTE: If the function is templated declaration, it should be not added into
2481 // LexicalDC. But described template is imported during import of
2482 // FunctionTemplateDecl (it happens later). So, we use source declaration
2483 // to determine if we should add the result function.
2484 if (!D->getDescribedFunctionTemplate())
2485 LexicalDC->addDeclInternal(ToFunction);
2486
2487 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
2488 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
2489
2490 return ToFunction;
2491}
2492
2493Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2494 return VisitFunctionDecl(D);
2495}
2496
2497Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2498 return VisitCXXMethodDecl(D);
2499}
2500
2501Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2502 return VisitCXXMethodDecl(D);
2503}
2504
2505Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2506 return VisitCXXMethodDecl(D);
2507}
2508
2509static unsigned getFieldIndex(Decl *F) {
2510 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
2511 if (!Owner)
2512 return 0;
2513
2514 unsigned Index = 1;
2515 for (const auto *D : Owner->noload_decls()) {
2516 if (D == F)
2517 return Index;
2518
2519 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2520 ++Index;
2521 }
2522
2523 return Index;
2524}
2525
2526Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2527 // Import the major distinguishing characteristics of a variable.
2528 DeclContext *DC, *LexicalDC;
2529 DeclarationName Name;
2530 SourceLocation Loc;
2531 NamedDecl *ToD;
2532 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2533 return nullptr;
2534 if (ToD)
2535 return ToD;
2536
2537 // Determine whether we've already imported this field.
2538 SmallVector<NamedDecl *, 2> FoundDecls;
2539 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2540 for (auto *FoundDecl : FoundDecls) {
2541 if (auto *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
2542 // For anonymous fields, match up by index.
2543 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2544 continue;
2545
2546 if (Importer.IsStructurallyEquivalent(D->getType(),
2547 FoundField->getType())) {
2548 Importer.Imported(D, FoundField);
2549 return FoundField;
2550 }
2551
2552 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2553 << Name << D->getType() << FoundField->getType();
2554 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2555 << FoundField->getType();
2556 return nullptr;
2557 }
2558 }
2559
2560 // Import the type.
2561 QualType T = Importer.Import(D->getType());
2562 if (T.isNull())
2563 return nullptr;
2564
2565 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2566 Expr *BitWidth = Importer.Import(D->getBitWidth());
2567 if (!BitWidth && D->getBitWidth())
2568 return nullptr;
2569
2570 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2571 Importer.Import(D->getInnerLocStart()),
2572 Loc, Name.getAsIdentifierInfo(),
2573 T, TInfo, BitWidth, D->isMutable(),
2574 D->getInClassInitStyle());
2575 ToField->setAccess(D->getAccess());
2576 ToField->setLexicalDeclContext(LexicalDC);
2577 if (Expr *FromInitializer = D->getInClassInitializer()) {
2578 Expr *ToInitializer = Importer.Import(FromInitializer);
2579 if (ToInitializer)
2580 ToField->setInClassInitializer(ToInitializer);
2581 else
2582 return nullptr;
2583 }
2584 ToField->setImplicit(D->isImplicit());
2585 Importer.Imported(D, ToField);
2586 LexicalDC->addDeclInternal(ToField);
2587 return ToField;
2588}
2589
2590Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2591 // Import the major distinguishing characteristics of a variable.
2592 DeclContext *DC, *LexicalDC;
2593 DeclarationName Name;
2594 SourceLocation Loc;
2595 NamedDecl *ToD;
2596 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2597 return nullptr;
2598 if (ToD)
2599 return ToD;
2600
2601 // Determine whether we've already imported this field.
2602 SmallVector<NamedDecl *, 2> FoundDecls;
2603 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2604 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2605 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
2606 // For anonymous indirect fields, match up by index.
2607 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2608 continue;
2609
2610 if (Importer.IsStructurallyEquivalent(D->getType(),
2611 FoundField->getType(),
2612 !Name.isEmpty())) {
2613 Importer.Imported(D, FoundField);
2614 return FoundField;
2615 }
2616
2617 // If there are more anonymous fields to check, continue.
2618 if (!Name && I < N-1)
2619 continue;
2620
2621 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2622 << Name << D->getType() << FoundField->getType();
2623 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2624 << FoundField->getType();
2625 return nullptr;
2626 }
2627 }
2628
2629 // Import the type.
2630 QualType T = Importer.Import(D->getType());
2631 if (T.isNull())
2632 return nullptr;
2633
2634 auto **NamedChain =
2635 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
2636
2637 unsigned i = 0;
2638 for (auto *PI : D->chain()) {
2639 Decl *D = Importer.Import(PI);
2640 if (!D)
2641 return nullptr;
2642 NamedChain[i++] = cast<NamedDecl>(D);
2643 }
2644
2645 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2646 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
2647 {NamedChain, D->getChainingSize()});
2648
2649 for (const auto *Attr : D->attrs())
2650 ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
2651
2652 ToIndirectField->setAccess(D->getAccess());
2653 ToIndirectField->setLexicalDeclContext(LexicalDC);
2654 Importer.Imported(D, ToIndirectField);
2655 LexicalDC->addDeclInternal(ToIndirectField);
2656 return ToIndirectField;
2657}
2658
2659Decl *ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
2660 // Import the major distinguishing characteristics of a declaration.
2661 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2662 DeclContext *LexicalDC = D->getDeclContext() == D->getLexicalDeclContext()
2663 ? DC : Importer.ImportContext(D->getLexicalDeclContext());
2664 if (!DC || !LexicalDC)
2665 return nullptr;
2666
2667 // Determine whether we've already imported this decl.
2668 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
2669 auto *RD = cast<CXXRecordDecl>(DC);
2670 FriendDecl *ImportedFriend = RD->getFirstFriend();
2671 StructuralEquivalenceContext Context(
2672 Importer.getFromContext(), Importer.getToContext(),
2673 Importer.getNonEquivalentDecls(), false, false);
2674
2675 while (ImportedFriend) {
2676 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
2677 if (Context.IsStructurallyEquivalent(D->getFriendDecl(),
2678 ImportedFriend->getFriendDecl()))
2679 return Importer.Imported(D, ImportedFriend);
2680
2681 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
2682 if (Importer.IsStructurallyEquivalent(
2683 D->getFriendType()->getType(),
2684 ImportedFriend->getFriendType()->getType(), true))
2685 return Importer.Imported(D, ImportedFriend);
2686 }
2687 ImportedFriend = ImportedFriend->getNextFriend();
2688 }
2689
2690 // Not found. Create it.
2691 FriendDecl::FriendUnion ToFU;
2692 if (NamedDecl *FriendD = D->getFriendDecl())
2693 ToFU = cast_or_null<NamedDecl>(Importer.Import(FriendD));
2694 else
2695 ToFU = Importer.Import(D->getFriendType());
2696 if (!ToFU)
2697 return nullptr;
2698
2699 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
2700 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
2701 for (unsigned I = 0; I < D->NumTPLists; I++) {
2702 TemplateParameterList *List = ImportTemplateParameterList(FromTPLists[I]);
2703 if (!List)
2704 return nullptr;
2705 ToTPLists[I] = List;
2706 }
2707
2708 FriendDecl *FrD = FriendDecl::Create(Importer.getToContext(), DC,
2709 Importer.Import(D->getLocation()),
2710 ToFU, Importer.Import(D->getFriendLoc()),
2711 ToTPLists);
2712
2713 Importer.Imported(D, FrD);
2714 RD->pushFriendDecl(FrD);
2715
2716 FrD->setAccess(D->getAccess());
2717 FrD->setLexicalDeclContext(LexicalDC);
2718 LexicalDC->addDeclInternal(FrD);
2719 return FrD;
2720}
2721
2722Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2723 // Import the major distinguishing characteristics of an ivar.
2724 DeclContext *DC, *LexicalDC;
2725 DeclarationName Name;
2726 SourceLocation Loc;
2727 NamedDecl *ToD;
2728 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2729 return nullptr;
2730 if (ToD)
2731 return ToD;
2732
2733 // Determine whether we've already imported this ivar
2734 SmallVector<NamedDecl *, 2> FoundDecls;
2735 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2736 for (auto *FoundDecl : FoundDecls) {
2737 if (auto *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
2738 if (Importer.IsStructurallyEquivalent(D->getType(),
2739 FoundIvar->getType())) {
2740 Importer.Imported(D, FoundIvar);
2741 return FoundIvar;
2742 }
2743
2744 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2745 << Name << D->getType() << FoundIvar->getType();
2746 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2747 << FoundIvar->getType();
2748 return nullptr;
2749 }
2750 }
2751
2752 // Import the type.
2753 QualType T = Importer.Import(D->getType());
2754 if (T.isNull())
2755 return nullptr;
2756
2757 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2758 Expr *BitWidth = Importer.Import(D->getBitWidth());
2759 if (!BitWidth && D->getBitWidth())
2760 return nullptr;
2761
2762 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2763 cast<ObjCContainerDecl>(DC),
2764 Importer.Import(D->getInnerLocStart()),
2765 Loc, Name.getAsIdentifierInfo(),
2766 T, TInfo, D->getAccessControl(),
2767 BitWidth, D->getSynthesize());
2768 ToIvar->setLexicalDeclContext(LexicalDC);
2769 Importer.Imported(D, ToIvar);
2770 LexicalDC->addDeclInternal(ToIvar);
2771 return ToIvar;
2772}
2773
2774Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2775 // Import the major distinguishing characteristics of a variable.
2776 DeclContext *DC, *LexicalDC;
2777 DeclarationName Name;
2778 SourceLocation Loc;
2779 NamedDecl *ToD;
2780 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2781 return nullptr;
2782 if (ToD)
2783 return ToD;
2784
2785 // Try to find a variable in our own ("to") context with the same name and
2786 // in the same context as the variable we're importing.
2787 if (D->isFileVarDecl()) {
2788 VarDecl *MergeWithVar = nullptr;
2789 SmallVector<NamedDecl *, 4> ConflictingDecls;
2790 unsigned IDNS = Decl::IDNS_Ordinary;
2791 SmallVector<NamedDecl *, 2> FoundDecls;
2792 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2793 for (auto *FoundDecl : FoundDecls) {
2794 if (!FoundDecl->isInIdentifierNamespace(IDNS))
2795 continue;
2796
2797 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
2798 // We have found a variable that we may need to merge with. Check it.
2799 if (FoundVar->hasExternalFormalLinkage() &&
2800 D->hasExternalFormalLinkage()) {
2801 if (Importer.IsStructurallyEquivalent(D->getType(),
2802 FoundVar->getType())) {
2803 MergeWithVar = FoundVar;
2804 break;
2805 }
2806
2807 const ArrayType *FoundArray
2808 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2809 const ArrayType *TArray
2810 = Importer.getToContext().getAsArrayType(D->getType());
2811 if (FoundArray && TArray) {
2812 if (isa<IncompleteArrayType>(FoundArray) &&
2813 isa<ConstantArrayType>(TArray)) {
2814 // Import the type.
2815 QualType T = Importer.Import(D->getType());
2816 if (T.isNull())
2817 return nullptr;
2818
2819 FoundVar->setType(T);
2820 MergeWithVar = FoundVar;
2821 break;
2822 } else if (isa<IncompleteArrayType>(TArray) &&
2823 isa<ConstantArrayType>(FoundArray)) {
2824 MergeWithVar = FoundVar;
2825 break;
2826 }
2827 }
2828
2829 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
2830 << Name << D->getType() << FoundVar->getType();
2831 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2832 << FoundVar->getType();
2833 }
2834 }
2835
2836 ConflictingDecls.push_back(FoundDecl);
2837 }
2838
2839 if (MergeWithVar) {
2840 // An equivalent variable with external linkage has been found. Link
2841 // the two declarations, then merge them.
2842 Importer.Imported(D, MergeWithVar);
2843
2844 if (VarDecl *DDef = D->getDefinition()) {
2845 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2846 Importer.ToDiag(ExistingDef->getLocation(),
2847 diag::err_odr_variable_multiple_def)
2848 << Name;
2849 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2850 } else {
2851 Expr *Init = Importer.Import(DDef->getInit());
2852 MergeWithVar->setInit(Init);
2853 if (DDef->isInitKnownICE()) {
2854 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2855 Eval->CheckedICE = true;
2856 Eval->IsICE = DDef->isInitICE();
2857 }
2858 }
2859 }
2860
2861 return MergeWithVar;
2862 }
2863
2864 if (!ConflictingDecls.empty()) {
2865 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2866 ConflictingDecls.data(),
2867 ConflictingDecls.size());
2868 if (!Name)
2869 return nullptr;
2870 }
2871 }
2872
2873 // Import the type.
2874 QualType T = Importer.Import(D->getType());
2875 if (T.isNull())
2876 return nullptr;
2877
2878 // Create the imported variable.
2879 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2880 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2881 Importer.Import(D->getInnerLocStart()),
2882 Loc, Name.getAsIdentifierInfo(),
2883 T, TInfo,
2884 D->getStorageClass());
2885 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
2886 ToVar->setAccess(D->getAccess());
2887 ToVar->setLexicalDeclContext(LexicalDC);
2888 Importer.Imported(D, ToVar);
2889
2890 // Templated declarations should never appear in the enclosing DeclContext.
2891 if (!D->getDescribedVarTemplate())
2892 LexicalDC->addDeclInternal(ToVar);
2893
2894 if (!D->isFileVarDecl() &&
2895 D->isUsed())
2896 ToVar->setIsUsed();
2897
2898 // Merge the initializer.
2899 if (ImportDefinition(D, ToVar))
2900 return nullptr;
2901
2902 if (D->isConstexpr())
2903 ToVar->setConstexpr(true);
2904
2905 return ToVar;
2906}
2907
2908Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2909 // Parameters are created in the translation unit's context, then moved
2910 // into the function declaration's context afterward.
2911 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2912
2913 // Import the name of this declaration.
2914 DeclarationName Name = Importer.Import(D->getDeclName());
2915 if (D->getDeclName() && !Name)
2916 return nullptr;
2917
2918 // Import the location of this declaration.
2919 SourceLocation Loc = Importer.Import(D->getLocation());
2920
2921 // Import the parameter's type.
2922 QualType T = Importer.Import(D->getType());
2923 if (T.isNull())
2924 return nullptr;
2925
2926 // Create the imported parameter.
2927 auto *ToParm = ImplicitParamDecl::Create(Importer.getToContext(), DC, Loc,
2928 Name.getAsIdentifierInfo(), T,
2929 D->getParameterKind());
2930 return Importer.Imported(D, ToParm);
2931}
2932
2933Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2934 // Parameters are created in the translation unit's context, then moved
2935 // into the function declaration's context afterward.
2936 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2937
2938 // Import the name of this declaration.
2939 DeclarationName Name = Importer.Import(D->getDeclName());
2940 if (D->getDeclName() && !Name)
2941 return nullptr;
2942
2943 // Import the location of this declaration.
2944 SourceLocation Loc = Importer.Import(D->getLocation());
2945
2946 // Import the parameter's type.
2947 QualType T = Importer.Import(D->getType());
2948 if (T.isNull())
2949 return nullptr;
2950
2951 // Create the imported parameter.
2952 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2953 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2954 Importer.Import(D->getInnerLocStart()),
2955 Loc, Name.getAsIdentifierInfo(),
2956 T, TInfo, D->getStorageClass(),
2957 /*DefaultArg*/ nullptr);
2958
2959 // Set the default argument.
2960 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
2961 ToParm->setKNRPromoted(D->isKNRPromoted());
2962
2963 Expr *ToDefArg = nullptr;
2964 Expr *FromDefArg = nullptr;
2965 if (D->hasUninstantiatedDefaultArg()) {
2966 FromDefArg = D->getUninstantiatedDefaultArg();
2967 ToDefArg = Importer.Import(FromDefArg);
2968 ToParm->setUninstantiatedDefaultArg(ToDefArg);
2969 } else if (D->hasUnparsedDefaultArg()) {
2970 ToParm->setUnparsedDefaultArg();
2971 } else if (D->hasDefaultArg()) {
2972 FromDefArg = D->getDefaultArg();
2973 ToDefArg = Importer.Import(FromDefArg);
2974 ToParm->setDefaultArg(ToDefArg);
2975 }
2976 if (FromDefArg && !ToDefArg)
2977 return nullptr;
2978
2979 if (D->isObjCMethodParameter()) {
2980 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
2981 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
2982 } else {
2983 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
2984 D->getFunctionScopeIndex());
2985 }
2986
2987 if (D->isUsed())
2988 ToParm->setIsUsed();
2989
2990 return Importer.Imported(D, ToParm);
2991}
2992
2993Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2994 // Import the major distinguishing characteristics of a method.
2995 DeclContext *DC, *LexicalDC;
2996 DeclarationName Name;
2997 SourceLocation Loc;
2998 NamedDecl *ToD;
2999 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3000 return nullptr;
3001 if (ToD)
3002 return ToD;
3003
3004 SmallVector<NamedDecl *, 2> FoundDecls;
3005 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3006 for (auto *FoundDecl : FoundDecls) {
3007 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
3008 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3009 continue;
3010
3011 // Check return types.
3012 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3013 FoundMethod->getReturnType())) {
3014 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
3015 << D->isInstanceMethod() << Name << D->getReturnType()
3016 << FoundMethod->getReturnType();
3017 Importer.ToDiag(FoundMethod->getLocation(),
3018 diag::note_odr_objc_method_here)
3019 << D->isInstanceMethod() << Name;
3020 return nullptr;
3021 }
3022
3023 // Check the number of parameters.
3024 if (D->param_size() != FoundMethod->param_size()) {
3025 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3026 << D->isInstanceMethod() << Name
3027 << D->param_size() << FoundMethod->param_size();
3028 Importer.ToDiag(FoundMethod->getLocation(),
3029 diag::note_odr_objc_method_here)
3030 << D->isInstanceMethod() << Name;
3031 return nullptr;
3032 }
3033
3034 // Check parameter types.
3035 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
3036 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3037 P != PEnd; ++P, ++FoundP) {
3038 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3039 (*FoundP)->getType())) {
3040 Importer.FromDiag((*P)->getLocation(),
3041 diag::err_odr_objc_method_param_type_inconsistent)
3042 << D->isInstanceMethod() << Name
3043 << (*P)->getType() << (*FoundP)->getType();
3044 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3045 << (*FoundP)->getType();
3046 return nullptr;
3047 }
3048 }
3049
3050 // Check variadic/non-variadic.
3051 // Check the number of parameters.
3052 if (D->isVariadic() != FoundMethod->isVariadic()) {
3053 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3054 << D->isInstanceMethod() << Name;
3055 Importer.ToDiag(FoundMethod->getLocation(),
3056 diag::note_odr_objc_method_here)
3057 << D->isInstanceMethod() << Name;
3058 return nullptr;
3059 }
3060
3061 // FIXME: Any other bits we need to merge?
3062 return Importer.Imported(D, FoundMethod);
3063 }
3064 }
3065
3066 // Import the result type.
3067 QualType ResultTy = Importer.Import(D->getReturnType());
3068 if (ResultTy.isNull())
3069 return nullptr;
3070
3071 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
3072
3073 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
3074 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3075 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3076 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3077 D->getImplementationControl(), D->hasRelatedResultType());
3078
3079 // FIXME: When we decide to merge method definitions, we'll need to
3080 // deal with implicit parameters.
3081
3082 // Import the parameters
3083 SmallVector<ParmVarDecl *, 5> ToParams;
3084 for (auto *FromP : D->parameters()) {
3085 auto *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
3086 if (!ToP)
3087 return nullptr;
3088
3089 ToParams.push_back(ToP);
3090 }
3091
3092 // Set the parameters.
3093 for (auto *ToParam : ToParams) {
3094 ToParam->setOwningFunction(ToMethod);
3095 ToMethod->addDeclInternal(ToParam);
3096 }
3097
3098 SmallVector<SourceLocation, 12> SelLocs;
3099 D->getSelectorLocs(SelLocs);
3100 for (auto &Loc : SelLocs)
3101 Loc = Importer.Import(Loc);
3102
3103 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
3104
3105 ToMethod->setLexicalDeclContext(LexicalDC);
3106 Importer.Imported(D, ToMethod);
3107 LexicalDC->addDeclInternal(ToMethod);
3108 return ToMethod;
3109}
3110
3111Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3112 // Import the major distinguishing characteristics of a category.
3113 DeclContext *DC, *LexicalDC;
3114 DeclarationName Name;
3115 SourceLocation Loc;
3116 NamedDecl *ToD;
3117 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3118 return nullptr;
3119 if (ToD)
3120 return ToD;
3121
3122 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3123 if (!BoundInfo)
3124 return nullptr;
3125
3126 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
3127 Importer.getToContext(), DC,
3128 D->getVariance(),
3129 Importer.Import(D->getVarianceLoc()),
3130 D->getIndex(),
3131 Importer.Import(D->getLocation()),
3132 Name.getAsIdentifierInfo(),
3133 Importer.Import(D->getColonLoc()),
3134 BoundInfo);
3135 Importer.Imported(D, Result);
3136 Result->setLexicalDeclContext(LexicalDC);
3137 return Result;
3138}
3139
3140Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3141 // Import the major distinguishing characteristics of a category.
3142 DeclContext *DC, *LexicalDC;
3143 DeclarationName Name;
3144 SourceLocation Loc;
3145 NamedDecl *ToD;
3146 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3147 return nullptr;
3148 if (ToD)
3149 return ToD;
3150
3151 auto *ToInterface =
3152 cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3153 if (!ToInterface)
3154 return nullptr;
3155
3156 // Determine if we've already encountered this category.
3157 ObjCCategoryDecl *MergeWithCategory
3158 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3159 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3160 if (!ToCategory) {
3161 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
3162 Importer.Import(D->getAtStartLoc()),
3163 Loc,
3164 Importer.Import(D->getCategoryNameLoc()),
3165 Name.getAsIdentifierInfo(),
3166 ToInterface,
3167 /*TypeParamList=*/nullptr,
3168 Importer.Import(D->getIvarLBraceLoc()),
3169 Importer.Import(D->getIvarRBraceLoc()));
3170 ToCategory->setLexicalDeclContext(LexicalDC);
3171 LexicalDC->addDeclInternal(ToCategory);
3172 Importer.Imported(D, ToCategory);
3173 // Import the type parameter list after calling Imported, to avoid
3174 // loops when bringing in their DeclContext.
3175 ToCategory->setTypeParamList(ImportObjCTypeParamList(
3176 D->getTypeParamList()));
3177
3178 // Import protocols
3179 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3180 SmallVector<SourceLocation, 4> ProtocolLocs;
3181 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3182 = D->protocol_loc_begin();
3183 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3184 FromProtoEnd = D->protocol_end();
3185 FromProto != FromProtoEnd;
3186 ++FromProto, ++FromProtoLoc) {
3187 auto *ToProto =
3188 cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3189 if (!ToProto)
3190 return nullptr;
3191 Protocols.push_back(ToProto);
3192 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3193 }
3194
3195 // FIXME: If we're merging, make sure that the protocol list is the same.
3196 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3197 ProtocolLocs.data(), Importer.getToContext());
3198 } else {
3199 Importer.Imported(D, ToCategory);
3200 }
3201
3202 // Import all of the members of this category.
3203 ImportDeclContext(D);
3204
3205 // If we have an implementation, import it as well.
3206 if (D->getImplementation()) {
3207 auto *Impl =
3208 cast_or_null<ObjCCategoryImplDecl>(
3209 Importer.Import(D->getImplementation()));
3210 if (!Impl)
3211 return nullptr;
3212
3213 ToCategory->setImplementation(Impl);
3214 }
3215
3216 return ToCategory;
3217}
3218
3219bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3220 ObjCProtocolDecl *To,
3221 ImportDefinitionKind Kind) {
3222 if (To->getDefinition()) {
3223 if (shouldForceImportDeclContext(Kind))
3224 ImportDeclContext(From);
3225 return false;
3226 }
3227
3228 // Start the protocol definition
3229 To->startDefinition();
3230
3231 // Import protocols
3232 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3233 SmallVector<SourceLocation, 4> ProtocolLocs;
3234 ObjCProtocolDecl::protocol_loc_iterator
3235 FromProtoLoc = From->protocol_loc_begin();
3236 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3237 FromProtoEnd = From->protocol_end();
3238 FromProto != FromProtoEnd;
3239 ++FromProto, ++FromProtoLoc) {
3240 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3241 if (!ToProto)
3242 return true;
3243 Protocols.push_back(ToProto);
3244 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3245 }
3246
3247 // FIXME: If we're merging, make sure that the protocol list is the same.
3248 To->setProtocolList(Protocols.data(), Protocols.size(),
3249 ProtocolLocs.data(), Importer.getToContext());
3250
3251 if (shouldForceImportDeclContext(Kind)) {
3252 // Import all of the members of this protocol.
3253 ImportDeclContext(From, /*ForceImport=*/true);
3254 }
3255 return false;
3256}
3257
3258Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
3259 // If this protocol has a definition in the translation unit we're coming
3260 // from, but this particular declaration is not that definition, import the
3261 // definition and map to that.
3262 ObjCProtocolDecl *Definition = D->getDefinition();
3263 if (Definition && Definition != D) {
3264 Decl *ImportedDef = Importer.Import(Definition);
3265 if (!ImportedDef)
3266 return nullptr;
3267
3268 return Importer.Imported(D, ImportedDef);
3269 }
3270
3271 // Import the major distinguishing characteristics of a protocol.
3272 DeclContext *DC, *LexicalDC;
3273 DeclarationName Name;
3274 SourceLocation Loc;
3275 NamedDecl *ToD;
3276 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3277 return nullptr;
3278 if (ToD)
3279 return ToD;
3280
3281 ObjCProtocolDecl *MergeWithProtocol = nullptr;
3282 SmallVector<NamedDecl *, 2> FoundDecls;
3283 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3284 for (auto *FoundDecl : FoundDecls) {
3285 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
3286 continue;
3287
3288 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
3289 break;
3290 }
3291
3292 ObjCProtocolDecl *ToProto = MergeWithProtocol;
3293 if (!ToProto) {
3294 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3295 Name.getAsIdentifierInfo(), Loc,
3296 Importer.Import(D->getAtStartLoc()),
3297 /*PrevDecl=*/nullptr);
3298 ToProto->setLexicalDeclContext(LexicalDC);
3299 LexicalDC->addDeclInternal(ToProto);
3300 }
3301
3302 Importer.Imported(D, ToProto);
3303
3304 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
3305 return nullptr;
3306
3307 return ToProto;
3308}
3309
3310Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3311 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3312 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3313
3314 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3315 SourceLocation LangLoc = Importer.Import(D->getLocation());
3316
3317 bool HasBraces = D->hasBraces();
3318
3319 LinkageSpecDecl *ToLinkageSpec =
3320 LinkageSpecDecl::Create(Importer.getToContext(),
3321 DC,
3322 ExternLoc,
3323 LangLoc,
3324 D->getLanguage(),
3325 HasBraces);
3326
3327 if (HasBraces) {
3328 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3329 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3330 }
3331
3332 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3333 LexicalDC->addDeclInternal(ToLinkageSpec);
3334
3335 Importer.Imported(D, ToLinkageSpec);
3336
3337 return ToLinkageSpec;
3338}
3339
3340Decl *ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
3341 DeclContext *DC, *LexicalDC;
3342 DeclarationName Name;
3343 SourceLocation Loc;
3344 NamedDecl *ToD = nullptr;
3345 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3346 return nullptr;
3347 if (ToD)
3348 return ToD;
3349
3350 DeclarationNameInfo NameInfo(Name,
3351 Importer.Import(D->getNameInfo().getLoc()));
3352 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3353
3354 UsingDecl *ToUsing = UsingDecl::Create(Importer.getToContext(), DC,
3355 Importer.Import(D->getUsingLoc()),
3356 Importer.Import(D->getQualifierLoc()),
3357 NameInfo, D->hasTypename());
3358 ToUsing->setLexicalDeclContext(LexicalDC);
3359 LexicalDC->addDeclInternal(ToUsing);
3360 Importer.Imported(D, ToUsing);
3361
3362 if (NamedDecl *FromPattern =
3363 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
3364 if (auto *ToPattern =
3365 dyn_cast_or_null<NamedDecl>(Importer.Import(FromPattern)))
3366 Importer.getToContext().setInstantiatedFromUsingDecl(ToUsing, ToPattern);
3367 else
3368 return nullptr;
3369 }
3370
3371 for (auto *FromShadow : D->shadows()) {
3372 if (auto *ToShadow =
3373 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromShadow)))
3374 ToUsing->addShadowDecl(ToShadow);
3375 else
3376 // FIXME: We return a nullptr here but the definition is already created
3377 // and available with lookups. How to fix this?..
3378 return nullptr;
3379 }
3380 return ToUsing;
3381}
3382
3383Decl *ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
3384 DeclContext *DC, *LexicalDC;
3385 DeclarationName Name;
3386 SourceLocation Loc;
3387 NamedDecl *ToD = nullptr;
3388 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3389 return nullptr;
3390 if (ToD)
3391 return ToD;
3392
3393 auto *ToUsing = dyn_cast_or_null<UsingDecl>(
3394 Importer.Import(D->getUsingDecl()));
3395 if (!ToUsing)
3396 return nullptr;
3397
3398 auto *ToTarget = dyn_cast_or_null<NamedDecl>(
3399 Importer.Import(D->getTargetDecl()));
3400 if (!ToTarget)
3401 return nullptr;
3402
3403 UsingShadowDecl *ToShadow = UsingShadowDecl::Create(
3404 Importer.getToContext(), DC, Loc, ToUsing, ToTarget);
3405
3406 ToShadow->setLexicalDeclContext(LexicalDC);
3407 ToShadow->setAccess(D->getAccess());
3408 Importer.Imported(D, ToShadow);
3409
3410 if (UsingShadowDecl *FromPattern =
3411 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
3412 if (auto *ToPattern =
3413 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromPattern)))
3414 Importer.getToContext().setInstantiatedFromUsingShadowDecl(ToShadow,
3415 ToPattern);
3416 else
3417 // FIXME: We return a nullptr here but the definition is already created
3418 // and available with lookups. How to fix this?..
3419 return nullptr;
3420 }
3421
3422 LexicalDC->addDeclInternal(ToShadow);
3423
3424 return ToShadow;
3425}
3426
3427Decl *ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
3428 DeclContext *DC, *LexicalDC;
3429 DeclarationName Name;
3430 SourceLocation Loc;
3431 NamedDecl *ToD = nullptr;
3432 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3433 return nullptr;
3434 if (ToD)
3435 return ToD;
3436
3437 DeclContext *ToComAncestor = Importer.ImportContext(D->getCommonAncestor());
3438 if (!ToComAncestor)
3439 return nullptr;
3440
3441 auto *ToNominated = cast_or_null<NamespaceDecl>(
3442 Importer.Import(D->getNominatedNamespace()));
3443 if (!ToNominated)
3444 return nullptr;
3445
3446 UsingDirectiveDecl *ToUsingDir = UsingDirectiveDecl::Create(
3447 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3448 Importer.Import(D->getNamespaceKeyLocation()),
3449 Importer.Import(D->getQualifierLoc()),
3450 Importer.Import(D->getIdentLocation()), ToNominated, ToComAncestor);
3451 ToUsingDir->setLexicalDeclContext(LexicalDC);
3452 LexicalDC->addDeclInternal(ToUsingDir);
3453 Importer.Imported(D, ToUsingDir);
3454
3455 return ToUsingDir;
3456}
3457
3458Decl *ASTNodeImporter::VisitUnresolvedUsingValueDecl(
3459 UnresolvedUsingValueDecl *D) {
3460 DeclContext *DC, *LexicalDC;
3461 DeclarationName Name;
3462 SourceLocation Loc;
3463 NamedDecl *ToD = nullptr;
3464 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3465 return nullptr;
3466 if (ToD)
3467 return ToD;
3468
3469 DeclarationNameInfo NameInfo(Name, Importer.Import(D->getNameInfo().getLoc()));
3470 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3471
3472 UnresolvedUsingValueDecl *ToUsingValue = UnresolvedUsingValueDecl::Create(
3473 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3474 Importer.Import(D->getQualifierLoc()), NameInfo,
3475 Importer.Import(D->getEllipsisLoc()));
3476
3477 Importer.Imported(D, ToUsingValue);
3478 ToUsingValue->setAccess(D->getAccess());
3479 ToUsingValue->setLexicalDeclContext(LexicalDC);
3480 LexicalDC->addDeclInternal(ToUsingValue);
3481
3482 return ToUsingValue;
3483}
3484
3485Decl *ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
3486 UnresolvedUsingTypenameDecl *D) {
3487 DeclContext *DC, *LexicalDC;
3488 DeclarationName Name;
3489 SourceLocation Loc;
3490 NamedDecl *ToD = nullptr;
3491 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3492 return nullptr;
3493 if (ToD)
3494 return ToD;
3495
3496 UnresolvedUsingTypenameDecl *ToUsing = UnresolvedUsingTypenameDecl::Create(
3497 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3498 Importer.Import(D->getTypenameLoc()),
3499 Importer.Import(D->getQualifierLoc()), Loc, Name,
3500 Importer.Import(D->getEllipsisLoc()));
3501
3502 Importer.Imported(D, ToUsing);
3503 ToUsing->setAccess(D->getAccess());
3504 ToUsing->setLexicalDeclContext(LexicalDC);
3505 LexicalDC->addDeclInternal(ToUsing);
3506
3507 return ToUsing;
3508}
3509
3510bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3511 ObjCInterfaceDecl *To,
3512 ImportDefinitionKind Kind) {
3513 if (To->getDefinition()) {
3514 // Check consistency of superclass.
3515 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3516 if (FromSuper) {
3517 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3518 if (!FromSuper)
3519 return true;
3520 }
3521
3522 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3523 if ((bool)FromSuper != (bool)ToSuper ||
3524 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3525 Importer.ToDiag(To->getLocation(),
3526 diag::err_odr_objc_superclass_inconsistent)
3527 << To->getDeclName();
3528 if (ToSuper)
3529 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3530 << To->getSuperClass()->getDeclName();
3531 else
3532 Importer.ToDiag(To->getLocation(),
3533 diag::note_odr_objc_missing_superclass);
3534 if (From->getSuperClass())
3535 Importer.FromDiag(From->getSuperClassLoc(),
3536 diag::note_odr_objc_superclass)
3537 << From->getSuperClass()->getDeclName();
3538 else
3539 Importer.FromDiag(From->getLocation(),
3540 diag::note_odr_objc_missing_superclass);
3541 }
3542
3543 if (shouldForceImportDeclContext(Kind))
3544 ImportDeclContext(From);
3545 return false;
3546 }
3547
3548 // Start the definition.
3549 To->startDefinition();
3550
3551 // If this class has a superclass, import it.
3552 if (From->getSuperClass()) {
3553 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3554 if (!SuperTInfo)
3555 return true;
3556
3557 To->setSuperClass(SuperTInfo);
3558 }
3559
3560 // Import protocols
3561 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3562 SmallVector<SourceLocation, 4> ProtocolLocs;
3563 ObjCInterfaceDecl::protocol_loc_iterator
3564 FromProtoLoc = From->protocol_loc_begin();
3565
3566 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3567 FromProtoEnd = From->protocol_end();
3568 FromProto != FromProtoEnd;
3569 ++FromProto, ++FromProtoLoc) {
3570 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3571 if (!ToProto)
3572 return true;
3573 Protocols.push_back(ToProto);
3574 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3575 }
3576
3577 // FIXME: If we're merging, make sure that the protocol list is the same.
3578 To->setProtocolList(Protocols.data(), Protocols.size(),
3579 ProtocolLocs.data(), Importer.getToContext());
3580
3581 // Import categories. When the categories themselves are imported, they'll
3582 // hook themselves into this interface.
3583 for (auto *Cat : From->known_categories())
3584 Importer.Import(Cat);
3585
3586 // If we have an @implementation, import it as well.
3587 if (From->getImplementation()) {
3588 auto *Impl = cast_or_null<ObjCImplementationDecl>(
3589 Importer.Import(From->getImplementation()));
3590 if (!Impl)
3591 return true;
3592
3593 To->setImplementation(Impl);
3594 }
3595
3596 if (shouldForceImportDeclContext(Kind)) {
3597 // Import all of the members of this class.
3598 ImportDeclContext(From, /*ForceImport=*/true);
3599 }
3600 return false;
3601}
3602
3603ObjCTypeParamList *
3604ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3605 if (!list)
3606 return nullptr;
3607
3608 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
3609 for (auto fromTypeParam : *list) {
3610 auto *toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3611 Importer.Import(fromTypeParam));
3612 if (!toTypeParam)
3613 return nullptr;
3614
3615 toTypeParams.push_back(toTypeParam);
3616 }
3617
3618 return ObjCTypeParamList::create(Importer.getToContext(),
3619 Importer.Import(list->getLAngleLoc()),
3620 toTypeParams,
3621 Importer.Import(list->getRAngleLoc()));
3622}
3623
3624Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
3625 // If this class has a definition in the translation unit we're coming from,
3626 // but this particular declaration is not that definition, import the
3627 // definition and map to that.
3628 ObjCInterfaceDecl *Definition = D->getDefinition();
3629 if (Definition && Definition != D) {
3630 Decl *ImportedDef = Importer.Import(Definition);
3631 if (!ImportedDef)
3632 return nullptr;
3633
3634 return Importer.Imported(D, ImportedDef);
3635 }
3636
3637 // Import the major distinguishing characteristics of an @interface.
3638 DeclContext *DC, *LexicalDC;
3639 DeclarationName Name;
3640 SourceLocation Loc;
3641 NamedDecl *ToD;
3642 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3643 return nullptr;
3644 if (ToD)
3645 return ToD;
3646
3647 // Look for an existing interface with the same name.
3648 ObjCInterfaceDecl *MergeWithIface = nullptr;
3649 SmallVector<NamedDecl *, 2> FoundDecls;
3650 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3651 for (auto *FoundDecl : FoundDecls) {
3652 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3653 continue;
3654
3655 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
3656 break;
3657 }
3658
3659 // Create an interface declaration, if one does not already exist.
3660 ObjCInterfaceDecl *ToIface = MergeWithIface;
3661 if (!ToIface) {
3662 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3663 Importer.Import(D->getAtStartLoc()),
3664 Name.getAsIdentifierInfo(),
3665 /*TypeParamList=*/nullptr,
3666 /*PrevDecl=*/nullptr, Loc,
3667 D->isImplicitInterfaceDecl());
3668 ToIface->setLexicalDeclContext(LexicalDC);
3669 LexicalDC->addDeclInternal(ToIface);
3670 }
3671 Importer.Imported(D, ToIface);
3672 // Import the type parameter list after calling Imported, to avoid
3673 // loops when bringing in their DeclContext.
3674 ToIface->setTypeParamList(ImportObjCTypeParamList(
3675 D->getTypeParamListAsWritten()));
3676
3677 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
3678 return nullptr;
3679
3680 return ToIface;
3681}
3682
3683Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3684 auto *Category = cast_or_null<ObjCCategoryDecl>(
3685 Importer.Import(D->getCategoryDecl()));
3686 if (!Category)
3687 return nullptr;
3688
3689 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3690 if (!ToImpl) {
3691 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3692 if (!DC)
3693 return nullptr;
3694
3695 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
3696 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
3697 Importer.Import(D->getIdentifier()),
3698 Category->getClassInterface(),
3699 Importer.Import(D->getLocation()),
3700 Importer.Import(D->getAtStartLoc()),
3701 CategoryNameLoc);
3702
3703 DeclContext *LexicalDC = DC;
3704 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3705 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3706 if (!LexicalDC)
3707 return nullptr;
3708
3709 ToImpl->setLexicalDeclContext(LexicalDC);
3710 }
3711
3712 LexicalDC->addDeclInternal(ToImpl);
3713 Category->setImplementation(ToImpl);
3714 }
3715
3716 Importer.Imported(D, ToImpl);
3717 ImportDeclContext(D);
3718 return ToImpl;
3719}
3720
3721Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3722 // Find the corresponding interface.
3723 auto *Iface = cast_or_null<ObjCInterfaceDecl>(
3724 Importer.Import(D->getClassInterface()));
3725 if (!Iface)
3726 return nullptr;
3727
3728 // Import the superclass, if any.
3729 ObjCInterfaceDecl *Super = nullptr;
3730 if (D->getSuperClass()) {
3731 Super = cast_or_null<ObjCInterfaceDecl>(
3732 Importer.Import(D->getSuperClass()));
3733 if (!Super)
3734 return nullptr;
3735 }
3736
3737 ObjCImplementationDecl *Impl = Iface->getImplementation();
3738 if (!Impl) {
3739 // We haven't imported an implementation yet. Create a new @implementation
3740 // now.
3741 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3742 Importer.ImportContext(D->getDeclContext()),
3743 Iface, Super,
3744 Importer.Import(D->getLocation()),
3745 Importer.Import(D->getAtStartLoc()),
3746 Importer.Import(D->getSuperClassLoc()),
3747 Importer.Import(D->getIvarLBraceLoc()),
3748 Importer.Import(D->getIvarRBraceLoc()));
3749
3750 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3751 DeclContext *LexicalDC
3752 = Importer.ImportContext(D->getLexicalDeclContext());
3753 if (!LexicalDC)
3754 return nullptr;
3755 Impl->setLexicalDeclContext(LexicalDC);
3756 }
3757
3758 // Associate the implementation with the class it implements.
3759 Iface->setImplementation(Impl);
3760 Importer.Imported(D, Iface->getImplementation());
3761 } else {
3762 Importer.Imported(D, Iface->getImplementation());
3763
3764 // Verify that the existing @implementation has the same superclass.
3765 if ((Super && !Impl->getSuperClass()) ||
3766 (!Super && Impl->getSuperClass()) ||
3767 (Super && Impl->getSuperClass() &&
3768 !declaresSameEntity(Super->getCanonicalDecl(),
3769 Impl->getSuperClass()))) {
3770 Importer.ToDiag(Impl->getLocation(),
3771 diag::err_odr_objc_superclass_inconsistent)
3772 << Iface->getDeclName();
3773 // FIXME: It would be nice to have the location of the superclass
3774 // below.
3775 if (Impl->getSuperClass())
3776 Importer.ToDiag(Impl->getLocation(),
3777 diag::note_odr_objc_superclass)
3778 << Impl->getSuperClass()->getDeclName();
3779 else
3780 Importer.ToDiag(Impl->getLocation(),
3781 diag::note_odr_objc_missing_superclass);
3782 if (D->getSuperClass())
3783 Importer.FromDiag(D->getLocation(),
3784 diag::note_odr_objc_superclass)
3785 << D->getSuperClass()->getDeclName();
3786 else
3787 Importer.FromDiag(D->getLocation(),
3788 diag::note_odr_objc_missing_superclass);
3789 return nullptr;
3790 }
3791 }
3792
3793 // Import all of the members of this @implementation.
3794 ImportDeclContext(D);
3795
3796 return Impl;
3797}
3798
3799Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3800 // Import the major distinguishing characteristics of an @property.
3801 DeclContext *DC, *LexicalDC;
3802 DeclarationName Name;
3803 SourceLocation Loc;
3804 NamedDecl *ToD;
3805 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3806 return nullptr;
3807 if (ToD)
3808 return ToD;
3809
3810 // Check whether we have already imported this property.
3811 SmallVector<NamedDecl *, 2> FoundDecls;
3812 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3813 for (auto *FoundDecl : FoundDecls) {
3814 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
3815 // Check property types.
3816 if (!Importer.IsStructurallyEquivalent(D->getType(),
3817 FoundProp->getType())) {
3818 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3819 << Name << D->getType() << FoundProp->getType();
3820 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3821 << FoundProp->getType();
3822 return nullptr;
3823 }
3824
3825 // FIXME: Check property attributes, getters, setters, etc.?
3826
3827 // Consider these properties to be equivalent.
3828 Importer.Imported(D, FoundProp);
3829 return FoundProp;
3830 }
3831 }
3832
3833 // Import the type.
3834 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
3835 if (!TSI)
3836 return nullptr;
3837
3838 // Create the new property.
3839 ObjCPropertyDecl *ToProperty
3840 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3841 Name.getAsIdentifierInfo(),
3842 Importer.Import(D->getAtLoc()),
3843 Importer.Import(D->getLParenLoc()),
3844 Importer.Import(D->getType()),
3845 TSI,
3846 D->getPropertyImplementation());
3847 Importer.Imported(D, ToProperty);
3848 ToProperty->setLexicalDeclContext(LexicalDC);
3849 LexicalDC->addDeclInternal(ToProperty);
3850
3851 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
3852 ToProperty->setPropertyAttributesAsWritten(
3853 D->getPropertyAttributesAsWritten());
3854 ToProperty->setGetterName(Importer.Import(D->getGetterName()),
3855 Importer.Import(D->getGetterNameLoc()));
3856 ToProperty->setSetterName(Importer.Import(D->getSetterName()),
3857 Importer.Import(D->getSetterNameLoc()));
3858 ToProperty->setGetterMethodDecl(
3859 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3860 ToProperty->setSetterMethodDecl(
3861 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3862 ToProperty->setPropertyIvarDecl(
3863 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3864 return ToProperty;
3865}
3866
3867Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3868 auto *Property = cast_or_null<ObjCPropertyDecl>(
3869 Importer.Import(D->getPropertyDecl()));
3870 if (!Property)
18
Assuming 'Property' is non-null
19
Taking false branch
3871 return nullptr;
3872
3873 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3874 if (!DC)
20
Assuming 'DC' is non-null
21
Taking false branch
3875 return nullptr;
3876
3877 // Import the lexical declaration context.
3878 DeclContext *LexicalDC = DC;
3879 if (D->getDeclContext() != D->getLexicalDeclContext()) {
22
Assuming the condition is false
23
Taking false branch
3880 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3881 if (!LexicalDC)
3882 return nullptr;
3883 }
3884
3885 auto *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3886 if (!InImpl)
24
Assuming 'InImpl' is non-null
25
Taking false branch
3887 return nullptr;
3888
3889 // Import the ivar (for an @synthesize).
3890 ObjCIvarDecl *Ivar = nullptr;
26
'Ivar' initialized to a null pointer value
3891 if (D->getPropertyIvarDecl()) {
27
Assuming the condition is false
28
Taking false branch
3892 Ivar = cast_or_null<ObjCIvarDecl>(
3893 Importer.Import(D->getPropertyIvarDecl()));
3894 if (!Ivar)
3895 return nullptr;
3896 }
3897
3898 ObjCPropertyImplDecl *ToImpl
3899 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
3900 Property->getQueryKind());
3901 if (!ToImpl) {
29
Assuming 'ToImpl' is non-null
30
Taking false branch
3902 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3903 Importer.Import(D->getLocStart()),
3904 Importer.Import(D->getLocation()),
3905 Property,
3906 D->getPropertyImplementation(),
3907 Ivar,
3908 Importer.Import(D->getPropertyIvarDeclLoc()));
3909 ToImpl->setLexicalDeclContext(LexicalDC);
3910 Importer.Imported(D, ToImpl);
3911 LexicalDC->addDeclInternal(ToImpl);
3912 } else {
3913 // Check that we have the same kind of property implementation (@synthesize
3914 // vs. @dynamic).
3915 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
31
Taking false branch
3916 Importer.ToDiag(ToImpl->getLocation(),
3917 diag::err_odr_objc_property_impl_kind_inconsistent)
3918 << Property->getDeclName()
3919 << (ToImpl->getPropertyImplementation()
3920 == ObjCPropertyImplDecl::Dynamic);
3921 Importer.FromDiag(D->getLocation(),
3922 diag::note_odr_objc_property_impl_kind)
3923 << D->getPropertyDecl()->getDeclName()
3924 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3925 return nullptr;
3926 }
3927
3928 // For @synthesize, check that we have the same
3929 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
32
Assuming the condition is true
34
Taking true branch
3930 Ivar != ToImpl->getPropertyIvarDecl()) {
33
Assuming the condition is true
3931 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3932 diag::err_odr_objc_synthesize_ivar_inconsistent)
3933 << Property->getDeclName()
3934 << ToImpl->getPropertyIvarDecl()->getDeclName()
3935 << Ivar->getDeclName();
35
Called C++ object pointer is null
3936 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3937 diag::note_odr_objc_synthesize_ivar_here)
3938 << D->getPropertyIvarDecl()->getDeclName();
3939 return nullptr;
3940 }
3941
3942 // Merge the existing implementation with the new implementation.
3943 Importer.Imported(D, ToImpl);
3944 }
3945
3946 return ToImpl;
3947}
3948
3949Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3950 // For template arguments, we adopt the translation unit as our declaration
3951 // context. This context will be fixed when the actual template declaration
3952 // is created.
3953
3954 // FIXME: Import default argument.
3955 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3956 Importer.getToContext().getTranslationUnitDecl(),
3957 Importer.Import(D->getLocStart()),
3958 Importer.Import(D->getLocation()),
3959 D->getDepth(),
3960 D->getIndex(),
3961 Importer.Import(D->getIdentifier()),
3962 D->wasDeclaredWithTypename(),
3963 D->isParameterPack());
3964}
3965
3966Decl *
3967ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3968 // Import the name of this declaration.
3969 DeclarationName Name = Importer.Import(D->getDeclName());
3970 if (D->getDeclName() && !Name)
3971 return nullptr;
3972
3973 // Import the location of this declaration.
3974 SourceLocation Loc = Importer.Import(D->getLocation());
3975
3976 // Import the type of this declaration.
3977 QualType T = Importer.Import(D->getType());
3978 if (T.isNull())
3979 return nullptr;
3980
3981 // Import type-source information.
3982 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3983 if (D->getTypeSourceInfo() && !TInfo)
3984 return nullptr;
3985
3986 // FIXME: Import default argument.
3987
3988 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3989 Importer.getToContext().getTranslationUnitDecl(),
3990 Importer.Import(D->getInnerLocStart()),
3991 Loc, D->getDepth(), D->getPosition(),
3992 Name.getAsIdentifierInfo(),
3993 T, D->isParameterPack(), TInfo);
3994}
3995
3996Decl *
3997ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3998 // Import the name of this declaration.
3999 DeclarationName Name = Importer.Import(D->getDeclName());
4000 if (D->getDeclName() && !Name)
4001 return nullptr;
4002
4003 // Import the location of this declaration.
4004 SourceLocation Loc = Importer.Import(D->getLocation());
4005
4006 // Import template parameters.
4007 TemplateParameterList *TemplateParams
4008 = ImportTemplateParameterList(D->getTemplateParameters());
4009 if (!TemplateParams)
4010 return nullptr;
4011
4012 // FIXME: Import default argument.
4013
4014 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
4015 Importer.getToContext().getTranslationUnitDecl(),
4016 Loc, D->getDepth(), D->getPosition(),
4017 D->isParameterPack(),
4018 Name.getAsIdentifierInfo(),
4019 TemplateParams);
4020}
4021
4022Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
4023 // If this record has a definition in the translation unit we're coming from,
4024 // but this particular declaration is not that definition, import the
4025 // definition and map to that.
4026 auto *Definition =
4027 cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
4028 if (Definition && Definition != D->getTemplatedDecl()) {
4029 Decl *ImportedDef
4030 = Importer.Import(Definition->getDescribedClassTemplate());
4031 if (!ImportedDef)
4032 return nullptr;
4033
4034 return Importer.Imported(D, ImportedDef);
4035 }
4036
4037 // Import the major distinguishing characteristics of this class template.
4038 DeclContext *DC, *LexicalDC;
4039 DeclarationName Name;
4040 SourceLocation Loc;
4041 NamedDecl *ToD;
4042 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4043 return nullptr;
4044 if (ToD)
4045 return ToD;
4046
4047 // We may already have a template of the same name; try to find and match it.
4048 if (!DC->isFunctionOrMethod()) {
4049 SmallVector<NamedDecl *, 4> ConflictingDecls;
4050 SmallVector<NamedDecl *, 2> FoundDecls;
4051 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
4052 for (auto *FoundDecl : FoundDecls) {
4053 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4054 continue;
4055
4056 Decl *Found = FoundDecl;
4057 if (auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found)) {
4058 if (IsStructuralMatch(D, FoundTemplate)) {
4059 // The class templates structurally match; call it the same template.
4060 // FIXME: We may be filling in a forward declaration here. Handle
4061 // this case!
4062 Importer.Imported(D->getTemplatedDecl(),
4063 FoundTemplate->getTemplatedDecl());
4064 return Importer.Imported(D, FoundTemplate);
4065 }
4066 }
4067
4068 ConflictingDecls.push_back(FoundDecl);
4069 }
4070
4071 if (!ConflictingDecls.empty()) {
4072 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4073 ConflictingDecls.data(),
4074 ConflictingDecls.size());
4075 }
4076
4077 if (!Name)
4078 return nullptr;
4079 }
4080
4081 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
4082
4083 // Create the declaration that is being templated.
4084 auto *ToTemplated = cast_or_null<CXXRecordDecl>(
4085 Importer.Import(FromTemplated));
4086 if (!ToTemplated)
4087 return nullptr;
4088
4089 // Resolve possible cyclic import.
4090 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
4091 return AlreadyImported;
4092
4093 // Create the class template declaration itself.
4094 TemplateParameterList *TemplateParams =
4095 ImportTemplateParameterList(D->getTemplateParameters());
4096 if (!TemplateParams)
4097 return nullptr;
4098
4099 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4100 Loc, Name, TemplateParams,
4101 ToTemplated);
4102 ToTemplated->setDescribedClassTemplate(D2);
4103
4104 D2->setAccess(D->getAccess());
4105 D2->setLexicalDeclContext(LexicalDC);
4106 LexicalDC->addDeclInternal(D2);
4107
4108 // Note the relationship between the class templates.
4109 Importer.Imported(D, D2);
4110 Importer.Imported(FromTemplated, ToTemplated);
4111
4112 if (FromTemplated->isCompleteDefinition() &&
4113 !ToTemplated->isCompleteDefinition()) {
4114 // FIXME: Import definition!
4115 }
4116
4117 return D2;
4118}
4119
4120Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4121 ClassTemplateSpecializationDecl *D) {
4122 // If this record has a definition in the translation unit we're coming from,
4123 // but this particular declaration is not that definition, import the
4124 // definition and map to that.
4125 TagDecl *Definition = D->getDefinition();
4126 if (Definition && Definition != D) {
4127 Decl *ImportedDef = Importer.Import(Definition);
4128 if (!ImportedDef)
4129 return nullptr;
4130
4131 return Importer.Imported(D, ImportedDef);
4132 }
4133
4134 auto *ClassTemplate =
4135 cast_or_null<ClassTemplateDecl>(Importer.Import(
4136 D->getSpecializedTemplate()));
4137 if (!ClassTemplate)
4138 return nullptr;
4139
4140 // Import the context of this declaration.
4141 DeclContext *DC = ClassTemplate->getDeclContext();
4142 if (!DC)
4143 return nullptr;
4144
4145 DeclContext *LexicalDC = DC;
4146 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4147 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4148 if (!LexicalDC)
4149 return nullptr;
4150 }
4151
4152 // Import the location of this declaration.
4153 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4154 SourceLocation IdLoc = Importer.Import(D->getLocation());
4155
4156 // Import template arguments.
4157 SmallVector<TemplateArgument, 2> TemplateArgs;
4158 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4159 D->getTemplateArgs().size(),
4160 TemplateArgs))
4161 return nullptr;
4162
4163 // Try to find an existing specialization with these template arguments.
4164 void *InsertPos = nullptr;
4165 ClassTemplateSpecializationDecl *D2
4166 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
4167 if (D2) {
4168 // We already have a class template specialization with these template
4169 // arguments.
4170
4171 // FIXME: Check for specialization vs. instantiation errors.
4172
4173 if (RecordDecl *FoundDef = D2->getDefinition()) {
4174 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
4175 // The record types structurally match, or the "from" translation
4176 // unit only had a forward declaration anyway; call it the same
4177 // function.
4178 return Importer.Imported(D, FoundDef);
4179 }
4180 }
4181 } else {
4182 // Create a new specialization.
4183 if (auto *PartialSpec =
4184 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
4185 // Import TemplateArgumentListInfo
4186 TemplateArgumentListInfo ToTAInfo;
4187 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
4188 if (ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
4189 return nullptr;
4190
4191 QualType CanonInjType = Importer.Import(
4192 PartialSpec->getInjectedSpecializationType());
4193 if (CanonInjType.isNull())
4194 return nullptr;
4195 CanonInjType = CanonInjType.getCanonicalType();
4196
4197 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4198 PartialSpec->getTemplateParameters());
4199 if (!ToTPList && PartialSpec->getTemplateParameters())
4200 return nullptr;
4201
4202 D2 = ClassTemplatePartialSpecializationDecl::Create(
4203 Importer.getToContext(), D->getTagKind(), DC, StartLoc, IdLoc,
4204 ToTPList, ClassTemplate,
4205 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
4206 ToTAInfo, CanonInjType, nullptr);
4207
4208 } else {
4209 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4210 D->getTagKind(), DC,
4211 StartLoc, IdLoc,
4212 ClassTemplate,
4213 TemplateArgs,
4214 /*PrevDecl=*/nullptr);
4215 }
4216
4217 D2->setSpecializationKind(D->getSpecializationKind());
4218
4219 // Add this specialization to the class template.
4220 ClassTemplate->AddSpecialization(D2, InsertPos);
4221
4222 // Import the qualifier, if any.
4223 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4224
4225 Importer.Imported(D, D2);
4226
4227 if (auto *TSI = D->getTypeAsWritten()) {
4228 TypeSourceInfo *TInfo = Importer.Import(TSI);
4229 if (!TInfo)
4230 return nullptr;
4231 D2->setTypeAsWritten(TInfo);
4232 D2->setTemplateKeywordLoc(Importer.Import(D->getTemplateKeywordLoc()));
4233 D2->setExternLoc(Importer.Import(D->getExternLoc()));
4234 }
4235
4236 SourceLocation POI = Importer.Import(D->getPointOfInstantiation());
4237 if (POI.isValid())
4238 D2->setPointOfInstantiation(POI);
4239 else if (D->getPointOfInstantiation().isValid())
4240 return nullptr;
4241
4242 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
4243
4244 // Add the specialization to this context.
4245 D2->setLexicalDeclContext(LexicalDC);
4246 LexicalDC->addDeclInternal(D2);
4247 }
4248 Importer.Imported(D, D2);
4249 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
4250 return nullptr;
4251
4252 return D2;
4253}
4254
4255Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4256 // If this variable has a definition in the translation unit we're coming
4257 // from,
4258 // but this particular declaration is not that definition, import the
4259 // definition and map to that.
4260 auto *Definition =
4261 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4262 if (Definition && Definition != D->getTemplatedDecl()) {
4263 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4264 if (!ImportedDef)
4265 return nullptr;
4266
4267 return Importer.Imported(D, ImportedDef);
4268 }
4269
4270 // Import the major distinguishing characteristics of this variable template.
4271 DeclContext *DC, *LexicalDC;
4272 DeclarationName Name;
4273 SourceLocation Loc;
4274 NamedDecl *ToD;
4275 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4276 return nullptr;
4277 if (ToD)
4278 return ToD;
4279
4280 // We may already have a template of the same name; try to find and match it.
4281 assert(!DC->isFunctionOrMethod() &&(static_cast <bool> (!DC->isFunctionOrMethod() &&
"Variable templates cannot be declared at function scope") ?
void (0) : __assert_fail ("!DC->isFunctionOrMethod() && \"Variable templates cannot be declared at function scope\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/AST/ASTImporter.cpp"
, 4282, __extension__ __PRETTY_FUNCTION__))
4282 "Variable templates cannot be declared at function scope")(static_cast <bool> (!DC->isFunctionOrMethod() &&
"Variable templates cannot be declared at function scope") ?
void (0) : __assert_fail ("!DC->isFunctionOrMethod() && \"Variable templates cannot be declared at function scope\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/AST/ASTImporter.cpp"
, 4282, __extension__ __PRETTY_FUNCTION__))
;
4283 SmallVector<NamedDecl *, 4> ConflictingDecls;
4284 SmallVector<NamedDecl *, 2> FoundDecls;
4285 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
4286 for (auto *FoundDecl : FoundDecls) {
4287 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4288 continue;
4289
4290 Decl *Found = FoundDecl;
4291 if (auto *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
4292 if (IsStructuralMatch(D, FoundTemplate)) {
4293 // The variable templates structurally match; call it the same template.
4294 Importer.Imported(D->getTemplatedDecl(),
4295 FoundTemplate->getTemplatedDecl());
4296 return Importer.Imported(D, FoundTemplate);
4297 }
4298 }
4299
4300 ConflictingDecls.push_back(FoundDecl);
4301 }
4302
4303 if (!ConflictingDecls.empty()) {
4304 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4305 ConflictingDecls.data(),
4306 ConflictingDecls.size());
4307 }
4308
4309 if (!Name)
4310 return nullptr;
4311
4312 VarDecl *DTemplated = D->getTemplatedDecl();
4313
4314 // Import the type.
4315 QualType T = Importer.Import(DTemplated->getType());
4316 if (T.isNull())
4317 return nullptr;
4318
4319 // Create the declaration that is being templated.
4320 auto *ToTemplated = dyn_cast_or_null<VarDecl>(Importer.Import(DTemplated));
4321 if (!ToTemplated)
4322 return nullptr;
4323
4324 // Create the variable template declaration itself.
4325 TemplateParameterList *TemplateParams =
4326 ImportTemplateParameterList(D->getTemplateParameters());
4327 if (!TemplateParams)
4328 return nullptr;
4329
4330 VarTemplateDecl *ToVarTD = VarTemplateDecl::Create(
4331 Importer.getToContext(), DC, Loc, Name, TemplateParams, ToTemplated);
4332 ToTemplated->setDescribedVarTemplate(ToVarTD);
4333
4334 ToVarTD->setAccess(D->getAccess());
4335 ToVarTD->setLexicalDeclContext(LexicalDC);
4336 LexicalDC->addDeclInternal(ToVarTD);
4337
4338 // Note the relationship between the variable templates.
4339 Importer.Imported(D, ToVarTD);
4340 Importer.Imported(DTemplated, ToTemplated);
4341
4342 if (DTemplated->isThisDeclarationADefinition() &&
4343 !ToTemplated->isThisDeclarationADefinition()) {
4344 // FIXME: Import definition!
4345 }
4346
4347 return ToVarTD;
4348}
4349
4350Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4351 VarTemplateSpecializationDecl *D) {
4352 // If this record has a definition in the translation unit we're coming from,
4353 // but this particular declaration is not that definition, import the
4354 // definition and map to that.
4355 VarDecl *Definition = D->getDefinition();
4356 if (Definition && Definition != D) {
4357 Decl *ImportedDef = Importer.Import(Definition);
4358 if (!ImportedDef)
4359 return nullptr;
4360
4361 return Importer.Imported(D, ImportedDef);
4362 }
4363
4364 auto *VarTemplate = cast_or_null<VarTemplateDecl>(
4365 Importer.Import(D->getSpecializedTemplate()));
4366 if (!VarTemplate)
4367 return nullptr;
4368
4369 // Import the context of this declaration.
4370 DeclContext *DC = VarTemplate->getDeclContext();
4371 if (!DC)
4372 return nullptr;
4373
4374 DeclContext *LexicalDC = DC;
4375 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4376 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4377 if (!LexicalDC)
4378 return nullptr;
4379 }
4380
4381 // Import the location of this declaration.
4382 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4383 SourceLocation IdLoc = Importer.Import(D->getLocation());
4384
4385 // Import template arguments.
4386 SmallVector<TemplateArgument, 2> TemplateArgs;
4387 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4388 D->getTemplateArgs().size(), TemplateArgs))
4389 return nullptr;
4390
4391 // Try to find an existing specialization with these template arguments.
4392 void *InsertPos = nullptr;
4393 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
4394 TemplateArgs, InsertPos);
4395 if (D2) {
4396 // We already have a variable template specialization with these template
4397 // arguments.
4398
4399 // FIXME: Check for specialization vs. instantiation errors.
4400
4401 if (VarDecl *FoundDef = D2->getDefinition()) {
4402 if (!D->isThisDeclarationADefinition() ||
4403 IsStructuralMatch(D, FoundDef)) {
4404 // The record types structurally match, or the "from" translation
4405 // unit only had a forward declaration anyway; call it the same
4406 // variable.
4407 return Importer.Imported(D, FoundDef);
4408 }
4409 }
4410 } else {
4411 // Import the type.
4412 QualType T = Importer.Import(D->getType());
4413 if (T.isNull())
4414 return nullptr;
4415
4416 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4417 if (D->getTypeSourceInfo() && !TInfo)
4418 return nullptr;
4419
4420 TemplateArgumentListInfo ToTAInfo;
4421 if (ImportTemplateArgumentListInfo(D->getTemplateArgsInfo(), ToTAInfo))
4422 return nullptr;
4423
4424 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
4425 // Create a new specialization.
4426 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
4427 // Import TemplateArgumentListInfo
4428 TemplateArgumentListInfo ArgInfos;
4429 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
4430 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
4431 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ArgInfos))
4432 return nullptr;
4433
4434 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4435 FromPartial->getTemplateParameters());
4436 if (!ToTPList)
4437 return nullptr;
4438
4439 auto *ToPartial = PartVarSpecDecl::Create(
4440 Importer.getToContext(), DC, StartLoc, IdLoc, ToTPList, VarTemplate,
4441 T, TInfo, D->getStorageClass(), TemplateArgs, ArgInfos);
4442
4443 auto *FromInst = FromPartial->getInstantiatedFromMember();
4444 auto *ToInst = cast_or_null<PartVarSpecDecl>(Importer.Import(FromInst));
4445 if (FromInst && !ToInst)
4446 return nullptr;
4447
4448 ToPartial->setInstantiatedFromMember(ToInst);
4449 if (FromPartial->isMemberSpecialization())
4450 ToPartial->setMemberSpecialization();
4451
4452 D2 = ToPartial;
4453 } else { // Full specialization
4454 D2 = VarTemplateSpecializationDecl::Create(
4455 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
4456 D->getStorageClass(), TemplateArgs);
4457 }
4458
4459 SourceLocation POI = D->getPointOfInstantiation();
4460 if (POI.isValid())
4461 D2->setPointOfInstantiation(Importer.Import(POI));
4462
4463 D2->setSpecializationKind(D->getSpecializationKind());
4464 D2->setTemplateArgsInfo(ToTAInfo);
4465
4466 // Add this specialization to the class template.
4467 VarTemplate->AddSpecialization(D2, InsertPos);
4468
4469 // Import the qualifier, if any.
4470 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4471
4472 if (D->isConstexpr())
4473 D2->setConstexpr(true);
4474
4475 // Add the specialization to this context.
4476 D2->setLexicalDeclContext(LexicalDC);
4477 LexicalDC->addDeclInternal(D2);
4478
4479 D2->setAccess(D->getAccess());
4480 }
4481
4482 Importer.Imported(D, D2);
4483
4484 // NOTE: isThisDeclarationADefinition() can return DeclarationOnly even if
4485 // declaration has initializer. Should this be fixed in the AST?.. Anyway,
4486 // we have to check the declaration for initializer - otherwise, it won't be
4487 // imported.
4488 if ((D->isThisDeclarationADefinition() || D->hasInit()) &&
4489 ImportDefinition(D, D2))
4490 return nullptr;
4491
4492 return D2;
4493}
4494
4495Decl *ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
4496 DeclContext *DC, *LexicalDC;
4497 DeclarationName Name;
4498 SourceLocation Loc;
4499 NamedDecl *ToD;
4500
4501 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4502 return nullptr;
4503
4504 if (ToD)
4505 return ToD;
4506
4507 // Try to find a function in our own ("to") context with the same name, same
4508 // type, and in the same context as the function we're importing.
4509 if (!LexicalDC->isFunctionOrMethod()) {
4510 unsigned IDNS = Decl::IDNS_Ordinary;
4511 SmallVector<NamedDecl *, 2> FoundDecls;
4512 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
4513 for (auto *FoundDecl : FoundDecls) {
4514 if (!FoundDecl->isInIdentifierNamespace(IDNS))
4515 continue;
4516
4517 if (auto *FoundFunction = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
4518 if (FoundFunction->hasExternalFormalLinkage() &&
4519 D->hasExternalFormalLinkage()) {
4520 if (IsStructuralMatch(D, FoundFunction)) {
4521 Importer.Imported(D, FoundFunction);
4522 // FIXME: Actually try to merge the body and other attributes.
4523 return FoundFunction;
4524 }
4525 }
4526 }
4527 }
4528 }
4529
4530 TemplateParameterList *Params =
4531 ImportTemplateParameterList(D->getTemplateParameters());
4532 if (!Params)
4533 return nullptr;
4534
4535 auto *TemplatedFD =
4536 cast_or_null<FunctionDecl>(Importer.Import(D->getTemplatedDecl()));
4537 if (!TemplatedFD)
4538 return nullptr;
4539
4540 FunctionTemplateDecl *ToFunc = FunctionTemplateDecl::Create(
4541 Importer.getToContext(), DC, Loc, Name, Params, TemplatedFD);
4542
4543 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
4544 ToFunc->setAccess(D->getAccess());
4545 ToFunc->setLexicalDeclContext(LexicalDC);
4546 Importer.Imported(D, ToFunc);
4547
4548 LexicalDC->addDeclInternal(ToFunc);
4549 return ToFunc;
4550}
4551
4552//----------------------------------------------------------------------------
4553// Import Statements
4554//----------------------------------------------------------------------------
4555
4556DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4557 if (DG.isNull())
4558 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4559 size_t NumDecls = DG.end() - DG.begin();
4560 SmallVector<Decl *, 1> ToDecls(NumDecls);
4561 auto &_Importer = this->Importer;
4562 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4563 [&_Importer](Decl *D) -> Decl * {
4564 return _Importer.Import(D);
4565 });
4566 return DeclGroupRef::Create(Importer.getToContext(),
4567 ToDecls.begin(),
4568 NumDecls);
4569}
4570
4571Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4572 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4573 << S->getStmtClassName();
4574 return nullptr;
4575}
4576
4577Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
4578 SmallVector<IdentifierInfo *, 4> Names;
4579 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4580 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
4581 // ToII is nullptr when no symbolic name is given for output operand
4582 // see ParseStmtAsm::ParseAsmOperandsOpt
4583 if (!ToII && S->getOutputIdentifier(I))
4584 return nullptr;
4585 Names.push_back(ToII);
4586 }
4587 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4588 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
4589 // ToII is nullptr when no symbolic name is given for input operand
4590 // see ParseStmtAsm::ParseAsmOperandsOpt
4591 if (!ToII && S->getInputIdentifier(I))
4592 return nullptr;
4593 Names.push_back(ToII);
4594 }
4595
4596 SmallVector<StringLiteral *, 4> Clobbers;
4597 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
4598 auto *Clobber = cast_or_null<StringLiteral>(
4599 Importer.Import(S->getClobberStringLiteral(I)));
4600 if (!Clobber)
4601 return nullptr;
4602 Clobbers.push_back(Clobber);
4603 }
4604
4605 SmallVector<StringLiteral *, 4> Constraints;
4606 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4607 auto *Output = cast_or_null<StringLiteral>(
4608 Importer.Import(S->getOutputConstraintLiteral(I)));
4609 if (!Output)
4610 return nullptr;
4611 Constraints.push_back(Output);
4612 }
4613
4614 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4615 auto *Input = cast_or_null<StringLiteral>(
4616 Importer.Import(S->getInputConstraintLiteral(I)));
4617 if (!Input)
4618 return nullptr;
4619 Constraints.push_back(Input);
4620 }
4621
4622 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
4623 if (ImportContainerChecked(S->outputs(), Exprs))
4624 return nullptr;
4625
4626 if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
4627 return nullptr;
4628
4629 auto *AsmStr = cast_or_null<StringLiteral>(
4630 Importer.Import(S->getAsmString()));
4631 if (!AsmStr)
4632 return nullptr;
4633
4634 return new (Importer.getToContext()) GCCAsmStmt(
4635 Importer.getToContext(),
4636 Importer.Import(S->getAsmLoc()),
4637 S->isSimple(),
4638 S->isVolatile(),
4639 S->getNumOutputs(),
4640 S->getNumInputs(),
4641 Names.data(),
4642 Constraints.data(),
4643 Exprs.data(),
4644 AsmStr,
4645 S->getNumClobbers(),
4646 Clobbers.data(),
4647 Importer.Import(S->getRParenLoc()));
4648}
4649
4650Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4651 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
4652 for (auto *ToD : ToDG) {
4653 if (!ToD)
4654 return nullptr;
4655 }
4656 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4657 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4658 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4659}
4660
4661Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4662 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4663 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4664 S->hasLeadingEmptyMacro());
4665}
4666
4667Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
4668 SmallVector<Stmt *, 8> ToStmts(S->size());
4669
4670 if (ImportContainerChecked(S->body(), ToStmts))
4671 return nullptr;
4672
4673 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4674 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
4675 return CompoundStmt::Create(Importer.getToContext(), ToStmts, ToLBraceLoc,
4676 ToRBraceLoc);
4677}
4678
4679Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4680 Expr *ToLHS = Importer.Import(S->getLHS());
4681 if (!ToLHS)
4682 return nullptr;
4683 Expr *ToRHS = Importer.Import(S->getRHS());
4684 if (!ToRHS && S->getRHS())
4685 return nullptr;
4686 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4687 if (!ToSubStmt && S->getSubStmt())
4688 return nullptr;
4689 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4690 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4691 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4692 auto *ToStmt = new (Importer.getToContext())
4693 CaseStmt(ToLHS, ToRHS, ToCaseLoc, ToEllipsisLoc, ToColonLoc);
4694 ToStmt->setSubStmt(ToSubStmt);
4695 return ToStmt;
4696}
4697
4698Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4699 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4700 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4701 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4702 if (!ToSubStmt && S->getSubStmt())
4703 return nullptr;
4704 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4705 ToSubStmt);
4706}
4707
4708Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4709 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
4710 auto *ToLabelDecl = cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
4711 if (!ToLabelDecl && S->getDecl())
4712 return nullptr;
4713 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4714 if (!ToSubStmt && S->getSubStmt())
4715 return nullptr;
4716 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4717 ToSubStmt);
4718}
4719
4720Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4721 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4722 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4723 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
4724 ASTContext &_ToContext = Importer.getToContext();
4725 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
4726 [&_ToContext](const Attr *A) -> const Attr * {
4727 return A->clone(_ToContext);
4728 });
4729 for (const auto *ToA : ToAttrs) {
4730 if (!ToA)
4731 return nullptr;
4732 }
4733 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4734 if (!ToSubStmt && S->getSubStmt())
4735 return nullptr;
4736 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4737 ToAttrs, ToSubStmt);
4738}
4739
4740Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4741 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
4742 Stmt *ToInit = Importer.Import(S->getInit());
4743 if (!ToInit && S->getInit())
4744 return nullptr;
4745 VarDecl *ToConditionVariable = nullptr;
4746 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4747 ToConditionVariable =
4748 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4749 if (!ToConditionVariable)
4750 return nullptr;
4751 }
4752 Expr *ToCondition = Importer.Import(S->getCond());
4753 if (!ToCondition && S->getCond())
4754 return nullptr;
4755 Stmt *ToThenStmt = Importer.Import(S->getThen());
4756 if (!ToThenStmt && S->getThen())
4757 return nullptr;
4758 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4759 Stmt *ToElseStmt = Importer.Import(S->getElse());
4760 if (!ToElseStmt && S->getElse())
4761 return nullptr;
4762 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
4763 ToIfLoc, S->isConstexpr(),
4764 ToInit,
4765 ToConditionVariable,
4766 ToCondition, ToThenStmt,
4767 ToElseLoc, ToElseStmt);
4768}
4769
4770Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
4771 Stmt *ToInit = Importer.Import(S->getInit());
4772 if (!ToInit && S->getInit())
4773 return nullptr;
4774 VarDecl *ToConditionVariable = nullptr;
4775 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4776 ToConditionVariable =
4777 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4778 if (!ToConditionVariable)
4779 return nullptr;
4780 }
4781 Expr *ToCondition = Importer.Import(S->getCond());
4782 if (!ToCondition && S->getCond())
4783 return nullptr;
4784 auto *ToStmt = new (Importer.getToContext()) SwitchStmt(
4785 Importer.getToContext(), ToInit,
4786 ToConditionVariable, ToCondition);
4787 Stmt *ToBody = Importer.Import(S->getBody());
4788 if (!ToBody && S->getBody())
4789 return nullptr;
4790 ToStmt->setBody(ToBody);
4791 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4792 // Now we have to re-chain the cases.
4793 SwitchCase *LastChainedSwitchCase = nullptr;
4794 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4795 SC = SC->getNextSwitchCase()) {
4796 auto *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
4797 if (!ToSC)
4798 return nullptr;
4799 if (LastChainedSwitchCase)
4800 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4801 else
4802 ToStmt->setSwitchCaseList(ToSC);
4803 LastChainedSwitchCase = ToSC;
4804 }
4805 return ToStmt;
4806}
4807
4808Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
4809 VarDecl *ToConditionVariable = nullptr;
4810 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4811 ToConditionVariable =
4812 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4813 if (!ToConditionVariable)
4814 return nullptr;
4815 }
4816 Expr *ToCondition = Importer.Import(S->getCond());
4817 if (!ToCondition && S->getCond())
4818 return nullptr;
4819 Stmt *ToBody = Importer.Import(S->getBody());
4820 if (!ToBody && S->getBody())
4821 return nullptr;
4822 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4823 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
4824 ToConditionVariable,
4825 ToCondition, ToBody,
4826 ToWhileLoc);
4827}
4828
4829Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
4830 Stmt *ToBody = Importer.Import(S->getBody());
4831 if (!ToBody && S->getBody())
4832 return nullptr;
4833 Expr *ToCondition = Importer.Import(S->getCond());
4834 if (!ToCondition && S->getCond())
4835 return nullptr;
4836 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
4837 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4838 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4839 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
4840 ToDoLoc, ToWhileLoc,
4841 ToRParenLoc);
4842}
4843
4844Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
4845 Stmt *ToInit = Importer.Import(S->getInit());
4846 if (!ToInit && S->getInit())
4847 return nullptr;
4848 Expr *ToCondition = Importer.Import(S->getCond());
4849 if (!ToCondition && S->getCond())
4850 return nullptr;
4851 VarDecl *ToConditionVariable = nullptr;
4852 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4853 ToConditionVariable =
4854 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4855 if (!ToConditionVariable)
4856 return nullptr;
4857 }
4858 Expr *ToInc = Importer.Import(S->getInc());
4859 if (!ToInc && S->getInc())
4860 return nullptr;
4861 Stmt *ToBody = Importer.Import(S->getBody());
4862 if (!ToBody && S->getBody())
4863 return nullptr;
4864 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4865 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
4866 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4867 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
4868 ToInit, ToCondition,
4869 ToConditionVariable,
4870 ToInc, ToBody,
4871 ToForLoc, ToLParenLoc,
4872 ToRParenLoc);
4873}
4874
4875Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
4876 LabelDecl *ToLabel = nullptr;
4877 if (LabelDecl *FromLabel = S->getLabel()) {
4878 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
4879 if (!ToLabel)
4880 return nullptr;
4881 }
4882 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4883 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
4884 return new (Importer.getToContext()) GotoStmt(ToLabel,
4885 ToGotoLoc, ToLabelLoc);
4886}
4887
4888Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
4889 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4890 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
4891 Expr *ToTarget = Importer.Import(S->getTarget());
4892 if (!ToTarget && S->getTarget())
4893 return nullptr;
4894 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
4895 ToTarget);
4896}
4897
4898Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
4899 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
4900 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
4901}
4902
4903Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
4904 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
4905 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
4906}
4907
4908Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
4909 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
4910 Expr *ToRetExpr = Importer.Import(S->getRetValue());
4911 if (!ToRetExpr && S->getRetValue())
4912 return nullptr;
4913 auto *NRVOCandidate = const_cast<VarDecl *>(S->getNRVOCandidate());
4914 auto *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
4915 if (!ToNRVOCandidate && NRVOCandidate)
4916 return nullptr;
4917 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
4918 ToNRVOCandidate);
4919}
4920
4921Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
4922 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
4923 VarDecl *ToExceptionDecl = nullptr;
4924 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
4925 ToExceptionDecl =
4926 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4927 if (!ToExceptionDecl)
4928 return nullptr;
4929 }
4930 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
4931 if (!ToHandlerBlock && S->getHandlerBlock())
4932 return nullptr;
4933 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
4934 ToExceptionDecl,
4935 ToHandlerBlock);
4936}
4937
4938Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
4939 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
4940 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
4941 if (!ToTryBlock && S->getTryBlock())
4942 return nullptr;
4943 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
4944 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
4945 CXXCatchStmt *FromHandler = S->getHandler(HI);
4946 if (Stmt *ToHandler = Importer.Import(FromHandler))
4947 ToHandlers[HI] = ToHandler;
4948 else
4949 return nullptr;
4950 }
4951 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
4952 ToHandlers);
4953}
4954
4955Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
4956 auto *ToRange =
4957 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
4958 if (!ToRange && S->getRangeStmt())
4959 return nullptr;
4960 auto *ToBegin =
4961 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
4962 if (!ToBegin && S->getBeginStmt())
4963 return nullptr;
4964 auto *ToEnd =
4965 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
4966 if (!ToEnd && S->getEndStmt())
4967 return nullptr;
4968 Expr *ToCond = Importer.Import(S->getCond());
4969 if (!ToCond && S->getCond())
4970 return nullptr;
4971 Expr *ToInc = Importer.Import(S->getInc());
4972 if (!ToInc && S->getInc())
4973 return nullptr;
4974 auto *ToLoopVar =
4975 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
4976 if (!ToLoopVar && S->getLoopVarStmt())
4977 return nullptr;
4978 Stmt *ToBody = Importer.Import(S->getBody());
4979 if (!ToBody && S->getBody())
4980 return nullptr;
4981 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4982 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
4983 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4984 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4985 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
4986 ToCond, ToInc,
4987 ToLoopVar, ToBody,
4988 ToForLoc, ToCoawaitLoc,
4989 ToColonLoc, ToRParenLoc);
4990}
4991
4992Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
4993 Stmt *ToElem = Importer.Import(S->getElement());
4994 if (!ToElem && S->getElement())
4995 return nullptr;
4996 Expr *ToCollect = Importer.Import(S->getCollection());
4997 if (!ToCollect && S->getCollection())
4998 return nullptr;
4999 Stmt *ToBody = Importer.Import(S->getBody());
5000 if (!ToBody && S->getBody())
5001 return nullptr;
5002 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5003 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5004 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
5005 ToCollect,
5006 ToBody, ToForLoc,
5007 ToRParenLoc);
5008}
5009
5010Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5011 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
5012 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5013 VarDecl *ToExceptionDecl = nullptr;
5014 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
5015 ToExceptionDecl =
5016 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5017 if (!ToExceptionDecl)
5018 return nullptr;
5019 }
5020 Stmt *ToBody = Importer.Import(S->getCatchBody());
5021 if (!ToBody && S->getCatchBody())
5022 return nullptr;
5023 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
5024 ToRParenLoc,
5025 ToExceptionDecl,
5026 ToBody);
5027}
5028
5029Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5030 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
5031 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
5032 if (!ToAtFinallyStmt && S->getFinallyBody())
5033 return nullptr;
5034 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
5035 ToAtFinallyStmt);
5036}
5037
5038Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5039 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
5040 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
5041 if (!ToAtTryStmt && S->getTryBody())
5042 return nullptr;
5043 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5044 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5045 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
5046 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
5047 ToCatchStmts[CI] = ToCatchStmt;
5048 else
5049 return nullptr;
5050 }
5051 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
5052 if (!ToAtFinallyStmt && S->getFinallyStmt())
5053 return nullptr;
5054 return ObjCAtTryStmt::Create(Importer.getToContext(),
5055 ToAtTryLoc, ToAtTryStmt,
5056 ToCatchStmts.begin(), ToCatchStmts.size(),
5057 ToAtFinallyStmt);
5058}
5059
5060Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
5061 (ObjCAtSynchronizedStmt *S) {
5062 SourceLocation ToAtSynchronizedLoc =
5063 Importer.Import(S->getAtSynchronizedLoc());
5064 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
5065 if (!ToSynchExpr && S->getSynchExpr())
5066 return nullptr;
5067 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
5068 if (!ToSynchBody && S->getSynchBody())
5069 return nullptr;
5070 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5071 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5072}
5073
5074Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5075 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5076 Expr *ToThrow = Importer.Import(S->getThrowExpr());
5077 if (!ToThrow && S->getThrowExpr())
5078 return nullptr;
5079 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5080}
5081
5082Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5083 (ObjCAutoreleasePoolStmt *S) {
5084 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5085 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5086 if (!ToSubStmt && S->getSubStmt())
5087 return nullptr;
5088 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5089 ToSubStmt);
5090}
5091
5092//----------------------------------------------------------------------------
5093// Import Expressions
5094//----------------------------------------------------------------------------
5095Expr *ASTNodeImporter::VisitExpr(Expr *E) {
5096 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5097 << E->getStmtClassName();
5098 return nullptr;
5099}
5100
5101Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
5102 QualType T = Importer.Import(E->getType());
5103 if (T.isNull())
5104 return nullptr;
5105
5106 Expr *SubExpr = Importer.Import(E->getSubExpr());
5107 if (!SubExpr && E->getSubExpr())
5108 return nullptr;
5109
5110 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
5111 if (!TInfo)
5112 return nullptr;
5113
5114 return new (Importer.getToContext()) VAArgExpr(
5115 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
5116 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
5117}
5118
5119Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
5120 QualType T = Importer.Import(E->getType());
5121 if (T.isNull())
5122 return nullptr;
5123
5124 return new (Importer.getToContext()) GNUNullExpr(
5125 T, Importer.Import(E->getLocStart()));
5126}
5127
5128Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
5129 QualType T = Importer.Import(E->getType());
5130 if (T.isNull())
5131 return nullptr;
5132
5133 auto *SL = cast_or_null<StringLiteral>(Importer.Import(E->getFunctionName()));
5134 if (!SL && E->getFunctionName())
5135 return nullptr;
5136
5137 return new (Importer.getToContext()) PredefinedExpr(
5138 Importer.Import(E->getLocStart()), T, E->getIdentType(), SL);
5139}
5140
5141Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
5142 auto *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
5143 if (!ToD)
5144 return nullptr;
5145
5146 NamedDecl *FoundD = nullptr;
5147 if (E->getDecl() != E->getFoundDecl()) {
5148 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5149 if (!FoundD)
5150 return nullptr;
5151 }
5152
5153 QualType T = Importer.Import(E->getType());
5154 if (T.isNull())
5155 return nullptr;
5156
5157 TemplateArgumentListInfo ToTAInfo;
5158 TemplateArgumentListInfo *ResInfo = nullptr;
5159 if (E->hasExplicitTemplateArgs()) {
5160 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
5161 return nullptr;
5162 ResInfo = &ToTAInfo;
5163 }
5164
5165 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5166 Importer.Import(E->getQualifierLoc()),
5167 Importer.Import(E->getTemplateKeywordLoc()),
5168 ToD,
5169 E->refersToEnclosingVariableOrCapture(),
5170 Importer.Import(E->getLocation()),
5171 T, E->getValueKind(),
5172 FoundD, ResInfo);
5173 if (E->hadMultipleCandidates())
5174 DRE->setHadMultipleCandidates(true);
5175 return DRE;
5176}
5177
5178Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
5179 QualType T = Importer.Import(E->getType());
5180 if (T.isNull())
5181 return nullptr;
5182
5183 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
5184}
5185
5186ASTNodeImporter::Designator
5187ASTNodeImporter::ImportDesignator(const Designator &D) {
5188 if (D.isFieldDesignator()) {
5189 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
5190 // Caller checks for import error
5191 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
5192 Importer.Import(D.getFieldLoc()));
5193 }
5194 if (D.isArrayDesignator())
5195 return Designator(D.getFirstExprIndex(),
5196 Importer.Import(D.getLBracketLoc()),
5197 Importer.Import(D.getRBracketLoc()));
5198
5199 assert(D.isArrayRangeDesignator())(static_cast <bool> (D.isArrayRangeDesignator()) ? void
(0) : __assert_fail ("D.isArrayRangeDesignator()", "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/AST/ASTImporter.cpp"
, 5199, __extension__ __PRETTY_FUNCTION__))
;
5200 return Designator(D.getFirstExprIndex(),
5201 Importer.Import(D.getLBracketLoc()),
5202 Importer.Import(D.getEllipsisLoc()),
5203 Importer.Import(D.getRBracketLoc()));
5204}
5205
5206
5207Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
5208 auto *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
5209 if (!Init)
5210 return nullptr;
5211
5212 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
5213 // List elements from the second, the first is Init itself
5214 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
5215 if (auto *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
5216 IndexExprs[I - 1] = Arg;
5217 else
5218 return nullptr;
5219 }
5220
5221 SmallVector<Designator, 4> Designators(DIE->size());
5222 llvm::transform(DIE->designators(), Designators.begin(),
5223 [this](const Designator &D) -> Designator {
5224 return ImportDesignator(D);
5225 });
5226
5227 for (const auto &D : DIE->designators())
5228 if (D.isFieldDesignator() && !D.getFieldName())
5229 return nullptr;
5230
5231 return DesignatedInitExpr::Create(
5232 Importer.getToContext(), Designators,
5233 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
5234 DIE->usesGNUSyntax(), Init);
5235}
5236
5237Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
5238 QualType T = Importer.Import(E->getType());
5239 if (T.isNull())
5240 return nullptr;
5241
5242 return new (Importer.getToContext())
5243 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
5244}
5245
5246Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5247 QualType T = Importer.Import(E->getType());
5248 if (T.isNull())
5249 return nullptr;
5250
5251 return IntegerLiteral::Create(Importer.getToContext(),
5252 E->getValue(), T,
5253 Importer.Import(E->getLocation()));
5254}
5255
5256Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
5257 QualType T = Importer.Import(E->getType());
5258 if (T.isNull())
5259 return nullptr;
5260
5261 return FloatingLiteral::Create(Importer.getToContext(),
5262 E->getValue(), E->isExact(), T,
5263 Importer.Import(E->getLocation()));
5264}
5265
5266Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5267 QualType T = Importer.Import(E->getType());
5268 if (T.isNull())
5269 return nullptr;
5270
5271 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5272 E->getKind(), T,
5273 Importer.Import(E->getLocation()));
5274}
5275
5276Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
5277 QualType T = Importer.Import(E->getType());
5278 if (T.isNull())
5279 return nullptr;
5280
5281 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
5282 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
5283
5284 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
5285 E->getKind(), E->isPascal(), T,
5286 Locations.data(), Locations.size());
5287}
5288
5289Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
5290 QualType T = Importer.Import(E->getType());
5291 if (T.isNull())
5292 return nullptr;
5293
5294 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
5295 if (!TInfo)
5296 return nullptr;
5297
5298 Expr *Init = Importer.Import(E->getInitializer());
5299 if (!Init)
5300 return nullptr;
5301
5302 return new (Importer.getToContext()) CompoundLiteralExpr(
5303 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
5304 Init, E->isFileScope());
5305}
5306
5307Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
5308 QualType T = Importer.Import(E->getType());
5309 if (T.isNull())
5310 return nullptr;
5311
5312 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
5313 if (ImportArrayChecked(
5314 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
5315 Exprs.begin()))
5316 return nullptr;
5317
5318 return new (Importer.getToContext()) AtomicExpr(
5319 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
5320 Importer.Import(E->getRParenLoc()));
5321}
5322
5323Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
5324 QualType T = Importer.Import(E->getType());
5325 if (T.isNull())
5326 return nullptr;
5327
5328 auto *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
5329 if (!ToLabel)
5330 return nullptr;
5331
5332 return new (Importer.getToContext()) AddrLabelExpr(
5333 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
5334 ToLabel, T);
5335}
5336
5337Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5338 Expr *SubExpr = Importer.Import(E->getSubExpr());
5339 if (!SubExpr)
5340 return nullptr;
5341
5342 return new (Importer.getToContext())
5343 ParenExpr(Importer.Import(E->getLParen()),
5344 Importer.Import(E->getRParen()),
5345 SubExpr);
5346}
5347
5348Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
5349 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
5350 if (ImportContainerChecked(E->exprs(), Exprs))
5351 return nullptr;
5352
5353 return new (Importer.getToContext()) ParenListExpr(
5354 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
5355 Exprs, Importer.Import(E->getLParenLoc()));
5356}
5357
5358Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
5359 QualType T = Importer.Import(E->getType());
5360 if (T.isNull())
5361 return nullptr;
5362
5363 auto *ToSubStmt = cast_or_null<CompoundStmt>(
5364 Importer.Import(E->getSubStmt()));
5365 if (!ToSubStmt && E->getSubStmt())
5366 return nullptr;
5367
5368 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
5369 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
5370}
5371
5372Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5373 QualType T = Importer.Import(E->getType());
5374 if (T.isNull())
5375 return nullptr;
5376
5377 Expr *SubExpr = Importer.Import(E->getSubExpr());
5378 if (!SubExpr)
5379 return nullptr;
5380
5381 return new (Importer.getToContext()) UnaryOperator(
5382 SubExpr, E->getOpcode(), T, E->getValueKind(), E->getObjectKind(),
5383 Importer.Import(E->getOperatorLoc()), E->canOverflow());
5384}
5385
5386Expr *
5387ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
5388 QualType ResultType = Importer.Import(E->getType());
5389
5390 if (E->isArgumentType()) {
5391 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5392 if (!TInfo)
5393 return nullptr;
5394
5395 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5396 TInfo, ResultType,
5397 Importer.Import(E->getOperatorLoc()),
5398 Importer.Import(E->getRParenLoc()));
5399 }
5400
5401 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5402 if (!SubExpr)
5403 return nullptr;
5404
5405 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5406 SubExpr, ResultType,
5407 Importer.Import(E->getOperatorLoc()),
5408 Importer.Import(E->getRParenLoc()));
5409}
5410
5411Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5412 QualType T = Importer.Import(E->getType());
5413 if (T.isNull())
5414 return nullptr;
5415
5416 Expr *LHS = Importer.Import(E->getLHS());
5417 if (!LHS)
5418 return nullptr;
5419
5420 Expr *RHS = Importer.Import(E->getRHS());
5421 if (!RHS)
5422 return nullptr;
5423
5424 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
5425 T, E->getValueKind(),
5426 E->getObjectKind(),
5427 Importer.Import(E->getOperatorLoc()),
5428 E->getFPFeatures());
5429}
5430
5431Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
5432 QualType T = Importer.Import(E->getType());
5433 if (T.isNull())
5434 return nullptr;
5435
5436 Expr *ToLHS = Importer.Import(E->getLHS());
5437 if (!ToLHS)
5438 return nullptr;
5439
5440 Expr *ToRHS = Importer.Import(E->getRHS());
5441 if (!ToRHS)
5442 return nullptr;
5443
5444 Expr *ToCond = Importer.Import(E->getCond());
5445 if (!ToCond)
5446 return nullptr;
5447
5448 return new (Importer.getToContext()) ConditionalOperator(
5449 ToCond, Importer.Import(E->getQuestionLoc()),
5450 ToLHS, Importer.Import(E->getColonLoc()),
5451 ToRHS, T, E->getValueKind(), E->getObjectKind());
5452}
5453
5454Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
5455 BinaryConditionalOperator *E) {
5456 QualType T = Importer.Import(E->getType());
5457 if (T.isNull())
5458 return nullptr;
5459
5460 Expr *Common = Importer.Import(E->getCommon());
5461 if (!Common)
5462 return nullptr;
5463
5464 Expr *Cond = Importer.Import(E->getCond());
5465 if (!Cond)
5466 return nullptr;
5467
5468 auto *OpaqueValue = cast_or_null<OpaqueValueExpr>(
5469 Importer.Import(E->getOpaqueValue()));
5470 if (!OpaqueValue)
5471 return nullptr;
5472
5473 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
5474 if (!TrueExpr)
5475 return nullptr;
5476
5477 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
5478 if (!FalseExpr)
5479 return nullptr;
5480
5481 return new (Importer.getToContext()) BinaryConditionalOperator(
5482 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
5483 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
5484 T, E->getValueKind(), E->getObjectKind());
5485}
5486
5487Expr *ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
5488 QualType T = Importer.Import(E->getType());
5489 if (T.isNull())
5490 return nullptr;
5491
5492 TypeSourceInfo *ToQueried = Importer.Import(E->getQueriedTypeSourceInfo());
5493 if (!ToQueried)
5494 return nullptr;
5495
5496 Expr *Dim = Importer.Import(E->getDimensionExpression());
5497 if (!Dim && E->getDimensionExpression())
5498 return nullptr;
5499
5500 return new (Importer.getToContext()) ArrayTypeTraitExpr(
5501 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5502 E->getValue(), Dim, Importer.Import(E->getLocEnd()), T);
5503}
5504
5505Expr *ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
5506 QualType T = Importer.Import(E->getType());
5507 if (T.isNull())
5508 return nullptr;
5509
5510 Expr *ToQueried = Importer.Import(E->getQueriedExpression());
5511 if (!ToQueried)
5512 return nullptr;
5513
5514 return new (Importer.getToContext()) ExpressionTraitExpr(
5515 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5516 E->getValue(), Importer.Import(E->getLocEnd()), T);
5517}
5518
5519Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
5520 QualType T = Importer.Import(E->getType());
5521 if (T.isNull())
5522 return nullptr;
5523
5524 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
5525 if (!SourceExpr && E->getSourceExpr())
5526 return nullptr;
5527
5528 return new (Importer.getToContext()) OpaqueValueExpr(
5529 Importer.Import(E->getLocation()), T, E->getValueKind(),
5530 E->getObjectKind(), SourceExpr);
5531}
5532
5533Expr *ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
5534 QualType T = Importer.Import(E->getType());
5535 if (T.isNull())
5536 return nullptr;
5537
5538 Expr *ToLHS = Importer.Import(E->getLHS());
5539 if (!ToLHS)
5540 return nullptr;
5541
5542 Expr *ToRHS = Importer.Import(E->getRHS());
5543 if (!ToRHS)
5544 return nullptr;
5545
5546 return new (Importer.getToContext()) ArraySubscriptExpr(
5547 ToLHS, ToRHS, T, E->getValueKind(), E->getObjectKind(),
5548 Importer.Import(E->getRBracketLoc()));
5549}
5550
5551Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5552 QualType T = Importer.Import(E->getType());
5553 if (T.isNull())
5554 return nullptr;
5555
5556 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5557 if (CompLHSType.isNull())
5558 return nullptr;
5559
5560 QualType CompResultType = Importer.Import(E->getComputationResultType());
5561 if (CompResultType.isNull())
5562 return nullptr;
5563
5564 Expr *LHS = Importer.Import(E->getLHS());
5565 if (!LHS)
5566 return nullptr;
5567
5568 Expr *RHS = Importer.Import(E->getRHS());
5569 if (!RHS)
5570 return nullptr;
5571
5572 return new (Importer.getToContext())
5573 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
5574 T, E->getValueKind(),
5575 E->getObjectKind(),
5576 CompLHSType, CompResultType,
5577 Importer.Import(E->getOperatorLoc()),
5578 E->getFPFeatures());
5579}
5580
5581bool ASTNodeImporter::ImportCastPath(CastExpr *CE, CXXCastPath &Path) {
5582 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
5583 if (CXXBaseSpecifier *Spec = Importer.Import(*I))
5584 Path.push_back(Spec);
5585 else
5586 return true;
5587 }
5588 return false;
5589}
5590
5591Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5592 QualType T = Importer.Import(E->getType());
5593 if (T.isNull())
5594 return nullptr;
5595
5596 Expr *SubExpr = Importer.Import(E->getSubExpr());
5597 if (!SubExpr)
5598 return nullptr;
5599
5600 CXXCastPath BasePath;
5601 if (ImportCastPath(E, BasePath))
5602 return nullptr;
5603
5604 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
5605 SubExpr, &BasePath, E->getValueKind());
5606}
5607
5608Expr *ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
5609 QualType T = Importer.Import(E->getType());
5610 if (T.isNull())
5611 return nullptr;
5612
5613 Expr *SubExpr = Importer.Import(E->getSubExpr());
5614 if (!SubExpr)
5615 return nullptr;
5616
5617 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5618 if (!TInfo && E->getTypeInfoAsWritten())
5619 return nullptr;
5620
5621 CXXCastPath BasePath;
5622 if (ImportCastPath(E, BasePath))
5623 return nullptr;
5624
5625 switch (E->getStmtClass()) {
5626 case Stmt::CStyleCastExprClass: {
5627 auto *CCE = cast<CStyleCastExpr>(E);
5628 return CStyleCastExpr::Create(Importer.getToContext(), T,
5629 E->getValueKind(), E->getCastKind(),
5630 SubExpr, &BasePath, TInfo,
5631 Importer.Import(CCE->getLParenLoc()),
5632 Importer.Import(CCE->getRParenLoc()));
5633 }
5634
5635 case Stmt::CXXFunctionalCastExprClass: {
5636 auto *FCE = cast<CXXFunctionalCastExpr>(E);
5637 return CXXFunctionalCastExpr::Create(Importer.getToContext(), T,
5638 E->getValueKind(), TInfo,
5639 E->getCastKind(), SubExpr, &BasePath,
5640 Importer.Import(FCE->getLParenLoc()),
5641 Importer.Import(FCE->getRParenLoc()));
5642 }
5643
5644 case Stmt::ObjCBridgedCastExprClass: {
5645 auto *OCE = cast<ObjCBridgedCastExpr>(E);
5646 return new (Importer.getToContext()) ObjCBridgedCastExpr(
5647 Importer.Import(OCE->getLParenLoc()), OCE->getBridgeKind(),
5648 E->getCastKind(), Importer.Import(OCE->getBridgeKeywordLoc()),
5649 TInfo, SubExpr);
5650 }
5651 default:
5652 break; // just fall through
5653 }
5654
5655 auto *Named = cast<CXXNamedCastExpr>(E);
5656 SourceLocation ExprLoc = Importer.Import(Named->getOperatorLoc()),
5657 RParenLoc = Importer.Import(Named->getRParenLoc());
5658 SourceRange Brackets = Importer.Import(Named->getAngleBrackets());
5659
5660 switch (E->getStmtClass()) {
5661 case Stmt::CXXStaticCastExprClass:
5662 return CXXStaticCastExpr::Create(Importer.getToContext(), T,
5663 E->getValueKind(), E->getCastKind(),
5664 SubExpr, &BasePath, TInfo,
5665 ExprLoc, RParenLoc, Brackets);
5666
5667 case Stmt::CXXDynamicCastExprClass:
5668 return CXXDynamicCastExpr::Create(Importer.getToContext(), T,
5669 E->getValueKind(), E->getCastKind(),
5670 SubExpr, &BasePath, TInfo,
5671 ExprLoc, RParenLoc, Brackets);
5672
5673 case Stmt::CXXReinterpretCastExprClass:
5674 return CXXReinterpretCastExpr::Create(Importer.getToContext(), T,
5675 E->getValueKind(), E->getCastKind(),
5676 SubExpr, &BasePath, TInfo,
5677 ExprLoc, RParenLoc, Brackets);
5678
5679 case Stmt::CXXConstCastExprClass:
5680 return CXXConstCastExpr::Create(Importer.getToContext(), T,
5681 E->getValueKind(), SubExpr, TInfo, ExprLoc,
5682 RParenLoc, Brackets);
5683 default:
5684 llvm_unreachable("Cast expression of unsupported type!")::llvm::llvm_unreachable_internal("Cast expression of unsupported type!"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/AST/ASTImporter.cpp"
, 5684)
;
5685 return nullptr;
5686 }
5687}
5688
5689Expr *ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *OE) {
5690 QualType T = Importer.Import(OE->getType());
5691 if (T.isNull())
5692 return nullptr;
5693
5694 SmallVector<OffsetOfNode, 4> Nodes;
5695 for (int I = 0, E = OE->getNumComponents(); I < E; ++I) {
5696 const OffsetOfNode &Node = OE->getComponent(I);
5697
5698 switch (Node.getKind()) {
5699 case OffsetOfNode::Array:
5700 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()),
5701 Node.getArrayExprIndex(),
5702 Importer.Import(Node.getLocEnd())));
5703 break;
5704
5705 case OffsetOfNode::Base: {
5706 CXXBaseSpecifier *BS = Importer.Import(Node.getBase());
5707 if (!BS && Node.getBase())
5708 return nullptr;
5709 Nodes.push_back(OffsetOfNode(BS));
5710 break;
5711 }
5712 case OffsetOfNode::Field: {
5713 auto *FD = cast_or_null<FieldDecl>(Importer.Import(Node.getField()));
5714 if (!FD)
5715 return nullptr;
5716 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), FD,
5717 Importer.Import(Node.getLocEnd())));
5718 break;
5719 }
5720 case OffsetOfNode::Identifier: {
5721 IdentifierInfo *ToII = Importer.Import(Node.getFieldName());
5722 if (!ToII)
5723 return nullptr;
5724 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), ToII,
5725 Importer.Import(Node.getLocEnd())));
5726 break;
5727 }
5728 }
5729 }
5730
5731 SmallVector<Expr *, 4> Exprs(OE->getNumExpressions());
5732 for (int I = 0, E = OE->getNumExpressions(); I < E; ++I) {
5733 Expr *ToIndexExpr = Importer.Import(OE->getIndexExpr(I));
5734 if (!ToIndexExpr)
5735 return nullptr;
5736 Exprs[I] = ToIndexExpr;
5737 }
5738
5739 TypeSourceInfo *TInfo = Importer.Import(OE->getTypeSourceInfo());
5740 if (!TInfo && OE->getTypeSourceInfo())
5741 return nullptr;
5742
5743 return OffsetOfExpr::Create(Importer.getToContext(), T,
5744 Importer.Import(OE->getOperatorLoc()),
5745 TInfo, Nodes, Exprs,
5746 Importer.Import(OE->getRParenLoc()));
5747}
5748
5749Expr *ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
5750 QualType T = Importer.Import(E->getType());
5751 if (T.isNull())
5752 return nullptr;
5753
5754 Expr *Operand = Importer.Import(E->getOperand());
5755 if (!Operand)
5756 return nullptr;
5757
5758 CanThrowResult CanThrow;
5759 if (E->isValueDependent())
5760 CanThrow = CT_Dependent;
5761 else
5762 CanThrow = E->getValue() ? CT_Can : CT_Cannot;
5763
5764 return new (Importer.getToContext()) CXXNoexceptExpr(
5765 T, Operand, CanThrow,
5766 Importer.Import(E->getLocStart()), Importer.Import(E->getLocEnd()));
5767}
5768
5769Expr *ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
5770 QualType T = Importer.Import(E->getType());
5771 if (T.isNull())
5772 return nullptr;
5773
5774 Expr *SubExpr = Importer.Import(E->getSubExpr());
5775 if (!SubExpr && E->getSubExpr())
5776 return nullptr;
5777
5778 return new (Importer.getToContext()) CXXThrowExpr(
5779 SubExpr, T, Importer.Import(E->getThrowLoc()),
5780 E->isThrownVariableInScope());
5781}
5782
5783Expr *ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
5784 auto *Param = cast_or_null<ParmVarDecl>(Importer.Import(E->getParam()));
5785 if (!Param)
5786 return nullptr;
5787
5788 return CXXDefaultArgExpr::Create(
5789 Importer.getToContext(), Importer.Import(E->getUsedLocation()), Param);
5790}
5791
5792Expr *ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
5793 QualType T = Importer.Import(E->getType());
5794 if (T.isNull())
5795 return nullptr;
5796
5797 TypeSourceInfo *TypeInfo = Importer.Import(E->getTypeSourceInfo());
5798 if (!TypeInfo)
5799 return nullptr;
5800
5801 return new (Importer.getToContext()) CXXScalarValueInitExpr(
5802 T, TypeInfo, Importer.Import(E->getRParenLoc()));
5803}
5804
5805Expr *ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
5806 Expr *SubExpr = Importer.Import(E->getSubExpr());
5807 if (!SubExpr)
5808 return nullptr;
5809
5810 auto *Dtor = cast_or_null<CXXDestructorDecl>(
5811 Importer.Import(const_cast<CXXDestructorDecl *>(
5812 E->getTemporary()->getDestructor())));
5813 if (!Dtor)
5814 return nullptr;
5815
5816 ASTContext &ToCtx = Importer.getToContext();
5817 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, Dtor);
5818 return CXXBindTemporaryExpr::Create(ToCtx, Temp, SubExpr);
5819}
5820
5821Expr *ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE) {
5822 QualType T = Importer.Import(CE->getType());
5823 if (T.isNull())
5824 return nullptr;
5825
5826 TypeSourceInfo *TInfo = Importer.Import(CE->getTypeSourceInfo());
5827 if (!TInfo)
5828 return nullptr;
5829
5830 SmallVector<Expr *, 8> Args(CE->getNumArgs());
5831 if (ImportContainerChecked(CE->arguments(), Args))
5832 return nullptr;
5833
5834 auto *Ctor = cast_or_null<CXXConstructorDecl>(
5835 Importer.Import(CE->getConstructor()));
5836 if (!Ctor)
5837 return nullptr;
5838
5839 return new (Importer.getToContext()) CXXTemporaryObjectExpr(
5840 Importer.getToContext(), Ctor, T, TInfo, Args,
5841 Importer.Import(CE->getParenOrBraceRange()), CE->hadMultipleCandidates(),
5842 CE->isListInitialization(), CE->isStdInitListInitialization(),
5843 CE->requiresZeroInitialization());
5844}
5845
5846Expr *
5847ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
5848 QualType T = Importer.Import(E->getType());
5849 if (T.isNull())
5850 return nullptr;
5851
5852 Expr *TempE = Importer.Import(E->GetTemporaryExpr());
5853 if (!TempE)
5854 return nullptr;
5855
5856 auto *ExtendedBy = cast_or_null<ValueDecl>(
5857 Importer.Import(const_cast<ValueDecl *>(E->getExtendingDecl())));
5858 if (!ExtendedBy && E->getExtendingDecl())
5859 return nullptr;
5860
5861 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
5862 T, TempE, E->isBoundToLvalueReference());
5863
5864 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
5865 ToMTE->setExtendingDecl(ExtendedBy, E->getManglingNumber());
5866 return ToMTE;
5867}
5868
5869Expr *ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
5870 QualType T = Importer.Import(E->getType());
5871 if (T.isNull())
5872 return nullptr;
5873
5874 Expr *Pattern = Importer.Import(E->getPattern());
5875 if (!Pattern)
5876 return nullptr;
5877
5878 return new (Importer.getToContext()) PackExpansionExpr(
5879 T, Pattern, Importer.Import(E->getEllipsisLoc()),
5880 E->getNumExpansions());
5881}
5882
5883Expr *ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
5884 auto *Pack = cast_or_null<NamedDecl>(Importer.Import(E->getPack()));
5885 if (!Pack)
5886 return nullptr;
5887
5888 Optional<unsigned> Length;
5889
5890 if (!E->isValueDependent())
5891 Length = E->getPackLength();
5892
5893 SmallVector<TemplateArgument, 8> PartialArguments;
5894 if (E->isPartiallySubstituted()) {
5895 if (ImportTemplateArguments(E->getPartialArguments().data(),
5896 E->getPartialArguments().size(),
5897 PartialArguments))
5898 return nullptr;
5899 }
5900
5901 return SizeOfPackExpr::Create(
5902 Importer.getToContext(), Importer.Import(E->getOperatorLoc()), Pack,
5903 Importer.Import(E->getPackLoc()), Importer.Import(E->getRParenLoc()),
5904 Length, PartialArguments);
5905}
5906
5907Expr *ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *CE) {
5908 QualType T = Importer.Import(CE->getType());
5909 if (T.isNull())
5910 return nullptr;
5911
5912 SmallVector<Expr *, 4> PlacementArgs(CE->getNumPlacementArgs());
5913 if (ImportContainerChecked(CE->placement_arguments(), PlacementArgs))
5914 return nullptr;
5915
5916 auto *OperatorNewDecl = cast_or_null<FunctionDecl>(
5917 Importer.Import(CE->getOperatorNew()));
5918 if (!OperatorNewDecl && CE->getOperatorNew())
5919 return nullptr;
5920
5921 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
5922 Importer.Import(CE->getOperatorDelete()));
5923 if (!OperatorDeleteDecl && CE->getOperatorDelete())
5924 return nullptr;
5925
5926 Expr *ToInit = Importer.Import(CE->getInitializer());
5927 if (!ToInit && CE->getInitializer())
5928 return nullptr;
5929
5930 TypeSourceInfo *TInfo = Importer.Import(CE->getAllocatedTypeSourceInfo());
5931 if (!TInfo)
5932 return nullptr;
5933
5934 Expr *ToArrSize = Importer.Import(CE->getArraySize());
5935 if (!ToArrSize && CE->getArraySize())
5936 return nullptr;
5937
5938 return new (Importer.getToContext()) CXXNewExpr(
5939 Importer.getToContext(),
5940 CE->isGlobalNew(),
5941 OperatorNewDecl, OperatorDeleteDecl,
5942 CE->passAlignment(),
5943 CE->doesUsualArrayDeleteWantSize(),
5944 PlacementArgs,
5945 Importer.Import(CE->getTypeIdParens()),
5946 ToArrSize, CE->getInitializationStyle(), ToInit, T, TInfo,
5947 Importer.Import(CE->getSourceRange()),
5948 Importer.Import(CE->getDirectInitRange()));
5949}
5950
5951Expr *ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
5952 QualType T = Importer.Import(E->getType());
5953 if (T.isNull())
5954 return nullptr;
5955
5956 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
5957 Importer.Import(E->getOperatorDelete()));
5958 if (!OperatorDeleteDecl && E->getOperatorDelete())
5959 return nullptr;
5960
5961 Expr *ToArg = Importer.Import(E->getArgument());
5962 if (!ToArg && E->getArgument())
5963 return nullptr;
5964
5965 return new (Importer.getToContext()) CXXDeleteExpr(
5966 T, E->isGlobalDelete(),
5967 E->isArrayForm(),
5968 E->isArrayFormAsWritten(),
5969 E->doesUsualArrayDeleteWantSize(),
5970 OperatorDeleteDecl,
5971 ToArg,
5972 Importer.Import(E->getLocStart()));
5973}
5974
5975Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
5976 QualType T = Importer.Import(E->getType());
5977 if (T.isNull())
5978 return nullptr;
5979
5980 auto *ToCCD =
5981 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
5982 if (!ToCCD)
5983 return nullptr;
5984
5985 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
5986 if (ImportContainerChecked(E->arguments(), ToArgs))
5987 return nullptr;
5988
5989 return CXXConstructExpr::Create(Importer.getToContext(), T,
5990 Importer.Import(E->getLocation()),
5991 ToCCD, E->isElidable(),
5992 ToArgs, E->hadMultipleCandidates(),
5993 E->isListInitialization(),
5994 E->isStdInitListInitialization(),
5995 E->requiresZeroInitialization(),
5996 E->getConstructionKind(),
5997 Importer.Import(E->getParenOrBraceRange()));
5998}
5999
6000Expr *ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *EWC) {
6001 Expr *SubExpr = Importer.Import(EWC->getSubExpr());
6002 if (!SubExpr && EWC->getSubExpr())
6003 return nullptr;
6004
6005 SmallVector<ExprWithCleanups::CleanupObject, 8> Objs(EWC->getNumObjects());
6006 for (unsigned I = 0, E = EWC->getNumObjects(); I < E; I++)
6007 if (ExprWithCleanups::CleanupObject Obj =
6008 cast_or_null<BlockDecl>(Importer.Import(EWC->getObject(I))))
6009 Objs[I] = Obj;
6010 else
6011 return nullptr;
6012
6013 return ExprWithCleanups::Create(Importer.getToContext(),
6014 SubExpr, EWC->cleanupsHaveSideEffects(),
6015 Objs);
6016}
6017
6018Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
6019 QualType T = Importer.Import(E->getType());
6020 if (T.isNull())
6021 return nullptr;
6022
6023 Expr *ToFn = Importer.Import(E->getCallee());
6024 if (!ToFn)
6025 return nullptr;
6026
6027 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
6028 if (ImportContainerChecked(E->arguments(), ToArgs))
6029 return nullptr;
6030
6031 return new (Importer.getToContext()) CXXMemberCallExpr(
6032 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
6033 Importer.Import(E->getRParenLoc()));
6034}
6035
6036Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
6037 QualType T = Importer.Import(E->getType());
6038 if (T.isNull())
6039 return nullptr;
6040
6041 return new (Importer.getToContext())
6042 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
6043}
6044
6045Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
6046 QualType T = Importer.Import(E->getType());
6047 if (T.isNull())
6048 return nullptr;
6049
6050 return new (Importer.getToContext())
6051 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
6052}
6053
6054
6055Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
6056 QualType T = Importer.Import(E->getType());
6057 if (T.isNull())
6058 return nullptr;
6059
6060 Expr *ToBase = Importer.Import(E->getBase());
6061 if (!ToBase && E->getBase())
6062 return nullptr;
6063
6064 auto *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
6065 if (!ToMember && E->getMemberDecl())
6066 return nullptr;
6067
6068 DeclAccessPair ToFoundDecl = DeclAccessPair::make(
6069 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
6070 E->getFoundDecl().getAccess());
6071
6072 DeclarationNameInfo ToMemberNameInfo(
6073 Importer.Import(E->getMemberNameInfo().getName()),
6074 Importer.Import(E->getMemberNameInfo().getLoc()));
6075
6076 if (E->hasExplicitTemplateArgs()) {
6077 return nullptr; // FIXME: handle template arguments
6078 }
6079
6080 return MemberExpr::Create(Importer.getToContext(), ToBase,
6081 E->isArrow(),
6082 Importer.Import(E->getOperatorLoc()),
6083 Importer.Import(E->getQualifierLoc()),
6084 Importer.Import(E->getTemplateKeywordLoc()),
6085 ToMember, ToFoundDecl, ToMemberNameInfo,
6086 nullptr, T, E->getValueKind(),
6087 E->getObjectKind());
6088}
6089
6090Expr *ASTNodeImporter::VisitCXXPseudoDestructorExpr(
6091 CXXPseudoDestructorExpr *E) {
6092 Expr *BaseE = Importer.Import(E->getBase());
6093 if (!BaseE)
6094 return nullptr;
6095
6096 TypeSourceInfo *ScopeInfo = Importer.Import(E->getScopeTypeInfo());
6097 if (!ScopeInfo && E->getScopeTypeInfo())
6098 return nullptr;
6099
6100 PseudoDestructorTypeStorage Storage;
6101 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
6102 IdentifierInfo *ToII = Importer.Import(FromII);
6103 if (!ToII)
6104 return nullptr;
6105 Storage = PseudoDestructorTypeStorage(
6106 ToII, Importer.Import(E->getDestroyedTypeLoc()));
6107 } else {
6108 TypeSourceInfo *TI = Importer.Import(E->getDestroyedTypeInfo());
6109 if (!TI)
6110 return nullptr;
6111 Storage = PseudoDestructorTypeStorage(TI);
6112 }
6113
6114 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
6115 Importer.getToContext(), BaseE, E->isArrow(),
6116 Importer.Import(E->getOperatorLoc()),
6117 Importer.Import(E->getQualifierLoc()),
6118 ScopeInfo, Importer.Import(E->getColonColonLoc()),
6119 Importer.Import(E->getTildeLoc()), Storage);
6120}
6121
6122Expr *ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
6123 CXXDependentScopeMemberExpr *E) {
6124 Expr *Base = nullptr;
6125 if (!E->isImplicitAccess()) {
6126 Base = Importer.Import(E->getBase());
6127 if (!Base)
6128 return nullptr;
6129 }
6130
6131 QualType BaseType = Importer.Import(E->getBaseType());
6132 if (BaseType.isNull())
6133 return nullptr;
6134
6135 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
6136 if (E->hasExplicitTemplateArgs()) {
6137 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6138 E->template_arguments(), ToTAInfo))
6139 return nullptr;
6140 ResInfo = &ToTAInfo;
6141 }
6142
6143 DeclarationName Name = Importer.Import(E->getMember());
6144 if (!E->getMember().isEmpty() && Name.isEmpty())
6145 return nullptr;
6146
6147 DeclarationNameInfo MemberNameInfo(Name, Importer.Import(E->getMemberLoc()));
6148 // Import additional name location/type info.
6149 ImportDeclarationNameLoc(E->getMemberNameInfo(), MemberNameInfo);
6150 auto ToFQ = Importer.Import(E->getFirstQualifierFoundInScope());
6151 if (!ToFQ && E->getFirstQualifierFoundInScope())
6152 return nullptr;
6153
6154 return CXXDependentScopeMemberExpr::Create(
6155 Importer.getToContext(), Base, BaseType, E->isArrow(),
6156 Importer.Import(E->getOperatorLoc()),
6157 Importer.Import(E->getQualifierLoc()),
6158 Importer.Import(E->getTemplateKeywordLoc()),
6159 cast_or_null<NamedDecl>(ToFQ), MemberNameInfo, ResInfo);
6160}
6161
6162Expr *ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
6163 CXXUnresolvedConstructExpr *CE) {
6164 unsigned NumArgs = CE->arg_size();
6165
6166 SmallVector<Expr *, 8> ToArgs(NumArgs);
6167 if (ImportArrayChecked(CE->arg_begin(), CE->arg_end(), ToArgs.begin()))
6168 return nullptr;
6169
6170 return CXXUnresolvedConstructExpr::Create(
6171 Importer.getToContext(), Importer.Import(CE->getTypeSourceInfo()),
6172 Importer.Import(CE->getLParenLoc()), llvm::makeArrayRef(ToArgs),
6173 Importer.Import(CE->getRParenLoc()));
6174}
6175
6176Expr *ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
6177 auto *NamingClass =
6178 cast_or_null<CXXRecordDecl>(Importer.Import(E->getNamingClass()));
6179 if (E->getNamingClass() && !NamingClass)
6180 return nullptr;
6181
6182 DeclarationName Name = Importer.Import(E->getName());
6183 if (E->getName() && !Name)
6184 return nullptr;
6185
6186 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc()));
6187 // Import additional name location/type info.
6188 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6189
6190 UnresolvedSet<8> ToDecls;
6191 for (auto *D : E->decls()) {
6192 if (auto *To = cast_or_null<NamedDecl>(Importer.Import(D)))
6193 ToDecls.addDecl(To);
6194 else
6195 return nullptr;
6196 }
6197
6198 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
6199 if (E->hasExplicitTemplateArgs()) {
6200 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6201 E->template_arguments(), ToTAInfo))
6202 return nullptr;
6203 ResInfo = &ToTAInfo;
6204 }
6205
6206 if (ResInfo || E->getTemplateKeywordLoc().isValid())
6207 return UnresolvedLookupExpr::Create(
6208 Importer.getToContext(), NamingClass,
6209 Importer.Import(E->getQualifierLoc()),
6210 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, E->requiresADL(),
6211 ResInfo, ToDecls.begin(), ToDecls.end());
6212
6213 return UnresolvedLookupExpr::Create(
6214 Importer.getToContext(), NamingClass,
6215 Importer.Import(E->getQualifierLoc()), NameInfo, E->requiresADL(),
6216 E->isOverloaded(), ToDecls.begin(), ToDecls.end());
6217}
6218
6219Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
6220 QualType T = Importer.Import(E->getType());
6221 if (T.isNull())
6222 return nullptr;
6223
6224 Expr *ToCallee = Importer.Import(E->getCallee());
6225 if (!ToCallee && E->getCallee())
6226 return nullptr;
6227
6228 unsigned NumArgs = E->getNumArgs();
6229 SmallVector<Expr *, 2> ToArgs(NumArgs);
6230 if (ImportContainerChecked(E->arguments(), ToArgs))
6231 return nullptr;
6232
6233 auto **ToArgs_Copied = new (Importer.getToContext()) Expr*[NumArgs];
6234
6235 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
6236 ToArgs_Copied[ai] = ToArgs[ai];
6237
6238 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
6239 return new (Importer.getToContext()) CXXOperatorCallExpr(
6240 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, T,
6241 OCE->getValueKind(), Importer.Import(OCE->getRParenLoc()),
6242 OCE->getFPFeatures());
6243 }
6244
6245 return new (Importer.getToContext())
6246 CallExpr(Importer.getToContext(), ToCallee,
6247 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
6248 Importer.Import(E->getRParenLoc()));
6249}
6250
6251Optional<LambdaCapture>
6252ASTNodeImporter::ImportLambdaCapture(const LambdaCapture &From) {
6253 VarDecl *Var = nullptr;
6254 if (From.capturesVariable()) {
6255 Var = cast_or_null<VarDecl>(Importer.Import(From.getCapturedVar()));
6256 if (!Var)
6257 return None;
6258 }
6259
6260 return LambdaCapture(Importer.Import(From.getLocation()), From.isImplicit(),
6261 From.getCaptureKind(), Var,
6262 From.isPackExpansion()
6263 ? Importer.Import(From.getEllipsisLoc())
6264 : SourceLocation());
6265}
6266
6267Expr *ASTNodeImporter::VisitLambdaExpr(LambdaExpr *LE) {
6268 CXXRecordDecl *FromClass = LE->getLambdaClass();
6269 auto *ToClass = dyn_cast_or_null<CXXRecordDecl>(Importer.Import(FromClass));
6270 if (!ToClass)
6271 return nullptr;
6272
6273 // NOTE: lambda classes are created with BeingDefined flag set up.
6274 // It means that ImportDefinition doesn't work for them and we should fill it
6275 // manually.
6276 if (ToClass->isBeingDefined()) {
6277 for (auto FromField : FromClass->fields()) {
6278 auto *ToField = cast_or_null<FieldDecl>(Importer.Import(FromField));
6279 if (!ToField)
6280 return nullptr;
6281 }
6282 }
6283
6284 auto *ToCallOp = dyn_cast_or_null<CXXMethodDecl>(
6285 Importer.Import(LE->getCallOperator()));
6286 if (!ToCallOp)
6287 return nullptr;
6288
6289 ToClass->completeDefinition();
6290
6291 unsigned NumCaptures = LE->capture_size();
6292 SmallVector<LambdaCapture, 8> Captures;
6293 Captures.reserve(NumCaptures);
6294 for (const auto &FromCapture : LE->captures()) {
6295 if (auto ToCapture = ImportLambdaCapture(FromCapture))
6296 Captures.push_back(*ToCapture);
6297 else
6298 return nullptr;
6299 }
6300
6301 SmallVector<Expr *, 8> InitCaptures(NumCaptures);
6302 if (ImportContainerChecked(LE->capture_inits(), InitCaptures))
6303 return nullptr;
6304
6305 return LambdaExpr::Create(Importer.getToContext(), ToClass,
6306 Importer.Import(LE->getIntroducerRange()),
6307 LE->getCaptureDefault(),
6308 Importer.Import(LE->getCaptureDefaultLoc()),
6309 Captures,
6310 LE->hasExplicitParameters(),
6311 LE->hasExplicitResultType(),
6312 InitCaptures,
6313 Importer.Import(LE->getLocEnd()),
6314 LE->containsUnexpandedParameterPack());
6315}
6316
6317Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
6318 QualType T = Importer.Import(ILE->getType());
6319 if (T.isNull())
6320 return nullptr;
6321
6322 SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
6323 if (ImportContainerChecked(ILE->inits(), Exprs))
6324 return nullptr;
6325
6326 ASTContext &ToCtx = Importer.getToContext();
6327 InitListExpr *To = new (ToCtx) InitListExpr(
6328 ToCtx, Importer.Import(ILE->getLBraceLoc()),
6329 Exprs, Importer.Import(ILE->getLBraceLoc()));
6330 To->setType(T);
6331
6332 if (ILE->hasArrayFiller()) {
6333 Expr *Filler = Importer.Import(ILE->getArrayFiller());
6334 if (!Filler)
6335 return nullptr;
6336 To->setArrayFiller(Filler);
6337 }
6338
6339 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
6340 auto *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
6341 if (!ToFD)
6342 return nullptr;
6343 To->setInitializedFieldInUnion(ToFD);
6344 }
6345
6346 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
6347 auto *ToSyntForm = cast_or_null<InitListExpr>(Importer.Import(SyntForm));
6348 if (!ToSyntForm)
6349 return nullptr;
6350 To->setSyntacticForm(ToSyntForm);
6351 }
6352
6353 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
6354 To->setValueDependent(ILE->isValueDependent());
6355 To->setInstantiationDependent(ILE->isInstantiationDependent());
6356
6357 return To;
6358}
6359
6360Expr *ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
6361 QualType ToType = Importer.Import(E->getType());
6362 if (ToType.isNull())
6363 return nullptr;
6364
6365 Expr *ToCommon = Importer.Import(E->getCommonExpr());
6366 if (!ToCommon && E->getCommonExpr())
6367 return nullptr;
6368
6369 Expr *ToSubExpr = Importer.Import(E->getSubExpr());
6370 if (!ToSubExpr && E->getSubExpr())
6371 return nullptr;
6372
6373 return new (Importer.getToContext())
6374 ArrayInitLoopExpr(ToType, ToCommon, ToSubExpr);
6375}
6376
6377Expr *ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
6378 QualType ToType = Importer.Import(E->getType());
6379