Bug Summary

File:tools/clang/lib/AST/ASTImporter.cpp
Warning:line 265, column 16
3rd function call argument is an uninitialized value

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-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-8/lib/clang/8.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/tools/clang/lib/AST -I /build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/AST -I /build/llvm-toolchain-snapshot-8~svn345461/tools/clang/include -I /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/include -I /build/llvm-toolchain-snapshot-8~svn345461/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/include/clang/8.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-8/lib/clang/8.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-8~svn345461/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-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-10-27-211344-32123-1 -x c++ /build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/AST/ASTImporter.cpp -faddrsig

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