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/ASTStructuralEquivalence.h" |
19 | #include "clang/AST/Attr.h" |
20 | #include "clang/AST/Decl.h" |
21 | #include "clang/AST/DeclAccessPair.h" |
22 | #include "clang/AST/DeclBase.h" |
23 | #include "clang/AST/DeclCXX.h" |
24 | #include "clang/AST/DeclFriend.h" |
25 | #include "clang/AST/DeclGroup.h" |
26 | #include "clang/AST/DeclObjC.h" |
27 | #include "clang/AST/DeclTemplate.h" |
28 | #include "clang/AST/DeclVisitor.h" |
29 | #include "clang/AST/DeclarationName.h" |
30 | #include "clang/AST/Expr.h" |
31 | #include "clang/AST/ExprCXX.h" |
32 | #include "clang/AST/ExprObjC.h" |
33 | #include "clang/AST/ExternalASTSource.h" |
34 | #include "clang/AST/LambdaCapture.h" |
35 | #include "clang/AST/NestedNameSpecifier.h" |
36 | #include "clang/AST/OperationKinds.h" |
37 | #include "clang/AST/Stmt.h" |
38 | #include "clang/AST/StmtCXX.h" |
39 | #include "clang/AST/StmtObjC.h" |
40 | #include "clang/AST/StmtVisitor.h" |
41 | #include "clang/AST/TemplateBase.h" |
42 | #include "clang/AST/TemplateName.h" |
43 | #include "clang/AST/Type.h" |
44 | #include "clang/AST/TypeLoc.h" |
45 | #include "clang/AST/TypeVisitor.h" |
46 | #include "clang/AST/UnresolvedSet.h" |
47 | #include "clang/Basic/Builtins.h" |
48 | #include "clang/Basic/ExceptionSpecificationType.h" |
49 | #include "clang/Basic/FileManager.h" |
50 | #include "clang/Basic/IdentifierTable.h" |
51 | #include "clang/Basic/LLVM.h" |
52 | #include "clang/Basic/LangOptions.h" |
53 | #include "clang/Basic/SourceLocation.h" |
54 | #include "clang/Basic/SourceManager.h" |
55 | #include "clang/Basic/Specifiers.h" |
56 | #include "llvm/ADT/APSInt.h" |
57 | #include "llvm/ADT/ArrayRef.h" |
58 | #include "llvm/ADT/DenseMap.h" |
59 | #include "llvm/ADT/STLExtras.h" |
60 | #include "llvm/ADT/ScopeExit.h" |
61 | #include "llvm/ADT/SmallVector.h" |
62 | #include "llvm/Support/Casting.h" |
63 | #include "llvm/Support/ErrorHandling.h" |
64 | #include "llvm/Support/MemoryBuffer.h" |
65 | #include <algorithm> |
66 | #include <cassert> |
67 | #include <cstddef> |
68 | #include <memory> |
69 | #include <optional> |
70 | #include <type_traits> |
71 | #include <utility> |
72 | |
73 | namespace clang { |
74 | |
75 | using llvm::make_error; |
76 | using llvm::Error; |
77 | using llvm::Expected; |
78 | using ExpectedTypePtr = llvm::Expected<const Type *>; |
79 | using ExpectedType = llvm::Expected<QualType>; |
80 | using ExpectedStmt = llvm::Expected<Stmt *>; |
81 | using ExpectedExpr = llvm::Expected<Expr *>; |
82 | using ExpectedDecl = llvm::Expected<Decl *>; |
83 | using ExpectedSLoc = llvm::Expected<SourceLocation>; |
84 | using ExpectedName = llvm::Expected<DeclarationName>; |
85 | |
86 | std::string ASTImportError::toString() const { |
87 | // FIXME: Improve error texts. |
88 | switch (Error) { |
89 | case NameConflict: |
90 | return "NameConflict" ; |
91 | case UnsupportedConstruct: |
92 | return "UnsupportedConstruct" ; |
93 | case Unknown: |
94 | return "Unknown error" ; |
95 | } |
96 | llvm_unreachable("Invalid error code." ); |
97 | return "Invalid error code." ; |
98 | } |
99 | |
100 | void ASTImportError::log(raw_ostream &OS) const { OS << toString(); } |
101 | |
102 | std::error_code ASTImportError::convertToErrorCode() const { |
103 | llvm_unreachable("Function not implemented." ); |
104 | } |
105 | |
106 | char ASTImportError::ID; |
107 | |
108 | template <class T> |
109 | SmallVector<Decl *, 2> |
110 | getCanonicalForwardRedeclChain(Redeclarable<T>* D) { |
111 | SmallVector<Decl *, 2> Redecls; |
112 | for (auto *R : D->getFirstDecl()->redecls()) { |
113 | if (R != D->getFirstDecl()) |
114 | Redecls.push_back(Elt: R); |
115 | } |
116 | Redecls.push_back(Elt: D->getFirstDecl()); |
117 | std::reverse(first: Redecls.begin(), last: Redecls.end()); |
118 | return Redecls; |
119 | } |
120 | |
121 | SmallVector<Decl*, 2> getCanonicalForwardRedeclChain(Decl* D) { |
122 | if (auto *FD = dyn_cast<FunctionDecl>(Val: D)) |
123 | return getCanonicalForwardRedeclChain<FunctionDecl>(FD); |
124 | if (auto *VD = dyn_cast<VarDecl>(Val: D)) |
125 | return getCanonicalForwardRedeclChain<VarDecl>(VD); |
126 | if (auto *TD = dyn_cast<TagDecl>(Val: D)) |
127 | return getCanonicalForwardRedeclChain<TagDecl>(TD); |
128 | llvm_unreachable("Bad declaration kind" ); |
129 | } |
130 | |
131 | void updateFlags(const Decl *From, Decl *To) { |
132 | // Check if some flags or attrs are new in 'From' and copy into 'To'. |
133 | // FIXME: Other flags or attrs? |
134 | if (From->isUsed(CheckUsedAttr: false) && !To->isUsed(CheckUsedAttr: false)) |
135 | To->setIsUsed(); |
136 | } |
137 | |
138 | /// How to handle import errors that occur when import of a child declaration |
139 | /// of a DeclContext fails. |
140 | class ChildErrorHandlingStrategy { |
141 | /// This context is imported (in the 'from' domain). |
142 | /// It is nullptr if a non-DeclContext is imported. |
143 | const DeclContext *const FromDC; |
144 | /// Ignore import errors of the children. |
145 | /// If true, the context can be imported successfully if a child |
146 | /// of it failed to import. Otherwise the import errors of the child nodes |
147 | /// are accumulated (joined) into the import error object of the parent. |
148 | /// (Import of a parent can fail in other ways.) |
149 | bool const IgnoreChildErrors; |
150 | |
151 | public: |
152 | ChildErrorHandlingStrategy(const DeclContext *FromDC) |
153 | : FromDC(FromDC), IgnoreChildErrors(!isa<TagDecl>(Val: FromDC)) {} |
154 | ChildErrorHandlingStrategy(const Decl *FromD) |
155 | : FromDC(dyn_cast<DeclContext>(Val: FromD)), |
156 | IgnoreChildErrors(!isa<TagDecl>(Val: FromD)) {} |
157 | |
158 | /// Process the import result of a child (of the current declaration). |
159 | /// \param ResultErr The import error that can be used as result of |
160 | /// importing the parent. This may be changed by the function. |
161 | /// \param ChildErr Result of importing a child. Can be success or error. |
162 | void handleChildImportResult(Error &ResultErr, Error &&ChildErr) { |
163 | if (ChildErr && !IgnoreChildErrors) |
164 | ResultErr = joinErrors(E1: std::move(ResultErr), E2: std::move(ChildErr)); |
165 | else |
166 | consumeError(Err: std::move(ChildErr)); |
167 | } |
168 | |
169 | /// Determine if import failure of a child does not cause import failure of |
170 | /// its parent. |
171 | bool ignoreChildErrorOnParent(Decl *FromChildD) const { |
172 | if (!IgnoreChildErrors || !FromDC) |
173 | return false; |
174 | return FromDC->containsDecl(D: FromChildD); |
175 | } |
176 | }; |
177 | |
178 | class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, ExpectedType>, |
179 | public DeclVisitor<ASTNodeImporter, ExpectedDecl>, |
180 | public StmtVisitor<ASTNodeImporter, ExpectedStmt> { |
181 | ASTImporter &Importer; |
182 | |
183 | // Use this instead of Importer.importInto . |
184 | template <typename ImportT> |
185 | [[nodiscard]] Error importInto(ImportT &To, const ImportT &From) { |
186 | return Importer.importInto(To, From); |
187 | } |
188 | |
189 | // Use this to import pointers of specific type. |
190 | template <typename ImportT> |
191 | [[nodiscard]] Error importInto(ImportT *&To, ImportT *From) { |
192 | auto ToOrErr = Importer.Import(From); |
193 | if (ToOrErr) |
194 | To = cast_or_null<ImportT>(*ToOrErr); |
195 | return ToOrErr.takeError(); |
196 | } |
197 | |
198 | // Call the import function of ASTImporter for a baseclass of type `T` and |
199 | // cast the return value to `T`. |
200 | template <typename T> |
201 | auto import(T *From) |
202 | -> std::conditional_t<std::is_base_of_v<Type, T>, Expected<const T *>, |
203 | Expected<T *>> { |
204 | auto ToOrErr = Importer.Import(From); |
205 | if (!ToOrErr) |
206 | return ToOrErr.takeError(); |
207 | return cast_or_null<T>(*ToOrErr); |
208 | } |
209 | |
210 | template <typename T> |
211 | auto import(const T *From) { |
212 | return import(const_cast<T *>(From)); |
213 | } |
214 | |
215 | // Call the import function of ASTImporter for type `T`. |
216 | template <typename T> |
217 | Expected<T> import(const T &From) { |
218 | return Importer.Import(From); |
219 | } |
220 | |
221 | // Import an std::optional<T> by importing the contained T, if any. |
222 | template <typename T> |
223 | Expected<std::optional<T>> import(std::optional<T> From) { |
224 | if (!From) |
225 | return std::nullopt; |
226 | return import(*From); |
227 | } |
228 | |
229 | ExplicitSpecifier importExplicitSpecifier(Error &Err, |
230 | ExplicitSpecifier ESpec); |
231 | |
232 | // Wrapper for an overload set. |
233 | template <typename ToDeclT> struct CallOverloadedCreateFun { |
234 | template <typename... Args> decltype(auto) operator()(Args &&... args) { |
235 | return ToDeclT::Create(std::forward<Args>(args)...); |
236 | } |
237 | }; |
238 | |
239 | // Always use these functions to create a Decl during import. There are |
240 | // certain tasks which must be done after the Decl was created, e.g. we |
241 | // must immediately register that as an imported Decl. The parameter `ToD` |
242 | // will be set to the newly created Decl or if had been imported before |
243 | // then to the already imported Decl. Returns a bool value set to true if |
244 | // the `FromD` had been imported before. |
245 | template <typename ToDeclT, typename FromDeclT, typename... Args> |
246 | [[nodiscard]] bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD, |
247 | Args &&...args) { |
248 | // There may be several overloads of ToDeclT::Create. We must make sure |
249 | // to call the one which would be chosen by the arguments, thus we use a |
250 | // wrapper for the overload set. |
251 | CallOverloadedCreateFun<ToDeclT> OC; |
252 | return GetImportedOrCreateSpecialDecl(ToD, OC, FromD, |
253 | std::forward<Args>(args)...); |
254 | } |
255 | // Use this overload if a special Type is needed to be created. E.g if we |
256 | // want to create a `TypeAliasDecl` and assign that to a `TypedefNameDecl` |
257 | // then: |
258 | // TypedefNameDecl *ToTypedef; |
259 | // GetImportedOrCreateDecl<TypeAliasDecl>(ToTypedef, FromD, ...); |
260 | template <typename NewDeclT, typename ToDeclT, typename FromDeclT, |
261 | typename... Args> |
262 | [[nodiscard]] bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD, |
263 | Args &&...args) { |
264 | CallOverloadedCreateFun<NewDeclT> OC; |
265 | return GetImportedOrCreateSpecialDecl(ToD, OC, FromD, |
266 | std::forward<Args>(args)...); |
267 | } |
268 | // Use this version if a special create function must be |
269 | // used, e.g. CXXRecordDecl::CreateLambda . |
270 | template <typename ToDeclT, typename CreateFunT, typename FromDeclT, |
271 | typename... Args> |
272 | [[nodiscard]] bool |
273 | GetImportedOrCreateSpecialDecl(ToDeclT *&ToD, CreateFunT CreateFun, |
274 | FromDeclT *FromD, Args &&...args) { |
275 | if (Importer.getImportDeclErrorIfAny(FromD)) { |
276 | ToD = nullptr; |
277 | return true; // Already imported but with error. |
278 | } |
279 | ToD = cast_or_null<ToDeclT>(Importer.GetAlreadyImportedOrNull(FromD)); |
280 | if (ToD) |
281 | return true; // Already imported. |
282 | ToD = CreateFun(std::forward<Args>(args)...); |
283 | // Keep track of imported Decls. |
284 | Importer.RegisterImportedDecl(FromD, ToD); |
285 | Importer.SharedState->markAsNewDecl(ToD); |
286 | InitializeImportedDecl(FromD, ToD); |
287 | return false; // A new Decl is created. |
288 | } |
289 | |
290 | void InitializeImportedDecl(Decl *FromD, Decl *ToD) { |
291 | ToD->IdentifierNamespace = FromD->IdentifierNamespace; |
292 | if (FromD->isUsed()) |
293 | ToD->setIsUsed(); |
294 | if (FromD->isImplicit()) |
295 | ToD->setImplicit(); |
296 | } |
297 | |
298 | // Check if we have found an existing definition. Returns with that |
299 | // definition if yes, otherwise returns null. |
300 | Decl *FindAndMapDefinition(FunctionDecl *D, FunctionDecl *FoundFunction) { |
301 | const FunctionDecl *Definition = nullptr; |
302 | if (D->doesThisDeclarationHaveABody() && |
303 | FoundFunction->hasBody(Definition)) |
304 | return Importer.MapImported(D, const_cast<FunctionDecl *>(Definition)); |
305 | return nullptr; |
306 | } |
307 | |
308 | void addDeclToContexts(Decl *FromD, Decl *ToD) { |
309 | if (Importer.isMinimalImport()) { |
310 | // In minimal import case the decl must be added even if it is not |
311 | // contained in original context, for LLDB compatibility. |
312 | // FIXME: Check if a better solution is possible. |
313 | if (!FromD->getDescribedTemplate() && |
314 | FromD->getFriendObjectKind() == Decl::FOK_None) |
315 | ToD->getLexicalDeclContext()->addDeclInternal(D: ToD); |
316 | return; |
317 | } |
318 | |
319 | DeclContext *FromDC = FromD->getDeclContext(); |
320 | DeclContext *FromLexicalDC = FromD->getLexicalDeclContext(); |
321 | DeclContext *ToDC = ToD->getDeclContext(); |
322 | DeclContext *ToLexicalDC = ToD->getLexicalDeclContext(); |
323 | |
324 | bool Visible = false; |
325 | if (FromDC->containsDeclAndLoad(D: FromD)) { |
326 | ToDC->addDeclInternal(D: ToD); |
327 | Visible = true; |
328 | } |
329 | if (ToDC != ToLexicalDC && FromLexicalDC->containsDeclAndLoad(D: FromD)) { |
330 | ToLexicalDC->addDeclInternal(D: ToD); |
331 | Visible = true; |
332 | } |
333 | |
334 | // If the Decl was added to any context, it was made already visible. |
335 | // Otherwise it is still possible that it should be visible. |
336 | if (!Visible) { |
337 | if (auto *FromNamed = dyn_cast<NamedDecl>(FromD)) { |
338 | auto *ToNamed = cast<NamedDecl>(ToD); |
339 | DeclContextLookupResult FromLookup = |
340 | FromDC->lookup(Name: FromNamed->getDeclName()); |
341 | if (llvm::is_contained(FromLookup, FromNamed)) |
342 | ToDC->makeDeclVisibleInContext(D: ToNamed); |
343 | } |
344 | } |
345 | } |
346 | |
347 | void updateLookupTableForTemplateParameters(TemplateParameterList &Params, |
348 | DeclContext *OldDC) { |
349 | ASTImporterLookupTable *LT = Importer.SharedState->getLookupTable(); |
350 | if (!LT) |
351 | return; |
352 | |
353 | for (NamedDecl *TP : Params) |
354 | LT->update(ND: TP, OldDC); |
355 | } |
356 | |
357 | void updateLookupTableForTemplateParameters(TemplateParameterList &Params) { |
358 | updateLookupTableForTemplateParameters( |
359 | Params, Importer.getToContext().getTranslationUnitDecl()); |
360 | } |
361 | |
362 | public: |
363 | explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) {} |
364 | |
365 | using TypeVisitor<ASTNodeImporter, ExpectedType>::Visit; |
366 | using DeclVisitor<ASTNodeImporter, ExpectedDecl>::Visit; |
367 | using StmtVisitor<ASTNodeImporter, ExpectedStmt>::Visit; |
368 | |
369 | // Importing types |
370 | ExpectedType VisitType(const Type *T); |
371 | #define TYPE(Class, Base) \ |
372 | ExpectedType Visit##Class##Type(const Class##Type *T); |
373 | #include "clang/AST/TypeNodes.inc" |
374 | |
375 | // Importing declarations |
376 | Error ImportDeclParts(NamedDecl *D, DeclarationName &Name, NamedDecl *&ToD, |
377 | SourceLocation &Loc); |
378 | Error ImportDeclParts( |
379 | NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC, |
380 | DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc); |
381 | Error ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr); |
382 | Error ImportDeclarationNameLoc( |
383 | const DeclarationNameInfo &From, DeclarationNameInfo &To); |
384 | Error ImportDeclContext(DeclContext *FromDC, bool ForceImport = false); |
385 | Error ImportDeclContext( |
386 | Decl *From, DeclContext *&ToDC, DeclContext *&ToLexicalDC); |
387 | Error ImportImplicitMethods(const CXXRecordDecl *From, CXXRecordDecl *To); |
388 | |
389 | Error ImportFieldDeclDefinition(const FieldDecl *From, const FieldDecl *To); |
390 | Expected<CXXCastPath> ImportCastPath(CastExpr *E); |
391 | Expected<APValue> ImportAPValue(const APValue &FromValue); |
392 | |
393 | using Designator = DesignatedInitExpr::Designator; |
394 | |
395 | /// What we should import from the definition. |
396 | enum ImportDefinitionKind { |
397 | /// Import the default subset of the definition, which might be |
398 | /// nothing (if minimal import is set) or might be everything (if minimal |
399 | /// import is not set). |
400 | IDK_Default, |
401 | /// Import everything. |
402 | IDK_Everything, |
403 | /// Import only the bare bones needed to establish a valid |
404 | /// DeclContext. |
405 | IDK_Basic |
406 | }; |
407 | |
408 | bool shouldForceImportDeclContext(ImportDefinitionKind IDK) { |
409 | return IDK == IDK_Everything || |
410 | (IDK == IDK_Default && !Importer.isMinimalImport()); |
411 | } |
412 | |
413 | Error ImportInitializer(VarDecl *From, VarDecl *To); |
414 | Error ImportDefinition( |
415 | RecordDecl *From, RecordDecl *To, |
416 | ImportDefinitionKind Kind = IDK_Default); |
417 | Error ImportDefinition( |
418 | EnumDecl *From, EnumDecl *To, |
419 | ImportDefinitionKind Kind = IDK_Default); |
420 | Error ImportDefinition( |
421 | ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, |
422 | ImportDefinitionKind Kind = IDK_Default); |
423 | Error ImportDefinition( |
424 | ObjCProtocolDecl *From, ObjCProtocolDecl *To, |
425 | ImportDefinitionKind Kind = IDK_Default); |
426 | Error ImportTemplateArguments(ArrayRef<TemplateArgument> FromArgs, |
427 | SmallVectorImpl<TemplateArgument> &ToArgs); |
428 | Expected<TemplateArgument> |
429 | ImportTemplateArgument(const TemplateArgument &From); |
430 | |
431 | template <typename InContainerTy> |
432 | Error ImportTemplateArgumentListInfo( |
433 | const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo); |
434 | |
435 | template<typename InContainerTy> |
436 | Error ImportTemplateArgumentListInfo( |
437 | SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc, |
438 | const InContainerTy &Container, TemplateArgumentListInfo &Result); |
439 | |
440 | using TemplateArgsTy = SmallVector<TemplateArgument, 8>; |
441 | using FunctionTemplateAndArgsTy = |
442 | std::tuple<FunctionTemplateDecl *, TemplateArgsTy>; |
443 | Expected<FunctionTemplateAndArgsTy> |
444 | ImportFunctionTemplateWithTemplateArgsFromSpecialization( |
445 | FunctionDecl *FromFD); |
446 | Error ImportTemplateParameterLists(const DeclaratorDecl *FromD, |
447 | DeclaratorDecl *ToD); |
448 | |
449 | Error ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD); |
450 | |
451 | Error ImportFunctionDeclBody(FunctionDecl *FromFD, FunctionDecl *ToFD); |
452 | |
453 | Error ImportDefaultArgOfParmVarDecl(const ParmVarDecl *FromParam, |
454 | ParmVarDecl *ToParam); |
455 | |
456 | Expected<InheritedConstructor> |
457 | ImportInheritedConstructor(const InheritedConstructor &From); |
458 | |
459 | template <typename T> |
460 | bool hasSameVisibilityContextAndLinkage(T *Found, T *From); |
461 | |
462 | bool IsStructuralMatch(Decl *From, Decl *To, bool Complain = true, |
463 | bool IgnoreTemplateParmDepth = false); |
464 | ExpectedDecl VisitDecl(Decl *D); |
465 | ExpectedDecl VisitImportDecl(ImportDecl *D); |
466 | ExpectedDecl VisitEmptyDecl(EmptyDecl *D); |
467 | ExpectedDecl VisitAccessSpecDecl(AccessSpecDecl *D); |
468 | ExpectedDecl VisitStaticAssertDecl(StaticAssertDecl *D); |
469 | ExpectedDecl VisitTranslationUnitDecl(TranslationUnitDecl *D); |
470 | ExpectedDecl VisitBindingDecl(BindingDecl *D); |
471 | ExpectedDecl VisitNamespaceDecl(NamespaceDecl *D); |
472 | ExpectedDecl VisitNamespaceAliasDecl(NamespaceAliasDecl *D); |
473 | ExpectedDecl VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias); |
474 | ExpectedDecl VisitTypedefDecl(TypedefDecl *D); |
475 | ExpectedDecl VisitTypeAliasDecl(TypeAliasDecl *D); |
476 | ExpectedDecl VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D); |
477 | ExpectedDecl VisitLabelDecl(LabelDecl *D); |
478 | ExpectedDecl VisitEnumDecl(EnumDecl *D); |
479 | ExpectedDecl VisitRecordDecl(RecordDecl *D); |
480 | ExpectedDecl VisitEnumConstantDecl(EnumConstantDecl *D); |
481 | ExpectedDecl VisitFunctionDecl(FunctionDecl *D); |
482 | ExpectedDecl VisitCXXMethodDecl(CXXMethodDecl *D); |
483 | ExpectedDecl VisitCXXConstructorDecl(CXXConstructorDecl *D); |
484 | ExpectedDecl VisitCXXDestructorDecl(CXXDestructorDecl *D); |
485 | ExpectedDecl VisitCXXConversionDecl(CXXConversionDecl *D); |
486 | ExpectedDecl VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D); |
487 | ExpectedDecl VisitFieldDecl(FieldDecl *D); |
488 | ExpectedDecl VisitIndirectFieldDecl(IndirectFieldDecl *D); |
489 | ExpectedDecl VisitFriendDecl(FriendDecl *D); |
490 | ExpectedDecl VisitObjCIvarDecl(ObjCIvarDecl *D); |
491 | ExpectedDecl VisitVarDecl(VarDecl *D); |
492 | ExpectedDecl VisitImplicitParamDecl(ImplicitParamDecl *D); |
493 | ExpectedDecl VisitParmVarDecl(ParmVarDecl *D); |
494 | ExpectedDecl VisitObjCMethodDecl(ObjCMethodDecl *D); |
495 | ExpectedDecl VisitObjCTypeParamDecl(ObjCTypeParamDecl *D); |
496 | ExpectedDecl VisitObjCCategoryDecl(ObjCCategoryDecl *D); |
497 | ExpectedDecl VisitObjCProtocolDecl(ObjCProtocolDecl *D); |
498 | ExpectedDecl VisitLinkageSpecDecl(LinkageSpecDecl *D); |
499 | ExpectedDecl VisitUsingDecl(UsingDecl *D); |
500 | ExpectedDecl VisitUsingShadowDecl(UsingShadowDecl *D); |
501 | ExpectedDecl VisitUsingDirectiveDecl(UsingDirectiveDecl *D); |
502 | ExpectedDecl VisitUsingPackDecl(UsingPackDecl *D); |
503 | ExpectedDecl ImportUsingShadowDecls(BaseUsingDecl *D, BaseUsingDecl *ToSI); |
504 | ExpectedDecl VisitUsingEnumDecl(UsingEnumDecl *D); |
505 | ExpectedDecl VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); |
506 | ExpectedDecl VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); |
507 | ExpectedDecl VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D); |
508 | ExpectedDecl |
509 | VisitLifetimeExtendedTemporaryDecl(LifetimeExtendedTemporaryDecl *D); |
510 | |
511 | Expected<ObjCTypeParamList *> |
512 | ImportObjCTypeParamList(ObjCTypeParamList *list); |
513 | |
514 | ExpectedDecl VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
515 | ExpectedDecl VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
516 | ExpectedDecl VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
517 | ExpectedDecl VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
518 | ExpectedDecl VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); |
519 | ExpectedDecl VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); |
520 | ExpectedDecl VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); |
521 | ExpectedDecl VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); |
522 | ExpectedDecl VisitClassTemplateDecl(ClassTemplateDecl *D); |
523 | ExpectedDecl VisitClassTemplateSpecializationDecl( |
524 | ClassTemplateSpecializationDecl *D); |
525 | ExpectedDecl VisitVarTemplateDecl(VarTemplateDecl *D); |
526 | ExpectedDecl VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D); |
527 | ExpectedDecl VisitFunctionTemplateDecl(FunctionTemplateDecl *D); |
528 | |
529 | // Importing statements |
530 | ExpectedStmt VisitStmt(Stmt *S); |
531 | ExpectedStmt VisitGCCAsmStmt(GCCAsmStmt *S); |
532 | ExpectedStmt VisitDeclStmt(DeclStmt *S); |
533 | ExpectedStmt VisitNullStmt(NullStmt *S); |
534 | ExpectedStmt VisitCompoundStmt(CompoundStmt *S); |
535 | ExpectedStmt VisitCaseStmt(CaseStmt *S); |
536 | ExpectedStmt VisitDefaultStmt(DefaultStmt *S); |
537 | ExpectedStmt VisitLabelStmt(LabelStmt *S); |
538 | ExpectedStmt VisitAttributedStmt(AttributedStmt *S); |
539 | ExpectedStmt VisitIfStmt(IfStmt *S); |
540 | ExpectedStmt VisitSwitchStmt(SwitchStmt *S); |
541 | ExpectedStmt VisitWhileStmt(WhileStmt *S); |
542 | ExpectedStmt VisitDoStmt(DoStmt *S); |
543 | ExpectedStmt VisitForStmt(ForStmt *S); |
544 | ExpectedStmt VisitGotoStmt(GotoStmt *S); |
545 | ExpectedStmt VisitIndirectGotoStmt(IndirectGotoStmt *S); |
546 | ExpectedStmt VisitContinueStmt(ContinueStmt *S); |
547 | ExpectedStmt VisitBreakStmt(BreakStmt *S); |
548 | ExpectedStmt VisitReturnStmt(ReturnStmt *S); |
549 | // FIXME: MSAsmStmt |
550 | // FIXME: SEHExceptStmt |
551 | // FIXME: SEHFinallyStmt |
552 | // FIXME: SEHTryStmt |
553 | // FIXME: SEHLeaveStmt |
554 | // FIXME: CapturedStmt |
555 | ExpectedStmt VisitCXXCatchStmt(CXXCatchStmt *S); |
556 | ExpectedStmt VisitCXXTryStmt(CXXTryStmt *S); |
557 | ExpectedStmt VisitCXXForRangeStmt(CXXForRangeStmt *S); |
558 | // FIXME: MSDependentExistsStmt |
559 | ExpectedStmt VisitObjCForCollectionStmt(ObjCForCollectionStmt *S); |
560 | ExpectedStmt VisitObjCAtCatchStmt(ObjCAtCatchStmt *S); |
561 | ExpectedStmt VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S); |
562 | ExpectedStmt VisitObjCAtTryStmt(ObjCAtTryStmt *S); |
563 | ExpectedStmt VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
564 | ExpectedStmt VisitObjCAtThrowStmt(ObjCAtThrowStmt *S); |
565 | ExpectedStmt VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S); |
566 | |
567 | // Importing expressions |
568 | ExpectedStmt VisitExpr(Expr *E); |
569 | ExpectedStmt VisitSourceLocExpr(SourceLocExpr *E); |
570 | ExpectedStmt VisitVAArgExpr(VAArgExpr *E); |
571 | ExpectedStmt VisitChooseExpr(ChooseExpr *E); |
572 | ExpectedStmt VisitConvertVectorExpr(ConvertVectorExpr *E); |
573 | ExpectedStmt VisitShuffleVectorExpr(ShuffleVectorExpr *E); |
574 | ExpectedStmt VisitGNUNullExpr(GNUNullExpr *E); |
575 | ExpectedStmt VisitGenericSelectionExpr(GenericSelectionExpr *E); |
576 | ExpectedStmt VisitPredefinedExpr(PredefinedExpr *E); |
577 | ExpectedStmt VisitDeclRefExpr(DeclRefExpr *E); |
578 | ExpectedStmt VisitImplicitValueInitExpr(ImplicitValueInitExpr *E); |
579 | ExpectedStmt VisitDesignatedInitExpr(DesignatedInitExpr *E); |
580 | ExpectedStmt VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E); |
581 | ExpectedStmt VisitIntegerLiteral(IntegerLiteral *E); |
582 | ExpectedStmt VisitFloatingLiteral(FloatingLiteral *E); |
583 | ExpectedStmt VisitImaginaryLiteral(ImaginaryLiteral *E); |
584 | ExpectedStmt VisitFixedPointLiteral(FixedPointLiteral *E); |
585 | ExpectedStmt VisitCharacterLiteral(CharacterLiteral *E); |
586 | ExpectedStmt VisitStringLiteral(StringLiteral *E); |
587 | ExpectedStmt VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
588 | ExpectedStmt VisitAtomicExpr(AtomicExpr *E); |
589 | ExpectedStmt VisitAddrLabelExpr(AddrLabelExpr *E); |
590 | ExpectedStmt VisitConstantExpr(ConstantExpr *E); |
591 | ExpectedStmt VisitParenExpr(ParenExpr *E); |
592 | ExpectedStmt VisitParenListExpr(ParenListExpr *E); |
593 | ExpectedStmt VisitStmtExpr(StmtExpr *E); |
594 | ExpectedStmt VisitUnaryOperator(UnaryOperator *E); |
595 | ExpectedStmt VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); |
596 | ExpectedStmt VisitBinaryOperator(BinaryOperator *E); |
597 | ExpectedStmt VisitConditionalOperator(ConditionalOperator *E); |
598 | ExpectedStmt VisitBinaryConditionalOperator(BinaryConditionalOperator *E); |
599 | ExpectedStmt VisitCXXRewrittenBinaryOperator(CXXRewrittenBinaryOperator *E); |
600 | ExpectedStmt VisitOpaqueValueExpr(OpaqueValueExpr *E); |
601 | ExpectedStmt VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E); |
602 | ExpectedStmt VisitExpressionTraitExpr(ExpressionTraitExpr *E); |
603 | ExpectedStmt VisitArraySubscriptExpr(ArraySubscriptExpr *E); |
604 | ExpectedStmt VisitCompoundAssignOperator(CompoundAssignOperator *E); |
605 | ExpectedStmt VisitImplicitCastExpr(ImplicitCastExpr *E); |
606 | ExpectedStmt VisitExplicitCastExpr(ExplicitCastExpr *E); |
607 | ExpectedStmt VisitOffsetOfExpr(OffsetOfExpr *OE); |
608 | ExpectedStmt VisitCXXThrowExpr(CXXThrowExpr *E); |
609 | ExpectedStmt VisitCXXNoexceptExpr(CXXNoexceptExpr *E); |
610 | ExpectedStmt VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E); |
611 | ExpectedStmt VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); |
612 | ExpectedStmt VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E); |
613 | ExpectedStmt VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E); |
614 | ExpectedStmt VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E); |
615 | ExpectedStmt VisitPackExpansionExpr(PackExpansionExpr *E); |
616 | ExpectedStmt VisitSizeOfPackExpr(SizeOfPackExpr *E); |
617 | ExpectedStmt VisitCXXNewExpr(CXXNewExpr *E); |
618 | ExpectedStmt VisitCXXDeleteExpr(CXXDeleteExpr *E); |
619 | ExpectedStmt VisitCXXConstructExpr(CXXConstructExpr *E); |
620 | ExpectedStmt VisitCXXMemberCallExpr(CXXMemberCallExpr *E); |
621 | ExpectedStmt VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E); |
622 | ExpectedStmt VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E); |
623 | ExpectedStmt VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E); |
624 | ExpectedStmt VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E); |
625 | ExpectedStmt VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E); |
626 | ExpectedStmt VisitExprWithCleanups(ExprWithCleanups *E); |
627 | ExpectedStmt VisitCXXThisExpr(CXXThisExpr *E); |
628 | ExpectedStmt VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E); |
629 | ExpectedStmt VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E); |
630 | ExpectedStmt VisitMemberExpr(MemberExpr *E); |
631 | ExpectedStmt VisitCallExpr(CallExpr *E); |
632 | ExpectedStmt VisitLambdaExpr(LambdaExpr *LE); |
633 | ExpectedStmt VisitInitListExpr(InitListExpr *E); |
634 | ExpectedStmt VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E); |
635 | ExpectedStmt VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E); |
636 | ExpectedStmt VisitArrayInitLoopExpr(ArrayInitLoopExpr *E); |
637 | ExpectedStmt VisitArrayInitIndexExpr(ArrayInitIndexExpr *E); |
638 | ExpectedStmt VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E); |
639 | ExpectedStmt VisitCXXNamedCastExpr(CXXNamedCastExpr *E); |
640 | ExpectedStmt VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E); |
641 | ExpectedStmt VisitTypeTraitExpr(TypeTraitExpr *E); |
642 | ExpectedStmt VisitCXXTypeidExpr(CXXTypeidExpr *E); |
643 | ExpectedStmt VisitCXXFoldExpr(CXXFoldExpr *E); |
644 | |
645 | // Helper for chaining together multiple imports. If an error is detected, |
646 | // subsequent imports will return default constructed nodes, so that failure |
647 | // can be detected with a single conditional branch after a sequence of |
648 | // imports. |
649 | template <typename T> T importChecked(Error &Err, const T &From) { |
650 | // Don't attempt to import nodes if we hit an error earlier. |
651 | if (Err) |
652 | return T{}; |
653 | Expected<T> MaybeVal = import(From); |
654 | if (!MaybeVal) { |
655 | Err = MaybeVal.takeError(); |
656 | return T{}; |
657 | } |
658 | return *MaybeVal; |
659 | } |
660 | |
661 | template<typename IIter, typename OIter> |
662 | Error ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) { |
663 | using ItemT = std::remove_reference_t<decltype(*Obegin)>; |
664 | for (; Ibegin != Iend; ++Ibegin, ++Obegin) { |
665 | Expected<ItemT> ToOrErr = import(*Ibegin); |
666 | if (!ToOrErr) |
667 | return ToOrErr.takeError(); |
668 | *Obegin = *ToOrErr; |
669 | } |
670 | return Error::success(); |
671 | } |
672 | |
673 | // Import every item from a container structure into an output container. |
674 | // If error occurs, stops at first error and returns the error. |
675 | // The output container should have space for all needed elements (it is not |
676 | // expanded, new items are put into from the beginning). |
677 | template<typename InContainerTy, typename OutContainerTy> |
678 | Error ImportContainerChecked( |
679 | const InContainerTy &InContainer, OutContainerTy &OutContainer) { |
680 | return ImportArrayChecked( |
681 | InContainer.begin(), InContainer.end(), OutContainer.begin()); |
682 | } |
683 | |
684 | template<typename InContainerTy, typename OIter> |
685 | Error ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) { |
686 | return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin); |
687 | } |
688 | |
689 | Error ImportOverriddenMethods(CXXMethodDecl *ToMethod, |
690 | CXXMethodDecl *FromMethod); |
691 | |
692 | Expected<FunctionDecl *> FindFunctionTemplateSpecialization( |
693 | FunctionDecl *FromFD); |
694 | |
695 | // Returns true if the given function has a placeholder return type and |
696 | // that type is declared inside the body of the function. |
697 | // E.g. auto f() { struct X{}; return X(); } |
698 | bool hasAutoReturnTypeDeclaredInside(FunctionDecl *D); |
699 | }; |
700 | |
701 | template <typename InContainerTy> |
702 | Error ASTNodeImporter::ImportTemplateArgumentListInfo( |
703 | SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc, |
704 | const InContainerTy &Container, TemplateArgumentListInfo &Result) { |
705 | auto ToLAngleLocOrErr = import(FromLAngleLoc); |
706 | if (!ToLAngleLocOrErr) |
707 | return ToLAngleLocOrErr.takeError(); |
708 | auto ToRAngleLocOrErr = import(FromRAngleLoc); |
709 | if (!ToRAngleLocOrErr) |
710 | return ToRAngleLocOrErr.takeError(); |
711 | |
712 | TemplateArgumentListInfo ToTAInfo(*ToLAngleLocOrErr, *ToRAngleLocOrErr); |
713 | if (auto Err = ImportTemplateArgumentListInfo(Container, ToTAInfo)) |
714 | return Err; |
715 | Result = ToTAInfo; |
716 | return Error::success(); |
717 | } |
718 | |
719 | template <> |
720 | Error ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>( |
721 | const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) { |
722 | return ImportTemplateArgumentListInfo( |
723 | From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result); |
724 | } |
725 | |
726 | template <> |
727 | Error ASTNodeImporter::ImportTemplateArgumentListInfo< |
728 | ASTTemplateArgumentListInfo>( |
729 | const ASTTemplateArgumentListInfo &From, |
730 | TemplateArgumentListInfo &Result) { |
731 | return ImportTemplateArgumentListInfo( |
732 | From.LAngleLoc, From.RAngleLoc, From.arguments(), Result); |
733 | } |
734 | |
735 | Expected<ASTNodeImporter::FunctionTemplateAndArgsTy> |
736 | ASTNodeImporter::ImportFunctionTemplateWithTemplateArgsFromSpecialization( |
737 | FunctionDecl *FromFD) { |
738 | assert(FromFD->getTemplatedKind() == |
739 | FunctionDecl::TK_FunctionTemplateSpecialization); |
740 | |
741 | FunctionTemplateAndArgsTy Result; |
742 | |
743 | auto *FTSInfo = FromFD->getTemplateSpecializationInfo(); |
744 | if (Error Err = importInto(std::get<0>(Result), FTSInfo->getTemplate())) |
745 | return std::move(Err); |
746 | |
747 | // Import template arguments. |
748 | if (Error Err = ImportTemplateArguments(FromArgs: FTSInfo->TemplateArguments->asArray(), |
749 | ToArgs&: std::get<1>(Result))) |
750 | return std::move(Err); |
751 | |
752 | return Result; |
753 | } |
754 | |
755 | template <> |
756 | Expected<TemplateParameterList *> |
757 | ASTNodeImporter::import(TemplateParameterList *From) { |
758 | SmallVector<NamedDecl *, 4> To(From->size()); |
759 | if (Error Err = ImportContainerChecked(*From, To)) |
760 | return std::move(Err); |
761 | |
762 | ExpectedExpr ToRequiresClause = import(From->getRequiresClause()); |
763 | if (!ToRequiresClause) |
764 | return ToRequiresClause.takeError(); |
765 | |
766 | auto ToTemplateLocOrErr = import(From->getTemplateLoc()); |
767 | if (!ToTemplateLocOrErr) |
768 | return ToTemplateLocOrErr.takeError(); |
769 | auto ToLAngleLocOrErr = import(From->getLAngleLoc()); |
770 | if (!ToLAngleLocOrErr) |
771 | return ToLAngleLocOrErr.takeError(); |
772 | auto ToRAngleLocOrErr = import(From->getRAngleLoc()); |
773 | if (!ToRAngleLocOrErr) |
774 | return ToRAngleLocOrErr.takeError(); |
775 | |
776 | return TemplateParameterList::Create( |
777 | C: Importer.getToContext(), |
778 | TemplateLoc: *ToTemplateLocOrErr, |
779 | LAngleLoc: *ToLAngleLocOrErr, |
780 | Params: To, |
781 | RAngleLoc: *ToRAngleLocOrErr, |
782 | RequiresClause: *ToRequiresClause); |
783 | } |
784 | |
785 | template <> |
786 | Expected<TemplateArgument> |
787 | ASTNodeImporter::import(const TemplateArgument &From) { |
788 | switch (From.getKind()) { |
789 | case TemplateArgument::Null: |
790 | return TemplateArgument(); |
791 | |
792 | case TemplateArgument::Type: { |
793 | ExpectedType ToTypeOrErr = import(From.getAsType()); |
794 | if (!ToTypeOrErr) |
795 | return ToTypeOrErr.takeError(); |
796 | return TemplateArgument(*ToTypeOrErr, /*isNullPtr*/ false, |
797 | From.getIsDefaulted()); |
798 | } |
799 | |
800 | case TemplateArgument::Integral: { |
801 | ExpectedType ToTypeOrErr = import(From.getIntegralType()); |
802 | if (!ToTypeOrErr) |
803 | return ToTypeOrErr.takeError(); |
804 | return TemplateArgument(From, *ToTypeOrErr); |
805 | } |
806 | |
807 | case TemplateArgument::Declaration: { |
808 | Expected<ValueDecl *> ToOrErr = import(From.getAsDecl()); |
809 | if (!ToOrErr) |
810 | return ToOrErr.takeError(); |
811 | ExpectedType ToTypeOrErr = import(From.getParamTypeForDecl()); |
812 | if (!ToTypeOrErr) |
813 | return ToTypeOrErr.takeError(); |
814 | return TemplateArgument(dyn_cast<ValueDecl>((*ToOrErr)->getCanonicalDecl()), |
815 | *ToTypeOrErr, From.getIsDefaulted()); |
816 | } |
817 | |
818 | case TemplateArgument::NullPtr: { |
819 | ExpectedType ToTypeOrErr = import(From.getNullPtrType()); |
820 | if (!ToTypeOrErr) |
821 | return ToTypeOrErr.takeError(); |
822 | return TemplateArgument(*ToTypeOrErr, /*isNullPtr*/ true, |
823 | From.getIsDefaulted()); |
824 | } |
825 | |
826 | case TemplateArgument::StructuralValue: { |
827 | ExpectedType ToTypeOrErr = import(From.getStructuralValueType()); |
828 | if (!ToTypeOrErr) |
829 | return ToTypeOrErr.takeError(); |
830 | Expected<APValue> ToValueOrErr = import(From.getAsStructuralValue()); |
831 | if (!ToValueOrErr) |
832 | return ToValueOrErr.takeError(); |
833 | return TemplateArgument(Importer.getToContext(), *ToTypeOrErr, |
834 | *ToValueOrErr); |
835 | } |
836 | |
837 | case TemplateArgument::Template: { |
838 | Expected<TemplateName> ToTemplateOrErr = import(From.getAsTemplate()); |
839 | if (!ToTemplateOrErr) |
840 | return ToTemplateOrErr.takeError(); |
841 | |
842 | return TemplateArgument(*ToTemplateOrErr, From.getIsDefaulted()); |
843 | } |
844 | |
845 | case TemplateArgument::TemplateExpansion: { |
846 | Expected<TemplateName> ToTemplateOrErr = |
847 | import(From.getAsTemplateOrTemplatePattern()); |
848 | if (!ToTemplateOrErr) |
849 | return ToTemplateOrErr.takeError(); |
850 | |
851 | return TemplateArgument(*ToTemplateOrErr, From.getNumTemplateExpansions(), |
852 | From.getIsDefaulted()); |
853 | } |
854 | |
855 | case TemplateArgument::Expression: |
856 | if (ExpectedExpr ToExpr = import(From.getAsExpr())) |
857 | return TemplateArgument(*ToExpr, From.getIsDefaulted()); |
858 | else |
859 | return ToExpr.takeError(); |
860 | |
861 | case TemplateArgument::Pack: { |
862 | SmallVector<TemplateArgument, 2> ToPack; |
863 | ToPack.reserve(From.pack_size()); |
864 | if (Error Err = ImportTemplateArguments(FromArgs: From.pack_elements(), ToArgs&: ToPack)) |
865 | return std::move(Err); |
866 | |
867 | return TemplateArgument( |
868 | llvm::ArrayRef(ToPack).copy(Importer.getToContext())); |
869 | } |
870 | } |
871 | |
872 | llvm_unreachable("Invalid template argument kind" ); |
873 | } |
874 | |
875 | template <> |
876 | Expected<TemplateArgumentLoc> |
877 | ASTNodeImporter::import(const TemplateArgumentLoc &TALoc) { |
878 | Expected<TemplateArgument> ArgOrErr = import(TALoc.getArgument()); |
879 | if (!ArgOrErr) |
880 | return ArgOrErr.takeError(); |
881 | TemplateArgument Arg = *ArgOrErr; |
882 | |
883 | TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo(); |
884 | |
885 | TemplateArgumentLocInfo ToInfo; |
886 | if (Arg.getKind() == TemplateArgument::Expression) { |
887 | ExpectedExpr E = import(FromInfo.getAsExpr()); |
888 | if (!E) |
889 | return E.takeError(); |
890 | ToInfo = TemplateArgumentLocInfo(*E); |
891 | } else if (Arg.getKind() == TemplateArgument::Type) { |
892 | if (auto TSIOrErr = import(FromInfo.getAsTypeSourceInfo())) |
893 | ToInfo = TemplateArgumentLocInfo(*TSIOrErr); |
894 | else |
895 | return TSIOrErr.takeError(); |
896 | } else { |
897 | auto ToTemplateQualifierLocOrErr = |
898 | import(FromInfo.getTemplateQualifierLoc()); |
899 | if (!ToTemplateQualifierLocOrErr) |
900 | return ToTemplateQualifierLocOrErr.takeError(); |
901 | auto ToTemplateNameLocOrErr = import(FromInfo.getTemplateNameLoc()); |
902 | if (!ToTemplateNameLocOrErr) |
903 | return ToTemplateNameLocOrErr.takeError(); |
904 | auto ToTemplateEllipsisLocOrErr = |
905 | import(FromInfo.getTemplateEllipsisLoc()); |
906 | if (!ToTemplateEllipsisLocOrErr) |
907 | return ToTemplateEllipsisLocOrErr.takeError(); |
908 | ToInfo = TemplateArgumentLocInfo( |
909 | Importer.getToContext(), *ToTemplateQualifierLocOrErr, |
910 | *ToTemplateNameLocOrErr, *ToTemplateEllipsisLocOrErr); |
911 | } |
912 | |
913 | return TemplateArgumentLoc(Arg, ToInfo); |
914 | } |
915 | |
916 | template <> |
917 | Expected<DeclGroupRef> ASTNodeImporter::import(const DeclGroupRef &DG) { |
918 | if (DG.isNull()) |
919 | return DeclGroupRef::Create(C&: Importer.getToContext(), Decls: nullptr, NumDecls: 0); |
920 | size_t NumDecls = DG.end() - DG.begin(); |
921 | SmallVector<Decl *, 1> ToDecls; |
922 | ToDecls.reserve(NumDecls); |
923 | for (Decl *FromD : DG) { |
924 | if (auto ToDOrErr = import(FromD)) |
925 | ToDecls.push_back(*ToDOrErr); |
926 | else |
927 | return ToDOrErr.takeError(); |
928 | } |
929 | return DeclGroupRef::Create(C&: Importer.getToContext(), |
930 | Decls: ToDecls.begin(), |
931 | NumDecls); |
932 | } |
933 | |
934 | template <> |
935 | Expected<ASTNodeImporter::Designator> |
936 | ASTNodeImporter::import(const Designator &D) { |
937 | if (D.isFieldDesignator()) { |
938 | IdentifierInfo *ToFieldName = Importer.Import(FromId: D.getFieldName()); |
939 | |
940 | ExpectedSLoc ToDotLocOrErr = import(D.getDotLoc()); |
941 | if (!ToDotLocOrErr) |
942 | return ToDotLocOrErr.takeError(); |
943 | |
944 | ExpectedSLoc ToFieldLocOrErr = import(D.getFieldLoc()); |
945 | if (!ToFieldLocOrErr) |
946 | return ToFieldLocOrErr.takeError(); |
947 | |
948 | return DesignatedInitExpr::Designator::CreateFieldDesignator( |
949 | FieldName: ToFieldName, DotLoc: *ToDotLocOrErr, FieldLoc: *ToFieldLocOrErr); |
950 | } |
951 | |
952 | ExpectedSLoc ToLBracketLocOrErr = import(D.getLBracketLoc()); |
953 | if (!ToLBracketLocOrErr) |
954 | return ToLBracketLocOrErr.takeError(); |
955 | |
956 | ExpectedSLoc ToRBracketLocOrErr = import(D.getRBracketLoc()); |
957 | if (!ToRBracketLocOrErr) |
958 | return ToRBracketLocOrErr.takeError(); |
959 | |
960 | if (D.isArrayDesignator()) |
961 | return Designator::CreateArrayDesignator(Index: D.getArrayIndex(), |
962 | LBracketLoc: *ToLBracketLocOrErr, |
963 | RBracketLoc: *ToRBracketLocOrErr); |
964 | |
965 | ExpectedSLoc ToEllipsisLocOrErr = import(D.getEllipsisLoc()); |
966 | if (!ToEllipsisLocOrErr) |
967 | return ToEllipsisLocOrErr.takeError(); |
968 | |
969 | assert(D.isArrayRangeDesignator()); |
970 | return Designator::CreateArrayRangeDesignator( |
971 | Index: D.getArrayIndex(), LBracketLoc: *ToLBracketLocOrErr, EllipsisLoc: *ToEllipsisLocOrErr, |
972 | RBracketLoc: *ToRBracketLocOrErr); |
973 | } |
974 | |
975 | template <> |
976 | Expected<ConceptReference *> ASTNodeImporter::import(ConceptReference *From) { |
977 | Error Err = Error::success(); |
978 | auto ToNNS = importChecked(Err, From->getNestedNameSpecifierLoc()); |
979 | auto ToTemplateKWLoc = importChecked(Err, From->getTemplateKWLoc()); |
980 | auto ToConceptNameLoc = |
981 | importChecked(Err, From->getConceptNameInfo().getLoc()); |
982 | auto ToConceptName = importChecked(Err, From->getConceptNameInfo().getName()); |
983 | auto ToFoundDecl = importChecked(Err, From->getFoundDecl()); |
984 | auto ToNamedConcept = importChecked(Err, From->getNamedConcept()); |
985 | if (Err) |
986 | return std::move(Err); |
987 | TemplateArgumentListInfo ToTAInfo; |
988 | const auto *ASTTemplateArgs = From->getTemplateArgsAsWritten(); |
989 | if (ASTTemplateArgs) |
990 | if (Error Err = ImportTemplateArgumentListInfo(*ASTTemplateArgs, ToTAInfo)) |
991 | return std::move(Err); |
992 | auto *ConceptRef = ConceptReference::Create( |
993 | C: Importer.getToContext(), NNS: ToNNS, TemplateKWLoc: ToTemplateKWLoc, |
994 | ConceptNameInfo: DeclarationNameInfo(ToConceptName, ToConceptNameLoc), FoundDecl: ToFoundDecl, |
995 | NamedConcept: ToNamedConcept, |
996 | ArgsAsWritten: ASTTemplateArgs ? ASTTemplateArgumentListInfo::Create( |
997 | C: Importer.getToContext(), List: ToTAInfo) |
998 | : nullptr); |
999 | return ConceptRef; |
1000 | } |
1001 | |
1002 | template <> |
1003 | Expected<LambdaCapture> ASTNodeImporter::import(const LambdaCapture &From) { |
1004 | ValueDecl *Var = nullptr; |
1005 | if (From.capturesVariable()) { |
1006 | if (auto VarOrErr = import(From.getCapturedVar())) |
1007 | Var = *VarOrErr; |
1008 | else |
1009 | return VarOrErr.takeError(); |
1010 | } |
1011 | |
1012 | auto LocationOrErr = import(From.getLocation()); |
1013 | if (!LocationOrErr) |
1014 | return LocationOrErr.takeError(); |
1015 | |
1016 | SourceLocation EllipsisLoc; |
1017 | if (From.isPackExpansion()) |
1018 | if (Error Err = importInto(EllipsisLoc, From.getEllipsisLoc())) |
1019 | return std::move(Err); |
1020 | |
1021 | return LambdaCapture( |
1022 | *LocationOrErr, From.isImplicit(), From.getCaptureKind(), Var, |
1023 | EllipsisLoc); |
1024 | } |
1025 | |
1026 | template <typename T> |
1027 | bool ASTNodeImporter::hasSameVisibilityContextAndLinkage(T *Found, T *From) { |
1028 | if (Found->getLinkageInternal() != From->getLinkageInternal()) |
1029 | return false; |
1030 | |
1031 | if (From->hasExternalFormalLinkage()) |
1032 | return Found->hasExternalFormalLinkage(); |
1033 | if (Importer.GetFromTU(ToD: Found) != From->getTranslationUnitDecl()) |
1034 | return false; |
1035 | if (From->isInAnonymousNamespace()) |
1036 | return Found->isInAnonymousNamespace(); |
1037 | else |
1038 | return !Found->isInAnonymousNamespace() && |
1039 | !Found->hasExternalFormalLinkage(); |
1040 | } |
1041 | |
1042 | template <> |
1043 | bool ASTNodeImporter::hasSameVisibilityContextAndLinkage(TypedefNameDecl *Found, |
1044 | TypedefNameDecl *From) { |
1045 | if (Found->getLinkageInternal() != From->getLinkageInternal()) |
1046 | return false; |
1047 | |
1048 | if (From->isInAnonymousNamespace() && Found->isInAnonymousNamespace()) |
1049 | return Importer.GetFromTU(Found) == From->getTranslationUnitDecl(); |
1050 | return From->isInAnonymousNamespace() == Found->isInAnonymousNamespace(); |
1051 | } |
1052 | |
1053 | } // namespace clang |
1054 | |
1055 | //---------------------------------------------------------------------------- |
1056 | // Import Types |
1057 | //---------------------------------------------------------------------------- |
1058 | |
1059 | using namespace clang; |
1060 | |
1061 | ExpectedType ASTNodeImporter::VisitType(const Type *T) { |
1062 | Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node) |
1063 | << T->getTypeClassName(); |
1064 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
1065 | } |
1066 | |
1067 | ExpectedType ASTNodeImporter::VisitAtomicType(const AtomicType *T){ |
1068 | ExpectedType UnderlyingTypeOrErr = import(From: T->getValueType()); |
1069 | if (!UnderlyingTypeOrErr) |
1070 | return UnderlyingTypeOrErr.takeError(); |
1071 | |
1072 | return Importer.getToContext().getAtomicType(T: *UnderlyingTypeOrErr); |
1073 | } |
1074 | |
1075 | ExpectedType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) { |
1076 | switch (T->getKind()) { |
1077 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
1078 | case BuiltinType::Id: \ |
1079 | return Importer.getToContext().SingletonId; |
1080 | #include "clang/Basic/OpenCLImageTypes.def" |
1081 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
1082 | case BuiltinType::Id: \ |
1083 | return Importer.getToContext().Id##Ty; |
1084 | #include "clang/Basic/OpenCLExtensionTypes.def" |
1085 | #define SVE_TYPE(Name, Id, SingletonId) \ |
1086 | case BuiltinType::Id: \ |
1087 | return Importer.getToContext().SingletonId; |
1088 | #include "clang/Basic/AArch64SVEACLETypes.def" |
1089 | #define PPC_VECTOR_TYPE(Name, Id, Size) \ |
1090 | case BuiltinType::Id: \ |
1091 | return Importer.getToContext().Id##Ty; |
1092 | #include "clang/Basic/PPCTypes.def" |
1093 | #define RVV_TYPE(Name, Id, SingletonId) \ |
1094 | case BuiltinType::Id: \ |
1095 | return Importer.getToContext().SingletonId; |
1096 | #include "clang/Basic/RISCVVTypes.def" |
1097 | #define WASM_TYPE(Name, Id, SingletonId) \ |
1098 | case BuiltinType::Id: \ |
1099 | return Importer.getToContext().SingletonId; |
1100 | #include "clang/Basic/WebAssemblyReferenceTypes.def" |
1101 | #define SHARED_SINGLETON_TYPE(Expansion) |
1102 | #define BUILTIN_TYPE(Id, SingletonId) \ |
1103 | case BuiltinType::Id: return Importer.getToContext().SingletonId; |
1104 | #include "clang/AST/BuiltinTypes.def" |
1105 | |
1106 | // FIXME: for Char16, Char32, and NullPtr, make sure that the "to" |
1107 | // context supports C++. |
1108 | |
1109 | // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to" |
1110 | // context supports ObjC. |
1111 | |
1112 | case BuiltinType::Char_U: |
1113 | // The context we're importing from has an unsigned 'char'. If we're |
1114 | // importing into a context with a signed 'char', translate to |
1115 | // 'unsigned char' instead. |
1116 | if (Importer.getToContext().getLangOpts().CharIsSigned) |
1117 | return Importer.getToContext().UnsignedCharTy; |
1118 | |
1119 | return Importer.getToContext().CharTy; |
1120 | |
1121 | case BuiltinType::Char_S: |
1122 | // The context we're importing from has an unsigned 'char'. If we're |
1123 | // importing into a context with a signed 'char', translate to |
1124 | // 'unsigned char' instead. |
1125 | if (!Importer.getToContext().getLangOpts().CharIsSigned) |
1126 | return Importer.getToContext().SignedCharTy; |
1127 | |
1128 | return Importer.getToContext().CharTy; |
1129 | |
1130 | case BuiltinType::WChar_S: |
1131 | case BuiltinType::WChar_U: |
1132 | // FIXME: If not in C++, shall we translate to the C equivalent of |
1133 | // wchar_t? |
1134 | return Importer.getToContext().WCharTy; |
1135 | } |
1136 | |
1137 | llvm_unreachable("Invalid BuiltinType Kind!" ); |
1138 | } |
1139 | |
1140 | ExpectedType ASTNodeImporter::VisitDecayedType(const DecayedType *T) { |
1141 | ExpectedType ToOriginalTypeOrErr = import(T->getOriginalType()); |
1142 | if (!ToOriginalTypeOrErr) |
1143 | return ToOriginalTypeOrErr.takeError(); |
1144 | |
1145 | return Importer.getToContext().getDecayedType(T: *ToOriginalTypeOrErr); |
1146 | } |
1147 | |
1148 | ExpectedType ASTNodeImporter::VisitComplexType(const ComplexType *T) { |
1149 | ExpectedType ToElementTypeOrErr = import(From: T->getElementType()); |
1150 | if (!ToElementTypeOrErr) |
1151 | return ToElementTypeOrErr.takeError(); |
1152 | |
1153 | return Importer.getToContext().getComplexType(T: *ToElementTypeOrErr); |
1154 | } |
1155 | |
1156 | ExpectedType ASTNodeImporter::VisitPointerType(const PointerType *T) { |
1157 | ExpectedType ToPointeeTypeOrErr = import(From: T->getPointeeType()); |
1158 | if (!ToPointeeTypeOrErr) |
1159 | return ToPointeeTypeOrErr.takeError(); |
1160 | |
1161 | return Importer.getToContext().getPointerType(T: *ToPointeeTypeOrErr); |
1162 | } |
1163 | |
1164 | ExpectedType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) { |
1165 | // FIXME: Check for blocks support in "to" context. |
1166 | ExpectedType ToPointeeTypeOrErr = import(From: T->getPointeeType()); |
1167 | if (!ToPointeeTypeOrErr) |
1168 | return ToPointeeTypeOrErr.takeError(); |
1169 | |
1170 | return Importer.getToContext().getBlockPointerType(T: *ToPointeeTypeOrErr); |
1171 | } |
1172 | |
1173 | ExpectedType |
1174 | ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) { |
1175 | // FIXME: Check for C++ support in "to" context. |
1176 | ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten()); |
1177 | if (!ToPointeeTypeOrErr) |
1178 | return ToPointeeTypeOrErr.takeError(); |
1179 | |
1180 | return Importer.getToContext().getLValueReferenceType(T: *ToPointeeTypeOrErr); |
1181 | } |
1182 | |
1183 | ExpectedType |
1184 | ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) { |
1185 | // FIXME: Check for C++0x support in "to" context. |
1186 | ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten()); |
1187 | if (!ToPointeeTypeOrErr) |
1188 | return ToPointeeTypeOrErr.takeError(); |
1189 | |
1190 | return Importer.getToContext().getRValueReferenceType(T: *ToPointeeTypeOrErr); |
1191 | } |
1192 | |
1193 | ExpectedType |
1194 | ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) { |
1195 | // FIXME: Check for C++ support in "to" context. |
1196 | ExpectedType ToPointeeTypeOrErr = import(From: T->getPointeeType()); |
1197 | if (!ToPointeeTypeOrErr) |
1198 | return ToPointeeTypeOrErr.takeError(); |
1199 | |
1200 | ExpectedTypePtr ClassTypeOrErr = import(From: T->getClass()); |
1201 | if (!ClassTypeOrErr) |
1202 | return ClassTypeOrErr.takeError(); |
1203 | |
1204 | return Importer.getToContext().getMemberPointerType(T: *ToPointeeTypeOrErr, |
1205 | Cls: *ClassTypeOrErr); |
1206 | } |
1207 | |
1208 | ExpectedType |
1209 | ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) { |
1210 | Error Err = Error::success(); |
1211 | auto ToElementType = importChecked(Err, T->getElementType()); |
1212 | auto ToSizeExpr = importChecked(Err, From: T->getSizeExpr()); |
1213 | if (Err) |
1214 | return std::move(Err); |
1215 | |
1216 | return Importer.getToContext().getConstantArrayType( |
1217 | EltTy: ToElementType, ArySize: T->getSize(), SizeExpr: ToSizeExpr, ASM: T->getSizeModifier(), |
1218 | IndexTypeQuals: T->getIndexTypeCVRQualifiers()); |
1219 | } |
1220 | |
1221 | ExpectedType |
1222 | ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) { |
1223 | ExpectedType ToElementTypeOrErr = import(T->getElementType()); |
1224 | if (!ToElementTypeOrErr) |
1225 | return ToElementTypeOrErr.takeError(); |
1226 | |
1227 | return Importer.getToContext().getIncompleteArrayType(EltTy: *ToElementTypeOrErr, |
1228 | ASM: T->getSizeModifier(), |
1229 | IndexTypeQuals: T->getIndexTypeCVRQualifiers()); |
1230 | } |
1231 | |
1232 | ExpectedType |
1233 | ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) { |
1234 | Error Err = Error::success(); |
1235 | QualType ToElementType = importChecked(Err, T->getElementType()); |
1236 | Expr *ToSizeExpr = importChecked(Err, From: T->getSizeExpr()); |
1237 | SourceRange ToBracketsRange = importChecked(Err, From: T->getBracketsRange()); |
1238 | if (Err) |
1239 | return std::move(Err); |
1240 | return Importer.getToContext().getVariableArrayType( |
1241 | EltTy: ToElementType, NumElts: ToSizeExpr, ASM: T->getSizeModifier(), |
1242 | IndexTypeQuals: T->getIndexTypeCVRQualifiers(), Brackets: ToBracketsRange); |
1243 | } |
1244 | |
1245 | ExpectedType ASTNodeImporter::VisitDependentSizedArrayType( |
1246 | const DependentSizedArrayType *T) { |
1247 | Error Err = Error::success(); |
1248 | QualType ToElementType = importChecked(Err, T->getElementType()); |
1249 | Expr *ToSizeExpr = importChecked(Err, From: T->getSizeExpr()); |
1250 | SourceRange ToBracketsRange = importChecked(Err, From: T->getBracketsRange()); |
1251 | if (Err) |
1252 | return std::move(Err); |
1253 | // SizeExpr may be null if size is not specified directly. |
1254 | // For example, 'int a[]'. |
1255 | |
1256 | return Importer.getToContext().getDependentSizedArrayType( |
1257 | EltTy: ToElementType, NumElts: ToSizeExpr, ASM: T->getSizeModifier(), |
1258 | IndexTypeQuals: T->getIndexTypeCVRQualifiers(), Brackets: ToBracketsRange); |
1259 | } |
1260 | |
1261 | ExpectedType ASTNodeImporter::VisitDependentSizedExtVectorType( |
1262 | const DependentSizedExtVectorType *T) { |
1263 | Error Err = Error::success(); |
1264 | QualType ToElementType = importChecked(Err, From: T->getElementType()); |
1265 | Expr *ToSizeExpr = importChecked(Err, From: T->getSizeExpr()); |
1266 | SourceLocation ToAttrLoc = importChecked(Err, From: T->getAttributeLoc()); |
1267 | if (Err) |
1268 | return std::move(Err); |
1269 | return Importer.getToContext().getDependentSizedExtVectorType( |
1270 | VectorType: ToElementType, SizeExpr: ToSizeExpr, AttrLoc: ToAttrLoc); |
1271 | } |
1272 | |
1273 | ExpectedType ASTNodeImporter::VisitVectorType(const VectorType *T) { |
1274 | ExpectedType ToElementTypeOrErr = import(From: T->getElementType()); |
1275 | if (!ToElementTypeOrErr) |
1276 | return ToElementTypeOrErr.takeError(); |
1277 | |
1278 | return Importer.getToContext().getVectorType(VectorType: *ToElementTypeOrErr, |
1279 | NumElts: T->getNumElements(), |
1280 | VecKind: T->getVectorKind()); |
1281 | } |
1282 | |
1283 | ExpectedType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) { |
1284 | ExpectedType ToElementTypeOrErr = import(T->getElementType()); |
1285 | if (!ToElementTypeOrErr) |
1286 | return ToElementTypeOrErr.takeError(); |
1287 | |
1288 | return Importer.getToContext().getExtVectorType(VectorType: *ToElementTypeOrErr, |
1289 | NumElts: T->getNumElements()); |
1290 | } |
1291 | |
1292 | ExpectedType |
1293 | ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { |
1294 | // FIXME: What happens if we're importing a function without a prototype |
1295 | // into C++? Should we make it variadic? |
1296 | ExpectedType ToReturnTypeOrErr = import(T->getReturnType()); |
1297 | if (!ToReturnTypeOrErr) |
1298 | return ToReturnTypeOrErr.takeError(); |
1299 | |
1300 | return Importer.getToContext().getFunctionNoProtoType(*ToReturnTypeOrErr, |
1301 | T->getExtInfo()); |
1302 | } |
1303 | |
1304 | ExpectedType |
1305 | ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) { |
1306 | ExpectedType ToReturnTypeOrErr = import(T->getReturnType()); |
1307 | if (!ToReturnTypeOrErr) |
1308 | return ToReturnTypeOrErr.takeError(); |
1309 | |
1310 | // Import argument types |
1311 | SmallVector<QualType, 4> ArgTypes; |
1312 | for (const auto &A : T->param_types()) { |
1313 | ExpectedType TyOrErr = import(From: A); |
1314 | if (!TyOrErr) |
1315 | return TyOrErr.takeError(); |
1316 | ArgTypes.push_back(Elt: *TyOrErr); |
1317 | } |
1318 | |
1319 | // Import exception types |
1320 | SmallVector<QualType, 4> ExceptionTypes; |
1321 | for (const auto &E : T->exceptions()) { |
1322 | ExpectedType TyOrErr = import(From: E); |
1323 | if (!TyOrErr) |
1324 | return TyOrErr.takeError(); |
1325 | ExceptionTypes.push_back(Elt: *TyOrErr); |
1326 | } |
1327 | |
1328 | FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo(); |
1329 | Error Err = Error::success(); |
1330 | FunctionProtoType::ExtProtoInfo ToEPI; |
1331 | ToEPI.ExtInfo = FromEPI.ExtInfo; |
1332 | ToEPI.Variadic = FromEPI.Variadic; |
1333 | ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn; |
1334 | ToEPI.TypeQuals = FromEPI.TypeQuals; |
1335 | ToEPI.RefQualifier = FromEPI.RefQualifier; |
1336 | ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type; |
1337 | ToEPI.ExceptionSpec.NoexceptExpr = |
1338 | importChecked(Err, FromEPI.ExceptionSpec.NoexceptExpr); |
1339 | ToEPI.ExceptionSpec.SourceDecl = |
1340 | importChecked(Err, FromEPI.ExceptionSpec.SourceDecl); |
1341 | ToEPI.ExceptionSpec.SourceTemplate = |
1342 | importChecked(Err, FromEPI.ExceptionSpec.SourceTemplate); |
1343 | ToEPI.ExceptionSpec.Exceptions = ExceptionTypes; |
1344 | |
1345 | if (Err) |
1346 | return std::move(Err); |
1347 | |
1348 | return Importer.getToContext().getFunctionType( |
1349 | ResultTy: *ToReturnTypeOrErr, Args: ArgTypes, EPI: ToEPI); |
1350 | } |
1351 | |
1352 | ExpectedType ASTNodeImporter::VisitUnresolvedUsingType( |
1353 | const UnresolvedUsingType *T) { |
1354 | Error Err = Error::success(); |
1355 | auto ToD = importChecked(Err, From: T->getDecl()); |
1356 | auto ToPrevD = importChecked(Err, T->getDecl()->getPreviousDecl()); |
1357 | if (Err) |
1358 | return std::move(Err); |
1359 | |
1360 | return Importer.getToContext().getTypeDeclType( |
1361 | Decl: ToD, PrevDecl: cast_or_null<TypeDecl>(ToPrevD)); |
1362 | } |
1363 | |
1364 | ExpectedType ASTNodeImporter::VisitParenType(const ParenType *T) { |
1365 | ExpectedType ToInnerTypeOrErr = import(From: T->getInnerType()); |
1366 | if (!ToInnerTypeOrErr) |
1367 | return ToInnerTypeOrErr.takeError(); |
1368 | |
1369 | return Importer.getToContext().getParenType(NamedType: *ToInnerTypeOrErr); |
1370 | } |
1371 | |
1372 | ExpectedType |
1373 | ASTNodeImporter::VisitPackIndexingType(clang::PackIndexingType const *T) { |
1374 | |
1375 | ExpectedType Pattern = import(From: T->getPattern()); |
1376 | if (!Pattern) |
1377 | return Pattern.takeError(); |
1378 | ExpectedExpr Index = import(From: T->getIndexExpr()); |
1379 | if (!Index) |
1380 | return Index.takeError(); |
1381 | return Importer.getToContext().getPackIndexingType(Pattern: *Pattern, IndexExpr: *Index); |
1382 | } |
1383 | |
1384 | ExpectedType ASTNodeImporter::VisitTypedefType(const TypedefType *T) { |
1385 | Expected<TypedefNameDecl *> ToDeclOrErr = import(From: T->getDecl()); |
1386 | if (!ToDeclOrErr) |
1387 | return ToDeclOrErr.takeError(); |
1388 | |
1389 | TypedefNameDecl *ToDecl = *ToDeclOrErr; |
1390 | if (ToDecl->getTypeForDecl()) |
1391 | return QualType(ToDecl->getTypeForDecl(), 0); |
1392 | |
1393 | ExpectedType ToUnderlyingTypeOrErr = import(From: T->desugar()); |
1394 | if (!ToUnderlyingTypeOrErr) |
1395 | return ToUnderlyingTypeOrErr.takeError(); |
1396 | |
1397 | return Importer.getToContext().getTypedefType(Decl: ToDecl, Underlying: *ToUnderlyingTypeOrErr); |
1398 | } |
1399 | |
1400 | ExpectedType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) { |
1401 | ExpectedExpr ToExprOrErr = import(From: T->getUnderlyingExpr()); |
1402 | if (!ToExprOrErr) |
1403 | return ToExprOrErr.takeError(); |
1404 | return Importer.getToContext().getTypeOfExprType(E: *ToExprOrErr, Kind: T->getKind()); |
1405 | } |
1406 | |
1407 | ExpectedType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) { |
1408 | ExpectedType ToUnderlyingTypeOrErr = import(From: T->getUnmodifiedType()); |
1409 | if (!ToUnderlyingTypeOrErr) |
1410 | return ToUnderlyingTypeOrErr.takeError(); |
1411 | return Importer.getToContext().getTypeOfType(QT: *ToUnderlyingTypeOrErr, |
1412 | Kind: T->getKind()); |
1413 | } |
1414 | |
1415 | ExpectedType ASTNodeImporter::VisitUsingType(const UsingType *T) { |
1416 | Expected<UsingShadowDecl *> FoundOrErr = import(From: T->getFoundDecl()); |
1417 | if (!FoundOrErr) |
1418 | return FoundOrErr.takeError(); |
1419 | Expected<QualType> UnderlyingOrErr = import(From: T->getUnderlyingType()); |
1420 | if (!UnderlyingOrErr) |
1421 | return UnderlyingOrErr.takeError(); |
1422 | |
1423 | return Importer.getToContext().getUsingType(Found: *FoundOrErr, Underlying: *UnderlyingOrErr); |
1424 | } |
1425 | |
1426 | ExpectedType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) { |
1427 | // FIXME: Make sure that the "to" context supports C++0x! |
1428 | ExpectedExpr ToExprOrErr = import(From: T->getUnderlyingExpr()); |
1429 | if (!ToExprOrErr) |
1430 | return ToExprOrErr.takeError(); |
1431 | |
1432 | ExpectedType ToUnderlyingTypeOrErr = import(From: T->getUnderlyingType()); |
1433 | if (!ToUnderlyingTypeOrErr) |
1434 | return ToUnderlyingTypeOrErr.takeError(); |
1435 | |
1436 | return Importer.getToContext().getDecltypeType( |
1437 | e: *ToExprOrErr, UnderlyingType: *ToUnderlyingTypeOrErr); |
1438 | } |
1439 | |
1440 | ExpectedType |
1441 | ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) { |
1442 | ExpectedType ToBaseTypeOrErr = import(From: T->getBaseType()); |
1443 | if (!ToBaseTypeOrErr) |
1444 | return ToBaseTypeOrErr.takeError(); |
1445 | |
1446 | ExpectedType ToUnderlyingTypeOrErr = import(From: T->getUnderlyingType()); |
1447 | if (!ToUnderlyingTypeOrErr) |
1448 | return ToUnderlyingTypeOrErr.takeError(); |
1449 | |
1450 | return Importer.getToContext().getUnaryTransformType( |
1451 | BaseType: *ToBaseTypeOrErr, UnderlyingType: *ToUnderlyingTypeOrErr, UKind: T->getUTTKind()); |
1452 | } |
1453 | |
1454 | ExpectedType ASTNodeImporter::VisitAutoType(const AutoType *T) { |
1455 | // FIXME: Make sure that the "to" context supports C++11! |
1456 | ExpectedType ToDeducedTypeOrErr = import(T->getDeducedType()); |
1457 | if (!ToDeducedTypeOrErr) |
1458 | return ToDeducedTypeOrErr.takeError(); |
1459 | |
1460 | ExpectedDecl ToTypeConstraintConcept = import(From: T->getTypeConstraintConcept()); |
1461 | if (!ToTypeConstraintConcept) |
1462 | return ToTypeConstraintConcept.takeError(); |
1463 | |
1464 | SmallVector<TemplateArgument, 2> ToTemplateArgs; |
1465 | if (Error Err = ImportTemplateArguments(FromArgs: T->getTypeConstraintArguments(), |
1466 | ToArgs&: ToTemplateArgs)) |
1467 | return std::move(Err); |
1468 | |
1469 | return Importer.getToContext().getAutoType( |
1470 | DeducedType: *ToDeducedTypeOrErr, Keyword: T->getKeyword(), /*IsDependent*/false, |
1471 | /*IsPack=*/false, TypeConstraintConcept: cast_or_null<ConceptDecl>(Val: *ToTypeConstraintConcept), |
1472 | TypeConstraintArgs: ToTemplateArgs); |
1473 | } |
1474 | |
1475 | ExpectedType ASTNodeImporter::VisitDeducedTemplateSpecializationType( |
1476 | const DeducedTemplateSpecializationType *T) { |
1477 | // FIXME: Make sure that the "to" context supports C++17! |
1478 | Expected<TemplateName> ToTemplateNameOrErr = import(From: T->getTemplateName()); |
1479 | if (!ToTemplateNameOrErr) |
1480 | return ToTemplateNameOrErr.takeError(); |
1481 | ExpectedType ToDeducedTypeOrErr = import(T->getDeducedType()); |
1482 | if (!ToDeducedTypeOrErr) |
1483 | return ToDeducedTypeOrErr.takeError(); |
1484 | |
1485 | return Importer.getToContext().getDeducedTemplateSpecializationType( |
1486 | Template: *ToTemplateNameOrErr, DeducedType: *ToDeducedTypeOrErr, IsDependent: T->isDependentType()); |
1487 | } |
1488 | |
1489 | ExpectedType ASTNodeImporter::VisitInjectedClassNameType( |
1490 | const InjectedClassNameType *T) { |
1491 | Expected<CXXRecordDecl *> ToDeclOrErr = import(From: T->getDecl()); |
1492 | if (!ToDeclOrErr) |
1493 | return ToDeclOrErr.takeError(); |
1494 | |
1495 | // The InjectedClassNameType is created in VisitRecordDecl when the |
1496 | // T->getDecl() is imported. Here we can return the existing type. |
1497 | const Type *Ty = (*ToDeclOrErr)->getTypeForDecl(); |
1498 | assert(Ty && isa<InjectedClassNameType>(Ty)); |
1499 | return QualType(Ty, 0); |
1500 | } |
1501 | |
1502 | ExpectedType ASTNodeImporter::VisitRecordType(const RecordType *T) { |
1503 | Expected<RecordDecl *> ToDeclOrErr = import(From: T->getDecl()); |
1504 | if (!ToDeclOrErr) |
1505 | return ToDeclOrErr.takeError(); |
1506 | |
1507 | return Importer.getToContext().getTagDeclType(*ToDeclOrErr); |
1508 | } |
1509 | |
1510 | ExpectedType ASTNodeImporter::VisitEnumType(const EnumType *T) { |
1511 | Expected<EnumDecl *> ToDeclOrErr = import(From: T->getDecl()); |
1512 | if (!ToDeclOrErr) |
1513 | return ToDeclOrErr.takeError(); |
1514 | |
1515 | return Importer.getToContext().getTagDeclType(*ToDeclOrErr); |
1516 | } |
1517 | |
1518 | ExpectedType ASTNodeImporter::VisitAttributedType(const AttributedType *T) { |
1519 | ExpectedType ToModifiedTypeOrErr = import(From: T->getModifiedType()); |
1520 | if (!ToModifiedTypeOrErr) |
1521 | return ToModifiedTypeOrErr.takeError(); |
1522 | ExpectedType ToEquivalentTypeOrErr = import(From: T->getEquivalentType()); |
1523 | if (!ToEquivalentTypeOrErr) |
1524 | return ToEquivalentTypeOrErr.takeError(); |
1525 | |
1526 | return Importer.getToContext().getAttributedType(attrKind: T->getAttrKind(), |
1527 | modifiedType: *ToModifiedTypeOrErr, equivalentType: *ToEquivalentTypeOrErr); |
1528 | } |
1529 | |
1530 | ExpectedType ASTNodeImporter::VisitTemplateTypeParmType( |
1531 | const TemplateTypeParmType *T) { |
1532 | Expected<TemplateTypeParmDecl *> ToDeclOrErr = import(From: T->getDecl()); |
1533 | if (!ToDeclOrErr) |
1534 | return ToDeclOrErr.takeError(); |
1535 | |
1536 | return Importer.getToContext().getTemplateTypeParmType( |
1537 | Depth: T->getDepth(), Index: T->getIndex(), ParameterPack: T->isParameterPack(), ParmDecl: *ToDeclOrErr); |
1538 | } |
1539 | |
1540 | ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmType( |
1541 | const SubstTemplateTypeParmType *T) { |
1542 | Expected<Decl *> ReplacedOrErr = import(From: T->getAssociatedDecl()); |
1543 | if (!ReplacedOrErr) |
1544 | return ReplacedOrErr.takeError(); |
1545 | |
1546 | ExpectedType ToReplacementTypeOrErr = import(From: T->getReplacementType()); |
1547 | if (!ToReplacementTypeOrErr) |
1548 | return ToReplacementTypeOrErr.takeError(); |
1549 | |
1550 | return Importer.getToContext().getSubstTemplateTypeParmType( |
1551 | Replacement: *ToReplacementTypeOrErr, AssociatedDecl: *ReplacedOrErr, Index: T->getIndex(), |
1552 | PackIndex: T->getPackIndex()); |
1553 | } |
1554 | |
1555 | ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmPackType( |
1556 | const SubstTemplateTypeParmPackType *T) { |
1557 | Expected<Decl *> ReplacedOrErr = import(From: T->getAssociatedDecl()); |
1558 | if (!ReplacedOrErr) |
1559 | return ReplacedOrErr.takeError(); |
1560 | |
1561 | Expected<TemplateArgument> ToArgumentPack = import(From: T->getArgumentPack()); |
1562 | if (!ToArgumentPack) |
1563 | return ToArgumentPack.takeError(); |
1564 | |
1565 | return Importer.getToContext().getSubstTemplateTypeParmPackType( |
1566 | AssociatedDecl: *ReplacedOrErr, Index: T->getIndex(), Final: T->getFinal(), ArgPack: *ToArgumentPack); |
1567 | } |
1568 | |
1569 | ExpectedType ASTNodeImporter::VisitTemplateSpecializationType( |
1570 | const TemplateSpecializationType *T) { |
1571 | auto ToTemplateOrErr = import(From: T->getTemplateName()); |
1572 | if (!ToTemplateOrErr) |
1573 | return ToTemplateOrErr.takeError(); |
1574 | |
1575 | SmallVector<TemplateArgument, 2> ToTemplateArgs; |
1576 | if (Error Err = |
1577 | ImportTemplateArguments(FromArgs: T->template_arguments(), ToArgs&: ToTemplateArgs)) |
1578 | return std::move(Err); |
1579 | |
1580 | QualType ToCanonType; |
1581 | if (!T->isCanonicalUnqualified()) { |
1582 | QualType FromCanonType |
1583 | = Importer.getFromContext().getCanonicalType(T: QualType(T, 0)); |
1584 | if (ExpectedType TyOrErr = import(From: FromCanonType)) |
1585 | ToCanonType = *TyOrErr; |
1586 | else |
1587 | return TyOrErr.takeError(); |
1588 | } |
1589 | return Importer.getToContext().getTemplateSpecializationType(T: *ToTemplateOrErr, |
1590 | Args: ToTemplateArgs, |
1591 | Canon: ToCanonType); |
1592 | } |
1593 | |
1594 | ExpectedType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) { |
1595 | // Note: the qualifier in an ElaboratedType is optional. |
1596 | auto ToQualifierOrErr = import(From: T->getQualifier()); |
1597 | if (!ToQualifierOrErr) |
1598 | return ToQualifierOrErr.takeError(); |
1599 | |
1600 | ExpectedType ToNamedTypeOrErr = import(From: T->getNamedType()); |
1601 | if (!ToNamedTypeOrErr) |
1602 | return ToNamedTypeOrErr.takeError(); |
1603 | |
1604 | Expected<TagDecl *> ToOwnedTagDeclOrErr = import(From: T->getOwnedTagDecl()); |
1605 | if (!ToOwnedTagDeclOrErr) |
1606 | return ToOwnedTagDeclOrErr.takeError(); |
1607 | |
1608 | return Importer.getToContext().getElaboratedType(Keyword: T->getKeyword(), |
1609 | NNS: *ToQualifierOrErr, |
1610 | NamedType: *ToNamedTypeOrErr, |
1611 | OwnedTagDecl: *ToOwnedTagDeclOrErr); |
1612 | } |
1613 | |
1614 | ExpectedType |
1615 | ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) { |
1616 | ExpectedType ToPatternOrErr = import(From: T->getPattern()); |
1617 | if (!ToPatternOrErr) |
1618 | return ToPatternOrErr.takeError(); |
1619 | |
1620 | return Importer.getToContext().getPackExpansionType(Pattern: *ToPatternOrErr, |
1621 | NumExpansions: T->getNumExpansions(), |
1622 | /*ExpactPack=*/ExpectPackInType: false); |
1623 | } |
1624 | |
1625 | ExpectedType ASTNodeImporter::VisitDependentTemplateSpecializationType( |
1626 | const DependentTemplateSpecializationType *T) { |
1627 | auto ToQualifierOrErr = import(From: T->getQualifier()); |
1628 | if (!ToQualifierOrErr) |
1629 | return ToQualifierOrErr.takeError(); |
1630 | |
1631 | IdentifierInfo *ToName = Importer.Import(FromId: T->getIdentifier()); |
1632 | |
1633 | SmallVector<TemplateArgument, 2> ToPack; |
1634 | ToPack.reserve(N: T->template_arguments().size()); |
1635 | if (Error Err = ImportTemplateArguments(FromArgs: T->template_arguments(), ToArgs&: ToPack)) |
1636 | return std::move(Err); |
1637 | |
1638 | return Importer.getToContext().getDependentTemplateSpecializationType( |
1639 | T->getKeyword(), *ToQualifierOrErr, ToName, ToPack); |
1640 | } |
1641 | |
1642 | ExpectedType |
1643 | ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) { |
1644 | auto ToQualifierOrErr = import(From: T->getQualifier()); |
1645 | if (!ToQualifierOrErr) |
1646 | return ToQualifierOrErr.takeError(); |
1647 | |
1648 | IdentifierInfo *Name = Importer.Import(FromId: T->getIdentifier()); |
1649 | |
1650 | QualType Canon; |
1651 | if (T != T->getCanonicalTypeInternal().getTypePtr()) { |
1652 | if (ExpectedType TyOrErr = import(T->getCanonicalTypeInternal())) |
1653 | Canon = (*TyOrErr).getCanonicalType(); |
1654 | else |
1655 | return TyOrErr.takeError(); |
1656 | } |
1657 | |
1658 | return Importer.getToContext().getDependentNameType(Keyword: T->getKeyword(), |
1659 | NNS: *ToQualifierOrErr, |
1660 | Name, Canon); |
1661 | } |
1662 | |
1663 | ExpectedType |
1664 | ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { |
1665 | Expected<ObjCInterfaceDecl *> ToDeclOrErr = import(From: T->getDecl()); |
1666 | if (!ToDeclOrErr) |
1667 | return ToDeclOrErr.takeError(); |
1668 | |
1669 | return Importer.getToContext().getObjCInterfaceType(Decl: *ToDeclOrErr); |
1670 | } |
1671 | |
1672 | ExpectedType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) { |
1673 | ExpectedType ToBaseTypeOrErr = import(From: T->getBaseType()); |
1674 | if (!ToBaseTypeOrErr) |
1675 | return ToBaseTypeOrErr.takeError(); |
1676 | |
1677 | SmallVector<QualType, 4> TypeArgs; |
1678 | for (auto TypeArg : T->getTypeArgsAsWritten()) { |
1679 | if (ExpectedType TyOrErr = import(From: TypeArg)) |
1680 | TypeArgs.push_back(Elt: *TyOrErr); |
1681 | else |
1682 | return TyOrErr.takeError(); |
1683 | } |
1684 | |
1685 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
1686 | for (auto *P : T->quals()) { |
1687 | if (Expected<ObjCProtocolDecl *> ProtocolOrErr = import(P)) |
1688 | Protocols.push_back(*ProtocolOrErr); |
1689 | else |
1690 | return ProtocolOrErr.takeError(); |
1691 | |
1692 | } |
1693 | |
1694 | return Importer.getToContext().getObjCObjectType(Base: *ToBaseTypeOrErr, typeArgs: TypeArgs, |
1695 | protocols: Protocols, |
1696 | isKindOf: T->isKindOfTypeAsWritten()); |
1697 | } |
1698 | |
1699 | ExpectedType |
1700 | ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { |
1701 | ExpectedType ToPointeeTypeOrErr = import(From: T->getPointeeType()); |
1702 | if (!ToPointeeTypeOrErr) |
1703 | return ToPointeeTypeOrErr.takeError(); |
1704 | |
1705 | return Importer.getToContext().getObjCObjectPointerType(OIT: *ToPointeeTypeOrErr); |
1706 | } |
1707 | |
1708 | ExpectedType |
1709 | ASTNodeImporter::VisitMacroQualifiedType(const MacroQualifiedType *T) { |
1710 | ExpectedType ToUnderlyingTypeOrErr = import(From: T->getUnderlyingType()); |
1711 | if (!ToUnderlyingTypeOrErr) |
1712 | return ToUnderlyingTypeOrErr.takeError(); |
1713 | |
1714 | IdentifierInfo *ToIdentifier = Importer.Import(FromId: T->getMacroIdentifier()); |
1715 | return Importer.getToContext().getMacroQualifiedType(UnderlyingTy: *ToUnderlyingTypeOrErr, |
1716 | MacroII: ToIdentifier); |
1717 | } |
1718 | |
1719 | ExpectedType clang::ASTNodeImporter::VisitAdjustedType(const AdjustedType *T) { |
1720 | Error Err = Error::success(); |
1721 | QualType ToOriginalType = importChecked(Err, From: T->getOriginalType()); |
1722 | QualType ToAdjustedType = importChecked(Err, From: T->getAdjustedType()); |
1723 | if (Err) |
1724 | return std::move(Err); |
1725 | |
1726 | return Importer.getToContext().getAdjustedType(Orig: ToOriginalType, |
1727 | New: ToAdjustedType); |
1728 | } |
1729 | |
1730 | ExpectedType clang::ASTNodeImporter::VisitBitIntType(const BitIntType *T) { |
1731 | return Importer.getToContext().getBitIntType(Unsigned: T->isUnsigned(), |
1732 | NumBits: T->getNumBits()); |
1733 | } |
1734 | |
1735 | ExpectedType clang::ASTNodeImporter::VisitBTFTagAttributedType( |
1736 | const clang::BTFTagAttributedType *T) { |
1737 | Error Err = Error::success(); |
1738 | const BTFTypeTagAttr *ToBTFAttr = importChecked(Err, From: T->getAttr()); |
1739 | QualType ToWrappedType = importChecked(Err, From: T->getWrappedType()); |
1740 | if (Err) |
1741 | return std::move(Err); |
1742 | |
1743 | return Importer.getToContext().getBTFTagAttributedType(BTFAttr: ToBTFAttr, |
1744 | Wrapped: ToWrappedType); |
1745 | } |
1746 | |
1747 | ExpectedType clang::ASTNodeImporter::VisitConstantMatrixType( |
1748 | const clang::ConstantMatrixType *T) { |
1749 | ExpectedType ToElementTypeOrErr = import(T->getElementType()); |
1750 | if (!ToElementTypeOrErr) |
1751 | return ToElementTypeOrErr.takeError(); |
1752 | |
1753 | return Importer.getToContext().getConstantMatrixType( |
1754 | ElementType: *ToElementTypeOrErr, NumRows: T->getNumRows(), NumColumns: T->getNumColumns()); |
1755 | } |
1756 | |
1757 | ExpectedType clang::ASTNodeImporter::VisitDependentAddressSpaceType( |
1758 | const clang::DependentAddressSpaceType *T) { |
1759 | Error Err = Error::success(); |
1760 | QualType ToPointeeType = importChecked(Err, From: T->getPointeeType()); |
1761 | Expr *ToAddrSpaceExpr = importChecked(Err, From: T->getAddrSpaceExpr()); |
1762 | SourceLocation ToAttrLoc = importChecked(Err, From: T->getAttributeLoc()); |
1763 | if (Err) |
1764 | return std::move(Err); |
1765 | |
1766 | return Importer.getToContext().getDependentAddressSpaceType( |
1767 | PointeeType: ToPointeeType, AddrSpaceExpr: ToAddrSpaceExpr, AttrLoc: ToAttrLoc); |
1768 | } |
1769 | |
1770 | ExpectedType clang::ASTNodeImporter::VisitDependentBitIntType( |
1771 | const clang::DependentBitIntType *T) { |
1772 | ExpectedExpr ToNumBitsExprOrErr = import(From: T->getNumBitsExpr()); |
1773 | if (!ToNumBitsExprOrErr) |
1774 | return ToNumBitsExprOrErr.takeError(); |
1775 | return Importer.getToContext().getDependentBitIntType(Unsigned: T->isUnsigned(), |
1776 | BitsExpr: *ToNumBitsExprOrErr); |
1777 | } |
1778 | |
1779 | ExpectedType clang::ASTNodeImporter::VisitDependentSizedMatrixType( |
1780 | const clang::DependentSizedMatrixType *T) { |
1781 | Error Err = Error::success(); |
1782 | QualType ToElementType = importChecked(Err, T->getElementType()); |
1783 | Expr *ToRowExpr = importChecked(Err, From: T->getRowExpr()); |
1784 | Expr *ToColumnExpr = importChecked(Err, From: T->getColumnExpr()); |
1785 | SourceLocation ToAttrLoc = importChecked(Err, From: T->getAttributeLoc()); |
1786 | if (Err) |
1787 | return std::move(Err); |
1788 | |
1789 | return Importer.getToContext().getDependentSizedMatrixType( |
1790 | ElementType: ToElementType, RowExpr: ToRowExpr, ColumnExpr: ToColumnExpr, AttrLoc: ToAttrLoc); |
1791 | } |
1792 | |
1793 | ExpectedType clang::ASTNodeImporter::VisitDependentVectorType( |
1794 | const clang::DependentVectorType *T) { |
1795 | Error Err = Error::success(); |
1796 | QualType ToElementType = importChecked(Err, From: T->getElementType()); |
1797 | Expr *ToSizeExpr = importChecked(Err, From: T->getSizeExpr()); |
1798 | SourceLocation ToAttrLoc = importChecked(Err, From: T->getAttributeLoc()); |
1799 | if (Err) |
1800 | return std::move(Err); |
1801 | |
1802 | return Importer.getToContext().getDependentVectorType( |
1803 | VectorType: ToElementType, SizeExpr: ToSizeExpr, AttrLoc: ToAttrLoc, VecKind: T->getVectorKind()); |
1804 | } |
1805 | |
1806 | ExpectedType clang::ASTNodeImporter::VisitObjCTypeParamType( |
1807 | const clang::ObjCTypeParamType *T) { |
1808 | Expected<ObjCTypeParamDecl *> ToDeclOrErr = import(From: T->getDecl()); |
1809 | if (!ToDeclOrErr) |
1810 | return ToDeclOrErr.takeError(); |
1811 | |
1812 | SmallVector<ObjCProtocolDecl *, 4> ToProtocols; |
1813 | for (ObjCProtocolDecl *FromProtocol : T->getProtocols()) { |
1814 | Expected<ObjCProtocolDecl *> ToProtocolOrErr = import(FromProtocol); |
1815 | if (!ToProtocolOrErr) |
1816 | return ToProtocolOrErr.takeError(); |
1817 | ToProtocols.push_back(*ToProtocolOrErr); |
1818 | } |
1819 | |
1820 | return Importer.getToContext().getObjCTypeParamType(Decl: *ToDeclOrErr, |
1821 | protocols: ToProtocols); |
1822 | } |
1823 | |
1824 | ExpectedType clang::ASTNodeImporter::VisitPipeType(const clang::PipeType *T) { |
1825 | ExpectedType ToElementTypeOrErr = import(From: T->getElementType()); |
1826 | if (!ToElementTypeOrErr) |
1827 | return ToElementTypeOrErr.takeError(); |
1828 | |
1829 | ASTContext &ToCtx = Importer.getToContext(); |
1830 | if (T->isReadOnly()) |
1831 | return ToCtx.getReadPipeType(T: *ToElementTypeOrErr); |
1832 | else |
1833 | return ToCtx.getWritePipeType(T: *ToElementTypeOrErr); |
1834 | } |
1835 | |
1836 | //---------------------------------------------------------------------------- |
1837 | // Import Declarations |
1838 | //---------------------------------------------------------------------------- |
1839 | Error ASTNodeImporter::ImportDeclParts( |
1840 | NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC, |
1841 | DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc) { |
1842 | // Check if RecordDecl is in FunctionDecl parameters to avoid infinite loop. |
1843 | // example: int struct_in_proto(struct data_t{int a;int b;} *d); |
1844 | // FIXME: We could support these constructs by importing a different type of |
1845 | // this parameter and by importing the original type of the parameter only |
1846 | // after the FunctionDecl is created. See |
1847 | // VisitFunctionDecl::UsedDifferentProtoType. |
1848 | DeclContext *OrigDC = D->getDeclContext(); |
1849 | FunctionDecl *FunDecl; |
1850 | if (isa<RecordDecl>(Val: D) && (FunDecl = dyn_cast<FunctionDecl>(Val: OrigDC)) && |
1851 | FunDecl->hasBody()) { |
1852 | auto getLeafPointeeType = [](const Type *T) { |
1853 | while (T->isPointerType() || T->isArrayType()) { |
1854 | T = T->getPointeeOrArrayElementType(); |
1855 | } |
1856 | return T; |
1857 | }; |
1858 | for (const ParmVarDecl *P : FunDecl->parameters()) { |
1859 | const Type *LeafT = |
1860 | getLeafPointeeType(P->getType().getCanonicalType().getTypePtr()); |
1861 | auto *RT = dyn_cast<RecordType>(Val: LeafT); |
1862 | if (RT && RT->getDecl() == D) { |
1863 | Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) |
1864 | << D->getDeclKindName(); |
1865 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
1866 | } |
1867 | } |
1868 | } |
1869 | |
1870 | // Import the context of this declaration. |
1871 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) |
1872 | return Err; |
1873 | |
1874 | // Import the name of this declaration. |
1875 | if (Error Err = importInto(To&: Name, From: D->getDeclName())) |
1876 | return Err; |
1877 | |
1878 | // Import the location of this declaration. |
1879 | if (Error Err = importInto(Loc, D->getLocation())) |
1880 | return Err; |
1881 | |
1882 | ToD = cast_or_null<NamedDecl>(Val: Importer.GetAlreadyImportedOrNull(D)); |
1883 | if (ToD) |
1884 | if (Error Err = ASTNodeImporter(*this).ImportDefinitionIfNeeded(D, ToD)) |
1885 | return Err; |
1886 | |
1887 | return Error::success(); |
1888 | } |
1889 | |
1890 | Error ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclarationName &Name, |
1891 | NamedDecl *&ToD, SourceLocation &Loc) { |
1892 | |
1893 | // Import the name of this declaration. |
1894 | if (Error Err = importInto(To&: Name, From: D->getDeclName())) |
1895 | return Err; |
1896 | |
1897 | // Import the location of this declaration. |
1898 | if (Error Err = importInto(Loc, D->getLocation())) |
1899 | return Err; |
1900 | |
1901 | ToD = cast_or_null<NamedDecl>(Val: Importer.GetAlreadyImportedOrNull(D)); |
1902 | if (ToD) |
1903 | if (Error Err = ASTNodeImporter(*this).ImportDefinitionIfNeeded(D, ToD)) |
1904 | return Err; |
1905 | |
1906 | return Error::success(); |
1907 | } |
1908 | |
1909 | Error ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) { |
1910 | if (!FromD) |
1911 | return Error::success(); |
1912 | |
1913 | if (!ToD) |
1914 | if (Error Err = importInto(To&: ToD, From: FromD)) |
1915 | return Err; |
1916 | |
1917 | if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(Val: FromD)) { |
1918 | if (RecordDecl *ToRecord = cast<RecordDecl>(Val: ToD)) { |
1919 | if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && |
1920 | !ToRecord->getDefinition()) { |
1921 | if (Error Err = ImportDefinition(From: FromRecord, To: ToRecord)) |
1922 | return Err; |
1923 | } |
1924 | } |
1925 | return Error::success(); |
1926 | } |
1927 | |
1928 | if (EnumDecl * = dyn_cast<EnumDecl>(Val: FromD)) { |
1929 | if (EnumDecl *ToEnum = cast<EnumDecl>(Val: ToD)) { |
1930 | if (FromEnum->getDefinition() && !ToEnum->getDefinition()) { |
1931 | if (Error Err = ImportDefinition(From: FromEnum, To: ToEnum)) |
1932 | return Err; |
1933 | } |
1934 | } |
1935 | return Error::success(); |
1936 | } |
1937 | |
1938 | return Error::success(); |
1939 | } |
1940 | |
1941 | Error |
1942 | ASTNodeImporter::ImportDeclarationNameLoc( |
1943 | const DeclarationNameInfo &From, DeclarationNameInfo& To) { |
1944 | // NOTE: To.Name and To.Loc are already imported. |
1945 | // We only have to import To.LocInfo. |
1946 | switch (To.getName().getNameKind()) { |
1947 | case DeclarationName::Identifier: |
1948 | case DeclarationName::ObjCZeroArgSelector: |
1949 | case DeclarationName::ObjCOneArgSelector: |
1950 | case DeclarationName::ObjCMultiArgSelector: |
1951 | case DeclarationName::CXXUsingDirective: |
1952 | case DeclarationName::CXXDeductionGuideName: |
1953 | return Error::success(); |
1954 | |
1955 | case DeclarationName::CXXOperatorName: { |
1956 | if (auto ToRangeOrErr = import(From: From.getCXXOperatorNameRange())) |
1957 | To.setCXXOperatorNameRange(*ToRangeOrErr); |
1958 | else |
1959 | return ToRangeOrErr.takeError(); |
1960 | return Error::success(); |
1961 | } |
1962 | case DeclarationName::CXXLiteralOperatorName: { |
1963 | if (ExpectedSLoc LocOrErr = import(From: From.getCXXLiteralOperatorNameLoc())) |
1964 | To.setCXXLiteralOperatorNameLoc(*LocOrErr); |
1965 | else |
1966 | return LocOrErr.takeError(); |
1967 | return Error::success(); |
1968 | } |
1969 | case DeclarationName::CXXConstructorName: |
1970 | case DeclarationName::CXXDestructorName: |
1971 | case DeclarationName::CXXConversionFunctionName: { |
1972 | if (auto ToTInfoOrErr = import(From: From.getNamedTypeInfo())) |
1973 | To.setNamedTypeInfo(*ToTInfoOrErr); |
1974 | else |
1975 | return ToTInfoOrErr.takeError(); |
1976 | return Error::success(); |
1977 | } |
1978 | } |
1979 | llvm_unreachable("Unknown name kind." ); |
1980 | } |
1981 | |
1982 | Error |
1983 | ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) { |
1984 | if (Importer.isMinimalImport() && !ForceImport) { |
1985 | auto ToDCOrErr = Importer.ImportContext(FromDC); |
1986 | return ToDCOrErr.takeError(); |
1987 | } |
1988 | |
1989 | // We use strict error handling in case of records and enums, but not |
1990 | // with e.g. namespaces. |
1991 | // |
1992 | // FIXME Clients of the ASTImporter should be able to choose an |
1993 | // appropriate error handling strategy for their needs. For instance, |
1994 | // they may not want to mark an entire namespace as erroneous merely |
1995 | // because there is an ODR error with two typedefs. As another example, |
1996 | // the client may allow EnumConstantDecls with same names but with |
1997 | // different values in two distinct translation units. |
1998 | ChildErrorHandlingStrategy HandleChildErrors(FromDC); |
1999 | |
2000 | auto MightNeedReordering = [](const Decl *D) { |
2001 | return isa<FieldDecl>(Val: D) || isa<IndirectFieldDecl>(Val: D) || isa<FriendDecl>(Val: D); |
2002 | }; |
2003 | |
2004 | // Import everything that might need reordering first. |
2005 | Error ChildErrors = Error::success(); |
2006 | for (auto *From : FromDC->decls()) { |
2007 | if (!MightNeedReordering(From)) |
2008 | continue; |
2009 | |
2010 | ExpectedDecl ImportedOrErr = import(From); |
2011 | |
2012 | // If we are in the process of ImportDefinition(...) for a RecordDecl we |
2013 | // want to make sure that we are also completing each FieldDecl. There |
2014 | // are currently cases where this does not happen and this is correctness |
2015 | // fix since operations such as code generation will expect this to be so. |
2016 | if (!ImportedOrErr) { |
2017 | HandleChildErrors.handleChildImportResult(ResultErr&: ChildErrors, |
2018 | ChildErr: ImportedOrErr.takeError()); |
2019 | continue; |
2020 | } |
2021 | FieldDecl *FieldFrom = dyn_cast_or_null<FieldDecl>(Val: From); |
2022 | Decl *ImportedDecl = *ImportedOrErr; |
2023 | FieldDecl *FieldTo = dyn_cast_or_null<FieldDecl>(Val: ImportedDecl); |
2024 | if (FieldFrom && FieldTo) { |
2025 | Error Err = ImportFieldDeclDefinition(From: FieldFrom, To: FieldTo); |
2026 | HandleChildErrors.handleChildImportResult(ResultErr&: ChildErrors, ChildErr: std::move(Err)); |
2027 | } |
2028 | } |
2029 | |
2030 | // We reorder declarations in RecordDecls because they may have another order |
2031 | // in the "to" context than they have in the "from" context. This may happen |
2032 | // e.g when we import a class like this: |
2033 | // struct declToImport { |
2034 | // int a = c + b; |
2035 | // int b = 1; |
2036 | // int c = 2; |
2037 | // }; |
2038 | // During the import of `a` we import first the dependencies in sequence, |
2039 | // thus the order would be `c`, `b`, `a`. We will get the normal order by |
2040 | // first removing the already imported members and then adding them in the |
2041 | // order as they appear in the "from" context. |
2042 | // |
2043 | // Keeping field order is vital because it determines structure layout. |
2044 | // |
2045 | // Here and below, we cannot call field_begin() method and its callers on |
2046 | // ToDC if it has an external storage. Calling field_begin() will |
2047 | // automatically load all the fields by calling |
2048 | // LoadFieldsFromExternalStorage(). LoadFieldsFromExternalStorage() would |
2049 | // call ASTImporter::Import(). This is because the ExternalASTSource |
2050 | // interface in LLDB is implemented by the means of the ASTImporter. However, |
2051 | // calling an import at this point would result in an uncontrolled import, we |
2052 | // must avoid that. |
2053 | |
2054 | auto ToDCOrErr = Importer.ImportContext(FromDC); |
2055 | if (!ToDCOrErr) { |
2056 | consumeError(Err: std::move(ChildErrors)); |
2057 | return ToDCOrErr.takeError(); |
2058 | } |
2059 | |
2060 | if (const auto *FromRD = dyn_cast<RecordDecl>(Val: FromDC)) { |
2061 | DeclContext *ToDC = *ToDCOrErr; |
2062 | // Remove all declarations, which may be in wrong order in the |
2063 | // lexical DeclContext and then add them in the proper order. |
2064 | for (auto *D : FromRD->decls()) { |
2065 | if (!MightNeedReordering(D)) |
2066 | continue; |
2067 | |
2068 | assert(D && "DC contains a null decl" ); |
2069 | if (Decl *ToD = Importer.GetAlreadyImportedOrNull(D)) { |
2070 | // Remove only the decls which we successfully imported. |
2071 | assert(ToDC == ToD->getLexicalDeclContext() && ToDC->containsDecl(ToD)); |
2072 | // Remove the decl from its wrong place in the linked list. |
2073 | ToDC->removeDecl(ToD); |
2074 | // Add the decl to the end of the linked list. |
2075 | // This time it will be at the proper place because the enclosing for |
2076 | // loop iterates in the original (good) order of the decls. |
2077 | ToDC->addDeclInternal(ToD); |
2078 | } |
2079 | } |
2080 | } |
2081 | |
2082 | // Import everything else. |
2083 | for (auto *From : FromDC->decls()) { |
2084 | if (MightNeedReordering(From)) |
2085 | continue; |
2086 | |
2087 | ExpectedDecl ImportedOrErr = import(From); |
2088 | if (!ImportedOrErr) |
2089 | HandleChildErrors.handleChildImportResult(ResultErr&: ChildErrors, |
2090 | ChildErr: ImportedOrErr.takeError()); |
2091 | } |
2092 | |
2093 | return ChildErrors; |
2094 | } |
2095 | |
2096 | Error ASTNodeImporter::ImportFieldDeclDefinition(const FieldDecl *From, |
2097 | const FieldDecl *To) { |
2098 | RecordDecl *FromRecordDecl = nullptr; |
2099 | RecordDecl *ToRecordDecl = nullptr; |
2100 | // If we have a field that is an ArrayType we need to check if the array |
2101 | // element is a RecordDecl and if so we need to import the definition. |
2102 | QualType FromType = From->getType(); |
2103 | QualType ToType = To->getType(); |
2104 | if (FromType->isArrayType()) { |
2105 | // getBaseElementTypeUnsafe(...) handles multi-dimensonal arrays for us. |
2106 | FromRecordDecl = FromType->getBaseElementTypeUnsafe()->getAsRecordDecl(); |
2107 | ToRecordDecl = ToType->getBaseElementTypeUnsafe()->getAsRecordDecl(); |
2108 | } |
2109 | |
2110 | if (!FromRecordDecl || !ToRecordDecl) { |
2111 | const RecordType *RecordFrom = FromType->getAs<RecordType>(); |
2112 | const RecordType *RecordTo = ToType->getAs<RecordType>(); |
2113 | |
2114 | if (RecordFrom && RecordTo) { |
2115 | FromRecordDecl = RecordFrom->getDecl(); |
2116 | ToRecordDecl = RecordTo->getDecl(); |
2117 | } |
2118 | } |
2119 | |
2120 | if (FromRecordDecl && ToRecordDecl) { |
2121 | if (FromRecordDecl->isCompleteDefinition() && |
2122 | !ToRecordDecl->isCompleteDefinition()) |
2123 | return ImportDefinition(From: FromRecordDecl, To: ToRecordDecl); |
2124 | } |
2125 | |
2126 | return Error::success(); |
2127 | } |
2128 | |
2129 | Error ASTNodeImporter::ImportDeclContext( |
2130 | Decl *FromD, DeclContext *&ToDC, DeclContext *&ToLexicalDC) { |
2131 | auto ToDCOrErr = Importer.ImportContext(FromDC: FromD->getDeclContext()); |
2132 | if (!ToDCOrErr) |
2133 | return ToDCOrErr.takeError(); |
2134 | ToDC = *ToDCOrErr; |
2135 | |
2136 | if (FromD->getDeclContext() != FromD->getLexicalDeclContext()) { |
2137 | auto ToLexicalDCOrErr = Importer.ImportContext( |
2138 | FromDC: FromD->getLexicalDeclContext()); |
2139 | if (!ToLexicalDCOrErr) |
2140 | return ToLexicalDCOrErr.takeError(); |
2141 | ToLexicalDC = *ToLexicalDCOrErr; |
2142 | } else |
2143 | ToLexicalDC = ToDC; |
2144 | |
2145 | return Error::success(); |
2146 | } |
2147 | |
2148 | Error ASTNodeImporter::ImportImplicitMethods( |
2149 | const CXXRecordDecl *From, CXXRecordDecl *To) { |
2150 | assert(From->isCompleteDefinition() && To->getDefinition() == To && |
2151 | "Import implicit methods to or from non-definition" ); |
2152 | |
2153 | for (CXXMethodDecl *FromM : From->methods()) |
2154 | if (FromM->isImplicit()) { |
2155 | Expected<CXXMethodDecl *> ToMOrErr = import(From: FromM); |
2156 | if (!ToMOrErr) |
2157 | return ToMOrErr.takeError(); |
2158 | } |
2159 | |
2160 | return Error::success(); |
2161 | } |
2162 | |
2163 | static Error setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To, |
2164 | ASTImporter &Importer) { |
2165 | if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) { |
2166 | if (ExpectedDecl ToTypedefOrErr = Importer.Import(FromTypedef)) |
2167 | To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(Val: *ToTypedefOrErr)); |
2168 | else |
2169 | return ToTypedefOrErr.takeError(); |
2170 | } |
2171 | return Error::success(); |
2172 | } |
2173 | |
2174 | Error ASTNodeImporter::ImportDefinition( |
2175 | RecordDecl *From, RecordDecl *To, ImportDefinitionKind Kind) { |
2176 | auto DefinitionCompleter = [To]() { |
2177 | // There are cases in LLDB when we first import a class without its |
2178 | // members. The class will have DefinitionData, but no members. Then, |
2179 | // importDefinition is called from LLDB, which tries to get the members, so |
2180 | // when we get here, the class already has the DefinitionData set, so we |
2181 | // must unset the CompleteDefinition here to be able to complete again the |
2182 | // definition. |
2183 | To->setCompleteDefinition(false); |
2184 | To->completeDefinition(); |
2185 | }; |
2186 | |
2187 | if (To->getDefinition() || To->isBeingDefined()) { |
2188 | if (Kind == IDK_Everything || |
2189 | // In case of lambdas, the class already has a definition ptr set, but |
2190 | // the contained decls are not imported yet. Also, isBeingDefined was |
2191 | // set in CXXRecordDecl::CreateLambda. We must import the contained |
2192 | // decls here and finish the definition. |
2193 | (To->isLambda() && shouldForceImportDeclContext(IDK: Kind))) { |
2194 | if (To->isLambda()) { |
2195 | auto *FromCXXRD = cast<CXXRecordDecl>(Val: From); |
2196 | SmallVector<LambdaCapture, 8> ToCaptures; |
2197 | ToCaptures.reserve(N: FromCXXRD->capture_size()); |
2198 | for (const auto &FromCapture : FromCXXRD->captures()) { |
2199 | if (auto ToCaptureOrErr = import(From: FromCapture)) |
2200 | ToCaptures.push_back(Elt: *ToCaptureOrErr); |
2201 | else |
2202 | return ToCaptureOrErr.takeError(); |
2203 | } |
2204 | cast<CXXRecordDecl>(Val: To)->setCaptures(Context&: Importer.getToContext(), |
2205 | Captures: ToCaptures); |
2206 | } |
2207 | |
2208 | Error Result = ImportDeclContext(From, /*ForceImport=*/true); |
2209 | // Finish the definition of the lambda, set isBeingDefined to false. |
2210 | if (To->isLambda()) |
2211 | DefinitionCompleter(); |
2212 | return Result; |
2213 | } |
2214 | |
2215 | return Error::success(); |
2216 | } |
2217 | |
2218 | To->startDefinition(); |
2219 | // Set the definition to complete even if it is really not complete during |
2220 | // import. Some AST constructs (expressions) require the record layout |
2221 | // to be calculated (see 'clang::computeDependence') at the time they are |
2222 | // constructed. Import of such AST node is possible during import of the |
2223 | // same record, there is no way to have a completely defined record (all |
2224 | // fields imported) at that time without multiple AST import passes. |
2225 | if (!Importer.isMinimalImport()) |
2226 | To->setCompleteDefinition(true); |
2227 | // Complete the definition even if error is returned. |
2228 | // The RecordDecl may be already part of the AST so it is better to |
2229 | // have it in complete state even if something is wrong with it. |
2230 | auto DefinitionCompleterScopeExit = |
2231 | llvm::make_scope_exit(F&: DefinitionCompleter); |
2232 | |
2233 | if (Error Err = setTypedefNameForAnonDecl(From, To, Importer)) |
2234 | return Err; |
2235 | |
2236 | // Add base classes. |
2237 | auto *ToCXX = dyn_cast<CXXRecordDecl>(Val: To); |
2238 | auto *FromCXX = dyn_cast<CXXRecordDecl>(Val: From); |
2239 | if (ToCXX && FromCXX && ToCXX->dataPtr() && FromCXX->dataPtr()) { |
2240 | |
2241 | struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data(); |
2242 | struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data(); |
2243 | |
2244 | #define FIELD(Name, Width, Merge) \ |
2245 | ToData.Name = FromData.Name; |
2246 | #include "clang/AST/CXXRecordDeclDefinitionBits.def" |
2247 | |
2248 | // Copy over the data stored in RecordDeclBits |
2249 | ToCXX->setArgPassingRestrictions(FromCXX->getArgPassingRestrictions()); |
2250 | |
2251 | SmallVector<CXXBaseSpecifier *, 4> Bases; |
2252 | for (const auto &Base1 : FromCXX->bases()) { |
2253 | ExpectedType TyOrErr = import(From: Base1.getType()); |
2254 | if (!TyOrErr) |
2255 | return TyOrErr.takeError(); |
2256 | |
2257 | SourceLocation EllipsisLoc; |
2258 | if (Base1.isPackExpansion()) { |
2259 | if (ExpectedSLoc LocOrErr = import(From: Base1.getEllipsisLoc())) |
2260 | EllipsisLoc = *LocOrErr; |
2261 | else |
2262 | return LocOrErr.takeError(); |
2263 | } |
2264 | |
2265 | // Ensure that we have a definition for the base. |
2266 | if (Error Err = |
2267 | ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl())) |
2268 | return Err; |
2269 | |
2270 | auto RangeOrErr = import(From: Base1.getSourceRange()); |
2271 | if (!RangeOrErr) |
2272 | return RangeOrErr.takeError(); |
2273 | |
2274 | auto TSIOrErr = import(From: Base1.getTypeSourceInfo()); |
2275 | if (!TSIOrErr) |
2276 | return TSIOrErr.takeError(); |
2277 | |
2278 | Bases.push_back( |
2279 | Elt: new (Importer.getToContext()) CXXBaseSpecifier( |
2280 | *RangeOrErr, |
2281 | Base1.isVirtual(), |
2282 | Base1.isBaseOfClass(), |
2283 | Base1.getAccessSpecifierAsWritten(), |
2284 | *TSIOrErr, |
2285 | EllipsisLoc)); |
2286 | } |
2287 | if (!Bases.empty()) |
2288 | ToCXX->setBases(Bases: Bases.data(), NumBases: Bases.size()); |
2289 | } |
2290 | |
2291 | if (shouldForceImportDeclContext(IDK: Kind)) { |
2292 | if (Error Err = ImportDeclContext(From, /*ForceImport=*/true)) |
2293 | return Err; |
2294 | } |
2295 | |
2296 | return Error::success(); |
2297 | } |
2298 | |
2299 | Error ASTNodeImporter::ImportInitializer(VarDecl *From, VarDecl *To) { |
2300 | if (To->getAnyInitializer()) |
2301 | return Error::success(); |
2302 | |
2303 | Expr *FromInit = From->getInit(); |
2304 | if (!FromInit) |
2305 | return Error::success(); |
2306 | |
2307 | ExpectedExpr ToInitOrErr = import(From: FromInit); |
2308 | if (!ToInitOrErr) |
2309 | return ToInitOrErr.takeError(); |
2310 | |
2311 | To->setInit(*ToInitOrErr); |
2312 | if (EvaluatedStmt *FromEval = From->getEvaluatedStmt()) { |
2313 | EvaluatedStmt *ToEval = To->ensureEvaluatedStmt(); |
2314 | ToEval->HasConstantInitialization = FromEval->HasConstantInitialization; |
2315 | ToEval->HasConstantDestruction = FromEval->HasConstantDestruction; |
2316 | // FIXME: Also import the initializer value. |
2317 | } |
2318 | |
2319 | // FIXME: Other bits to merge? |
2320 | return Error::success(); |
2321 | } |
2322 | |
2323 | Error ASTNodeImporter::ImportDefinition( |
2324 | EnumDecl *From, EnumDecl *To, ImportDefinitionKind Kind) { |
2325 | if (To->getDefinition() || To->isBeingDefined()) { |
2326 | if (Kind == IDK_Everything) |
2327 | return ImportDeclContext(From, /*ForceImport=*/true); |
2328 | return Error::success(); |
2329 | } |
2330 | |
2331 | To->startDefinition(); |
2332 | |
2333 | if (Error Err = setTypedefNameForAnonDecl(From, To, Importer)) |
2334 | return Err; |
2335 | |
2336 | ExpectedType ToTypeOrErr = |
2337 | import(From: Importer.getFromContext().getTypeDeclType(From)); |
2338 | if (!ToTypeOrErr) |
2339 | return ToTypeOrErr.takeError(); |
2340 | |
2341 | ExpectedType ToPromotionTypeOrErr = import(From: From->getPromotionType()); |
2342 | if (!ToPromotionTypeOrErr) |
2343 | return ToPromotionTypeOrErr.takeError(); |
2344 | |
2345 | if (shouldForceImportDeclContext(IDK: Kind)) |
2346 | if (Error Err = ImportDeclContext(From, /*ForceImport=*/true)) |
2347 | return Err; |
2348 | |
2349 | // FIXME: we might need to merge the number of positive or negative bits |
2350 | // if the enumerator lists don't match. |
2351 | To->completeDefinition(NewType: *ToTypeOrErr, PromotionType: *ToPromotionTypeOrErr, |
2352 | NumPositiveBits: From->getNumPositiveBits(), |
2353 | NumNegativeBits: From->getNumNegativeBits()); |
2354 | return Error::success(); |
2355 | } |
2356 | |
2357 | Error ASTNodeImporter::ImportTemplateArguments( |
2358 | ArrayRef<TemplateArgument> FromArgs, |
2359 | SmallVectorImpl<TemplateArgument> &ToArgs) { |
2360 | for (const auto &Arg : FromArgs) { |
2361 | if (auto ToOrErr = import(From: Arg)) |
2362 | ToArgs.push_back(Elt: *ToOrErr); |
2363 | else |
2364 | return ToOrErr.takeError(); |
2365 | } |
2366 | |
2367 | return Error::success(); |
2368 | } |
2369 | |
2370 | // FIXME: Do not forget to remove this and use only 'import'. |
2371 | Expected<TemplateArgument> |
2372 | ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) { |
2373 | return import(From); |
2374 | } |
2375 | |
2376 | template <typename InContainerTy> |
2377 | Error ASTNodeImporter::ImportTemplateArgumentListInfo( |
2378 | const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) { |
2379 | for (const auto &FromLoc : Container) { |
2380 | if (auto ToLocOrErr = import(FromLoc)) |
2381 | ToTAInfo.addArgument(Loc: *ToLocOrErr); |
2382 | else |
2383 | return ToLocOrErr.takeError(); |
2384 | } |
2385 | return Error::success(); |
2386 | } |
2387 | |
2388 | static StructuralEquivalenceKind |
2389 | getStructuralEquivalenceKind(const ASTImporter &Importer) { |
2390 | return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal |
2391 | : StructuralEquivalenceKind::Default; |
2392 | } |
2393 | |
2394 | bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain, |
2395 | bool IgnoreTemplateParmDepth) { |
2396 | // Eliminate a potential failure point where we attempt to re-import |
2397 | // something we're trying to import while completing ToRecord. |
2398 | Decl *ToOrigin = Importer.GetOriginalDecl(To); |
2399 | if (ToOrigin) { |
2400 | To = ToOrigin; |
2401 | } |
2402 | |
2403 | StructuralEquivalenceContext Ctx( |
2404 | Importer.getFromContext(), Importer.getToContext(), |
2405 | Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer), |
2406 | /*StrictTypeSpelling=*/false, Complain, /*ErrorOnTagTypeMismatch=*/false, |
2407 | IgnoreTemplateParmDepth); |
2408 | return Ctx.IsEquivalent(D1: From, D2: To); |
2409 | } |
2410 | |
2411 | ExpectedDecl ASTNodeImporter::VisitDecl(Decl *D) { |
2412 | Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) |
2413 | << D->getDeclKindName(); |
2414 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
2415 | } |
2416 | |
2417 | ExpectedDecl ASTNodeImporter::VisitImportDecl(ImportDecl *D) { |
2418 | Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) |
2419 | << D->getDeclKindName(); |
2420 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
2421 | } |
2422 | |
2423 | ExpectedDecl ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) { |
2424 | // Import the context of this declaration. |
2425 | DeclContext *DC, *LexicalDC; |
2426 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) |
2427 | return std::move(Err); |
2428 | |
2429 | // Import the location of this declaration. |
2430 | ExpectedSLoc LocOrErr = import(D->getLocation()); |
2431 | if (!LocOrErr) |
2432 | return LocOrErr.takeError(); |
2433 | |
2434 | EmptyDecl *ToD; |
2435 | if (GetImportedOrCreateDecl(ToD, FromD: D, args&: Importer.getToContext(), args&: DC, args&: *LocOrErr)) |
2436 | return ToD; |
2437 | |
2438 | ToD->setLexicalDeclContext(LexicalDC); |
2439 | LexicalDC->addDeclInternal(ToD); |
2440 | return ToD; |
2441 | } |
2442 | |
2443 | ExpectedDecl ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
2444 | TranslationUnitDecl *ToD = |
2445 | Importer.getToContext().getTranslationUnitDecl(); |
2446 | |
2447 | Importer.MapImported(D, ToD); |
2448 | |
2449 | return ToD; |
2450 | } |
2451 | |
2452 | ExpectedDecl ASTNodeImporter::VisitBindingDecl(BindingDecl *D) { |
2453 | DeclContext *DC, *LexicalDC; |
2454 | DeclarationName Name; |
2455 | SourceLocation Loc; |
2456 | NamedDecl *ToND; |
2457 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToND, Loc)) |
2458 | return std::move(Err); |
2459 | if (ToND) |
2460 | return ToND; |
2461 | |
2462 | BindingDecl *ToD; |
2463 | if (GetImportedOrCreateDecl(ToD, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, |
2464 | args: Name.getAsIdentifierInfo())) |
2465 | return ToD; |
2466 | |
2467 | Error Err = Error::success(); |
2468 | QualType ToType = importChecked(Err, D->getType()); |
2469 | Expr *ToBinding = importChecked(Err, From: D->getBinding()); |
2470 | ValueDecl *ToDecomposedDecl = importChecked(Err, From: D->getDecomposedDecl()); |
2471 | if (Err) |
2472 | return std::move(Err); |
2473 | |
2474 | ToD->setBinding(DeclaredType: ToType, Binding: ToBinding); |
2475 | ToD->setDecomposedDecl(ToDecomposedDecl); |
2476 | addDeclToContexts(D, ToD); |
2477 | |
2478 | return ToD; |
2479 | } |
2480 | |
2481 | ExpectedDecl ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) { |
2482 | ExpectedSLoc LocOrErr = import(D->getLocation()); |
2483 | if (!LocOrErr) |
2484 | return LocOrErr.takeError(); |
2485 | auto ColonLocOrErr = import(From: D->getColonLoc()); |
2486 | if (!ColonLocOrErr) |
2487 | return ColonLocOrErr.takeError(); |
2488 | |
2489 | // Import the context of this declaration. |
2490 | auto DCOrErr = Importer.ImportContext(FromDC: D->getDeclContext()); |
2491 | if (!DCOrErr) |
2492 | return DCOrErr.takeError(); |
2493 | DeclContext *DC = *DCOrErr; |
2494 | |
2495 | AccessSpecDecl *ToD; |
2496 | if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), D->getAccess(), |
2497 | DC, *LocOrErr, *ColonLocOrErr)) |
2498 | return ToD; |
2499 | |
2500 | // Lexical DeclContext and Semantic DeclContext |
2501 | // is always the same for the accessSpec. |
2502 | ToD->setLexicalDeclContext(DC); |
2503 | DC->addDeclInternal(ToD); |
2504 | |
2505 | return ToD; |
2506 | } |
2507 | |
2508 | ExpectedDecl ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) { |
2509 | auto DCOrErr = Importer.ImportContext(FromDC: D->getDeclContext()); |
2510 | if (!DCOrErr) |
2511 | return DCOrErr.takeError(); |
2512 | DeclContext *DC = *DCOrErr; |
2513 | DeclContext *LexicalDC = DC; |
2514 | |
2515 | Error Err = Error::success(); |
2516 | auto ToLocation = importChecked(Err, D->getLocation()); |
2517 | auto ToRParenLoc = importChecked(Err, From: D->getRParenLoc()); |
2518 | auto ToAssertExpr = importChecked(Err, From: D->getAssertExpr()); |
2519 | auto ToMessage = importChecked(Err, From: D->getMessage()); |
2520 | if (Err) |
2521 | return std::move(Err); |
2522 | |
2523 | StaticAssertDecl *ToD; |
2524 | if (GetImportedOrCreateDecl( |
2525 | ToD, D, Importer.getToContext(), DC, ToLocation, ToAssertExpr, ToMessage, |
2526 | ToRParenLoc, D->isFailed())) |
2527 | return ToD; |
2528 | |
2529 | ToD->setLexicalDeclContext(LexicalDC); |
2530 | LexicalDC->addDeclInternal(ToD); |
2531 | return ToD; |
2532 | } |
2533 | |
2534 | ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) { |
2535 | // Import the major distinguishing characteristics of this namespace. |
2536 | DeclContext *DC, *LexicalDC; |
2537 | DeclarationName Name; |
2538 | SourceLocation Loc; |
2539 | NamedDecl *ToD; |
2540 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
2541 | return std::move(Err); |
2542 | if (ToD) |
2543 | return ToD; |
2544 | |
2545 | NamespaceDecl *MergeWithNamespace = nullptr; |
2546 | if (!Name) { |
2547 | // This is an anonymous namespace. Adopt an existing anonymous |
2548 | // namespace if we can. |
2549 | // FIXME: Not testable. |
2550 | if (auto *TU = dyn_cast<TranslationUnitDecl>(Val: DC)) |
2551 | MergeWithNamespace = TU->getAnonymousNamespace(); |
2552 | else |
2553 | MergeWithNamespace = cast<NamespaceDecl>(Val: DC)->getAnonymousNamespace(); |
2554 | } else { |
2555 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
2556 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
2557 | for (auto *FoundDecl : FoundDecls) { |
2558 | if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace)) |
2559 | continue; |
2560 | |
2561 | if (auto *FoundNS = dyn_cast<NamespaceDecl>(Val: FoundDecl)) { |
2562 | MergeWithNamespace = FoundNS; |
2563 | ConflictingDecls.clear(); |
2564 | break; |
2565 | } |
2566 | |
2567 | ConflictingDecls.push_back(Elt: FoundDecl); |
2568 | } |
2569 | |
2570 | if (!ConflictingDecls.empty()) { |
2571 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
2572 | Name, DC, IDNS: Decl::IDNS_Namespace, Decls: ConflictingDecls.data(), |
2573 | NumDecls: ConflictingDecls.size()); |
2574 | if (NameOrErr) |
2575 | Name = NameOrErr.get(); |
2576 | else |
2577 | return NameOrErr.takeError(); |
2578 | } |
2579 | } |
2580 | |
2581 | ExpectedSLoc BeginLocOrErr = import(From: D->getBeginLoc()); |
2582 | if (!BeginLocOrErr) |
2583 | return BeginLocOrErr.takeError(); |
2584 | ExpectedSLoc RBraceLocOrErr = import(From: D->getRBraceLoc()); |
2585 | if (!RBraceLocOrErr) |
2586 | return RBraceLocOrErr.takeError(); |
2587 | |
2588 | // Create the "to" namespace, if needed. |
2589 | NamespaceDecl *ToNamespace = MergeWithNamespace; |
2590 | if (!ToNamespace) { |
2591 | if (GetImportedOrCreateDecl(ToD&: ToNamespace, FromD: D, args&: Importer.getToContext(), args&: DC, |
2592 | args: D->isInline(), args&: *BeginLocOrErr, args&: Loc, |
2593 | args: Name.getAsIdentifierInfo(), |
2594 | /*PrevDecl=*/args: nullptr, args: D->isNested())) |
2595 | return ToNamespace; |
2596 | ToNamespace->setRBraceLoc(*RBraceLocOrErr); |
2597 | ToNamespace->setLexicalDeclContext(LexicalDC); |
2598 | LexicalDC->addDeclInternal(ToNamespace); |
2599 | |
2600 | // If this is an anonymous namespace, register it as the anonymous |
2601 | // namespace within its context. |
2602 | if (!Name) { |
2603 | if (auto *TU = dyn_cast<TranslationUnitDecl>(Val: DC)) |
2604 | TU->setAnonymousNamespace(ToNamespace); |
2605 | else |
2606 | cast<NamespaceDecl>(Val: DC)->setAnonymousNamespace(ToNamespace); |
2607 | } |
2608 | } |
2609 | Importer.MapImported(D, ToNamespace); |
2610 | |
2611 | if (Error Err = ImportDeclContext(D)) |
2612 | return std::move(Err); |
2613 | |
2614 | return ToNamespace; |
2615 | } |
2616 | |
2617 | ExpectedDecl ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
2618 | // Import the major distinguishing characteristics of this namespace. |
2619 | DeclContext *DC, *LexicalDC; |
2620 | DeclarationName Name; |
2621 | SourceLocation Loc; |
2622 | NamedDecl *LookupD; |
2623 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc)) |
2624 | return std::move(Err); |
2625 | if (LookupD) |
2626 | return LookupD; |
2627 | |
2628 | // NOTE: No conflict resolution is done for namespace aliases now. |
2629 | |
2630 | Error Err = Error::success(); |
2631 | auto ToNamespaceLoc = importChecked(Err, From: D->getNamespaceLoc()); |
2632 | auto ToAliasLoc = importChecked(Err, From: D->getAliasLoc()); |
2633 | auto ToQualifierLoc = importChecked(Err, From: D->getQualifierLoc()); |
2634 | auto ToTargetNameLoc = importChecked(Err, From: D->getTargetNameLoc()); |
2635 | auto ToNamespace = importChecked(Err, From: D->getNamespace()); |
2636 | if (Err) |
2637 | return std::move(Err); |
2638 | |
2639 | IdentifierInfo *ToIdentifier = Importer.Import(D->getIdentifier()); |
2640 | |
2641 | NamespaceAliasDecl *ToD; |
2642 | if (GetImportedOrCreateDecl( |
2643 | ToD, FromD: D, args&: Importer.getToContext(), args&: DC, args&: ToNamespaceLoc, args&: ToAliasLoc, |
2644 | args&: ToIdentifier, args&: ToQualifierLoc, args&: ToTargetNameLoc, args&: ToNamespace)) |
2645 | return ToD; |
2646 | |
2647 | ToD->setLexicalDeclContext(LexicalDC); |
2648 | LexicalDC->addDeclInternal(ToD); |
2649 | |
2650 | return ToD; |
2651 | } |
2652 | |
2653 | ExpectedDecl |
2654 | ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) { |
2655 | // Import the major distinguishing characteristics of this typedef. |
2656 | DeclarationName Name; |
2657 | SourceLocation Loc; |
2658 | NamedDecl *ToD; |
2659 | // Do not import the DeclContext, we will import it once the TypedefNameDecl |
2660 | // is created. |
2661 | if (Error Err = ImportDeclParts(D, Name, ToD, Loc)) |
2662 | return std::move(Err); |
2663 | if (ToD) |
2664 | return ToD; |
2665 | |
2666 | DeclContext *DC = cast_or_null<DeclContext>( |
2667 | Importer.GetAlreadyImportedOrNull(FromD: cast<Decl>(D->getDeclContext()))); |
2668 | DeclContext *LexicalDC = |
2669 | cast_or_null<DeclContext>(Importer.GetAlreadyImportedOrNull( |
2670 | FromD: cast<Decl>(D->getLexicalDeclContext()))); |
2671 | |
2672 | // If this typedef is not in block scope, determine whether we've |
2673 | // seen a typedef with the same name (that we can merge with) or any |
2674 | // other entity by that name (which name lookup could conflict with). |
2675 | // Note: Repeated typedefs are not valid in C99: |
2676 | // 'typedef int T; typedef int T;' is invalid |
2677 | // We do not care about this now. |
2678 | if (DC && !DC->isFunctionOrMethod()) { |
2679 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
2680 | unsigned IDNS = Decl::IDNS_Ordinary; |
2681 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
2682 | for (auto *FoundDecl : FoundDecls) { |
2683 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) |
2684 | continue; |
2685 | if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) { |
2686 | if (!hasSameVisibilityContextAndLinkage(FoundTypedef, D)) |
2687 | continue; |
2688 | |
2689 | QualType FromUT = D->getUnderlyingType(); |
2690 | QualType FoundUT = FoundTypedef->getUnderlyingType(); |
2691 | if (Importer.IsStructurallyEquivalent(FromUT, FoundUT)) { |
2692 | // If the underlying declarations are unnamed records these can be |
2693 | // imported as different types. We should create a distinct typedef |
2694 | // node in this case. |
2695 | // If we found an existing underlying type with a record in a |
2696 | // different context (than the imported), this is already reason for |
2697 | // having distinct typedef nodes for these. |
2698 | // Again this can create situation like |
2699 | // 'typedef int T; typedef int T;' but this is hard to avoid without |
2700 | // a rename strategy at import. |
2701 | if (!FromUT.isNull() && !FoundUT.isNull()) { |
2702 | RecordDecl *FromR = FromUT->getAsRecordDecl(); |
2703 | RecordDecl *FoundR = FoundUT->getAsRecordDecl(); |
2704 | if (FromR && FoundR && |
2705 | !hasSameVisibilityContextAndLinkage(FoundR, FromR)) |
2706 | continue; |
2707 | } |
2708 | // If the "From" context has a complete underlying type but we |
2709 | // already have a complete underlying type then return with that. |
2710 | if (!FromUT->isIncompleteType() && !FoundUT->isIncompleteType()) |
2711 | return Importer.MapImported(D, FoundTypedef); |
2712 | // FIXME Handle redecl chain. When you do that make consistent changes |
2713 | // in ASTImporterLookupTable too. |
2714 | } else { |
2715 | ConflictingDecls.push_back(FoundDecl); |
2716 | } |
2717 | } |
2718 | } |
2719 | |
2720 | if (!ConflictingDecls.empty()) { |
2721 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
2722 | Name, DC, IDNS, Decls: ConflictingDecls.data(), NumDecls: ConflictingDecls.size()); |
2723 | if (NameOrErr) |
2724 | Name = NameOrErr.get(); |
2725 | else |
2726 | return NameOrErr.takeError(); |
2727 | } |
2728 | } |
2729 | |
2730 | Error Err = Error::success(); |
2731 | auto ToUnderlyingType = importChecked(Err, From: D->getUnderlyingType()); |
2732 | auto ToTypeSourceInfo = importChecked(Err, From: D->getTypeSourceInfo()); |
2733 | auto ToBeginLoc = importChecked(Err, D->getBeginLoc()); |
2734 | if (Err) |
2735 | return std::move(Err); |
2736 | |
2737 | // Create the new typedef node. |
2738 | // FIXME: ToUnderlyingType is not used. |
2739 | (void)ToUnderlyingType; |
2740 | TypedefNameDecl *ToTypedef; |
2741 | if (IsAlias) { |
2742 | if (GetImportedOrCreateDecl<TypeAliasDecl>( |
2743 | ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc, |
2744 | Name.getAsIdentifierInfo(), ToTypeSourceInfo)) |
2745 | return ToTypedef; |
2746 | } else if (GetImportedOrCreateDecl<TypedefDecl>( |
2747 | ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc, |
2748 | Name.getAsIdentifierInfo(), ToTypeSourceInfo)) |
2749 | return ToTypedef; |
2750 | |
2751 | // Import the DeclContext and set it to the Typedef. |
2752 | if ((Err = ImportDeclContext(D, DC, LexicalDC))) |
2753 | return std::move(Err); |
2754 | ToTypedef->setDeclContext(DC); |
2755 | ToTypedef->setLexicalDeclContext(LexicalDC); |
2756 | // Add to the lookupTable because we could not do that in MapImported. |
2757 | Importer.AddToLookupTable(ToTypedef); |
2758 | |
2759 | ToTypedef->setAccess(D->getAccess()); |
2760 | |
2761 | // Templated declarations should not appear in DeclContext. |
2762 | TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(Val: D) : nullptr; |
2763 | if (!FromAlias || !FromAlias->getDescribedAliasTemplate()) |
2764 | LexicalDC->addDeclInternal(ToTypedef); |
2765 | |
2766 | return ToTypedef; |
2767 | } |
2768 | |
2769 | ExpectedDecl ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) { |
2770 | return VisitTypedefNameDecl(D, /*IsAlias=*/false); |
2771 | } |
2772 | |
2773 | ExpectedDecl ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) { |
2774 | return VisitTypedefNameDecl(D, /*IsAlias=*/true); |
2775 | } |
2776 | |
2777 | ExpectedDecl |
2778 | ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { |
2779 | // Import the major distinguishing characteristics of this typedef. |
2780 | DeclContext *DC, *LexicalDC; |
2781 | DeclarationName Name; |
2782 | SourceLocation Loc; |
2783 | NamedDecl *FoundD; |
2784 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc)) |
2785 | return std::move(Err); |
2786 | if (FoundD) |
2787 | return FoundD; |
2788 | |
2789 | // If this typedef is not in block scope, determine whether we've |
2790 | // seen a typedef with the same name (that we can merge with) or any |
2791 | // other entity by that name (which name lookup could conflict with). |
2792 | if (!DC->isFunctionOrMethod()) { |
2793 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
2794 | unsigned IDNS = Decl::IDNS_Ordinary; |
2795 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
2796 | for (auto *FoundDecl : FoundDecls) { |
2797 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) |
2798 | continue; |
2799 | if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(Val: FoundDecl)) { |
2800 | if (IsStructuralMatch(D, FoundAlias)) |
2801 | return Importer.MapImported(D, FoundAlias); |
2802 | ConflictingDecls.push_back(Elt: FoundDecl); |
2803 | } |
2804 | } |
2805 | |
2806 | if (!ConflictingDecls.empty()) { |
2807 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
2808 | Name, DC, IDNS, Decls: ConflictingDecls.data(), NumDecls: ConflictingDecls.size()); |
2809 | if (NameOrErr) |
2810 | Name = NameOrErr.get(); |
2811 | else |
2812 | return NameOrErr.takeError(); |
2813 | } |
2814 | } |
2815 | |
2816 | Error Err = Error::success(); |
2817 | auto ToTemplateParameters = importChecked(Err, D->getTemplateParameters()); |
2818 | auto ToTemplatedDecl = importChecked(Err, From: D->getTemplatedDecl()); |
2819 | if (Err) |
2820 | return std::move(Err); |
2821 | |
2822 | TypeAliasTemplateDecl *ToAlias; |
2823 | if (GetImportedOrCreateDecl(ToAlias, D, Importer.getToContext(), DC, Loc, |
2824 | Name, ToTemplateParameters, ToTemplatedDecl)) |
2825 | return ToAlias; |
2826 | |
2827 | ToTemplatedDecl->setDescribedAliasTemplate(ToAlias); |
2828 | |
2829 | ToAlias->setAccess(D->getAccess()); |
2830 | ToAlias->setLexicalDeclContext(LexicalDC); |
2831 | LexicalDC->addDeclInternal(ToAlias); |
2832 | if (DC != Importer.getToContext().getTranslationUnitDecl()) |
2833 | updateLookupTableForTemplateParameters(*ToTemplateParameters); |
2834 | return ToAlias; |
2835 | } |
2836 | |
2837 | ExpectedDecl ASTNodeImporter::VisitLabelDecl(LabelDecl *D) { |
2838 | // Import the major distinguishing characteristics of this label. |
2839 | DeclContext *DC, *LexicalDC; |
2840 | DeclarationName Name; |
2841 | SourceLocation Loc; |
2842 | NamedDecl *ToD; |
2843 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
2844 | return std::move(Err); |
2845 | if (ToD) |
2846 | return ToD; |
2847 | |
2848 | assert(LexicalDC->isFunctionOrMethod()); |
2849 | |
2850 | LabelDecl *ToLabel; |
2851 | if (D->isGnuLocal()) { |
2852 | ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc()); |
2853 | if (!BeginLocOrErr) |
2854 | return BeginLocOrErr.takeError(); |
2855 | if (GetImportedOrCreateDecl(ToD&: ToLabel, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, |
2856 | args: Name.getAsIdentifierInfo(), args&: *BeginLocOrErr)) |
2857 | return ToLabel; |
2858 | |
2859 | } else { |
2860 | if (GetImportedOrCreateDecl(ToD&: ToLabel, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, |
2861 | args: Name.getAsIdentifierInfo())) |
2862 | return ToLabel; |
2863 | |
2864 | } |
2865 | |
2866 | Expected<LabelStmt *> ToStmtOrErr = import(From: D->getStmt()); |
2867 | if (!ToStmtOrErr) |
2868 | return ToStmtOrErr.takeError(); |
2869 | |
2870 | ToLabel->setStmt(*ToStmtOrErr); |
2871 | ToLabel->setLexicalDeclContext(LexicalDC); |
2872 | LexicalDC->addDeclInternal(ToLabel); |
2873 | return ToLabel; |
2874 | } |
2875 | |
2876 | ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) { |
2877 | // Import the major distinguishing characteristics of this enum. |
2878 | DeclContext *DC, *LexicalDC; |
2879 | DeclarationName Name; |
2880 | SourceLocation Loc; |
2881 | NamedDecl *ToD; |
2882 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
2883 | return std::move(Err); |
2884 | if (ToD) |
2885 | return ToD; |
2886 | |
2887 | // Figure out what enum name we're looking for. |
2888 | unsigned IDNS = Decl::IDNS_Tag; |
2889 | DeclarationName SearchName = Name; |
2890 | if (!SearchName && D->getTypedefNameForAnonDecl()) { |
2891 | if (Error Err = importInto( |
2892 | SearchName, D->getTypedefNameForAnonDecl()->getDeclName())) |
2893 | return std::move(Err); |
2894 | IDNS = Decl::IDNS_Ordinary; |
2895 | } else if (Importer.getToContext().getLangOpts().CPlusPlus) |
2896 | IDNS |= Decl::IDNS_Ordinary; |
2897 | |
2898 | // We may already have an enum of the same name; try to find and match it. |
2899 | EnumDecl *PrevDecl = nullptr; |
2900 | if (!DC->isFunctionOrMethod() && SearchName) { |
2901 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
2902 | auto FoundDecls = |
2903 | Importer.findDeclsInToCtx(DC, Name: SearchName); |
2904 | for (auto *FoundDecl : FoundDecls) { |
2905 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) |
2906 | continue; |
2907 | |
2908 | if (auto *Typedef = dyn_cast<TypedefNameDecl>(Val: FoundDecl)) { |
2909 | if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) |
2910 | FoundDecl = Tag->getDecl(); |
2911 | } |
2912 | |
2913 | if (auto *FoundEnum = dyn_cast<EnumDecl>(Val: FoundDecl)) { |
2914 | if (!hasSameVisibilityContextAndLinkage(Found: FoundEnum, From: D)) |
2915 | continue; |
2916 | if (IsStructuralMatch(D, FoundEnum)) { |
2917 | EnumDecl *FoundDef = FoundEnum->getDefinition(); |
2918 | if (D->isThisDeclarationADefinition() && FoundDef) |
2919 | return Importer.MapImported(D, FoundDef); |
2920 | PrevDecl = FoundEnum->getMostRecentDecl(); |
2921 | break; |
2922 | } |
2923 | ConflictingDecls.push_back(Elt: FoundDecl); |
2924 | } |
2925 | } |
2926 | |
2927 | if (!ConflictingDecls.empty()) { |
2928 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
2929 | Name: SearchName, DC, IDNS, Decls: ConflictingDecls.data(), |
2930 | NumDecls: ConflictingDecls.size()); |
2931 | if (NameOrErr) |
2932 | Name = NameOrErr.get(); |
2933 | else |
2934 | return NameOrErr.takeError(); |
2935 | } |
2936 | } |
2937 | |
2938 | Error Err = Error::success(); |
2939 | auto ToBeginLoc = importChecked(Err, D->getBeginLoc()); |
2940 | auto ToQualifierLoc = importChecked(Err, D->getQualifierLoc()); |
2941 | auto ToIntegerType = importChecked(Err, From: D->getIntegerType()); |
2942 | auto ToBraceRange = importChecked(Err, D->getBraceRange()); |
2943 | if (Err) |
2944 | return std::move(Err); |
2945 | |
2946 | // Create the enum declaration. |
2947 | EnumDecl *D2; |
2948 | if (GetImportedOrCreateDecl( |
2949 | D2, D, Importer.getToContext(), DC, ToBeginLoc, |
2950 | Loc, Name.getAsIdentifierInfo(), PrevDecl, D->isScoped(), |
2951 | D->isScopedUsingClassTag(), D->isFixed())) |
2952 | return D2; |
2953 | |
2954 | D2->setQualifierInfo(ToQualifierLoc); |
2955 | D2->setIntegerType(ToIntegerType); |
2956 | D2->setBraceRange(ToBraceRange); |
2957 | D2->setAccess(D->getAccess()); |
2958 | D2->setLexicalDeclContext(LexicalDC); |
2959 | addDeclToContexts(D, D2); |
2960 | |
2961 | if (MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo()) { |
2962 | TemplateSpecializationKind SK = MemberInfo->getTemplateSpecializationKind(); |
2963 | EnumDecl *FromInst = D->getInstantiatedFromMemberEnum(); |
2964 | if (Expected<EnumDecl *> ToInstOrErr = import(From: FromInst)) |
2965 | D2->setInstantiationOfMemberEnum(ED: *ToInstOrErr, TSK: SK); |
2966 | else |
2967 | return ToInstOrErr.takeError(); |
2968 | if (ExpectedSLoc POIOrErr = import(From: MemberInfo->getPointOfInstantiation())) |
2969 | D2->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr); |
2970 | else |
2971 | return POIOrErr.takeError(); |
2972 | } |
2973 | |
2974 | // Import the definition |
2975 | if (D->isCompleteDefinition()) |
2976 | if (Error Err = ImportDefinition(From: D, To: D2)) |
2977 | return std::move(Err); |
2978 | |
2979 | return D2; |
2980 | } |
2981 | |
2982 | ExpectedDecl ASTNodeImporter::VisitRecordDecl(RecordDecl *D) { |
2983 | bool IsFriendTemplate = false; |
2984 | if (auto *DCXX = dyn_cast<CXXRecordDecl>(Val: D)) { |
2985 | IsFriendTemplate = |
2986 | DCXX->getDescribedClassTemplate() && |
2987 | DCXX->getDescribedClassTemplate()->getFriendObjectKind() != |
2988 | Decl::FOK_None; |
2989 | } |
2990 | |
2991 | // Import the major distinguishing characteristics of this record. |
2992 | DeclContext *DC = nullptr, *LexicalDC = nullptr; |
2993 | DeclarationName Name; |
2994 | SourceLocation Loc; |
2995 | NamedDecl *ToD = nullptr; |
2996 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
2997 | return std::move(Err); |
2998 | if (ToD) |
2999 | return ToD; |
3000 | |
3001 | // Figure out what structure name we're looking for. |
3002 | unsigned IDNS = Decl::IDNS_Tag; |
3003 | DeclarationName SearchName = Name; |
3004 | if (!SearchName && D->getTypedefNameForAnonDecl()) { |
3005 | if (Error Err = importInto( |
3006 | SearchName, D->getTypedefNameForAnonDecl()->getDeclName())) |
3007 | return std::move(Err); |
3008 | IDNS = Decl::IDNS_Ordinary; |
3009 | } else if (Importer.getToContext().getLangOpts().CPlusPlus) |
3010 | IDNS |= Decl::IDNS_Ordinary | Decl::IDNS_TagFriend; |
3011 | |
3012 | bool IsDependentContext = DC != LexicalDC ? LexicalDC->isDependentContext() |
3013 | : DC->isDependentContext(); |
3014 | bool DependentFriend = IsFriendTemplate && IsDependentContext; |
3015 | |
3016 | // We may already have a record of the same name; try to find and match it. |
3017 | RecordDecl *PrevDecl = nullptr; |
3018 | if (!DependentFriend && !DC->isFunctionOrMethod() && !D->isLambda()) { |
3019 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
3020 | auto FoundDecls = |
3021 | Importer.findDeclsInToCtx(DC, Name: SearchName); |
3022 | if (!FoundDecls.empty()) { |
3023 | // We're going to have to compare D against potentially conflicting Decls, |
3024 | // so complete it. |
3025 | if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition()) |
3026 | D->getASTContext().getExternalSource()->CompleteType(D); |
3027 | } |
3028 | |
3029 | for (auto *FoundDecl : FoundDecls) { |
3030 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) |
3031 | continue; |
3032 | |
3033 | Decl *Found = FoundDecl; |
3034 | if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) { |
3035 | if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) |
3036 | Found = Tag->getDecl(); |
3037 | } |
3038 | |
3039 | if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) { |
3040 | // Do not emit false positive diagnostic in case of unnamed |
3041 | // struct/union and in case of anonymous structs. Would be false |
3042 | // because there may be several anonymous/unnamed structs in a class. |
3043 | // E.g. these are both valid: |
3044 | // struct A { // unnamed structs |
3045 | // struct { struct A *next; } entry0; |
3046 | // struct { struct A *next; } entry1; |
3047 | // }; |
3048 | // struct X { struct { int a; }; struct { int b; }; }; // anon structs |
3049 | if (!SearchName) |
3050 | if (!IsStructuralMatch(From: D, To: FoundRecord, Complain: false)) |
3051 | continue; |
3052 | |
3053 | if (!hasSameVisibilityContextAndLinkage(FoundRecord, D)) |
3054 | continue; |
3055 | |
3056 | if (IsStructuralMatch(From: D, To: FoundRecord)) { |
3057 | RecordDecl *FoundDef = FoundRecord->getDefinition(); |
3058 | if (D->isThisDeclarationADefinition() && FoundDef) { |
3059 | // FIXME: Structural equivalence check should check for same |
3060 | // user-defined methods. |
3061 | Importer.MapImported(D, FoundDef); |
3062 | if (const auto *DCXX = dyn_cast<CXXRecordDecl>(Val: D)) { |
3063 | auto *FoundCXX = dyn_cast<CXXRecordDecl>(Val: FoundDef); |
3064 | assert(FoundCXX && "Record type mismatch" ); |
3065 | |
3066 | if (!Importer.isMinimalImport()) |
3067 | // FoundDef may not have every implicit method that D has |
3068 | // because implicit methods are created only if they are used. |
3069 | if (Error Err = ImportImplicitMethods(From: DCXX, To: FoundCXX)) |
3070 | return std::move(Err); |
3071 | } |
3072 | } |
3073 | PrevDecl = FoundRecord->getMostRecentDecl(); |
3074 | break; |
3075 | } |
3076 | ConflictingDecls.push_back(Elt: FoundDecl); |
3077 | } // kind is RecordDecl |
3078 | } // for |
3079 | |
3080 | if (!ConflictingDecls.empty() && SearchName) { |
3081 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
3082 | Name: SearchName, DC, IDNS, Decls: ConflictingDecls.data(), |
3083 | NumDecls: ConflictingDecls.size()); |
3084 | if (NameOrErr) |
3085 | Name = NameOrErr.get(); |
3086 | else |
3087 | return NameOrErr.takeError(); |
3088 | } |
3089 | } |
3090 | |
3091 | ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc()); |
3092 | if (!BeginLocOrErr) |
3093 | return BeginLocOrErr.takeError(); |
3094 | |
3095 | // Create the record declaration. |
3096 | RecordDecl *D2 = nullptr; |
3097 | CXXRecordDecl *D2CXX = nullptr; |
3098 | if (auto *DCXX = dyn_cast<CXXRecordDecl>(Val: D)) { |
3099 | if (DCXX->isLambda()) { |
3100 | auto TInfoOrErr = import(From: DCXX->getLambdaTypeInfo()); |
3101 | if (!TInfoOrErr) |
3102 | return TInfoOrErr.takeError(); |
3103 | if (GetImportedOrCreateSpecialDecl( |
3104 | ToD&: D2CXX, CreateFun: CXXRecordDecl::CreateLambda, FromD: D, args&: Importer.getToContext(), |
3105 | args&: DC, args&: *TInfoOrErr, args&: Loc, args: DCXX->getLambdaDependencyKind(), |
3106 | args: DCXX->isGenericLambda(), args: DCXX->getLambdaCaptureDefault())) |
3107 | return D2CXX; |
3108 | CXXRecordDecl::LambdaNumbering Numbering = DCXX->getLambdaNumbering(); |
3109 | ExpectedDecl CDeclOrErr = import(From: Numbering.ContextDecl); |
3110 | if (!CDeclOrErr) |
3111 | return CDeclOrErr.takeError(); |
3112 | Numbering.ContextDecl = *CDeclOrErr; |
3113 | D2CXX->setLambdaNumbering(Numbering); |
3114 | } else if (DCXX->isInjectedClassName()) { |
3115 | // We have to be careful to do a similar dance to the one in |
3116 | // Sema::ActOnStartCXXMemberDeclarations |
3117 | const bool DelayTypeCreation = true; |
3118 | if (GetImportedOrCreateDecl( |
3119 | D2CXX, D, Importer.getToContext(), D->getTagKind(), DC, |
3120 | *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(), |
3121 | cast_or_null<CXXRecordDecl>(Val: PrevDecl), DelayTypeCreation)) |
3122 | return D2CXX; |
3123 | Importer.getToContext().getTypeDeclType( |
3124 | D2CXX, dyn_cast<CXXRecordDecl>(Val: DC)); |
3125 | } else { |
3126 | if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(), |
3127 | D->getTagKind(), DC, *BeginLocOrErr, Loc, |
3128 | Name.getAsIdentifierInfo(), |
3129 | cast_or_null<CXXRecordDecl>(Val: PrevDecl))) |
3130 | return D2CXX; |
3131 | } |
3132 | |
3133 | D2 = D2CXX; |
3134 | D2->setAccess(D->getAccess()); |
3135 | D2->setLexicalDeclContext(LexicalDC); |
3136 | addDeclToContexts(D, D2); |
3137 | |
3138 | if (ClassTemplateDecl *FromDescribed = |
3139 | DCXX->getDescribedClassTemplate()) { |
3140 | ClassTemplateDecl *ToDescribed; |
3141 | if (Error Err = importInto(To&: ToDescribed, From: FromDescribed)) |
3142 | return std::move(Err); |
3143 | D2CXX->setDescribedClassTemplate(ToDescribed); |
3144 | if (!DCXX->isInjectedClassName() && !IsFriendTemplate) { |
3145 | // In a record describing a template the type should be an |
3146 | // InjectedClassNameType (see Sema::CheckClassTemplate). Update the |
3147 | // previously set type to the correct value here (ToDescribed is not |
3148 | // available at record create). |
3149 | CXXRecordDecl *Injected = nullptr; |
3150 | for (NamedDecl *Found : D2CXX->noload_lookup(Name)) { |
3151 | auto *Record = dyn_cast<CXXRecordDecl>(Found); |
3152 | if (Record && Record->isInjectedClassName()) { |
3153 | Injected = Record; |
3154 | break; |
3155 | } |
3156 | } |
3157 | // Create an injected type for the whole redecl chain. |
3158 | // The chain may contain an already existing injected type at the start, |
3159 | // if yes this should be reused. We must ensure that only one type |
3160 | // object exists for the injected type (including the injected record |
3161 | // declaration), ASTContext does not check it. |
3162 | SmallVector<Decl *, 2> Redecls = |
3163 | getCanonicalForwardRedeclChain(D2CXX); |
3164 | const Type *FrontTy = |
3165 | cast<CXXRecordDecl>(Val: Redecls.front())->getTypeForDecl(); |
3166 | QualType InjSpec; |
3167 | if (auto *InjTy = FrontTy->getAs<InjectedClassNameType>()) |
3168 | InjSpec = InjTy->getInjectedSpecializationType(); |
3169 | else |
3170 | InjSpec = ToDescribed->getInjectedClassNameSpecialization(); |
3171 | for (auto *R : Redecls) { |
3172 | auto *RI = cast<CXXRecordDecl>(R); |
3173 | if (R != Redecls.front() || |
3174 | !isa<InjectedClassNameType>(RI->getTypeForDecl())) |
3175 | RI->setTypeForDecl(nullptr); |
3176 | // This function tries to get the injected type from getTypeForDecl, |
3177 | // then from the previous declaration if possible. If not, it creates |
3178 | // a new type. |
3179 | Importer.getToContext().getInjectedClassNameType(RI, InjSpec); |
3180 | } |
3181 | // Set the new type for the injected decl too. |
3182 | if (Injected) { |
3183 | Injected->setTypeForDecl(nullptr); |
3184 | // This function will copy the injected type from D2CXX into Injected. |
3185 | // The injected decl does not have a previous decl to copy from. |
3186 | Importer.getToContext().getTypeDeclType(Injected, D2CXX); |
3187 | } |
3188 | } |
3189 | } else if (MemberSpecializationInfo *MemberInfo = |
3190 | DCXX->getMemberSpecializationInfo()) { |
3191 | TemplateSpecializationKind SK = |
3192 | MemberInfo->getTemplateSpecializationKind(); |
3193 | CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass(); |
3194 | |
3195 | if (Expected<CXXRecordDecl *> ToInstOrErr = import(From: FromInst)) |
3196 | D2CXX->setInstantiationOfMemberClass(RD: *ToInstOrErr, TSK: SK); |
3197 | else |
3198 | return ToInstOrErr.takeError(); |
3199 | |
3200 | if (ExpectedSLoc POIOrErr = |
3201 | import(From: MemberInfo->getPointOfInstantiation())) |
3202 | D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation( |
3203 | *POIOrErr); |
3204 | else |
3205 | return POIOrErr.takeError(); |
3206 | } |
3207 | |
3208 | } else { |
3209 | if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), |
3210 | D->getTagKind(), DC, *BeginLocOrErr, Loc, |
3211 | Name.getAsIdentifierInfo(), PrevDecl)) |
3212 | return D2; |
3213 | D2->setLexicalDeclContext(LexicalDC); |
3214 | addDeclToContexts(D, D2); |
3215 | } |
3216 | |
3217 | if (auto BraceRangeOrErr = import(D->getBraceRange())) |
3218 | D2->setBraceRange(*BraceRangeOrErr); |
3219 | else |
3220 | return BraceRangeOrErr.takeError(); |
3221 | if (auto QualifierLocOrErr = import(D->getQualifierLoc())) |
3222 | D2->setQualifierInfo(*QualifierLocOrErr); |
3223 | else |
3224 | return QualifierLocOrErr.takeError(); |
3225 | |
3226 | if (D->isAnonymousStructOrUnion()) |
3227 | D2->setAnonymousStructOrUnion(true); |
3228 | |
3229 | if (D->isCompleteDefinition()) |
3230 | if (Error Err = ImportDefinition(From: D, To: D2, Kind: IDK_Default)) |
3231 | return std::move(Err); |
3232 | |
3233 | return D2; |
3234 | } |
3235 | |
3236 | ExpectedDecl ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) { |
3237 | // Import the major distinguishing characteristics of this enumerator. |
3238 | DeclContext *DC, *LexicalDC; |
3239 | DeclarationName Name; |
3240 | SourceLocation Loc; |
3241 | NamedDecl *ToD; |
3242 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
3243 | return std::move(Err); |
3244 | if (ToD) |
3245 | return ToD; |
3246 | |
3247 | // Determine whether there are any other declarations with the same name and |
3248 | // in the same context. |
3249 | if (!LexicalDC->isFunctionOrMethod()) { |
3250 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
3251 | unsigned IDNS = Decl::IDNS_Ordinary; |
3252 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
3253 | for (auto *FoundDecl : FoundDecls) { |
3254 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) |
3255 | continue; |
3256 | |
3257 | if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(Val: FoundDecl)) { |
3258 | if (IsStructuralMatch(D, FoundEnumConstant)) |
3259 | return Importer.MapImported(D, FoundEnumConstant); |
3260 | ConflictingDecls.push_back(Elt: FoundDecl); |
3261 | } |
3262 | } |
3263 | |
3264 | if (!ConflictingDecls.empty()) { |
3265 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
3266 | Name, DC, IDNS, Decls: ConflictingDecls.data(), NumDecls: ConflictingDecls.size()); |
3267 | if (NameOrErr) |
3268 | Name = NameOrErr.get(); |
3269 | else |
3270 | return NameOrErr.takeError(); |
3271 | } |
3272 | } |
3273 | |
3274 | ExpectedType TypeOrErr = import(D->getType()); |
3275 | if (!TypeOrErr) |
3276 | return TypeOrErr.takeError(); |
3277 | |
3278 | ExpectedExpr InitOrErr = import(From: D->getInitExpr()); |
3279 | if (!InitOrErr) |
3280 | return InitOrErr.takeError(); |
3281 | |
3282 | EnumConstantDecl *ToEnumerator; |
3283 | if (GetImportedOrCreateDecl( |
3284 | ToD&: ToEnumerator, FromD: D, args&: Importer.getToContext(), args: cast<EnumDecl>(Val: DC), args&: Loc, |
3285 | args: Name.getAsIdentifierInfo(), args&: *TypeOrErr, args&: *InitOrErr, args: D->getInitVal())) |
3286 | return ToEnumerator; |
3287 | |
3288 | ToEnumerator->setAccess(D->getAccess()); |
3289 | ToEnumerator->setLexicalDeclContext(LexicalDC); |
3290 | LexicalDC->addDeclInternal(ToEnumerator); |
3291 | return ToEnumerator; |
3292 | } |
3293 | |
3294 | Error ASTNodeImporter::ImportTemplateParameterLists(const DeclaratorDecl *FromD, |
3295 | DeclaratorDecl *ToD) { |
3296 | unsigned int Num = FromD->getNumTemplateParameterLists(); |
3297 | if (Num == 0) |
3298 | return Error::success(); |
3299 | SmallVector<TemplateParameterList *, 2> ToTPLists(Num); |
3300 | for (unsigned int I = 0; I < Num; ++I) |
3301 | if (Expected<TemplateParameterList *> ToTPListOrErr = |
3302 | import(From: FromD->getTemplateParameterList(index: I))) |
3303 | ToTPLists[I] = *ToTPListOrErr; |
3304 | else |
3305 | return ToTPListOrErr.takeError(); |
3306 | ToD->setTemplateParameterListsInfo(Context&: Importer.ToContext, TPLists: ToTPLists); |
3307 | return Error::success(); |
3308 | } |
3309 | |
3310 | Error ASTNodeImporter::ImportTemplateInformation( |
3311 | FunctionDecl *FromFD, FunctionDecl *ToFD) { |
3312 | switch (FromFD->getTemplatedKind()) { |
3313 | case FunctionDecl::TK_NonTemplate: |
3314 | case FunctionDecl::TK_FunctionTemplate: |
3315 | return Error::success(); |
3316 | |
3317 | case FunctionDecl::TK_DependentNonTemplate: |
3318 | if (Expected<FunctionDecl *> InstFDOrErr = |
3319 | import(From: FromFD->getInstantiatedFromDecl())) |
3320 | ToFD->setInstantiatedFromDecl(*InstFDOrErr); |
3321 | return Error::success(); |
3322 | case FunctionDecl::TK_MemberSpecialization: { |
3323 | TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind(); |
3324 | |
3325 | if (Expected<FunctionDecl *> InstFDOrErr = |
3326 | import(From: FromFD->getInstantiatedFromMemberFunction())) |
3327 | ToFD->setInstantiationOfMemberFunction(FD: *InstFDOrErr, TSK); |
3328 | else |
3329 | return InstFDOrErr.takeError(); |
3330 | |
3331 | if (ExpectedSLoc POIOrErr = import( |
3332 | From: FromFD->getMemberSpecializationInfo()->getPointOfInstantiation())) |
3333 | ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr); |
3334 | else |
3335 | return POIOrErr.takeError(); |
3336 | |
3337 | return Error::success(); |
3338 | } |
3339 | |
3340 | case FunctionDecl::TK_FunctionTemplateSpecialization: { |
3341 | auto FunctionAndArgsOrErr = |
3342 | ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD); |
3343 | if (!FunctionAndArgsOrErr) |
3344 | return FunctionAndArgsOrErr.takeError(); |
3345 | |
3346 | TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy( |
3347 | Context&: Importer.getToContext(), Args: std::get<1>(*FunctionAndArgsOrErr)); |
3348 | |
3349 | auto *FTSInfo = FromFD->getTemplateSpecializationInfo(); |
3350 | TemplateArgumentListInfo ToTAInfo; |
3351 | const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten; |
3352 | if (FromTAArgsAsWritten) |
3353 | if (Error Err = ImportTemplateArgumentListInfo( |
3354 | Container: *FromTAArgsAsWritten, ToTAInfo)) |
3355 | return Err; |
3356 | |
3357 | ExpectedSLoc POIOrErr = import(From: FTSInfo->getPointOfInstantiation()); |
3358 | if (!POIOrErr) |
3359 | return POIOrErr.takeError(); |
3360 | |
3361 | if (Error Err = ImportTemplateParameterLists(FromFD, ToFD)) |
3362 | return Err; |
3363 | |
3364 | TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind(); |
3365 | ToFD->setFunctionTemplateSpecialization( |
3366 | std::get<0>(*FunctionAndArgsOrErr), ToTAList, /* InsertPos= */ nullptr, |
3367 | TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, *POIOrErr); |
3368 | return Error::success(); |
3369 | } |
3370 | |
3371 | case FunctionDecl::TK_DependentFunctionTemplateSpecialization: { |
3372 | auto *FromInfo = FromFD->getDependentSpecializationInfo(); |
3373 | UnresolvedSet<8> Candidates; |
3374 | for (FunctionTemplateDecl *FTD : FromInfo->getCandidates()) { |
3375 | if (Expected<FunctionTemplateDecl *> ToFTDOrErr = import(From: FTD)) |
3376 | Candidates.addDecl(*ToFTDOrErr); |
3377 | else |
3378 | return ToFTDOrErr.takeError(); |
3379 | } |
3380 | |
3381 | // Import TemplateArgumentListInfo. |
3382 | TemplateArgumentListInfo ToTAInfo; |
3383 | const auto *FromTAArgsAsWritten = FromInfo->TemplateArgumentsAsWritten; |
3384 | if (FromTAArgsAsWritten) |
3385 | if (Error Err = |
3386 | ImportTemplateArgumentListInfo(Container: *FromTAArgsAsWritten, ToTAInfo)) |
3387 | return Err; |
3388 | |
3389 | ToFD->setDependentTemplateSpecialization( |
3390 | Context&: Importer.getToContext(), Templates: Candidates, |
3391 | TemplateArgs: FromTAArgsAsWritten ? &ToTAInfo : nullptr); |
3392 | return Error::success(); |
3393 | } |
3394 | } |
3395 | llvm_unreachable("All cases should be covered!" ); |
3396 | } |
3397 | |
3398 | Expected<FunctionDecl *> |
3399 | ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) { |
3400 | auto FunctionAndArgsOrErr = |
3401 | ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD); |
3402 | if (!FunctionAndArgsOrErr) |
3403 | return FunctionAndArgsOrErr.takeError(); |
3404 | |
3405 | FunctionTemplateDecl *Template; |
3406 | TemplateArgsTy ToTemplArgs; |
3407 | std::tie(args&: Template, args&: ToTemplArgs) = *FunctionAndArgsOrErr; |
3408 | void *InsertPos = nullptr; |
3409 | auto *FoundSpec = Template->findSpecialization(Args: ToTemplArgs, InsertPos); |
3410 | return FoundSpec; |
3411 | } |
3412 | |
3413 | Error ASTNodeImporter::ImportFunctionDeclBody(FunctionDecl *FromFD, |
3414 | FunctionDecl *ToFD) { |
3415 | if (Stmt *FromBody = FromFD->getBody()) { |
3416 | if (ExpectedStmt ToBodyOrErr = import(From: FromBody)) |
3417 | ToFD->setBody(*ToBodyOrErr); |
3418 | else |
3419 | return ToBodyOrErr.takeError(); |
3420 | } |
3421 | return Error::success(); |
3422 | } |
3423 | |
3424 | // Returns true if the given D has a DeclContext up to the TranslationUnitDecl |
3425 | // which is equal to the given DC, or D is equal to DC. |
3426 | static bool isAncestorDeclContextOf(const DeclContext *DC, const Decl *D) { |
3427 | const DeclContext *DCi = dyn_cast<DeclContext>(Val: D); |
3428 | if (!DCi) |
3429 | DCi = D->getDeclContext(); |
3430 | assert(DCi && "Declaration should have a context" ); |
3431 | while (DCi != D->getTranslationUnitDecl()) { |
3432 | if (DCi == DC) |
3433 | return true; |
3434 | DCi = DCi->getParent(); |
3435 | } |
3436 | return false; |
3437 | } |
3438 | |
3439 | // Check if there is a declaration that has 'DC' as parent context and is |
3440 | // referenced from statement 'S' or one of its children. The search is done in |
3441 | // BFS order through children of 'S'. |
3442 | static bool isAncestorDeclContextOf(const DeclContext *DC, const Stmt *S) { |
3443 | SmallVector<const Stmt *> ToProcess; |
3444 | ToProcess.push_back(Elt: S); |
3445 | while (!ToProcess.empty()) { |
3446 | const Stmt *CurrentS = ToProcess.pop_back_val(); |
3447 | ToProcess.append(in_start: CurrentS->child_begin(), in_end: CurrentS->child_end()); |
3448 | if (const auto *DeclRef = dyn_cast<DeclRefExpr>(Val: CurrentS)) { |
3449 | if (const Decl *D = DeclRef->getDecl()) |
3450 | if (isAncestorDeclContextOf(DC, D)) |
3451 | return true; |
3452 | } else if (const auto *E = |
3453 | dyn_cast_or_null<SubstNonTypeTemplateParmExpr>(Val: CurrentS)) { |
3454 | if (const Decl *D = E->getAssociatedDecl()) |
3455 | if (isAncestorDeclContextOf(DC, D)) |
3456 | return true; |
3457 | } |
3458 | } |
3459 | return false; |
3460 | } |
3461 | |
3462 | namespace { |
3463 | /// Check if a type has any reference to a declaration that is inside the body |
3464 | /// of a function. |
3465 | /// The \c CheckType(QualType) function should be used to determine |
3466 | /// this property. |
3467 | /// |
3468 | /// The type visitor visits one type object only (not recursive). |
3469 | /// To find all referenced declarations we must discover all type objects until |
3470 | /// the canonical type is reached (walk over typedef and similar objects). This |
3471 | /// is done by loop over all "sugar" type objects. For every such type we must |
3472 | /// check all declarations that are referenced from it. For this check the |
3473 | /// visitor is used. In the visit functions all referenced declarations except |
3474 | /// the one that follows in the sugar chain (if any) must be checked. For this |
3475 | /// check the same visitor is re-used (it has no state-dependent data). |
3476 | /// |
3477 | /// The visit functions have 3 possible return values: |
3478 | /// - True, found a declaration inside \c ParentDC. |
3479 | /// - False, found declarations only outside \c ParentDC and it is not possible |
3480 | /// to find more declarations (the "sugar" chain does not continue). |
3481 | /// - Empty optional value, found no declarations or only outside \c ParentDC, |
3482 | /// but it is possible to find more declarations in the type "sugar" chain. |
3483 | /// The loop over the "sugar" types can be implemented by using type visit |
3484 | /// functions only (call \c CheckType with the desugared type). With the current |
3485 | /// solution no visit function is needed if the type has only a desugared type |
3486 | /// as data. |
3487 | class IsTypeDeclaredInsideVisitor |
3488 | : public TypeVisitor<IsTypeDeclaredInsideVisitor, std::optional<bool>> { |
3489 | public: |
3490 | IsTypeDeclaredInsideVisitor(const FunctionDecl *ParentDC) |
3491 | : ParentDC(ParentDC) {} |
3492 | |
3493 | bool CheckType(QualType T) { |
3494 | // Check the chain of "sugar" types. |
3495 | // The "sugar" types are typedef or similar types that have the same |
3496 | // canonical type. |
3497 | if (std::optional<bool> Res = Visit(T: T.getTypePtr())) |
3498 | return *Res; |
3499 | QualType DsT = |
3500 | T.getSingleStepDesugaredType(Context: ParentDC->getParentASTContext()); |
3501 | while (DsT != T) { |
3502 | if (std::optional<bool> Res = Visit(T: DsT.getTypePtr())) |
3503 | return *Res; |
3504 | T = DsT; |
3505 | DsT = T.getSingleStepDesugaredType(Context: ParentDC->getParentASTContext()); |
3506 | } |
3507 | return false; |
3508 | } |
3509 | |
3510 | std::optional<bool> VisitTagType(const TagType *T) { |
3511 | if (auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(Val: T->getDecl())) |
3512 | for (const auto &Arg : Spec->getTemplateArgs().asArray()) |
3513 | if (checkTemplateArgument(Arg)) |
3514 | return true; |
3515 | return isAncestorDeclContextOf(ParentDC, T->getDecl()); |
3516 | } |
3517 | |
3518 | std::optional<bool> VisitPointerType(const PointerType *T) { |
3519 | return CheckType(T: T->getPointeeType()); |
3520 | } |
3521 | |
3522 | std::optional<bool> VisitReferenceType(const ReferenceType *T) { |
3523 | return CheckType(T: T->getPointeeTypeAsWritten()); |
3524 | } |
3525 | |
3526 | std::optional<bool> VisitTypedefType(const TypedefType *T) { |
3527 | const TypedefNameDecl *TD = T->getDecl(); |
3528 | assert(TD); |
3529 | return isAncestorDeclContextOf(ParentDC, TD); |
3530 | } |
3531 | |
3532 | std::optional<bool> VisitUsingType(const UsingType *T) { |
3533 | if (T->getFoundDecl() && |
3534 | isAncestorDeclContextOf(ParentDC, T->getFoundDecl())) |
3535 | return true; |
3536 | |
3537 | return {}; |
3538 | } |
3539 | |
3540 | std::optional<bool> |
3541 | VisitTemplateSpecializationType(const TemplateSpecializationType *T) { |
3542 | for (const auto &Arg : T->template_arguments()) |
3543 | if (checkTemplateArgument(Arg)) |
3544 | return true; |
3545 | // This type is a "sugar" to a record type, it can have a desugared type. |
3546 | return {}; |
3547 | } |
3548 | |
3549 | std::optional<bool> |
3550 | VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
3551 | // The "associated declaration" can be the same as ParentDC. |
3552 | if (isAncestorDeclContextOf(DC: ParentDC, D: T->getAssociatedDecl())) |
3553 | return true; |
3554 | return {}; |
3555 | } |
3556 | |
3557 | std::optional<bool> VisitConstantArrayType(const ConstantArrayType *T) { |
3558 | if (T->getSizeExpr() && isAncestorDeclContextOf(ParentDC, T->getSizeExpr())) |
3559 | return true; |
3560 | |
3561 | return CheckType(T: T->getElementType()); |
3562 | } |
3563 | |
3564 | std::optional<bool> VisitVariableArrayType(const VariableArrayType *T) { |
3565 | llvm_unreachable( |
3566 | "Variable array should not occur in deduced return type of a function" ); |
3567 | } |
3568 | |
3569 | std::optional<bool> VisitIncompleteArrayType(const IncompleteArrayType *T) { |
3570 | llvm_unreachable("Incomplete array should not occur in deduced return type " |
3571 | "of a function" ); |
3572 | } |
3573 | |
3574 | std::optional<bool> VisitDependentArrayType(const IncompleteArrayType *T) { |
3575 | llvm_unreachable("Dependent array should not occur in deduced return type " |
3576 | "of a function" ); |
3577 | } |
3578 | |
3579 | private: |
3580 | const DeclContext *const ParentDC; |
3581 | |
3582 | bool checkTemplateArgument(const TemplateArgument &Arg) { |
3583 | switch (Arg.getKind()) { |
3584 | case TemplateArgument::Null: |
3585 | return false; |
3586 | case TemplateArgument::Integral: |
3587 | return CheckType(T: Arg.getIntegralType()); |
3588 | case TemplateArgument::Type: |
3589 | return CheckType(T: Arg.getAsType()); |
3590 | case TemplateArgument::Expression: |
3591 | return isAncestorDeclContextOf(ParentDC, Arg.getAsExpr()); |
3592 | case TemplateArgument::Declaration: |
3593 | // FIXME: The declaration in this case is not allowed to be in a function? |
3594 | return isAncestorDeclContextOf(ParentDC, Arg.getAsDecl()); |
3595 | case TemplateArgument::NullPtr: |
3596 | // FIXME: The type is not allowed to be in the function? |
3597 | return CheckType(T: Arg.getNullPtrType()); |
3598 | case TemplateArgument::StructuralValue: |
3599 | return CheckType(T: Arg.getStructuralValueType()); |
3600 | case TemplateArgument::Pack: |
3601 | for (const auto &PackArg : Arg.getPackAsArray()) |
3602 | if (checkTemplateArgument(Arg: PackArg)) |
3603 | return true; |
3604 | return false; |
3605 | case TemplateArgument::Template: |
3606 | // Templates can not be defined locally in functions. |
3607 | // A template passed as argument can be not in ParentDC. |
3608 | return false; |
3609 | case TemplateArgument::TemplateExpansion: |
3610 | // Templates can not be defined locally in functions. |
3611 | // A template passed as argument can be not in ParentDC. |
3612 | return false; |
3613 | } |
3614 | llvm_unreachable("Unknown TemplateArgument::ArgKind enum" ); |
3615 | }; |
3616 | }; |
3617 | } // namespace |
3618 | |
3619 | /// This function checks if the function has 'auto' return type that contains |
3620 | /// a reference (in any way) to a declaration inside the same function. |
3621 | bool ASTNodeImporter::hasAutoReturnTypeDeclaredInside(FunctionDecl *D) { |
3622 | QualType FromTy = D->getType(); |
3623 | const auto *FromFPT = FromTy->getAs<FunctionProtoType>(); |
3624 | assert(FromFPT && "Must be called on FunctionProtoType" ); |
3625 | |
3626 | QualType RetT = FromFPT->getReturnType(); |
3627 | if (isa<AutoType>(Val: RetT.getTypePtr())) { |
3628 | FunctionDecl *Def = D->getDefinition(); |
3629 | IsTypeDeclaredInsideVisitor Visitor(Def ? Def : D); |
3630 | return Visitor.CheckType(T: RetT); |
3631 | } |
3632 | |
3633 | return false; |
3634 | } |
3635 | |
3636 | ExplicitSpecifier |
3637 | ASTNodeImporter::importExplicitSpecifier(Error &Err, ExplicitSpecifier ESpec) { |
3638 | Expr *ExplicitExpr = ESpec.getExpr(); |
3639 | if (ExplicitExpr) |
3640 | ExplicitExpr = importChecked(Err, From: ESpec.getExpr()); |
3641 | return ExplicitSpecifier(ExplicitExpr, ESpec.getKind()); |
3642 | } |
3643 | |
3644 | ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) { |
3645 | |
3646 | SmallVector<Decl *, 2> Redecls = getCanonicalForwardRedeclChain(D); |
3647 | auto RedeclIt = Redecls.begin(); |
3648 | // Import the first part of the decl chain. I.e. import all previous |
3649 | // declarations starting from the canonical decl. |
3650 | for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) { |
3651 | ExpectedDecl ToRedeclOrErr = import(*RedeclIt); |
3652 | if (!ToRedeclOrErr) |
3653 | return ToRedeclOrErr.takeError(); |
3654 | } |
3655 | assert(*RedeclIt == D); |
3656 | |
3657 | // Import the major distinguishing characteristics of this function. |
3658 | DeclContext *DC, *LexicalDC; |
3659 | DeclarationName Name; |
3660 | SourceLocation Loc; |
3661 | NamedDecl *ToD; |
3662 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
3663 | return std::move(Err); |
3664 | if (ToD) |
3665 | return ToD; |
3666 | |
3667 | FunctionDecl *FoundByLookup = nullptr; |
3668 | FunctionTemplateDecl *FromFT = D->getDescribedFunctionTemplate(); |
3669 | |
3670 | // If this is a function template specialization, then try to find the same |
3671 | // existing specialization in the "to" context. The lookup below will not |
3672 | // find any specialization, but would find the primary template; thus, we |
3673 | // have to skip normal lookup in case of specializations. |
3674 | // FIXME handle member function templates (TK_MemberSpecialization) similarly? |
3675 | if (D->getTemplatedKind() == |
3676 | FunctionDecl::TK_FunctionTemplateSpecialization) { |
3677 | auto FoundFunctionOrErr = FindFunctionTemplateSpecialization(FromFD: D); |
3678 | if (!FoundFunctionOrErr) |
3679 | return FoundFunctionOrErr.takeError(); |
3680 | if (FunctionDecl *FoundFunction = *FoundFunctionOrErr) { |
3681 | if (Decl *Def = FindAndMapDefinition(D, FoundFunction)) |
3682 | return Def; |
3683 | FoundByLookup = FoundFunction; |
3684 | } |
3685 | } |
3686 | // Try to find a function in our own ("to") context with the same name, same |
3687 | // type, and in the same context as the function we're importing. |
3688 | else if (!LexicalDC->isFunctionOrMethod()) { |
3689 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
3690 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend; |
3691 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
3692 | for (auto *FoundDecl : FoundDecls) { |
3693 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) |
3694 | continue; |
3695 | |
3696 | if (auto *FoundFunction = dyn_cast<FunctionDecl>(Val: FoundDecl)) { |
3697 | if (!hasSameVisibilityContextAndLinkage(Found: FoundFunction, From: D)) |
3698 | continue; |
3699 | |
3700 | if (IsStructuralMatch(D, FoundFunction)) { |
3701 | if (Decl *Def = FindAndMapDefinition(D, FoundFunction)) |
3702 | return Def; |
3703 | FoundByLookup = FoundFunction; |
3704 | break; |
3705 | } |
3706 | // FIXME: Check for overloading more carefully, e.g., by boosting |
3707 | // Sema::IsOverload out to the AST library. |
3708 | |
3709 | // Function overloading is okay in C++. |
3710 | if (Importer.getToContext().getLangOpts().CPlusPlus) |
3711 | continue; |
3712 | |
3713 | // Complain about inconsistent function types. |
3714 | Importer.ToDiag(Loc, diag::warn_odr_function_type_inconsistent) |
3715 | << Name << D->getType() << FoundFunction->getType(); |
3716 | Importer.ToDiag(FoundFunction->getLocation(), diag::note_odr_value_here) |
3717 | << FoundFunction->getType(); |
3718 | ConflictingDecls.push_back(Elt: FoundDecl); |
3719 | } |
3720 | } |
3721 | |
3722 | if (!ConflictingDecls.empty()) { |
3723 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
3724 | Name, DC, IDNS, Decls: ConflictingDecls.data(), NumDecls: ConflictingDecls.size()); |
3725 | if (NameOrErr) |
3726 | Name = NameOrErr.get(); |
3727 | else |
3728 | return NameOrErr.takeError(); |
3729 | } |
3730 | } |
3731 | |
3732 | // We do not allow more than one in-class declaration of a function. This is |
3733 | // because AST clients like VTableBuilder asserts on this. VTableBuilder |
3734 | // assumes there is only one in-class declaration. Building a redecl |
3735 | // chain would result in more than one in-class declaration for |
3736 | // overrides (even if they are part of the same redecl chain inside the |
3737 | // derived class.) |
3738 | if (FoundByLookup) { |
3739 | if (isa<CXXMethodDecl>(Val: FoundByLookup)) { |
3740 | if (D->getLexicalDeclContext() == D->getDeclContext()) { |
3741 | if (!D->doesThisDeclarationHaveABody()) { |
3742 | if (FunctionTemplateDecl *DescribedD = |
3743 | D->getDescribedFunctionTemplate()) { |
3744 | // Handle a "templated" function together with its described |
3745 | // template. This avoids need for a similar check at import of the |
3746 | // described template. |
3747 | assert(FoundByLookup->getDescribedFunctionTemplate() && |
3748 | "Templated function mapped to non-templated?" ); |
3749 | Importer.MapImported(DescribedD, |
3750 | FoundByLookup->getDescribedFunctionTemplate()); |
3751 | } |
3752 | return Importer.MapImported(D, FoundByLookup); |
3753 | } else { |
3754 | // Let's continue and build up the redecl chain in this case. |
3755 | // FIXME Merge the functions into one decl. |
3756 | } |
3757 | } |
3758 | } |
3759 | } |
3760 | |
3761 | DeclarationNameInfo NameInfo(Name, Loc); |
3762 | // Import additional name location/type info. |
3763 | if (Error Err = ImportDeclarationNameLoc(From: D->getNameInfo(), To&: NameInfo)) |
3764 | return std::move(Err); |
3765 | |
3766 | QualType FromTy = D->getType(); |
3767 | TypeSourceInfo *FromTSI = D->getTypeSourceInfo(); |
3768 | // Set to true if we do not import the type of the function as is. There are |
3769 | // cases when the original type would result in an infinite recursion during |
3770 | // the import. To avoid an infinite recursion when importing, we create the |
3771 | // FunctionDecl with a simplified function type and update it only after the |
3772 | // relevant AST nodes are already imported. |
3773 | // The type is related to TypeSourceInfo (it references the type), so we must |
3774 | // do the same with TypeSourceInfo. |
3775 | bool UsedDifferentProtoType = false; |
3776 | if (const auto *FromFPT = FromTy->getAs<FunctionProtoType>()) { |
3777 | QualType FromReturnTy = FromFPT->getReturnType(); |
3778 | // Functions with auto return type may define a struct inside their body |
3779 | // and the return type could refer to that struct. |
3780 | // E.g.: auto foo() { struct X{}; return X(); } |
3781 | // To avoid an infinite recursion when importing, create the FunctionDecl |
3782 | // with a simplified return type. |
3783 | if (hasAutoReturnTypeDeclaredInside(D)) { |
3784 | FromReturnTy = Importer.getFromContext().VoidTy; |
3785 | UsedDifferentProtoType = true; |
3786 | } |
3787 | FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo(); |
3788 | // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the |
3789 | // FunctionDecl that we are importing the FunctionProtoType for. |
3790 | // To avoid an infinite recursion when importing, create the FunctionDecl |
3791 | // with a simplified function type. |
3792 | if (FromEPI.ExceptionSpec.SourceDecl || |
3793 | FromEPI.ExceptionSpec.SourceTemplate || |
3794 | FromEPI.ExceptionSpec.NoexceptExpr) { |
3795 | FunctionProtoType::ExtProtoInfo DefaultEPI; |
3796 | FromEPI = DefaultEPI; |
3797 | UsedDifferentProtoType = true; |
3798 | } |
3799 | FromTy = Importer.getFromContext().getFunctionType( |
3800 | ResultTy: FromReturnTy, Args: FromFPT->getParamTypes(), EPI: FromEPI); |
3801 | FromTSI = Importer.getFromContext().getTrivialTypeSourceInfo( |
3802 | T: FromTy, Loc: D->getBeginLoc()); |
3803 | } |
3804 | |
3805 | Error Err = Error::success(); |
3806 | auto T = importChecked(Err, From: FromTy); |
3807 | auto TInfo = importChecked(Err, From: FromTSI); |
3808 | auto ToInnerLocStart = importChecked(Err, D->getInnerLocStart()); |
3809 | auto ToEndLoc = importChecked(Err, D->getEndLoc()); |
3810 | auto ToDefaultLoc = importChecked(Err, From: D->getDefaultLoc()); |
3811 | auto ToQualifierLoc = importChecked(Err, D->getQualifierLoc()); |
3812 | auto TrailingRequiresClause = |
3813 | importChecked(Err, D->getTrailingRequiresClause()); |
3814 | if (Err) |
3815 | return std::move(Err); |
3816 | |
3817 | // Import the function parameters. |
3818 | SmallVector<ParmVarDecl *, 8> Parameters; |
3819 | for (auto *P : D->parameters()) { |
3820 | if (Expected<ParmVarDecl *> ToPOrErr = import(From: P)) |
3821 | Parameters.push_back(Elt: *ToPOrErr); |
3822 | else |
3823 | return ToPOrErr.takeError(); |
3824 | } |
3825 | |
3826 | // Create the imported function. |
3827 | FunctionDecl *ToFunction = nullptr; |
3828 | if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(Val: D)) { |
3829 | ExplicitSpecifier ESpec = |
3830 | importExplicitSpecifier(Err, ESpec: FromConstructor->getExplicitSpecifier()); |
3831 | if (Err) |
3832 | return std::move(Err); |
3833 | auto ToInheritedConstructor = InheritedConstructor(); |
3834 | if (FromConstructor->isInheritingConstructor()) { |
3835 | Expected<InheritedConstructor> ImportedInheritedCtor = |
3836 | import(From: FromConstructor->getInheritedConstructor()); |
3837 | if (!ImportedInheritedCtor) |
3838 | return ImportedInheritedCtor.takeError(); |
3839 | ToInheritedConstructor = *ImportedInheritedCtor; |
3840 | } |
3841 | if (GetImportedOrCreateDecl<CXXConstructorDecl>( |
3842 | ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(Val: DC), |
3843 | ToInnerLocStart, NameInfo, T, TInfo, ESpec, D->UsesFPIntrin(), |
3844 | D->isInlineSpecified(), D->isImplicit(), D->getConstexprKind(), |
3845 | ToInheritedConstructor, TrailingRequiresClause)) |
3846 | return ToFunction; |
3847 | } else if (CXXDestructorDecl *FromDtor = dyn_cast<CXXDestructorDecl>(Val: D)) { |
3848 | |
3849 | Error Err = Error::success(); |
3850 | auto ToOperatorDelete = importChecked( |
3851 | Err, From: const_cast<FunctionDecl *>(FromDtor->getOperatorDelete())); |
3852 | auto ToThisArg = importChecked(Err, From: FromDtor->getOperatorDeleteThisArg()); |
3853 | if (Err) |
3854 | return std::move(Err); |
3855 | |
3856 | if (GetImportedOrCreateDecl<CXXDestructorDecl>( |
3857 | ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(Val: DC), |
3858 | ToInnerLocStart, NameInfo, T, TInfo, D->UsesFPIntrin(), |
3859 | D->isInlineSpecified(), D->isImplicit(), D->getConstexprKind(), |
3860 | TrailingRequiresClause)) |
3861 | return ToFunction; |
3862 | |
3863 | CXXDestructorDecl *ToDtor = cast<CXXDestructorDecl>(Val: ToFunction); |
3864 | |
3865 | ToDtor->setOperatorDelete(OD: ToOperatorDelete, ThisArg: ToThisArg); |
3866 | } else if (CXXConversionDecl *FromConversion = |
3867 | dyn_cast<CXXConversionDecl>(Val: D)) { |
3868 | ExplicitSpecifier ESpec = |
3869 | importExplicitSpecifier(Err, ESpec: FromConversion->getExplicitSpecifier()); |
3870 | if (Err) |
3871 | return std::move(Err); |
3872 | if (GetImportedOrCreateDecl<CXXConversionDecl>( |
3873 | ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(Val: DC), |
3874 | ToInnerLocStart, NameInfo, T, TInfo, D->UsesFPIntrin(), |
3875 | D->isInlineSpecified(), ESpec, D->getConstexprKind(), |
3876 | SourceLocation(), TrailingRequiresClause)) |
3877 | return ToFunction; |
3878 | } else if (auto *Method = dyn_cast<CXXMethodDecl>(Val: D)) { |
3879 | if (GetImportedOrCreateDecl<CXXMethodDecl>( |
3880 | ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(Val: DC), |
3881 | ToInnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(), |
3882 | Method->UsesFPIntrin(), Method->isInlineSpecified(), |
3883 | D->getConstexprKind(), SourceLocation(), TrailingRequiresClause)) |
3884 | return ToFunction; |
3885 | } else if (auto *Guide = dyn_cast<CXXDeductionGuideDecl>(Val: D)) { |
3886 | ExplicitSpecifier ESpec = |
3887 | importExplicitSpecifier(Err, ESpec: Guide->getExplicitSpecifier()); |
3888 | CXXConstructorDecl *Ctor = |
3889 | importChecked(Err, From: Guide->getCorrespondingConstructor()); |
3890 | if (Err) |
3891 | return std::move(Err); |
3892 | if (GetImportedOrCreateDecl<CXXDeductionGuideDecl>( |
3893 | ToFunction, D, Importer.getToContext(), DC, ToInnerLocStart, ESpec, |
3894 | NameInfo, T, TInfo, ToEndLoc, Ctor)) |
3895 | return ToFunction; |
3896 | cast<CXXDeductionGuideDecl>(Val: ToFunction) |
3897 | ->setDeductionCandidateKind(Guide->getDeductionCandidateKind()); |
3898 | } else { |
3899 | if (GetImportedOrCreateDecl( |
3900 | ToFunction, D, Importer.getToContext(), DC, ToInnerLocStart, |
3901 | NameInfo, T, TInfo, D->getStorageClass(), D->UsesFPIntrin(), |
3902 | D->isInlineSpecified(), D->hasWrittenPrototype(), |
3903 | D->getConstexprKind(), TrailingRequiresClause)) |
3904 | return ToFunction; |
3905 | } |
3906 | |
3907 | // Connect the redecl chain. |
3908 | if (FoundByLookup) { |
3909 | auto *Recent = const_cast<FunctionDecl *>( |
3910 | FoundByLookup->getMostRecentDecl()); |
3911 | ToFunction->setPreviousDecl(Recent); |
3912 | // FIXME Probably we should merge exception specifications. E.g. In the |
3913 | // "To" context the existing function may have exception specification with |
3914 | // noexcept-unevaluated, while the newly imported function may have an |
3915 | // evaluated noexcept. A call to adjustExceptionSpec() on the imported |
3916 | // decl and its redeclarations may be required. |
3917 | } |
3918 | |
3919 | ToFunction->setQualifierInfo(ToQualifierLoc); |
3920 | ToFunction->setAccess(D->getAccess()); |
3921 | ToFunction->setLexicalDeclContext(LexicalDC); |
3922 | ToFunction->setVirtualAsWritten(D->isVirtualAsWritten()); |
3923 | ToFunction->setTrivial(D->isTrivial()); |
3924 | ToFunction->setIsPureVirtual(D->isPureVirtual()); |
3925 | ToFunction->setDefaulted(D->isDefaulted()); |
3926 | ToFunction->setExplicitlyDefaulted(D->isExplicitlyDefaulted()); |
3927 | ToFunction->setDeletedAsWritten(D->isDeletedAsWritten()); |
3928 | ToFunction->setFriendConstraintRefersToEnclosingTemplate( |
3929 | D->FriendConstraintRefersToEnclosingTemplate()); |
3930 | ToFunction->setRangeEnd(ToEndLoc); |
3931 | ToFunction->setDefaultLoc(ToDefaultLoc); |
3932 | |
3933 | // Set the parameters. |
3934 | for (auto *Param : Parameters) { |
3935 | Param->setOwningFunction(ToFunction); |
3936 | ToFunction->addDeclInternal(Param); |
3937 | if (ASTImporterLookupTable *LT = Importer.SharedState->getLookupTable()) |
3938 | LT->update(Param, Importer.getToContext().getTranslationUnitDecl()); |
3939 | } |
3940 | ToFunction->setParams(Parameters); |
3941 | |
3942 | // We need to complete creation of FunctionProtoTypeLoc manually with setting |
3943 | // params it refers to. |
3944 | if (TInfo) { |
3945 | if (auto ProtoLoc = |
3946 | TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) { |
3947 | for (unsigned I = 0, N = Parameters.size(); I != N; ++I) |
3948 | ProtoLoc.setParam(I, Parameters[I]); |
3949 | } |
3950 | } |
3951 | |
3952 | // Import the describing template function, if any. |
3953 | if (FromFT) { |
3954 | auto ToFTOrErr = import(From: FromFT); |
3955 | if (!ToFTOrErr) |
3956 | return ToFTOrErr.takeError(); |
3957 | } |
3958 | |
3959 | // Import Ctor initializers. |
3960 | if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(Val: D)) { |
3961 | if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) { |
3962 | SmallVector<CXXCtorInitializer *, 4> CtorInitializers(NumInitializers); |
3963 | // Import first, then allocate memory and copy if there was no error. |
3964 | if (Error Err = ImportContainerChecked( |
3965 | InContainer: FromConstructor->inits(), OutContainer&: CtorInitializers)) |
3966 | return std::move(Err); |
3967 | auto **Memory = |
3968 | new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers]; |
3969 | std::copy(first: CtorInitializers.begin(), last: CtorInitializers.end(), result: Memory); |
3970 | auto *ToCtor = cast<CXXConstructorDecl>(Val: ToFunction); |
3971 | ToCtor->setCtorInitializers(Memory); |
3972 | ToCtor->setNumCtorInitializers(NumInitializers); |
3973 | } |
3974 | } |
3975 | |
3976 | // If it is a template, import all related things. |
3977 | if (Error Err = ImportTemplateInformation(FromFD: D, ToFD: ToFunction)) |
3978 | return std::move(Err); |
3979 | |
3980 | if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(Val: D)) |
3981 | if (Error Err = ImportOverriddenMethods(ToMethod: cast<CXXMethodDecl>(Val: ToFunction), |
3982 | FromMethod: FromCXXMethod)) |
3983 | return std::move(Err); |
3984 | |
3985 | if (D->doesThisDeclarationHaveABody()) { |
3986 | Error Err = ImportFunctionDeclBody(FromFD: D, ToFD: ToFunction); |
3987 | |
3988 | if (Err) |
3989 | return std::move(Err); |
3990 | } |
3991 | |
3992 | // Import and set the original type in case we used another type. |
3993 | if (UsedDifferentProtoType) { |
3994 | if (ExpectedType TyOrErr = import(D->getType())) |
3995 | ToFunction->setType(*TyOrErr); |
3996 | else |
3997 | return TyOrErr.takeError(); |
3998 | if (Expected<TypeSourceInfo *> TSIOrErr = import(D->getTypeSourceInfo())) |
3999 | ToFunction->setTypeSourceInfo(*TSIOrErr); |
4000 | else |
4001 | return TSIOrErr.takeError(); |
4002 | } |
4003 | |
4004 | // FIXME: Other bits to merge? |
4005 | |
4006 | addDeclToContexts(D, ToFunction); |
4007 | |
4008 | // Import the rest of the chain. I.e. import all subsequent declarations. |
4009 | for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) { |
4010 | ExpectedDecl ToRedeclOrErr = import(*RedeclIt); |
4011 | if (!ToRedeclOrErr) |
4012 | return ToRedeclOrErr.takeError(); |
4013 | } |
4014 | |
4015 | return ToFunction; |
4016 | } |
4017 | |
4018 | ExpectedDecl ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) { |
4019 | return VisitFunctionDecl(D); |
4020 | } |
4021 | |
4022 | ExpectedDecl ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
4023 | return VisitCXXMethodDecl(D); |
4024 | } |
4025 | |
4026 | ExpectedDecl ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
4027 | return VisitCXXMethodDecl(D); |
4028 | } |
4029 | |
4030 | ExpectedDecl ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) { |
4031 | return VisitCXXMethodDecl(D); |
4032 | } |
4033 | |
4034 | ExpectedDecl |
4035 | ASTNodeImporter::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) { |
4036 | return VisitFunctionDecl(D); |
4037 | } |
4038 | |
4039 | ExpectedDecl ASTNodeImporter::VisitFieldDecl(FieldDecl *D) { |
4040 | // Import the major distinguishing characteristics of a variable. |
4041 | DeclContext *DC, *LexicalDC; |
4042 | DeclarationName Name; |
4043 | SourceLocation Loc; |
4044 | NamedDecl *ToD; |
4045 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
4046 | return std::move(Err); |
4047 | if (ToD) |
4048 | return ToD; |
4049 | |
4050 | // Determine whether we've already imported this field. |
4051 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
4052 | for (auto *FoundDecl : FoundDecls) { |
4053 | if (FieldDecl *FoundField = dyn_cast<FieldDecl>(Val: FoundDecl)) { |
4054 | // For anonymous fields, match up by index. |
4055 | if (!Name && |
4056 | ASTImporter::getFieldIndex(D) != |
4057 | ASTImporter::getFieldIndex(FoundField)) |
4058 | continue; |
4059 | |
4060 | if (Importer.IsStructurallyEquivalent(From: D->getType(), |
4061 | To: FoundField->getType())) { |
4062 | Importer.MapImported(D, FoundField); |
4063 | // In case of a FieldDecl of a ClassTemplateSpecializationDecl, the |
4064 | // initializer of a FieldDecl might not had been instantiated in the |
4065 | // "To" context. However, the "From" context might instantiated that, |
4066 | // thus we have to merge that. |
4067 | // Note: `hasInClassInitializer()` is not the same as non-null |
4068 | // `getInClassInitializer()` value. |
4069 | if (Expr *FromInitializer = D->getInClassInitializer()) { |
4070 | if (ExpectedExpr ToInitializerOrErr = import(From: FromInitializer)) { |
4071 | // Import of the FromInitializer may result in the setting of |
4072 | // InClassInitializer. If not, set it here. |
4073 | assert(FoundField->hasInClassInitializer() && |
4074 | "Field should have an in-class initializer if it has an " |
4075 | "expression for it." ); |
4076 | if (!FoundField->getInClassInitializer()) |
4077 | FoundField->setInClassInitializer(*ToInitializerOrErr); |
4078 | } else { |
4079 | return ToInitializerOrErr.takeError(); |
4080 | } |
4081 | } |
4082 | return FoundField; |
4083 | } |
4084 | |
4085 | // FIXME: Why is this case not handled with calling HandleNameConflict? |
4086 | Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent) |
4087 | << Name << D->getType() << FoundField->getType(); |
4088 | Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) |
4089 | << FoundField->getType(); |
4090 | |
4091 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
4092 | } |
4093 | } |
4094 | |
4095 | Error Err = Error::success(); |
4096 | auto ToType = importChecked(Err, D->getType()); |
4097 | auto ToTInfo = importChecked(Err, D->getTypeSourceInfo()); |
4098 | auto ToBitWidth = importChecked(Err, From: D->getBitWidth()); |
4099 | auto ToInnerLocStart = importChecked(Err, D->getInnerLocStart()); |
4100 | if (Err) |
4101 | return std::move(Err); |
4102 | const Type *ToCapturedVLAType = nullptr; |
4103 | if (Error Err = Importer.importInto( |
4104 | To&: ToCapturedVLAType, From: cast_or_null<Type>(Val: D->getCapturedVLAType()))) |
4105 | return std::move(Err); |
4106 | |
4107 | FieldDecl *ToField; |
4108 | if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC, |
4109 | ToInnerLocStart, Loc, Name.getAsIdentifierInfo(), |
4110 | ToType, ToTInfo, ToBitWidth, D->isMutable(), |
4111 | D->getInClassInitStyle())) |
4112 | return ToField; |
4113 | |
4114 | // We need [[no_unqiue_address]] attributes to be added to FieldDecl, before |
4115 | // we add fields in CXXRecordDecl::addedMember, otherwise record will be |
4116 | // marked as having non-zero size. |
4117 | Err = Importer.ImportAttrs(ToField, D); |
4118 | if (Err) |
4119 | return std::move(Err); |
4120 | ToField->setAccess(D->getAccess()); |
4121 | ToField->setLexicalDeclContext(LexicalDC); |
4122 | ToField->setImplicit(D->isImplicit()); |
4123 | if (ToCapturedVLAType) |
4124 | ToField->setCapturedVLAType(cast<VariableArrayType>(Val: ToCapturedVLAType)); |
4125 | LexicalDC->addDeclInternal(ToField); |
4126 | // Import initializer only after the field was created, it may have recursive |
4127 | // reference to the field. |
4128 | auto ToInitializer = importChecked(Err, From: D->getInClassInitializer()); |
4129 | if (Err) |
4130 | return std::move(Err); |
4131 | if (ToInitializer) { |
4132 | auto *AlreadyImported = ToField->getInClassInitializer(); |
4133 | if (AlreadyImported) |
4134 | assert(ToInitializer == AlreadyImported && |
4135 | "Duplicate import of in-class initializer." ); |
4136 | else |
4137 | ToField->setInClassInitializer(ToInitializer); |
4138 | } |
4139 | |
4140 | return ToField; |
4141 | } |
4142 | |
4143 | ExpectedDecl ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) { |
4144 | // Import the major distinguishing characteristics of a variable. |
4145 | DeclContext *DC, *LexicalDC; |
4146 | DeclarationName Name; |
4147 | SourceLocation Loc; |
4148 | NamedDecl *ToD; |
4149 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
4150 | return std::move(Err); |
4151 | if (ToD) |
4152 | return ToD; |
4153 | |
4154 | // Determine whether we've already imported this field. |
4155 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
4156 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
4157 | if (auto *FoundField = dyn_cast<IndirectFieldDecl>(Val: FoundDecls[I])) { |
4158 | // For anonymous indirect fields, match up by index. |
4159 | if (!Name && |
4160 | ASTImporter::getFieldIndex(D) != |
4161 | ASTImporter::getFieldIndex(FoundField)) |
4162 | continue; |
4163 | |
4164 | if (Importer.IsStructurallyEquivalent(From: D->getType(), |
4165 | To: FoundField->getType(), |
4166 | Complain: !Name.isEmpty())) { |
4167 | Importer.MapImported(D, FoundField); |
4168 | return FoundField; |
4169 | } |
4170 | |
4171 | // If there are more anonymous fields to check, continue. |
4172 | if (!Name && I < N-1) |
4173 | continue; |
4174 | |
4175 | // FIXME: Why is this case not handled with calling HandleNameConflict? |
4176 | Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent) |
4177 | << Name << D->getType() << FoundField->getType(); |
4178 | Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) |
4179 | << FoundField->getType(); |
4180 | |
4181 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
4182 | } |
4183 | } |
4184 | |
4185 | // Import the type. |
4186 | auto TypeOrErr = import(D->getType()); |
4187 | if (!TypeOrErr) |
4188 | return TypeOrErr.takeError(); |
4189 | |
4190 | auto **NamedChain = |
4191 | new (Importer.getToContext()) NamedDecl*[D->getChainingSize()]; |
4192 | |
4193 | unsigned i = 0; |
4194 | for (auto *PI : D->chain()) |
4195 | if (Expected<NamedDecl *> ToD = import(From: PI)) |
4196 | NamedChain[i++] = *ToD; |
4197 | else |
4198 | return ToD.takeError(); |
4199 | |
4200 | llvm::MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()}; |
4201 | IndirectFieldDecl *ToIndirectField; |
4202 | if (GetImportedOrCreateDecl(ToIndirectField, D, Importer.getToContext(), DC, |
4203 | Loc, Name.getAsIdentifierInfo(), *TypeOrErr, CH)) |
4204 | // FIXME here we leak `NamedChain` which is allocated before |
4205 | return ToIndirectField; |
4206 | |
4207 | ToIndirectField->setAccess(D->getAccess()); |
4208 | ToIndirectField->setLexicalDeclContext(LexicalDC); |
4209 | LexicalDC->addDeclInternal(ToIndirectField); |
4210 | return ToIndirectField; |
4211 | } |
4212 | |
4213 | /// Used as return type of getFriendCountAndPosition. |
4214 | struct FriendCountAndPosition { |
4215 | /// Number of similar looking friends. |
4216 | unsigned int TotalCount; |
4217 | /// Index of the specific FriendDecl. |
4218 | unsigned int IndexOfDecl; |
4219 | }; |
4220 | |
4221 | static bool IsEquivalentFriend(ASTImporter &Importer, FriendDecl *FD1, |
4222 | FriendDecl *FD2) { |
4223 | if ((!FD1->getFriendType()) != (!FD2->getFriendType())) |
4224 | return false; |
4225 | |
4226 | if (const TypeSourceInfo *TSI = FD1->getFriendType()) |
4227 | return Importer.IsStructurallyEquivalent( |
4228 | From: TSI->getType(), To: FD2->getFriendType()->getType(), /*Complain=*/false); |
4229 | |
4230 | ASTImporter::NonEquivalentDeclSet NonEquivalentDecls; |
4231 | StructuralEquivalenceContext Ctx( |
4232 | FD1->getASTContext(), FD2->getASTContext(), NonEquivalentDecls, |
4233 | StructuralEquivalenceKind::Default, |
4234 | /* StrictTypeSpelling = */ false, /* Complain = */ false); |
4235 | return Ctx.IsEquivalent(FD1, FD2); |
4236 | } |
4237 | |
4238 | static FriendCountAndPosition getFriendCountAndPosition(ASTImporter &Importer, |
4239 | FriendDecl *FD) { |
4240 | unsigned int FriendCount = 0; |
4241 | std::optional<unsigned int> FriendPosition; |
4242 | const auto *RD = cast<CXXRecordDecl>(FD->getLexicalDeclContext()); |
4243 | |
4244 | for (FriendDecl *FoundFriend : RD->friends()) { |
4245 | if (FoundFriend == FD) { |
4246 | FriendPosition = FriendCount; |
4247 | ++FriendCount; |
4248 | } else if (IsEquivalentFriend(Importer, FD, FoundFriend)) { |
4249 | ++FriendCount; |
4250 | } |
4251 | } |
4252 | |
4253 | assert(FriendPosition && "Friend decl not found in own parent." ); |
4254 | |
4255 | return {.TotalCount: FriendCount, .IndexOfDecl: *FriendPosition}; |
4256 | } |
4257 | |
4258 | ExpectedDecl ASTNodeImporter::VisitFriendDecl(FriendDecl *D) { |
4259 | // Import the major distinguishing characteristics of a declaration. |
4260 | DeclContext *DC, *LexicalDC; |
4261 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) |
4262 | return std::move(Err); |
4263 | |
4264 | // Determine whether we've already imported this decl. |
4265 | // FriendDecl is not a NamedDecl so we cannot use lookup. |
4266 | // We try to maintain order and count of redundant friend declarations. |
4267 | const auto *RD = cast<CXXRecordDecl>(Val: DC); |
4268 | SmallVector<FriendDecl *, 2> ImportedEquivalentFriends; |
4269 | for (FriendDecl *ImportedFriend : RD->friends()) |
4270 | if (IsEquivalentFriend(Importer, FD1: D, FD2: ImportedFriend)) |
4271 | ImportedEquivalentFriends.push_back(Elt: ImportedFriend); |
4272 | |
4273 | FriendCountAndPosition CountAndPosition = |
4274 | getFriendCountAndPosition(Importer, FD: D); |
4275 | |
4276 | assert(ImportedEquivalentFriends.size() <= CountAndPosition.TotalCount && |
4277 | "Class with non-matching friends is imported, ODR check wrong?" ); |
4278 | if (ImportedEquivalentFriends.size() == CountAndPosition.TotalCount) |
4279 | return Importer.MapImported( |
4280 | D, ImportedEquivalentFriends[CountAndPosition.IndexOfDecl]); |
4281 | |
4282 | // Not found. Create it. |
4283 | // The declarations will be put into order later by ImportDeclContext. |
4284 | FriendDecl::FriendUnion ToFU; |
4285 | if (NamedDecl *FriendD = D->getFriendDecl()) { |
4286 | NamedDecl *ToFriendD; |
4287 | if (Error Err = importInto(To&: ToFriendD, From: FriendD)) |
4288 | return std::move(Err); |
4289 | |
4290 | if (FriendD->getFriendObjectKind() != Decl::FOK_None && |
4291 | !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator))) |
4292 | ToFriendD->setObjectOfFriendDecl(false); |
4293 | |
4294 | ToFU = ToFriendD; |
4295 | } else { // The friend is a type, not a decl. |
4296 | if (auto TSIOrErr = import(From: D->getFriendType())) |
4297 | ToFU = *TSIOrErr; |
4298 | else |
4299 | return TSIOrErr.takeError(); |
4300 | } |
4301 | |
4302 | SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists); |
4303 | auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>(); |
4304 | for (unsigned I = 0; I < D->NumTPLists; I++) { |
4305 | if (auto ListOrErr = import(FromTPLists[I])) |
4306 | ToTPLists[I] = *ListOrErr; |
4307 | else |
4308 | return ListOrErr.takeError(); |
4309 | } |
4310 | |
4311 | auto LocationOrErr = import(D->getLocation()); |
4312 | if (!LocationOrErr) |
4313 | return LocationOrErr.takeError(); |
4314 | auto FriendLocOrErr = import(From: D->getFriendLoc()); |
4315 | if (!FriendLocOrErr) |
4316 | return FriendLocOrErr.takeError(); |
4317 | |
4318 | FriendDecl *FrD; |
4319 | if (GetImportedOrCreateDecl(FrD, D, Importer.getToContext(), DC, |
4320 | *LocationOrErr, ToFU, |
4321 | *FriendLocOrErr, ToTPLists)) |
4322 | return FrD; |
4323 | |
4324 | FrD->setAccess(D->getAccess()); |
4325 | FrD->setLexicalDeclContext(LexicalDC); |
4326 | LexicalDC->addDeclInternal(FrD); |
4327 | return FrD; |
4328 | } |
4329 | |
4330 | ExpectedDecl ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) { |
4331 | // Import the major distinguishing characteristics of an ivar. |
4332 | DeclContext *DC, *LexicalDC; |
4333 | DeclarationName Name; |
4334 | SourceLocation Loc; |
4335 | NamedDecl *ToD; |
4336 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
4337 | return std::move(Err); |
4338 | if (ToD) |
4339 | return ToD; |
4340 | |
4341 | // Determine whether we've already imported this ivar |
4342 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
4343 | for (auto *FoundDecl : FoundDecls) { |
4344 | if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(Val: FoundDecl)) { |
4345 | if (Importer.IsStructurallyEquivalent(From: D->getType(), |
4346 | To: FoundIvar->getType())) { |
4347 | Importer.MapImported(D, FoundIvar); |
4348 | return FoundIvar; |
4349 | } |
4350 | |
4351 | Importer.ToDiag(Loc, diag::warn_odr_ivar_type_inconsistent) |
4352 | << Name << D->getType() << FoundIvar->getType(); |
4353 | Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here) |
4354 | << FoundIvar->getType(); |
4355 | |
4356 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
4357 | } |
4358 | } |
4359 | |
4360 | Error Err = Error::success(); |
4361 | auto ToType = importChecked(Err, D->getType()); |
4362 | auto ToTypeSourceInfo = importChecked(Err, D->getTypeSourceInfo()); |
4363 | auto ToBitWidth = importChecked(Err, D->getBitWidth()); |
4364 | auto ToInnerLocStart = importChecked(Err, D->getInnerLocStart()); |
4365 | if (Err) |
4366 | return std::move(Err); |
4367 | |
4368 | ObjCIvarDecl *ToIvar; |
4369 | if (GetImportedOrCreateDecl( |
4370 | ToIvar, D, Importer.getToContext(), cast<ObjCContainerDecl>(Val: DC), |
4371 | ToInnerLocStart, Loc, Name.getAsIdentifierInfo(), |
4372 | ToType, ToTypeSourceInfo, |
4373 | D->getAccessControl(),ToBitWidth, D->getSynthesize())) |
4374 | return ToIvar; |
4375 | |
4376 | ToIvar->setLexicalDeclContext(LexicalDC); |
4377 | LexicalDC->addDeclInternal(ToIvar); |
4378 | return ToIvar; |
4379 | } |
4380 | |
4381 | ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) { |
4382 | |
4383 | SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D); |
4384 | auto RedeclIt = Redecls.begin(); |
4385 | // Import the first part of the decl chain. I.e. import all previous |
4386 | // declarations starting from the canonical decl. |
4387 | for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) { |
4388 | ExpectedDecl RedeclOrErr = import(*RedeclIt); |
4389 | if (!RedeclOrErr) |
4390 | return RedeclOrErr.takeError(); |
4391 | } |
4392 | assert(*RedeclIt == D); |
4393 | |
4394 | // Import the major distinguishing characteristics of a variable. |
4395 | DeclContext *DC, *LexicalDC; |
4396 | DeclarationName Name; |
4397 | SourceLocation Loc; |
4398 | NamedDecl *ToD; |
4399 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
4400 | return std::move(Err); |
4401 | if (ToD) |
4402 | return ToD; |
4403 | |
4404 | // Try to find a variable in our own ("to") context with the same name and |
4405 | // in the same context as the variable we're importing. |
4406 | VarDecl *FoundByLookup = nullptr; |
4407 | if (D->isFileVarDecl()) { |
4408 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
4409 | unsigned IDNS = Decl::IDNS_Ordinary; |
4410 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
4411 | for (auto *FoundDecl : FoundDecls) { |
4412 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) |
4413 | continue; |
4414 | |
4415 | if (auto *FoundVar = dyn_cast<VarDecl>(Val: FoundDecl)) { |
4416 | if (!hasSameVisibilityContextAndLinkage(Found: FoundVar, From: D)) |
4417 | continue; |
4418 | if (Importer.IsStructurallyEquivalent(From: D->getType(), |
4419 | To: FoundVar->getType())) { |
4420 | |
4421 | // The VarDecl in the "From" context has a definition, but in the |
4422 | // "To" context we already have a definition. |
4423 | VarDecl *FoundDef = FoundVar->getDefinition(); |
4424 | if (D->isThisDeclarationADefinition() && FoundDef) |
4425 | // FIXME Check for ODR error if the two definitions have |
4426 | // different initializers? |
4427 | return Importer.MapImported(D, FoundDef); |
4428 | |
4429 | // The VarDecl in the "From" context has an initializer, but in the |
4430 | // "To" context we already have an initializer. |
4431 | const VarDecl *FoundDInit = nullptr; |
4432 | if (D->getInit() && FoundVar->getAnyInitializer(D&: FoundDInit)) |
4433 | // FIXME Diagnose ODR error if the two initializers are different? |
4434 | return Importer.MapImported(D, const_cast<VarDecl*>(FoundDInit)); |
4435 | |
4436 | FoundByLookup = FoundVar; |
4437 | break; |
4438 | } |
4439 | |
4440 | const ArrayType *FoundArray |
4441 | = Importer.getToContext().getAsArrayType(T: FoundVar->getType()); |
4442 | const ArrayType *TArray |
4443 | = Importer.getToContext().getAsArrayType(T: D->getType()); |
4444 | if (FoundArray && TArray) { |
4445 | if (isa<IncompleteArrayType>(Val: FoundArray) && |
4446 | isa<ConstantArrayType>(Val: TArray)) { |
4447 | // Import the type. |
4448 | if (auto TyOrErr = import(D->getType())) |
4449 | FoundVar->setType(*TyOrErr); |
4450 | else |
4451 | return TyOrErr.takeError(); |
4452 | |
4453 | FoundByLookup = FoundVar; |
4454 | break; |
4455 | } else if (isa<IncompleteArrayType>(Val: TArray) && |
4456 | isa<ConstantArrayType>(Val: FoundArray)) { |
4457 | FoundByLookup = FoundVar; |
4458 | break; |
4459 | } |
4460 | } |
4461 | |
4462 | Importer.ToDiag(Loc, diag::warn_odr_variable_type_inconsistent) |
4463 | << Name << D->getType() << FoundVar->getType(); |
4464 | Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here) |
4465 | << FoundVar->getType(); |
4466 | ConflictingDecls.push_back(Elt: FoundDecl); |
4467 | } |
4468 | } |
4469 | |
4470 | if (!ConflictingDecls.empty()) { |
4471 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
4472 | Name, DC, IDNS, Decls: ConflictingDecls.data(), NumDecls: ConflictingDecls.size()); |
4473 | if (NameOrErr) |
4474 | Name = NameOrErr.get(); |
4475 | else |
4476 | return NameOrErr.takeError(); |
4477 | } |
4478 | } |
4479 | |
4480 | Error Err = Error::success(); |
4481 | auto ToType = importChecked(Err, D->getType()); |
4482 | auto ToTypeSourceInfo = importChecked(Err, D->getTypeSourceInfo()); |
4483 | auto ToInnerLocStart = importChecked(Err, D->getInnerLocStart()); |
4484 | auto ToQualifierLoc = importChecked(Err, D->getQualifierLoc()); |
4485 | if (Err) |
4486 | return std::move(Err); |
4487 | |
4488 | VarDecl *ToVar; |
4489 | if (auto *FromDecomp = dyn_cast<DecompositionDecl>(Val: D)) { |
4490 | SmallVector<BindingDecl *> Bindings(FromDecomp->bindings().size()); |
4491 | if (Error Err = |
4492 | ImportArrayChecked(InContainer: FromDecomp->bindings(), Obegin: Bindings.begin())) |
4493 | return std::move(Err); |
4494 | DecompositionDecl *ToDecomp; |
4495 | if (GetImportedOrCreateDecl( |
4496 | ToDecomp, FromDecomp, Importer.getToContext(), DC, ToInnerLocStart, |
4497 | Loc, ToType, ToTypeSourceInfo, D->getStorageClass(), Bindings)) |
4498 | return ToDecomp; |
4499 | ToVar = ToDecomp; |
4500 | } else { |
4501 | // Create the imported variable. |
4502 | if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC, |
4503 | ToInnerLocStart, Loc, |
4504 | Name.getAsIdentifierInfo(), ToType, |
4505 | ToTypeSourceInfo, D->getStorageClass())) |
4506 | return ToVar; |
4507 | } |
4508 | |
4509 | ToVar->setTSCSpec(D->getTSCSpec()); |
4510 | ToVar->setQualifierInfo(ToQualifierLoc); |
4511 | ToVar->setAccess(D->getAccess()); |
4512 | ToVar->setLexicalDeclContext(LexicalDC); |
4513 | |
4514 | if (FoundByLookup) { |
4515 | auto *Recent = const_cast<VarDecl *>(FoundByLookup->getMostRecentDecl()); |
4516 | ToVar->setPreviousDecl(Recent); |
4517 | } |
4518 | |
4519 | // Import the described template, if any. |
4520 | if (D->getDescribedVarTemplate()) { |
4521 | auto ToVTOrErr = import(From: D->getDescribedVarTemplate()); |
4522 | if (!ToVTOrErr) |
4523 | return ToVTOrErr.takeError(); |
4524 | } else if (MemberSpecializationInfo *MSI = D->getMemberSpecializationInfo()) { |
4525 | TemplateSpecializationKind SK = MSI->getTemplateSpecializationKind(); |
4526 | VarDecl *FromInst = D->getInstantiatedFromStaticDataMember(); |
4527 | if (Expected<VarDecl *> ToInstOrErr = import(From: FromInst)) |
4528 | ToVar->setInstantiationOfStaticDataMember(VD: *ToInstOrErr, TSK: SK); |
4529 | else |
4530 | return ToInstOrErr.takeError(); |
4531 | if (ExpectedSLoc POIOrErr = import(From: MSI->getPointOfInstantiation())) |
4532 | ToVar->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr); |
4533 | else |
4534 | return POIOrErr.takeError(); |
4535 | } |
4536 | |
4537 | if (Error Err = ImportInitializer(From: D, To: ToVar)) |
4538 | return std::move(Err); |
4539 | |
4540 | if (D->isConstexpr()) |
4541 | ToVar->setConstexpr(true); |
4542 | |
4543 | addDeclToContexts(D, ToVar); |
4544 | |
4545 | // Import the rest of the chain. I.e. import all subsequent declarations. |
4546 | for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) { |
4547 | ExpectedDecl RedeclOrErr = import(*RedeclIt); |
4548 | if (!RedeclOrErr) |
4549 | return RedeclOrErr.takeError(); |
4550 | } |
4551 | |
4552 | return ToVar; |
4553 | } |
4554 | |
4555 | ExpectedDecl ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) { |
4556 | // Parameters are created in the translation unit's context, then moved |
4557 | // into the function declaration's context afterward. |
4558 | DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); |
4559 | |
4560 | Error Err = Error::success(); |
4561 | auto ToDeclName = importChecked(Err, D->getDeclName()); |
4562 | auto ToLocation = importChecked(Err, D->getLocation()); |
4563 | auto ToType = importChecked(Err, D->getType()); |
4564 | if (Err) |
4565 | return std::move(Err); |
4566 | |
4567 | // Create the imported parameter. |
4568 | ImplicitParamDecl *ToParm = nullptr; |
4569 | if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC, |
4570 | ToLocation, ToDeclName.getAsIdentifierInfo(), |
4571 | ToType, D->getParameterKind())) |
4572 | return ToParm; |
4573 | return ToParm; |
4574 | } |
4575 | |
4576 | Error ASTNodeImporter::ImportDefaultArgOfParmVarDecl( |
4577 | const ParmVarDecl *FromParam, ParmVarDecl *ToParam) { |
4578 | ToParam->setHasInheritedDefaultArg(FromParam->hasInheritedDefaultArg()); |
4579 | ToParam->setExplicitObjectParameterLoc( |
4580 | FromParam->getExplicitObjectParamThisLoc()); |
4581 | ToParam->setKNRPromoted(FromParam->isKNRPromoted()); |
4582 | |
4583 | if (FromParam->hasUninstantiatedDefaultArg()) { |
4584 | if (auto ToDefArgOrErr = import(From: FromParam->getUninstantiatedDefaultArg())) |
4585 | ToParam->setUninstantiatedDefaultArg(*ToDefArgOrErr); |
4586 | else |
4587 | return ToDefArgOrErr.takeError(); |
4588 | } else if (FromParam->hasUnparsedDefaultArg()) { |
4589 | ToParam->setUnparsedDefaultArg(); |
4590 | } else if (FromParam->hasDefaultArg()) { |
4591 | if (auto ToDefArgOrErr = import(From: FromParam->getDefaultArg())) |
4592 | ToParam->setDefaultArg(*ToDefArgOrErr); |
4593 | else |
4594 | return ToDefArgOrErr.takeError(); |
4595 | } |
4596 | |
4597 | return Error::success(); |
4598 | } |
4599 | |
4600 | Expected<InheritedConstructor> |
4601 | ASTNodeImporter::ImportInheritedConstructor(const InheritedConstructor &From) { |
4602 | Error Err = Error::success(); |
4603 | CXXConstructorDecl *ToBaseCtor = importChecked(Err, From: From.getConstructor()); |
4604 | ConstructorUsingShadowDecl *ToShadow = |
4605 | importChecked(Err, From: From.getShadowDecl()); |
4606 | if (Err) |
4607 | return std::move(Err); |
4608 | return InheritedConstructor(ToShadow, ToBaseCtor); |
4609 | } |
4610 | |
4611 | ExpectedDecl ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) { |
4612 | // Parameters are created in the translation unit's context, then moved |
4613 | // into the function declaration's context afterward. |
4614 | DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); |
4615 | |
4616 | Error Err = Error::success(); |
4617 | auto ToDeclName = importChecked(Err, D->getDeclName()); |
4618 | auto ToLocation = importChecked(Err, D->getLocation()); |
4619 | auto ToInnerLocStart = importChecked(Err, D->getInnerLocStart()); |
4620 | auto ToType = importChecked(Err, D->getType()); |
4621 | auto ToTypeSourceInfo = importChecked(Err, D->getTypeSourceInfo()); |
4622 | if (Err) |
4623 | return std::move(Err); |
4624 | |
4625 | ParmVarDecl *ToParm; |
4626 | if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC, |
4627 | ToInnerLocStart, ToLocation, |
4628 | ToDeclName.getAsIdentifierInfo(), ToType, |
4629 | ToTypeSourceInfo, D->getStorageClass(), |
4630 | /*DefaultArg*/ nullptr)) |
4631 | return ToParm; |
4632 | |
4633 | // Set the default argument. It should be no problem if it was already done. |
4634 | // Do not import the default expression before GetImportedOrCreateDecl call |
4635 | // to avoid possible infinite import loop because circular dependency. |
4636 | if (Error Err = ImportDefaultArgOfParmVarDecl(FromParam: D, ToParam: ToParm)) |
4637 | return std::move(Err); |
4638 | |
4639 | if (D->isObjCMethodParameter()) { |
4640 | ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex()); |
4641 | ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier()); |
4642 | } else { |
4643 | ToParm->setScopeInfo(scopeDepth: D->getFunctionScopeDepth(), |
4644 | parameterIndex: D->getFunctionScopeIndex()); |
4645 | } |
4646 | |
4647 | return ToParm; |
4648 | } |
4649 | |
4650 | ExpectedDecl ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) { |
4651 | // Import the major distinguishing characteristics of a method. |
4652 | DeclContext *DC, *LexicalDC; |
4653 | DeclarationName Name; |
4654 | SourceLocation Loc; |
4655 | NamedDecl *ToD; |
4656 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
4657 | return std::move(Err); |
4658 | if (ToD) |
4659 | return ToD; |
4660 | |
4661 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
4662 | for (auto *FoundDecl : FoundDecls) { |
4663 | if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(Val: FoundDecl)) { |
4664 | if (FoundMethod->isInstanceMethod() != D->isInstanceMethod()) |
4665 | continue; |
4666 | |
4667 | // Check return types. |
4668 | if (!Importer.IsStructurallyEquivalent(From: D->getReturnType(), |
4669 | To: FoundMethod->getReturnType())) { |
4670 | Importer.ToDiag(Loc, diag::warn_odr_objc_method_result_type_inconsistent) |
4671 | << D->isInstanceMethod() << Name << D->getReturnType() |
4672 | << FoundMethod->getReturnType(); |
4673 | Importer.ToDiag(FoundMethod->getLocation(), |
4674 | diag::note_odr_objc_method_here) |
4675 | << D->isInstanceMethod() << Name; |
4676 | |
4677 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
4678 | } |
4679 | |
4680 | // Check the number of parameters. |
4681 | if (D->param_size() != FoundMethod->param_size()) { |
4682 | Importer.ToDiag(Loc, diag::warn_odr_objc_method_num_params_inconsistent) |
4683 | << D->isInstanceMethod() << Name |
4684 | << D->param_size() << FoundMethod->param_size(); |
4685 | Importer.ToDiag(FoundMethod->getLocation(), |
4686 | diag::note_odr_objc_method_here) |
4687 | << D->isInstanceMethod() << Name; |
4688 | |
4689 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
4690 | } |
4691 | |
4692 | // Check parameter types. |
4693 | for (ObjCMethodDecl::param_iterator P = D->param_begin(), |
4694 | PEnd = D->param_end(), FoundP = FoundMethod->param_begin(); |
4695 | P != PEnd; ++P, ++FoundP) { |
4696 | if (!Importer.IsStructurallyEquivalent(From: (*P)->getType(), |
4697 | To: (*FoundP)->getType())) { |
4698 | Importer.FromDiag((*P)->getLocation(), |
4699 | diag::warn_odr_objc_method_param_type_inconsistent) |
4700 | << D->isInstanceMethod() << Name |
4701 | << (*P)->getType() << (*FoundP)->getType(); |
4702 | Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here) |
4703 | << (*FoundP)->getType(); |
4704 | |
4705 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
4706 | } |
4707 | } |
4708 | |
4709 | // Check variadic/non-variadic. |
4710 | // Check the number of parameters. |
4711 | if (D->isVariadic() != FoundMethod->isVariadic()) { |
4712 | Importer.ToDiag(Loc, diag::warn_odr_objc_method_variadic_inconsistent) |
4713 | << D->isInstanceMethod() << Name; |
4714 | Importer.ToDiag(FoundMethod->getLocation(), |
4715 | diag::note_odr_objc_method_here) |
4716 | << D->isInstanceMethod() << Name; |
4717 | |
4718 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
4719 | } |
4720 | |
4721 | // FIXME: Any other bits we need to merge? |
4722 | return Importer.MapImported(D, FoundMethod); |
4723 | } |
4724 | } |
4725 | |
4726 | Error Err = Error::success(); |
4727 | auto ToEndLoc = importChecked(Err, From: D->getEndLoc()); |
4728 | auto ToReturnType = importChecked(Err, From: D->getReturnType()); |
4729 | auto ToReturnTypeSourceInfo = |
4730 | importChecked(Err, From: D->getReturnTypeSourceInfo()); |
4731 | if (Err) |
4732 | return std::move(Err); |
4733 | |
4734 | ObjCMethodDecl *ToMethod; |
4735 | if (GetImportedOrCreateDecl( |
4736 | ToMethod, D, Importer.getToContext(), Loc, ToEndLoc, |
4737 | Name.getObjCSelector(), ToReturnType, ToReturnTypeSourceInfo, DC, |
4738 | D->isInstanceMethod(), D->isVariadic(), D->isPropertyAccessor(), |
4739 | D->isSynthesizedAccessorStub(), D->isImplicit(), D->isDefined(), |
4740 | D->getImplementationControl(), D->hasRelatedResultType())) |
4741 | return ToMethod; |
4742 | |
4743 | // FIXME: When we decide to merge method definitions, we'll need to |
4744 | // deal with implicit parameters. |
4745 | |
4746 | // Import the parameters |
4747 | SmallVector<ParmVarDecl *, 5> ToParams; |
4748 | for (auto *FromP : D->parameters()) { |
4749 | if (Expected<ParmVarDecl *> ToPOrErr = import(From: FromP)) |
4750 | ToParams.push_back(Elt: *ToPOrErr); |
4751 | else |
4752 | return ToPOrErr.takeError(); |
4753 | } |
4754 | |
4755 | // Set the parameters. |
4756 | for (auto *ToParam : ToParams) { |
4757 | ToParam->setOwningFunction(ToMethod); |
4758 | ToMethod->addDeclInternal(ToParam); |
4759 | } |
4760 | |
4761 | SmallVector<SourceLocation, 12> FromSelLocs; |
4762 | D->getSelectorLocs(SelLocs&: FromSelLocs); |
4763 | SmallVector<SourceLocation, 12> ToSelLocs(FromSelLocs.size()); |
4764 | if (Error Err = ImportContainerChecked(InContainer: FromSelLocs, OutContainer&: ToSelLocs)) |
4765 | return std::move(Err); |
4766 | |
4767 | ToMethod->setMethodParams(C&: Importer.getToContext(), Params: ToParams, SelLocs: ToSelLocs); |
4768 | |
4769 | ToMethod->setLexicalDeclContext(LexicalDC); |
4770 | LexicalDC->addDeclInternal(ToMethod); |
4771 | |
4772 | // Implicit params are declared when Sema encounters the definition but this |
4773 | // never happens when the method is imported. Manually declare the implicit |
4774 | // params now that the MethodDecl knows its class interface. |
4775 | if (D->getSelfDecl()) |
4776 | ToMethod->createImplicitParams(Context&: Importer.getToContext(), |
4777 | ID: ToMethod->getClassInterface()); |
4778 | |
4779 | return ToMethod; |
4780 | } |
4781 | |
4782 | ExpectedDecl ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) { |
4783 | // Import the major distinguishing characteristics of a category. |
4784 | DeclContext *DC, *LexicalDC; |
4785 | DeclarationName Name; |
4786 | SourceLocation Loc; |
4787 | NamedDecl *ToD; |
4788 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
4789 | return std::move(Err); |
4790 | if (ToD) |
4791 | return ToD; |
4792 | |
4793 | Error Err = Error::success(); |
4794 | auto ToVarianceLoc = importChecked(Err, From: D->getVarianceLoc()); |
4795 | auto ToLocation = importChecked(Err, D->getLocation()); |
4796 | auto ToColonLoc = importChecked(Err, From: D->getColonLoc()); |
4797 | auto ToTypeSourceInfo = importChecked(Err, D->getTypeSourceInfo()); |
4798 | if (Err) |
4799 | return std::move(Err); |
4800 | |
4801 | ObjCTypeParamDecl *Result; |
4802 | if (GetImportedOrCreateDecl( |
4803 | Result, D, Importer.getToContext(), DC, D->getVariance(), |
4804 | ToVarianceLoc, D->getIndex(), |
4805 | ToLocation, Name.getAsIdentifierInfo(), |
4806 | ToColonLoc, ToTypeSourceInfo)) |
4807 | return Result; |
4808 | |
4809 | // Only import 'ObjCTypeParamType' after the decl is created. |
4810 | auto ToTypeForDecl = importChecked(Err, D->getTypeForDecl()); |
4811 | if (Err) |
4812 | return std::move(Err); |
4813 | Result->setTypeForDecl(ToTypeForDecl); |
4814 | Result->setLexicalDeclContext(LexicalDC); |
4815 | return Result; |
4816 | } |
4817 | |
4818 | ExpectedDecl ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) { |
4819 | // Import the major distinguishing characteristics of a category. |
4820 | DeclContext *DC, *LexicalDC; |
4821 | DeclarationName Name; |
4822 | SourceLocation Loc; |
4823 | NamedDecl *ToD; |
4824 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
4825 | return std::move(Err); |
4826 | if (ToD) |
4827 | return ToD; |
4828 | |
4829 | ObjCInterfaceDecl *ToInterface; |
4830 | if (Error Err = importInto(To&: ToInterface, From: D->getClassInterface())) |
4831 | return std::move(Err); |
4832 | |
4833 | // Determine if we've already encountered this category. |
4834 | ObjCCategoryDecl *MergeWithCategory |
4835 | = ToInterface->FindCategoryDeclaration(CategoryId: Name.getAsIdentifierInfo()); |
4836 | ObjCCategoryDecl *ToCategory = MergeWithCategory; |
4837 | if (!ToCategory) { |
4838 | |
4839 | Error Err = Error::success(); |
4840 | auto ToAtStartLoc = importChecked(Err, D->getAtStartLoc()); |
4841 | auto ToCategoryNameLoc = importChecked(Err, From: D->getCategoryNameLoc()); |
4842 | auto ToIvarLBraceLoc = importChecked(Err, From: D->getIvarLBraceLoc()); |
4843 | auto ToIvarRBraceLoc = importChecked(Err, From: D->getIvarRBraceLoc()); |
4844 | if (Err) |
4845 | return std::move(Err); |
4846 | |
4847 | if (GetImportedOrCreateDecl(ToCategory, D, Importer.getToContext(), DC, |
4848 | ToAtStartLoc, Loc, |
4849 | ToCategoryNameLoc, |
4850 | Name.getAsIdentifierInfo(), ToInterface, |
4851 | /*TypeParamList=*/nullptr, |
4852 | ToIvarLBraceLoc, |
4853 | ToIvarRBraceLoc)) |
4854 | return ToCategory; |
4855 | |
4856 | ToCategory->setLexicalDeclContext(LexicalDC); |
4857 | LexicalDC->addDeclInternal(ToCategory); |
4858 | // Import the type parameter list after MapImported, to avoid |
4859 | // loops when bringing in their DeclContext. |
4860 | if (auto PListOrErr = ImportObjCTypeParamList(list: D->getTypeParamList())) |
4861 | ToCategory->setTypeParamList(*PListOrErr); |
4862 | else |
4863 | return PListOrErr.takeError(); |
4864 | |
4865 | // Import protocols |
4866 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
4867 | SmallVector<SourceLocation, 4> ProtocolLocs; |
4868 | ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc |
4869 | = D->protocol_loc_begin(); |
4870 | for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(), |
4871 | FromProtoEnd = D->protocol_end(); |
4872 | FromProto != FromProtoEnd; |
4873 | ++FromProto, ++FromProtoLoc) { |
4874 | if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(From: *FromProto)) |
4875 | Protocols.push_back(Elt: *ToProtoOrErr); |
4876 | else |
4877 | return ToProtoOrErr.takeError(); |
4878 | |
4879 | if (ExpectedSLoc ToProtoLocOrErr = import(From: *FromProtoLoc)) |
4880 | ProtocolLocs.push_back(Elt: *ToProtoLocOrErr); |
4881 | else |
4882 | return ToProtoLocOrErr.takeError(); |
4883 | } |
4884 | |
4885 | // FIXME: If we're merging, make sure that the protocol list is the same. |
4886 | ToCategory->setProtocolList(List: Protocols.data(), Num: Protocols.size(), |
4887 | Locs: ProtocolLocs.data(), C&: Importer.getToContext()); |
4888 | |
4889 | } else { |
4890 | Importer.MapImported(D, ToCategory); |
4891 | } |
4892 | |
4893 | // Import all of the members of this category. |
4894 | if (Error Err = ImportDeclContext(D)) |
4895 | return std::move(Err); |
4896 | |
4897 | // If we have an implementation, import it as well. |
4898 | if (D->getImplementation()) { |
4899 | if (Expected<ObjCCategoryImplDecl *> ToImplOrErr = |
4900 | import(From: D->getImplementation())) |
4901 | ToCategory->setImplementation(*ToImplOrErr); |
4902 | else |
4903 | return ToImplOrErr.takeError(); |
4904 | } |
4905 | |
4906 | return ToCategory; |
4907 | } |
4908 | |
4909 | Error ASTNodeImporter::ImportDefinition( |
4910 | ObjCProtocolDecl *From, ObjCProtocolDecl *To, ImportDefinitionKind Kind) { |
4911 | if (To->getDefinition()) { |
4912 | if (shouldForceImportDeclContext(IDK: Kind)) |
4913 | if (Error Err = ImportDeclContext(From)) |
4914 | return Err; |
4915 | return Error::success(); |
4916 | } |
4917 | |
4918 | // Start the protocol definition |
4919 | To->startDefinition(); |
4920 | |
4921 | // Import protocols |
4922 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
4923 | SmallVector<SourceLocation, 4> ProtocolLocs; |
4924 | ObjCProtocolDecl::protocol_loc_iterator FromProtoLoc = |
4925 | From->protocol_loc_begin(); |
4926 | for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(), |
4927 | FromProtoEnd = From->protocol_end(); |
4928 | FromProto != FromProtoEnd; |
4929 | ++FromProto, ++FromProtoLoc) { |
4930 | if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(From: *FromProto)) |
4931 | Protocols.push_back(Elt: *ToProtoOrErr); |
4932 | else |
4933 | return ToProtoOrErr.takeError(); |
4934 | |
4935 | if (ExpectedSLoc ToProtoLocOrErr = import(From: *FromProtoLoc)) |
4936 | ProtocolLocs.push_back(Elt: *ToProtoLocOrErr); |
4937 | else |
4938 | return ToProtoLocOrErr.takeError(); |
4939 | |
4940 | } |
4941 | |
4942 | // FIXME: If we're merging, make sure that the protocol list is the same. |
4943 | To->setProtocolList(List: Protocols.data(), Num: Protocols.size(), |
4944 | Locs: ProtocolLocs.data(), C&: Importer.getToContext()); |
4945 | |
4946 | if (shouldForceImportDeclContext(IDK: Kind)) { |
4947 | // Import all of the members of this protocol. |
4948 | if (Error Err = ImportDeclContext(From, /*ForceImport=*/true)) |
4949 | return Err; |
4950 | } |
4951 | return Error::success(); |
4952 | } |
4953 | |
4954 | ExpectedDecl ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) { |
4955 | // If this protocol has a definition in the translation unit we're coming |
4956 | // from, but this particular declaration is not that definition, import the |
4957 | // definition and map to that. |
4958 | ObjCProtocolDecl *Definition = D->getDefinition(); |
4959 | if (Definition && Definition != D) { |
4960 | if (ExpectedDecl ImportedDefOrErr = import(From: Definition)) |
4961 | return Importer.MapImported(D, *ImportedDefOrErr); |
4962 | else |
4963 | return ImportedDefOrErr.takeError(); |
4964 | } |
4965 | |
4966 | // Import the major distinguishing characteristics of a protocol. |
4967 | DeclContext *DC, *LexicalDC; |
4968 | DeclarationName Name; |
4969 | SourceLocation Loc; |
4970 | NamedDecl *ToD; |
4971 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
4972 | return std::move(Err); |
4973 | if (ToD) |
4974 | return ToD; |
4975 | |
4976 | ObjCProtocolDecl *MergeWithProtocol = nullptr; |
4977 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
4978 | for (auto *FoundDecl : FoundDecls) { |
4979 | if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol)) |
4980 | continue; |
4981 | |
4982 | if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(Val: FoundDecl))) |
4983 | break; |
4984 | } |
4985 | |
4986 | ObjCProtocolDecl *ToProto = MergeWithProtocol; |
4987 | if (!ToProto) { |
4988 | auto ToAtBeginLocOrErr = import(D->getAtStartLoc()); |
4989 | if (!ToAtBeginLocOrErr) |
4990 | return ToAtBeginLocOrErr.takeError(); |
4991 | |
4992 | if (GetImportedOrCreateDecl(ToProto, D, Importer.getToContext(), DC, |
4993 | Name.getAsIdentifierInfo(), Loc, |
4994 | *ToAtBeginLocOrErr, |
4995 | /*PrevDecl=*/nullptr)) |
4996 | return ToProto; |
4997 | ToProto->setLexicalDeclContext(LexicalDC); |
4998 | LexicalDC->addDeclInternal(ToProto); |
4999 | } |
5000 | |
5001 | Importer.MapImported(D, ToProto); |
5002 | |
5003 | if (D->isThisDeclarationADefinition()) |
5004 | if (Error Err = ImportDefinition(From: D, To: ToProto)) |
5005 | return std::move(Err); |
5006 | |
5007 | return ToProto; |
5008 | } |
5009 | |
5010 | ExpectedDecl ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
5011 | DeclContext *DC, *LexicalDC; |
5012 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) |
5013 | return std::move(Err); |
5014 | |
5015 | ExpectedSLoc ExternLocOrErr = import(From: D->getExternLoc()); |
5016 | if (!ExternLocOrErr) |
5017 | return ExternLocOrErr.takeError(); |
5018 | |
5019 | ExpectedSLoc LangLocOrErr = import(D->getLocation()); |
5020 | if (!LangLocOrErr) |
5021 | return LangLocOrErr.takeError(); |
5022 | |
5023 | bool HasBraces = D->hasBraces(); |
5024 | |
5025 | LinkageSpecDecl *ToLinkageSpec; |
5026 | if (GetImportedOrCreateDecl(ToD&: ToLinkageSpec, FromD: D, args&: Importer.getToContext(), args&: DC, |
5027 | args&: *ExternLocOrErr, args&: *LangLocOrErr, |
5028 | args: D->getLanguage(), args&: HasBraces)) |
5029 | return ToLinkageSpec; |
5030 | |
5031 | if (HasBraces) { |
5032 | ExpectedSLoc RBraceLocOrErr = import(From: D->getRBraceLoc()); |
5033 | if (!RBraceLocOrErr) |
5034 | return RBraceLocOrErr.takeError(); |
5035 | ToLinkageSpec->setRBraceLoc(*RBraceLocOrErr); |
5036 | } |
5037 | |
5038 | ToLinkageSpec->setLexicalDeclContext(LexicalDC); |
5039 | LexicalDC->addDeclInternal(ToLinkageSpec); |
5040 | |
5041 | return ToLinkageSpec; |
5042 | } |
5043 | |
5044 | ExpectedDecl ASTNodeImporter::ImportUsingShadowDecls(BaseUsingDecl *D, |
5045 | BaseUsingDecl *ToSI) { |
5046 | for (UsingShadowDecl *FromShadow : D->shadows()) { |
5047 | if (Expected<UsingShadowDecl *> ToShadowOrErr = import(From: FromShadow)) |
5048 | ToSI->addShadowDecl(S: *ToShadowOrErr); |
5049 | else |
5050 | // FIXME: We return error here but the definition is already created |
5051 | // and available with lookups. How to fix this?.. |
5052 | return ToShadowOrErr.takeError(); |
5053 | } |
5054 | return ToSI; |
5055 | } |
5056 | |
5057 | ExpectedDecl ASTNodeImporter::VisitUsingDecl(UsingDecl *D) { |
5058 | DeclContext *DC, *LexicalDC; |
5059 | DeclarationName Name; |
5060 | SourceLocation Loc; |
5061 | NamedDecl *ToD = nullptr; |
5062 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
5063 | return std::move(Err); |
5064 | if (ToD) |
5065 | return ToD; |
5066 | |
5067 | Error Err = Error::success(); |
5068 | auto ToLoc = importChecked(Err, From: D->getNameInfo().getLoc()); |
5069 | auto ToUsingLoc = importChecked(Err, From: D->getUsingLoc()); |
5070 | auto ToQualifierLoc = importChecked(Err, From: D->getQualifierLoc()); |
5071 | if (Err) |
5072 | return std::move(Err); |
5073 | |
5074 | DeclarationNameInfo NameInfo(Name, ToLoc); |
5075 | if (Error Err = ImportDeclarationNameLoc(From: D->getNameInfo(), To&: NameInfo)) |
5076 | return std::move(Err); |
5077 | |
5078 | UsingDecl *ToUsing; |
5079 | if (GetImportedOrCreateDecl(ToD&: ToUsing, FromD: D, args&: Importer.getToContext(), args&: DC, |
5080 | args&: ToUsingLoc, args&: ToQualifierLoc, args&: NameInfo, |
5081 | args: D->hasTypename())) |
5082 | return ToUsing; |
5083 | |
5084 | ToUsing->setLexicalDeclContext(LexicalDC); |
5085 | LexicalDC->addDeclInternal(ToUsing); |
5086 | |
5087 | if (NamedDecl *FromPattern = |
5088 | Importer.getFromContext().getInstantiatedFromUsingDecl(D)) { |
5089 | if (Expected<NamedDecl *> ToPatternOrErr = import(From: FromPattern)) |
5090 | Importer.getToContext().setInstantiatedFromUsingDecl( |
5091 | ToUsing, *ToPatternOrErr); |
5092 | else |
5093 | return ToPatternOrErr.takeError(); |
5094 | } |
5095 | |
5096 | return ImportUsingShadowDecls(D, ToUsing); |
5097 | } |
5098 | |
5099 | ExpectedDecl ASTNodeImporter::VisitUsingEnumDecl(UsingEnumDecl *D) { |
5100 | DeclContext *DC, *LexicalDC; |
5101 | DeclarationName Name; |
5102 | SourceLocation Loc; |
5103 | NamedDecl *ToD = nullptr; |
5104 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
5105 | return std::move(Err); |
5106 | if (ToD) |
5107 | return ToD; |
5108 | |
5109 | Error Err = Error::success(); |
5110 | auto ToUsingLoc = importChecked(Err, From: D->getUsingLoc()); |
5111 | auto ToEnumLoc = importChecked(Err, From: D->getEnumLoc()); |
5112 | auto ToNameLoc = importChecked(Err, D->getLocation()); |
5113 | auto *ToEnumType = importChecked(Err, From: D->getEnumType()); |
5114 | if (Err) |
5115 | return std::move(Err); |
5116 | |
5117 | UsingEnumDecl *ToUsingEnum; |
5118 | if (GetImportedOrCreateDecl(ToUsingEnum, D, Importer.getToContext(), DC, |
5119 | ToUsingLoc, ToEnumLoc, ToNameLoc, ToEnumType)) |
5120 | return ToUsingEnum; |
5121 | |
5122 | ToUsingEnum->setLexicalDeclContext(LexicalDC); |
5123 | LexicalDC->addDeclInternal(ToUsingEnum); |
5124 | |
5125 | if (UsingEnumDecl *FromPattern = |
5126 | Importer.getFromContext().getInstantiatedFromUsingEnumDecl(Inst: D)) { |
5127 | if (Expected<UsingEnumDecl *> ToPatternOrErr = import(From: FromPattern)) |
5128 | Importer.getToContext().setInstantiatedFromUsingEnumDecl(Inst: ToUsingEnum, |
5129 | Pattern: *ToPatternOrErr); |
5130 | else |
5131 | return ToPatternOrErr.takeError(); |
5132 | } |
5133 | |
5134 | return ImportUsingShadowDecls(D, ToUsingEnum); |
5135 | } |
5136 | |
5137 | ExpectedDecl ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) { |
5138 | DeclContext *DC, *LexicalDC; |
5139 | DeclarationName Name; |
5140 | SourceLocation Loc; |
5141 | NamedDecl *ToD = nullptr; |
5142 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
5143 | return std::move(Err); |
5144 | if (ToD) |
5145 | return ToD; |
5146 | |
5147 | Expected<BaseUsingDecl *> ToIntroducerOrErr = import(From: D->getIntroducer()); |
5148 | if (!ToIntroducerOrErr) |
5149 | return ToIntroducerOrErr.takeError(); |
5150 | |
5151 | Expected<NamedDecl *> ToTargetOrErr = import(From: D->getTargetDecl()); |
5152 | if (!ToTargetOrErr) |
5153 | return ToTargetOrErr.takeError(); |
5154 | |
5155 | UsingShadowDecl *ToShadow; |
5156 | if (auto *FromConstructorUsingShadow = |
5157 | dyn_cast<ConstructorUsingShadowDecl>(Val: D)) { |
5158 | Error Err = Error::success(); |
5159 | ConstructorUsingShadowDecl *Nominated = importChecked( |
5160 | Err, From: FromConstructorUsingShadow->getNominatedBaseClassShadowDecl()); |
5161 | if (Err) |
5162 | return std::move(Err); |
5163 | // The 'Target' parameter of ConstructorUsingShadowDecl constructor |
5164 | // is really the "NominatedBaseClassShadowDecl" value if it exists |
5165 | // (see code of ConstructorUsingShadowDecl::ConstructorUsingShadowDecl). |
5166 | // We should pass the NominatedBaseClassShadowDecl to it (if non-null) to |
5167 | // get the correct values. |
5168 | if (GetImportedOrCreateDecl<ConstructorUsingShadowDecl>( |
5169 | ToShadow, D, Importer.getToContext(), DC, Loc, |
5170 | cast<UsingDecl>(Val: *ToIntroducerOrErr), |
5171 | Nominated ? Nominated : *ToTargetOrErr, |
5172 | FromConstructorUsingShadow->constructsVirtualBase())) |
5173 | return ToShadow; |
5174 | } else { |
5175 | if (GetImportedOrCreateDecl(ToD&: ToShadow, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, |
5176 | args&: Name, args&: *ToIntroducerOrErr, args&: *ToTargetOrErr)) |
5177 | return ToShadow; |
5178 | } |
5179 | |
5180 | ToShadow->setLexicalDeclContext(LexicalDC); |
5181 | ToShadow->setAccess(D->getAccess()); |
5182 | |
5183 | if (UsingShadowDecl *FromPattern = |
5184 | Importer.getFromContext().getInstantiatedFromUsingShadowDecl(Inst: D)) { |
5185 | if (Expected<UsingShadowDecl *> ToPatternOrErr = import(From: FromPattern)) |
5186 | Importer.getToContext().setInstantiatedFromUsingShadowDecl( |
5187 | Inst: ToShadow, Pattern: *ToPatternOrErr); |
5188 | else |
5189 | // FIXME: We return error here but the definition is already created |
5190 | // and available with lookups. How to fix this?.. |
5191 | return ToPatternOrErr.takeError(); |
5192 | } |
5193 | |
5194 | LexicalDC->addDeclInternal(ToShadow); |
5195 | |
5196 | return ToShadow; |
5197 | } |
5198 | |
5199 | ExpectedDecl ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
5200 | DeclContext *DC, *LexicalDC; |
5201 | DeclarationName Name; |
5202 | SourceLocation Loc; |
5203 | NamedDecl *ToD = nullptr; |
5204 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
5205 | return std::move(Err); |
5206 | if (ToD) |
5207 | return ToD; |
5208 | |
5209 | auto ToComAncestorOrErr = Importer.ImportContext(FromDC: D->getCommonAncestor()); |
5210 | if (!ToComAncestorOrErr) |
5211 | return ToComAncestorOrErr.takeError(); |
5212 | |
5213 | Error Err = Error::success(); |
5214 | auto ToNominatedNamespace = importChecked(Err, From: D->getNominatedNamespace()); |
5215 | auto ToUsingLoc = importChecked(Err, From: D->getUsingLoc()); |
5216 | auto ToNamespaceKeyLocation = |
5217 | importChecked(Err, From: D->getNamespaceKeyLocation()); |
5218 | auto ToQualifierLoc = importChecked(Err, From: D->getQualifierLoc()); |
5219 | auto ToIdentLocation = importChecked(Err, From: D->getIdentLocation()); |
5220 | if (Err) |
5221 | return std::move(Err); |
5222 | |
5223 | UsingDirectiveDecl *ToUsingDir; |
5224 | if (GetImportedOrCreateDecl(ToD&: ToUsingDir, FromD: D, args&: Importer.getToContext(), args&: DC, |
5225 | args&: ToUsingLoc, |
5226 | args&: ToNamespaceKeyLocation, |
5227 | args&: ToQualifierLoc, |
5228 | args&: ToIdentLocation, |
5229 | args&: ToNominatedNamespace, args&: *ToComAncestorOrErr)) |
5230 | return ToUsingDir; |
5231 | |
5232 | ToUsingDir->setLexicalDeclContext(LexicalDC); |
5233 | LexicalDC->addDeclInternal(ToUsingDir); |
5234 | |
5235 | return ToUsingDir; |
5236 | } |
5237 | |
5238 | ExpectedDecl ASTNodeImporter::VisitUsingPackDecl(UsingPackDecl *D) { |
5239 | DeclContext *DC, *LexicalDC; |
5240 | DeclarationName Name; |
5241 | SourceLocation Loc; |
5242 | NamedDecl *ToD = nullptr; |
5243 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
5244 | return std::move(Err); |
5245 | if (ToD) |
5246 | return ToD; |
5247 | |
5248 | auto ToInstantiatedFromUsingOrErr = |
5249 | Importer.Import(D->getInstantiatedFromUsingDecl()); |
5250 | if (!ToInstantiatedFromUsingOrErr) |
5251 | return ToInstantiatedFromUsingOrErr.takeError(); |
5252 | SmallVector<NamedDecl *, 4> Expansions(D->expansions().size()); |
5253 | if (Error Err = ImportArrayChecked(InContainer: D->expansions(), Obegin: Expansions.begin())) |
5254 | return std::move(Err); |
5255 | |
5256 | UsingPackDecl *ToUsingPack; |
5257 | if (GetImportedOrCreateDecl(ToUsingPack, D, Importer.getToContext(), DC, |
5258 | cast<NamedDecl>(*ToInstantiatedFromUsingOrErr), |
5259 | Expansions)) |
5260 | return ToUsingPack; |
5261 | |
5262 | addDeclToContexts(D, ToUsingPack); |
5263 | |
5264 | return ToUsingPack; |
5265 | } |
5266 | |
5267 | ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingValueDecl( |
5268 | UnresolvedUsingValueDecl *D) { |
5269 | DeclContext *DC, *LexicalDC; |
5270 | DeclarationName Name; |
5271 | SourceLocation Loc; |
5272 | NamedDecl *ToD = nullptr; |
5273 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
5274 | return std::move(Err); |
5275 | if (ToD) |
5276 | return ToD; |
5277 | |
5278 | Error Err = Error::success(); |
5279 | auto ToLoc = importChecked(Err, From: D->getNameInfo().getLoc()); |
5280 | auto ToUsingLoc = importChecked(Err, From: D->getUsingLoc()); |
5281 | auto ToQualifierLoc = importChecked(Err, From: D->getQualifierLoc()); |
5282 | auto ToEllipsisLoc = importChecked(Err, From: D->getEllipsisLoc()); |
5283 | if (Err) |
5284 | return std::move(Err); |
5285 | |
5286 | DeclarationNameInfo NameInfo(Name, ToLoc); |
5287 | if (Error Err = ImportDeclarationNameLoc(From: D->getNameInfo(), To&: NameInfo)) |
5288 | return std::move(Err); |
5289 | |
5290 | UnresolvedUsingValueDecl *ToUsingValue; |
5291 | if (GetImportedOrCreateDecl(ToD&: ToUsingValue, FromD: D, args&: Importer.getToContext(), args&: DC, |
5292 | args&: ToUsingLoc, args&: ToQualifierLoc, args&: NameInfo, |
5293 | args&: ToEllipsisLoc)) |
5294 | return ToUsingValue; |
5295 | |
5296 | ToUsingValue->setAccess(D->getAccess()); |
5297 | ToUsingValue->setLexicalDeclContext(LexicalDC); |
5298 | LexicalDC->addDeclInternal(ToUsingValue); |
5299 | |
5300 | return ToUsingValue; |
5301 | } |
5302 | |
5303 | ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingTypenameDecl( |
5304 | UnresolvedUsingTypenameDecl *D) { |
5305 | DeclContext *DC, *LexicalDC; |
5306 | DeclarationName Name; |
5307 | SourceLocation Loc; |
5308 | NamedDecl *ToD = nullptr; |
5309 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
5310 | return std::move(Err); |
5311 | if (ToD) |
5312 | return ToD; |
5313 | |
5314 | Error Err = Error::success(); |
5315 | auto ToUsingLoc = importChecked(Err, From: D->getUsingLoc()); |
5316 | auto ToTypenameLoc = importChecked(Err, From: D->getTypenameLoc()); |
5317 | auto ToQualifierLoc = importChecked(Err, From: D->getQualifierLoc()); |
5318 | auto ToEllipsisLoc = importChecked(Err, From: D->getEllipsisLoc()); |
5319 | if (Err) |
5320 | return std::move(Err); |
5321 | |
5322 | UnresolvedUsingTypenameDecl *ToUsing; |
5323 | if (GetImportedOrCreateDecl(ToD&: ToUsing, FromD: D, args&: Importer.getToContext(), args&: DC, |
5324 | args&: ToUsingLoc, args&: ToTypenameLoc, |
5325 | args&: ToQualifierLoc, args&: Loc, args&: Name, args&: ToEllipsisLoc)) |
5326 | return ToUsing; |
5327 | |
5328 | ToUsing->setAccess(D->getAccess()); |
5329 | ToUsing->setLexicalDeclContext(LexicalDC); |
5330 | LexicalDC->addDeclInternal(ToUsing); |
5331 | |
5332 | return ToUsing; |
5333 | } |
5334 | |
5335 | ExpectedDecl ASTNodeImporter::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) { |
5336 | Decl* ToD = nullptr; |
5337 | switch (D->getBuiltinTemplateKind()) { |
5338 | case BuiltinTemplateKind::BTK__make_integer_seq: |
5339 | ToD = Importer.getToContext().getMakeIntegerSeqDecl(); |
5340 | break; |
5341 | case BuiltinTemplateKind::BTK__type_pack_element: |
5342 | ToD = Importer.getToContext().getTypePackElementDecl(); |
5343 | break; |
5344 | } |
5345 | assert(ToD && "BuiltinTemplateDecl of unsupported kind!" ); |
5346 | Importer.MapImported(D, ToD); |
5347 | return ToD; |
5348 | } |
5349 | |
5350 | Error ASTNodeImporter::ImportDefinition( |
5351 | ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, ImportDefinitionKind Kind) { |
5352 | if (To->getDefinition()) { |
5353 | // Check consistency of superclass. |
5354 | ObjCInterfaceDecl *FromSuper = From->getSuperClass(); |
5355 | if (FromSuper) { |
5356 | if (auto FromSuperOrErr = import(From: FromSuper)) |
5357 | FromSuper = *FromSuperOrErr; |
5358 | else |
5359 | return FromSuperOrErr.takeError(); |
5360 | } |
5361 | |
5362 | ObjCInterfaceDecl *ToSuper = To->getSuperClass(); |
5363 | if ((bool)FromSuper != (bool)ToSuper || |
5364 | (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) { |
5365 | Importer.ToDiag(To->getLocation(), |
5366 | diag::warn_odr_objc_superclass_inconsistent) |
5367 | << To->getDeclName(); |
5368 | if (ToSuper) |
5369 | Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass) |
5370 | << To->getSuperClass()->getDeclName(); |
5371 | else |
5372 | Importer.ToDiag(To->getLocation(), |
5373 | diag::note_odr_objc_missing_superclass); |
5374 | if (From->getSuperClass()) |
5375 | Importer.FromDiag(From->getSuperClassLoc(), |
5376 | diag::note_odr_objc_superclass) |
5377 | << From->getSuperClass()->getDeclName(); |
5378 | else |
5379 | Importer.FromDiag(From->getLocation(), |
5380 | diag::note_odr_objc_missing_superclass); |
5381 | } |
5382 | |
5383 | if (shouldForceImportDeclContext(IDK: Kind)) |
5384 | if (Error Err = ImportDeclContext(From)) |
5385 | return Err; |
5386 | return Error::success(); |
5387 | } |
5388 | |
5389 | // Start the definition. |
5390 | To->startDefinition(); |
5391 | |
5392 | // If this class has a superclass, import it. |
5393 | if (From->getSuperClass()) { |
5394 | if (auto SuperTInfoOrErr = import(From: From->getSuperClassTInfo())) |
5395 | To->setSuperClass(*SuperTInfoOrErr); |
5396 | else |
5397 | return SuperTInfoOrErr.takeError(); |
5398 | } |
5399 | |
5400 | // Import protocols |
5401 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
5402 | SmallVector<SourceLocation, 4> ProtocolLocs; |
5403 | ObjCInterfaceDecl::protocol_loc_iterator FromProtoLoc = |
5404 | From->protocol_loc_begin(); |
5405 | |
5406 | for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(), |
5407 | FromProtoEnd = From->protocol_end(); |
5408 | FromProto != FromProtoEnd; |
5409 | ++FromProto, ++FromProtoLoc) { |
5410 | if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(From: *FromProto)) |
5411 | Protocols.push_back(Elt: *ToProtoOrErr); |
5412 | else |
5413 | return ToProtoOrErr.takeError(); |
5414 | |
5415 | if (ExpectedSLoc ToProtoLocOrErr = import(From: *FromProtoLoc)) |
5416 | ProtocolLocs.push_back(Elt: *ToProtoLocOrErr); |
5417 | else |
5418 | return ToProtoLocOrErr.takeError(); |
5419 | |
5420 | } |
5421 | |
5422 | // FIXME: If we're merging, make sure that the protocol list is the same. |
5423 | To->setProtocolList(List: Protocols.data(), Num: Protocols.size(), |
5424 | Locs: ProtocolLocs.data(), C&: Importer.getToContext()); |
5425 | |
5426 | // Import categories. When the categories themselves are imported, they'll |
5427 | // hook themselves into this interface. |
5428 | for (auto *Cat : From->known_categories()) { |
5429 | auto ToCatOrErr = import(From: Cat); |
5430 | if (!ToCatOrErr) |
5431 | return ToCatOrErr.takeError(); |
5432 | } |
5433 | |
5434 | // If we have an @implementation, import it as well. |
5435 | if (From->getImplementation()) { |
5436 | if (Expected<ObjCImplementationDecl *> ToImplOrErr = |
5437 | import(From: From->getImplementation())) |
5438 | To->setImplementation(*ToImplOrErr); |
5439 | else |
5440 | return ToImplOrErr.takeError(); |
5441 | } |
5442 | |
5443 | // Import all of the members of this class. |
5444 | if (Error Err = ImportDeclContext(From, /*ForceImport=*/true)) |
5445 | return Err; |
5446 | |
5447 | return Error::success(); |
5448 | } |
5449 | |
5450 | Expected<ObjCTypeParamList *> |
5451 | ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) { |
5452 | if (!list) |
5453 | return nullptr; |
5454 | |
5455 | SmallVector<ObjCTypeParamDecl *, 4> toTypeParams; |
5456 | for (auto *fromTypeParam : *list) { |
5457 | if (auto toTypeParamOrErr = import(From: fromTypeParam)) |
5458 | toTypeParams.push_back(Elt: *toTypeParamOrErr); |
5459 | else |
5460 | return toTypeParamOrErr.takeError(); |
5461 | } |
5462 | |
5463 | auto LAngleLocOrErr = import(From: list->getLAngleLoc()); |
5464 | if (!LAngleLocOrErr) |
5465 | return LAngleLocOrErr.takeError(); |
5466 | |
5467 | auto RAngleLocOrErr = import(From: list->getRAngleLoc()); |
5468 | if (!RAngleLocOrErr) |
5469 | return RAngleLocOrErr.takeError(); |
5470 | |
5471 | return ObjCTypeParamList::create(ctx&: Importer.getToContext(), |
5472 | lAngleLoc: *LAngleLocOrErr, |
5473 | typeParams: toTypeParams, |
5474 | rAngleLoc: *RAngleLocOrErr); |
5475 | } |
5476 | |
5477 | ExpectedDecl ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
5478 | // If this class has a definition in the translation unit we're coming from, |
5479 | // but this particular declaration is not that definition, import the |
5480 | // definition and map to that. |
5481 | ObjCInterfaceDecl *Definition = D->getDefinition(); |
5482 | if (Definition && Definition != D) { |
5483 | if (ExpectedDecl ImportedDefOrErr = import(From: Definition)) |
5484 | return Importer.MapImported(D, *ImportedDefOrErr); |
5485 | else |
5486 | return ImportedDefOrErr.takeError(); |
5487 | } |
5488 | |
5489 | // Import the major distinguishing characteristics of an @interface. |
5490 | DeclContext *DC, *LexicalDC; |
5491 | DeclarationName Name; |
5492 | SourceLocation Loc; |
5493 | NamedDecl *ToD; |
5494 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
5495 | return std::move(Err); |
5496 | if (ToD) |
5497 | return ToD; |
5498 | |
5499 | // Look for an existing interface with the same name. |
5500 | ObjCInterfaceDecl *MergeWithIface = nullptr; |
5501 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
5502 | for (auto *FoundDecl : FoundDecls) { |
5503 | if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
5504 | continue; |
5505 | |
5506 | if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(Val: FoundDecl))) |
5507 | break; |
5508 | } |
5509 | |
5510 | // Create an interface declaration, if one does not already exist. |
5511 | ObjCInterfaceDecl *ToIface = MergeWithIface; |
5512 | if (!ToIface) { |
5513 | ExpectedSLoc AtBeginLocOrErr = import(D->getAtStartLoc()); |
5514 | if (!AtBeginLocOrErr) |
5515 | return AtBeginLocOrErr.takeError(); |
5516 | |
5517 | if (GetImportedOrCreateDecl( |
5518 | ToD&: ToIface, FromD: D, args&: Importer.getToContext(), args&: DC, |
5519 | args&: *AtBeginLocOrErr, args: Name.getAsIdentifierInfo(), |
5520 | /*TypeParamList=*/args: nullptr, |
5521 | /*PrevDecl=*/args: nullptr, args&: Loc, args: D->isImplicitInterfaceDecl())) |
5522 | return ToIface; |
5523 | ToIface->setLexicalDeclContext(LexicalDC); |
5524 | LexicalDC->addDeclInternal(ToIface); |
5525 | } |
5526 | Importer.MapImported(D, ToIface); |
5527 | // Import the type parameter list after MapImported, to avoid |
5528 | // loops when bringing in their DeclContext. |
5529 | if (auto ToPListOrErr = |
5530 | ImportObjCTypeParamList(list: D->getTypeParamListAsWritten())) |
5531 | ToIface->setTypeParamList(*ToPListOrErr); |
5532 | else |
5533 | return ToPListOrErr.takeError(); |
5534 | |
5535 | if (D->isThisDeclarationADefinition()) |
5536 | if (Error Err = ImportDefinition(From: D, To: ToIface)) |
5537 | return std::move(Err); |
5538 | |
5539 | return ToIface; |
5540 | } |
5541 | |
5542 | ExpectedDecl |
5543 | ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
5544 | ObjCCategoryDecl *Category; |
5545 | if (Error Err = importInto(To&: Category, From: D->getCategoryDecl())) |
5546 | return std::move(Err); |
5547 | |
5548 | ObjCCategoryImplDecl *ToImpl = Category->getImplementation(); |
5549 | if (!ToImpl) { |
5550 | DeclContext *DC, *LexicalDC; |
5551 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) |
5552 | return std::move(Err); |
5553 | |
5554 | Error Err = Error::success(); |
5555 | auto ToLocation = importChecked(Err, D->getLocation()); |
5556 | auto ToAtStartLoc = importChecked(Err, D->getAtStartLoc()); |
5557 | auto ToCategoryNameLoc = importChecked(Err, From: D->getCategoryNameLoc()); |
5558 | if (Err) |
5559 | return std::move(Err); |
5560 | |
5561 | if (GetImportedOrCreateDecl( |
5562 | ToImpl, D, Importer.getToContext(), DC, |
5563 | Importer.Import(D->getIdentifier()), Category->getClassInterface(), |
5564 | ToLocation, ToAtStartLoc, ToCategoryNameLoc)) |
5565 | return ToImpl; |
5566 | |
5567 | ToImpl->setLexicalDeclContext(LexicalDC); |
5568 | LexicalDC->addDeclInternal(ToImpl); |
5569 | Category->setImplementation(ToImpl); |
5570 | } |
5571 | |
5572 | Importer.MapImported(D, ToImpl); |
5573 | if (Error Err = ImportDeclContext(D)) |
5574 | return std::move(Err); |
5575 | |
5576 | return ToImpl; |
5577 | } |
5578 | |
5579 | ExpectedDecl |
5580 | ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
5581 | // Find the corresponding interface. |
5582 | ObjCInterfaceDecl *Iface; |
5583 | if (Error Err = importInto(Iface, D->getClassInterface())) |
5584 | return std::move(Err); |
5585 | |
5586 | // Import the superclass, if any. |
5587 | ObjCInterfaceDecl *Super; |
5588 | if (Error Err = importInto(To&: Super, From: D->getSuperClass())) |
5589 | return std::move(Err); |
5590 | |
5591 | ObjCImplementationDecl *Impl = Iface->getImplementation(); |
5592 | if (!Impl) { |
5593 | // We haven't imported an implementation yet. Create a new @implementation |
5594 | // now. |
5595 | DeclContext *DC, *LexicalDC; |
5596 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) |
5597 | return std::move(Err); |
5598 | |
5599 | Error Err = Error::success(); |
5600 | auto ToLocation = importChecked(Err, D->getLocation()); |
5601 | auto ToAtStartLoc = importChecked(Err, D->getAtStartLoc()); |
5602 | auto ToSuperClassLoc = importChecked(Err, From: D->getSuperClassLoc()); |
5603 | auto ToIvarLBraceLoc = importChecked(Err, From: D->getIvarLBraceLoc()); |
5604 | auto ToIvarRBraceLoc = importChecked(Err, From: D->getIvarRBraceLoc()); |
5605 | if (Err) |
5606 | return std::move(Err); |
5607 | |
5608 | if (GetImportedOrCreateDecl(Impl, D, Importer.getToContext(), |
5609 | DC, Iface, Super, |
5610 | ToLocation, |
5611 | ToAtStartLoc, |
5612 | ToSuperClassLoc, |
5613 | ToIvarLBraceLoc, |
5614 | ToIvarRBraceLoc)) |
5615 | return Impl; |
5616 | |
5617 | Impl->setLexicalDeclContext(LexicalDC); |
5618 | |
5619 | // Associate the implementation with the class it implements. |
5620 | Iface->setImplementation(Impl); |
5621 | Importer.MapImported(D, Iface->getImplementation()); |
5622 | } else { |
5623 | Importer.MapImported(D, Iface->getImplementation()); |
5624 | |
5625 | // Verify that the existing @implementation has the same superclass. |
5626 | if ((Super && !Impl->getSuperClass()) || |
5627 | (!Super && Impl->getSuperClass()) || |
5628 | (Super && Impl->getSuperClass() && |
5629 | !declaresSameEntity(Super->getCanonicalDecl(), |
5630 | Impl->getSuperClass()))) { |
5631 | Importer.ToDiag(Impl->getLocation(), |
5632 | diag::warn_odr_objc_superclass_inconsistent) |
5633 | << Iface->getDeclName(); |
5634 | // FIXME: It would be nice to have the location of the superclass |
5635 | // below. |
5636 | if (Impl->getSuperClass()) |
5637 | Importer.ToDiag(Impl->getLocation(), |
5638 | diag::note_odr_objc_superclass) |
5639 | << Impl->getSuperClass()->getDeclName(); |
5640 | else |
5641 | Importer.ToDiag(Impl->getLocation(), |
5642 | diag::note_odr_objc_missing_superclass); |
5643 | if (D->getSuperClass()) |
5644 | Importer.FromDiag(D->getLocation(), |
5645 | diag::note_odr_objc_superclass) |
5646 | << D->getSuperClass()->getDeclName(); |
5647 | else |
5648 | Importer.FromDiag(D->getLocation(), |
5649 | diag::note_odr_objc_missing_superclass); |
5650 | |
5651 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
5652 | } |
5653 | } |
5654 | |
5655 | // Import all of the members of this @implementation. |
5656 | if (Error Err = ImportDeclContext(D)) |
5657 | return std::move(Err); |
5658 | |
5659 | return Impl; |
5660 | } |
5661 | |
5662 | ExpectedDecl ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { |
5663 | // Import the major distinguishing characteristics of an @property. |
5664 | DeclContext *DC, *LexicalDC; |
5665 | DeclarationName Name; |
5666 | SourceLocation Loc; |
5667 | NamedDecl *ToD; |
5668 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
5669 | return std::move(Err); |
5670 | if (ToD) |
5671 | return ToD; |
5672 | |
5673 | // Check whether we have already imported this property. |
5674 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
5675 | for (auto *FoundDecl : FoundDecls) { |
5676 | if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(Val: FoundDecl)) { |
5677 | // Instance and class properties can share the same name but are different |
5678 | // declarations. |
5679 | if (FoundProp->isInstanceProperty() != D->isInstanceProperty()) |
5680 | continue; |
5681 | |
5682 | // Check property types. |
5683 | if (!Importer.IsStructurallyEquivalent(From: D->getType(), |
5684 | To: FoundProp->getType())) { |
5685 | Importer.ToDiag(Loc, diag::warn_odr_objc_property_type_inconsistent) |
5686 | << Name << D->getType() << FoundProp->getType(); |
5687 | Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here) |
5688 | << FoundProp->getType(); |
5689 | |
5690 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
5691 | } |
5692 | |
5693 | // FIXME: Check property attributes, getters, setters, etc.? |
5694 | |
5695 | // Consider these properties to be equivalent. |
5696 | Importer.MapImported(D, FoundProp); |
5697 | return FoundProp; |
5698 | } |
5699 | } |
5700 | |
5701 | Error Err = Error::success(); |
5702 | auto ToType = importChecked(Err, From: D->getType()); |
5703 | auto ToTypeSourceInfo = importChecked(Err, From: D->getTypeSourceInfo()); |
5704 | auto ToAtLoc = importChecked(Err, From: D->getAtLoc()); |
5705 | auto ToLParenLoc = importChecked(Err, From: D->getLParenLoc()); |
5706 | if (Err) |
5707 | return std::move(Err); |
5708 | |
5709 | // Create the new property. |
5710 | ObjCPropertyDecl *ToProperty; |
5711 | if (GetImportedOrCreateDecl( |
5712 | ToD&: ToProperty, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, |
5713 | args: Name.getAsIdentifierInfo(), args&: ToAtLoc, |
5714 | args&: ToLParenLoc, args&: ToType, |
5715 | args&: ToTypeSourceInfo, args: D->getPropertyImplementation())) |
5716 | return ToProperty; |
5717 | |
5718 | auto ToGetterName = importChecked(Err, From: D->getGetterName()); |
5719 | auto ToSetterName = importChecked(Err, From: D->getSetterName()); |
5720 | auto ToGetterNameLoc = importChecked(Err, From: D->getGetterNameLoc()); |
5721 | auto ToSetterNameLoc = importChecked(Err, From: D->getSetterNameLoc()); |
5722 | auto ToGetterMethodDecl = importChecked(Err, From: D->getGetterMethodDecl()); |
5723 | auto ToSetterMethodDecl = importChecked(Err, From: D->getSetterMethodDecl()); |
5724 | auto ToPropertyIvarDecl = importChecked(Err, From: D->getPropertyIvarDecl()); |
5725 | if (Err) |
5726 | return std::move(Err); |
5727 | |
5728 | ToProperty->setLexicalDeclContext(LexicalDC); |
5729 | LexicalDC->addDeclInternal(ToProperty); |
5730 | |
5731 | ToProperty->setPropertyAttributes(D->getPropertyAttributes()); |
5732 | ToProperty->setPropertyAttributesAsWritten( |
5733 | D->getPropertyAttributesAsWritten()); |
5734 | ToProperty->setGetterName(Sel: ToGetterName, Loc: ToGetterNameLoc); |
5735 | ToProperty->setSetterName(Sel: ToSetterName, Loc: ToSetterNameLoc); |
5736 | ToProperty->setGetterMethodDecl(ToGetterMethodDecl); |
5737 | ToProperty->setSetterMethodDecl(ToSetterMethodDecl); |
5738 | ToProperty->setPropertyIvarDecl(ToPropertyIvarDecl); |
5739 | return ToProperty; |
5740 | } |
5741 | |
5742 | ExpectedDecl |
5743 | ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { |
5744 | ObjCPropertyDecl *Property; |
5745 | if (Error Err = importInto(To&: Property, From: D->getPropertyDecl())) |
5746 | return std::move(Err); |
5747 | |
5748 | DeclContext *DC, *LexicalDC; |
5749 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) |
5750 | return std::move(Err); |
5751 | |
5752 | auto *InImpl = cast<ObjCImplDecl>(Val: LexicalDC); |
5753 | |
5754 | // Import the ivar (for an @synthesize). |
5755 | ObjCIvarDecl *Ivar = nullptr; |
5756 | if (Error Err = importInto(To&: Ivar, From: D->getPropertyIvarDecl())) |
5757 | return std::move(Err); |
5758 | |
5759 | ObjCPropertyImplDecl *ToImpl |
5760 | = InImpl->FindPropertyImplDecl(propertyId: Property->getIdentifier(), |
5761 | queryKind: Property->getQueryKind()); |
5762 | if (!ToImpl) { |
5763 | |
5764 | Error Err = Error::success(); |
5765 | auto ToBeginLoc = importChecked(Err, From: D->getBeginLoc()); |
5766 | auto ToLocation = importChecked(Err, D->getLocation()); |
5767 | auto ToPropertyIvarDeclLoc = |
5768 | importChecked(Err, From: D->getPropertyIvarDeclLoc()); |
5769 | if (Err) |
5770 | return std::move(Err); |
5771 | |
5772 | if (GetImportedOrCreateDecl(ToImpl, D, Importer.getToContext(), DC, |
5773 | ToBeginLoc, |
5774 | ToLocation, Property, |
5775 | D->getPropertyImplementation(), Ivar, |
5776 | ToPropertyIvarDeclLoc)) |
5777 | return ToImpl; |
5778 | |
5779 | ToImpl->setLexicalDeclContext(LexicalDC); |
5780 | LexicalDC->addDeclInternal(ToImpl); |
5781 | } else { |
5782 | // Check that we have the same kind of property implementation (@synthesize |
5783 | // vs. @dynamic). |
5784 | if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) { |
5785 | Importer.ToDiag(ToImpl->getLocation(), |
5786 | diag::warn_odr_objc_property_impl_kind_inconsistent) |
5787 | << Property->getDeclName() |
5788 | << (ToImpl->getPropertyImplementation() |
5789 | == ObjCPropertyImplDecl::Dynamic); |
5790 | Importer.FromDiag(D->getLocation(), |
5791 | diag::note_odr_objc_property_impl_kind) |
5792 | << D->getPropertyDecl()->getDeclName() |
5793 | << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); |
5794 | |
5795 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
5796 | } |
5797 | |
5798 | // For @synthesize, check that we have the same |
5799 | if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize && |
5800 | Ivar != ToImpl->getPropertyIvarDecl()) { |
5801 | Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(), |
5802 | diag::warn_odr_objc_synthesize_ivar_inconsistent) |
5803 | << Property->getDeclName() |
5804 | << ToImpl->getPropertyIvarDecl()->getDeclName() |
5805 | << Ivar->getDeclName(); |
5806 | Importer.FromDiag(D->getPropertyIvarDeclLoc(), |
5807 | diag::note_odr_objc_synthesize_ivar_here) |
5808 | << D->getPropertyIvarDecl()->getDeclName(); |
5809 | |
5810 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
5811 | } |
5812 | |
5813 | // Merge the existing implementation with the new implementation. |
5814 | Importer.MapImported(D, ToImpl); |
5815 | } |
5816 | |
5817 | return ToImpl; |
5818 | } |
5819 | |
5820 | ExpectedDecl |
5821 | ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
5822 | // For template arguments, we adopt the translation unit as our declaration |
5823 | // context. This context will be fixed when the actual template declaration |
5824 | // is created. |
5825 | |
5826 | ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc()); |
5827 | if (!BeginLocOrErr) |
5828 | return BeginLocOrErr.takeError(); |
5829 | |
5830 | ExpectedSLoc LocationOrErr = import(D->getLocation()); |
5831 | if (!LocationOrErr) |
5832 | return LocationOrErr.takeError(); |
5833 | |
5834 | TemplateTypeParmDecl *ToD = nullptr; |
5835 | if (GetImportedOrCreateDecl( |
5836 | ToD, D, Importer.getToContext(), |
5837 | Importer.getToContext().getTranslationUnitDecl(), |
5838 | *BeginLocOrErr, *LocationOrErr, |
5839 | D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()), |
5840 | D->wasDeclaredWithTypename(), D->isParameterPack(), |
5841 | D->hasTypeConstraint())) |
5842 | return ToD; |
5843 | |
5844 | // Import the type-constraint |
5845 | if (const TypeConstraint *TC = D->getTypeConstraint()) { |
5846 | |
5847 | Error Err = Error::success(); |
5848 | auto ToConceptRef = importChecked(Err, From: TC->getConceptReference()); |
5849 | auto ToIDC = importChecked(Err, From: TC->getImmediatelyDeclaredConstraint()); |
5850 | if (Err) |
5851 | return std::move(Err); |
5852 | |
5853 | ToD->setTypeConstraint(CR: ToConceptRef, ImmediatelyDeclaredConstraint: ToIDC); |
5854 | } |
5855 | |
5856 | if (D->hasDefaultArgument()) { |
5857 | Expected<TypeSourceInfo *> ToDefaultArgOrErr = |
5858 | import(From: D->getDefaultArgumentInfo()); |
5859 | if (!ToDefaultArgOrErr) |
5860 | return ToDefaultArgOrErr.takeError(); |
5861 | ToD->setDefaultArgument(*ToDefaultArgOrErr); |
5862 | } |
5863 | |
5864 | return ToD; |
5865 | } |
5866 | |
5867 | ExpectedDecl |
5868 | ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
5869 | |
5870 | Error Err = Error::success(); |
5871 | auto ToDeclName = importChecked(Err, D->getDeclName()); |
5872 | auto ToLocation = importChecked(Err, D->getLocation()); |
5873 | auto ToType = importChecked(Err, D->getType()); |
5874 | auto ToTypeSourceInfo = importChecked(Err, D->getTypeSourceInfo()); |
5875 | auto ToInnerLocStart = importChecked(Err, D->getInnerLocStart()); |
5876 | if (Err) |
5877 | return std::move(Err); |
5878 | |
5879 | NonTypeTemplateParmDecl *ToD = nullptr; |
5880 | if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), |
5881 | Importer.getToContext().getTranslationUnitDecl(), |
5882 | ToInnerLocStart, ToLocation, D->getDepth(), |
5883 | D->getPosition(), |
5884 | ToDeclName.getAsIdentifierInfo(), ToType, |
5885 | D->isParameterPack(), ToTypeSourceInfo)) |
5886 | return ToD; |
5887 | |
5888 | if (D->hasDefaultArgument()) { |
5889 | ExpectedExpr ToDefaultArgOrErr = import(From: D->getDefaultArgument()); |
5890 | if (!ToDefaultArgOrErr) |
5891 | return ToDefaultArgOrErr.takeError(); |
5892 | ToD->setDefaultArgument(*ToDefaultArgOrErr); |
5893 | } |
5894 | |
5895 | return ToD; |
5896 | } |
5897 | |
5898 | ExpectedDecl |
5899 | ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
5900 | // Import the name of this declaration. |
5901 | auto NameOrErr = import(D->getDeclName()); |
5902 | if (!NameOrErr) |
5903 | return NameOrErr.takeError(); |
5904 | |
5905 | // Import the location of this declaration. |
5906 | ExpectedSLoc LocationOrErr = import(D->getLocation()); |
5907 | if (!LocationOrErr) |
5908 | return LocationOrErr.takeError(); |
5909 | |
5910 | // Import template parameters. |
5911 | auto TemplateParamsOrErr = import(D->getTemplateParameters()); |
5912 | if (!TemplateParamsOrErr) |
5913 | return TemplateParamsOrErr.takeError(); |
5914 | |
5915 | TemplateTemplateParmDecl *ToD = nullptr; |
5916 | if (GetImportedOrCreateDecl( |
5917 | ToD, D, Importer.getToContext(), |
5918 | Importer.getToContext().getTranslationUnitDecl(), *LocationOrErr, |
5919 | D->getDepth(), D->getPosition(), D->isParameterPack(), |
5920 | (*NameOrErr).getAsIdentifierInfo(), *TemplateParamsOrErr)) |
5921 | return ToD; |
5922 | |
5923 | if (D->hasDefaultArgument()) { |
5924 | Expected<TemplateArgumentLoc> ToDefaultArgOrErr = |
5925 | import(From: D->getDefaultArgument()); |
5926 | if (!ToDefaultArgOrErr) |
5927 | return ToDefaultArgOrErr.takeError(); |
5928 | ToD->setDefaultArgument(C: Importer.getToContext(), DefArg: *ToDefaultArgOrErr); |
5929 | } |
5930 | |
5931 | return ToD; |
5932 | } |
5933 | |
5934 | // Returns the definition for a (forward) declaration of a TemplateDecl, if |
5935 | // it has any definition in the redecl chain. |
5936 | template <typename T> static auto getTemplateDefinition(T *D) -> T * { |
5937 | assert(D->getTemplatedDecl() && "Should be called on templates only" ); |
5938 | auto *ToTemplatedDef = D->getTemplatedDecl()->getDefinition(); |
5939 | if (!ToTemplatedDef) |
5940 | return nullptr; |
5941 | auto *TemplateWithDef = ToTemplatedDef->getDescribedTemplate(); |
5942 | return cast_or_null<T>(TemplateWithDef); |
5943 | } |
5944 | |
5945 | ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
5946 | |
5947 | // Import the major distinguishing characteristics of this class template. |
5948 | DeclContext *DC, *LexicalDC; |
5949 | DeclarationName Name; |
5950 | SourceLocation Loc; |
5951 | NamedDecl *ToD; |
5952 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
5953 | return std::move(Err); |
5954 | if (ToD) |
5955 | return ToD; |
5956 | |
5957 | // Should check if a declaration is friend in a dependent context. |
5958 | // Such templates are not linked together in a declaration chain. |
5959 | // The ASTImporter strategy is to map existing forward declarations to |
5960 | // imported ones only if strictly necessary, otherwise import these as new |
5961 | // forward declarations. In case of the "dependent friend" declarations, new |
5962 | // declarations are created, but not linked in a declaration chain. |
5963 | auto IsDependentFriend = [](ClassTemplateDecl *TD) { |
5964 | return TD->getFriendObjectKind() != Decl::FOK_None && |
5965 | TD->getLexicalDeclContext()->isDependentContext(); |
5966 | }; |
5967 | bool DependentFriend = IsDependentFriend(D); |
5968 | |
5969 | ClassTemplateDecl *FoundByLookup = nullptr; |
5970 | |
5971 | // We may already have a template of the same name; try to find and match it. |
5972 | if (!DC->isFunctionOrMethod()) { |
5973 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
5974 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
5975 | for (auto *FoundDecl : FoundDecls) { |
5976 | if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary | |
5977 | Decl::IDNS_TagFriend)) |
5978 | continue; |
5979 | |
5980 | Decl *Found = FoundDecl; |
5981 | auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Val: Found); |
5982 | if (FoundTemplate) { |
5983 | if (!hasSameVisibilityContextAndLinkage(FoundTemplate, D)) |
5984 | continue; |
5985 | |
5986 | // FIXME: sufficient conditon for 'IgnoreTemplateParmDepth'? |
5987 | bool IgnoreTemplateParmDepth = |
5988 | (FoundTemplate->getFriendObjectKind() != Decl::FOK_None) != |
5989 | (D->getFriendObjectKind() != Decl::FOK_None); |
5990 | if (IsStructuralMatch(From: D, To: FoundTemplate, /*Complain=*/true, |
5991 | IgnoreTemplateParmDepth)) { |
5992 | if (DependentFriend || IsDependentFriend(FoundTemplate)) |
5993 | continue; |
5994 | |
5995 | ClassTemplateDecl *TemplateWithDef = |
5996 | getTemplateDefinition(FoundTemplate); |
5997 | if (D->isThisDeclarationADefinition() && TemplateWithDef) |
5998 | return Importer.MapImported(D, TemplateWithDef); |
5999 | if (!FoundByLookup) |
6000 | FoundByLookup = FoundTemplate; |
6001 | // Search in all matches because there may be multiple decl chains, |
6002 | // see ASTTests test ImportExistingFriendClassTemplateDef. |
6003 | continue; |
6004 | } |
6005 | ConflictingDecls.push_back(Elt: FoundDecl); |
6006 | } |
6007 | } |
6008 | |
6009 | if (!ConflictingDecls.empty()) { |
6010 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
6011 | Name, DC, IDNS: Decl::IDNS_Ordinary, Decls: ConflictingDecls.data(), |
6012 | NumDecls: ConflictingDecls.size()); |
6013 | if (NameOrErr) |
6014 | Name = NameOrErr.get(); |
6015 | else |
6016 | return NameOrErr.takeError(); |
6017 | } |
6018 | } |
6019 | |
6020 | CXXRecordDecl *FromTemplated = D->getTemplatedDecl(); |
6021 | |
6022 | auto TemplateParamsOrErr = import(D->getTemplateParameters()); |
6023 | if (!TemplateParamsOrErr) |
6024 | return TemplateParamsOrErr.takeError(); |
6025 | |
6026 | // Create the declaration that is being templated. |
6027 | CXXRecordDecl *ToTemplated; |
6028 | if (Error Err = importInto(To&: ToTemplated, From: FromTemplated)) |
6029 | return std::move(Err); |
6030 | |
6031 | // Create the class template declaration itself. |
6032 | ClassTemplateDecl *D2; |
6033 | if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name, |
6034 | *TemplateParamsOrErr, ToTemplated)) |
6035 | return D2; |
6036 | |
6037 | ToTemplated->setDescribedClassTemplate(D2); |
6038 | |
6039 | D2->setAccess(D->getAccess()); |
6040 | D2->setLexicalDeclContext(LexicalDC); |
6041 | |
6042 | addDeclToContexts(D, D2); |
6043 | updateLookupTableForTemplateParameters(**TemplateParamsOrErr); |
6044 | |
6045 | if (FoundByLookup) { |
6046 | auto *Recent = |
6047 | const_cast<ClassTemplateDecl *>(FoundByLookup->getMostRecentDecl()); |
6048 | |
6049 | // It is possible that during the import of the class template definition |
6050 | // we start the import of a fwd friend decl of the very same class template |
6051 | // and we add the fwd friend decl to the lookup table. But the ToTemplated |
6052 | // had been created earlier and by that time the lookup could not find |
6053 | // anything existing, so it has no previous decl. Later, (still during the |
6054 | // import of the fwd friend decl) we start to import the definition again |
6055 | // and this time the lookup finds the previous fwd friend class template. |
6056 | // In this case we must set up the previous decl for the templated decl. |
6057 | if (!ToTemplated->getPreviousDecl()) { |
6058 | assert(FoundByLookup->getTemplatedDecl() && |
6059 | "Found decl must have its templated decl set" ); |
6060 | CXXRecordDecl *PrevTemplated = |
6061 | FoundByLookup->getTemplatedDecl()->getMostRecentDecl(); |
6062 | if (ToTemplated != PrevTemplated) |
6063 | ToTemplated->setPreviousDecl(PrevTemplated); |
6064 | } |
6065 | |
6066 | D2->setPreviousDecl(Recent); |
6067 | } |
6068 | |
6069 | return D2; |
6070 | } |
6071 | |
6072 | ExpectedDecl ASTNodeImporter::VisitClassTemplateSpecializationDecl( |
6073 | ClassTemplateSpecializationDecl *D) { |
6074 | ClassTemplateDecl *ClassTemplate; |
6075 | if (Error Err = importInto(To&: ClassTemplate, From: D->getSpecializedTemplate())) |
6076 | return std::move(Err); |
6077 | |
6078 | // Import the context of this declaration. |
6079 | DeclContext *DC, *LexicalDC; |
6080 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) |
6081 | return std::move(Err); |
6082 | |
6083 | // Import template arguments. |
6084 | SmallVector<TemplateArgument, 2> TemplateArgs; |
6085 | if (Error Err = |
6086 | ImportTemplateArguments(FromArgs: D->getTemplateArgs().asArray(), ToArgs&: TemplateArgs)) |
6087 | return std::move(Err); |
6088 | // Try to find an existing specialization with these template arguments and |
6089 | // template parameter list. |
6090 | void *InsertPos = nullptr; |
6091 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; |
6092 | ClassTemplatePartialSpecializationDecl *PartialSpec = |
6093 | dyn_cast<ClassTemplatePartialSpecializationDecl>(Val: D); |
6094 | |
6095 | // Import template parameters. |
6096 | TemplateParameterList *ToTPList = nullptr; |
6097 | |
6098 | if (PartialSpec) { |
6099 | auto ToTPListOrErr = import(From: PartialSpec->getTemplateParameters()); |
6100 | if (!ToTPListOrErr) |
6101 | return ToTPListOrErr.takeError(); |
6102 | ToTPList = *ToTPListOrErr; |
6103 | PrevDecl = ClassTemplate->findPartialSpecialization(Args: TemplateArgs, |
6104 | TPL: *ToTPListOrErr, |
6105 | InsertPos); |
6106 | } else |
6107 | PrevDecl = ClassTemplate->findSpecialization(Args: TemplateArgs, InsertPos); |
6108 | |
6109 | if (PrevDecl) { |
6110 | if (IsStructuralMatch(D, PrevDecl)) { |
6111 | CXXRecordDecl *PrevDefinition = PrevDecl->getDefinition(); |
6112 | if (D->isThisDeclarationADefinition() && PrevDefinition) { |
6113 | Importer.MapImported(D, PrevDefinition); |
6114 | // Import those default field initializers which have been |
6115 | // instantiated in the "From" context, but not in the "To" context. |
6116 | for (auto *FromField : D->fields()) { |
6117 | auto ToOrErr = import(FromField); |
6118 | if (!ToOrErr) |
6119 | return ToOrErr.takeError(); |
6120 | } |
6121 | |
6122 | // Import those methods which have been instantiated in the |
6123 | // "From" context, but not in the "To" context. |
6124 | for (CXXMethodDecl *FromM : D->methods()) { |
6125 | auto ToOrErr = import(FromM); |
6126 | if (!ToOrErr) |
6127 | return ToOrErr.takeError(); |
6128 | } |
6129 | |
6130 | // TODO Import instantiated default arguments. |
6131 | // TODO Import instantiated exception specifications. |
6132 | // |
6133 | // Generally, ASTCommon.h/DeclUpdateKind enum gives a very good hint |
6134 | // what else could be fused during an AST merge. |
6135 | return PrevDefinition; |
6136 | } |
6137 | } else { // ODR violation. |
6138 | // FIXME HandleNameConflict |
6139 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
6140 | } |
6141 | } |
6142 | |
6143 | // Import the location of this declaration. |
6144 | ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc()); |
6145 | if (!BeginLocOrErr) |
6146 | return BeginLocOrErr.takeError(); |
6147 | ExpectedSLoc IdLocOrErr = import(D->getLocation()); |
6148 | if (!IdLocOrErr) |
6149 | return IdLocOrErr.takeError(); |
6150 | |
6151 | // Create the specialization. |
6152 | ClassTemplateSpecializationDecl *D2 = nullptr; |
6153 | if (PartialSpec) { |
6154 | // Import TemplateArgumentListInfo. |
6155 | TemplateArgumentListInfo ToTAInfo; |
6156 | const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten(); |
6157 | if (Error Err = ImportTemplateArgumentListInfo(Container: ASTTemplateArgs, ToTAInfo)) |
6158 | return std::move(Err); |
6159 | |
6160 | QualType CanonInjType; |
6161 | if (Error Err = importInto( |
6162 | To&: CanonInjType, From: PartialSpec->getInjectedSpecializationType())) |
6163 | return std::move(Err); |
6164 | CanonInjType = CanonInjType.getCanonicalType(); |
6165 | |
6166 | if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>( |
6167 | D2, D, Importer.getToContext(), D->getTagKind(), DC, *BeginLocOrErr, |
6168 | *IdLocOrErr, ToTPList, ClassTemplate, |
6169 | llvm::ArrayRef(TemplateArgs.data(), TemplateArgs.size()), ToTAInfo, |
6170 | CanonInjType, |
6171 | cast_or_null<ClassTemplatePartialSpecializationDecl>(Val: PrevDecl))) |
6172 | return D2; |
6173 | |
6174 | // Update InsertPos, because preceding import calls may have invalidated |
6175 | // it by adding new specializations. |
6176 | auto *PartSpec2 = cast<ClassTemplatePartialSpecializationDecl>(Val: D2); |
6177 | if (!ClassTemplate->findPartialSpecialization(Args: TemplateArgs, TPL: ToTPList, |
6178 | InsertPos)) |
6179 | // Add this partial specialization to the class template. |
6180 | ClassTemplate->AddPartialSpecialization(D: PartSpec2, InsertPos); |
6181 | if (Expected<ClassTemplatePartialSpecializationDecl *> ToInstOrErr = |
6182 | import(From: PartialSpec->getInstantiatedFromMember())) |
6183 | PartSpec2->setInstantiatedFromMember(*ToInstOrErr); |
6184 | else |
6185 | return ToInstOrErr.takeError(); |
6186 | |
6187 | updateLookupTableForTemplateParameters(Params&: *ToTPList); |
6188 | } else { // Not a partial specialization. |
6189 | if (GetImportedOrCreateDecl( |
6190 | D2, D, Importer.getToContext(), D->getTagKind(), DC, |
6191 | *BeginLocOrErr, *IdLocOrErr, ClassTemplate, TemplateArgs, |
6192 | PrevDecl)) |
6193 | return D2; |
6194 | |
6195 | // Update InsertPos, because preceding import calls may have invalidated |
6196 | // it by adding new specializations. |
6197 | if (!ClassTemplate->findSpecialization(Args: TemplateArgs, InsertPos)) |
6198 | // Add this specialization to the class template. |
6199 | ClassTemplate->AddSpecialization(D: D2, InsertPos); |
6200 | } |
6201 | |
6202 | D2->setSpecializationKind(D->getSpecializationKind()); |
6203 | |
6204 | // Set the context of this specialization/instantiation. |
6205 | D2->setLexicalDeclContext(LexicalDC); |
6206 | |
6207 | // Add to the DC only if it was an explicit specialization/instantiation. |
6208 | if (D2->isExplicitInstantiationOrSpecialization()) { |
6209 | LexicalDC->addDeclInternal(D2); |
6210 | } |
6211 | |
6212 | if (auto BraceRangeOrErr = import(D->getBraceRange())) |
6213 | D2->setBraceRange(*BraceRangeOrErr); |
6214 | else |
6215 | return BraceRangeOrErr.takeError(); |
6216 | |
6217 | // Import the qualifier, if any. |
6218 | if (auto LocOrErr = import(D->getQualifierLoc())) |
6219 | D2->setQualifierInfo(*LocOrErr); |
6220 | else |
6221 | return LocOrErr.takeError(); |
6222 | |
6223 | if (auto *TSI = D->getTypeAsWritten()) { |
6224 | if (auto TInfoOrErr = import(From: TSI)) |
6225 | D2->setTypeAsWritten(*TInfoOrErr); |
6226 | else |
6227 | return TInfoOrErr.takeError(); |
6228 | |
6229 | if (auto LocOrErr = import(From: D->getTemplateKeywordLoc())) |
6230 | D2->setTemplateKeywordLoc(*LocOrErr); |
6231 | else |
6232 | return LocOrErr.takeError(); |
6233 | |
6234 | if (auto LocOrErr = import(From: D->getExternLoc())) |
6235 | D2->setExternLoc(*LocOrErr); |
6236 | else |
6237 | return LocOrErr.takeError(); |
6238 | } |
6239 | |
6240 | if (D->getPointOfInstantiation().isValid()) { |
6241 | if (auto POIOrErr = import(From: D->getPointOfInstantiation())) |
6242 | D2->setPointOfInstantiation(*POIOrErr); |
6243 | else |
6244 | return POIOrErr.takeError(); |
6245 | } |
6246 | |
6247 | D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind()); |
6248 | |
6249 | if (auto P = D->getInstantiatedFrom()) { |
6250 | if (auto *CTD = P.dyn_cast<ClassTemplateDecl *>()) { |
6251 | if (auto CTDorErr = import(From: CTD)) |
6252 | D2->setInstantiationOf(*CTDorErr); |
6253 | } else { |
6254 | auto *CTPSD = cast<ClassTemplatePartialSpecializationDecl *>(Val&: P); |
6255 | auto CTPSDOrErr = import(From: CTPSD); |
6256 | if (!CTPSDOrErr) |
6257 | return CTPSDOrErr.takeError(); |
6258 | const TemplateArgumentList &DArgs = D->getTemplateInstantiationArgs(); |
6259 | SmallVector<TemplateArgument, 2> D2ArgsVec(DArgs.size()); |
6260 | for (unsigned I = 0; I < DArgs.size(); ++I) { |
6261 | const TemplateArgument &DArg = DArgs[I]; |
6262 | if (auto ArgOrErr = import(From: DArg)) |
6263 | D2ArgsVec[I] = *ArgOrErr; |
6264 | else |
6265 | return ArgOrErr.takeError(); |
6266 | } |
6267 | D2->setInstantiationOf( |
6268 | PartialSpec: *CTPSDOrErr, |
6269 | TemplateArgs: TemplateArgumentList::CreateCopy(Context&: Importer.getToContext(), Args: D2ArgsVec)); |
6270 | } |
6271 | } |
6272 | |
6273 | if (D->isCompleteDefinition()) |
6274 | if (Error Err = ImportDefinition(D, D2)) |
6275 | return std::move(Err); |
6276 | |
6277 | return D2; |
6278 | } |
6279 | |
6280 | ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) { |
6281 | // Import the major distinguishing characteristics of this variable template. |
6282 | DeclContext *DC, *LexicalDC; |
6283 | DeclarationName Name; |
6284 | SourceLocation Loc; |
6285 | NamedDecl *ToD; |
6286 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
6287 | return std::move(Err); |
6288 | if (ToD) |
6289 | return ToD; |
6290 | |
6291 | // We may already have a template of the same name; try to find and match it. |
6292 | assert(!DC->isFunctionOrMethod() && |
6293 | "Variable templates cannot be declared at function scope" ); |
6294 | |
6295 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
6296 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
6297 | VarTemplateDecl *FoundByLookup = nullptr; |
6298 | for (auto *FoundDecl : FoundDecls) { |
6299 | if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
6300 | continue; |
6301 | |
6302 | if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Val: FoundDecl)) { |
6303 | // Use the templated decl, some linkage flags are set only there. |
6304 | if (!hasSameVisibilityContextAndLinkage(Found: FoundTemplate->getTemplatedDecl(), |
6305 | From: D->getTemplatedDecl())) |
6306 | continue; |
6307 | if (IsStructuralMatch(D, FoundTemplate)) { |
6308 | // FIXME Check for ODR error if the two definitions have |
6309 | // different initializers? |
6310 | VarTemplateDecl *FoundDef = getTemplateDefinition(D: FoundTemplate); |
6311 | if (D->getDeclContext()->isRecord()) { |
6312 | assert(FoundTemplate->getDeclContext()->isRecord() && |
6313 | "Member variable template imported as non-member, " |
6314 | "inconsistent imported AST?" ); |
6315 | if (FoundDef) |
6316 | return Importer.MapImported(D, FoundDef); |
6317 | if (!D->isThisDeclarationADefinition()) |
6318 | return Importer.MapImported(D, FoundTemplate); |
6319 | } else { |
6320 | if (FoundDef && D->isThisDeclarationADefinition()) |
6321 | return Importer.MapImported(D, FoundDef); |
6322 | } |
6323 | FoundByLookup = FoundTemplate; |
6324 | break; |
6325 | } |
6326 | ConflictingDecls.push_back(Elt: FoundDecl); |
6327 | } |
6328 | } |
6329 | |
6330 | if (!ConflictingDecls.empty()) { |
6331 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
6332 | Name, DC, IDNS: Decl::IDNS_Ordinary, Decls: ConflictingDecls.data(), |
6333 | NumDecls: ConflictingDecls.size()); |
6334 | if (NameOrErr) |
6335 | Name = NameOrErr.get(); |
6336 | else |
6337 | return NameOrErr.takeError(); |
6338 | } |
6339 | |
6340 | VarDecl *DTemplated = D->getTemplatedDecl(); |
6341 | |
6342 | // Import the type. |
6343 | // FIXME: Value not used? |
6344 | ExpectedType TypeOrErr = import(DTemplated->getType()); |
6345 | if (!TypeOrErr) |
6346 | return TypeOrErr.takeError(); |
6347 | |
6348 | // Create the declaration that is being templated. |
6349 | VarDecl *ToTemplated; |
6350 | if (Error Err = importInto(To&: ToTemplated, From: DTemplated)) |
6351 | return std::move(Err); |
6352 | |
6353 | // Create the variable template declaration itself. |
6354 | auto TemplateParamsOrErr = import(D->getTemplateParameters()); |
6355 | if (!TemplateParamsOrErr) |
6356 | return TemplateParamsOrErr.takeError(); |
6357 | |
6358 | VarTemplateDecl *ToVarTD; |
6359 | if (GetImportedOrCreateDecl(ToVarTD, D, Importer.getToContext(), DC, Loc, |
6360 | Name, *TemplateParamsOrErr, ToTemplated)) |
6361 | return ToVarTD; |
6362 | |
6363 | ToTemplated->setDescribedVarTemplate(ToVarTD); |
6364 | |
6365 | ToVarTD->setAccess(D->getAccess()); |
6366 | ToVarTD->setLexicalDeclContext(LexicalDC); |
6367 | LexicalDC->addDeclInternal(ToVarTD); |
6368 | if (DC != Importer.getToContext().getTranslationUnitDecl()) |
6369 | updateLookupTableForTemplateParameters(**TemplateParamsOrErr); |
6370 | |
6371 | if (FoundByLookup) { |
6372 | auto *Recent = |
6373 | const_cast<VarTemplateDecl *>(FoundByLookup->getMostRecentDecl()); |
6374 | if (!ToTemplated->getPreviousDecl()) { |
6375 | auto *PrevTemplated = |
6376 | FoundByLookup->getTemplatedDecl()->getMostRecentDecl(); |
6377 | if (ToTemplated != PrevTemplated) |
6378 | ToTemplated->setPreviousDecl(PrevTemplated); |
6379 | } |
6380 | ToVarTD->setPreviousDecl(Recent); |
6381 | } |
6382 | |
6383 | return ToVarTD; |
6384 | } |
6385 | |
6386 | ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl( |
6387 | VarTemplateSpecializationDecl *D) { |
6388 | // A VarTemplateSpecializationDecl inherits from VarDecl, the import is done |
6389 | // in an analog way (but specialized for this case). |
6390 | |
6391 | SmallVector<Decl *, 2> Redecls = getCanonicalForwardRedeclChain(D); |
6392 | auto RedeclIt = Redecls.begin(); |
6393 | // Import the first part of the decl chain. I.e. import all previous |
6394 | // declarations starting from the canonical decl. |
6395 | for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) { |
6396 | ExpectedDecl RedeclOrErr = import(*RedeclIt); |
6397 | if (!RedeclOrErr) |
6398 | return RedeclOrErr.takeError(); |
6399 | } |
6400 | assert(*RedeclIt == D); |
6401 | |
6402 | VarTemplateDecl *VarTemplate = nullptr; |
6403 | if (Error Err = importInto(To&: VarTemplate, From: D->getSpecializedTemplate())) |
6404 | return std::move(Err); |
6405 | |
6406 | // Import the context of this declaration. |
6407 | DeclContext *DC, *LexicalDC; |
6408 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) |
6409 | return std::move(Err); |
6410 | |
6411 | // Import the location of this declaration. |
6412 | ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc()); |
6413 | if (!BeginLocOrErr) |
6414 | return BeginLocOrErr.takeError(); |
6415 | |
6416 | auto IdLocOrErr = import(D->getLocation()); |
6417 | if (!IdLocOrErr) |
6418 | return IdLocOrErr.takeError(); |
6419 | |
6420 | // Import template arguments. |
6421 | SmallVector<TemplateArgument, 2> TemplateArgs; |
6422 | if (Error Err = |
6423 | ImportTemplateArguments(FromArgs: D->getTemplateArgs().asArray(), ToArgs&: TemplateArgs)) |
6424 | return std::move(Err); |
6425 | |
6426 | // Try to find an existing specialization with these template arguments. |
6427 | void *InsertPos = nullptr; |
6428 | VarTemplateSpecializationDecl *FoundSpecialization = |
6429 | VarTemplate->findSpecialization(Args: TemplateArgs, InsertPos); |
6430 | if (FoundSpecialization) { |
6431 | if (IsStructuralMatch(D, FoundSpecialization)) { |
6432 | VarDecl *FoundDef = FoundSpecialization->getDefinition(); |
6433 | if (D->getDeclContext()->isRecord()) { |
6434 | // In a record, it is allowed only to have one optional declaration and |
6435 | // one definition of the (static or constexpr) variable template. |
6436 | assert( |
6437 | FoundSpecialization->getDeclContext()->isRecord() && |
6438 | "Member variable template specialization imported as non-member, " |
6439 | "inconsistent imported AST?" ); |
6440 | if (FoundDef) |
6441 | return Importer.MapImported(D, FoundDef); |
6442 | if (!D->isThisDeclarationADefinition()) |
6443 | return Importer.MapImported(D, FoundSpecialization); |
6444 | } else { |
6445 | // If definition is imported and there is already one, map to it. |
6446 | // Otherwise create a new variable and link it to the existing. |
6447 | if (FoundDef && D->isThisDeclarationADefinition()) |
6448 | return Importer.MapImported(D, FoundDef); |
6449 | } |
6450 | } else { |
6451 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
6452 | } |
6453 | } |
6454 | |
6455 | VarTemplateSpecializationDecl *D2 = nullptr; |
6456 | |
6457 | TemplateArgumentListInfo ToTAInfo; |
6458 | if (const ASTTemplateArgumentListInfo *Args = D->getTemplateArgsInfo()) { |
6459 | if (Error Err = ImportTemplateArgumentListInfo(Container: *Args, ToTAInfo)) |
6460 | return std::move(Err); |
6461 | } |
6462 | |
6463 | using PartVarSpecDecl = VarTemplatePartialSpecializationDecl; |
6464 | // Create a new specialization. |
6465 | if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(Val: D)) { |
6466 | // Import TemplateArgumentListInfo |
6467 | TemplateArgumentListInfo ArgInfos; |
6468 | const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten(); |
6469 | // NOTE: FromTAArgsAsWritten and template parameter list are non-null. |
6470 | if (Error Err = |
6471 | ImportTemplateArgumentListInfo(Container: *FromTAArgsAsWritten, ToTAInfo&: ArgInfos)) |
6472 | return std::move(Err); |
6473 | |
6474 | auto ToTPListOrErr = import(From: FromPartial->getTemplateParameters()); |
6475 | if (!ToTPListOrErr) |
6476 | return ToTPListOrErr.takeError(); |
6477 | |
6478 | PartVarSpecDecl *ToPartial; |
6479 | if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC, |
6480 | *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr, |
6481 | VarTemplate, QualType(), nullptr, |
6482 | D->getStorageClass(), TemplateArgs, ArgInfos)) |
6483 | return ToPartial; |
6484 | |
6485 | if (Expected<PartVarSpecDecl *> ToInstOrErr = |
6486 | import(From: FromPartial->getInstantiatedFromMember())) |
6487 | ToPartial->setInstantiatedFromMember(*ToInstOrErr); |
6488 | else |
6489 | return ToInstOrErr.takeError(); |
6490 | |
6491 | if (FromPartial->isMemberSpecialization()) |
6492 | ToPartial->setMemberSpecialization(); |
6493 | |
6494 | D2 = ToPartial; |
6495 | |
6496 | // FIXME: Use this update if VarTemplatePartialSpecializationDecl is fixed |
6497 | // to adopt template parameters. |
6498 | // updateLookupTableForTemplateParameters(**ToTPListOrErr); |
6499 | } else { // Full specialization |
6500 | if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, |
6501 | *BeginLocOrErr, *IdLocOrErr, VarTemplate, |
6502 | QualType(), nullptr, D->getStorageClass(), |
6503 | TemplateArgs)) |
6504 | return D2; |
6505 | } |
6506 | |
6507 | QualType T; |
6508 | if (Error Err = importInto(T, D->getType())) |
6509 | return std::move(Err); |
6510 | D2->setType(T); |
6511 | |
6512 | auto TInfoOrErr = import(D->getTypeSourceInfo()); |
6513 | if (!TInfoOrErr) |
6514 | return TInfoOrErr.takeError(); |
6515 | D2->setTypeSourceInfo(*TInfoOrErr); |
6516 | |
6517 | if (D->getPointOfInstantiation().isValid()) { |
6518 | if (ExpectedSLoc POIOrErr = import(From: D->getPointOfInstantiation())) |
6519 | D2->setPointOfInstantiation(*POIOrErr); |
6520 | else |
6521 | return POIOrErr.takeError(); |
6522 | } |
6523 | |
6524 | D2->setSpecializationKind(D->getSpecializationKind()); |
6525 | D2->setTemplateArgsInfo(ToTAInfo); |
6526 | |
6527 | if (auto LocOrErr = import(D->getQualifierLoc())) |
6528 | D2->setQualifierInfo(*LocOrErr); |
6529 | else |
6530 | return LocOrErr.takeError(); |
6531 | |
6532 | if (D->isConstexpr()) |
6533 | D2->setConstexpr(true); |
6534 | |
6535 | D2->setAccess(D->getAccess()); |
6536 | |
6537 | if (Error Err = ImportInitializer(D, D2)) |
6538 | return std::move(Err); |
6539 | |
6540 | if (FoundSpecialization) |
6541 | D2->setPreviousDecl(FoundSpecialization->getMostRecentDecl()); |
6542 | |
6543 | VarTemplate->AddSpecialization(D: D2, InsertPos); |
6544 | |
6545 | addDeclToContexts(D, D2); |
6546 | |
6547 | // Import the rest of the chain. I.e. import all subsequent declarations. |
6548 | for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) { |
6549 | ExpectedDecl RedeclOrErr = import(*RedeclIt); |
6550 | if (!RedeclOrErr) |
6551 | return RedeclOrErr.takeError(); |
6552 | } |
6553 | |
6554 | return D2; |
6555 | } |
6556 | |
6557 | ExpectedDecl |
6558 | ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
6559 | DeclContext *DC, *LexicalDC; |
6560 | DeclarationName Name; |
6561 | SourceLocation Loc; |
6562 | NamedDecl *ToD; |
6563 | |
6564 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
6565 | return std::move(Err); |
6566 | |
6567 | if (ToD) |
6568 | return ToD; |
6569 | |
6570 | const FunctionTemplateDecl *FoundByLookup = nullptr; |
6571 | |
6572 | // Try to find a function in our own ("to") context with the same name, same |
6573 | // type, and in the same context as the function we're importing. |
6574 | // FIXME Split this into a separate function. |
6575 | if (!LexicalDC->isFunctionOrMethod()) { |
6576 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend; |
6577 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
6578 | for (auto *FoundDecl : FoundDecls) { |
6579 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) |
6580 | continue; |
6581 | |
6582 | if (auto *FoundTemplate = dyn_cast<FunctionTemplateDecl>(Val: FoundDecl)) { |
6583 | if (!hasSameVisibilityContextAndLinkage(Found: FoundTemplate, From: D)) |
6584 | continue; |
6585 | if (IsStructuralMatch(D, FoundTemplate)) { |
6586 | FunctionTemplateDecl *TemplateWithDef = |
6587 | getTemplateDefinition(D: FoundTemplate); |
6588 | if (D->isThisDeclarationADefinition() && TemplateWithDef) |
6589 | return Importer.MapImported(D, TemplateWithDef); |
6590 | |
6591 | FoundByLookup = FoundTemplate; |
6592 | break; |
6593 | // TODO: handle conflicting names |
6594 | } |
6595 | } |
6596 | } |
6597 | } |
6598 | |
6599 | auto ParamsOrErr = import(D->getTemplateParameters()); |
6600 | if (!ParamsOrErr) |
6601 | return ParamsOrErr.takeError(); |
6602 | TemplateParameterList *Params = *ParamsOrErr; |
6603 | |
6604 | FunctionDecl *TemplatedFD; |
6605 | if (Error Err = importInto(To&: TemplatedFD, From: D->getTemplatedDecl())) |
6606 | return std::move(Err); |
6607 | |
6608 | // At creation of the template the template parameters are "adopted" |
6609 | // (DeclContext is changed). After this possible change the lookup table |
6610 | // must be updated. |
6611 | // At deduction guides the DeclContext of the template parameters may be |
6612 | // different from what we would expect, it may be the class template, or a |
6613 | // probably different CXXDeductionGuideDecl. This may come from the fact that |
6614 | // the template parameter objects may be shared between deduction guides or |
6615 | // the class template, and at creation of multiple FunctionTemplateDecl |
6616 | // objects (for deduction guides) the same parameters are re-used. The |
6617 | // "adoption" happens multiple times with different parent, even recursively |
6618 | // for TemplateTemplateParmDecl. The same happens at import when the |
6619 | // FunctionTemplateDecl objects are created, but in different order. |
6620 | // In this way the DeclContext of these template parameters is not necessarily |
6621 | // the same as in the "from" context. |
6622 | SmallVector<DeclContext *, 2> OldParamDC; |
6623 | OldParamDC.reserve(N: Params->size()); |
6624 | llvm::transform(Range&: *Params, d_first: std::back_inserter(x&: OldParamDC), |
6625 | F: [](NamedDecl *ND) { return ND->getDeclContext(); }); |
6626 | |
6627 | FunctionTemplateDecl *ToFunc; |
6628 | if (GetImportedOrCreateDecl(ToD&: ToFunc, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, args&: Name, |
6629 | args&: Params, args&: TemplatedFD)) |
6630 | return ToFunc; |
6631 | |
6632 | TemplatedFD->setDescribedFunctionTemplate(ToFunc); |
6633 | |
6634 | ToFunc->setAccess(D->getAccess()); |
6635 | ToFunc->setLexicalDeclContext(LexicalDC); |
6636 | addDeclToContexts(D, ToFunc); |
6637 | |
6638 | ASTImporterLookupTable *LT = Importer.SharedState->getLookupTable(); |
6639 | if (LT && !OldParamDC.empty()) { |
6640 | for (unsigned int I = 0; I < OldParamDC.size(); ++I) |
6641 | LT->updateForced(ND: Params->getParam(Idx: I), OldDC: OldParamDC[I]); |
6642 | } |
6643 | |
6644 | if (FoundByLookup) { |
6645 | auto *Recent = |
6646 | const_cast<FunctionTemplateDecl *>(FoundByLookup->getMostRecentDecl()); |
6647 | if (!TemplatedFD->getPreviousDecl()) { |
6648 | assert(FoundByLookup->getTemplatedDecl() && |
6649 | "Found decl must have its templated decl set" ); |
6650 | auto *PrevTemplated = |
6651 | FoundByLookup->getTemplatedDecl()->getMostRecentDecl(); |
6652 | if (TemplatedFD != PrevTemplated) |
6653 | TemplatedFD->setPreviousDecl(PrevTemplated); |
6654 | } |
6655 | ToFunc->setPreviousDecl(Recent); |
6656 | } |
6657 | |
6658 | return ToFunc; |
6659 | } |
6660 | |
6661 | //---------------------------------------------------------------------------- |
6662 | // Import Statements |
6663 | //---------------------------------------------------------------------------- |
6664 | |
6665 | ExpectedStmt ASTNodeImporter::VisitStmt(Stmt *S) { |
6666 | Importer.FromDiag(S->getBeginLoc(), diag::err_unsupported_ast_node) |
6667 | << S->getStmtClassName(); |
6668 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
6669 | } |
6670 | |
6671 | |
6672 | ExpectedStmt ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) { |
6673 | if (Importer.returnWithErrorInTest()) |
6674 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
6675 | SmallVector<IdentifierInfo *, 4> Names; |
6676 | for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) { |
6677 | IdentifierInfo *ToII = Importer.Import(FromId: S->getOutputIdentifier(i: I)); |
6678 | // ToII is nullptr when no symbolic name is given for output operand |
6679 | // see ParseStmtAsm::ParseAsmOperandsOpt |
6680 | Names.push_back(Elt: ToII); |
6681 | } |
6682 | |
6683 | for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) { |
6684 | IdentifierInfo *ToII = Importer.Import(FromId: S->getInputIdentifier(i: I)); |
6685 | // ToII is nullptr when no symbolic name is given for input operand |
6686 | // see ParseStmtAsm::ParseAsmOperandsOpt |
6687 | Names.push_back(Elt: ToII); |
6688 | } |
6689 | |
6690 | SmallVector<StringLiteral *, 4> Clobbers; |
6691 | for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) { |
6692 | if (auto ClobberOrErr = import(From: S->getClobberStringLiteral(i: I))) |
6693 | Clobbers.push_back(Elt: *ClobberOrErr); |
6694 | else |
6695 | return ClobberOrErr.takeError(); |
6696 | |
6697 | } |
6698 | |
6699 | SmallVector<StringLiteral *, 4> Constraints; |
6700 | for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) { |
6701 | if (auto OutputOrErr = import(From: S->getOutputConstraintLiteral(i: I))) |
6702 | Constraints.push_back(Elt: *OutputOrErr); |
6703 | else |
6704 | return OutputOrErr.takeError(); |
6705 | } |
6706 | |
6707 | for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) { |
6708 | if (auto InputOrErr = import(From: S->getInputConstraintLiteral(i: I))) |
6709 | Constraints.push_back(Elt: *InputOrErr); |
6710 | else |
6711 | return InputOrErr.takeError(); |
6712 | } |
6713 | |
6714 | SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs() + |
6715 | S->getNumLabels()); |
6716 | if (Error Err = ImportContainerChecked(S->outputs(), Exprs)) |
6717 | return std::move(Err); |
6718 | |
6719 | if (Error Err = |
6720 | ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs())) |
6721 | return std::move(Err); |
6722 | |
6723 | if (Error Err = ImportArrayChecked( |
6724 | S->labels(), Exprs.begin() + S->getNumOutputs() + S->getNumInputs())) |
6725 | return std::move(Err); |
6726 | |
6727 | ExpectedSLoc AsmLocOrErr = import(From: S->getAsmLoc()); |
6728 | if (!AsmLocOrErr) |
6729 | return AsmLocOrErr.takeError(); |
6730 | auto AsmStrOrErr = import(From: S->getAsmString()); |
6731 | if (!AsmStrOrErr) |
6732 | return AsmStrOrErr.takeError(); |
6733 | ExpectedSLoc RParenLocOrErr = import(From: S->getRParenLoc()); |
6734 | if (!RParenLocOrErr) |
6735 | return RParenLocOrErr.takeError(); |
6736 | |
6737 | return new (Importer.getToContext()) GCCAsmStmt( |
6738 | Importer.getToContext(), |
6739 | *AsmLocOrErr, |
6740 | S->isSimple(), |
6741 | S->isVolatile(), |
6742 | S->getNumOutputs(), |
6743 | S->getNumInputs(), |
6744 | Names.data(), |
6745 | Constraints.data(), |
6746 | Exprs.data(), |
6747 | *AsmStrOrErr, |
6748 | S->getNumClobbers(), |
6749 | Clobbers.data(), |
6750 | S->getNumLabels(), |
6751 | *RParenLocOrErr); |
6752 | } |
6753 | |
6754 | ExpectedStmt ASTNodeImporter::VisitDeclStmt(DeclStmt *S) { |
6755 | |
6756 | Error Err = Error::success(); |
6757 | auto ToDG = importChecked(Err, From: S->getDeclGroup()); |
6758 | auto ToBeginLoc = importChecked(Err, From: S->getBeginLoc()); |
6759 | auto ToEndLoc = importChecked(Err, From: S->getEndLoc()); |
6760 | if (Err) |
6761 | return std::move(Err); |
6762 | return new (Importer.getToContext()) DeclStmt(ToDG, ToBeginLoc, ToEndLoc); |
6763 | } |
6764 | |
6765 | ExpectedStmt ASTNodeImporter::VisitNullStmt(NullStmt *S) { |
6766 | ExpectedSLoc ToSemiLocOrErr = import(From: S->getSemiLoc()); |
6767 | if (!ToSemiLocOrErr) |
6768 | return ToSemiLocOrErr.takeError(); |
6769 | return new (Importer.getToContext()) NullStmt( |
6770 | *ToSemiLocOrErr, S->hasLeadingEmptyMacro()); |
6771 | } |
6772 | |
6773 | ExpectedStmt ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) { |
6774 | SmallVector<Stmt *, 8> ToStmts(S->size()); |
6775 | |
6776 | if (Error Err = ImportContainerChecked(InContainer: S->body(), OutContainer&: ToStmts)) |
6777 | return std::move(Err); |
6778 | |
6779 | ExpectedSLoc ToLBracLocOrErr = import(From: S->getLBracLoc()); |
6780 | if (!ToLBracLocOrErr) |
6781 | return ToLBracLocOrErr.takeError(); |
6782 | |
6783 | ExpectedSLoc ToRBracLocOrErr = import(From: S->getRBracLoc()); |
6784 | if (!ToRBracLocOrErr) |
6785 | return ToRBracLocOrErr.takeError(); |
6786 | |
6787 | FPOptionsOverride FPO = |
6788 | S->hasStoredFPFeatures() ? S->getStoredFPFeatures() : FPOptionsOverride(); |
6789 | return CompoundStmt::Create(C: Importer.getToContext(), Stmts: ToStmts, FPFeatures: FPO, |
6790 | LB: *ToLBracLocOrErr, RB: *ToRBracLocOrErr); |
6791 | } |
6792 | |
6793 | ExpectedStmt ASTNodeImporter::VisitCaseStmt(CaseStmt *S) { |
6794 | |
6795 | Error Err = Error::success(); |
6796 | auto ToLHS = importChecked(Err, From: S->getLHS()); |
6797 | auto ToRHS = importChecked(Err, From: S->getRHS()); |
6798 | auto ToSubStmt = importChecked(Err, From: S->getSubStmt()); |
6799 | auto ToCaseLoc = importChecked(Err, From: S->getCaseLoc()); |
6800 | auto ToEllipsisLoc = importChecked(Err, From: S->getEllipsisLoc()); |
6801 | auto ToColonLoc = importChecked(Err, S->getColonLoc()); |
6802 | if (Err) |
6803 | return std::move(Err); |
6804 | |
6805 | auto *ToStmt = CaseStmt::Create(Ctx: Importer.getToContext(), lhs: ToLHS, rhs: ToRHS, |
6806 | caseLoc: ToCaseLoc, ellipsisLoc: ToEllipsisLoc, colonLoc: ToColonLoc); |
6807 | ToStmt->setSubStmt(ToSubStmt); |
6808 | |
6809 | return ToStmt; |
6810 | } |
6811 | |
6812 | ExpectedStmt ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) { |
6813 | |
6814 | Error Err = Error::success(); |
6815 | auto ToDefaultLoc = importChecked(Err, From: S->getDefaultLoc()); |
6816 | auto ToColonLoc = importChecked(Err, From: S->getColonLoc()); |
6817 | auto ToSubStmt = importChecked(Err, From: S->getSubStmt()); |
6818 | if (Err) |
6819 | return std::move(Err); |
6820 | |
6821 | return new (Importer.getToContext()) DefaultStmt( |
6822 | ToDefaultLoc, ToColonLoc, ToSubStmt); |
6823 | } |
6824 | |
6825 | ExpectedStmt ASTNodeImporter::VisitLabelStmt(LabelStmt *S) { |
6826 | |
6827 | Error Err = Error::success(); |
6828 | auto ToIdentLoc = importChecked(Err, From: S->getIdentLoc()); |
6829 | auto ToLabelDecl = importChecked(Err, From: S->getDecl()); |
6830 | auto ToSubStmt = importChecked(Err, From: S->getSubStmt()); |
6831 | if (Err) |
6832 | return std::move(Err); |
6833 | |
6834 | return new (Importer.getToContext()) LabelStmt( |
6835 | ToIdentLoc, ToLabelDecl, ToSubStmt); |
6836 | } |
6837 | |
6838 | ExpectedStmt ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) { |
6839 | ExpectedSLoc ToAttrLocOrErr = import(From: S->getAttrLoc()); |
6840 | if (!ToAttrLocOrErr) |
6841 | return ToAttrLocOrErr.takeError(); |
6842 | ArrayRef<const Attr*> FromAttrs(S->getAttrs()); |
6843 | SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size()); |
6844 | if (Error Err = ImportContainerChecked(InContainer: FromAttrs, OutContainer&: ToAttrs)) |
6845 | return std::move(Err); |
6846 | ExpectedStmt ToSubStmtOrErr = import(From: S->getSubStmt()); |
6847 | if (!ToSubStmtOrErr) |
6848 | return ToSubStmtOrErr.takeError(); |
6849 | |
6850 | return AttributedStmt::Create( |
6851 | C: Importer.getToContext(), Loc: *ToAttrLocOrErr, Attrs: ToAttrs, SubStmt: *ToSubStmtOrErr); |
6852 | } |
6853 | |
6854 | ExpectedStmt ASTNodeImporter::VisitIfStmt(IfStmt *S) { |
6855 | |
6856 | Error Err = Error::success(); |
6857 | auto ToIfLoc = importChecked(Err, From: S->getIfLoc()); |
6858 | auto ToInit = importChecked(Err, From: S->getInit()); |
6859 | auto ToConditionVariable = importChecked(Err, From: S->getConditionVariable()); |
6860 | auto ToCond = importChecked(Err, From: S->getCond()); |
6861 | auto ToLParenLoc = importChecked(Err, From: S->getLParenLoc()); |
6862 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
6863 | auto ToThen = importChecked(Err, From: S->getThen()); |
6864 | auto ToElseLoc = importChecked(Err, From: S->getElseLoc()); |
6865 | auto ToElse = importChecked(Err, From: S->getElse()); |
6866 | if (Err) |
6867 | return std::move(Err); |
6868 | |
6869 | return IfStmt::Create(Ctx: Importer.getToContext(), IL: ToIfLoc, Kind: S->getStatementKind(), |
6870 | Init: ToInit, Var: ToConditionVariable, Cond: ToCond, LPL: ToLParenLoc, |
6871 | RPL: ToRParenLoc, Then: ToThen, EL: ToElseLoc, Else: ToElse); |
6872 | } |
6873 | |
6874 | ExpectedStmt ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) { |
6875 | |
6876 | Error Err = Error::success(); |
6877 | auto ToInit = importChecked(Err, From: S->getInit()); |
6878 | auto ToConditionVariable = importChecked(Err, From: S->getConditionVariable()); |
6879 | auto ToCond = importChecked(Err, From: S->getCond()); |
6880 | auto ToLParenLoc = importChecked(Err, From: S->getLParenLoc()); |
6881 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
6882 | auto ToBody = importChecked(Err, From: S->getBody()); |
6883 | auto ToSwitchLoc = importChecked(Err, From: S->getSwitchLoc()); |
6884 | if (Err) |
6885 | return std::move(Err); |
6886 | |
6887 | auto *ToStmt = |
6888 | SwitchStmt::Create(Ctx: Importer.getToContext(), Init: ToInit, Var: ToConditionVariable, |
6889 | Cond: ToCond, LParenLoc: ToLParenLoc, RParenLoc: ToRParenLoc); |
6890 | ToStmt->setBody(ToBody); |
6891 | ToStmt->setSwitchLoc(ToSwitchLoc); |
6892 | |
6893 | // Now we have to re-chain the cases. |
6894 | SwitchCase *LastChainedSwitchCase = nullptr; |
6895 | for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr; |
6896 | SC = SC->getNextSwitchCase()) { |
6897 | Expected<SwitchCase *> ToSCOrErr = import(From: SC); |
6898 | if (!ToSCOrErr) |
6899 | return ToSCOrErr.takeError(); |
6900 | if (LastChainedSwitchCase) |
6901 | LastChainedSwitchCase->setNextSwitchCase(*ToSCOrErr); |
6902 | else |
6903 | ToStmt->setSwitchCaseList(*ToSCOrErr); |
6904 | LastChainedSwitchCase = *ToSCOrErr; |
6905 | } |
6906 | |
6907 | return ToStmt; |
6908 | } |
6909 | |
6910 | ExpectedStmt ASTNodeImporter::VisitWhileStmt(WhileStmt *S) { |
6911 | |
6912 | Error Err = Error::success(); |
6913 | auto ToConditionVariable = importChecked(Err, From: S->getConditionVariable()); |
6914 | auto ToCond = importChecked(Err, From: S->getCond()); |
6915 | auto ToBody = importChecked(Err, From: S->getBody()); |
6916 | auto ToWhileLoc = importChecked(Err, From: S->getWhileLoc()); |
6917 | auto ToLParenLoc = importChecked(Err, From: S->getLParenLoc()); |
6918 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
6919 | if (Err) |
6920 | return std::move(Err); |
6921 | |
6922 | return WhileStmt::Create(Ctx: Importer.getToContext(), Var: ToConditionVariable, Cond: ToCond, |
6923 | Body: ToBody, WL: ToWhileLoc, LParenLoc: ToLParenLoc, RParenLoc: ToRParenLoc); |
6924 | } |
6925 | |
6926 | ExpectedStmt ASTNodeImporter::VisitDoStmt(DoStmt *S) { |
6927 | |
6928 | Error Err = Error::success(); |
6929 | auto ToBody = importChecked(Err, From: S->getBody()); |
6930 | auto ToCond = importChecked(Err, From: S->getCond()); |
6931 | auto ToDoLoc = importChecked(Err, From: S->getDoLoc()); |
6932 | auto ToWhileLoc = importChecked(Err, From: S->getWhileLoc()); |
6933 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
6934 | if (Err) |
6935 | return std::move(Err); |
6936 | |
6937 | return new (Importer.getToContext()) DoStmt( |
6938 | ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc); |
6939 | } |
6940 | |
6941 | ExpectedStmt ASTNodeImporter::VisitForStmt(ForStmt *S) { |
6942 | |
6943 | Error Err = Error::success(); |
6944 | auto ToInit = importChecked(Err, From: S->getInit()); |
6945 | auto ToCond = importChecked(Err, From: S->getCond()); |
6946 | auto ToConditionVariable = importChecked(Err, From: S->getConditionVariable()); |
6947 | auto ToInc = importChecked(Err, From: S->getInc()); |
6948 | auto ToBody = importChecked(Err, From: S->getBody()); |
6949 | auto ToForLoc = importChecked(Err, From: S->getForLoc()); |
6950 | auto ToLParenLoc = importChecked(Err, From: S->getLParenLoc()); |
6951 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
6952 | if (Err) |
6953 | return std::move(Err); |
6954 | |
6955 | return new (Importer.getToContext()) ForStmt( |
6956 | Importer.getToContext(), |
6957 | ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc, ToLParenLoc, |
6958 | ToRParenLoc); |
6959 | } |
6960 | |
6961 | ExpectedStmt ASTNodeImporter::VisitGotoStmt(GotoStmt *S) { |
6962 | |
6963 | Error Err = Error::success(); |
6964 | auto ToLabel = importChecked(Err, From: S->getLabel()); |
6965 | auto ToGotoLoc = importChecked(Err, From: S->getGotoLoc()); |
6966 | auto ToLabelLoc = importChecked(Err, From: S->getLabelLoc()); |
6967 | if (Err) |
6968 | return std::move(Err); |
6969 | |
6970 | return new (Importer.getToContext()) GotoStmt( |
6971 | ToLabel, ToGotoLoc, ToLabelLoc); |
6972 | } |
6973 | |
6974 | ExpectedStmt ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) { |
6975 | |
6976 | Error Err = Error::success(); |
6977 | auto ToGotoLoc = importChecked(Err, From: S->getGotoLoc()); |
6978 | auto ToStarLoc = importChecked(Err, From: S->getStarLoc()); |
6979 | auto ToTarget = importChecked(Err, From: S->getTarget()); |
6980 | if (Err) |
6981 | return std::move(Err); |
6982 | |
6983 | return new (Importer.getToContext()) IndirectGotoStmt( |
6984 | ToGotoLoc, ToStarLoc, ToTarget); |
6985 | } |
6986 | |
6987 | ExpectedStmt ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) { |
6988 | ExpectedSLoc ToContinueLocOrErr = import(From: S->getContinueLoc()); |
6989 | if (!ToContinueLocOrErr) |
6990 | return ToContinueLocOrErr.takeError(); |
6991 | return new (Importer.getToContext()) ContinueStmt(*ToContinueLocOrErr); |
6992 | } |
6993 | |
6994 | ExpectedStmt ASTNodeImporter::VisitBreakStmt(BreakStmt *S) { |
6995 | auto ToBreakLocOrErr = import(From: S->getBreakLoc()); |
6996 | if (!ToBreakLocOrErr) |
6997 | return ToBreakLocOrErr.takeError(); |
6998 | return new (Importer.getToContext()) BreakStmt(*ToBreakLocOrErr); |
6999 | } |
7000 | |
7001 | ExpectedStmt ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) { |
7002 | |
7003 | Error Err = Error::success(); |
7004 | auto ToReturnLoc = importChecked(Err, From: S->getReturnLoc()); |
7005 | auto ToRetValue = importChecked(Err, From: S->getRetValue()); |
7006 | auto ToNRVOCandidate = importChecked(Err, From: S->getNRVOCandidate()); |
7007 | if (Err) |
7008 | return std::move(Err); |
7009 | |
7010 | return ReturnStmt::Create(Ctx: Importer.getToContext(), RL: ToReturnLoc, E: ToRetValue, |
7011 | NRVOCandidate: ToNRVOCandidate); |
7012 | } |
7013 | |
7014 | ExpectedStmt ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) { |
7015 | |
7016 | Error Err = Error::success(); |
7017 | auto ToCatchLoc = importChecked(Err, From: S->getCatchLoc()); |
7018 | auto ToExceptionDecl = importChecked(Err, From: S->getExceptionDecl()); |
7019 | auto ToHandlerBlock = importChecked(Err, From: S->getHandlerBlock()); |
7020 | if (Err) |
7021 | return std::move(Err); |
7022 | |
7023 | return new (Importer.getToContext()) CXXCatchStmt ( |
7024 | ToCatchLoc, ToExceptionDecl, ToHandlerBlock); |
7025 | } |
7026 | |
7027 | ExpectedStmt ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) { |
7028 | ExpectedSLoc ToTryLocOrErr = import(From: S->getTryLoc()); |
7029 | if (!ToTryLocOrErr) |
7030 | return ToTryLocOrErr.takeError(); |
7031 | |
7032 | ExpectedStmt ToTryBlockOrErr = import(From: S->getTryBlock()); |
7033 | if (!ToTryBlockOrErr) |
7034 | return ToTryBlockOrErr.takeError(); |
7035 | |
7036 | SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers()); |
7037 | for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) { |
7038 | CXXCatchStmt *FromHandler = S->getHandler(i: HI); |
7039 | if (auto ToHandlerOrErr = import(From: FromHandler)) |
7040 | ToHandlers[HI] = *ToHandlerOrErr; |
7041 | else |
7042 | return ToHandlerOrErr.takeError(); |
7043 | } |
7044 | |
7045 | return CXXTryStmt::Create(C: Importer.getToContext(), tryLoc: *ToTryLocOrErr, |
7046 | tryBlock: cast<CompoundStmt>(Val: *ToTryBlockOrErr), handlers: ToHandlers); |
7047 | } |
7048 | |
7049 | ExpectedStmt ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) { |
7050 | |
7051 | Error Err = Error::success(); |
7052 | auto ToInit = importChecked(Err, From: S->getInit()); |
7053 | auto ToRangeStmt = importChecked(Err, From: S->getRangeStmt()); |
7054 | auto ToBeginStmt = importChecked(Err, From: S->getBeginStmt()); |
7055 | auto ToEndStmt = importChecked(Err, From: S->getEndStmt()); |
7056 | auto ToCond = importChecked(Err, From: S->getCond()); |
7057 | auto ToInc = importChecked(Err, From: S->getInc()); |
7058 | auto ToLoopVarStmt = importChecked(Err, From: S->getLoopVarStmt()); |
7059 | auto ToBody = importChecked(Err, From: S->getBody()); |
7060 | auto ToForLoc = importChecked(Err, From: S->getForLoc()); |
7061 | auto ToCoawaitLoc = importChecked(Err, From: S->getCoawaitLoc()); |
7062 | auto ToColonLoc = importChecked(Err, From: S->getColonLoc()); |
7063 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
7064 | if (Err) |
7065 | return std::move(Err); |
7066 | |
7067 | return new (Importer.getToContext()) CXXForRangeStmt( |
7068 | ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt, |
7069 | ToBody, ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc); |
7070 | } |
7071 | |
7072 | ExpectedStmt |
7073 | ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) { |
7074 | Error Err = Error::success(); |
7075 | auto ToElement = importChecked(Err, From: S->getElement()); |
7076 | auto ToCollection = importChecked(Err, From: S->getCollection()); |
7077 | auto ToBody = importChecked(Err, From: S->getBody()); |
7078 | auto ToForLoc = importChecked(Err, From: S->getForLoc()); |
7079 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
7080 | if (Err) |
7081 | return std::move(Err); |
7082 | |
7083 | return new (Importer.getToContext()) ObjCForCollectionStmt(ToElement, |
7084 | ToCollection, |
7085 | ToBody, |
7086 | ToForLoc, |
7087 | ToRParenLoc); |
7088 | } |
7089 | |
7090 | ExpectedStmt ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) { |
7091 | |
7092 | Error Err = Error::success(); |
7093 | auto ToAtCatchLoc = importChecked(Err, From: S->getAtCatchLoc()); |
7094 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
7095 | auto ToCatchParamDecl = importChecked(Err, From: S->getCatchParamDecl()); |
7096 | auto ToCatchBody = importChecked(Err, From: S->getCatchBody()); |
7097 | if (Err) |
7098 | return std::move(Err); |
7099 | |
7100 | return new (Importer.getToContext()) ObjCAtCatchStmt ( |
7101 | ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody); |
7102 | } |
7103 | |
7104 | ExpectedStmt ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) { |
7105 | ExpectedSLoc ToAtFinallyLocOrErr = import(From: S->getAtFinallyLoc()); |
7106 | if (!ToAtFinallyLocOrErr) |
7107 | return ToAtFinallyLocOrErr.takeError(); |
7108 | ExpectedStmt ToAtFinallyStmtOrErr = import(From: S->getFinallyBody()); |
7109 | if (!ToAtFinallyStmtOrErr) |
7110 | return ToAtFinallyStmtOrErr.takeError(); |
7111 | return new (Importer.getToContext()) ObjCAtFinallyStmt(*ToAtFinallyLocOrErr, |
7112 | *ToAtFinallyStmtOrErr); |
7113 | } |
7114 | |
7115 | ExpectedStmt ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) { |
7116 | |
7117 | Error Err = Error::success(); |
7118 | auto ToAtTryLoc = importChecked(Err, From: S->getAtTryLoc()); |
7119 | auto ToTryBody = importChecked(Err, From: S->getTryBody()); |
7120 | auto ToFinallyStmt = importChecked(Err, From: S->getFinallyStmt()); |
7121 | if (Err) |
7122 | return std::move(Err); |
7123 | |
7124 | SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts()); |
7125 | for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) { |
7126 | ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(I: CI); |
7127 | if (ExpectedStmt ToCatchStmtOrErr = import(From: FromCatchStmt)) |
7128 | ToCatchStmts[CI] = *ToCatchStmtOrErr; |
7129 | else |
7130 | return ToCatchStmtOrErr.takeError(); |
7131 | } |
7132 | |
7133 | return ObjCAtTryStmt::Create(Context: Importer.getToContext(), |
7134 | atTryLoc: ToAtTryLoc, atTryStmt: ToTryBody, |
7135 | CatchStmts: ToCatchStmts.begin(), NumCatchStmts: ToCatchStmts.size(), |
7136 | atFinallyStmt: ToFinallyStmt); |
7137 | } |
7138 | |
7139 | ExpectedStmt |
7140 | ASTNodeImporter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
7141 | |
7142 | Error Err = Error::success(); |
7143 | auto ToAtSynchronizedLoc = importChecked(Err, From: S->getAtSynchronizedLoc()); |
7144 | auto ToSynchExpr = importChecked(Err, From: S->getSynchExpr()); |
7145 | auto ToSynchBody = importChecked(Err, From: S->getSynchBody()); |
7146 | if (Err) |
7147 | return std::move(Err); |
7148 | |
7149 | return new (Importer.getToContext()) ObjCAtSynchronizedStmt( |
7150 | ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody); |
7151 | } |
7152 | |
7153 | ExpectedStmt ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) { |
7154 | ExpectedSLoc ToThrowLocOrErr = import(From: S->getThrowLoc()); |
7155 | if (!ToThrowLocOrErr) |
7156 | return ToThrowLocOrErr.takeError(); |
7157 | ExpectedExpr ToThrowExprOrErr = import(From: S->getThrowExpr()); |
7158 | if (!ToThrowExprOrErr) |
7159 | return ToThrowExprOrErr.takeError(); |
7160 | return new (Importer.getToContext()) ObjCAtThrowStmt( |
7161 | *ToThrowLocOrErr, *ToThrowExprOrErr); |
7162 | } |
7163 | |
7164 | ExpectedStmt ASTNodeImporter::VisitObjCAutoreleasePoolStmt( |
7165 | ObjCAutoreleasePoolStmt *S) { |
7166 | ExpectedSLoc ToAtLocOrErr = import(From: S->getAtLoc()); |
7167 | if (!ToAtLocOrErr) |
7168 | return ToAtLocOrErr.takeError(); |
7169 | ExpectedStmt ToSubStmtOrErr = import(From: S->getSubStmt()); |
7170 | if (!ToSubStmtOrErr) |
7171 | return ToSubStmtOrErr.takeError(); |
7172 | return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(*ToAtLocOrErr, |
7173 | *ToSubStmtOrErr); |
7174 | } |
7175 | |
7176 | //---------------------------------------------------------------------------- |
7177 | // Import Expressions |
7178 | //---------------------------------------------------------------------------- |
7179 | ExpectedStmt ASTNodeImporter::VisitExpr(Expr *E) { |
7180 | Importer.FromDiag(E->getBeginLoc(), diag::err_unsupported_ast_node) |
7181 | << E->getStmtClassName(); |
7182 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
7183 | } |
7184 | |
7185 | ExpectedStmt ASTNodeImporter::VisitSourceLocExpr(SourceLocExpr *E) { |
7186 | Error Err = Error::success(); |
7187 | auto ToType = importChecked(Err, E->getType()); |
7188 | auto BLoc = importChecked(Err, From: E->getBeginLoc()); |
7189 | auto RParenLoc = importChecked(Err, From: E->getEndLoc()); |
7190 | if (Err) |
7191 | return std::move(Err); |
7192 | auto ParentContextOrErr = Importer.ImportContext(FromDC: E->getParentContext()); |
7193 | if (!ParentContextOrErr) |
7194 | return ParentContextOrErr.takeError(); |
7195 | |
7196 | return new (Importer.getToContext()) |
7197 | SourceLocExpr(Importer.getToContext(), E->getIdentKind(), ToType, BLoc, |
7198 | RParenLoc, *ParentContextOrErr); |
7199 | } |
7200 | |
7201 | ExpectedStmt ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) { |
7202 | |
7203 | Error Err = Error::success(); |
7204 | auto ToBuiltinLoc = importChecked(Err, From: E->getBuiltinLoc()); |
7205 | auto ToSubExpr = importChecked(Err, From: E->getSubExpr()); |
7206 | auto ToWrittenTypeInfo = importChecked(Err, From: E->getWrittenTypeInfo()); |
7207 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
7208 | auto ToType = importChecked(Err, E->getType()); |
7209 | if (Err) |
7210 | return std::move(Err); |
7211 | |
7212 | return new (Importer.getToContext()) VAArgExpr( |
7213 | ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType, |
7214 | E->isMicrosoftABI()); |
7215 | } |
7216 | |
7217 | ExpectedStmt ASTNodeImporter::VisitChooseExpr(ChooseExpr *E) { |
7218 | |
7219 | Error Err = Error::success(); |
7220 | auto ToCond = importChecked(Err, From: E->getCond()); |
7221 | auto ToLHS = importChecked(Err, From: E->getLHS()); |
7222 | auto ToRHS = importChecked(Err, From: E->getRHS()); |
7223 | auto ToBuiltinLoc = importChecked(Err, From: E->getBuiltinLoc()); |
7224 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
7225 | auto ToType = importChecked(Err, E->getType()); |
7226 | if (Err) |
7227 | return std::move(Err); |
7228 | |
7229 | ExprValueKind VK = E->getValueKind(); |
7230 | ExprObjectKind OK = E->getObjectKind(); |
7231 | |
7232 | // The value of CondIsTrue only matters if the value is not |
7233 | // condition-dependent. |
7234 | bool CondIsTrue = !E->isConditionDependent() && E->isConditionTrue(); |
7235 | |
7236 | return new (Importer.getToContext()) |
7237 | ChooseExpr(ToBuiltinLoc, ToCond, ToLHS, ToRHS, ToType, VK, OK, |
7238 | ToRParenLoc, CondIsTrue); |
7239 | } |
7240 | |
7241 | ExpectedStmt ASTNodeImporter::VisitConvertVectorExpr(ConvertVectorExpr *E) { |
7242 | Error Err = Error::success(); |
7243 | auto *ToSrcExpr = importChecked(Err, From: E->getSrcExpr()); |
7244 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
7245 | auto ToBuiltinLoc = importChecked(Err, From: E->getBuiltinLoc()); |
7246 | auto ToType = importChecked(Err, E->getType()); |
7247 | auto *ToTSI = importChecked(Err, From: E->getTypeSourceInfo()); |
7248 | if (Err) |
7249 | return std::move(Err); |
7250 | |
7251 | return new (Importer.getToContext()) |
7252 | ConvertVectorExpr(ToSrcExpr, ToTSI, ToType, E->getValueKind(), |
7253 | E->getObjectKind(), ToBuiltinLoc, ToRParenLoc); |
7254 | } |
7255 | |
7256 | ExpectedStmt ASTNodeImporter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) { |
7257 | Error Err = Error::success(); |
7258 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
7259 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
7260 | auto ToType = importChecked(Err, E->getType()); |
7261 | const unsigned NumSubExprs = E->getNumSubExprs(); |
7262 | |
7263 | llvm::SmallVector<Expr *, 8> ToSubExprs; |
7264 | llvm::ArrayRef<Expr *> FromSubExprs(E->getSubExprs(), NumSubExprs); |
7265 | ToSubExprs.resize(N: NumSubExprs); |
7266 | |
7267 | if ((Err = ImportContainerChecked(InContainer: FromSubExprs, OutContainer&: ToSubExprs))) |
7268 | return std::move(Err); |
7269 | |
7270 | return new (Importer.getToContext()) ShuffleVectorExpr( |
7271 | Importer.getToContext(), ToSubExprs, ToType, ToBeginLoc, ToRParenLoc); |
7272 | } |
7273 | |
7274 | ExpectedStmt ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) { |
7275 | ExpectedType TypeOrErr = import(E->getType()); |
7276 | if (!TypeOrErr) |
7277 | return TypeOrErr.takeError(); |
7278 | |
7279 | ExpectedSLoc BeginLocOrErr = import(From: E->getBeginLoc()); |
7280 | if (!BeginLocOrErr) |
7281 | return BeginLocOrErr.takeError(); |
7282 | |
7283 | return new (Importer.getToContext()) GNUNullExpr(*TypeOrErr, *BeginLocOrErr); |
7284 | } |
7285 | |
7286 | ExpectedStmt |
7287 | ASTNodeImporter::VisitGenericSelectionExpr(GenericSelectionExpr *E) { |
7288 | Error Err = Error::success(); |
7289 | auto ToGenericLoc = importChecked(Err, From: E->getGenericLoc()); |
7290 | Expr *ToControllingExpr = nullptr; |
7291 | TypeSourceInfo *ToControllingType = nullptr; |
7292 | if (E->isExprPredicate()) |
7293 | ToControllingExpr = importChecked(Err, From: E->getControllingExpr()); |
7294 | else |
7295 | ToControllingType = importChecked(Err, From: E->getControllingType()); |
7296 | assert((ToControllingExpr || ToControllingType) && |
7297 | "Either the controlling expr or type must be nonnull" ); |
7298 | auto ToDefaultLoc = importChecked(Err, From: E->getDefaultLoc()); |
7299 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
7300 | if (Err) |
7301 | return std::move(Err); |
7302 | |
7303 | ArrayRef<const TypeSourceInfo *> FromAssocTypes(E->getAssocTypeSourceInfos()); |
7304 | SmallVector<TypeSourceInfo *, 1> ToAssocTypes(FromAssocTypes.size()); |
7305 | if (Error Err = ImportContainerChecked(InContainer: FromAssocTypes, OutContainer&: ToAssocTypes)) |
7306 | return std::move(Err); |
7307 | |
7308 | ArrayRef<const Expr *> FromAssocExprs(E->getAssocExprs()); |
7309 | SmallVector<Expr *, 1> ToAssocExprs(FromAssocExprs.size()); |
7310 | if (Error Err = ImportContainerChecked(InContainer: FromAssocExprs, OutContainer&: ToAssocExprs)) |
7311 | return std::move(Err); |
7312 | |
7313 | const ASTContext &ToCtx = Importer.getToContext(); |
7314 | if (E->isResultDependent()) { |
7315 | if (ToControllingExpr) { |
7316 | return GenericSelectionExpr::Create( |
7317 | ToCtx, ToGenericLoc, ToControllingExpr, llvm::ArrayRef(ToAssocTypes), |
7318 | llvm::ArrayRef(ToAssocExprs), ToDefaultLoc, ToRParenLoc, |
7319 | E->containsUnexpandedParameterPack()); |
7320 | } |
7321 | return GenericSelectionExpr::Create( |
7322 | ToCtx, ToGenericLoc, ToControllingType, llvm::ArrayRef(ToAssocTypes), |
7323 | llvm::ArrayRef(ToAssocExprs), ToDefaultLoc, ToRParenLoc, |
7324 | E->containsUnexpandedParameterPack()); |
7325 | } |
7326 | |
7327 | if (ToControllingExpr) { |
7328 | return GenericSelectionExpr::Create( |
7329 | ToCtx, ToGenericLoc, ToControllingExpr, llvm::ArrayRef(ToAssocTypes), |
7330 | llvm::ArrayRef(ToAssocExprs), ToDefaultLoc, ToRParenLoc, |
7331 | E->containsUnexpandedParameterPack(), E->getResultIndex()); |
7332 | } |
7333 | return GenericSelectionExpr::Create( |
7334 | ToCtx, ToGenericLoc, ToControllingType, llvm::ArrayRef(ToAssocTypes), |
7335 | llvm::ArrayRef(ToAssocExprs), ToDefaultLoc, ToRParenLoc, |
7336 | E->containsUnexpandedParameterPack(), E->getResultIndex()); |
7337 | } |
7338 | |
7339 | ExpectedStmt ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) { |
7340 | |
7341 | Error Err = Error::success(); |
7342 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
7343 | auto ToType = importChecked(Err, E->getType()); |
7344 | auto ToFunctionName = importChecked(Err, From: E->getFunctionName()); |
7345 | if (Err) |
7346 | return std::move(Err); |
7347 | |
7348 | return PredefinedExpr::Create(Ctx: Importer.getToContext(), L: ToBeginLoc, FNTy: ToType, |
7349 | IK: E->getIdentKind(), IsTransparent: E->isTransparent(), |
7350 | SL: ToFunctionName); |
7351 | } |
7352 | |
7353 | ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) { |
7354 | |
7355 | Error Err = Error::success(); |
7356 | auto ToQualifierLoc = importChecked(Err, From: E->getQualifierLoc()); |
7357 | auto ToTemplateKeywordLoc = importChecked(Err, From: E->getTemplateKeywordLoc()); |
7358 | auto ToDecl = importChecked(Err, From: E->getDecl()); |
7359 | auto ToLocation = importChecked(Err, From: E->getLocation()); |
7360 | auto ToType = importChecked(Err, E->getType()); |
7361 | if (Err) |
7362 | return std::move(Err); |
7363 | |
7364 | NamedDecl *ToFoundD = nullptr; |
7365 | if (E->getDecl() != E->getFoundDecl()) { |
7366 | auto FoundDOrErr = import(From: E->getFoundDecl()); |
7367 | if (!FoundDOrErr) |
7368 | return FoundDOrErr.takeError(); |
7369 | ToFoundD = *FoundDOrErr; |
7370 | } |
7371 | |
7372 | TemplateArgumentListInfo ToTAInfo; |
7373 | TemplateArgumentListInfo *ToResInfo = nullptr; |
7374 | if (E->hasExplicitTemplateArgs()) { |
7375 | if (Error Err = |
7376 | ImportTemplateArgumentListInfo(FromLAngleLoc: E->getLAngleLoc(), FromRAngleLoc: E->getRAngleLoc(), |
7377 | Container: E->template_arguments(), Result&: ToTAInfo)) |
7378 | return std::move(Err); |
7379 | ToResInfo = &ToTAInfo; |
7380 | } |
7381 | |
7382 | auto *ToE = DeclRefExpr::Create( |
7383 | Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc, ToDecl, |
7384 | E->refersToEnclosingVariableOrCapture(), ToLocation, ToType, |
7385 | E->getValueKind(), ToFoundD, ToResInfo, E->isNonOdrUse()); |
7386 | if (E->hadMultipleCandidates()) |
7387 | ToE->setHadMultipleCandidates(true); |
7388 | ToE->setIsImmediateEscalating(E->isImmediateEscalating()); |
7389 | return ToE; |
7390 | } |
7391 | |
7392 | ExpectedStmt ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { |
7393 | ExpectedType TypeOrErr = import(E->getType()); |
7394 | if (!TypeOrErr) |
7395 | return TypeOrErr.takeError(); |
7396 | |
7397 | return new (Importer.getToContext()) ImplicitValueInitExpr(*TypeOrErr); |
7398 | } |
7399 | |
7400 | ExpectedStmt ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *E) { |
7401 | ExpectedExpr ToInitOrErr = import(From: E->getInit()); |
7402 | if (!ToInitOrErr) |
7403 | return ToInitOrErr.takeError(); |
7404 | |
7405 | ExpectedSLoc ToEqualOrColonLocOrErr = import(From: E->getEqualOrColonLoc()); |
7406 | if (!ToEqualOrColonLocOrErr) |
7407 | return ToEqualOrColonLocOrErr.takeError(); |
7408 | |
7409 | SmallVector<Expr *, 4> ToIndexExprs(E->getNumSubExprs() - 1); |
7410 | // List elements from the second, the first is Init itself |
7411 | for (unsigned I = 1, N = E->getNumSubExprs(); I < N; I++) { |
7412 | if (ExpectedExpr ToArgOrErr = import(From: E->getSubExpr(Idx: I))) |
7413 | ToIndexExprs[I - 1] = *ToArgOrErr; |
7414 | else |
7415 | return ToArgOrErr.takeError(); |
7416 | } |
7417 | |
7418 | SmallVector<Designator, 4> ToDesignators(E->size()); |
7419 | if (Error Err = ImportContainerChecked(InContainer: E->designators(), OutContainer&: ToDesignators)) |
7420 | return std::move(Err); |
7421 | |
7422 | return DesignatedInitExpr::Create( |
7423 | C: Importer.getToContext(), Designators: ToDesignators, |
7424 | IndexExprs: ToIndexExprs, EqualOrColonLoc: *ToEqualOrColonLocOrErr, |
7425 | GNUSyntax: E->usesGNUSyntax(), Init: *ToInitOrErr); |
7426 | } |
7427 | |
7428 | ExpectedStmt |
7429 | ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) { |
7430 | ExpectedType ToTypeOrErr = import(E->getType()); |
7431 | if (!ToTypeOrErr) |
7432 | return ToTypeOrErr.takeError(); |
7433 | |
7434 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
7435 | if (!ToLocationOrErr) |
7436 | return ToLocationOrErr.takeError(); |
7437 | |
7438 | return new (Importer.getToContext()) CXXNullPtrLiteralExpr( |
7439 | *ToTypeOrErr, *ToLocationOrErr); |
7440 | } |
7441 | |
7442 | ExpectedStmt ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) { |
7443 | ExpectedType ToTypeOrErr = import(E->getType()); |
7444 | if (!ToTypeOrErr) |
7445 | return ToTypeOrErr.takeError(); |
7446 | |
7447 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
7448 | if (!ToLocationOrErr) |
7449 | return ToLocationOrErr.takeError(); |
7450 | |
7451 | return IntegerLiteral::Create( |
7452 | Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr); |
7453 | } |
7454 | |
7455 | |
7456 | ExpectedStmt ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) { |
7457 | ExpectedType ToTypeOrErr = import(E->getType()); |
7458 | if (!ToTypeOrErr) |
7459 | return ToTypeOrErr.takeError(); |
7460 | |
7461 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
7462 | if (!ToLocationOrErr) |
7463 | return ToLocationOrErr.takeError(); |
7464 | |
7465 | return FloatingLiteral::Create( |
7466 | C: Importer.getToContext(), V: E->getValue(), isexact: E->isExact(), |
7467 | Type: *ToTypeOrErr, L: *ToLocationOrErr); |
7468 | } |
7469 | |
7470 | ExpectedStmt ASTNodeImporter::VisitImaginaryLiteral(ImaginaryLiteral *E) { |
7471 | auto ToTypeOrErr = import(E->getType()); |
7472 | if (!ToTypeOrErr) |
7473 | return ToTypeOrErr.takeError(); |
7474 | |
7475 | ExpectedExpr ToSubExprOrErr = import(From: E->getSubExpr()); |
7476 | if (!ToSubExprOrErr) |
7477 | return ToSubExprOrErr.takeError(); |
7478 | |
7479 | return new (Importer.getToContext()) ImaginaryLiteral( |
7480 | *ToSubExprOrErr, *ToTypeOrErr); |
7481 | } |
7482 | |
7483 | ExpectedStmt ASTNodeImporter::VisitFixedPointLiteral(FixedPointLiteral *E) { |
7484 | auto ToTypeOrErr = import(E->getType()); |
7485 | if (!ToTypeOrErr) |
7486 | return ToTypeOrErr.takeError(); |
7487 | |
7488 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
7489 | if (!ToLocationOrErr) |
7490 | return ToLocationOrErr.takeError(); |
7491 | |
7492 | return new (Importer.getToContext()) FixedPointLiteral( |
7493 | Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr, |
7494 | Importer.getToContext().getFixedPointScale(Ty: *ToTypeOrErr)); |
7495 | } |
7496 | |
7497 | ExpectedStmt ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) { |
7498 | ExpectedType ToTypeOrErr = import(E->getType()); |
7499 | if (!ToTypeOrErr) |
7500 | return ToTypeOrErr.takeError(); |
7501 | |
7502 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
7503 | if (!ToLocationOrErr) |
7504 | return ToLocationOrErr.takeError(); |
7505 | |
7506 | return new (Importer.getToContext()) CharacterLiteral( |
7507 | E->getValue(), E->getKind(), *ToTypeOrErr, *ToLocationOrErr); |
7508 | } |
7509 | |
7510 | ExpectedStmt ASTNodeImporter::VisitStringLiteral(StringLiteral *E) { |
7511 | ExpectedType ToTypeOrErr = import(E->getType()); |
7512 | if (!ToTypeOrErr) |
7513 | return ToTypeOrErr.takeError(); |
7514 | |
7515 | SmallVector<SourceLocation, 4> ToLocations(E->getNumConcatenated()); |
7516 | if (Error Err = ImportArrayChecked( |
7517 | Ibegin: E->tokloc_begin(), Iend: E->tokloc_end(), Obegin: ToLocations.begin())) |
7518 | return std::move(Err); |
7519 | |
7520 | return StringLiteral::Create( |
7521 | Ctx: Importer.getToContext(), Str: E->getBytes(), Kind: E->getKind(), Pascal: E->isPascal(), |
7522 | Ty: *ToTypeOrErr, Loc: ToLocations.data(), NumConcatenated: ToLocations.size()); |
7523 | } |
7524 | |
7525 | ExpectedStmt ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
7526 | |
7527 | Error Err = Error::success(); |
7528 | auto ToLParenLoc = importChecked(Err, From: E->getLParenLoc()); |
7529 | auto ToTypeSourceInfo = importChecked(Err, From: E->getTypeSourceInfo()); |
7530 | auto ToType = importChecked(Err, E->getType()); |
7531 | auto ToInitializer = importChecked(Err, From: E->getInitializer()); |
7532 | if (Err) |
7533 | return std::move(Err); |
7534 | |
7535 | return new (Importer.getToContext()) CompoundLiteralExpr( |
7536 | ToLParenLoc, ToTypeSourceInfo, ToType, E->getValueKind(), |
7537 | ToInitializer, E->isFileScope()); |
7538 | } |
7539 | |
7540 | ExpectedStmt ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) { |
7541 | |
7542 | Error Err = Error::success(); |
7543 | auto ToBuiltinLoc = importChecked(Err, From: E->getBuiltinLoc()); |
7544 | auto ToType = importChecked(Err, E->getType()); |
7545 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
7546 | if (Err) |
7547 | return std::move(Err); |
7548 | |
7549 | SmallVector<Expr *, 6> ToExprs(E->getNumSubExprs()); |
7550 | if (Error Err = ImportArrayChecked( |
7551 | Ibegin: E->getSubExprs(), Iend: E->getSubExprs() + E->getNumSubExprs(), |
7552 | Obegin: ToExprs.begin())) |
7553 | return std::move(Err); |
7554 | |
7555 | return new (Importer.getToContext()) AtomicExpr( |
7556 | |
7557 | ToBuiltinLoc, ToExprs, ToType, E->getOp(), ToRParenLoc); |
7558 | } |
7559 | |
7560 | ExpectedStmt ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) { |
7561 | Error Err = Error::success(); |
7562 | auto ToAmpAmpLoc = importChecked(Err, From: E->getAmpAmpLoc()); |
7563 | auto ToLabelLoc = importChecked(Err, From: E->getLabelLoc()); |
7564 | auto ToLabel = importChecked(Err, From: E->getLabel()); |
7565 | auto ToType = importChecked(Err, E->getType()); |
7566 | if (Err) |
7567 | return std::move(Err); |
7568 | |
7569 | return new (Importer.getToContext()) AddrLabelExpr( |
7570 | ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType); |
7571 | } |
7572 | ExpectedStmt ASTNodeImporter::VisitConstantExpr(ConstantExpr *E) { |
7573 | Error Err = Error::success(); |
7574 | auto ToSubExpr = importChecked(Err, E->getSubExpr()); |
7575 | auto ToResult = importChecked(Err, From: E->getAPValueResult()); |
7576 | if (Err) |
7577 | return std::move(Err); |
7578 | |
7579 | return ConstantExpr::Create(Importer.getToContext(), ToSubExpr, ToResult); |
7580 | } |
7581 | ExpectedStmt ASTNodeImporter::VisitParenExpr(ParenExpr *E) { |
7582 | Error Err = Error::success(); |
7583 | auto ToLParen = importChecked(Err, From: E->getLParen()); |
7584 | auto ToRParen = importChecked(Err, From: E->getRParen()); |
7585 | auto ToSubExpr = importChecked(Err, From: E->getSubExpr()); |
7586 | if (Err) |
7587 | return std::move(Err); |
7588 | |
7589 | return new (Importer.getToContext()) |
7590 | ParenExpr(ToLParen, ToRParen, ToSubExpr); |
7591 | } |
7592 | |
7593 | ExpectedStmt ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) { |
7594 | SmallVector<Expr *, 4> ToExprs(E->getNumExprs()); |
7595 | if (Error Err = ImportContainerChecked(InContainer: E->exprs(), OutContainer&: ToExprs)) |
7596 | return std::move(Err); |
7597 | |
7598 | ExpectedSLoc ToLParenLocOrErr = import(From: E->getLParenLoc()); |
7599 | if (!ToLParenLocOrErr) |
7600 | return ToLParenLocOrErr.takeError(); |
7601 | |
7602 | ExpectedSLoc ToRParenLocOrErr = import(From: E->getRParenLoc()); |
7603 | if (!ToRParenLocOrErr) |
7604 | return ToRParenLocOrErr.takeError(); |
7605 | |
7606 | return ParenListExpr::Create(Ctx: Importer.getToContext(), LParenLoc: *ToLParenLocOrErr, |
7607 | Exprs: ToExprs, RParenLoc: *ToRParenLocOrErr); |
7608 | } |
7609 | |
7610 | ExpectedStmt ASTNodeImporter::VisitStmtExpr(StmtExpr *E) { |
7611 | Error Err = Error::success(); |
7612 | auto ToSubStmt = importChecked(Err, From: E->getSubStmt()); |
7613 | auto ToType = importChecked(Err, E->getType()); |
7614 | auto ToLParenLoc = importChecked(Err, From: E->getLParenLoc()); |
7615 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
7616 | if (Err) |
7617 | return std::move(Err); |
7618 | |
7619 | return new (Importer.getToContext()) |
7620 | StmtExpr(ToSubStmt, ToType, ToLParenLoc, ToRParenLoc, |
7621 | E->getTemplateDepth()); |
7622 | } |
7623 | |
7624 | ExpectedStmt ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) { |
7625 | Error Err = Error::success(); |
7626 | auto ToSubExpr = importChecked(Err, From: E->getSubExpr()); |
7627 | auto ToType = importChecked(Err, E->getType()); |
7628 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
7629 | if (Err) |
7630 | return std::move(Err); |
7631 | |
7632 | auto *UO = UnaryOperator::CreateEmpty(C: Importer.getToContext(), |
7633 | hasFPFeatures: E->hasStoredFPFeatures()); |
7634 | UO->setType(ToType); |
7635 | UO->setSubExpr(ToSubExpr); |
7636 | UO->setOpcode(E->getOpcode()); |
7637 | UO->setOperatorLoc(ToOperatorLoc); |
7638 | UO->setCanOverflow(E->canOverflow()); |
7639 | if (E->hasStoredFPFeatures()) |
7640 | UO->setStoredFPFeatures(E->getStoredFPFeatures()); |
7641 | |
7642 | return UO; |
7643 | } |
7644 | |
7645 | ExpectedStmt |
7646 | |
7647 | ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) { |
7648 | Error Err = Error::success(); |
7649 | auto ToType = importChecked(Err, E->getType()); |
7650 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
7651 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
7652 | if (Err) |
7653 | return std::move(Err); |
7654 | |
7655 | if (E->isArgumentType()) { |
7656 | Expected<TypeSourceInfo *> ToArgumentTypeInfoOrErr = |
7657 | import(From: E->getArgumentTypeInfo()); |
7658 | if (!ToArgumentTypeInfoOrErr) |
7659 | return ToArgumentTypeInfoOrErr.takeError(); |
7660 | |
7661 | return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr( |
7662 | E->getKind(), *ToArgumentTypeInfoOrErr, ToType, ToOperatorLoc, |
7663 | ToRParenLoc); |
7664 | } |
7665 | |
7666 | ExpectedExpr ToArgumentExprOrErr = import(From: E->getArgumentExpr()); |
7667 | if (!ToArgumentExprOrErr) |
7668 | return ToArgumentExprOrErr.takeError(); |
7669 | |
7670 | return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr( |
7671 | E->getKind(), *ToArgumentExprOrErr, ToType, ToOperatorLoc, ToRParenLoc); |
7672 | } |
7673 | |
7674 | ExpectedStmt ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) { |
7675 | Error Err = Error::success(); |
7676 | auto ToLHS = importChecked(Err, From: E->getLHS()); |
7677 | auto ToRHS = importChecked(Err, From: E->getRHS()); |
7678 | auto ToType = importChecked(Err, E->getType()); |
7679 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
7680 | if (Err) |
7681 | return std::move(Err); |
7682 | |
7683 | return BinaryOperator::Create( |
7684 | C: Importer.getToContext(), lhs: ToLHS, rhs: ToRHS, opc: E->getOpcode(), ResTy: ToType, |
7685 | VK: E->getValueKind(), OK: E->getObjectKind(), opLoc: ToOperatorLoc, |
7686 | FPFeatures: E->getFPFeatures()); |
7687 | } |
7688 | |
7689 | ExpectedStmt ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) { |
7690 | Error Err = Error::success(); |
7691 | auto ToCond = importChecked(Err, From: E->getCond()); |
7692 | auto ToQuestionLoc = importChecked(Err, E->getQuestionLoc()); |
7693 | auto ToLHS = importChecked(Err, From: E->getLHS()); |
7694 | auto ToColonLoc = importChecked(Err, E->getColonLoc()); |
7695 | auto ToRHS = importChecked(Err, From: E->getRHS()); |
7696 | auto ToType = importChecked(Err, E->getType()); |
7697 | if (Err) |
7698 | return std::move(Err); |
7699 | |
7700 | return new (Importer.getToContext()) ConditionalOperator( |
7701 | ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType, |
7702 | E->getValueKind(), E->getObjectKind()); |
7703 | } |
7704 | |
7705 | ExpectedStmt |
7706 | ASTNodeImporter::VisitBinaryConditionalOperator(BinaryConditionalOperator *E) { |
7707 | Error Err = Error::success(); |
7708 | auto ToCommon = importChecked(Err, From: E->getCommon()); |
7709 | auto ToOpaqueValue = importChecked(Err, From: E->getOpaqueValue()); |
7710 | auto ToCond = importChecked(Err, From: E->getCond()); |
7711 | auto ToTrueExpr = importChecked(Err, From: E->getTrueExpr()); |
7712 | auto ToFalseExpr = importChecked(Err, From: E->getFalseExpr()); |
7713 | auto ToQuestionLoc = importChecked(Err, E->getQuestionLoc()); |
7714 | auto ToColonLoc = importChecked(Err, E->getColonLoc()); |
7715 | auto ToType = importChecked(Err, E->getType()); |
7716 | if (Err) |
7717 | return std::move(Err); |
7718 | |
7719 | return new (Importer.getToContext()) BinaryConditionalOperator( |
7720 | ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr, |
7721 | ToQuestionLoc, ToColonLoc, ToType, E->getValueKind(), |
7722 | E->getObjectKind()); |
7723 | } |
7724 | |
7725 | ExpectedStmt ASTNodeImporter::VisitCXXRewrittenBinaryOperator( |
7726 | CXXRewrittenBinaryOperator *E) { |
7727 | Error Err = Error::success(); |
7728 | auto ToSemanticForm = importChecked(Err, From: E->getSemanticForm()); |
7729 | if (Err) |
7730 | return std::move(Err); |
7731 | |
7732 | return new (Importer.getToContext()) |
7733 | CXXRewrittenBinaryOperator(ToSemanticForm, E->isReversed()); |
7734 | } |
7735 | |
7736 | ExpectedStmt ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { |
7737 | Error Err = Error::success(); |
7738 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
7739 | auto ToQueriedTypeSourceInfo = |
7740 | importChecked(Err, From: E->getQueriedTypeSourceInfo()); |
7741 | auto ToDimensionExpression = importChecked(Err, From: E->getDimensionExpression()); |
7742 | auto ToEndLoc = importChecked(Err, From: E->getEndLoc()); |
7743 | auto ToType = importChecked(Err, E->getType()); |
7744 | if (Err) |
7745 | return std::move(Err); |
7746 | |
7747 | return new (Importer.getToContext()) ArrayTypeTraitExpr( |
7748 | ToBeginLoc, E->getTrait(), ToQueriedTypeSourceInfo, E->getValue(), |
7749 | ToDimensionExpression, ToEndLoc, ToType); |
7750 | } |
7751 | |
7752 | ExpectedStmt ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { |
7753 | Error Err = Error::success(); |
7754 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
7755 | auto ToQueriedExpression = importChecked(Err, From: E->getQueriedExpression()); |
7756 | auto ToEndLoc = importChecked(Err, From: E->getEndLoc()); |
7757 | auto ToType = importChecked(Err, E->getType()); |
7758 | if (Err) |
7759 | return std::move(Err); |
7760 | |
7761 | return new (Importer.getToContext()) ExpressionTraitExpr( |
7762 | ToBeginLoc, E->getTrait(), ToQueriedExpression, E->getValue(), |
7763 | ToEndLoc, ToType); |
7764 | } |
7765 | |
7766 | ExpectedStmt ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) { |
7767 | Error Err = Error::success(); |
7768 | auto ToLocation = importChecked(Err, From: E->getLocation()); |
7769 | auto ToType = importChecked(Err, E->getType()); |
7770 | auto ToSourceExpr = importChecked(Err, From: E->getSourceExpr()); |
7771 | if (Err) |
7772 | return std::move(Err); |
7773 | |
7774 | return new (Importer.getToContext()) OpaqueValueExpr( |
7775 | ToLocation, ToType, E->getValueKind(), E->getObjectKind(), ToSourceExpr); |
7776 | } |
7777 | |
7778 | ExpectedStmt ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { |
7779 | Error Err = Error::success(); |
7780 | auto ToLHS = importChecked(Err, From: E->getLHS()); |
7781 | auto ToRHS = importChecked(Err, From: E->getRHS()); |
7782 | auto ToType = importChecked(Err, E->getType()); |
7783 | auto ToRBracketLoc = importChecked(Err, From: E->getRBracketLoc()); |
7784 | if (Err) |
7785 | return std::move(Err); |
7786 | |
7787 | return new (Importer.getToContext()) ArraySubscriptExpr( |
7788 | ToLHS, ToRHS, ToType, E->getValueKind(), E->getObjectKind(), |
7789 | ToRBracketLoc); |
7790 | } |
7791 | |
7792 | ExpectedStmt |
7793 | ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) { |
7794 | Error Err = Error::success(); |
7795 | auto ToLHS = importChecked(Err, E->getLHS()); |
7796 | auto ToRHS = importChecked(Err, E->getRHS()); |
7797 | auto ToType = importChecked(Err, E->getType()); |
7798 | auto ToComputationLHSType = importChecked(Err, From: E->getComputationLHSType()); |
7799 | auto ToComputationResultType = |
7800 | importChecked(Err, From: E->getComputationResultType()); |
7801 | auto ToOperatorLoc = importChecked(Err, E->getOperatorLoc()); |
7802 | if (Err) |
7803 | return std::move(Err); |
7804 | |
7805 | return CompoundAssignOperator::Create( |
7806 | C: Importer.getToContext(), lhs: ToLHS, rhs: ToRHS, opc: E->getOpcode(), ResTy: ToType, |
7807 | VK: E->getValueKind(), OK: E->getObjectKind(), opLoc: ToOperatorLoc, |
7808 | FPFeatures: E->getFPFeatures(), |
7809 | CompLHSType: ToComputationLHSType, CompResultType: ToComputationResultType); |
7810 | } |
7811 | |
7812 | Expected<CXXCastPath> |
7813 | ASTNodeImporter::ImportCastPath(CastExpr *CE) { |
7814 | CXXCastPath Path; |
7815 | for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) { |
7816 | if (auto SpecOrErr = import(From: *I)) |
7817 | Path.push_back(Elt: *SpecOrErr); |
7818 | else |
7819 | return SpecOrErr.takeError(); |
7820 | } |
7821 | return Path; |
7822 | } |
7823 | |
7824 | ExpectedStmt ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) { |
7825 | ExpectedType ToTypeOrErr = import(E->getType()); |
7826 | if (!ToTypeOrErr) |
7827 | return ToTypeOrErr.takeError(); |
7828 | |
7829 | ExpectedExpr ToSubExprOrErr = import(E->getSubExpr()); |
7830 | if (!ToSubExprOrErr) |
7831 | return ToSubExprOrErr.takeError(); |
7832 | |
7833 | Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E); |
7834 | if (!ToBasePathOrErr) |
7835 | return ToBasePathOrErr.takeError(); |
7836 | |
7837 | return ImplicitCastExpr::Create( |
7838 | Context: Importer.getToContext(), T: *ToTypeOrErr, Kind: E->getCastKind(), Operand: *ToSubExprOrErr, |
7839 | BasePath: &(*ToBasePathOrErr), Cat: E->getValueKind(), FPO: E->getFPFeatures()); |
7840 | } |
7841 | |
7842 | ExpectedStmt ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) { |
7843 | Error Err = Error::success(); |
7844 | auto ToType = importChecked(Err, E->getType()); |
7845 | auto ToSubExpr = importChecked(Err, E->getSubExpr()); |
7846 | auto ToTypeInfoAsWritten = importChecked(Err, From: E->getTypeInfoAsWritten()); |
7847 | if (Err) |
7848 | return std::move(Err); |
7849 | |
7850 | Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E); |
7851 | if (!ToBasePathOrErr) |
7852 | return ToBasePathOrErr.takeError(); |
7853 | CXXCastPath *ToBasePath = &(*ToBasePathOrErr); |
7854 | |
7855 | switch (E->getStmtClass()) { |
7856 | case Stmt::CStyleCastExprClass: { |
7857 | auto *CCE = cast<CStyleCastExpr>(Val: E); |
7858 | ExpectedSLoc ToLParenLocOrErr = import(From: CCE->getLParenLoc()); |
7859 | if (!ToLParenLocOrErr) |
7860 | return ToLParenLocOrErr.takeError(); |
7861 | ExpectedSLoc ToRParenLocOrErr = import(From: CCE->getRParenLoc()); |
7862 | if (!ToRParenLocOrErr) |
7863 | return ToRParenLocOrErr.takeError(); |
7864 | return CStyleCastExpr::Create( |
7865 | Context: Importer.getToContext(), T: ToType, VK: E->getValueKind(), K: E->getCastKind(), |
7866 | Op: ToSubExpr, BasePath: ToBasePath, FPO: CCE->getFPFeatures(), WrittenTy: ToTypeInfoAsWritten, |
7867 | L: *ToLParenLocOrErr, R: *ToRParenLocOrErr); |
7868 | } |
7869 | |
7870 | case Stmt::CXXFunctionalCastExprClass: { |
7871 | auto *FCE = cast<CXXFunctionalCastExpr>(Val: E); |
7872 | ExpectedSLoc ToLParenLocOrErr = import(From: FCE->getLParenLoc()); |
7873 | if (!ToLParenLocOrErr) |
7874 | return ToLParenLocOrErr.takeError(); |
7875 | ExpectedSLoc ToRParenLocOrErr = import(From: FCE->getRParenLoc()); |
7876 | if (!ToRParenLocOrErr) |
7877 | return ToRParenLocOrErr.takeError(); |
7878 | return CXXFunctionalCastExpr::Create( |
7879 | Context: Importer.getToContext(), T: ToType, VK: E->getValueKind(), Written: ToTypeInfoAsWritten, |
7880 | Kind: E->getCastKind(), Op: ToSubExpr, Path: ToBasePath, FPO: FCE->getFPFeatures(), |
7881 | LPLoc: *ToLParenLocOrErr, RPLoc: *ToRParenLocOrErr); |
7882 | } |
7883 | |
7884 | case Stmt::ObjCBridgedCastExprClass: { |
7885 | auto *OCE = cast<ObjCBridgedCastExpr>(Val: E); |
7886 | ExpectedSLoc ToLParenLocOrErr = import(From: OCE->getLParenLoc()); |
7887 | if (!ToLParenLocOrErr) |
7888 | return ToLParenLocOrErr.takeError(); |
7889 | ExpectedSLoc ToBridgeKeywordLocOrErr = import(From: OCE->getBridgeKeywordLoc()); |
7890 | if (!ToBridgeKeywordLocOrErr) |
7891 | return ToBridgeKeywordLocOrErr.takeError(); |
7892 | return new (Importer.getToContext()) ObjCBridgedCastExpr( |
7893 | *ToLParenLocOrErr, OCE->getBridgeKind(), E->getCastKind(), |
7894 | *ToBridgeKeywordLocOrErr, ToTypeInfoAsWritten, ToSubExpr); |
7895 | } |
7896 | case Stmt::BuiltinBitCastExprClass: { |
7897 | auto *BBC = cast<BuiltinBitCastExpr>(Val: E); |
7898 | ExpectedSLoc ToKWLocOrErr = import(From: BBC->getBeginLoc()); |
7899 | if (!ToKWLocOrErr) |
7900 | return ToKWLocOrErr.takeError(); |
7901 | ExpectedSLoc ToRParenLocOrErr = import(From: BBC->getEndLoc()); |
7902 | if (!ToRParenLocOrErr) |
7903 | return ToRParenLocOrErr.takeError(); |
7904 | return new (Importer.getToContext()) BuiltinBitCastExpr( |
7905 | ToType, E->getValueKind(), E->getCastKind(), ToSubExpr, |
7906 | ToTypeInfoAsWritten, *ToKWLocOrErr, *ToRParenLocOrErr); |
7907 | } |
7908 | default: |
7909 | llvm_unreachable("Cast expression of unsupported type!" ); |
7910 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
7911 | } |
7912 | } |
7913 | |
7914 | ExpectedStmt ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *E) { |
7915 | SmallVector<OffsetOfNode, 4> ToNodes; |
7916 | for (int I = 0, N = E->getNumComponents(); I < N; ++I) { |
7917 | const OffsetOfNode &FromNode = E->getComponent(Idx: I); |
7918 | |
7919 | SourceLocation ToBeginLoc, ToEndLoc; |
7920 | |
7921 | if (FromNode.getKind() != OffsetOfNode::Base) { |
7922 | Error Err = Error::success(); |
7923 | ToBeginLoc = importChecked(Err, From: FromNode.getBeginLoc()); |
7924 | ToEndLoc = importChecked(Err, From: FromNode.getEndLoc()); |
7925 | if (Err) |
7926 | return std::move(Err); |
7927 | } |
7928 | |
7929 | switch (FromNode.getKind()) { |
7930 | case OffsetOfNode::Array: |
7931 | ToNodes.push_back( |
7932 | Elt: OffsetOfNode(ToBeginLoc, FromNode.getArrayExprIndex(), ToEndLoc)); |
7933 | break; |
7934 | case OffsetOfNode::Base: { |
7935 | auto ToBSOrErr = import(From: FromNode.getBase()); |
7936 | if (!ToBSOrErr) |
7937 | return ToBSOrErr.takeError(); |
7938 | ToNodes.push_back(Elt: OffsetOfNode(*ToBSOrErr)); |
7939 | break; |
7940 | } |
7941 | case OffsetOfNode::Field: { |
7942 | auto ToFieldOrErr = import(From: FromNode.getField()); |
7943 | if (!ToFieldOrErr) |
7944 | return ToFieldOrErr.takeError(); |
7945 | ToNodes.push_back(Elt: OffsetOfNode(ToBeginLoc, *ToFieldOrErr, ToEndLoc)); |
7946 | break; |
7947 | } |
7948 | case OffsetOfNode::Identifier: { |
7949 | IdentifierInfo *ToII = Importer.Import(FromId: FromNode.getFieldName()); |
7950 | ToNodes.push_back(Elt: OffsetOfNode(ToBeginLoc, ToII, ToEndLoc)); |
7951 | break; |
7952 | } |
7953 | } |
7954 | } |
7955 | |
7956 | SmallVector<Expr *, 4> ToExprs(E->getNumExpressions()); |
7957 | for (int I = 0, N = E->getNumExpressions(); I < N; ++I) { |
7958 | ExpectedExpr ToIndexExprOrErr = import(From: E->getIndexExpr(Idx: I)); |
7959 | if (!ToIndexExprOrErr) |
7960 | return ToIndexExprOrErr.takeError(); |
7961 | ToExprs[I] = *ToIndexExprOrErr; |
7962 | } |
7963 | |
7964 | Error Err = Error::success(); |
7965 | auto ToType = importChecked(Err, E->getType()); |
7966 | auto ToTypeSourceInfo = importChecked(Err, From: E->getTypeSourceInfo()); |
7967 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
7968 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
7969 | if (Err) |
7970 | return std::move(Err); |
7971 | |
7972 | return OffsetOfExpr::Create( |
7973 | C: Importer.getToContext(), type: ToType, OperatorLoc: ToOperatorLoc, tsi: ToTypeSourceInfo, comps: ToNodes, |
7974 | exprs: ToExprs, RParenLoc: ToRParenLoc); |
7975 | } |
7976 | |
7977 | ExpectedStmt ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { |
7978 | Error Err = Error::success(); |
7979 | auto ToType = importChecked(Err, E->getType()); |
7980 | auto ToOperand = importChecked(Err, From: E->getOperand()); |
7981 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
7982 | auto ToEndLoc = importChecked(Err, From: E->getEndLoc()); |
7983 | if (Err) |
7984 | return std::move(Err); |
7985 | |
7986 | CanThrowResult ToCanThrow; |
7987 | if (E->isValueDependent()) |
7988 | ToCanThrow = CT_Dependent; |
7989 | else |
7990 | ToCanThrow = E->getValue() ? CT_Can : CT_Cannot; |
7991 | |
7992 | return new (Importer.getToContext()) CXXNoexceptExpr( |
7993 | ToType, ToOperand, ToCanThrow, ToBeginLoc, ToEndLoc); |
7994 | } |
7995 | |
7996 | ExpectedStmt ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) { |
7997 | Error Err = Error::success(); |
7998 | auto ToSubExpr = importChecked(Err, From: E->getSubExpr()); |
7999 | auto ToType = importChecked(Err, E->getType()); |
8000 | auto ToThrowLoc = importChecked(Err, From: E->getThrowLoc()); |
8001 | if (Err) |
8002 | return std::move(Err); |
8003 | |
8004 | return new (Importer.getToContext()) CXXThrowExpr( |
8005 | ToSubExpr, ToType, ToThrowLoc, E->isThrownVariableInScope()); |
8006 | } |
8007 | |
8008 | ExpectedStmt ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { |
8009 | ExpectedSLoc ToUsedLocOrErr = import(From: E->getUsedLocation()); |
8010 | if (!ToUsedLocOrErr) |
8011 | return ToUsedLocOrErr.takeError(); |
8012 | |
8013 | auto ToParamOrErr = import(From: E->getParam()); |
8014 | if (!ToParamOrErr) |
8015 | return ToParamOrErr.takeError(); |
8016 | |
8017 | auto UsedContextOrErr = Importer.ImportContext(FromDC: E->getUsedContext()); |
8018 | if (!UsedContextOrErr) |
8019 | return UsedContextOrErr.takeError(); |
8020 | |
8021 | // Import the default arg if it was not imported yet. |
8022 | // This is needed because it can happen that during the import of the |
8023 | // default expression (from VisitParmVarDecl) the same ParmVarDecl is |
8024 | // encountered here. The default argument for a ParmVarDecl is set in the |
8025 | // ParmVarDecl only after it is imported (set in VisitParmVarDecl if not here, |
8026 | // see VisitParmVarDecl). |
8027 | ParmVarDecl *ToParam = *ToParamOrErr; |
8028 | if (!ToParam->getDefaultArg()) { |
8029 | std::optional<ParmVarDecl *> FromParam = |
8030 | Importer.getImportedFromDecl(ToD: ToParam); |
8031 | assert(FromParam && "ParmVarDecl was not imported?" ); |
8032 | |
8033 | if (Error Err = ImportDefaultArgOfParmVarDecl(FromParam: *FromParam, ToParam)) |
8034 | return std::move(Err); |
8035 | } |
8036 | Expr *RewrittenInit = nullptr; |
8037 | if (E->hasRewrittenInit()) { |
8038 | ExpectedExpr ExprOrErr = import(From: E->getRewrittenExpr()); |
8039 | if (!ExprOrErr) |
8040 | return ExprOrErr.takeError(); |
8041 | RewrittenInit = ExprOrErr.get(); |
8042 | } |
8043 | return CXXDefaultArgExpr::Create(C: Importer.getToContext(), Loc: *ToUsedLocOrErr, |
8044 | Param: *ToParamOrErr, RewrittenExpr: RewrittenInit, |
8045 | UsedContext: *UsedContextOrErr); |
8046 | } |
8047 | |
8048 | ExpectedStmt |
8049 | ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { |
8050 | Error Err = Error::success(); |
8051 | auto ToType = importChecked(Err, E->getType()); |
8052 | auto ToTypeSourceInfo = importChecked(Err, From: E->getTypeSourceInfo()); |
8053 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
8054 | if (Err) |
8055 | return std::move(Err); |
8056 | |
8057 | return new (Importer.getToContext()) CXXScalarValueInitExpr( |
8058 | ToType, ToTypeSourceInfo, ToRParenLoc); |
8059 | } |
8060 | |
8061 | ExpectedStmt |
8062 | ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { |
8063 | ExpectedExpr ToSubExprOrErr = import(From: E->getSubExpr()); |
8064 | if (!ToSubExprOrErr) |
8065 | return ToSubExprOrErr.takeError(); |
8066 | |
8067 | auto ToDtorOrErr = import(From: E->getTemporary()->getDestructor()); |
8068 | if (!ToDtorOrErr) |
8069 | return ToDtorOrErr.takeError(); |
8070 | |
8071 | ASTContext &ToCtx = Importer.getToContext(); |
8072 | CXXTemporary *Temp = CXXTemporary::Create(C: ToCtx, Destructor: *ToDtorOrErr); |
8073 | return CXXBindTemporaryExpr::Create(C: ToCtx, Temp, SubExpr: *ToSubExprOrErr); |
8074 | } |
8075 | |
8076 | ExpectedStmt |
8077 | |
8078 | ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) { |
8079 | Error Err = Error::success(); |
8080 | auto ToConstructor = importChecked(Err, E->getConstructor()); |
8081 | auto ToType = importChecked(Err, E->getType()); |
8082 | auto ToTypeSourceInfo = importChecked(Err, From: E->getTypeSourceInfo()); |
8083 | auto ToParenOrBraceRange = importChecked(Err, E->getParenOrBraceRange()); |
8084 | if (Err) |
8085 | return std::move(Err); |
8086 | |
8087 | SmallVector<Expr *, 8> ToArgs(E->getNumArgs()); |
8088 | if (Error Err = ImportContainerChecked(E->arguments(), ToArgs)) |
8089 | return std::move(Err); |
8090 | |
8091 | return CXXTemporaryObjectExpr::Create( |
8092 | Ctx: Importer.getToContext(), Cons: ToConstructor, Ty: ToType, TSI: ToTypeSourceInfo, Args: ToArgs, |
8093 | ParenOrBraceRange: ToParenOrBraceRange, HadMultipleCandidates: E->hadMultipleCandidates(), |
8094 | ListInitialization: E->isListInitialization(), StdInitListInitialization: E->isStdInitListInitialization(), |
8095 | ZeroInitialization: E->requiresZeroInitialization()); |
8096 | } |
8097 | |
8098 | ExpectedDecl ASTNodeImporter::VisitLifetimeExtendedTemporaryDecl( |
8099 | LifetimeExtendedTemporaryDecl *D) { |
8100 | DeclContext *DC, *LexicalDC; |
8101 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) |
8102 | return std::move(Err); |
8103 | |
8104 | Error Err = Error::success(); |
8105 | auto Temporary = importChecked(Err, From: D->getTemporaryExpr()); |
8106 | auto ExtendingDecl = importChecked(Err, From: D->getExtendingDecl()); |
8107 | if (Err) |
8108 | return std::move(Err); |
8109 | // FIXME: Should ManglingNumber get numbers associated with 'to' context? |
8110 | |
8111 | LifetimeExtendedTemporaryDecl *To; |
8112 | if (GetImportedOrCreateDecl(ToD&: To, FromD: D, args&: Temporary, args&: ExtendingDecl, |
8113 | args: D->getManglingNumber())) |
8114 | return To; |
8115 | |
8116 | To->setLexicalDeclContext(LexicalDC); |
8117 | LexicalDC->addDeclInternal(To); |
8118 | return To; |
8119 | } |
8120 | |
8121 | ExpectedStmt |
8122 | ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) { |
8123 | Error Err = Error::success(); |
8124 | auto ToType = importChecked(Err, E->getType()); |
8125 | Expr *ToTemporaryExpr = importChecked( |
8126 | Err, From: E->getLifetimeExtendedTemporaryDecl() ? nullptr : E->getSubExpr()); |
8127 | auto ToMaterializedDecl = |
8128 | importChecked(Err, From: E->getLifetimeExtendedTemporaryDecl()); |
8129 | if (Err) |
8130 | return std::move(Err); |
8131 | |
8132 | if (!ToTemporaryExpr) |
8133 | ToTemporaryExpr = cast<Expr>(Val: ToMaterializedDecl->getTemporaryExpr()); |
8134 | |
8135 | auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr( |
8136 | ToType, ToTemporaryExpr, E->isBoundToLvalueReference(), |
8137 | ToMaterializedDecl); |
8138 | |
8139 | return ToMTE; |
8140 | } |
8141 | |
8142 | ExpectedStmt ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) { |
8143 | Error Err = Error::success(); |
8144 | auto ToType = importChecked(Err, E->getType()); |
8145 | auto ToPattern = importChecked(Err, From: E->getPattern()); |
8146 | auto ToEllipsisLoc = importChecked(Err, From: E->getEllipsisLoc()); |
8147 | if (Err) |
8148 | return std::move(Err); |
8149 | |
8150 | return new (Importer.getToContext()) PackExpansionExpr( |
8151 | ToType, ToPattern, ToEllipsisLoc, E->getNumExpansions()); |
8152 | } |
8153 | |
8154 | ExpectedStmt ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) { |
8155 | Error Err = Error::success(); |
8156 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
8157 | auto ToPack = importChecked(Err, From: E->getPack()); |
8158 | auto ToPackLoc = importChecked(Err, From: E->getPackLoc()); |
8159 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
8160 | if (Err) |
8161 | return std::move(Err); |
8162 | |
8163 | std::optional<unsigned> Length; |
8164 | if (!E->isValueDependent()) |
8165 | Length = E->getPackLength(); |
8166 | |
8167 | SmallVector<TemplateArgument, 8> ToPartialArguments; |
8168 | if (E->isPartiallySubstituted()) { |
8169 | if (Error Err = ImportTemplateArguments(FromArgs: E->getPartialArguments(), |
8170 | ToArgs&: ToPartialArguments)) |
8171 | return std::move(Err); |
8172 | } |
8173 | |
8174 | return SizeOfPackExpr::Create( |
8175 | Context&: Importer.getToContext(), OperatorLoc: ToOperatorLoc, Pack: ToPack, PackLoc: ToPackLoc, RParenLoc: ToRParenLoc, |
8176 | Length, PartialArgs: ToPartialArguments); |
8177 | } |
8178 | |
8179 | |
8180 | ExpectedStmt ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *E) { |
8181 | Error Err = Error::success(); |
8182 | auto ToOperatorNew = importChecked(Err, From: E->getOperatorNew()); |
8183 | auto ToOperatorDelete = importChecked(Err, From: E->getOperatorDelete()); |
8184 | auto ToTypeIdParens = importChecked(Err, From: E->getTypeIdParens()); |
8185 | auto ToArraySize = importChecked(Err, From: E->getArraySize()); |
8186 | auto ToInitializer = importChecked(Err, From: E->getInitializer()); |
8187 | auto ToType = importChecked(Err, E->getType()); |
8188 | auto ToAllocatedTypeSourceInfo = |
8189 | importChecked(Err, From: E->getAllocatedTypeSourceInfo()); |
8190 | auto ToSourceRange = importChecked(Err, From: E->getSourceRange()); |
8191 | auto ToDirectInitRange = importChecked(Err, From: E->getDirectInitRange()); |
8192 | if (Err) |
8193 | return std::move(Err); |
8194 | |
8195 | SmallVector<Expr *, 4> ToPlacementArgs(E->getNumPlacementArgs()); |
8196 | if (Error Err = |
8197 | ImportContainerChecked(InContainer: E->placement_arguments(), OutContainer&: ToPlacementArgs)) |
8198 | return std::move(Err); |
8199 | |
8200 | return CXXNewExpr::Create( |
8201 | Ctx: Importer.getToContext(), IsGlobalNew: E->isGlobalNew(), OperatorNew: ToOperatorNew, |
8202 | OperatorDelete: ToOperatorDelete, ShouldPassAlignment: E->passAlignment(), UsualArrayDeleteWantsSize: E->doesUsualArrayDeleteWantSize(), |
8203 | PlacementArgs: ToPlacementArgs, TypeIdParens: ToTypeIdParens, ArraySize: ToArraySize, InitializationStyle: E->getInitializationStyle(), |
8204 | Initializer: ToInitializer, Ty: ToType, AllocatedTypeInfo: ToAllocatedTypeSourceInfo, Range: ToSourceRange, |
8205 | DirectInitRange: ToDirectInitRange); |
8206 | } |
8207 | |
8208 | ExpectedStmt ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
8209 | Error Err = Error::success(); |
8210 | auto ToType = importChecked(Err, E->getType()); |
8211 | auto ToOperatorDelete = importChecked(Err, From: E->getOperatorDelete()); |
8212 | auto ToArgument = importChecked(Err, From: E->getArgument()); |
8213 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
8214 | if (Err) |
8215 | return std::move(Err); |
8216 | |
8217 | return new (Importer.getToContext()) CXXDeleteExpr( |
8218 | ToType, E->isGlobalDelete(), E->isArrayForm(), E->isArrayFormAsWritten(), |
8219 | E->doesUsualArrayDeleteWantSize(), ToOperatorDelete, ToArgument, |
8220 | ToBeginLoc); |
8221 | } |
8222 | |
8223 | ExpectedStmt ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) { |
8224 | Error Err = Error::success(); |
8225 | auto ToType = importChecked(Err, E->getType()); |
8226 | auto ToLocation = importChecked(Err, From: E->getLocation()); |
8227 | auto ToConstructor = importChecked(Err, From: E->getConstructor()); |
8228 | auto ToParenOrBraceRange = importChecked(Err, From: E->getParenOrBraceRange()); |
8229 | if (Err) |
8230 | return std::move(Err); |
8231 | |
8232 | SmallVector<Expr *, 6> ToArgs(E->getNumArgs()); |
8233 | if (Error Err = ImportContainerChecked(InContainer: E->arguments(), OutContainer&: ToArgs)) |
8234 | return std::move(Err); |
8235 | |
8236 | CXXConstructExpr *ToE = CXXConstructExpr::Create( |
8237 | Ctx: Importer.getToContext(), Ty: ToType, Loc: ToLocation, Ctor: ToConstructor, |
8238 | Elidable: E->isElidable(), Args: ToArgs, HadMultipleCandidates: E->hadMultipleCandidates(), |
8239 | ListInitialization: E->isListInitialization(), StdInitListInitialization: E->isStdInitListInitialization(), |
8240 | ZeroInitialization: E->requiresZeroInitialization(), ConstructKind: E->getConstructionKind(), |
8241 | ParenOrBraceRange: ToParenOrBraceRange); |
8242 | ToE->setIsImmediateEscalating(E->isImmediateEscalating()); |
8243 | return ToE; |
8244 | } |
8245 | |
8246 | ExpectedStmt ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *E) { |
8247 | ExpectedExpr ToSubExprOrErr = import(E->getSubExpr()); |
8248 | if (!ToSubExprOrErr) |
8249 | return ToSubExprOrErr.takeError(); |
8250 | |
8251 | SmallVector<ExprWithCleanups::CleanupObject, 8> ToObjects(E->getNumObjects()); |
8252 | if (Error Err = ImportContainerChecked(InContainer: E->getObjects(), OutContainer&: ToObjects)) |
8253 | return std::move(Err); |
8254 | |
8255 | return ExprWithCleanups::Create( |
8256 | C: Importer.getToContext(), subexpr: *ToSubExprOrErr, CleanupsHaveSideEffects: E->cleanupsHaveSideEffects(), |
8257 | objects: ToObjects); |
8258 | } |
8259 | |
8260 | ExpectedStmt ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) { |
8261 | Error Err = Error::success(); |
8262 | auto ToCallee = importChecked(Err, E->getCallee()); |
8263 | auto ToType = importChecked(Err, E->getType()); |
8264 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); |
8265 | if (Err) |
8266 | return std::move(Err); |
8267 | |
8268 | SmallVector<Expr *, 4> ToArgs(E->getNumArgs()); |
8269 | if (Error Err = ImportContainerChecked(E->arguments(), ToArgs)) |
8270 | return std::move(Err); |
8271 | |
8272 | return CXXMemberCallExpr::Create(Ctx: Importer.getToContext(), Fn: ToCallee, Args: ToArgs, |
8273 | Ty: ToType, VK: E->getValueKind(), RP: ToRParenLoc, |
8274 | FPFeatures: E->getFPFeatures()); |
8275 | } |
8276 | |
8277 | ExpectedStmt ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) { |
8278 | ExpectedType ToTypeOrErr = import(E->getType()); |
8279 | if (!ToTypeOrErr) |
8280 | return ToTypeOrErr.takeError(); |
8281 | |
8282 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
8283 | if (!ToLocationOrErr) |
8284 | return ToLocationOrErr.takeError(); |
8285 | |
8286 | return CXXThisExpr::Create(Ctx: Importer.getToContext(), L: *ToLocationOrErr, |
8287 | Ty: *ToTypeOrErr, IsImplicit: E->isImplicit()); |
8288 | } |
8289 | |
8290 | ExpectedStmt ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) { |
8291 | ExpectedType ToTypeOrErr = import(E->getType()); |
8292 | if (!ToTypeOrErr) |
8293 | return ToTypeOrErr.takeError(); |
8294 | |
8295 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
8296 | if (!ToLocationOrErr) |
8297 | return ToLocationOrErr.takeError(); |
8298 | |
8299 | return CXXBoolLiteralExpr::Create(C: Importer.getToContext(), Val: E->getValue(), |
8300 | Ty: *ToTypeOrErr, Loc: *ToLocationOrErr); |
8301 | } |
8302 | |
8303 | ExpectedStmt ASTNodeImporter::VisitMemberExpr(MemberExpr *E) { |
8304 | Error Err = Error::success(); |
8305 | auto ToBase = importChecked(Err, From: E->getBase()); |
8306 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
8307 | auto ToQualifierLoc = importChecked(Err, From: E->getQualifierLoc()); |
8308 | auto ToTemplateKeywordLoc = importChecked(Err, From: E->getTemplateKeywordLoc()); |
8309 | auto ToMemberDecl = importChecked(Err, From: E->getMemberDecl()); |
8310 | auto ToType = importChecked(Err, E->getType()); |
8311 | auto ToDecl = importChecked(Err, From: E->getFoundDecl().getDecl()); |
8312 | auto ToName = importChecked(Err, From: E->getMemberNameInfo().getName()); |
8313 | auto ToLoc = importChecked(Err, From: E->getMemberNameInfo().getLoc()); |
8314 | if (Err) |
8315 | return std::move(Err); |
8316 | |
8317 | DeclAccessPair ToFoundDecl = |
8318 | DeclAccessPair::make(D: ToDecl, AS: E->getFoundDecl().getAccess()); |
8319 | |
8320 | DeclarationNameInfo ToMemberNameInfo(ToName, ToLoc); |
8321 | |
8322 | TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr; |
8323 | if (E->hasExplicitTemplateArgs()) { |
8324 | if (Error Err = |
8325 | ImportTemplateArgumentListInfo(FromLAngleLoc: E->getLAngleLoc(), FromRAngleLoc: E->getRAngleLoc(), |
8326 | Container: E->template_arguments(), Result&: ToTAInfo)) |
8327 | return std::move(Err); |
8328 | ResInfo = &ToTAInfo; |
8329 | } |
8330 | |
8331 | return MemberExpr::Create(C: Importer.getToContext(), Base: ToBase, IsArrow: E->isArrow(), |
8332 | OperatorLoc: ToOperatorLoc, QualifierLoc: ToQualifierLoc, TemplateKWLoc: ToTemplateKeywordLoc, |
8333 | MemberDecl: ToMemberDecl, FoundDecl: ToFoundDecl, MemberNameInfo: ToMemberNameInfo, |
8334 | TemplateArgs: ResInfo, T: ToType, VK: E->getValueKind(), |
8335 | OK: E->getObjectKind(), NOUR: E->isNonOdrUse()); |
8336 | } |
8337 | |
8338 | ExpectedStmt |
8339 | ASTNodeImporter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { |
8340 | Error Err = Error::success(); |
8341 | auto ToBase = importChecked(Err, From: E->getBase()); |
8342 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
8343 | auto ToQualifierLoc = importChecked(Err, From: E->getQualifierLoc()); |
8344 | auto ToScopeTypeInfo = importChecked(Err, From: E->getScopeTypeInfo()); |
8345 | auto ToColonColonLoc = importChecked(Err, From: E->getColonColonLoc()); |
8346 | auto ToTildeLoc = importChecked(Err, From: E->getTildeLoc()); |
8347 | if (Err) |
8348 | return std::move(Err); |
8349 | |
8350 | PseudoDestructorTypeStorage Storage; |
8351 | if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) { |
8352 | IdentifierInfo *ToII = Importer.Import(FromId: FromII); |
8353 | ExpectedSLoc ToDestroyedTypeLocOrErr = import(From: E->getDestroyedTypeLoc()); |
8354 | if (!ToDestroyedTypeLocOrErr) |
8355 | return ToDestroyedTypeLocOrErr.takeError(); |
8356 | Storage = PseudoDestructorTypeStorage(ToII, *ToDestroyedTypeLocOrErr); |
8357 | } else { |
8358 | if (auto ToTIOrErr = import(From: E->getDestroyedTypeInfo())) |
8359 | Storage = PseudoDestructorTypeStorage(*ToTIOrErr); |
8360 | else |
8361 | return ToTIOrErr.takeError(); |
8362 | } |
8363 | |
8364 | return new (Importer.getToContext()) CXXPseudoDestructorExpr( |
8365 | Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc, |
8366 | ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc, ToTildeLoc, Storage); |
8367 | } |
8368 | |
8369 | ExpectedStmt ASTNodeImporter::VisitCXXDependentScopeMemberExpr( |
8370 | CXXDependentScopeMemberExpr *E) { |
8371 | Error Err = Error::success(); |
8372 | auto ToType = importChecked(Err, E->getType()); |
8373 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
8374 | auto ToQualifierLoc = importChecked(Err, From: E->getQualifierLoc()); |
8375 | auto ToTemplateKeywordLoc = importChecked(Err, From: E->getTemplateKeywordLoc()); |
8376 | auto ToFirstQualifierFoundInScope = |
8377 | importChecked(Err, From: E->getFirstQualifierFoundInScope()); |
8378 | if (Err) |
8379 | return std::move(Err); |
8380 | |
8381 | Expr *ToBase = nullptr; |
8382 | if (!E->isImplicitAccess()) { |
8383 | if (ExpectedExpr ToBaseOrErr = import(From: E->getBase())) |
8384 | ToBase = *ToBaseOrErr; |
8385 | else |
8386 | return ToBaseOrErr.takeError(); |
8387 | } |
8388 | |
8389 | TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr; |
8390 | |
8391 | if (E->hasExplicitTemplateArgs()) { |
8392 | if (Error Err = |
8393 | ImportTemplateArgumentListInfo(FromLAngleLoc: E->getLAngleLoc(), FromRAngleLoc: E->getRAngleLoc(), |
8394 | Container: E->template_arguments(), Result&: ToTAInfo)) |
8395 | return std::move(Err); |
8396 | ResInfo = &ToTAInfo; |
8397 | } |
8398 | auto ToMember = importChecked(Err, From: E->getMember()); |
8399 | auto ToMemberLoc = importChecked(Err, From: E->getMemberLoc()); |
8400 | if (Err) |
8401 | return std::move(Err); |
8402 | DeclarationNameInfo ToMemberNameInfo(ToMember, ToMemberLoc); |
8403 | |
8404 | // Import additional name location/type info. |
8405 | if (Error Err = |
8406 | ImportDeclarationNameLoc(From: E->getMemberNameInfo(), To&: ToMemberNameInfo)) |
8407 | return std::move(Err); |
8408 | |
8409 | return CXXDependentScopeMemberExpr::Create( |
8410 | Ctx: Importer.getToContext(), Base: ToBase, BaseType: ToType, IsArrow: E->isArrow(), OperatorLoc: ToOperatorLoc, |
8411 | QualifierLoc: ToQualifierLoc, TemplateKWLoc: ToTemplateKeywordLoc, FirstQualifierFoundInScope: ToFirstQualifierFoundInScope, |
8412 | MemberNameInfo: ToMemberNameInfo, TemplateArgs: ResInfo); |
8413 | } |
8414 | |
8415 | ExpectedStmt |
8416 | ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) { |
8417 | Error Err = Error::success(); |
8418 | auto ToQualifierLoc = importChecked(Err, From: E->getQualifierLoc()); |
8419 | auto ToTemplateKeywordLoc = importChecked(Err, From: E->getTemplateKeywordLoc()); |
8420 | auto ToDeclName = importChecked(Err, From: E->getDeclName()); |
8421 | auto ToNameLoc = importChecked(Err, From: E->getNameInfo().getLoc()); |
8422 | auto ToLAngleLoc = importChecked(Err, From: E->getLAngleLoc()); |
8423 | auto ToRAngleLoc = importChecked(Err, From: E->getRAngleLoc()); |
8424 | if (Err) |
8425 | return std::move(Err); |
8426 | |
8427 | DeclarationNameInfo ToNameInfo(ToDeclName, ToNameLoc); |
8428 | if (Error Err = ImportDeclarationNameLoc(From: E->getNameInfo(), To&: ToNameInfo)) |
8429 | return std::move(Err); |
8430 | |
8431 | TemplateArgumentListInfo ToTAInfo(ToLAngleLoc, ToRAngleLoc); |
8432 | TemplateArgumentListInfo *ResInfo = nullptr; |
8433 | if (E->hasExplicitTemplateArgs()) { |
8434 | if (Error Err = |
8435 | ImportTemplateArgumentListInfo(Container: E->template_arguments(), ToTAInfo)) |
8436 | return std::move(Err); |
8437 | ResInfo = &ToTAInfo; |
8438 | } |
8439 | |
8440 | return DependentScopeDeclRefExpr::Create( |
8441 | Context: Importer.getToContext(), QualifierLoc: ToQualifierLoc, TemplateKWLoc: ToTemplateKeywordLoc, |
8442 | NameInfo: ToNameInfo, TemplateArgs: ResInfo); |
8443 | } |
8444 | |
8445 | ExpectedStmt ASTNodeImporter::VisitCXXUnresolvedConstructExpr( |
8446 | CXXUnresolvedConstructExpr *E) { |
8447 | Error Err = Error::success(); |
8448 | auto ToLParenLoc = importChecked(Err, From: E->getLParenLoc()); |
8449 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
8450 | auto ToType = importChecked(Err, E->getType()); |
8451 | auto ToTypeSourceInfo = importChecked(Err, From: E->getTypeSourceInfo()); |
8452 | if (Err) |
8453 | return std::move(Err); |
8454 | |
8455 | SmallVector<Expr *, 8> ToArgs(E->getNumArgs()); |
8456 | if (Error Err = |
8457 | ImportArrayChecked(Ibegin: E->arg_begin(), Iend: E->arg_end(), Obegin: ToArgs.begin())) |
8458 | return std::move(Err); |
8459 | |
8460 | return CXXUnresolvedConstructExpr::Create( |
8461 | Context: Importer.getToContext(), T: ToType, TSI: ToTypeSourceInfo, LParenLoc: ToLParenLoc, |
8462 | Args: llvm::ArrayRef(ToArgs), RParenLoc: ToRParenLoc, IsListInit: E->isListInitialization()); |
8463 | } |
8464 | |
8465 | ExpectedStmt |
8466 | ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) { |
8467 | Expected<CXXRecordDecl *> ToNamingClassOrErr = import(From: E->getNamingClass()); |
8468 | if (!ToNamingClassOrErr) |
8469 | return ToNamingClassOrErr.takeError(); |
8470 | |
8471 | auto ToQualifierLocOrErr = import(E->getQualifierLoc()); |
8472 | if (!ToQualifierLocOrErr) |
8473 | return ToQualifierLocOrErr.takeError(); |
8474 | |
8475 | Error Err = Error::success(); |
8476 | auto ToName = importChecked(Err, E->getName()); |
8477 | auto ToNameLoc = importChecked(Err, E->getNameLoc()); |
8478 | if (Err) |
8479 | return std::move(Err); |
8480 | DeclarationNameInfo ToNameInfo(ToName, ToNameLoc); |
8481 | |
8482 | // Import additional name location/type info. |
8483 | if (Error Err = ImportDeclarationNameLoc(From: E->getNameInfo(), To&: ToNameInfo)) |
8484 | return std::move(Err); |
8485 | |
8486 | UnresolvedSet<8> ToDecls; |
8487 | for (auto *D : E->decls()) |
8488 | if (auto ToDOrErr = import(D)) |
8489 | ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr)); |
8490 | else |
8491 | return ToDOrErr.takeError(); |
8492 | |
8493 | if (E->hasExplicitTemplateArgs()) { |
8494 | TemplateArgumentListInfo ToTAInfo; |
8495 | if (Error Err = ImportTemplateArgumentListInfo( |
8496 | E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(), |
8497 | ToTAInfo)) |
8498 | return std::move(Err); |
8499 | |
8500 | ExpectedSLoc ToTemplateKeywordLocOrErr = import(E->getTemplateKeywordLoc()); |
8501 | if (!ToTemplateKeywordLocOrErr) |
8502 | return ToTemplateKeywordLocOrErr.takeError(); |
8503 | |
8504 | const bool KnownDependent = |
8505 | (E->getDependence() & ExprDependence::TypeValue) == |
8506 | ExprDependence::TypeValue; |
8507 | return UnresolvedLookupExpr::Create( |
8508 | Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr, |
8509 | *ToTemplateKeywordLocOrErr, ToNameInfo, E->requiresADL(), &ToTAInfo, |
8510 | ToDecls.begin(), ToDecls.end(), KnownDependent); |
8511 | } |
8512 | |
8513 | return UnresolvedLookupExpr::Create( |
8514 | Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr, |
8515 | ToNameInfo, E->requiresADL(), E->isOverloaded(), ToDecls.begin(), |
8516 | ToDecls.end()); |
8517 | } |
8518 | |
8519 | ExpectedStmt |
8520 | ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) { |
8521 | Error Err = Error::success(); |
8522 | auto ToType = importChecked(Err, E->getType()); |
8523 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
8524 | auto ToQualifierLoc = importChecked(Err, E->getQualifierLoc()); |
8525 | auto ToTemplateKeywordLoc = importChecked(Err, E->getTemplateKeywordLoc()); |
8526 | auto ToName = importChecked(Err, E->getName()); |
8527 | auto ToNameLoc = importChecked(Err, E->getNameLoc()); |
8528 | if (Err) |
8529 | return std::move(Err); |
8530 | |
8531 | DeclarationNameInfo ToNameInfo(ToName, ToNameLoc); |
8532 | // Import additional name location/type info. |
8533 | if (Error Err = ImportDeclarationNameLoc(From: E->getNameInfo(), To&: ToNameInfo)) |
8534 | return std::move(Err); |
8535 | |
8536 | UnresolvedSet<8> ToDecls; |
8537 | for (Decl *D : E->decls()) |
8538 | if (auto ToDOrErr = import(D)) |
8539 | ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr)); |
8540 | else |
8541 | return ToDOrErr.takeError(); |
8542 | |
8543 | TemplateArgumentListInfo ToTAInfo; |
8544 | TemplateArgumentListInfo *ResInfo = nullptr; |
8545 | if (E->hasExplicitTemplateArgs()) { |
8546 | TemplateArgumentListInfo FromTAInfo; |
8547 | E->copyTemplateArgumentsInto(FromTAInfo); |
8548 | if (Error Err = ImportTemplateArgumentListInfo(Container: FromTAInfo, ToTAInfo)) |
8549 | return std::move(Err); |
8550 | ResInfo = &ToTAInfo; |
8551 | } |
8552 | |
8553 | Expr *ToBase = nullptr; |
8554 | if (!E->isImplicitAccess()) { |
8555 | if (ExpectedExpr ToBaseOrErr = import(From: E->getBase())) |
8556 | ToBase = *ToBaseOrErr; |
8557 | else |
8558 | return ToBaseOrErr.takeError(); |
8559 | } |
8560 | |
8561 | return UnresolvedMemberExpr::Create( |
8562 | Context: Importer.getToContext(), HasUnresolvedUsing: E->hasUnresolvedUsing(), Base: ToBase, BaseType: ToType, |
8563 | IsArrow: E->isArrow(), OperatorLoc: ToOperatorLoc, QualifierLoc: ToQualifierLoc, TemplateKWLoc: ToTemplateKeywordLoc, |
8564 | MemberNameInfo: ToNameInfo, TemplateArgs: ResInfo, Begin: ToDecls.begin(), End: ToDecls.end()); |
8565 | } |
8566 | |
8567 | ExpectedStmt ASTNodeImporter::VisitCallExpr(CallExpr *E) { |
8568 | Error Err = Error::success(); |
8569 | auto ToCallee = importChecked(Err, From: E->getCallee()); |
8570 | auto ToType = importChecked(Err, E->getType()); |
8571 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
8572 | if (Err) |
8573 | return std::move(Err); |
8574 | |
8575 | unsigned NumArgs = E->getNumArgs(); |
8576 | llvm::SmallVector<Expr *, 2> ToArgs(NumArgs); |
8577 | if (Error Err = ImportContainerChecked(InContainer: E->arguments(), OutContainer&: ToArgs)) |
8578 | return std::move(Err); |
8579 | |
8580 | if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(Val: E)) { |
8581 | return CXXOperatorCallExpr::Create( |
8582 | Ctx: Importer.getToContext(), OpKind: OCE->getOperator(), Fn: ToCallee, Args: ToArgs, Ty: ToType, |
8583 | VK: OCE->getValueKind(), OperatorLoc: ToRParenLoc, FPFeatures: OCE->getFPFeatures(), |
8584 | UsesADL: OCE->getADLCallKind()); |
8585 | } |
8586 | |
8587 | return CallExpr::Create(Ctx: Importer.getToContext(), Fn: ToCallee, Args: ToArgs, Ty: ToType, |
8588 | VK: E->getValueKind(), RParenLoc: ToRParenLoc, FPFeatures: E->getFPFeatures(), |
8589 | /*MinNumArgs=*/0, UsesADL: E->getADLCallKind()); |
8590 | } |
8591 | |
8592 | ExpectedStmt ASTNodeImporter::VisitLambdaExpr(LambdaExpr *E) { |
8593 | CXXRecordDecl *FromClass = E->getLambdaClass(); |
8594 | auto ToClassOrErr = import(From: FromClass); |
8595 | if (!ToClassOrErr) |
8596 | return ToClassOrErr.takeError(); |
8597 | CXXRecordDecl *ToClass = *ToClassOrErr; |
8598 | |
8599 | auto ToCallOpOrErr = import(From: E->getCallOperator()); |
8600 | if (!ToCallOpOrErr) |
8601 | return ToCallOpOrErr.takeError(); |
8602 | |
8603 | SmallVector<Expr *, 8> ToCaptureInits(E->capture_size()); |
8604 | if (Error Err = ImportContainerChecked(InContainer: E->capture_inits(), OutContainer&: ToCaptureInits)) |
8605 | return std::move(Err); |
8606 | |
8607 | Error Err = Error::success(); |
8608 | auto ToIntroducerRange = importChecked(Err, From: E->getIntroducerRange()); |
8609 | auto ToCaptureDefaultLoc = importChecked(Err, From: E->getCaptureDefaultLoc()); |
8610 | auto ToEndLoc = importChecked(Err, From: E->getEndLoc()); |
8611 | if (Err) |
8612 | return std::move(Err); |
8613 | |
8614 | return LambdaExpr::Create(C: Importer.getToContext(), Class: ToClass, IntroducerRange: ToIntroducerRange, |
8615 | CaptureDefault: E->getCaptureDefault(), CaptureDefaultLoc: ToCaptureDefaultLoc, |
8616 | ExplicitParams: E->hasExplicitParameters(), |
8617 | ExplicitResultType: E->hasExplicitResultType(), CaptureInits: ToCaptureInits, |
8618 | ClosingBrace: ToEndLoc, ContainsUnexpandedParameterPack: E->containsUnexpandedParameterPack()); |
8619 | } |
8620 | |
8621 | |
8622 | ExpectedStmt ASTNodeImporter::VisitInitListExpr(InitListExpr *E) { |
8623 | Error Err = Error::success(); |
8624 | auto ToLBraceLoc = importChecked(Err, From: E->getLBraceLoc()); |
8625 | auto ToRBraceLoc = importChecked(Err, From: E->getRBraceLoc()); |
8626 | auto ToType = importChecked(Err, E->getType()); |
8627 | if (Err) |
8628 | return std::move(Err); |
8629 | |
8630 | SmallVector<Expr *, 4> ToExprs(E->getNumInits()); |
8631 | if (Error Err = ImportContainerChecked(InContainer: E->inits(), OutContainer&: ToExprs)) |
8632 | return std::move(Err); |
8633 | |
8634 | ASTContext &ToCtx = Importer.getToContext(); |
8635 | InitListExpr *To = new (ToCtx) InitListExpr( |
8636 | ToCtx, ToLBraceLoc, ToExprs, ToRBraceLoc); |
8637 | To->setType(ToType); |
8638 | |
8639 | if (E->hasArrayFiller()) { |
8640 | if (ExpectedExpr ToFillerOrErr = import(From: E->getArrayFiller())) |
8641 | To->setArrayFiller(*ToFillerOrErr); |
8642 | else |
8643 | return ToFillerOrErr.takeError(); |
8644 | } |
8645 | |
8646 | if (FieldDecl *FromFD = E->getInitializedFieldInUnion()) { |
8647 | if (auto ToFDOrErr = import(From: FromFD)) |
8648 | To->setInitializedFieldInUnion(*ToFDOrErr); |
8649 | else |
8650 | return ToFDOrErr.takeError(); |
8651 | } |
8652 | |
8653 | if (InitListExpr *SyntForm = E->getSyntacticForm()) { |
8654 | if (auto ToSyntFormOrErr = import(From: SyntForm)) |
8655 | To->setSyntacticForm(*ToSyntFormOrErr); |
8656 | else |
8657 | return ToSyntFormOrErr.takeError(); |
8658 | } |
8659 | |
8660 | // Copy InitListExprBitfields, which are not handled in the ctor of |
8661 | // InitListExpr. |
8662 | To->sawArrayRangeDesignator(ARD: E->hadArrayRangeDesignator()); |
8663 | |
8664 | return To; |
8665 | } |
8666 | |
8667 | ExpectedStmt ASTNodeImporter::VisitCXXStdInitializerListExpr( |
8668 | CXXStdInitializerListExpr *E) { |
8669 | ExpectedType ToTypeOrErr = import(E->getType()); |
8670 | if (!ToTypeOrErr) |
8671 | return ToTypeOrErr.takeError(); |
8672 | |
8673 | ExpectedExpr ToSubExprOrErr = import(From: E->getSubExpr()); |
8674 | if (!ToSubExprOrErr) |
8675 | return ToSubExprOrErr.takeError(); |
8676 | |
8677 | return new (Importer.getToContext()) CXXStdInitializerListExpr( |
8678 | *ToTypeOrErr, *ToSubExprOrErr); |
8679 | } |
8680 | |
8681 | ExpectedStmt ASTNodeImporter::VisitCXXInheritedCtorInitExpr( |
8682 | CXXInheritedCtorInitExpr *E) { |
8683 | Error Err = Error::success(); |
8684 | auto ToLocation = importChecked(Err, From: E->getLocation()); |
8685 | auto ToType = importChecked(Err, E->getType()); |
8686 | auto ToConstructor = importChecked(Err, From: E->getConstructor()); |
8687 | if (Err) |
8688 | return std::move(Err); |
8689 | |
8690 | return new (Importer.getToContext()) CXXInheritedCtorInitExpr( |
8691 | ToLocation, ToType, ToConstructor, E->constructsVBase(), |
8692 | E->inheritedFromVBase()); |
8693 | } |
8694 | |
8695 | ExpectedStmt ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) { |
8696 | Error Err = Error::success(); |
8697 | auto ToType = importChecked(Err, E->getType()); |
8698 | auto ToCommonExpr = importChecked(Err, From: E->getCommonExpr()); |
8699 | auto ToSubExpr = importChecked(Err, From: E->getSubExpr()); |
8700 | if (Err) |
8701 | return std::move(Err); |
8702 | |
8703 | return new (Importer.getToContext()) ArrayInitLoopExpr( |
8704 | ToType, ToCommonExpr, ToSubExpr); |
8705 | } |
8706 | |
8707 | ExpectedStmt ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) { |
8708 | ExpectedType ToTypeOrErr = import(E->getType()); |
8709 | if (!ToTypeOrErr) |
8710 | return ToTypeOrErr.takeError(); |
8711 | return new (Importer.getToContext()) ArrayInitIndexExpr(*ToTypeOrErr); |
8712 | } |
8713 | |
8714 | ExpectedStmt ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) { |
8715 | ExpectedSLoc ToBeginLocOrErr = import(From: E->getBeginLoc()); |
8716 | if (!ToBeginLocOrErr) |
8717 | return ToBeginLocOrErr.takeError(); |
8718 | |
8719 | auto ToFieldOrErr = import(From: E->getField()); |
8720 | if (!ToFieldOrErr) |
8721 | return ToFieldOrErr.takeError(); |
8722 | |
8723 | auto UsedContextOrErr = Importer.ImportContext(FromDC: E->getUsedContext()); |
8724 | if (!UsedContextOrErr) |
8725 | return UsedContextOrErr.takeError(); |
8726 | |
8727 | FieldDecl *ToField = *ToFieldOrErr; |
8728 | assert(ToField->hasInClassInitializer() && |
8729 | "Field should have in-class initializer if there is a default init " |
8730 | "expression that uses it." ); |
8731 | if (!ToField->getInClassInitializer()) { |
8732 | // The in-class initializer may be not yet set in "To" AST even if the |
8733 | // field is already there. This must be set here to make construction of |
8734 | // CXXDefaultInitExpr work. |
8735 | auto ToInClassInitializerOrErr = |
8736 | import(From: E->getField()->getInClassInitializer()); |
8737 | if (!ToInClassInitializerOrErr) |
8738 | return ToInClassInitializerOrErr.takeError(); |
8739 | ToField->setInClassInitializer(*ToInClassInitializerOrErr); |
8740 | } |
8741 | |
8742 | Expr *RewrittenInit = nullptr; |
8743 | if (E->hasRewrittenInit()) { |
8744 | ExpectedExpr ExprOrErr = import(From: E->getRewrittenExpr()); |
8745 | if (!ExprOrErr) |
8746 | return ExprOrErr.takeError(); |
8747 | RewrittenInit = ExprOrErr.get(); |
8748 | } |
8749 | |
8750 | return CXXDefaultInitExpr::Create(Ctx: Importer.getToContext(), Loc: *ToBeginLocOrErr, |
8751 | Field: ToField, UsedContext: *UsedContextOrErr, RewrittenInitExpr: RewrittenInit); |
8752 | } |
8753 | |
8754 | ExpectedStmt ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) { |
8755 | Error Err = Error::success(); |
8756 | auto ToType = importChecked(Err, E->getType()); |
8757 | auto ToSubExpr = importChecked(Err, E->getSubExpr()); |
8758 | auto ToTypeInfoAsWritten = importChecked(Err, E->getTypeInfoAsWritten()); |
8759 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
8760 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
8761 | auto ToAngleBrackets = importChecked(Err, From: E->getAngleBrackets()); |
8762 | if (Err) |
8763 | return std::move(Err); |
8764 | |
8765 | ExprValueKind VK = E->getValueKind(); |
8766 | CastKind CK = E->getCastKind(); |
8767 | auto ToBasePathOrErr = ImportCastPath(E); |
8768 | if (!ToBasePathOrErr) |
8769 | return ToBasePathOrErr.takeError(); |
8770 | |
8771 | if (auto CCE = dyn_cast<CXXStaticCastExpr>(Val: E)) { |
8772 | return CXXStaticCastExpr::Create( |
8773 | Context: Importer.getToContext(), T: ToType, VK, K: CK, Op: ToSubExpr, Path: &(*ToBasePathOrErr), |
8774 | Written: ToTypeInfoAsWritten, FPO: CCE->getFPFeatures(), L: ToOperatorLoc, RParenLoc: ToRParenLoc, |
8775 | AngleBrackets: ToAngleBrackets); |
8776 | } else if (isa<CXXDynamicCastExpr>(Val: E)) { |
8777 | return CXXDynamicCastExpr::Create( |
8778 | Context: Importer.getToContext(), T: ToType, VK, Kind: CK, Op: ToSubExpr, Path: &(*ToBasePathOrErr), |
8779 | Written: ToTypeInfoAsWritten, L: ToOperatorLoc, RParenLoc: ToRParenLoc, AngleBrackets: ToAngleBrackets); |
8780 | } else if (isa<CXXReinterpretCastExpr>(Val: E)) { |
8781 | return CXXReinterpretCastExpr::Create( |
8782 | Context: Importer.getToContext(), T: ToType, VK, Kind: CK, Op: ToSubExpr, Path: &(*ToBasePathOrErr), |
8783 | WrittenTy: ToTypeInfoAsWritten, L: ToOperatorLoc, RParenLoc: ToRParenLoc, AngleBrackets: ToAngleBrackets); |
8784 | } else if (isa<CXXConstCastExpr>(Val: E)) { |
8785 | return CXXConstCastExpr::Create( |
8786 | Context: Importer.getToContext(), T: ToType, VK, Op: ToSubExpr, WrittenTy: ToTypeInfoAsWritten, |
8787 | L: ToOperatorLoc, RParenLoc: ToRParenLoc, AngleBrackets: ToAngleBrackets); |
8788 | } else { |
8789 | llvm_unreachable("Unknown cast type" ); |
8790 | return make_error<ASTImportError>(); |
8791 | } |
8792 | } |
8793 | |
8794 | ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr( |
8795 | SubstNonTypeTemplateParmExpr *E) { |
8796 | Error Err = Error::success(); |
8797 | auto ToType = importChecked(Err, E->getType()); |
8798 | auto ToExprLoc = importChecked(Err, E->getExprLoc()); |
8799 | auto ToAssociatedDecl = importChecked(Err, From: E->getAssociatedDecl()); |
8800 | auto ToReplacement = importChecked(Err, From: E->getReplacement()); |
8801 | if (Err) |
8802 | return std::move(Err); |
8803 | |
8804 | return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr( |
8805 | ToType, E->getValueKind(), ToExprLoc, ToReplacement, ToAssociatedDecl, |
8806 | E->getIndex(), E->getPackIndex(), E->isReferenceParameter()); |
8807 | } |
8808 | |
8809 | ExpectedStmt ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) { |
8810 | Error Err = Error::success(); |
8811 | auto ToType = importChecked(Err, E->getType()); |
8812 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
8813 | auto ToEndLoc = importChecked(Err, From: E->getEndLoc()); |
8814 | if (Err) |
8815 | return std::move(Err); |
8816 | |
8817 | SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs()); |
8818 | if (Error Err = ImportContainerChecked(InContainer: E->getArgs(), OutContainer&: ToArgs)) |
8819 | return std::move(Err); |
8820 | |
8821 | // According to Sema::BuildTypeTrait(), if E is value-dependent, |
8822 | // Value is always false. |
8823 | bool ToValue = (E->isValueDependent() ? false : E->getValue()); |
8824 | |
8825 | return TypeTraitExpr::Create( |
8826 | C: Importer.getToContext(), T: ToType, Loc: ToBeginLoc, Kind: E->getTrait(), Args: ToArgs, |
8827 | RParenLoc: ToEndLoc, Value: ToValue); |
8828 | } |
8829 | |
8830 | ExpectedStmt ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) { |
8831 | ExpectedType ToTypeOrErr = import(E->getType()); |
8832 | if (!ToTypeOrErr) |
8833 | return ToTypeOrErr.takeError(); |
8834 | |
8835 | auto ToSourceRangeOrErr = import(From: E->getSourceRange()); |
8836 | if (!ToSourceRangeOrErr) |
8837 | return ToSourceRangeOrErr.takeError(); |
8838 | |
8839 | if (E->isTypeOperand()) { |
8840 | if (auto ToTSIOrErr = import(From: E->getTypeOperandSourceInfo())) |
8841 | return new (Importer.getToContext()) CXXTypeidExpr( |
8842 | *ToTypeOrErr, *ToTSIOrErr, *ToSourceRangeOrErr); |
8843 | else |
8844 | return ToTSIOrErr.takeError(); |
8845 | } |
8846 | |
8847 | ExpectedExpr ToExprOperandOrErr = import(From: E->getExprOperand()); |
8848 | if (!ToExprOperandOrErr) |
8849 | return ToExprOperandOrErr.takeError(); |
8850 | |
8851 | return new (Importer.getToContext()) CXXTypeidExpr( |
8852 | *ToTypeOrErr, *ToExprOperandOrErr, *ToSourceRangeOrErr); |
8853 | } |
8854 | |
8855 | ExpectedStmt ASTNodeImporter::VisitCXXFoldExpr(CXXFoldExpr *E) { |
8856 | Error Err = Error::success(); |
8857 | |
8858 | QualType ToType = importChecked(Err, E->getType()); |
8859 | UnresolvedLookupExpr *ToCallee = importChecked(Err, From: E->getCallee()); |
8860 | SourceLocation ToLParenLoc = importChecked(Err, From: E->getLParenLoc()); |
8861 | Expr *ToLHS = importChecked(Err, From: E->getLHS()); |
8862 | SourceLocation ToEllipsisLoc = importChecked(Err, From: E->getEllipsisLoc()); |
8863 | Expr *ToRHS = importChecked(Err, From: E->getRHS()); |
8864 | SourceLocation ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
8865 | |
8866 | if (Err) |
8867 | return std::move(Err); |
8868 | |
8869 | return new (Importer.getToContext()) |
8870 | CXXFoldExpr(ToType, ToCallee, ToLParenLoc, ToLHS, E->getOperator(), |
8871 | ToEllipsisLoc, ToRHS, ToRParenLoc, E->getNumExpansions()); |
8872 | } |
8873 | |
8874 | Error ASTNodeImporter::ImportOverriddenMethods(CXXMethodDecl *ToMethod, |
8875 | CXXMethodDecl *FromMethod) { |
8876 | Error ImportErrors = Error::success(); |
8877 | for (auto *FromOverriddenMethod : FromMethod->overridden_methods()) { |
8878 | if (auto ImportedOrErr = import(From: FromOverriddenMethod)) |
8879 | ToMethod->getCanonicalDecl()->addOverriddenMethod(MD: cast<CXXMethodDecl>( |
8880 | Val: (*ImportedOrErr)->getCanonicalDecl())); |
8881 | else |
8882 | ImportErrors = |
8883 | joinErrors(E1: std::move(ImportErrors), E2: ImportedOrErr.takeError()); |
8884 | } |
8885 | return ImportErrors; |
8886 | } |
8887 | |
8888 | ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager, |
8889 | ASTContext &FromContext, FileManager &FromFileManager, |
8890 | bool MinimalImport, |
8891 | std::shared_ptr<ASTImporterSharedState> SharedState) |
8892 | : SharedState(SharedState), ToContext(ToContext), FromContext(FromContext), |
8893 | ToFileManager(ToFileManager), FromFileManager(FromFileManager), |
8894 | Minimal(MinimalImport), ODRHandling(ODRHandlingType::Conservative) { |
8895 | |
8896 | // Create a default state without the lookup table: LLDB case. |
8897 | if (!SharedState) { |
8898 | this->SharedState = std::make_shared<ASTImporterSharedState>(); |
8899 | } |
8900 | |
8901 | ImportedDecls[FromContext.getTranslationUnitDecl()] = |
8902 | ToContext.getTranslationUnitDecl(); |
8903 | } |
8904 | |
8905 | ASTImporter::~ASTImporter() = default; |
8906 | |
8907 | std::optional<unsigned> ASTImporter::getFieldIndex(Decl *F) { |
8908 | assert(F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) && |
8909 | "Try to get field index for non-field." ); |
8910 | |
8911 | auto *Owner = dyn_cast<RecordDecl>(Val: F->getDeclContext()); |
8912 | if (!Owner) |
8913 | return std::nullopt; |
8914 | |
8915 | unsigned Index = 0; |
8916 | for (const auto *D : Owner->decls()) { |
8917 | if (D == F) |
8918 | return Index; |
8919 | |
8920 | if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D)) |
8921 | ++Index; |
8922 | } |
8923 | |
8924 | llvm_unreachable("Field was not found in its parent context." ); |
8925 | |
8926 | return std::nullopt; |
8927 | } |
8928 | |
8929 | ASTImporter::FoundDeclsTy |
8930 | ASTImporter::findDeclsInToCtx(DeclContext *DC, DeclarationName Name) { |
8931 | // We search in the redecl context because of transparent contexts. |
8932 | // E.g. a simple C language enum is a transparent context: |
8933 | // enum E { A, B }; |
8934 | // Now if we had a global variable in the TU |
8935 | // int A; |
8936 | // then the enum constant 'A' and the variable 'A' violates ODR. |
8937 | // We can diagnose this only if we search in the redecl context. |
8938 | DeclContext *ReDC = DC->getRedeclContext(); |
8939 | if (SharedState->getLookupTable()) { |
8940 | ASTImporterLookupTable::LookupResult LookupResult = |
8941 | SharedState->getLookupTable()->lookup(DC: ReDC, Name); |
8942 | return FoundDeclsTy(LookupResult.begin(), LookupResult.end()); |
8943 | } else { |
8944 | DeclContext::lookup_result NoloadLookupResult = ReDC->noload_lookup(Name); |
8945 | FoundDeclsTy Result(NoloadLookupResult.begin(), NoloadLookupResult.end()); |
8946 | // We must search by the slow case of localUncachedLookup because that is |
8947 | // working even if there is no LookupPtr for the DC. We could use |
8948 | // DC::buildLookup() to create the LookupPtr, but that would load external |
8949 | // decls again, we must avoid that case. |
8950 | // Also, even if we had the LookupPtr, we must find Decls which are not |
8951 | // in the LookupPtr, so we need the slow case. |
8952 | // These cases are handled in ASTImporterLookupTable, but we cannot use |
8953 | // that with LLDB since that traverses through the AST which initiates the |
8954 | // load of external decls again via DC::decls(). And again, we must avoid |
8955 | // loading external decls during the import. |
8956 | if (Result.empty()) |
8957 | ReDC->localUncachedLookup(Name, Results&: Result); |
8958 | return Result; |
8959 | } |
8960 | } |
8961 | |
8962 | void ASTImporter::AddToLookupTable(Decl *ToD) { |
8963 | SharedState->addDeclToLookup(D: ToD); |
8964 | } |
8965 | |
8966 | Expected<Decl *> ASTImporter::ImportImpl(Decl *FromD) { |
8967 | // Import the decl using ASTNodeImporter. |
8968 | ASTNodeImporter Importer(*this); |
8969 | return Importer.Visit(FromD); |
8970 | } |
8971 | |
8972 | void ASTImporter::RegisterImportedDecl(Decl *FromD, Decl *ToD) { |
8973 | MapImported(From: FromD, To: ToD); |
8974 | } |
8975 | |
8976 | llvm::Expected<ExprWithCleanups::CleanupObject> |
8977 | ASTImporter::Import(ExprWithCleanups::CleanupObject From) { |
8978 | if (auto *CLE = From.dyn_cast<CompoundLiteralExpr *>()) { |
8979 | if (Expected<Expr *> R = Import(From: CLE)) |
8980 | return ExprWithCleanups::CleanupObject(cast<CompoundLiteralExpr>(Val: *R)); |
8981 | } |
8982 | |
8983 | // FIXME: Handle BlockDecl when we implement importing BlockExpr in |
8984 | // ASTNodeImporter. |
8985 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
8986 | } |
8987 | |
8988 | ExpectedTypePtr ASTImporter::Import(const Type *FromT) { |
8989 | if (!FromT) |
8990 | return FromT; |
8991 | |
8992 | // Check whether we've already imported this type. |
8993 | llvm::DenseMap<const Type *, const Type *>::iterator Pos = |
8994 | ImportedTypes.find(Val: FromT); |
8995 | if (Pos != ImportedTypes.end()) |
8996 | return Pos->second; |
8997 | |
8998 | // Import the type. |
8999 | ASTNodeImporter Importer(*this); |
9000 | ExpectedType ToTOrErr = Importer.Visit(T: FromT); |
9001 | if (!ToTOrErr) |
9002 | return ToTOrErr.takeError(); |
9003 | |
9004 | // Record the imported type. |
9005 | ImportedTypes[FromT] = ToTOrErr->getTypePtr(); |
9006 | |
9007 | return ToTOrErr->getTypePtr(); |
9008 | } |
9009 | |
9010 | Expected<QualType> ASTImporter::Import(QualType FromT) { |
9011 | if (FromT.isNull()) |
9012 | return QualType{}; |
9013 | |
9014 | ExpectedTypePtr ToTyOrErr = Import(FromT: FromT.getTypePtr()); |
9015 | if (!ToTyOrErr) |
9016 | return ToTyOrErr.takeError(); |
9017 | |
9018 | return ToContext.getQualifiedType(T: *ToTyOrErr, Qs: FromT.getLocalQualifiers()); |
9019 | } |
9020 | |
9021 | Expected<TypeSourceInfo *> ASTImporter::Import(TypeSourceInfo *FromTSI) { |
9022 | if (!FromTSI) |
9023 | return FromTSI; |
9024 | |
9025 | // FIXME: For now we just create a "trivial" type source info based |
9026 | // on the type and a single location. Implement a real version of this. |
9027 | ExpectedType TOrErr = Import(FromT: FromTSI->getType()); |
9028 | if (!TOrErr) |
9029 | return TOrErr.takeError(); |
9030 | ExpectedSLoc BeginLocOrErr = Import(FromLoc: FromTSI->getTypeLoc().getBeginLoc()); |
9031 | if (!BeginLocOrErr) |
9032 | return BeginLocOrErr.takeError(); |
9033 | |
9034 | return ToContext.getTrivialTypeSourceInfo(T: *TOrErr, Loc: *BeginLocOrErr); |
9035 | } |
9036 | |
9037 | namespace { |
9038 | // To use this object, it should be created before the new attribute is created, |
9039 | // and destructed after it is created. The construction already performs the |
9040 | // import of the data. |
9041 | template <typename T> struct AttrArgImporter { |
9042 | AttrArgImporter(const AttrArgImporter<T> &) = delete; |
9043 | AttrArgImporter(AttrArgImporter<T> &&) = default; |
9044 | AttrArgImporter<T> &operator=(const AttrArgImporter<T> &) = delete; |
9045 | AttrArgImporter<T> &operator=(AttrArgImporter<T> &&) = default; |
9046 | |
9047 | AttrArgImporter(ASTNodeImporter &I, Error &Err, const T &From) |
9048 | : To(I.importChecked(Err, From)) {} |
9049 | |
9050 | const T &value() { return To; } |
9051 | |
9052 | private: |
9053 | T To; |
9054 | }; |
9055 | |
9056 | // To use this object, it should be created before the new attribute is created, |
9057 | // and destructed after it is created. The construction already performs the |
9058 | // import of the data. The array data is accessible in a pointer form, this form |
9059 | // is used by the attribute classes. This object should be created once for the |
9060 | // array data to be imported (the array size is not imported, just copied). |
9061 | template <typename T> struct AttrArgArrayImporter { |
9062 | AttrArgArrayImporter(const AttrArgArrayImporter<T> &) = delete; |
9063 | AttrArgArrayImporter(AttrArgArrayImporter<T> &&) = default; |
9064 | AttrArgArrayImporter<T> &operator=(const AttrArgArrayImporter<T> &) = delete; |
9065 | AttrArgArrayImporter<T> &operator=(AttrArgArrayImporter<T> &&) = default; |
9066 | |
9067 | AttrArgArrayImporter(ASTNodeImporter &I, Error &Err, |
9068 | const llvm::iterator_range<T *> &From, |
9069 | unsigned ArraySize) { |
9070 | if (Err) |
9071 | return; |
9072 | To.reserve(ArraySize); |
9073 | Err = I.ImportContainerChecked(From, To); |
9074 | } |
9075 | |
9076 | T *value() { return To.data(); } |
9077 | |
9078 | private: |
9079 | llvm::SmallVector<T, 2> To; |
9080 | }; |
9081 | |
9082 | class AttrImporter { |
9083 | Error Err{Error::success()}; |
9084 | Attr *ToAttr = nullptr; |
9085 | ASTImporter &Importer; |
9086 | ASTNodeImporter NImporter; |
9087 | |
9088 | public: |
9089 | AttrImporter(ASTImporter &I) : Importer(I), NImporter(I) {} |
9090 | |
9091 | // Useful for accessing the imported attribute. |
9092 | template <typename T> T *castAttrAs() { return cast<T>(ToAttr); } |
9093 | template <typename T> const T *castAttrAs() const { return cast<T>(ToAttr); } |
9094 | |
9095 | // Create an "importer" for an attribute parameter. |
9096 | // Result of the 'value()' of that object is to be passed to the function |
9097 | // 'importAttr', in the order that is expected by the attribute class. |
9098 | template <class T> AttrArgImporter<T> importArg(const T &From) { |
9099 | return AttrArgImporter<T>(NImporter, Err, From); |
9100 | } |
9101 | |
9102 | // Create an "importer" for an attribute parameter that has array type. |
9103 | // Result of the 'value()' of that object is to be passed to the function |
9104 | // 'importAttr', then the size of the array as next argument. |
9105 | template <typename T> |
9106 | AttrArgArrayImporter<T> importArrayArg(const llvm::iterator_range<T *> &From, |
9107 | unsigned ArraySize) { |
9108 | return AttrArgArrayImporter<T>(NImporter, Err, From, ArraySize); |
9109 | } |
9110 | |
9111 | // Create an attribute object with the specified arguments. |
9112 | // The 'FromAttr' is the original (not imported) attribute, the 'ImportedArg' |
9113 | // should be values that are passed to the 'Create' function of the attribute. |
9114 | // (The 'Create' with 'ASTContext' first and 'AttributeCommonInfo' last is |
9115 | // used here.) As much data is copied or imported from the old attribute |
9116 | // as possible. The passed arguments should be already imported. |
9117 | // If an import error happens, the internal error is set to it, and any |
9118 | // further import attempt is ignored. |
9119 | template <typename T, typename... Arg> |
9120 | void importAttr(const T *FromAttr, Arg &&...ImportedArg) { |
9121 | static_assert(std::is_base_of<Attr, T>::value, |
9122 | "T should be subclass of Attr." ); |
9123 | assert(!ToAttr && "Use one AttrImporter to import one Attribute object." ); |
9124 | |
9125 | const IdentifierInfo *ToAttrName = Importer.Import(FromAttr->getAttrName()); |
9126 | const IdentifierInfo *ToScopeName = |
9127 | Importer.Import(FromAttr->getScopeName()); |
9128 | SourceRange ToAttrRange = |
9129 | NImporter.importChecked(Err, FromAttr->getRange()); |
9130 | SourceLocation ToScopeLoc = |
9131 | NImporter.importChecked(Err, FromAttr->getScopeLoc()); |
9132 | |
9133 | if (Err) |
9134 | return; |
9135 | |
9136 | AttributeCommonInfo ToI(ToAttrName, ToScopeName, ToAttrRange, ToScopeLoc, |
9137 | FromAttr->getParsedKind(), FromAttr->getForm()); |
9138 | // The "SemanticSpelling" is not needed to be passed to the constructor. |
9139 | // That value is recalculated from the SpellingListIndex if needed. |
9140 | ToAttr = T::Create(Importer.getToContext(), |
9141 | std::forward<Arg>(ImportedArg)..., ToI); |
9142 | |
9143 | ToAttr->setImplicit(FromAttr->isImplicit()); |
9144 | ToAttr->setPackExpansion(FromAttr->isPackExpansion()); |
9145 | if (auto *ToInheritableAttr = dyn_cast<InheritableAttr>(Val: ToAttr)) |
9146 | ToInheritableAttr->setInherited(FromAttr->isInherited()); |
9147 | } |
9148 | |
9149 | // Create a clone of the 'FromAttr' and import its source range only. |
9150 | // This causes objects with invalid references to be created if the 'FromAttr' |
9151 | // contains other data that should be imported. |
9152 | void cloneAttr(const Attr *FromAttr) { |
9153 | assert(!ToAttr && "Use one AttrImporter to import one Attribute object." ); |
9154 | |
9155 | SourceRange ToRange = NImporter.importChecked(Err, From: FromAttr->getRange()); |
9156 | if (Err) |
9157 | return; |
9158 | |
9159 | ToAttr = FromAttr->clone(C&: Importer.getToContext()); |
9160 | ToAttr->setRange(ToRange); |
9161 | ToAttr->setAttrName(Importer.Import(FromId: FromAttr->getAttrName())); |
9162 | } |
9163 | |
9164 | // Get the result of the previous import attempt (can be used only once). |
9165 | llvm::Expected<Attr *> getResult() && { |
9166 | if (Err) |
9167 | return std::move(Err); |
9168 | assert(ToAttr && "Attribute should be created." ); |
9169 | return ToAttr; |
9170 | } |
9171 | }; |
9172 | } // namespace |
9173 | |
9174 | Expected<Attr *> ASTImporter::Import(const Attr *FromAttr) { |
9175 | AttrImporter AI(*this); |
9176 | |
9177 | // FIXME: Is there some kind of AttrVisitor to use here? |
9178 | switch (FromAttr->getKind()) { |
9179 | case attr::Aligned: { |
9180 | auto *From = cast<AlignedAttr>(FromAttr); |
9181 | if (From->isAlignmentExpr()) |
9182 | AI.importAttr(From, true, AI.importArg(From->getAlignmentExpr()).value()); |
9183 | else |
9184 | AI.importAttr(From, false, |
9185 | AI.importArg(From->getAlignmentType()).value()); |
9186 | break; |
9187 | } |
9188 | |
9189 | case attr::AlignValue: { |
9190 | auto *From = cast<AlignValueAttr>(FromAttr); |
9191 | AI.importAttr(From, AI.importArg(From->getAlignment()).value()); |
9192 | break; |
9193 | } |
9194 | |
9195 | case attr::Format: { |
9196 | const auto *From = cast<FormatAttr>(FromAttr); |
9197 | AI.importAttr(From, Import(From->getType()), From->getFormatIdx(), |
9198 | From->getFirstArg()); |
9199 | break; |
9200 | } |
9201 | |
9202 | case attr::EnableIf: { |
9203 | const auto *From = cast<EnableIfAttr>(FromAttr); |
9204 | AI.importAttr(From, AI.importArg(From->getCond()).value(), |
9205 | From->getMessage()); |
9206 | break; |
9207 | } |
9208 | |
9209 | case attr::AssertCapability: { |
9210 | const auto *From = cast<AssertCapabilityAttr>(FromAttr); |
9211 | AI.importAttr(From, |
9212 | AI.importArrayArg(From->args(), From->args_size()).value(), |
9213 | From->args_size()); |
9214 | break; |
9215 | } |
9216 | case attr::AcquireCapability: { |
9217 | const auto *From = cast<AcquireCapabilityAttr>(FromAttr); |
9218 | AI.importAttr(From, |
9219 | AI.importArrayArg(From->args(), From->args_size()).value(), |
9220 | From->args_size()); |
9221 | break; |
9222 | } |
9223 | case attr::TryAcquireCapability: { |
9224 | const auto *From = cast<TryAcquireCapabilityAttr>(FromAttr); |
9225 | AI.importAttr(From, AI.importArg(From->getSuccessValue()).value(), |
9226 | AI.importArrayArg(From->args(), From->args_size()).value(), |
9227 | From->args_size()); |
9228 | break; |
9229 | } |
9230 | case attr::ReleaseCapability: { |
9231 | const auto *From = cast<ReleaseCapabilityAttr>(FromAttr); |
9232 | AI.importAttr(From, |
9233 | AI.importArrayArg(From->args(), From->args_size()).value(), |
9234 | From->args_size()); |
9235 | break; |
9236 | } |
9237 | case attr::RequiresCapability: { |
9238 | const auto *From = cast<RequiresCapabilityAttr>(FromAttr); |
9239 | AI.importAttr(From, |
9240 | AI.importArrayArg(From->args(), From->args_size()).value(), |
9241 | From->args_size()); |
9242 | break; |
9243 | } |
9244 | case attr::GuardedBy: { |
9245 | const auto *From = cast<GuardedByAttr>(FromAttr); |
9246 | AI.importAttr(From, AI.importArg(From->getArg()).value()); |
9247 | break; |
9248 | } |
9249 | case attr::PtGuardedBy: { |
9250 | const auto *From = cast<PtGuardedByAttr>(FromAttr); |
9251 | AI.importAttr(From, AI.importArg(From->getArg()).value()); |
9252 | break; |
9253 | } |
9254 | case attr::AcquiredAfter: { |
9255 | const auto *From = cast<AcquiredAfterAttr>(FromAttr); |
9256 | AI.importAttr(From, |
9257 | AI.importArrayArg(From->args(), From->args_size()).value(), |
9258 | From->args_size()); |
9259 | break; |
9260 | } |
9261 | case attr::AcquiredBefore: { |
9262 | const auto *From = cast<AcquiredBeforeAttr>(FromAttr); |
9263 | AI.importAttr(From, |
9264 | AI.importArrayArg(From->args(), From->args_size()).value(), |
9265 | From->args_size()); |
9266 | break; |
9267 | } |
9268 | case attr::AssertExclusiveLock: { |
9269 | const auto *From = cast<AssertExclusiveLockAttr>(FromAttr); |
9270 | AI.importAttr(From, |
9271 | AI.importArrayArg(From->args(), From->args_size()).value(), |
9272 | From->args_size()); |
9273 | break; |
9274 | } |
9275 | case attr::AssertSharedLock: { |
9276 | const auto *From = cast<AssertSharedLockAttr>(FromAttr); |
9277 | AI.importAttr(From, |
9278 | AI.importArrayArg(From->args(), From->args_size()).value(), |
9279 | From->args_size()); |
9280 | break; |
9281 | } |
9282 | case attr::ExclusiveTrylockFunction: { |
9283 | const auto *From = cast<ExclusiveTrylockFunctionAttr>(FromAttr); |
9284 | AI.importAttr(From, AI.importArg(From->getSuccessValue()).value(), |
9285 | AI.importArrayArg(From->args(), From->args_size()).value(), |
9286 | From->args_size()); |
9287 | break; |
9288 | } |
9289 | case attr::SharedTrylockFunction: { |
9290 | const auto *From = cast<SharedTrylockFunctionAttr>(FromAttr); |
9291 | AI.importAttr(From, AI.importArg(From->getSuccessValue()).value(), |
9292 | AI.importArrayArg(From->args(), From->args_size()).value(), |
9293 | From->args_size()); |
9294 | break; |
9295 | } |
9296 | case attr::LockReturned: { |
9297 | const auto *From = cast<LockReturnedAttr>(FromAttr); |
9298 | AI.importAttr(From, AI.importArg(From->getArg()).value()); |
9299 | break; |
9300 | } |
9301 | case attr::LocksExcluded: { |
9302 | const auto *From = cast<LocksExcludedAttr>(FromAttr); |
9303 | AI.importAttr(From, |
9304 | AI.importArrayArg(From->args(), From->args_size()).value(), |
9305 | From->args_size()); |
9306 | break; |
9307 | } |
9308 | case attr::CountedBy: { |
9309 | AI.cloneAttr(FromAttr); |
9310 | const auto *CBA = cast<CountedByAttr>(FromAttr); |
9311 | Expected<SourceRange> SR = Import(CBA->getCountedByFieldLoc()).get(); |
9312 | if (!SR) |
9313 | return SR.takeError(); |
9314 | AI.castAttrAs<CountedByAttr>()->setCountedByFieldLoc(SR.get()); |
9315 | break; |
9316 | } |
9317 | |
9318 | default: { |
9319 | // The default branch works for attributes that have no arguments to import. |
9320 | // FIXME: Handle every attribute type that has arguments of type to import |
9321 | // (most often Expr* or Decl* or type) in the switch above. |
9322 | AI.cloneAttr(FromAttr); |
9323 | break; |
9324 | } |
9325 | } |
9326 | |
9327 | return std::move(AI).getResult(); |
9328 | } |
9329 | |
9330 | Decl *ASTImporter::GetAlreadyImportedOrNull(const Decl *FromD) const { |
9331 | return ImportedDecls.lookup(Val: FromD); |
9332 | } |
9333 | |
9334 | TranslationUnitDecl *ASTImporter::GetFromTU(Decl *ToD) { |
9335 | auto FromDPos = ImportedFromDecls.find(Val: ToD); |
9336 | if (FromDPos == ImportedFromDecls.end()) |
9337 | return nullptr; |
9338 | return FromDPos->second->getTranslationUnitDecl(); |
9339 | } |
9340 | |
9341 | Error ASTImporter::ImportAttrs(Decl *ToD, Decl *FromD) { |
9342 | if (!FromD->hasAttrs() || ToD->hasAttrs()) |
9343 | return Error::success(); |
9344 | for (const Attr *FromAttr : FromD->getAttrs()) { |
9345 | auto ToAttrOrErr = Import(FromAttr); |
9346 | if (ToAttrOrErr) |
9347 | ToD->addAttr(A: *ToAttrOrErr); |
9348 | else |
9349 | return ToAttrOrErr.takeError(); |
9350 | } |
9351 | return Error::success(); |
9352 | } |
9353 | |
9354 | Expected<Decl *> ASTImporter::Import(Decl *FromD) { |
9355 | if (!FromD) |
9356 | return nullptr; |
9357 | |
9358 | // Push FromD to the stack, and remove that when we return. |
9359 | ImportPath.push(D: FromD); |
9360 | auto ImportPathBuilder = |
9361 | llvm::make_scope_exit(F: [this]() { ImportPath.pop(); }); |
9362 | |
9363 | // Check whether there was a previous failed import. |
9364 | // If yes return the existing error. |
9365 | if (auto Error = getImportDeclErrorIfAny(FromD)) |
9366 | return make_error<ASTImportError>(Args&: *Error); |
9367 | |
9368 | // Check whether we've already imported this declaration. |
9369 | Decl *ToD = GetAlreadyImportedOrNull(FromD); |
9370 | if (ToD) { |
9371 | // Already imported (possibly from another TU) and with an error. |
9372 | if (auto Error = SharedState->getImportDeclErrorIfAny(ToD)) { |
9373 | setImportDeclError(From: FromD, Error: *Error); |
9374 | return make_error<ASTImportError>(Args&: *Error); |
9375 | } |
9376 | |
9377 | // If FromD has some updated flags after last import, apply it. |
9378 | updateFlags(From: FromD, To: ToD); |
9379 | // If we encounter a cycle during an import then we save the relevant part |
9380 | // of the import path associated to the Decl. |
9381 | if (ImportPath.hasCycleAtBack()) |
9382 | SavedImportPaths[FromD].push_back(Elt: ImportPath.copyCycleAtBack()); |
9383 | return ToD; |
9384 | } |
9385 | |
9386 | // Import the declaration. |
9387 | ExpectedDecl ToDOrErr = ImportImpl(FromD); |
9388 | if (!ToDOrErr) { |
9389 | // Failed to import. |
9390 | |
9391 | auto Pos = ImportedDecls.find(Val: FromD); |
9392 | if (Pos != ImportedDecls.end()) { |
9393 | // Import failed after the object was created. |
9394 | // Remove all references to it. |
9395 | auto *ToD = Pos->second; |
9396 | ImportedDecls.erase(I: Pos); |
9397 | |
9398 | // ImportedDecls and ImportedFromDecls are not symmetric. It may happen |
9399 | // (e.g. with namespaces) that several decls from the 'from' context are |
9400 | // mapped to the same decl in the 'to' context. If we removed entries |
9401 | // from the LookupTable here then we may end up removing them multiple |
9402 | // times. |
9403 | |
9404 | // The Lookuptable contains decls only which are in the 'to' context. |
9405 | // Remove from the Lookuptable only if it is *imported* into the 'to' |
9406 | // context (and do not remove it if it was added during the initial |
9407 | // traverse of the 'to' context). |
9408 | auto PosF = ImportedFromDecls.find(Val: ToD); |
9409 | if (PosF != ImportedFromDecls.end()) { |
9410 | // In the case of TypedefNameDecl we create the Decl first and only |
9411 | // then we import and set its DeclContext. So, the DC might not be set |
9412 | // when we reach here. |
9413 | if (ToD->getDeclContext()) |
9414 | SharedState->removeDeclFromLookup(D: ToD); |
9415 | ImportedFromDecls.erase(I: PosF); |
9416 | } |
9417 | |
9418 | // FIXME: AST may contain remaining references to the failed object. |
9419 | // However, the ImportDeclErrors in the shared state contains all the |
9420 | // failed objects together with their error. |
9421 | } |
9422 | |
9423 | // Error encountered for the first time. |
9424 | // After takeError the error is not usable any more in ToDOrErr. |
9425 | // Get a copy of the error object (any more simple solution for this?). |
9426 | ASTImportError ErrOut; |
9427 | handleAllErrors(E: ToDOrErr.takeError(), |
9428 | Handlers: [&ErrOut](const ASTImportError &E) { ErrOut = E; }); |
9429 | setImportDeclError(From: FromD, Error: ErrOut); |
9430 | // Set the error for the mapped to Decl, which is in the "to" context. |
9431 | if (Pos != ImportedDecls.end()) |
9432 | SharedState->setImportDeclError(To: Pos->second, Error: ErrOut); |
9433 | |
9434 | // Set the error for all nodes which have been created before we |
9435 | // recognized the error. |
9436 | for (const auto &Path : SavedImportPaths[FromD]) { |
9437 | // The import path contains import-dependency nodes first. |
9438 | // Save the node that was imported as dependency of the current node. |
9439 | Decl *PrevFromDi = FromD; |
9440 | for (Decl *FromDi : Path) { |
9441 | // Begin and end of the path equals 'FromD', skip it. |
9442 | if (FromDi == FromD) |
9443 | continue; |
9444 | // We should not set import error on a node and all following nodes in |
9445 | // the path if child import errors are ignored. |
9446 | if (ChildErrorHandlingStrategy(FromDi).ignoreChildErrorOnParent( |
9447 | FromChildD: PrevFromDi)) |
9448 | break; |
9449 | PrevFromDi = FromDi; |
9450 | setImportDeclError(From: FromDi, Error: ErrOut); |
9451 | //FIXME Should we remove these Decls from ImportedDecls? |
9452 | // Set the error for the mapped to Decl, which is in the "to" context. |
9453 | auto Ii = ImportedDecls.find(Val: FromDi); |
9454 | if (Ii != ImportedDecls.end()) |
9455 | SharedState->setImportDeclError(To: Ii->second, Error: ErrOut); |
9456 | // FIXME Should we remove these Decls from the LookupTable, |
9457 | // and from ImportedFromDecls? |
9458 | } |
9459 | } |
9460 | SavedImportPaths.erase(Val: FromD); |
9461 | |
9462 | // Do not return ToDOrErr, error was taken out of it. |
9463 | return make_error<ASTImportError>(Args&: ErrOut); |
9464 | } |
9465 | |
9466 | ToD = *ToDOrErr; |
9467 | |
9468 | // FIXME: Handle the "already imported with error" case. We can get here |
9469 | // nullptr only if GetImportedOrCreateDecl returned nullptr (after a |
9470 | // previously failed create was requested). |
9471 | // Later GetImportedOrCreateDecl can be updated to return the error. |
9472 | if (!ToD) { |
9473 | auto Err = getImportDeclErrorIfAny(FromD); |
9474 | assert(Err); |
9475 | return make_error<ASTImportError>(Args&: *Err); |
9476 | } |
9477 | |
9478 | // We could import from the current TU without error. But previously we |
9479 | // already had imported a Decl as `ToD` from another TU (with another |
9480 | // ASTImporter object) and with an error. |
9481 | if (auto Error = SharedState->getImportDeclErrorIfAny(ToD)) { |
9482 | setImportDeclError(From: FromD, Error: *Error); |
9483 | return make_error<ASTImportError>(Args&: *Error); |
9484 | } |
9485 | // Make sure that ImportImpl registered the imported decl. |
9486 | assert(ImportedDecls.count(FromD) != 0 && "Missing call to MapImported?" ); |
9487 | if (auto Error = ImportAttrs(ToD, FromD)) |
9488 | return std::move(Error); |
9489 | |
9490 | // Notify subclasses. |
9491 | Imported(From: FromD, To: ToD); |
9492 | |
9493 | updateFlags(From: FromD, To: ToD); |
9494 | SavedImportPaths.erase(Val: FromD); |
9495 | return ToDOrErr; |
9496 | } |
9497 | |
9498 | llvm::Expected<InheritedConstructor> |
9499 | ASTImporter::Import(const InheritedConstructor &From) { |
9500 | return ASTNodeImporter(*this).ImportInheritedConstructor(From); |
9501 | } |
9502 | |
9503 | Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) { |
9504 | if (!FromDC) |
9505 | return FromDC; |
9506 | |
9507 | ExpectedDecl ToDCOrErr = Import(FromD: cast<Decl>(Val: FromDC)); |
9508 | if (!ToDCOrErr) |
9509 | return ToDCOrErr.takeError(); |
9510 | auto *ToDC = cast<DeclContext>(Val: *ToDCOrErr); |
9511 | |
9512 | // When we're using a record/enum/Objective-C class/protocol as a context, we |
9513 | // need it to have a definition. |
9514 | if (auto *ToRecord = dyn_cast<RecordDecl>(Val: ToDC)) { |
9515 | auto *FromRecord = cast<RecordDecl>(Val: FromDC); |
9516 | if (ToRecord->isCompleteDefinition()) |
9517 | return ToDC; |
9518 | |
9519 | // If FromRecord is not defined we need to force it to be. |
9520 | // Simply calling CompleteDecl(...) for a RecordDecl will break some cases |
9521 | // it will start the definition but we never finish it. |
9522 | // If there are base classes they won't be imported and we will |
9523 | // be missing anything that we inherit from those bases. |
9524 | if (FromRecord->getASTContext().getExternalSource() && |
9525 | !FromRecord->isCompleteDefinition()) |
9526 | FromRecord->getASTContext().getExternalSource()->CompleteType(FromRecord); |
9527 | |
9528 | if (FromRecord->isCompleteDefinition()) |
9529 | if (Error Err = ASTNodeImporter(*this).ImportDefinition( |
9530 | From: FromRecord, To: ToRecord, Kind: ASTNodeImporter::IDK_Basic)) |
9531 | return std::move(Err); |
9532 | } else if (auto *ToEnum = dyn_cast<EnumDecl>(Val: ToDC)) { |
9533 | auto * = cast<EnumDecl>(Val: FromDC); |
9534 | if (ToEnum->isCompleteDefinition()) { |
9535 | // Do nothing. |
9536 | } else if (FromEnum->isCompleteDefinition()) { |
9537 | if (Error Err = ASTNodeImporter(*this).ImportDefinition( |
9538 | From: FromEnum, To: ToEnum, Kind: ASTNodeImporter::IDK_Basic)) |
9539 | return std::move(Err); |
9540 | } else { |
9541 | CompleteDecl(ToEnum); |
9542 | } |
9543 | } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(Val: ToDC)) { |
9544 | auto *FromClass = cast<ObjCInterfaceDecl>(Val: FromDC); |
9545 | if (ToClass->getDefinition()) { |
9546 | // Do nothing. |
9547 | } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) { |
9548 | if (Error Err = ASTNodeImporter(*this).ImportDefinition( |
9549 | From: FromDef, To: ToClass, Kind: ASTNodeImporter::IDK_Basic)) |
9550 | return std::move(Err); |
9551 | } else { |
9552 | CompleteDecl(ToClass); |
9553 | } |
9554 | } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(Val: ToDC)) { |
9555 | auto *FromProto = cast<ObjCProtocolDecl>(Val: FromDC); |
9556 | if (ToProto->getDefinition()) { |
9557 | // Do nothing. |
9558 | } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) { |
9559 | if (Error Err = ASTNodeImporter(*this).ImportDefinition( |
9560 | From: FromDef, To: ToProto, Kind: ASTNodeImporter::IDK_Basic)) |
9561 | return std::move(Err); |
9562 | } else { |
9563 | CompleteDecl(ToProto); |
9564 | } |
9565 | } |
9566 | |
9567 | return ToDC; |
9568 | } |
9569 | |
9570 | Expected<Expr *> ASTImporter::Import(Expr *FromE) { |
9571 | if (ExpectedStmt ToSOrErr = Import(FromS: cast_or_null<Stmt>(Val: FromE))) |
9572 | return cast_or_null<Expr>(Val: *ToSOrErr); |
9573 | else |
9574 | return ToSOrErr.takeError(); |
9575 | } |
9576 | |
9577 | Expected<Stmt *> ASTImporter::Import(Stmt *FromS) { |
9578 | if (!FromS) |
9579 | return nullptr; |
9580 | |
9581 | // Check whether we've already imported this statement. |
9582 | llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(Val: FromS); |
9583 | if (Pos != ImportedStmts.end()) |
9584 | return Pos->second; |
9585 | |
9586 | // Import the statement. |
9587 | ASTNodeImporter Importer(*this); |
9588 | ExpectedStmt ToSOrErr = Importer.Visit(FromS); |
9589 | if (!ToSOrErr) |
9590 | return ToSOrErr; |
9591 | |
9592 | if (auto *ToE = dyn_cast<Expr>(*ToSOrErr)) { |
9593 | auto *FromE = cast<Expr>(Val: FromS); |
9594 | // Copy ExprBitfields, which may not be handled in Expr subclasses |
9595 | // constructors. |
9596 | ToE->setValueKind(FromE->getValueKind()); |
9597 | ToE->setObjectKind(FromE->getObjectKind()); |
9598 | ToE->setDependence(FromE->getDependence()); |
9599 | } |
9600 | |
9601 | // Record the imported statement object. |
9602 | ImportedStmts[FromS] = *ToSOrErr; |
9603 | return ToSOrErr; |
9604 | } |
9605 | |
9606 | Expected<NestedNameSpecifier *> |
9607 | ASTImporter::Import(NestedNameSpecifier *FromNNS) { |
9608 | if (!FromNNS) |
9609 | return nullptr; |
9610 | |
9611 | NestedNameSpecifier *Prefix = nullptr; |
9612 | if (Error Err = importInto(To&: Prefix, From: FromNNS->getPrefix())) |
9613 | return std::move(Err); |
9614 | |
9615 | switch (FromNNS->getKind()) { |
9616 | case NestedNameSpecifier::Identifier: |
9617 | assert(FromNNS->getAsIdentifier() && "NNS should contain identifier." ); |
9618 | return NestedNameSpecifier::Create(Context: ToContext, Prefix, |
9619 | II: Import(FromId: FromNNS->getAsIdentifier())); |
9620 | |
9621 | case NestedNameSpecifier::Namespace: |
9622 | if (ExpectedDecl NSOrErr = Import(FromNNS->getAsNamespace())) { |
9623 | return NestedNameSpecifier::Create(Context: ToContext, Prefix, |
9624 | NS: cast<NamespaceDecl>(Val: *NSOrErr)); |
9625 | } else |
9626 | return NSOrErr.takeError(); |
9627 | |
9628 | case NestedNameSpecifier::NamespaceAlias: |
9629 | if (ExpectedDecl NSADOrErr = Import(FromNNS->getAsNamespaceAlias())) |
9630 | return NestedNameSpecifier::Create(Context: ToContext, Prefix, |
9631 | Alias: cast<NamespaceAliasDecl>(Val: *NSADOrErr)); |
9632 | else |
9633 | return NSADOrErr.takeError(); |
9634 | |
9635 | case NestedNameSpecifier::Global: |
9636 | return NestedNameSpecifier::GlobalSpecifier(Context: ToContext); |
9637 | |
9638 | case NestedNameSpecifier::Super: |
9639 | if (ExpectedDecl RDOrErr = Import(FromNNS->getAsRecordDecl())) |
9640 | return NestedNameSpecifier::SuperSpecifier(Context: ToContext, |
9641 | RD: cast<CXXRecordDecl>(Val: *RDOrErr)); |
9642 | else |
9643 | return RDOrErr.takeError(); |
9644 | |
9645 | case NestedNameSpecifier::TypeSpec: |
9646 | case NestedNameSpecifier::TypeSpecWithTemplate: |
9647 | if (ExpectedTypePtr TyOrErr = Import(FromT: FromNNS->getAsType())) { |
9648 | bool TSTemplate = |
9649 | FromNNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate; |
9650 | return NestedNameSpecifier::Create(Context: ToContext, Prefix, Template: TSTemplate, |
9651 | T: *TyOrErr); |
9652 | } else { |
9653 | return TyOrErr.takeError(); |
9654 | } |
9655 | } |
9656 | |
9657 | llvm_unreachable("Invalid nested name specifier kind" ); |
9658 | } |
9659 | |
9660 | Expected<NestedNameSpecifierLoc> |
9661 | ASTImporter::Import(NestedNameSpecifierLoc FromNNS) { |
9662 | // Copied from NestedNameSpecifier mostly. |
9663 | SmallVector<NestedNameSpecifierLoc , 8> NestedNames; |
9664 | NestedNameSpecifierLoc NNS = FromNNS; |
9665 | |
9666 | // Push each of the nested-name-specifiers's onto a stack for |
9667 | // serialization in reverse order. |
9668 | while (NNS) { |
9669 | NestedNames.push_back(Elt: NNS); |
9670 | NNS = NNS.getPrefix(); |
9671 | } |
9672 | |
9673 | NestedNameSpecifierLocBuilder Builder; |
9674 | |
9675 | while (!NestedNames.empty()) { |
9676 | NNS = NestedNames.pop_back_val(); |
9677 | NestedNameSpecifier *Spec = nullptr; |
9678 | if (Error Err = importInto(To&: Spec, From: NNS.getNestedNameSpecifier())) |
9679 | return std::move(Err); |
9680 | |
9681 | NestedNameSpecifier::SpecifierKind Kind = Spec->getKind(); |
9682 | |
9683 | SourceLocation ToLocalBeginLoc, ToLocalEndLoc; |
9684 | if (Kind != NestedNameSpecifier::Super) { |
9685 | if (Error Err = importInto(To&: ToLocalBeginLoc, From: NNS.getLocalBeginLoc())) |
9686 | return std::move(Err); |
9687 | |
9688 | if (Kind != NestedNameSpecifier::Global) |
9689 | if (Error Err = importInto(To&: ToLocalEndLoc, From: NNS.getLocalEndLoc())) |
9690 | return std::move(Err); |
9691 | } |
9692 | |
9693 | switch (Kind) { |
9694 | case NestedNameSpecifier::Identifier: |
9695 | Builder.Extend(Context&: getToContext(), Identifier: Spec->getAsIdentifier(), IdentifierLoc: ToLocalBeginLoc, |
9696 | ColonColonLoc: ToLocalEndLoc); |
9697 | break; |
9698 | |
9699 | case NestedNameSpecifier::Namespace: |
9700 | Builder.Extend(Context&: getToContext(), Namespace: Spec->getAsNamespace(), NamespaceLoc: ToLocalBeginLoc, |
9701 | ColonColonLoc: ToLocalEndLoc); |
9702 | break; |
9703 | |
9704 | case NestedNameSpecifier::NamespaceAlias: |
9705 | Builder.Extend(Context&: getToContext(), Alias: Spec->getAsNamespaceAlias(), |
9706 | AliasLoc: ToLocalBeginLoc, ColonColonLoc: ToLocalEndLoc); |
9707 | break; |
9708 | |
9709 | case NestedNameSpecifier::TypeSpec: |
9710 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
9711 | SourceLocation ToTLoc; |
9712 | if (Error Err = importInto(To&: ToTLoc, From: NNS.getTypeLoc().getBeginLoc())) |
9713 | return std::move(Err); |
9714 | TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo( |
9715 | T: QualType(Spec->getAsType(), 0), Loc: ToTLoc); |
9716 | if (Kind == NestedNameSpecifier::TypeSpecWithTemplate) |
9717 | // ToLocalBeginLoc is here the location of the 'template' keyword. |
9718 | Builder.Extend(Context&: getToContext(), TemplateKWLoc: ToLocalBeginLoc, TL: TSI->getTypeLoc(), |
9719 | ColonColonLoc: ToLocalEndLoc); |
9720 | else |
9721 | // No location for 'template' keyword here. |
9722 | Builder.Extend(Context&: getToContext(), TemplateKWLoc: SourceLocation{}, TL: TSI->getTypeLoc(), |
9723 | ColonColonLoc: ToLocalEndLoc); |
9724 | break; |
9725 | } |
9726 | |
9727 | case NestedNameSpecifier::Global: |
9728 | Builder.MakeGlobal(Context&: getToContext(), ColonColonLoc: ToLocalBeginLoc); |
9729 | break; |
9730 | |
9731 | case NestedNameSpecifier::Super: { |
9732 | auto ToSourceRangeOrErr = Import(FromRange: NNS.getSourceRange()); |
9733 | if (!ToSourceRangeOrErr) |
9734 | return ToSourceRangeOrErr.takeError(); |
9735 | |
9736 | Builder.MakeSuper(Context&: getToContext(), RD: Spec->getAsRecordDecl(), |
9737 | SuperLoc: ToSourceRangeOrErr->getBegin(), |
9738 | ColonColonLoc: ToSourceRangeOrErr->getEnd()); |
9739 | } |
9740 | } |
9741 | } |
9742 | |
9743 | return Builder.getWithLocInContext(Context&: getToContext()); |
9744 | } |
9745 | |
9746 | Expected<TemplateName> ASTImporter::Import(TemplateName From) { |
9747 | switch (From.getKind()) { |
9748 | case TemplateName::Template: |
9749 | if (ExpectedDecl ToTemplateOrErr = Import(From.getAsTemplateDecl())) |
9750 | return TemplateName(cast<TemplateDecl>(Val: (*ToTemplateOrErr)->getCanonicalDecl())); |
9751 | else |
9752 | return ToTemplateOrErr.takeError(); |
9753 | |
9754 | case TemplateName::OverloadedTemplate: { |
9755 | OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate(); |
9756 | UnresolvedSet<2> ToTemplates; |
9757 | for (auto *I : *FromStorage) { |
9758 | if (auto ToOrErr = Import(I)) |
9759 | ToTemplates.addDecl(cast<NamedDecl>(*ToOrErr)); |
9760 | else |
9761 | return ToOrErr.takeError(); |
9762 | } |
9763 | return ToContext.getOverloadedTemplateName(Begin: ToTemplates.begin(), |
9764 | End: ToTemplates.end()); |
9765 | } |
9766 | |
9767 | case TemplateName::AssumedTemplate: { |
9768 | AssumedTemplateStorage *FromStorage = From.getAsAssumedTemplateName(); |
9769 | auto DeclNameOrErr = Import(FromName: FromStorage->getDeclName()); |
9770 | if (!DeclNameOrErr) |
9771 | return DeclNameOrErr.takeError(); |
9772 | return ToContext.getAssumedTemplateName(Name: *DeclNameOrErr); |
9773 | } |
9774 | |
9775 | case TemplateName::QualifiedTemplate: { |
9776 | QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName(); |
9777 | auto QualifierOrErr = Import(FromNNS: QTN->getQualifier()); |
9778 | if (!QualifierOrErr) |
9779 | return QualifierOrErr.takeError(); |
9780 | auto TNOrErr = Import(From: QTN->getUnderlyingTemplate()); |
9781 | if (!TNOrErr) |
9782 | return TNOrErr.takeError(); |
9783 | return ToContext.getQualifiedTemplateName( |
9784 | NNS: *QualifierOrErr, TemplateKeyword: QTN->hasTemplateKeyword(), Template: *TNOrErr); |
9785 | } |
9786 | |
9787 | case TemplateName::DependentTemplate: { |
9788 | DependentTemplateName *DTN = From.getAsDependentTemplateName(); |
9789 | auto QualifierOrErr = Import(FromNNS: DTN->getQualifier()); |
9790 | if (!QualifierOrErr) |
9791 | return QualifierOrErr.takeError(); |
9792 | |
9793 | if (DTN->isIdentifier()) { |
9794 | return ToContext.getDependentTemplateName(NNS: *QualifierOrErr, |
9795 | Name: Import(FromId: DTN->getIdentifier())); |
9796 | } |
9797 | |
9798 | return ToContext.getDependentTemplateName(NNS: *QualifierOrErr, |
9799 | Operator: DTN->getOperator()); |
9800 | } |
9801 | |
9802 | case TemplateName::SubstTemplateTemplateParm: { |
9803 | SubstTemplateTemplateParmStorage *Subst = |
9804 | From.getAsSubstTemplateTemplateParm(); |
9805 | auto ReplacementOrErr = Import(From: Subst->getReplacement()); |
9806 | if (!ReplacementOrErr) |
9807 | return ReplacementOrErr.takeError(); |
9808 | |
9809 | auto AssociatedDeclOrErr = Import(FromD: Subst->getAssociatedDecl()); |
9810 | if (!AssociatedDeclOrErr) |
9811 | return AssociatedDeclOrErr.takeError(); |
9812 | |
9813 | return ToContext.getSubstTemplateTemplateParm( |
9814 | replacement: *ReplacementOrErr, AssociatedDecl: *AssociatedDeclOrErr, Index: Subst->getIndex(), |
9815 | PackIndex: Subst->getPackIndex()); |
9816 | } |
9817 | |
9818 | case TemplateName::SubstTemplateTemplateParmPack: { |
9819 | SubstTemplateTemplateParmPackStorage *SubstPack = |
9820 | From.getAsSubstTemplateTemplateParmPack(); |
9821 | ASTNodeImporter Importer(*this); |
9822 | auto ArgPackOrErr = |
9823 | Importer.ImportTemplateArgument(From: SubstPack->getArgumentPack()); |
9824 | if (!ArgPackOrErr) |
9825 | return ArgPackOrErr.takeError(); |
9826 | |
9827 | auto AssociatedDeclOrErr = Import(FromD: SubstPack->getAssociatedDecl()); |
9828 | if (!AssociatedDeclOrErr) |
9829 | return AssociatedDeclOrErr.takeError(); |
9830 | |
9831 | return ToContext.getSubstTemplateTemplateParmPack( |
9832 | ArgPack: *ArgPackOrErr, AssociatedDecl: *AssociatedDeclOrErr, Index: SubstPack->getIndex(), |
9833 | Final: SubstPack->getFinal()); |
9834 | } |
9835 | case TemplateName::UsingTemplate: { |
9836 | auto UsingOrError = Import(From.getAsUsingShadowDecl()); |
9837 | if (!UsingOrError) |
9838 | return UsingOrError.takeError(); |
9839 | return TemplateName(cast<UsingShadowDecl>(*UsingOrError)); |
9840 | } |
9841 | } |
9842 | |
9843 | llvm_unreachable("Invalid template name kind" ); |
9844 | } |
9845 | |
9846 | Expected<SourceLocation> ASTImporter::Import(SourceLocation FromLoc) { |
9847 | if (FromLoc.isInvalid()) |
9848 | return SourceLocation{}; |
9849 | |
9850 | SourceManager &FromSM = FromContext.getSourceManager(); |
9851 | bool IsBuiltin = FromSM.isWrittenInBuiltinFile(Loc: FromLoc); |
9852 | |
9853 | std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(Loc: FromLoc); |
9854 | Expected<FileID> ToFileIDOrErr = Import(Decomposed.first, IsBuiltin); |
9855 | if (!ToFileIDOrErr) |
9856 | return ToFileIDOrErr.takeError(); |
9857 | SourceManager &ToSM = ToContext.getSourceManager(); |
9858 | return ToSM.getComposedLoc(FID: *ToFileIDOrErr, Offset: Decomposed.second); |
9859 | } |
9860 | |
9861 | Expected<SourceRange> ASTImporter::Import(SourceRange FromRange) { |
9862 | SourceLocation ToBegin, ToEnd; |
9863 | if (Error Err = importInto(To&: ToBegin, From: FromRange.getBegin())) |
9864 | return std::move(Err); |
9865 | if (Error Err = importInto(To&: ToEnd, From: FromRange.getEnd())) |
9866 | return std::move(Err); |
9867 | |
9868 | return SourceRange(ToBegin, ToEnd); |
9869 | } |
9870 | |
9871 | Expected<FileID> ASTImporter::Import(FileID FromID, bool IsBuiltin) { |
9872 | llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(Val: FromID); |
9873 | if (Pos != ImportedFileIDs.end()) |
9874 | return Pos->second; |
9875 | |
9876 | SourceManager &FromSM = FromContext.getSourceManager(); |
9877 | SourceManager &ToSM = ToContext.getSourceManager(); |
9878 | const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FID: FromID); |
9879 | |
9880 | // Map the FromID to the "to" source manager. |
9881 | FileID ToID; |
9882 | if (FromSLoc.isExpansion()) { |
9883 | const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion(); |
9884 | ExpectedSLoc ToSpLoc = Import(FromLoc: FromEx.getSpellingLoc()); |
9885 | if (!ToSpLoc) |
9886 | return ToSpLoc.takeError(); |
9887 | ExpectedSLoc ToExLocS = Import(FromLoc: FromEx.getExpansionLocStart()); |
9888 | if (!ToExLocS) |
9889 | return ToExLocS.takeError(); |
9890 | unsigned ExLength = FromSM.getFileIDSize(FID: FromID); |
9891 | SourceLocation MLoc; |
9892 | if (FromEx.isMacroArgExpansion()) { |
9893 | MLoc = ToSM.createMacroArgExpansionLoc(SpellingLoc: *ToSpLoc, ExpansionLoc: *ToExLocS, Length: ExLength); |
9894 | } else { |
9895 | if (ExpectedSLoc ToExLocE = Import(FromLoc: FromEx.getExpansionLocEnd())) |
9896 | MLoc = ToSM.createExpansionLoc(SpellingLoc: *ToSpLoc, ExpansionLocStart: *ToExLocS, ExpansionLocEnd: *ToExLocE, Length: ExLength, |
9897 | ExpansionIsTokenRange: FromEx.isExpansionTokenRange()); |
9898 | else |
9899 | return ToExLocE.takeError(); |
9900 | } |
9901 | ToID = ToSM.getFileID(SpellingLoc: MLoc); |
9902 | } else { |
9903 | const SrcMgr::ContentCache *Cache = &FromSLoc.getFile().getContentCache(); |
9904 | |
9905 | if (!IsBuiltin && !Cache->BufferOverridden) { |
9906 | // Include location of this file. |
9907 | ExpectedSLoc ToIncludeLoc = Import(FromLoc: FromSLoc.getFile().getIncludeLoc()); |
9908 | if (!ToIncludeLoc) |
9909 | return ToIncludeLoc.takeError(); |
9910 | |
9911 | // Every FileID that is not the main FileID needs to have a valid include |
9912 | // location so that the include chain points to the main FileID. When |
9913 | // importing the main FileID (which has no include location), we need to |
9914 | // create a fake include location in the main file to keep this property |
9915 | // intact. |
9916 | SourceLocation ToIncludeLocOrFakeLoc = *ToIncludeLoc; |
9917 | if (FromID == FromSM.getMainFileID()) |
9918 | ToIncludeLocOrFakeLoc = ToSM.getLocForStartOfFile(FID: ToSM.getMainFileID()); |
9919 | |
9920 | if (Cache->OrigEntry && Cache->OrigEntry->getDir()) { |
9921 | // FIXME: We probably want to use getVirtualFile(), so we don't hit the |
9922 | // disk again |
9923 | // FIXME: We definitely want to re-use the existing MemoryBuffer, rather |
9924 | // than mmap the files several times. |
9925 | auto Entry = |
9926 | ToFileManager.getOptionalFileRef(Filename: Cache->OrigEntry->getName()); |
9927 | // FIXME: The filename may be a virtual name that does probably not |
9928 | // point to a valid file and we get no Entry here. In this case try with |
9929 | // the memory buffer below. |
9930 | if (Entry) |
9931 | ToID = ToSM.createFileID(SourceFile: *Entry, IncludePos: ToIncludeLocOrFakeLoc, |
9932 | FileCharacter: FromSLoc.getFile().getFileCharacteristic()); |
9933 | } |
9934 | } |
9935 | |
9936 | if (ToID.isInvalid() || IsBuiltin) { |
9937 | // FIXME: We want to re-use the existing MemoryBuffer! |
9938 | std::optional<llvm::MemoryBufferRef> FromBuf = |
9939 | Cache->getBufferOrNone(Diag&: FromContext.getDiagnostics(), |
9940 | FM&: FromSM.getFileManager(), Loc: SourceLocation{}); |
9941 | if (!FromBuf) |
9942 | return llvm::make_error<ASTImportError>(Args: ASTImportError::Unknown); |
9943 | |
9944 | std::unique_ptr<llvm::MemoryBuffer> ToBuf = |
9945 | llvm::MemoryBuffer::getMemBufferCopy(InputData: FromBuf->getBuffer(), |
9946 | BufferName: FromBuf->getBufferIdentifier()); |
9947 | ToID = ToSM.createFileID(Buffer: std::move(ToBuf), |
9948 | FileCharacter: FromSLoc.getFile().getFileCharacteristic()); |
9949 | } |
9950 | } |
9951 | |
9952 | assert(ToID.isValid() && "Unexpected invalid fileID was created." ); |
9953 | |
9954 | ImportedFileIDs[FromID] = ToID; |
9955 | return ToID; |
9956 | } |
9957 | |
9958 | Expected<CXXCtorInitializer *> ASTImporter::Import(CXXCtorInitializer *From) { |
9959 | ExpectedExpr ToExprOrErr = Import(FromE: From->getInit()); |
9960 | if (!ToExprOrErr) |
9961 | return ToExprOrErr.takeError(); |
9962 | |
9963 | auto LParenLocOrErr = Import(FromLoc: From->getLParenLoc()); |
9964 | if (!LParenLocOrErr) |
9965 | return LParenLocOrErr.takeError(); |
9966 | |
9967 | auto RParenLocOrErr = Import(FromLoc: From->getRParenLoc()); |
9968 | if (!RParenLocOrErr) |
9969 | return RParenLocOrErr.takeError(); |
9970 | |
9971 | if (From->isBaseInitializer()) { |
9972 | auto ToTInfoOrErr = Import(FromTSI: From->getTypeSourceInfo()); |
9973 | if (!ToTInfoOrErr) |
9974 | return ToTInfoOrErr.takeError(); |
9975 | |
9976 | SourceLocation EllipsisLoc; |
9977 | if (From->isPackExpansion()) |
9978 | if (Error Err = importInto(To&: EllipsisLoc, From: From->getEllipsisLoc())) |
9979 | return std::move(Err); |
9980 | |
9981 | return new (ToContext) CXXCtorInitializer( |
9982 | ToContext, *ToTInfoOrErr, From->isBaseVirtual(), *LParenLocOrErr, |
9983 | *ToExprOrErr, *RParenLocOrErr, EllipsisLoc); |
9984 | } else if (From->isMemberInitializer()) { |
9985 | ExpectedDecl ToFieldOrErr = Import(From->getMember()); |
9986 | if (!ToFieldOrErr) |
9987 | return ToFieldOrErr.takeError(); |
9988 | |
9989 | auto MemberLocOrErr = Import(FromLoc: From->getMemberLocation()); |
9990 | if (!MemberLocOrErr) |
9991 | return MemberLocOrErr.takeError(); |
9992 | |
9993 | return new (ToContext) CXXCtorInitializer( |
9994 | ToContext, cast_or_null<FieldDecl>(Val: *ToFieldOrErr), *MemberLocOrErr, |
9995 | *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr); |
9996 | } else if (From->isIndirectMemberInitializer()) { |
9997 | ExpectedDecl ToIFieldOrErr = Import(From->getIndirectMember()); |
9998 | if (!ToIFieldOrErr) |
9999 | return ToIFieldOrErr.takeError(); |
10000 | |
10001 | auto MemberLocOrErr = Import(FromLoc: From->getMemberLocation()); |
10002 | if (!MemberLocOrErr) |
10003 | return MemberLocOrErr.takeError(); |
10004 | |
10005 | return new (ToContext) CXXCtorInitializer( |
10006 | ToContext, cast_or_null<IndirectFieldDecl>(Val: *ToIFieldOrErr), |
10007 | *MemberLocOrErr, *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr); |
10008 | } else if (From->isDelegatingInitializer()) { |
10009 | auto ToTInfoOrErr = Import(FromTSI: From->getTypeSourceInfo()); |
10010 | if (!ToTInfoOrErr) |
10011 | return ToTInfoOrErr.takeError(); |
10012 | |
10013 | return new (ToContext) |
10014 | CXXCtorInitializer(ToContext, *ToTInfoOrErr, *LParenLocOrErr, |
10015 | *ToExprOrErr, *RParenLocOrErr); |
10016 | } else { |
10017 | // FIXME: assert? |
10018 | return make_error<ASTImportError>(); |
10019 | } |
10020 | } |
10021 | |
10022 | Expected<CXXBaseSpecifier *> |
10023 | ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) { |
10024 | auto Pos = ImportedCXXBaseSpecifiers.find(Val: BaseSpec); |
10025 | if (Pos != ImportedCXXBaseSpecifiers.end()) |
10026 | return Pos->second; |
10027 | |
10028 | Expected<SourceRange> ToSourceRange = Import(FromRange: BaseSpec->getSourceRange()); |
10029 | if (!ToSourceRange) |
10030 | return ToSourceRange.takeError(); |
10031 | Expected<TypeSourceInfo *> ToTSI = Import(FromTSI: BaseSpec->getTypeSourceInfo()); |
10032 | if (!ToTSI) |
10033 | return ToTSI.takeError(); |
10034 | ExpectedSLoc ToEllipsisLoc = Import(FromLoc: BaseSpec->getEllipsisLoc()); |
10035 | if (!ToEllipsisLoc) |
10036 | return ToEllipsisLoc.takeError(); |
10037 | CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier( |
10038 | *ToSourceRange, BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(), |
10039 | BaseSpec->getAccessSpecifierAsWritten(), *ToTSI, *ToEllipsisLoc); |
10040 | ImportedCXXBaseSpecifiers[BaseSpec] = Imported; |
10041 | return Imported; |
10042 | } |
10043 | |
10044 | llvm::Expected<APValue> ASTImporter::Import(const APValue &FromValue) { |
10045 | ASTNodeImporter Importer(*this); |
10046 | return Importer.ImportAPValue(FromValue); |
10047 | } |
10048 | |
10049 | Error ASTImporter::ImportDefinition(Decl *From) { |
10050 | ExpectedDecl ToOrErr = Import(FromD: From); |
10051 | if (!ToOrErr) |
10052 | return ToOrErr.takeError(); |
10053 | Decl *To = *ToOrErr; |
10054 | |
10055 | auto *FromDC = cast<DeclContext>(Val: From); |
10056 | ASTNodeImporter Importer(*this); |
10057 | |
10058 | if (auto *ToRecord = dyn_cast<RecordDecl>(Val: To)) { |
10059 | if (!ToRecord->getDefinition()) { |
10060 | return Importer.ImportDefinition( |
10061 | From: cast<RecordDecl>(Val: FromDC), To: ToRecord, |
10062 | Kind: ASTNodeImporter::IDK_Everything); |
10063 | } |
10064 | } |
10065 | |
10066 | if (auto *ToEnum = dyn_cast<EnumDecl>(Val: To)) { |
10067 | if (!ToEnum->getDefinition()) { |
10068 | return Importer.ImportDefinition( |
10069 | From: cast<EnumDecl>(Val: FromDC), To: ToEnum, Kind: ASTNodeImporter::IDK_Everything); |
10070 | } |
10071 | } |
10072 | |
10073 | if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(Val: To)) { |
10074 | if (!ToIFace->getDefinition()) { |
10075 | return Importer.ImportDefinition( |
10076 | From: cast<ObjCInterfaceDecl>(Val: FromDC), To: ToIFace, |
10077 | Kind: ASTNodeImporter::IDK_Everything); |
10078 | } |
10079 | } |
10080 | |
10081 | if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(Val: To)) { |
10082 | if (!ToProto->getDefinition()) { |
10083 | return Importer.ImportDefinition( |
10084 | From: cast<ObjCProtocolDecl>(Val: FromDC), To: ToProto, |
10085 | Kind: ASTNodeImporter::IDK_Everything); |
10086 | } |
10087 | } |
10088 | |
10089 | return Importer.ImportDeclContext(FromDC, ForceImport: true); |
10090 | } |
10091 | |
10092 | Expected<DeclarationName> ASTImporter::Import(DeclarationName FromName) { |
10093 | if (!FromName) |
10094 | return DeclarationName{}; |
10095 | |
10096 | switch (FromName.getNameKind()) { |
10097 | case DeclarationName::Identifier: |
10098 | return DeclarationName(Import(FromId: FromName.getAsIdentifierInfo())); |
10099 | |
10100 | case DeclarationName::ObjCZeroArgSelector: |
10101 | case DeclarationName::ObjCOneArgSelector: |
10102 | case DeclarationName::ObjCMultiArgSelector: |
10103 | if (auto ToSelOrErr = Import(FromSel: FromName.getObjCSelector())) |
10104 | return DeclarationName(*ToSelOrErr); |
10105 | else |
10106 | return ToSelOrErr.takeError(); |
10107 | |
10108 | case DeclarationName::CXXConstructorName: { |
10109 | if (auto ToTyOrErr = Import(FromT: FromName.getCXXNameType())) |
10110 | return ToContext.DeclarationNames.getCXXConstructorName( |
10111 | Ty: ToContext.getCanonicalType(T: *ToTyOrErr)); |
10112 | else |
10113 | return ToTyOrErr.takeError(); |
10114 | } |
10115 | |
10116 | case DeclarationName::CXXDestructorName: { |
10117 | if (auto ToTyOrErr = Import(FromT: FromName.getCXXNameType())) |
10118 | return ToContext.DeclarationNames.getCXXDestructorName( |
10119 | Ty: ToContext.getCanonicalType(T: *ToTyOrErr)); |
10120 | else |
10121 | return ToTyOrErr.takeError(); |
10122 | } |
10123 | |
10124 | case DeclarationName::CXXDeductionGuideName: { |
10125 | if (auto ToTemplateOrErr = Import(FromName.getCXXDeductionGuideTemplate())) |
10126 | return ToContext.DeclarationNames.getCXXDeductionGuideName( |
10127 | TD: cast<TemplateDecl>(*ToTemplateOrErr)); |
10128 | else |
10129 | return ToTemplateOrErr.takeError(); |
10130 | } |
10131 | |
10132 | case DeclarationName::CXXConversionFunctionName: { |
10133 | if (auto ToTyOrErr = Import(FromT: FromName.getCXXNameType())) |
10134 | return ToContext.DeclarationNames.getCXXConversionFunctionName( |
10135 | Ty: ToContext.getCanonicalType(T: *ToTyOrErr)); |
10136 | else |
10137 | return ToTyOrErr.takeError(); |
10138 | } |
10139 | |
10140 | case DeclarationName::CXXOperatorName: |
10141 | return ToContext.DeclarationNames.getCXXOperatorName( |
10142 | Op: FromName.getCXXOverloadedOperator()); |
10143 | |
10144 | case DeclarationName::CXXLiteralOperatorName: |
10145 | return ToContext.DeclarationNames.getCXXLiteralOperatorName( |
10146 | II: Import(FromId: FromName.getCXXLiteralIdentifier())); |
10147 | |
10148 | case DeclarationName::CXXUsingDirective: |
10149 | // FIXME: STATICS! |
10150 | return DeclarationName::getUsingDirectiveName(); |
10151 | } |
10152 | |
10153 | llvm_unreachable("Invalid DeclarationName Kind!" ); |
10154 | } |
10155 | |
10156 | IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) { |
10157 | if (!FromId) |
10158 | return nullptr; |
10159 | |
10160 | IdentifierInfo *ToId = &ToContext.Idents.get(Name: FromId->getName()); |
10161 | |
10162 | if (!ToId->getBuiltinID() && FromId->getBuiltinID()) |
10163 | ToId->setBuiltinID(FromId->getBuiltinID()); |
10164 | |
10165 | return ToId; |
10166 | } |
10167 | |
10168 | Expected<Selector> ASTImporter::Import(Selector FromSel) { |
10169 | if (FromSel.isNull()) |
10170 | return Selector{}; |
10171 | |
10172 | SmallVector<IdentifierInfo *, 4> Idents; |
10173 | Idents.push_back(Elt: Import(FromId: FromSel.getIdentifierInfoForSlot(argIndex: 0))); |
10174 | for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I) |
10175 | Idents.push_back(Elt: Import(FromId: FromSel.getIdentifierInfoForSlot(argIndex: I))); |
10176 | return ToContext.Selectors.getSelector(NumArgs: FromSel.getNumArgs(), IIV: Idents.data()); |
10177 | } |
10178 | |
10179 | llvm::Expected<APValue> |
10180 | ASTNodeImporter::ImportAPValue(const APValue &FromValue) { |
10181 | APValue Result; |
10182 | llvm::Error Err = llvm::Error::success(); |
10183 | auto ImportLoop = [&](const APValue *From, APValue *To, unsigned Size) { |
10184 | for (unsigned Idx = 0; Idx < Size; Idx++) { |
10185 | APValue Tmp = importChecked(Err, From: From[Idx]); |
10186 | To[Idx] = Tmp; |
10187 | } |
10188 | }; |
10189 | switch (FromValue.getKind()) { |
10190 | case APValue::None: |
10191 | case APValue::Indeterminate: |
10192 | case APValue::Int: |
10193 | case APValue::Float: |
10194 | case APValue::FixedPoint: |
10195 | case APValue::ComplexInt: |
10196 | case APValue::ComplexFloat: |
10197 | Result = FromValue; |
10198 | break; |
10199 | case APValue::Vector: { |
10200 | Result.MakeVector(); |
10201 | MutableArrayRef<APValue> Elts = |
10202 | Result.setVectorUninit(FromValue.getVectorLength()); |
10203 | ImportLoop(((const APValue::Vec *)(const char *)&FromValue.Data)->Elts, |
10204 | Elts.data(), FromValue.getVectorLength()); |
10205 | break; |
10206 | } |
10207 | case APValue::Array: |
10208 | Result.MakeArray(InitElts: FromValue.getArrayInitializedElts(), |
10209 | Size: FromValue.getArraySize()); |
10210 | ImportLoop(((const APValue::Arr *)(const char *)&FromValue.Data)->Elts, |
10211 | ((const APValue::Arr *)(const char *)&Result.Data)->Elts, |
10212 | FromValue.getArrayInitializedElts()); |
10213 | break; |
10214 | case APValue::Struct: |
10215 | Result.MakeStruct(B: FromValue.getStructNumBases(), |
10216 | M: FromValue.getStructNumFields()); |
10217 | ImportLoop( |
10218 | ((const APValue::StructData *)(const char *)&FromValue.Data)->Elts, |
10219 | ((const APValue::StructData *)(const char *)&Result.Data)->Elts, |
10220 | FromValue.getStructNumBases() + FromValue.getStructNumFields()); |
10221 | break; |
10222 | case APValue::Union: { |
10223 | Result.MakeUnion(); |
10224 | const Decl *ImpFDecl = importChecked(Err, From: FromValue.getUnionField()); |
10225 | APValue ImpValue = importChecked(Err, From: FromValue.getUnionValue()); |
10226 | if (Err) |
10227 | return std::move(Err); |
10228 | Result.setUnion(Field: cast<FieldDecl>(Val: ImpFDecl), Value: ImpValue); |
10229 | break; |
10230 | } |
10231 | case APValue::AddrLabelDiff: { |
10232 | Result.MakeAddrLabelDiff(); |
10233 | const Expr *ImpLHS = importChecked(Err, From: FromValue.getAddrLabelDiffLHS()); |
10234 | const Expr *ImpRHS = importChecked(Err, From: FromValue.getAddrLabelDiffRHS()); |
10235 | if (Err) |
10236 | return std::move(Err); |
10237 | Result.setAddrLabelDiff(LHSExpr: cast<AddrLabelExpr>(Val: ImpLHS), |
10238 | RHSExpr: cast<AddrLabelExpr>(Val: ImpRHS)); |
10239 | break; |
10240 | } |
10241 | case APValue::MemberPointer: { |
10242 | const Decl *ImpMemPtrDecl = |
10243 | importChecked(Err, From: FromValue.getMemberPointerDecl()); |
10244 | if (Err) |
10245 | return std::move(Err); |
10246 | MutableArrayRef<const CXXRecordDecl *> ToPath = |
10247 | Result.setMemberPointerUninit( |
10248 | Member: cast<const ValueDecl>(Val: ImpMemPtrDecl), |
10249 | IsDerivedMember: FromValue.isMemberPointerToDerivedMember(), |
10250 | Size: FromValue.getMemberPointerPath().size()); |
10251 | llvm::ArrayRef<const CXXRecordDecl *> FromPath = |
10252 | Result.getMemberPointerPath(); |
10253 | for (unsigned Idx = 0; Idx < FromValue.getMemberPointerPath().size(); |
10254 | Idx++) { |
10255 | const Decl *ImpDecl = importChecked(Err, From: FromPath[Idx]); |
10256 | if (Err) |
10257 | return std::move(Err); |
10258 | ToPath[Idx] = cast<const CXXRecordDecl>(Val: ImpDecl->getCanonicalDecl()); |
10259 | } |
10260 | break; |
10261 | } |
10262 | case APValue::LValue: |
10263 | APValue::LValueBase Base; |
10264 | QualType FromElemTy; |
10265 | if (FromValue.getLValueBase()) { |
10266 | assert(!FromValue.getLValueBase().is<DynamicAllocLValue>() && |
10267 | "in C++20 dynamic allocation are transient so they shouldn't " |
10268 | "appear in the AST" ); |
10269 | if (!FromValue.getLValueBase().is<TypeInfoLValue>()) { |
10270 | if (const auto *E = |
10271 | FromValue.getLValueBase().dyn_cast<const Expr *>()) { |
10272 | FromElemTy = E->getType(); |
10273 | const Expr *ImpExpr = importChecked(Err, From: E); |
10274 | if (Err) |
10275 | return std::move(Err); |
10276 | Base = APValue::LValueBase(ImpExpr, |
10277 | FromValue.getLValueBase().getCallIndex(), |
10278 | FromValue.getLValueBase().getVersion()); |
10279 | } else { |
10280 | FromElemTy = |
10281 | FromValue.getLValueBase().get<const ValueDecl *>()->getType(); |
10282 | const Decl *ImpDecl = importChecked( |
10283 | Err, From: FromValue.getLValueBase().get<const ValueDecl *>()); |
10284 | if (Err) |
10285 | return std::move(Err); |
10286 | Base = APValue::LValueBase(cast<ValueDecl>(Val: ImpDecl), |
10287 | FromValue.getLValueBase().getCallIndex(), |
10288 | FromValue.getLValueBase().getVersion()); |
10289 | } |
10290 | } else { |
10291 | FromElemTy = FromValue.getLValueBase().getTypeInfoType(); |
10292 | const Type *ImpTypeInfo = importChecked( |
10293 | Err, From: FromValue.getLValueBase().get<TypeInfoLValue>().getType()); |
10294 | QualType ImpType = |
10295 | importChecked(Err, From: FromValue.getLValueBase().getTypeInfoType()); |
10296 | if (Err) |
10297 | return std::move(Err); |
10298 | Base = APValue::LValueBase::getTypeInfo(LV: TypeInfoLValue(ImpTypeInfo), |
10299 | TypeInfo: ImpType); |
10300 | } |
10301 | } |
10302 | CharUnits Offset = FromValue.getLValueOffset(); |
10303 | unsigned PathLength = FromValue.getLValuePath().size(); |
10304 | Result.MakeLValue(); |
10305 | if (FromValue.hasLValuePath()) { |
10306 | MutableArrayRef<APValue::LValuePathEntry> ToPath = Result.setLValueUninit( |
10307 | B: Base, O: Offset, Size: PathLength, OnePastTheEnd: FromValue.isLValueOnePastTheEnd(), |
10308 | IsNullPtr: FromValue.isNullPointer()); |
10309 | llvm::ArrayRef<APValue::LValuePathEntry> FromPath = |
10310 | FromValue.getLValuePath(); |
10311 | for (unsigned LoopIdx = 0; LoopIdx < PathLength; LoopIdx++) { |
10312 | if (FromElemTy->isRecordType()) { |
10313 | const Decl *FromDecl = |
10314 | FromPath[LoopIdx].getAsBaseOrMember().getPointer(); |
10315 | const Decl *ImpDecl = importChecked(Err, From: FromDecl); |
10316 | if (Err) |
10317 | return std::move(Err); |
10318 | if (auto *RD = dyn_cast<CXXRecordDecl>(Val: FromDecl)) |
10319 | FromElemTy = Importer.FromContext.getRecordType(RD); |
10320 | else |
10321 | FromElemTy = cast<ValueDecl>(Val: FromDecl)->getType(); |
10322 | ToPath[LoopIdx] = APValue::LValuePathEntry(APValue::BaseOrMemberType( |
10323 | ImpDecl, FromPath[LoopIdx].getAsBaseOrMember().getInt())); |
10324 | } else { |
10325 | FromElemTy = |
10326 | Importer.FromContext.getAsArrayType(T: FromElemTy)->getElementType(); |
10327 | ToPath[LoopIdx] = APValue::LValuePathEntry::ArrayIndex( |
10328 | Index: FromPath[LoopIdx].getAsArrayIndex()); |
10329 | } |
10330 | } |
10331 | } else |
10332 | Result.setLValue(B: Base, O: Offset, APValue::NoLValuePath{}, |
10333 | IsNullPtr: FromValue.isNullPointer()); |
10334 | } |
10335 | if (Err) |
10336 | return std::move(Err); |
10337 | return Result; |
10338 | } |
10339 | |
10340 | Expected<DeclarationName> ASTImporter::HandleNameConflict(DeclarationName Name, |
10341 | DeclContext *DC, |
10342 | unsigned IDNS, |
10343 | NamedDecl **Decls, |
10344 | unsigned NumDecls) { |
10345 | if (ODRHandling == ODRHandlingType::Conservative) |
10346 | // Report error at any name conflict. |
10347 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
10348 | else |
10349 | // Allow to create the new Decl with the same name. |
10350 | return Name; |
10351 | } |
10352 | |
10353 | DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) { |
10354 | if (LastDiagFromFrom) |
10355 | ToContext.getDiagnostics().notePriorDiagnosticFrom( |
10356 | Other: FromContext.getDiagnostics()); |
10357 | LastDiagFromFrom = false; |
10358 | return ToContext.getDiagnostics().Report(Loc, DiagID); |
10359 | } |
10360 | |
10361 | DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) { |
10362 | if (!LastDiagFromFrom) |
10363 | FromContext.getDiagnostics().notePriorDiagnosticFrom( |
10364 | Other: ToContext.getDiagnostics()); |
10365 | LastDiagFromFrom = true; |
10366 | return FromContext.getDiagnostics().Report(Loc, DiagID); |
10367 | } |
10368 | |
10369 | void ASTImporter::CompleteDecl (Decl *D) { |
10370 | if (auto *ID = dyn_cast<ObjCInterfaceDecl>(Val: D)) { |
10371 | if (!ID->getDefinition()) |
10372 | ID->startDefinition(); |
10373 | } |
10374 | else if (auto *PD = dyn_cast<ObjCProtocolDecl>(Val: D)) { |
10375 | if (!PD->getDefinition()) |
10376 | PD->startDefinition(); |
10377 | } |
10378 | else if (auto *TD = dyn_cast<TagDecl>(Val: D)) { |
10379 | if (!TD->getDefinition() && !TD->isBeingDefined()) { |
10380 | TD->startDefinition(); |
10381 | TD->setCompleteDefinition(true); |
10382 | } |
10383 | } |
10384 | else { |
10385 | assert(0 && "CompleteDecl called on a Decl that can't be completed" ); |
10386 | } |
10387 | } |
10388 | |
10389 | Decl *ASTImporter::MapImported(Decl *From, Decl *To) { |
10390 | llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(Val: From); |
10391 | assert((Pos == ImportedDecls.end() || Pos->second == To) && |
10392 | "Try to import an already imported Decl" ); |
10393 | if (Pos != ImportedDecls.end()) |
10394 | return Pos->second; |
10395 | ImportedDecls[From] = To; |
10396 | // This mapping should be maintained only in this function. Therefore do not |
10397 | // check for additional consistency. |
10398 | ImportedFromDecls[To] = From; |
10399 | // In the case of TypedefNameDecl we create the Decl first and only then we |
10400 | // import and set its DeclContext. So, the DC is still not set when we reach |
10401 | // here from GetImportedOrCreateDecl. |
10402 | if (To->getDeclContext()) |
10403 | AddToLookupTable(ToD: To); |
10404 | return To; |
10405 | } |
10406 | |
10407 | std::optional<ASTImportError> |
10408 | ASTImporter::getImportDeclErrorIfAny(Decl *FromD) const { |
10409 | auto Pos = ImportDeclErrors.find(Val: FromD); |
10410 | if (Pos != ImportDeclErrors.end()) |
10411 | return Pos->second; |
10412 | else |
10413 | return std::nullopt; |
10414 | } |
10415 | |
10416 | void ASTImporter::setImportDeclError(Decl *From, ASTImportError Error) { |
10417 | auto InsertRes = ImportDeclErrors.insert(KV: {From, Error}); |
10418 | (void)InsertRes; |
10419 | // Either we set the error for the first time, or we already had set one and |
10420 | // now we want to set the same error. |
10421 | assert(InsertRes.second || InsertRes.first->second.Error == Error.Error); |
10422 | } |
10423 | |
10424 | bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To, |
10425 | bool Complain) { |
10426 | llvm::DenseMap<const Type *, const Type *>::iterator Pos = |
10427 | ImportedTypes.find(Val: From.getTypePtr()); |
10428 | if (Pos != ImportedTypes.end()) { |
10429 | if (ExpectedType ToFromOrErr = Import(FromT: From)) { |
10430 | if (ToContext.hasSameType(T1: *ToFromOrErr, T2: To)) |
10431 | return true; |
10432 | } else { |
10433 | llvm::consumeError(Err: ToFromOrErr.takeError()); |
10434 | } |
10435 | } |
10436 | |
10437 | StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls, |
10438 | getStructuralEquivalenceKind(Importer: *this), false, |
10439 | Complain); |
10440 | return Ctx.IsEquivalent(T1: From, T2: To); |
10441 | } |
10442 | |