Bug Summary

File:clang/lib/AST/ASTImporter.cpp
Warning:line 208, column 16
2nd function call argument is an uninitialized value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -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 -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -relaxed-aliasing -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -fno-split-dwarf-inlining -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-12/lib/clang/12.0.0 -D CLANG_VENDOR="Debian " -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-12~++20200806111125+5446ec85070/build-llvm/tools/clang/lib/AST -I /build/llvm-toolchain-snapshot-12~++20200806111125+5446ec85070/clang/lib/AST -I /build/llvm-toolchain-snapshot-12~++20200806111125+5446ec85070/clang/include -I /build/llvm-toolchain-snapshot-12~++20200806111125+5446ec85070/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-12~++20200806111125+5446ec85070/build-llvm/include -I /build/llvm-toolchain-snapshot-12~++20200806111125+5446ec85070/llvm/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/local/include -internal-isystem /usr/lib/llvm-12/lib/clang/12.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++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-12~++20200806111125+5446ec85070/build-llvm/tools/clang/lib/AST -fdebug-prefix-map=/build/llvm-toolchain-snapshot-12~++20200806111125+5446ec85070=. -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2020-08-06-171148-17323-1 -x c++ /build/llvm-toolchain-snapshot-12~++20200806111125+5446ec85070/clang/lib/AST/ASTImporter.cpp

/build/llvm-toolchain-snapshot-12~++20200806111125+5446ec85070/clang/lib/AST/ASTImporter.cpp

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