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/ASTContext.h" |
16 | #include "clang/AST/ASTDiagnostic.h" |
17 | #include "clang/AST/ASTImporterSharedState.h" |
18 | #include "clang/AST/ASTLambda.h" |
19 | #include "clang/AST/ASTStructuralEquivalence.h" |
20 | #include "clang/AST/Attr.h" |
21 | #include "clang/AST/Decl.h" |
22 | #include "clang/AST/DeclAccessPair.h" |
23 | #include "clang/AST/DeclBase.h" |
24 | #include "clang/AST/DeclCXX.h" |
25 | #include "clang/AST/DeclFriend.h" |
26 | #include "clang/AST/DeclGroup.h" |
27 | #include "clang/AST/DeclObjC.h" |
28 | #include "clang/AST/DeclTemplate.h" |
29 | #include "clang/AST/DeclVisitor.h" |
30 | #include "clang/AST/DeclarationName.h" |
31 | #include "clang/AST/Expr.h" |
32 | #include "clang/AST/ExprCXX.h" |
33 | #include "clang/AST/ExprObjC.h" |
34 | #include "clang/AST/ExternalASTSource.h" |
35 | #include "clang/AST/LambdaCapture.h" |
36 | #include "clang/AST/NestedNameSpecifier.h" |
37 | #include "clang/AST/OperationKinds.h" |
38 | #include "clang/AST/Stmt.h" |
39 | #include "clang/AST/StmtCXX.h" |
40 | #include "clang/AST/StmtObjC.h" |
41 | #include "clang/AST/StmtVisitor.h" |
42 | #include "clang/AST/TemplateBase.h" |
43 | #include "clang/AST/TemplateName.h" |
44 | #include "clang/AST/Type.h" |
45 | #include "clang/AST/TypeLoc.h" |
46 | #include "clang/AST/TypeVisitor.h" |
47 | #include "clang/AST/UnresolvedSet.h" |
48 | #include "clang/Basic/Builtins.h" |
49 | #include "clang/Basic/ExceptionSpecificationType.h" |
50 | #include "clang/Basic/FileManager.h" |
51 | #include "clang/Basic/IdentifierTable.h" |
52 | #include "clang/Basic/LLVM.h" |
53 | #include "clang/Basic/LangOptions.h" |
54 | #include "clang/Basic/SourceLocation.h" |
55 | #include "clang/Basic/SourceManager.h" |
56 | #include "clang/Basic/Specifiers.h" |
57 | #include "llvm/ADT/ArrayRef.h" |
58 | #include "llvm/ADT/DenseMap.h" |
59 | #include "llvm/ADT/STLExtras.h" |
60 | #include "llvm/ADT/ScopeExit.h" |
61 | #include "llvm/ADT/SmallVector.h" |
62 | #include "llvm/Support/ErrorHandling.h" |
63 | #include "llvm/Support/MemoryBuffer.h" |
64 | #include <algorithm> |
65 | #include <cassert> |
66 | #include <cstddef> |
67 | #include <memory> |
68 | #include <optional> |
69 | #include <type_traits> |
70 | #include <utility> |
71 | |
72 | namespace clang { |
73 | |
74 | using llvm::make_error; |
75 | using llvm::Error; |
76 | using llvm::Expected; |
77 | using ExpectedTypePtr = llvm::Expected<const Type *>; |
78 | using ExpectedType = llvm::Expected<QualType>; |
79 | using ExpectedStmt = llvm::Expected<Stmt *>; |
80 | using ExpectedExpr = llvm::Expected<Expr *>; |
81 | using ExpectedDecl = llvm::Expected<Decl *>; |
82 | using ExpectedSLoc = llvm::Expected<SourceLocation>; |
83 | using ExpectedName = llvm::Expected<DeclarationName>; |
84 | |
85 | std::string ASTImportError::toString() const { |
86 | // FIXME: Improve error texts. |
87 | switch (Error) { |
88 | case NameConflict: |
89 | return "NameConflict" ; |
90 | case UnsupportedConstruct: |
91 | return "UnsupportedConstruct" ; |
92 | case Unknown: |
93 | return "Unknown error" ; |
94 | } |
95 | llvm_unreachable("Invalid error code." ); |
96 | return "Invalid error code." ; |
97 | } |
98 | |
99 | void ASTImportError::log(raw_ostream &OS) const { OS << toString(); } |
100 | |
101 | std::error_code ASTImportError::convertToErrorCode() const { |
102 | llvm_unreachable("Function not implemented." ); |
103 | } |
104 | |
105 | char ASTImportError::ID; |
106 | |
107 | template <class T> |
108 | static SmallVector<Decl *, 2> |
109 | getCanonicalForwardRedeclChain(Redeclarable<T> *D) { |
110 | SmallVector<Decl *, 2> Redecls; |
111 | for (auto *R : D->getFirstDecl()->redecls()) { |
112 | if (R != D->getFirstDecl()) |
113 | Redecls.push_back(Elt: R); |
114 | } |
115 | Redecls.push_back(Elt: D->getFirstDecl()); |
116 | std::reverse(first: Redecls.begin(), last: Redecls.end()); |
117 | return Redecls; |
118 | } |
119 | |
120 | SmallVector<Decl*, 2> getCanonicalForwardRedeclChain(Decl* D) { |
121 | if (auto *FD = dyn_cast<FunctionDecl>(Val: D)) |
122 | return getCanonicalForwardRedeclChain<FunctionDecl>(D: FD); |
123 | if (auto *VD = dyn_cast<VarDecl>(Val: D)) |
124 | return getCanonicalForwardRedeclChain<VarDecl>(D: VD); |
125 | if (auto *TD = dyn_cast<TagDecl>(Val: D)) |
126 | return getCanonicalForwardRedeclChain<TagDecl>(D: TD); |
127 | llvm_unreachable("Bad declaration kind" ); |
128 | } |
129 | |
130 | static void updateFlags(const Decl *From, Decl *To) { |
131 | // Check if some flags or attrs are new in 'From' and copy into 'To'. |
132 | // FIXME: Other flags or attrs? |
133 | if (From->isUsed(CheckUsedAttr: false) && !To->isUsed(CheckUsedAttr: false)) |
134 | To->setIsUsed(); |
135 | } |
136 | |
137 | /// How to handle import errors that occur when import of a child declaration |
138 | /// of a DeclContext fails. |
139 | class ChildErrorHandlingStrategy { |
140 | /// This context is imported (in the 'from' domain). |
141 | /// It is nullptr if a non-DeclContext is imported. |
142 | const DeclContext *const FromDC; |
143 | /// Ignore import errors of the children. |
144 | /// If true, the context can be imported successfully if a child |
145 | /// of it failed to import. Otherwise the import errors of the child nodes |
146 | /// are accumulated (joined) into the import error object of the parent. |
147 | /// (Import of a parent can fail in other ways.) |
148 | bool const IgnoreChildErrors; |
149 | |
150 | public: |
151 | ChildErrorHandlingStrategy(const DeclContext *FromDC) |
152 | : FromDC(FromDC), IgnoreChildErrors(!isa<TagDecl>(Val: FromDC)) {} |
153 | ChildErrorHandlingStrategy(const Decl *FromD) |
154 | : FromDC(dyn_cast<DeclContext>(Val: FromD)), |
155 | IgnoreChildErrors(!isa<TagDecl>(Val: FromD)) {} |
156 | |
157 | /// Process the import result of a child (of the current declaration). |
158 | /// \param ResultErr The import error that can be used as result of |
159 | /// importing the parent. This may be changed by the function. |
160 | /// \param ChildErr Result of importing a child. Can be success or error. |
161 | void handleChildImportResult(Error &ResultErr, Error &&ChildErr) { |
162 | if (ChildErr && !IgnoreChildErrors) |
163 | ResultErr = joinErrors(E1: std::move(ResultErr), E2: std::move(ChildErr)); |
164 | else |
165 | consumeError(Err: std::move(ChildErr)); |
166 | } |
167 | |
168 | /// Determine if import failure of a child does not cause import failure of |
169 | /// its parent. |
170 | bool ignoreChildErrorOnParent(Decl *FromChildD) const { |
171 | if (!IgnoreChildErrors || !FromDC) |
172 | return false; |
173 | return FromDC->containsDecl(D: FromChildD); |
174 | } |
175 | }; |
176 | |
177 | class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, ExpectedType>, |
178 | public DeclVisitor<ASTNodeImporter, ExpectedDecl>, |
179 | public StmtVisitor<ASTNodeImporter, ExpectedStmt> { |
180 | ASTImporter &Importer; |
181 | |
182 | // Use this instead of Importer.importInto . |
183 | template <typename ImportT> |
184 | [[nodiscard]] Error importInto(ImportT &To, const ImportT &From) { |
185 | return Importer.importInto(To, From); |
186 | } |
187 | |
188 | // Use this to import pointers of specific type. |
189 | template <typename ImportT> |
190 | [[nodiscard]] Error importInto(ImportT *&To, ImportT *From) { |
191 | auto ToOrErr = Importer.Import(From); |
192 | if (ToOrErr) |
193 | To = cast_or_null<ImportT>(*ToOrErr); |
194 | return ToOrErr.takeError(); |
195 | } |
196 | |
197 | // Call the import function of ASTImporter for a baseclass of type `T` and |
198 | // cast the return value to `T`. |
199 | template <typename T> |
200 | auto import(T *From) |
201 | -> std::conditional_t<std::is_base_of_v<Type, T>, Expected<const T *>, |
202 | Expected<T *>> { |
203 | auto ToOrErr = Importer.Import(From); |
204 | if (!ToOrErr) |
205 | return ToOrErr.takeError(); |
206 | return cast_or_null<T>(*ToOrErr); |
207 | } |
208 | |
209 | template <typename T> |
210 | auto import(const T *From) { |
211 | return import(const_cast<T *>(From)); |
212 | } |
213 | |
214 | // Call the import function of ASTImporter for type `T`. |
215 | template <typename T> |
216 | Expected<T> import(const T &From) { |
217 | return Importer.Import(From); |
218 | } |
219 | |
220 | // Import an std::optional<T> by importing the contained T, if any. |
221 | template <typename T> |
222 | Expected<std::optional<T>> import(std::optional<T> From) { |
223 | if (!From) |
224 | return std::nullopt; |
225 | return import(*From); |
226 | } |
227 | |
228 | ExplicitSpecifier importExplicitSpecifier(Error &Err, |
229 | ExplicitSpecifier ESpec); |
230 | |
231 | // Wrapper for an overload set. |
232 | template <typename ToDeclT> struct CallOverloadedCreateFun { |
233 | template <typename... Args> decltype(auto) operator()(Args &&... args) { |
234 | return ToDeclT::Create(std::forward<Args>(args)...); |
235 | } |
236 | }; |
237 | |
238 | // Always use these functions to create a Decl during import. There are |
239 | // certain tasks which must be done after the Decl was created, e.g. we |
240 | // must immediately register that as an imported Decl. The parameter `ToD` |
241 | // will be set to the newly created Decl or if had been imported before |
242 | // then to the already imported Decl. Returns a bool value set to true if |
243 | // the `FromD` had been imported before. |
244 | template <typename ToDeclT, typename FromDeclT, typename... Args> |
245 | [[nodiscard]] bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD, |
246 | Args &&...args) { |
247 | // There may be several overloads of ToDeclT::Create. We must make sure |
248 | // to call the one which would be chosen by the arguments, thus we use a |
249 | // wrapper for the overload set. |
250 | CallOverloadedCreateFun<ToDeclT> OC; |
251 | return GetImportedOrCreateSpecialDecl(ToD, OC, FromD, |
252 | std::forward<Args>(args)...); |
253 | } |
254 | // Use this overload if a special Type is needed to be created. E.g if we |
255 | // want to create a `TypeAliasDecl` and assign that to a `TypedefNameDecl` |
256 | // then: |
257 | // TypedefNameDecl *ToTypedef; |
258 | // GetImportedOrCreateDecl<TypeAliasDecl>(ToTypedef, FromD, ...); |
259 | template <typename NewDeclT, typename ToDeclT, typename FromDeclT, |
260 | typename... Args> |
261 | [[nodiscard]] bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD, |
262 | Args &&...args) { |
263 | CallOverloadedCreateFun<NewDeclT> OC; |
264 | return GetImportedOrCreateSpecialDecl(ToD, OC, FromD, |
265 | std::forward<Args>(args)...); |
266 | } |
267 | // Use this version if a special create function must be |
268 | // used, e.g. CXXRecordDecl::CreateLambda . |
269 | template <typename ToDeclT, typename CreateFunT, typename FromDeclT, |
270 | typename... Args> |
271 | [[nodiscard]] bool |
272 | GetImportedOrCreateSpecialDecl(ToDeclT *&ToD, CreateFunT CreateFun, |
273 | FromDeclT *FromD, Args &&...args) { |
274 | if (Importer.getImportDeclErrorIfAny(FromD)) { |
275 | ToD = nullptr; |
276 | return true; // Already imported but with error. |
277 | } |
278 | ToD = cast_or_null<ToDeclT>(Importer.GetAlreadyImportedOrNull(FromD)); |
279 | if (ToD) |
280 | return true; // Already imported. |
281 | ToD = CreateFun(std::forward<Args>(args)...); |
282 | // Keep track of imported Decls. |
283 | Importer.RegisterImportedDecl(FromD, ToD); |
284 | Importer.SharedState->markAsNewDecl(ToD); |
285 | InitializeImportedDecl(FromD, ToD); |
286 | return false; // A new Decl is created. |
287 | } |
288 | |
289 | void InitializeImportedDecl(Decl *FromD, Decl *ToD) { |
290 | ToD->IdentifierNamespace = FromD->IdentifierNamespace; |
291 | if (FromD->isUsed()) |
292 | ToD->setIsUsed(); |
293 | if (FromD->isImplicit()) |
294 | ToD->setImplicit(); |
295 | } |
296 | |
297 | // Check if we have found an existing definition. Returns with that |
298 | // definition if yes, otherwise returns null. |
299 | Decl *FindAndMapDefinition(FunctionDecl *D, FunctionDecl *FoundFunction) { |
300 | const FunctionDecl *Definition = nullptr; |
301 | if (D->doesThisDeclarationHaveABody() && |
302 | FoundFunction->hasBody(Definition)) |
303 | return Importer.MapImported(From: D, To: const_cast<FunctionDecl *>(Definition)); |
304 | return nullptr; |
305 | } |
306 | |
307 | void addDeclToContexts(Decl *FromD, Decl *ToD) { |
308 | if (Importer.isMinimalImport()) { |
309 | // In minimal import case the decl must be added even if it is not |
310 | // contained in original context, for LLDB compatibility. |
311 | // FIXME: Check if a better solution is possible. |
312 | if (!FromD->getDescribedTemplate() && |
313 | FromD->getFriendObjectKind() == Decl::FOK_None) |
314 | ToD->getLexicalDeclContext()->addDeclInternal(D: ToD); |
315 | return; |
316 | } |
317 | |
318 | DeclContext *FromDC = FromD->getDeclContext(); |
319 | DeclContext *FromLexicalDC = FromD->getLexicalDeclContext(); |
320 | DeclContext *ToDC = ToD->getDeclContext(); |
321 | DeclContext *ToLexicalDC = ToD->getLexicalDeclContext(); |
322 | |
323 | bool Visible = false; |
324 | if (FromDC->containsDeclAndLoad(D: FromD)) { |
325 | ToDC->addDeclInternal(D: ToD); |
326 | Visible = true; |
327 | } |
328 | if (ToDC != ToLexicalDC && FromLexicalDC->containsDeclAndLoad(D: FromD)) { |
329 | ToLexicalDC->addDeclInternal(D: ToD); |
330 | Visible = true; |
331 | } |
332 | |
333 | // If the Decl was added to any context, it was made already visible. |
334 | // Otherwise it is still possible that it should be visible. |
335 | if (!Visible) { |
336 | if (auto *FromNamed = dyn_cast<NamedDecl>(Val: FromD)) { |
337 | auto *ToNamed = cast<NamedDecl>(Val: ToD); |
338 | DeclContextLookupResult FromLookup = |
339 | FromDC->lookup(Name: FromNamed->getDeclName()); |
340 | if (llvm::is_contained(Range&: FromLookup, Element: FromNamed)) |
341 | ToDC->makeDeclVisibleInContext(D: ToNamed); |
342 | } |
343 | } |
344 | } |
345 | |
346 | void updateLookupTableForTemplateParameters(TemplateParameterList &Params, |
347 | DeclContext *OldDC) { |
348 | ASTImporterLookupTable *LT = Importer.SharedState->getLookupTable(); |
349 | if (!LT) |
350 | return; |
351 | |
352 | for (NamedDecl *TP : Params) |
353 | LT->update(ND: TP, OldDC); |
354 | } |
355 | |
356 | void updateLookupTableForTemplateParameters(TemplateParameterList &Params) { |
357 | updateLookupTableForTemplateParameters( |
358 | Params, OldDC: Importer.getToContext().getTranslationUnitDecl()); |
359 | } |
360 | |
361 | template <typename TemplateParmDeclT> |
362 | Error importTemplateParameterDefaultArgument(const TemplateParmDeclT *D, |
363 | TemplateParmDeclT *ToD) { |
364 | if (D->hasDefaultArgument()) { |
365 | if (D->defaultArgumentWasInherited()) { |
366 | Expected<TemplateParmDeclT *> ToInheritedFromOrErr = |
367 | import(D->getDefaultArgStorage().getInheritedFrom()); |
368 | if (!ToInheritedFromOrErr) |
369 | return ToInheritedFromOrErr.takeError(); |
370 | TemplateParmDeclT *ToInheritedFrom = *ToInheritedFromOrErr; |
371 | if (!ToInheritedFrom->hasDefaultArgument()) { |
372 | // Resolve possible circular dependency between default value of the |
373 | // template argument and the template declaration. |
374 | Expected<TemplateArgumentLoc> ToInheritedDefaultArgOrErr = |
375 | import(D->getDefaultArgStorage() |
376 | .getInheritedFrom() |
377 | ->getDefaultArgument()); |
378 | if (!ToInheritedDefaultArgOrErr) |
379 | return ToInheritedDefaultArgOrErr.takeError(); |
380 | ToInheritedFrom->setDefaultArgument(Importer.getToContext(), |
381 | *ToInheritedDefaultArgOrErr); |
382 | } |
383 | ToD->setInheritedDefaultArgument(ToD->getASTContext(), |
384 | ToInheritedFrom); |
385 | } else { |
386 | Expected<TemplateArgumentLoc> ToDefaultArgOrErr = |
387 | import(D->getDefaultArgument()); |
388 | if (!ToDefaultArgOrErr) |
389 | return ToDefaultArgOrErr.takeError(); |
390 | // Default argument could have been set in the |
391 | // '!ToInheritedFrom->hasDefaultArgument()' branch above. |
392 | if (!ToD->hasDefaultArgument()) |
393 | ToD->setDefaultArgument(Importer.getToContext(), |
394 | *ToDefaultArgOrErr); |
395 | } |
396 | } |
397 | return Error::success(); |
398 | } |
399 | |
400 | public: |
401 | explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) {} |
402 | |
403 | using TypeVisitor<ASTNodeImporter, ExpectedType>::Visit; |
404 | using DeclVisitor<ASTNodeImporter, ExpectedDecl>::Visit; |
405 | using StmtVisitor<ASTNodeImporter, ExpectedStmt>::Visit; |
406 | |
407 | // Importing types |
408 | ExpectedType VisitType(const Type *T); |
409 | #define TYPE(Class, Base) \ |
410 | ExpectedType Visit##Class##Type(const Class##Type *T); |
411 | #include "clang/AST/TypeNodes.inc" |
412 | |
413 | // Importing declarations |
414 | Error ImportDeclParts(NamedDecl *D, DeclarationName &Name, NamedDecl *&ToD, |
415 | SourceLocation &Loc); |
416 | Error ImportDeclParts( |
417 | NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC, |
418 | DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc); |
419 | Error ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr); |
420 | Error ImportDeclarationNameLoc( |
421 | const DeclarationNameInfo &From, DeclarationNameInfo &To); |
422 | Error ImportDeclContext(DeclContext *FromDC, bool ForceImport = false); |
423 | Error ImportDeclContext( |
424 | Decl *From, DeclContext *&ToDC, DeclContext *&ToLexicalDC); |
425 | Error ImportImplicitMethods(const CXXRecordDecl *From, CXXRecordDecl *To); |
426 | |
427 | Error ImportFieldDeclDefinition(const FieldDecl *From, const FieldDecl *To); |
428 | Expected<CXXCastPath> ImportCastPath(CastExpr *E); |
429 | Expected<APValue> ImportAPValue(const APValue &FromValue); |
430 | |
431 | using Designator = DesignatedInitExpr::Designator; |
432 | |
433 | /// What we should import from the definition. |
434 | enum ImportDefinitionKind { |
435 | /// Import the default subset of the definition, which might be |
436 | /// nothing (if minimal import is set) or might be everything (if minimal |
437 | /// import is not set). |
438 | IDK_Default, |
439 | /// Import everything. |
440 | IDK_Everything, |
441 | /// Import only the bare bones needed to establish a valid |
442 | /// DeclContext. |
443 | IDK_Basic |
444 | }; |
445 | |
446 | bool shouldForceImportDeclContext(ImportDefinitionKind IDK) { |
447 | return IDK == IDK_Everything || |
448 | (IDK == IDK_Default && !Importer.isMinimalImport()); |
449 | } |
450 | |
451 | Error ImportInitializer(VarDecl *From, VarDecl *To); |
452 | Error ImportDefinition( |
453 | RecordDecl *From, RecordDecl *To, |
454 | ImportDefinitionKind Kind = IDK_Default); |
455 | Error ImportDefinition( |
456 | EnumDecl *From, EnumDecl *To, |
457 | ImportDefinitionKind Kind = IDK_Default); |
458 | Error ImportDefinition( |
459 | ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, |
460 | ImportDefinitionKind Kind = IDK_Default); |
461 | Error ImportDefinition( |
462 | ObjCProtocolDecl *From, ObjCProtocolDecl *To, |
463 | ImportDefinitionKind Kind = IDK_Default); |
464 | Error ImportTemplateArguments(ArrayRef<TemplateArgument> FromArgs, |
465 | SmallVectorImpl<TemplateArgument> &ToArgs); |
466 | Expected<TemplateArgument> |
467 | ImportTemplateArgument(const TemplateArgument &From); |
468 | |
469 | template <typename InContainerTy> |
470 | Error ImportTemplateArgumentListInfo( |
471 | const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo); |
472 | |
473 | template<typename InContainerTy> |
474 | Error ImportTemplateArgumentListInfo( |
475 | SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc, |
476 | const InContainerTy &Container, TemplateArgumentListInfo &Result); |
477 | |
478 | using TemplateArgsTy = SmallVector<TemplateArgument, 8>; |
479 | using FunctionTemplateAndArgsTy = |
480 | std::tuple<FunctionTemplateDecl *, TemplateArgsTy>; |
481 | Expected<FunctionTemplateAndArgsTy> |
482 | ImportFunctionTemplateWithTemplateArgsFromSpecialization( |
483 | FunctionDecl *FromFD); |
484 | |
485 | template <typename DeclTy> |
486 | Error ImportTemplateParameterLists(const DeclTy *FromD, DeclTy *ToD); |
487 | |
488 | Error ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD); |
489 | |
490 | Error ImportFunctionDeclBody(FunctionDecl *FromFD, FunctionDecl *ToFD); |
491 | |
492 | Error ImportDefaultArgOfParmVarDecl(const ParmVarDecl *FromParam, |
493 | ParmVarDecl *ToParam); |
494 | |
495 | Expected<InheritedConstructor> |
496 | ImportInheritedConstructor(const InheritedConstructor &From); |
497 | |
498 | template <typename T> |
499 | bool hasSameVisibilityContextAndLinkage(T *Found, T *From); |
500 | |
501 | bool IsStructuralMatch(Decl *From, Decl *To, bool Complain = true, |
502 | bool IgnoreTemplateParmDepth = false); |
503 | ExpectedDecl VisitDecl(Decl *D); |
504 | ExpectedDecl VisitImportDecl(ImportDecl *D); |
505 | ExpectedDecl VisitEmptyDecl(EmptyDecl *D); |
506 | ExpectedDecl VisitAccessSpecDecl(AccessSpecDecl *D); |
507 | ExpectedDecl VisitStaticAssertDecl(StaticAssertDecl *D); |
508 | ExpectedDecl VisitTranslationUnitDecl(TranslationUnitDecl *D); |
509 | ExpectedDecl VisitBindingDecl(BindingDecl *D); |
510 | ExpectedDecl VisitNamespaceDecl(NamespaceDecl *D); |
511 | ExpectedDecl VisitNamespaceAliasDecl(NamespaceAliasDecl *D); |
512 | ExpectedDecl VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias); |
513 | ExpectedDecl VisitTypedefDecl(TypedefDecl *D); |
514 | ExpectedDecl VisitTypeAliasDecl(TypeAliasDecl *D); |
515 | ExpectedDecl VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D); |
516 | ExpectedDecl VisitLabelDecl(LabelDecl *D); |
517 | ExpectedDecl VisitEnumDecl(EnumDecl *D); |
518 | ExpectedDecl VisitRecordDecl(RecordDecl *D); |
519 | ExpectedDecl VisitEnumConstantDecl(EnumConstantDecl *D); |
520 | ExpectedDecl VisitFunctionDecl(FunctionDecl *D); |
521 | ExpectedDecl VisitCXXMethodDecl(CXXMethodDecl *D); |
522 | ExpectedDecl VisitCXXConstructorDecl(CXXConstructorDecl *D); |
523 | ExpectedDecl VisitCXXDestructorDecl(CXXDestructorDecl *D); |
524 | ExpectedDecl VisitCXXConversionDecl(CXXConversionDecl *D); |
525 | ExpectedDecl VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D); |
526 | ExpectedDecl VisitFieldDecl(FieldDecl *D); |
527 | ExpectedDecl VisitIndirectFieldDecl(IndirectFieldDecl *D); |
528 | ExpectedDecl VisitFriendDecl(FriendDecl *D); |
529 | ExpectedDecl VisitObjCIvarDecl(ObjCIvarDecl *D); |
530 | ExpectedDecl VisitVarDecl(VarDecl *D); |
531 | ExpectedDecl VisitImplicitParamDecl(ImplicitParamDecl *D); |
532 | ExpectedDecl VisitParmVarDecl(ParmVarDecl *D); |
533 | ExpectedDecl VisitObjCMethodDecl(ObjCMethodDecl *D); |
534 | ExpectedDecl VisitObjCTypeParamDecl(ObjCTypeParamDecl *D); |
535 | ExpectedDecl VisitObjCCategoryDecl(ObjCCategoryDecl *D); |
536 | ExpectedDecl VisitObjCProtocolDecl(ObjCProtocolDecl *D); |
537 | ExpectedDecl VisitLinkageSpecDecl(LinkageSpecDecl *D); |
538 | ExpectedDecl VisitUsingDecl(UsingDecl *D); |
539 | ExpectedDecl VisitUsingShadowDecl(UsingShadowDecl *D); |
540 | ExpectedDecl VisitUsingDirectiveDecl(UsingDirectiveDecl *D); |
541 | ExpectedDecl VisitUsingPackDecl(UsingPackDecl *D); |
542 | ExpectedDecl ImportUsingShadowDecls(BaseUsingDecl *D, BaseUsingDecl *ToSI); |
543 | ExpectedDecl VisitUsingEnumDecl(UsingEnumDecl *D); |
544 | ExpectedDecl VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); |
545 | ExpectedDecl VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); |
546 | ExpectedDecl VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D); |
547 | ExpectedDecl |
548 | VisitLifetimeExtendedTemporaryDecl(LifetimeExtendedTemporaryDecl *D); |
549 | |
550 | Expected<ObjCTypeParamList *> |
551 | ImportObjCTypeParamList(ObjCTypeParamList *list); |
552 | |
553 | ExpectedDecl VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
554 | ExpectedDecl VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
555 | ExpectedDecl VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
556 | ExpectedDecl VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
557 | ExpectedDecl VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); |
558 | ExpectedDecl VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); |
559 | ExpectedDecl VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); |
560 | ExpectedDecl VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); |
561 | ExpectedDecl VisitClassTemplateDecl(ClassTemplateDecl *D); |
562 | ExpectedDecl VisitClassTemplateSpecializationDecl( |
563 | ClassTemplateSpecializationDecl *D); |
564 | ExpectedDecl VisitVarTemplateDecl(VarTemplateDecl *D); |
565 | ExpectedDecl VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D); |
566 | ExpectedDecl VisitFunctionTemplateDecl(FunctionTemplateDecl *D); |
567 | |
568 | // Importing statements |
569 | ExpectedStmt VisitStmt(Stmt *S); |
570 | ExpectedStmt VisitGCCAsmStmt(GCCAsmStmt *S); |
571 | ExpectedStmt VisitDeclStmt(DeclStmt *S); |
572 | ExpectedStmt VisitNullStmt(NullStmt *S); |
573 | ExpectedStmt VisitCompoundStmt(CompoundStmt *S); |
574 | ExpectedStmt VisitCaseStmt(CaseStmt *S); |
575 | ExpectedStmt VisitDefaultStmt(DefaultStmt *S); |
576 | ExpectedStmt VisitLabelStmt(LabelStmt *S); |
577 | ExpectedStmt VisitAttributedStmt(AttributedStmt *S); |
578 | ExpectedStmt VisitIfStmt(IfStmt *S); |
579 | ExpectedStmt VisitSwitchStmt(SwitchStmt *S); |
580 | ExpectedStmt VisitWhileStmt(WhileStmt *S); |
581 | ExpectedStmt VisitDoStmt(DoStmt *S); |
582 | ExpectedStmt VisitForStmt(ForStmt *S); |
583 | ExpectedStmt VisitGotoStmt(GotoStmt *S); |
584 | ExpectedStmt VisitIndirectGotoStmt(IndirectGotoStmt *S); |
585 | ExpectedStmt VisitContinueStmt(ContinueStmt *S); |
586 | ExpectedStmt VisitBreakStmt(BreakStmt *S); |
587 | ExpectedStmt VisitReturnStmt(ReturnStmt *S); |
588 | // FIXME: MSAsmStmt |
589 | // FIXME: SEHExceptStmt |
590 | // FIXME: SEHFinallyStmt |
591 | // FIXME: SEHTryStmt |
592 | // FIXME: SEHLeaveStmt |
593 | // FIXME: CapturedStmt |
594 | ExpectedStmt VisitCXXCatchStmt(CXXCatchStmt *S); |
595 | ExpectedStmt VisitCXXTryStmt(CXXTryStmt *S); |
596 | ExpectedStmt VisitCXXForRangeStmt(CXXForRangeStmt *S); |
597 | // FIXME: MSDependentExistsStmt |
598 | ExpectedStmt VisitObjCForCollectionStmt(ObjCForCollectionStmt *S); |
599 | ExpectedStmt VisitObjCAtCatchStmt(ObjCAtCatchStmt *S); |
600 | ExpectedStmt VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S); |
601 | ExpectedStmt VisitObjCAtTryStmt(ObjCAtTryStmt *S); |
602 | ExpectedStmt VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
603 | ExpectedStmt VisitObjCAtThrowStmt(ObjCAtThrowStmt *S); |
604 | ExpectedStmt VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S); |
605 | |
606 | // Importing expressions |
607 | ExpectedStmt VisitExpr(Expr *E); |
608 | ExpectedStmt VisitSourceLocExpr(SourceLocExpr *E); |
609 | ExpectedStmt VisitVAArgExpr(VAArgExpr *E); |
610 | ExpectedStmt VisitChooseExpr(ChooseExpr *E); |
611 | ExpectedStmt VisitConvertVectorExpr(ConvertVectorExpr *E); |
612 | ExpectedStmt VisitShuffleVectorExpr(ShuffleVectorExpr *E); |
613 | ExpectedStmt VisitGNUNullExpr(GNUNullExpr *E); |
614 | ExpectedStmt VisitGenericSelectionExpr(GenericSelectionExpr *E); |
615 | ExpectedStmt VisitPredefinedExpr(PredefinedExpr *E); |
616 | ExpectedStmt VisitDeclRefExpr(DeclRefExpr *E); |
617 | ExpectedStmt VisitImplicitValueInitExpr(ImplicitValueInitExpr *E); |
618 | ExpectedStmt VisitDesignatedInitExpr(DesignatedInitExpr *E); |
619 | ExpectedStmt VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E); |
620 | ExpectedStmt VisitIntegerLiteral(IntegerLiteral *E); |
621 | ExpectedStmt VisitFloatingLiteral(FloatingLiteral *E); |
622 | ExpectedStmt VisitImaginaryLiteral(ImaginaryLiteral *E); |
623 | ExpectedStmt VisitFixedPointLiteral(FixedPointLiteral *E); |
624 | ExpectedStmt VisitCharacterLiteral(CharacterLiteral *E); |
625 | ExpectedStmt VisitStringLiteral(StringLiteral *E); |
626 | ExpectedStmt VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
627 | ExpectedStmt VisitAtomicExpr(AtomicExpr *E); |
628 | ExpectedStmt VisitAddrLabelExpr(AddrLabelExpr *E); |
629 | ExpectedStmt VisitConstantExpr(ConstantExpr *E); |
630 | ExpectedStmt VisitParenExpr(ParenExpr *E); |
631 | ExpectedStmt VisitParenListExpr(ParenListExpr *E); |
632 | ExpectedStmt VisitStmtExpr(StmtExpr *E); |
633 | ExpectedStmt VisitUnaryOperator(UnaryOperator *E); |
634 | ExpectedStmt VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); |
635 | ExpectedStmt VisitBinaryOperator(BinaryOperator *E); |
636 | ExpectedStmt VisitConditionalOperator(ConditionalOperator *E); |
637 | ExpectedStmt VisitBinaryConditionalOperator(BinaryConditionalOperator *E); |
638 | ExpectedStmt VisitCXXRewrittenBinaryOperator(CXXRewrittenBinaryOperator *E); |
639 | ExpectedStmt VisitOpaqueValueExpr(OpaqueValueExpr *E); |
640 | ExpectedStmt VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E); |
641 | ExpectedStmt VisitExpressionTraitExpr(ExpressionTraitExpr *E); |
642 | ExpectedStmt VisitArraySubscriptExpr(ArraySubscriptExpr *E); |
643 | ExpectedStmt VisitCompoundAssignOperator(CompoundAssignOperator *E); |
644 | ExpectedStmt VisitImplicitCastExpr(ImplicitCastExpr *E); |
645 | ExpectedStmt VisitExplicitCastExpr(ExplicitCastExpr *E); |
646 | ExpectedStmt VisitOffsetOfExpr(OffsetOfExpr *OE); |
647 | ExpectedStmt VisitCXXThrowExpr(CXXThrowExpr *E); |
648 | ExpectedStmt VisitCXXNoexceptExpr(CXXNoexceptExpr *E); |
649 | ExpectedStmt VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E); |
650 | ExpectedStmt VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); |
651 | ExpectedStmt VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E); |
652 | ExpectedStmt VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E); |
653 | ExpectedStmt VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E); |
654 | ExpectedStmt VisitPackExpansionExpr(PackExpansionExpr *E); |
655 | ExpectedStmt VisitSizeOfPackExpr(SizeOfPackExpr *E); |
656 | ExpectedStmt VisitCXXNewExpr(CXXNewExpr *E); |
657 | ExpectedStmt VisitCXXDeleteExpr(CXXDeleteExpr *E); |
658 | ExpectedStmt VisitCXXConstructExpr(CXXConstructExpr *E); |
659 | ExpectedStmt VisitCXXMemberCallExpr(CXXMemberCallExpr *E); |
660 | ExpectedStmt VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E); |
661 | ExpectedStmt VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E); |
662 | ExpectedStmt VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E); |
663 | ExpectedStmt VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E); |
664 | ExpectedStmt VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E); |
665 | ExpectedStmt VisitExprWithCleanups(ExprWithCleanups *E); |
666 | ExpectedStmt VisitCXXThisExpr(CXXThisExpr *E); |
667 | ExpectedStmt VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E); |
668 | ExpectedStmt VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E); |
669 | ExpectedStmt VisitMemberExpr(MemberExpr *E); |
670 | ExpectedStmt VisitCallExpr(CallExpr *E); |
671 | ExpectedStmt VisitLambdaExpr(LambdaExpr *LE); |
672 | ExpectedStmt VisitInitListExpr(InitListExpr *E); |
673 | ExpectedStmt VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E); |
674 | ExpectedStmt VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E); |
675 | ExpectedStmt VisitArrayInitLoopExpr(ArrayInitLoopExpr *E); |
676 | ExpectedStmt VisitArrayInitIndexExpr(ArrayInitIndexExpr *E); |
677 | ExpectedStmt VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E); |
678 | ExpectedStmt VisitCXXNamedCastExpr(CXXNamedCastExpr *E); |
679 | ExpectedStmt VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E); |
680 | ExpectedStmt VisitTypeTraitExpr(TypeTraitExpr *E); |
681 | ExpectedStmt VisitCXXTypeidExpr(CXXTypeidExpr *E); |
682 | ExpectedStmt VisitCXXFoldExpr(CXXFoldExpr *E); |
683 | |
684 | // Helper for chaining together multiple imports. If an error is detected, |
685 | // subsequent imports will return default constructed nodes, so that failure |
686 | // can be detected with a single conditional branch after a sequence of |
687 | // imports. |
688 | template <typename T> T importChecked(Error &Err, const T &From) { |
689 | // Don't attempt to import nodes if we hit an error earlier. |
690 | if (Err) |
691 | return T{}; |
692 | Expected<T> MaybeVal = import(From); |
693 | if (!MaybeVal) { |
694 | Err = MaybeVal.takeError(); |
695 | return T{}; |
696 | } |
697 | return *MaybeVal; |
698 | } |
699 | |
700 | template<typename IIter, typename OIter> |
701 | Error ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) { |
702 | using ItemT = std::remove_reference_t<decltype(*Obegin)>; |
703 | for (; Ibegin != Iend; ++Ibegin, ++Obegin) { |
704 | Expected<ItemT> ToOrErr = import(*Ibegin); |
705 | if (!ToOrErr) |
706 | return ToOrErr.takeError(); |
707 | *Obegin = *ToOrErr; |
708 | } |
709 | return Error::success(); |
710 | } |
711 | |
712 | // Import every item from a container structure into an output container. |
713 | // If error occurs, stops at first error and returns the error. |
714 | // The output container should have space for all needed elements (it is not |
715 | // expanded, new items are put into from the beginning). |
716 | template<typename InContainerTy, typename OutContainerTy> |
717 | Error ImportContainerChecked( |
718 | const InContainerTy &InContainer, OutContainerTy &OutContainer) { |
719 | return ImportArrayChecked( |
720 | InContainer.begin(), InContainer.end(), OutContainer.begin()); |
721 | } |
722 | |
723 | template<typename InContainerTy, typename OIter> |
724 | Error ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) { |
725 | return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin); |
726 | } |
727 | |
728 | Error ImportOverriddenMethods(CXXMethodDecl *ToMethod, |
729 | CXXMethodDecl *FromMethod); |
730 | |
731 | Expected<FunctionDecl *> FindFunctionTemplateSpecialization( |
732 | FunctionDecl *FromFD); |
733 | |
734 | // Returns true if the given function has a placeholder return type and |
735 | // that type is declared inside the body of the function. |
736 | // E.g. auto f() { struct X{}; return X(); } |
737 | bool hasReturnTypeDeclaredInside(FunctionDecl *D); |
738 | }; |
739 | |
740 | template <typename InContainerTy> |
741 | Error ASTNodeImporter::ImportTemplateArgumentListInfo( |
742 | SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc, |
743 | const InContainerTy &Container, TemplateArgumentListInfo &Result) { |
744 | auto ToLAngleLocOrErr = import(From: FromLAngleLoc); |
745 | if (!ToLAngleLocOrErr) |
746 | return ToLAngleLocOrErr.takeError(); |
747 | auto ToRAngleLocOrErr = import(From: FromRAngleLoc); |
748 | if (!ToRAngleLocOrErr) |
749 | return ToRAngleLocOrErr.takeError(); |
750 | |
751 | TemplateArgumentListInfo ToTAInfo(*ToLAngleLocOrErr, *ToRAngleLocOrErr); |
752 | if (auto Err = ImportTemplateArgumentListInfo(Container, ToTAInfo)) |
753 | return Err; |
754 | Result = ToTAInfo; |
755 | return Error::success(); |
756 | } |
757 | |
758 | template <> |
759 | Error ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>( |
760 | const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) { |
761 | return ImportTemplateArgumentListInfo( |
762 | FromLAngleLoc: From.getLAngleLoc(), FromRAngleLoc: From.getRAngleLoc(), Container: From.arguments(), Result); |
763 | } |
764 | |
765 | template <> |
766 | Error ASTNodeImporter::ImportTemplateArgumentListInfo< |
767 | ASTTemplateArgumentListInfo>( |
768 | const ASTTemplateArgumentListInfo &From, |
769 | TemplateArgumentListInfo &Result) { |
770 | return ImportTemplateArgumentListInfo( |
771 | FromLAngleLoc: From.LAngleLoc, FromRAngleLoc: From.RAngleLoc, Container: From.arguments(), Result); |
772 | } |
773 | |
774 | Expected<ASTNodeImporter::FunctionTemplateAndArgsTy> |
775 | ASTNodeImporter::ImportFunctionTemplateWithTemplateArgsFromSpecialization( |
776 | FunctionDecl *FromFD) { |
777 | assert(FromFD->getTemplatedKind() == |
778 | FunctionDecl::TK_FunctionTemplateSpecialization); |
779 | |
780 | FunctionTemplateAndArgsTy Result; |
781 | |
782 | auto *FTSInfo = FromFD->getTemplateSpecializationInfo(); |
783 | if (Error Err = importInto(To&: std::get<0>(t&: Result), From: FTSInfo->getTemplate())) |
784 | return std::move(Err); |
785 | |
786 | // Import template arguments. |
787 | if (Error Err = ImportTemplateArguments(FromArgs: FTSInfo->TemplateArguments->asArray(), |
788 | ToArgs&: std::get<1>(t&: Result))) |
789 | return std::move(Err); |
790 | |
791 | return Result; |
792 | } |
793 | |
794 | template <> |
795 | Expected<TemplateParameterList *> |
796 | ASTNodeImporter::import(TemplateParameterList *From) { |
797 | SmallVector<NamedDecl *, 4> To(From->size()); |
798 | if (Error Err = ImportContainerChecked(InContainer: *From, OutContainer&: To)) |
799 | return std::move(Err); |
800 | |
801 | ExpectedExpr ToRequiresClause = import(From: From->getRequiresClause()); |
802 | if (!ToRequiresClause) |
803 | return ToRequiresClause.takeError(); |
804 | |
805 | auto ToTemplateLocOrErr = import(From: From->getTemplateLoc()); |
806 | if (!ToTemplateLocOrErr) |
807 | return ToTemplateLocOrErr.takeError(); |
808 | auto ToLAngleLocOrErr = import(From: From->getLAngleLoc()); |
809 | if (!ToLAngleLocOrErr) |
810 | return ToLAngleLocOrErr.takeError(); |
811 | auto ToRAngleLocOrErr = import(From: From->getRAngleLoc()); |
812 | if (!ToRAngleLocOrErr) |
813 | return ToRAngleLocOrErr.takeError(); |
814 | |
815 | return TemplateParameterList::Create( |
816 | C: Importer.getToContext(), |
817 | TemplateLoc: *ToTemplateLocOrErr, |
818 | LAngleLoc: *ToLAngleLocOrErr, |
819 | Params: To, |
820 | RAngleLoc: *ToRAngleLocOrErr, |
821 | RequiresClause: *ToRequiresClause); |
822 | } |
823 | |
824 | template <> |
825 | Expected<TemplateArgument> |
826 | ASTNodeImporter::import(const TemplateArgument &From) { |
827 | switch (From.getKind()) { |
828 | case TemplateArgument::Null: |
829 | return TemplateArgument(); |
830 | |
831 | case TemplateArgument::Type: { |
832 | ExpectedType ToTypeOrErr = import(From: From.getAsType()); |
833 | if (!ToTypeOrErr) |
834 | return ToTypeOrErr.takeError(); |
835 | return TemplateArgument(*ToTypeOrErr, /*isNullPtr*/ false, |
836 | From.getIsDefaulted()); |
837 | } |
838 | |
839 | case TemplateArgument::Integral: { |
840 | ExpectedType ToTypeOrErr = import(From: From.getIntegralType()); |
841 | if (!ToTypeOrErr) |
842 | return ToTypeOrErr.takeError(); |
843 | return TemplateArgument(From, *ToTypeOrErr); |
844 | } |
845 | |
846 | case TemplateArgument::Declaration: { |
847 | Expected<ValueDecl *> ToOrErr = import(From: From.getAsDecl()); |
848 | if (!ToOrErr) |
849 | return ToOrErr.takeError(); |
850 | ExpectedType ToTypeOrErr = import(From: From.getParamTypeForDecl()); |
851 | if (!ToTypeOrErr) |
852 | return ToTypeOrErr.takeError(); |
853 | return TemplateArgument(dyn_cast<ValueDecl>(Val: (*ToOrErr)->getCanonicalDecl()), |
854 | *ToTypeOrErr, From.getIsDefaulted()); |
855 | } |
856 | |
857 | case TemplateArgument::NullPtr: { |
858 | ExpectedType ToTypeOrErr = import(From: From.getNullPtrType()); |
859 | if (!ToTypeOrErr) |
860 | return ToTypeOrErr.takeError(); |
861 | return TemplateArgument(*ToTypeOrErr, /*isNullPtr*/ true, |
862 | From.getIsDefaulted()); |
863 | } |
864 | |
865 | case TemplateArgument::StructuralValue: { |
866 | ExpectedType ToTypeOrErr = import(From: From.getStructuralValueType()); |
867 | if (!ToTypeOrErr) |
868 | return ToTypeOrErr.takeError(); |
869 | Expected<APValue> ToValueOrErr = import(From: From.getAsStructuralValue()); |
870 | if (!ToValueOrErr) |
871 | return ToValueOrErr.takeError(); |
872 | return TemplateArgument(Importer.getToContext(), *ToTypeOrErr, |
873 | *ToValueOrErr); |
874 | } |
875 | |
876 | case TemplateArgument::Template: { |
877 | Expected<TemplateName> ToTemplateOrErr = import(From: From.getAsTemplate()); |
878 | if (!ToTemplateOrErr) |
879 | return ToTemplateOrErr.takeError(); |
880 | |
881 | return TemplateArgument(*ToTemplateOrErr, From.getIsDefaulted()); |
882 | } |
883 | |
884 | case TemplateArgument::TemplateExpansion: { |
885 | Expected<TemplateName> ToTemplateOrErr = |
886 | import(From: From.getAsTemplateOrTemplatePattern()); |
887 | if (!ToTemplateOrErr) |
888 | return ToTemplateOrErr.takeError(); |
889 | |
890 | return TemplateArgument(*ToTemplateOrErr, From.getNumTemplateExpansions(), |
891 | From.getIsDefaulted()); |
892 | } |
893 | |
894 | case TemplateArgument::Expression: |
895 | if (ExpectedExpr ToExpr = import(From: From.getAsExpr())) |
896 | return TemplateArgument(*ToExpr, From.isCanonicalExpr(), |
897 | From.getIsDefaulted()); |
898 | else |
899 | return ToExpr.takeError(); |
900 | |
901 | case TemplateArgument::Pack: { |
902 | SmallVector<TemplateArgument, 2> ToPack; |
903 | ToPack.reserve(N: From.pack_size()); |
904 | if (Error Err = ImportTemplateArguments(FromArgs: From.pack_elements(), ToArgs&: ToPack)) |
905 | return std::move(Err); |
906 | |
907 | return TemplateArgument(ArrayRef(ToPack).copy(A&: Importer.getToContext())); |
908 | } |
909 | } |
910 | |
911 | llvm_unreachable("Invalid template argument kind" ); |
912 | } |
913 | |
914 | template <> |
915 | Expected<TemplateArgumentLoc> |
916 | ASTNodeImporter::import(const TemplateArgumentLoc &TALoc) { |
917 | Expected<TemplateArgument> ArgOrErr = import(From: TALoc.getArgument()); |
918 | if (!ArgOrErr) |
919 | return ArgOrErr.takeError(); |
920 | TemplateArgument Arg = *ArgOrErr; |
921 | |
922 | TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo(); |
923 | |
924 | TemplateArgumentLocInfo ToInfo; |
925 | if (Arg.getKind() == TemplateArgument::Expression) { |
926 | ExpectedExpr E = import(From: FromInfo.getAsExpr()); |
927 | if (!E) |
928 | return E.takeError(); |
929 | ToInfo = TemplateArgumentLocInfo(*E); |
930 | } else if (Arg.getKind() == TemplateArgument::Type) { |
931 | if (auto TSIOrErr = import(From: FromInfo.getAsTypeSourceInfo())) |
932 | ToInfo = TemplateArgumentLocInfo(*TSIOrErr); |
933 | else |
934 | return TSIOrErr.takeError(); |
935 | } else { |
936 | auto ToTemplateQualifierLocOrErr = |
937 | import(From: FromInfo.getTemplateQualifierLoc()); |
938 | if (!ToTemplateQualifierLocOrErr) |
939 | return ToTemplateQualifierLocOrErr.takeError(); |
940 | auto ToTemplateNameLocOrErr = import(From: FromInfo.getTemplateNameLoc()); |
941 | if (!ToTemplateNameLocOrErr) |
942 | return ToTemplateNameLocOrErr.takeError(); |
943 | auto ToTemplateEllipsisLocOrErr = |
944 | import(From: FromInfo.getTemplateEllipsisLoc()); |
945 | if (!ToTemplateEllipsisLocOrErr) |
946 | return ToTemplateEllipsisLocOrErr.takeError(); |
947 | ToInfo = TemplateArgumentLocInfo( |
948 | Importer.getToContext(), *ToTemplateQualifierLocOrErr, |
949 | *ToTemplateNameLocOrErr, *ToTemplateEllipsisLocOrErr); |
950 | } |
951 | |
952 | return TemplateArgumentLoc(Arg, ToInfo); |
953 | } |
954 | |
955 | template <> |
956 | Expected<DeclGroupRef> ASTNodeImporter::import(const DeclGroupRef &DG) { |
957 | if (DG.isNull()) |
958 | return DeclGroupRef::Create(C&: Importer.getToContext(), Decls: nullptr, NumDecls: 0); |
959 | size_t NumDecls = DG.end() - DG.begin(); |
960 | SmallVector<Decl *, 1> ToDecls; |
961 | ToDecls.reserve(N: NumDecls); |
962 | for (Decl *FromD : DG) { |
963 | if (auto ToDOrErr = import(From: FromD)) |
964 | ToDecls.push_back(Elt: *ToDOrErr); |
965 | else |
966 | return ToDOrErr.takeError(); |
967 | } |
968 | return DeclGroupRef::Create(C&: Importer.getToContext(), |
969 | Decls: ToDecls.begin(), |
970 | NumDecls); |
971 | } |
972 | |
973 | template <> |
974 | Expected<ASTNodeImporter::Designator> |
975 | ASTNodeImporter::import(const Designator &D) { |
976 | if (D.isFieldDesignator()) { |
977 | IdentifierInfo *ToFieldName = Importer.Import(FromId: D.getFieldName()); |
978 | |
979 | ExpectedSLoc ToDotLocOrErr = import(From: D.getDotLoc()); |
980 | if (!ToDotLocOrErr) |
981 | return ToDotLocOrErr.takeError(); |
982 | |
983 | ExpectedSLoc ToFieldLocOrErr = import(From: D.getFieldLoc()); |
984 | if (!ToFieldLocOrErr) |
985 | return ToFieldLocOrErr.takeError(); |
986 | |
987 | return DesignatedInitExpr::Designator::CreateFieldDesignator( |
988 | FieldName: ToFieldName, DotLoc: *ToDotLocOrErr, FieldLoc: *ToFieldLocOrErr); |
989 | } |
990 | |
991 | ExpectedSLoc ToLBracketLocOrErr = import(From: D.getLBracketLoc()); |
992 | if (!ToLBracketLocOrErr) |
993 | return ToLBracketLocOrErr.takeError(); |
994 | |
995 | ExpectedSLoc ToRBracketLocOrErr = import(From: D.getRBracketLoc()); |
996 | if (!ToRBracketLocOrErr) |
997 | return ToRBracketLocOrErr.takeError(); |
998 | |
999 | if (D.isArrayDesignator()) |
1000 | return Designator::CreateArrayDesignator(Index: D.getArrayIndex(), |
1001 | LBracketLoc: *ToLBracketLocOrErr, |
1002 | RBracketLoc: *ToRBracketLocOrErr); |
1003 | |
1004 | ExpectedSLoc ToEllipsisLocOrErr = import(From: D.getEllipsisLoc()); |
1005 | if (!ToEllipsisLocOrErr) |
1006 | return ToEllipsisLocOrErr.takeError(); |
1007 | |
1008 | assert(D.isArrayRangeDesignator()); |
1009 | return Designator::CreateArrayRangeDesignator( |
1010 | Index: D.getArrayIndex(), LBracketLoc: *ToLBracketLocOrErr, EllipsisLoc: *ToEllipsisLocOrErr, |
1011 | RBracketLoc: *ToRBracketLocOrErr); |
1012 | } |
1013 | |
1014 | template <> |
1015 | Expected<ConceptReference *> ASTNodeImporter::import(ConceptReference *From) { |
1016 | Error Err = Error::success(); |
1017 | auto ToNNS = importChecked(Err, From: From->getNestedNameSpecifierLoc()); |
1018 | auto ToTemplateKWLoc = importChecked(Err, From: From->getTemplateKWLoc()); |
1019 | auto ToConceptNameLoc = |
1020 | importChecked(Err, From: From->getConceptNameInfo().getLoc()); |
1021 | auto ToConceptName = importChecked(Err, From: From->getConceptNameInfo().getName()); |
1022 | auto ToFoundDecl = importChecked(Err, From: From->getFoundDecl()); |
1023 | auto ToNamedConcept = importChecked(Err, From: From->getNamedConcept()); |
1024 | if (Err) |
1025 | return std::move(Err); |
1026 | TemplateArgumentListInfo ToTAInfo; |
1027 | const auto *ASTTemplateArgs = From->getTemplateArgsAsWritten(); |
1028 | if (ASTTemplateArgs) |
1029 | if (Error Err = ImportTemplateArgumentListInfo(From: *ASTTemplateArgs, Result&: ToTAInfo)) |
1030 | return std::move(Err); |
1031 | auto *ConceptRef = ConceptReference::Create( |
1032 | C: Importer.getToContext(), NNS: ToNNS, TemplateKWLoc: ToTemplateKWLoc, |
1033 | ConceptNameInfo: DeclarationNameInfo(ToConceptName, ToConceptNameLoc), FoundDecl: ToFoundDecl, |
1034 | NamedConcept: ToNamedConcept, |
1035 | ArgsAsWritten: ASTTemplateArgs ? ASTTemplateArgumentListInfo::Create( |
1036 | C: Importer.getToContext(), List: ToTAInfo) |
1037 | : nullptr); |
1038 | return ConceptRef; |
1039 | } |
1040 | |
1041 | template <> |
1042 | Expected<LambdaCapture> ASTNodeImporter::import(const LambdaCapture &From) { |
1043 | ValueDecl *Var = nullptr; |
1044 | if (From.capturesVariable()) { |
1045 | if (auto VarOrErr = import(From: From.getCapturedVar())) |
1046 | Var = *VarOrErr; |
1047 | else |
1048 | return VarOrErr.takeError(); |
1049 | } |
1050 | |
1051 | auto LocationOrErr = import(From: From.getLocation()); |
1052 | if (!LocationOrErr) |
1053 | return LocationOrErr.takeError(); |
1054 | |
1055 | SourceLocation EllipsisLoc; |
1056 | if (From.isPackExpansion()) |
1057 | if (Error Err = importInto(To&: EllipsisLoc, From: From.getEllipsisLoc())) |
1058 | return std::move(Err); |
1059 | |
1060 | return LambdaCapture( |
1061 | *LocationOrErr, From.isImplicit(), From.getCaptureKind(), Var, |
1062 | EllipsisLoc); |
1063 | } |
1064 | |
1065 | template <typename T> |
1066 | bool ASTNodeImporter::hasSameVisibilityContextAndLinkage(T *Found, T *From) { |
1067 | if (Found->getLinkageInternal() != From->getLinkageInternal()) |
1068 | return false; |
1069 | |
1070 | if (From->hasExternalFormalLinkage()) |
1071 | return Found->hasExternalFormalLinkage(); |
1072 | if (Importer.GetFromTU(ToD: Found) != From->getTranslationUnitDecl()) |
1073 | return false; |
1074 | if (From->isInAnonymousNamespace()) |
1075 | return Found->isInAnonymousNamespace(); |
1076 | else |
1077 | return !Found->isInAnonymousNamespace() && |
1078 | !Found->hasExternalFormalLinkage(); |
1079 | } |
1080 | |
1081 | template <> |
1082 | bool ASTNodeImporter::hasSameVisibilityContextAndLinkage(TypedefNameDecl *Found, |
1083 | TypedefNameDecl *From) { |
1084 | if (Found->getLinkageInternal() != From->getLinkageInternal()) |
1085 | return false; |
1086 | |
1087 | if (From->isInAnonymousNamespace() && Found->isInAnonymousNamespace()) |
1088 | return Importer.GetFromTU(ToD: Found) == From->getTranslationUnitDecl(); |
1089 | return From->isInAnonymousNamespace() == Found->isInAnonymousNamespace(); |
1090 | } |
1091 | |
1092 | } // namespace clang |
1093 | |
1094 | //---------------------------------------------------------------------------- |
1095 | // Import Types |
1096 | //---------------------------------------------------------------------------- |
1097 | |
1098 | using namespace clang; |
1099 | |
1100 | ExpectedType ASTNodeImporter::VisitType(const Type *T) { |
1101 | Importer.FromDiag(Loc: SourceLocation(), DiagID: diag::err_unsupported_ast_node) |
1102 | << T->getTypeClassName(); |
1103 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
1104 | } |
1105 | |
1106 | ExpectedType ASTNodeImporter::VisitAtomicType(const AtomicType *T){ |
1107 | ExpectedType UnderlyingTypeOrErr = import(From: T->getValueType()); |
1108 | if (!UnderlyingTypeOrErr) |
1109 | return UnderlyingTypeOrErr.takeError(); |
1110 | |
1111 | return Importer.getToContext().getAtomicType(T: *UnderlyingTypeOrErr); |
1112 | } |
1113 | |
1114 | ExpectedType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) { |
1115 | switch (T->getKind()) { |
1116 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
1117 | case BuiltinType::Id: \ |
1118 | return Importer.getToContext().SingletonId; |
1119 | #include "clang/Basic/OpenCLImageTypes.def" |
1120 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
1121 | case BuiltinType::Id: \ |
1122 | return Importer.getToContext().Id##Ty; |
1123 | #include "clang/Basic/OpenCLExtensionTypes.def" |
1124 | #define SVE_TYPE(Name, Id, SingletonId) \ |
1125 | case BuiltinType::Id: \ |
1126 | return Importer.getToContext().SingletonId; |
1127 | #include "clang/Basic/AArch64ACLETypes.def" |
1128 | #define PPC_VECTOR_TYPE(Name, Id, Size) \ |
1129 | case BuiltinType::Id: \ |
1130 | return Importer.getToContext().Id##Ty; |
1131 | #include "clang/Basic/PPCTypes.def" |
1132 | #define RVV_TYPE(Name, Id, SingletonId) \ |
1133 | case BuiltinType::Id: \ |
1134 | return Importer.getToContext().SingletonId; |
1135 | #include "clang/Basic/RISCVVTypes.def" |
1136 | #define WASM_TYPE(Name, Id, SingletonId) \ |
1137 | case BuiltinType::Id: \ |
1138 | return Importer.getToContext().SingletonId; |
1139 | #include "clang/Basic/WebAssemblyReferenceTypes.def" |
1140 | #define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) \ |
1141 | case BuiltinType::Id: \ |
1142 | return Importer.getToContext().SingletonId; |
1143 | #include "clang/Basic/AMDGPUTypes.def" |
1144 | #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \ |
1145 | case BuiltinType::Id: \ |
1146 | return Importer.getToContext().SingletonId; |
1147 | #include "clang/Basic/HLSLIntangibleTypes.def" |
1148 | #define SHARED_SINGLETON_TYPE(Expansion) |
1149 | #define BUILTIN_TYPE(Id, SingletonId) \ |
1150 | case BuiltinType::Id: return Importer.getToContext().SingletonId; |
1151 | #include "clang/AST/BuiltinTypes.def" |
1152 | |
1153 | // FIXME: for Char16, Char32, and NullPtr, make sure that the "to" |
1154 | // context supports C++. |
1155 | |
1156 | // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to" |
1157 | // context supports ObjC. |
1158 | |
1159 | case BuiltinType::Char_U: |
1160 | // The context we're importing from has an unsigned 'char'. If we're |
1161 | // importing into a context with a signed 'char', translate to |
1162 | // 'unsigned char' instead. |
1163 | if (Importer.getToContext().getLangOpts().CharIsSigned) |
1164 | return Importer.getToContext().UnsignedCharTy; |
1165 | |
1166 | return Importer.getToContext().CharTy; |
1167 | |
1168 | case BuiltinType::Char_S: |
1169 | // The context we're importing from has an unsigned 'char'. If we're |
1170 | // importing into a context with a signed 'char', translate to |
1171 | // 'unsigned char' instead. |
1172 | if (!Importer.getToContext().getLangOpts().CharIsSigned) |
1173 | return Importer.getToContext().SignedCharTy; |
1174 | |
1175 | return Importer.getToContext().CharTy; |
1176 | |
1177 | case BuiltinType::WChar_S: |
1178 | case BuiltinType::WChar_U: |
1179 | // FIXME: If not in C++, shall we translate to the C equivalent of |
1180 | // wchar_t? |
1181 | return Importer.getToContext().WCharTy; |
1182 | } |
1183 | |
1184 | llvm_unreachable("Invalid BuiltinType Kind!" ); |
1185 | } |
1186 | |
1187 | ExpectedType ASTNodeImporter::VisitDecayedType(const DecayedType *T) { |
1188 | ExpectedType ToOriginalTypeOrErr = import(From: T->getOriginalType()); |
1189 | if (!ToOriginalTypeOrErr) |
1190 | return ToOriginalTypeOrErr.takeError(); |
1191 | |
1192 | return Importer.getToContext().getDecayedType(T: *ToOriginalTypeOrErr); |
1193 | } |
1194 | |
1195 | ExpectedType ASTNodeImporter::VisitComplexType(const ComplexType *T) { |
1196 | ExpectedType ToElementTypeOrErr = import(From: T->getElementType()); |
1197 | if (!ToElementTypeOrErr) |
1198 | return ToElementTypeOrErr.takeError(); |
1199 | |
1200 | return Importer.getToContext().getComplexType(T: *ToElementTypeOrErr); |
1201 | } |
1202 | |
1203 | ExpectedType ASTNodeImporter::VisitPointerType(const PointerType *T) { |
1204 | ExpectedType ToPointeeTypeOrErr = import(From: T->getPointeeType()); |
1205 | if (!ToPointeeTypeOrErr) |
1206 | return ToPointeeTypeOrErr.takeError(); |
1207 | |
1208 | return Importer.getToContext().getPointerType(T: *ToPointeeTypeOrErr); |
1209 | } |
1210 | |
1211 | ExpectedType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) { |
1212 | // FIXME: Check for blocks support in "to" context. |
1213 | ExpectedType ToPointeeTypeOrErr = import(From: T->getPointeeType()); |
1214 | if (!ToPointeeTypeOrErr) |
1215 | return ToPointeeTypeOrErr.takeError(); |
1216 | |
1217 | return Importer.getToContext().getBlockPointerType(T: *ToPointeeTypeOrErr); |
1218 | } |
1219 | |
1220 | ExpectedType |
1221 | ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) { |
1222 | // FIXME: Check for C++ support in "to" context. |
1223 | ExpectedType ToPointeeTypeOrErr = import(From: T->getPointeeTypeAsWritten()); |
1224 | if (!ToPointeeTypeOrErr) |
1225 | return ToPointeeTypeOrErr.takeError(); |
1226 | |
1227 | return Importer.getToContext().getLValueReferenceType(T: *ToPointeeTypeOrErr); |
1228 | } |
1229 | |
1230 | ExpectedType |
1231 | ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) { |
1232 | // FIXME: Check for C++0x support in "to" context. |
1233 | ExpectedType ToPointeeTypeOrErr = import(From: T->getPointeeTypeAsWritten()); |
1234 | if (!ToPointeeTypeOrErr) |
1235 | return ToPointeeTypeOrErr.takeError(); |
1236 | |
1237 | return Importer.getToContext().getRValueReferenceType(T: *ToPointeeTypeOrErr); |
1238 | } |
1239 | |
1240 | ExpectedType |
1241 | ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) { |
1242 | // FIXME: Check for C++ support in "to" context. |
1243 | ExpectedType ToPointeeTypeOrErr = import(From: T->getPointeeType()); |
1244 | if (!ToPointeeTypeOrErr) |
1245 | return ToPointeeTypeOrErr.takeError(); |
1246 | |
1247 | auto QualifierOrErr = import(From: T->getQualifier()); |
1248 | if (!QualifierOrErr) |
1249 | return QualifierOrErr.takeError(); |
1250 | |
1251 | auto ClsOrErr = import(From: T->getMostRecentCXXRecordDecl()); |
1252 | if (!ClsOrErr) |
1253 | return ClsOrErr.takeError(); |
1254 | |
1255 | return Importer.getToContext().getMemberPointerType( |
1256 | T: *ToPointeeTypeOrErr, Qualifier: *QualifierOrErr, Cls: *ClsOrErr); |
1257 | } |
1258 | |
1259 | ExpectedType |
1260 | ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) { |
1261 | Error Err = Error::success(); |
1262 | auto ToElementType = importChecked(Err, From: T->getElementType()); |
1263 | auto ToSizeExpr = importChecked(Err, From: T->getSizeExpr()); |
1264 | if (Err) |
1265 | return std::move(Err); |
1266 | |
1267 | return Importer.getToContext().getConstantArrayType( |
1268 | EltTy: ToElementType, ArySize: T->getSize(), SizeExpr: ToSizeExpr, ASM: T->getSizeModifier(), |
1269 | IndexTypeQuals: T->getIndexTypeCVRQualifiers()); |
1270 | } |
1271 | |
1272 | ExpectedType |
1273 | ASTNodeImporter::VisitArrayParameterType(const ArrayParameterType *T) { |
1274 | ExpectedType ToArrayTypeOrErr = VisitConstantArrayType(T); |
1275 | if (!ToArrayTypeOrErr) |
1276 | return ToArrayTypeOrErr.takeError(); |
1277 | |
1278 | return Importer.getToContext().getArrayParameterType(Ty: *ToArrayTypeOrErr); |
1279 | } |
1280 | |
1281 | ExpectedType |
1282 | ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) { |
1283 | ExpectedType ToElementTypeOrErr = import(From: T->getElementType()); |
1284 | if (!ToElementTypeOrErr) |
1285 | return ToElementTypeOrErr.takeError(); |
1286 | |
1287 | return Importer.getToContext().getIncompleteArrayType(EltTy: *ToElementTypeOrErr, |
1288 | ASM: T->getSizeModifier(), |
1289 | IndexTypeQuals: T->getIndexTypeCVRQualifiers()); |
1290 | } |
1291 | |
1292 | ExpectedType |
1293 | ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) { |
1294 | Error Err = Error::success(); |
1295 | QualType ToElementType = importChecked(Err, From: T->getElementType()); |
1296 | Expr *ToSizeExpr = importChecked(Err, From: T->getSizeExpr()); |
1297 | if (Err) |
1298 | return std::move(Err); |
1299 | return Importer.getToContext().getVariableArrayType( |
1300 | EltTy: ToElementType, NumElts: ToSizeExpr, ASM: T->getSizeModifier(), |
1301 | IndexTypeQuals: T->getIndexTypeCVRQualifiers()); |
1302 | } |
1303 | |
1304 | ExpectedType ASTNodeImporter::VisitDependentSizedArrayType( |
1305 | const DependentSizedArrayType *T) { |
1306 | Error Err = Error::success(); |
1307 | QualType ToElementType = importChecked(Err, From: T->getElementType()); |
1308 | Expr *ToSizeExpr = importChecked(Err, From: T->getSizeExpr()); |
1309 | if (Err) |
1310 | return std::move(Err); |
1311 | // SizeExpr may be null if size is not specified directly. |
1312 | // For example, 'int a[]'. |
1313 | |
1314 | return Importer.getToContext().getDependentSizedArrayType( |
1315 | EltTy: ToElementType, NumElts: ToSizeExpr, ASM: T->getSizeModifier(), |
1316 | IndexTypeQuals: T->getIndexTypeCVRQualifiers()); |
1317 | } |
1318 | |
1319 | ExpectedType ASTNodeImporter::VisitDependentSizedExtVectorType( |
1320 | const DependentSizedExtVectorType *T) { |
1321 | Error Err = Error::success(); |
1322 | QualType ToElementType = importChecked(Err, From: T->getElementType()); |
1323 | Expr *ToSizeExpr = importChecked(Err, From: T->getSizeExpr()); |
1324 | SourceLocation ToAttrLoc = importChecked(Err, From: T->getAttributeLoc()); |
1325 | if (Err) |
1326 | return std::move(Err); |
1327 | return Importer.getToContext().getDependentSizedExtVectorType( |
1328 | VectorType: ToElementType, SizeExpr: ToSizeExpr, AttrLoc: ToAttrLoc); |
1329 | } |
1330 | |
1331 | ExpectedType ASTNodeImporter::VisitVectorType(const VectorType *T) { |
1332 | ExpectedType ToElementTypeOrErr = import(From: T->getElementType()); |
1333 | if (!ToElementTypeOrErr) |
1334 | return ToElementTypeOrErr.takeError(); |
1335 | |
1336 | return Importer.getToContext().getVectorType(VectorType: *ToElementTypeOrErr, |
1337 | NumElts: T->getNumElements(), |
1338 | VecKind: T->getVectorKind()); |
1339 | } |
1340 | |
1341 | ExpectedType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) { |
1342 | ExpectedType ToElementTypeOrErr = import(From: T->getElementType()); |
1343 | if (!ToElementTypeOrErr) |
1344 | return ToElementTypeOrErr.takeError(); |
1345 | |
1346 | return Importer.getToContext().getExtVectorType(VectorType: *ToElementTypeOrErr, |
1347 | NumElts: T->getNumElements()); |
1348 | } |
1349 | |
1350 | ExpectedType |
1351 | ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { |
1352 | // FIXME: What happens if we're importing a function without a prototype |
1353 | // into C++? Should we make it variadic? |
1354 | ExpectedType ToReturnTypeOrErr = import(From: T->getReturnType()); |
1355 | if (!ToReturnTypeOrErr) |
1356 | return ToReturnTypeOrErr.takeError(); |
1357 | |
1358 | return Importer.getToContext().getFunctionNoProtoType(ResultTy: *ToReturnTypeOrErr, |
1359 | Info: T->getExtInfo()); |
1360 | } |
1361 | |
1362 | ExpectedType |
1363 | ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) { |
1364 | ExpectedType ToReturnTypeOrErr = import(From: T->getReturnType()); |
1365 | if (!ToReturnTypeOrErr) |
1366 | return ToReturnTypeOrErr.takeError(); |
1367 | |
1368 | // Import argument types |
1369 | SmallVector<QualType, 4> ArgTypes; |
1370 | for (const auto &A : T->param_types()) { |
1371 | ExpectedType TyOrErr = import(From: A); |
1372 | if (!TyOrErr) |
1373 | return TyOrErr.takeError(); |
1374 | ArgTypes.push_back(Elt: *TyOrErr); |
1375 | } |
1376 | |
1377 | // Import exception types |
1378 | SmallVector<QualType, 4> ExceptionTypes; |
1379 | for (const auto &E : T->exceptions()) { |
1380 | ExpectedType TyOrErr = import(From: E); |
1381 | if (!TyOrErr) |
1382 | return TyOrErr.takeError(); |
1383 | ExceptionTypes.push_back(Elt: *TyOrErr); |
1384 | } |
1385 | |
1386 | FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo(); |
1387 | Error Err = Error::success(); |
1388 | FunctionProtoType::ExtProtoInfo ToEPI; |
1389 | ToEPI.ExtInfo = FromEPI.ExtInfo; |
1390 | ToEPI.Variadic = FromEPI.Variadic; |
1391 | ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn; |
1392 | ToEPI.TypeQuals = FromEPI.TypeQuals; |
1393 | ToEPI.RefQualifier = FromEPI.RefQualifier; |
1394 | ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type; |
1395 | ToEPI.ExceptionSpec.NoexceptExpr = |
1396 | importChecked(Err, From: FromEPI.ExceptionSpec.NoexceptExpr); |
1397 | ToEPI.ExceptionSpec.SourceDecl = |
1398 | importChecked(Err, From: FromEPI.ExceptionSpec.SourceDecl); |
1399 | ToEPI.ExceptionSpec.SourceTemplate = |
1400 | importChecked(Err, From: FromEPI.ExceptionSpec.SourceTemplate); |
1401 | ToEPI.ExceptionSpec.Exceptions = ExceptionTypes; |
1402 | |
1403 | if (Err) |
1404 | return std::move(Err); |
1405 | |
1406 | return Importer.getToContext().getFunctionType( |
1407 | ResultTy: *ToReturnTypeOrErr, Args: ArgTypes, EPI: ToEPI); |
1408 | } |
1409 | |
1410 | ExpectedType ASTNodeImporter::VisitUnresolvedUsingType( |
1411 | const UnresolvedUsingType *T) { |
1412 | Error Err = Error::success(); |
1413 | auto ToD = importChecked(Err, From: T->getDecl()); |
1414 | auto ToPrevD = importChecked(Err, From: T->getDecl()->getPreviousDecl()); |
1415 | if (Err) |
1416 | return std::move(Err); |
1417 | |
1418 | return Importer.getToContext().getTypeDeclType( |
1419 | Decl: ToD, PrevDecl: cast_or_null<TypeDecl>(Val: ToPrevD)); |
1420 | } |
1421 | |
1422 | ExpectedType ASTNodeImporter::VisitParenType(const ParenType *T) { |
1423 | ExpectedType ToInnerTypeOrErr = import(From: T->getInnerType()); |
1424 | if (!ToInnerTypeOrErr) |
1425 | return ToInnerTypeOrErr.takeError(); |
1426 | |
1427 | return Importer.getToContext().getParenType(NamedType: *ToInnerTypeOrErr); |
1428 | } |
1429 | |
1430 | ExpectedType |
1431 | ASTNodeImporter::VisitPackIndexingType(clang::PackIndexingType const *T) { |
1432 | |
1433 | ExpectedType Pattern = import(From: T->getPattern()); |
1434 | if (!Pattern) |
1435 | return Pattern.takeError(); |
1436 | ExpectedExpr Index = import(From: T->getIndexExpr()); |
1437 | if (!Index) |
1438 | return Index.takeError(); |
1439 | return Importer.getToContext().getPackIndexingType(Pattern: *Pattern, IndexExpr: *Index); |
1440 | } |
1441 | |
1442 | ExpectedType ASTNodeImporter::VisitTypedefType(const TypedefType *T) { |
1443 | Expected<TypedefNameDecl *> ToDeclOrErr = import(From: T->getDecl()); |
1444 | if (!ToDeclOrErr) |
1445 | return ToDeclOrErr.takeError(); |
1446 | |
1447 | TypedefNameDecl *ToDecl = *ToDeclOrErr; |
1448 | if (ToDecl->getTypeForDecl()) |
1449 | return QualType(ToDecl->getTypeForDecl(), 0); |
1450 | |
1451 | ExpectedType ToUnderlyingTypeOrErr = import(From: T->desugar()); |
1452 | if (!ToUnderlyingTypeOrErr) |
1453 | return ToUnderlyingTypeOrErr.takeError(); |
1454 | |
1455 | return Importer.getToContext().getTypedefType(Decl: ToDecl, Underlying: *ToUnderlyingTypeOrErr); |
1456 | } |
1457 | |
1458 | ExpectedType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) { |
1459 | ExpectedExpr ToExprOrErr = import(From: T->getUnderlyingExpr()); |
1460 | if (!ToExprOrErr) |
1461 | return ToExprOrErr.takeError(); |
1462 | return Importer.getToContext().getTypeOfExprType(E: *ToExprOrErr, Kind: T->getKind()); |
1463 | } |
1464 | |
1465 | ExpectedType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) { |
1466 | ExpectedType ToUnderlyingTypeOrErr = import(From: T->getUnmodifiedType()); |
1467 | if (!ToUnderlyingTypeOrErr) |
1468 | return ToUnderlyingTypeOrErr.takeError(); |
1469 | return Importer.getToContext().getTypeOfType(QT: *ToUnderlyingTypeOrErr, |
1470 | Kind: T->getKind()); |
1471 | } |
1472 | |
1473 | ExpectedType ASTNodeImporter::VisitUsingType(const UsingType *T) { |
1474 | Expected<UsingShadowDecl *> FoundOrErr = import(From: T->getFoundDecl()); |
1475 | if (!FoundOrErr) |
1476 | return FoundOrErr.takeError(); |
1477 | Expected<QualType> UnderlyingOrErr = import(From: T->getUnderlyingType()); |
1478 | if (!UnderlyingOrErr) |
1479 | return UnderlyingOrErr.takeError(); |
1480 | |
1481 | return Importer.getToContext().getUsingType(Found: *FoundOrErr, Underlying: *UnderlyingOrErr); |
1482 | } |
1483 | |
1484 | ExpectedType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) { |
1485 | // FIXME: Make sure that the "to" context supports C++0x! |
1486 | ExpectedExpr ToExprOrErr = import(From: T->getUnderlyingExpr()); |
1487 | if (!ToExprOrErr) |
1488 | return ToExprOrErr.takeError(); |
1489 | |
1490 | ExpectedType ToUnderlyingTypeOrErr = import(From: T->getUnderlyingType()); |
1491 | if (!ToUnderlyingTypeOrErr) |
1492 | return ToUnderlyingTypeOrErr.takeError(); |
1493 | |
1494 | return Importer.getToContext().getDecltypeType( |
1495 | e: *ToExprOrErr, UnderlyingType: *ToUnderlyingTypeOrErr); |
1496 | } |
1497 | |
1498 | ExpectedType |
1499 | ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) { |
1500 | ExpectedType ToBaseTypeOrErr = import(From: T->getBaseType()); |
1501 | if (!ToBaseTypeOrErr) |
1502 | return ToBaseTypeOrErr.takeError(); |
1503 | |
1504 | ExpectedType ToUnderlyingTypeOrErr = import(From: T->getUnderlyingType()); |
1505 | if (!ToUnderlyingTypeOrErr) |
1506 | return ToUnderlyingTypeOrErr.takeError(); |
1507 | |
1508 | return Importer.getToContext().getUnaryTransformType( |
1509 | BaseType: *ToBaseTypeOrErr, UnderlyingType: *ToUnderlyingTypeOrErr, UKind: T->getUTTKind()); |
1510 | } |
1511 | |
1512 | ExpectedType ASTNodeImporter::VisitAutoType(const AutoType *T) { |
1513 | // FIXME: Make sure that the "to" context supports C++11! |
1514 | ExpectedType ToDeducedTypeOrErr = import(From: T->getDeducedType()); |
1515 | if (!ToDeducedTypeOrErr) |
1516 | return ToDeducedTypeOrErr.takeError(); |
1517 | |
1518 | ExpectedDecl ToTypeConstraintConcept = import(From: T->getTypeConstraintConcept()); |
1519 | if (!ToTypeConstraintConcept) |
1520 | return ToTypeConstraintConcept.takeError(); |
1521 | |
1522 | SmallVector<TemplateArgument, 2> ToTemplateArgs; |
1523 | if (Error Err = ImportTemplateArguments(FromArgs: T->getTypeConstraintArguments(), |
1524 | ToArgs&: ToTemplateArgs)) |
1525 | return std::move(Err); |
1526 | |
1527 | return Importer.getToContext().getAutoType( |
1528 | DeducedType: *ToDeducedTypeOrErr, Keyword: T->getKeyword(), /*IsDependent*/false, |
1529 | /*IsPack=*/false, TypeConstraintConcept: cast_or_null<ConceptDecl>(Val: *ToTypeConstraintConcept), |
1530 | TypeConstraintArgs: ToTemplateArgs); |
1531 | } |
1532 | |
1533 | ExpectedType ASTNodeImporter::VisitDeducedTemplateSpecializationType( |
1534 | const DeducedTemplateSpecializationType *T) { |
1535 | // FIXME: Make sure that the "to" context supports C++17! |
1536 | Expected<TemplateName> ToTemplateNameOrErr = import(From: T->getTemplateName()); |
1537 | if (!ToTemplateNameOrErr) |
1538 | return ToTemplateNameOrErr.takeError(); |
1539 | ExpectedType ToDeducedTypeOrErr = import(From: T->getDeducedType()); |
1540 | if (!ToDeducedTypeOrErr) |
1541 | return ToDeducedTypeOrErr.takeError(); |
1542 | |
1543 | return Importer.getToContext().getDeducedTemplateSpecializationType( |
1544 | Template: *ToTemplateNameOrErr, DeducedType: *ToDeducedTypeOrErr, IsDependent: T->isDependentType()); |
1545 | } |
1546 | |
1547 | ExpectedType ASTNodeImporter::VisitInjectedClassNameType( |
1548 | const InjectedClassNameType *T) { |
1549 | Expected<CXXRecordDecl *> ToDeclOrErr = import(From: T->getDecl()); |
1550 | if (!ToDeclOrErr) |
1551 | return ToDeclOrErr.takeError(); |
1552 | |
1553 | // The InjectedClassNameType is created in VisitRecordDecl when the |
1554 | // T->getDecl() is imported. Here we can return the existing type. |
1555 | const Type *Ty = (*ToDeclOrErr)->getTypeForDecl(); |
1556 | assert(isa_and_nonnull<InjectedClassNameType>(Ty)); |
1557 | return QualType(Ty, 0); |
1558 | } |
1559 | |
1560 | ExpectedType ASTNodeImporter::VisitRecordType(const RecordType *T) { |
1561 | Expected<RecordDecl *> ToDeclOrErr = import(From: T->getDecl()); |
1562 | if (!ToDeclOrErr) |
1563 | return ToDeclOrErr.takeError(); |
1564 | |
1565 | return Importer.getToContext().getTagDeclType(Decl: *ToDeclOrErr); |
1566 | } |
1567 | |
1568 | ExpectedType ASTNodeImporter::VisitEnumType(const EnumType *T) { |
1569 | Expected<EnumDecl *> ToDeclOrErr = import(From: T->getDecl()); |
1570 | if (!ToDeclOrErr) |
1571 | return ToDeclOrErr.takeError(); |
1572 | |
1573 | return Importer.getToContext().getTagDeclType(Decl: *ToDeclOrErr); |
1574 | } |
1575 | |
1576 | ExpectedType ASTNodeImporter::VisitAttributedType(const AttributedType *T) { |
1577 | ExpectedType ToModifiedTypeOrErr = import(From: T->getModifiedType()); |
1578 | if (!ToModifiedTypeOrErr) |
1579 | return ToModifiedTypeOrErr.takeError(); |
1580 | ExpectedType ToEquivalentTypeOrErr = import(From: T->getEquivalentType()); |
1581 | if (!ToEquivalentTypeOrErr) |
1582 | return ToEquivalentTypeOrErr.takeError(); |
1583 | |
1584 | return Importer.getToContext().getAttributedType( |
1585 | attrKind: T->getAttrKind(), modifiedType: *ToModifiedTypeOrErr, equivalentType: *ToEquivalentTypeOrErr, |
1586 | attr: T->getAttr()); |
1587 | } |
1588 | |
1589 | ExpectedType |
1590 | ASTNodeImporter::VisitCountAttributedType(const CountAttributedType *T) { |
1591 | ExpectedType ToWrappedTypeOrErr = import(From: T->desugar()); |
1592 | if (!ToWrappedTypeOrErr) |
1593 | return ToWrappedTypeOrErr.takeError(); |
1594 | |
1595 | Error Err = Error::success(); |
1596 | Expr *CountExpr = importChecked(Err, From: T->getCountExpr()); |
1597 | |
1598 | SmallVector<TypeCoupledDeclRefInfo, 1> CoupledDecls; |
1599 | for (const TypeCoupledDeclRefInfo &TI : T->dependent_decls()) { |
1600 | Expected<ValueDecl *> ToDeclOrErr = import(From: TI.getDecl()); |
1601 | if (!ToDeclOrErr) |
1602 | return ToDeclOrErr.takeError(); |
1603 | CoupledDecls.emplace_back(Args&: *ToDeclOrErr, Args: TI.isDeref()); |
1604 | } |
1605 | |
1606 | return Importer.getToContext().getCountAttributedType( |
1607 | T: *ToWrappedTypeOrErr, CountExpr, CountInBytes: T->isCountInBytes(), OrNull: T->isOrNull(), |
1608 | DependentDecls: ArrayRef(CoupledDecls)); |
1609 | } |
1610 | |
1611 | ExpectedType ASTNodeImporter::VisitTemplateTypeParmType( |
1612 | const TemplateTypeParmType *T) { |
1613 | Expected<TemplateTypeParmDecl *> ToDeclOrErr = import(From: T->getDecl()); |
1614 | if (!ToDeclOrErr) |
1615 | return ToDeclOrErr.takeError(); |
1616 | |
1617 | return Importer.getToContext().getTemplateTypeParmType( |
1618 | Depth: T->getDepth(), Index: T->getIndex(), ParameterPack: T->isParameterPack(), ParmDecl: *ToDeclOrErr); |
1619 | } |
1620 | |
1621 | ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmType( |
1622 | const SubstTemplateTypeParmType *T) { |
1623 | Expected<Decl *> ReplacedOrErr = import(From: T->getAssociatedDecl()); |
1624 | if (!ReplacedOrErr) |
1625 | return ReplacedOrErr.takeError(); |
1626 | |
1627 | ExpectedType ToReplacementTypeOrErr = import(From: T->getReplacementType()); |
1628 | if (!ToReplacementTypeOrErr) |
1629 | return ToReplacementTypeOrErr.takeError(); |
1630 | |
1631 | return Importer.getToContext().getSubstTemplateTypeParmType( |
1632 | Replacement: *ToReplacementTypeOrErr, AssociatedDecl: *ReplacedOrErr, Index: T->getIndex(), PackIndex: T->getPackIndex(), |
1633 | Final: T->getFinal()); |
1634 | } |
1635 | |
1636 | ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmPackType( |
1637 | const SubstTemplateTypeParmPackType *T) { |
1638 | Expected<Decl *> ReplacedOrErr = import(From: T->getAssociatedDecl()); |
1639 | if (!ReplacedOrErr) |
1640 | return ReplacedOrErr.takeError(); |
1641 | |
1642 | Expected<TemplateArgument> ToArgumentPack = import(From: T->getArgumentPack()); |
1643 | if (!ToArgumentPack) |
1644 | return ToArgumentPack.takeError(); |
1645 | |
1646 | return Importer.getToContext().getSubstTemplateTypeParmPackType( |
1647 | AssociatedDecl: *ReplacedOrErr, Index: T->getIndex(), Final: T->getFinal(), ArgPack: *ToArgumentPack); |
1648 | } |
1649 | |
1650 | ExpectedType ASTNodeImporter::VisitTemplateSpecializationType( |
1651 | const TemplateSpecializationType *T) { |
1652 | auto ToTemplateOrErr = import(From: T->getTemplateName()); |
1653 | if (!ToTemplateOrErr) |
1654 | return ToTemplateOrErr.takeError(); |
1655 | |
1656 | SmallVector<TemplateArgument, 2> ToTemplateArgs; |
1657 | if (Error Err = |
1658 | ImportTemplateArguments(FromArgs: T->template_arguments(), ToArgs&: ToTemplateArgs)) |
1659 | return std::move(Err); |
1660 | |
1661 | ExpectedType ToUnderlyingOrErr = |
1662 | T->isCanonicalUnqualified() ? QualType() : import(From: T->desugar()); |
1663 | if (!ToUnderlyingOrErr) |
1664 | return ToUnderlyingOrErr.takeError(); |
1665 | return Importer.getToContext().getTemplateSpecializationType( |
1666 | T: *ToTemplateOrErr, SpecifiedArgs: ToTemplateArgs, CanonicalArgs: {}, Underlying: *ToUnderlyingOrErr); |
1667 | } |
1668 | |
1669 | ExpectedType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) { |
1670 | // Note: the qualifier in an ElaboratedType is optional. |
1671 | auto ToQualifierOrErr = import(From: T->getQualifier()); |
1672 | if (!ToQualifierOrErr) |
1673 | return ToQualifierOrErr.takeError(); |
1674 | |
1675 | ExpectedType ToNamedTypeOrErr = import(From: T->getNamedType()); |
1676 | if (!ToNamedTypeOrErr) |
1677 | return ToNamedTypeOrErr.takeError(); |
1678 | |
1679 | Expected<TagDecl *> ToOwnedTagDeclOrErr = import(From: T->getOwnedTagDecl()); |
1680 | if (!ToOwnedTagDeclOrErr) |
1681 | return ToOwnedTagDeclOrErr.takeError(); |
1682 | |
1683 | return Importer.getToContext().getElaboratedType(Keyword: T->getKeyword(), |
1684 | NNS: *ToQualifierOrErr, |
1685 | NamedType: *ToNamedTypeOrErr, |
1686 | OwnedTagDecl: *ToOwnedTagDeclOrErr); |
1687 | } |
1688 | |
1689 | ExpectedType |
1690 | ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) { |
1691 | ExpectedType ToPatternOrErr = import(From: T->getPattern()); |
1692 | if (!ToPatternOrErr) |
1693 | return ToPatternOrErr.takeError(); |
1694 | |
1695 | return Importer.getToContext().getPackExpansionType(Pattern: *ToPatternOrErr, |
1696 | NumExpansions: T->getNumExpansions(), |
1697 | /*ExpactPack=*/ExpectPackInType: false); |
1698 | } |
1699 | |
1700 | ExpectedType ASTNodeImporter::VisitDependentTemplateSpecializationType( |
1701 | const DependentTemplateSpecializationType *T) { |
1702 | const DependentTemplateStorage &DTN = T->getDependentTemplateName(); |
1703 | auto QualifierOrErr = import(From: DTN.getQualifier()); |
1704 | if (!QualifierOrErr) |
1705 | return QualifierOrErr.takeError(); |
1706 | |
1707 | SmallVector<TemplateArgument, 2> ToPack; |
1708 | ToPack.reserve(N: T->template_arguments().size()); |
1709 | if (Error Err = ImportTemplateArguments(FromArgs: T->template_arguments(), ToArgs&: ToPack)) |
1710 | return std::move(Err); |
1711 | |
1712 | return Importer.getToContext().getDependentTemplateSpecializationType( |
1713 | Keyword: T->getKeyword(), |
1714 | Name: {*QualifierOrErr, Importer.Import(FromIO: DTN.getName()), |
1715 | DTN.hasTemplateKeyword()}, |
1716 | Args: ToPack); |
1717 | } |
1718 | |
1719 | ExpectedType |
1720 | ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) { |
1721 | auto ToQualifierOrErr = import(From: T->getQualifier()); |
1722 | if (!ToQualifierOrErr) |
1723 | return ToQualifierOrErr.takeError(); |
1724 | |
1725 | IdentifierInfo *Name = Importer.Import(FromId: T->getIdentifier()); |
1726 | return Importer.getToContext().getDependentNameType(Keyword: T->getKeyword(), |
1727 | NNS: *ToQualifierOrErr, Name); |
1728 | } |
1729 | |
1730 | ExpectedType |
1731 | ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { |
1732 | Expected<ObjCInterfaceDecl *> ToDeclOrErr = import(From: T->getDecl()); |
1733 | if (!ToDeclOrErr) |
1734 | return ToDeclOrErr.takeError(); |
1735 | |
1736 | return Importer.getToContext().getObjCInterfaceType(Decl: *ToDeclOrErr); |
1737 | } |
1738 | |
1739 | ExpectedType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) { |
1740 | ExpectedType ToBaseTypeOrErr = import(From: T->getBaseType()); |
1741 | if (!ToBaseTypeOrErr) |
1742 | return ToBaseTypeOrErr.takeError(); |
1743 | |
1744 | SmallVector<QualType, 4> TypeArgs; |
1745 | for (auto TypeArg : T->getTypeArgsAsWritten()) { |
1746 | if (ExpectedType TyOrErr = import(From: TypeArg)) |
1747 | TypeArgs.push_back(Elt: *TyOrErr); |
1748 | else |
1749 | return TyOrErr.takeError(); |
1750 | } |
1751 | |
1752 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
1753 | for (auto *P : T->quals()) { |
1754 | if (Expected<ObjCProtocolDecl *> ProtocolOrErr = import(From: P)) |
1755 | Protocols.push_back(Elt: *ProtocolOrErr); |
1756 | else |
1757 | return ProtocolOrErr.takeError(); |
1758 | |
1759 | } |
1760 | |
1761 | return Importer.getToContext().getObjCObjectType(Base: *ToBaseTypeOrErr, typeArgs: TypeArgs, |
1762 | protocols: Protocols, |
1763 | isKindOf: T->isKindOfTypeAsWritten()); |
1764 | } |
1765 | |
1766 | ExpectedType |
1767 | ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { |
1768 | ExpectedType ToPointeeTypeOrErr = import(From: T->getPointeeType()); |
1769 | if (!ToPointeeTypeOrErr) |
1770 | return ToPointeeTypeOrErr.takeError(); |
1771 | |
1772 | return Importer.getToContext().getObjCObjectPointerType(OIT: *ToPointeeTypeOrErr); |
1773 | } |
1774 | |
1775 | ExpectedType |
1776 | ASTNodeImporter::VisitMacroQualifiedType(const MacroQualifiedType *T) { |
1777 | ExpectedType ToUnderlyingTypeOrErr = import(From: T->getUnderlyingType()); |
1778 | if (!ToUnderlyingTypeOrErr) |
1779 | return ToUnderlyingTypeOrErr.takeError(); |
1780 | |
1781 | IdentifierInfo *ToIdentifier = Importer.Import(FromId: T->getMacroIdentifier()); |
1782 | return Importer.getToContext().getMacroQualifiedType(UnderlyingTy: *ToUnderlyingTypeOrErr, |
1783 | MacroII: ToIdentifier); |
1784 | } |
1785 | |
1786 | ExpectedType clang::ASTNodeImporter::VisitAdjustedType(const AdjustedType *T) { |
1787 | Error Err = Error::success(); |
1788 | QualType ToOriginalType = importChecked(Err, From: T->getOriginalType()); |
1789 | QualType ToAdjustedType = importChecked(Err, From: T->getAdjustedType()); |
1790 | if (Err) |
1791 | return std::move(Err); |
1792 | |
1793 | return Importer.getToContext().getAdjustedType(Orig: ToOriginalType, |
1794 | New: ToAdjustedType); |
1795 | } |
1796 | |
1797 | ExpectedType clang::ASTNodeImporter::VisitBitIntType(const BitIntType *T) { |
1798 | return Importer.getToContext().getBitIntType(Unsigned: T->isUnsigned(), |
1799 | NumBits: T->getNumBits()); |
1800 | } |
1801 | |
1802 | ExpectedType clang::ASTNodeImporter::VisitBTFTagAttributedType( |
1803 | const clang::BTFTagAttributedType *T) { |
1804 | Error Err = Error::success(); |
1805 | const BTFTypeTagAttr *ToBTFAttr = importChecked(Err, From: T->getAttr()); |
1806 | QualType ToWrappedType = importChecked(Err, From: T->getWrappedType()); |
1807 | if (Err) |
1808 | return std::move(Err); |
1809 | |
1810 | return Importer.getToContext().getBTFTagAttributedType(BTFAttr: ToBTFAttr, |
1811 | Wrapped: ToWrappedType); |
1812 | } |
1813 | |
1814 | ExpectedType clang::ASTNodeImporter::VisitHLSLAttributedResourceType( |
1815 | const clang::HLSLAttributedResourceType *T) { |
1816 | Error Err = Error::success(); |
1817 | const HLSLAttributedResourceType::Attributes &ToAttrs = T->getAttrs(); |
1818 | QualType ToWrappedType = importChecked(Err, From: T->getWrappedType()); |
1819 | QualType ToContainedType = importChecked(Err, From: T->getContainedType()); |
1820 | if (Err) |
1821 | return std::move(Err); |
1822 | |
1823 | return Importer.getToContext().getHLSLAttributedResourceType( |
1824 | Wrapped: ToWrappedType, Contained: ToContainedType, Attrs: ToAttrs); |
1825 | } |
1826 | |
1827 | ExpectedType clang::ASTNodeImporter::VisitHLSLInlineSpirvType( |
1828 | const clang::HLSLInlineSpirvType *T) { |
1829 | Error Err = Error::success(); |
1830 | |
1831 | uint32_t ToOpcode = T->getOpcode(); |
1832 | uint32_t ToSize = T->getSize(); |
1833 | uint32_t ToAlignment = T->getAlignment(); |
1834 | |
1835 | llvm::SmallVector<SpirvOperand> ToOperands; |
1836 | |
1837 | for (auto &Operand : T->getOperands()) { |
1838 | using SpirvOperandKind = SpirvOperand::SpirvOperandKind; |
1839 | |
1840 | switch (Operand.getKind()) { |
1841 | case SpirvOperandKind::ConstantId: |
1842 | ToOperands.push_back(Elt: SpirvOperand::createConstant( |
1843 | ResultType: importChecked(Err, From: Operand.getResultType()), Val: Operand.getValue())); |
1844 | break; |
1845 | case SpirvOperandKind::Literal: |
1846 | ToOperands.push_back(Elt: SpirvOperand::createLiteral(Val: Operand.getValue())); |
1847 | break; |
1848 | case SpirvOperandKind::TypeId: |
1849 | ToOperands.push_back(Elt: SpirvOperand::createType( |
1850 | T: importChecked(Err, From: Operand.getResultType()))); |
1851 | break; |
1852 | default: |
1853 | llvm_unreachable("Invalid SpirvOperand kind" ); |
1854 | } |
1855 | |
1856 | if (Err) |
1857 | return std::move(Err); |
1858 | } |
1859 | |
1860 | return Importer.getToContext().getHLSLInlineSpirvType( |
1861 | Opcode: ToOpcode, Size: ToSize, Alignment: ToAlignment, Operands: ToOperands); |
1862 | } |
1863 | |
1864 | ExpectedType clang::ASTNodeImporter::VisitConstantMatrixType( |
1865 | const clang::ConstantMatrixType *T) { |
1866 | ExpectedType ToElementTypeOrErr = import(From: T->getElementType()); |
1867 | if (!ToElementTypeOrErr) |
1868 | return ToElementTypeOrErr.takeError(); |
1869 | |
1870 | return Importer.getToContext().getConstantMatrixType( |
1871 | ElementType: *ToElementTypeOrErr, NumRows: T->getNumRows(), NumColumns: T->getNumColumns()); |
1872 | } |
1873 | |
1874 | ExpectedType clang::ASTNodeImporter::VisitDependentAddressSpaceType( |
1875 | const clang::DependentAddressSpaceType *T) { |
1876 | Error Err = Error::success(); |
1877 | QualType ToPointeeType = importChecked(Err, From: T->getPointeeType()); |
1878 | Expr *ToAddrSpaceExpr = importChecked(Err, From: T->getAddrSpaceExpr()); |
1879 | SourceLocation ToAttrLoc = importChecked(Err, From: T->getAttributeLoc()); |
1880 | if (Err) |
1881 | return std::move(Err); |
1882 | |
1883 | return Importer.getToContext().getDependentAddressSpaceType( |
1884 | PointeeType: ToPointeeType, AddrSpaceExpr: ToAddrSpaceExpr, AttrLoc: ToAttrLoc); |
1885 | } |
1886 | |
1887 | ExpectedType clang::ASTNodeImporter::VisitDependentBitIntType( |
1888 | const clang::DependentBitIntType *T) { |
1889 | ExpectedExpr ToNumBitsExprOrErr = import(From: T->getNumBitsExpr()); |
1890 | if (!ToNumBitsExprOrErr) |
1891 | return ToNumBitsExprOrErr.takeError(); |
1892 | return Importer.getToContext().getDependentBitIntType(Unsigned: T->isUnsigned(), |
1893 | BitsExpr: *ToNumBitsExprOrErr); |
1894 | } |
1895 | |
1896 | ExpectedType clang::ASTNodeImporter::VisitDependentSizedMatrixType( |
1897 | const clang::DependentSizedMatrixType *T) { |
1898 | Error Err = Error::success(); |
1899 | QualType ToElementType = importChecked(Err, From: T->getElementType()); |
1900 | Expr *ToRowExpr = importChecked(Err, From: T->getRowExpr()); |
1901 | Expr *ToColumnExpr = importChecked(Err, From: T->getColumnExpr()); |
1902 | SourceLocation ToAttrLoc = importChecked(Err, From: T->getAttributeLoc()); |
1903 | if (Err) |
1904 | return std::move(Err); |
1905 | |
1906 | return Importer.getToContext().getDependentSizedMatrixType( |
1907 | ElementType: ToElementType, RowExpr: ToRowExpr, ColumnExpr: ToColumnExpr, AttrLoc: ToAttrLoc); |
1908 | } |
1909 | |
1910 | ExpectedType clang::ASTNodeImporter::VisitDependentVectorType( |
1911 | const clang::DependentVectorType *T) { |
1912 | Error Err = Error::success(); |
1913 | QualType ToElementType = importChecked(Err, From: T->getElementType()); |
1914 | Expr *ToSizeExpr = importChecked(Err, From: T->getSizeExpr()); |
1915 | SourceLocation ToAttrLoc = importChecked(Err, From: T->getAttributeLoc()); |
1916 | if (Err) |
1917 | return std::move(Err); |
1918 | |
1919 | return Importer.getToContext().getDependentVectorType( |
1920 | VectorType: ToElementType, SizeExpr: ToSizeExpr, AttrLoc: ToAttrLoc, VecKind: T->getVectorKind()); |
1921 | } |
1922 | |
1923 | ExpectedType clang::ASTNodeImporter::VisitObjCTypeParamType( |
1924 | const clang::ObjCTypeParamType *T) { |
1925 | Expected<ObjCTypeParamDecl *> ToDeclOrErr = import(From: T->getDecl()); |
1926 | if (!ToDeclOrErr) |
1927 | return ToDeclOrErr.takeError(); |
1928 | |
1929 | SmallVector<ObjCProtocolDecl *, 4> ToProtocols; |
1930 | for (ObjCProtocolDecl *FromProtocol : T->getProtocols()) { |
1931 | Expected<ObjCProtocolDecl *> ToProtocolOrErr = import(From: FromProtocol); |
1932 | if (!ToProtocolOrErr) |
1933 | return ToProtocolOrErr.takeError(); |
1934 | ToProtocols.push_back(Elt: *ToProtocolOrErr); |
1935 | } |
1936 | |
1937 | return Importer.getToContext().getObjCTypeParamType(Decl: *ToDeclOrErr, |
1938 | protocols: ToProtocols); |
1939 | } |
1940 | |
1941 | ExpectedType clang::ASTNodeImporter::VisitPipeType(const clang::PipeType *T) { |
1942 | ExpectedType ToElementTypeOrErr = import(From: T->getElementType()); |
1943 | if (!ToElementTypeOrErr) |
1944 | return ToElementTypeOrErr.takeError(); |
1945 | |
1946 | ASTContext &ToCtx = Importer.getToContext(); |
1947 | if (T->isReadOnly()) |
1948 | return ToCtx.getReadPipeType(T: *ToElementTypeOrErr); |
1949 | else |
1950 | return ToCtx.getWritePipeType(T: *ToElementTypeOrErr); |
1951 | } |
1952 | |
1953 | //---------------------------------------------------------------------------- |
1954 | // Import Declarations |
1955 | //---------------------------------------------------------------------------- |
1956 | Error ASTNodeImporter::ImportDeclParts( |
1957 | NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC, |
1958 | DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc) { |
1959 | // Check if RecordDecl is in FunctionDecl parameters to avoid infinite loop. |
1960 | // example: int struct_in_proto(struct data_t{int a;int b;} *d); |
1961 | // FIXME: We could support these constructs by importing a different type of |
1962 | // this parameter and by importing the original type of the parameter only |
1963 | // after the FunctionDecl is created. See |
1964 | // VisitFunctionDecl::UsedDifferentProtoType. |
1965 | DeclContext *OrigDC = D->getDeclContext(); |
1966 | FunctionDecl *FunDecl; |
1967 | if (isa<RecordDecl>(Val: D) && (FunDecl = dyn_cast<FunctionDecl>(Val: OrigDC)) && |
1968 | FunDecl->hasBody()) { |
1969 | auto getLeafPointeeType = [](const Type *T) { |
1970 | while (T->isPointerType() || T->isArrayType()) { |
1971 | T = T->getPointeeOrArrayElementType(); |
1972 | } |
1973 | return T; |
1974 | }; |
1975 | for (const ParmVarDecl *P : FunDecl->parameters()) { |
1976 | const Type *LeafT = |
1977 | getLeafPointeeType(P->getType().getCanonicalType().getTypePtr()); |
1978 | auto *RT = dyn_cast<RecordType>(Val: LeafT); |
1979 | if (RT && RT->getDecl() == D) { |
1980 | Importer.FromDiag(Loc: D->getLocation(), DiagID: diag::err_unsupported_ast_node) |
1981 | << D->getDeclKindName(); |
1982 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
1983 | } |
1984 | } |
1985 | } |
1986 | |
1987 | // Import the context of this declaration. |
1988 | if (Error Err = ImportDeclContext(From: D, ToDC&: DC, ToLexicalDC&: LexicalDC)) |
1989 | return Err; |
1990 | |
1991 | // Import the name of this declaration. |
1992 | if (Error Err = importInto(To&: Name, From: D->getDeclName())) |
1993 | return Err; |
1994 | |
1995 | // Import the location of this declaration. |
1996 | if (Error Err = importInto(To&: Loc, From: D->getLocation())) |
1997 | return Err; |
1998 | |
1999 | ToD = cast_or_null<NamedDecl>(Val: Importer.GetAlreadyImportedOrNull(FromD: D)); |
2000 | if (ToD) |
2001 | if (Error Err = ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD: D, ToD)) |
2002 | return Err; |
2003 | |
2004 | return Error::success(); |
2005 | } |
2006 | |
2007 | Error ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclarationName &Name, |
2008 | NamedDecl *&ToD, SourceLocation &Loc) { |
2009 | |
2010 | // Import the name of this declaration. |
2011 | if (Error Err = importInto(To&: Name, From: D->getDeclName())) |
2012 | return Err; |
2013 | |
2014 | // Import the location of this declaration. |
2015 | if (Error Err = importInto(To&: Loc, From: D->getLocation())) |
2016 | return Err; |
2017 | |
2018 | ToD = cast_or_null<NamedDecl>(Val: Importer.GetAlreadyImportedOrNull(FromD: D)); |
2019 | if (ToD) |
2020 | if (Error Err = ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD: D, ToD)) |
2021 | return Err; |
2022 | |
2023 | return Error::success(); |
2024 | } |
2025 | |
2026 | Error ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) { |
2027 | if (!FromD) |
2028 | return Error::success(); |
2029 | |
2030 | if (!ToD) |
2031 | if (Error Err = importInto(To&: ToD, From: FromD)) |
2032 | return Err; |
2033 | |
2034 | if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(Val: FromD)) { |
2035 | if (RecordDecl *ToRecord = cast<RecordDecl>(Val: ToD)) { |
2036 | if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && |
2037 | !ToRecord->getDefinition()) { |
2038 | if (Error Err = ImportDefinition(From: FromRecord, To: ToRecord)) |
2039 | return Err; |
2040 | } |
2041 | } |
2042 | return Error::success(); |
2043 | } |
2044 | |
2045 | if (EnumDecl * = dyn_cast<EnumDecl>(Val: FromD)) { |
2046 | if (EnumDecl *ToEnum = cast<EnumDecl>(Val: ToD)) { |
2047 | if (FromEnum->getDefinition() && !ToEnum->getDefinition()) { |
2048 | if (Error Err = ImportDefinition(From: FromEnum, To: ToEnum)) |
2049 | return Err; |
2050 | } |
2051 | } |
2052 | return Error::success(); |
2053 | } |
2054 | |
2055 | return Error::success(); |
2056 | } |
2057 | |
2058 | Error |
2059 | ASTNodeImporter::ImportDeclarationNameLoc( |
2060 | const DeclarationNameInfo &From, DeclarationNameInfo& To) { |
2061 | // NOTE: To.Name and To.Loc are already imported. |
2062 | // We only have to import To.LocInfo. |
2063 | switch (To.getName().getNameKind()) { |
2064 | case DeclarationName::Identifier: |
2065 | case DeclarationName::ObjCZeroArgSelector: |
2066 | case DeclarationName::ObjCOneArgSelector: |
2067 | case DeclarationName::ObjCMultiArgSelector: |
2068 | case DeclarationName::CXXUsingDirective: |
2069 | case DeclarationName::CXXDeductionGuideName: |
2070 | return Error::success(); |
2071 | |
2072 | case DeclarationName::CXXOperatorName: { |
2073 | if (auto ToRangeOrErr = import(From: From.getCXXOperatorNameRange())) |
2074 | To.setCXXOperatorNameRange(*ToRangeOrErr); |
2075 | else |
2076 | return ToRangeOrErr.takeError(); |
2077 | return Error::success(); |
2078 | } |
2079 | case DeclarationName::CXXLiteralOperatorName: { |
2080 | if (ExpectedSLoc LocOrErr = import(From: From.getCXXLiteralOperatorNameLoc())) |
2081 | To.setCXXLiteralOperatorNameLoc(*LocOrErr); |
2082 | else |
2083 | return LocOrErr.takeError(); |
2084 | return Error::success(); |
2085 | } |
2086 | case DeclarationName::CXXConstructorName: |
2087 | case DeclarationName::CXXDestructorName: |
2088 | case DeclarationName::CXXConversionFunctionName: { |
2089 | if (auto ToTInfoOrErr = import(From: From.getNamedTypeInfo())) |
2090 | To.setNamedTypeInfo(*ToTInfoOrErr); |
2091 | else |
2092 | return ToTInfoOrErr.takeError(); |
2093 | return Error::success(); |
2094 | } |
2095 | } |
2096 | llvm_unreachable("Unknown name kind." ); |
2097 | } |
2098 | |
2099 | Error |
2100 | ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) { |
2101 | if (Importer.isMinimalImport() && !ForceImport) { |
2102 | auto ToDCOrErr = Importer.ImportContext(FromDC); |
2103 | return ToDCOrErr.takeError(); |
2104 | } |
2105 | |
2106 | // We use strict error handling in case of records and enums, but not |
2107 | // with e.g. namespaces. |
2108 | // |
2109 | // FIXME Clients of the ASTImporter should be able to choose an |
2110 | // appropriate error handling strategy for their needs. For instance, |
2111 | // they may not want to mark an entire namespace as erroneous merely |
2112 | // because there is an ODR error with two typedefs. As another example, |
2113 | // the client may allow EnumConstantDecls with same names but with |
2114 | // different values in two distinct translation units. |
2115 | ChildErrorHandlingStrategy HandleChildErrors(FromDC); |
2116 | |
2117 | auto MightNeedReordering = [](const Decl *D) { |
2118 | return isa<FieldDecl>(Val: D) || isa<IndirectFieldDecl>(Val: D) || isa<FriendDecl>(Val: D); |
2119 | }; |
2120 | |
2121 | // Import everything that might need reordering first. |
2122 | Error ChildErrors = Error::success(); |
2123 | for (auto *From : FromDC->decls()) { |
2124 | if (!MightNeedReordering(From)) |
2125 | continue; |
2126 | |
2127 | ExpectedDecl ImportedOrErr = import(From); |
2128 | |
2129 | // If we are in the process of ImportDefinition(...) for a RecordDecl we |
2130 | // want to make sure that we are also completing each FieldDecl. There |
2131 | // are currently cases where this does not happen and this is correctness |
2132 | // fix since operations such as code generation will expect this to be so. |
2133 | if (!ImportedOrErr) { |
2134 | HandleChildErrors.handleChildImportResult(ResultErr&: ChildErrors, |
2135 | ChildErr: ImportedOrErr.takeError()); |
2136 | continue; |
2137 | } |
2138 | FieldDecl *FieldFrom = dyn_cast_or_null<FieldDecl>(Val: From); |
2139 | Decl *ImportedDecl = *ImportedOrErr; |
2140 | FieldDecl *FieldTo = dyn_cast_or_null<FieldDecl>(Val: ImportedDecl); |
2141 | if (FieldFrom && FieldTo) { |
2142 | Error Err = ImportFieldDeclDefinition(From: FieldFrom, To: FieldTo); |
2143 | HandleChildErrors.handleChildImportResult(ResultErr&: ChildErrors, ChildErr: std::move(Err)); |
2144 | } |
2145 | } |
2146 | |
2147 | // We reorder declarations in RecordDecls because they may have another order |
2148 | // in the "to" context than they have in the "from" context. This may happen |
2149 | // e.g when we import a class like this: |
2150 | // struct declToImport { |
2151 | // int a = c + b; |
2152 | // int b = 1; |
2153 | // int c = 2; |
2154 | // }; |
2155 | // During the import of `a` we import first the dependencies in sequence, |
2156 | // thus the order would be `c`, `b`, `a`. We will get the normal order by |
2157 | // first removing the already imported members and then adding them in the |
2158 | // order as they appear in the "from" context. |
2159 | // |
2160 | // Keeping field order is vital because it determines structure layout. |
2161 | // |
2162 | // Here and below, we cannot call field_begin() method and its callers on |
2163 | // ToDC if it has an external storage. Calling field_begin() will |
2164 | // automatically load all the fields by calling |
2165 | // LoadFieldsFromExternalStorage(). LoadFieldsFromExternalStorage() would |
2166 | // call ASTImporter::Import(). This is because the ExternalASTSource |
2167 | // interface in LLDB is implemented by the means of the ASTImporter. However, |
2168 | // calling an import at this point would result in an uncontrolled import, we |
2169 | // must avoid that. |
2170 | |
2171 | auto ToDCOrErr = Importer.ImportContext(FromDC); |
2172 | if (!ToDCOrErr) { |
2173 | consumeError(Err: std::move(ChildErrors)); |
2174 | return ToDCOrErr.takeError(); |
2175 | } |
2176 | |
2177 | if (const auto *FromRD = dyn_cast<RecordDecl>(Val: FromDC)) { |
2178 | DeclContext *ToDC = *ToDCOrErr; |
2179 | // Remove all declarations, which may be in wrong order in the |
2180 | // lexical DeclContext and then add them in the proper order. |
2181 | for (auto *D : FromRD->decls()) { |
2182 | if (!MightNeedReordering(D)) |
2183 | continue; |
2184 | |
2185 | assert(D && "DC contains a null decl" ); |
2186 | if (Decl *ToD = Importer.GetAlreadyImportedOrNull(FromD: D)) { |
2187 | // Remove only the decls which we successfully imported. |
2188 | assert(ToDC == ToD->getLexicalDeclContext() && ToDC->containsDecl(ToD)); |
2189 | // Remove the decl from its wrong place in the linked list. |
2190 | ToDC->removeDecl(D: ToD); |
2191 | // Add the decl to the end of the linked list. |
2192 | // This time it will be at the proper place because the enclosing for |
2193 | // loop iterates in the original (good) order of the decls. |
2194 | ToDC->addDeclInternal(D: ToD); |
2195 | } |
2196 | } |
2197 | } |
2198 | |
2199 | // Import everything else. |
2200 | for (auto *From : FromDC->decls()) { |
2201 | if (MightNeedReordering(From)) |
2202 | continue; |
2203 | |
2204 | ExpectedDecl ImportedOrErr = import(From); |
2205 | if (!ImportedOrErr) |
2206 | HandleChildErrors.handleChildImportResult(ResultErr&: ChildErrors, |
2207 | ChildErr: ImportedOrErr.takeError()); |
2208 | } |
2209 | |
2210 | return ChildErrors; |
2211 | } |
2212 | |
2213 | Error ASTNodeImporter::ImportFieldDeclDefinition(const FieldDecl *From, |
2214 | const FieldDecl *To) { |
2215 | RecordDecl *FromRecordDecl = nullptr; |
2216 | RecordDecl *ToRecordDecl = nullptr; |
2217 | // If we have a field that is an ArrayType we need to check if the array |
2218 | // element is a RecordDecl and if so we need to import the definition. |
2219 | QualType FromType = From->getType(); |
2220 | QualType ToType = To->getType(); |
2221 | if (FromType->isArrayType()) { |
2222 | // getBaseElementTypeUnsafe(...) handles multi-dimensional arrays for us. |
2223 | FromRecordDecl = FromType->getBaseElementTypeUnsafe()->getAsRecordDecl(); |
2224 | ToRecordDecl = ToType->getBaseElementTypeUnsafe()->getAsRecordDecl(); |
2225 | } |
2226 | |
2227 | if (!FromRecordDecl || !ToRecordDecl) { |
2228 | const RecordType *RecordFrom = FromType->getAs<RecordType>(); |
2229 | const RecordType *RecordTo = ToType->getAs<RecordType>(); |
2230 | |
2231 | if (RecordFrom && RecordTo) { |
2232 | FromRecordDecl = RecordFrom->getDecl(); |
2233 | ToRecordDecl = RecordTo->getDecl(); |
2234 | } |
2235 | } |
2236 | |
2237 | if (FromRecordDecl && ToRecordDecl) { |
2238 | if (FromRecordDecl->isCompleteDefinition() && |
2239 | !ToRecordDecl->isCompleteDefinition()) |
2240 | return ImportDefinition(From: FromRecordDecl, To: ToRecordDecl); |
2241 | } |
2242 | |
2243 | return Error::success(); |
2244 | } |
2245 | |
2246 | Error ASTNodeImporter::ImportDeclContext( |
2247 | Decl *FromD, DeclContext *&ToDC, DeclContext *&ToLexicalDC) { |
2248 | auto ToDCOrErr = Importer.ImportContext(FromDC: FromD->getDeclContext()); |
2249 | if (!ToDCOrErr) |
2250 | return ToDCOrErr.takeError(); |
2251 | ToDC = *ToDCOrErr; |
2252 | |
2253 | if (FromD->getDeclContext() != FromD->getLexicalDeclContext()) { |
2254 | auto ToLexicalDCOrErr = Importer.ImportContext( |
2255 | FromDC: FromD->getLexicalDeclContext()); |
2256 | if (!ToLexicalDCOrErr) |
2257 | return ToLexicalDCOrErr.takeError(); |
2258 | ToLexicalDC = *ToLexicalDCOrErr; |
2259 | } else |
2260 | ToLexicalDC = ToDC; |
2261 | |
2262 | return Error::success(); |
2263 | } |
2264 | |
2265 | Error ASTNodeImporter::ImportImplicitMethods( |
2266 | const CXXRecordDecl *From, CXXRecordDecl *To) { |
2267 | assert(From->isCompleteDefinition() && To->getDefinition() == To && |
2268 | "Import implicit methods to or from non-definition" ); |
2269 | |
2270 | for (CXXMethodDecl *FromM : From->methods()) |
2271 | if (FromM->isImplicit()) { |
2272 | Expected<CXXMethodDecl *> ToMOrErr = import(From: FromM); |
2273 | if (!ToMOrErr) |
2274 | return ToMOrErr.takeError(); |
2275 | } |
2276 | |
2277 | return Error::success(); |
2278 | } |
2279 | |
2280 | static Error setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To, |
2281 | ASTImporter &Importer) { |
2282 | if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) { |
2283 | if (ExpectedDecl ToTypedefOrErr = Importer.Import(FromD: FromTypedef)) |
2284 | To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(Val: *ToTypedefOrErr)); |
2285 | else |
2286 | return ToTypedefOrErr.takeError(); |
2287 | } |
2288 | return Error::success(); |
2289 | } |
2290 | |
2291 | Error ASTNodeImporter::ImportDefinition( |
2292 | RecordDecl *From, RecordDecl *To, ImportDefinitionKind Kind) { |
2293 | auto DefinitionCompleter = [To]() { |
2294 | // There are cases in LLDB when we first import a class without its |
2295 | // members. The class will have DefinitionData, but no members. Then, |
2296 | // importDefinition is called from LLDB, which tries to get the members, so |
2297 | // when we get here, the class already has the DefinitionData set, so we |
2298 | // must unset the CompleteDefinition here to be able to complete again the |
2299 | // definition. |
2300 | To->setCompleteDefinition(false); |
2301 | To->completeDefinition(); |
2302 | }; |
2303 | |
2304 | if (To->getDefinition() || To->isBeingDefined()) { |
2305 | if (Kind == IDK_Everything || |
2306 | // In case of lambdas, the class already has a definition ptr set, but |
2307 | // the contained decls are not imported yet. Also, isBeingDefined was |
2308 | // set in CXXRecordDecl::CreateLambda. We must import the contained |
2309 | // decls here and finish the definition. |
2310 | (To->isLambda() && shouldForceImportDeclContext(IDK: Kind))) { |
2311 | if (To->isLambda()) { |
2312 | auto *FromCXXRD = cast<CXXRecordDecl>(Val: From); |
2313 | SmallVector<LambdaCapture, 8> ToCaptures; |
2314 | ToCaptures.reserve(N: FromCXXRD->capture_size()); |
2315 | for (const auto &FromCapture : FromCXXRD->captures()) { |
2316 | if (auto ToCaptureOrErr = import(From: FromCapture)) |
2317 | ToCaptures.push_back(Elt: *ToCaptureOrErr); |
2318 | else |
2319 | return ToCaptureOrErr.takeError(); |
2320 | } |
2321 | cast<CXXRecordDecl>(Val: To)->setCaptures(Context&: Importer.getToContext(), |
2322 | Captures: ToCaptures); |
2323 | } |
2324 | |
2325 | Error Result = ImportDeclContext(FromDC: From, /*ForceImport=*/true); |
2326 | // Finish the definition of the lambda, set isBeingDefined to false. |
2327 | if (To->isLambda()) |
2328 | DefinitionCompleter(); |
2329 | return Result; |
2330 | } |
2331 | |
2332 | return Error::success(); |
2333 | } |
2334 | |
2335 | To->startDefinition(); |
2336 | // Set the definition to complete even if it is really not complete during |
2337 | // import. Some AST constructs (expressions) require the record layout |
2338 | // to be calculated (see 'clang::computeDependence') at the time they are |
2339 | // constructed. Import of such AST node is possible during import of the |
2340 | // same record, there is no way to have a completely defined record (all |
2341 | // fields imported) at that time without multiple AST import passes. |
2342 | if (!Importer.isMinimalImport()) |
2343 | To->setCompleteDefinition(true); |
2344 | // Complete the definition even if error is returned. |
2345 | // The RecordDecl may be already part of the AST so it is better to |
2346 | // have it in complete state even if something is wrong with it. |
2347 | auto DefinitionCompleterScopeExit = |
2348 | llvm::make_scope_exit(F&: DefinitionCompleter); |
2349 | |
2350 | if (Error Err = setTypedefNameForAnonDecl(From, To, Importer)) |
2351 | return Err; |
2352 | |
2353 | // Add base classes. |
2354 | auto *ToCXX = dyn_cast<CXXRecordDecl>(Val: To); |
2355 | auto *FromCXX = dyn_cast<CXXRecordDecl>(Val: From); |
2356 | if (ToCXX && FromCXX && ToCXX->dataPtr() && FromCXX->dataPtr()) { |
2357 | |
2358 | struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data(); |
2359 | struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data(); |
2360 | |
2361 | #define FIELD(Name, Width, Merge) \ |
2362 | ToData.Name = FromData.Name; |
2363 | #include "clang/AST/CXXRecordDeclDefinitionBits.def" |
2364 | |
2365 | // Copy over the data stored in RecordDeclBits |
2366 | ToCXX->setArgPassingRestrictions(FromCXX->getArgPassingRestrictions()); |
2367 | |
2368 | SmallVector<CXXBaseSpecifier *, 4> Bases; |
2369 | for (const auto &Base1 : FromCXX->bases()) { |
2370 | ExpectedType TyOrErr = import(From: Base1.getType()); |
2371 | if (!TyOrErr) |
2372 | return TyOrErr.takeError(); |
2373 | |
2374 | SourceLocation EllipsisLoc; |
2375 | if (Base1.isPackExpansion()) { |
2376 | if (ExpectedSLoc LocOrErr = import(From: Base1.getEllipsisLoc())) |
2377 | EllipsisLoc = *LocOrErr; |
2378 | else |
2379 | return LocOrErr.takeError(); |
2380 | } |
2381 | |
2382 | // Ensure that we have a definition for the base. |
2383 | if (Error Err = |
2384 | ImportDefinitionIfNeeded(FromD: Base1.getType()->getAsCXXRecordDecl())) |
2385 | return Err; |
2386 | |
2387 | auto RangeOrErr = import(From: Base1.getSourceRange()); |
2388 | if (!RangeOrErr) |
2389 | return RangeOrErr.takeError(); |
2390 | |
2391 | auto TSIOrErr = import(From: Base1.getTypeSourceInfo()); |
2392 | if (!TSIOrErr) |
2393 | return TSIOrErr.takeError(); |
2394 | |
2395 | Bases.push_back( |
2396 | Elt: new (Importer.getToContext()) CXXBaseSpecifier( |
2397 | *RangeOrErr, |
2398 | Base1.isVirtual(), |
2399 | Base1.isBaseOfClass(), |
2400 | Base1.getAccessSpecifierAsWritten(), |
2401 | *TSIOrErr, |
2402 | EllipsisLoc)); |
2403 | } |
2404 | if (!Bases.empty()) |
2405 | ToCXX->setBases(Bases: Bases.data(), NumBases: Bases.size()); |
2406 | } |
2407 | |
2408 | if (shouldForceImportDeclContext(IDK: Kind)) { |
2409 | if (Error Err = ImportDeclContext(FromDC: From, /*ForceImport=*/true)) |
2410 | return Err; |
2411 | } |
2412 | |
2413 | return Error::success(); |
2414 | } |
2415 | |
2416 | Error ASTNodeImporter::ImportInitializer(VarDecl *From, VarDecl *To) { |
2417 | if (To->getAnyInitializer()) |
2418 | return Error::success(); |
2419 | |
2420 | Expr *FromInit = From->getInit(); |
2421 | if (!FromInit) |
2422 | return Error::success(); |
2423 | |
2424 | ExpectedExpr ToInitOrErr = import(From: FromInit); |
2425 | if (!ToInitOrErr) |
2426 | return ToInitOrErr.takeError(); |
2427 | |
2428 | To->setInit(*ToInitOrErr); |
2429 | if (EvaluatedStmt *FromEval = From->getEvaluatedStmt()) { |
2430 | EvaluatedStmt *ToEval = To->ensureEvaluatedStmt(); |
2431 | ToEval->HasConstantInitialization = FromEval->HasConstantInitialization; |
2432 | ToEval->HasConstantDestruction = FromEval->HasConstantDestruction; |
2433 | // FIXME: Also import the initializer value. |
2434 | } |
2435 | |
2436 | // FIXME: Other bits to merge? |
2437 | return Error::success(); |
2438 | } |
2439 | |
2440 | Error ASTNodeImporter::ImportDefinition( |
2441 | EnumDecl *From, EnumDecl *To, ImportDefinitionKind Kind) { |
2442 | if (To->getDefinition() || To->isBeingDefined()) { |
2443 | if (Kind == IDK_Everything) |
2444 | return ImportDeclContext(FromDC: From, /*ForceImport=*/true); |
2445 | return Error::success(); |
2446 | } |
2447 | |
2448 | To->startDefinition(); |
2449 | |
2450 | if (Error Err = setTypedefNameForAnonDecl(From, To, Importer)) |
2451 | return Err; |
2452 | |
2453 | ExpectedType ToTypeOrErr = |
2454 | import(From: Importer.getFromContext().getTypeDeclType(Decl: From)); |
2455 | if (!ToTypeOrErr) |
2456 | return ToTypeOrErr.takeError(); |
2457 | |
2458 | ExpectedType ToPromotionTypeOrErr = import(From: From->getPromotionType()); |
2459 | if (!ToPromotionTypeOrErr) |
2460 | return ToPromotionTypeOrErr.takeError(); |
2461 | |
2462 | if (shouldForceImportDeclContext(IDK: Kind)) |
2463 | if (Error Err = ImportDeclContext(FromDC: From, /*ForceImport=*/true)) |
2464 | return Err; |
2465 | |
2466 | // FIXME: we might need to merge the number of positive or negative bits |
2467 | // if the enumerator lists don't match. |
2468 | To->completeDefinition(NewType: *ToTypeOrErr, PromotionType: *ToPromotionTypeOrErr, |
2469 | NumPositiveBits: From->getNumPositiveBits(), |
2470 | NumNegativeBits: From->getNumNegativeBits()); |
2471 | return Error::success(); |
2472 | } |
2473 | |
2474 | Error ASTNodeImporter::ImportTemplateArguments( |
2475 | ArrayRef<TemplateArgument> FromArgs, |
2476 | SmallVectorImpl<TemplateArgument> &ToArgs) { |
2477 | for (const auto &Arg : FromArgs) { |
2478 | if (auto ToOrErr = import(From: Arg)) |
2479 | ToArgs.push_back(Elt: *ToOrErr); |
2480 | else |
2481 | return ToOrErr.takeError(); |
2482 | } |
2483 | |
2484 | return Error::success(); |
2485 | } |
2486 | |
2487 | // FIXME: Do not forget to remove this and use only 'import'. |
2488 | Expected<TemplateArgument> |
2489 | ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) { |
2490 | return import(From); |
2491 | } |
2492 | |
2493 | template <typename InContainerTy> |
2494 | Error ASTNodeImporter::ImportTemplateArgumentListInfo( |
2495 | const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) { |
2496 | for (const auto &FromLoc : Container) { |
2497 | if (auto ToLocOrErr = import(FromLoc)) |
2498 | ToTAInfo.addArgument(Loc: *ToLocOrErr); |
2499 | else |
2500 | return ToLocOrErr.takeError(); |
2501 | } |
2502 | return Error::success(); |
2503 | } |
2504 | |
2505 | static StructuralEquivalenceKind |
2506 | getStructuralEquivalenceKind(const ASTImporter &Importer) { |
2507 | return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal |
2508 | : StructuralEquivalenceKind::Default; |
2509 | } |
2510 | |
2511 | bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain, |
2512 | bool IgnoreTemplateParmDepth) { |
2513 | // Eliminate a potential failure point where we attempt to re-import |
2514 | // something we're trying to import while completing ToRecord. |
2515 | Decl *ToOrigin = Importer.GetOriginalDecl(To); |
2516 | if (ToOrigin) { |
2517 | To = ToOrigin; |
2518 | } |
2519 | |
2520 | StructuralEquivalenceContext Ctx( |
2521 | Importer.getToContext().getLangOpts(), Importer.getFromContext(), |
2522 | Importer.getToContext(), Importer.getNonEquivalentDecls(), |
2523 | getStructuralEquivalenceKind(Importer), |
2524 | /*StrictTypeSpelling=*/false, Complain, /*ErrorOnTagTypeMismatch=*/false, |
2525 | IgnoreTemplateParmDepth); |
2526 | return Ctx.IsEquivalent(D1: From, D2: To); |
2527 | } |
2528 | |
2529 | ExpectedDecl ASTNodeImporter::VisitDecl(Decl *D) { |
2530 | Importer.FromDiag(Loc: D->getLocation(), DiagID: diag::err_unsupported_ast_node) |
2531 | << D->getDeclKindName(); |
2532 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
2533 | } |
2534 | |
2535 | ExpectedDecl ASTNodeImporter::VisitImportDecl(ImportDecl *D) { |
2536 | Importer.FromDiag(Loc: D->getLocation(), DiagID: diag::err_unsupported_ast_node) |
2537 | << D->getDeclKindName(); |
2538 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
2539 | } |
2540 | |
2541 | ExpectedDecl ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) { |
2542 | // Import the context of this declaration. |
2543 | DeclContext *DC, *LexicalDC; |
2544 | if (Error Err = ImportDeclContext(FromD: D, ToDC&: DC, ToLexicalDC&: LexicalDC)) |
2545 | return std::move(Err); |
2546 | |
2547 | // Import the location of this declaration. |
2548 | ExpectedSLoc LocOrErr = import(From: D->getLocation()); |
2549 | if (!LocOrErr) |
2550 | return LocOrErr.takeError(); |
2551 | |
2552 | EmptyDecl *ToD; |
2553 | if (GetImportedOrCreateDecl(ToD, FromD: D, args&: Importer.getToContext(), args&: DC, args&: *LocOrErr)) |
2554 | return ToD; |
2555 | |
2556 | ToD->setLexicalDeclContext(LexicalDC); |
2557 | LexicalDC->addDeclInternal(D: ToD); |
2558 | return ToD; |
2559 | } |
2560 | |
2561 | ExpectedDecl ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
2562 | TranslationUnitDecl *ToD = |
2563 | Importer.getToContext().getTranslationUnitDecl(); |
2564 | |
2565 | Importer.MapImported(From: D, To: ToD); |
2566 | |
2567 | return ToD; |
2568 | } |
2569 | |
2570 | ExpectedDecl ASTNodeImporter::VisitBindingDecl(BindingDecl *D) { |
2571 | DeclContext *DC, *LexicalDC; |
2572 | DeclarationName Name; |
2573 | SourceLocation Loc; |
2574 | NamedDecl *ToND; |
2575 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD&: ToND, Loc)) |
2576 | return std::move(Err); |
2577 | if (ToND) |
2578 | return ToND; |
2579 | |
2580 | BindingDecl *ToD; |
2581 | if (GetImportedOrCreateDecl(ToD, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, |
2582 | args: Name.getAsIdentifierInfo(), args: D->getType())) |
2583 | return ToD; |
2584 | |
2585 | Error Err = Error::success(); |
2586 | QualType ToType = importChecked(Err, From: D->getType()); |
2587 | Expr *ToBinding = importChecked(Err, From: D->getBinding()); |
2588 | ValueDecl *ToDecomposedDecl = importChecked(Err, From: D->getDecomposedDecl()); |
2589 | if (Err) |
2590 | return std::move(Err); |
2591 | |
2592 | ToD->setBinding(DeclaredType: ToType, Binding: ToBinding); |
2593 | ToD->setDecomposedDecl(ToDecomposedDecl); |
2594 | addDeclToContexts(FromD: D, ToD); |
2595 | |
2596 | return ToD; |
2597 | } |
2598 | |
2599 | ExpectedDecl ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) { |
2600 | ExpectedSLoc LocOrErr = import(From: D->getLocation()); |
2601 | if (!LocOrErr) |
2602 | return LocOrErr.takeError(); |
2603 | auto ColonLocOrErr = import(From: D->getColonLoc()); |
2604 | if (!ColonLocOrErr) |
2605 | return ColonLocOrErr.takeError(); |
2606 | |
2607 | // Import the context of this declaration. |
2608 | auto DCOrErr = Importer.ImportContext(FromDC: D->getDeclContext()); |
2609 | if (!DCOrErr) |
2610 | return DCOrErr.takeError(); |
2611 | DeclContext *DC = *DCOrErr; |
2612 | |
2613 | AccessSpecDecl *ToD; |
2614 | if (GetImportedOrCreateDecl(ToD, FromD: D, args&: Importer.getToContext(), args: D->getAccess(), |
2615 | args&: DC, args&: *LocOrErr, args&: *ColonLocOrErr)) |
2616 | return ToD; |
2617 | |
2618 | // Lexical DeclContext and Semantic DeclContext |
2619 | // is always the same for the accessSpec. |
2620 | ToD->setLexicalDeclContext(DC); |
2621 | DC->addDeclInternal(D: ToD); |
2622 | |
2623 | return ToD; |
2624 | } |
2625 | |
2626 | ExpectedDecl ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) { |
2627 | auto DCOrErr = Importer.ImportContext(FromDC: D->getDeclContext()); |
2628 | if (!DCOrErr) |
2629 | return DCOrErr.takeError(); |
2630 | DeclContext *DC = *DCOrErr; |
2631 | DeclContext *LexicalDC = DC; |
2632 | |
2633 | Error Err = Error::success(); |
2634 | auto ToLocation = importChecked(Err, From: D->getLocation()); |
2635 | auto ToRParenLoc = importChecked(Err, From: D->getRParenLoc()); |
2636 | auto ToAssertExpr = importChecked(Err, From: D->getAssertExpr()); |
2637 | auto ToMessage = importChecked(Err, From: D->getMessage()); |
2638 | if (Err) |
2639 | return std::move(Err); |
2640 | |
2641 | StaticAssertDecl *ToD; |
2642 | if (GetImportedOrCreateDecl( |
2643 | ToD, FromD: D, args&: Importer.getToContext(), args&: DC, args&: ToLocation, args&: ToAssertExpr, args&: ToMessage, |
2644 | args&: ToRParenLoc, args: D->isFailed())) |
2645 | return ToD; |
2646 | |
2647 | ToD->setLexicalDeclContext(LexicalDC); |
2648 | LexicalDC->addDeclInternal(D: ToD); |
2649 | return ToD; |
2650 | } |
2651 | |
2652 | ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) { |
2653 | // Import the major distinguishing characteristics of this namespace. |
2654 | DeclContext *DC, *LexicalDC; |
2655 | DeclarationName Name; |
2656 | SourceLocation Loc; |
2657 | NamedDecl *ToD; |
2658 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
2659 | return std::move(Err); |
2660 | if (ToD) |
2661 | return ToD; |
2662 | |
2663 | NamespaceDecl *MergeWithNamespace = nullptr; |
2664 | if (!Name) { |
2665 | // This is an anonymous namespace. Adopt an existing anonymous |
2666 | // namespace if we can. |
2667 | DeclContext *EnclosingDC = DC->getEnclosingNamespaceContext(); |
2668 | if (auto *TU = dyn_cast<TranslationUnitDecl>(Val: EnclosingDC)) |
2669 | MergeWithNamespace = TU->getAnonymousNamespace(); |
2670 | else |
2671 | MergeWithNamespace = |
2672 | cast<NamespaceDecl>(Val: EnclosingDC)->getAnonymousNamespace(); |
2673 | } else { |
2674 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
2675 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
2676 | for (auto *FoundDecl : FoundDecls) { |
2677 | if (!FoundDecl->isInIdentifierNamespace(NS: Decl::IDNS_Namespace)) |
2678 | continue; |
2679 | |
2680 | if (auto *FoundNS = dyn_cast<NamespaceDecl>(Val: FoundDecl)) { |
2681 | MergeWithNamespace = FoundNS; |
2682 | ConflictingDecls.clear(); |
2683 | break; |
2684 | } |
2685 | |
2686 | ConflictingDecls.push_back(Elt: FoundDecl); |
2687 | } |
2688 | |
2689 | if (!ConflictingDecls.empty()) { |
2690 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
2691 | Name, DC, IDNS: Decl::IDNS_Namespace, Decls: ConflictingDecls.data(), |
2692 | NumDecls: ConflictingDecls.size()); |
2693 | if (NameOrErr) |
2694 | Name = NameOrErr.get(); |
2695 | else |
2696 | return NameOrErr.takeError(); |
2697 | } |
2698 | } |
2699 | |
2700 | ExpectedSLoc BeginLocOrErr = import(From: D->getBeginLoc()); |
2701 | if (!BeginLocOrErr) |
2702 | return BeginLocOrErr.takeError(); |
2703 | ExpectedSLoc RBraceLocOrErr = import(From: D->getRBraceLoc()); |
2704 | if (!RBraceLocOrErr) |
2705 | return RBraceLocOrErr.takeError(); |
2706 | |
2707 | // Create the "to" namespace, if needed. |
2708 | NamespaceDecl *ToNamespace = MergeWithNamespace; |
2709 | if (!ToNamespace) { |
2710 | if (GetImportedOrCreateDecl(ToD&: ToNamespace, FromD: D, args&: Importer.getToContext(), args&: DC, |
2711 | args: D->isInline(), args&: *BeginLocOrErr, args&: Loc, |
2712 | args: Name.getAsIdentifierInfo(), |
2713 | /*PrevDecl=*/args: nullptr, args: D->isNested())) |
2714 | return ToNamespace; |
2715 | ToNamespace->setRBraceLoc(*RBraceLocOrErr); |
2716 | ToNamespace->setLexicalDeclContext(LexicalDC); |
2717 | LexicalDC->addDeclInternal(D: ToNamespace); |
2718 | |
2719 | // If this is an anonymous namespace, register it as the anonymous |
2720 | // namespace within its context. |
2721 | if (!Name) { |
2722 | if (auto *TU = dyn_cast<TranslationUnitDecl>(Val: DC)) |
2723 | TU->setAnonymousNamespace(ToNamespace); |
2724 | else |
2725 | cast<NamespaceDecl>(Val: DC)->setAnonymousNamespace(ToNamespace); |
2726 | } |
2727 | } |
2728 | Importer.MapImported(From: D, To: ToNamespace); |
2729 | |
2730 | if (Error Err = ImportDeclContext(FromDC: D)) |
2731 | return std::move(Err); |
2732 | |
2733 | return ToNamespace; |
2734 | } |
2735 | |
2736 | ExpectedDecl ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
2737 | // Import the major distinguishing characteristics of this namespace. |
2738 | DeclContext *DC, *LexicalDC; |
2739 | DeclarationName Name; |
2740 | SourceLocation Loc; |
2741 | NamedDecl *LookupD; |
2742 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD&: LookupD, Loc)) |
2743 | return std::move(Err); |
2744 | if (LookupD) |
2745 | return LookupD; |
2746 | |
2747 | // NOTE: No conflict resolution is done for namespace aliases now. |
2748 | |
2749 | Error Err = Error::success(); |
2750 | auto ToNamespaceLoc = importChecked(Err, From: D->getNamespaceLoc()); |
2751 | auto ToAliasLoc = importChecked(Err, From: D->getAliasLoc()); |
2752 | auto ToQualifierLoc = importChecked(Err, From: D->getQualifierLoc()); |
2753 | auto ToTargetNameLoc = importChecked(Err, From: D->getTargetNameLoc()); |
2754 | auto ToNamespace = importChecked(Err, From: D->getNamespace()); |
2755 | if (Err) |
2756 | return std::move(Err); |
2757 | |
2758 | IdentifierInfo *ToIdentifier = Importer.Import(FromId: D->getIdentifier()); |
2759 | |
2760 | NamespaceAliasDecl *ToD; |
2761 | if (GetImportedOrCreateDecl( |
2762 | ToD, FromD: D, args&: Importer.getToContext(), args&: DC, args&: ToNamespaceLoc, args&: ToAliasLoc, |
2763 | args&: ToIdentifier, args&: ToQualifierLoc, args&: ToTargetNameLoc, args&: ToNamespace)) |
2764 | return ToD; |
2765 | |
2766 | ToD->setLexicalDeclContext(LexicalDC); |
2767 | LexicalDC->addDeclInternal(D: ToD); |
2768 | |
2769 | return ToD; |
2770 | } |
2771 | |
2772 | ExpectedDecl |
2773 | ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) { |
2774 | // Import the major distinguishing characteristics of this typedef. |
2775 | DeclarationName Name; |
2776 | SourceLocation Loc; |
2777 | NamedDecl *ToD; |
2778 | // Do not import the DeclContext, we will import it once the TypedefNameDecl |
2779 | // is created. |
2780 | if (Error Err = ImportDeclParts(D, Name, ToD, Loc)) |
2781 | return std::move(Err); |
2782 | if (ToD) |
2783 | return ToD; |
2784 | |
2785 | DeclContext *DC = cast_or_null<DeclContext>( |
2786 | Val: Importer.GetAlreadyImportedOrNull(FromD: cast<Decl>(Val: D->getDeclContext()))); |
2787 | DeclContext *LexicalDC = |
2788 | cast_or_null<DeclContext>(Val: Importer.GetAlreadyImportedOrNull( |
2789 | FromD: cast<Decl>(Val: D->getLexicalDeclContext()))); |
2790 | |
2791 | // If this typedef is not in block scope, determine whether we've |
2792 | // seen a typedef with the same name (that we can merge with) or any |
2793 | // other entity by that name (which name lookup could conflict with). |
2794 | // Note: Repeated typedefs are not valid in C99: |
2795 | // 'typedef int T; typedef int T;' is invalid |
2796 | // We do not care about this now. |
2797 | if (DC && !DC->isFunctionOrMethod()) { |
2798 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
2799 | unsigned IDNS = Decl::IDNS_Ordinary; |
2800 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
2801 | for (auto *FoundDecl : FoundDecls) { |
2802 | if (!FoundDecl->isInIdentifierNamespace(NS: IDNS)) |
2803 | continue; |
2804 | if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(Val: FoundDecl)) { |
2805 | if (!hasSameVisibilityContextAndLinkage(Found: FoundTypedef, From: D)) |
2806 | continue; |
2807 | |
2808 | QualType FromUT = D->getUnderlyingType(); |
2809 | QualType FoundUT = FoundTypedef->getUnderlyingType(); |
2810 | if (Importer.IsStructurallyEquivalent(From: FromUT, To: FoundUT)) { |
2811 | // If the underlying declarations are unnamed records these can be |
2812 | // imported as different types. We should create a distinct typedef |
2813 | // node in this case. |
2814 | // If we found an existing underlying type with a record in a |
2815 | // different context (than the imported), this is already reason for |
2816 | // having distinct typedef nodes for these. |
2817 | // Again this can create situation like |
2818 | // 'typedef int T; typedef int T;' but this is hard to avoid without |
2819 | // a rename strategy at import. |
2820 | if (!FromUT.isNull() && !FoundUT.isNull()) { |
2821 | RecordDecl *FromR = FromUT->getAsRecordDecl(); |
2822 | RecordDecl *FoundR = FoundUT->getAsRecordDecl(); |
2823 | if (FromR && FoundR && |
2824 | !hasSameVisibilityContextAndLinkage(Found: FoundR, From: FromR)) |
2825 | continue; |
2826 | } |
2827 | // If the "From" context has a complete underlying type but we |
2828 | // already have a complete underlying type then return with that. |
2829 | if (!FromUT->isIncompleteType() && !FoundUT->isIncompleteType()) |
2830 | return Importer.MapImported(From: D, To: FoundTypedef); |
2831 | // FIXME Handle redecl chain. When you do that make consistent changes |
2832 | // in ASTImporterLookupTable too. |
2833 | } else { |
2834 | ConflictingDecls.push_back(Elt: FoundDecl); |
2835 | } |
2836 | } |
2837 | } |
2838 | |
2839 | if (!ConflictingDecls.empty()) { |
2840 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
2841 | Name, DC, IDNS, Decls: ConflictingDecls.data(), NumDecls: ConflictingDecls.size()); |
2842 | if (NameOrErr) |
2843 | Name = NameOrErr.get(); |
2844 | else |
2845 | return NameOrErr.takeError(); |
2846 | } |
2847 | } |
2848 | |
2849 | Error Err = Error::success(); |
2850 | auto ToUnderlyingType = importChecked(Err, From: D->getUnderlyingType()); |
2851 | auto ToTypeSourceInfo = importChecked(Err, From: D->getTypeSourceInfo()); |
2852 | auto ToBeginLoc = importChecked(Err, From: D->getBeginLoc()); |
2853 | if (Err) |
2854 | return std::move(Err); |
2855 | |
2856 | // Create the new typedef node. |
2857 | // FIXME: ToUnderlyingType is not used. |
2858 | (void)ToUnderlyingType; |
2859 | TypedefNameDecl *ToTypedef; |
2860 | if (IsAlias) { |
2861 | if (GetImportedOrCreateDecl<TypeAliasDecl>( |
2862 | ToD&: ToTypedef, FromD: D, args&: Importer.getToContext(), args&: DC, args&: ToBeginLoc, args&: Loc, |
2863 | args: Name.getAsIdentifierInfo(), args&: ToTypeSourceInfo)) |
2864 | return ToTypedef; |
2865 | } else if (GetImportedOrCreateDecl<TypedefDecl>( |
2866 | ToD&: ToTypedef, FromD: D, args&: Importer.getToContext(), args&: DC, args&: ToBeginLoc, args&: Loc, |
2867 | args: Name.getAsIdentifierInfo(), args&: ToTypeSourceInfo)) |
2868 | return ToTypedef; |
2869 | |
2870 | // Import the DeclContext and set it to the Typedef. |
2871 | if ((Err = ImportDeclContext(FromD: D, ToDC&: DC, ToLexicalDC&: LexicalDC))) |
2872 | return std::move(Err); |
2873 | ToTypedef->setDeclContext(DC); |
2874 | ToTypedef->setLexicalDeclContext(LexicalDC); |
2875 | // Add to the lookupTable because we could not do that in MapImported. |
2876 | Importer.AddToLookupTable(ToD: ToTypedef); |
2877 | |
2878 | ToTypedef->setAccess(D->getAccess()); |
2879 | |
2880 | // Templated declarations should not appear in DeclContext. |
2881 | TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(Val: D) : nullptr; |
2882 | if (!FromAlias || !FromAlias->getDescribedAliasTemplate()) |
2883 | LexicalDC->addDeclInternal(D: ToTypedef); |
2884 | |
2885 | return ToTypedef; |
2886 | } |
2887 | |
2888 | ExpectedDecl ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) { |
2889 | return VisitTypedefNameDecl(D, /*IsAlias=*/false); |
2890 | } |
2891 | |
2892 | ExpectedDecl ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) { |
2893 | return VisitTypedefNameDecl(D, /*IsAlias=*/true); |
2894 | } |
2895 | |
2896 | ExpectedDecl |
2897 | ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { |
2898 | // Import the major distinguishing characteristics of this typedef. |
2899 | DeclContext *DC, *LexicalDC; |
2900 | DeclarationName Name; |
2901 | SourceLocation Loc; |
2902 | NamedDecl *FoundD; |
2903 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD&: FoundD, Loc)) |
2904 | return std::move(Err); |
2905 | if (FoundD) |
2906 | return FoundD; |
2907 | |
2908 | // If this typedef is not in block scope, determine whether we've |
2909 | // seen a typedef with the same name (that we can merge with) or any |
2910 | // other entity by that name (which name lookup could conflict with). |
2911 | if (!DC->isFunctionOrMethod()) { |
2912 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
2913 | unsigned IDNS = Decl::IDNS_Ordinary; |
2914 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
2915 | for (auto *FoundDecl : FoundDecls) { |
2916 | if (!FoundDecl->isInIdentifierNamespace(NS: IDNS)) |
2917 | continue; |
2918 | if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(Val: FoundDecl)) { |
2919 | if (IsStructuralMatch(From: D, To: FoundAlias)) |
2920 | return Importer.MapImported(From: D, To: FoundAlias); |
2921 | ConflictingDecls.push_back(Elt: FoundDecl); |
2922 | } |
2923 | } |
2924 | |
2925 | if (!ConflictingDecls.empty()) { |
2926 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
2927 | Name, DC, IDNS, Decls: ConflictingDecls.data(), NumDecls: ConflictingDecls.size()); |
2928 | if (NameOrErr) |
2929 | Name = NameOrErr.get(); |
2930 | else |
2931 | return NameOrErr.takeError(); |
2932 | } |
2933 | } |
2934 | |
2935 | Error Err = Error::success(); |
2936 | auto ToTemplateParameters = importChecked(Err, From: D->getTemplateParameters()); |
2937 | auto ToTemplatedDecl = importChecked(Err, From: D->getTemplatedDecl()); |
2938 | if (Err) |
2939 | return std::move(Err); |
2940 | |
2941 | TypeAliasTemplateDecl *ToAlias; |
2942 | if (GetImportedOrCreateDecl(ToD&: ToAlias, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, |
2943 | args&: Name, args&: ToTemplateParameters, args&: ToTemplatedDecl)) |
2944 | return ToAlias; |
2945 | |
2946 | ToTemplatedDecl->setDescribedAliasTemplate(ToAlias); |
2947 | |
2948 | ToAlias->setAccess(D->getAccess()); |
2949 | ToAlias->setLexicalDeclContext(LexicalDC); |
2950 | LexicalDC->addDeclInternal(D: ToAlias); |
2951 | if (DC != Importer.getToContext().getTranslationUnitDecl()) |
2952 | updateLookupTableForTemplateParameters(Params&: *ToTemplateParameters); |
2953 | return ToAlias; |
2954 | } |
2955 | |
2956 | ExpectedDecl ASTNodeImporter::VisitLabelDecl(LabelDecl *D) { |
2957 | // Import the major distinguishing characteristics of this label. |
2958 | DeclContext *DC, *LexicalDC; |
2959 | DeclarationName Name; |
2960 | SourceLocation Loc; |
2961 | NamedDecl *ToD; |
2962 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
2963 | return std::move(Err); |
2964 | if (ToD) |
2965 | return ToD; |
2966 | |
2967 | assert(LexicalDC->isFunctionOrMethod()); |
2968 | |
2969 | LabelDecl *ToLabel; |
2970 | if (D->isGnuLocal()) { |
2971 | ExpectedSLoc BeginLocOrErr = import(From: D->getBeginLoc()); |
2972 | if (!BeginLocOrErr) |
2973 | return BeginLocOrErr.takeError(); |
2974 | if (GetImportedOrCreateDecl(ToD&: ToLabel, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, |
2975 | args: Name.getAsIdentifierInfo(), args&: *BeginLocOrErr)) |
2976 | return ToLabel; |
2977 | |
2978 | } else { |
2979 | if (GetImportedOrCreateDecl(ToD&: ToLabel, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, |
2980 | args: Name.getAsIdentifierInfo())) |
2981 | return ToLabel; |
2982 | |
2983 | } |
2984 | |
2985 | Expected<LabelStmt *> ToStmtOrErr = import(From: D->getStmt()); |
2986 | if (!ToStmtOrErr) |
2987 | return ToStmtOrErr.takeError(); |
2988 | |
2989 | ToLabel->setStmt(*ToStmtOrErr); |
2990 | ToLabel->setLexicalDeclContext(LexicalDC); |
2991 | LexicalDC->addDeclInternal(D: ToLabel); |
2992 | return ToLabel; |
2993 | } |
2994 | |
2995 | ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) { |
2996 | // Import the major distinguishing characteristics of this enum. |
2997 | DeclContext *DC, *LexicalDC; |
2998 | DeclarationName Name; |
2999 | SourceLocation Loc; |
3000 | NamedDecl *ToD; |
3001 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
3002 | return std::move(Err); |
3003 | if (ToD) |
3004 | return ToD; |
3005 | |
3006 | // Figure out what enum name we're looking for. |
3007 | unsigned IDNS = Decl::IDNS_Tag; |
3008 | DeclarationName SearchName = Name; |
3009 | if (!SearchName && D->getTypedefNameForAnonDecl()) { |
3010 | if (Error Err = importInto( |
3011 | To&: SearchName, From: D->getTypedefNameForAnonDecl()->getDeclName())) |
3012 | return std::move(Err); |
3013 | IDNS = Decl::IDNS_Ordinary; |
3014 | } else if (Importer.getToContext().getLangOpts().CPlusPlus) |
3015 | IDNS |= Decl::IDNS_Ordinary; |
3016 | |
3017 | // We may already have an enum of the same name; try to find and match it. |
3018 | EnumDecl *PrevDecl = nullptr; |
3019 | if (!DC->isFunctionOrMethod()) { |
3020 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
3021 | auto FoundDecls = |
3022 | Importer.findDeclsInToCtx(DC, Name: SearchName); |
3023 | for (auto *FoundDecl : FoundDecls) { |
3024 | if (!FoundDecl->isInIdentifierNamespace(NS: IDNS)) |
3025 | continue; |
3026 | |
3027 | if (auto *Typedef = dyn_cast<TypedefNameDecl>(Val: FoundDecl)) { |
3028 | if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) |
3029 | FoundDecl = Tag->getDecl(); |
3030 | } |
3031 | |
3032 | if (auto *FoundEnum = dyn_cast<EnumDecl>(Val: FoundDecl)) { |
3033 | if (!hasSameVisibilityContextAndLinkage(Found: FoundEnum, From: D)) |
3034 | continue; |
3035 | if (IsStructuralMatch(From: D, To: FoundEnum, Complain: !SearchName.isEmpty())) { |
3036 | EnumDecl *FoundDef = FoundEnum->getDefinition(); |
3037 | if (D->isThisDeclarationADefinition() && FoundDef) |
3038 | return Importer.MapImported(From: D, To: FoundDef); |
3039 | PrevDecl = FoundEnum->getMostRecentDecl(); |
3040 | break; |
3041 | } |
3042 | ConflictingDecls.push_back(Elt: FoundDecl); |
3043 | } |
3044 | } |
3045 | |
3046 | // In case of unnamed enums, we try to find an existing similar one, if none |
3047 | // was found, perform the import always. |
3048 | // Structural in-equivalence is not detected in this way here, but it may |
3049 | // be found when the parent decl is imported (if the enum is part of a |
3050 | // class). To make this totally exact a more difficult solution is needed. |
3051 | if (SearchName && !ConflictingDecls.empty()) { |
3052 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
3053 | Name: SearchName, DC, IDNS, Decls: ConflictingDecls.data(), |
3054 | NumDecls: ConflictingDecls.size()); |
3055 | if (NameOrErr) |
3056 | Name = NameOrErr.get(); |
3057 | else |
3058 | return NameOrErr.takeError(); |
3059 | } |
3060 | } |
3061 | |
3062 | Error Err = Error::success(); |
3063 | auto ToBeginLoc = importChecked(Err, From: D->getBeginLoc()); |
3064 | auto ToQualifierLoc = importChecked(Err, From: D->getQualifierLoc()); |
3065 | auto ToIntegerType = importChecked(Err, From: D->getIntegerType()); |
3066 | auto ToBraceRange = importChecked(Err, From: D->getBraceRange()); |
3067 | if (Err) |
3068 | return std::move(Err); |
3069 | |
3070 | // Create the enum declaration. |
3071 | EnumDecl *D2; |
3072 | if (GetImportedOrCreateDecl( |
3073 | ToD&: D2, FromD: D, args&: Importer.getToContext(), args&: DC, args&: ToBeginLoc, |
3074 | args&: Loc, args: Name.getAsIdentifierInfo(), args&: PrevDecl, args: D->isScoped(), |
3075 | args: D->isScopedUsingClassTag(), args: D->isFixed())) |
3076 | return D2; |
3077 | |
3078 | D2->setQualifierInfo(ToQualifierLoc); |
3079 | D2->setIntegerType(ToIntegerType); |
3080 | D2->setBraceRange(ToBraceRange); |
3081 | D2->setAccess(D->getAccess()); |
3082 | D2->setLexicalDeclContext(LexicalDC); |
3083 | addDeclToContexts(FromD: D, ToD: D2); |
3084 | |
3085 | if (MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo()) { |
3086 | TemplateSpecializationKind SK = MemberInfo->getTemplateSpecializationKind(); |
3087 | EnumDecl *FromInst = D->getInstantiatedFromMemberEnum(); |
3088 | if (Expected<EnumDecl *> ToInstOrErr = import(From: FromInst)) |
3089 | D2->setInstantiationOfMemberEnum(ED: *ToInstOrErr, TSK: SK); |
3090 | else |
3091 | return ToInstOrErr.takeError(); |
3092 | if (ExpectedSLoc POIOrErr = import(From: MemberInfo->getPointOfInstantiation())) |
3093 | D2->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr); |
3094 | else |
3095 | return POIOrErr.takeError(); |
3096 | } |
3097 | |
3098 | // Import the definition |
3099 | if (D->isCompleteDefinition()) |
3100 | if (Error Err = ImportDefinition(From: D, To: D2)) |
3101 | return std::move(Err); |
3102 | |
3103 | return D2; |
3104 | } |
3105 | |
3106 | ExpectedDecl ASTNodeImporter::VisitRecordDecl(RecordDecl *D) { |
3107 | bool IsFriendTemplate = false; |
3108 | if (auto *DCXX = dyn_cast<CXXRecordDecl>(Val: D)) { |
3109 | IsFriendTemplate = |
3110 | DCXX->getDescribedClassTemplate() && |
3111 | DCXX->getDescribedClassTemplate()->getFriendObjectKind() != |
3112 | Decl::FOK_None; |
3113 | } |
3114 | |
3115 | // Import the major distinguishing characteristics of this record. |
3116 | DeclContext *DC = nullptr, *LexicalDC = nullptr; |
3117 | DeclarationName Name; |
3118 | SourceLocation Loc; |
3119 | NamedDecl *ToD = nullptr; |
3120 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
3121 | return std::move(Err); |
3122 | if (ToD) |
3123 | return ToD; |
3124 | |
3125 | // Figure out what structure name we're looking for. |
3126 | unsigned IDNS = Decl::IDNS_Tag; |
3127 | DeclarationName SearchName = Name; |
3128 | if (!SearchName && D->getTypedefNameForAnonDecl()) { |
3129 | if (Error Err = importInto( |
3130 | To&: SearchName, From: D->getTypedefNameForAnonDecl()->getDeclName())) |
3131 | return std::move(Err); |
3132 | IDNS = Decl::IDNS_Ordinary; |
3133 | } else if (Importer.getToContext().getLangOpts().CPlusPlus) |
3134 | IDNS |= Decl::IDNS_Ordinary | Decl::IDNS_TagFriend; |
3135 | |
3136 | bool IsDependentContext = DC != LexicalDC ? LexicalDC->isDependentContext() |
3137 | : DC->isDependentContext(); |
3138 | bool DependentFriend = IsFriendTemplate && IsDependentContext; |
3139 | |
3140 | // We may already have a record of the same name; try to find and match it. |
3141 | RecordDecl *PrevDecl = nullptr; |
3142 | if (!DependentFriend && !DC->isFunctionOrMethod() && !D->isLambda()) { |
3143 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
3144 | auto FoundDecls = |
3145 | Importer.findDeclsInToCtx(DC, Name: SearchName); |
3146 | if (!FoundDecls.empty()) { |
3147 | // We're going to have to compare D against potentially conflicting Decls, |
3148 | // so complete it. |
3149 | if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition()) |
3150 | D->getASTContext().getExternalSource()->CompleteType(Tag: D); |
3151 | } |
3152 | |
3153 | for (auto *FoundDecl : FoundDecls) { |
3154 | if (!FoundDecl->isInIdentifierNamespace(NS: IDNS)) |
3155 | continue; |
3156 | |
3157 | Decl *Found = FoundDecl; |
3158 | if (auto *Typedef = dyn_cast<TypedefNameDecl>(Val: Found)) { |
3159 | if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) |
3160 | Found = Tag->getDecl(); |
3161 | } |
3162 | |
3163 | if (auto *FoundRecord = dyn_cast<RecordDecl>(Val: Found)) { |
3164 | // Do not emit false positive diagnostic in case of unnamed |
3165 | // struct/union and in case of anonymous structs. Would be false |
3166 | // because there may be several anonymous/unnamed structs in a class. |
3167 | // E.g. these are both valid: |
3168 | // struct A { // unnamed structs |
3169 | // struct { struct A *next; } entry0; |
3170 | // struct { struct A *next; } entry1; |
3171 | // }; |
3172 | // struct X { struct { int a; }; struct { int b; }; }; // anon structs |
3173 | if (!SearchName) |
3174 | if (!IsStructuralMatch(From: D, To: FoundRecord, Complain: false)) |
3175 | continue; |
3176 | |
3177 | if (!hasSameVisibilityContextAndLinkage(Found: FoundRecord, From: D)) |
3178 | continue; |
3179 | |
3180 | if (IsStructuralMatch(From: D, To: FoundRecord)) { |
3181 | RecordDecl *FoundDef = FoundRecord->getDefinition(); |
3182 | if (D->isThisDeclarationADefinition() && FoundDef) { |
3183 | // FIXME: Structural equivalence check should check for same |
3184 | // user-defined methods. |
3185 | Importer.MapImported(From: D, To: FoundDef); |
3186 | if (const auto *DCXX = dyn_cast<CXXRecordDecl>(Val: D)) { |
3187 | auto *FoundCXX = dyn_cast<CXXRecordDecl>(Val: FoundDef); |
3188 | assert(FoundCXX && "Record type mismatch" ); |
3189 | |
3190 | if (!Importer.isMinimalImport()) |
3191 | // FoundDef may not have every implicit method that D has |
3192 | // because implicit methods are created only if they are used. |
3193 | if (Error Err = ImportImplicitMethods(From: DCXX, To: FoundCXX)) |
3194 | return std::move(Err); |
3195 | } |
3196 | // FIXME: We can return FoundDef here. |
3197 | } |
3198 | PrevDecl = FoundRecord->getMostRecentDecl(); |
3199 | break; |
3200 | } |
3201 | ConflictingDecls.push_back(Elt: FoundDecl); |
3202 | } // kind is RecordDecl |
3203 | } // for |
3204 | |
3205 | if (!ConflictingDecls.empty() && SearchName) { |
3206 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
3207 | Name: SearchName, DC, IDNS, Decls: ConflictingDecls.data(), |
3208 | NumDecls: ConflictingDecls.size()); |
3209 | if (NameOrErr) |
3210 | Name = NameOrErr.get(); |
3211 | else |
3212 | return NameOrErr.takeError(); |
3213 | } |
3214 | } |
3215 | |
3216 | ExpectedSLoc BeginLocOrErr = import(From: D->getBeginLoc()); |
3217 | if (!BeginLocOrErr) |
3218 | return BeginLocOrErr.takeError(); |
3219 | |
3220 | // Create the record declaration. |
3221 | RecordDecl *D2 = nullptr; |
3222 | CXXRecordDecl *D2CXX = nullptr; |
3223 | if (auto *DCXX = dyn_cast<CXXRecordDecl>(Val: D)) { |
3224 | if (DCXX->isLambda()) { |
3225 | auto TInfoOrErr = import(From: DCXX->getLambdaTypeInfo()); |
3226 | if (!TInfoOrErr) |
3227 | return TInfoOrErr.takeError(); |
3228 | if (GetImportedOrCreateSpecialDecl( |
3229 | ToD&: D2CXX, CreateFun: CXXRecordDecl::CreateLambda, FromD: D, args&: Importer.getToContext(), |
3230 | args&: DC, args&: *TInfoOrErr, args&: Loc, args: DCXX->getLambdaDependencyKind(), |
3231 | args: DCXX->isGenericLambda(), args: DCXX->getLambdaCaptureDefault())) |
3232 | return D2CXX; |
3233 | CXXRecordDecl::LambdaNumbering Numbering = DCXX->getLambdaNumbering(); |
3234 | ExpectedDecl CDeclOrErr = import(From: Numbering.ContextDecl); |
3235 | if (!CDeclOrErr) |
3236 | return CDeclOrErr.takeError(); |
3237 | Numbering.ContextDecl = *CDeclOrErr; |
3238 | D2CXX->setLambdaNumbering(Numbering); |
3239 | } else if (DCXX->isInjectedClassName()) { |
3240 | // We have to be careful to do a similar dance to the one in |
3241 | // Sema::ActOnStartCXXMemberDeclarations |
3242 | const bool DelayTypeCreation = true; |
3243 | if (GetImportedOrCreateDecl( |
3244 | ToD&: D2CXX, FromD: D, args&: Importer.getToContext(), args: D->getTagKind(), args&: DC, |
3245 | args&: *BeginLocOrErr, args&: Loc, args: Name.getAsIdentifierInfo(), |
3246 | args: cast_or_null<CXXRecordDecl>(Val: PrevDecl), args: DelayTypeCreation)) |
3247 | return D2CXX; |
3248 | Importer.getToContext().getTypeDeclType( |
3249 | Decl: D2CXX, PrevDecl: dyn_cast<CXXRecordDecl>(Val: DC)); |
3250 | } else { |
3251 | if (GetImportedOrCreateDecl(ToD&: D2CXX, FromD: D, args&: Importer.getToContext(), |
3252 | args: D->getTagKind(), args&: DC, args&: *BeginLocOrErr, args&: Loc, |
3253 | args: Name.getAsIdentifierInfo(), |
3254 | args: cast_or_null<CXXRecordDecl>(Val: PrevDecl))) |
3255 | return D2CXX; |
3256 | } |
3257 | |
3258 | D2 = D2CXX; |
3259 | D2->setAccess(D->getAccess()); |
3260 | D2->setLexicalDeclContext(LexicalDC); |
3261 | addDeclToContexts(FromD: D, ToD: D2); |
3262 | |
3263 | if (ClassTemplateDecl *FromDescribed = |
3264 | DCXX->getDescribedClassTemplate()) { |
3265 | ClassTemplateDecl *ToDescribed; |
3266 | if (Error Err = importInto(To&: ToDescribed, From: FromDescribed)) |
3267 | return std::move(Err); |
3268 | D2CXX->setDescribedClassTemplate(ToDescribed); |
3269 | if (!DCXX->isInjectedClassName() && !IsFriendTemplate) { |
3270 | // In a record describing a template the type should be an |
3271 | // InjectedClassNameType (see Sema::CheckClassTemplate). Update the |
3272 | // previously set type to the correct value here (ToDescribed is not |
3273 | // available at record create). |
3274 | CXXRecordDecl *Injected = nullptr; |
3275 | for (NamedDecl *Found : D2CXX->noload_lookup(Name)) { |
3276 | auto *Record = dyn_cast<CXXRecordDecl>(Val: Found); |
3277 | if (Record && Record->isInjectedClassName()) { |
3278 | Injected = Record; |
3279 | break; |
3280 | } |
3281 | } |
3282 | // Create an injected type for the whole redecl chain. |
3283 | // The chain may contain an already existing injected type at the start, |
3284 | // if yes this should be reused. We must ensure that only one type |
3285 | // object exists for the injected type (including the injected record |
3286 | // declaration), ASTContext does not check it. |
3287 | SmallVector<Decl *, 2> Redecls = |
3288 | getCanonicalForwardRedeclChain(D: D2CXX); |
3289 | const Type *FrontTy = |
3290 | cast<CXXRecordDecl>(Val: Redecls.front())->getTypeForDecl(); |
3291 | QualType InjSpec; |
3292 | if (auto *InjTy = FrontTy->getAs<InjectedClassNameType>()) |
3293 | InjSpec = InjTy->getInjectedSpecializationType(); |
3294 | else |
3295 | InjSpec = ToDescribed->getInjectedClassNameSpecialization(); |
3296 | for (auto *R : Redecls) { |
3297 | auto *RI = cast<CXXRecordDecl>(Val: R); |
3298 | if (R != Redecls.front() || |
3299 | !isa<InjectedClassNameType>(Val: RI->getTypeForDecl())) |
3300 | RI->setTypeForDecl(nullptr); |
3301 | // This function tries to get the injected type from getTypeForDecl, |
3302 | // then from the previous declaration if possible. If not, it creates |
3303 | // a new type. |
3304 | Importer.getToContext().getInjectedClassNameType(Decl: RI, TST: InjSpec); |
3305 | } |
3306 | // Set the new type for the injected decl too. |
3307 | if (Injected) { |
3308 | Injected->setTypeForDecl(nullptr); |
3309 | // This function will copy the injected type from D2CXX into Injected. |
3310 | // The injected decl does not have a previous decl to copy from. |
3311 | Importer.getToContext().getTypeDeclType(Decl: Injected, PrevDecl: D2CXX); |
3312 | } |
3313 | } |
3314 | } else if (MemberSpecializationInfo *MemberInfo = |
3315 | DCXX->getMemberSpecializationInfo()) { |
3316 | TemplateSpecializationKind SK = |
3317 | MemberInfo->getTemplateSpecializationKind(); |
3318 | CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass(); |
3319 | |
3320 | if (Expected<CXXRecordDecl *> ToInstOrErr = import(From: FromInst)) |
3321 | D2CXX->setInstantiationOfMemberClass(RD: *ToInstOrErr, TSK: SK); |
3322 | else |
3323 | return ToInstOrErr.takeError(); |
3324 | |
3325 | if (ExpectedSLoc POIOrErr = |
3326 | import(From: MemberInfo->getPointOfInstantiation())) |
3327 | D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation( |
3328 | *POIOrErr); |
3329 | else |
3330 | return POIOrErr.takeError(); |
3331 | } |
3332 | |
3333 | } else { |
3334 | if (GetImportedOrCreateDecl(ToD&: D2, FromD: D, args&: Importer.getToContext(), |
3335 | args: D->getTagKind(), args&: DC, args&: *BeginLocOrErr, args&: Loc, |
3336 | args: Name.getAsIdentifierInfo(), args&: PrevDecl)) |
3337 | return D2; |
3338 | D2->setLexicalDeclContext(LexicalDC); |
3339 | addDeclToContexts(FromD: D, ToD: D2); |
3340 | } |
3341 | |
3342 | if (auto BraceRangeOrErr = import(From: D->getBraceRange())) |
3343 | D2->setBraceRange(*BraceRangeOrErr); |
3344 | else |
3345 | return BraceRangeOrErr.takeError(); |
3346 | if (auto QualifierLocOrErr = import(From: D->getQualifierLoc())) |
3347 | D2->setQualifierInfo(*QualifierLocOrErr); |
3348 | else |
3349 | return QualifierLocOrErr.takeError(); |
3350 | |
3351 | if (D->isAnonymousStructOrUnion()) |
3352 | D2->setAnonymousStructOrUnion(true); |
3353 | |
3354 | if (D->isCompleteDefinition()) |
3355 | if (Error Err = ImportDefinition(From: D, To: D2, Kind: IDK_Default)) |
3356 | return std::move(Err); |
3357 | |
3358 | return D2; |
3359 | } |
3360 | |
3361 | ExpectedDecl ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) { |
3362 | // Import the major distinguishing characteristics of this enumerator. |
3363 | DeclContext *DC, *LexicalDC; |
3364 | DeclarationName Name; |
3365 | SourceLocation Loc; |
3366 | NamedDecl *ToD; |
3367 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
3368 | return std::move(Err); |
3369 | if (ToD) |
3370 | return ToD; |
3371 | |
3372 | // Determine whether there are any other declarations with the same name and |
3373 | // in the same context. |
3374 | if (!LexicalDC->isFunctionOrMethod()) { |
3375 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
3376 | unsigned IDNS = Decl::IDNS_Ordinary; |
3377 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
3378 | for (auto *FoundDecl : FoundDecls) { |
3379 | if (!FoundDecl->isInIdentifierNamespace(NS: IDNS)) |
3380 | continue; |
3381 | |
3382 | if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(Val: FoundDecl)) { |
3383 | if (IsStructuralMatch(From: D, To: FoundEnumConstant)) |
3384 | return Importer.MapImported(From: D, To: FoundEnumConstant); |
3385 | ConflictingDecls.push_back(Elt: FoundDecl); |
3386 | } |
3387 | } |
3388 | |
3389 | if (!ConflictingDecls.empty()) { |
3390 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
3391 | Name, DC, IDNS, Decls: ConflictingDecls.data(), NumDecls: ConflictingDecls.size()); |
3392 | if (NameOrErr) |
3393 | Name = NameOrErr.get(); |
3394 | else |
3395 | return NameOrErr.takeError(); |
3396 | } |
3397 | } |
3398 | |
3399 | ExpectedType TypeOrErr = import(From: D->getType()); |
3400 | if (!TypeOrErr) |
3401 | return TypeOrErr.takeError(); |
3402 | |
3403 | ExpectedExpr InitOrErr = import(From: D->getInitExpr()); |
3404 | if (!InitOrErr) |
3405 | return InitOrErr.takeError(); |
3406 | |
3407 | EnumConstantDecl *ToEnumerator; |
3408 | if (GetImportedOrCreateDecl( |
3409 | ToD&: ToEnumerator, FromD: D, args&: Importer.getToContext(), args: cast<EnumDecl>(Val: DC), args&: Loc, |
3410 | args: Name.getAsIdentifierInfo(), args&: *TypeOrErr, args&: *InitOrErr, args: D->getInitVal())) |
3411 | return ToEnumerator; |
3412 | |
3413 | ToEnumerator->setAccess(D->getAccess()); |
3414 | ToEnumerator->setLexicalDeclContext(LexicalDC); |
3415 | LexicalDC->addDeclInternal(D: ToEnumerator); |
3416 | return ToEnumerator; |
3417 | } |
3418 | |
3419 | template <typename DeclTy> |
3420 | Error ASTNodeImporter::ImportTemplateParameterLists(const DeclTy *FromD, |
3421 | DeclTy *ToD) { |
3422 | unsigned int Num = FromD->getNumTemplateParameterLists(); |
3423 | if (Num == 0) |
3424 | return Error::success(); |
3425 | SmallVector<TemplateParameterList *, 2> ToTPLists(Num); |
3426 | for (unsigned int I = 0; I < Num; ++I) |
3427 | if (Expected<TemplateParameterList *> ToTPListOrErr = |
3428 | import(FromD->getTemplateParameterList(I))) |
3429 | ToTPLists[I] = *ToTPListOrErr; |
3430 | else |
3431 | return ToTPListOrErr.takeError(); |
3432 | ToD->setTemplateParameterListsInfo(Importer.ToContext, ToTPLists); |
3433 | return Error::success(); |
3434 | } |
3435 | |
3436 | Error ASTNodeImporter::ImportTemplateInformation( |
3437 | FunctionDecl *FromFD, FunctionDecl *ToFD) { |
3438 | switch (FromFD->getTemplatedKind()) { |
3439 | case FunctionDecl::TK_NonTemplate: |
3440 | case FunctionDecl::TK_FunctionTemplate: |
3441 | return Error::success(); |
3442 | |
3443 | case FunctionDecl::TK_DependentNonTemplate: |
3444 | if (Expected<FunctionDecl *> InstFDOrErr = |
3445 | import(From: FromFD->getInstantiatedFromDecl())) |
3446 | ToFD->setInstantiatedFromDecl(*InstFDOrErr); |
3447 | return Error::success(); |
3448 | case FunctionDecl::TK_MemberSpecialization: { |
3449 | TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind(); |
3450 | |
3451 | if (Expected<FunctionDecl *> InstFDOrErr = |
3452 | import(From: FromFD->getInstantiatedFromMemberFunction())) |
3453 | ToFD->setInstantiationOfMemberFunction(FD: *InstFDOrErr, TSK); |
3454 | else |
3455 | return InstFDOrErr.takeError(); |
3456 | |
3457 | if (ExpectedSLoc POIOrErr = import( |
3458 | From: FromFD->getMemberSpecializationInfo()->getPointOfInstantiation())) |
3459 | ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr); |
3460 | else |
3461 | return POIOrErr.takeError(); |
3462 | |
3463 | return Error::success(); |
3464 | } |
3465 | |
3466 | case FunctionDecl::TK_FunctionTemplateSpecialization: { |
3467 | auto FunctionAndArgsOrErr = |
3468 | ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD); |
3469 | if (!FunctionAndArgsOrErr) |
3470 | return FunctionAndArgsOrErr.takeError(); |
3471 | |
3472 | TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy( |
3473 | Context&: Importer.getToContext(), Args: std::get<1>(t&: *FunctionAndArgsOrErr)); |
3474 | |
3475 | auto *FTSInfo = FromFD->getTemplateSpecializationInfo(); |
3476 | TemplateArgumentListInfo ToTAInfo; |
3477 | const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten; |
3478 | if (FromTAArgsAsWritten) |
3479 | if (Error Err = ImportTemplateArgumentListInfo( |
3480 | From: *FromTAArgsAsWritten, Result&: ToTAInfo)) |
3481 | return Err; |
3482 | |
3483 | ExpectedSLoc POIOrErr = import(From: FTSInfo->getPointOfInstantiation()); |
3484 | if (!POIOrErr) |
3485 | return POIOrErr.takeError(); |
3486 | |
3487 | if (Error Err = ImportTemplateParameterLists(FromD: FromFD, ToD: ToFD)) |
3488 | return Err; |
3489 | |
3490 | TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind(); |
3491 | ToFD->setFunctionTemplateSpecialization( |
3492 | Template: std::get<0>(t&: *FunctionAndArgsOrErr), TemplateArgs: ToTAList, /* InsertPos= */ nullptr, |
3493 | TSK, TemplateArgsAsWritten: FromTAArgsAsWritten ? &ToTAInfo : nullptr, PointOfInstantiation: *POIOrErr); |
3494 | return Error::success(); |
3495 | } |
3496 | |
3497 | case FunctionDecl::TK_DependentFunctionTemplateSpecialization: { |
3498 | auto *FromInfo = FromFD->getDependentSpecializationInfo(); |
3499 | UnresolvedSet<8> Candidates; |
3500 | for (FunctionTemplateDecl *FTD : FromInfo->getCandidates()) { |
3501 | if (Expected<FunctionTemplateDecl *> ToFTDOrErr = import(From: FTD)) |
3502 | Candidates.addDecl(D: *ToFTDOrErr); |
3503 | else |
3504 | return ToFTDOrErr.takeError(); |
3505 | } |
3506 | |
3507 | // Import TemplateArgumentListInfo. |
3508 | TemplateArgumentListInfo ToTAInfo; |
3509 | const auto *FromTAArgsAsWritten = FromInfo->TemplateArgumentsAsWritten; |
3510 | if (FromTAArgsAsWritten) |
3511 | if (Error Err = |
3512 | ImportTemplateArgumentListInfo(From: *FromTAArgsAsWritten, Result&: ToTAInfo)) |
3513 | return Err; |
3514 | |
3515 | ToFD->setDependentTemplateSpecialization( |
3516 | Context&: Importer.getToContext(), Templates: Candidates, |
3517 | TemplateArgs: FromTAArgsAsWritten ? &ToTAInfo : nullptr); |
3518 | return Error::success(); |
3519 | } |
3520 | } |
3521 | llvm_unreachable("All cases should be covered!" ); |
3522 | } |
3523 | |
3524 | Expected<FunctionDecl *> |
3525 | ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) { |
3526 | auto FunctionAndArgsOrErr = |
3527 | ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD); |
3528 | if (!FunctionAndArgsOrErr) |
3529 | return FunctionAndArgsOrErr.takeError(); |
3530 | |
3531 | FunctionTemplateDecl *Template; |
3532 | TemplateArgsTy ToTemplArgs; |
3533 | std::tie(args&: Template, args&: ToTemplArgs) = *FunctionAndArgsOrErr; |
3534 | void *InsertPos = nullptr; |
3535 | auto *FoundSpec = Template->findSpecialization(Args: ToTemplArgs, InsertPos); |
3536 | return FoundSpec; |
3537 | } |
3538 | |
3539 | Error ASTNodeImporter::ImportFunctionDeclBody(FunctionDecl *FromFD, |
3540 | FunctionDecl *ToFD) { |
3541 | if (Stmt *FromBody = FromFD->getBody()) { |
3542 | if (ExpectedStmt ToBodyOrErr = import(From: FromBody)) |
3543 | ToFD->setBody(*ToBodyOrErr); |
3544 | else |
3545 | return ToBodyOrErr.takeError(); |
3546 | } |
3547 | return Error::success(); |
3548 | } |
3549 | |
3550 | // Returns true if the given D has a DeclContext up to the TranslationUnitDecl |
3551 | // which is equal to the given DC, or D is equal to DC. |
3552 | static bool isAncestorDeclContextOf(const DeclContext *DC, const Decl *D) { |
3553 | const DeclContext *DCi = dyn_cast<DeclContext>(Val: D); |
3554 | if (!DCi) |
3555 | DCi = D->getDeclContext(); |
3556 | assert(DCi && "Declaration should have a context" ); |
3557 | while (DCi != D->getTranslationUnitDecl()) { |
3558 | if (DCi == DC) |
3559 | return true; |
3560 | DCi = DCi->getParent(); |
3561 | } |
3562 | return false; |
3563 | } |
3564 | |
3565 | // Check if there is a declaration that has 'DC' as parent context and is |
3566 | // referenced from statement 'S' or one of its children. The search is done in |
3567 | // BFS order through children of 'S'. |
3568 | static bool isAncestorDeclContextOf(const DeclContext *DC, const Stmt *S) { |
3569 | SmallVector<const Stmt *> ToProcess; |
3570 | ToProcess.push_back(Elt: S); |
3571 | while (!ToProcess.empty()) { |
3572 | const Stmt *CurrentS = ToProcess.pop_back_val(); |
3573 | ToProcess.append(in_start: CurrentS->child_begin(), in_end: CurrentS->child_end()); |
3574 | if (const auto *DeclRef = dyn_cast<DeclRefExpr>(Val: CurrentS)) { |
3575 | if (const Decl *D = DeclRef->getDecl()) |
3576 | if (isAncestorDeclContextOf(DC, D)) |
3577 | return true; |
3578 | } else if (const auto *E = |
3579 | dyn_cast_or_null<SubstNonTypeTemplateParmExpr>(Val: CurrentS)) { |
3580 | if (const Decl *D = E->getAssociatedDecl()) |
3581 | if (isAncestorDeclContextOf(DC, D)) |
3582 | return true; |
3583 | } |
3584 | } |
3585 | return false; |
3586 | } |
3587 | |
3588 | namespace { |
3589 | /// Check if a type has any reference to a declaration that is inside the body |
3590 | /// of a function. |
3591 | /// The \c CheckType(QualType) function should be used to determine |
3592 | /// this property. |
3593 | /// |
3594 | /// The type visitor visits one type object only (not recursive). |
3595 | /// To find all referenced declarations we must discover all type objects until |
3596 | /// the canonical type is reached (walk over typedef and similar objects). This |
3597 | /// is done by loop over all "sugar" type objects. For every such type we must |
3598 | /// check all declarations that are referenced from it. For this check the |
3599 | /// visitor is used. In the visit functions all referenced declarations except |
3600 | /// the one that follows in the sugar chain (if any) must be checked. For this |
3601 | /// check the same visitor is re-used (it has no state-dependent data). |
3602 | /// |
3603 | /// The visit functions have 3 possible return values: |
3604 | /// - True, found a declaration inside \c ParentDC. |
3605 | /// - False, found declarations only outside \c ParentDC and it is not possible |
3606 | /// to find more declarations (the "sugar" chain does not continue). |
3607 | /// - Empty optional value, found no declarations or only outside \c ParentDC, |
3608 | /// but it is possible to find more declarations in the type "sugar" chain. |
3609 | /// The loop over the "sugar" types can be implemented by using type visit |
3610 | /// functions only (call \c CheckType with the desugared type). With the current |
3611 | /// solution no visit function is needed if the type has only a desugared type |
3612 | /// as data. |
3613 | class IsTypeDeclaredInsideVisitor |
3614 | : public TypeVisitor<IsTypeDeclaredInsideVisitor, std::optional<bool>> { |
3615 | public: |
3616 | IsTypeDeclaredInsideVisitor(const FunctionDecl *ParentDC) |
3617 | : ParentDC(ParentDC) {} |
3618 | |
3619 | bool CheckType(QualType T) { |
3620 | // Check the chain of "sugar" types. |
3621 | // The "sugar" types are typedef or similar types that have the same |
3622 | // canonical type. |
3623 | if (std::optional<bool> Res = Visit(T: T.getTypePtr())) |
3624 | return *Res; |
3625 | QualType DsT = |
3626 | T.getSingleStepDesugaredType(Context: ParentDC->getParentASTContext()); |
3627 | while (DsT != T) { |
3628 | if (std::optional<bool> Res = Visit(T: DsT.getTypePtr())) |
3629 | return *Res; |
3630 | T = DsT; |
3631 | DsT = T.getSingleStepDesugaredType(Context: ParentDC->getParentASTContext()); |
3632 | } |
3633 | return false; |
3634 | } |
3635 | |
3636 | std::optional<bool> VisitTagType(const TagType *T) { |
3637 | if (auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(Val: T->getDecl())) |
3638 | for (const auto &Arg : Spec->getTemplateArgs().asArray()) |
3639 | if (checkTemplateArgument(Arg)) |
3640 | return true; |
3641 | return isAncestorDeclContextOf(DC: ParentDC, D: T->getDecl()); |
3642 | } |
3643 | |
3644 | std::optional<bool> VisitPointerType(const PointerType *T) { |
3645 | return CheckType(T: T->getPointeeType()); |
3646 | } |
3647 | |
3648 | std::optional<bool> VisitReferenceType(const ReferenceType *T) { |
3649 | return CheckType(T: T->getPointeeTypeAsWritten()); |
3650 | } |
3651 | |
3652 | std::optional<bool> VisitTypedefType(const TypedefType *T) { |
3653 | const TypedefNameDecl *TD = T->getDecl(); |
3654 | assert(TD); |
3655 | return isAncestorDeclContextOf(DC: ParentDC, D: TD); |
3656 | } |
3657 | |
3658 | std::optional<bool> VisitUsingType(const UsingType *T) { |
3659 | if (T->getFoundDecl() && |
3660 | isAncestorDeclContextOf(DC: ParentDC, D: T->getFoundDecl())) |
3661 | return true; |
3662 | |
3663 | return {}; |
3664 | } |
3665 | |
3666 | std::optional<bool> |
3667 | VisitTemplateSpecializationType(const TemplateSpecializationType *T) { |
3668 | for (const auto &Arg : T->template_arguments()) |
3669 | if (checkTemplateArgument(Arg)) |
3670 | return true; |
3671 | // This type is a "sugar" to a record type, it can have a desugared type. |
3672 | return {}; |
3673 | } |
3674 | |
3675 | std::optional<bool> VisitUnaryTransformType(const UnaryTransformType *T) { |
3676 | return CheckType(T: T->getBaseType()); |
3677 | } |
3678 | |
3679 | std::optional<bool> |
3680 | VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
3681 | // The "associated declaration" can be the same as ParentDC. |
3682 | if (isAncestorDeclContextOf(DC: ParentDC, D: T->getAssociatedDecl())) |
3683 | return true; |
3684 | return {}; |
3685 | } |
3686 | |
3687 | std::optional<bool> VisitConstantArrayType(const ConstantArrayType *T) { |
3688 | if (T->getSizeExpr() && isAncestorDeclContextOf(DC: ParentDC, S: T->getSizeExpr())) |
3689 | return true; |
3690 | |
3691 | return CheckType(T: T->getElementType()); |
3692 | } |
3693 | |
3694 | std::optional<bool> VisitVariableArrayType(const VariableArrayType *T) { |
3695 | llvm_unreachable( |
3696 | "Variable array should not occur in deduced return type of a function" ); |
3697 | } |
3698 | |
3699 | std::optional<bool> VisitIncompleteArrayType(const IncompleteArrayType *T) { |
3700 | llvm_unreachable("Incomplete array should not occur in deduced return type " |
3701 | "of a function" ); |
3702 | } |
3703 | |
3704 | std::optional<bool> VisitDependentArrayType(const IncompleteArrayType *T) { |
3705 | llvm_unreachable("Dependent array should not occur in deduced return type " |
3706 | "of a function" ); |
3707 | } |
3708 | |
3709 | private: |
3710 | const DeclContext *const ParentDC; |
3711 | |
3712 | bool checkTemplateArgument(const TemplateArgument &Arg) { |
3713 | switch (Arg.getKind()) { |
3714 | case TemplateArgument::Null: |
3715 | return false; |
3716 | case TemplateArgument::Integral: |
3717 | return CheckType(T: Arg.getIntegralType()); |
3718 | case TemplateArgument::Type: |
3719 | return CheckType(T: Arg.getAsType()); |
3720 | case TemplateArgument::Expression: |
3721 | return isAncestorDeclContextOf(DC: ParentDC, S: Arg.getAsExpr()); |
3722 | case TemplateArgument::Declaration: |
3723 | // FIXME: The declaration in this case is not allowed to be in a function? |
3724 | return isAncestorDeclContextOf(DC: ParentDC, D: Arg.getAsDecl()); |
3725 | case TemplateArgument::NullPtr: |
3726 | // FIXME: The type is not allowed to be in the function? |
3727 | return CheckType(T: Arg.getNullPtrType()); |
3728 | case TemplateArgument::StructuralValue: |
3729 | return CheckType(T: Arg.getStructuralValueType()); |
3730 | case TemplateArgument::Pack: |
3731 | for (const auto &PackArg : Arg.getPackAsArray()) |
3732 | if (checkTemplateArgument(Arg: PackArg)) |
3733 | return true; |
3734 | return false; |
3735 | case TemplateArgument::Template: |
3736 | // Templates can not be defined locally in functions. |
3737 | // A template passed as argument can be not in ParentDC. |
3738 | return false; |
3739 | case TemplateArgument::TemplateExpansion: |
3740 | // Templates can not be defined locally in functions. |
3741 | // A template passed as argument can be not in ParentDC. |
3742 | return false; |
3743 | } |
3744 | llvm_unreachable("Unknown TemplateArgument::ArgKind enum" ); |
3745 | }; |
3746 | }; |
3747 | } // namespace |
3748 | |
3749 | /// This function checks if the given function has a return type that contains |
3750 | /// a reference (in any way) to a declaration inside the same function. |
3751 | bool ASTNodeImporter::hasReturnTypeDeclaredInside(FunctionDecl *D) { |
3752 | QualType FromTy = D->getType(); |
3753 | const auto *FromFPT = FromTy->getAs<FunctionProtoType>(); |
3754 | assert(FromFPT && "Must be called on FunctionProtoType" ); |
3755 | |
3756 | auto IsCXX11Lambda = [&]() { |
3757 | if (Importer.FromContext.getLangOpts().CPlusPlus14) // C++14 or later |
3758 | return false; |
3759 | |
3760 | return isLambdaMethod(DC: D); |
3761 | }; |
3762 | |
3763 | QualType RetT = FromFPT->getReturnType(); |
3764 | if (isa<AutoType>(Val: RetT.getTypePtr()) || IsCXX11Lambda()) { |
3765 | FunctionDecl *Def = D->getDefinition(); |
3766 | IsTypeDeclaredInsideVisitor Visitor(Def ? Def : D); |
3767 | return Visitor.CheckType(T: RetT); |
3768 | } |
3769 | |
3770 | return false; |
3771 | } |
3772 | |
3773 | ExplicitSpecifier |
3774 | ASTNodeImporter::importExplicitSpecifier(Error &Err, ExplicitSpecifier ESpec) { |
3775 | Expr *ExplicitExpr = ESpec.getExpr(); |
3776 | if (ExplicitExpr) |
3777 | ExplicitExpr = importChecked(Err, From: ESpec.getExpr()); |
3778 | return ExplicitSpecifier(ExplicitExpr, ESpec.getKind()); |
3779 | } |
3780 | |
3781 | ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) { |
3782 | |
3783 | SmallVector<Decl *, 2> Redecls = getCanonicalForwardRedeclChain(D); |
3784 | auto RedeclIt = Redecls.begin(); |
3785 | // Import the first part of the decl chain. I.e. import all previous |
3786 | // declarations starting from the canonical decl. |
3787 | for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) { |
3788 | ExpectedDecl ToRedeclOrErr = import(From: *RedeclIt); |
3789 | if (!ToRedeclOrErr) |
3790 | return ToRedeclOrErr.takeError(); |
3791 | } |
3792 | assert(*RedeclIt == D); |
3793 | |
3794 | // Import the major distinguishing characteristics of this function. |
3795 | DeclContext *DC, *LexicalDC; |
3796 | DeclarationName Name; |
3797 | SourceLocation Loc; |
3798 | NamedDecl *ToD; |
3799 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
3800 | return std::move(Err); |
3801 | if (ToD) |
3802 | return ToD; |
3803 | |
3804 | FunctionDecl *FoundByLookup = nullptr; |
3805 | FunctionTemplateDecl *FromFT = D->getDescribedFunctionTemplate(); |
3806 | |
3807 | // If this is a function template specialization, then try to find the same |
3808 | // existing specialization in the "to" context. The lookup below will not |
3809 | // find any specialization, but would find the primary template; thus, we |
3810 | // have to skip normal lookup in case of specializations. |
3811 | // FIXME handle member function templates (TK_MemberSpecialization) similarly? |
3812 | if (D->getTemplatedKind() == |
3813 | FunctionDecl::TK_FunctionTemplateSpecialization) { |
3814 | auto FoundFunctionOrErr = FindFunctionTemplateSpecialization(FromFD: D); |
3815 | if (!FoundFunctionOrErr) |
3816 | return FoundFunctionOrErr.takeError(); |
3817 | if (FunctionDecl *FoundFunction = *FoundFunctionOrErr) { |
3818 | if (Decl *Def = FindAndMapDefinition(D, FoundFunction)) |
3819 | return Def; |
3820 | FoundByLookup = FoundFunction; |
3821 | } |
3822 | } |
3823 | // Try to find a function in our own ("to") context with the same name, same |
3824 | // type, and in the same context as the function we're importing. |
3825 | else if (!LexicalDC->isFunctionOrMethod()) { |
3826 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
3827 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend; |
3828 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
3829 | for (auto *FoundDecl : FoundDecls) { |
3830 | if (!FoundDecl->isInIdentifierNamespace(NS: IDNS)) |
3831 | continue; |
3832 | |
3833 | if (auto *FoundFunction = dyn_cast<FunctionDecl>(Val: FoundDecl)) { |
3834 | if (!hasSameVisibilityContextAndLinkage(Found: FoundFunction, From: D)) |
3835 | continue; |
3836 | |
3837 | if (IsStructuralMatch(From: D, To: FoundFunction)) { |
3838 | if (Decl *Def = FindAndMapDefinition(D, FoundFunction)) |
3839 | return Def; |
3840 | FoundByLookup = FoundFunction; |
3841 | break; |
3842 | } |
3843 | // FIXME: Check for overloading more carefully, e.g., by boosting |
3844 | // Sema::IsOverload out to the AST library. |
3845 | |
3846 | // Function overloading is okay in C++. |
3847 | if (Importer.getToContext().getLangOpts().CPlusPlus) |
3848 | continue; |
3849 | |
3850 | // Complain about inconsistent function types. |
3851 | Importer.ToDiag(Loc, DiagID: diag::warn_odr_function_type_inconsistent) |
3852 | << Name << D->getType() << FoundFunction->getType(); |
3853 | Importer.ToDiag(Loc: FoundFunction->getLocation(), DiagID: diag::note_odr_value_here) |
3854 | << FoundFunction->getType(); |
3855 | ConflictingDecls.push_back(Elt: FoundDecl); |
3856 | } |
3857 | } |
3858 | |
3859 | if (!ConflictingDecls.empty()) { |
3860 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
3861 | Name, DC, IDNS, Decls: ConflictingDecls.data(), NumDecls: ConflictingDecls.size()); |
3862 | if (NameOrErr) |
3863 | Name = NameOrErr.get(); |
3864 | else |
3865 | return NameOrErr.takeError(); |
3866 | } |
3867 | } |
3868 | |
3869 | // We do not allow more than one in-class declaration of a function. This is |
3870 | // because AST clients like VTableBuilder asserts on this. VTableBuilder |
3871 | // assumes there is only one in-class declaration. Building a redecl |
3872 | // chain would result in more than one in-class declaration for |
3873 | // overrides (even if they are part of the same redecl chain inside the |
3874 | // derived class.) |
3875 | if (FoundByLookup) { |
3876 | if (isa<CXXMethodDecl>(Val: FoundByLookup)) { |
3877 | if (D->getLexicalDeclContext() == D->getDeclContext()) { |
3878 | if (!D->doesThisDeclarationHaveABody()) { |
3879 | if (FunctionTemplateDecl *DescribedD = |
3880 | D->getDescribedFunctionTemplate()) { |
3881 | // Handle a "templated" function together with its described |
3882 | // template. This avoids need for a similar check at import of the |
3883 | // described template. |
3884 | assert(FoundByLookup->getDescribedFunctionTemplate() && |
3885 | "Templated function mapped to non-templated?" ); |
3886 | Importer.MapImported(From: DescribedD, |
3887 | To: FoundByLookup->getDescribedFunctionTemplate()); |
3888 | } |
3889 | return Importer.MapImported(From: D, To: FoundByLookup); |
3890 | } else { |
3891 | // Let's continue and build up the redecl chain in this case. |
3892 | // FIXME Merge the functions into one decl. |
3893 | } |
3894 | } |
3895 | } |
3896 | } |
3897 | |
3898 | DeclarationNameInfo NameInfo(Name, Loc); |
3899 | // Import additional name location/type info. |
3900 | if (Error Err = ImportDeclarationNameLoc(From: D->getNameInfo(), To&: NameInfo)) |
3901 | return std::move(Err); |
3902 | |
3903 | QualType FromTy = D->getType(); |
3904 | TypeSourceInfo *FromTSI = D->getTypeSourceInfo(); |
3905 | // Set to true if we do not import the type of the function as is. There are |
3906 | // cases when the original type would result in an infinite recursion during |
3907 | // the import. To avoid an infinite recursion when importing, we create the |
3908 | // FunctionDecl with a simplified function type and update it only after the |
3909 | // relevant AST nodes are already imported. |
3910 | // The type is related to TypeSourceInfo (it references the type), so we must |
3911 | // do the same with TypeSourceInfo. |
3912 | bool UsedDifferentProtoType = false; |
3913 | if (const auto *FromFPT = FromTy->getAs<FunctionProtoType>()) { |
3914 | QualType FromReturnTy = FromFPT->getReturnType(); |
3915 | // Functions with auto return type may define a struct inside their body |
3916 | // and the return type could refer to that struct. |
3917 | // E.g.: auto foo() { struct X{}; return X(); } |
3918 | // To avoid an infinite recursion when importing, create the FunctionDecl |
3919 | // with a simplified return type. |
3920 | if (hasReturnTypeDeclaredInside(D)) { |
3921 | FromReturnTy = Importer.getFromContext().VoidTy; |
3922 | UsedDifferentProtoType = true; |
3923 | } |
3924 | FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo(); |
3925 | // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the |
3926 | // FunctionDecl that we are importing the FunctionProtoType for. |
3927 | // To avoid an infinite recursion when importing, create the FunctionDecl |
3928 | // with a simplified function type. |
3929 | if (FromEPI.ExceptionSpec.SourceDecl || |
3930 | FromEPI.ExceptionSpec.SourceTemplate || |
3931 | FromEPI.ExceptionSpec.NoexceptExpr) { |
3932 | FunctionProtoType::ExtProtoInfo DefaultEPI; |
3933 | FromEPI = DefaultEPI; |
3934 | UsedDifferentProtoType = true; |
3935 | } |
3936 | FromTy = Importer.getFromContext().getFunctionType( |
3937 | ResultTy: FromReturnTy, Args: FromFPT->getParamTypes(), EPI: FromEPI); |
3938 | FromTSI = Importer.getFromContext().getTrivialTypeSourceInfo( |
3939 | T: FromTy, Loc: D->getBeginLoc()); |
3940 | } |
3941 | |
3942 | Error Err = Error::success(); |
3943 | auto T = importChecked(Err, From: FromTy); |
3944 | auto TInfo = importChecked(Err, From: FromTSI); |
3945 | auto ToInnerLocStart = importChecked(Err, From: D->getInnerLocStart()); |
3946 | auto ToEndLoc = importChecked(Err, From: D->getEndLoc()); |
3947 | auto ToDefaultLoc = importChecked(Err, From: D->getDefaultLoc()); |
3948 | auto ToQualifierLoc = importChecked(Err, From: D->getQualifierLoc()); |
3949 | AssociatedConstraint TrailingRequiresClause = D->getTrailingRequiresClause(); |
3950 | TrailingRequiresClause.ConstraintExpr = |
3951 | importChecked(Err, From: TrailingRequiresClause.ConstraintExpr); |
3952 | if (Err) |
3953 | return std::move(Err); |
3954 | |
3955 | // Import the function parameters. |
3956 | SmallVector<ParmVarDecl *, 8> Parameters; |
3957 | for (auto *P : D->parameters()) { |
3958 | if (Expected<ParmVarDecl *> ToPOrErr = import(From: P)) |
3959 | Parameters.push_back(Elt: *ToPOrErr); |
3960 | else |
3961 | return ToPOrErr.takeError(); |
3962 | } |
3963 | |
3964 | // Create the imported function. |
3965 | FunctionDecl *ToFunction = nullptr; |
3966 | if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(Val: D)) { |
3967 | ExplicitSpecifier ESpec = |
3968 | importExplicitSpecifier(Err, ESpec: FromConstructor->getExplicitSpecifier()); |
3969 | if (Err) |
3970 | return std::move(Err); |
3971 | auto ToInheritedConstructor = InheritedConstructor(); |
3972 | if (FromConstructor->isInheritingConstructor()) { |
3973 | Expected<InheritedConstructor> ImportedInheritedCtor = |
3974 | import(From: FromConstructor->getInheritedConstructor()); |
3975 | if (!ImportedInheritedCtor) |
3976 | return ImportedInheritedCtor.takeError(); |
3977 | ToInheritedConstructor = *ImportedInheritedCtor; |
3978 | } |
3979 | if (GetImportedOrCreateDecl<CXXConstructorDecl>( |
3980 | ToD&: ToFunction, FromD: D, args&: Importer.getToContext(), args: cast<CXXRecordDecl>(Val: DC), |
3981 | args&: ToInnerLocStart, args&: NameInfo, args&: T, args&: TInfo, args&: ESpec, args: D->UsesFPIntrin(), |
3982 | args: D->isInlineSpecified(), args: D->isImplicit(), args: D->getConstexprKind(), |
3983 | args&: ToInheritedConstructor, args&: TrailingRequiresClause)) |
3984 | return ToFunction; |
3985 | } else if (CXXDestructorDecl *FromDtor = dyn_cast<CXXDestructorDecl>(Val: D)) { |
3986 | |
3987 | Error Err = Error::success(); |
3988 | auto ToOperatorDelete = importChecked( |
3989 | Err, From: const_cast<FunctionDecl *>(FromDtor->getOperatorDelete())); |
3990 | auto ToThisArg = importChecked(Err, From: FromDtor->getOperatorDeleteThisArg()); |
3991 | if (Err) |
3992 | return std::move(Err); |
3993 | |
3994 | if (GetImportedOrCreateDecl<CXXDestructorDecl>( |
3995 | ToD&: ToFunction, FromD: D, args&: Importer.getToContext(), args: cast<CXXRecordDecl>(Val: DC), |
3996 | args&: ToInnerLocStart, args&: NameInfo, args&: T, args&: TInfo, args: D->UsesFPIntrin(), |
3997 | args: D->isInlineSpecified(), args: D->isImplicit(), args: D->getConstexprKind(), |
3998 | args&: TrailingRequiresClause)) |
3999 | return ToFunction; |
4000 | |
4001 | CXXDestructorDecl *ToDtor = cast<CXXDestructorDecl>(Val: ToFunction); |
4002 | |
4003 | ToDtor->setOperatorDelete(OD: ToOperatorDelete, ThisArg: ToThisArg); |
4004 | } else if (CXXConversionDecl *FromConversion = |
4005 | dyn_cast<CXXConversionDecl>(Val: D)) { |
4006 | ExplicitSpecifier ESpec = |
4007 | importExplicitSpecifier(Err, ESpec: FromConversion->getExplicitSpecifier()); |
4008 | if (Err) |
4009 | return std::move(Err); |
4010 | if (GetImportedOrCreateDecl<CXXConversionDecl>( |
4011 | ToD&: ToFunction, FromD: D, args&: Importer.getToContext(), args: cast<CXXRecordDecl>(Val: DC), |
4012 | args&: ToInnerLocStart, args&: NameInfo, args&: T, args&: TInfo, args: D->UsesFPIntrin(), |
4013 | args: D->isInlineSpecified(), args&: ESpec, args: D->getConstexprKind(), |
4014 | args: SourceLocation(), args&: TrailingRequiresClause)) |
4015 | return ToFunction; |
4016 | } else if (auto *Method = dyn_cast<CXXMethodDecl>(Val: D)) { |
4017 | if (GetImportedOrCreateDecl<CXXMethodDecl>( |
4018 | ToD&: ToFunction, FromD: D, args&: Importer.getToContext(), args: cast<CXXRecordDecl>(Val: DC), |
4019 | args&: ToInnerLocStart, args&: NameInfo, args&: T, args&: TInfo, args: Method->getStorageClass(), |
4020 | args: Method->UsesFPIntrin(), args: Method->isInlineSpecified(), |
4021 | args: D->getConstexprKind(), args: SourceLocation(), args&: TrailingRequiresClause)) |
4022 | return ToFunction; |
4023 | } else if (auto *Guide = dyn_cast<CXXDeductionGuideDecl>(Val: D)) { |
4024 | ExplicitSpecifier ESpec = |
4025 | importExplicitSpecifier(Err, ESpec: Guide->getExplicitSpecifier()); |
4026 | CXXConstructorDecl *Ctor = |
4027 | importChecked(Err, From: Guide->getCorrespondingConstructor()); |
4028 | const CXXDeductionGuideDecl *SourceDG = |
4029 | importChecked(Err, From: Guide->getSourceDeductionGuide()); |
4030 | if (Err) |
4031 | return std::move(Err); |
4032 | if (GetImportedOrCreateDecl<CXXDeductionGuideDecl>( |
4033 | ToD&: ToFunction, FromD: D, args&: Importer.getToContext(), args&: DC, args&: ToInnerLocStart, args&: ESpec, |
4034 | args&: NameInfo, args&: T, args&: TInfo, args&: ToEndLoc, args&: Ctor, |
4035 | args: Guide->getDeductionCandidateKind(), args&: TrailingRequiresClause, |
4036 | args&: SourceDG, args: Guide->getSourceDeductionGuideKind())) |
4037 | return ToFunction; |
4038 | } else { |
4039 | if (GetImportedOrCreateDecl( |
4040 | ToD&: ToFunction, FromD: D, args&: Importer.getToContext(), args&: DC, args&: ToInnerLocStart, |
4041 | args&: NameInfo, args&: T, args&: TInfo, args: D->getStorageClass(), args: D->UsesFPIntrin(), |
4042 | args: D->isInlineSpecified(), args: D->hasWrittenPrototype(), |
4043 | args: D->getConstexprKind(), args&: TrailingRequiresClause)) |
4044 | return ToFunction; |
4045 | } |
4046 | |
4047 | // Connect the redecl chain. |
4048 | if (FoundByLookup) { |
4049 | auto *Recent = const_cast<FunctionDecl *>( |
4050 | FoundByLookup->getMostRecentDecl()); |
4051 | ToFunction->setPreviousDecl(Recent); |
4052 | // FIXME Probably we should merge exception specifications. E.g. In the |
4053 | // "To" context the existing function may have exception specification with |
4054 | // noexcept-unevaluated, while the newly imported function may have an |
4055 | // evaluated noexcept. A call to adjustExceptionSpec() on the imported |
4056 | // decl and its redeclarations may be required. |
4057 | } |
4058 | |
4059 | StringLiteral *Msg = D->getDeletedMessage(); |
4060 | if (Msg) { |
4061 | auto Imported = import(From: Msg); |
4062 | if (!Imported) |
4063 | return Imported.takeError(); |
4064 | Msg = *Imported; |
4065 | } |
4066 | |
4067 | ToFunction->setQualifierInfo(ToQualifierLoc); |
4068 | ToFunction->setAccess(D->getAccess()); |
4069 | ToFunction->setLexicalDeclContext(LexicalDC); |
4070 | ToFunction->setVirtualAsWritten(D->isVirtualAsWritten()); |
4071 | ToFunction->setTrivial(D->isTrivial()); |
4072 | ToFunction->setIsPureVirtual(D->isPureVirtual()); |
4073 | ToFunction->setDefaulted(D->isDefaulted()); |
4074 | ToFunction->setExplicitlyDefaulted(D->isExplicitlyDefaulted()); |
4075 | ToFunction->setDeletedAsWritten(D: D->isDeletedAsWritten()); |
4076 | ToFunction->setFriendConstraintRefersToEnclosingTemplate( |
4077 | D->FriendConstraintRefersToEnclosingTemplate()); |
4078 | ToFunction->setIsDestroyingOperatorDelete(D->isDestroyingOperatorDelete()); |
4079 | ToFunction->setIsTypeAwareOperatorNewOrDelete( |
4080 | D->isTypeAwareOperatorNewOrDelete()); |
4081 | ToFunction->setRangeEnd(ToEndLoc); |
4082 | ToFunction->setDefaultLoc(ToDefaultLoc); |
4083 | |
4084 | if (Msg) |
4085 | ToFunction->setDefaultedOrDeletedInfo( |
4086 | FunctionDecl::DefaultedOrDeletedFunctionInfo::Create( |
4087 | Context&: Importer.getToContext(), Lookups: {}, DeletedMessage: Msg)); |
4088 | |
4089 | // Set the parameters. |
4090 | for (auto *Param : Parameters) { |
4091 | Param->setOwningFunction(ToFunction); |
4092 | ToFunction->addDeclInternal(D: Param); |
4093 | if (ASTImporterLookupTable *LT = Importer.SharedState->getLookupTable()) |
4094 | LT->update(ND: Param, OldDC: Importer.getToContext().getTranslationUnitDecl()); |
4095 | } |
4096 | ToFunction->setParams(Parameters); |
4097 | |
4098 | // We need to complete creation of FunctionProtoTypeLoc manually with setting |
4099 | // params it refers to. |
4100 | if (TInfo) { |
4101 | if (auto ProtoLoc = |
4102 | TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) { |
4103 | for (unsigned I = 0, N = Parameters.size(); I != N; ++I) |
4104 | ProtoLoc.setParam(i: I, VD: Parameters[I]); |
4105 | } |
4106 | } |
4107 | |
4108 | // Import the describing template function, if any. |
4109 | if (FromFT) { |
4110 | auto ToFTOrErr = import(From: FromFT); |
4111 | if (!ToFTOrErr) |
4112 | return ToFTOrErr.takeError(); |
4113 | } |
4114 | |
4115 | // Import Ctor initializers. |
4116 | if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(Val: D)) { |
4117 | if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) { |
4118 | SmallVector<CXXCtorInitializer *, 4> CtorInitializers(NumInitializers); |
4119 | // Import first, then allocate memory and copy if there was no error. |
4120 | if (Error Err = ImportContainerChecked( |
4121 | InContainer: FromConstructor->inits(), OutContainer&: CtorInitializers)) |
4122 | return std::move(Err); |
4123 | auto **Memory = |
4124 | new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers]; |
4125 | llvm::copy(Range&: CtorInitializers, Out: Memory); |
4126 | auto *ToCtor = cast<CXXConstructorDecl>(Val: ToFunction); |
4127 | ToCtor->setCtorInitializers(Memory); |
4128 | ToCtor->setNumCtorInitializers(NumInitializers); |
4129 | } |
4130 | } |
4131 | |
4132 | // If it is a template, import all related things. |
4133 | if (Error Err = ImportTemplateInformation(FromFD: D, ToFD: ToFunction)) |
4134 | return std::move(Err); |
4135 | |
4136 | if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(Val: D)) |
4137 | if (Error Err = ImportOverriddenMethods(ToMethod: cast<CXXMethodDecl>(Val: ToFunction), |
4138 | FromMethod: FromCXXMethod)) |
4139 | return std::move(Err); |
4140 | |
4141 | if (D->doesThisDeclarationHaveABody()) { |
4142 | Error Err = ImportFunctionDeclBody(FromFD: D, ToFD: ToFunction); |
4143 | |
4144 | if (Err) |
4145 | return std::move(Err); |
4146 | } |
4147 | |
4148 | // Import and set the original type in case we used another type. |
4149 | if (UsedDifferentProtoType) { |
4150 | if (ExpectedType TyOrErr = import(From: D->getType())) |
4151 | ToFunction->setType(*TyOrErr); |
4152 | else |
4153 | return TyOrErr.takeError(); |
4154 | if (Expected<TypeSourceInfo *> TSIOrErr = import(From: D->getTypeSourceInfo())) |
4155 | ToFunction->setTypeSourceInfo(*TSIOrErr); |
4156 | else |
4157 | return TSIOrErr.takeError(); |
4158 | } |
4159 | |
4160 | // FIXME: Other bits to merge? |
4161 | |
4162 | addDeclToContexts(FromD: D, ToD: ToFunction); |
4163 | |
4164 | // Import the rest of the chain. I.e. import all subsequent declarations. |
4165 | for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) { |
4166 | ExpectedDecl ToRedeclOrErr = import(From: *RedeclIt); |
4167 | if (!ToRedeclOrErr) |
4168 | return ToRedeclOrErr.takeError(); |
4169 | } |
4170 | |
4171 | return ToFunction; |
4172 | } |
4173 | |
4174 | ExpectedDecl ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) { |
4175 | return VisitFunctionDecl(D); |
4176 | } |
4177 | |
4178 | ExpectedDecl ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
4179 | return VisitCXXMethodDecl(D); |
4180 | } |
4181 | |
4182 | ExpectedDecl ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
4183 | return VisitCXXMethodDecl(D); |
4184 | } |
4185 | |
4186 | ExpectedDecl ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) { |
4187 | return VisitCXXMethodDecl(D); |
4188 | } |
4189 | |
4190 | ExpectedDecl |
4191 | ASTNodeImporter::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) { |
4192 | return VisitFunctionDecl(D); |
4193 | } |
4194 | |
4195 | ExpectedDecl ASTNodeImporter::VisitFieldDecl(FieldDecl *D) { |
4196 | // Import the major distinguishing characteristics of a variable. |
4197 | DeclContext *DC, *LexicalDC; |
4198 | DeclarationName Name; |
4199 | SourceLocation Loc; |
4200 | NamedDecl *ToD; |
4201 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
4202 | return std::move(Err); |
4203 | if (ToD) |
4204 | return ToD; |
4205 | |
4206 | // Determine whether we've already imported this field. |
4207 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
4208 | for (auto *FoundDecl : FoundDecls) { |
4209 | if (FieldDecl *FoundField = dyn_cast<FieldDecl>(Val: FoundDecl)) { |
4210 | // For anonymous fields, match up by index. |
4211 | if (!Name && |
4212 | ASTImporter::getFieldIndex(F: D) != |
4213 | ASTImporter::getFieldIndex(F: FoundField)) |
4214 | continue; |
4215 | |
4216 | if (Importer.IsStructurallyEquivalent(From: D->getType(), |
4217 | To: FoundField->getType())) { |
4218 | Importer.MapImported(From: D, To: FoundField); |
4219 | // In case of a FieldDecl of a ClassTemplateSpecializationDecl, the |
4220 | // initializer of a FieldDecl might not had been instantiated in the |
4221 | // "To" context. However, the "From" context might instantiated that, |
4222 | // thus we have to merge that. |
4223 | // Note: `hasInClassInitializer()` is not the same as non-null |
4224 | // `getInClassInitializer()` value. |
4225 | if (Expr *FromInitializer = D->getInClassInitializer()) { |
4226 | if (ExpectedExpr ToInitializerOrErr = import(From: FromInitializer)) { |
4227 | // Import of the FromInitializer may result in the setting of |
4228 | // InClassInitializer. If not, set it here. |
4229 | assert(FoundField->hasInClassInitializer() && |
4230 | "Field should have an in-class initializer if it has an " |
4231 | "expression for it." ); |
4232 | if (!FoundField->getInClassInitializer()) |
4233 | FoundField->setInClassInitializer(*ToInitializerOrErr); |
4234 | } else { |
4235 | return ToInitializerOrErr.takeError(); |
4236 | } |
4237 | } |
4238 | return FoundField; |
4239 | } |
4240 | |
4241 | // FIXME: Why is this case not handled with calling HandleNameConflict? |
4242 | Importer.ToDiag(Loc, DiagID: diag::warn_odr_field_type_inconsistent) |
4243 | << Name << D->getType() << FoundField->getType(); |
4244 | Importer.ToDiag(Loc: FoundField->getLocation(), DiagID: diag::note_odr_value_here) |
4245 | << FoundField->getType(); |
4246 | |
4247 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
4248 | } |
4249 | } |
4250 | |
4251 | Error Err = Error::success(); |
4252 | auto ToType = importChecked(Err, From: D->getType()); |
4253 | auto ToTInfo = importChecked(Err, From: D->getTypeSourceInfo()); |
4254 | auto ToBitWidth = importChecked(Err, From: D->getBitWidth()); |
4255 | auto ToInnerLocStart = importChecked(Err, From: D->getInnerLocStart()); |
4256 | if (Err) |
4257 | return std::move(Err); |
4258 | const Type *ToCapturedVLAType = nullptr; |
4259 | if (Error Err = Importer.importInto( |
4260 | To&: ToCapturedVLAType, From: cast_or_null<Type>(Val: D->getCapturedVLAType()))) |
4261 | return std::move(Err); |
4262 | |
4263 | FieldDecl *ToField; |
4264 | if (GetImportedOrCreateDecl(ToD&: ToField, FromD: D, args&: Importer.getToContext(), args&: DC, |
4265 | args&: ToInnerLocStart, args&: Loc, args: Name.getAsIdentifierInfo(), |
4266 | args&: ToType, args&: ToTInfo, args&: ToBitWidth, args: D->isMutable(), |
4267 | args: D->getInClassInitStyle())) |
4268 | return ToField; |
4269 | |
4270 | ToField->setAccess(D->getAccess()); |
4271 | ToField->setLexicalDeclContext(LexicalDC); |
4272 | ToField->setImplicit(D->isImplicit()); |
4273 | if (ToCapturedVLAType) |
4274 | ToField->setCapturedVLAType(cast<VariableArrayType>(Val: ToCapturedVLAType)); |
4275 | LexicalDC->addDeclInternal(D: ToField); |
4276 | // Import initializer only after the field was created, it may have recursive |
4277 | // reference to the field. |
4278 | auto ToInitializer = importChecked(Err, From: D->getInClassInitializer()); |
4279 | if (Err) |
4280 | return std::move(Err); |
4281 | if (ToInitializer) { |
4282 | auto *AlreadyImported = ToField->getInClassInitializer(); |
4283 | if (AlreadyImported) |
4284 | assert(ToInitializer == AlreadyImported && |
4285 | "Duplicate import of in-class initializer." ); |
4286 | else |
4287 | ToField->setInClassInitializer(ToInitializer); |
4288 | } |
4289 | |
4290 | return ToField; |
4291 | } |
4292 | |
4293 | ExpectedDecl ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) { |
4294 | // Import the major distinguishing characteristics of a variable. |
4295 | DeclContext *DC, *LexicalDC; |
4296 | DeclarationName Name; |
4297 | SourceLocation Loc; |
4298 | NamedDecl *ToD; |
4299 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
4300 | return std::move(Err); |
4301 | if (ToD) |
4302 | return ToD; |
4303 | |
4304 | // Determine whether we've already imported this field. |
4305 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
4306 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
4307 | if (auto *FoundField = dyn_cast<IndirectFieldDecl>(Val: FoundDecls[I])) { |
4308 | // For anonymous indirect fields, match up by index. |
4309 | if (!Name && |
4310 | ASTImporter::getFieldIndex(F: D) != |
4311 | ASTImporter::getFieldIndex(F: FoundField)) |
4312 | continue; |
4313 | |
4314 | if (Importer.IsStructurallyEquivalent(From: D->getType(), |
4315 | To: FoundField->getType(), |
4316 | Complain: !Name.isEmpty())) { |
4317 | Importer.MapImported(From: D, To: FoundField); |
4318 | return FoundField; |
4319 | } |
4320 | |
4321 | // If there are more anonymous fields to check, continue. |
4322 | if (!Name && I < N-1) |
4323 | continue; |
4324 | |
4325 | // FIXME: Why is this case not handled with calling HandleNameConflict? |
4326 | Importer.ToDiag(Loc, DiagID: diag::warn_odr_field_type_inconsistent) |
4327 | << Name << D->getType() << FoundField->getType(); |
4328 | Importer.ToDiag(Loc: FoundField->getLocation(), DiagID: diag::note_odr_value_here) |
4329 | << FoundField->getType(); |
4330 | |
4331 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
4332 | } |
4333 | } |
4334 | |
4335 | // Import the type. |
4336 | auto TypeOrErr = import(From: D->getType()); |
4337 | if (!TypeOrErr) |
4338 | return TypeOrErr.takeError(); |
4339 | |
4340 | auto **NamedChain = |
4341 | new (Importer.getToContext()) NamedDecl*[D->getChainingSize()]; |
4342 | |
4343 | unsigned i = 0; |
4344 | for (auto *PI : D->chain()) |
4345 | if (Expected<NamedDecl *> ToD = import(From: PI)) |
4346 | NamedChain[i++] = *ToD; |
4347 | else |
4348 | return ToD.takeError(); |
4349 | |
4350 | MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()}; |
4351 | IndirectFieldDecl *ToIndirectField; |
4352 | if (GetImportedOrCreateDecl(ToD&: ToIndirectField, FromD: D, args&: Importer.getToContext(), args&: DC, |
4353 | args&: Loc, args: Name.getAsIdentifierInfo(), args&: *TypeOrErr, args&: CH)) |
4354 | // FIXME here we leak `NamedChain` which is allocated before |
4355 | return ToIndirectField; |
4356 | |
4357 | ToIndirectField->setAccess(D->getAccess()); |
4358 | ToIndirectField->setLexicalDeclContext(LexicalDC); |
4359 | LexicalDC->addDeclInternal(D: ToIndirectField); |
4360 | return ToIndirectField; |
4361 | } |
4362 | |
4363 | /// Used as return type of getFriendCountAndPosition. |
4364 | struct FriendCountAndPosition { |
4365 | /// Number of similar looking friends. |
4366 | unsigned int TotalCount; |
4367 | /// Index of the specific FriendDecl. |
4368 | unsigned int IndexOfDecl; |
4369 | }; |
4370 | |
4371 | static bool IsEquivalentFriend(ASTImporter &Importer, FriendDecl *FD1, |
4372 | FriendDecl *FD2) { |
4373 | if ((!FD1->getFriendType()) != (!FD2->getFriendType())) |
4374 | return false; |
4375 | |
4376 | if (const TypeSourceInfo *TSI = FD1->getFriendType()) |
4377 | return Importer.IsStructurallyEquivalent( |
4378 | From: TSI->getType(), To: FD2->getFriendType()->getType(), /*Complain=*/false); |
4379 | |
4380 | ASTImporter::NonEquivalentDeclSet NonEquivalentDecls; |
4381 | StructuralEquivalenceContext Ctx( |
4382 | Importer.getToContext().getLangOpts(), FD1->getASTContext(), |
4383 | FD2->getASTContext(), NonEquivalentDecls, |
4384 | StructuralEquivalenceKind::Default, |
4385 | /* StrictTypeSpelling = */ false, /* Complain = */ false); |
4386 | return Ctx.IsEquivalent(D1: FD1, D2: FD2); |
4387 | } |
4388 | |
4389 | static FriendCountAndPosition getFriendCountAndPosition(ASTImporter &Importer, |
4390 | FriendDecl *FD) { |
4391 | unsigned int FriendCount = 0; |
4392 | UnsignedOrNone FriendPosition = std::nullopt; |
4393 | const auto *RD = cast<CXXRecordDecl>(Val: FD->getLexicalDeclContext()); |
4394 | |
4395 | for (FriendDecl *FoundFriend : RD->friends()) { |
4396 | if (FoundFriend == FD) { |
4397 | FriendPosition = FriendCount; |
4398 | ++FriendCount; |
4399 | } else if (IsEquivalentFriend(Importer, FD1: FD, FD2: FoundFriend)) { |
4400 | ++FriendCount; |
4401 | } |
4402 | } |
4403 | |
4404 | assert(FriendPosition && "Friend decl not found in own parent." ); |
4405 | |
4406 | return {.TotalCount: FriendCount, .IndexOfDecl: *FriendPosition}; |
4407 | } |
4408 | |
4409 | ExpectedDecl ASTNodeImporter::VisitFriendDecl(FriendDecl *D) { |
4410 | // Import the major distinguishing characteristics of a declaration. |
4411 | DeclContext *DC, *LexicalDC; |
4412 | if (Error Err = ImportDeclContext(FromD: D, ToDC&: DC, ToLexicalDC&: LexicalDC)) |
4413 | return std::move(Err); |
4414 | |
4415 | // Determine whether we've already imported this decl. |
4416 | // FriendDecl is not a NamedDecl so we cannot use lookup. |
4417 | // We try to maintain order and count of redundant friend declarations. |
4418 | const auto *RD = cast<CXXRecordDecl>(Val: DC); |
4419 | SmallVector<FriendDecl *, 2> ImportedEquivalentFriends; |
4420 | for (FriendDecl *ImportedFriend : RD->friends()) |
4421 | if (IsEquivalentFriend(Importer, FD1: D, FD2: ImportedFriend)) |
4422 | ImportedEquivalentFriends.push_back(Elt: ImportedFriend); |
4423 | |
4424 | FriendCountAndPosition CountAndPosition = |
4425 | getFriendCountAndPosition(Importer, FD: D); |
4426 | |
4427 | assert(ImportedEquivalentFriends.size() <= CountAndPosition.TotalCount && |
4428 | "Class with non-matching friends is imported, ODR check wrong?" ); |
4429 | if (ImportedEquivalentFriends.size() == CountAndPosition.TotalCount) |
4430 | return Importer.MapImported( |
4431 | From: D, To: ImportedEquivalentFriends[CountAndPosition.IndexOfDecl]); |
4432 | |
4433 | // Not found. Create it. |
4434 | // The declarations will be put into order later by ImportDeclContext. |
4435 | FriendDecl::FriendUnion ToFU; |
4436 | if (NamedDecl *FriendD = D->getFriendDecl()) { |
4437 | NamedDecl *ToFriendD; |
4438 | if (Error Err = importInto(To&: ToFriendD, From: FriendD)) |
4439 | return std::move(Err); |
4440 | |
4441 | if (FriendD->getFriendObjectKind() != Decl::FOK_None && |
4442 | !(FriendD->isInIdentifierNamespace(NS: Decl::IDNS_NonMemberOperator))) |
4443 | ToFriendD->setObjectOfFriendDecl(false); |
4444 | |
4445 | ToFU = ToFriendD; |
4446 | } else { // The friend is a type, not a decl. |
4447 | if (auto TSIOrErr = import(From: D->getFriendType())) |
4448 | ToFU = *TSIOrErr; |
4449 | else |
4450 | return TSIOrErr.takeError(); |
4451 | } |
4452 | |
4453 | SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists); |
4454 | auto **FromTPLists = D->getTrailingObjects(); |
4455 | for (unsigned I = 0; I < D->NumTPLists; I++) { |
4456 | if (auto ListOrErr = import(From: FromTPLists[I])) |
4457 | ToTPLists[I] = *ListOrErr; |
4458 | else |
4459 | return ListOrErr.takeError(); |
4460 | } |
4461 | |
4462 | auto LocationOrErr = import(From: D->getLocation()); |
4463 | if (!LocationOrErr) |
4464 | return LocationOrErr.takeError(); |
4465 | auto FriendLocOrErr = import(From: D->getFriendLoc()); |
4466 | if (!FriendLocOrErr) |
4467 | return FriendLocOrErr.takeError(); |
4468 | auto EllipsisLocOrErr = import(From: D->getEllipsisLoc()); |
4469 | if (!EllipsisLocOrErr) |
4470 | return EllipsisLocOrErr.takeError(); |
4471 | |
4472 | FriendDecl *FrD; |
4473 | if (GetImportedOrCreateDecl(ToD&: FrD, FromD: D, args&: Importer.getToContext(), args&: DC, |
4474 | args&: *LocationOrErr, args&: ToFU, args&: *FriendLocOrErr, |
4475 | args&: *EllipsisLocOrErr, args&: ToTPLists)) |
4476 | return FrD; |
4477 | |
4478 | FrD->setAccess(D->getAccess()); |
4479 | FrD->setLexicalDeclContext(LexicalDC); |
4480 | LexicalDC->addDeclInternal(D: FrD); |
4481 | return FrD; |
4482 | } |
4483 | |
4484 | ExpectedDecl ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) { |
4485 | // Import the major distinguishing characteristics of an ivar. |
4486 | DeclContext *DC, *LexicalDC; |
4487 | DeclarationName Name; |
4488 | SourceLocation Loc; |
4489 | NamedDecl *ToD; |
4490 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
4491 | return std::move(Err); |
4492 | if (ToD) |
4493 | return ToD; |
4494 | |
4495 | // Determine whether we've already imported this ivar |
4496 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
4497 | for (auto *FoundDecl : FoundDecls) { |
4498 | if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(Val: FoundDecl)) { |
4499 | if (Importer.IsStructurallyEquivalent(From: D->getType(), |
4500 | To: FoundIvar->getType())) { |
4501 | Importer.MapImported(From: D, To: FoundIvar); |
4502 | return FoundIvar; |
4503 | } |
4504 | |
4505 | Importer.ToDiag(Loc, DiagID: diag::warn_odr_ivar_type_inconsistent) |
4506 | << Name << D->getType() << FoundIvar->getType(); |
4507 | Importer.ToDiag(Loc: FoundIvar->getLocation(), DiagID: diag::note_odr_value_here) |
4508 | << FoundIvar->getType(); |
4509 | |
4510 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
4511 | } |
4512 | } |
4513 | |
4514 | Error Err = Error::success(); |
4515 | auto ToType = importChecked(Err, From: D->getType()); |
4516 | auto ToTypeSourceInfo = importChecked(Err, From: D->getTypeSourceInfo()); |
4517 | auto ToBitWidth = importChecked(Err, From: D->getBitWidth()); |
4518 | auto ToInnerLocStart = importChecked(Err, From: D->getInnerLocStart()); |
4519 | if (Err) |
4520 | return std::move(Err); |
4521 | |
4522 | ObjCIvarDecl *ToIvar; |
4523 | if (GetImportedOrCreateDecl( |
4524 | ToD&: ToIvar, FromD: D, args&: Importer.getToContext(), args: cast<ObjCContainerDecl>(Val: DC), |
4525 | args&: ToInnerLocStart, args&: Loc, args: Name.getAsIdentifierInfo(), |
4526 | args&: ToType, args&: ToTypeSourceInfo, |
4527 | args: D->getAccessControl(),args&: ToBitWidth, args: D->getSynthesize())) |
4528 | return ToIvar; |
4529 | |
4530 | ToIvar->setLexicalDeclContext(LexicalDC); |
4531 | LexicalDC->addDeclInternal(D: ToIvar); |
4532 | return ToIvar; |
4533 | } |
4534 | |
4535 | ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) { |
4536 | |
4537 | SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D); |
4538 | auto RedeclIt = Redecls.begin(); |
4539 | // Import the first part of the decl chain. I.e. import all previous |
4540 | // declarations starting from the canonical decl. |
4541 | for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) { |
4542 | ExpectedDecl RedeclOrErr = import(From: *RedeclIt); |
4543 | if (!RedeclOrErr) |
4544 | return RedeclOrErr.takeError(); |
4545 | } |
4546 | assert(*RedeclIt == D); |
4547 | |
4548 | // Import the major distinguishing characteristics of a variable. |
4549 | DeclContext *DC, *LexicalDC; |
4550 | DeclarationName Name; |
4551 | SourceLocation Loc; |
4552 | NamedDecl *ToD; |
4553 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
4554 | return std::move(Err); |
4555 | if (ToD) |
4556 | return ToD; |
4557 | |
4558 | // Try to find a variable in our own ("to") context with the same name and |
4559 | // in the same context as the variable we're importing. |
4560 | VarDecl *FoundByLookup = nullptr; |
4561 | if (D->isFileVarDecl()) { |
4562 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
4563 | unsigned IDNS = Decl::IDNS_Ordinary; |
4564 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
4565 | for (auto *FoundDecl : FoundDecls) { |
4566 | if (!FoundDecl->isInIdentifierNamespace(NS: IDNS)) |
4567 | continue; |
4568 | |
4569 | if (auto *FoundVar = dyn_cast<VarDecl>(Val: FoundDecl)) { |
4570 | if (!hasSameVisibilityContextAndLinkage(Found: FoundVar, From: D)) |
4571 | continue; |
4572 | if (Importer.IsStructurallyEquivalent(From: D->getType(), |
4573 | To: FoundVar->getType())) { |
4574 | |
4575 | // The VarDecl in the "From" context has a definition, but in the |
4576 | // "To" context we already have a definition. |
4577 | VarDecl *FoundDef = FoundVar->getDefinition(); |
4578 | if (D->isThisDeclarationADefinition() && FoundDef) |
4579 | // FIXME Check for ODR error if the two definitions have |
4580 | // different initializers? |
4581 | return Importer.MapImported(From: D, To: FoundDef); |
4582 | |
4583 | // The VarDecl in the "From" context has an initializer, but in the |
4584 | // "To" context we already have an initializer. |
4585 | const VarDecl *FoundDInit = nullptr; |
4586 | if (D->getInit() && FoundVar->getAnyInitializer(D&: FoundDInit)) |
4587 | // FIXME Diagnose ODR error if the two initializers are different? |
4588 | return Importer.MapImported(From: D, To: const_cast<VarDecl*>(FoundDInit)); |
4589 | |
4590 | FoundByLookup = FoundVar; |
4591 | break; |
4592 | } |
4593 | |
4594 | const ArrayType *FoundArray |
4595 | = Importer.getToContext().getAsArrayType(T: FoundVar->getType()); |
4596 | const ArrayType *TArray |
4597 | = Importer.getToContext().getAsArrayType(T: D->getType()); |
4598 | if (FoundArray && TArray) { |
4599 | if (isa<IncompleteArrayType>(Val: FoundArray) && |
4600 | isa<ConstantArrayType>(Val: TArray)) { |
4601 | // Import the type. |
4602 | if (auto TyOrErr = import(From: D->getType())) |
4603 | FoundVar->setType(*TyOrErr); |
4604 | else |
4605 | return TyOrErr.takeError(); |
4606 | |
4607 | FoundByLookup = FoundVar; |
4608 | break; |
4609 | } else if (isa<IncompleteArrayType>(Val: TArray) && |
4610 | isa<ConstantArrayType>(Val: FoundArray)) { |
4611 | FoundByLookup = FoundVar; |
4612 | break; |
4613 | } |
4614 | } |
4615 | |
4616 | Importer.ToDiag(Loc, DiagID: diag::warn_odr_variable_type_inconsistent) |
4617 | << Name << D->getType() << FoundVar->getType(); |
4618 | Importer.ToDiag(Loc: FoundVar->getLocation(), DiagID: diag::note_odr_value_here) |
4619 | << FoundVar->getType(); |
4620 | ConflictingDecls.push_back(Elt: FoundDecl); |
4621 | } |
4622 | } |
4623 | |
4624 | if (!ConflictingDecls.empty()) { |
4625 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
4626 | Name, DC, IDNS, Decls: ConflictingDecls.data(), NumDecls: ConflictingDecls.size()); |
4627 | if (NameOrErr) |
4628 | Name = NameOrErr.get(); |
4629 | else |
4630 | return NameOrErr.takeError(); |
4631 | } |
4632 | } |
4633 | |
4634 | Error Err = Error::success(); |
4635 | auto ToType = importChecked(Err, From: D->getType()); |
4636 | auto ToTypeSourceInfo = importChecked(Err, From: D->getTypeSourceInfo()); |
4637 | auto ToInnerLocStart = importChecked(Err, From: D->getInnerLocStart()); |
4638 | auto ToQualifierLoc = importChecked(Err, From: D->getQualifierLoc()); |
4639 | if (Err) |
4640 | return std::move(Err); |
4641 | |
4642 | VarDecl *ToVar; |
4643 | if (auto *FromDecomp = dyn_cast<DecompositionDecl>(Val: D)) { |
4644 | SmallVector<BindingDecl *> Bindings(FromDecomp->bindings().size()); |
4645 | if (Error Err = |
4646 | ImportArrayChecked(InContainer: FromDecomp->bindings(), Obegin: Bindings.begin())) |
4647 | return std::move(Err); |
4648 | DecompositionDecl *ToDecomp; |
4649 | if (GetImportedOrCreateDecl( |
4650 | ToD&: ToDecomp, FromD: FromDecomp, args&: Importer.getToContext(), args&: DC, args&: ToInnerLocStart, |
4651 | args&: Loc, args&: ToType, args&: ToTypeSourceInfo, args: D->getStorageClass(), args&: Bindings)) |
4652 | return ToDecomp; |
4653 | ToVar = ToDecomp; |
4654 | } else { |
4655 | // Create the imported variable. |
4656 | if (GetImportedOrCreateDecl(ToD&: ToVar, FromD: D, args&: Importer.getToContext(), args&: DC, |
4657 | args&: ToInnerLocStart, args&: Loc, |
4658 | args: Name.getAsIdentifierInfo(), args&: ToType, |
4659 | args&: ToTypeSourceInfo, args: D->getStorageClass())) |
4660 | return ToVar; |
4661 | } |
4662 | |
4663 | ToVar->setTSCSpec(D->getTSCSpec()); |
4664 | ToVar->setQualifierInfo(ToQualifierLoc); |
4665 | ToVar->setAccess(D->getAccess()); |
4666 | ToVar->setLexicalDeclContext(LexicalDC); |
4667 | if (D->isInlineSpecified()) |
4668 | ToVar->setInlineSpecified(); |
4669 | if (D->isInline()) |
4670 | ToVar->setImplicitlyInline(); |
4671 | |
4672 | if (FoundByLookup) { |
4673 | auto *Recent = const_cast<VarDecl *>(FoundByLookup->getMostRecentDecl()); |
4674 | ToVar->setPreviousDecl(Recent); |
4675 | } |
4676 | |
4677 | // Import the described template, if any. |
4678 | if (D->getDescribedVarTemplate()) { |
4679 | auto ToVTOrErr = import(From: D->getDescribedVarTemplate()); |
4680 | if (!ToVTOrErr) |
4681 | return ToVTOrErr.takeError(); |
4682 | } else if (MemberSpecializationInfo *MSI = D->getMemberSpecializationInfo()) { |
4683 | TemplateSpecializationKind SK = MSI->getTemplateSpecializationKind(); |
4684 | VarDecl *FromInst = D->getInstantiatedFromStaticDataMember(); |
4685 | if (Expected<VarDecl *> ToInstOrErr = import(From: FromInst)) |
4686 | ToVar->setInstantiationOfStaticDataMember(VD: *ToInstOrErr, TSK: SK); |
4687 | else |
4688 | return ToInstOrErr.takeError(); |
4689 | if (ExpectedSLoc POIOrErr = import(From: MSI->getPointOfInstantiation())) |
4690 | ToVar->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr); |
4691 | else |
4692 | return POIOrErr.takeError(); |
4693 | } |
4694 | |
4695 | if (Error Err = ImportInitializer(From: D, To: ToVar)) |
4696 | return std::move(Err); |
4697 | |
4698 | if (D->isConstexpr()) |
4699 | ToVar->setConstexpr(true); |
4700 | |
4701 | addDeclToContexts(FromD: D, ToD: ToVar); |
4702 | |
4703 | // Import the rest of the chain. I.e. import all subsequent declarations. |
4704 | for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) { |
4705 | ExpectedDecl RedeclOrErr = import(From: *RedeclIt); |
4706 | if (!RedeclOrErr) |
4707 | return RedeclOrErr.takeError(); |
4708 | } |
4709 | |
4710 | return ToVar; |
4711 | } |
4712 | |
4713 | ExpectedDecl ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) { |
4714 | // Parameters are created in the translation unit's context, then moved |
4715 | // into the function declaration's context afterward. |
4716 | DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); |
4717 | |
4718 | Error Err = Error::success(); |
4719 | auto ToDeclName = importChecked(Err, From: D->getDeclName()); |
4720 | auto ToLocation = importChecked(Err, From: D->getLocation()); |
4721 | auto ToType = importChecked(Err, From: D->getType()); |
4722 | if (Err) |
4723 | return std::move(Err); |
4724 | |
4725 | // Create the imported parameter. |
4726 | ImplicitParamDecl *ToParm = nullptr; |
4727 | if (GetImportedOrCreateDecl(ToD&: ToParm, FromD: D, args&: Importer.getToContext(), args&: DC, |
4728 | args&: ToLocation, args: ToDeclName.getAsIdentifierInfo(), |
4729 | args&: ToType, args: D->getParameterKind())) |
4730 | return ToParm; |
4731 | return ToParm; |
4732 | } |
4733 | |
4734 | Error ASTNodeImporter::ImportDefaultArgOfParmVarDecl( |
4735 | const ParmVarDecl *FromParam, ParmVarDecl *ToParam) { |
4736 | |
4737 | if (auto LocOrErr = import(From: FromParam->getExplicitObjectParamThisLoc())) |
4738 | ToParam->setExplicitObjectParameterLoc(*LocOrErr); |
4739 | else |
4740 | return LocOrErr.takeError(); |
4741 | |
4742 | ToParam->setHasInheritedDefaultArg(FromParam->hasInheritedDefaultArg()); |
4743 | ToParam->setKNRPromoted(FromParam->isKNRPromoted()); |
4744 | |
4745 | if (FromParam->hasUninstantiatedDefaultArg()) { |
4746 | if (auto ToDefArgOrErr = import(From: FromParam->getUninstantiatedDefaultArg())) |
4747 | ToParam->setUninstantiatedDefaultArg(*ToDefArgOrErr); |
4748 | else |
4749 | return ToDefArgOrErr.takeError(); |
4750 | } else if (FromParam->hasUnparsedDefaultArg()) { |
4751 | ToParam->setUnparsedDefaultArg(); |
4752 | } else if (FromParam->hasDefaultArg()) { |
4753 | if (auto ToDefArgOrErr = import(From: FromParam->getDefaultArg())) |
4754 | ToParam->setDefaultArg(*ToDefArgOrErr); |
4755 | else |
4756 | return ToDefArgOrErr.takeError(); |
4757 | } |
4758 | |
4759 | return Error::success(); |
4760 | } |
4761 | |
4762 | Expected<InheritedConstructor> |
4763 | ASTNodeImporter::ImportInheritedConstructor(const InheritedConstructor &From) { |
4764 | Error Err = Error::success(); |
4765 | CXXConstructorDecl *ToBaseCtor = importChecked(Err, From: From.getConstructor()); |
4766 | ConstructorUsingShadowDecl *ToShadow = |
4767 | importChecked(Err, From: From.getShadowDecl()); |
4768 | if (Err) |
4769 | return std::move(Err); |
4770 | return InheritedConstructor(ToShadow, ToBaseCtor); |
4771 | } |
4772 | |
4773 | ExpectedDecl ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) { |
4774 | // Parameters are created in the translation unit's context, then moved |
4775 | // into the function declaration's context afterward. |
4776 | DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); |
4777 | |
4778 | Error Err = Error::success(); |
4779 | auto ToDeclName = importChecked(Err, From: D->getDeclName()); |
4780 | auto ToLocation = importChecked(Err, From: D->getLocation()); |
4781 | auto ToInnerLocStart = importChecked(Err, From: D->getInnerLocStart()); |
4782 | auto ToType = importChecked(Err, From: D->getType()); |
4783 | auto ToTypeSourceInfo = importChecked(Err, From: D->getTypeSourceInfo()); |
4784 | if (Err) |
4785 | return std::move(Err); |
4786 | |
4787 | ParmVarDecl *ToParm; |
4788 | if (GetImportedOrCreateDecl(ToD&: ToParm, FromD: D, args&: Importer.getToContext(), args&: DC, |
4789 | args&: ToInnerLocStart, args&: ToLocation, |
4790 | args: ToDeclName.getAsIdentifierInfo(), args&: ToType, |
4791 | args&: ToTypeSourceInfo, args: D->getStorageClass(), |
4792 | /*DefaultArg*/ args: nullptr)) |
4793 | return ToParm; |
4794 | |
4795 | // Set the default argument. It should be no problem if it was already done. |
4796 | // Do not import the default expression before GetImportedOrCreateDecl call |
4797 | // to avoid possible infinite import loop because circular dependency. |
4798 | if (Error Err = ImportDefaultArgOfParmVarDecl(FromParam: D, ToParam: ToParm)) |
4799 | return std::move(Err); |
4800 | |
4801 | if (D->isObjCMethodParameter()) { |
4802 | ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex()); |
4803 | ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier()); |
4804 | } else { |
4805 | ToParm->setScopeInfo(scopeDepth: D->getFunctionScopeDepth(), |
4806 | parameterIndex: D->getFunctionScopeIndex()); |
4807 | } |
4808 | |
4809 | return ToParm; |
4810 | } |
4811 | |
4812 | ExpectedDecl ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) { |
4813 | // Import the major distinguishing characteristics of a method. |
4814 | DeclContext *DC, *LexicalDC; |
4815 | DeclarationName Name; |
4816 | SourceLocation Loc; |
4817 | NamedDecl *ToD; |
4818 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
4819 | return std::move(Err); |
4820 | if (ToD) |
4821 | return ToD; |
4822 | |
4823 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
4824 | for (auto *FoundDecl : FoundDecls) { |
4825 | if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(Val: FoundDecl)) { |
4826 | if (FoundMethod->isInstanceMethod() != D->isInstanceMethod()) |
4827 | continue; |
4828 | |
4829 | // Check return types. |
4830 | if (!Importer.IsStructurallyEquivalent(From: D->getReturnType(), |
4831 | To: FoundMethod->getReturnType())) { |
4832 | Importer.ToDiag(Loc, DiagID: diag::warn_odr_objc_method_result_type_inconsistent) |
4833 | << D->isInstanceMethod() << Name << D->getReturnType() |
4834 | << FoundMethod->getReturnType(); |
4835 | Importer.ToDiag(Loc: FoundMethod->getLocation(), |
4836 | DiagID: diag::note_odr_objc_method_here) |
4837 | << D->isInstanceMethod() << Name; |
4838 | |
4839 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
4840 | } |
4841 | |
4842 | // Check the number of parameters. |
4843 | if (D->param_size() != FoundMethod->param_size()) { |
4844 | Importer.ToDiag(Loc, DiagID: diag::warn_odr_objc_method_num_params_inconsistent) |
4845 | << D->isInstanceMethod() << Name |
4846 | << D->param_size() << FoundMethod->param_size(); |
4847 | Importer.ToDiag(Loc: FoundMethod->getLocation(), |
4848 | DiagID: diag::note_odr_objc_method_here) |
4849 | << D->isInstanceMethod() << Name; |
4850 | |
4851 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
4852 | } |
4853 | |
4854 | // Check parameter types. |
4855 | for (ObjCMethodDecl::param_iterator P = D->param_begin(), |
4856 | PEnd = D->param_end(), FoundP = FoundMethod->param_begin(); |
4857 | P != PEnd; ++P, ++FoundP) { |
4858 | if (!Importer.IsStructurallyEquivalent(From: (*P)->getType(), |
4859 | To: (*FoundP)->getType())) { |
4860 | Importer.FromDiag(Loc: (*P)->getLocation(), |
4861 | DiagID: diag::warn_odr_objc_method_param_type_inconsistent) |
4862 | << D->isInstanceMethod() << Name |
4863 | << (*P)->getType() << (*FoundP)->getType(); |
4864 | Importer.ToDiag(Loc: (*FoundP)->getLocation(), DiagID: diag::note_odr_value_here) |
4865 | << (*FoundP)->getType(); |
4866 | |
4867 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
4868 | } |
4869 | } |
4870 | |
4871 | // Check variadic/non-variadic. |
4872 | // Check the number of parameters. |
4873 | if (D->isVariadic() != FoundMethod->isVariadic()) { |
4874 | Importer.ToDiag(Loc, DiagID: diag::warn_odr_objc_method_variadic_inconsistent) |
4875 | << D->isInstanceMethod() << Name; |
4876 | Importer.ToDiag(Loc: FoundMethod->getLocation(), |
4877 | DiagID: diag::note_odr_objc_method_here) |
4878 | << D->isInstanceMethod() << Name; |
4879 | |
4880 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
4881 | } |
4882 | |
4883 | // FIXME: Any other bits we need to merge? |
4884 | return Importer.MapImported(From: D, To: FoundMethod); |
4885 | } |
4886 | } |
4887 | |
4888 | Error Err = Error::success(); |
4889 | auto ToEndLoc = importChecked(Err, From: D->getEndLoc()); |
4890 | auto ToReturnType = importChecked(Err, From: D->getReturnType()); |
4891 | auto ToReturnTypeSourceInfo = |
4892 | importChecked(Err, From: D->getReturnTypeSourceInfo()); |
4893 | if (Err) |
4894 | return std::move(Err); |
4895 | |
4896 | ObjCMethodDecl *ToMethod; |
4897 | if (GetImportedOrCreateDecl( |
4898 | ToD&: ToMethod, FromD: D, args&: Importer.getToContext(), args&: Loc, args&: ToEndLoc, |
4899 | args: Name.getObjCSelector(), args&: ToReturnType, args&: ToReturnTypeSourceInfo, args&: DC, |
4900 | args: D->isInstanceMethod(), args: D->isVariadic(), args: D->isPropertyAccessor(), |
4901 | args: D->isSynthesizedAccessorStub(), args: D->isImplicit(), args: D->isDefined(), |
4902 | args: D->getImplementationControl(), args: D->hasRelatedResultType())) |
4903 | return ToMethod; |
4904 | |
4905 | // FIXME: When we decide to merge method definitions, we'll need to |
4906 | // deal with implicit parameters. |
4907 | |
4908 | // Import the parameters |
4909 | SmallVector<ParmVarDecl *, 5> ToParams; |
4910 | for (auto *FromP : D->parameters()) { |
4911 | if (Expected<ParmVarDecl *> ToPOrErr = import(From: FromP)) |
4912 | ToParams.push_back(Elt: *ToPOrErr); |
4913 | else |
4914 | return ToPOrErr.takeError(); |
4915 | } |
4916 | |
4917 | // Set the parameters. |
4918 | for (auto *ToParam : ToParams) { |
4919 | ToParam->setOwningFunction(ToMethod); |
4920 | ToMethod->addDeclInternal(D: ToParam); |
4921 | } |
4922 | |
4923 | SmallVector<SourceLocation, 12> FromSelLocs; |
4924 | D->getSelectorLocs(SelLocs&: FromSelLocs); |
4925 | SmallVector<SourceLocation, 12> ToSelLocs(FromSelLocs.size()); |
4926 | if (Error Err = ImportContainerChecked(InContainer: FromSelLocs, OutContainer&: ToSelLocs)) |
4927 | return std::move(Err); |
4928 | |
4929 | ToMethod->setMethodParams(C&: Importer.getToContext(), Params: ToParams, SelLocs: ToSelLocs); |
4930 | |
4931 | ToMethod->setLexicalDeclContext(LexicalDC); |
4932 | LexicalDC->addDeclInternal(D: ToMethod); |
4933 | |
4934 | // Implicit params are declared when Sema encounters the definition but this |
4935 | // never happens when the method is imported. Manually declare the implicit |
4936 | // params now that the MethodDecl knows its class interface. |
4937 | if (D->getSelfDecl()) |
4938 | ToMethod->createImplicitParams(Context&: Importer.getToContext(), |
4939 | ID: ToMethod->getClassInterface()); |
4940 | |
4941 | return ToMethod; |
4942 | } |
4943 | |
4944 | ExpectedDecl ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) { |
4945 | // Import the major distinguishing characteristics of a category. |
4946 | DeclContext *DC, *LexicalDC; |
4947 | DeclarationName Name; |
4948 | SourceLocation Loc; |
4949 | NamedDecl *ToD; |
4950 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
4951 | return std::move(Err); |
4952 | if (ToD) |
4953 | return ToD; |
4954 | |
4955 | Error Err = Error::success(); |
4956 | auto ToVarianceLoc = importChecked(Err, From: D->getVarianceLoc()); |
4957 | auto ToLocation = importChecked(Err, From: D->getLocation()); |
4958 | auto ToColonLoc = importChecked(Err, From: D->getColonLoc()); |
4959 | auto ToTypeSourceInfo = importChecked(Err, From: D->getTypeSourceInfo()); |
4960 | if (Err) |
4961 | return std::move(Err); |
4962 | |
4963 | ObjCTypeParamDecl *Result; |
4964 | if (GetImportedOrCreateDecl( |
4965 | ToD&: Result, FromD: D, args&: Importer.getToContext(), args&: DC, args: D->getVariance(), |
4966 | args&: ToVarianceLoc, args: D->getIndex(), |
4967 | args&: ToLocation, args: Name.getAsIdentifierInfo(), |
4968 | args&: ToColonLoc, args&: ToTypeSourceInfo)) |
4969 | return Result; |
4970 | |
4971 | // Only import 'ObjCTypeParamType' after the decl is created. |
4972 | auto ToTypeForDecl = importChecked(Err, From: D->getTypeForDecl()); |
4973 | if (Err) |
4974 | return std::move(Err); |
4975 | Result->setTypeForDecl(ToTypeForDecl); |
4976 | Result->setLexicalDeclContext(LexicalDC); |
4977 | return Result; |
4978 | } |
4979 | |
4980 | ExpectedDecl ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) { |
4981 | // Import the major distinguishing characteristics of a category. |
4982 | DeclContext *DC, *LexicalDC; |
4983 | DeclarationName Name; |
4984 | SourceLocation Loc; |
4985 | NamedDecl *ToD; |
4986 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
4987 | return std::move(Err); |
4988 | if (ToD) |
4989 | return ToD; |
4990 | |
4991 | ObjCInterfaceDecl *ToInterface; |
4992 | if (Error Err = importInto(To&: ToInterface, From: D->getClassInterface())) |
4993 | return std::move(Err); |
4994 | |
4995 | // Determine if we've already encountered this category. |
4996 | ObjCCategoryDecl *MergeWithCategory |
4997 | = ToInterface->FindCategoryDeclaration(CategoryId: Name.getAsIdentifierInfo()); |
4998 | ObjCCategoryDecl *ToCategory = MergeWithCategory; |
4999 | if (!ToCategory) { |
5000 | |
5001 | Error Err = Error::success(); |
5002 | auto ToAtStartLoc = importChecked(Err, From: D->getAtStartLoc()); |
5003 | auto ToCategoryNameLoc = importChecked(Err, From: D->getCategoryNameLoc()); |
5004 | auto ToIvarLBraceLoc = importChecked(Err, From: D->getIvarLBraceLoc()); |
5005 | auto ToIvarRBraceLoc = importChecked(Err, From: D->getIvarRBraceLoc()); |
5006 | if (Err) |
5007 | return std::move(Err); |
5008 | |
5009 | if (GetImportedOrCreateDecl(ToD&: ToCategory, FromD: D, args&: Importer.getToContext(), args&: DC, |
5010 | args&: ToAtStartLoc, args&: Loc, |
5011 | args&: ToCategoryNameLoc, |
5012 | args: Name.getAsIdentifierInfo(), args&: ToInterface, |
5013 | /*TypeParamList=*/args: nullptr, |
5014 | args&: ToIvarLBraceLoc, |
5015 | args&: ToIvarRBraceLoc)) |
5016 | return ToCategory; |
5017 | |
5018 | ToCategory->setLexicalDeclContext(LexicalDC); |
5019 | LexicalDC->addDeclInternal(D: ToCategory); |
5020 | // Import the type parameter list after MapImported, to avoid |
5021 | // loops when bringing in their DeclContext. |
5022 | if (auto PListOrErr = ImportObjCTypeParamList(list: D->getTypeParamList())) |
5023 | ToCategory->setTypeParamList(*PListOrErr); |
5024 | else |
5025 | return PListOrErr.takeError(); |
5026 | |
5027 | // Import protocols |
5028 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
5029 | SmallVector<SourceLocation, 4> ProtocolLocs; |
5030 | ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc |
5031 | = D->protocol_loc_begin(); |
5032 | for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(), |
5033 | FromProtoEnd = D->protocol_end(); |
5034 | FromProto != FromProtoEnd; |
5035 | ++FromProto, ++FromProtoLoc) { |
5036 | if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(From: *FromProto)) |
5037 | Protocols.push_back(Elt: *ToProtoOrErr); |
5038 | else |
5039 | return ToProtoOrErr.takeError(); |
5040 | |
5041 | if (ExpectedSLoc ToProtoLocOrErr = import(From: *FromProtoLoc)) |
5042 | ProtocolLocs.push_back(Elt: *ToProtoLocOrErr); |
5043 | else |
5044 | return ToProtoLocOrErr.takeError(); |
5045 | } |
5046 | |
5047 | // FIXME: If we're merging, make sure that the protocol list is the same. |
5048 | ToCategory->setProtocolList(List: Protocols.data(), Num: Protocols.size(), |
5049 | Locs: ProtocolLocs.data(), C&: Importer.getToContext()); |
5050 | |
5051 | } else { |
5052 | Importer.MapImported(From: D, To: ToCategory); |
5053 | } |
5054 | |
5055 | // Import all of the members of this category. |
5056 | if (Error Err = ImportDeclContext(FromDC: D)) |
5057 | return std::move(Err); |
5058 | |
5059 | // If we have an implementation, import it as well. |
5060 | if (D->getImplementation()) { |
5061 | if (Expected<ObjCCategoryImplDecl *> ToImplOrErr = |
5062 | import(From: D->getImplementation())) |
5063 | ToCategory->setImplementation(*ToImplOrErr); |
5064 | else |
5065 | return ToImplOrErr.takeError(); |
5066 | } |
5067 | |
5068 | return ToCategory; |
5069 | } |
5070 | |
5071 | Error ASTNodeImporter::ImportDefinition( |
5072 | ObjCProtocolDecl *From, ObjCProtocolDecl *To, ImportDefinitionKind Kind) { |
5073 | if (To->getDefinition()) { |
5074 | if (shouldForceImportDeclContext(IDK: Kind)) |
5075 | if (Error Err = ImportDeclContext(FromDC: From)) |
5076 | return Err; |
5077 | return Error::success(); |
5078 | } |
5079 | |
5080 | // Start the protocol definition |
5081 | To->startDefinition(); |
5082 | |
5083 | // Import protocols |
5084 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
5085 | SmallVector<SourceLocation, 4> ProtocolLocs; |
5086 | ObjCProtocolDecl::protocol_loc_iterator FromProtoLoc = |
5087 | From->protocol_loc_begin(); |
5088 | for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(), |
5089 | FromProtoEnd = From->protocol_end(); |
5090 | FromProto != FromProtoEnd; |
5091 | ++FromProto, ++FromProtoLoc) { |
5092 | if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(From: *FromProto)) |
5093 | Protocols.push_back(Elt: *ToProtoOrErr); |
5094 | else |
5095 | return ToProtoOrErr.takeError(); |
5096 | |
5097 | if (ExpectedSLoc ToProtoLocOrErr = import(From: *FromProtoLoc)) |
5098 | ProtocolLocs.push_back(Elt: *ToProtoLocOrErr); |
5099 | else |
5100 | return ToProtoLocOrErr.takeError(); |
5101 | |
5102 | } |
5103 | |
5104 | // FIXME: If we're merging, make sure that the protocol list is the same. |
5105 | To->setProtocolList(List: Protocols.data(), Num: Protocols.size(), |
5106 | Locs: ProtocolLocs.data(), C&: Importer.getToContext()); |
5107 | |
5108 | if (shouldForceImportDeclContext(IDK: Kind)) { |
5109 | // Import all of the members of this protocol. |
5110 | if (Error Err = ImportDeclContext(FromDC: From, /*ForceImport=*/true)) |
5111 | return Err; |
5112 | } |
5113 | return Error::success(); |
5114 | } |
5115 | |
5116 | ExpectedDecl ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) { |
5117 | // If this protocol has a definition in the translation unit we're coming |
5118 | // from, but this particular declaration is not that definition, import the |
5119 | // definition and map to that. |
5120 | ObjCProtocolDecl *Definition = D->getDefinition(); |
5121 | if (Definition && Definition != D) { |
5122 | if (ExpectedDecl ImportedDefOrErr = import(From: Definition)) |
5123 | return Importer.MapImported(From: D, To: *ImportedDefOrErr); |
5124 | else |
5125 | return ImportedDefOrErr.takeError(); |
5126 | } |
5127 | |
5128 | // Import the major distinguishing characteristics of a protocol. |
5129 | DeclContext *DC, *LexicalDC; |
5130 | DeclarationName Name; |
5131 | SourceLocation Loc; |
5132 | NamedDecl *ToD; |
5133 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
5134 | return std::move(Err); |
5135 | if (ToD) |
5136 | return ToD; |
5137 | |
5138 | ObjCProtocolDecl *MergeWithProtocol = nullptr; |
5139 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
5140 | for (auto *FoundDecl : FoundDecls) { |
5141 | if (!FoundDecl->isInIdentifierNamespace(NS: Decl::IDNS_ObjCProtocol)) |
5142 | continue; |
5143 | |
5144 | if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(Val: FoundDecl))) |
5145 | break; |
5146 | } |
5147 | |
5148 | ObjCProtocolDecl *ToProto = MergeWithProtocol; |
5149 | if (!ToProto) { |
5150 | auto ToAtBeginLocOrErr = import(From: D->getAtStartLoc()); |
5151 | if (!ToAtBeginLocOrErr) |
5152 | return ToAtBeginLocOrErr.takeError(); |
5153 | |
5154 | if (GetImportedOrCreateDecl(ToD&: ToProto, FromD: D, args&: Importer.getToContext(), args&: DC, |
5155 | args: Name.getAsIdentifierInfo(), args&: Loc, |
5156 | args&: *ToAtBeginLocOrErr, |
5157 | /*PrevDecl=*/args: nullptr)) |
5158 | return ToProto; |
5159 | ToProto->setLexicalDeclContext(LexicalDC); |
5160 | LexicalDC->addDeclInternal(D: ToProto); |
5161 | } |
5162 | |
5163 | Importer.MapImported(From: D, To: ToProto); |
5164 | |
5165 | if (D->isThisDeclarationADefinition()) |
5166 | if (Error Err = ImportDefinition(From: D, To: ToProto)) |
5167 | return std::move(Err); |
5168 | |
5169 | return ToProto; |
5170 | } |
5171 | |
5172 | ExpectedDecl ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
5173 | DeclContext *DC, *LexicalDC; |
5174 | if (Error Err = ImportDeclContext(FromD: D, ToDC&: DC, ToLexicalDC&: LexicalDC)) |
5175 | return std::move(Err); |
5176 | |
5177 | ExpectedSLoc ExternLocOrErr = import(From: D->getExternLoc()); |
5178 | if (!ExternLocOrErr) |
5179 | return ExternLocOrErr.takeError(); |
5180 | |
5181 | ExpectedSLoc LangLocOrErr = import(From: D->getLocation()); |
5182 | if (!LangLocOrErr) |
5183 | return LangLocOrErr.takeError(); |
5184 | |
5185 | bool HasBraces = D->hasBraces(); |
5186 | |
5187 | LinkageSpecDecl *ToLinkageSpec; |
5188 | if (GetImportedOrCreateDecl(ToD&: ToLinkageSpec, FromD: D, args&: Importer.getToContext(), args&: DC, |
5189 | args&: *ExternLocOrErr, args&: *LangLocOrErr, |
5190 | args: D->getLanguage(), args&: HasBraces)) |
5191 | return ToLinkageSpec; |
5192 | |
5193 | if (HasBraces) { |
5194 | ExpectedSLoc RBraceLocOrErr = import(From: D->getRBraceLoc()); |
5195 | if (!RBraceLocOrErr) |
5196 | return RBraceLocOrErr.takeError(); |
5197 | ToLinkageSpec->setRBraceLoc(*RBraceLocOrErr); |
5198 | } |
5199 | |
5200 | ToLinkageSpec->setLexicalDeclContext(LexicalDC); |
5201 | LexicalDC->addDeclInternal(D: ToLinkageSpec); |
5202 | |
5203 | return ToLinkageSpec; |
5204 | } |
5205 | |
5206 | ExpectedDecl ASTNodeImporter::ImportUsingShadowDecls(BaseUsingDecl *D, |
5207 | BaseUsingDecl *ToSI) { |
5208 | for (UsingShadowDecl *FromShadow : D->shadows()) { |
5209 | if (Expected<UsingShadowDecl *> ToShadowOrErr = import(From: FromShadow)) |
5210 | ToSI->addShadowDecl(S: *ToShadowOrErr); |
5211 | else |
5212 | // FIXME: We return error here but the definition is already created |
5213 | // and available with lookups. How to fix this?.. |
5214 | return ToShadowOrErr.takeError(); |
5215 | } |
5216 | return ToSI; |
5217 | } |
5218 | |
5219 | ExpectedDecl ASTNodeImporter::VisitUsingDecl(UsingDecl *D) { |
5220 | DeclContext *DC, *LexicalDC; |
5221 | DeclarationName Name; |
5222 | SourceLocation Loc; |
5223 | NamedDecl *ToD = nullptr; |
5224 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
5225 | return std::move(Err); |
5226 | if (ToD) |
5227 | return ToD; |
5228 | |
5229 | Error Err = Error::success(); |
5230 | auto ToLoc = importChecked(Err, From: D->getNameInfo().getLoc()); |
5231 | auto ToUsingLoc = importChecked(Err, From: D->getUsingLoc()); |
5232 | auto ToQualifierLoc = importChecked(Err, From: D->getQualifierLoc()); |
5233 | if (Err) |
5234 | return std::move(Err); |
5235 | |
5236 | DeclarationNameInfo NameInfo(Name, ToLoc); |
5237 | if (Error Err = ImportDeclarationNameLoc(From: D->getNameInfo(), To&: NameInfo)) |
5238 | return std::move(Err); |
5239 | |
5240 | UsingDecl *ToUsing; |
5241 | if (GetImportedOrCreateDecl(ToD&: ToUsing, FromD: D, args&: Importer.getToContext(), args&: DC, |
5242 | args&: ToUsingLoc, args&: ToQualifierLoc, args&: NameInfo, |
5243 | args: D->hasTypename())) |
5244 | return ToUsing; |
5245 | |
5246 | ToUsing->setLexicalDeclContext(LexicalDC); |
5247 | LexicalDC->addDeclInternal(D: ToUsing); |
5248 | |
5249 | if (NamedDecl *FromPattern = |
5250 | Importer.getFromContext().getInstantiatedFromUsingDecl(Inst: D)) { |
5251 | if (Expected<NamedDecl *> ToPatternOrErr = import(From: FromPattern)) |
5252 | Importer.getToContext().setInstantiatedFromUsingDecl( |
5253 | Inst: ToUsing, Pattern: *ToPatternOrErr); |
5254 | else |
5255 | return ToPatternOrErr.takeError(); |
5256 | } |
5257 | |
5258 | return ImportUsingShadowDecls(D, ToSI: ToUsing); |
5259 | } |
5260 | |
5261 | ExpectedDecl ASTNodeImporter::VisitUsingEnumDecl(UsingEnumDecl *D) { |
5262 | DeclContext *DC, *LexicalDC; |
5263 | DeclarationName Name; |
5264 | SourceLocation Loc; |
5265 | NamedDecl *ToD = nullptr; |
5266 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
5267 | return std::move(Err); |
5268 | if (ToD) |
5269 | return ToD; |
5270 | |
5271 | Error Err = Error::success(); |
5272 | auto ToUsingLoc = importChecked(Err, From: D->getUsingLoc()); |
5273 | auto ToEnumLoc = importChecked(Err, From: D->getEnumLoc()); |
5274 | auto ToNameLoc = importChecked(Err, From: D->getLocation()); |
5275 | auto *ToEnumType = importChecked(Err, From: D->getEnumType()); |
5276 | if (Err) |
5277 | return std::move(Err); |
5278 | |
5279 | UsingEnumDecl *ToUsingEnum; |
5280 | if (GetImportedOrCreateDecl(ToD&: ToUsingEnum, FromD: D, args&: Importer.getToContext(), args&: DC, |
5281 | args&: ToUsingLoc, args&: ToEnumLoc, args&: ToNameLoc, args&: ToEnumType)) |
5282 | return ToUsingEnum; |
5283 | |
5284 | ToUsingEnum->setLexicalDeclContext(LexicalDC); |
5285 | LexicalDC->addDeclInternal(D: ToUsingEnum); |
5286 | |
5287 | if (UsingEnumDecl *FromPattern = |
5288 | Importer.getFromContext().getInstantiatedFromUsingEnumDecl(Inst: D)) { |
5289 | if (Expected<UsingEnumDecl *> ToPatternOrErr = import(From: FromPattern)) |
5290 | Importer.getToContext().setInstantiatedFromUsingEnumDecl(Inst: ToUsingEnum, |
5291 | Pattern: *ToPatternOrErr); |
5292 | else |
5293 | return ToPatternOrErr.takeError(); |
5294 | } |
5295 | |
5296 | return ImportUsingShadowDecls(D, ToSI: ToUsingEnum); |
5297 | } |
5298 | |
5299 | ExpectedDecl ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) { |
5300 | DeclContext *DC, *LexicalDC; |
5301 | DeclarationName Name; |
5302 | SourceLocation Loc; |
5303 | NamedDecl *ToD = nullptr; |
5304 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
5305 | return std::move(Err); |
5306 | if (ToD) |
5307 | return ToD; |
5308 | |
5309 | Expected<BaseUsingDecl *> ToIntroducerOrErr = import(From: D->getIntroducer()); |
5310 | if (!ToIntroducerOrErr) |
5311 | return ToIntroducerOrErr.takeError(); |
5312 | |
5313 | Expected<NamedDecl *> ToTargetOrErr = import(From: D->getTargetDecl()); |
5314 | if (!ToTargetOrErr) |
5315 | return ToTargetOrErr.takeError(); |
5316 | |
5317 | UsingShadowDecl *ToShadow; |
5318 | if (auto *FromConstructorUsingShadow = |
5319 | dyn_cast<ConstructorUsingShadowDecl>(Val: D)) { |
5320 | Error Err = Error::success(); |
5321 | ConstructorUsingShadowDecl *Nominated = importChecked( |
5322 | Err, From: FromConstructorUsingShadow->getNominatedBaseClassShadowDecl()); |
5323 | if (Err) |
5324 | return std::move(Err); |
5325 | // The 'Target' parameter of ConstructorUsingShadowDecl constructor |
5326 | // is really the "NominatedBaseClassShadowDecl" value if it exists |
5327 | // (see code of ConstructorUsingShadowDecl::ConstructorUsingShadowDecl). |
5328 | // We should pass the NominatedBaseClassShadowDecl to it (if non-null) to |
5329 | // get the correct values. |
5330 | if (GetImportedOrCreateDecl<ConstructorUsingShadowDecl>( |
5331 | ToD&: ToShadow, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, |
5332 | args: cast<UsingDecl>(Val: *ToIntroducerOrErr), |
5333 | args: Nominated ? Nominated : *ToTargetOrErr, |
5334 | args: FromConstructorUsingShadow->constructsVirtualBase())) |
5335 | return ToShadow; |
5336 | } else { |
5337 | if (GetImportedOrCreateDecl(ToD&: ToShadow, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, |
5338 | args&: Name, args&: *ToIntroducerOrErr, args&: *ToTargetOrErr)) |
5339 | return ToShadow; |
5340 | } |
5341 | |
5342 | ToShadow->setLexicalDeclContext(LexicalDC); |
5343 | ToShadow->setAccess(D->getAccess()); |
5344 | |
5345 | if (UsingShadowDecl *FromPattern = |
5346 | Importer.getFromContext().getInstantiatedFromUsingShadowDecl(Inst: D)) { |
5347 | if (Expected<UsingShadowDecl *> ToPatternOrErr = import(From: FromPattern)) |
5348 | Importer.getToContext().setInstantiatedFromUsingShadowDecl( |
5349 | Inst: ToShadow, Pattern: *ToPatternOrErr); |
5350 | else |
5351 | // FIXME: We return error here but the definition is already created |
5352 | // and available with lookups. How to fix this?.. |
5353 | return ToPatternOrErr.takeError(); |
5354 | } |
5355 | |
5356 | LexicalDC->addDeclInternal(D: ToShadow); |
5357 | |
5358 | return ToShadow; |
5359 | } |
5360 | |
5361 | ExpectedDecl ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
5362 | DeclContext *DC, *LexicalDC; |
5363 | DeclarationName Name; |
5364 | SourceLocation Loc; |
5365 | NamedDecl *ToD = nullptr; |
5366 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
5367 | return std::move(Err); |
5368 | if (ToD) |
5369 | return ToD; |
5370 | |
5371 | auto ToComAncestorOrErr = Importer.ImportContext(FromDC: D->getCommonAncestor()); |
5372 | if (!ToComAncestorOrErr) |
5373 | return ToComAncestorOrErr.takeError(); |
5374 | |
5375 | Error Err = Error::success(); |
5376 | auto ToNominatedNamespace = importChecked(Err, From: D->getNominatedNamespace()); |
5377 | auto ToUsingLoc = importChecked(Err, From: D->getUsingLoc()); |
5378 | auto ToNamespaceKeyLocation = |
5379 | importChecked(Err, From: D->getNamespaceKeyLocation()); |
5380 | auto ToQualifierLoc = importChecked(Err, From: D->getQualifierLoc()); |
5381 | auto ToIdentLocation = importChecked(Err, From: D->getIdentLocation()); |
5382 | if (Err) |
5383 | return std::move(Err); |
5384 | |
5385 | UsingDirectiveDecl *ToUsingDir; |
5386 | if (GetImportedOrCreateDecl(ToD&: ToUsingDir, FromD: D, args&: Importer.getToContext(), args&: DC, |
5387 | args&: ToUsingLoc, |
5388 | args&: ToNamespaceKeyLocation, |
5389 | args&: ToQualifierLoc, |
5390 | args&: ToIdentLocation, |
5391 | args&: ToNominatedNamespace, args&: *ToComAncestorOrErr)) |
5392 | return ToUsingDir; |
5393 | |
5394 | ToUsingDir->setLexicalDeclContext(LexicalDC); |
5395 | LexicalDC->addDeclInternal(D: ToUsingDir); |
5396 | |
5397 | return ToUsingDir; |
5398 | } |
5399 | |
5400 | ExpectedDecl ASTNodeImporter::VisitUsingPackDecl(UsingPackDecl *D) { |
5401 | DeclContext *DC, *LexicalDC; |
5402 | DeclarationName Name; |
5403 | SourceLocation Loc; |
5404 | NamedDecl *ToD = nullptr; |
5405 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
5406 | return std::move(Err); |
5407 | if (ToD) |
5408 | return ToD; |
5409 | |
5410 | auto ToInstantiatedFromUsingOrErr = |
5411 | Importer.Import(FromD: D->getInstantiatedFromUsingDecl()); |
5412 | if (!ToInstantiatedFromUsingOrErr) |
5413 | return ToInstantiatedFromUsingOrErr.takeError(); |
5414 | SmallVector<NamedDecl *, 4> Expansions(D->expansions().size()); |
5415 | if (Error Err = ImportArrayChecked(InContainer: D->expansions(), Obegin: Expansions.begin())) |
5416 | return std::move(Err); |
5417 | |
5418 | UsingPackDecl *ToUsingPack; |
5419 | if (GetImportedOrCreateDecl(ToD&: ToUsingPack, FromD: D, args&: Importer.getToContext(), args&: DC, |
5420 | args: cast<NamedDecl>(Val: *ToInstantiatedFromUsingOrErr), |
5421 | args&: Expansions)) |
5422 | return ToUsingPack; |
5423 | |
5424 | addDeclToContexts(FromD: D, ToD: ToUsingPack); |
5425 | |
5426 | return ToUsingPack; |
5427 | } |
5428 | |
5429 | ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingValueDecl( |
5430 | UnresolvedUsingValueDecl *D) { |
5431 | DeclContext *DC, *LexicalDC; |
5432 | DeclarationName Name; |
5433 | SourceLocation Loc; |
5434 | NamedDecl *ToD = nullptr; |
5435 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
5436 | return std::move(Err); |
5437 | if (ToD) |
5438 | return ToD; |
5439 | |
5440 | Error Err = Error::success(); |
5441 | auto ToLoc = importChecked(Err, From: D->getNameInfo().getLoc()); |
5442 | auto ToUsingLoc = importChecked(Err, From: D->getUsingLoc()); |
5443 | auto ToQualifierLoc = importChecked(Err, From: D->getQualifierLoc()); |
5444 | auto ToEllipsisLoc = importChecked(Err, From: D->getEllipsisLoc()); |
5445 | if (Err) |
5446 | return std::move(Err); |
5447 | |
5448 | DeclarationNameInfo NameInfo(Name, ToLoc); |
5449 | if (Error Err = ImportDeclarationNameLoc(From: D->getNameInfo(), To&: NameInfo)) |
5450 | return std::move(Err); |
5451 | |
5452 | UnresolvedUsingValueDecl *ToUsingValue; |
5453 | if (GetImportedOrCreateDecl(ToD&: ToUsingValue, FromD: D, args&: Importer.getToContext(), args&: DC, |
5454 | args&: ToUsingLoc, args&: ToQualifierLoc, args&: NameInfo, |
5455 | args&: ToEllipsisLoc)) |
5456 | return ToUsingValue; |
5457 | |
5458 | ToUsingValue->setAccess(D->getAccess()); |
5459 | ToUsingValue->setLexicalDeclContext(LexicalDC); |
5460 | LexicalDC->addDeclInternal(D: ToUsingValue); |
5461 | |
5462 | return ToUsingValue; |
5463 | } |
5464 | |
5465 | ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingTypenameDecl( |
5466 | UnresolvedUsingTypenameDecl *D) { |
5467 | DeclContext *DC, *LexicalDC; |
5468 | DeclarationName Name; |
5469 | SourceLocation Loc; |
5470 | NamedDecl *ToD = nullptr; |
5471 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
5472 | return std::move(Err); |
5473 | if (ToD) |
5474 | return ToD; |
5475 | |
5476 | Error Err = Error::success(); |
5477 | auto ToUsingLoc = importChecked(Err, From: D->getUsingLoc()); |
5478 | auto ToTypenameLoc = importChecked(Err, From: D->getTypenameLoc()); |
5479 | auto ToQualifierLoc = importChecked(Err, From: D->getQualifierLoc()); |
5480 | auto ToEllipsisLoc = importChecked(Err, From: D->getEllipsisLoc()); |
5481 | if (Err) |
5482 | return std::move(Err); |
5483 | |
5484 | UnresolvedUsingTypenameDecl *ToUsing; |
5485 | if (GetImportedOrCreateDecl(ToD&: ToUsing, FromD: D, args&: Importer.getToContext(), args&: DC, |
5486 | args&: ToUsingLoc, args&: ToTypenameLoc, |
5487 | args&: ToQualifierLoc, args&: Loc, args&: Name, args&: ToEllipsisLoc)) |
5488 | return ToUsing; |
5489 | |
5490 | ToUsing->setAccess(D->getAccess()); |
5491 | ToUsing->setLexicalDeclContext(LexicalDC); |
5492 | LexicalDC->addDeclInternal(D: ToUsing); |
5493 | |
5494 | return ToUsing; |
5495 | } |
5496 | |
5497 | ExpectedDecl ASTNodeImporter::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) { |
5498 | Decl* ToD = nullptr; |
5499 | switch (D->getBuiltinTemplateKind()) { |
5500 | #define BuiltinTemplate(BTName) \ |
5501 | case BuiltinTemplateKind::BTK##BTName: \ |
5502 | ToD = Importer.getToContext().get##BTName##Decl(); \ |
5503 | break; |
5504 | #include "clang/Basic/BuiltinTemplates.inc" |
5505 | } |
5506 | assert(ToD && "BuiltinTemplateDecl of unsupported kind!" ); |
5507 | Importer.MapImported(From: D, To: ToD); |
5508 | return ToD; |
5509 | } |
5510 | |
5511 | Error ASTNodeImporter::ImportDefinition( |
5512 | ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, ImportDefinitionKind Kind) { |
5513 | if (To->getDefinition()) { |
5514 | // Check consistency of superclass. |
5515 | ObjCInterfaceDecl *FromSuper = From->getSuperClass(); |
5516 | if (FromSuper) { |
5517 | if (auto FromSuperOrErr = import(From: FromSuper)) |
5518 | FromSuper = *FromSuperOrErr; |
5519 | else |
5520 | return FromSuperOrErr.takeError(); |
5521 | } |
5522 | |
5523 | ObjCInterfaceDecl *ToSuper = To->getSuperClass(); |
5524 | if ((bool)FromSuper != (bool)ToSuper || |
5525 | (FromSuper && !declaresSameEntity(D1: FromSuper, D2: ToSuper))) { |
5526 | Importer.ToDiag(Loc: To->getLocation(), |
5527 | DiagID: diag::warn_odr_objc_superclass_inconsistent) |
5528 | << To->getDeclName(); |
5529 | if (ToSuper) |
5530 | Importer.ToDiag(Loc: To->getSuperClassLoc(), DiagID: diag::note_odr_objc_superclass) |
5531 | << To->getSuperClass()->getDeclName(); |
5532 | else |
5533 | Importer.ToDiag(Loc: To->getLocation(), |
5534 | DiagID: diag::note_odr_objc_missing_superclass); |
5535 | if (From->getSuperClass()) |
5536 | Importer.FromDiag(Loc: From->getSuperClassLoc(), |
5537 | DiagID: diag::note_odr_objc_superclass) |
5538 | << From->getSuperClass()->getDeclName(); |
5539 | else |
5540 | Importer.FromDiag(Loc: From->getLocation(), |
5541 | DiagID: diag::note_odr_objc_missing_superclass); |
5542 | } |
5543 | |
5544 | if (shouldForceImportDeclContext(IDK: Kind)) |
5545 | if (Error Err = ImportDeclContext(FromDC: From)) |
5546 | return Err; |
5547 | return Error::success(); |
5548 | } |
5549 | |
5550 | // Start the definition. |
5551 | To->startDefinition(); |
5552 | |
5553 | // If this class has a superclass, import it. |
5554 | if (From->getSuperClass()) { |
5555 | if (auto SuperTInfoOrErr = import(From: From->getSuperClassTInfo())) |
5556 | To->setSuperClass(*SuperTInfoOrErr); |
5557 | else |
5558 | return SuperTInfoOrErr.takeError(); |
5559 | } |
5560 | |
5561 | // Import protocols |
5562 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
5563 | SmallVector<SourceLocation, 4> ProtocolLocs; |
5564 | ObjCInterfaceDecl::protocol_loc_iterator FromProtoLoc = |
5565 | From->protocol_loc_begin(); |
5566 | |
5567 | for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(), |
5568 | FromProtoEnd = From->protocol_end(); |
5569 | FromProto != FromProtoEnd; |
5570 | ++FromProto, ++FromProtoLoc) { |
5571 | if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(From: *FromProto)) |
5572 | Protocols.push_back(Elt: *ToProtoOrErr); |
5573 | else |
5574 | return ToProtoOrErr.takeError(); |
5575 | |
5576 | if (ExpectedSLoc ToProtoLocOrErr = import(From: *FromProtoLoc)) |
5577 | ProtocolLocs.push_back(Elt: *ToProtoLocOrErr); |
5578 | else |
5579 | return ToProtoLocOrErr.takeError(); |
5580 | |
5581 | } |
5582 | |
5583 | // FIXME: If we're merging, make sure that the protocol list is the same. |
5584 | To->setProtocolList(List: Protocols.data(), Num: Protocols.size(), |
5585 | Locs: ProtocolLocs.data(), C&: Importer.getToContext()); |
5586 | |
5587 | // Import categories. When the categories themselves are imported, they'll |
5588 | // hook themselves into this interface. |
5589 | for (auto *Cat : From->known_categories()) { |
5590 | auto ToCatOrErr = import(From: Cat); |
5591 | if (!ToCatOrErr) |
5592 | return ToCatOrErr.takeError(); |
5593 | } |
5594 | |
5595 | // If we have an @implementation, import it as well. |
5596 | if (From->getImplementation()) { |
5597 | if (Expected<ObjCImplementationDecl *> ToImplOrErr = |
5598 | import(From: From->getImplementation())) |
5599 | To->setImplementation(*ToImplOrErr); |
5600 | else |
5601 | return ToImplOrErr.takeError(); |
5602 | } |
5603 | |
5604 | // Import all of the members of this class. |
5605 | if (Error Err = ImportDeclContext(FromDC: From, /*ForceImport=*/true)) |
5606 | return Err; |
5607 | |
5608 | return Error::success(); |
5609 | } |
5610 | |
5611 | Expected<ObjCTypeParamList *> |
5612 | ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) { |
5613 | if (!list) |
5614 | return nullptr; |
5615 | |
5616 | SmallVector<ObjCTypeParamDecl *, 4> toTypeParams; |
5617 | for (auto *fromTypeParam : *list) { |
5618 | if (auto toTypeParamOrErr = import(From: fromTypeParam)) |
5619 | toTypeParams.push_back(Elt: *toTypeParamOrErr); |
5620 | else |
5621 | return toTypeParamOrErr.takeError(); |
5622 | } |
5623 | |
5624 | auto LAngleLocOrErr = import(From: list->getLAngleLoc()); |
5625 | if (!LAngleLocOrErr) |
5626 | return LAngleLocOrErr.takeError(); |
5627 | |
5628 | auto RAngleLocOrErr = import(From: list->getRAngleLoc()); |
5629 | if (!RAngleLocOrErr) |
5630 | return RAngleLocOrErr.takeError(); |
5631 | |
5632 | return ObjCTypeParamList::create(ctx&: Importer.getToContext(), |
5633 | lAngleLoc: *LAngleLocOrErr, |
5634 | typeParams: toTypeParams, |
5635 | rAngleLoc: *RAngleLocOrErr); |
5636 | } |
5637 | |
5638 | ExpectedDecl ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
5639 | // If this class has a definition in the translation unit we're coming from, |
5640 | // but this particular declaration is not that definition, import the |
5641 | // definition and map to that. |
5642 | ObjCInterfaceDecl *Definition = D->getDefinition(); |
5643 | if (Definition && Definition != D) { |
5644 | if (ExpectedDecl ImportedDefOrErr = import(From: Definition)) |
5645 | return Importer.MapImported(From: D, To: *ImportedDefOrErr); |
5646 | else |
5647 | return ImportedDefOrErr.takeError(); |
5648 | } |
5649 | |
5650 | // Import the major distinguishing characteristics of an @interface. |
5651 | DeclContext *DC, *LexicalDC; |
5652 | DeclarationName Name; |
5653 | SourceLocation Loc; |
5654 | NamedDecl *ToD; |
5655 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
5656 | return std::move(Err); |
5657 | if (ToD) |
5658 | return ToD; |
5659 | |
5660 | // Look for an existing interface with the same name. |
5661 | ObjCInterfaceDecl *MergeWithIface = nullptr; |
5662 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
5663 | for (auto *FoundDecl : FoundDecls) { |
5664 | if (!FoundDecl->isInIdentifierNamespace(NS: Decl::IDNS_Ordinary)) |
5665 | continue; |
5666 | |
5667 | if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(Val: FoundDecl))) |
5668 | break; |
5669 | } |
5670 | |
5671 | // Create an interface declaration, if one does not already exist. |
5672 | ObjCInterfaceDecl *ToIface = MergeWithIface; |
5673 | if (!ToIface) { |
5674 | ExpectedSLoc AtBeginLocOrErr = import(From: D->getAtStartLoc()); |
5675 | if (!AtBeginLocOrErr) |
5676 | return AtBeginLocOrErr.takeError(); |
5677 | |
5678 | if (GetImportedOrCreateDecl( |
5679 | ToD&: ToIface, FromD: D, args&: Importer.getToContext(), args&: DC, |
5680 | args&: *AtBeginLocOrErr, args: Name.getAsIdentifierInfo(), |
5681 | /*TypeParamList=*/args: nullptr, |
5682 | /*PrevDecl=*/args: nullptr, args&: Loc, args: D->isImplicitInterfaceDecl())) |
5683 | return ToIface; |
5684 | ToIface->setLexicalDeclContext(LexicalDC); |
5685 | LexicalDC->addDeclInternal(D: ToIface); |
5686 | } |
5687 | Importer.MapImported(From: D, To: ToIface); |
5688 | // Import the type parameter list after MapImported, to avoid |
5689 | // loops when bringing in their DeclContext. |
5690 | if (auto ToPListOrErr = |
5691 | ImportObjCTypeParamList(list: D->getTypeParamListAsWritten())) |
5692 | ToIface->setTypeParamList(*ToPListOrErr); |
5693 | else |
5694 | return ToPListOrErr.takeError(); |
5695 | |
5696 | if (D->isThisDeclarationADefinition()) |
5697 | if (Error Err = ImportDefinition(From: D, To: ToIface)) |
5698 | return std::move(Err); |
5699 | |
5700 | return ToIface; |
5701 | } |
5702 | |
5703 | ExpectedDecl |
5704 | ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
5705 | ObjCCategoryDecl *Category; |
5706 | if (Error Err = importInto(To&: Category, From: D->getCategoryDecl())) |
5707 | return std::move(Err); |
5708 | |
5709 | ObjCCategoryImplDecl *ToImpl = Category->getImplementation(); |
5710 | if (!ToImpl) { |
5711 | DeclContext *DC, *LexicalDC; |
5712 | if (Error Err = ImportDeclContext(FromD: D, ToDC&: DC, ToLexicalDC&: LexicalDC)) |
5713 | return std::move(Err); |
5714 | |
5715 | Error Err = Error::success(); |
5716 | auto ToLocation = importChecked(Err, From: D->getLocation()); |
5717 | auto ToAtStartLoc = importChecked(Err, From: D->getAtStartLoc()); |
5718 | auto ToCategoryNameLoc = importChecked(Err, From: D->getCategoryNameLoc()); |
5719 | if (Err) |
5720 | return std::move(Err); |
5721 | |
5722 | if (GetImportedOrCreateDecl( |
5723 | ToD&: ToImpl, FromD: D, args&: Importer.getToContext(), args&: DC, |
5724 | args: Importer.Import(FromId: D->getIdentifier()), args: Category->getClassInterface(), |
5725 | args&: ToLocation, args&: ToAtStartLoc, args&: ToCategoryNameLoc)) |
5726 | return ToImpl; |
5727 | |
5728 | ToImpl->setLexicalDeclContext(LexicalDC); |
5729 | LexicalDC->addDeclInternal(D: ToImpl); |
5730 | Category->setImplementation(ToImpl); |
5731 | } |
5732 | |
5733 | Importer.MapImported(From: D, To: ToImpl); |
5734 | if (Error Err = ImportDeclContext(FromDC: D)) |
5735 | return std::move(Err); |
5736 | |
5737 | return ToImpl; |
5738 | } |
5739 | |
5740 | ExpectedDecl |
5741 | ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
5742 | // Find the corresponding interface. |
5743 | ObjCInterfaceDecl *Iface; |
5744 | if (Error Err = importInto(To&: Iface, From: D->getClassInterface())) |
5745 | return std::move(Err); |
5746 | |
5747 | // Import the superclass, if any. |
5748 | ObjCInterfaceDecl *Super; |
5749 | if (Error Err = importInto(To&: Super, From: D->getSuperClass())) |
5750 | return std::move(Err); |
5751 | |
5752 | ObjCImplementationDecl *Impl = Iface->getImplementation(); |
5753 | if (!Impl) { |
5754 | // We haven't imported an implementation yet. Create a new @implementation |
5755 | // now. |
5756 | DeclContext *DC, *LexicalDC; |
5757 | if (Error Err = ImportDeclContext(FromD: D, ToDC&: DC, ToLexicalDC&: LexicalDC)) |
5758 | return std::move(Err); |
5759 | |
5760 | Error Err = Error::success(); |
5761 | auto ToLocation = importChecked(Err, From: D->getLocation()); |
5762 | auto ToAtStartLoc = importChecked(Err, From: D->getAtStartLoc()); |
5763 | auto ToSuperClassLoc = importChecked(Err, From: D->getSuperClassLoc()); |
5764 | auto ToIvarLBraceLoc = importChecked(Err, From: D->getIvarLBraceLoc()); |
5765 | auto ToIvarRBraceLoc = importChecked(Err, From: D->getIvarRBraceLoc()); |
5766 | if (Err) |
5767 | return std::move(Err); |
5768 | |
5769 | if (GetImportedOrCreateDecl(ToD&: Impl, FromD: D, args&: Importer.getToContext(), |
5770 | args&: DC, args&: Iface, args&: Super, |
5771 | args&: ToLocation, |
5772 | args&: ToAtStartLoc, |
5773 | args&: ToSuperClassLoc, |
5774 | args&: ToIvarLBraceLoc, |
5775 | args&: ToIvarRBraceLoc)) |
5776 | return Impl; |
5777 | |
5778 | Impl->setLexicalDeclContext(LexicalDC); |
5779 | |
5780 | // Associate the implementation with the class it implements. |
5781 | Iface->setImplementation(Impl); |
5782 | Importer.MapImported(From: D, To: Iface->getImplementation()); |
5783 | } else { |
5784 | Importer.MapImported(From: D, To: Iface->getImplementation()); |
5785 | |
5786 | // Verify that the existing @implementation has the same superclass. |
5787 | if ((Super && !Impl->getSuperClass()) || |
5788 | (!Super && Impl->getSuperClass()) || |
5789 | (Super && Impl->getSuperClass() && |
5790 | !declaresSameEntity(D1: Super->getCanonicalDecl(), |
5791 | D2: Impl->getSuperClass()))) { |
5792 | Importer.ToDiag(Loc: Impl->getLocation(), |
5793 | DiagID: diag::warn_odr_objc_superclass_inconsistent) |
5794 | << Iface->getDeclName(); |
5795 | // FIXME: It would be nice to have the location of the superclass |
5796 | // below. |
5797 | if (Impl->getSuperClass()) |
5798 | Importer.ToDiag(Loc: Impl->getLocation(), |
5799 | DiagID: diag::note_odr_objc_superclass) |
5800 | << Impl->getSuperClass()->getDeclName(); |
5801 | else |
5802 | Importer.ToDiag(Loc: Impl->getLocation(), |
5803 | DiagID: diag::note_odr_objc_missing_superclass); |
5804 | if (D->getSuperClass()) |
5805 | Importer.FromDiag(Loc: D->getLocation(), |
5806 | DiagID: diag::note_odr_objc_superclass) |
5807 | << D->getSuperClass()->getDeclName(); |
5808 | else |
5809 | Importer.FromDiag(Loc: D->getLocation(), |
5810 | DiagID: diag::note_odr_objc_missing_superclass); |
5811 | |
5812 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
5813 | } |
5814 | } |
5815 | |
5816 | // Import all of the members of this @implementation. |
5817 | if (Error Err = ImportDeclContext(FromDC: D)) |
5818 | return std::move(Err); |
5819 | |
5820 | return Impl; |
5821 | } |
5822 | |
5823 | ExpectedDecl ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { |
5824 | // Import the major distinguishing characteristics of an @property. |
5825 | DeclContext *DC, *LexicalDC; |
5826 | DeclarationName Name; |
5827 | SourceLocation Loc; |
5828 | NamedDecl *ToD; |
5829 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
5830 | return std::move(Err); |
5831 | if (ToD) |
5832 | return ToD; |
5833 | |
5834 | // Check whether we have already imported this property. |
5835 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
5836 | for (auto *FoundDecl : FoundDecls) { |
5837 | if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(Val: FoundDecl)) { |
5838 | // Instance and class properties can share the same name but are different |
5839 | // declarations. |
5840 | if (FoundProp->isInstanceProperty() != D->isInstanceProperty()) |
5841 | continue; |
5842 | |
5843 | // Check property types. |
5844 | if (!Importer.IsStructurallyEquivalent(From: D->getType(), |
5845 | To: FoundProp->getType())) { |
5846 | Importer.ToDiag(Loc, DiagID: diag::warn_odr_objc_property_type_inconsistent) |
5847 | << Name << D->getType() << FoundProp->getType(); |
5848 | Importer.ToDiag(Loc: FoundProp->getLocation(), DiagID: diag::note_odr_value_here) |
5849 | << FoundProp->getType(); |
5850 | |
5851 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
5852 | } |
5853 | |
5854 | // FIXME: Check property attributes, getters, setters, etc.? |
5855 | |
5856 | // Consider these properties to be equivalent. |
5857 | Importer.MapImported(From: D, To: FoundProp); |
5858 | return FoundProp; |
5859 | } |
5860 | } |
5861 | |
5862 | Error Err = Error::success(); |
5863 | auto ToType = importChecked(Err, From: D->getType()); |
5864 | auto ToTypeSourceInfo = importChecked(Err, From: D->getTypeSourceInfo()); |
5865 | auto ToAtLoc = importChecked(Err, From: D->getAtLoc()); |
5866 | auto ToLParenLoc = importChecked(Err, From: D->getLParenLoc()); |
5867 | if (Err) |
5868 | return std::move(Err); |
5869 | |
5870 | // Create the new property. |
5871 | ObjCPropertyDecl *ToProperty; |
5872 | if (GetImportedOrCreateDecl( |
5873 | ToD&: ToProperty, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, |
5874 | args: Name.getAsIdentifierInfo(), args&: ToAtLoc, |
5875 | args&: ToLParenLoc, args&: ToType, |
5876 | args&: ToTypeSourceInfo, args: D->getPropertyImplementation())) |
5877 | return ToProperty; |
5878 | |
5879 | auto ToGetterName = importChecked(Err, From: D->getGetterName()); |
5880 | auto ToSetterName = importChecked(Err, From: D->getSetterName()); |
5881 | auto ToGetterNameLoc = importChecked(Err, From: D->getGetterNameLoc()); |
5882 | auto ToSetterNameLoc = importChecked(Err, From: D->getSetterNameLoc()); |
5883 | auto ToGetterMethodDecl = importChecked(Err, From: D->getGetterMethodDecl()); |
5884 | auto ToSetterMethodDecl = importChecked(Err, From: D->getSetterMethodDecl()); |
5885 | auto ToPropertyIvarDecl = importChecked(Err, From: D->getPropertyIvarDecl()); |
5886 | if (Err) |
5887 | return std::move(Err); |
5888 | |
5889 | ToProperty->setLexicalDeclContext(LexicalDC); |
5890 | LexicalDC->addDeclInternal(D: ToProperty); |
5891 | |
5892 | ToProperty->setPropertyAttributes(D->getPropertyAttributes()); |
5893 | ToProperty->setPropertyAttributesAsWritten( |
5894 | D->getPropertyAttributesAsWritten()); |
5895 | ToProperty->setGetterName(Sel: ToGetterName, Loc: ToGetterNameLoc); |
5896 | ToProperty->setSetterName(Sel: ToSetterName, Loc: ToSetterNameLoc); |
5897 | ToProperty->setGetterMethodDecl(ToGetterMethodDecl); |
5898 | ToProperty->setSetterMethodDecl(ToSetterMethodDecl); |
5899 | ToProperty->setPropertyIvarDecl(ToPropertyIvarDecl); |
5900 | return ToProperty; |
5901 | } |
5902 | |
5903 | ExpectedDecl |
5904 | ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { |
5905 | ObjCPropertyDecl *Property; |
5906 | if (Error Err = importInto(To&: Property, From: D->getPropertyDecl())) |
5907 | return std::move(Err); |
5908 | |
5909 | DeclContext *DC, *LexicalDC; |
5910 | if (Error Err = ImportDeclContext(FromD: D, ToDC&: DC, ToLexicalDC&: LexicalDC)) |
5911 | return std::move(Err); |
5912 | |
5913 | auto *InImpl = cast<ObjCImplDecl>(Val: LexicalDC); |
5914 | |
5915 | // Import the ivar (for an @synthesize). |
5916 | ObjCIvarDecl *Ivar = nullptr; |
5917 | if (Error Err = importInto(To&: Ivar, From: D->getPropertyIvarDecl())) |
5918 | return std::move(Err); |
5919 | |
5920 | ObjCPropertyImplDecl *ToImpl |
5921 | = InImpl->FindPropertyImplDecl(propertyId: Property->getIdentifier(), |
5922 | queryKind: Property->getQueryKind()); |
5923 | if (!ToImpl) { |
5924 | |
5925 | Error Err = Error::success(); |
5926 | auto ToBeginLoc = importChecked(Err, From: D->getBeginLoc()); |
5927 | auto ToLocation = importChecked(Err, From: D->getLocation()); |
5928 | auto ToPropertyIvarDeclLoc = |
5929 | importChecked(Err, From: D->getPropertyIvarDeclLoc()); |
5930 | if (Err) |
5931 | return std::move(Err); |
5932 | |
5933 | if (GetImportedOrCreateDecl(ToD&: ToImpl, FromD: D, args&: Importer.getToContext(), args&: DC, |
5934 | args&: ToBeginLoc, |
5935 | args&: ToLocation, args&: Property, |
5936 | args: D->getPropertyImplementation(), args&: Ivar, |
5937 | args&: ToPropertyIvarDeclLoc)) |
5938 | return ToImpl; |
5939 | |
5940 | ToImpl->setLexicalDeclContext(LexicalDC); |
5941 | LexicalDC->addDeclInternal(D: ToImpl); |
5942 | } else { |
5943 | // Check that we have the same kind of property implementation (@synthesize |
5944 | // vs. @dynamic). |
5945 | if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) { |
5946 | Importer.ToDiag(Loc: ToImpl->getLocation(), |
5947 | DiagID: diag::warn_odr_objc_property_impl_kind_inconsistent) |
5948 | << Property->getDeclName() |
5949 | << (ToImpl->getPropertyImplementation() |
5950 | == ObjCPropertyImplDecl::Dynamic); |
5951 | Importer.FromDiag(Loc: D->getLocation(), |
5952 | DiagID: diag::note_odr_objc_property_impl_kind) |
5953 | << D->getPropertyDecl()->getDeclName() |
5954 | << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); |
5955 | |
5956 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
5957 | } |
5958 | |
5959 | // For @synthesize, check that we have the same |
5960 | if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize && |
5961 | Ivar != ToImpl->getPropertyIvarDecl()) { |
5962 | Importer.ToDiag(Loc: ToImpl->getPropertyIvarDeclLoc(), |
5963 | DiagID: diag::warn_odr_objc_synthesize_ivar_inconsistent) |
5964 | << Property->getDeclName() |
5965 | << ToImpl->getPropertyIvarDecl()->getDeclName() |
5966 | << Ivar->getDeclName(); |
5967 | Importer.FromDiag(Loc: D->getPropertyIvarDeclLoc(), |
5968 | DiagID: diag::note_odr_objc_synthesize_ivar_here) |
5969 | << D->getPropertyIvarDecl()->getDeclName(); |
5970 | |
5971 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
5972 | } |
5973 | |
5974 | // Merge the existing implementation with the new implementation. |
5975 | Importer.MapImported(From: D, To: ToImpl); |
5976 | } |
5977 | |
5978 | return ToImpl; |
5979 | } |
5980 | |
5981 | ExpectedDecl |
5982 | ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
5983 | // For template arguments, we adopt the translation unit as our declaration |
5984 | // context. This context will be fixed when (during) the actual template |
5985 | // declaration is created. |
5986 | |
5987 | ExpectedSLoc BeginLocOrErr = import(From: D->getBeginLoc()); |
5988 | if (!BeginLocOrErr) |
5989 | return BeginLocOrErr.takeError(); |
5990 | |
5991 | ExpectedSLoc LocationOrErr = import(From: D->getLocation()); |
5992 | if (!LocationOrErr) |
5993 | return LocationOrErr.takeError(); |
5994 | |
5995 | TemplateTypeParmDecl *ToD = nullptr; |
5996 | if (GetImportedOrCreateDecl( |
5997 | ToD, FromD: D, args&: Importer.getToContext(), |
5998 | args: Importer.getToContext().getTranslationUnitDecl(), |
5999 | args&: *BeginLocOrErr, args&: *LocationOrErr, |
6000 | args: D->getDepth(), args: D->getIndex(), args: Importer.Import(FromId: D->getIdentifier()), |
6001 | args: D->wasDeclaredWithTypename(), args: D->isParameterPack(), |
6002 | args: D->hasTypeConstraint())) |
6003 | return ToD; |
6004 | |
6005 | // Import the type-constraint |
6006 | if (const TypeConstraint *TC = D->getTypeConstraint()) { |
6007 | |
6008 | Error Err = Error::success(); |
6009 | auto ToConceptRef = importChecked(Err, From: TC->getConceptReference()); |
6010 | auto ToIDC = importChecked(Err, From: TC->getImmediatelyDeclaredConstraint()); |
6011 | if (Err) |
6012 | return std::move(Err); |
6013 | |
6014 | ToD->setTypeConstraint(CR: ToConceptRef, ImmediatelyDeclaredConstraint: ToIDC, ArgPackSubstIndex: TC->getArgPackSubstIndex()); |
6015 | } |
6016 | |
6017 | if (Error Err = importTemplateParameterDefaultArgument(D, ToD)) |
6018 | return Err; |
6019 | |
6020 | return ToD; |
6021 | } |
6022 | |
6023 | ExpectedDecl |
6024 | ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
6025 | |
6026 | Error Err = Error::success(); |
6027 | auto ToDeclName = importChecked(Err, From: D->getDeclName()); |
6028 | auto ToLocation = importChecked(Err, From: D->getLocation()); |
6029 | auto ToType = importChecked(Err, From: D->getType()); |
6030 | auto ToTypeSourceInfo = importChecked(Err, From: D->getTypeSourceInfo()); |
6031 | auto ToInnerLocStart = importChecked(Err, From: D->getInnerLocStart()); |
6032 | if (Err) |
6033 | return std::move(Err); |
6034 | |
6035 | NonTypeTemplateParmDecl *ToD = nullptr; |
6036 | if (GetImportedOrCreateDecl(ToD, FromD: D, args&: Importer.getToContext(), |
6037 | args: Importer.getToContext().getTranslationUnitDecl(), |
6038 | args&: ToInnerLocStart, args&: ToLocation, args: D->getDepth(), |
6039 | args: D->getPosition(), |
6040 | args: ToDeclName.getAsIdentifierInfo(), args&: ToType, |
6041 | args: D->isParameterPack(), args&: ToTypeSourceInfo)) |
6042 | return ToD; |
6043 | |
6044 | Err = importTemplateParameterDefaultArgument(D, ToD); |
6045 | if (Err) |
6046 | return Err; |
6047 | |
6048 | return ToD; |
6049 | } |
6050 | |
6051 | ExpectedDecl |
6052 | ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
6053 | bool IsCanonical = false; |
6054 | if (auto *CanonD = Importer.getFromContext() |
6055 | .findCanonicalTemplateTemplateParmDeclInternal(TTP: D); |
6056 | CanonD == D) |
6057 | IsCanonical = true; |
6058 | |
6059 | // Import the name of this declaration. |
6060 | auto NameOrErr = import(From: D->getDeclName()); |
6061 | if (!NameOrErr) |
6062 | return NameOrErr.takeError(); |
6063 | |
6064 | // Import the location of this declaration. |
6065 | ExpectedSLoc LocationOrErr = import(From: D->getLocation()); |
6066 | if (!LocationOrErr) |
6067 | return LocationOrErr.takeError(); |
6068 | |
6069 | // Import template parameters. |
6070 | auto TemplateParamsOrErr = import(From: D->getTemplateParameters()); |
6071 | if (!TemplateParamsOrErr) |
6072 | return TemplateParamsOrErr.takeError(); |
6073 | |
6074 | TemplateTemplateParmDecl *ToD = nullptr; |
6075 | if (GetImportedOrCreateDecl( |
6076 | ToD, FromD: D, args&: Importer.getToContext(), |
6077 | args: Importer.getToContext().getTranslationUnitDecl(), args&: *LocationOrErr, |
6078 | args: D->getDepth(), args: D->getPosition(), args: D->isParameterPack(), |
6079 | args: (*NameOrErr).getAsIdentifierInfo(), args: D->wasDeclaredWithTypename(), |
6080 | args&: *TemplateParamsOrErr)) |
6081 | return ToD; |
6082 | |
6083 | if (Error Err = importTemplateParameterDefaultArgument(D, ToD)) |
6084 | return Err; |
6085 | |
6086 | if (IsCanonical) |
6087 | return Importer.getToContext() |
6088 | .insertCanonicalTemplateTemplateParmDeclInternal(CanonTTP: ToD); |
6089 | |
6090 | return ToD; |
6091 | } |
6092 | |
6093 | // Returns the definition for a (forward) declaration of a TemplateDecl, if |
6094 | // it has any definition in the redecl chain. |
6095 | template <typename T> static auto getTemplateDefinition(T *D) -> T * { |
6096 | assert(D->getTemplatedDecl() && "Should be called on templates only" ); |
6097 | auto *ToTemplatedDef = D->getTemplatedDecl()->getDefinition(); |
6098 | if (!ToTemplatedDef) |
6099 | return nullptr; |
6100 | auto *TemplateWithDef = ToTemplatedDef->getDescribedTemplate(); |
6101 | return cast_or_null<T>(TemplateWithDef); |
6102 | } |
6103 | |
6104 | ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
6105 | |
6106 | // Import the major distinguishing characteristics of this class template. |
6107 | DeclContext *DC, *LexicalDC; |
6108 | DeclarationName Name; |
6109 | SourceLocation Loc; |
6110 | NamedDecl *ToD; |
6111 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
6112 | return std::move(Err); |
6113 | if (ToD) |
6114 | return ToD; |
6115 | |
6116 | // Should check if a declaration is friend in a dependent context. |
6117 | // Such templates are not linked together in a declaration chain. |
6118 | // The ASTImporter strategy is to map existing forward declarations to |
6119 | // imported ones only if strictly necessary, otherwise import these as new |
6120 | // forward declarations. In case of the "dependent friend" declarations, new |
6121 | // declarations are created, but not linked in a declaration chain. |
6122 | auto IsDependentFriend = [](ClassTemplateDecl *TD) { |
6123 | return TD->getFriendObjectKind() != Decl::FOK_None && |
6124 | TD->getLexicalDeclContext()->isDependentContext(); |
6125 | }; |
6126 | bool DependentFriend = IsDependentFriend(D); |
6127 | |
6128 | ClassTemplateDecl *FoundByLookup = nullptr; |
6129 | |
6130 | // We may already have a template of the same name; try to find and match it. |
6131 | if (!DC->isFunctionOrMethod()) { |
6132 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
6133 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
6134 | for (auto *FoundDecl : FoundDecls) { |
6135 | if (!FoundDecl->isInIdentifierNamespace(NS: Decl::IDNS_Ordinary | |
6136 | Decl::IDNS_TagFriend)) |
6137 | continue; |
6138 | |
6139 | auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Val: FoundDecl); |
6140 | if (FoundTemplate) { |
6141 | if (!hasSameVisibilityContextAndLinkage(Found: FoundTemplate, From: D)) |
6142 | continue; |
6143 | |
6144 | // FIXME: sufficient condition for 'IgnoreTemplateParmDepth'? |
6145 | bool IgnoreTemplateParmDepth = |
6146 | (FoundTemplate->getFriendObjectKind() != Decl::FOK_None) != |
6147 | (D->getFriendObjectKind() != Decl::FOK_None); |
6148 | if (IsStructuralMatch(From: D, To: FoundTemplate, /*Complain=*/true, |
6149 | IgnoreTemplateParmDepth)) { |
6150 | if (DependentFriend || IsDependentFriend(FoundTemplate)) |
6151 | continue; |
6152 | |
6153 | ClassTemplateDecl *TemplateWithDef = |
6154 | getTemplateDefinition(D: FoundTemplate); |
6155 | if (D->isThisDeclarationADefinition() && TemplateWithDef) |
6156 | return Importer.MapImported(From: D, To: TemplateWithDef); |
6157 | if (!FoundByLookup) |
6158 | FoundByLookup = FoundTemplate; |
6159 | // Search in all matches because there may be multiple decl chains, |
6160 | // see ASTTests test ImportExistingFriendClassTemplateDef. |
6161 | continue; |
6162 | } |
6163 | // When importing a friend, it is possible that multiple declarations |
6164 | // with same name can co-exist in specific cases (if a template contains |
6165 | // a friend template and has a specialization). For this case the |
6166 | // declarations should match, except that the "template depth" is |
6167 | // different. No linking of previous declaration is needed in this case. |
6168 | // FIXME: This condition may need refinement. |
6169 | if (D->getFriendObjectKind() != Decl::FOK_None && |
6170 | FoundTemplate->getFriendObjectKind() != Decl::FOK_None && |
6171 | D->getFriendObjectKind() != FoundTemplate->getFriendObjectKind() && |
6172 | IsStructuralMatch(From: D, To: FoundTemplate, /*Complain=*/false, |
6173 | /*IgnoreTemplateParmDepth=*/true)) |
6174 | continue; |
6175 | |
6176 | ConflictingDecls.push_back(Elt: FoundDecl); |
6177 | } |
6178 | } |
6179 | |
6180 | if (!ConflictingDecls.empty()) { |
6181 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
6182 | Name, DC, IDNS: Decl::IDNS_Ordinary, Decls: ConflictingDecls.data(), |
6183 | NumDecls: ConflictingDecls.size()); |
6184 | if (NameOrErr) |
6185 | Name = NameOrErr.get(); |
6186 | else |
6187 | return NameOrErr.takeError(); |
6188 | } |
6189 | } |
6190 | |
6191 | CXXRecordDecl *FromTemplated = D->getTemplatedDecl(); |
6192 | |
6193 | auto TemplateParamsOrErr = import(From: D->getTemplateParameters()); |
6194 | if (!TemplateParamsOrErr) |
6195 | return TemplateParamsOrErr.takeError(); |
6196 | |
6197 | // Create the declaration that is being templated. |
6198 | CXXRecordDecl *ToTemplated; |
6199 | if (Error Err = importInto(To&: ToTemplated, From: FromTemplated)) |
6200 | return std::move(Err); |
6201 | |
6202 | // Create the class template declaration itself. |
6203 | ClassTemplateDecl *D2; |
6204 | if (GetImportedOrCreateDecl(ToD&: D2, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, args&: Name, |
6205 | args&: *TemplateParamsOrErr, args&: ToTemplated)) |
6206 | return D2; |
6207 | |
6208 | ToTemplated->setDescribedClassTemplate(D2); |
6209 | |
6210 | D2->setAccess(D->getAccess()); |
6211 | D2->setLexicalDeclContext(LexicalDC); |
6212 | |
6213 | addDeclToContexts(FromD: D, ToD: D2); |
6214 | updateLookupTableForTemplateParameters(Params&: **TemplateParamsOrErr); |
6215 | |
6216 | if (FoundByLookup) { |
6217 | auto *Recent = |
6218 | const_cast<ClassTemplateDecl *>(FoundByLookup->getMostRecentDecl()); |
6219 | |
6220 | // It is possible that during the import of the class template definition |
6221 | // we start the import of a fwd friend decl of the very same class template |
6222 | // and we add the fwd friend decl to the lookup table. But the ToTemplated |
6223 | // had been created earlier and by that time the lookup could not find |
6224 | // anything existing, so it has no previous decl. Later, (still during the |
6225 | // import of the fwd friend decl) we start to import the definition again |
6226 | // and this time the lookup finds the previous fwd friend class template. |
6227 | // In this case we must set up the previous decl for the templated decl. |
6228 | if (!ToTemplated->getPreviousDecl()) { |
6229 | assert(FoundByLookup->getTemplatedDecl() && |
6230 | "Found decl must have its templated decl set" ); |
6231 | CXXRecordDecl *PrevTemplated = |
6232 | FoundByLookup->getTemplatedDecl()->getMostRecentDecl(); |
6233 | if (ToTemplated != PrevTemplated) |
6234 | ToTemplated->setPreviousDecl(PrevTemplated); |
6235 | } |
6236 | |
6237 | D2->setPreviousDecl(Recent); |
6238 | } |
6239 | |
6240 | return D2; |
6241 | } |
6242 | |
6243 | ExpectedDecl ASTNodeImporter::VisitClassTemplateSpecializationDecl( |
6244 | ClassTemplateSpecializationDecl *D) { |
6245 | ClassTemplateDecl *ClassTemplate; |
6246 | if (Error Err = importInto(To&: ClassTemplate, From: D->getSpecializedTemplate())) |
6247 | return std::move(Err); |
6248 | |
6249 | // Import the context of this declaration. |
6250 | DeclContext *DC, *LexicalDC; |
6251 | if (Error Err = ImportDeclContext(FromD: D, ToDC&: DC, ToLexicalDC&: LexicalDC)) |
6252 | return std::move(Err); |
6253 | |
6254 | // Import template arguments. |
6255 | SmallVector<TemplateArgument, 2> TemplateArgs; |
6256 | if (Error Err = |
6257 | ImportTemplateArguments(FromArgs: D->getTemplateArgs().asArray(), ToArgs&: TemplateArgs)) |
6258 | return std::move(Err); |
6259 | // Try to find an existing specialization with these template arguments and |
6260 | // template parameter list. |
6261 | void *InsertPos = nullptr; |
6262 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; |
6263 | ClassTemplatePartialSpecializationDecl *PartialSpec = |
6264 | dyn_cast<ClassTemplatePartialSpecializationDecl>(Val: D); |
6265 | |
6266 | // Import template parameters. |
6267 | TemplateParameterList *ToTPList = nullptr; |
6268 | |
6269 | if (PartialSpec) { |
6270 | auto ToTPListOrErr = import(From: PartialSpec->getTemplateParameters()); |
6271 | if (!ToTPListOrErr) |
6272 | return ToTPListOrErr.takeError(); |
6273 | ToTPList = *ToTPListOrErr; |
6274 | PrevDecl = ClassTemplate->findPartialSpecialization(Args: TemplateArgs, |
6275 | TPL: *ToTPListOrErr, |
6276 | InsertPos); |
6277 | } else |
6278 | PrevDecl = ClassTemplate->findSpecialization(Args: TemplateArgs, InsertPos); |
6279 | |
6280 | if (PrevDecl) { |
6281 | if (IsStructuralMatch(From: D, To: PrevDecl)) { |
6282 | CXXRecordDecl *PrevDefinition = PrevDecl->getDefinition(); |
6283 | if (D->isThisDeclarationADefinition() && PrevDefinition) { |
6284 | Importer.MapImported(From: D, To: PrevDefinition); |
6285 | // Import those default field initializers which have been |
6286 | // instantiated in the "From" context, but not in the "To" context. |
6287 | for (auto *FromField : D->fields()) { |
6288 | auto ToOrErr = import(From: FromField); |
6289 | if (!ToOrErr) |
6290 | return ToOrErr.takeError(); |
6291 | } |
6292 | |
6293 | // Import those methods which have been instantiated in the |
6294 | // "From" context, but not in the "To" context. |
6295 | for (CXXMethodDecl *FromM : D->methods()) { |
6296 | auto ToOrErr = import(From: FromM); |
6297 | if (!ToOrErr) |
6298 | return ToOrErr.takeError(); |
6299 | } |
6300 | |
6301 | // TODO Import instantiated default arguments. |
6302 | // TODO Import instantiated exception specifications. |
6303 | // |
6304 | // Generally, ASTCommon.h/DeclUpdateKind enum gives a very good hint |
6305 | // what else could be fused during an AST merge. |
6306 | return PrevDefinition; |
6307 | } |
6308 | } else { // ODR violation. |
6309 | // FIXME HandleNameConflict |
6310 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
6311 | } |
6312 | } |
6313 | |
6314 | // Import the location of this declaration. |
6315 | ExpectedSLoc BeginLocOrErr = import(From: D->getBeginLoc()); |
6316 | if (!BeginLocOrErr) |
6317 | return BeginLocOrErr.takeError(); |
6318 | ExpectedSLoc IdLocOrErr = import(From: D->getLocation()); |
6319 | if (!IdLocOrErr) |
6320 | return IdLocOrErr.takeError(); |
6321 | |
6322 | // Import TemplateArgumentListInfo. |
6323 | TemplateArgumentListInfo ToTAInfo; |
6324 | if (const auto *ASTTemplateArgs = D->getTemplateArgsAsWritten()) { |
6325 | if (Error Err = ImportTemplateArgumentListInfo(From: *ASTTemplateArgs, Result&: ToTAInfo)) |
6326 | return std::move(Err); |
6327 | } |
6328 | |
6329 | // Create the specialization. |
6330 | ClassTemplateSpecializationDecl *D2 = nullptr; |
6331 | if (PartialSpec) { |
6332 | QualType CanonInjType; |
6333 | if (Error Err = importInto( |
6334 | To&: CanonInjType, From: PartialSpec->getInjectedSpecializationType())) |
6335 | return std::move(Err); |
6336 | CanonInjType = CanonInjType.getCanonicalType(); |
6337 | |
6338 | if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>( |
6339 | ToD&: D2, FromD: D, args&: Importer.getToContext(), args: D->getTagKind(), args&: DC, args&: *BeginLocOrErr, |
6340 | args&: *IdLocOrErr, args&: ToTPList, args&: ClassTemplate, args: ArrayRef(TemplateArgs), |
6341 | args&: CanonInjType, |
6342 | args: cast_or_null<ClassTemplatePartialSpecializationDecl>(Val: PrevDecl))) |
6343 | return D2; |
6344 | |
6345 | // Update InsertPos, because preceding import calls may have invalidated |
6346 | // it by adding new specializations. |
6347 | auto *PartSpec2 = cast<ClassTemplatePartialSpecializationDecl>(Val: D2); |
6348 | if (!ClassTemplate->findPartialSpecialization(Args: TemplateArgs, TPL: ToTPList, |
6349 | InsertPos)) |
6350 | // Add this partial specialization to the class template. |
6351 | ClassTemplate->AddPartialSpecialization(D: PartSpec2, InsertPos); |
6352 | if (Expected<ClassTemplatePartialSpecializationDecl *> ToInstOrErr = |
6353 | import(From: PartialSpec->getInstantiatedFromMember())) |
6354 | PartSpec2->setInstantiatedFromMember(*ToInstOrErr); |
6355 | else |
6356 | return ToInstOrErr.takeError(); |
6357 | |
6358 | updateLookupTableForTemplateParameters(Params&: *ToTPList); |
6359 | } else { // Not a partial specialization. |
6360 | if (GetImportedOrCreateDecl(ToD&: D2, FromD: D, args&: Importer.getToContext(), args: D->getTagKind(), |
6361 | args&: DC, args&: *BeginLocOrErr, args&: *IdLocOrErr, args&: ClassTemplate, |
6362 | args&: TemplateArgs, args: D->hasStrictPackMatch(), |
6363 | args&: PrevDecl)) |
6364 | return D2; |
6365 | |
6366 | // Update InsertPos, because preceding import calls may have invalidated |
6367 | // it by adding new specializations. |
6368 | if (!ClassTemplate->findSpecialization(Args: TemplateArgs, InsertPos)) |
6369 | // Add this specialization to the class template. |
6370 | ClassTemplate->AddSpecialization(D: D2, InsertPos); |
6371 | } |
6372 | |
6373 | D2->setSpecializationKind(D->getSpecializationKind()); |
6374 | |
6375 | // Set the context of this specialization/instantiation. |
6376 | D2->setLexicalDeclContext(LexicalDC); |
6377 | |
6378 | // Add to the DC only if it was an explicit specialization/instantiation. |
6379 | if (D2->isExplicitInstantiationOrSpecialization()) { |
6380 | LexicalDC->addDeclInternal(D: D2); |
6381 | } |
6382 | |
6383 | if (auto BraceRangeOrErr = import(From: D->getBraceRange())) |
6384 | D2->setBraceRange(*BraceRangeOrErr); |
6385 | else |
6386 | return BraceRangeOrErr.takeError(); |
6387 | |
6388 | if (Error Err = ImportTemplateParameterLists(FromD: D, ToD: D2)) |
6389 | return std::move(Err); |
6390 | |
6391 | // Import the qualifier, if any. |
6392 | if (auto LocOrErr = import(From: D->getQualifierLoc())) |
6393 | D2->setQualifierInfo(*LocOrErr); |
6394 | else |
6395 | return LocOrErr.takeError(); |
6396 | |
6397 | if (D->getTemplateArgsAsWritten()) |
6398 | D2->setTemplateArgsAsWritten(ToTAInfo); |
6399 | |
6400 | if (auto LocOrErr = import(From: D->getTemplateKeywordLoc())) |
6401 | D2->setTemplateKeywordLoc(*LocOrErr); |
6402 | else |
6403 | return LocOrErr.takeError(); |
6404 | |
6405 | if (auto LocOrErr = import(From: D->getExternKeywordLoc())) |
6406 | D2->setExternKeywordLoc(*LocOrErr); |
6407 | else |
6408 | return LocOrErr.takeError(); |
6409 | |
6410 | if (D->getPointOfInstantiation().isValid()) { |
6411 | if (auto POIOrErr = import(From: D->getPointOfInstantiation())) |
6412 | D2->setPointOfInstantiation(*POIOrErr); |
6413 | else |
6414 | return POIOrErr.takeError(); |
6415 | } |
6416 | |
6417 | D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind()); |
6418 | |
6419 | if (auto P = D->getInstantiatedFrom()) { |
6420 | if (auto *CTD = dyn_cast<ClassTemplateDecl *>(Val&: P)) { |
6421 | if (auto CTDorErr = import(From: CTD)) |
6422 | D2->setInstantiationOf(*CTDorErr); |
6423 | } else { |
6424 | auto *CTPSD = cast<ClassTemplatePartialSpecializationDecl *>(Val&: P); |
6425 | auto CTPSDOrErr = import(From: CTPSD); |
6426 | if (!CTPSDOrErr) |
6427 | return CTPSDOrErr.takeError(); |
6428 | const TemplateArgumentList &DArgs = D->getTemplateInstantiationArgs(); |
6429 | SmallVector<TemplateArgument, 2> D2ArgsVec(DArgs.size()); |
6430 | for (unsigned I = 0; I < DArgs.size(); ++I) { |
6431 | const TemplateArgument &DArg = DArgs[I]; |
6432 | if (auto ArgOrErr = import(From: DArg)) |
6433 | D2ArgsVec[I] = *ArgOrErr; |
6434 | else |
6435 | return ArgOrErr.takeError(); |
6436 | } |
6437 | D2->setInstantiationOf( |
6438 | PartialSpec: *CTPSDOrErr, |
6439 | TemplateArgs: TemplateArgumentList::CreateCopy(Context&: Importer.getToContext(), Args: D2ArgsVec)); |
6440 | } |
6441 | } |
6442 | |
6443 | if (D->isCompleteDefinition()) |
6444 | if (Error Err = ImportDefinition(From: D, To: D2)) |
6445 | return std::move(Err); |
6446 | |
6447 | return D2; |
6448 | } |
6449 | |
6450 | ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) { |
6451 | // Import the major distinguishing characteristics of this variable template. |
6452 | DeclContext *DC, *LexicalDC; |
6453 | DeclarationName Name; |
6454 | SourceLocation Loc; |
6455 | NamedDecl *ToD; |
6456 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
6457 | return std::move(Err); |
6458 | if (ToD) |
6459 | return ToD; |
6460 | |
6461 | // We may already have a template of the same name; try to find and match it. |
6462 | assert(!DC->isFunctionOrMethod() && |
6463 | "Variable templates cannot be declared at function scope" ); |
6464 | |
6465 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
6466 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
6467 | VarTemplateDecl *FoundByLookup = nullptr; |
6468 | for (auto *FoundDecl : FoundDecls) { |
6469 | if (!FoundDecl->isInIdentifierNamespace(NS: Decl::IDNS_Ordinary)) |
6470 | continue; |
6471 | |
6472 | if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Val: FoundDecl)) { |
6473 | // Use the templated decl, some linkage flags are set only there. |
6474 | if (!hasSameVisibilityContextAndLinkage(Found: FoundTemplate->getTemplatedDecl(), |
6475 | From: D->getTemplatedDecl())) |
6476 | continue; |
6477 | if (IsStructuralMatch(From: D, To: FoundTemplate)) { |
6478 | // FIXME Check for ODR error if the two definitions have |
6479 | // different initializers? |
6480 | VarTemplateDecl *FoundDef = getTemplateDefinition(D: FoundTemplate); |
6481 | if (D->getDeclContext()->isRecord()) { |
6482 | assert(FoundTemplate->getDeclContext()->isRecord() && |
6483 | "Member variable template imported as non-member, " |
6484 | "inconsistent imported AST?" ); |
6485 | if (FoundDef) |
6486 | return Importer.MapImported(From: D, To: FoundDef); |
6487 | if (!D->isThisDeclarationADefinition()) |
6488 | return Importer.MapImported(From: D, To: FoundTemplate); |
6489 | } else { |
6490 | if (FoundDef && D->isThisDeclarationADefinition()) |
6491 | return Importer.MapImported(From: D, To: FoundDef); |
6492 | } |
6493 | FoundByLookup = FoundTemplate; |
6494 | break; |
6495 | } |
6496 | ConflictingDecls.push_back(Elt: FoundDecl); |
6497 | } |
6498 | } |
6499 | |
6500 | if (!ConflictingDecls.empty()) { |
6501 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
6502 | Name, DC, IDNS: Decl::IDNS_Ordinary, Decls: ConflictingDecls.data(), |
6503 | NumDecls: ConflictingDecls.size()); |
6504 | if (NameOrErr) |
6505 | Name = NameOrErr.get(); |
6506 | else |
6507 | return NameOrErr.takeError(); |
6508 | } |
6509 | |
6510 | VarDecl *DTemplated = D->getTemplatedDecl(); |
6511 | |
6512 | // Import the type. |
6513 | // FIXME: Value not used? |
6514 | ExpectedType TypeOrErr = import(From: DTemplated->getType()); |
6515 | if (!TypeOrErr) |
6516 | return TypeOrErr.takeError(); |
6517 | |
6518 | // Create the declaration that is being templated. |
6519 | VarDecl *ToTemplated; |
6520 | if (Error Err = importInto(To&: ToTemplated, From: DTemplated)) |
6521 | return std::move(Err); |
6522 | |
6523 | // Create the variable template declaration itself. |
6524 | auto TemplateParamsOrErr = import(From: D->getTemplateParameters()); |
6525 | if (!TemplateParamsOrErr) |
6526 | return TemplateParamsOrErr.takeError(); |
6527 | |
6528 | VarTemplateDecl *ToVarTD; |
6529 | if (GetImportedOrCreateDecl(ToD&: ToVarTD, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, |
6530 | args&: Name, args&: *TemplateParamsOrErr, args&: ToTemplated)) |
6531 | return ToVarTD; |
6532 | |
6533 | ToTemplated->setDescribedVarTemplate(ToVarTD); |
6534 | |
6535 | ToVarTD->setAccess(D->getAccess()); |
6536 | ToVarTD->setLexicalDeclContext(LexicalDC); |
6537 | LexicalDC->addDeclInternal(D: ToVarTD); |
6538 | if (DC != Importer.getToContext().getTranslationUnitDecl()) |
6539 | updateLookupTableForTemplateParameters(Params&: **TemplateParamsOrErr); |
6540 | |
6541 | if (FoundByLookup) { |
6542 | auto *Recent = |
6543 | const_cast<VarTemplateDecl *>(FoundByLookup->getMostRecentDecl()); |
6544 | if (!ToTemplated->getPreviousDecl()) { |
6545 | auto *PrevTemplated = |
6546 | FoundByLookup->getTemplatedDecl()->getMostRecentDecl(); |
6547 | if (ToTemplated != PrevTemplated) |
6548 | ToTemplated->setPreviousDecl(PrevTemplated); |
6549 | } |
6550 | ToVarTD->setPreviousDecl(Recent); |
6551 | } |
6552 | |
6553 | return ToVarTD; |
6554 | } |
6555 | |
6556 | ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl( |
6557 | VarTemplateSpecializationDecl *D) { |
6558 | // A VarTemplateSpecializationDecl inherits from VarDecl, the import is done |
6559 | // in an analog way (but specialized for this case). |
6560 | |
6561 | SmallVector<Decl *, 2> Redecls = getCanonicalForwardRedeclChain(D); |
6562 | auto RedeclIt = Redecls.begin(); |
6563 | // Import the first part of the decl chain. I.e. import all previous |
6564 | // declarations starting from the canonical decl. |
6565 | for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) { |
6566 | ExpectedDecl RedeclOrErr = import(From: *RedeclIt); |
6567 | if (!RedeclOrErr) |
6568 | return RedeclOrErr.takeError(); |
6569 | } |
6570 | assert(*RedeclIt == D); |
6571 | |
6572 | VarTemplateDecl *VarTemplate = nullptr; |
6573 | if (Error Err = importInto(To&: VarTemplate, From: D->getSpecializedTemplate())) |
6574 | return std::move(Err); |
6575 | |
6576 | // Import the context of this declaration. |
6577 | DeclContext *DC, *LexicalDC; |
6578 | if (Error Err = ImportDeclContext(FromD: D, ToDC&: DC, ToLexicalDC&: LexicalDC)) |
6579 | return std::move(Err); |
6580 | |
6581 | // Import the location of this declaration. |
6582 | ExpectedSLoc BeginLocOrErr = import(From: D->getBeginLoc()); |
6583 | if (!BeginLocOrErr) |
6584 | return BeginLocOrErr.takeError(); |
6585 | |
6586 | auto IdLocOrErr = import(From: D->getLocation()); |
6587 | if (!IdLocOrErr) |
6588 | return IdLocOrErr.takeError(); |
6589 | |
6590 | // Import template arguments. |
6591 | SmallVector<TemplateArgument, 2> TemplateArgs; |
6592 | if (Error Err = |
6593 | ImportTemplateArguments(FromArgs: D->getTemplateArgs().asArray(), ToArgs&: TemplateArgs)) |
6594 | return std::move(Err); |
6595 | |
6596 | // Try to find an existing specialization with these template arguments. |
6597 | void *InsertPos = nullptr; |
6598 | VarTemplateSpecializationDecl *FoundSpecialization = |
6599 | VarTemplate->findSpecialization(Args: TemplateArgs, InsertPos); |
6600 | if (FoundSpecialization) { |
6601 | if (IsStructuralMatch(From: D, To: FoundSpecialization)) { |
6602 | VarDecl *FoundDef = FoundSpecialization->getDefinition(); |
6603 | if (D->getDeclContext()->isRecord()) { |
6604 | // In a record, it is allowed only to have one optional declaration and |
6605 | // one definition of the (static or constexpr) variable template. |
6606 | assert( |
6607 | FoundSpecialization->getDeclContext()->isRecord() && |
6608 | "Member variable template specialization imported as non-member, " |
6609 | "inconsistent imported AST?" ); |
6610 | if (FoundDef) |
6611 | return Importer.MapImported(From: D, To: FoundDef); |
6612 | if (!D->isThisDeclarationADefinition()) |
6613 | return Importer.MapImported(From: D, To: FoundSpecialization); |
6614 | } else { |
6615 | // If definition is imported and there is already one, map to it. |
6616 | // Otherwise create a new variable and link it to the existing. |
6617 | if (FoundDef && D->isThisDeclarationADefinition()) |
6618 | return Importer.MapImported(From: D, To: FoundDef); |
6619 | } |
6620 | } else { |
6621 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
6622 | } |
6623 | } |
6624 | |
6625 | VarTemplateSpecializationDecl *D2 = nullptr; |
6626 | |
6627 | TemplateArgumentListInfo ToTAInfo; |
6628 | if (const auto *Args = D->getTemplateArgsAsWritten()) { |
6629 | if (Error Err = ImportTemplateArgumentListInfo(From: *Args, Result&: ToTAInfo)) |
6630 | return std::move(Err); |
6631 | } |
6632 | |
6633 | using PartVarSpecDecl = VarTemplatePartialSpecializationDecl; |
6634 | // Create a new specialization. |
6635 | if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(Val: D)) { |
6636 | auto ToTPListOrErr = import(From: FromPartial->getTemplateParameters()); |
6637 | if (!ToTPListOrErr) |
6638 | return ToTPListOrErr.takeError(); |
6639 | |
6640 | PartVarSpecDecl *ToPartial; |
6641 | if (GetImportedOrCreateDecl(ToD&: ToPartial, FromD: D, args&: Importer.getToContext(), args&: DC, |
6642 | args&: *BeginLocOrErr, args&: *IdLocOrErr, args&: *ToTPListOrErr, |
6643 | args&: VarTemplate, args: QualType(), args: nullptr, |
6644 | args: D->getStorageClass(), args&: TemplateArgs)) |
6645 | return ToPartial; |
6646 | |
6647 | if (Expected<PartVarSpecDecl *> ToInstOrErr = |
6648 | import(From: FromPartial->getInstantiatedFromMember())) |
6649 | ToPartial->setInstantiatedFromMember(*ToInstOrErr); |
6650 | else |
6651 | return ToInstOrErr.takeError(); |
6652 | |
6653 | if (FromPartial->isMemberSpecialization()) |
6654 | ToPartial->setMemberSpecialization(); |
6655 | |
6656 | D2 = ToPartial; |
6657 | |
6658 | // FIXME: Use this update if VarTemplatePartialSpecializationDecl is fixed |
6659 | // to adopt template parameters. |
6660 | // updateLookupTableForTemplateParameters(**ToTPListOrErr); |
6661 | } else { // Full specialization |
6662 | if (GetImportedOrCreateDecl(ToD&: D2, FromD: D, args&: Importer.getToContext(), args&: DC, |
6663 | args&: *BeginLocOrErr, args&: *IdLocOrErr, args&: VarTemplate, |
6664 | args: QualType(), args: nullptr, args: D->getStorageClass(), |
6665 | args&: TemplateArgs)) |
6666 | return D2; |
6667 | } |
6668 | |
6669 | // Update InsertPos, because preceding import calls may have invalidated |
6670 | // it by adding new specializations. |
6671 | if (!VarTemplate->findSpecialization(Args: TemplateArgs, InsertPos)) |
6672 | VarTemplate->AddSpecialization(D: D2, InsertPos); |
6673 | |
6674 | QualType T; |
6675 | if (Error Err = importInto(To&: T, From: D->getType())) |
6676 | return std::move(Err); |
6677 | D2->setType(T); |
6678 | |
6679 | auto TInfoOrErr = import(From: D->getTypeSourceInfo()); |
6680 | if (!TInfoOrErr) |
6681 | return TInfoOrErr.takeError(); |
6682 | D2->setTypeSourceInfo(*TInfoOrErr); |
6683 | |
6684 | if (D->getPointOfInstantiation().isValid()) { |
6685 | if (ExpectedSLoc POIOrErr = import(From: D->getPointOfInstantiation())) |
6686 | D2->setPointOfInstantiation(*POIOrErr); |
6687 | else |
6688 | return POIOrErr.takeError(); |
6689 | } |
6690 | |
6691 | D2->setSpecializationKind(D->getSpecializationKind()); |
6692 | |
6693 | if (D->getTemplateArgsAsWritten()) |
6694 | D2->setTemplateArgsAsWritten(ToTAInfo); |
6695 | |
6696 | if (auto LocOrErr = import(From: D->getQualifierLoc())) |
6697 | D2->setQualifierInfo(*LocOrErr); |
6698 | else |
6699 | return LocOrErr.takeError(); |
6700 | |
6701 | if (D->isConstexpr()) |
6702 | D2->setConstexpr(true); |
6703 | |
6704 | D2->setAccess(D->getAccess()); |
6705 | |
6706 | if (Error Err = ImportInitializer(From: D, To: D2)) |
6707 | return std::move(Err); |
6708 | |
6709 | if (FoundSpecialization) |
6710 | D2->setPreviousDecl(FoundSpecialization->getMostRecentDecl()); |
6711 | |
6712 | addDeclToContexts(FromD: D, ToD: D2); |
6713 | |
6714 | // Import the rest of the chain. I.e. import all subsequent declarations. |
6715 | for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) { |
6716 | ExpectedDecl RedeclOrErr = import(From: *RedeclIt); |
6717 | if (!RedeclOrErr) |
6718 | return RedeclOrErr.takeError(); |
6719 | } |
6720 | |
6721 | return D2; |
6722 | } |
6723 | |
6724 | ExpectedDecl |
6725 | ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
6726 | DeclContext *DC, *LexicalDC; |
6727 | DeclarationName Name; |
6728 | SourceLocation Loc; |
6729 | NamedDecl *ToD; |
6730 | |
6731 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
6732 | return std::move(Err); |
6733 | |
6734 | if (ToD) |
6735 | return ToD; |
6736 | |
6737 | const FunctionTemplateDecl *FoundByLookup = nullptr; |
6738 | |
6739 | // Try to find a function in our own ("to") context with the same name, same |
6740 | // type, and in the same context as the function we're importing. |
6741 | // FIXME Split this into a separate function. |
6742 | if (!LexicalDC->isFunctionOrMethod()) { |
6743 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend; |
6744 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
6745 | for (auto *FoundDecl : FoundDecls) { |
6746 | if (!FoundDecl->isInIdentifierNamespace(NS: IDNS)) |
6747 | continue; |
6748 | |
6749 | if (auto *FoundTemplate = dyn_cast<FunctionTemplateDecl>(Val: FoundDecl)) { |
6750 | if (!hasSameVisibilityContextAndLinkage(Found: FoundTemplate, From: D)) |
6751 | continue; |
6752 | if (IsStructuralMatch(From: D, To: FoundTemplate)) { |
6753 | FunctionTemplateDecl *TemplateWithDef = |
6754 | getTemplateDefinition(D: FoundTemplate); |
6755 | if (D->isThisDeclarationADefinition() && TemplateWithDef) |
6756 | return Importer.MapImported(From: D, To: TemplateWithDef); |
6757 | |
6758 | FoundByLookup = FoundTemplate; |
6759 | break; |
6760 | // TODO: handle conflicting names |
6761 | } |
6762 | } |
6763 | } |
6764 | } |
6765 | |
6766 | auto ParamsOrErr = import(From: D->getTemplateParameters()); |
6767 | if (!ParamsOrErr) |
6768 | return ParamsOrErr.takeError(); |
6769 | TemplateParameterList *Params = *ParamsOrErr; |
6770 | |
6771 | FunctionDecl *TemplatedFD; |
6772 | if (Error Err = importInto(To&: TemplatedFD, From: D->getTemplatedDecl())) |
6773 | return std::move(Err); |
6774 | |
6775 | // At creation of the template the template parameters are "adopted" |
6776 | // (DeclContext is changed). After this possible change the lookup table |
6777 | // must be updated. |
6778 | // At deduction guides the DeclContext of the template parameters may be |
6779 | // different from what we would expect, it may be the class template, or a |
6780 | // probably different CXXDeductionGuideDecl. This may come from the fact that |
6781 | // the template parameter objects may be shared between deduction guides or |
6782 | // the class template, and at creation of multiple FunctionTemplateDecl |
6783 | // objects (for deduction guides) the same parameters are re-used. The |
6784 | // "adoption" happens multiple times with different parent, even recursively |
6785 | // for TemplateTemplateParmDecl. The same happens at import when the |
6786 | // FunctionTemplateDecl objects are created, but in different order. |
6787 | // In this way the DeclContext of these template parameters is not necessarily |
6788 | // the same as in the "from" context. |
6789 | SmallVector<DeclContext *, 2> OldParamDC; |
6790 | OldParamDC.reserve(N: Params->size()); |
6791 | llvm::transform(Range&: *Params, d_first: std::back_inserter(x&: OldParamDC), |
6792 | F: [](NamedDecl *ND) { return ND->getDeclContext(); }); |
6793 | |
6794 | FunctionTemplateDecl *ToFunc; |
6795 | if (GetImportedOrCreateDecl(ToD&: ToFunc, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, args&: Name, |
6796 | args&: Params, args&: TemplatedFD)) |
6797 | return ToFunc; |
6798 | |
6799 | // Fail if TemplatedFD is already part of a template. |
6800 | // The template should have been found by structural equivalence check before, |
6801 | // or ToFunc should be already imported. |
6802 | // If not, there is AST incompatibility that can be caused by previous import |
6803 | // errors. (NameConflict is not exact here.) |
6804 | if (TemplatedFD->getDescribedTemplate()) |
6805 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
6806 | |
6807 | TemplatedFD->setDescribedFunctionTemplate(ToFunc); |
6808 | |
6809 | ToFunc->setAccess(D->getAccess()); |
6810 | ToFunc->setLexicalDeclContext(LexicalDC); |
6811 | addDeclToContexts(FromD: D, ToD: ToFunc); |
6812 | |
6813 | ASTImporterLookupTable *LT = Importer.SharedState->getLookupTable(); |
6814 | if (LT && !OldParamDC.empty()) { |
6815 | for (unsigned int I = 0; I < OldParamDC.size(); ++I) |
6816 | LT->updateForced(ND: Params->getParam(Idx: I), OldDC: OldParamDC[I]); |
6817 | } |
6818 | |
6819 | if (FoundByLookup) { |
6820 | auto *Recent = |
6821 | const_cast<FunctionTemplateDecl *>(FoundByLookup->getMostRecentDecl()); |
6822 | if (!TemplatedFD->getPreviousDecl()) { |
6823 | assert(FoundByLookup->getTemplatedDecl() && |
6824 | "Found decl must have its templated decl set" ); |
6825 | auto *PrevTemplated = |
6826 | FoundByLookup->getTemplatedDecl()->getMostRecentDecl(); |
6827 | if (TemplatedFD != PrevTemplated) |
6828 | TemplatedFD->setPreviousDecl(PrevTemplated); |
6829 | } |
6830 | ToFunc->setPreviousDecl(Recent); |
6831 | } |
6832 | |
6833 | return ToFunc; |
6834 | } |
6835 | |
6836 | //---------------------------------------------------------------------------- |
6837 | // Import Statements |
6838 | //---------------------------------------------------------------------------- |
6839 | |
6840 | ExpectedStmt ASTNodeImporter::VisitStmt(Stmt *S) { |
6841 | Importer.FromDiag(Loc: S->getBeginLoc(), DiagID: diag::err_unsupported_ast_node) |
6842 | << S->getStmtClassName(); |
6843 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
6844 | } |
6845 | |
6846 | |
6847 | ExpectedStmt ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) { |
6848 | if (Importer.returnWithErrorInTest()) |
6849 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
6850 | SmallVector<IdentifierInfo *, 4> Names; |
6851 | for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) { |
6852 | IdentifierInfo *ToII = Importer.Import(FromId: S->getOutputIdentifier(i: I)); |
6853 | // ToII is nullptr when no symbolic name is given for output operand |
6854 | // see ParseStmtAsm::ParseAsmOperandsOpt |
6855 | Names.push_back(Elt: ToII); |
6856 | } |
6857 | |
6858 | for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) { |
6859 | IdentifierInfo *ToII = Importer.Import(FromId: S->getInputIdentifier(i: I)); |
6860 | // ToII is nullptr when no symbolic name is given for input operand |
6861 | // see ParseStmtAsm::ParseAsmOperandsOpt |
6862 | Names.push_back(Elt: ToII); |
6863 | } |
6864 | |
6865 | SmallVector<Expr *, 4> Clobbers; |
6866 | for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) { |
6867 | if (auto ClobberOrErr = import(From: S->getClobberExpr(i: I))) |
6868 | Clobbers.push_back(Elt: *ClobberOrErr); |
6869 | else |
6870 | return ClobberOrErr.takeError(); |
6871 | |
6872 | } |
6873 | |
6874 | SmallVector<Expr *, 4> Constraints; |
6875 | for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) { |
6876 | if (auto OutputOrErr = import(From: S->getOutputConstraintExpr(i: I))) |
6877 | Constraints.push_back(Elt: *OutputOrErr); |
6878 | else |
6879 | return OutputOrErr.takeError(); |
6880 | } |
6881 | |
6882 | for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) { |
6883 | if (auto InputOrErr = import(From: S->getInputConstraintExpr(i: I))) |
6884 | Constraints.push_back(Elt: *InputOrErr); |
6885 | else |
6886 | return InputOrErr.takeError(); |
6887 | } |
6888 | |
6889 | SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs() + |
6890 | S->getNumLabels()); |
6891 | if (Error Err = ImportContainerChecked(InContainer: S->outputs(), OutContainer&: Exprs)) |
6892 | return std::move(Err); |
6893 | |
6894 | if (Error Err = |
6895 | ImportArrayChecked(InContainer: S->inputs(), Obegin: Exprs.begin() + S->getNumOutputs())) |
6896 | return std::move(Err); |
6897 | |
6898 | if (Error Err = ImportArrayChecked( |
6899 | InContainer: S->labels(), Obegin: Exprs.begin() + S->getNumOutputs() + S->getNumInputs())) |
6900 | return std::move(Err); |
6901 | |
6902 | ExpectedSLoc AsmLocOrErr = import(From: S->getAsmLoc()); |
6903 | if (!AsmLocOrErr) |
6904 | return AsmLocOrErr.takeError(); |
6905 | auto AsmStrOrErr = import(From: S->getAsmStringExpr()); |
6906 | if (!AsmStrOrErr) |
6907 | return AsmStrOrErr.takeError(); |
6908 | ExpectedSLoc RParenLocOrErr = import(From: S->getRParenLoc()); |
6909 | if (!RParenLocOrErr) |
6910 | return RParenLocOrErr.takeError(); |
6911 | |
6912 | return new (Importer.getToContext()) GCCAsmStmt( |
6913 | Importer.getToContext(), |
6914 | *AsmLocOrErr, |
6915 | S->isSimple(), |
6916 | S->isVolatile(), |
6917 | S->getNumOutputs(), |
6918 | S->getNumInputs(), |
6919 | Names.data(), |
6920 | Constraints.data(), |
6921 | Exprs.data(), |
6922 | *AsmStrOrErr, |
6923 | S->getNumClobbers(), |
6924 | Clobbers.data(), |
6925 | S->getNumLabels(), |
6926 | *RParenLocOrErr); |
6927 | } |
6928 | |
6929 | ExpectedStmt ASTNodeImporter::VisitDeclStmt(DeclStmt *S) { |
6930 | |
6931 | Error Err = Error::success(); |
6932 | auto ToDG = importChecked(Err, From: S->getDeclGroup()); |
6933 | auto ToBeginLoc = importChecked(Err, From: S->getBeginLoc()); |
6934 | auto ToEndLoc = importChecked(Err, From: S->getEndLoc()); |
6935 | if (Err) |
6936 | return std::move(Err); |
6937 | return new (Importer.getToContext()) DeclStmt(ToDG, ToBeginLoc, ToEndLoc); |
6938 | } |
6939 | |
6940 | ExpectedStmt ASTNodeImporter::VisitNullStmt(NullStmt *S) { |
6941 | ExpectedSLoc ToSemiLocOrErr = import(From: S->getSemiLoc()); |
6942 | if (!ToSemiLocOrErr) |
6943 | return ToSemiLocOrErr.takeError(); |
6944 | return new (Importer.getToContext()) NullStmt( |
6945 | *ToSemiLocOrErr, S->hasLeadingEmptyMacro()); |
6946 | } |
6947 | |
6948 | ExpectedStmt ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) { |
6949 | SmallVector<Stmt *, 8> ToStmts(S->size()); |
6950 | |
6951 | if (Error Err = ImportContainerChecked(InContainer: S->body(), OutContainer&: ToStmts)) |
6952 | return std::move(Err); |
6953 | |
6954 | ExpectedSLoc ToLBracLocOrErr = import(From: S->getLBracLoc()); |
6955 | if (!ToLBracLocOrErr) |
6956 | return ToLBracLocOrErr.takeError(); |
6957 | |
6958 | ExpectedSLoc ToRBracLocOrErr = import(From: S->getRBracLoc()); |
6959 | if (!ToRBracLocOrErr) |
6960 | return ToRBracLocOrErr.takeError(); |
6961 | |
6962 | FPOptionsOverride FPO = |
6963 | S->hasStoredFPFeatures() ? S->getStoredFPFeatures() : FPOptionsOverride(); |
6964 | return CompoundStmt::Create(C: Importer.getToContext(), Stmts: ToStmts, FPFeatures: FPO, |
6965 | LB: *ToLBracLocOrErr, RB: *ToRBracLocOrErr); |
6966 | } |
6967 | |
6968 | ExpectedStmt ASTNodeImporter::VisitCaseStmt(CaseStmt *S) { |
6969 | |
6970 | Error Err = Error::success(); |
6971 | auto ToLHS = importChecked(Err, From: S->getLHS()); |
6972 | auto ToRHS = importChecked(Err, From: S->getRHS()); |
6973 | auto ToSubStmt = importChecked(Err, From: S->getSubStmt()); |
6974 | auto ToCaseLoc = importChecked(Err, From: S->getCaseLoc()); |
6975 | auto ToEllipsisLoc = importChecked(Err, From: S->getEllipsisLoc()); |
6976 | auto ToColonLoc = importChecked(Err, From: S->getColonLoc()); |
6977 | if (Err) |
6978 | return std::move(Err); |
6979 | |
6980 | auto *ToStmt = CaseStmt::Create(Ctx: Importer.getToContext(), lhs: ToLHS, rhs: ToRHS, |
6981 | caseLoc: ToCaseLoc, ellipsisLoc: ToEllipsisLoc, colonLoc: ToColonLoc); |
6982 | ToStmt->setSubStmt(ToSubStmt); |
6983 | |
6984 | return ToStmt; |
6985 | } |
6986 | |
6987 | ExpectedStmt ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) { |
6988 | |
6989 | Error Err = Error::success(); |
6990 | auto ToDefaultLoc = importChecked(Err, From: S->getDefaultLoc()); |
6991 | auto ToColonLoc = importChecked(Err, From: S->getColonLoc()); |
6992 | auto ToSubStmt = importChecked(Err, From: S->getSubStmt()); |
6993 | if (Err) |
6994 | return std::move(Err); |
6995 | |
6996 | return new (Importer.getToContext()) DefaultStmt( |
6997 | ToDefaultLoc, ToColonLoc, ToSubStmt); |
6998 | } |
6999 | |
7000 | ExpectedStmt ASTNodeImporter::VisitLabelStmt(LabelStmt *S) { |
7001 | |
7002 | Error Err = Error::success(); |
7003 | auto ToIdentLoc = importChecked(Err, From: S->getIdentLoc()); |
7004 | auto ToLabelDecl = importChecked(Err, From: S->getDecl()); |
7005 | auto ToSubStmt = importChecked(Err, From: S->getSubStmt()); |
7006 | if (Err) |
7007 | return std::move(Err); |
7008 | |
7009 | return new (Importer.getToContext()) LabelStmt( |
7010 | ToIdentLoc, ToLabelDecl, ToSubStmt); |
7011 | } |
7012 | |
7013 | ExpectedStmt ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) { |
7014 | ExpectedSLoc ToAttrLocOrErr = import(From: S->getAttrLoc()); |
7015 | if (!ToAttrLocOrErr) |
7016 | return ToAttrLocOrErr.takeError(); |
7017 | ArrayRef<const Attr*> FromAttrs(S->getAttrs()); |
7018 | SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size()); |
7019 | if (Error Err = ImportContainerChecked(InContainer: FromAttrs, OutContainer&: ToAttrs)) |
7020 | return std::move(Err); |
7021 | ExpectedStmt ToSubStmtOrErr = import(From: S->getSubStmt()); |
7022 | if (!ToSubStmtOrErr) |
7023 | return ToSubStmtOrErr.takeError(); |
7024 | |
7025 | return AttributedStmt::Create( |
7026 | C: Importer.getToContext(), Loc: *ToAttrLocOrErr, Attrs: ToAttrs, SubStmt: *ToSubStmtOrErr); |
7027 | } |
7028 | |
7029 | ExpectedStmt ASTNodeImporter::VisitIfStmt(IfStmt *S) { |
7030 | |
7031 | Error Err = Error::success(); |
7032 | auto ToIfLoc = importChecked(Err, From: S->getIfLoc()); |
7033 | auto ToInit = importChecked(Err, From: S->getInit()); |
7034 | auto ToConditionVariable = importChecked(Err, From: S->getConditionVariable()); |
7035 | auto ToCond = importChecked(Err, From: S->getCond()); |
7036 | auto ToLParenLoc = importChecked(Err, From: S->getLParenLoc()); |
7037 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
7038 | auto ToThen = importChecked(Err, From: S->getThen()); |
7039 | auto ToElseLoc = importChecked(Err, From: S->getElseLoc()); |
7040 | auto ToElse = importChecked(Err, From: S->getElse()); |
7041 | if (Err) |
7042 | return std::move(Err); |
7043 | |
7044 | return IfStmt::Create(Ctx: Importer.getToContext(), IL: ToIfLoc, Kind: S->getStatementKind(), |
7045 | Init: ToInit, Var: ToConditionVariable, Cond: ToCond, LPL: ToLParenLoc, |
7046 | RPL: ToRParenLoc, Then: ToThen, EL: ToElseLoc, Else: ToElse); |
7047 | } |
7048 | |
7049 | ExpectedStmt ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) { |
7050 | |
7051 | Error Err = Error::success(); |
7052 | auto ToInit = importChecked(Err, From: S->getInit()); |
7053 | auto ToConditionVariable = importChecked(Err, From: S->getConditionVariable()); |
7054 | auto ToCond = importChecked(Err, From: S->getCond()); |
7055 | auto ToLParenLoc = importChecked(Err, From: S->getLParenLoc()); |
7056 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
7057 | auto ToBody = importChecked(Err, From: S->getBody()); |
7058 | auto ToSwitchLoc = importChecked(Err, From: S->getSwitchLoc()); |
7059 | if (Err) |
7060 | return std::move(Err); |
7061 | |
7062 | auto *ToStmt = |
7063 | SwitchStmt::Create(Ctx: Importer.getToContext(), Init: ToInit, Var: ToConditionVariable, |
7064 | Cond: ToCond, LParenLoc: ToLParenLoc, RParenLoc: ToRParenLoc); |
7065 | ToStmt->setBody(ToBody); |
7066 | ToStmt->setSwitchLoc(ToSwitchLoc); |
7067 | |
7068 | // Now we have to re-chain the cases. |
7069 | SwitchCase *LastChainedSwitchCase = nullptr; |
7070 | for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr; |
7071 | SC = SC->getNextSwitchCase()) { |
7072 | Expected<SwitchCase *> ToSCOrErr = import(From: SC); |
7073 | if (!ToSCOrErr) |
7074 | return ToSCOrErr.takeError(); |
7075 | if (LastChainedSwitchCase) |
7076 | LastChainedSwitchCase->setNextSwitchCase(*ToSCOrErr); |
7077 | else |
7078 | ToStmt->setSwitchCaseList(*ToSCOrErr); |
7079 | LastChainedSwitchCase = *ToSCOrErr; |
7080 | } |
7081 | |
7082 | return ToStmt; |
7083 | } |
7084 | |
7085 | ExpectedStmt ASTNodeImporter::VisitWhileStmt(WhileStmt *S) { |
7086 | |
7087 | Error Err = Error::success(); |
7088 | auto ToConditionVariable = importChecked(Err, From: S->getConditionVariable()); |
7089 | auto ToCond = importChecked(Err, From: S->getCond()); |
7090 | auto ToBody = importChecked(Err, From: S->getBody()); |
7091 | auto ToWhileLoc = importChecked(Err, From: S->getWhileLoc()); |
7092 | auto ToLParenLoc = importChecked(Err, From: S->getLParenLoc()); |
7093 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
7094 | if (Err) |
7095 | return std::move(Err); |
7096 | |
7097 | return WhileStmt::Create(Ctx: Importer.getToContext(), Var: ToConditionVariable, Cond: ToCond, |
7098 | Body: ToBody, WL: ToWhileLoc, LParenLoc: ToLParenLoc, RParenLoc: ToRParenLoc); |
7099 | } |
7100 | |
7101 | ExpectedStmt ASTNodeImporter::VisitDoStmt(DoStmt *S) { |
7102 | |
7103 | Error Err = Error::success(); |
7104 | auto ToBody = importChecked(Err, From: S->getBody()); |
7105 | auto ToCond = importChecked(Err, From: S->getCond()); |
7106 | auto ToDoLoc = importChecked(Err, From: S->getDoLoc()); |
7107 | auto ToWhileLoc = importChecked(Err, From: S->getWhileLoc()); |
7108 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
7109 | if (Err) |
7110 | return std::move(Err); |
7111 | |
7112 | return new (Importer.getToContext()) DoStmt( |
7113 | ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc); |
7114 | } |
7115 | |
7116 | ExpectedStmt ASTNodeImporter::VisitForStmt(ForStmt *S) { |
7117 | |
7118 | Error Err = Error::success(); |
7119 | auto ToInit = importChecked(Err, From: S->getInit()); |
7120 | auto ToCond = importChecked(Err, From: S->getCond()); |
7121 | auto ToConditionVariable = importChecked(Err, From: S->getConditionVariable()); |
7122 | auto ToInc = importChecked(Err, From: S->getInc()); |
7123 | auto ToBody = importChecked(Err, From: S->getBody()); |
7124 | auto ToForLoc = importChecked(Err, From: S->getForLoc()); |
7125 | auto ToLParenLoc = importChecked(Err, From: S->getLParenLoc()); |
7126 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
7127 | if (Err) |
7128 | return std::move(Err); |
7129 | |
7130 | return new (Importer.getToContext()) ForStmt( |
7131 | Importer.getToContext(), |
7132 | ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc, ToLParenLoc, |
7133 | ToRParenLoc); |
7134 | } |
7135 | |
7136 | ExpectedStmt ASTNodeImporter::VisitGotoStmt(GotoStmt *S) { |
7137 | |
7138 | Error Err = Error::success(); |
7139 | auto ToLabel = importChecked(Err, From: S->getLabel()); |
7140 | auto ToGotoLoc = importChecked(Err, From: S->getGotoLoc()); |
7141 | auto ToLabelLoc = importChecked(Err, From: S->getLabelLoc()); |
7142 | if (Err) |
7143 | return std::move(Err); |
7144 | |
7145 | return new (Importer.getToContext()) GotoStmt( |
7146 | ToLabel, ToGotoLoc, ToLabelLoc); |
7147 | } |
7148 | |
7149 | ExpectedStmt ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) { |
7150 | |
7151 | Error Err = Error::success(); |
7152 | auto ToGotoLoc = importChecked(Err, From: S->getGotoLoc()); |
7153 | auto ToStarLoc = importChecked(Err, From: S->getStarLoc()); |
7154 | auto ToTarget = importChecked(Err, From: S->getTarget()); |
7155 | if (Err) |
7156 | return std::move(Err); |
7157 | |
7158 | return new (Importer.getToContext()) IndirectGotoStmt( |
7159 | ToGotoLoc, ToStarLoc, ToTarget); |
7160 | } |
7161 | |
7162 | ExpectedStmt ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) { |
7163 | ExpectedSLoc ToContinueLocOrErr = import(From: S->getContinueLoc()); |
7164 | if (!ToContinueLocOrErr) |
7165 | return ToContinueLocOrErr.takeError(); |
7166 | return new (Importer.getToContext()) ContinueStmt(*ToContinueLocOrErr); |
7167 | } |
7168 | |
7169 | ExpectedStmt ASTNodeImporter::VisitBreakStmt(BreakStmt *S) { |
7170 | auto ToBreakLocOrErr = import(From: S->getBreakLoc()); |
7171 | if (!ToBreakLocOrErr) |
7172 | return ToBreakLocOrErr.takeError(); |
7173 | return new (Importer.getToContext()) BreakStmt(*ToBreakLocOrErr); |
7174 | } |
7175 | |
7176 | ExpectedStmt ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) { |
7177 | |
7178 | Error Err = Error::success(); |
7179 | auto ToReturnLoc = importChecked(Err, From: S->getReturnLoc()); |
7180 | auto ToRetValue = importChecked(Err, From: S->getRetValue()); |
7181 | auto ToNRVOCandidate = importChecked(Err, From: S->getNRVOCandidate()); |
7182 | if (Err) |
7183 | return std::move(Err); |
7184 | |
7185 | return ReturnStmt::Create(Ctx: Importer.getToContext(), RL: ToReturnLoc, E: ToRetValue, |
7186 | NRVOCandidate: ToNRVOCandidate); |
7187 | } |
7188 | |
7189 | ExpectedStmt ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) { |
7190 | |
7191 | Error Err = Error::success(); |
7192 | auto ToCatchLoc = importChecked(Err, From: S->getCatchLoc()); |
7193 | auto ToExceptionDecl = importChecked(Err, From: S->getExceptionDecl()); |
7194 | auto ToHandlerBlock = importChecked(Err, From: S->getHandlerBlock()); |
7195 | if (Err) |
7196 | return std::move(Err); |
7197 | |
7198 | return new (Importer.getToContext()) CXXCatchStmt ( |
7199 | ToCatchLoc, ToExceptionDecl, ToHandlerBlock); |
7200 | } |
7201 | |
7202 | ExpectedStmt ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) { |
7203 | ExpectedSLoc ToTryLocOrErr = import(From: S->getTryLoc()); |
7204 | if (!ToTryLocOrErr) |
7205 | return ToTryLocOrErr.takeError(); |
7206 | |
7207 | ExpectedStmt ToTryBlockOrErr = import(From: S->getTryBlock()); |
7208 | if (!ToTryBlockOrErr) |
7209 | return ToTryBlockOrErr.takeError(); |
7210 | |
7211 | SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers()); |
7212 | for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) { |
7213 | CXXCatchStmt *FromHandler = S->getHandler(i: HI); |
7214 | if (auto ToHandlerOrErr = import(From: FromHandler)) |
7215 | ToHandlers[HI] = *ToHandlerOrErr; |
7216 | else |
7217 | return ToHandlerOrErr.takeError(); |
7218 | } |
7219 | |
7220 | return CXXTryStmt::Create(C: Importer.getToContext(), tryLoc: *ToTryLocOrErr, |
7221 | tryBlock: cast<CompoundStmt>(Val: *ToTryBlockOrErr), handlers: ToHandlers); |
7222 | } |
7223 | |
7224 | ExpectedStmt ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) { |
7225 | |
7226 | Error Err = Error::success(); |
7227 | auto ToInit = importChecked(Err, From: S->getInit()); |
7228 | auto ToRangeStmt = importChecked(Err, From: S->getRangeStmt()); |
7229 | auto ToBeginStmt = importChecked(Err, From: S->getBeginStmt()); |
7230 | auto ToEndStmt = importChecked(Err, From: S->getEndStmt()); |
7231 | auto ToCond = importChecked(Err, From: S->getCond()); |
7232 | auto ToInc = importChecked(Err, From: S->getInc()); |
7233 | auto ToLoopVarStmt = importChecked(Err, From: S->getLoopVarStmt()); |
7234 | auto ToBody = importChecked(Err, From: S->getBody()); |
7235 | auto ToForLoc = importChecked(Err, From: S->getForLoc()); |
7236 | auto ToCoawaitLoc = importChecked(Err, From: S->getCoawaitLoc()); |
7237 | auto ToColonLoc = importChecked(Err, From: S->getColonLoc()); |
7238 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
7239 | if (Err) |
7240 | return std::move(Err); |
7241 | |
7242 | return new (Importer.getToContext()) CXXForRangeStmt( |
7243 | ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt, |
7244 | ToBody, ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc); |
7245 | } |
7246 | |
7247 | ExpectedStmt |
7248 | ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) { |
7249 | Error Err = Error::success(); |
7250 | auto ToElement = importChecked(Err, From: S->getElement()); |
7251 | auto ToCollection = importChecked(Err, From: S->getCollection()); |
7252 | auto ToBody = importChecked(Err, From: S->getBody()); |
7253 | auto ToForLoc = importChecked(Err, From: S->getForLoc()); |
7254 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
7255 | if (Err) |
7256 | return std::move(Err); |
7257 | |
7258 | return new (Importer.getToContext()) ObjCForCollectionStmt(ToElement, |
7259 | ToCollection, |
7260 | ToBody, |
7261 | ToForLoc, |
7262 | ToRParenLoc); |
7263 | } |
7264 | |
7265 | ExpectedStmt ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) { |
7266 | |
7267 | Error Err = Error::success(); |
7268 | auto ToAtCatchLoc = importChecked(Err, From: S->getAtCatchLoc()); |
7269 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
7270 | auto ToCatchParamDecl = importChecked(Err, From: S->getCatchParamDecl()); |
7271 | auto ToCatchBody = importChecked(Err, From: S->getCatchBody()); |
7272 | if (Err) |
7273 | return std::move(Err); |
7274 | |
7275 | return new (Importer.getToContext()) ObjCAtCatchStmt ( |
7276 | ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody); |
7277 | } |
7278 | |
7279 | ExpectedStmt ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) { |
7280 | ExpectedSLoc ToAtFinallyLocOrErr = import(From: S->getAtFinallyLoc()); |
7281 | if (!ToAtFinallyLocOrErr) |
7282 | return ToAtFinallyLocOrErr.takeError(); |
7283 | ExpectedStmt ToAtFinallyStmtOrErr = import(From: S->getFinallyBody()); |
7284 | if (!ToAtFinallyStmtOrErr) |
7285 | return ToAtFinallyStmtOrErr.takeError(); |
7286 | return new (Importer.getToContext()) ObjCAtFinallyStmt(*ToAtFinallyLocOrErr, |
7287 | *ToAtFinallyStmtOrErr); |
7288 | } |
7289 | |
7290 | ExpectedStmt ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) { |
7291 | |
7292 | Error Err = Error::success(); |
7293 | auto ToAtTryLoc = importChecked(Err, From: S->getAtTryLoc()); |
7294 | auto ToTryBody = importChecked(Err, From: S->getTryBody()); |
7295 | auto ToFinallyStmt = importChecked(Err, From: S->getFinallyStmt()); |
7296 | if (Err) |
7297 | return std::move(Err); |
7298 | |
7299 | SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts()); |
7300 | for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) { |
7301 | ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(I: CI); |
7302 | if (ExpectedStmt ToCatchStmtOrErr = import(From: FromCatchStmt)) |
7303 | ToCatchStmts[CI] = *ToCatchStmtOrErr; |
7304 | else |
7305 | return ToCatchStmtOrErr.takeError(); |
7306 | } |
7307 | |
7308 | return ObjCAtTryStmt::Create(Context: Importer.getToContext(), |
7309 | atTryLoc: ToAtTryLoc, atTryStmt: ToTryBody, |
7310 | CatchStmts: ToCatchStmts.begin(), NumCatchStmts: ToCatchStmts.size(), |
7311 | atFinallyStmt: ToFinallyStmt); |
7312 | } |
7313 | |
7314 | ExpectedStmt |
7315 | ASTNodeImporter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
7316 | |
7317 | Error Err = Error::success(); |
7318 | auto ToAtSynchronizedLoc = importChecked(Err, From: S->getAtSynchronizedLoc()); |
7319 | auto ToSynchExpr = importChecked(Err, From: S->getSynchExpr()); |
7320 | auto ToSynchBody = importChecked(Err, From: S->getSynchBody()); |
7321 | if (Err) |
7322 | return std::move(Err); |
7323 | |
7324 | return new (Importer.getToContext()) ObjCAtSynchronizedStmt( |
7325 | ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody); |
7326 | } |
7327 | |
7328 | ExpectedStmt ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) { |
7329 | ExpectedSLoc ToThrowLocOrErr = import(From: S->getThrowLoc()); |
7330 | if (!ToThrowLocOrErr) |
7331 | return ToThrowLocOrErr.takeError(); |
7332 | ExpectedExpr ToThrowExprOrErr = import(From: S->getThrowExpr()); |
7333 | if (!ToThrowExprOrErr) |
7334 | return ToThrowExprOrErr.takeError(); |
7335 | return new (Importer.getToContext()) ObjCAtThrowStmt( |
7336 | *ToThrowLocOrErr, *ToThrowExprOrErr); |
7337 | } |
7338 | |
7339 | ExpectedStmt ASTNodeImporter::VisitObjCAutoreleasePoolStmt( |
7340 | ObjCAutoreleasePoolStmt *S) { |
7341 | ExpectedSLoc ToAtLocOrErr = import(From: S->getAtLoc()); |
7342 | if (!ToAtLocOrErr) |
7343 | return ToAtLocOrErr.takeError(); |
7344 | ExpectedStmt ToSubStmtOrErr = import(From: S->getSubStmt()); |
7345 | if (!ToSubStmtOrErr) |
7346 | return ToSubStmtOrErr.takeError(); |
7347 | return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(*ToAtLocOrErr, |
7348 | *ToSubStmtOrErr); |
7349 | } |
7350 | |
7351 | //---------------------------------------------------------------------------- |
7352 | // Import Expressions |
7353 | //---------------------------------------------------------------------------- |
7354 | ExpectedStmt ASTNodeImporter::VisitExpr(Expr *E) { |
7355 | Importer.FromDiag(Loc: E->getBeginLoc(), DiagID: diag::err_unsupported_ast_node) |
7356 | << E->getStmtClassName(); |
7357 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
7358 | } |
7359 | |
7360 | ExpectedStmt ASTNodeImporter::VisitSourceLocExpr(SourceLocExpr *E) { |
7361 | Error Err = Error::success(); |
7362 | auto ToType = importChecked(Err, From: E->getType()); |
7363 | auto BLoc = importChecked(Err, From: E->getBeginLoc()); |
7364 | auto RParenLoc = importChecked(Err, From: E->getEndLoc()); |
7365 | if (Err) |
7366 | return std::move(Err); |
7367 | auto ParentContextOrErr = Importer.ImportContext(FromDC: E->getParentContext()); |
7368 | if (!ParentContextOrErr) |
7369 | return ParentContextOrErr.takeError(); |
7370 | |
7371 | return new (Importer.getToContext()) |
7372 | SourceLocExpr(Importer.getToContext(), E->getIdentKind(), ToType, BLoc, |
7373 | RParenLoc, *ParentContextOrErr); |
7374 | } |
7375 | |
7376 | ExpectedStmt ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) { |
7377 | |
7378 | Error Err = Error::success(); |
7379 | auto ToBuiltinLoc = importChecked(Err, From: E->getBuiltinLoc()); |
7380 | auto ToSubExpr = importChecked(Err, From: E->getSubExpr()); |
7381 | auto ToWrittenTypeInfo = importChecked(Err, From: E->getWrittenTypeInfo()); |
7382 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
7383 | auto ToType = importChecked(Err, From: E->getType()); |
7384 | if (Err) |
7385 | return std::move(Err); |
7386 | |
7387 | return new (Importer.getToContext()) VAArgExpr( |
7388 | ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType, |
7389 | E->isMicrosoftABI()); |
7390 | } |
7391 | |
7392 | ExpectedStmt ASTNodeImporter::VisitChooseExpr(ChooseExpr *E) { |
7393 | |
7394 | Error Err = Error::success(); |
7395 | auto ToCond = importChecked(Err, From: E->getCond()); |
7396 | auto ToLHS = importChecked(Err, From: E->getLHS()); |
7397 | auto ToRHS = importChecked(Err, From: E->getRHS()); |
7398 | auto ToBuiltinLoc = importChecked(Err, From: E->getBuiltinLoc()); |
7399 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
7400 | auto ToType = importChecked(Err, From: E->getType()); |
7401 | if (Err) |
7402 | return std::move(Err); |
7403 | |
7404 | ExprValueKind VK = E->getValueKind(); |
7405 | ExprObjectKind OK = E->getObjectKind(); |
7406 | |
7407 | // The value of CondIsTrue only matters if the value is not |
7408 | // condition-dependent. |
7409 | bool CondIsTrue = !E->isConditionDependent() && E->isConditionTrue(); |
7410 | |
7411 | return new (Importer.getToContext()) |
7412 | ChooseExpr(ToBuiltinLoc, ToCond, ToLHS, ToRHS, ToType, VK, OK, |
7413 | ToRParenLoc, CondIsTrue); |
7414 | } |
7415 | |
7416 | ExpectedStmt ASTNodeImporter::VisitConvertVectorExpr(ConvertVectorExpr *E) { |
7417 | Error Err = Error::success(); |
7418 | auto *ToSrcExpr = importChecked(Err, From: E->getSrcExpr()); |
7419 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
7420 | auto ToBuiltinLoc = importChecked(Err, From: E->getBuiltinLoc()); |
7421 | auto ToType = importChecked(Err, From: E->getType()); |
7422 | auto *ToTSI = importChecked(Err, From: E->getTypeSourceInfo()); |
7423 | if (Err) |
7424 | return std::move(Err); |
7425 | |
7426 | return ConvertVectorExpr::Create( |
7427 | C: Importer.getToContext(), SrcExpr: ToSrcExpr, TI: ToTSI, DstType: ToType, VK: E->getValueKind(), |
7428 | OK: E->getObjectKind(), BuiltinLoc: ToBuiltinLoc, RParenLoc: ToRParenLoc, |
7429 | FPFeatures: E->getStoredFPFeaturesOrDefault()); |
7430 | } |
7431 | |
7432 | ExpectedStmt ASTNodeImporter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) { |
7433 | Error Err = Error::success(); |
7434 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
7435 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
7436 | auto ToType = importChecked(Err, From: E->getType()); |
7437 | const unsigned NumSubExprs = E->getNumSubExprs(); |
7438 | |
7439 | llvm::SmallVector<Expr *, 8> ToSubExprs; |
7440 | ArrayRef<Expr *> FromSubExprs(E->getSubExprs(), NumSubExprs); |
7441 | ToSubExprs.resize(N: NumSubExprs); |
7442 | |
7443 | if ((Err = ImportContainerChecked(InContainer: FromSubExprs, OutContainer&: ToSubExprs))) |
7444 | return std::move(Err); |
7445 | |
7446 | return new (Importer.getToContext()) ShuffleVectorExpr( |
7447 | Importer.getToContext(), ToSubExprs, ToType, ToBeginLoc, ToRParenLoc); |
7448 | } |
7449 | |
7450 | ExpectedStmt ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) { |
7451 | ExpectedType TypeOrErr = import(From: E->getType()); |
7452 | if (!TypeOrErr) |
7453 | return TypeOrErr.takeError(); |
7454 | |
7455 | ExpectedSLoc BeginLocOrErr = import(From: E->getBeginLoc()); |
7456 | if (!BeginLocOrErr) |
7457 | return BeginLocOrErr.takeError(); |
7458 | |
7459 | return new (Importer.getToContext()) GNUNullExpr(*TypeOrErr, *BeginLocOrErr); |
7460 | } |
7461 | |
7462 | ExpectedStmt |
7463 | ASTNodeImporter::VisitGenericSelectionExpr(GenericSelectionExpr *E) { |
7464 | Error Err = Error::success(); |
7465 | auto ToGenericLoc = importChecked(Err, From: E->getGenericLoc()); |
7466 | Expr *ToControllingExpr = nullptr; |
7467 | TypeSourceInfo *ToControllingType = nullptr; |
7468 | if (E->isExprPredicate()) |
7469 | ToControllingExpr = importChecked(Err, From: E->getControllingExpr()); |
7470 | else |
7471 | ToControllingType = importChecked(Err, From: E->getControllingType()); |
7472 | assert((ToControllingExpr || ToControllingType) && |
7473 | "Either the controlling expr or type must be nonnull" ); |
7474 | auto ToDefaultLoc = importChecked(Err, From: E->getDefaultLoc()); |
7475 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
7476 | if (Err) |
7477 | return std::move(Err); |
7478 | |
7479 | ArrayRef<const TypeSourceInfo *> FromAssocTypes(E->getAssocTypeSourceInfos()); |
7480 | SmallVector<TypeSourceInfo *, 1> ToAssocTypes(FromAssocTypes.size()); |
7481 | if (Error Err = ImportContainerChecked(InContainer: FromAssocTypes, OutContainer&: ToAssocTypes)) |
7482 | return std::move(Err); |
7483 | |
7484 | ArrayRef<const Expr *> FromAssocExprs(E->getAssocExprs()); |
7485 | SmallVector<Expr *, 1> ToAssocExprs(FromAssocExprs.size()); |
7486 | if (Error Err = ImportContainerChecked(InContainer: FromAssocExprs, OutContainer&: ToAssocExprs)) |
7487 | return std::move(Err); |
7488 | |
7489 | const ASTContext &ToCtx = Importer.getToContext(); |
7490 | if (E->isResultDependent()) { |
7491 | if (ToControllingExpr) { |
7492 | return GenericSelectionExpr::Create( |
7493 | Context: ToCtx, GenericLoc: ToGenericLoc, ControllingExpr: ToControllingExpr, AssocTypes: ArrayRef(ToAssocTypes), |
7494 | AssocExprs: ArrayRef(ToAssocExprs), DefaultLoc: ToDefaultLoc, RParenLoc: ToRParenLoc, |
7495 | ContainsUnexpandedParameterPack: E->containsUnexpandedParameterPack()); |
7496 | } |
7497 | return GenericSelectionExpr::Create( |
7498 | Context: ToCtx, GenericLoc: ToGenericLoc, ControllingType: ToControllingType, AssocTypes: ArrayRef(ToAssocTypes), |
7499 | AssocExprs: ArrayRef(ToAssocExprs), DefaultLoc: ToDefaultLoc, RParenLoc: ToRParenLoc, |
7500 | ContainsUnexpandedParameterPack: E->containsUnexpandedParameterPack()); |
7501 | } |
7502 | |
7503 | if (ToControllingExpr) { |
7504 | return GenericSelectionExpr::Create( |
7505 | Context: ToCtx, GenericLoc: ToGenericLoc, ControllingExpr: ToControllingExpr, AssocTypes: ArrayRef(ToAssocTypes), |
7506 | AssocExprs: ArrayRef(ToAssocExprs), DefaultLoc: ToDefaultLoc, RParenLoc: ToRParenLoc, |
7507 | ContainsUnexpandedParameterPack: E->containsUnexpandedParameterPack(), ResultIndex: E->getResultIndex()); |
7508 | } |
7509 | return GenericSelectionExpr::Create( |
7510 | Context: ToCtx, GenericLoc: ToGenericLoc, ControllingType: ToControllingType, AssocTypes: ArrayRef(ToAssocTypes), |
7511 | AssocExprs: ArrayRef(ToAssocExprs), DefaultLoc: ToDefaultLoc, RParenLoc: ToRParenLoc, |
7512 | ContainsUnexpandedParameterPack: E->containsUnexpandedParameterPack(), ResultIndex: E->getResultIndex()); |
7513 | } |
7514 | |
7515 | ExpectedStmt ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) { |
7516 | |
7517 | Error Err = Error::success(); |
7518 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
7519 | auto ToType = importChecked(Err, From: E->getType()); |
7520 | auto ToFunctionName = importChecked(Err, From: E->getFunctionName()); |
7521 | if (Err) |
7522 | return std::move(Err); |
7523 | |
7524 | return PredefinedExpr::Create(Ctx: Importer.getToContext(), L: ToBeginLoc, FNTy: ToType, |
7525 | IK: E->getIdentKind(), IsTransparent: E->isTransparent(), |
7526 | SL: ToFunctionName); |
7527 | } |
7528 | |
7529 | ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) { |
7530 | |
7531 | Error Err = Error::success(); |
7532 | auto ToQualifierLoc = importChecked(Err, From: E->getQualifierLoc()); |
7533 | auto ToTemplateKeywordLoc = importChecked(Err, From: E->getTemplateKeywordLoc()); |
7534 | auto ToDecl = importChecked(Err, From: E->getDecl()); |
7535 | auto ToLocation = importChecked(Err, From: E->getLocation()); |
7536 | auto ToType = importChecked(Err, From: E->getType()); |
7537 | if (Err) |
7538 | return std::move(Err); |
7539 | |
7540 | NamedDecl *ToFoundD = nullptr; |
7541 | if (E->getDecl() != E->getFoundDecl()) { |
7542 | auto FoundDOrErr = import(From: E->getFoundDecl()); |
7543 | if (!FoundDOrErr) |
7544 | return FoundDOrErr.takeError(); |
7545 | ToFoundD = *FoundDOrErr; |
7546 | } |
7547 | |
7548 | TemplateArgumentListInfo ToTAInfo; |
7549 | TemplateArgumentListInfo *ToResInfo = nullptr; |
7550 | if (E->hasExplicitTemplateArgs()) { |
7551 | if (Error Err = |
7552 | ImportTemplateArgumentListInfo(FromLAngleLoc: E->getLAngleLoc(), FromRAngleLoc: E->getRAngleLoc(), |
7553 | Container: E->template_arguments(), Result&: ToTAInfo)) |
7554 | return std::move(Err); |
7555 | ToResInfo = &ToTAInfo; |
7556 | } |
7557 | |
7558 | auto *ToE = DeclRefExpr::Create( |
7559 | Context: Importer.getToContext(), QualifierLoc: ToQualifierLoc, TemplateKWLoc: ToTemplateKeywordLoc, D: ToDecl, |
7560 | RefersToEnclosingVariableOrCapture: E->refersToEnclosingVariableOrCapture(), NameLoc: ToLocation, T: ToType, |
7561 | VK: E->getValueKind(), FoundD: ToFoundD, TemplateArgs: ToResInfo, NOUR: E->isNonOdrUse()); |
7562 | if (E->hadMultipleCandidates()) |
7563 | ToE->setHadMultipleCandidates(true); |
7564 | ToE->setIsImmediateEscalating(E->isImmediateEscalating()); |
7565 | return ToE; |
7566 | } |
7567 | |
7568 | ExpectedStmt ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { |
7569 | ExpectedType TypeOrErr = import(From: E->getType()); |
7570 | if (!TypeOrErr) |
7571 | return TypeOrErr.takeError(); |
7572 | |
7573 | return new (Importer.getToContext()) ImplicitValueInitExpr(*TypeOrErr); |
7574 | } |
7575 | |
7576 | ExpectedStmt ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *E) { |
7577 | ExpectedExpr ToInitOrErr = import(From: E->getInit()); |
7578 | if (!ToInitOrErr) |
7579 | return ToInitOrErr.takeError(); |
7580 | |
7581 | ExpectedSLoc ToEqualOrColonLocOrErr = import(From: E->getEqualOrColonLoc()); |
7582 | if (!ToEqualOrColonLocOrErr) |
7583 | return ToEqualOrColonLocOrErr.takeError(); |
7584 | |
7585 | SmallVector<Expr *, 4> ToIndexExprs(E->getNumSubExprs() - 1); |
7586 | // List elements from the second, the first is Init itself |
7587 | for (unsigned I = 1, N = E->getNumSubExprs(); I < N; I++) { |
7588 | if (ExpectedExpr ToArgOrErr = import(From: E->getSubExpr(Idx: I))) |
7589 | ToIndexExprs[I - 1] = *ToArgOrErr; |
7590 | else |
7591 | return ToArgOrErr.takeError(); |
7592 | } |
7593 | |
7594 | SmallVector<Designator, 4> ToDesignators(E->size()); |
7595 | if (Error Err = ImportContainerChecked(InContainer: E->designators(), OutContainer&: ToDesignators)) |
7596 | return std::move(Err); |
7597 | |
7598 | return DesignatedInitExpr::Create( |
7599 | C: Importer.getToContext(), Designators: ToDesignators, |
7600 | IndexExprs: ToIndexExprs, EqualOrColonLoc: *ToEqualOrColonLocOrErr, |
7601 | GNUSyntax: E->usesGNUSyntax(), Init: *ToInitOrErr); |
7602 | } |
7603 | |
7604 | ExpectedStmt |
7605 | ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) { |
7606 | ExpectedType ToTypeOrErr = import(From: E->getType()); |
7607 | if (!ToTypeOrErr) |
7608 | return ToTypeOrErr.takeError(); |
7609 | |
7610 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
7611 | if (!ToLocationOrErr) |
7612 | return ToLocationOrErr.takeError(); |
7613 | |
7614 | return new (Importer.getToContext()) CXXNullPtrLiteralExpr( |
7615 | *ToTypeOrErr, *ToLocationOrErr); |
7616 | } |
7617 | |
7618 | ExpectedStmt ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) { |
7619 | ExpectedType ToTypeOrErr = import(From: E->getType()); |
7620 | if (!ToTypeOrErr) |
7621 | return ToTypeOrErr.takeError(); |
7622 | |
7623 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
7624 | if (!ToLocationOrErr) |
7625 | return ToLocationOrErr.takeError(); |
7626 | |
7627 | return IntegerLiteral::Create( |
7628 | C: Importer.getToContext(), V: E->getValue(), type: *ToTypeOrErr, l: *ToLocationOrErr); |
7629 | } |
7630 | |
7631 | |
7632 | ExpectedStmt ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) { |
7633 | ExpectedType ToTypeOrErr = import(From: E->getType()); |
7634 | if (!ToTypeOrErr) |
7635 | return ToTypeOrErr.takeError(); |
7636 | |
7637 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
7638 | if (!ToLocationOrErr) |
7639 | return ToLocationOrErr.takeError(); |
7640 | |
7641 | return FloatingLiteral::Create( |
7642 | C: Importer.getToContext(), V: E->getValue(), isexact: E->isExact(), |
7643 | Type: *ToTypeOrErr, L: *ToLocationOrErr); |
7644 | } |
7645 | |
7646 | ExpectedStmt ASTNodeImporter::VisitImaginaryLiteral(ImaginaryLiteral *E) { |
7647 | auto ToTypeOrErr = import(From: E->getType()); |
7648 | if (!ToTypeOrErr) |
7649 | return ToTypeOrErr.takeError(); |
7650 | |
7651 | ExpectedExpr ToSubExprOrErr = import(From: E->getSubExpr()); |
7652 | if (!ToSubExprOrErr) |
7653 | return ToSubExprOrErr.takeError(); |
7654 | |
7655 | return new (Importer.getToContext()) ImaginaryLiteral( |
7656 | *ToSubExprOrErr, *ToTypeOrErr); |
7657 | } |
7658 | |
7659 | ExpectedStmt ASTNodeImporter::VisitFixedPointLiteral(FixedPointLiteral *E) { |
7660 | auto ToTypeOrErr = import(From: E->getType()); |
7661 | if (!ToTypeOrErr) |
7662 | return ToTypeOrErr.takeError(); |
7663 | |
7664 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
7665 | if (!ToLocationOrErr) |
7666 | return ToLocationOrErr.takeError(); |
7667 | |
7668 | return new (Importer.getToContext()) FixedPointLiteral( |
7669 | Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr, |
7670 | Importer.getToContext().getFixedPointScale(Ty: *ToTypeOrErr)); |
7671 | } |
7672 | |
7673 | ExpectedStmt ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) { |
7674 | ExpectedType ToTypeOrErr = import(From: E->getType()); |
7675 | if (!ToTypeOrErr) |
7676 | return ToTypeOrErr.takeError(); |
7677 | |
7678 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
7679 | if (!ToLocationOrErr) |
7680 | return ToLocationOrErr.takeError(); |
7681 | |
7682 | return new (Importer.getToContext()) CharacterLiteral( |
7683 | E->getValue(), E->getKind(), *ToTypeOrErr, *ToLocationOrErr); |
7684 | } |
7685 | |
7686 | ExpectedStmt ASTNodeImporter::VisitStringLiteral(StringLiteral *E) { |
7687 | ExpectedType ToTypeOrErr = import(From: E->getType()); |
7688 | if (!ToTypeOrErr) |
7689 | return ToTypeOrErr.takeError(); |
7690 | |
7691 | SmallVector<SourceLocation, 4> ToLocations(E->getNumConcatenated()); |
7692 | if (Error Err = ImportArrayChecked( |
7693 | Ibegin: E->tokloc_begin(), Iend: E->tokloc_end(), Obegin: ToLocations.begin())) |
7694 | return std::move(Err); |
7695 | |
7696 | return StringLiteral::Create(Ctx: Importer.getToContext(), Str: E->getBytes(), |
7697 | Kind: E->getKind(), Pascal: E->isPascal(), Ty: *ToTypeOrErr, |
7698 | Locs: ToLocations); |
7699 | } |
7700 | |
7701 | ExpectedStmt ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
7702 | |
7703 | Error Err = Error::success(); |
7704 | auto ToLParenLoc = importChecked(Err, From: E->getLParenLoc()); |
7705 | auto ToTypeSourceInfo = importChecked(Err, From: E->getTypeSourceInfo()); |
7706 | auto ToType = importChecked(Err, From: E->getType()); |
7707 | auto ToInitializer = importChecked(Err, From: E->getInitializer()); |
7708 | if (Err) |
7709 | return std::move(Err); |
7710 | |
7711 | return new (Importer.getToContext()) CompoundLiteralExpr( |
7712 | ToLParenLoc, ToTypeSourceInfo, ToType, E->getValueKind(), |
7713 | ToInitializer, E->isFileScope()); |
7714 | } |
7715 | |
7716 | ExpectedStmt ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) { |
7717 | |
7718 | Error Err = Error::success(); |
7719 | auto ToBuiltinLoc = importChecked(Err, From: E->getBuiltinLoc()); |
7720 | auto ToType = importChecked(Err, From: E->getType()); |
7721 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
7722 | if (Err) |
7723 | return std::move(Err); |
7724 | |
7725 | SmallVector<Expr *, 6> ToExprs(E->getNumSubExprs()); |
7726 | if (Error Err = ImportArrayChecked( |
7727 | Ibegin: E->getSubExprs(), Iend: E->getSubExprs() + E->getNumSubExprs(), |
7728 | Obegin: ToExprs.begin())) |
7729 | return std::move(Err); |
7730 | |
7731 | return new (Importer.getToContext()) AtomicExpr( |
7732 | |
7733 | ToBuiltinLoc, ToExprs, ToType, E->getOp(), ToRParenLoc); |
7734 | } |
7735 | |
7736 | ExpectedStmt ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) { |
7737 | Error Err = Error::success(); |
7738 | auto ToAmpAmpLoc = importChecked(Err, From: E->getAmpAmpLoc()); |
7739 | auto ToLabelLoc = importChecked(Err, From: E->getLabelLoc()); |
7740 | auto ToLabel = importChecked(Err, From: E->getLabel()); |
7741 | auto ToType = importChecked(Err, From: E->getType()); |
7742 | if (Err) |
7743 | return std::move(Err); |
7744 | |
7745 | return new (Importer.getToContext()) AddrLabelExpr( |
7746 | ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType); |
7747 | } |
7748 | ExpectedStmt ASTNodeImporter::VisitConstantExpr(ConstantExpr *E) { |
7749 | Error Err = Error::success(); |
7750 | auto ToSubExpr = importChecked(Err, From: E->getSubExpr()); |
7751 | auto ToResult = importChecked(Err, From: E->getAPValueResult()); |
7752 | if (Err) |
7753 | return std::move(Err); |
7754 | |
7755 | return ConstantExpr::Create(Context: Importer.getToContext(), E: ToSubExpr, Result: ToResult); |
7756 | } |
7757 | ExpectedStmt ASTNodeImporter::VisitParenExpr(ParenExpr *E) { |
7758 | Error Err = Error::success(); |
7759 | auto ToLParen = importChecked(Err, From: E->getLParen()); |
7760 | auto ToRParen = importChecked(Err, From: E->getRParen()); |
7761 | auto ToSubExpr = importChecked(Err, From: E->getSubExpr()); |
7762 | if (Err) |
7763 | return std::move(Err); |
7764 | |
7765 | return new (Importer.getToContext()) |
7766 | ParenExpr(ToLParen, ToRParen, ToSubExpr); |
7767 | } |
7768 | |
7769 | ExpectedStmt ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) { |
7770 | SmallVector<Expr *, 4> ToExprs(E->getNumExprs()); |
7771 | if (Error Err = ImportContainerChecked(InContainer: E->exprs(), OutContainer&: ToExprs)) |
7772 | return std::move(Err); |
7773 | |
7774 | ExpectedSLoc ToLParenLocOrErr = import(From: E->getLParenLoc()); |
7775 | if (!ToLParenLocOrErr) |
7776 | return ToLParenLocOrErr.takeError(); |
7777 | |
7778 | ExpectedSLoc ToRParenLocOrErr = import(From: E->getRParenLoc()); |
7779 | if (!ToRParenLocOrErr) |
7780 | return ToRParenLocOrErr.takeError(); |
7781 | |
7782 | return ParenListExpr::Create(Ctx: Importer.getToContext(), LParenLoc: *ToLParenLocOrErr, |
7783 | Exprs: ToExprs, RParenLoc: *ToRParenLocOrErr); |
7784 | } |
7785 | |
7786 | ExpectedStmt ASTNodeImporter::VisitStmtExpr(StmtExpr *E) { |
7787 | Error Err = Error::success(); |
7788 | auto ToSubStmt = importChecked(Err, From: E->getSubStmt()); |
7789 | auto ToType = importChecked(Err, From: E->getType()); |
7790 | auto ToLParenLoc = importChecked(Err, From: E->getLParenLoc()); |
7791 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
7792 | if (Err) |
7793 | return std::move(Err); |
7794 | |
7795 | return new (Importer.getToContext()) |
7796 | StmtExpr(ToSubStmt, ToType, ToLParenLoc, ToRParenLoc, |
7797 | E->getTemplateDepth()); |
7798 | } |
7799 | |
7800 | ExpectedStmt ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) { |
7801 | Error Err = Error::success(); |
7802 | auto ToSubExpr = importChecked(Err, From: E->getSubExpr()); |
7803 | auto ToType = importChecked(Err, From: E->getType()); |
7804 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
7805 | if (Err) |
7806 | return std::move(Err); |
7807 | |
7808 | auto *UO = UnaryOperator::CreateEmpty(C: Importer.getToContext(), |
7809 | hasFPFeatures: E->hasStoredFPFeatures()); |
7810 | UO->setType(ToType); |
7811 | UO->setSubExpr(ToSubExpr); |
7812 | UO->setOpcode(E->getOpcode()); |
7813 | UO->setOperatorLoc(ToOperatorLoc); |
7814 | UO->setCanOverflow(E->canOverflow()); |
7815 | if (E->hasStoredFPFeatures()) |
7816 | UO->setStoredFPFeatures(E->getStoredFPFeatures()); |
7817 | |
7818 | return UO; |
7819 | } |
7820 | |
7821 | ExpectedStmt |
7822 | |
7823 | ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) { |
7824 | Error Err = Error::success(); |
7825 | auto ToType = importChecked(Err, From: E->getType()); |
7826 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
7827 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
7828 | if (Err) |
7829 | return std::move(Err); |
7830 | |
7831 | if (E->isArgumentType()) { |
7832 | Expected<TypeSourceInfo *> ToArgumentTypeInfoOrErr = |
7833 | import(From: E->getArgumentTypeInfo()); |
7834 | if (!ToArgumentTypeInfoOrErr) |
7835 | return ToArgumentTypeInfoOrErr.takeError(); |
7836 | |
7837 | return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr( |
7838 | E->getKind(), *ToArgumentTypeInfoOrErr, ToType, ToOperatorLoc, |
7839 | ToRParenLoc); |
7840 | } |
7841 | |
7842 | ExpectedExpr ToArgumentExprOrErr = import(From: E->getArgumentExpr()); |
7843 | if (!ToArgumentExprOrErr) |
7844 | return ToArgumentExprOrErr.takeError(); |
7845 | |
7846 | return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr( |
7847 | E->getKind(), *ToArgumentExprOrErr, ToType, ToOperatorLoc, ToRParenLoc); |
7848 | } |
7849 | |
7850 | ExpectedStmt ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) { |
7851 | Error Err = Error::success(); |
7852 | auto ToLHS = importChecked(Err, From: E->getLHS()); |
7853 | auto ToRHS = importChecked(Err, From: E->getRHS()); |
7854 | auto ToType = importChecked(Err, From: E->getType()); |
7855 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
7856 | if (Err) |
7857 | return std::move(Err); |
7858 | |
7859 | return BinaryOperator::Create( |
7860 | C: Importer.getToContext(), lhs: ToLHS, rhs: ToRHS, opc: E->getOpcode(), ResTy: ToType, |
7861 | VK: E->getValueKind(), OK: E->getObjectKind(), opLoc: ToOperatorLoc, |
7862 | FPFeatures: E->getFPFeatures()); |
7863 | } |
7864 | |
7865 | ExpectedStmt ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) { |
7866 | Error Err = Error::success(); |
7867 | auto ToCond = importChecked(Err, From: E->getCond()); |
7868 | auto ToQuestionLoc = importChecked(Err, From: E->getQuestionLoc()); |
7869 | auto ToLHS = importChecked(Err, From: E->getLHS()); |
7870 | auto ToColonLoc = importChecked(Err, From: E->getColonLoc()); |
7871 | auto ToRHS = importChecked(Err, From: E->getRHS()); |
7872 | auto ToType = importChecked(Err, From: E->getType()); |
7873 | if (Err) |
7874 | return std::move(Err); |
7875 | |
7876 | return new (Importer.getToContext()) ConditionalOperator( |
7877 | ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType, |
7878 | E->getValueKind(), E->getObjectKind()); |
7879 | } |
7880 | |
7881 | ExpectedStmt |
7882 | ASTNodeImporter::VisitBinaryConditionalOperator(BinaryConditionalOperator *E) { |
7883 | Error Err = Error::success(); |
7884 | auto ToCommon = importChecked(Err, From: E->getCommon()); |
7885 | auto ToOpaqueValue = importChecked(Err, From: E->getOpaqueValue()); |
7886 | auto ToCond = importChecked(Err, From: E->getCond()); |
7887 | auto ToTrueExpr = importChecked(Err, From: E->getTrueExpr()); |
7888 | auto ToFalseExpr = importChecked(Err, From: E->getFalseExpr()); |
7889 | auto ToQuestionLoc = importChecked(Err, From: E->getQuestionLoc()); |
7890 | auto ToColonLoc = importChecked(Err, From: E->getColonLoc()); |
7891 | auto ToType = importChecked(Err, From: E->getType()); |
7892 | if (Err) |
7893 | return std::move(Err); |
7894 | |
7895 | return new (Importer.getToContext()) BinaryConditionalOperator( |
7896 | ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr, |
7897 | ToQuestionLoc, ToColonLoc, ToType, E->getValueKind(), |
7898 | E->getObjectKind()); |
7899 | } |
7900 | |
7901 | ExpectedStmt ASTNodeImporter::VisitCXXRewrittenBinaryOperator( |
7902 | CXXRewrittenBinaryOperator *E) { |
7903 | Error Err = Error::success(); |
7904 | auto ToSemanticForm = importChecked(Err, From: E->getSemanticForm()); |
7905 | if (Err) |
7906 | return std::move(Err); |
7907 | |
7908 | return new (Importer.getToContext()) |
7909 | CXXRewrittenBinaryOperator(ToSemanticForm, E->isReversed()); |
7910 | } |
7911 | |
7912 | ExpectedStmt ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { |
7913 | Error Err = Error::success(); |
7914 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
7915 | auto ToQueriedTypeSourceInfo = |
7916 | importChecked(Err, From: E->getQueriedTypeSourceInfo()); |
7917 | auto ToDimensionExpression = importChecked(Err, From: E->getDimensionExpression()); |
7918 | auto ToEndLoc = importChecked(Err, From: E->getEndLoc()); |
7919 | auto ToType = importChecked(Err, From: E->getType()); |
7920 | if (Err) |
7921 | return std::move(Err); |
7922 | |
7923 | return new (Importer.getToContext()) ArrayTypeTraitExpr( |
7924 | ToBeginLoc, E->getTrait(), ToQueriedTypeSourceInfo, E->getValue(), |
7925 | ToDimensionExpression, ToEndLoc, ToType); |
7926 | } |
7927 | |
7928 | ExpectedStmt ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { |
7929 | Error Err = Error::success(); |
7930 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
7931 | auto ToQueriedExpression = importChecked(Err, From: E->getQueriedExpression()); |
7932 | auto ToEndLoc = importChecked(Err, From: E->getEndLoc()); |
7933 | auto ToType = importChecked(Err, From: E->getType()); |
7934 | if (Err) |
7935 | return std::move(Err); |
7936 | |
7937 | return new (Importer.getToContext()) ExpressionTraitExpr( |
7938 | ToBeginLoc, E->getTrait(), ToQueriedExpression, E->getValue(), |
7939 | ToEndLoc, ToType); |
7940 | } |
7941 | |
7942 | ExpectedStmt ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) { |
7943 | Error Err = Error::success(); |
7944 | auto ToLocation = importChecked(Err, From: E->getLocation()); |
7945 | auto ToType = importChecked(Err, From: E->getType()); |
7946 | auto ToSourceExpr = importChecked(Err, From: E->getSourceExpr()); |
7947 | if (Err) |
7948 | return std::move(Err); |
7949 | |
7950 | return new (Importer.getToContext()) OpaqueValueExpr( |
7951 | ToLocation, ToType, E->getValueKind(), E->getObjectKind(), ToSourceExpr); |
7952 | } |
7953 | |
7954 | ExpectedStmt ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { |
7955 | Error Err = Error::success(); |
7956 | auto ToLHS = importChecked(Err, From: E->getLHS()); |
7957 | auto ToRHS = importChecked(Err, From: E->getRHS()); |
7958 | auto ToType = importChecked(Err, From: E->getType()); |
7959 | auto ToRBracketLoc = importChecked(Err, From: E->getRBracketLoc()); |
7960 | if (Err) |
7961 | return std::move(Err); |
7962 | |
7963 | return new (Importer.getToContext()) ArraySubscriptExpr( |
7964 | ToLHS, ToRHS, ToType, E->getValueKind(), E->getObjectKind(), |
7965 | ToRBracketLoc); |
7966 | } |
7967 | |
7968 | ExpectedStmt |
7969 | ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) { |
7970 | Error Err = Error::success(); |
7971 | auto ToLHS = importChecked(Err, From: E->getLHS()); |
7972 | auto ToRHS = importChecked(Err, From: E->getRHS()); |
7973 | auto ToType = importChecked(Err, From: E->getType()); |
7974 | auto ToComputationLHSType = importChecked(Err, From: E->getComputationLHSType()); |
7975 | auto ToComputationResultType = |
7976 | importChecked(Err, From: E->getComputationResultType()); |
7977 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
7978 | if (Err) |
7979 | return std::move(Err); |
7980 | |
7981 | return CompoundAssignOperator::Create( |
7982 | C: Importer.getToContext(), lhs: ToLHS, rhs: ToRHS, opc: E->getOpcode(), ResTy: ToType, |
7983 | VK: E->getValueKind(), OK: E->getObjectKind(), opLoc: ToOperatorLoc, |
7984 | FPFeatures: E->getFPFeatures(), |
7985 | CompLHSType: ToComputationLHSType, CompResultType: ToComputationResultType); |
7986 | } |
7987 | |
7988 | Expected<CXXCastPath> |
7989 | ASTNodeImporter::ImportCastPath(CastExpr *CE) { |
7990 | CXXCastPath Path; |
7991 | for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) { |
7992 | if (auto SpecOrErr = import(From: *I)) |
7993 | Path.push_back(Elt: *SpecOrErr); |
7994 | else |
7995 | return SpecOrErr.takeError(); |
7996 | } |
7997 | return Path; |
7998 | } |
7999 | |
8000 | ExpectedStmt ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) { |
8001 | ExpectedType ToTypeOrErr = import(From: E->getType()); |
8002 | if (!ToTypeOrErr) |
8003 | return ToTypeOrErr.takeError(); |
8004 | |
8005 | ExpectedExpr ToSubExprOrErr = import(From: E->getSubExpr()); |
8006 | if (!ToSubExprOrErr) |
8007 | return ToSubExprOrErr.takeError(); |
8008 | |
8009 | Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(CE: E); |
8010 | if (!ToBasePathOrErr) |
8011 | return ToBasePathOrErr.takeError(); |
8012 | |
8013 | return ImplicitCastExpr::Create( |
8014 | Context: Importer.getToContext(), T: *ToTypeOrErr, Kind: E->getCastKind(), Operand: *ToSubExprOrErr, |
8015 | BasePath: &(*ToBasePathOrErr), Cat: E->getValueKind(), FPO: E->getFPFeatures()); |
8016 | } |
8017 | |
8018 | ExpectedStmt ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) { |
8019 | Error Err = Error::success(); |
8020 | auto ToType = importChecked(Err, From: E->getType()); |
8021 | auto ToSubExpr = importChecked(Err, From: E->getSubExpr()); |
8022 | auto ToTypeInfoAsWritten = importChecked(Err, From: E->getTypeInfoAsWritten()); |
8023 | if (Err) |
8024 | return std::move(Err); |
8025 | |
8026 | Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(CE: E); |
8027 | if (!ToBasePathOrErr) |
8028 | return ToBasePathOrErr.takeError(); |
8029 | CXXCastPath *ToBasePath = &(*ToBasePathOrErr); |
8030 | |
8031 | switch (E->getStmtClass()) { |
8032 | case Stmt::CStyleCastExprClass: { |
8033 | auto *CCE = cast<CStyleCastExpr>(Val: E); |
8034 | ExpectedSLoc ToLParenLocOrErr = import(From: CCE->getLParenLoc()); |
8035 | if (!ToLParenLocOrErr) |
8036 | return ToLParenLocOrErr.takeError(); |
8037 | ExpectedSLoc ToRParenLocOrErr = import(From: CCE->getRParenLoc()); |
8038 | if (!ToRParenLocOrErr) |
8039 | return ToRParenLocOrErr.takeError(); |
8040 | return CStyleCastExpr::Create( |
8041 | Context: Importer.getToContext(), T: ToType, VK: E->getValueKind(), K: E->getCastKind(), |
8042 | Op: ToSubExpr, BasePath: ToBasePath, FPO: CCE->getFPFeatures(), WrittenTy: ToTypeInfoAsWritten, |
8043 | L: *ToLParenLocOrErr, R: *ToRParenLocOrErr); |
8044 | } |
8045 | |
8046 | case Stmt::CXXFunctionalCastExprClass: { |
8047 | auto *FCE = cast<CXXFunctionalCastExpr>(Val: E); |
8048 | ExpectedSLoc ToLParenLocOrErr = import(From: FCE->getLParenLoc()); |
8049 | if (!ToLParenLocOrErr) |
8050 | return ToLParenLocOrErr.takeError(); |
8051 | ExpectedSLoc ToRParenLocOrErr = import(From: FCE->getRParenLoc()); |
8052 | if (!ToRParenLocOrErr) |
8053 | return ToRParenLocOrErr.takeError(); |
8054 | return CXXFunctionalCastExpr::Create( |
8055 | Context: Importer.getToContext(), T: ToType, VK: E->getValueKind(), Written: ToTypeInfoAsWritten, |
8056 | Kind: E->getCastKind(), Op: ToSubExpr, Path: ToBasePath, FPO: FCE->getFPFeatures(), |
8057 | LPLoc: *ToLParenLocOrErr, RPLoc: *ToRParenLocOrErr); |
8058 | } |
8059 | |
8060 | case Stmt::ObjCBridgedCastExprClass: { |
8061 | auto *OCE = cast<ObjCBridgedCastExpr>(Val: E); |
8062 | ExpectedSLoc ToLParenLocOrErr = import(From: OCE->getLParenLoc()); |
8063 | if (!ToLParenLocOrErr) |
8064 | return ToLParenLocOrErr.takeError(); |
8065 | ExpectedSLoc ToBridgeKeywordLocOrErr = import(From: OCE->getBridgeKeywordLoc()); |
8066 | if (!ToBridgeKeywordLocOrErr) |
8067 | return ToBridgeKeywordLocOrErr.takeError(); |
8068 | return new (Importer.getToContext()) ObjCBridgedCastExpr( |
8069 | *ToLParenLocOrErr, OCE->getBridgeKind(), E->getCastKind(), |
8070 | *ToBridgeKeywordLocOrErr, ToTypeInfoAsWritten, ToSubExpr); |
8071 | } |
8072 | case Stmt::BuiltinBitCastExprClass: { |
8073 | auto *BBC = cast<BuiltinBitCastExpr>(Val: E); |
8074 | ExpectedSLoc ToKWLocOrErr = import(From: BBC->getBeginLoc()); |
8075 | if (!ToKWLocOrErr) |
8076 | return ToKWLocOrErr.takeError(); |
8077 | ExpectedSLoc ToRParenLocOrErr = import(From: BBC->getEndLoc()); |
8078 | if (!ToRParenLocOrErr) |
8079 | return ToRParenLocOrErr.takeError(); |
8080 | return new (Importer.getToContext()) BuiltinBitCastExpr( |
8081 | ToType, E->getValueKind(), E->getCastKind(), ToSubExpr, |
8082 | ToTypeInfoAsWritten, *ToKWLocOrErr, *ToRParenLocOrErr); |
8083 | } |
8084 | default: |
8085 | llvm_unreachable("Cast expression of unsupported type!" ); |
8086 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
8087 | } |
8088 | } |
8089 | |
8090 | ExpectedStmt ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *E) { |
8091 | SmallVector<OffsetOfNode, 4> ToNodes; |
8092 | for (int I = 0, N = E->getNumComponents(); I < N; ++I) { |
8093 | const OffsetOfNode &FromNode = E->getComponent(Idx: I); |
8094 | |
8095 | SourceLocation ToBeginLoc, ToEndLoc; |
8096 | |
8097 | if (FromNode.getKind() != OffsetOfNode::Base) { |
8098 | Error Err = Error::success(); |
8099 | ToBeginLoc = importChecked(Err, From: FromNode.getBeginLoc()); |
8100 | ToEndLoc = importChecked(Err, From: FromNode.getEndLoc()); |
8101 | if (Err) |
8102 | return std::move(Err); |
8103 | } |
8104 | |
8105 | switch (FromNode.getKind()) { |
8106 | case OffsetOfNode::Array: |
8107 | ToNodes.push_back( |
8108 | Elt: OffsetOfNode(ToBeginLoc, FromNode.getArrayExprIndex(), ToEndLoc)); |
8109 | break; |
8110 | case OffsetOfNode::Base: { |
8111 | auto ToBSOrErr = import(From: FromNode.getBase()); |
8112 | if (!ToBSOrErr) |
8113 | return ToBSOrErr.takeError(); |
8114 | ToNodes.push_back(Elt: OffsetOfNode(*ToBSOrErr)); |
8115 | break; |
8116 | } |
8117 | case OffsetOfNode::Field: { |
8118 | auto ToFieldOrErr = import(From: FromNode.getField()); |
8119 | if (!ToFieldOrErr) |
8120 | return ToFieldOrErr.takeError(); |
8121 | ToNodes.push_back(Elt: OffsetOfNode(ToBeginLoc, *ToFieldOrErr, ToEndLoc)); |
8122 | break; |
8123 | } |
8124 | case OffsetOfNode::Identifier: { |
8125 | IdentifierInfo *ToII = Importer.Import(FromId: FromNode.getFieldName()); |
8126 | ToNodes.push_back(Elt: OffsetOfNode(ToBeginLoc, ToII, ToEndLoc)); |
8127 | break; |
8128 | } |
8129 | } |
8130 | } |
8131 | |
8132 | SmallVector<Expr *, 4> ToExprs(E->getNumExpressions()); |
8133 | for (int I = 0, N = E->getNumExpressions(); I < N; ++I) { |
8134 | ExpectedExpr ToIndexExprOrErr = import(From: E->getIndexExpr(Idx: I)); |
8135 | if (!ToIndexExprOrErr) |
8136 | return ToIndexExprOrErr.takeError(); |
8137 | ToExprs[I] = *ToIndexExprOrErr; |
8138 | } |
8139 | |
8140 | Error Err = Error::success(); |
8141 | auto ToType = importChecked(Err, From: E->getType()); |
8142 | auto ToTypeSourceInfo = importChecked(Err, From: E->getTypeSourceInfo()); |
8143 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
8144 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
8145 | if (Err) |
8146 | return std::move(Err); |
8147 | |
8148 | return OffsetOfExpr::Create( |
8149 | C: Importer.getToContext(), type: ToType, OperatorLoc: ToOperatorLoc, tsi: ToTypeSourceInfo, comps: ToNodes, |
8150 | exprs: ToExprs, RParenLoc: ToRParenLoc); |
8151 | } |
8152 | |
8153 | ExpectedStmt ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { |
8154 | Error Err = Error::success(); |
8155 | auto ToType = importChecked(Err, From: E->getType()); |
8156 | auto ToOperand = importChecked(Err, From: E->getOperand()); |
8157 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
8158 | auto ToEndLoc = importChecked(Err, From: E->getEndLoc()); |
8159 | if (Err) |
8160 | return std::move(Err); |
8161 | |
8162 | CanThrowResult ToCanThrow; |
8163 | if (E->isValueDependent()) |
8164 | ToCanThrow = CT_Dependent; |
8165 | else |
8166 | ToCanThrow = E->getValue() ? CT_Can : CT_Cannot; |
8167 | |
8168 | return new (Importer.getToContext()) CXXNoexceptExpr( |
8169 | ToType, ToOperand, ToCanThrow, ToBeginLoc, ToEndLoc); |
8170 | } |
8171 | |
8172 | ExpectedStmt ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) { |
8173 | Error Err = Error::success(); |
8174 | auto ToSubExpr = importChecked(Err, From: E->getSubExpr()); |
8175 | auto ToType = importChecked(Err, From: E->getType()); |
8176 | auto ToThrowLoc = importChecked(Err, From: E->getThrowLoc()); |
8177 | if (Err) |
8178 | return std::move(Err); |
8179 | |
8180 | return new (Importer.getToContext()) CXXThrowExpr( |
8181 | ToSubExpr, ToType, ToThrowLoc, E->isThrownVariableInScope()); |
8182 | } |
8183 | |
8184 | ExpectedStmt ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { |
8185 | ExpectedSLoc ToUsedLocOrErr = import(From: E->getUsedLocation()); |
8186 | if (!ToUsedLocOrErr) |
8187 | return ToUsedLocOrErr.takeError(); |
8188 | |
8189 | auto ToParamOrErr = import(From: E->getParam()); |
8190 | if (!ToParamOrErr) |
8191 | return ToParamOrErr.takeError(); |
8192 | |
8193 | auto UsedContextOrErr = Importer.ImportContext(FromDC: E->getUsedContext()); |
8194 | if (!UsedContextOrErr) |
8195 | return UsedContextOrErr.takeError(); |
8196 | |
8197 | // Import the default arg if it was not imported yet. |
8198 | // This is needed because it can happen that during the import of the |
8199 | // default expression (from VisitParmVarDecl) the same ParmVarDecl is |
8200 | // encountered here. The default argument for a ParmVarDecl is set in the |
8201 | // ParmVarDecl only after it is imported (set in VisitParmVarDecl if not here, |
8202 | // see VisitParmVarDecl). |
8203 | ParmVarDecl *ToParam = *ToParamOrErr; |
8204 | if (!ToParam->getDefaultArg()) { |
8205 | std::optional<ParmVarDecl *> FromParam = |
8206 | Importer.getImportedFromDecl(ToD: ToParam); |
8207 | assert(FromParam && "ParmVarDecl was not imported?" ); |
8208 | |
8209 | if (Error Err = ImportDefaultArgOfParmVarDecl(FromParam: *FromParam, ToParam)) |
8210 | return std::move(Err); |
8211 | } |
8212 | Expr *RewrittenInit = nullptr; |
8213 | if (E->hasRewrittenInit()) { |
8214 | ExpectedExpr ExprOrErr = import(From: E->getRewrittenExpr()); |
8215 | if (!ExprOrErr) |
8216 | return ExprOrErr.takeError(); |
8217 | RewrittenInit = ExprOrErr.get(); |
8218 | } |
8219 | return CXXDefaultArgExpr::Create(C: Importer.getToContext(), Loc: *ToUsedLocOrErr, |
8220 | Param: *ToParamOrErr, RewrittenExpr: RewrittenInit, |
8221 | UsedContext: *UsedContextOrErr); |
8222 | } |
8223 | |
8224 | ExpectedStmt |
8225 | ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { |
8226 | Error Err = Error::success(); |
8227 | auto ToType = importChecked(Err, From: E->getType()); |
8228 | auto ToTypeSourceInfo = importChecked(Err, From: E->getTypeSourceInfo()); |
8229 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
8230 | if (Err) |
8231 | return std::move(Err); |
8232 | |
8233 | return new (Importer.getToContext()) CXXScalarValueInitExpr( |
8234 | ToType, ToTypeSourceInfo, ToRParenLoc); |
8235 | } |
8236 | |
8237 | ExpectedStmt |
8238 | ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { |
8239 | ExpectedExpr ToSubExprOrErr = import(From: E->getSubExpr()); |
8240 | if (!ToSubExprOrErr) |
8241 | return ToSubExprOrErr.takeError(); |
8242 | |
8243 | auto ToDtorOrErr = import(From: E->getTemporary()->getDestructor()); |
8244 | if (!ToDtorOrErr) |
8245 | return ToDtorOrErr.takeError(); |
8246 | |
8247 | ASTContext &ToCtx = Importer.getToContext(); |
8248 | CXXTemporary *Temp = CXXTemporary::Create(C: ToCtx, Destructor: *ToDtorOrErr); |
8249 | return CXXBindTemporaryExpr::Create(C: ToCtx, Temp, SubExpr: *ToSubExprOrErr); |
8250 | } |
8251 | |
8252 | ExpectedStmt |
8253 | |
8254 | ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) { |
8255 | Error Err = Error::success(); |
8256 | auto ToConstructor = importChecked(Err, From: E->getConstructor()); |
8257 | auto ToType = importChecked(Err, From: E->getType()); |
8258 | auto ToTypeSourceInfo = importChecked(Err, From: E->getTypeSourceInfo()); |
8259 | auto ToParenOrBraceRange = importChecked(Err, From: E->getParenOrBraceRange()); |
8260 | if (Err) |
8261 | return std::move(Err); |
8262 | |
8263 | SmallVector<Expr *, 8> ToArgs(E->getNumArgs()); |
8264 | if (Error Err = ImportContainerChecked(InContainer: E->arguments(), OutContainer&: ToArgs)) |
8265 | return std::move(Err); |
8266 | |
8267 | return CXXTemporaryObjectExpr::Create( |
8268 | Ctx: Importer.getToContext(), Cons: ToConstructor, Ty: ToType, TSI: ToTypeSourceInfo, Args: ToArgs, |
8269 | ParenOrBraceRange: ToParenOrBraceRange, HadMultipleCandidates: E->hadMultipleCandidates(), |
8270 | ListInitialization: E->isListInitialization(), StdInitListInitialization: E->isStdInitListInitialization(), |
8271 | ZeroInitialization: E->requiresZeroInitialization()); |
8272 | } |
8273 | |
8274 | ExpectedDecl ASTNodeImporter::VisitLifetimeExtendedTemporaryDecl( |
8275 | LifetimeExtendedTemporaryDecl *D) { |
8276 | DeclContext *DC, *LexicalDC; |
8277 | if (Error Err = ImportDeclContext(FromD: D, ToDC&: DC, ToLexicalDC&: LexicalDC)) |
8278 | return std::move(Err); |
8279 | |
8280 | Error Err = Error::success(); |
8281 | auto Temporary = importChecked(Err, From: D->getTemporaryExpr()); |
8282 | auto ExtendingDecl = importChecked(Err, From: D->getExtendingDecl()); |
8283 | if (Err) |
8284 | return std::move(Err); |
8285 | // FIXME: Should ManglingNumber get numbers associated with 'to' context? |
8286 | |
8287 | LifetimeExtendedTemporaryDecl *To; |
8288 | if (GetImportedOrCreateDecl(ToD&: To, FromD: D, args&: Temporary, args&: ExtendingDecl, |
8289 | args: D->getManglingNumber())) |
8290 | return To; |
8291 | |
8292 | To->setLexicalDeclContext(LexicalDC); |
8293 | LexicalDC->addDeclInternal(D: To); |
8294 | return To; |
8295 | } |
8296 | |
8297 | ExpectedStmt |
8298 | ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) { |
8299 | Error Err = Error::success(); |
8300 | auto ToType = importChecked(Err, From: E->getType()); |
8301 | Expr *ToTemporaryExpr = importChecked( |
8302 | Err, From: E->getLifetimeExtendedTemporaryDecl() ? nullptr : E->getSubExpr()); |
8303 | auto ToMaterializedDecl = |
8304 | importChecked(Err, From: E->getLifetimeExtendedTemporaryDecl()); |
8305 | if (Err) |
8306 | return std::move(Err); |
8307 | |
8308 | if (!ToTemporaryExpr) |
8309 | ToTemporaryExpr = cast<Expr>(Val: ToMaterializedDecl->getTemporaryExpr()); |
8310 | |
8311 | auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr( |
8312 | ToType, ToTemporaryExpr, E->isBoundToLvalueReference(), |
8313 | ToMaterializedDecl); |
8314 | |
8315 | return ToMTE; |
8316 | } |
8317 | |
8318 | ExpectedStmt ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) { |
8319 | Error Err = Error::success(); |
8320 | auto *ToPattern = importChecked(Err, From: E->getPattern()); |
8321 | auto ToEllipsisLoc = importChecked(Err, From: E->getEllipsisLoc()); |
8322 | if (Err) |
8323 | return std::move(Err); |
8324 | |
8325 | return new (Importer.getToContext()) |
8326 | PackExpansionExpr(ToPattern, ToEllipsisLoc, E->getNumExpansions()); |
8327 | } |
8328 | |
8329 | ExpectedStmt ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) { |
8330 | Error Err = Error::success(); |
8331 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
8332 | auto ToPack = importChecked(Err, From: E->getPack()); |
8333 | auto ToPackLoc = importChecked(Err, From: E->getPackLoc()); |
8334 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
8335 | if (Err) |
8336 | return std::move(Err); |
8337 | |
8338 | UnsignedOrNone Length = std::nullopt; |
8339 | if (!E->isValueDependent()) |
8340 | Length = E->getPackLength(); |
8341 | |
8342 | SmallVector<TemplateArgument, 8> ToPartialArguments; |
8343 | if (E->isPartiallySubstituted()) { |
8344 | if (Error Err = ImportTemplateArguments(FromArgs: E->getPartialArguments(), |
8345 | ToArgs&: ToPartialArguments)) |
8346 | return std::move(Err); |
8347 | } |
8348 | |
8349 | return SizeOfPackExpr::Create( |
8350 | Context&: Importer.getToContext(), OperatorLoc: ToOperatorLoc, Pack: ToPack, PackLoc: ToPackLoc, RParenLoc: ToRParenLoc, |
8351 | Length, PartialArgs: ToPartialArguments); |
8352 | } |
8353 | |
8354 | |
8355 | ExpectedStmt ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *E) { |
8356 | Error Err = Error::success(); |
8357 | auto ToOperatorNew = importChecked(Err, From: E->getOperatorNew()); |
8358 | auto ToOperatorDelete = importChecked(Err, From: E->getOperatorDelete()); |
8359 | auto ToTypeIdParens = importChecked(Err, From: E->getTypeIdParens()); |
8360 | auto ToArraySize = importChecked(Err, From: E->getArraySize()); |
8361 | auto ToInitializer = importChecked(Err, From: E->getInitializer()); |
8362 | auto ToType = importChecked(Err, From: E->getType()); |
8363 | auto ToAllocatedTypeSourceInfo = |
8364 | importChecked(Err, From: E->getAllocatedTypeSourceInfo()); |
8365 | auto ToSourceRange = importChecked(Err, From: E->getSourceRange()); |
8366 | auto ToDirectInitRange = importChecked(Err, From: E->getDirectInitRange()); |
8367 | if (Err) |
8368 | return std::move(Err); |
8369 | |
8370 | SmallVector<Expr *, 4> ToPlacementArgs(E->getNumPlacementArgs()); |
8371 | if (Error Err = |
8372 | ImportContainerChecked(InContainer: E->placement_arguments(), OutContainer&: ToPlacementArgs)) |
8373 | return std::move(Err); |
8374 | |
8375 | return CXXNewExpr::Create( |
8376 | Ctx: Importer.getToContext(), IsGlobalNew: E->isGlobalNew(), OperatorNew: ToOperatorNew, |
8377 | OperatorDelete: ToOperatorDelete, IAP: E->implicitAllocationParameters(), |
8378 | UsualArrayDeleteWantsSize: E->doesUsualArrayDeleteWantSize(), PlacementArgs: ToPlacementArgs, TypeIdParens: ToTypeIdParens, |
8379 | ArraySize: ToArraySize, InitializationStyle: E->getInitializationStyle(), Initializer: ToInitializer, Ty: ToType, |
8380 | AllocatedTypeInfo: ToAllocatedTypeSourceInfo, Range: ToSourceRange, DirectInitRange: ToDirectInitRange); |
8381 | } |
8382 | |
8383 | ExpectedStmt ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
8384 | Error Err = Error::success(); |
8385 | auto ToType = importChecked(Err, From: E->getType()); |
8386 | auto ToOperatorDelete = importChecked(Err, From: E->getOperatorDelete()); |
8387 | auto ToArgument = importChecked(Err, From: E->getArgument()); |
8388 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
8389 | if (Err) |
8390 | return std::move(Err); |
8391 | |
8392 | return new (Importer.getToContext()) CXXDeleteExpr( |
8393 | ToType, E->isGlobalDelete(), E->isArrayForm(), E->isArrayFormAsWritten(), |
8394 | E->doesUsualArrayDeleteWantSize(), ToOperatorDelete, ToArgument, |
8395 | ToBeginLoc); |
8396 | } |
8397 | |
8398 | ExpectedStmt ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) { |
8399 | Error Err = Error::success(); |
8400 | auto ToType = importChecked(Err, From: E->getType()); |
8401 | auto ToLocation = importChecked(Err, From: E->getLocation()); |
8402 | auto ToConstructor = importChecked(Err, From: E->getConstructor()); |
8403 | auto ToParenOrBraceRange = importChecked(Err, From: E->getParenOrBraceRange()); |
8404 | if (Err) |
8405 | return std::move(Err); |
8406 | |
8407 | SmallVector<Expr *, 6> ToArgs(E->getNumArgs()); |
8408 | if (Error Err = ImportContainerChecked(InContainer: E->arguments(), OutContainer&: ToArgs)) |
8409 | return std::move(Err); |
8410 | |
8411 | CXXConstructExpr *ToE = CXXConstructExpr::Create( |
8412 | Ctx: Importer.getToContext(), Ty: ToType, Loc: ToLocation, Ctor: ToConstructor, |
8413 | Elidable: E->isElidable(), Args: ToArgs, HadMultipleCandidates: E->hadMultipleCandidates(), |
8414 | ListInitialization: E->isListInitialization(), StdInitListInitialization: E->isStdInitListInitialization(), |
8415 | ZeroInitialization: E->requiresZeroInitialization(), ConstructKind: E->getConstructionKind(), |
8416 | ParenOrBraceRange: ToParenOrBraceRange); |
8417 | ToE->setIsImmediateEscalating(E->isImmediateEscalating()); |
8418 | return ToE; |
8419 | } |
8420 | |
8421 | ExpectedStmt ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *E) { |
8422 | ExpectedExpr ToSubExprOrErr = import(From: E->getSubExpr()); |
8423 | if (!ToSubExprOrErr) |
8424 | return ToSubExprOrErr.takeError(); |
8425 | |
8426 | SmallVector<ExprWithCleanups::CleanupObject, 8> ToObjects(E->getNumObjects()); |
8427 | if (Error Err = ImportContainerChecked(InContainer: E->getObjects(), OutContainer&: ToObjects)) |
8428 | return std::move(Err); |
8429 | |
8430 | return ExprWithCleanups::Create( |
8431 | C: Importer.getToContext(), subexpr: *ToSubExprOrErr, CleanupsHaveSideEffects: E->cleanupsHaveSideEffects(), |
8432 | objects: ToObjects); |
8433 | } |
8434 | |
8435 | ExpectedStmt ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) { |
8436 | Error Err = Error::success(); |
8437 | auto ToCallee = importChecked(Err, From: E->getCallee()); |
8438 | auto ToType = importChecked(Err, From: E->getType()); |
8439 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
8440 | if (Err) |
8441 | return std::move(Err); |
8442 | |
8443 | SmallVector<Expr *, 4> ToArgs(E->getNumArgs()); |
8444 | if (Error Err = ImportContainerChecked(InContainer: E->arguments(), OutContainer&: ToArgs)) |
8445 | return std::move(Err); |
8446 | |
8447 | return CXXMemberCallExpr::Create(Ctx: Importer.getToContext(), Fn: ToCallee, Args: ToArgs, |
8448 | Ty: ToType, VK: E->getValueKind(), RP: ToRParenLoc, |
8449 | FPFeatures: E->getFPFeatures()); |
8450 | } |
8451 | |
8452 | ExpectedStmt ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) { |
8453 | ExpectedType ToTypeOrErr = import(From: E->getType()); |
8454 | if (!ToTypeOrErr) |
8455 | return ToTypeOrErr.takeError(); |
8456 | |
8457 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
8458 | if (!ToLocationOrErr) |
8459 | return ToLocationOrErr.takeError(); |
8460 | |
8461 | return CXXThisExpr::Create(Ctx: Importer.getToContext(), L: *ToLocationOrErr, |
8462 | Ty: *ToTypeOrErr, IsImplicit: E->isImplicit()); |
8463 | } |
8464 | |
8465 | ExpectedStmt ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) { |
8466 | ExpectedType ToTypeOrErr = import(From: E->getType()); |
8467 | if (!ToTypeOrErr) |
8468 | return ToTypeOrErr.takeError(); |
8469 | |
8470 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
8471 | if (!ToLocationOrErr) |
8472 | return ToLocationOrErr.takeError(); |
8473 | |
8474 | return CXXBoolLiteralExpr::Create(C: Importer.getToContext(), Val: E->getValue(), |
8475 | Ty: *ToTypeOrErr, Loc: *ToLocationOrErr); |
8476 | } |
8477 | |
8478 | ExpectedStmt ASTNodeImporter::VisitMemberExpr(MemberExpr *E) { |
8479 | Error Err = Error::success(); |
8480 | auto ToBase = importChecked(Err, From: E->getBase()); |
8481 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
8482 | auto ToQualifierLoc = importChecked(Err, From: E->getQualifierLoc()); |
8483 | auto ToTemplateKeywordLoc = importChecked(Err, From: E->getTemplateKeywordLoc()); |
8484 | auto ToMemberDecl = importChecked(Err, From: E->getMemberDecl()); |
8485 | auto ToType = importChecked(Err, From: E->getType()); |
8486 | auto ToDecl = importChecked(Err, From: E->getFoundDecl().getDecl()); |
8487 | auto ToName = importChecked(Err, From: E->getMemberNameInfo().getName()); |
8488 | auto ToLoc = importChecked(Err, From: E->getMemberNameInfo().getLoc()); |
8489 | if (Err) |
8490 | return std::move(Err); |
8491 | |
8492 | DeclAccessPair ToFoundDecl = |
8493 | DeclAccessPair::make(D: ToDecl, AS: E->getFoundDecl().getAccess()); |
8494 | |
8495 | DeclarationNameInfo ToMemberNameInfo(ToName, ToLoc); |
8496 | |
8497 | TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr; |
8498 | if (E->hasExplicitTemplateArgs()) { |
8499 | if (Error Err = |
8500 | ImportTemplateArgumentListInfo(FromLAngleLoc: E->getLAngleLoc(), FromRAngleLoc: E->getRAngleLoc(), |
8501 | Container: E->template_arguments(), Result&: ToTAInfo)) |
8502 | return std::move(Err); |
8503 | ResInfo = &ToTAInfo; |
8504 | } |
8505 | |
8506 | return MemberExpr::Create(C: Importer.getToContext(), Base: ToBase, IsArrow: E->isArrow(), |
8507 | OperatorLoc: ToOperatorLoc, QualifierLoc: ToQualifierLoc, TemplateKWLoc: ToTemplateKeywordLoc, |
8508 | MemberDecl: ToMemberDecl, FoundDecl: ToFoundDecl, MemberNameInfo: ToMemberNameInfo, |
8509 | TemplateArgs: ResInfo, T: ToType, VK: E->getValueKind(), |
8510 | OK: E->getObjectKind(), NOUR: E->isNonOdrUse()); |
8511 | } |
8512 | |
8513 | ExpectedStmt |
8514 | ASTNodeImporter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { |
8515 | Error Err = Error::success(); |
8516 | auto ToBase = importChecked(Err, From: E->getBase()); |
8517 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
8518 | auto ToQualifierLoc = importChecked(Err, From: E->getQualifierLoc()); |
8519 | auto ToScopeTypeInfo = importChecked(Err, From: E->getScopeTypeInfo()); |
8520 | auto ToColonColonLoc = importChecked(Err, From: E->getColonColonLoc()); |
8521 | auto ToTildeLoc = importChecked(Err, From: E->getTildeLoc()); |
8522 | if (Err) |
8523 | return std::move(Err); |
8524 | |
8525 | PseudoDestructorTypeStorage Storage; |
8526 | if (const IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) { |
8527 | const IdentifierInfo *ToII = Importer.Import(FromId: FromII); |
8528 | ExpectedSLoc ToDestroyedTypeLocOrErr = import(From: E->getDestroyedTypeLoc()); |
8529 | if (!ToDestroyedTypeLocOrErr) |
8530 | return ToDestroyedTypeLocOrErr.takeError(); |
8531 | Storage = PseudoDestructorTypeStorage(ToII, *ToDestroyedTypeLocOrErr); |
8532 | } else { |
8533 | if (auto ToTIOrErr = import(From: E->getDestroyedTypeInfo())) |
8534 | Storage = PseudoDestructorTypeStorage(*ToTIOrErr); |
8535 | else |
8536 | return ToTIOrErr.takeError(); |
8537 | } |
8538 | |
8539 | return new (Importer.getToContext()) CXXPseudoDestructorExpr( |
8540 | Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc, |
8541 | ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc, ToTildeLoc, Storage); |
8542 | } |
8543 | |
8544 | ExpectedStmt ASTNodeImporter::VisitCXXDependentScopeMemberExpr( |
8545 | CXXDependentScopeMemberExpr *E) { |
8546 | Error Err = Error::success(); |
8547 | auto ToType = importChecked(Err, From: E->getType()); |
8548 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
8549 | auto ToQualifierLoc = importChecked(Err, From: E->getQualifierLoc()); |
8550 | auto ToTemplateKeywordLoc = importChecked(Err, From: E->getTemplateKeywordLoc()); |
8551 | auto ToFirstQualifierFoundInScope = |
8552 | importChecked(Err, From: E->getFirstQualifierFoundInScope()); |
8553 | if (Err) |
8554 | return std::move(Err); |
8555 | |
8556 | Expr *ToBase = nullptr; |
8557 | if (!E->isImplicitAccess()) { |
8558 | if (ExpectedExpr ToBaseOrErr = import(From: E->getBase())) |
8559 | ToBase = *ToBaseOrErr; |
8560 | else |
8561 | return ToBaseOrErr.takeError(); |
8562 | } |
8563 | |
8564 | TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr; |
8565 | |
8566 | if (E->hasExplicitTemplateArgs()) { |
8567 | if (Error Err = |
8568 | ImportTemplateArgumentListInfo(FromLAngleLoc: E->getLAngleLoc(), FromRAngleLoc: E->getRAngleLoc(), |
8569 | Container: E->template_arguments(), Result&: ToTAInfo)) |
8570 | return std::move(Err); |
8571 | ResInfo = &ToTAInfo; |
8572 | } |
8573 | auto ToMember = importChecked(Err, From: E->getMember()); |
8574 | auto ToMemberLoc = importChecked(Err, From: E->getMemberLoc()); |
8575 | if (Err) |
8576 | return std::move(Err); |
8577 | DeclarationNameInfo ToMemberNameInfo(ToMember, ToMemberLoc); |
8578 | |
8579 | // Import additional name location/type info. |
8580 | if (Error Err = |
8581 | ImportDeclarationNameLoc(From: E->getMemberNameInfo(), To&: ToMemberNameInfo)) |
8582 | return std::move(Err); |
8583 | |
8584 | return CXXDependentScopeMemberExpr::Create( |
8585 | Ctx: Importer.getToContext(), Base: ToBase, BaseType: ToType, IsArrow: E->isArrow(), OperatorLoc: ToOperatorLoc, |
8586 | QualifierLoc: ToQualifierLoc, TemplateKWLoc: ToTemplateKeywordLoc, FirstQualifierFoundInScope: ToFirstQualifierFoundInScope, |
8587 | MemberNameInfo: ToMemberNameInfo, TemplateArgs: ResInfo); |
8588 | } |
8589 | |
8590 | ExpectedStmt |
8591 | ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) { |
8592 | Error Err = Error::success(); |
8593 | auto ToQualifierLoc = importChecked(Err, From: E->getQualifierLoc()); |
8594 | auto ToTemplateKeywordLoc = importChecked(Err, From: E->getTemplateKeywordLoc()); |
8595 | auto ToDeclName = importChecked(Err, From: E->getDeclName()); |
8596 | auto ToNameLoc = importChecked(Err, From: E->getNameInfo().getLoc()); |
8597 | auto ToLAngleLoc = importChecked(Err, From: E->getLAngleLoc()); |
8598 | auto ToRAngleLoc = importChecked(Err, From: E->getRAngleLoc()); |
8599 | if (Err) |
8600 | return std::move(Err); |
8601 | |
8602 | DeclarationNameInfo ToNameInfo(ToDeclName, ToNameLoc); |
8603 | if (Error Err = ImportDeclarationNameLoc(From: E->getNameInfo(), To&: ToNameInfo)) |
8604 | return std::move(Err); |
8605 | |
8606 | TemplateArgumentListInfo ToTAInfo(ToLAngleLoc, ToRAngleLoc); |
8607 | TemplateArgumentListInfo *ResInfo = nullptr; |
8608 | if (E->hasExplicitTemplateArgs()) { |
8609 | if (Error Err = |
8610 | ImportTemplateArgumentListInfo(Container: E->template_arguments(), ToTAInfo)) |
8611 | return std::move(Err); |
8612 | ResInfo = &ToTAInfo; |
8613 | } |
8614 | |
8615 | return DependentScopeDeclRefExpr::Create( |
8616 | Context: Importer.getToContext(), QualifierLoc: ToQualifierLoc, TemplateKWLoc: ToTemplateKeywordLoc, |
8617 | NameInfo: ToNameInfo, TemplateArgs: ResInfo); |
8618 | } |
8619 | |
8620 | ExpectedStmt ASTNodeImporter::VisitCXXUnresolvedConstructExpr( |
8621 | CXXUnresolvedConstructExpr *E) { |
8622 | Error Err = Error::success(); |
8623 | auto ToLParenLoc = importChecked(Err, From: E->getLParenLoc()); |
8624 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
8625 | auto ToType = importChecked(Err, From: E->getType()); |
8626 | auto ToTypeSourceInfo = importChecked(Err, From: E->getTypeSourceInfo()); |
8627 | if (Err) |
8628 | return std::move(Err); |
8629 | |
8630 | SmallVector<Expr *, 8> ToArgs(E->getNumArgs()); |
8631 | if (Error Err = |
8632 | ImportArrayChecked(Ibegin: E->arg_begin(), Iend: E->arg_end(), Obegin: ToArgs.begin())) |
8633 | return std::move(Err); |
8634 | |
8635 | return CXXUnresolvedConstructExpr::Create( |
8636 | Context: Importer.getToContext(), T: ToType, TSI: ToTypeSourceInfo, LParenLoc: ToLParenLoc, |
8637 | Args: ArrayRef(ToArgs), RParenLoc: ToRParenLoc, IsListInit: E->isListInitialization()); |
8638 | } |
8639 | |
8640 | ExpectedStmt |
8641 | ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) { |
8642 | Expected<CXXRecordDecl *> ToNamingClassOrErr = import(From: E->getNamingClass()); |
8643 | if (!ToNamingClassOrErr) |
8644 | return ToNamingClassOrErr.takeError(); |
8645 | |
8646 | auto ToQualifierLocOrErr = import(From: E->getQualifierLoc()); |
8647 | if (!ToQualifierLocOrErr) |
8648 | return ToQualifierLocOrErr.takeError(); |
8649 | |
8650 | Error Err = Error::success(); |
8651 | auto ToName = importChecked(Err, From: E->getName()); |
8652 | auto ToNameLoc = importChecked(Err, From: E->getNameLoc()); |
8653 | if (Err) |
8654 | return std::move(Err); |
8655 | DeclarationNameInfo ToNameInfo(ToName, ToNameLoc); |
8656 | |
8657 | // Import additional name location/type info. |
8658 | if (Error Err = ImportDeclarationNameLoc(From: E->getNameInfo(), To&: ToNameInfo)) |
8659 | return std::move(Err); |
8660 | |
8661 | UnresolvedSet<8> ToDecls; |
8662 | for (auto *D : E->decls()) |
8663 | if (auto ToDOrErr = import(From: D)) |
8664 | ToDecls.addDecl(D: cast<NamedDecl>(Val: *ToDOrErr)); |
8665 | else |
8666 | return ToDOrErr.takeError(); |
8667 | |
8668 | if (E->hasExplicitTemplateArgs()) { |
8669 | TemplateArgumentListInfo ToTAInfo; |
8670 | if (Error Err = ImportTemplateArgumentListInfo( |
8671 | FromLAngleLoc: E->getLAngleLoc(), FromRAngleLoc: E->getRAngleLoc(), Container: E->template_arguments(), |
8672 | Result&: ToTAInfo)) |
8673 | return std::move(Err); |
8674 | |
8675 | ExpectedSLoc ToTemplateKeywordLocOrErr = import(From: E->getTemplateKeywordLoc()); |
8676 | if (!ToTemplateKeywordLocOrErr) |
8677 | return ToTemplateKeywordLocOrErr.takeError(); |
8678 | |
8679 | const bool KnownDependent = |
8680 | (E->getDependence() & ExprDependence::TypeValue) == |
8681 | ExprDependence::TypeValue; |
8682 | return UnresolvedLookupExpr::Create( |
8683 | Context: Importer.getToContext(), NamingClass: *ToNamingClassOrErr, QualifierLoc: *ToQualifierLocOrErr, |
8684 | TemplateKWLoc: *ToTemplateKeywordLocOrErr, NameInfo: ToNameInfo, RequiresADL: E->requiresADL(), Args: &ToTAInfo, |
8685 | Begin: ToDecls.begin(), End: ToDecls.end(), KnownDependent, |
8686 | /*KnownInstantiationDependent=*/E->isInstantiationDependent()); |
8687 | } |
8688 | |
8689 | return UnresolvedLookupExpr::Create( |
8690 | Context: Importer.getToContext(), NamingClass: *ToNamingClassOrErr, QualifierLoc: *ToQualifierLocOrErr, |
8691 | NameInfo: ToNameInfo, RequiresADL: E->requiresADL(), Begin: ToDecls.begin(), End: ToDecls.end(), |
8692 | /*KnownDependent=*/E->isTypeDependent(), |
8693 | /*KnownInstantiationDependent=*/E->isInstantiationDependent()); |
8694 | } |
8695 | |
8696 | ExpectedStmt |
8697 | ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) { |
8698 | Error Err = Error::success(); |
8699 | auto ToType = importChecked(Err, From: E->getType()); |
8700 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
8701 | auto ToQualifierLoc = importChecked(Err, From: E->getQualifierLoc()); |
8702 | auto ToTemplateKeywordLoc = importChecked(Err, From: E->getTemplateKeywordLoc()); |
8703 | auto ToName = importChecked(Err, From: E->getName()); |
8704 | auto ToNameLoc = importChecked(Err, From: E->getNameLoc()); |
8705 | if (Err) |
8706 | return std::move(Err); |
8707 | |
8708 | DeclarationNameInfo ToNameInfo(ToName, ToNameLoc); |
8709 | // Import additional name location/type info. |
8710 | if (Error Err = ImportDeclarationNameLoc(From: E->getNameInfo(), To&: ToNameInfo)) |
8711 | return std::move(Err); |
8712 | |
8713 | UnresolvedSet<8> ToDecls; |
8714 | for (Decl *D : E->decls()) |
8715 | if (auto ToDOrErr = import(From: D)) |
8716 | ToDecls.addDecl(D: cast<NamedDecl>(Val: *ToDOrErr)); |
8717 | else |
8718 | return ToDOrErr.takeError(); |
8719 | |
8720 | TemplateArgumentListInfo ToTAInfo; |
8721 | TemplateArgumentListInfo *ResInfo = nullptr; |
8722 | if (E->hasExplicitTemplateArgs()) { |
8723 | TemplateArgumentListInfo FromTAInfo; |
8724 | E->copyTemplateArgumentsInto(List&: FromTAInfo); |
8725 | if (Error Err = ImportTemplateArgumentListInfo(From: FromTAInfo, Result&: ToTAInfo)) |
8726 | return std::move(Err); |
8727 | ResInfo = &ToTAInfo; |
8728 | } |
8729 | |
8730 | Expr *ToBase = nullptr; |
8731 | if (!E->isImplicitAccess()) { |
8732 | if (ExpectedExpr ToBaseOrErr = import(From: E->getBase())) |
8733 | ToBase = *ToBaseOrErr; |
8734 | else |
8735 | return ToBaseOrErr.takeError(); |
8736 | } |
8737 | |
8738 | return UnresolvedMemberExpr::Create( |
8739 | Context: Importer.getToContext(), HasUnresolvedUsing: E->hasUnresolvedUsing(), Base: ToBase, BaseType: ToType, |
8740 | IsArrow: E->isArrow(), OperatorLoc: ToOperatorLoc, QualifierLoc: ToQualifierLoc, TemplateKWLoc: ToTemplateKeywordLoc, |
8741 | MemberNameInfo: ToNameInfo, TemplateArgs: ResInfo, Begin: ToDecls.begin(), End: ToDecls.end()); |
8742 | } |
8743 | |
8744 | ExpectedStmt ASTNodeImporter::VisitCallExpr(CallExpr *E) { |
8745 | Error Err = Error::success(); |
8746 | auto ToCallee = importChecked(Err, From: E->getCallee()); |
8747 | auto ToType = importChecked(Err, From: E->getType()); |
8748 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
8749 | if (Err) |
8750 | return std::move(Err); |
8751 | |
8752 | unsigned NumArgs = E->getNumArgs(); |
8753 | llvm::SmallVector<Expr *, 2> ToArgs(NumArgs); |
8754 | if (Error Err = ImportContainerChecked(InContainer: E->arguments(), OutContainer&: ToArgs)) |
8755 | return std::move(Err); |
8756 | |
8757 | if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(Val: E)) { |
8758 | return CXXOperatorCallExpr::Create( |
8759 | Ctx: Importer.getToContext(), OpKind: OCE->getOperator(), Fn: ToCallee, Args: ToArgs, Ty: ToType, |
8760 | VK: OCE->getValueKind(), OperatorLoc: ToRParenLoc, FPFeatures: OCE->getFPFeatures(), |
8761 | UsesADL: OCE->getADLCallKind()); |
8762 | } |
8763 | |
8764 | return CallExpr::Create(Ctx: Importer.getToContext(), Fn: ToCallee, Args: ToArgs, Ty: ToType, |
8765 | VK: E->getValueKind(), RParenLoc: ToRParenLoc, FPFeatures: E->getFPFeatures(), |
8766 | /*MinNumArgs=*/0, UsesADL: E->getADLCallKind()); |
8767 | } |
8768 | |
8769 | ExpectedStmt ASTNodeImporter::VisitLambdaExpr(LambdaExpr *E) { |
8770 | CXXRecordDecl *FromClass = E->getLambdaClass(); |
8771 | auto ToClassOrErr = import(From: FromClass); |
8772 | if (!ToClassOrErr) |
8773 | return ToClassOrErr.takeError(); |
8774 | CXXRecordDecl *ToClass = *ToClassOrErr; |
8775 | |
8776 | auto ToCallOpOrErr = import(From: E->getCallOperator()); |
8777 | if (!ToCallOpOrErr) |
8778 | return ToCallOpOrErr.takeError(); |
8779 | |
8780 | SmallVector<Expr *, 8> ToCaptureInits(E->capture_size()); |
8781 | if (Error Err = ImportContainerChecked(InContainer: E->capture_inits(), OutContainer&: ToCaptureInits)) |
8782 | return std::move(Err); |
8783 | |
8784 | Error Err = Error::success(); |
8785 | auto ToIntroducerRange = importChecked(Err, From: E->getIntroducerRange()); |
8786 | auto ToCaptureDefaultLoc = importChecked(Err, From: E->getCaptureDefaultLoc()); |
8787 | auto ToEndLoc = importChecked(Err, From: E->getEndLoc()); |
8788 | if (Err) |
8789 | return std::move(Err); |
8790 | |
8791 | return LambdaExpr::Create(C: Importer.getToContext(), Class: ToClass, IntroducerRange: ToIntroducerRange, |
8792 | CaptureDefault: E->getCaptureDefault(), CaptureDefaultLoc: ToCaptureDefaultLoc, |
8793 | ExplicitParams: E->hasExplicitParameters(), |
8794 | ExplicitResultType: E->hasExplicitResultType(), CaptureInits: ToCaptureInits, |
8795 | ClosingBrace: ToEndLoc, ContainsUnexpandedParameterPack: E->containsUnexpandedParameterPack()); |
8796 | } |
8797 | |
8798 | |
8799 | ExpectedStmt ASTNodeImporter::VisitInitListExpr(InitListExpr *E) { |
8800 | Error Err = Error::success(); |
8801 | auto ToLBraceLoc = importChecked(Err, From: E->getLBraceLoc()); |
8802 | auto ToRBraceLoc = importChecked(Err, From: E->getRBraceLoc()); |
8803 | auto ToType = importChecked(Err, From: E->getType()); |
8804 | if (Err) |
8805 | return std::move(Err); |
8806 | |
8807 | SmallVector<Expr *, 4> ToExprs(E->getNumInits()); |
8808 | if (Error Err = ImportContainerChecked(InContainer: E->inits(), OutContainer&: ToExprs)) |
8809 | return std::move(Err); |
8810 | |
8811 | ASTContext &ToCtx = Importer.getToContext(); |
8812 | InitListExpr *To = new (ToCtx) InitListExpr( |
8813 | ToCtx, ToLBraceLoc, ToExprs, ToRBraceLoc); |
8814 | To->setType(ToType); |
8815 | |
8816 | if (E->hasArrayFiller()) { |
8817 | if (ExpectedExpr ToFillerOrErr = import(From: E->getArrayFiller())) |
8818 | To->setArrayFiller(*ToFillerOrErr); |
8819 | else |
8820 | return ToFillerOrErr.takeError(); |
8821 | } |
8822 | |
8823 | if (FieldDecl *FromFD = E->getInitializedFieldInUnion()) { |
8824 | if (auto ToFDOrErr = import(From: FromFD)) |
8825 | To->setInitializedFieldInUnion(*ToFDOrErr); |
8826 | else |
8827 | return ToFDOrErr.takeError(); |
8828 | } |
8829 | |
8830 | if (InitListExpr *SyntForm = E->getSyntacticForm()) { |
8831 | if (auto ToSyntFormOrErr = import(From: SyntForm)) |
8832 | To->setSyntacticForm(*ToSyntFormOrErr); |
8833 | else |
8834 | return ToSyntFormOrErr.takeError(); |
8835 | } |
8836 | |
8837 | // Copy InitListExprBitfields, which are not handled in the ctor of |
8838 | // InitListExpr. |
8839 | To->sawArrayRangeDesignator(ARD: E->hadArrayRangeDesignator()); |
8840 | |
8841 | return To; |
8842 | } |
8843 | |
8844 | ExpectedStmt ASTNodeImporter::VisitCXXStdInitializerListExpr( |
8845 | CXXStdInitializerListExpr *E) { |
8846 | ExpectedType ToTypeOrErr = import(From: E->getType()); |
8847 | if (!ToTypeOrErr) |
8848 | return ToTypeOrErr.takeError(); |
8849 | |
8850 | ExpectedExpr ToSubExprOrErr = import(From: E->getSubExpr()); |
8851 | if (!ToSubExprOrErr) |
8852 | return ToSubExprOrErr.takeError(); |
8853 | |
8854 | return new (Importer.getToContext()) CXXStdInitializerListExpr( |
8855 | *ToTypeOrErr, *ToSubExprOrErr); |
8856 | } |
8857 | |
8858 | ExpectedStmt ASTNodeImporter::VisitCXXInheritedCtorInitExpr( |
8859 | CXXInheritedCtorInitExpr *E) { |
8860 | Error Err = Error::success(); |
8861 | auto ToLocation = importChecked(Err, From: E->getLocation()); |
8862 | auto ToType = importChecked(Err, From: E->getType()); |
8863 | auto ToConstructor = importChecked(Err, From: E->getConstructor()); |
8864 | if (Err) |
8865 | return std::move(Err); |
8866 | |
8867 | return new (Importer.getToContext()) CXXInheritedCtorInitExpr( |
8868 | ToLocation, ToType, ToConstructor, E->constructsVBase(), |
8869 | E->inheritedFromVBase()); |
8870 | } |
8871 | |
8872 | ExpectedStmt ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) { |
8873 | Error Err = Error::success(); |
8874 | auto ToType = importChecked(Err, From: E->getType()); |
8875 | auto ToCommonExpr = importChecked(Err, From: E->getCommonExpr()); |
8876 | auto ToSubExpr = importChecked(Err, From: E->getSubExpr()); |
8877 | if (Err) |
8878 | return std::move(Err); |
8879 | |
8880 | return new (Importer.getToContext()) ArrayInitLoopExpr( |
8881 | ToType, ToCommonExpr, ToSubExpr); |
8882 | } |
8883 | |
8884 | ExpectedStmt ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) { |
8885 | ExpectedType ToTypeOrErr = import(From: E->getType()); |
8886 | if (!ToTypeOrErr) |
8887 | return ToTypeOrErr.takeError(); |
8888 | return new (Importer.getToContext()) ArrayInitIndexExpr(*ToTypeOrErr); |
8889 | } |
8890 | |
8891 | ExpectedStmt ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) { |
8892 | ExpectedSLoc ToBeginLocOrErr = import(From: E->getBeginLoc()); |
8893 | if (!ToBeginLocOrErr) |
8894 | return ToBeginLocOrErr.takeError(); |
8895 | |
8896 | auto ToFieldOrErr = import(From: E->getField()); |
8897 | if (!ToFieldOrErr) |
8898 | return ToFieldOrErr.takeError(); |
8899 | |
8900 | auto UsedContextOrErr = Importer.ImportContext(FromDC: E->getUsedContext()); |
8901 | if (!UsedContextOrErr) |
8902 | return UsedContextOrErr.takeError(); |
8903 | |
8904 | FieldDecl *ToField = *ToFieldOrErr; |
8905 | assert(ToField->hasInClassInitializer() && |
8906 | "Field should have in-class initializer if there is a default init " |
8907 | "expression that uses it." ); |
8908 | if (!ToField->getInClassInitializer()) { |
8909 | // The in-class initializer may be not yet set in "To" AST even if the |
8910 | // field is already there. This must be set here to make construction of |
8911 | // CXXDefaultInitExpr work. |
8912 | auto ToInClassInitializerOrErr = |
8913 | import(From: E->getField()->getInClassInitializer()); |
8914 | if (!ToInClassInitializerOrErr) |
8915 | return ToInClassInitializerOrErr.takeError(); |
8916 | ToField->setInClassInitializer(*ToInClassInitializerOrErr); |
8917 | } |
8918 | |
8919 | Expr *RewrittenInit = nullptr; |
8920 | if (E->hasRewrittenInit()) { |
8921 | ExpectedExpr ExprOrErr = import(From: E->getRewrittenExpr()); |
8922 | if (!ExprOrErr) |
8923 | return ExprOrErr.takeError(); |
8924 | RewrittenInit = ExprOrErr.get(); |
8925 | } |
8926 | |
8927 | return CXXDefaultInitExpr::Create(Ctx: Importer.getToContext(), Loc: *ToBeginLocOrErr, |
8928 | Field: ToField, UsedContext: *UsedContextOrErr, RewrittenInitExpr: RewrittenInit); |
8929 | } |
8930 | |
8931 | ExpectedStmt ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) { |
8932 | Error Err = Error::success(); |
8933 | auto ToType = importChecked(Err, From: E->getType()); |
8934 | auto ToSubExpr = importChecked(Err, From: E->getSubExpr()); |
8935 | auto ToTypeInfoAsWritten = importChecked(Err, From: E->getTypeInfoAsWritten()); |
8936 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
8937 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
8938 | auto ToAngleBrackets = importChecked(Err, From: E->getAngleBrackets()); |
8939 | if (Err) |
8940 | return std::move(Err); |
8941 | |
8942 | ExprValueKind VK = E->getValueKind(); |
8943 | CastKind CK = E->getCastKind(); |
8944 | auto ToBasePathOrErr = ImportCastPath(CE: E); |
8945 | if (!ToBasePathOrErr) |
8946 | return ToBasePathOrErr.takeError(); |
8947 | |
8948 | if (auto CCE = dyn_cast<CXXStaticCastExpr>(Val: E)) { |
8949 | return CXXStaticCastExpr::Create( |
8950 | Context: Importer.getToContext(), T: ToType, VK, K: CK, Op: ToSubExpr, Path: &(*ToBasePathOrErr), |
8951 | Written: ToTypeInfoAsWritten, FPO: CCE->getFPFeatures(), L: ToOperatorLoc, RParenLoc: ToRParenLoc, |
8952 | AngleBrackets: ToAngleBrackets); |
8953 | } else if (isa<CXXDynamicCastExpr>(Val: E)) { |
8954 | return CXXDynamicCastExpr::Create( |
8955 | Context: Importer.getToContext(), T: ToType, VK, Kind: CK, Op: ToSubExpr, Path: &(*ToBasePathOrErr), |
8956 | Written: ToTypeInfoAsWritten, L: ToOperatorLoc, RParenLoc: ToRParenLoc, AngleBrackets: ToAngleBrackets); |
8957 | } else if (isa<CXXReinterpretCastExpr>(Val: E)) { |
8958 | return CXXReinterpretCastExpr::Create( |
8959 | Context: Importer.getToContext(), T: ToType, VK, Kind: CK, Op: ToSubExpr, Path: &(*ToBasePathOrErr), |
8960 | WrittenTy: ToTypeInfoAsWritten, L: ToOperatorLoc, RParenLoc: ToRParenLoc, AngleBrackets: ToAngleBrackets); |
8961 | } else if (isa<CXXConstCastExpr>(Val: E)) { |
8962 | return CXXConstCastExpr::Create( |
8963 | Context: Importer.getToContext(), T: ToType, VK, Op: ToSubExpr, WrittenTy: ToTypeInfoAsWritten, |
8964 | L: ToOperatorLoc, RParenLoc: ToRParenLoc, AngleBrackets: ToAngleBrackets); |
8965 | } else { |
8966 | llvm_unreachable("Unknown cast type" ); |
8967 | return make_error<ASTImportError>(); |
8968 | } |
8969 | } |
8970 | |
8971 | ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr( |
8972 | SubstNonTypeTemplateParmExpr *E) { |
8973 | Error Err = Error::success(); |
8974 | auto ToType = importChecked(Err, From: E->getType()); |
8975 | auto ToNameLoc = importChecked(Err, From: E->getNameLoc()); |
8976 | auto ToAssociatedDecl = importChecked(Err, From: E->getAssociatedDecl()); |
8977 | auto ToReplacement = importChecked(Err, From: E->getReplacement()); |
8978 | if (Err) |
8979 | return std::move(Err); |
8980 | |
8981 | return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr( |
8982 | ToType, E->getValueKind(), ToNameLoc, ToReplacement, ToAssociatedDecl, |
8983 | E->getIndex(), E->getPackIndex(), E->isReferenceParameter(), |
8984 | E->getFinal()); |
8985 | } |
8986 | |
8987 | ExpectedStmt ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) { |
8988 | Error Err = Error::success(); |
8989 | auto ToType = importChecked(Err, From: E->getType()); |
8990 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
8991 | auto ToEndLoc = importChecked(Err, From: E->getEndLoc()); |
8992 | if (Err) |
8993 | return std::move(Err); |
8994 | |
8995 | SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs()); |
8996 | if (Error Err = ImportContainerChecked(InContainer: E->getArgs(), OutContainer&: ToArgs)) |
8997 | return std::move(Err); |
8998 | |
8999 | if (E->isStoredAsBoolean()) { |
9000 | // According to Sema::BuildTypeTrait(), if E is value-dependent, |
9001 | // Value is always false. |
9002 | bool ToValue = (E->isValueDependent() ? false : E->getBoolValue()); |
9003 | return TypeTraitExpr::Create(C: Importer.getToContext(), T: ToType, Loc: ToBeginLoc, |
9004 | Kind: E->getTrait(), Args: ToArgs, RParenLoc: ToEndLoc, Value: ToValue); |
9005 | } |
9006 | return TypeTraitExpr::Create(C: Importer.getToContext(), T: ToType, Loc: ToBeginLoc, |
9007 | Kind: E->getTrait(), Args: ToArgs, RParenLoc: ToEndLoc, |
9008 | Value: E->getAPValue()); |
9009 | } |
9010 | |
9011 | ExpectedStmt ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) { |
9012 | ExpectedType ToTypeOrErr = import(From: E->getType()); |
9013 | if (!ToTypeOrErr) |
9014 | return ToTypeOrErr.takeError(); |
9015 | |
9016 | auto ToSourceRangeOrErr = import(From: E->getSourceRange()); |
9017 | if (!ToSourceRangeOrErr) |
9018 | return ToSourceRangeOrErr.takeError(); |
9019 | |
9020 | if (E->isTypeOperand()) { |
9021 | if (auto ToTSIOrErr = import(From: E->getTypeOperandSourceInfo())) |
9022 | return new (Importer.getToContext()) CXXTypeidExpr( |
9023 | *ToTypeOrErr, *ToTSIOrErr, *ToSourceRangeOrErr); |
9024 | else |
9025 | return ToTSIOrErr.takeError(); |
9026 | } |
9027 | |
9028 | ExpectedExpr ToExprOperandOrErr = import(From: E->getExprOperand()); |
9029 | if (!ToExprOperandOrErr) |
9030 | return ToExprOperandOrErr.takeError(); |
9031 | |
9032 | return new (Importer.getToContext()) CXXTypeidExpr( |
9033 | *ToTypeOrErr, *ToExprOperandOrErr, *ToSourceRangeOrErr); |
9034 | } |
9035 | |
9036 | ExpectedStmt ASTNodeImporter::VisitCXXFoldExpr(CXXFoldExpr *E) { |
9037 | Error Err = Error::success(); |
9038 | |
9039 | QualType ToType = importChecked(Err, From: E->getType()); |
9040 | UnresolvedLookupExpr *ToCallee = importChecked(Err, From: E->getCallee()); |
9041 | SourceLocation ToLParenLoc = importChecked(Err, From: E->getLParenLoc()); |
9042 | Expr *ToLHS = importChecked(Err, From: E->getLHS()); |
9043 | SourceLocation ToEllipsisLoc = importChecked(Err, From: E->getEllipsisLoc()); |
9044 | Expr *ToRHS = importChecked(Err, From: E->getRHS()); |
9045 | SourceLocation ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
9046 | |
9047 | if (Err) |
9048 | return std::move(Err); |
9049 | |
9050 | return new (Importer.getToContext()) |
9051 | CXXFoldExpr(ToType, ToCallee, ToLParenLoc, ToLHS, E->getOperator(), |
9052 | ToEllipsisLoc, ToRHS, ToRParenLoc, E->getNumExpansions()); |
9053 | } |
9054 | |
9055 | Error ASTNodeImporter::ImportOverriddenMethods(CXXMethodDecl *ToMethod, |
9056 | CXXMethodDecl *FromMethod) { |
9057 | Error ImportErrors = Error::success(); |
9058 | for (auto *FromOverriddenMethod : FromMethod->overridden_methods()) { |
9059 | if (auto ImportedOrErr = import(From: FromOverriddenMethod)) |
9060 | ToMethod->getCanonicalDecl()->addOverriddenMethod(MD: cast<CXXMethodDecl>( |
9061 | Val: (*ImportedOrErr)->getCanonicalDecl())); |
9062 | else |
9063 | ImportErrors = |
9064 | joinErrors(E1: std::move(ImportErrors), E2: ImportedOrErr.takeError()); |
9065 | } |
9066 | return ImportErrors; |
9067 | } |
9068 | |
9069 | ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager, |
9070 | ASTContext &FromContext, FileManager &FromFileManager, |
9071 | bool MinimalImport, |
9072 | std::shared_ptr<ASTImporterSharedState> SharedState) |
9073 | : SharedState(SharedState), ToContext(ToContext), FromContext(FromContext), |
9074 | ToFileManager(ToFileManager), FromFileManager(FromFileManager), |
9075 | Minimal(MinimalImport), ODRHandling(ODRHandlingType::Conservative) { |
9076 | |
9077 | // Create a default state without the lookup table: LLDB case. |
9078 | if (!SharedState) { |
9079 | this->SharedState = std::make_shared<ASTImporterSharedState>(); |
9080 | } |
9081 | |
9082 | ImportedDecls[FromContext.getTranslationUnitDecl()] = |
9083 | ToContext.getTranslationUnitDecl(); |
9084 | } |
9085 | |
9086 | ASTImporter::~ASTImporter() = default; |
9087 | |
9088 | UnsignedOrNone ASTImporter::getFieldIndex(Decl *F) { |
9089 | assert(F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) && |
9090 | "Try to get field index for non-field." ); |
9091 | |
9092 | auto *Owner = dyn_cast<RecordDecl>(Val: F->getDeclContext()); |
9093 | if (!Owner) |
9094 | return std::nullopt; |
9095 | |
9096 | unsigned Index = 0; |
9097 | for (const auto *D : Owner->decls()) { |
9098 | if (D == F) |
9099 | return Index; |
9100 | |
9101 | if (isa<FieldDecl>(Val: *D) || isa<IndirectFieldDecl>(Val: *D)) |
9102 | ++Index; |
9103 | } |
9104 | |
9105 | llvm_unreachable("Field was not found in its parent context." ); |
9106 | |
9107 | return std::nullopt; |
9108 | } |
9109 | |
9110 | ASTImporter::FoundDeclsTy |
9111 | ASTImporter::findDeclsInToCtx(DeclContext *DC, DeclarationName Name) { |
9112 | // We search in the redecl context because of transparent contexts. |
9113 | // E.g. a simple C language enum is a transparent context: |
9114 | // enum E { A, B }; |
9115 | // Now if we had a global variable in the TU |
9116 | // int A; |
9117 | // then the enum constant 'A' and the variable 'A' violates ODR. |
9118 | // We can diagnose this only if we search in the redecl context. |
9119 | DeclContext *ReDC = DC->getRedeclContext(); |
9120 | if (SharedState->getLookupTable()) { |
9121 | if (ReDC->isNamespace()) { |
9122 | // Namespaces can be reopened. |
9123 | // Lookup table does not handle this, we must search here in all linked |
9124 | // namespaces. |
9125 | FoundDeclsTy Result; |
9126 | SmallVector<Decl *, 2> NSChain = |
9127 | getCanonicalForwardRedeclChain<NamespaceDecl>( |
9128 | D: dyn_cast<NamespaceDecl>(Val: ReDC)); |
9129 | for (auto *D : NSChain) { |
9130 | ASTImporterLookupTable::LookupResult LookupResult = |
9131 | SharedState->getLookupTable()->lookup(DC: dyn_cast<NamespaceDecl>(Val: D), |
9132 | Name); |
9133 | Result.append(in_start: LookupResult.begin(), in_end: LookupResult.end()); |
9134 | } |
9135 | return Result; |
9136 | } else { |
9137 | ASTImporterLookupTable::LookupResult LookupResult = |
9138 | SharedState->getLookupTable()->lookup(DC: ReDC, Name); |
9139 | return FoundDeclsTy(LookupResult.begin(), LookupResult.end()); |
9140 | } |
9141 | } else { |
9142 | DeclContext::lookup_result NoloadLookupResult = ReDC->noload_lookup(Name); |
9143 | FoundDeclsTy Result(NoloadLookupResult.begin(), NoloadLookupResult.end()); |
9144 | // We must search by the slow case of localUncachedLookup because that is |
9145 | // working even if there is no LookupPtr for the DC. We could use |
9146 | // DC::buildLookup() to create the LookupPtr, but that would load external |
9147 | // decls again, we must avoid that case. |
9148 | // Also, even if we had the LookupPtr, we must find Decls which are not |
9149 | // in the LookupPtr, so we need the slow case. |
9150 | // These cases are handled in ASTImporterLookupTable, but we cannot use |
9151 | // that with LLDB since that traverses through the AST which initiates the |
9152 | // load of external decls again via DC::decls(). And again, we must avoid |
9153 | // loading external decls during the import. |
9154 | if (Result.empty()) |
9155 | ReDC->localUncachedLookup(Name, Results&: Result); |
9156 | return Result; |
9157 | } |
9158 | } |
9159 | |
9160 | void ASTImporter::AddToLookupTable(Decl *ToD) { |
9161 | SharedState->addDeclToLookup(D: ToD); |
9162 | } |
9163 | |
9164 | Expected<Decl *> ASTImporter::ImportImpl(Decl *FromD) { |
9165 | // Import the decl using ASTNodeImporter. |
9166 | ASTNodeImporter Importer(*this); |
9167 | return Importer.Visit(D: FromD); |
9168 | } |
9169 | |
9170 | void ASTImporter::RegisterImportedDecl(Decl *FromD, Decl *ToD) { |
9171 | MapImported(From: FromD, To: ToD); |
9172 | } |
9173 | |
9174 | llvm::Expected<ExprWithCleanups::CleanupObject> |
9175 | ASTImporter::Import(ExprWithCleanups::CleanupObject From) { |
9176 | if (auto *CLE = From.dyn_cast<CompoundLiteralExpr *>()) { |
9177 | if (Expected<Expr *> R = Import(FromE: CLE)) |
9178 | return ExprWithCleanups::CleanupObject(cast<CompoundLiteralExpr>(Val: *R)); |
9179 | } |
9180 | |
9181 | // FIXME: Handle BlockDecl when we implement importing BlockExpr in |
9182 | // ASTNodeImporter. |
9183 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
9184 | } |
9185 | |
9186 | ExpectedTypePtr ASTImporter::Import(const Type *FromT) { |
9187 | if (!FromT) |
9188 | return FromT; |
9189 | |
9190 | // Check whether we've already imported this type. |
9191 | llvm::DenseMap<const Type *, const Type *>::iterator Pos = |
9192 | ImportedTypes.find(Val: FromT); |
9193 | if (Pos != ImportedTypes.end()) |
9194 | return Pos->second; |
9195 | |
9196 | // Import the type. |
9197 | ASTNodeImporter Importer(*this); |
9198 | ExpectedType ToTOrErr = Importer.Visit(T: FromT); |
9199 | if (!ToTOrErr) |
9200 | return ToTOrErr.takeError(); |
9201 | |
9202 | // Record the imported type. |
9203 | ImportedTypes[FromT] = ToTOrErr->getTypePtr(); |
9204 | |
9205 | return ToTOrErr->getTypePtr(); |
9206 | } |
9207 | |
9208 | Expected<QualType> ASTImporter::Import(QualType FromT) { |
9209 | if (FromT.isNull()) |
9210 | return QualType{}; |
9211 | |
9212 | ExpectedTypePtr ToTyOrErr = Import(FromT: FromT.getTypePtr()); |
9213 | if (!ToTyOrErr) |
9214 | return ToTyOrErr.takeError(); |
9215 | |
9216 | return ToContext.getQualifiedType(T: *ToTyOrErr, Qs: FromT.getLocalQualifiers()); |
9217 | } |
9218 | |
9219 | Expected<TypeSourceInfo *> ASTImporter::Import(TypeSourceInfo *FromTSI) { |
9220 | if (!FromTSI) |
9221 | return FromTSI; |
9222 | |
9223 | // FIXME: For now we just create a "trivial" type source info based |
9224 | // on the type and a single location. Implement a real version of this. |
9225 | ExpectedType TOrErr = Import(FromT: FromTSI->getType()); |
9226 | if (!TOrErr) |
9227 | return TOrErr.takeError(); |
9228 | ExpectedSLoc BeginLocOrErr = Import(FromLoc: FromTSI->getTypeLoc().getBeginLoc()); |
9229 | if (!BeginLocOrErr) |
9230 | return BeginLocOrErr.takeError(); |
9231 | |
9232 | return ToContext.getTrivialTypeSourceInfo(T: *TOrErr, Loc: *BeginLocOrErr); |
9233 | } |
9234 | |
9235 | namespace { |
9236 | // To use this object, it should be created before the new attribute is created, |
9237 | // and destructed after it is created. The construction already performs the |
9238 | // import of the data. |
9239 | template <typename T> struct AttrArgImporter { |
9240 | AttrArgImporter(const AttrArgImporter<T> &) = delete; |
9241 | AttrArgImporter(AttrArgImporter<T> &&) = default; |
9242 | AttrArgImporter<T> &operator=(const AttrArgImporter<T> &) = delete; |
9243 | AttrArgImporter<T> &operator=(AttrArgImporter<T> &&) = default; |
9244 | |
9245 | AttrArgImporter(ASTNodeImporter &I, Error &Err, const T &From) |
9246 | : To(I.importChecked(Err, From)) {} |
9247 | |
9248 | const T &value() { return To; } |
9249 | |
9250 | private: |
9251 | T To; |
9252 | }; |
9253 | |
9254 | // To use this object, it should be created before the new attribute is created, |
9255 | // and destructed after it is created. The construction already performs the |
9256 | // import of the data. The array data is accessible in a pointer form, this form |
9257 | // is used by the attribute classes. This object should be created once for the |
9258 | // array data to be imported (the array size is not imported, just copied). |
9259 | template <typename T> struct AttrArgArrayImporter { |
9260 | AttrArgArrayImporter(const AttrArgArrayImporter<T> &) = delete; |
9261 | AttrArgArrayImporter(AttrArgArrayImporter<T> &&) = default; |
9262 | AttrArgArrayImporter<T> &operator=(const AttrArgArrayImporter<T> &) = delete; |
9263 | AttrArgArrayImporter<T> &operator=(AttrArgArrayImporter<T> &&) = default; |
9264 | |
9265 | AttrArgArrayImporter(ASTNodeImporter &I, Error &Err, |
9266 | const llvm::iterator_range<T *> &From, |
9267 | unsigned ArraySize) { |
9268 | if (Err) |
9269 | return; |
9270 | To.reserve(ArraySize); |
9271 | Err = I.ImportContainerChecked(From, To); |
9272 | } |
9273 | |
9274 | T *value() { return To.data(); } |
9275 | |
9276 | private: |
9277 | llvm::SmallVector<T, 2> To; |
9278 | }; |
9279 | |
9280 | class AttrImporter { |
9281 | Error Err{Error::success()}; |
9282 | Attr *ToAttr = nullptr; |
9283 | ASTImporter &Importer; |
9284 | ASTNodeImporter NImporter; |
9285 | |
9286 | public: |
9287 | AttrImporter(ASTImporter &I) : Importer(I), NImporter(I) {} |
9288 | |
9289 | // Useful for accessing the imported attribute. |
9290 | template <typename T> T *castAttrAs() { return cast<T>(ToAttr); } |
9291 | template <typename T> const T *castAttrAs() const { return cast<T>(ToAttr); } |
9292 | |
9293 | // Create an "importer" for an attribute parameter. |
9294 | // Result of the 'value()' of that object is to be passed to the function |
9295 | // 'importAttr', in the order that is expected by the attribute class. |
9296 | template <class T> AttrArgImporter<T> importArg(const T &From) { |
9297 | return AttrArgImporter<T>(NImporter, Err, From); |
9298 | } |
9299 | |
9300 | // Create an "importer" for an attribute parameter that has array type. |
9301 | // Result of the 'value()' of that object is to be passed to the function |
9302 | // 'importAttr', then the size of the array as next argument. |
9303 | template <typename T> |
9304 | AttrArgArrayImporter<T> importArrayArg(const llvm::iterator_range<T *> &From, |
9305 | unsigned ArraySize) { |
9306 | return AttrArgArrayImporter<T>(NImporter, Err, From, ArraySize); |
9307 | } |
9308 | |
9309 | // Create an attribute object with the specified arguments. |
9310 | // The 'FromAttr' is the original (not imported) attribute, the 'ImportedArg' |
9311 | // should be values that are passed to the 'Create' function of the attribute. |
9312 | // (The 'Create' with 'ASTContext' first and 'AttributeCommonInfo' last is |
9313 | // used here.) As much data is copied or imported from the old attribute |
9314 | // as possible. The passed arguments should be already imported. |
9315 | // If an import error happens, the internal error is set to it, and any |
9316 | // further import attempt is ignored. |
9317 | template <typename T, typename... Arg> |
9318 | void importAttr(const T *FromAttr, Arg &&...ImportedArg) { |
9319 | static_assert(std::is_base_of<Attr, T>::value, |
9320 | "T should be subclass of Attr." ); |
9321 | assert(!ToAttr && "Use one AttrImporter to import one Attribute object." ); |
9322 | |
9323 | const IdentifierInfo *ToAttrName = Importer.Import(FromAttr->getAttrName()); |
9324 | const IdentifierInfo *ToScopeName = |
9325 | Importer.Import(FromAttr->getScopeName()); |
9326 | SourceRange ToAttrRange = |
9327 | NImporter.importChecked(Err, FromAttr->getRange()); |
9328 | SourceLocation ToScopeLoc = |
9329 | NImporter.importChecked(Err, FromAttr->getScopeLoc()); |
9330 | |
9331 | if (Err) |
9332 | return; |
9333 | |
9334 | AttributeCommonInfo ToI( |
9335 | ToAttrName, AttributeScopeInfo(ToScopeName, ToScopeLoc), ToAttrRange, |
9336 | FromAttr->getParsedKind(), FromAttr->getForm()); |
9337 | // The "SemanticSpelling" is not needed to be passed to the constructor. |
9338 | // That value is recalculated from the SpellingListIndex if needed. |
9339 | ToAttr = T::Create(Importer.getToContext(), |
9340 | std::forward<Arg>(ImportedArg)..., ToI); |
9341 | |
9342 | ToAttr->setImplicit(FromAttr->isImplicit()); |
9343 | ToAttr->setPackExpansion(FromAttr->isPackExpansion()); |
9344 | if (auto *ToInheritableAttr = dyn_cast<InheritableAttr>(Val: ToAttr)) |
9345 | ToInheritableAttr->setInherited(FromAttr->isInherited()); |
9346 | } |
9347 | |
9348 | // Create a clone of the 'FromAttr' and import its source range only. |
9349 | // This causes objects with invalid references to be created if the 'FromAttr' |
9350 | // contains other data that should be imported. |
9351 | void cloneAttr(const Attr *FromAttr) { |
9352 | assert(!ToAttr && "Use one AttrImporter to import one Attribute object." ); |
9353 | |
9354 | SourceRange ToRange = NImporter.importChecked(Err, From: FromAttr->getRange()); |
9355 | if (Err) |
9356 | return; |
9357 | |
9358 | ToAttr = FromAttr->clone(C&: Importer.getToContext()); |
9359 | ToAttr->setRange(ToRange); |
9360 | ToAttr->setAttrName(Importer.Import(FromId: FromAttr->getAttrName())); |
9361 | } |
9362 | |
9363 | // Get the result of the previous import attempt (can be used only once). |
9364 | llvm::Expected<Attr *> getResult() && { |
9365 | if (Err) |
9366 | return std::move(Err); |
9367 | assert(ToAttr && "Attribute should be created." ); |
9368 | return ToAttr; |
9369 | } |
9370 | }; |
9371 | } // namespace |
9372 | |
9373 | Expected<Attr *> ASTImporter::Import(const Attr *FromAttr) { |
9374 | AttrImporter AI(*this); |
9375 | |
9376 | // FIXME: Is there some kind of AttrVisitor to use here? |
9377 | switch (FromAttr->getKind()) { |
9378 | case attr::Aligned: { |
9379 | auto *From = cast<AlignedAttr>(Val: FromAttr); |
9380 | if (From->isAlignmentExpr()) |
9381 | AI.importAttr(FromAttr: From, ImportedArg: true, ImportedArg: AI.importArg(From: From->getAlignmentExpr()).value()); |
9382 | else |
9383 | AI.importAttr(FromAttr: From, ImportedArg: false, |
9384 | ImportedArg: AI.importArg(From: From->getAlignmentType()).value()); |
9385 | break; |
9386 | } |
9387 | |
9388 | case attr::AlignValue: { |
9389 | auto *From = cast<AlignValueAttr>(Val: FromAttr); |
9390 | AI.importAttr(FromAttr: From, ImportedArg: AI.importArg(From: From->getAlignment()).value()); |
9391 | break; |
9392 | } |
9393 | |
9394 | case attr::Format: { |
9395 | const auto *From = cast<FormatAttr>(Val: FromAttr); |
9396 | AI.importAttr(FromAttr: From, ImportedArg: Import(FromId: From->getType()), ImportedArg: From->getFormatIdx(), |
9397 | ImportedArg: From->getFirstArg()); |
9398 | break; |
9399 | } |
9400 | |
9401 | case attr::EnableIf: { |
9402 | const auto *From = cast<EnableIfAttr>(Val: FromAttr); |
9403 | AI.importAttr(FromAttr: From, ImportedArg: AI.importArg(From: From->getCond()).value(), |
9404 | ImportedArg: From->getMessage()); |
9405 | break; |
9406 | } |
9407 | |
9408 | case attr::AssertCapability: { |
9409 | const auto *From = cast<AssertCapabilityAttr>(Val: FromAttr); |
9410 | AI.importAttr(FromAttr: From, |
9411 | ImportedArg: AI.importArrayArg(From: From->args(), ArraySize: From->args_size()).value(), |
9412 | ImportedArg: From->args_size()); |
9413 | break; |
9414 | } |
9415 | case attr::AcquireCapability: { |
9416 | const auto *From = cast<AcquireCapabilityAttr>(Val: FromAttr); |
9417 | AI.importAttr(FromAttr: From, |
9418 | ImportedArg: AI.importArrayArg(From: From->args(), ArraySize: From->args_size()).value(), |
9419 | ImportedArg: From->args_size()); |
9420 | break; |
9421 | } |
9422 | case attr::TryAcquireCapability: { |
9423 | const auto *From = cast<TryAcquireCapabilityAttr>(Val: FromAttr); |
9424 | AI.importAttr(FromAttr: From, ImportedArg: AI.importArg(From: From->getSuccessValue()).value(), |
9425 | ImportedArg: AI.importArrayArg(From: From->args(), ArraySize: From->args_size()).value(), |
9426 | ImportedArg: From->args_size()); |
9427 | break; |
9428 | } |
9429 | case attr::ReleaseCapability: { |
9430 | const auto *From = cast<ReleaseCapabilityAttr>(Val: FromAttr); |
9431 | AI.importAttr(FromAttr: From, |
9432 | ImportedArg: AI.importArrayArg(From: From->args(), ArraySize: From->args_size()).value(), |
9433 | ImportedArg: From->args_size()); |
9434 | break; |
9435 | } |
9436 | case attr::RequiresCapability: { |
9437 | const auto *From = cast<RequiresCapabilityAttr>(Val: FromAttr); |
9438 | AI.importAttr(FromAttr: From, |
9439 | ImportedArg: AI.importArrayArg(From: From->args(), ArraySize: From->args_size()).value(), |
9440 | ImportedArg: From->args_size()); |
9441 | break; |
9442 | } |
9443 | case attr::GuardedBy: { |
9444 | const auto *From = cast<GuardedByAttr>(Val: FromAttr); |
9445 | AI.importAttr(FromAttr: From, ImportedArg: AI.importArg(From: From->getArg()).value()); |
9446 | break; |
9447 | } |
9448 | case attr::PtGuardedBy: { |
9449 | const auto *From = cast<PtGuardedByAttr>(Val: FromAttr); |
9450 | AI.importAttr(FromAttr: From, ImportedArg: AI.importArg(From: From->getArg()).value()); |
9451 | break; |
9452 | } |
9453 | case attr::AcquiredAfter: { |
9454 | const auto *From = cast<AcquiredAfterAttr>(Val: FromAttr); |
9455 | AI.importAttr(FromAttr: From, |
9456 | ImportedArg: AI.importArrayArg(From: From->args(), ArraySize: From->args_size()).value(), |
9457 | ImportedArg: From->args_size()); |
9458 | break; |
9459 | } |
9460 | case attr::AcquiredBefore: { |
9461 | const auto *From = cast<AcquiredBeforeAttr>(Val: FromAttr); |
9462 | AI.importAttr(FromAttr: From, |
9463 | ImportedArg: AI.importArrayArg(From: From->args(), ArraySize: From->args_size()).value(), |
9464 | ImportedArg: From->args_size()); |
9465 | break; |
9466 | } |
9467 | case attr::LockReturned: { |
9468 | const auto *From = cast<LockReturnedAttr>(Val: FromAttr); |
9469 | AI.importAttr(FromAttr: From, ImportedArg: AI.importArg(From: From->getArg()).value()); |
9470 | break; |
9471 | } |
9472 | case attr::LocksExcluded: { |
9473 | const auto *From = cast<LocksExcludedAttr>(Val: FromAttr); |
9474 | AI.importAttr(FromAttr: From, |
9475 | ImportedArg: AI.importArrayArg(From: From->args(), ArraySize: From->args_size()).value(), |
9476 | ImportedArg: From->args_size()); |
9477 | break; |
9478 | } |
9479 | default: { |
9480 | // The default branch works for attributes that have no arguments to import. |
9481 | // FIXME: Handle every attribute type that has arguments of type to import |
9482 | // (most often Expr* or Decl* or type) in the switch above. |
9483 | AI.cloneAttr(FromAttr); |
9484 | break; |
9485 | } |
9486 | } |
9487 | |
9488 | return std::move(AI).getResult(); |
9489 | } |
9490 | |
9491 | Decl *ASTImporter::GetAlreadyImportedOrNull(const Decl *FromD) const { |
9492 | return ImportedDecls.lookup(Val: FromD); |
9493 | } |
9494 | |
9495 | TranslationUnitDecl *ASTImporter::GetFromTU(Decl *ToD) { |
9496 | auto FromDPos = ImportedFromDecls.find(Val: ToD); |
9497 | if (FromDPos == ImportedFromDecls.end()) |
9498 | return nullptr; |
9499 | return FromDPos->second->getTranslationUnitDecl(); |
9500 | } |
9501 | |
9502 | Expected<Decl *> ASTImporter::Import(Decl *FromD) { |
9503 | if (!FromD) |
9504 | return nullptr; |
9505 | |
9506 | // Push FromD to the stack, and remove that when we return. |
9507 | ImportPath.push(D: FromD); |
9508 | auto ImportPathBuilder = |
9509 | llvm::make_scope_exit(F: [this]() { ImportPath.pop(); }); |
9510 | |
9511 | // Check whether there was a previous failed import. |
9512 | // If yes return the existing error. |
9513 | if (auto Error = getImportDeclErrorIfAny(FromD)) |
9514 | return make_error<ASTImportError>(Args&: *Error); |
9515 | |
9516 | // Check whether we've already imported this declaration. |
9517 | Decl *ToD = GetAlreadyImportedOrNull(FromD); |
9518 | if (ToD) { |
9519 | // Already imported (possibly from another TU) and with an error. |
9520 | if (auto Error = SharedState->getImportDeclErrorIfAny(ToD)) { |
9521 | setImportDeclError(From: FromD, Error: *Error); |
9522 | return make_error<ASTImportError>(Args&: *Error); |
9523 | } |
9524 | |
9525 | // If FromD has some updated flags after last import, apply it. |
9526 | updateFlags(From: FromD, To: ToD); |
9527 | // If we encounter a cycle during an import then we save the relevant part |
9528 | // of the import path associated to the Decl. |
9529 | if (ImportPath.hasCycleAtBack()) |
9530 | SavedImportPaths[FromD].push_back(Elt: ImportPath.copyCycleAtBack()); |
9531 | return ToD; |
9532 | } |
9533 | |
9534 | // Import the declaration. |
9535 | ExpectedDecl ToDOrErr = ImportImpl(FromD); |
9536 | if (!ToDOrErr) { |
9537 | // Failed to import. |
9538 | |
9539 | auto Pos = ImportedDecls.find(Val: FromD); |
9540 | if (Pos != ImportedDecls.end()) { |
9541 | // Import failed after the object was created. |
9542 | // Remove all references to it. |
9543 | auto *ToD = Pos->second; |
9544 | ImportedDecls.erase(I: Pos); |
9545 | |
9546 | // ImportedDecls and ImportedFromDecls are not symmetric. It may happen |
9547 | // (e.g. with namespaces) that several decls from the 'from' context are |
9548 | // mapped to the same decl in the 'to' context. If we removed entries |
9549 | // from the LookupTable here then we may end up removing them multiple |
9550 | // times. |
9551 | |
9552 | // The Lookuptable contains decls only which are in the 'to' context. |
9553 | // Remove from the Lookuptable only if it is *imported* into the 'to' |
9554 | // context (and do not remove it if it was added during the initial |
9555 | // traverse of the 'to' context). |
9556 | auto PosF = ImportedFromDecls.find(Val: ToD); |
9557 | if (PosF != ImportedFromDecls.end()) { |
9558 | // In the case of TypedefNameDecl we create the Decl first and only |
9559 | // then we import and set its DeclContext. So, the DC might not be set |
9560 | // when we reach here. |
9561 | if (ToD->getDeclContext()) |
9562 | SharedState->removeDeclFromLookup(D: ToD); |
9563 | ImportedFromDecls.erase(I: PosF); |
9564 | } |
9565 | |
9566 | // FIXME: AST may contain remaining references to the failed object. |
9567 | // However, the ImportDeclErrors in the shared state contains all the |
9568 | // failed objects together with their error. |
9569 | } |
9570 | |
9571 | // Error encountered for the first time. |
9572 | // After takeError the error is not usable any more in ToDOrErr. |
9573 | // Get a copy of the error object (any more simple solution for this?). |
9574 | ASTImportError ErrOut; |
9575 | handleAllErrors(E: ToDOrErr.takeError(), |
9576 | Handlers: [&ErrOut](const ASTImportError &E) { ErrOut = E; }); |
9577 | setImportDeclError(From: FromD, Error: ErrOut); |
9578 | // Set the error for the mapped to Decl, which is in the "to" context. |
9579 | if (Pos != ImportedDecls.end()) |
9580 | SharedState->setImportDeclError(To: Pos->second, Error: ErrOut); |
9581 | |
9582 | // Set the error for all nodes which have been created before we |
9583 | // recognized the error. |
9584 | for (const auto &Path : SavedImportPaths[FromD]) { |
9585 | // The import path contains import-dependency nodes first. |
9586 | // Save the node that was imported as dependency of the current node. |
9587 | Decl *PrevFromDi = FromD; |
9588 | for (Decl *FromDi : Path) { |
9589 | // Begin and end of the path equals 'FromD', skip it. |
9590 | if (FromDi == FromD) |
9591 | continue; |
9592 | // We should not set import error on a node and all following nodes in |
9593 | // the path if child import errors are ignored. |
9594 | if (ChildErrorHandlingStrategy(FromDi).ignoreChildErrorOnParent( |
9595 | FromChildD: PrevFromDi)) |
9596 | break; |
9597 | PrevFromDi = FromDi; |
9598 | setImportDeclError(From: FromDi, Error: ErrOut); |
9599 | //FIXME Should we remove these Decls from ImportedDecls? |
9600 | // Set the error for the mapped to Decl, which is in the "to" context. |
9601 | auto Ii = ImportedDecls.find(Val: FromDi); |
9602 | if (Ii != ImportedDecls.end()) |
9603 | SharedState->setImportDeclError(To: Ii->second, Error: ErrOut); |
9604 | // FIXME Should we remove these Decls from the LookupTable, |
9605 | // and from ImportedFromDecls? |
9606 | } |
9607 | } |
9608 | SavedImportPaths.erase(Val: FromD); |
9609 | |
9610 | // Do not return ToDOrErr, error was taken out of it. |
9611 | return make_error<ASTImportError>(Args&: ErrOut); |
9612 | } |
9613 | |
9614 | ToD = *ToDOrErr; |
9615 | |
9616 | // FIXME: Handle the "already imported with error" case. We can get here |
9617 | // nullptr only if GetImportedOrCreateDecl returned nullptr (after a |
9618 | // previously failed create was requested). |
9619 | // Later GetImportedOrCreateDecl can be updated to return the error. |
9620 | if (!ToD) { |
9621 | auto Err = getImportDeclErrorIfAny(FromD); |
9622 | assert(Err); |
9623 | return make_error<ASTImportError>(Args&: *Err); |
9624 | } |
9625 | |
9626 | // We could import from the current TU without error. But previously we |
9627 | // already had imported a Decl as `ToD` from another TU (with another |
9628 | // ASTImporter object) and with an error. |
9629 | if (auto Error = SharedState->getImportDeclErrorIfAny(ToD)) { |
9630 | setImportDeclError(From: FromD, Error: *Error); |
9631 | return make_error<ASTImportError>(Args&: *Error); |
9632 | } |
9633 | // Make sure that ImportImpl registered the imported decl. |
9634 | assert(ImportedDecls.count(FromD) != 0 && "Missing call to MapImported?" ); |
9635 | |
9636 | if (FromD->hasAttrs()) |
9637 | for (const Attr *FromAttr : FromD->getAttrs()) { |
9638 | auto ToAttrOrErr = Import(FromAttr); |
9639 | if (ToAttrOrErr) |
9640 | ToD->addAttr(A: *ToAttrOrErr); |
9641 | else |
9642 | return ToAttrOrErr.takeError(); |
9643 | } |
9644 | |
9645 | // Notify subclasses. |
9646 | Imported(From: FromD, To: ToD); |
9647 | |
9648 | updateFlags(From: FromD, To: ToD); |
9649 | SavedImportPaths.erase(Val: FromD); |
9650 | return ToDOrErr; |
9651 | } |
9652 | |
9653 | llvm::Expected<InheritedConstructor> |
9654 | ASTImporter::Import(const InheritedConstructor &From) { |
9655 | return ASTNodeImporter(*this).ImportInheritedConstructor(From); |
9656 | } |
9657 | |
9658 | Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) { |
9659 | if (!FromDC) |
9660 | return FromDC; |
9661 | |
9662 | ExpectedDecl ToDCOrErr = Import(FromD: cast<Decl>(Val: FromDC)); |
9663 | if (!ToDCOrErr) |
9664 | return ToDCOrErr.takeError(); |
9665 | auto *ToDC = cast<DeclContext>(Val: *ToDCOrErr); |
9666 | |
9667 | // When we're using a record/enum/Objective-C class/protocol as a context, we |
9668 | // need it to have a definition. |
9669 | if (auto *ToRecord = dyn_cast<RecordDecl>(Val: ToDC)) { |
9670 | auto *FromRecord = cast<RecordDecl>(Val: FromDC); |
9671 | if (ToRecord->isCompleteDefinition()) |
9672 | return ToDC; |
9673 | |
9674 | // If FromRecord is not defined we need to force it to be. |
9675 | // Simply calling CompleteDecl(...) for a RecordDecl will break some cases |
9676 | // it will start the definition but we never finish it. |
9677 | // If there are base classes they won't be imported and we will |
9678 | // be missing anything that we inherit from those bases. |
9679 | if (FromRecord->getASTContext().getExternalSource() && |
9680 | !FromRecord->isCompleteDefinition()) |
9681 | FromRecord->getASTContext().getExternalSource()->CompleteType(Tag: FromRecord); |
9682 | |
9683 | if (FromRecord->isCompleteDefinition()) |
9684 | if (Error Err = ASTNodeImporter(*this).ImportDefinition( |
9685 | From: FromRecord, To: ToRecord, Kind: ASTNodeImporter::IDK_Basic)) |
9686 | return std::move(Err); |
9687 | } else if (auto *ToEnum = dyn_cast<EnumDecl>(Val: ToDC)) { |
9688 | auto * = cast<EnumDecl>(Val: FromDC); |
9689 | if (ToEnum->isCompleteDefinition()) { |
9690 | // Do nothing. |
9691 | } else if (FromEnum->isCompleteDefinition()) { |
9692 | if (Error Err = ASTNodeImporter(*this).ImportDefinition( |
9693 | From: FromEnum, To: ToEnum, Kind: ASTNodeImporter::IDK_Basic)) |
9694 | return std::move(Err); |
9695 | } else { |
9696 | CompleteDecl(D: ToEnum); |
9697 | } |
9698 | } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(Val: ToDC)) { |
9699 | auto *FromClass = cast<ObjCInterfaceDecl>(Val: FromDC); |
9700 | if (ToClass->getDefinition()) { |
9701 | // Do nothing. |
9702 | } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) { |
9703 | if (Error Err = ASTNodeImporter(*this).ImportDefinition( |
9704 | From: FromDef, To: ToClass, Kind: ASTNodeImporter::IDK_Basic)) |
9705 | return std::move(Err); |
9706 | } else { |
9707 | CompleteDecl(D: ToClass); |
9708 | } |
9709 | } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(Val: ToDC)) { |
9710 | auto *FromProto = cast<ObjCProtocolDecl>(Val: FromDC); |
9711 | if (ToProto->getDefinition()) { |
9712 | // Do nothing. |
9713 | } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) { |
9714 | if (Error Err = ASTNodeImporter(*this).ImportDefinition( |
9715 | From: FromDef, To: ToProto, Kind: ASTNodeImporter::IDK_Basic)) |
9716 | return std::move(Err); |
9717 | } else { |
9718 | CompleteDecl(D: ToProto); |
9719 | } |
9720 | } |
9721 | |
9722 | return ToDC; |
9723 | } |
9724 | |
9725 | Expected<Expr *> ASTImporter::Import(Expr *FromE) { |
9726 | if (ExpectedStmt ToSOrErr = Import(FromS: cast_or_null<Stmt>(Val: FromE))) |
9727 | return cast_or_null<Expr>(Val: *ToSOrErr); |
9728 | else |
9729 | return ToSOrErr.takeError(); |
9730 | } |
9731 | |
9732 | Expected<Stmt *> ASTImporter::Import(Stmt *FromS) { |
9733 | if (!FromS) |
9734 | return nullptr; |
9735 | |
9736 | // Check whether we've already imported this statement. |
9737 | llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(Val: FromS); |
9738 | if (Pos != ImportedStmts.end()) |
9739 | return Pos->second; |
9740 | |
9741 | // Import the statement. |
9742 | ASTNodeImporter Importer(*this); |
9743 | ExpectedStmt ToSOrErr = Importer.Visit(S: FromS); |
9744 | if (!ToSOrErr) |
9745 | return ToSOrErr; |
9746 | |
9747 | if (auto *ToE = dyn_cast<Expr>(Val: *ToSOrErr)) { |
9748 | auto *FromE = cast<Expr>(Val: FromS); |
9749 | // Copy ExprBitfields, which may not be handled in Expr subclasses |
9750 | // constructors. |
9751 | ToE->setValueKind(FromE->getValueKind()); |
9752 | ToE->setObjectKind(FromE->getObjectKind()); |
9753 | ToE->setDependence(FromE->getDependence()); |
9754 | } |
9755 | |
9756 | // Record the imported statement object. |
9757 | ImportedStmts[FromS] = *ToSOrErr; |
9758 | return ToSOrErr; |
9759 | } |
9760 | |
9761 | Expected<NestedNameSpecifier *> |
9762 | ASTImporter::Import(NestedNameSpecifier *FromNNS) { |
9763 | if (!FromNNS) |
9764 | return nullptr; |
9765 | |
9766 | NestedNameSpecifier *Prefix = nullptr; |
9767 | if (Error Err = importInto(To&: Prefix, From: FromNNS->getPrefix())) |
9768 | return std::move(Err); |
9769 | |
9770 | switch (FromNNS->getKind()) { |
9771 | case NestedNameSpecifier::Identifier: |
9772 | assert(FromNNS->getAsIdentifier() && "NNS should contain identifier." ); |
9773 | return NestedNameSpecifier::Create(Context: ToContext, Prefix, |
9774 | II: Import(FromId: FromNNS->getAsIdentifier())); |
9775 | |
9776 | case NestedNameSpecifier::Namespace: |
9777 | if (ExpectedDecl NSOrErr = Import(FromD: FromNNS->getAsNamespace())) { |
9778 | return NestedNameSpecifier::Create(Context: ToContext, Prefix, |
9779 | NS: cast<NamespaceDecl>(Val: *NSOrErr)); |
9780 | } else |
9781 | return NSOrErr.takeError(); |
9782 | |
9783 | case NestedNameSpecifier::NamespaceAlias: |
9784 | if (ExpectedDecl NSADOrErr = Import(FromD: FromNNS->getAsNamespaceAlias())) |
9785 | return NestedNameSpecifier::Create(Context: ToContext, Prefix, |
9786 | Alias: cast<NamespaceAliasDecl>(Val: *NSADOrErr)); |
9787 | else |
9788 | return NSADOrErr.takeError(); |
9789 | |
9790 | case NestedNameSpecifier::Global: |
9791 | return NestedNameSpecifier::GlobalSpecifier(Context: ToContext); |
9792 | |
9793 | case NestedNameSpecifier::Super: |
9794 | if (ExpectedDecl RDOrErr = Import(FromD: FromNNS->getAsRecordDecl())) |
9795 | return NestedNameSpecifier::SuperSpecifier(Context: ToContext, |
9796 | RD: cast<CXXRecordDecl>(Val: *RDOrErr)); |
9797 | else |
9798 | return RDOrErr.takeError(); |
9799 | |
9800 | case NestedNameSpecifier::TypeSpec: |
9801 | if (ExpectedTypePtr TyOrErr = Import(FromT: FromNNS->getAsType())) { |
9802 | return NestedNameSpecifier::Create(Context: ToContext, Prefix, T: *TyOrErr); |
9803 | } else { |
9804 | return TyOrErr.takeError(); |
9805 | } |
9806 | } |
9807 | |
9808 | llvm_unreachable("Invalid nested name specifier kind" ); |
9809 | } |
9810 | |
9811 | Expected<NestedNameSpecifierLoc> |
9812 | ASTImporter::Import(NestedNameSpecifierLoc FromNNS) { |
9813 | // Copied from NestedNameSpecifier mostly. |
9814 | SmallVector<NestedNameSpecifierLoc , 8> NestedNames; |
9815 | NestedNameSpecifierLoc NNS = FromNNS; |
9816 | |
9817 | // Push each of the nested-name-specifiers's onto a stack for |
9818 | // serialization in reverse order. |
9819 | while (NNS) { |
9820 | NestedNames.push_back(Elt: NNS); |
9821 | NNS = NNS.getPrefix(); |
9822 | } |
9823 | |
9824 | NestedNameSpecifierLocBuilder Builder; |
9825 | |
9826 | while (!NestedNames.empty()) { |
9827 | NNS = NestedNames.pop_back_val(); |
9828 | NestedNameSpecifier *Spec = nullptr; |
9829 | if (Error Err = importInto(To&: Spec, From: NNS.getNestedNameSpecifier())) |
9830 | return std::move(Err); |
9831 | |
9832 | NestedNameSpecifier::SpecifierKind Kind = Spec->getKind(); |
9833 | |
9834 | SourceLocation ToLocalBeginLoc, ToLocalEndLoc; |
9835 | if (Kind != NestedNameSpecifier::Super) { |
9836 | if (Error Err = importInto(To&: ToLocalBeginLoc, From: NNS.getLocalBeginLoc())) |
9837 | return std::move(Err); |
9838 | |
9839 | if (Kind != NestedNameSpecifier::Global) |
9840 | if (Error Err = importInto(To&: ToLocalEndLoc, From: NNS.getLocalEndLoc())) |
9841 | return std::move(Err); |
9842 | } |
9843 | |
9844 | switch (Kind) { |
9845 | case NestedNameSpecifier::Identifier: |
9846 | Builder.Extend(Context&: getToContext(), Identifier: Spec->getAsIdentifier(), IdentifierLoc: ToLocalBeginLoc, |
9847 | ColonColonLoc: ToLocalEndLoc); |
9848 | break; |
9849 | |
9850 | case NestedNameSpecifier::Namespace: |
9851 | Builder.Extend(Context&: getToContext(), Namespace: Spec->getAsNamespace(), NamespaceLoc: ToLocalBeginLoc, |
9852 | ColonColonLoc: ToLocalEndLoc); |
9853 | break; |
9854 | |
9855 | case NestedNameSpecifier::NamespaceAlias: |
9856 | Builder.Extend(Context&: getToContext(), Alias: Spec->getAsNamespaceAlias(), |
9857 | AliasLoc: ToLocalBeginLoc, ColonColonLoc: ToLocalEndLoc); |
9858 | break; |
9859 | |
9860 | case NestedNameSpecifier::TypeSpec: { |
9861 | SourceLocation ToTLoc; |
9862 | if (Error Err = importInto(To&: ToTLoc, From: NNS.getTypeLoc().getBeginLoc())) |
9863 | return std::move(Err); |
9864 | TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo( |
9865 | T: QualType(Spec->getAsType(), 0), Loc: ToTLoc); |
9866 | Builder.Extend(Context&: getToContext(), TL: TSI->getTypeLoc(), ColonColonLoc: ToLocalEndLoc); |
9867 | break; |
9868 | } |
9869 | |
9870 | case NestedNameSpecifier::Global: |
9871 | Builder.MakeGlobal(Context&: getToContext(), ColonColonLoc: ToLocalBeginLoc); |
9872 | break; |
9873 | |
9874 | case NestedNameSpecifier::Super: { |
9875 | auto ToSourceRangeOrErr = Import(FromRange: NNS.getSourceRange()); |
9876 | if (!ToSourceRangeOrErr) |
9877 | return ToSourceRangeOrErr.takeError(); |
9878 | |
9879 | Builder.MakeSuper(Context&: getToContext(), RD: Spec->getAsRecordDecl(), |
9880 | SuperLoc: ToSourceRangeOrErr->getBegin(), |
9881 | ColonColonLoc: ToSourceRangeOrErr->getEnd()); |
9882 | } |
9883 | } |
9884 | } |
9885 | |
9886 | return Builder.getWithLocInContext(Context&: getToContext()); |
9887 | } |
9888 | |
9889 | Expected<TemplateName> ASTImporter::Import(TemplateName From) { |
9890 | switch (From.getKind()) { |
9891 | case TemplateName::Template: |
9892 | if (ExpectedDecl ToTemplateOrErr = Import(FromD: From.getAsTemplateDecl())) |
9893 | return TemplateName(cast<TemplateDecl>(Val: (*ToTemplateOrErr)->getCanonicalDecl())); |
9894 | else |
9895 | return ToTemplateOrErr.takeError(); |
9896 | |
9897 | case TemplateName::OverloadedTemplate: { |
9898 | OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate(); |
9899 | UnresolvedSet<2> ToTemplates; |
9900 | for (auto *I : *FromStorage) { |
9901 | if (auto ToOrErr = Import(FromD: I)) |
9902 | ToTemplates.addDecl(D: cast<NamedDecl>(Val: *ToOrErr)); |
9903 | else |
9904 | return ToOrErr.takeError(); |
9905 | } |
9906 | return ToContext.getOverloadedTemplateName(Begin: ToTemplates.begin(), |
9907 | End: ToTemplates.end()); |
9908 | } |
9909 | |
9910 | case TemplateName::AssumedTemplate: { |
9911 | AssumedTemplateStorage *FromStorage = From.getAsAssumedTemplateName(); |
9912 | auto DeclNameOrErr = Import(FromName: FromStorage->getDeclName()); |
9913 | if (!DeclNameOrErr) |
9914 | return DeclNameOrErr.takeError(); |
9915 | return ToContext.getAssumedTemplateName(Name: *DeclNameOrErr); |
9916 | } |
9917 | |
9918 | case TemplateName::QualifiedTemplate: { |
9919 | QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName(); |
9920 | auto QualifierOrErr = Import(FromNNS: QTN->getQualifier()); |
9921 | if (!QualifierOrErr) |
9922 | return QualifierOrErr.takeError(); |
9923 | auto TNOrErr = Import(From: QTN->getUnderlyingTemplate()); |
9924 | if (!TNOrErr) |
9925 | return TNOrErr.takeError(); |
9926 | return ToContext.getQualifiedTemplateName( |
9927 | NNS: *QualifierOrErr, TemplateKeyword: QTN->hasTemplateKeyword(), Template: *TNOrErr); |
9928 | } |
9929 | |
9930 | case TemplateName::DependentTemplate: { |
9931 | DependentTemplateName *DTN = From.getAsDependentTemplateName(); |
9932 | auto QualifierOrErr = Import(FromNNS: DTN->getQualifier()); |
9933 | if (!QualifierOrErr) |
9934 | return QualifierOrErr.takeError(); |
9935 | return ToContext.getDependentTemplateName( |
9936 | Name: {*QualifierOrErr, Import(FromIO: DTN->getName()), DTN->hasTemplateKeyword()}); |
9937 | } |
9938 | |
9939 | case TemplateName::SubstTemplateTemplateParm: { |
9940 | SubstTemplateTemplateParmStorage *Subst = |
9941 | From.getAsSubstTemplateTemplateParm(); |
9942 | auto ReplacementOrErr = Import(From: Subst->getReplacement()); |
9943 | if (!ReplacementOrErr) |
9944 | return ReplacementOrErr.takeError(); |
9945 | |
9946 | auto AssociatedDeclOrErr = Import(FromD: Subst->getAssociatedDecl()); |
9947 | if (!AssociatedDeclOrErr) |
9948 | return AssociatedDeclOrErr.takeError(); |
9949 | |
9950 | return ToContext.getSubstTemplateTemplateParm( |
9951 | replacement: *ReplacementOrErr, AssociatedDecl: *AssociatedDeclOrErr, Index: Subst->getIndex(), |
9952 | PackIndex: Subst->getPackIndex(), Final: Subst->getFinal()); |
9953 | } |
9954 | |
9955 | case TemplateName::SubstTemplateTemplateParmPack: { |
9956 | SubstTemplateTemplateParmPackStorage *SubstPack = |
9957 | From.getAsSubstTemplateTemplateParmPack(); |
9958 | ASTNodeImporter Importer(*this); |
9959 | auto ArgPackOrErr = |
9960 | Importer.ImportTemplateArgument(From: SubstPack->getArgumentPack()); |
9961 | if (!ArgPackOrErr) |
9962 | return ArgPackOrErr.takeError(); |
9963 | |
9964 | auto AssociatedDeclOrErr = Import(FromD: SubstPack->getAssociatedDecl()); |
9965 | if (!AssociatedDeclOrErr) |
9966 | return AssociatedDeclOrErr.takeError(); |
9967 | |
9968 | return ToContext.getSubstTemplateTemplateParmPack( |
9969 | ArgPack: *ArgPackOrErr, AssociatedDecl: *AssociatedDeclOrErr, Index: SubstPack->getIndex(), |
9970 | Final: SubstPack->getFinal()); |
9971 | } |
9972 | case TemplateName::UsingTemplate: { |
9973 | auto UsingOrError = Import(FromD: From.getAsUsingShadowDecl()); |
9974 | if (!UsingOrError) |
9975 | return UsingOrError.takeError(); |
9976 | return TemplateName(cast<UsingShadowDecl>(Val: *UsingOrError)); |
9977 | } |
9978 | case TemplateName::DeducedTemplate: |
9979 | llvm_unreachable("Unexpected DeducedTemplate" ); |
9980 | } |
9981 | |
9982 | llvm_unreachable("Invalid template name kind" ); |
9983 | } |
9984 | |
9985 | Expected<SourceLocation> ASTImporter::Import(SourceLocation FromLoc) { |
9986 | if (FromLoc.isInvalid()) |
9987 | return SourceLocation{}; |
9988 | |
9989 | SourceManager &FromSM = FromContext.getSourceManager(); |
9990 | bool IsBuiltin = FromSM.isWrittenInBuiltinFile(Loc: FromLoc); |
9991 | |
9992 | FileIDAndOffset Decomposed = FromSM.getDecomposedLoc(Loc: FromLoc); |
9993 | Expected<FileID> ToFileIDOrErr = Import(Decomposed.first, IsBuiltin); |
9994 | if (!ToFileIDOrErr) |
9995 | return ToFileIDOrErr.takeError(); |
9996 | SourceManager &ToSM = ToContext.getSourceManager(); |
9997 | return ToSM.getComposedLoc(FID: *ToFileIDOrErr, Offset: Decomposed.second); |
9998 | } |
9999 | |
10000 | Expected<SourceRange> ASTImporter::Import(SourceRange FromRange) { |
10001 | SourceLocation ToBegin, ToEnd; |
10002 | if (Error Err = importInto(To&: ToBegin, From: FromRange.getBegin())) |
10003 | return std::move(Err); |
10004 | if (Error Err = importInto(To&: ToEnd, From: FromRange.getEnd())) |
10005 | return std::move(Err); |
10006 | |
10007 | return SourceRange(ToBegin, ToEnd); |
10008 | } |
10009 | |
10010 | Expected<FileID> ASTImporter::Import(FileID FromID, bool IsBuiltin) { |
10011 | llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(Val: FromID); |
10012 | if (Pos != ImportedFileIDs.end()) |
10013 | return Pos->second; |
10014 | |
10015 | SourceManager &FromSM = FromContext.getSourceManager(); |
10016 | SourceManager &ToSM = ToContext.getSourceManager(); |
10017 | const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FID: FromID); |
10018 | |
10019 | // Map the FromID to the "to" source manager. |
10020 | FileID ToID; |
10021 | if (FromSLoc.isExpansion()) { |
10022 | const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion(); |
10023 | ExpectedSLoc ToSpLoc = Import(FromLoc: FromEx.getSpellingLoc()); |
10024 | if (!ToSpLoc) |
10025 | return ToSpLoc.takeError(); |
10026 | ExpectedSLoc ToExLocS = Import(FromLoc: FromEx.getExpansionLocStart()); |
10027 | if (!ToExLocS) |
10028 | return ToExLocS.takeError(); |
10029 | unsigned ExLength = FromSM.getFileIDSize(FID: FromID); |
10030 | SourceLocation MLoc; |
10031 | if (FromEx.isMacroArgExpansion()) { |
10032 | MLoc = ToSM.createMacroArgExpansionLoc(SpellingLoc: *ToSpLoc, ExpansionLoc: *ToExLocS, Length: ExLength); |
10033 | } else { |
10034 | if (ExpectedSLoc ToExLocE = Import(FromLoc: FromEx.getExpansionLocEnd())) |
10035 | MLoc = ToSM.createExpansionLoc(SpellingLoc: *ToSpLoc, ExpansionLocStart: *ToExLocS, ExpansionLocEnd: *ToExLocE, Length: ExLength, |
10036 | ExpansionIsTokenRange: FromEx.isExpansionTokenRange()); |
10037 | else |
10038 | return ToExLocE.takeError(); |
10039 | } |
10040 | ToID = ToSM.getFileID(SpellingLoc: MLoc); |
10041 | } else { |
10042 | const SrcMgr::ContentCache *Cache = &FromSLoc.getFile().getContentCache(); |
10043 | |
10044 | if (!IsBuiltin && !Cache->BufferOverridden) { |
10045 | // Include location of this file. |
10046 | ExpectedSLoc ToIncludeLoc = Import(FromLoc: FromSLoc.getFile().getIncludeLoc()); |
10047 | if (!ToIncludeLoc) |
10048 | return ToIncludeLoc.takeError(); |
10049 | |
10050 | // Every FileID that is not the main FileID needs to have a valid include |
10051 | // location so that the include chain points to the main FileID. When |
10052 | // importing the main FileID (which has no include location), we need to |
10053 | // create a fake include location in the main file to keep this property |
10054 | // intact. |
10055 | SourceLocation ToIncludeLocOrFakeLoc = *ToIncludeLoc; |
10056 | if (FromID == FromSM.getMainFileID()) |
10057 | ToIncludeLocOrFakeLoc = ToSM.getLocForStartOfFile(FID: ToSM.getMainFileID()); |
10058 | |
10059 | if (Cache->OrigEntry && Cache->OrigEntry->getDir()) { |
10060 | // FIXME: We probably want to use getVirtualFileRef(), so we don't hit |
10061 | // the disk again |
10062 | // FIXME: We definitely want to re-use the existing MemoryBuffer, rather |
10063 | // than mmap the files several times. |
10064 | auto Entry = |
10065 | ToFileManager.getOptionalFileRef(Filename: Cache->OrigEntry->getName()); |
10066 | // FIXME: The filename may be a virtual name that does probably not |
10067 | // point to a valid file and we get no Entry here. In this case try with |
10068 | // the memory buffer below. |
10069 | if (Entry) |
10070 | ToID = ToSM.createFileID(SourceFile: *Entry, IncludePos: ToIncludeLocOrFakeLoc, |
10071 | FileCharacter: FromSLoc.getFile().getFileCharacteristic()); |
10072 | } |
10073 | } |
10074 | |
10075 | if (ToID.isInvalid() || IsBuiltin) { |
10076 | // FIXME: We want to re-use the existing MemoryBuffer! |
10077 | std::optional<llvm::MemoryBufferRef> FromBuf = |
10078 | Cache->getBufferOrNone(Diag&: FromContext.getDiagnostics(), |
10079 | FM&: FromSM.getFileManager(), Loc: SourceLocation{}); |
10080 | if (!FromBuf) |
10081 | return llvm::make_error<ASTImportError>(Args: ASTImportError::Unknown); |
10082 | |
10083 | std::unique_ptr<llvm::MemoryBuffer> ToBuf = |
10084 | llvm::MemoryBuffer::getMemBufferCopy(InputData: FromBuf->getBuffer(), |
10085 | BufferName: FromBuf->getBufferIdentifier()); |
10086 | ToID = ToSM.createFileID(Buffer: std::move(ToBuf), |
10087 | FileCharacter: FromSLoc.getFile().getFileCharacteristic()); |
10088 | } |
10089 | } |
10090 | |
10091 | assert(ToID.isValid() && "Unexpected invalid fileID was created." ); |
10092 | |
10093 | ImportedFileIDs[FromID] = ToID; |
10094 | return ToID; |
10095 | } |
10096 | |
10097 | Expected<CXXCtorInitializer *> ASTImporter::Import(CXXCtorInitializer *From) { |
10098 | ExpectedExpr ToExprOrErr = Import(FromE: From->getInit()); |
10099 | if (!ToExprOrErr) |
10100 | return ToExprOrErr.takeError(); |
10101 | |
10102 | auto LParenLocOrErr = Import(FromLoc: From->getLParenLoc()); |
10103 | if (!LParenLocOrErr) |
10104 | return LParenLocOrErr.takeError(); |
10105 | |
10106 | auto RParenLocOrErr = Import(FromLoc: From->getRParenLoc()); |
10107 | if (!RParenLocOrErr) |
10108 | return RParenLocOrErr.takeError(); |
10109 | |
10110 | if (From->isBaseInitializer()) { |
10111 | auto ToTInfoOrErr = Import(FromTSI: From->getTypeSourceInfo()); |
10112 | if (!ToTInfoOrErr) |
10113 | return ToTInfoOrErr.takeError(); |
10114 | |
10115 | SourceLocation EllipsisLoc; |
10116 | if (From->isPackExpansion()) |
10117 | if (Error Err = importInto(To&: EllipsisLoc, From: From->getEllipsisLoc())) |
10118 | return std::move(Err); |
10119 | |
10120 | return new (ToContext) CXXCtorInitializer( |
10121 | ToContext, *ToTInfoOrErr, From->isBaseVirtual(), *LParenLocOrErr, |
10122 | *ToExprOrErr, *RParenLocOrErr, EllipsisLoc); |
10123 | } else if (From->isMemberInitializer()) { |
10124 | ExpectedDecl ToFieldOrErr = Import(FromD: From->getMember()); |
10125 | if (!ToFieldOrErr) |
10126 | return ToFieldOrErr.takeError(); |
10127 | |
10128 | auto MemberLocOrErr = Import(FromLoc: From->getMemberLocation()); |
10129 | if (!MemberLocOrErr) |
10130 | return MemberLocOrErr.takeError(); |
10131 | |
10132 | return new (ToContext) CXXCtorInitializer( |
10133 | ToContext, cast_or_null<FieldDecl>(Val: *ToFieldOrErr), *MemberLocOrErr, |
10134 | *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr); |
10135 | } else if (From->isIndirectMemberInitializer()) { |
10136 | ExpectedDecl ToIFieldOrErr = Import(FromD: From->getIndirectMember()); |
10137 | if (!ToIFieldOrErr) |
10138 | return ToIFieldOrErr.takeError(); |
10139 | |
10140 | auto MemberLocOrErr = Import(FromLoc: From->getMemberLocation()); |
10141 | if (!MemberLocOrErr) |
10142 | return MemberLocOrErr.takeError(); |
10143 | |
10144 | return new (ToContext) CXXCtorInitializer( |
10145 | ToContext, cast_or_null<IndirectFieldDecl>(Val: *ToIFieldOrErr), |
10146 | *MemberLocOrErr, *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr); |
10147 | } else if (From->isDelegatingInitializer()) { |
10148 | auto ToTInfoOrErr = Import(FromTSI: From->getTypeSourceInfo()); |
10149 | if (!ToTInfoOrErr) |
10150 | return ToTInfoOrErr.takeError(); |
10151 | |
10152 | return new (ToContext) |
10153 | CXXCtorInitializer(ToContext, *ToTInfoOrErr, *LParenLocOrErr, |
10154 | *ToExprOrErr, *RParenLocOrErr); |
10155 | } else { |
10156 | // FIXME: assert? |
10157 | return make_error<ASTImportError>(); |
10158 | } |
10159 | } |
10160 | |
10161 | Expected<CXXBaseSpecifier *> |
10162 | ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) { |
10163 | auto Pos = ImportedCXXBaseSpecifiers.find(Val: BaseSpec); |
10164 | if (Pos != ImportedCXXBaseSpecifiers.end()) |
10165 | return Pos->second; |
10166 | |
10167 | Expected<SourceRange> ToSourceRange = Import(FromRange: BaseSpec->getSourceRange()); |
10168 | if (!ToSourceRange) |
10169 | return ToSourceRange.takeError(); |
10170 | Expected<TypeSourceInfo *> ToTSI = Import(FromTSI: BaseSpec->getTypeSourceInfo()); |
10171 | if (!ToTSI) |
10172 | return ToTSI.takeError(); |
10173 | ExpectedSLoc ToEllipsisLoc = Import(FromLoc: BaseSpec->getEllipsisLoc()); |
10174 | if (!ToEllipsisLoc) |
10175 | return ToEllipsisLoc.takeError(); |
10176 | CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier( |
10177 | *ToSourceRange, BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(), |
10178 | BaseSpec->getAccessSpecifierAsWritten(), *ToTSI, *ToEllipsisLoc); |
10179 | ImportedCXXBaseSpecifiers[BaseSpec] = Imported; |
10180 | return Imported; |
10181 | } |
10182 | |
10183 | llvm::Expected<APValue> ASTImporter::Import(const APValue &FromValue) { |
10184 | ASTNodeImporter Importer(*this); |
10185 | return Importer.ImportAPValue(FromValue); |
10186 | } |
10187 | |
10188 | Error ASTImporter::ImportDefinition(Decl *From) { |
10189 | ExpectedDecl ToOrErr = Import(FromD: From); |
10190 | if (!ToOrErr) |
10191 | return ToOrErr.takeError(); |
10192 | Decl *To = *ToOrErr; |
10193 | |
10194 | auto *FromDC = cast<DeclContext>(Val: From); |
10195 | ASTNodeImporter Importer(*this); |
10196 | |
10197 | if (auto *ToRecord = dyn_cast<RecordDecl>(Val: To)) { |
10198 | if (!ToRecord->getDefinition()) { |
10199 | return Importer.ImportDefinition( |
10200 | From: cast<RecordDecl>(Val: FromDC), To: ToRecord, |
10201 | Kind: ASTNodeImporter::IDK_Everything); |
10202 | } |
10203 | } |
10204 | |
10205 | if (auto *ToEnum = dyn_cast<EnumDecl>(Val: To)) { |
10206 | if (!ToEnum->getDefinition()) { |
10207 | return Importer.ImportDefinition( |
10208 | From: cast<EnumDecl>(Val: FromDC), To: ToEnum, Kind: ASTNodeImporter::IDK_Everything); |
10209 | } |
10210 | } |
10211 | |
10212 | if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(Val: To)) { |
10213 | if (!ToIFace->getDefinition()) { |
10214 | return Importer.ImportDefinition( |
10215 | From: cast<ObjCInterfaceDecl>(Val: FromDC), To: ToIFace, |
10216 | Kind: ASTNodeImporter::IDK_Everything); |
10217 | } |
10218 | } |
10219 | |
10220 | if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(Val: To)) { |
10221 | if (!ToProto->getDefinition()) { |
10222 | return Importer.ImportDefinition( |
10223 | From: cast<ObjCProtocolDecl>(Val: FromDC), To: ToProto, |
10224 | Kind: ASTNodeImporter::IDK_Everything); |
10225 | } |
10226 | } |
10227 | |
10228 | return Importer.ImportDeclContext(FromDC, ForceImport: true); |
10229 | } |
10230 | |
10231 | Expected<DeclarationName> ASTImporter::Import(DeclarationName FromName) { |
10232 | if (!FromName) |
10233 | return DeclarationName{}; |
10234 | |
10235 | switch (FromName.getNameKind()) { |
10236 | case DeclarationName::Identifier: |
10237 | return DeclarationName(Import(FromId: FromName.getAsIdentifierInfo())); |
10238 | |
10239 | case DeclarationName::ObjCZeroArgSelector: |
10240 | case DeclarationName::ObjCOneArgSelector: |
10241 | case DeclarationName::ObjCMultiArgSelector: |
10242 | if (auto ToSelOrErr = Import(FromSel: FromName.getObjCSelector())) |
10243 | return DeclarationName(*ToSelOrErr); |
10244 | else |
10245 | return ToSelOrErr.takeError(); |
10246 | |
10247 | case DeclarationName::CXXConstructorName: { |
10248 | if (auto ToTyOrErr = Import(FromT: FromName.getCXXNameType())) |
10249 | return ToContext.DeclarationNames.getCXXConstructorName( |
10250 | Ty: ToContext.getCanonicalType(T: *ToTyOrErr)); |
10251 | else |
10252 | return ToTyOrErr.takeError(); |
10253 | } |
10254 | |
10255 | case DeclarationName::CXXDestructorName: { |
10256 | if (auto ToTyOrErr = Import(FromT: FromName.getCXXNameType())) |
10257 | return ToContext.DeclarationNames.getCXXDestructorName( |
10258 | Ty: ToContext.getCanonicalType(T: *ToTyOrErr)); |
10259 | else |
10260 | return ToTyOrErr.takeError(); |
10261 | } |
10262 | |
10263 | case DeclarationName::CXXDeductionGuideName: { |
10264 | if (auto ToTemplateOrErr = Import(FromD: FromName.getCXXDeductionGuideTemplate())) |
10265 | return ToContext.DeclarationNames.getCXXDeductionGuideName( |
10266 | TD: cast<TemplateDecl>(Val: *ToTemplateOrErr)); |
10267 | else |
10268 | return ToTemplateOrErr.takeError(); |
10269 | } |
10270 | |
10271 | case DeclarationName::CXXConversionFunctionName: { |
10272 | if (auto ToTyOrErr = Import(FromT: FromName.getCXXNameType())) |
10273 | return ToContext.DeclarationNames.getCXXConversionFunctionName( |
10274 | Ty: ToContext.getCanonicalType(T: *ToTyOrErr)); |
10275 | else |
10276 | return ToTyOrErr.takeError(); |
10277 | } |
10278 | |
10279 | case DeclarationName::CXXOperatorName: |
10280 | return ToContext.DeclarationNames.getCXXOperatorName( |
10281 | Op: FromName.getCXXOverloadedOperator()); |
10282 | |
10283 | case DeclarationName::CXXLiteralOperatorName: |
10284 | return ToContext.DeclarationNames.getCXXLiteralOperatorName( |
10285 | II: Import(FromId: FromName.getCXXLiteralIdentifier())); |
10286 | |
10287 | case DeclarationName::CXXUsingDirective: |
10288 | // FIXME: STATICS! |
10289 | return DeclarationName::getUsingDirectiveName(); |
10290 | } |
10291 | |
10292 | llvm_unreachable("Invalid DeclarationName Kind!" ); |
10293 | } |
10294 | |
10295 | IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) { |
10296 | if (!FromId) |
10297 | return nullptr; |
10298 | |
10299 | IdentifierInfo *ToId = &ToContext.Idents.get(Name: FromId->getName()); |
10300 | |
10301 | if (!ToId->getBuiltinID() && FromId->getBuiltinID()) |
10302 | ToId->setBuiltinID(FromId->getBuiltinID()); |
10303 | |
10304 | return ToId; |
10305 | } |
10306 | |
10307 | IdentifierOrOverloadedOperator |
10308 | ASTImporter::Import(IdentifierOrOverloadedOperator FromIO) { |
10309 | if (const IdentifierInfo *FromII = FromIO.getIdentifier()) |
10310 | return Import(FromId: FromII); |
10311 | return FromIO.getOperator(); |
10312 | } |
10313 | |
10314 | Expected<Selector> ASTImporter::Import(Selector FromSel) { |
10315 | if (FromSel.isNull()) |
10316 | return Selector{}; |
10317 | |
10318 | SmallVector<const IdentifierInfo *, 4> Idents; |
10319 | Idents.push_back(Elt: Import(FromId: FromSel.getIdentifierInfoForSlot(argIndex: 0))); |
10320 | for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I) |
10321 | Idents.push_back(Elt: Import(FromId: FromSel.getIdentifierInfoForSlot(argIndex: I))); |
10322 | return ToContext.Selectors.getSelector(NumArgs: FromSel.getNumArgs(), IIV: Idents.data()); |
10323 | } |
10324 | |
10325 | llvm::Expected<APValue> |
10326 | ASTNodeImporter::ImportAPValue(const APValue &FromValue) { |
10327 | APValue Result; |
10328 | llvm::Error Err = llvm::Error::success(); |
10329 | auto ImportLoop = [&](const APValue *From, APValue *To, unsigned Size) { |
10330 | for (unsigned Idx = 0; Idx < Size; Idx++) { |
10331 | APValue Tmp = importChecked(Err, From: From[Idx]); |
10332 | To[Idx] = Tmp; |
10333 | } |
10334 | }; |
10335 | switch (FromValue.getKind()) { |
10336 | case APValue::None: |
10337 | case APValue::Indeterminate: |
10338 | case APValue::Int: |
10339 | case APValue::Float: |
10340 | case APValue::FixedPoint: |
10341 | case APValue::ComplexInt: |
10342 | case APValue::ComplexFloat: |
10343 | Result = FromValue; |
10344 | break; |
10345 | case APValue::Vector: { |
10346 | Result.MakeVector(); |
10347 | MutableArrayRef<APValue> Elts = |
10348 | Result.setVectorUninit(FromValue.getVectorLength()); |
10349 | ImportLoop(((const APValue::Vec *)(const char *)&FromValue.Data)->Elts, |
10350 | Elts.data(), FromValue.getVectorLength()); |
10351 | break; |
10352 | } |
10353 | case APValue::Array: |
10354 | Result.MakeArray(InitElts: FromValue.getArrayInitializedElts(), |
10355 | Size: FromValue.getArraySize()); |
10356 | ImportLoop(((const APValue::Arr *)(const char *)&FromValue.Data)->Elts, |
10357 | ((const APValue::Arr *)(const char *)&Result.Data)->Elts, |
10358 | FromValue.getArrayInitializedElts()); |
10359 | break; |
10360 | case APValue::Struct: |
10361 | Result.MakeStruct(B: FromValue.getStructNumBases(), |
10362 | M: FromValue.getStructNumFields()); |
10363 | ImportLoop( |
10364 | ((const APValue::StructData *)(const char *)&FromValue.Data)->Elts, |
10365 | ((const APValue::StructData *)(const char *)&Result.Data)->Elts, |
10366 | FromValue.getStructNumBases() + FromValue.getStructNumFields()); |
10367 | break; |
10368 | case APValue::Union: { |
10369 | Result.MakeUnion(); |
10370 | const Decl *ImpFDecl = importChecked(Err, From: FromValue.getUnionField()); |
10371 | APValue ImpValue = importChecked(Err, From: FromValue.getUnionValue()); |
10372 | if (Err) |
10373 | return std::move(Err); |
10374 | Result.setUnion(Field: cast<FieldDecl>(Val: ImpFDecl), Value: ImpValue); |
10375 | break; |
10376 | } |
10377 | case APValue::AddrLabelDiff: { |
10378 | Result.MakeAddrLabelDiff(); |
10379 | const Expr *ImpLHS = importChecked(Err, From: FromValue.getAddrLabelDiffLHS()); |
10380 | const Expr *ImpRHS = importChecked(Err, From: FromValue.getAddrLabelDiffRHS()); |
10381 | if (Err) |
10382 | return std::move(Err); |
10383 | Result.setAddrLabelDiff(LHSExpr: cast<AddrLabelExpr>(Val: ImpLHS), |
10384 | RHSExpr: cast<AddrLabelExpr>(Val: ImpRHS)); |
10385 | break; |
10386 | } |
10387 | case APValue::MemberPointer: { |
10388 | const Decl *ImpMemPtrDecl = |
10389 | importChecked(Err, From: FromValue.getMemberPointerDecl()); |
10390 | if (Err) |
10391 | return std::move(Err); |
10392 | MutableArrayRef<const CXXRecordDecl *> ToPath = |
10393 | Result.setMemberPointerUninit( |
10394 | Member: cast<const ValueDecl>(Val: ImpMemPtrDecl), |
10395 | IsDerivedMember: FromValue.isMemberPointerToDerivedMember(), |
10396 | Size: FromValue.getMemberPointerPath().size()); |
10397 | ArrayRef<const CXXRecordDecl *> FromPath = Result.getMemberPointerPath(); |
10398 | for (unsigned Idx = 0; Idx < FromValue.getMemberPointerPath().size(); |
10399 | Idx++) { |
10400 | const Decl *ImpDecl = importChecked(Err, From: FromPath[Idx]); |
10401 | if (Err) |
10402 | return std::move(Err); |
10403 | ToPath[Idx] = cast<const CXXRecordDecl>(Val: ImpDecl->getCanonicalDecl()); |
10404 | } |
10405 | break; |
10406 | } |
10407 | case APValue::LValue: |
10408 | APValue::LValueBase Base; |
10409 | QualType FromElemTy; |
10410 | if (FromValue.getLValueBase()) { |
10411 | assert(!FromValue.getLValueBase().is<DynamicAllocLValue>() && |
10412 | "in C++20 dynamic allocation are transient so they shouldn't " |
10413 | "appear in the AST" ); |
10414 | if (!FromValue.getLValueBase().is<TypeInfoLValue>()) { |
10415 | if (const auto *E = |
10416 | FromValue.getLValueBase().dyn_cast<const Expr *>()) { |
10417 | FromElemTy = E->getType(); |
10418 | const Expr *ImpExpr = importChecked(Err, From: E); |
10419 | if (Err) |
10420 | return std::move(Err); |
10421 | Base = APValue::LValueBase(ImpExpr, |
10422 | FromValue.getLValueBase().getCallIndex(), |
10423 | FromValue.getLValueBase().getVersion()); |
10424 | } else { |
10425 | FromElemTy = |
10426 | FromValue.getLValueBase().get<const ValueDecl *>()->getType(); |
10427 | const Decl *ImpDecl = importChecked( |
10428 | Err, From: FromValue.getLValueBase().get<const ValueDecl *>()); |
10429 | if (Err) |
10430 | return std::move(Err); |
10431 | Base = APValue::LValueBase(cast<ValueDecl>(Val: ImpDecl), |
10432 | FromValue.getLValueBase().getCallIndex(), |
10433 | FromValue.getLValueBase().getVersion()); |
10434 | } |
10435 | } else { |
10436 | FromElemTy = FromValue.getLValueBase().getTypeInfoType(); |
10437 | const Type *ImpTypeInfo = importChecked( |
10438 | Err, From: FromValue.getLValueBase().get<TypeInfoLValue>().getType()); |
10439 | QualType ImpType = |
10440 | importChecked(Err, From: FromValue.getLValueBase().getTypeInfoType()); |
10441 | if (Err) |
10442 | return std::move(Err); |
10443 | Base = APValue::LValueBase::getTypeInfo(LV: TypeInfoLValue(ImpTypeInfo), |
10444 | TypeInfo: ImpType); |
10445 | } |
10446 | } |
10447 | CharUnits Offset = FromValue.getLValueOffset(); |
10448 | unsigned PathLength = FromValue.getLValuePath().size(); |
10449 | Result.MakeLValue(); |
10450 | if (FromValue.hasLValuePath()) { |
10451 | MutableArrayRef<APValue::LValuePathEntry> ToPath = Result.setLValueUninit( |
10452 | B: Base, O: Offset, Size: PathLength, OnePastTheEnd: FromValue.isLValueOnePastTheEnd(), |
10453 | IsNullPtr: FromValue.isNullPointer()); |
10454 | ArrayRef<APValue::LValuePathEntry> FromPath = FromValue.getLValuePath(); |
10455 | for (unsigned LoopIdx = 0; LoopIdx < PathLength; LoopIdx++) { |
10456 | if (FromElemTy->isRecordType()) { |
10457 | const Decl *FromDecl = |
10458 | FromPath[LoopIdx].getAsBaseOrMember().getPointer(); |
10459 | const Decl *ImpDecl = importChecked(Err, From: FromDecl); |
10460 | if (Err) |
10461 | return std::move(Err); |
10462 | if (auto *RD = dyn_cast<CXXRecordDecl>(Val: FromDecl)) |
10463 | FromElemTy = Importer.FromContext.getRecordType(Decl: RD); |
10464 | else |
10465 | FromElemTy = cast<ValueDecl>(Val: FromDecl)->getType(); |
10466 | ToPath[LoopIdx] = APValue::LValuePathEntry(APValue::BaseOrMemberType( |
10467 | ImpDecl, FromPath[LoopIdx].getAsBaseOrMember().getInt())); |
10468 | } else { |
10469 | FromElemTy = |
10470 | Importer.FromContext.getAsArrayType(T: FromElemTy)->getElementType(); |
10471 | ToPath[LoopIdx] = APValue::LValuePathEntry::ArrayIndex( |
10472 | Index: FromPath[LoopIdx].getAsArrayIndex()); |
10473 | } |
10474 | } |
10475 | } else |
10476 | Result.setLValue(B: Base, O: Offset, APValue::NoLValuePath{}, |
10477 | IsNullPtr: FromValue.isNullPointer()); |
10478 | } |
10479 | if (Err) |
10480 | return std::move(Err); |
10481 | return Result; |
10482 | } |
10483 | |
10484 | Expected<DeclarationName> ASTImporter::HandleNameConflict(DeclarationName Name, |
10485 | DeclContext *DC, |
10486 | unsigned IDNS, |
10487 | NamedDecl **Decls, |
10488 | unsigned NumDecls) { |
10489 | if (ODRHandling == ODRHandlingType::Conservative) |
10490 | // Report error at any name conflict. |
10491 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
10492 | else |
10493 | // Allow to create the new Decl with the same name. |
10494 | return Name; |
10495 | } |
10496 | |
10497 | DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) { |
10498 | if (LastDiagFromFrom) |
10499 | ToContext.getDiagnostics().notePriorDiagnosticFrom( |
10500 | Other: FromContext.getDiagnostics()); |
10501 | LastDiagFromFrom = false; |
10502 | return ToContext.getDiagnostics().Report(Loc, DiagID); |
10503 | } |
10504 | |
10505 | DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) { |
10506 | if (!LastDiagFromFrom) |
10507 | FromContext.getDiagnostics().notePriorDiagnosticFrom( |
10508 | Other: ToContext.getDiagnostics()); |
10509 | LastDiagFromFrom = true; |
10510 | return FromContext.getDiagnostics().Report(Loc, DiagID); |
10511 | } |
10512 | |
10513 | void ASTImporter::CompleteDecl (Decl *D) { |
10514 | if (auto *ID = dyn_cast<ObjCInterfaceDecl>(Val: D)) { |
10515 | if (!ID->getDefinition()) |
10516 | ID->startDefinition(); |
10517 | } |
10518 | else if (auto *PD = dyn_cast<ObjCProtocolDecl>(Val: D)) { |
10519 | if (!PD->getDefinition()) |
10520 | PD->startDefinition(); |
10521 | } |
10522 | else if (auto *TD = dyn_cast<TagDecl>(Val: D)) { |
10523 | if (!TD->getDefinition() && !TD->isBeingDefined()) { |
10524 | TD->startDefinition(); |
10525 | TD->setCompleteDefinition(true); |
10526 | } |
10527 | } |
10528 | else { |
10529 | assert(0 && "CompleteDecl called on a Decl that can't be completed" ); |
10530 | } |
10531 | } |
10532 | |
10533 | Decl *ASTImporter::MapImported(Decl *From, Decl *To) { |
10534 | auto [Pos, Inserted] = ImportedDecls.try_emplace(Key: From, Args&: To); |
10535 | assert((Inserted || Pos->second == To) && |
10536 | "Try to import an already imported Decl" ); |
10537 | if (!Inserted) |
10538 | return Pos->second; |
10539 | // This mapping should be maintained only in this function. Therefore do not |
10540 | // check for additional consistency. |
10541 | ImportedFromDecls[To] = From; |
10542 | // In the case of TypedefNameDecl we create the Decl first and only then we |
10543 | // import and set its DeclContext. So, the DC is still not set when we reach |
10544 | // here from GetImportedOrCreateDecl. |
10545 | if (To->getDeclContext()) |
10546 | AddToLookupTable(ToD: To); |
10547 | return To; |
10548 | } |
10549 | |
10550 | std::optional<ASTImportError> |
10551 | ASTImporter::getImportDeclErrorIfAny(Decl *FromD) const { |
10552 | auto Pos = ImportDeclErrors.find(Val: FromD); |
10553 | if (Pos != ImportDeclErrors.end()) |
10554 | return Pos->second; |
10555 | else |
10556 | return std::nullopt; |
10557 | } |
10558 | |
10559 | void ASTImporter::setImportDeclError(Decl *From, ASTImportError Error) { |
10560 | auto InsertRes = ImportDeclErrors.insert(KV: {From, Error}); |
10561 | (void)InsertRes; |
10562 | // Either we set the error for the first time, or we already had set one and |
10563 | // now we want to set the same error. |
10564 | assert(InsertRes.second || InsertRes.first->second.Error == Error.Error); |
10565 | } |
10566 | |
10567 | bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To, |
10568 | bool Complain) { |
10569 | llvm::DenseMap<const Type *, const Type *>::iterator Pos = |
10570 | ImportedTypes.find(Val: From.getTypePtr()); |
10571 | if (Pos != ImportedTypes.end()) { |
10572 | if (ExpectedType ToFromOrErr = Import(FromT: From)) { |
10573 | if (ToContext.hasSameType(T1: *ToFromOrErr, T2: To)) |
10574 | return true; |
10575 | } else { |
10576 | llvm::consumeError(Err: ToFromOrErr.takeError()); |
10577 | } |
10578 | } |
10579 | |
10580 | StructuralEquivalenceContext Ctx( |
10581 | getToContext().getLangOpts(), FromContext, ToContext, NonEquivalentDecls, |
10582 | getStructuralEquivalenceKind(Importer: *this), false, Complain); |
10583 | return Ctx.IsEquivalent(T1: From, T2: To); |
10584 | } |
10585 | |