| 1 | //===- ASTImporter.cpp - Importing ASTs from other Contexts ---------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines the ASTImporter class which imports AST nodes from one |
| 10 | // context into another context. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/ASTImporter.h" |
| 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/ASTDiagnostic.h" |
| 17 | #include "clang/AST/ASTImporterSharedState.h" |
| 18 | #include "clang/AST/ASTLambda.h" |
| 19 | #include "clang/AST/ASTStructuralEquivalence.h" |
| 20 | #include "clang/AST/Attr.h" |
| 21 | #include "clang/AST/Decl.h" |
| 22 | #include "clang/AST/DeclAccessPair.h" |
| 23 | #include "clang/AST/DeclBase.h" |
| 24 | #include "clang/AST/DeclCXX.h" |
| 25 | #include "clang/AST/DeclFriend.h" |
| 26 | #include "clang/AST/DeclGroup.h" |
| 27 | #include "clang/AST/DeclObjC.h" |
| 28 | #include "clang/AST/DeclTemplate.h" |
| 29 | #include "clang/AST/DeclVisitor.h" |
| 30 | #include "clang/AST/DeclarationName.h" |
| 31 | #include "clang/AST/Expr.h" |
| 32 | #include "clang/AST/ExprCXX.h" |
| 33 | #include "clang/AST/ExprObjC.h" |
| 34 | #include "clang/AST/ExternalASTSource.h" |
| 35 | #include "clang/AST/LambdaCapture.h" |
| 36 | #include "clang/AST/NestedNameSpecifier.h" |
| 37 | #include "clang/AST/OperationKinds.h" |
| 38 | #include "clang/AST/Stmt.h" |
| 39 | #include "clang/AST/StmtCXX.h" |
| 40 | #include "clang/AST/StmtObjC.h" |
| 41 | #include "clang/AST/StmtVisitor.h" |
| 42 | #include "clang/AST/TemplateBase.h" |
| 43 | #include "clang/AST/TemplateName.h" |
| 44 | #include "clang/AST/Type.h" |
| 45 | #include "clang/AST/TypeLoc.h" |
| 46 | #include "clang/AST/TypeVisitor.h" |
| 47 | #include "clang/AST/UnresolvedSet.h" |
| 48 | #include "clang/Basic/Builtins.h" |
| 49 | #include "clang/Basic/ExceptionSpecificationType.h" |
| 50 | #include "clang/Basic/FileManager.h" |
| 51 | #include "clang/Basic/IdentifierTable.h" |
| 52 | #include "clang/Basic/LLVM.h" |
| 53 | #include "clang/Basic/LangOptions.h" |
| 54 | #include "clang/Basic/SourceLocation.h" |
| 55 | #include "clang/Basic/SourceManager.h" |
| 56 | #include "clang/Basic/Specifiers.h" |
| 57 | #include "llvm/ADT/ArrayRef.h" |
| 58 | #include "llvm/ADT/DenseMap.h" |
| 59 | #include "llvm/ADT/STLExtras.h" |
| 60 | #include "llvm/ADT/ScopeExit.h" |
| 61 | #include "llvm/ADT/SmallVector.h" |
| 62 | #include "llvm/Support/ErrorHandling.h" |
| 63 | #include "llvm/Support/MemoryBuffer.h" |
| 64 | #include <algorithm> |
| 65 | #include <cassert> |
| 66 | #include <cstddef> |
| 67 | #include <memory> |
| 68 | #include <optional> |
| 69 | #include <type_traits> |
| 70 | #include <utility> |
| 71 | |
| 72 | namespace clang { |
| 73 | |
| 74 | using llvm::make_error; |
| 75 | using llvm::Error; |
| 76 | using llvm::Expected; |
| 77 | using ExpectedTypePtr = llvm::Expected<const Type *>; |
| 78 | using ExpectedType = llvm::Expected<QualType>; |
| 79 | using ExpectedStmt = llvm::Expected<Stmt *>; |
| 80 | using ExpectedExpr = llvm::Expected<Expr *>; |
| 81 | using ExpectedDecl = llvm::Expected<Decl *>; |
| 82 | using ExpectedSLoc = llvm::Expected<SourceLocation>; |
| 83 | using ExpectedName = llvm::Expected<DeclarationName>; |
| 84 | |
| 85 | std::string ASTImportError::toString() const { |
| 86 | // FIXME: Improve error texts. |
| 87 | switch (Error) { |
| 88 | case NameConflict: |
| 89 | return "NameConflict" ; |
| 90 | case UnsupportedConstruct: |
| 91 | return "UnsupportedConstruct" ; |
| 92 | case Unknown: |
| 93 | return "Unknown error" ; |
| 94 | } |
| 95 | llvm_unreachable("Invalid error code." ); |
| 96 | return "Invalid error code." ; |
| 97 | } |
| 98 | |
| 99 | void ASTImportError::log(raw_ostream &OS) const { OS << toString(); } |
| 100 | |
| 101 | std::error_code ASTImportError::convertToErrorCode() const { |
| 102 | llvm_unreachable("Function not implemented." ); |
| 103 | } |
| 104 | |
| 105 | char ASTImportError::ID; |
| 106 | |
| 107 | template <class T> |
| 108 | static SmallVector<Decl *, 2> |
| 109 | getCanonicalForwardRedeclChain(Redeclarable<T> *D) { |
| 110 | SmallVector<Decl *, 2> Redecls; |
| 111 | for (auto *R : D->getFirstDecl()->redecls()) { |
| 112 | if (R != D->getFirstDecl()) |
| 113 | Redecls.push_back(Elt: R); |
| 114 | } |
| 115 | Redecls.push_back(Elt: D->getFirstDecl()); |
| 116 | std::reverse(first: Redecls.begin(), last: Redecls.end()); |
| 117 | return Redecls; |
| 118 | } |
| 119 | |
| 120 | SmallVector<Decl*, 2> getCanonicalForwardRedeclChain(Decl* D) { |
| 121 | if (auto *FD = dyn_cast<FunctionDecl>(Val: D)) |
| 122 | return getCanonicalForwardRedeclChain<FunctionDecl>(D: FD); |
| 123 | if (auto *VD = dyn_cast<VarDecl>(Val: D)) |
| 124 | return getCanonicalForwardRedeclChain<VarDecl>(D: VD); |
| 125 | if (auto *TD = dyn_cast<TagDecl>(Val: D)) |
| 126 | return getCanonicalForwardRedeclChain<TagDecl>(D: TD); |
| 127 | llvm_unreachable("Bad declaration kind" ); |
| 128 | } |
| 129 | |
| 130 | static void updateFlags(const Decl *From, Decl *To) { |
| 131 | // Check if some flags or attrs are new in 'From' and copy into 'To'. |
| 132 | // FIXME: Other flags or attrs? |
| 133 | if (From->isUsed(CheckUsedAttr: false) && !To->isUsed(CheckUsedAttr: false)) |
| 134 | To->setIsUsed(); |
| 135 | } |
| 136 | |
| 137 | /// How to handle import errors that occur when import of a child declaration |
| 138 | /// of a DeclContext fails. |
| 139 | class ChildErrorHandlingStrategy { |
| 140 | /// This context is imported (in the 'from' domain). |
| 141 | /// It is nullptr if a non-DeclContext is imported. |
| 142 | const DeclContext *const FromDC; |
| 143 | /// Ignore import errors of the children. |
| 144 | /// If true, the context can be imported successfully if a child |
| 145 | /// of it failed to import. Otherwise the import errors of the child nodes |
| 146 | /// are accumulated (joined) into the import error object of the parent. |
| 147 | /// (Import of a parent can fail in other ways.) |
| 148 | bool const IgnoreChildErrors; |
| 149 | |
| 150 | public: |
| 151 | ChildErrorHandlingStrategy(const DeclContext *FromDC) |
| 152 | : FromDC(FromDC), IgnoreChildErrors(!isa<TagDecl>(Val: FromDC)) {} |
| 153 | ChildErrorHandlingStrategy(const Decl *FromD) |
| 154 | : FromDC(dyn_cast<DeclContext>(Val: FromD)), |
| 155 | IgnoreChildErrors(!isa<TagDecl>(Val: FromD)) {} |
| 156 | |
| 157 | /// Process the import result of a child (of the current declaration). |
| 158 | /// \param ResultErr The import error that can be used as result of |
| 159 | /// importing the parent. This may be changed by the function. |
| 160 | /// \param ChildErr Result of importing a child. Can be success or error. |
| 161 | void handleChildImportResult(Error &ResultErr, Error &&ChildErr) { |
| 162 | if (ChildErr && !IgnoreChildErrors) |
| 163 | ResultErr = joinErrors(E1: std::move(ResultErr), E2: std::move(ChildErr)); |
| 164 | else |
| 165 | consumeError(Err: std::move(ChildErr)); |
| 166 | } |
| 167 | |
| 168 | /// Determine if import failure of a child does not cause import failure of |
| 169 | /// its parent. |
| 170 | bool ignoreChildErrorOnParent(Decl *FromChildD) const { |
| 171 | if (!IgnoreChildErrors || !FromDC) |
| 172 | return false; |
| 173 | return FromDC->containsDecl(D: FromChildD); |
| 174 | } |
| 175 | }; |
| 176 | |
| 177 | class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, ExpectedType>, |
| 178 | public DeclVisitor<ASTNodeImporter, ExpectedDecl>, |
| 179 | public StmtVisitor<ASTNodeImporter, ExpectedStmt> { |
| 180 | ASTImporter &Importer; |
| 181 | |
| 182 | // Use this instead of Importer.importInto . |
| 183 | template <typename ImportT> |
| 184 | [[nodiscard]] Error importInto(ImportT &To, const ImportT &From) { |
| 185 | return Importer.importInto(To, From); |
| 186 | } |
| 187 | |
| 188 | // Use this to import pointers of specific type. |
| 189 | template <typename ImportT> |
| 190 | [[nodiscard]] Error importInto(ImportT *&To, ImportT *From) { |
| 191 | auto ToOrErr = Importer.Import(From); |
| 192 | if (ToOrErr) |
| 193 | To = cast_or_null<ImportT>(*ToOrErr); |
| 194 | return ToOrErr.takeError(); |
| 195 | } |
| 196 | |
| 197 | // Call the import function of ASTImporter for a baseclass of type `T` and |
| 198 | // cast the return value to `T`. |
| 199 | template <typename T> |
| 200 | auto import(T *From) |
| 201 | -> std::conditional_t<std::is_base_of_v<Type, T>, Expected<const T *>, |
| 202 | Expected<T *>> { |
| 203 | auto ToOrErr = Importer.Import(From); |
| 204 | if (!ToOrErr) |
| 205 | return ToOrErr.takeError(); |
| 206 | return cast_or_null<T>(*ToOrErr); |
| 207 | } |
| 208 | |
| 209 | template <typename T> |
| 210 | auto import(const T *From) { |
| 211 | return import(const_cast<T *>(From)); |
| 212 | } |
| 213 | |
| 214 | // Call the import function of ASTImporter for type `T`. |
| 215 | template <typename T> |
| 216 | Expected<T> import(const T &From) { |
| 217 | return Importer.Import(From); |
| 218 | } |
| 219 | |
| 220 | // Import an std::optional<T> by importing the contained T, if any. |
| 221 | template <typename T> |
| 222 | Expected<std::optional<T>> import(std::optional<T> From) { |
| 223 | if (!From) |
| 224 | return std::nullopt; |
| 225 | return import(*From); |
| 226 | } |
| 227 | |
| 228 | ExplicitSpecifier importExplicitSpecifier(Error &Err, |
| 229 | ExplicitSpecifier ESpec); |
| 230 | |
| 231 | // Wrapper for an overload set. |
| 232 | template <typename ToDeclT> struct CallOverloadedCreateFun { |
| 233 | template <typename... Args> decltype(auto) operator()(Args &&... args) { |
| 234 | return ToDeclT::Create(std::forward<Args>(args)...); |
| 235 | } |
| 236 | }; |
| 237 | |
| 238 | // Always use these functions to create a Decl during import. There are |
| 239 | // certain tasks which must be done after the Decl was created, e.g. we |
| 240 | // must immediately register that as an imported Decl. The parameter `ToD` |
| 241 | // will be set to the newly created Decl or if had been imported before |
| 242 | // then to the already imported Decl. Returns a bool value set to true if |
| 243 | // the `FromD` had been imported before. |
| 244 | template <typename ToDeclT, typename FromDeclT, typename... Args> |
| 245 | [[nodiscard]] bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD, |
| 246 | Args &&...args) { |
| 247 | // There may be several overloads of ToDeclT::Create. We must make sure |
| 248 | // to call the one which would be chosen by the arguments, thus we use a |
| 249 | // wrapper for the overload set. |
| 250 | CallOverloadedCreateFun<ToDeclT> OC; |
| 251 | return GetImportedOrCreateSpecialDecl(ToD, OC, FromD, |
| 252 | std::forward<Args>(args)...); |
| 253 | } |
| 254 | // Use this overload if a special Type is needed to be created. E.g if we |
| 255 | // want to create a `TypeAliasDecl` and assign that to a `TypedefNameDecl` |
| 256 | // then: |
| 257 | // TypedefNameDecl *ToTypedef; |
| 258 | // GetImportedOrCreateDecl<TypeAliasDecl>(ToTypedef, FromD, ...); |
| 259 | template <typename NewDeclT, typename ToDeclT, typename FromDeclT, |
| 260 | typename... Args> |
| 261 | [[nodiscard]] bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD, |
| 262 | Args &&...args) { |
| 263 | CallOverloadedCreateFun<NewDeclT> OC; |
| 264 | return GetImportedOrCreateSpecialDecl(ToD, OC, FromD, |
| 265 | std::forward<Args>(args)...); |
| 266 | } |
| 267 | // Use this version if a special create function must be |
| 268 | // used, e.g. CXXRecordDecl::CreateLambda . |
| 269 | template <typename ToDeclT, typename CreateFunT, typename FromDeclT, |
| 270 | typename... Args> |
| 271 | [[nodiscard]] bool |
| 272 | GetImportedOrCreateSpecialDecl(ToDeclT *&ToD, CreateFunT CreateFun, |
| 273 | FromDeclT *FromD, Args &&...args) { |
| 274 | if (Importer.getImportDeclErrorIfAny(FromD)) { |
| 275 | ToD = nullptr; |
| 276 | return true; // Already imported but with error. |
| 277 | } |
| 278 | ToD = cast_or_null<ToDeclT>(Importer.GetAlreadyImportedOrNull(FromD)); |
| 279 | if (ToD) |
| 280 | return true; // Already imported. |
| 281 | ToD = CreateFun(std::forward<Args>(args)...); |
| 282 | // Keep track of imported Decls. |
| 283 | Importer.RegisterImportedDecl(FromD, ToD); |
| 284 | Importer.SharedState->markAsNewDecl(ToD); |
| 285 | InitializeImportedDecl(FromD, ToD); |
| 286 | return false; // A new Decl is created. |
| 287 | } |
| 288 | |
| 289 | void InitializeImportedDecl(Decl *FromD, Decl *ToD) { |
| 290 | ToD->IdentifierNamespace = FromD->IdentifierNamespace; |
| 291 | if (FromD->isUsed()) |
| 292 | ToD->setIsUsed(); |
| 293 | if (FromD->isImplicit()) |
| 294 | ToD->setImplicit(); |
| 295 | } |
| 296 | |
| 297 | // Check if we have found an existing definition. Returns with that |
| 298 | // definition if yes, otherwise returns null. |
| 299 | Decl *FindAndMapDefinition(FunctionDecl *D, FunctionDecl *FoundFunction) { |
| 300 | const FunctionDecl *Definition = nullptr; |
| 301 | if (D->doesThisDeclarationHaveABody() && |
| 302 | FoundFunction->hasBody(Definition)) |
| 303 | return Importer.MapImported(From: D, To: const_cast<FunctionDecl *>(Definition)); |
| 304 | return nullptr; |
| 305 | } |
| 306 | |
| 307 | void addDeclToContexts(Decl *FromD, Decl *ToD) { |
| 308 | if (Importer.isMinimalImport()) { |
| 309 | // In minimal import case the decl must be added even if it is not |
| 310 | // contained in original context, for LLDB compatibility. |
| 311 | // FIXME: Check if a better solution is possible. |
| 312 | if (!FromD->getDescribedTemplate() && |
| 313 | FromD->getFriendObjectKind() == Decl::FOK_None) |
| 314 | ToD->getLexicalDeclContext()->addDeclInternal(D: ToD); |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | DeclContext *FromDC = FromD->getDeclContext(); |
| 319 | DeclContext *FromLexicalDC = FromD->getLexicalDeclContext(); |
| 320 | DeclContext *ToDC = ToD->getDeclContext(); |
| 321 | DeclContext *ToLexicalDC = ToD->getLexicalDeclContext(); |
| 322 | |
| 323 | bool Visible = false; |
| 324 | if (FromDC->containsDeclAndLoad(D: FromD)) { |
| 325 | ToDC->addDeclInternal(D: ToD); |
| 326 | Visible = true; |
| 327 | } |
| 328 | if (ToDC != ToLexicalDC && FromLexicalDC->containsDeclAndLoad(D: FromD)) { |
| 329 | ToLexicalDC->addDeclInternal(D: ToD); |
| 330 | Visible = true; |
| 331 | } |
| 332 | |
| 333 | // If the Decl was added to any context, it was made already visible. |
| 334 | // Otherwise it is still possible that it should be visible. |
| 335 | if (!Visible) { |
| 336 | if (auto *FromNamed = dyn_cast<NamedDecl>(Val: FromD)) { |
| 337 | auto *ToNamed = cast<NamedDecl>(Val: ToD); |
| 338 | DeclContextLookupResult FromLookup = |
| 339 | FromDC->lookup(Name: FromNamed->getDeclName()); |
| 340 | if (llvm::is_contained(Range&: FromLookup, Element: FromNamed)) |
| 341 | ToDC->makeDeclVisibleInContext(D: ToNamed); |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | void updateLookupTableForTemplateParameters(TemplateParameterList &Params, |
| 347 | DeclContext *OldDC) { |
| 348 | ASTImporterLookupTable *LT = Importer.SharedState->getLookupTable(); |
| 349 | if (!LT) |
| 350 | return; |
| 351 | |
| 352 | for (NamedDecl *TP : Params) |
| 353 | LT->update(ND: TP, OldDC); |
| 354 | } |
| 355 | |
| 356 | void updateLookupTableForTemplateParameters(TemplateParameterList &Params) { |
| 357 | updateLookupTableForTemplateParameters( |
| 358 | Params, OldDC: Importer.getToContext().getTranslationUnitDecl()); |
| 359 | } |
| 360 | |
| 361 | template <typename TemplateParmDeclT> |
| 362 | Error importTemplateParameterDefaultArgument(const TemplateParmDeclT *D, |
| 363 | TemplateParmDeclT *ToD) { |
| 364 | if (D->hasDefaultArgument()) { |
| 365 | if (D->defaultArgumentWasInherited()) { |
| 366 | Expected<TemplateParmDeclT *> ToInheritedFromOrErr = |
| 367 | import(D->getDefaultArgStorage().getInheritedFrom()); |
| 368 | if (!ToInheritedFromOrErr) |
| 369 | return ToInheritedFromOrErr.takeError(); |
| 370 | TemplateParmDeclT *ToInheritedFrom = *ToInheritedFromOrErr; |
| 371 | if (!ToInheritedFrom->hasDefaultArgument()) { |
| 372 | // Resolve possible circular dependency between default value of the |
| 373 | // template argument and the template declaration. |
| 374 | Expected<TemplateArgumentLoc> ToInheritedDefaultArgOrErr = |
| 375 | import(D->getDefaultArgStorage() |
| 376 | .getInheritedFrom() |
| 377 | ->getDefaultArgument()); |
| 378 | if (!ToInheritedDefaultArgOrErr) |
| 379 | return ToInheritedDefaultArgOrErr.takeError(); |
| 380 | ToInheritedFrom->setDefaultArgument(Importer.getToContext(), |
| 381 | *ToInheritedDefaultArgOrErr); |
| 382 | } |
| 383 | ToD->setInheritedDefaultArgument(ToD->getASTContext(), |
| 384 | ToInheritedFrom); |
| 385 | } else { |
| 386 | Expected<TemplateArgumentLoc> ToDefaultArgOrErr = |
| 387 | import(D->getDefaultArgument()); |
| 388 | if (!ToDefaultArgOrErr) |
| 389 | return ToDefaultArgOrErr.takeError(); |
| 390 | // Default argument could have been set in the |
| 391 | // '!ToInheritedFrom->hasDefaultArgument()' branch above. |
| 392 | if (!ToD->hasDefaultArgument()) |
| 393 | ToD->setDefaultArgument(Importer.getToContext(), |
| 394 | *ToDefaultArgOrErr); |
| 395 | } |
| 396 | } |
| 397 | return Error::success(); |
| 398 | } |
| 399 | |
| 400 | public: |
| 401 | explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) {} |
| 402 | |
| 403 | using TypeVisitor<ASTNodeImporter, ExpectedType>::Visit; |
| 404 | using DeclVisitor<ASTNodeImporter, ExpectedDecl>::Visit; |
| 405 | using StmtVisitor<ASTNodeImporter, ExpectedStmt>::Visit; |
| 406 | |
| 407 | // Importing types |
| 408 | ExpectedType VisitType(const Type *T); |
| 409 | #define TYPE(Class, Base) \ |
| 410 | ExpectedType Visit##Class##Type(const Class##Type *T); |
| 411 | #include "clang/AST/TypeNodes.inc" |
| 412 | |
| 413 | // Importing declarations |
| 414 | Error ImportDeclParts(NamedDecl *D, DeclarationName &Name, NamedDecl *&ToD, |
| 415 | SourceLocation &Loc); |
| 416 | Error ImportDeclParts( |
| 417 | NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC, |
| 418 | DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc); |
| 419 | Error ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr); |
| 420 | Error ImportDeclarationNameLoc( |
| 421 | const DeclarationNameInfo &From, DeclarationNameInfo &To); |
| 422 | Error ImportDeclContext(DeclContext *FromDC, bool ForceImport = false); |
| 423 | Error ImportDeclContext( |
| 424 | Decl *From, DeclContext *&ToDC, DeclContext *&ToLexicalDC); |
| 425 | Error ImportImplicitMethods(const CXXRecordDecl *From, CXXRecordDecl *To); |
| 426 | |
| 427 | Error ImportFieldDeclDefinition(const FieldDecl *From, const FieldDecl *To); |
| 428 | Expected<CXXCastPath> ImportCastPath(CastExpr *E); |
| 429 | Expected<APValue> ImportAPValue(const APValue &FromValue); |
| 430 | |
| 431 | using Designator = DesignatedInitExpr::Designator; |
| 432 | |
| 433 | /// What we should import from the definition. |
| 434 | enum ImportDefinitionKind { |
| 435 | /// Import the default subset of the definition, which might be |
| 436 | /// nothing (if minimal import is set) or might be everything (if minimal |
| 437 | /// import is not set). |
| 438 | IDK_Default, |
| 439 | /// Import everything. |
| 440 | IDK_Everything, |
| 441 | /// Import only the bare bones needed to establish a valid |
| 442 | /// DeclContext. |
| 443 | IDK_Basic |
| 444 | }; |
| 445 | |
| 446 | bool shouldForceImportDeclContext(ImportDefinitionKind IDK) { |
| 447 | return IDK == IDK_Everything || |
| 448 | (IDK == IDK_Default && !Importer.isMinimalImport()); |
| 449 | } |
| 450 | |
| 451 | Error ImportInitializer(VarDecl *From, VarDecl *To); |
| 452 | Error ImportDefinition( |
| 453 | RecordDecl *From, RecordDecl *To, |
| 454 | ImportDefinitionKind Kind = IDK_Default); |
| 455 | Error ImportDefinition( |
| 456 | EnumDecl *From, EnumDecl *To, |
| 457 | ImportDefinitionKind Kind = IDK_Default); |
| 458 | Error ImportDefinition( |
| 459 | ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, |
| 460 | ImportDefinitionKind Kind = IDK_Default); |
| 461 | Error ImportDefinition( |
| 462 | ObjCProtocolDecl *From, ObjCProtocolDecl *To, |
| 463 | ImportDefinitionKind Kind = IDK_Default); |
| 464 | Error ImportTemplateArguments(ArrayRef<TemplateArgument> FromArgs, |
| 465 | SmallVectorImpl<TemplateArgument> &ToArgs); |
| 466 | Expected<TemplateArgument> |
| 467 | ImportTemplateArgument(const TemplateArgument &From); |
| 468 | |
| 469 | template <typename InContainerTy> |
| 470 | Error ImportTemplateArgumentListInfo( |
| 471 | const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo); |
| 472 | |
| 473 | template<typename InContainerTy> |
| 474 | Error ImportTemplateArgumentListInfo( |
| 475 | SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc, |
| 476 | const InContainerTy &Container, TemplateArgumentListInfo &Result); |
| 477 | |
| 478 | using TemplateArgsTy = SmallVector<TemplateArgument, 8>; |
| 479 | using FunctionTemplateAndArgsTy = |
| 480 | std::tuple<FunctionTemplateDecl *, TemplateArgsTy>; |
| 481 | Expected<FunctionTemplateAndArgsTy> |
| 482 | ImportFunctionTemplateWithTemplateArgsFromSpecialization( |
| 483 | FunctionDecl *FromFD); |
| 484 | |
| 485 | template <typename DeclTy> |
| 486 | Error ImportTemplateParameterLists(const DeclTy *FromD, DeclTy *ToD); |
| 487 | |
| 488 | Error ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD); |
| 489 | |
| 490 | Error ImportFunctionDeclBody(FunctionDecl *FromFD, FunctionDecl *ToFD); |
| 491 | |
| 492 | Error ImportDefaultArgOfParmVarDecl(const ParmVarDecl *FromParam, |
| 493 | ParmVarDecl *ToParam); |
| 494 | |
| 495 | Expected<InheritedConstructor> |
| 496 | ImportInheritedConstructor(const InheritedConstructor &From); |
| 497 | |
| 498 | // Use for allocating string for newly imported object. |
| 499 | StringRef ImportASTStringRef(StringRef FromStr); |
| 500 | Error ImportConstraintSatisfaction(const ASTConstraintSatisfaction &FromSat, |
| 501 | ConstraintSatisfaction &ToSat); |
| 502 | Expected<concepts::Requirement *> |
| 503 | ImportTypeRequirement(concepts::TypeRequirement *From); |
| 504 | Expected<concepts::Requirement *> |
| 505 | ImportExprRequirement(concepts::ExprRequirement *From); |
| 506 | Expected<concepts::Requirement *> |
| 507 | ImportNestedRequirement(concepts::NestedRequirement *From); |
| 508 | |
| 509 | template <typename T> |
| 510 | bool hasSameVisibilityContextAndLinkage(T *Found, T *From); |
| 511 | |
| 512 | bool IsStructuralMatch(Decl *From, Decl *To, bool Complain = true, |
| 513 | bool IgnoreTemplateParmDepth = false); |
| 514 | ExpectedDecl VisitDecl(Decl *D); |
| 515 | ExpectedDecl VisitImportDecl(ImportDecl *D); |
| 516 | ExpectedDecl VisitEmptyDecl(EmptyDecl *D); |
| 517 | ExpectedDecl VisitAccessSpecDecl(AccessSpecDecl *D); |
| 518 | ExpectedDecl VisitStaticAssertDecl(StaticAssertDecl *D); |
| 519 | ExpectedDecl VisitTranslationUnitDecl(TranslationUnitDecl *D); |
| 520 | ExpectedDecl VisitFileScopeAsmDecl(FileScopeAsmDecl *D); |
| 521 | ExpectedDecl VisitBindingDecl(BindingDecl *D); |
| 522 | ExpectedDecl VisitNamespaceDecl(NamespaceDecl *D); |
| 523 | ExpectedDecl VisitNamespaceAliasDecl(NamespaceAliasDecl *D); |
| 524 | ExpectedDecl VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias); |
| 525 | ExpectedDecl VisitTypedefDecl(TypedefDecl *D); |
| 526 | ExpectedDecl VisitTypeAliasDecl(TypeAliasDecl *D); |
| 527 | ExpectedDecl VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D); |
| 528 | ExpectedDecl VisitLabelDecl(LabelDecl *D); |
| 529 | ExpectedDecl VisitEnumDecl(EnumDecl *D); |
| 530 | ExpectedDecl VisitRecordDecl(RecordDecl *D); |
| 531 | ExpectedDecl VisitEnumConstantDecl(EnumConstantDecl *D); |
| 532 | ExpectedDecl VisitFunctionDecl(FunctionDecl *D); |
| 533 | ExpectedDecl VisitCXXMethodDecl(CXXMethodDecl *D); |
| 534 | ExpectedDecl VisitCXXConstructorDecl(CXXConstructorDecl *D); |
| 535 | ExpectedDecl VisitCXXDestructorDecl(CXXDestructorDecl *D); |
| 536 | ExpectedDecl VisitCXXConversionDecl(CXXConversionDecl *D); |
| 537 | ExpectedDecl VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D); |
| 538 | ExpectedDecl VisitFieldDecl(FieldDecl *D); |
| 539 | ExpectedDecl VisitIndirectFieldDecl(IndirectFieldDecl *D); |
| 540 | ExpectedDecl VisitFriendDecl(FriendDecl *D); |
| 541 | ExpectedDecl VisitObjCIvarDecl(ObjCIvarDecl *D); |
| 542 | ExpectedDecl VisitVarDecl(VarDecl *D); |
| 543 | ExpectedDecl VisitImplicitParamDecl(ImplicitParamDecl *D); |
| 544 | ExpectedDecl VisitParmVarDecl(ParmVarDecl *D); |
| 545 | ExpectedDecl VisitObjCMethodDecl(ObjCMethodDecl *D); |
| 546 | ExpectedDecl VisitObjCTypeParamDecl(ObjCTypeParamDecl *D); |
| 547 | ExpectedDecl VisitObjCCategoryDecl(ObjCCategoryDecl *D); |
| 548 | ExpectedDecl VisitObjCProtocolDecl(ObjCProtocolDecl *D); |
| 549 | ExpectedDecl VisitLinkageSpecDecl(LinkageSpecDecl *D); |
| 550 | ExpectedDecl VisitUsingDecl(UsingDecl *D); |
| 551 | ExpectedDecl VisitUsingShadowDecl(UsingShadowDecl *D); |
| 552 | ExpectedDecl VisitUsingDirectiveDecl(UsingDirectiveDecl *D); |
| 553 | ExpectedDecl VisitUsingPackDecl(UsingPackDecl *D); |
| 554 | ExpectedDecl ImportUsingShadowDecls(BaseUsingDecl *D, BaseUsingDecl *ToSI); |
| 555 | ExpectedDecl VisitUsingEnumDecl(UsingEnumDecl *D); |
| 556 | ExpectedDecl VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); |
| 557 | ExpectedDecl VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); |
| 558 | ExpectedDecl VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D); |
| 559 | ExpectedDecl |
| 560 | VisitLifetimeExtendedTemporaryDecl(LifetimeExtendedTemporaryDecl *D); |
| 561 | |
| 562 | Expected<ObjCTypeParamList *> |
| 563 | ImportObjCTypeParamList(ObjCTypeParamList *list); |
| 564 | |
| 565 | ExpectedDecl VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
| 566 | ExpectedDecl VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
| 567 | ExpectedDecl VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
| 568 | ExpectedDecl VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
| 569 | ExpectedDecl VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); |
| 570 | ExpectedDecl VisitTemplateParamObjectDecl(TemplateParamObjectDecl *D); |
| 571 | ExpectedDecl VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); |
| 572 | ExpectedDecl VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); |
| 573 | ExpectedDecl VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); |
| 574 | ExpectedDecl VisitClassTemplateDecl(ClassTemplateDecl *D); |
| 575 | ExpectedDecl VisitClassTemplateSpecializationDecl( |
| 576 | ClassTemplateSpecializationDecl *D); |
| 577 | ExpectedDecl VisitVarTemplateDecl(VarTemplateDecl *D); |
| 578 | ExpectedDecl VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D); |
| 579 | ExpectedDecl VisitFunctionTemplateDecl(FunctionTemplateDecl *D); |
| 580 | ExpectedDecl VisitConceptDecl(ConceptDecl* D); |
| 581 | ExpectedDecl VisitRequiresExprBodyDecl(RequiresExprBodyDecl* E); |
| 582 | ExpectedDecl VisitImplicitConceptSpecializationDecl(ImplicitConceptSpecializationDecl* D); |
| 583 | |
| 584 | // Importing statements |
| 585 | ExpectedStmt VisitStmt(Stmt *S); |
| 586 | ExpectedStmt VisitGCCAsmStmt(GCCAsmStmt *S); |
| 587 | ExpectedStmt VisitDeclStmt(DeclStmt *S); |
| 588 | ExpectedStmt VisitNullStmt(NullStmt *S); |
| 589 | ExpectedStmt VisitCompoundStmt(CompoundStmt *S); |
| 590 | ExpectedStmt VisitCaseStmt(CaseStmt *S); |
| 591 | ExpectedStmt VisitDefaultStmt(DefaultStmt *S); |
| 592 | ExpectedStmt VisitLabelStmt(LabelStmt *S); |
| 593 | ExpectedStmt VisitAttributedStmt(AttributedStmt *S); |
| 594 | ExpectedStmt VisitIfStmt(IfStmt *S); |
| 595 | ExpectedStmt VisitSwitchStmt(SwitchStmt *S); |
| 596 | ExpectedStmt VisitWhileStmt(WhileStmt *S); |
| 597 | ExpectedStmt VisitDoStmt(DoStmt *S); |
| 598 | ExpectedStmt VisitForStmt(ForStmt *S); |
| 599 | ExpectedStmt VisitGotoStmt(GotoStmt *S); |
| 600 | ExpectedStmt VisitIndirectGotoStmt(IndirectGotoStmt *S); |
| 601 | ExpectedStmt VisitContinueStmt(ContinueStmt *S); |
| 602 | ExpectedStmt VisitBreakStmt(BreakStmt *S); |
| 603 | ExpectedStmt VisitReturnStmt(ReturnStmt *S); |
| 604 | // FIXME: MSAsmStmt |
| 605 | // FIXME: SEHExceptStmt |
| 606 | // FIXME: SEHFinallyStmt |
| 607 | // FIXME: SEHTryStmt |
| 608 | // FIXME: SEHLeaveStmt |
| 609 | // FIXME: CapturedStmt |
| 610 | ExpectedStmt VisitCXXCatchStmt(CXXCatchStmt *S); |
| 611 | ExpectedStmt VisitCXXTryStmt(CXXTryStmt *S); |
| 612 | ExpectedStmt VisitCXXForRangeStmt(CXXForRangeStmt *S); |
| 613 | // FIXME: MSDependentExistsStmt |
| 614 | ExpectedStmt VisitObjCForCollectionStmt(ObjCForCollectionStmt *S); |
| 615 | ExpectedStmt VisitObjCAtCatchStmt(ObjCAtCatchStmt *S); |
| 616 | ExpectedStmt VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S); |
| 617 | ExpectedStmt VisitObjCAtTryStmt(ObjCAtTryStmt *S); |
| 618 | ExpectedStmt VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
| 619 | ExpectedStmt VisitObjCAtThrowStmt(ObjCAtThrowStmt *S); |
| 620 | ExpectedStmt VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S); |
| 621 | |
| 622 | // Importing expressions |
| 623 | ExpectedStmt VisitExpr(Expr *E); |
| 624 | ExpectedStmt VisitSourceLocExpr(SourceLocExpr *E); |
| 625 | ExpectedStmt VisitVAArgExpr(VAArgExpr *E); |
| 626 | ExpectedStmt VisitChooseExpr(ChooseExpr *E); |
| 627 | ExpectedStmt VisitConvertVectorExpr(ConvertVectorExpr *E); |
| 628 | ExpectedStmt VisitShuffleVectorExpr(ShuffleVectorExpr *E); |
| 629 | ExpectedStmt VisitGNUNullExpr(GNUNullExpr *E); |
| 630 | ExpectedStmt VisitGenericSelectionExpr(GenericSelectionExpr *E); |
| 631 | ExpectedStmt VisitPredefinedExpr(PredefinedExpr *E); |
| 632 | ExpectedStmt VisitDeclRefExpr(DeclRefExpr *E); |
| 633 | ExpectedStmt VisitImplicitValueInitExpr(ImplicitValueInitExpr *E); |
| 634 | ExpectedStmt VisitDesignatedInitExpr(DesignatedInitExpr *E); |
| 635 | ExpectedStmt VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E); |
| 636 | ExpectedStmt VisitIntegerLiteral(IntegerLiteral *E); |
| 637 | ExpectedStmt VisitFloatingLiteral(FloatingLiteral *E); |
| 638 | ExpectedStmt VisitImaginaryLiteral(ImaginaryLiteral *E); |
| 639 | ExpectedStmt VisitFixedPointLiteral(FixedPointLiteral *E); |
| 640 | ExpectedStmt VisitCharacterLiteral(CharacterLiteral *E); |
| 641 | ExpectedStmt VisitStringLiteral(StringLiteral *E); |
| 642 | ExpectedStmt VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
| 643 | ExpectedStmt VisitAtomicExpr(AtomicExpr *E); |
| 644 | ExpectedStmt VisitAddrLabelExpr(AddrLabelExpr *E); |
| 645 | ExpectedStmt VisitConstantExpr(ConstantExpr *E); |
| 646 | ExpectedStmt VisitParenExpr(ParenExpr *E); |
| 647 | ExpectedStmt VisitParenListExpr(ParenListExpr *E); |
| 648 | ExpectedStmt VisitStmtExpr(StmtExpr *E); |
| 649 | ExpectedStmt VisitUnaryOperator(UnaryOperator *E); |
| 650 | ExpectedStmt VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); |
| 651 | ExpectedStmt VisitBinaryOperator(BinaryOperator *E); |
| 652 | ExpectedStmt VisitConditionalOperator(ConditionalOperator *E); |
| 653 | ExpectedStmt VisitBinaryConditionalOperator(BinaryConditionalOperator *E); |
| 654 | ExpectedStmt VisitCXXRewrittenBinaryOperator(CXXRewrittenBinaryOperator *E); |
| 655 | ExpectedStmt VisitOpaqueValueExpr(OpaqueValueExpr *E); |
| 656 | ExpectedStmt VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E); |
| 657 | ExpectedStmt VisitExpressionTraitExpr(ExpressionTraitExpr *E); |
| 658 | ExpectedStmt VisitArraySubscriptExpr(ArraySubscriptExpr *E); |
| 659 | ExpectedStmt VisitCompoundAssignOperator(CompoundAssignOperator *E); |
| 660 | ExpectedStmt VisitImplicitCastExpr(ImplicitCastExpr *E); |
| 661 | ExpectedStmt VisitExplicitCastExpr(ExplicitCastExpr *E); |
| 662 | ExpectedStmt VisitOffsetOfExpr(OffsetOfExpr *OE); |
| 663 | ExpectedStmt VisitCXXThrowExpr(CXXThrowExpr *E); |
| 664 | ExpectedStmt VisitCXXNoexceptExpr(CXXNoexceptExpr *E); |
| 665 | ExpectedStmt VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E); |
| 666 | ExpectedStmt VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); |
| 667 | ExpectedStmt VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E); |
| 668 | ExpectedStmt VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E); |
| 669 | ExpectedStmt VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E); |
| 670 | ExpectedStmt VisitPackExpansionExpr(PackExpansionExpr *E); |
| 671 | ExpectedStmt VisitSizeOfPackExpr(SizeOfPackExpr *E); |
| 672 | ExpectedStmt VisitCXXNewExpr(CXXNewExpr *E); |
| 673 | ExpectedStmt VisitCXXDeleteExpr(CXXDeleteExpr *E); |
| 674 | ExpectedStmt VisitCXXConstructExpr(CXXConstructExpr *E); |
| 675 | ExpectedStmt VisitCXXMemberCallExpr(CXXMemberCallExpr *E); |
| 676 | ExpectedStmt VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E); |
| 677 | ExpectedStmt VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E); |
| 678 | ExpectedStmt VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E); |
| 679 | ExpectedStmt VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E); |
| 680 | ExpectedStmt VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E); |
| 681 | ExpectedStmt VisitExprWithCleanups(ExprWithCleanups *E); |
| 682 | ExpectedStmt VisitCXXThisExpr(CXXThisExpr *E); |
| 683 | ExpectedStmt VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E); |
| 684 | ExpectedStmt VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E); |
| 685 | ExpectedStmt VisitMemberExpr(MemberExpr *E); |
| 686 | ExpectedStmt VisitCallExpr(CallExpr *E); |
| 687 | ExpectedStmt VisitLambdaExpr(LambdaExpr *LE); |
| 688 | ExpectedStmt VisitInitListExpr(InitListExpr *E); |
| 689 | ExpectedStmt VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E); |
| 690 | ExpectedStmt VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E); |
| 691 | ExpectedStmt VisitArrayInitLoopExpr(ArrayInitLoopExpr *E); |
| 692 | ExpectedStmt VisitArrayInitIndexExpr(ArrayInitIndexExpr *E); |
| 693 | ExpectedStmt VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E); |
| 694 | ExpectedStmt VisitCXXNamedCastExpr(CXXNamedCastExpr *E); |
| 695 | ExpectedStmt VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E); |
| 696 | ExpectedStmt VisitTypeTraitExpr(TypeTraitExpr *E); |
| 697 | ExpectedStmt VisitCXXTypeidExpr(CXXTypeidExpr *E); |
| 698 | ExpectedStmt VisitCXXFoldExpr(CXXFoldExpr *E); |
| 699 | ExpectedStmt VisitRequiresExpr(RequiresExpr* E); |
| 700 | ExpectedStmt VisitConceptSpecializationExpr(ConceptSpecializationExpr* E); |
| 701 | ExpectedStmt |
| 702 | VisitSubstNonTypeTemplateParmPackExpr(SubstNonTypeTemplateParmPackExpr *E); |
| 703 | ExpectedStmt VisitPseudoObjectExpr(PseudoObjectExpr *E); |
| 704 | ExpectedStmt VisitCXXParenListInitExpr(CXXParenListInitExpr *E); |
| 705 | |
| 706 | // Helper for chaining together multiple imports. If an error is detected, |
| 707 | // subsequent imports will return default constructed nodes, so that failure |
| 708 | // can be detected with a single conditional branch after a sequence of |
| 709 | // imports. |
| 710 | template <typename T> T importChecked(Error &Err, const T &From) { |
| 711 | // Don't attempt to import nodes if we hit an error earlier. |
| 712 | if (Err) |
| 713 | return T{}; |
| 714 | Expected<T> MaybeVal = import(From); |
| 715 | if (!MaybeVal) { |
| 716 | Err = MaybeVal.takeError(); |
| 717 | return T{}; |
| 718 | } |
| 719 | return *MaybeVal; |
| 720 | } |
| 721 | |
| 722 | template<typename IIter, typename OIter> |
| 723 | Error ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) { |
| 724 | using ItemT = std::remove_reference_t<decltype(*Obegin)>; |
| 725 | for (; Ibegin != Iend; ++Ibegin, ++Obegin) { |
| 726 | Expected<ItemT> ToOrErr = import(*Ibegin); |
| 727 | if (!ToOrErr) |
| 728 | return ToOrErr.takeError(); |
| 729 | *Obegin = *ToOrErr; |
| 730 | } |
| 731 | return Error::success(); |
| 732 | } |
| 733 | |
| 734 | // Import every item from a container structure into an output container. |
| 735 | // If error occurs, stops at first error and returns the error. |
| 736 | // The output container should have space for all needed elements (it is not |
| 737 | // expanded, new items are put into from the beginning). |
| 738 | template<typename InContainerTy, typename OutContainerTy> |
| 739 | Error ImportContainerChecked( |
| 740 | const InContainerTy &InContainer, OutContainerTy &OutContainer) { |
| 741 | return ImportArrayChecked( |
| 742 | InContainer.begin(), InContainer.end(), OutContainer.begin()); |
| 743 | } |
| 744 | |
| 745 | template<typename InContainerTy, typename OIter> |
| 746 | Error ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) { |
| 747 | return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin); |
| 748 | } |
| 749 | |
| 750 | Error ImportOverriddenMethods(CXXMethodDecl *ToMethod, |
| 751 | CXXMethodDecl *FromMethod); |
| 752 | |
| 753 | Expected<FunctionDecl *> FindFunctionTemplateSpecialization( |
| 754 | FunctionDecl *FromFD); |
| 755 | |
| 756 | // Returns true if the given function has a placeholder return type and |
| 757 | // that type is declared inside the body of the function. |
| 758 | // E.g. auto f() { struct X{}; return X(); } |
| 759 | bool hasReturnTypeDeclaredInside(FunctionDecl *D); |
| 760 | }; |
| 761 | |
| 762 | template <typename InContainerTy> |
| 763 | Error ASTNodeImporter::ImportTemplateArgumentListInfo( |
| 764 | SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc, |
| 765 | const InContainerTy &Container, TemplateArgumentListInfo &Result) { |
| 766 | auto ToLAngleLocOrErr = import(From: FromLAngleLoc); |
| 767 | if (!ToLAngleLocOrErr) |
| 768 | return ToLAngleLocOrErr.takeError(); |
| 769 | auto ToRAngleLocOrErr = import(From: FromRAngleLoc); |
| 770 | if (!ToRAngleLocOrErr) |
| 771 | return ToRAngleLocOrErr.takeError(); |
| 772 | |
| 773 | TemplateArgumentListInfo ToTAInfo(*ToLAngleLocOrErr, *ToRAngleLocOrErr); |
| 774 | if (auto Err = ImportTemplateArgumentListInfo(Container, ToTAInfo)) |
| 775 | return Err; |
| 776 | Result = std::move(ToTAInfo); |
| 777 | return Error::success(); |
| 778 | } |
| 779 | |
| 780 | template <> |
| 781 | Error ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>( |
| 782 | const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) { |
| 783 | return ImportTemplateArgumentListInfo( |
| 784 | FromLAngleLoc: From.getLAngleLoc(), FromRAngleLoc: From.getRAngleLoc(), Container: From.arguments(), Result); |
| 785 | } |
| 786 | |
| 787 | template <> |
| 788 | Error ASTNodeImporter::ImportTemplateArgumentListInfo< |
| 789 | ASTTemplateArgumentListInfo>( |
| 790 | const ASTTemplateArgumentListInfo &From, |
| 791 | TemplateArgumentListInfo &Result) { |
| 792 | return ImportTemplateArgumentListInfo( |
| 793 | FromLAngleLoc: From.LAngleLoc, FromRAngleLoc: From.RAngleLoc, Container: From.arguments(), Result); |
| 794 | } |
| 795 | |
| 796 | Expected<ASTNodeImporter::FunctionTemplateAndArgsTy> |
| 797 | ASTNodeImporter::ImportFunctionTemplateWithTemplateArgsFromSpecialization( |
| 798 | FunctionDecl *FromFD) { |
| 799 | assert(FromFD->getTemplatedKind() == |
| 800 | FunctionDecl::TK_FunctionTemplateSpecialization); |
| 801 | |
| 802 | FunctionTemplateAndArgsTy Result; |
| 803 | |
| 804 | auto *FTSInfo = FromFD->getTemplateSpecializationInfo(); |
| 805 | if (Error Err = importInto(To&: std::get<0>(t&: Result), From: FTSInfo->getTemplate())) |
| 806 | return std::move(Err); |
| 807 | |
| 808 | // Import template arguments. |
| 809 | if (Error Err = ImportTemplateArguments(FromArgs: FTSInfo->TemplateArguments->asArray(), |
| 810 | ToArgs&: std::get<1>(t&: Result))) |
| 811 | return std::move(Err); |
| 812 | |
| 813 | return Result; |
| 814 | } |
| 815 | |
| 816 | template <> |
| 817 | Expected<TemplateParameterList *> |
| 818 | ASTNodeImporter::import(TemplateParameterList *From) { |
| 819 | SmallVector<NamedDecl *, 4> To(From->size()); |
| 820 | if (Error Err = ImportContainerChecked(InContainer: *From, OutContainer&: To)) |
| 821 | return std::move(Err); |
| 822 | |
| 823 | ExpectedExpr ToRequiresClause = import(From: From->getRequiresClause()); |
| 824 | if (!ToRequiresClause) |
| 825 | return ToRequiresClause.takeError(); |
| 826 | |
| 827 | auto ToTemplateLocOrErr = import(From: From->getTemplateLoc()); |
| 828 | if (!ToTemplateLocOrErr) |
| 829 | return ToTemplateLocOrErr.takeError(); |
| 830 | auto ToLAngleLocOrErr = import(From: From->getLAngleLoc()); |
| 831 | if (!ToLAngleLocOrErr) |
| 832 | return ToLAngleLocOrErr.takeError(); |
| 833 | auto ToRAngleLocOrErr = import(From: From->getRAngleLoc()); |
| 834 | if (!ToRAngleLocOrErr) |
| 835 | return ToRAngleLocOrErr.takeError(); |
| 836 | |
| 837 | return TemplateParameterList::Create( |
| 838 | C: Importer.getToContext(), |
| 839 | TemplateLoc: *ToTemplateLocOrErr, |
| 840 | LAngleLoc: *ToLAngleLocOrErr, |
| 841 | Params: To, |
| 842 | RAngleLoc: *ToRAngleLocOrErr, |
| 843 | RequiresClause: *ToRequiresClause); |
| 844 | } |
| 845 | |
| 846 | template <> |
| 847 | Expected<TemplateArgument> |
| 848 | ASTNodeImporter::import(const TemplateArgument &From) { |
| 849 | switch (From.getKind()) { |
| 850 | case TemplateArgument::Null: |
| 851 | return TemplateArgument(); |
| 852 | |
| 853 | case TemplateArgument::Type: { |
| 854 | ExpectedType ToTypeOrErr = import(From: From.getAsType()); |
| 855 | if (!ToTypeOrErr) |
| 856 | return ToTypeOrErr.takeError(); |
| 857 | return TemplateArgument(*ToTypeOrErr, /*isNullPtr*/ false, |
| 858 | From.getIsDefaulted()); |
| 859 | } |
| 860 | |
| 861 | case TemplateArgument::Integral: { |
| 862 | ExpectedType ToTypeOrErr = import(From: From.getIntegralType()); |
| 863 | if (!ToTypeOrErr) |
| 864 | return ToTypeOrErr.takeError(); |
| 865 | return TemplateArgument(From, *ToTypeOrErr); |
| 866 | } |
| 867 | |
| 868 | case TemplateArgument::Declaration: { |
| 869 | Expected<ValueDecl *> ToOrErr = import(From: From.getAsDecl()); |
| 870 | if (!ToOrErr) |
| 871 | return ToOrErr.takeError(); |
| 872 | ExpectedType ToTypeOrErr = import(From: From.getParamTypeForDecl()); |
| 873 | if (!ToTypeOrErr) |
| 874 | return ToTypeOrErr.takeError(); |
| 875 | return TemplateArgument(dyn_cast<ValueDecl>(Val: (*ToOrErr)->getCanonicalDecl()), |
| 876 | *ToTypeOrErr, From.getIsDefaulted()); |
| 877 | } |
| 878 | |
| 879 | case TemplateArgument::NullPtr: { |
| 880 | ExpectedType ToTypeOrErr = import(From: From.getNullPtrType()); |
| 881 | if (!ToTypeOrErr) |
| 882 | return ToTypeOrErr.takeError(); |
| 883 | return TemplateArgument(*ToTypeOrErr, /*isNullPtr*/ true, |
| 884 | From.getIsDefaulted()); |
| 885 | } |
| 886 | |
| 887 | case TemplateArgument::StructuralValue: { |
| 888 | ExpectedType ToTypeOrErr = import(From: From.getStructuralValueType()); |
| 889 | if (!ToTypeOrErr) |
| 890 | return ToTypeOrErr.takeError(); |
| 891 | Expected<APValue> ToValueOrErr = import(From: From.getAsStructuralValue()); |
| 892 | if (!ToValueOrErr) |
| 893 | return ToValueOrErr.takeError(); |
| 894 | return TemplateArgument(Importer.getToContext(), *ToTypeOrErr, |
| 895 | *ToValueOrErr); |
| 896 | } |
| 897 | |
| 898 | case TemplateArgument::Template: { |
| 899 | Expected<TemplateName> ToTemplateOrErr = import(From: From.getAsTemplate()); |
| 900 | if (!ToTemplateOrErr) |
| 901 | return ToTemplateOrErr.takeError(); |
| 902 | |
| 903 | return TemplateArgument(*ToTemplateOrErr, From.getIsDefaulted()); |
| 904 | } |
| 905 | |
| 906 | case TemplateArgument::TemplateExpansion: { |
| 907 | Expected<TemplateName> ToTemplateOrErr = |
| 908 | import(From: From.getAsTemplateOrTemplatePattern()); |
| 909 | if (!ToTemplateOrErr) |
| 910 | return ToTemplateOrErr.takeError(); |
| 911 | |
| 912 | return TemplateArgument(*ToTemplateOrErr, From.getNumTemplateExpansions(), |
| 913 | From.getIsDefaulted()); |
| 914 | } |
| 915 | |
| 916 | case TemplateArgument::Expression: |
| 917 | if (ExpectedExpr ToExpr = import(From: From.getAsExpr())) |
| 918 | return TemplateArgument(*ToExpr, From.isCanonicalExpr(), |
| 919 | From.getIsDefaulted()); |
| 920 | else |
| 921 | return ToExpr.takeError(); |
| 922 | |
| 923 | case TemplateArgument::Pack: { |
| 924 | SmallVector<TemplateArgument, 2> ToPack; |
| 925 | ToPack.reserve(N: From.pack_size()); |
| 926 | if (Error Err = ImportTemplateArguments(FromArgs: From.pack_elements(), ToArgs&: ToPack)) |
| 927 | return std::move(Err); |
| 928 | |
| 929 | return TemplateArgument(ArrayRef(ToPack).copy(A&: Importer.getToContext())); |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | llvm_unreachable("Invalid template argument kind" ); |
| 934 | } |
| 935 | |
| 936 | template <> |
| 937 | Expected<TemplateArgumentLoc> |
| 938 | ASTNodeImporter::import(const TemplateArgumentLoc &TALoc) { |
| 939 | Expected<TemplateArgument> ArgOrErr = import(From: TALoc.getArgument()); |
| 940 | if (!ArgOrErr) |
| 941 | return ArgOrErr.takeError(); |
| 942 | TemplateArgument Arg = *ArgOrErr; |
| 943 | |
| 944 | TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo(); |
| 945 | |
| 946 | TemplateArgumentLocInfo ToInfo; |
| 947 | if (Arg.getKind() == TemplateArgument::Expression) { |
| 948 | ExpectedExpr E = import(From: FromInfo.getAsExpr()); |
| 949 | if (!E) |
| 950 | return E.takeError(); |
| 951 | ToInfo = TemplateArgumentLocInfo(*E); |
| 952 | } else if (Arg.getKind() == TemplateArgument::Type) { |
| 953 | if (auto TSIOrErr = import(From: FromInfo.getAsTypeSourceInfo())) |
| 954 | ToInfo = TemplateArgumentLocInfo(*TSIOrErr); |
| 955 | else |
| 956 | return TSIOrErr.takeError(); |
| 957 | } else { |
| 958 | auto ToTemplateKWLocOrErr = import(From: FromInfo.getTemplateKwLoc()); |
| 959 | if (!ToTemplateKWLocOrErr) |
| 960 | return ToTemplateKWLocOrErr.takeError(); |
| 961 | auto ToTemplateQualifierLocOrErr = import(From: TALoc.getTemplateQualifierLoc()); |
| 962 | if (!ToTemplateQualifierLocOrErr) |
| 963 | return ToTemplateQualifierLocOrErr.takeError(); |
| 964 | auto ToTemplateNameLocOrErr = import(From: FromInfo.getTemplateNameLoc()); |
| 965 | if (!ToTemplateNameLocOrErr) |
| 966 | return ToTemplateNameLocOrErr.takeError(); |
| 967 | auto ToTemplateEllipsisLocOrErr = |
| 968 | import(From: FromInfo.getTemplateEllipsisLoc()); |
| 969 | if (!ToTemplateEllipsisLocOrErr) |
| 970 | return ToTemplateEllipsisLocOrErr.takeError(); |
| 971 | ToInfo = TemplateArgumentLocInfo( |
| 972 | Importer.getToContext(), *ToTemplateKWLocOrErr, |
| 973 | *ToTemplateQualifierLocOrErr, *ToTemplateNameLocOrErr, |
| 974 | *ToTemplateEllipsisLocOrErr); |
| 975 | } |
| 976 | |
| 977 | return TemplateArgumentLoc(Arg, ToInfo); |
| 978 | } |
| 979 | |
| 980 | template <> |
| 981 | Expected<DeclGroupRef> ASTNodeImporter::import(const DeclGroupRef &DG) { |
| 982 | if (DG.isNull()) |
| 983 | return DeclGroupRef::Create(C&: Importer.getToContext(), Decls: nullptr, NumDecls: 0); |
| 984 | size_t NumDecls = DG.end() - DG.begin(); |
| 985 | SmallVector<Decl *, 1> ToDecls; |
| 986 | ToDecls.reserve(N: NumDecls); |
| 987 | for (Decl *FromD : DG) { |
| 988 | if (auto ToDOrErr = import(From: FromD)) |
| 989 | ToDecls.push_back(Elt: *ToDOrErr); |
| 990 | else |
| 991 | return ToDOrErr.takeError(); |
| 992 | } |
| 993 | return DeclGroupRef::Create(C&: Importer.getToContext(), |
| 994 | Decls: ToDecls.begin(), |
| 995 | NumDecls); |
| 996 | } |
| 997 | |
| 998 | template <> |
| 999 | Expected<ASTNodeImporter::Designator> |
| 1000 | ASTNodeImporter::import(const Designator &D) { |
| 1001 | if (D.isFieldDesignator()) { |
| 1002 | IdentifierInfo *ToFieldName = Importer.Import(FromId: D.getFieldName()); |
| 1003 | |
| 1004 | ExpectedSLoc ToDotLocOrErr = import(From: D.getDotLoc()); |
| 1005 | if (!ToDotLocOrErr) |
| 1006 | return ToDotLocOrErr.takeError(); |
| 1007 | |
| 1008 | ExpectedSLoc ToFieldLocOrErr = import(From: D.getFieldLoc()); |
| 1009 | if (!ToFieldLocOrErr) |
| 1010 | return ToFieldLocOrErr.takeError(); |
| 1011 | |
| 1012 | return DesignatedInitExpr::Designator::CreateFieldDesignator( |
| 1013 | FieldName: ToFieldName, DotLoc: *ToDotLocOrErr, FieldLoc: *ToFieldLocOrErr); |
| 1014 | } |
| 1015 | |
| 1016 | ExpectedSLoc ToLBracketLocOrErr = import(From: D.getLBracketLoc()); |
| 1017 | if (!ToLBracketLocOrErr) |
| 1018 | return ToLBracketLocOrErr.takeError(); |
| 1019 | |
| 1020 | ExpectedSLoc ToRBracketLocOrErr = import(From: D.getRBracketLoc()); |
| 1021 | if (!ToRBracketLocOrErr) |
| 1022 | return ToRBracketLocOrErr.takeError(); |
| 1023 | |
| 1024 | if (D.isArrayDesignator()) |
| 1025 | return Designator::CreateArrayDesignator(Index: D.getArrayIndex(), |
| 1026 | LBracketLoc: *ToLBracketLocOrErr, |
| 1027 | RBracketLoc: *ToRBracketLocOrErr); |
| 1028 | |
| 1029 | ExpectedSLoc ToEllipsisLocOrErr = import(From: D.getEllipsisLoc()); |
| 1030 | if (!ToEllipsisLocOrErr) |
| 1031 | return ToEllipsisLocOrErr.takeError(); |
| 1032 | |
| 1033 | assert(D.isArrayRangeDesignator()); |
| 1034 | return Designator::CreateArrayRangeDesignator( |
| 1035 | Index: D.getArrayIndex(), LBracketLoc: *ToLBracketLocOrErr, EllipsisLoc: *ToEllipsisLocOrErr, |
| 1036 | RBracketLoc: *ToRBracketLocOrErr); |
| 1037 | } |
| 1038 | |
| 1039 | template <> |
| 1040 | Expected<ConceptReference *> ASTNodeImporter::import(ConceptReference *From) { |
| 1041 | Error Err = Error::success(); |
| 1042 | auto ToNNS = importChecked(Err, From: From->getNestedNameSpecifierLoc()); |
| 1043 | auto ToTemplateKWLoc = importChecked(Err, From: From->getTemplateKWLoc()); |
| 1044 | auto ToConceptNameLoc = |
| 1045 | importChecked(Err, From: From->getConceptNameInfo().getLoc()); |
| 1046 | auto ToConceptName = importChecked(Err, From: From->getConceptNameInfo().getName()); |
| 1047 | auto ToFoundDecl = importChecked(Err, From: From->getFoundDecl()); |
| 1048 | auto ToNamedConcept = importChecked(Err, From: From->getNamedConcept()); |
| 1049 | if (Err) |
| 1050 | return std::move(Err); |
| 1051 | TemplateArgumentListInfo ToTAInfo; |
| 1052 | const auto *ASTTemplateArgs = From->getTemplateArgsAsWritten(); |
| 1053 | if (ASTTemplateArgs) |
| 1054 | if (Error Err = ImportTemplateArgumentListInfo(From: *ASTTemplateArgs, Result&: ToTAInfo)) |
| 1055 | return std::move(Err); |
| 1056 | auto *ConceptRef = ConceptReference::Create( |
| 1057 | C: Importer.getToContext(), NNS: ToNNS, TemplateKWLoc: ToTemplateKWLoc, |
| 1058 | ConceptNameInfo: DeclarationNameInfo(ToConceptName, ToConceptNameLoc), FoundDecl: ToFoundDecl, |
| 1059 | NamedConcept: ToNamedConcept, |
| 1060 | ArgsAsWritten: ASTTemplateArgs ? ASTTemplateArgumentListInfo::Create( |
| 1061 | C: Importer.getToContext(), List: ToTAInfo) |
| 1062 | : nullptr); |
| 1063 | return ConceptRef; |
| 1064 | } |
| 1065 | |
| 1066 | StringRef ASTNodeImporter::ImportASTStringRef(StringRef FromStr) { |
| 1067 | char *ToStore = new (Importer.getToContext()) char[FromStr.size()]; |
| 1068 | std::copy(first: FromStr.begin(), last: FromStr.end(), result: ToStore); |
| 1069 | return StringRef(ToStore, FromStr.size()); |
| 1070 | } |
| 1071 | |
| 1072 | Error ASTNodeImporter::ImportConstraintSatisfaction( |
| 1073 | const ASTConstraintSatisfaction &FromSat, ConstraintSatisfaction &ToSat) { |
| 1074 | ToSat.IsSatisfied = FromSat.IsSatisfied; |
| 1075 | ToSat.ContainsErrors = FromSat.ContainsErrors; |
| 1076 | if (!ToSat.IsSatisfied) { |
| 1077 | for (auto Record = FromSat.begin(); Record != FromSat.end(); ++Record) { |
| 1078 | if (const Expr *E = Record->dyn_cast<const Expr *>()) { |
| 1079 | ExpectedExpr ToSecondExpr = import(From: E); |
| 1080 | if (!ToSecondExpr) |
| 1081 | return ToSecondExpr.takeError(); |
| 1082 | ToSat.Details.emplace_back(Args&: ToSecondExpr.get()); |
| 1083 | } else if (auto CR = Record->dyn_cast<const ConceptReference *>()) { |
| 1084 | Expected<ConceptReference *> ToCROrErr = import(From: CR); |
| 1085 | if (!ToCROrErr) |
| 1086 | return ToCROrErr.takeError(); |
| 1087 | ToSat.Details.emplace_back(Args&: ToCROrErr.get()); |
| 1088 | } else { |
| 1089 | auto Pair = |
| 1090 | Record->dyn_cast<const ConstraintSubstitutionDiagnostic *>(); |
| 1091 | |
| 1092 | ExpectedSLoc ToPairFirst = import(From: Pair->first); |
| 1093 | if (!ToPairFirst) |
| 1094 | return ToPairFirst.takeError(); |
| 1095 | StringRef ToPairSecond = ImportASTStringRef(FromStr: Pair->second); |
| 1096 | ToSat.Details.emplace_back(Args: new (Importer.getToContext()) |
| 1097 | ConstraintSubstitutionDiagnostic{ |
| 1098 | ToPairFirst.get(), ToPairSecond}); |
| 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | return Error::success(); |
| 1103 | } |
| 1104 | |
| 1105 | template <> |
| 1106 | Expected<concepts::Requirement::SubstitutionDiagnostic *> |
| 1107 | ASTNodeImporter::import( |
| 1108 | concepts::Requirement::SubstitutionDiagnostic *FromDiag) { |
| 1109 | StringRef ToEntity = ImportASTStringRef(FromStr: FromDiag->SubstitutedEntity); |
| 1110 | ExpectedSLoc ToLoc = import(From: FromDiag->DiagLoc); |
| 1111 | if (!ToLoc) |
| 1112 | return ToLoc.takeError(); |
| 1113 | StringRef ToDiagMessage = ImportASTStringRef(FromStr: FromDiag->DiagMessage); |
| 1114 | return new (Importer.getToContext()) |
| 1115 | concepts::Requirement::SubstitutionDiagnostic{.SubstitutedEntity: ToEntity, .DiagLoc: ToLoc.get(), |
| 1116 | .DiagMessage: ToDiagMessage}; |
| 1117 | } |
| 1118 | |
| 1119 | Expected<concepts::Requirement *> |
| 1120 | ASTNodeImporter::ImportTypeRequirement(concepts::TypeRequirement *From) { |
| 1121 | using namespace concepts; |
| 1122 | |
| 1123 | if (From->isSubstitutionFailure()) { |
| 1124 | auto DiagOrErr = import(FromDiag: From->getSubstitutionDiagnostic()); |
| 1125 | if (!DiagOrErr) |
| 1126 | return DiagOrErr.takeError(); |
| 1127 | return new (Importer.getToContext()) TypeRequirement(*DiagOrErr); |
| 1128 | } else { |
| 1129 | Expected<TypeSourceInfo *> ToType = import(From: From->getType()); |
| 1130 | if (!ToType) |
| 1131 | return ToType.takeError(); |
| 1132 | return new (Importer.getToContext()) TypeRequirement(*ToType); |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | Expected<concepts::Requirement *> |
| 1137 | ASTNodeImporter::ImportExprRequirement(concepts::ExprRequirement *From) { |
| 1138 | using namespace concepts; |
| 1139 | |
| 1140 | bool IsRKSimple = From->getKind() == Requirement::RK_Simple; |
| 1141 | ExprRequirement::SatisfactionStatus Status = From->getSatisfactionStatus(); |
| 1142 | |
| 1143 | std::optional<ExprRequirement::ReturnTypeRequirement> Req; |
| 1144 | ConceptSpecializationExpr *SubstitutedConstraintExpr = nullptr; |
| 1145 | |
| 1146 | if (IsRKSimple) { |
| 1147 | Req.emplace(); |
| 1148 | } else { |
| 1149 | const ExprRequirement::ReturnTypeRequirement &FromTypeRequirement = |
| 1150 | From->getReturnTypeRequirement(); |
| 1151 | |
| 1152 | if (FromTypeRequirement.isTypeConstraint()) { |
| 1153 | const bool IsDependent = FromTypeRequirement.isDependent(); |
| 1154 | auto ParamsOrErr = |
| 1155 | import(From: FromTypeRequirement.getTypeConstraintTemplateParameterList()); |
| 1156 | if (!ParamsOrErr) |
| 1157 | return ParamsOrErr.takeError(); |
| 1158 | if (Status >= ExprRequirement::SS_ConstraintsNotSatisfied) { |
| 1159 | auto SubstConstraintExprOrErr = |
| 1160 | import(From: From->getReturnTypeRequirementSubstitutedConstraintExpr()); |
| 1161 | if (!SubstConstraintExprOrErr) |
| 1162 | return SubstConstraintExprOrErr.takeError(); |
| 1163 | SubstitutedConstraintExpr = SubstConstraintExprOrErr.get(); |
| 1164 | } |
| 1165 | Req.emplace(args&: ParamsOrErr.get(), args: IsDependent); |
| 1166 | } else if (FromTypeRequirement.isSubstitutionFailure()) { |
| 1167 | auto DiagOrErr = import(FromDiag: FromTypeRequirement.getSubstitutionDiagnostic()); |
| 1168 | if (!DiagOrErr) |
| 1169 | return DiagOrErr.takeError(); |
| 1170 | Req.emplace(args&: DiagOrErr.get()); |
| 1171 | } else { |
| 1172 | Req.emplace(); |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | ExpectedSLoc NoexceptLocOrErr = import(From: From->getNoexceptLoc()); |
| 1177 | if (!NoexceptLocOrErr) |
| 1178 | return NoexceptLocOrErr.takeError(); |
| 1179 | |
| 1180 | if (Status == ExprRequirement::SS_ExprSubstitutionFailure) { |
| 1181 | auto DiagOrErr = import(FromDiag: From->getExprSubstitutionDiagnostic()); |
| 1182 | if (!DiagOrErr) |
| 1183 | return DiagOrErr.takeError(); |
| 1184 | return new (Importer.getToContext()) ExprRequirement( |
| 1185 | *DiagOrErr, IsRKSimple, *NoexceptLocOrErr, std::move(*Req)); |
| 1186 | } else { |
| 1187 | Expected<Expr *> ExprOrErr = import(From: From->getExpr()); |
| 1188 | if (!ExprOrErr) |
| 1189 | return ExprOrErr.takeError(); |
| 1190 | return new (Importer.getToContext()) concepts::ExprRequirement( |
| 1191 | *ExprOrErr, IsRKSimple, *NoexceptLocOrErr, std::move(*Req), Status, |
| 1192 | SubstitutedConstraintExpr); |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | Expected<concepts::Requirement *> |
| 1197 | ASTNodeImporter::ImportNestedRequirement(concepts::NestedRequirement *From) { |
| 1198 | using namespace concepts; |
| 1199 | |
| 1200 | const ASTConstraintSatisfaction &FromSatisfaction = |
| 1201 | From->getConstraintSatisfaction(); |
| 1202 | if (From->hasInvalidConstraint()) { |
| 1203 | StringRef ToEntity = ImportASTStringRef(FromStr: From->getInvalidConstraintEntity()); |
| 1204 | ASTConstraintSatisfaction *ToSatisfaction = |
| 1205 | ASTConstraintSatisfaction::Rebuild(C: Importer.getToContext(), |
| 1206 | Satisfaction: FromSatisfaction); |
| 1207 | return new (Importer.getToContext()) |
| 1208 | NestedRequirement(ToEntity, ToSatisfaction); |
| 1209 | } else { |
| 1210 | ExpectedExpr ToExpr = import(From: From->getConstraintExpr()); |
| 1211 | if (!ToExpr) |
| 1212 | return ToExpr.takeError(); |
| 1213 | if (ToExpr.get()->isInstantiationDependent()) { |
| 1214 | return new (Importer.getToContext()) NestedRequirement(ToExpr.get()); |
| 1215 | } else { |
| 1216 | ConstraintSatisfaction Satisfaction; |
| 1217 | if (Error Err = |
| 1218 | ImportConstraintSatisfaction(FromSat: FromSatisfaction, ToSat&: Satisfaction)) |
| 1219 | return std::move(Err); |
| 1220 | return new (Importer.getToContext()) NestedRequirement( |
| 1221 | Importer.getToContext(), ToExpr.get(), Satisfaction); |
| 1222 | } |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | template <> |
| 1227 | Expected<concepts::Requirement *> |
| 1228 | ASTNodeImporter::import(concepts::Requirement *FromRequire) { |
| 1229 | switch (FromRequire->getKind()) { |
| 1230 | case concepts::Requirement::RequirementKind::RK_Type: |
| 1231 | return ImportTypeRequirement(From: cast<concepts::TypeRequirement>(Val: FromRequire)); |
| 1232 | case concepts::Requirement::RequirementKind::RK_Compound: |
| 1233 | case concepts::Requirement::RequirementKind::RK_Simple: |
| 1234 | return ImportExprRequirement(From: cast<concepts::ExprRequirement>(Val: FromRequire)); |
| 1235 | case concepts::Requirement::RequirementKind::RK_Nested: |
| 1236 | return ImportNestedRequirement( |
| 1237 | From: cast<concepts::NestedRequirement>(Val: FromRequire)); |
| 1238 | } |
| 1239 | llvm_unreachable("Unhandled requirement kind" ); |
| 1240 | } |
| 1241 | |
| 1242 | template <> |
| 1243 | Expected<LambdaCapture> ASTNodeImporter::import(const LambdaCapture &From) { |
| 1244 | ValueDecl *Var = nullptr; |
| 1245 | if (From.capturesVariable()) { |
| 1246 | if (auto VarOrErr = import(From: From.getCapturedVar())) |
| 1247 | Var = *VarOrErr; |
| 1248 | else |
| 1249 | return VarOrErr.takeError(); |
| 1250 | } |
| 1251 | |
| 1252 | auto LocationOrErr = import(From: From.getLocation()); |
| 1253 | if (!LocationOrErr) |
| 1254 | return LocationOrErr.takeError(); |
| 1255 | |
| 1256 | SourceLocation EllipsisLoc; |
| 1257 | if (From.isPackExpansion()) |
| 1258 | if (Error Err = importInto(To&: EllipsisLoc, From: From.getEllipsisLoc())) |
| 1259 | return std::move(Err); |
| 1260 | |
| 1261 | return LambdaCapture( |
| 1262 | *LocationOrErr, From.isImplicit(), From.getCaptureKind(), Var, |
| 1263 | EllipsisLoc); |
| 1264 | } |
| 1265 | |
| 1266 | template <typename T> |
| 1267 | bool ASTNodeImporter::hasSameVisibilityContextAndLinkage(T *Found, T *From) { |
| 1268 | if (Found->getLinkageInternal() != From->getLinkageInternal()) |
| 1269 | return false; |
| 1270 | |
| 1271 | if (From->hasExternalFormalLinkage()) |
| 1272 | return Found->hasExternalFormalLinkage(); |
| 1273 | if (Importer.GetFromTU(ToD: Found) != From->getTranslationUnitDecl()) |
| 1274 | return false; |
| 1275 | if (From->isInAnonymousNamespace()) |
| 1276 | return Found->isInAnonymousNamespace(); |
| 1277 | else |
| 1278 | return !Found->isInAnonymousNamespace() && |
| 1279 | !Found->hasExternalFormalLinkage(); |
| 1280 | } |
| 1281 | |
| 1282 | template <> |
| 1283 | bool ASTNodeImporter::hasSameVisibilityContextAndLinkage(TypedefNameDecl *Found, |
| 1284 | TypedefNameDecl *From) { |
| 1285 | if (Found->getLinkageInternal() != From->getLinkageInternal()) |
| 1286 | return false; |
| 1287 | |
| 1288 | if (From->isInAnonymousNamespace() && Found->isInAnonymousNamespace()) |
| 1289 | return Importer.GetFromTU(ToD: Found) == From->getTranslationUnitDecl(); |
| 1290 | return From->isInAnonymousNamespace() == Found->isInAnonymousNamespace(); |
| 1291 | } |
| 1292 | |
| 1293 | } // namespace clang |
| 1294 | |
| 1295 | //---------------------------------------------------------------------------- |
| 1296 | // Import Types |
| 1297 | //---------------------------------------------------------------------------- |
| 1298 | |
| 1299 | using namespace clang; |
| 1300 | |
| 1301 | auto ASTImporter::FunctionDeclImportCycleDetector::makeScopedCycleDetection( |
| 1302 | const FunctionDecl *D) { |
| 1303 | const FunctionDecl *LambdaD = nullptr; |
| 1304 | if (!isCycle(D) && D) { |
| 1305 | FunctionDeclsWithImportInProgress.insert(V: D); |
| 1306 | LambdaD = D; |
| 1307 | } |
| 1308 | return llvm::scope_exit([this, LambdaD]() { |
| 1309 | if (LambdaD) { |
| 1310 | FunctionDeclsWithImportInProgress.erase(V: LambdaD); |
| 1311 | } |
| 1312 | }); |
| 1313 | } |
| 1314 | |
| 1315 | bool ASTImporter::FunctionDeclImportCycleDetector::isCycle( |
| 1316 | const FunctionDecl *D) const { |
| 1317 | return FunctionDeclsWithImportInProgress.find(V: D) != |
| 1318 | FunctionDeclsWithImportInProgress.end(); |
| 1319 | } |
| 1320 | |
| 1321 | ExpectedType ASTNodeImporter::VisitType(const Type *T) { |
| 1322 | Importer.FromDiag(Loc: SourceLocation(), DiagID: diag::err_unsupported_ast_node) |
| 1323 | << T->getTypeClassName(); |
| 1324 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
| 1325 | } |
| 1326 | |
| 1327 | ExpectedType ASTNodeImporter::VisitAtomicType(const AtomicType *T){ |
| 1328 | ExpectedType UnderlyingTypeOrErr = import(From: T->getValueType()); |
| 1329 | if (!UnderlyingTypeOrErr) |
| 1330 | return UnderlyingTypeOrErr.takeError(); |
| 1331 | |
| 1332 | return Importer.getToContext().getAtomicType(T: *UnderlyingTypeOrErr); |
| 1333 | } |
| 1334 | |
| 1335 | ExpectedType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) { |
| 1336 | switch (T->getKind()) { |
| 1337 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 1338 | case BuiltinType::Id: \ |
| 1339 | return Importer.getToContext().SingletonId; |
| 1340 | #include "clang/Basic/OpenCLImageTypes.def" |
| 1341 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
| 1342 | case BuiltinType::Id: \ |
| 1343 | return Importer.getToContext().Id##Ty; |
| 1344 | #include "clang/Basic/OpenCLExtensionTypes.def" |
| 1345 | #define SVE_TYPE(Name, Id, SingletonId) \ |
| 1346 | case BuiltinType::Id: \ |
| 1347 | return Importer.getToContext().SingletonId; |
| 1348 | #include "clang/Basic/AArch64ACLETypes.def" |
| 1349 | #define PPC_VECTOR_TYPE(Name, Id, Size) \ |
| 1350 | case BuiltinType::Id: \ |
| 1351 | return Importer.getToContext().Id##Ty; |
| 1352 | #include "clang/Basic/PPCTypes.def" |
| 1353 | #define RVV_TYPE(Name, Id, SingletonId) \ |
| 1354 | case BuiltinType::Id: \ |
| 1355 | return Importer.getToContext().SingletonId; |
| 1356 | #include "clang/Basic/RISCVVTypes.def" |
| 1357 | #define WASM_TYPE(Name, Id, SingletonId) \ |
| 1358 | case BuiltinType::Id: \ |
| 1359 | return Importer.getToContext().SingletonId; |
| 1360 | #include "clang/Basic/WebAssemblyReferenceTypes.def" |
| 1361 | #define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) \ |
| 1362 | case BuiltinType::Id: \ |
| 1363 | return Importer.getToContext().SingletonId; |
| 1364 | #include "clang/Basic/AMDGPUTypes.def" |
| 1365 | #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \ |
| 1366 | case BuiltinType::Id: \ |
| 1367 | return Importer.getToContext().SingletonId; |
| 1368 | #include "clang/Basic/HLSLIntangibleTypes.def" |
| 1369 | #define SHARED_SINGLETON_TYPE(Expansion) |
| 1370 | #define BUILTIN_TYPE(Id, SingletonId) \ |
| 1371 | case BuiltinType::Id: return Importer.getToContext().SingletonId; |
| 1372 | #include "clang/AST/BuiltinTypes.def" |
| 1373 | |
| 1374 | // FIXME: for Char16, Char32, and NullPtr, make sure that the "to" |
| 1375 | // context supports C++. |
| 1376 | |
| 1377 | // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to" |
| 1378 | // context supports ObjC. |
| 1379 | |
| 1380 | case BuiltinType::Char_U: |
| 1381 | // The context we're importing from has an unsigned 'char'. If we're |
| 1382 | // importing into a context with a signed 'char', translate to |
| 1383 | // 'unsigned char' instead. |
| 1384 | if (Importer.getToContext().getLangOpts().CharIsSigned) |
| 1385 | return Importer.getToContext().UnsignedCharTy; |
| 1386 | |
| 1387 | return Importer.getToContext().CharTy; |
| 1388 | |
| 1389 | case BuiltinType::Char_S: |
| 1390 | // The context we're importing from has an unsigned 'char'. If we're |
| 1391 | // importing into a context with a signed 'char', translate to |
| 1392 | // 'unsigned char' instead. |
| 1393 | if (!Importer.getToContext().getLangOpts().CharIsSigned) |
| 1394 | return Importer.getToContext().SignedCharTy; |
| 1395 | |
| 1396 | return Importer.getToContext().CharTy; |
| 1397 | |
| 1398 | case BuiltinType::WChar_S: |
| 1399 | case BuiltinType::WChar_U: |
| 1400 | // FIXME: If not in C++, shall we translate to the C equivalent of |
| 1401 | // wchar_t? |
| 1402 | return Importer.getToContext().WCharTy; |
| 1403 | } |
| 1404 | |
| 1405 | llvm_unreachable("Invalid BuiltinType Kind!" ); |
| 1406 | } |
| 1407 | |
| 1408 | ExpectedType ASTNodeImporter::VisitDecayedType(const DecayedType *T) { |
| 1409 | ExpectedType ToOriginalTypeOrErr = import(From: T->getOriginalType()); |
| 1410 | if (!ToOriginalTypeOrErr) |
| 1411 | return ToOriginalTypeOrErr.takeError(); |
| 1412 | |
| 1413 | return Importer.getToContext().getDecayedType(T: *ToOriginalTypeOrErr); |
| 1414 | } |
| 1415 | |
| 1416 | ExpectedType ASTNodeImporter::VisitComplexType(const ComplexType *T) { |
| 1417 | ExpectedType ToElementTypeOrErr = import(From: T->getElementType()); |
| 1418 | if (!ToElementTypeOrErr) |
| 1419 | return ToElementTypeOrErr.takeError(); |
| 1420 | |
| 1421 | return Importer.getToContext().getComplexType(T: *ToElementTypeOrErr); |
| 1422 | } |
| 1423 | |
| 1424 | ExpectedType ASTNodeImporter::VisitPointerType(const PointerType *T) { |
| 1425 | ExpectedType ToPointeeTypeOrErr = import(From: T->getPointeeType()); |
| 1426 | if (!ToPointeeTypeOrErr) |
| 1427 | return ToPointeeTypeOrErr.takeError(); |
| 1428 | |
| 1429 | return Importer.getToContext().getPointerType(T: *ToPointeeTypeOrErr); |
| 1430 | } |
| 1431 | |
| 1432 | ExpectedType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) { |
| 1433 | // FIXME: Check for blocks support in "to" context. |
| 1434 | ExpectedType ToPointeeTypeOrErr = import(From: T->getPointeeType()); |
| 1435 | if (!ToPointeeTypeOrErr) |
| 1436 | return ToPointeeTypeOrErr.takeError(); |
| 1437 | |
| 1438 | return Importer.getToContext().getBlockPointerType(T: *ToPointeeTypeOrErr); |
| 1439 | } |
| 1440 | |
| 1441 | ExpectedType |
| 1442 | ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) { |
| 1443 | // FIXME: Check for C++ support in "to" context. |
| 1444 | ExpectedType ToPointeeTypeOrErr = import(From: T->getPointeeTypeAsWritten()); |
| 1445 | if (!ToPointeeTypeOrErr) |
| 1446 | return ToPointeeTypeOrErr.takeError(); |
| 1447 | |
| 1448 | return Importer.getToContext().getLValueReferenceType(T: *ToPointeeTypeOrErr); |
| 1449 | } |
| 1450 | |
| 1451 | ExpectedType |
| 1452 | ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) { |
| 1453 | // FIXME: Check for C++0x support in "to" context. |
| 1454 | ExpectedType ToPointeeTypeOrErr = import(From: T->getPointeeTypeAsWritten()); |
| 1455 | if (!ToPointeeTypeOrErr) |
| 1456 | return ToPointeeTypeOrErr.takeError(); |
| 1457 | |
| 1458 | return Importer.getToContext().getRValueReferenceType(T: *ToPointeeTypeOrErr); |
| 1459 | } |
| 1460 | |
| 1461 | ExpectedType |
| 1462 | ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) { |
| 1463 | // FIXME: Check for C++ support in "to" context. |
| 1464 | ExpectedType ToPointeeTypeOrErr = import(From: T->getPointeeType()); |
| 1465 | if (!ToPointeeTypeOrErr) |
| 1466 | return ToPointeeTypeOrErr.takeError(); |
| 1467 | |
| 1468 | auto QualifierOrErr = import(From: T->getQualifier()); |
| 1469 | if (!QualifierOrErr) |
| 1470 | return QualifierOrErr.takeError(); |
| 1471 | |
| 1472 | auto ClsOrErr = import(From: T->getMostRecentCXXRecordDecl()); |
| 1473 | if (!ClsOrErr) |
| 1474 | return ClsOrErr.takeError(); |
| 1475 | |
| 1476 | return Importer.getToContext().getMemberPointerType( |
| 1477 | T: *ToPointeeTypeOrErr, Qualifier: *QualifierOrErr, Cls: *ClsOrErr); |
| 1478 | } |
| 1479 | |
| 1480 | ExpectedType |
| 1481 | ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) { |
| 1482 | Error Err = Error::success(); |
| 1483 | auto ToElementType = importChecked(Err, From: T->getElementType()); |
| 1484 | auto ToSizeExpr = importChecked(Err, From: T->getSizeExpr()); |
| 1485 | if (Err) |
| 1486 | return std::move(Err); |
| 1487 | |
| 1488 | return Importer.getToContext().getConstantArrayType( |
| 1489 | EltTy: ToElementType, ArySize: T->getSize(), SizeExpr: ToSizeExpr, ASM: T->getSizeModifier(), |
| 1490 | IndexTypeQuals: T->getIndexTypeCVRQualifiers()); |
| 1491 | } |
| 1492 | |
| 1493 | ExpectedType |
| 1494 | ASTNodeImporter::VisitArrayParameterType(const ArrayParameterType *T) { |
| 1495 | ExpectedType ToArrayTypeOrErr = VisitConstantArrayType(T); |
| 1496 | if (!ToArrayTypeOrErr) |
| 1497 | return ToArrayTypeOrErr.takeError(); |
| 1498 | |
| 1499 | return Importer.getToContext().getArrayParameterType(Ty: *ToArrayTypeOrErr); |
| 1500 | } |
| 1501 | |
| 1502 | ExpectedType |
| 1503 | ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) { |
| 1504 | ExpectedType ToElementTypeOrErr = import(From: T->getElementType()); |
| 1505 | if (!ToElementTypeOrErr) |
| 1506 | return ToElementTypeOrErr.takeError(); |
| 1507 | |
| 1508 | return Importer.getToContext().getIncompleteArrayType(EltTy: *ToElementTypeOrErr, |
| 1509 | ASM: T->getSizeModifier(), |
| 1510 | IndexTypeQuals: T->getIndexTypeCVRQualifiers()); |
| 1511 | } |
| 1512 | |
| 1513 | ExpectedType |
| 1514 | ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) { |
| 1515 | Error Err = Error::success(); |
| 1516 | QualType ToElementType = importChecked(Err, From: T->getElementType()); |
| 1517 | Expr *ToSizeExpr = importChecked(Err, From: T->getSizeExpr()); |
| 1518 | if (Err) |
| 1519 | return std::move(Err); |
| 1520 | return Importer.getToContext().getVariableArrayType( |
| 1521 | EltTy: ToElementType, NumElts: ToSizeExpr, ASM: T->getSizeModifier(), |
| 1522 | IndexTypeQuals: T->getIndexTypeCVRQualifiers()); |
| 1523 | } |
| 1524 | |
| 1525 | ExpectedType ASTNodeImporter::VisitDependentSizedArrayType( |
| 1526 | const DependentSizedArrayType *T) { |
| 1527 | Error Err = Error::success(); |
| 1528 | QualType ToElementType = importChecked(Err, From: T->getElementType()); |
| 1529 | Expr *ToSizeExpr = importChecked(Err, From: T->getSizeExpr()); |
| 1530 | if (Err) |
| 1531 | return std::move(Err); |
| 1532 | // SizeExpr may be null if size is not specified directly. |
| 1533 | // For example, 'int a[]'. |
| 1534 | |
| 1535 | return Importer.getToContext().getDependentSizedArrayType( |
| 1536 | EltTy: ToElementType, NumElts: ToSizeExpr, ASM: T->getSizeModifier(), |
| 1537 | IndexTypeQuals: T->getIndexTypeCVRQualifiers()); |
| 1538 | } |
| 1539 | |
| 1540 | ExpectedType ASTNodeImporter::VisitDependentSizedExtVectorType( |
| 1541 | const DependentSizedExtVectorType *T) { |
| 1542 | Error Err = Error::success(); |
| 1543 | QualType ToElementType = importChecked(Err, From: T->getElementType()); |
| 1544 | Expr *ToSizeExpr = importChecked(Err, From: T->getSizeExpr()); |
| 1545 | SourceLocation ToAttrLoc = importChecked(Err, From: T->getAttributeLoc()); |
| 1546 | if (Err) |
| 1547 | return std::move(Err); |
| 1548 | return Importer.getToContext().getDependentSizedExtVectorType( |
| 1549 | VectorType: ToElementType, SizeExpr: ToSizeExpr, AttrLoc: ToAttrLoc); |
| 1550 | } |
| 1551 | |
| 1552 | ExpectedType ASTNodeImporter::VisitVectorType(const VectorType *T) { |
| 1553 | ExpectedType ToElementTypeOrErr = import(From: T->getElementType()); |
| 1554 | if (!ToElementTypeOrErr) |
| 1555 | return ToElementTypeOrErr.takeError(); |
| 1556 | |
| 1557 | return Importer.getToContext().getVectorType(VectorType: *ToElementTypeOrErr, |
| 1558 | NumElts: T->getNumElements(), |
| 1559 | VecKind: T->getVectorKind()); |
| 1560 | } |
| 1561 | |
| 1562 | ExpectedType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) { |
| 1563 | ExpectedType ToElementTypeOrErr = import(From: T->getElementType()); |
| 1564 | if (!ToElementTypeOrErr) |
| 1565 | return ToElementTypeOrErr.takeError(); |
| 1566 | |
| 1567 | return Importer.getToContext().getExtVectorType(VectorType: *ToElementTypeOrErr, |
| 1568 | NumElts: T->getNumElements()); |
| 1569 | } |
| 1570 | |
| 1571 | ExpectedType |
| 1572 | ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { |
| 1573 | // FIXME: What happens if we're importing a function without a prototype |
| 1574 | // into C++? Should we make it variadic? |
| 1575 | ExpectedType ToReturnTypeOrErr = import(From: T->getReturnType()); |
| 1576 | if (!ToReturnTypeOrErr) |
| 1577 | return ToReturnTypeOrErr.takeError(); |
| 1578 | |
| 1579 | return Importer.getToContext().getFunctionNoProtoType(ResultTy: *ToReturnTypeOrErr, |
| 1580 | Info: T->getExtInfo()); |
| 1581 | } |
| 1582 | |
| 1583 | ExpectedType |
| 1584 | ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) { |
| 1585 | ExpectedType ToReturnTypeOrErr = import(From: T->getReturnType()); |
| 1586 | if (!ToReturnTypeOrErr) |
| 1587 | return ToReturnTypeOrErr.takeError(); |
| 1588 | |
| 1589 | // Import argument types |
| 1590 | SmallVector<QualType, 4> ArgTypes; |
| 1591 | for (const auto &A : T->param_types()) { |
| 1592 | ExpectedType TyOrErr = import(From: A); |
| 1593 | if (!TyOrErr) |
| 1594 | return TyOrErr.takeError(); |
| 1595 | ArgTypes.push_back(Elt: *TyOrErr); |
| 1596 | } |
| 1597 | |
| 1598 | // Import exception types |
| 1599 | SmallVector<QualType, 4> ExceptionTypes; |
| 1600 | for (const auto &E : T->exceptions()) { |
| 1601 | ExpectedType TyOrErr = import(From: E); |
| 1602 | if (!TyOrErr) |
| 1603 | return TyOrErr.takeError(); |
| 1604 | ExceptionTypes.push_back(Elt: *TyOrErr); |
| 1605 | } |
| 1606 | |
| 1607 | FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo(); |
| 1608 | Error Err = Error::success(); |
| 1609 | FunctionProtoType::ExtProtoInfo ToEPI; |
| 1610 | ToEPI.ExtInfo = FromEPI.ExtInfo; |
| 1611 | ToEPI.Variadic = FromEPI.Variadic; |
| 1612 | ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn; |
| 1613 | ToEPI.TypeQuals = FromEPI.TypeQuals; |
| 1614 | ToEPI.RefQualifier = FromEPI.RefQualifier; |
| 1615 | ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type; |
| 1616 | ToEPI.ExceptionSpec.NoexceptExpr = |
| 1617 | importChecked(Err, From: FromEPI.ExceptionSpec.NoexceptExpr); |
| 1618 | ToEPI.ExceptionSpec.SourceDecl = |
| 1619 | importChecked(Err, From: FromEPI.ExceptionSpec.SourceDecl); |
| 1620 | ToEPI.ExceptionSpec.SourceTemplate = |
| 1621 | importChecked(Err, From: FromEPI.ExceptionSpec.SourceTemplate); |
| 1622 | ToEPI.ExceptionSpec.Exceptions = ExceptionTypes; |
| 1623 | |
| 1624 | if (Err) |
| 1625 | return std::move(Err); |
| 1626 | |
| 1627 | return Importer.getToContext().getFunctionType( |
| 1628 | ResultTy: *ToReturnTypeOrErr, Args: ArgTypes, EPI: ToEPI); |
| 1629 | } |
| 1630 | |
| 1631 | ExpectedType ASTNodeImporter::VisitUnresolvedUsingType( |
| 1632 | const UnresolvedUsingType *T) { |
| 1633 | Error Err = Error::success(); |
| 1634 | auto ToQualifier = importChecked(Err, From: T->getQualifier()); |
| 1635 | auto *ToD = importChecked(Err, From: T->getDecl()); |
| 1636 | if (Err) |
| 1637 | return std::move(Err); |
| 1638 | |
| 1639 | if (T->isCanonicalUnqualified()) |
| 1640 | return Importer.getToContext().getCanonicalUnresolvedUsingType(D: ToD); |
| 1641 | return Importer.getToContext().getUnresolvedUsingType(Keyword: T->getKeyword(), |
| 1642 | Qualifier: ToQualifier, D: ToD); |
| 1643 | } |
| 1644 | |
| 1645 | ExpectedType ASTNodeImporter::VisitParenType(const ParenType *T) { |
| 1646 | ExpectedType ToInnerTypeOrErr = import(From: T->getInnerType()); |
| 1647 | if (!ToInnerTypeOrErr) |
| 1648 | return ToInnerTypeOrErr.takeError(); |
| 1649 | |
| 1650 | return Importer.getToContext().getParenType(NamedType: *ToInnerTypeOrErr); |
| 1651 | } |
| 1652 | |
| 1653 | ExpectedType |
| 1654 | ASTNodeImporter::VisitPackIndexingType(clang::PackIndexingType const *T) { |
| 1655 | |
| 1656 | ExpectedType Pattern = import(From: T->getPattern()); |
| 1657 | if (!Pattern) |
| 1658 | return Pattern.takeError(); |
| 1659 | ExpectedExpr Index = import(From: T->getIndexExpr()); |
| 1660 | if (!Index) |
| 1661 | return Index.takeError(); |
| 1662 | return Importer.getToContext().getPackIndexingType(Pattern: *Pattern, IndexExpr: *Index); |
| 1663 | } |
| 1664 | |
| 1665 | ExpectedType ASTNodeImporter::VisitTypedefType(const TypedefType *T) { |
| 1666 | Expected<TypedefNameDecl *> ToDeclOrErr = import(From: T->getDecl()); |
| 1667 | if (!ToDeclOrErr) |
| 1668 | return ToDeclOrErr.takeError(); |
| 1669 | |
| 1670 | auto ToQualifierOrErr = import(From: T->getQualifier()); |
| 1671 | if (!ToQualifierOrErr) |
| 1672 | return ToQualifierOrErr.takeError(); |
| 1673 | |
| 1674 | ExpectedType ToUnderlyingTypeOrErr = |
| 1675 | T->typeMatchesDecl() ? QualType() : import(From: T->desugar()); |
| 1676 | if (!ToUnderlyingTypeOrErr) |
| 1677 | return ToUnderlyingTypeOrErr.takeError(); |
| 1678 | |
| 1679 | return Importer.getToContext().getTypedefType( |
| 1680 | Keyword: T->getKeyword(), Qualifier: *ToQualifierOrErr, Decl: *ToDeclOrErr, UnderlyingType: *ToUnderlyingTypeOrErr); |
| 1681 | } |
| 1682 | |
| 1683 | ExpectedType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) { |
| 1684 | ExpectedExpr ToExprOrErr = import(From: T->getUnderlyingExpr()); |
| 1685 | if (!ToExprOrErr) |
| 1686 | return ToExprOrErr.takeError(); |
| 1687 | return Importer.getToContext().getTypeOfExprType(E: *ToExprOrErr, Kind: T->getKind()); |
| 1688 | } |
| 1689 | |
| 1690 | ExpectedType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) { |
| 1691 | ExpectedType ToUnderlyingTypeOrErr = import(From: T->getUnmodifiedType()); |
| 1692 | if (!ToUnderlyingTypeOrErr) |
| 1693 | return ToUnderlyingTypeOrErr.takeError(); |
| 1694 | return Importer.getToContext().getTypeOfType(QT: *ToUnderlyingTypeOrErr, |
| 1695 | Kind: T->getKind()); |
| 1696 | } |
| 1697 | |
| 1698 | ExpectedType ASTNodeImporter::VisitUsingType(const UsingType *T) { |
| 1699 | Error Err = Error::success(); |
| 1700 | auto ToQualifier = importChecked(Err, From: T->getQualifier()); |
| 1701 | auto *ToD = importChecked(Err, From: T->getDecl()); |
| 1702 | QualType ToT = importChecked(Err, From: T->desugar()); |
| 1703 | if (Err) |
| 1704 | return std::move(Err); |
| 1705 | return Importer.getToContext().getUsingType(Keyword: T->getKeyword(), Qualifier: ToQualifier, D: ToD, |
| 1706 | UnderlyingType: ToT); |
| 1707 | } |
| 1708 | |
| 1709 | ExpectedType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) { |
| 1710 | // FIXME: Make sure that the "to" context supports C++0x! |
| 1711 | ExpectedExpr ToExprOrErr = import(From: T->getUnderlyingExpr()); |
| 1712 | if (!ToExprOrErr) |
| 1713 | return ToExprOrErr.takeError(); |
| 1714 | |
| 1715 | ExpectedType ToUnderlyingTypeOrErr = import(From: T->getUnderlyingType()); |
| 1716 | if (!ToUnderlyingTypeOrErr) |
| 1717 | return ToUnderlyingTypeOrErr.takeError(); |
| 1718 | |
| 1719 | return Importer.getToContext().getDecltypeType( |
| 1720 | e: *ToExprOrErr, UnderlyingType: *ToUnderlyingTypeOrErr); |
| 1721 | } |
| 1722 | |
| 1723 | ExpectedType |
| 1724 | ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) { |
| 1725 | ExpectedType ToBaseTypeOrErr = import(From: T->getBaseType()); |
| 1726 | if (!ToBaseTypeOrErr) |
| 1727 | return ToBaseTypeOrErr.takeError(); |
| 1728 | |
| 1729 | ExpectedType ToUnderlyingTypeOrErr = import(From: T->getUnderlyingType()); |
| 1730 | if (!ToUnderlyingTypeOrErr) |
| 1731 | return ToUnderlyingTypeOrErr.takeError(); |
| 1732 | |
| 1733 | return Importer.getToContext().getUnaryTransformType( |
| 1734 | BaseType: *ToBaseTypeOrErr, UnderlyingType: *ToUnderlyingTypeOrErr, UKind: T->getUTTKind()); |
| 1735 | } |
| 1736 | |
| 1737 | ExpectedType ASTNodeImporter::VisitAutoType(const AutoType *T) { |
| 1738 | // FIXME: Make sure that the "to" context supports C++11! |
| 1739 | ExpectedType ToDeducedTypeOrErr = import(From: T->getDeducedType()); |
| 1740 | if (!ToDeducedTypeOrErr) |
| 1741 | return ToDeducedTypeOrErr.takeError(); |
| 1742 | |
| 1743 | Expected<TemplateDecl *> ToTypeConstraint = |
| 1744 | import(From: T->getTypeConstraintConcept()); |
| 1745 | if (!ToTypeConstraint) |
| 1746 | return ToTypeConstraint.takeError(); |
| 1747 | |
| 1748 | SmallVector<TemplateArgument, 2> ToTemplateArgs; |
| 1749 | if (Error Err = ImportTemplateArguments(FromArgs: T->getTypeConstraintArguments(), |
| 1750 | ToArgs&: ToTemplateArgs)) |
| 1751 | return std::move(Err); |
| 1752 | |
| 1753 | return Importer.getToContext().getAutoType( |
| 1754 | DK: T->getDeducedKind(), DeducedAsType: *ToDeducedTypeOrErr, Keyword: T->getKeyword(), |
| 1755 | TypeConstraintConcept: *ToTypeConstraint, TypeConstraintArgs: ToTemplateArgs); |
| 1756 | } |
| 1757 | |
| 1758 | ExpectedType ASTNodeImporter::VisitDeducedTemplateSpecializationType( |
| 1759 | const DeducedTemplateSpecializationType *T) { |
| 1760 | // FIXME: Make sure that the "to" context supports C++17! |
| 1761 | Expected<TemplateName> ToTemplateNameOrErr = import(From: T->getTemplateName()); |
| 1762 | if (!ToTemplateNameOrErr) |
| 1763 | return ToTemplateNameOrErr.takeError(); |
| 1764 | ExpectedType ToDeducedTypeOrErr = import(From: T->getDeducedType()); |
| 1765 | if (!ToDeducedTypeOrErr) |
| 1766 | return ToDeducedTypeOrErr.takeError(); |
| 1767 | |
| 1768 | return Importer.getToContext().getDeducedTemplateSpecializationType( |
| 1769 | DK: T->getDeducedKind(), DeducedAsType: *ToDeducedTypeOrErr, Keyword: T->getKeyword(), |
| 1770 | Template: *ToTemplateNameOrErr); |
| 1771 | } |
| 1772 | |
| 1773 | ExpectedType ASTNodeImporter::VisitTagType(const TagType *T) { |
| 1774 | TagDecl *DeclForType = T->getDecl(); |
| 1775 | Expected<TagDecl *> ToDeclOrErr = import(From: DeclForType); |
| 1776 | if (!ToDeclOrErr) |
| 1777 | return ToDeclOrErr.takeError(); |
| 1778 | |
| 1779 | // If there is a definition of the 'OriginalDecl', it should be imported to |
| 1780 | // have all information for the type in the "To" AST. (In some cases no |
| 1781 | // other reference may exist to the definition decl and it would not be |
| 1782 | // imported otherwise.) |
| 1783 | Expected<TagDecl *> ToDefDeclOrErr = import(From: DeclForType->getDefinition()); |
| 1784 | if (!ToDefDeclOrErr) |
| 1785 | return ToDefDeclOrErr.takeError(); |
| 1786 | |
| 1787 | if (T->isCanonicalUnqualified()) |
| 1788 | return Importer.getToContext().getCanonicalTagType(TD: *ToDeclOrErr); |
| 1789 | |
| 1790 | auto ToQualifierOrErr = import(From: T->getQualifier()); |
| 1791 | if (!ToQualifierOrErr) |
| 1792 | return ToQualifierOrErr.takeError(); |
| 1793 | |
| 1794 | return Importer.getToContext().getTagType(Keyword: T->getKeyword(), Qualifier: *ToQualifierOrErr, |
| 1795 | TD: *ToDeclOrErr, OwnsTag: T->isTagOwned()); |
| 1796 | } |
| 1797 | |
| 1798 | ExpectedType ASTNodeImporter::VisitEnumType(const EnumType *T) { |
| 1799 | return VisitTagType(T); |
| 1800 | } |
| 1801 | |
| 1802 | ExpectedType ASTNodeImporter::VisitRecordType(const RecordType *T) { |
| 1803 | return VisitTagType(T); |
| 1804 | } |
| 1805 | |
| 1806 | ExpectedType |
| 1807 | ASTNodeImporter::VisitInjectedClassNameType(const InjectedClassNameType *T) { |
| 1808 | return VisitTagType(T); |
| 1809 | } |
| 1810 | |
| 1811 | ExpectedType ASTNodeImporter::VisitAttributedType(const AttributedType *T) { |
| 1812 | ExpectedType ToModifiedTypeOrErr = import(From: T->getModifiedType()); |
| 1813 | if (!ToModifiedTypeOrErr) |
| 1814 | return ToModifiedTypeOrErr.takeError(); |
| 1815 | ExpectedType ToEquivalentTypeOrErr = import(From: T->getEquivalentType()); |
| 1816 | if (!ToEquivalentTypeOrErr) |
| 1817 | return ToEquivalentTypeOrErr.takeError(); |
| 1818 | |
| 1819 | return Importer.getToContext().getAttributedType( |
| 1820 | attrKind: T->getAttrKind(), modifiedType: *ToModifiedTypeOrErr, equivalentType: *ToEquivalentTypeOrErr, |
| 1821 | attr: T->getAttr()); |
| 1822 | } |
| 1823 | |
| 1824 | ExpectedType |
| 1825 | ASTNodeImporter::VisitCountAttributedType(const CountAttributedType *T) { |
| 1826 | ExpectedType ToWrappedTypeOrErr = import(From: T->desugar()); |
| 1827 | if (!ToWrappedTypeOrErr) |
| 1828 | return ToWrappedTypeOrErr.takeError(); |
| 1829 | |
| 1830 | Error Err = Error::success(); |
| 1831 | Expr *CountExpr = importChecked(Err, From: T->getCountExpr()); |
| 1832 | |
| 1833 | SmallVector<TypeCoupledDeclRefInfo, 1> CoupledDecls; |
| 1834 | for (const TypeCoupledDeclRefInfo &TI : T->dependent_decls()) { |
| 1835 | Expected<ValueDecl *> ToDeclOrErr = import(From: TI.getDecl()); |
| 1836 | if (!ToDeclOrErr) |
| 1837 | return ToDeclOrErr.takeError(); |
| 1838 | CoupledDecls.emplace_back(Args&: *ToDeclOrErr, Args: TI.isDeref()); |
| 1839 | } |
| 1840 | |
| 1841 | return Importer.getToContext().getCountAttributedType( |
| 1842 | T: *ToWrappedTypeOrErr, CountExpr, CountInBytes: T->isCountInBytes(), OrNull: T->isOrNull(), |
| 1843 | DependentDecls: ArrayRef(CoupledDecls)); |
| 1844 | } |
| 1845 | |
| 1846 | ExpectedType ASTNodeImporter::VisitTemplateTypeParmType( |
| 1847 | const TemplateTypeParmType *T) { |
| 1848 | Expected<TemplateTypeParmDecl *> ToDeclOrErr = import(From: T->getDecl()); |
| 1849 | if (!ToDeclOrErr) |
| 1850 | return ToDeclOrErr.takeError(); |
| 1851 | |
| 1852 | return Importer.getToContext().getTemplateTypeParmType( |
| 1853 | Depth: T->getDepth(), Index: T->getIndex(), ParameterPack: T->isParameterPack(), ParmDecl: *ToDeclOrErr); |
| 1854 | } |
| 1855 | |
| 1856 | ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmType( |
| 1857 | const SubstTemplateTypeParmType *T) { |
| 1858 | Expected<Decl *> ReplacedOrErr = import(From: T->getAssociatedDecl()); |
| 1859 | if (!ReplacedOrErr) |
| 1860 | return ReplacedOrErr.takeError(); |
| 1861 | |
| 1862 | ExpectedType ToReplacementTypeOrErr = import(From: T->getReplacementType()); |
| 1863 | if (!ToReplacementTypeOrErr) |
| 1864 | return ToReplacementTypeOrErr.takeError(); |
| 1865 | |
| 1866 | return Importer.getToContext().getSubstTemplateTypeParmType( |
| 1867 | Replacement: *ToReplacementTypeOrErr, AssociatedDecl: *ReplacedOrErr, Index: T->getIndex(), PackIndex: T->getPackIndex(), |
| 1868 | Final: T->getFinal()); |
| 1869 | } |
| 1870 | |
| 1871 | ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmPackType( |
| 1872 | const SubstTemplateTypeParmPackType *T) { |
| 1873 | Expected<Decl *> ReplacedOrErr = import(From: T->getAssociatedDecl()); |
| 1874 | if (!ReplacedOrErr) |
| 1875 | return ReplacedOrErr.takeError(); |
| 1876 | |
| 1877 | Expected<TemplateArgument> ToArgumentPack = import(From: T->getArgumentPack()); |
| 1878 | if (!ToArgumentPack) |
| 1879 | return ToArgumentPack.takeError(); |
| 1880 | |
| 1881 | return Importer.getToContext().getSubstTemplateTypeParmPackType( |
| 1882 | AssociatedDecl: *ReplacedOrErr, Index: T->getIndex(), Final: T->getFinal(), ArgPack: *ToArgumentPack); |
| 1883 | } |
| 1884 | |
| 1885 | ExpectedType ASTNodeImporter::VisitSubstBuiltinTemplatePackType( |
| 1886 | const SubstBuiltinTemplatePackType *T) { |
| 1887 | Expected<TemplateArgument> ToArgumentPack = import(From: T->getArgumentPack()); |
| 1888 | if (!ToArgumentPack) |
| 1889 | return ToArgumentPack.takeError(); |
| 1890 | return Importer.getToContext().getSubstBuiltinTemplatePack(ArgPack: *ToArgumentPack); |
| 1891 | } |
| 1892 | |
| 1893 | ExpectedType ASTNodeImporter::VisitTemplateSpecializationType( |
| 1894 | const TemplateSpecializationType *T) { |
| 1895 | auto ToTemplateOrErr = import(From: T->getTemplateName()); |
| 1896 | if (!ToTemplateOrErr) |
| 1897 | return ToTemplateOrErr.takeError(); |
| 1898 | |
| 1899 | SmallVector<TemplateArgument, 2> ToTemplateArgs; |
| 1900 | if (Error Err = |
| 1901 | ImportTemplateArguments(FromArgs: T->template_arguments(), ToArgs&: ToTemplateArgs)) |
| 1902 | return std::move(Err); |
| 1903 | |
| 1904 | ExpectedType ToUnderlyingOrErr = |
| 1905 | T->isCanonicalUnqualified() ? QualType() : import(From: T->desugar()); |
| 1906 | if (!ToUnderlyingOrErr) |
| 1907 | return ToUnderlyingOrErr.takeError(); |
| 1908 | return Importer.getToContext().getTemplateSpecializationType( |
| 1909 | Keyword: T->getKeyword(), T: *ToTemplateOrErr, SpecifiedArgs: ToTemplateArgs, CanonicalArgs: {}, |
| 1910 | Underlying: *ToUnderlyingOrErr); |
| 1911 | } |
| 1912 | |
| 1913 | ExpectedType |
| 1914 | ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) { |
| 1915 | ExpectedType ToPatternOrErr = import(From: T->getPattern()); |
| 1916 | if (!ToPatternOrErr) |
| 1917 | return ToPatternOrErr.takeError(); |
| 1918 | |
| 1919 | return Importer.getToContext().getPackExpansionType(Pattern: *ToPatternOrErr, |
| 1920 | NumExpansions: T->getNumExpansions(), |
| 1921 | /*ExpactPack=*/ExpectPackInType: false); |
| 1922 | } |
| 1923 | |
| 1924 | ExpectedType |
| 1925 | ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) { |
| 1926 | auto ToQualifierOrErr = import(From: T->getQualifier()); |
| 1927 | if (!ToQualifierOrErr) |
| 1928 | return ToQualifierOrErr.takeError(); |
| 1929 | |
| 1930 | IdentifierInfo *Name = Importer.Import(FromId: T->getIdentifier()); |
| 1931 | return Importer.getToContext().getDependentNameType(Keyword: T->getKeyword(), |
| 1932 | NNS: *ToQualifierOrErr, Name); |
| 1933 | } |
| 1934 | |
| 1935 | ExpectedType |
| 1936 | ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { |
| 1937 | Expected<ObjCInterfaceDecl *> ToDeclOrErr = import(From: T->getDecl()); |
| 1938 | if (!ToDeclOrErr) |
| 1939 | return ToDeclOrErr.takeError(); |
| 1940 | |
| 1941 | return Importer.getToContext().getObjCInterfaceType(Decl: *ToDeclOrErr); |
| 1942 | } |
| 1943 | |
| 1944 | ExpectedType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) { |
| 1945 | ExpectedType ToBaseTypeOrErr = import(From: T->getBaseType()); |
| 1946 | if (!ToBaseTypeOrErr) |
| 1947 | return ToBaseTypeOrErr.takeError(); |
| 1948 | |
| 1949 | SmallVector<QualType, 4> TypeArgs; |
| 1950 | for (auto TypeArg : T->getTypeArgsAsWritten()) { |
| 1951 | if (ExpectedType TyOrErr = import(From: TypeArg)) |
| 1952 | TypeArgs.push_back(Elt: *TyOrErr); |
| 1953 | else |
| 1954 | return TyOrErr.takeError(); |
| 1955 | } |
| 1956 | |
| 1957 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 1958 | for (auto *P : T->quals()) { |
| 1959 | if (Expected<ObjCProtocolDecl *> ProtocolOrErr = import(From: P)) |
| 1960 | Protocols.push_back(Elt: *ProtocolOrErr); |
| 1961 | else |
| 1962 | return ProtocolOrErr.takeError(); |
| 1963 | |
| 1964 | } |
| 1965 | |
| 1966 | return Importer.getToContext().getObjCObjectType(Base: *ToBaseTypeOrErr, typeArgs: TypeArgs, |
| 1967 | protocols: Protocols, |
| 1968 | isKindOf: T->isKindOfTypeAsWritten()); |
| 1969 | } |
| 1970 | |
| 1971 | ExpectedType |
| 1972 | ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { |
| 1973 | ExpectedType ToPointeeTypeOrErr = import(From: T->getPointeeType()); |
| 1974 | if (!ToPointeeTypeOrErr) |
| 1975 | return ToPointeeTypeOrErr.takeError(); |
| 1976 | |
| 1977 | return Importer.getToContext().getObjCObjectPointerType(OIT: *ToPointeeTypeOrErr); |
| 1978 | } |
| 1979 | |
| 1980 | ExpectedType |
| 1981 | ASTNodeImporter::VisitMacroQualifiedType(const MacroQualifiedType *T) { |
| 1982 | ExpectedType ToUnderlyingTypeOrErr = import(From: T->getUnderlyingType()); |
| 1983 | if (!ToUnderlyingTypeOrErr) |
| 1984 | return ToUnderlyingTypeOrErr.takeError(); |
| 1985 | |
| 1986 | IdentifierInfo *ToIdentifier = Importer.Import(FromId: T->getMacroIdentifier()); |
| 1987 | return Importer.getToContext().getMacroQualifiedType(UnderlyingTy: *ToUnderlyingTypeOrErr, |
| 1988 | MacroII: ToIdentifier); |
| 1989 | } |
| 1990 | |
| 1991 | ExpectedType clang::ASTNodeImporter::VisitAdjustedType(const AdjustedType *T) { |
| 1992 | Error Err = Error::success(); |
| 1993 | QualType ToOriginalType = importChecked(Err, From: T->getOriginalType()); |
| 1994 | QualType ToAdjustedType = importChecked(Err, From: T->getAdjustedType()); |
| 1995 | if (Err) |
| 1996 | return std::move(Err); |
| 1997 | |
| 1998 | return Importer.getToContext().getAdjustedType(Orig: ToOriginalType, |
| 1999 | New: ToAdjustedType); |
| 2000 | } |
| 2001 | |
| 2002 | ExpectedType clang::ASTNodeImporter::VisitBitIntType(const BitIntType *T) { |
| 2003 | return Importer.getToContext().getBitIntType(Unsigned: T->isUnsigned(), |
| 2004 | NumBits: T->getNumBits()); |
| 2005 | } |
| 2006 | |
| 2007 | ExpectedType clang::ASTNodeImporter::VisitBTFTagAttributedType( |
| 2008 | const clang::BTFTagAttributedType *T) { |
| 2009 | Error Err = Error::success(); |
| 2010 | const BTFTypeTagAttr *ToBTFAttr = importChecked(Err, From: T->getAttr()); |
| 2011 | QualType ToWrappedType = importChecked(Err, From: T->getWrappedType()); |
| 2012 | if (Err) |
| 2013 | return std::move(Err); |
| 2014 | |
| 2015 | return Importer.getToContext().getBTFTagAttributedType(BTFAttr: ToBTFAttr, |
| 2016 | Wrapped: ToWrappedType); |
| 2017 | } |
| 2018 | |
| 2019 | ExpectedType clang::ASTNodeImporter::VisitOverflowBehaviorType( |
| 2020 | const clang::OverflowBehaviorType *T) { |
| 2021 | Error Err = Error::success(); |
| 2022 | OverflowBehaviorType::OverflowBehaviorKind ToKind = T->getBehaviorKind(); |
| 2023 | QualType ToUnderlyingType = importChecked(Err, From: T->getUnderlyingType()); |
| 2024 | if (Err) |
| 2025 | return std::move(Err); |
| 2026 | |
| 2027 | return Importer.getToContext().getOverflowBehaviorType(Kind: ToKind, |
| 2028 | Wrapped: ToUnderlyingType); |
| 2029 | } |
| 2030 | |
| 2031 | ExpectedType clang::ASTNodeImporter::VisitHLSLAttributedResourceType( |
| 2032 | const clang::HLSLAttributedResourceType *T) { |
| 2033 | Error Err = Error::success(); |
| 2034 | const HLSLAttributedResourceType::Attributes &ToAttrs = T->getAttrs(); |
| 2035 | QualType ToWrappedType = importChecked(Err, From: T->getWrappedType()); |
| 2036 | QualType ToContainedType = importChecked(Err, From: T->getContainedType()); |
| 2037 | if (Err) |
| 2038 | return std::move(Err); |
| 2039 | |
| 2040 | return Importer.getToContext().getHLSLAttributedResourceType( |
| 2041 | Wrapped: ToWrappedType, Contained: ToContainedType, Attrs: ToAttrs); |
| 2042 | } |
| 2043 | |
| 2044 | ExpectedType clang::ASTNodeImporter::VisitHLSLInlineSpirvType( |
| 2045 | const clang::HLSLInlineSpirvType *T) { |
| 2046 | Error Err = Error::success(); |
| 2047 | |
| 2048 | uint32_t ToOpcode = T->getOpcode(); |
| 2049 | uint32_t ToSize = T->getSize(); |
| 2050 | uint32_t ToAlignment = T->getAlignment(); |
| 2051 | |
| 2052 | llvm::SmallVector<SpirvOperand> ToOperands; |
| 2053 | |
| 2054 | for (auto &Operand : T->getOperands()) { |
| 2055 | using SpirvOperandKind = SpirvOperand::SpirvOperandKind; |
| 2056 | |
| 2057 | switch (Operand.getKind()) { |
| 2058 | case SpirvOperandKind::ConstantId: |
| 2059 | ToOperands.push_back(Elt: SpirvOperand::createConstant( |
| 2060 | ResultType: importChecked(Err, From: Operand.getResultType()), Val: Operand.getValue())); |
| 2061 | break; |
| 2062 | case SpirvOperandKind::Literal: |
| 2063 | ToOperands.push_back(Elt: SpirvOperand::createLiteral(Val: Operand.getValue())); |
| 2064 | break; |
| 2065 | case SpirvOperandKind::TypeId: |
| 2066 | ToOperands.push_back(Elt: SpirvOperand::createType( |
| 2067 | T: importChecked(Err, From: Operand.getResultType()))); |
| 2068 | break; |
| 2069 | default: |
| 2070 | llvm_unreachable("Invalid SpirvOperand kind" ); |
| 2071 | } |
| 2072 | |
| 2073 | if (Err) |
| 2074 | return std::move(Err); |
| 2075 | } |
| 2076 | |
| 2077 | return Importer.getToContext().getHLSLInlineSpirvType( |
| 2078 | Opcode: ToOpcode, Size: ToSize, Alignment: ToAlignment, Operands: ToOperands); |
| 2079 | } |
| 2080 | |
| 2081 | ExpectedType clang::ASTNodeImporter::VisitConstantMatrixType( |
| 2082 | const clang::ConstantMatrixType *T) { |
| 2083 | ExpectedType ToElementTypeOrErr = import(From: T->getElementType()); |
| 2084 | if (!ToElementTypeOrErr) |
| 2085 | return ToElementTypeOrErr.takeError(); |
| 2086 | |
| 2087 | return Importer.getToContext().getConstantMatrixType( |
| 2088 | ElementType: *ToElementTypeOrErr, NumRows: T->getNumRows(), NumColumns: T->getNumColumns()); |
| 2089 | } |
| 2090 | |
| 2091 | ExpectedType clang::ASTNodeImporter::VisitDependentAddressSpaceType( |
| 2092 | const clang::DependentAddressSpaceType *T) { |
| 2093 | Error Err = Error::success(); |
| 2094 | QualType ToPointeeType = importChecked(Err, From: T->getPointeeType()); |
| 2095 | Expr *ToAddrSpaceExpr = importChecked(Err, From: T->getAddrSpaceExpr()); |
| 2096 | SourceLocation ToAttrLoc = importChecked(Err, From: T->getAttributeLoc()); |
| 2097 | if (Err) |
| 2098 | return std::move(Err); |
| 2099 | |
| 2100 | return Importer.getToContext().getDependentAddressSpaceType( |
| 2101 | PointeeType: ToPointeeType, AddrSpaceExpr: ToAddrSpaceExpr, AttrLoc: ToAttrLoc); |
| 2102 | } |
| 2103 | |
| 2104 | ExpectedType clang::ASTNodeImporter::VisitDependentBitIntType( |
| 2105 | const clang::DependentBitIntType *T) { |
| 2106 | ExpectedExpr ToNumBitsExprOrErr = import(From: T->getNumBitsExpr()); |
| 2107 | if (!ToNumBitsExprOrErr) |
| 2108 | return ToNumBitsExprOrErr.takeError(); |
| 2109 | return Importer.getToContext().getDependentBitIntType(Unsigned: T->isUnsigned(), |
| 2110 | BitsExpr: *ToNumBitsExprOrErr); |
| 2111 | } |
| 2112 | |
| 2113 | ExpectedType clang::ASTNodeImporter::VisitPredefinedSugarType( |
| 2114 | const clang::PredefinedSugarType *T) { |
| 2115 | return Importer.getToContext().getPredefinedSugarType(KD: T->getKind()); |
| 2116 | } |
| 2117 | |
| 2118 | ExpectedType clang::ASTNodeImporter::VisitDependentSizedMatrixType( |
| 2119 | const clang::DependentSizedMatrixType *T) { |
| 2120 | Error Err = Error::success(); |
| 2121 | QualType ToElementType = importChecked(Err, From: T->getElementType()); |
| 2122 | Expr *ToRowExpr = importChecked(Err, From: T->getRowExpr()); |
| 2123 | Expr *ToColumnExpr = importChecked(Err, From: T->getColumnExpr()); |
| 2124 | SourceLocation ToAttrLoc = importChecked(Err, From: T->getAttributeLoc()); |
| 2125 | if (Err) |
| 2126 | return std::move(Err); |
| 2127 | |
| 2128 | return Importer.getToContext().getDependentSizedMatrixType( |
| 2129 | ElementType: ToElementType, RowExpr: ToRowExpr, ColumnExpr: ToColumnExpr, AttrLoc: ToAttrLoc); |
| 2130 | } |
| 2131 | |
| 2132 | ExpectedType clang::ASTNodeImporter::VisitDependentVectorType( |
| 2133 | const clang::DependentVectorType *T) { |
| 2134 | Error Err = Error::success(); |
| 2135 | QualType ToElementType = importChecked(Err, From: T->getElementType()); |
| 2136 | Expr *ToSizeExpr = importChecked(Err, From: T->getSizeExpr()); |
| 2137 | SourceLocation ToAttrLoc = importChecked(Err, From: T->getAttributeLoc()); |
| 2138 | if (Err) |
| 2139 | return std::move(Err); |
| 2140 | |
| 2141 | return Importer.getToContext().getDependentVectorType( |
| 2142 | VectorType: ToElementType, SizeExpr: ToSizeExpr, AttrLoc: ToAttrLoc, VecKind: T->getVectorKind()); |
| 2143 | } |
| 2144 | |
| 2145 | ExpectedType clang::ASTNodeImporter::VisitObjCTypeParamType( |
| 2146 | const clang::ObjCTypeParamType *T) { |
| 2147 | Expected<ObjCTypeParamDecl *> ToDeclOrErr = import(From: T->getDecl()); |
| 2148 | if (!ToDeclOrErr) |
| 2149 | return ToDeclOrErr.takeError(); |
| 2150 | |
| 2151 | SmallVector<ObjCProtocolDecl *, 4> ToProtocols; |
| 2152 | for (ObjCProtocolDecl *FromProtocol : T->getProtocols()) { |
| 2153 | Expected<ObjCProtocolDecl *> ToProtocolOrErr = import(From: FromProtocol); |
| 2154 | if (!ToProtocolOrErr) |
| 2155 | return ToProtocolOrErr.takeError(); |
| 2156 | ToProtocols.push_back(Elt: *ToProtocolOrErr); |
| 2157 | } |
| 2158 | |
| 2159 | return Importer.getToContext().getObjCTypeParamType(Decl: *ToDeclOrErr, |
| 2160 | protocols: ToProtocols); |
| 2161 | } |
| 2162 | |
| 2163 | ExpectedType clang::ASTNodeImporter::VisitPipeType(const clang::PipeType *T) { |
| 2164 | ExpectedType ToElementTypeOrErr = import(From: T->getElementType()); |
| 2165 | if (!ToElementTypeOrErr) |
| 2166 | return ToElementTypeOrErr.takeError(); |
| 2167 | |
| 2168 | ASTContext &ToCtx = Importer.getToContext(); |
| 2169 | if (T->isReadOnly()) |
| 2170 | return ToCtx.getReadPipeType(T: *ToElementTypeOrErr); |
| 2171 | else |
| 2172 | return ToCtx.getWritePipeType(T: *ToElementTypeOrErr); |
| 2173 | } |
| 2174 | |
| 2175 | //---------------------------------------------------------------------------- |
| 2176 | // Import Declarations |
| 2177 | //---------------------------------------------------------------------------- |
| 2178 | Error ASTNodeImporter::ImportDeclParts( |
| 2179 | NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC, |
| 2180 | DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc) { |
| 2181 | // Check if RecordDecl is in FunctionDecl parameters to avoid infinite loop. |
| 2182 | // example: int struct_in_proto(struct data_t{int a;int b;} *d); |
| 2183 | // FIXME: We could support these constructs by importing a different type of |
| 2184 | // this parameter and by importing the original type of the parameter only |
| 2185 | // after the FunctionDecl is created. See |
| 2186 | // VisitFunctionDecl::UsedDifferentProtoType. |
| 2187 | DeclContext *OrigDC = D->getDeclContext(); |
| 2188 | FunctionDecl *FunDecl; |
| 2189 | if (isa<RecordDecl>(Val: D) && (FunDecl = dyn_cast<FunctionDecl>(Val: OrigDC)) && |
| 2190 | FunDecl->hasBody()) { |
| 2191 | auto getLeafPointeeType = [](const Type *T) { |
| 2192 | while (T->isPointerType() || T->isArrayType()) { |
| 2193 | T = T->getPointeeOrArrayElementType(); |
| 2194 | } |
| 2195 | return T; |
| 2196 | }; |
| 2197 | for (const ParmVarDecl *P : FunDecl->parameters()) { |
| 2198 | const Type *LeafT = |
| 2199 | getLeafPointeeType(P->getType().getCanonicalType().getTypePtr()); |
| 2200 | auto *RT = dyn_cast<RecordType>(Val: LeafT); |
| 2201 | if (RT && RT->getDecl() == D) { |
| 2202 | Importer.FromDiag(Loc: D->getLocation(), DiagID: diag::err_unsupported_ast_node) |
| 2203 | << D->getDeclKindName(); |
| 2204 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
| 2205 | } |
| 2206 | } |
| 2207 | } |
| 2208 | |
| 2209 | // Import the context of this declaration. |
| 2210 | if (Error Err = ImportDeclContext(From: D, ToDC&: DC, ToLexicalDC&: LexicalDC)) |
| 2211 | return Err; |
| 2212 | |
| 2213 | // Import the name of this declaration. |
| 2214 | if (Error Err = importInto(To&: Name, From: D->getDeclName())) |
| 2215 | return Err; |
| 2216 | |
| 2217 | // Import the location of this declaration. |
| 2218 | if (Error Err = importInto(To&: Loc, From: D->getLocation())) |
| 2219 | return Err; |
| 2220 | |
| 2221 | ToD = cast_or_null<NamedDecl>(Val: Importer.GetAlreadyImportedOrNull(FromD: D)); |
| 2222 | if (ToD) |
| 2223 | if (Error Err = ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD: D, ToD)) |
| 2224 | return Err; |
| 2225 | |
| 2226 | return Error::success(); |
| 2227 | } |
| 2228 | |
| 2229 | Error ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclarationName &Name, |
| 2230 | NamedDecl *&ToD, SourceLocation &Loc) { |
| 2231 | |
| 2232 | // Import the name of this declaration. |
| 2233 | if (Error Err = importInto(To&: Name, From: D->getDeclName())) |
| 2234 | return Err; |
| 2235 | |
| 2236 | // Import the location of this declaration. |
| 2237 | if (Error Err = importInto(To&: Loc, From: D->getLocation())) |
| 2238 | return Err; |
| 2239 | |
| 2240 | ToD = cast_or_null<NamedDecl>(Val: Importer.GetAlreadyImportedOrNull(FromD: D)); |
| 2241 | if (ToD) |
| 2242 | if (Error Err = ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD: D, ToD)) |
| 2243 | return Err; |
| 2244 | |
| 2245 | return Error::success(); |
| 2246 | } |
| 2247 | |
| 2248 | Error ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) { |
| 2249 | if (!FromD) |
| 2250 | return Error::success(); |
| 2251 | |
| 2252 | if (!ToD) |
| 2253 | if (Error Err = importInto(To&: ToD, From: FromD)) |
| 2254 | return Err; |
| 2255 | |
| 2256 | if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(Val: FromD)) { |
| 2257 | if (RecordDecl *ToRecord = cast<RecordDecl>(Val: ToD)) { |
| 2258 | if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && |
| 2259 | !ToRecord->getDefinition()) { |
| 2260 | if (Error Err = ImportDefinition(From: FromRecord, To: ToRecord)) |
| 2261 | return Err; |
| 2262 | } |
| 2263 | } |
| 2264 | return Error::success(); |
| 2265 | } |
| 2266 | |
| 2267 | if (EnumDecl * = dyn_cast<EnumDecl>(Val: FromD)) { |
| 2268 | if (EnumDecl *ToEnum = cast<EnumDecl>(Val: ToD)) { |
| 2269 | if (FromEnum->getDefinition() && !ToEnum->getDefinition()) { |
| 2270 | if (Error Err = ImportDefinition(From: FromEnum, To: ToEnum)) |
| 2271 | return Err; |
| 2272 | } |
| 2273 | } |
| 2274 | return Error::success(); |
| 2275 | } |
| 2276 | |
| 2277 | return Error::success(); |
| 2278 | } |
| 2279 | |
| 2280 | Error |
| 2281 | ASTNodeImporter::ImportDeclarationNameLoc( |
| 2282 | const DeclarationNameInfo &From, DeclarationNameInfo& To) { |
| 2283 | // NOTE: To.Name and To.Loc are already imported. |
| 2284 | // We only have to import To.LocInfo. |
| 2285 | switch (To.getName().getNameKind()) { |
| 2286 | case DeclarationName::Identifier: |
| 2287 | case DeclarationName::ObjCZeroArgSelector: |
| 2288 | case DeclarationName::ObjCOneArgSelector: |
| 2289 | case DeclarationName::ObjCMultiArgSelector: |
| 2290 | case DeclarationName::CXXUsingDirective: |
| 2291 | case DeclarationName::CXXDeductionGuideName: |
| 2292 | return Error::success(); |
| 2293 | |
| 2294 | case DeclarationName::CXXOperatorName: { |
| 2295 | if (auto ToRangeOrErr = import(From: From.getCXXOperatorNameRange())) |
| 2296 | To.setCXXOperatorNameRange(*ToRangeOrErr); |
| 2297 | else |
| 2298 | return ToRangeOrErr.takeError(); |
| 2299 | return Error::success(); |
| 2300 | } |
| 2301 | case DeclarationName::CXXLiteralOperatorName: { |
| 2302 | if (ExpectedSLoc LocOrErr = import(From: From.getCXXLiteralOperatorNameLoc())) |
| 2303 | To.setCXXLiteralOperatorNameLoc(*LocOrErr); |
| 2304 | else |
| 2305 | return LocOrErr.takeError(); |
| 2306 | return Error::success(); |
| 2307 | } |
| 2308 | case DeclarationName::CXXConstructorName: |
| 2309 | case DeclarationName::CXXDestructorName: |
| 2310 | case DeclarationName::CXXConversionFunctionName: { |
| 2311 | if (auto ToTInfoOrErr = import(From: From.getNamedTypeInfo())) |
| 2312 | To.setNamedTypeInfo(*ToTInfoOrErr); |
| 2313 | else |
| 2314 | return ToTInfoOrErr.takeError(); |
| 2315 | return Error::success(); |
| 2316 | } |
| 2317 | } |
| 2318 | llvm_unreachable("Unknown name kind." ); |
| 2319 | } |
| 2320 | |
| 2321 | Error |
| 2322 | ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) { |
| 2323 | if (Importer.isMinimalImport() && !ForceImport) { |
| 2324 | auto ToDCOrErr = Importer.ImportContext(FromDC); |
| 2325 | return ToDCOrErr.takeError(); |
| 2326 | } |
| 2327 | |
| 2328 | // We use strict error handling in case of records and enums, but not |
| 2329 | // with e.g. namespaces. |
| 2330 | // |
| 2331 | // FIXME Clients of the ASTImporter should be able to choose an |
| 2332 | // appropriate error handling strategy for their needs. For instance, |
| 2333 | // they may not want to mark an entire namespace as erroneous merely |
| 2334 | // because there is an ODR error with two typedefs. As another example, |
| 2335 | // the client may allow EnumConstantDecls with same names but with |
| 2336 | // different values in two distinct translation units. |
| 2337 | ChildErrorHandlingStrategy HandleChildErrors(FromDC); |
| 2338 | |
| 2339 | auto MightNeedReordering = [](const Decl *D) { |
| 2340 | return isa<FieldDecl>(Val: D) || isa<IndirectFieldDecl>(Val: D) || isa<FriendDecl>(Val: D); |
| 2341 | }; |
| 2342 | |
| 2343 | // Import everything that might need reordering first. |
| 2344 | Error ChildErrors = Error::success(); |
| 2345 | for (auto *From : FromDC->decls()) { |
| 2346 | if (!MightNeedReordering(From)) |
| 2347 | continue; |
| 2348 | |
| 2349 | ExpectedDecl ImportedOrErr = import(From); |
| 2350 | |
| 2351 | // If we are in the process of ImportDefinition(...) for a RecordDecl we |
| 2352 | // want to make sure that we are also completing each FieldDecl. There |
| 2353 | // are currently cases where this does not happen and this is correctness |
| 2354 | // fix since operations such as code generation will expect this to be so. |
| 2355 | if (!ImportedOrErr) { |
| 2356 | HandleChildErrors.handleChildImportResult(ResultErr&: ChildErrors, |
| 2357 | ChildErr: ImportedOrErr.takeError()); |
| 2358 | continue; |
| 2359 | } |
| 2360 | FieldDecl *FieldFrom = dyn_cast_or_null<FieldDecl>(Val: From); |
| 2361 | Decl *ImportedDecl = *ImportedOrErr; |
| 2362 | FieldDecl *FieldTo = dyn_cast_or_null<FieldDecl>(Val: ImportedDecl); |
| 2363 | if (FieldFrom && FieldTo) { |
| 2364 | Error Err = ImportFieldDeclDefinition(From: FieldFrom, To: FieldTo); |
| 2365 | HandleChildErrors.handleChildImportResult(ResultErr&: ChildErrors, ChildErr: std::move(Err)); |
| 2366 | } |
| 2367 | } |
| 2368 | |
| 2369 | // We reorder declarations in RecordDecls because they may have another order |
| 2370 | // in the "to" context than they have in the "from" context. This may happen |
| 2371 | // e.g when we import a class like this: |
| 2372 | // struct declToImport { |
| 2373 | // int a = c + b; |
| 2374 | // int b = 1; |
| 2375 | // int c = 2; |
| 2376 | // }; |
| 2377 | // During the import of `a` we import first the dependencies in sequence, |
| 2378 | // thus the order would be `c`, `b`, `a`. We will get the normal order by |
| 2379 | // first removing the already imported members and then adding them in the |
| 2380 | // order as they appear in the "from" context. |
| 2381 | // |
| 2382 | // Keeping field order is vital because it determines structure layout. |
| 2383 | // |
| 2384 | // Here and below, we cannot call field_begin() method and its callers on |
| 2385 | // ToDC if it has an external storage. Calling field_begin() will |
| 2386 | // automatically load all the fields by calling |
| 2387 | // LoadFieldsFromExternalStorage(). LoadFieldsFromExternalStorage() would |
| 2388 | // call ASTImporter::Import(). This is because the ExternalASTSource |
| 2389 | // interface in LLDB is implemented by the means of the ASTImporter. However, |
| 2390 | // calling an import at this point would result in an uncontrolled import, we |
| 2391 | // must avoid that. |
| 2392 | |
| 2393 | auto ToDCOrErr = Importer.ImportContext(FromDC); |
| 2394 | if (!ToDCOrErr) { |
| 2395 | consumeError(Err: std::move(ChildErrors)); |
| 2396 | return ToDCOrErr.takeError(); |
| 2397 | } |
| 2398 | |
| 2399 | if (const auto *FromRD = dyn_cast<RecordDecl>(Val: FromDC)) { |
| 2400 | DeclContext *ToDC = *ToDCOrErr; |
| 2401 | // Remove all declarations, which may be in wrong order in the |
| 2402 | // lexical DeclContext and then add them in the proper order. |
| 2403 | for (auto *D : FromRD->decls()) { |
| 2404 | if (!MightNeedReordering(D)) |
| 2405 | continue; |
| 2406 | |
| 2407 | assert(D && "DC contains a null decl" ); |
| 2408 | if (Decl *ToD = Importer.GetAlreadyImportedOrNull(FromD: D)) { |
| 2409 | // Remove only the decls which we successfully imported. |
| 2410 | assert(ToDC == ToD->getLexicalDeclContext() && ToDC->containsDecl(ToD)); |
| 2411 | // Remove the decl from its wrong place in the linked list. |
| 2412 | ToDC->removeDecl(D: ToD); |
| 2413 | // Add the decl to the end of the linked list. |
| 2414 | // This time it will be at the proper place because the enclosing for |
| 2415 | // loop iterates in the original (good) order of the decls. |
| 2416 | ToDC->addDeclInternal(D: ToD); |
| 2417 | } |
| 2418 | } |
| 2419 | } |
| 2420 | |
| 2421 | // Import everything else. |
| 2422 | for (auto *From : FromDC->decls()) { |
| 2423 | if (MightNeedReordering(From)) |
| 2424 | continue; |
| 2425 | |
| 2426 | ExpectedDecl ImportedOrErr = import(From); |
| 2427 | if (!ImportedOrErr) |
| 2428 | HandleChildErrors.handleChildImportResult(ResultErr&: ChildErrors, |
| 2429 | ChildErr: ImportedOrErr.takeError()); |
| 2430 | } |
| 2431 | |
| 2432 | return ChildErrors; |
| 2433 | } |
| 2434 | |
| 2435 | Error ASTNodeImporter::ImportFieldDeclDefinition(const FieldDecl *From, |
| 2436 | const FieldDecl *To) { |
| 2437 | RecordDecl *FromRecordDecl = nullptr; |
| 2438 | RecordDecl *ToRecordDecl = nullptr; |
| 2439 | // If we have a field that is an ArrayType we need to check if the array |
| 2440 | // element is a RecordDecl and if so we need to import the definition. |
| 2441 | QualType FromType = From->getType(); |
| 2442 | QualType ToType = To->getType(); |
| 2443 | if (FromType->isArrayType()) { |
| 2444 | // getBaseElementTypeUnsafe(...) handles multi-dimensional arrays for us. |
| 2445 | FromRecordDecl = FromType->getBaseElementTypeUnsafe()->getAsRecordDecl(); |
| 2446 | ToRecordDecl = ToType->getBaseElementTypeUnsafe()->getAsRecordDecl(); |
| 2447 | } |
| 2448 | |
| 2449 | if (!FromRecordDecl || !ToRecordDecl) { |
| 2450 | const RecordType *RecordFrom = FromType->getAs<RecordType>(); |
| 2451 | const RecordType *RecordTo = ToType->getAs<RecordType>(); |
| 2452 | |
| 2453 | if (RecordFrom && RecordTo) { |
| 2454 | FromRecordDecl = RecordFrom->getDecl(); |
| 2455 | ToRecordDecl = RecordTo->getDecl(); |
| 2456 | } |
| 2457 | } |
| 2458 | |
| 2459 | if (FromRecordDecl && ToRecordDecl) { |
| 2460 | if (FromRecordDecl->isCompleteDefinition() && |
| 2461 | !ToRecordDecl->isCompleteDefinition()) |
| 2462 | return ImportDefinition(From: FromRecordDecl, To: ToRecordDecl); |
| 2463 | } |
| 2464 | |
| 2465 | return Error::success(); |
| 2466 | } |
| 2467 | |
| 2468 | Error ASTNodeImporter::ImportDeclContext( |
| 2469 | Decl *FromD, DeclContext *&ToDC, DeclContext *&ToLexicalDC) { |
| 2470 | auto ToDCOrErr = Importer.ImportContext(FromDC: FromD->getDeclContext()); |
| 2471 | if (!ToDCOrErr) |
| 2472 | return ToDCOrErr.takeError(); |
| 2473 | ToDC = *ToDCOrErr; |
| 2474 | |
| 2475 | if (FromD->getDeclContext() != FromD->getLexicalDeclContext()) { |
| 2476 | auto ToLexicalDCOrErr = Importer.ImportContext( |
| 2477 | FromDC: FromD->getLexicalDeclContext()); |
| 2478 | if (!ToLexicalDCOrErr) |
| 2479 | return ToLexicalDCOrErr.takeError(); |
| 2480 | ToLexicalDC = *ToLexicalDCOrErr; |
| 2481 | } else |
| 2482 | ToLexicalDC = ToDC; |
| 2483 | |
| 2484 | return Error::success(); |
| 2485 | } |
| 2486 | |
| 2487 | Error ASTNodeImporter::ImportImplicitMethods( |
| 2488 | const CXXRecordDecl *From, CXXRecordDecl *To) { |
| 2489 | assert(From->isCompleteDefinition() && To->getDefinition() == To && |
| 2490 | "Import implicit methods to or from non-definition" ); |
| 2491 | |
| 2492 | for (CXXMethodDecl *FromM : From->methods()) |
| 2493 | if (FromM->isImplicit()) { |
| 2494 | Expected<CXXMethodDecl *> ToMOrErr = import(From: FromM); |
| 2495 | if (!ToMOrErr) |
| 2496 | return ToMOrErr.takeError(); |
| 2497 | } |
| 2498 | |
| 2499 | return Error::success(); |
| 2500 | } |
| 2501 | |
| 2502 | static Error setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To, |
| 2503 | ASTImporter &Importer) { |
| 2504 | if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) { |
| 2505 | if (ExpectedDecl ToTypedefOrErr = Importer.Import(FromD: FromTypedef)) |
| 2506 | To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(Val: *ToTypedefOrErr)); |
| 2507 | else |
| 2508 | return ToTypedefOrErr.takeError(); |
| 2509 | } |
| 2510 | return Error::success(); |
| 2511 | } |
| 2512 | |
| 2513 | Error ASTNodeImporter::ImportDefinition( |
| 2514 | RecordDecl *From, RecordDecl *To, ImportDefinitionKind Kind) { |
| 2515 | auto DefinitionCompleter = [To]() { |
| 2516 | // There are cases in LLDB when we first import a class without its |
| 2517 | // members. The class will have DefinitionData, but no members. Then, |
| 2518 | // importDefinition is called from LLDB, which tries to get the members, so |
| 2519 | // when we get here, the class already has the DefinitionData set, so we |
| 2520 | // must unset the CompleteDefinition here to be able to complete again the |
| 2521 | // definition. |
| 2522 | To->setCompleteDefinition(false); |
| 2523 | To->completeDefinition(); |
| 2524 | }; |
| 2525 | |
| 2526 | if (To->getDefinition() || To->isBeingDefined()) { |
| 2527 | if (Kind == IDK_Everything || |
| 2528 | // In case of lambdas, the class already has a definition ptr set, but |
| 2529 | // the contained decls are not imported yet. Also, isBeingDefined was |
| 2530 | // set in CXXRecordDecl::CreateLambda. We must import the contained |
| 2531 | // decls here and finish the definition. |
| 2532 | (To->isLambda() && shouldForceImportDeclContext(IDK: Kind))) { |
| 2533 | if (To->isLambda()) { |
| 2534 | auto *FromCXXRD = cast<CXXRecordDecl>(Val: From); |
| 2535 | SmallVector<LambdaCapture, 8> ToCaptures; |
| 2536 | ToCaptures.reserve(N: FromCXXRD->capture_size()); |
| 2537 | for (const auto &FromCapture : FromCXXRD->captures()) { |
| 2538 | if (auto ToCaptureOrErr = import(From: FromCapture)) |
| 2539 | ToCaptures.push_back(Elt: *ToCaptureOrErr); |
| 2540 | else |
| 2541 | return ToCaptureOrErr.takeError(); |
| 2542 | } |
| 2543 | cast<CXXRecordDecl>(Val: To)->setCaptures(Context&: Importer.getToContext(), |
| 2544 | Captures: ToCaptures); |
| 2545 | } |
| 2546 | |
| 2547 | Error Result = ImportDeclContext(FromDC: From, /*ForceImport=*/true); |
| 2548 | // Finish the definition of the lambda, set isBeingDefined to false. |
| 2549 | if (To->isLambda()) |
| 2550 | DefinitionCompleter(); |
| 2551 | return Result; |
| 2552 | } |
| 2553 | |
| 2554 | return Error::success(); |
| 2555 | } |
| 2556 | |
| 2557 | To->startDefinition(); |
| 2558 | // Set the definition to complete even if it is really not complete during |
| 2559 | // import. Some AST constructs (expressions) require the record layout |
| 2560 | // to be calculated (see 'clang::computeDependence') at the time they are |
| 2561 | // constructed. Import of such AST node is possible during import of the |
| 2562 | // same record, there is no way to have a completely defined record (all |
| 2563 | // fields imported) at that time without multiple AST import passes. |
| 2564 | if (!Importer.isMinimalImport()) |
| 2565 | To->setCompleteDefinition(true); |
| 2566 | // Complete the definition even if error is returned. |
| 2567 | // The RecordDecl may be already part of the AST so it is better to |
| 2568 | // have it in complete state even if something is wrong with it. |
| 2569 | llvm::scope_exit DefinitionCompleterScopeExit(DefinitionCompleter); |
| 2570 | |
| 2571 | if (Error Err = setTypedefNameForAnonDecl(From, To, Importer)) |
| 2572 | return Err; |
| 2573 | |
| 2574 | // Add base classes. |
| 2575 | auto *ToCXX = dyn_cast<CXXRecordDecl>(Val: To); |
| 2576 | auto *FromCXX = dyn_cast<CXXRecordDecl>(Val: From); |
| 2577 | if (ToCXX && FromCXX && ToCXX->dataPtr() && FromCXX->dataPtr()) { |
| 2578 | |
| 2579 | struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data(); |
| 2580 | struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data(); |
| 2581 | |
| 2582 | #define FIELD(Name, Width, Merge) \ |
| 2583 | ToData.Name = FromData.Name; |
| 2584 | #include "clang/AST/CXXRecordDeclDefinitionBits.def" |
| 2585 | |
| 2586 | // Copy over the data stored in RecordDeclBits |
| 2587 | ToCXX->setArgPassingRestrictions(FromCXX->getArgPassingRestrictions()); |
| 2588 | |
| 2589 | SmallVector<CXXBaseSpecifier *, 4> Bases; |
| 2590 | for (const auto &Base1 : FromCXX->bases()) { |
| 2591 | ExpectedType TyOrErr = import(From: Base1.getType()); |
| 2592 | if (!TyOrErr) |
| 2593 | return TyOrErr.takeError(); |
| 2594 | |
| 2595 | SourceLocation EllipsisLoc; |
| 2596 | if (Base1.isPackExpansion()) { |
| 2597 | if (ExpectedSLoc LocOrErr = import(From: Base1.getEllipsisLoc())) |
| 2598 | EllipsisLoc = *LocOrErr; |
| 2599 | else |
| 2600 | return LocOrErr.takeError(); |
| 2601 | } |
| 2602 | |
| 2603 | // Ensure that we have a definition for the base. |
| 2604 | if (Error Err = |
| 2605 | ImportDefinitionIfNeeded(FromD: Base1.getType()->getAsCXXRecordDecl())) |
| 2606 | return Err; |
| 2607 | |
| 2608 | auto RangeOrErr = import(From: Base1.getSourceRange()); |
| 2609 | if (!RangeOrErr) |
| 2610 | return RangeOrErr.takeError(); |
| 2611 | |
| 2612 | auto TSIOrErr = import(From: Base1.getTypeSourceInfo()); |
| 2613 | if (!TSIOrErr) |
| 2614 | return TSIOrErr.takeError(); |
| 2615 | |
| 2616 | Bases.push_back( |
| 2617 | Elt: new (Importer.getToContext()) CXXBaseSpecifier( |
| 2618 | *RangeOrErr, |
| 2619 | Base1.isVirtual(), |
| 2620 | Base1.isBaseOfClass(), |
| 2621 | Base1.getAccessSpecifierAsWritten(), |
| 2622 | *TSIOrErr, |
| 2623 | EllipsisLoc)); |
| 2624 | } |
| 2625 | if (!Bases.empty()) |
| 2626 | ToCXX->setBases(Bases: Bases.data(), NumBases: Bases.size()); |
| 2627 | } |
| 2628 | |
| 2629 | if (shouldForceImportDeclContext(IDK: Kind)) { |
| 2630 | if (Error Err = ImportDeclContext(FromDC: From, /*ForceImport=*/true)) |
| 2631 | return Err; |
| 2632 | } |
| 2633 | |
| 2634 | return Error::success(); |
| 2635 | } |
| 2636 | |
| 2637 | Error ASTNodeImporter::ImportInitializer(VarDecl *From, VarDecl *To) { |
| 2638 | if (To->getAnyInitializer()) |
| 2639 | return Error::success(); |
| 2640 | |
| 2641 | Expr *FromInit = From->getInit(); |
| 2642 | if (!FromInit) |
| 2643 | return Error::success(); |
| 2644 | |
| 2645 | ExpectedExpr ToInitOrErr = import(From: FromInit); |
| 2646 | if (!ToInitOrErr) |
| 2647 | return ToInitOrErr.takeError(); |
| 2648 | |
| 2649 | To->setInit(*ToInitOrErr); |
| 2650 | if (EvaluatedStmt *FromEval = From->getEvaluatedStmt()) { |
| 2651 | EvaluatedStmt *ToEval = To->ensureEvaluatedStmt(); |
| 2652 | ToEval->HasConstantInitialization = FromEval->HasConstantInitialization; |
| 2653 | ToEval->HasConstantDestruction = FromEval->HasConstantDestruction; |
| 2654 | // FIXME: Also import the initializer value. |
| 2655 | } |
| 2656 | |
| 2657 | // FIXME: Other bits to merge? |
| 2658 | return Error::success(); |
| 2659 | } |
| 2660 | |
| 2661 | Error ASTNodeImporter::ImportDefinition( |
| 2662 | EnumDecl *From, EnumDecl *To, ImportDefinitionKind Kind) { |
| 2663 | if (To->getDefinition() || To->isBeingDefined()) { |
| 2664 | if (Kind == IDK_Everything) |
| 2665 | return ImportDeclContext(FromDC: From, /*ForceImport=*/true); |
| 2666 | return Error::success(); |
| 2667 | } |
| 2668 | |
| 2669 | To->startDefinition(); |
| 2670 | |
| 2671 | if (Error Err = setTypedefNameForAnonDecl(From, To, Importer)) |
| 2672 | return Err; |
| 2673 | |
| 2674 | ExpectedType ToTypeOrErr = |
| 2675 | import(From: QualType(Importer.getFromContext().getCanonicalTagType(TD: From))); |
| 2676 | if (!ToTypeOrErr) |
| 2677 | return ToTypeOrErr.takeError(); |
| 2678 | |
| 2679 | ExpectedType ToPromotionTypeOrErr = import(From: From->getPromotionType()); |
| 2680 | if (!ToPromotionTypeOrErr) |
| 2681 | return ToPromotionTypeOrErr.takeError(); |
| 2682 | |
| 2683 | if (shouldForceImportDeclContext(IDK: Kind)) |
| 2684 | if (Error Err = ImportDeclContext(FromDC: From, /*ForceImport=*/true)) |
| 2685 | return Err; |
| 2686 | |
| 2687 | // FIXME: we might need to merge the number of positive or negative bits |
| 2688 | // if the enumerator lists don't match. |
| 2689 | To->completeDefinition(NewType: *ToTypeOrErr, PromotionType: *ToPromotionTypeOrErr, |
| 2690 | NumPositiveBits: From->getNumPositiveBits(), |
| 2691 | NumNegativeBits: From->getNumNegativeBits()); |
| 2692 | return Error::success(); |
| 2693 | } |
| 2694 | |
| 2695 | Error ASTNodeImporter::ImportTemplateArguments( |
| 2696 | ArrayRef<TemplateArgument> FromArgs, |
| 2697 | SmallVectorImpl<TemplateArgument> &ToArgs) { |
| 2698 | for (const auto &Arg : FromArgs) { |
| 2699 | if (auto ToOrErr = import(From: Arg)) |
| 2700 | ToArgs.push_back(Elt: *ToOrErr); |
| 2701 | else |
| 2702 | return ToOrErr.takeError(); |
| 2703 | } |
| 2704 | |
| 2705 | return Error::success(); |
| 2706 | } |
| 2707 | |
| 2708 | // FIXME: Do not forget to remove this and use only 'import'. |
| 2709 | Expected<TemplateArgument> |
| 2710 | ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) { |
| 2711 | return import(From); |
| 2712 | } |
| 2713 | |
| 2714 | template <typename InContainerTy> |
| 2715 | Error ASTNodeImporter::ImportTemplateArgumentListInfo( |
| 2716 | const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) { |
| 2717 | for (const auto &FromLoc : Container) { |
| 2718 | if (auto ToLocOrErr = import(FromLoc)) |
| 2719 | ToTAInfo.addArgument(Loc: *ToLocOrErr); |
| 2720 | else |
| 2721 | return ToLocOrErr.takeError(); |
| 2722 | } |
| 2723 | return Error::success(); |
| 2724 | } |
| 2725 | |
| 2726 | static StructuralEquivalenceKind |
| 2727 | getStructuralEquivalenceKind(const ASTImporter &Importer) { |
| 2728 | return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal |
| 2729 | : StructuralEquivalenceKind::Default; |
| 2730 | } |
| 2731 | |
| 2732 | bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain, |
| 2733 | bool IgnoreTemplateParmDepth) { |
| 2734 | // Eliminate a potential failure point where we attempt to re-import |
| 2735 | // something we're trying to import while completing ToRecord. |
| 2736 | Decl *ToOrigin = Importer.GetOriginalDecl(To); |
| 2737 | if (ToOrigin) { |
| 2738 | To = ToOrigin; |
| 2739 | } |
| 2740 | |
| 2741 | StructuralEquivalenceContext Ctx( |
| 2742 | Importer.getToContext().getLangOpts(), Importer.getFromContext(), |
| 2743 | Importer.getToContext(), Importer.getNonEquivalentDecls(), |
| 2744 | getStructuralEquivalenceKind(Importer), |
| 2745 | /*StrictTypeSpelling=*/false, Complain, /*ErrorOnTagTypeMismatch=*/false, |
| 2746 | IgnoreTemplateParmDepth); |
| 2747 | return Ctx.IsEquivalent(D1: From, D2: To); |
| 2748 | } |
| 2749 | |
| 2750 | ExpectedDecl ASTNodeImporter::VisitDecl(Decl *D) { |
| 2751 | Importer.FromDiag(Loc: D->getLocation(), DiagID: diag::err_unsupported_ast_node) |
| 2752 | << D->getDeclKindName(); |
| 2753 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
| 2754 | } |
| 2755 | |
| 2756 | ExpectedDecl ASTNodeImporter::VisitImportDecl(ImportDecl *D) { |
| 2757 | Importer.FromDiag(Loc: D->getLocation(), DiagID: diag::err_unsupported_ast_node) |
| 2758 | << D->getDeclKindName(); |
| 2759 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
| 2760 | } |
| 2761 | |
| 2762 | ExpectedDecl ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) { |
| 2763 | // Import the context of this declaration. |
| 2764 | DeclContext *DC, *LexicalDC; |
| 2765 | if (Error Err = ImportDeclContext(FromD: D, ToDC&: DC, ToLexicalDC&: LexicalDC)) |
| 2766 | return std::move(Err); |
| 2767 | |
| 2768 | // Import the location of this declaration. |
| 2769 | ExpectedSLoc LocOrErr = import(From: D->getLocation()); |
| 2770 | if (!LocOrErr) |
| 2771 | return LocOrErr.takeError(); |
| 2772 | |
| 2773 | EmptyDecl *ToD; |
| 2774 | if (GetImportedOrCreateDecl(ToD, FromD: D, args&: Importer.getToContext(), args&: DC, args&: *LocOrErr)) |
| 2775 | return ToD; |
| 2776 | |
| 2777 | ToD->setLexicalDeclContext(LexicalDC); |
| 2778 | LexicalDC->addDeclInternal(D: ToD); |
| 2779 | return ToD; |
| 2780 | } |
| 2781 | |
| 2782 | ExpectedDecl ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 2783 | TranslationUnitDecl *ToD = |
| 2784 | Importer.getToContext().getTranslationUnitDecl(); |
| 2785 | |
| 2786 | Importer.MapImported(From: D, To: ToD); |
| 2787 | |
| 2788 | return ToD; |
| 2789 | } |
| 2790 | |
| 2791 | ExpectedDecl ASTNodeImporter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) { |
| 2792 | Error Err = Error::success(); |
| 2793 | Expr *ToAsmString = importChecked(Err, From: D->getAsmStringExpr()); |
| 2794 | SourceLocation ToAsmLoc = importChecked(Err, From: D->getAsmLoc()); |
| 2795 | SourceLocation ToRParenLoc = importChecked(Err, From: D->getRParenLoc()); |
| 2796 | if (Err) |
| 2797 | return std::move(Err); |
| 2798 | |
| 2799 | auto DCOrErr = Importer.ImportContext(FromDC: D->getDeclContext()); |
| 2800 | if (!DCOrErr) |
| 2801 | return DCOrErr.takeError(); |
| 2802 | DeclContext *DC = *DCOrErr; |
| 2803 | |
| 2804 | FileScopeAsmDecl *ToD; |
| 2805 | if (GetImportedOrCreateDecl(ToD, FromD: D, args&: Importer.getToContext(), args&: DC, args&: ToAsmString, |
| 2806 | args&: ToAsmLoc, args&: ToRParenLoc)) |
| 2807 | return ToD; |
| 2808 | |
| 2809 | ToD->setLexicalDeclContext(DC); |
| 2810 | DC->addDeclInternal(D: ToD); |
| 2811 | |
| 2812 | return ToD; |
| 2813 | } |
| 2814 | |
| 2815 | ExpectedDecl ASTNodeImporter::VisitBindingDecl(BindingDecl *D) { |
| 2816 | DeclContext *DC, *LexicalDC; |
| 2817 | DeclarationName Name; |
| 2818 | SourceLocation Loc; |
| 2819 | NamedDecl *ToND; |
| 2820 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD&: ToND, Loc)) |
| 2821 | return std::move(Err); |
| 2822 | if (ToND) |
| 2823 | return ToND; |
| 2824 | |
| 2825 | BindingDecl *ToD; |
| 2826 | if (GetImportedOrCreateDecl(ToD, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, |
| 2827 | args: Name.getAsIdentifierInfo(), args: D->getType())) |
| 2828 | return ToD; |
| 2829 | |
| 2830 | Error Err = Error::success(); |
| 2831 | QualType ToType = importChecked(Err, From: D->getType()); |
| 2832 | Expr *ToBinding = importChecked(Err, From: D->getBinding()); |
| 2833 | ValueDecl *ToDecomposedDecl = importChecked(Err, From: D->getDecomposedDecl()); |
| 2834 | if (Err) |
| 2835 | return std::move(Err); |
| 2836 | |
| 2837 | ToD->setBinding(DeclaredType: ToType, Binding: ToBinding); |
| 2838 | ToD->setDecomposedDecl(ToDecomposedDecl); |
| 2839 | addDeclToContexts(FromD: D, ToD); |
| 2840 | |
| 2841 | return ToD; |
| 2842 | } |
| 2843 | |
| 2844 | ExpectedDecl ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) { |
| 2845 | ExpectedSLoc LocOrErr = import(From: D->getLocation()); |
| 2846 | if (!LocOrErr) |
| 2847 | return LocOrErr.takeError(); |
| 2848 | auto ColonLocOrErr = import(From: D->getColonLoc()); |
| 2849 | if (!ColonLocOrErr) |
| 2850 | return ColonLocOrErr.takeError(); |
| 2851 | |
| 2852 | // Import the context of this declaration. |
| 2853 | auto DCOrErr = Importer.ImportContext(FromDC: D->getDeclContext()); |
| 2854 | if (!DCOrErr) |
| 2855 | return DCOrErr.takeError(); |
| 2856 | DeclContext *DC = *DCOrErr; |
| 2857 | |
| 2858 | AccessSpecDecl *ToD; |
| 2859 | if (GetImportedOrCreateDecl(ToD, FromD: D, args&: Importer.getToContext(), args: D->getAccess(), |
| 2860 | args&: DC, args&: *LocOrErr, args&: *ColonLocOrErr)) |
| 2861 | return ToD; |
| 2862 | |
| 2863 | // Lexical DeclContext and Semantic DeclContext |
| 2864 | // is always the same for the accessSpec. |
| 2865 | ToD->setLexicalDeclContext(DC); |
| 2866 | DC->addDeclInternal(D: ToD); |
| 2867 | |
| 2868 | return ToD; |
| 2869 | } |
| 2870 | |
| 2871 | ExpectedDecl ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 2872 | auto DCOrErr = Importer.ImportContext(FromDC: D->getDeclContext()); |
| 2873 | if (!DCOrErr) |
| 2874 | return DCOrErr.takeError(); |
| 2875 | DeclContext *DC = *DCOrErr; |
| 2876 | DeclContext *LexicalDC = DC; |
| 2877 | |
| 2878 | Error Err = Error::success(); |
| 2879 | auto ToLocation = importChecked(Err, From: D->getLocation()); |
| 2880 | auto ToRParenLoc = importChecked(Err, From: D->getRParenLoc()); |
| 2881 | auto ToAssertExpr = importChecked(Err, From: D->getAssertExpr()); |
| 2882 | auto ToMessage = importChecked(Err, From: D->getMessage()); |
| 2883 | if (Err) |
| 2884 | return std::move(Err); |
| 2885 | |
| 2886 | StaticAssertDecl *ToD; |
| 2887 | if (GetImportedOrCreateDecl( |
| 2888 | ToD, FromD: D, args&: Importer.getToContext(), args&: DC, args&: ToLocation, args&: ToAssertExpr, args&: ToMessage, |
| 2889 | args&: ToRParenLoc, args: D->isFailed())) |
| 2890 | return ToD; |
| 2891 | |
| 2892 | ToD->setLexicalDeclContext(LexicalDC); |
| 2893 | LexicalDC->addDeclInternal(D: ToD); |
| 2894 | return ToD; |
| 2895 | } |
| 2896 | |
| 2897 | ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) { |
| 2898 | // Import the major distinguishing characteristics of this namespace. |
| 2899 | DeclContext *DC, *LexicalDC; |
| 2900 | DeclarationName Name; |
| 2901 | SourceLocation Loc; |
| 2902 | NamedDecl *ToD; |
| 2903 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 2904 | return std::move(Err); |
| 2905 | if (ToD) |
| 2906 | return ToD; |
| 2907 | |
| 2908 | NamespaceDecl *MergeWithNamespace = nullptr; |
| 2909 | if (!Name) { |
| 2910 | // This is an anonymous namespace. Adopt an existing anonymous |
| 2911 | // namespace if we can. |
| 2912 | DeclContext *EnclosingDC = DC->getEnclosingNamespaceContext(); |
| 2913 | if (auto *TU = dyn_cast<TranslationUnitDecl>(Val: EnclosingDC)) |
| 2914 | MergeWithNamespace = TU->getAnonymousNamespace(); |
| 2915 | else |
| 2916 | MergeWithNamespace = |
| 2917 | cast<NamespaceDecl>(Val: EnclosingDC)->getAnonymousNamespace(); |
| 2918 | } else { |
| 2919 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 2920 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 2921 | for (auto *FoundDecl : FoundDecls) { |
| 2922 | if (!FoundDecl->isInIdentifierNamespace(NS: Decl::IDNS_Namespace)) |
| 2923 | continue; |
| 2924 | |
| 2925 | if (auto *FoundNS = dyn_cast<NamespaceDecl>(Val: FoundDecl)) { |
| 2926 | MergeWithNamespace = FoundNS; |
| 2927 | ConflictingDecls.clear(); |
| 2928 | break; |
| 2929 | } |
| 2930 | |
| 2931 | ConflictingDecls.push_back(Elt: FoundDecl); |
| 2932 | } |
| 2933 | |
| 2934 | if (!ConflictingDecls.empty()) { |
| 2935 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
| 2936 | Name, DC, IDNS: Decl::IDNS_Namespace, Decls: ConflictingDecls.data(), |
| 2937 | NumDecls: ConflictingDecls.size()); |
| 2938 | if (NameOrErr) |
| 2939 | Name = NameOrErr.get(); |
| 2940 | else |
| 2941 | return NameOrErr.takeError(); |
| 2942 | } |
| 2943 | } |
| 2944 | |
| 2945 | ExpectedSLoc BeginLocOrErr = import(From: D->getBeginLoc()); |
| 2946 | if (!BeginLocOrErr) |
| 2947 | return BeginLocOrErr.takeError(); |
| 2948 | ExpectedSLoc RBraceLocOrErr = import(From: D->getRBraceLoc()); |
| 2949 | if (!RBraceLocOrErr) |
| 2950 | return RBraceLocOrErr.takeError(); |
| 2951 | |
| 2952 | // Create the "to" namespace, if needed. |
| 2953 | NamespaceDecl *ToNamespace = MergeWithNamespace; |
| 2954 | if (!ToNamespace) { |
| 2955 | if (GetImportedOrCreateDecl(ToD&: ToNamespace, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 2956 | args: D->isInline(), args&: *BeginLocOrErr, args&: Loc, |
| 2957 | args: Name.getAsIdentifierInfo(), |
| 2958 | /*PrevDecl=*/args: nullptr, args: D->isNested())) |
| 2959 | return ToNamespace; |
| 2960 | ToNamespace->setRBraceLoc(*RBraceLocOrErr); |
| 2961 | ToNamespace->setLexicalDeclContext(LexicalDC); |
| 2962 | LexicalDC->addDeclInternal(D: ToNamespace); |
| 2963 | |
| 2964 | // If this is an anonymous namespace, register it as the anonymous |
| 2965 | // namespace within its context. |
| 2966 | if (!Name) { |
| 2967 | if (auto *TU = dyn_cast<TranslationUnitDecl>(Val: DC)) |
| 2968 | TU->setAnonymousNamespace(ToNamespace); |
| 2969 | else |
| 2970 | cast<NamespaceDecl>(Val: DC)->setAnonymousNamespace(ToNamespace); |
| 2971 | } |
| 2972 | } |
| 2973 | Importer.MapImported(From: D, To: ToNamespace); |
| 2974 | |
| 2975 | if (Error Err = ImportDeclContext(FromDC: D)) |
| 2976 | return std::move(Err); |
| 2977 | |
| 2978 | return ToNamespace; |
| 2979 | } |
| 2980 | |
| 2981 | ExpectedDecl ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
| 2982 | // Import the major distinguishing characteristics of this namespace. |
| 2983 | DeclContext *DC, *LexicalDC; |
| 2984 | DeclarationName Name; |
| 2985 | SourceLocation Loc; |
| 2986 | NamedDecl *LookupD; |
| 2987 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD&: LookupD, Loc)) |
| 2988 | return std::move(Err); |
| 2989 | if (LookupD) |
| 2990 | return LookupD; |
| 2991 | |
| 2992 | // NOTE: No conflict resolution is done for namespace aliases now. |
| 2993 | |
| 2994 | Error Err = Error::success(); |
| 2995 | auto ToNamespaceLoc = importChecked(Err, From: D->getNamespaceLoc()); |
| 2996 | auto ToAliasLoc = importChecked(Err, From: D->getAliasLoc()); |
| 2997 | auto ToQualifierLoc = importChecked(Err, From: D->getQualifierLoc()); |
| 2998 | auto ToTargetNameLoc = importChecked(Err, From: D->getTargetNameLoc()); |
| 2999 | auto ToNamespace = importChecked(Err, From: D->getNamespace()); |
| 3000 | if (Err) |
| 3001 | return std::move(Err); |
| 3002 | |
| 3003 | IdentifierInfo *ToIdentifier = Importer.Import(FromId: D->getIdentifier()); |
| 3004 | |
| 3005 | NamespaceAliasDecl *ToD; |
| 3006 | if (GetImportedOrCreateDecl( |
| 3007 | ToD, FromD: D, args&: Importer.getToContext(), args&: DC, args&: ToNamespaceLoc, args&: ToAliasLoc, |
| 3008 | args&: ToIdentifier, args&: ToQualifierLoc, args&: ToTargetNameLoc, args&: ToNamespace)) |
| 3009 | return ToD; |
| 3010 | |
| 3011 | ToD->setLexicalDeclContext(LexicalDC); |
| 3012 | LexicalDC->addDeclInternal(D: ToD); |
| 3013 | |
| 3014 | return ToD; |
| 3015 | } |
| 3016 | |
| 3017 | ExpectedDecl |
| 3018 | ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) { |
| 3019 | // Import the major distinguishing characteristics of this typedef. |
| 3020 | DeclarationName Name; |
| 3021 | SourceLocation Loc; |
| 3022 | NamedDecl *ToD; |
| 3023 | // Do not import the DeclContext, we will import it once the TypedefNameDecl |
| 3024 | // is created. |
| 3025 | if (Error Err = ImportDeclParts(D, Name, ToD, Loc)) |
| 3026 | return std::move(Err); |
| 3027 | if (ToD) |
| 3028 | return ToD; |
| 3029 | |
| 3030 | DeclContext *DC = cast_or_null<DeclContext>( |
| 3031 | Val: Importer.GetAlreadyImportedOrNull(FromD: cast<Decl>(Val: D->getDeclContext()))); |
| 3032 | DeclContext *LexicalDC = |
| 3033 | cast_or_null<DeclContext>(Val: Importer.GetAlreadyImportedOrNull( |
| 3034 | FromD: cast<Decl>(Val: D->getLexicalDeclContext()))); |
| 3035 | |
| 3036 | // If this typedef is not in block scope, determine whether we've |
| 3037 | // seen a typedef with the same name (that we can merge with) or any |
| 3038 | // other entity by that name (which name lookup could conflict with). |
| 3039 | // Note: Repeated typedefs are not valid in C99: |
| 3040 | // 'typedef int T; typedef int T;' is invalid |
| 3041 | // We do not care about this now. |
| 3042 | if (DC && !DC->isFunctionOrMethod()) { |
| 3043 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 3044 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 3045 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 3046 | for (auto *FoundDecl : FoundDecls) { |
| 3047 | if (!FoundDecl->isInIdentifierNamespace(NS: IDNS)) |
| 3048 | continue; |
| 3049 | if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(Val: FoundDecl)) { |
| 3050 | if (!hasSameVisibilityContextAndLinkage(Found: FoundTypedef, From: D)) |
| 3051 | continue; |
| 3052 | |
| 3053 | QualType FromUT = D->getUnderlyingType(); |
| 3054 | QualType FoundUT = FoundTypedef->getUnderlyingType(); |
| 3055 | if (Importer.IsStructurallyEquivalent(From: FromUT, To: FoundUT)) { |
| 3056 | // If the underlying declarations are unnamed records these can be |
| 3057 | // imported as different types. We should create a distinct typedef |
| 3058 | // node in this case. |
| 3059 | // If we found an existing underlying type with a record in a |
| 3060 | // different context (than the imported), this is already reason for |
| 3061 | // having distinct typedef nodes for these. |
| 3062 | // Again this can create situation like |
| 3063 | // 'typedef int T; typedef int T;' but this is hard to avoid without |
| 3064 | // a rename strategy at import. |
| 3065 | if (!FromUT.isNull() && !FoundUT.isNull()) { |
| 3066 | RecordDecl *FromR = FromUT->getAsRecordDecl(); |
| 3067 | RecordDecl *FoundR = FoundUT->getAsRecordDecl(); |
| 3068 | if (FromR && FoundR && |
| 3069 | !hasSameVisibilityContextAndLinkage(Found: FoundR, From: FromR)) |
| 3070 | continue; |
| 3071 | } |
| 3072 | // If the "From" context has a complete underlying type but we |
| 3073 | // already have a complete underlying type then return with that. |
| 3074 | if (!FromUT->isIncompleteType() && !FoundUT->isIncompleteType()) |
| 3075 | return Importer.MapImported(From: D, To: FoundTypedef); |
| 3076 | // FIXME Handle redecl chain. When you do that make consistent changes |
| 3077 | // in ASTImporterLookupTable too. |
| 3078 | } else { |
| 3079 | ConflictingDecls.push_back(Elt: FoundDecl); |
| 3080 | } |
| 3081 | } |
| 3082 | } |
| 3083 | |
| 3084 | if (!ConflictingDecls.empty()) { |
| 3085 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
| 3086 | Name, DC, IDNS, Decls: ConflictingDecls.data(), NumDecls: ConflictingDecls.size()); |
| 3087 | if (NameOrErr) |
| 3088 | Name = NameOrErr.get(); |
| 3089 | else |
| 3090 | return NameOrErr.takeError(); |
| 3091 | } |
| 3092 | } |
| 3093 | |
| 3094 | Error Err = Error::success(); |
| 3095 | auto ToUnderlyingType = importChecked(Err, From: D->getUnderlyingType()); |
| 3096 | auto ToTypeSourceInfo = importChecked(Err, From: D->getTypeSourceInfo()); |
| 3097 | auto ToBeginLoc = importChecked(Err, From: D->getBeginLoc()); |
| 3098 | if (Err) |
| 3099 | return std::move(Err); |
| 3100 | |
| 3101 | // Create the new typedef node. |
| 3102 | // FIXME: ToUnderlyingType is not used. |
| 3103 | (void)ToUnderlyingType; |
| 3104 | TypedefNameDecl *ToTypedef; |
| 3105 | if (IsAlias) { |
| 3106 | if (GetImportedOrCreateDecl<TypeAliasDecl>( |
| 3107 | ToD&: ToTypedef, FromD: D, args&: Importer.getToContext(), args&: DC, args&: ToBeginLoc, args&: Loc, |
| 3108 | args: Name.getAsIdentifierInfo(), args&: ToTypeSourceInfo)) |
| 3109 | return ToTypedef; |
| 3110 | } else if (GetImportedOrCreateDecl<TypedefDecl>( |
| 3111 | ToD&: ToTypedef, FromD: D, args&: Importer.getToContext(), args&: DC, args&: ToBeginLoc, args&: Loc, |
| 3112 | args: Name.getAsIdentifierInfo(), args&: ToTypeSourceInfo)) |
| 3113 | return ToTypedef; |
| 3114 | |
| 3115 | // Import the DeclContext and set it to the Typedef. |
| 3116 | if ((Err = ImportDeclContext(FromD: D, ToDC&: DC, ToLexicalDC&: LexicalDC))) |
| 3117 | return std::move(Err); |
| 3118 | ToTypedef->setDeclContext(DC); |
| 3119 | ToTypedef->setLexicalDeclContext(LexicalDC); |
| 3120 | // Add to the lookupTable because we could not do that in MapImported. |
| 3121 | Importer.AddToLookupTable(ToD: ToTypedef); |
| 3122 | |
| 3123 | ToTypedef->setAccess(D->getAccess()); |
| 3124 | |
| 3125 | // Templated declarations should not appear in DeclContext. |
| 3126 | TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(Val: D) : nullptr; |
| 3127 | if (!FromAlias || !FromAlias->getDescribedAliasTemplate()) |
| 3128 | LexicalDC->addDeclInternal(D: ToTypedef); |
| 3129 | |
| 3130 | return ToTypedef; |
| 3131 | } |
| 3132 | |
| 3133 | ExpectedDecl ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) { |
| 3134 | return VisitTypedefNameDecl(D, /*IsAlias=*/false); |
| 3135 | } |
| 3136 | |
| 3137 | ExpectedDecl ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| 3138 | return VisitTypedefNameDecl(D, /*IsAlias=*/true); |
| 3139 | } |
| 3140 | |
| 3141 | ExpectedDecl |
| 3142 | ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { |
| 3143 | // Import the major distinguishing characteristics of this typedef. |
| 3144 | DeclContext *DC, *LexicalDC; |
| 3145 | DeclarationName Name; |
| 3146 | SourceLocation Loc; |
| 3147 | NamedDecl *FoundD; |
| 3148 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD&: FoundD, Loc)) |
| 3149 | return std::move(Err); |
| 3150 | if (FoundD) |
| 3151 | return FoundD; |
| 3152 | |
| 3153 | // If this typedef is not in block scope, determine whether we've |
| 3154 | // seen a typedef with the same name (that we can merge with) or any |
| 3155 | // other entity by that name (which name lookup could conflict with). |
| 3156 | if (!DC->isFunctionOrMethod()) { |
| 3157 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 3158 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 3159 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 3160 | for (auto *FoundDecl : FoundDecls) { |
| 3161 | if (!FoundDecl->isInIdentifierNamespace(NS: IDNS)) |
| 3162 | continue; |
| 3163 | if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(Val: FoundDecl)) { |
| 3164 | if (IsStructuralMatch(From: D, To: FoundAlias)) |
| 3165 | return Importer.MapImported(From: D, To: FoundAlias); |
| 3166 | ConflictingDecls.push_back(Elt: FoundDecl); |
| 3167 | } |
| 3168 | } |
| 3169 | |
| 3170 | if (!ConflictingDecls.empty()) { |
| 3171 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
| 3172 | Name, DC, IDNS, Decls: ConflictingDecls.data(), NumDecls: ConflictingDecls.size()); |
| 3173 | if (NameOrErr) |
| 3174 | Name = NameOrErr.get(); |
| 3175 | else |
| 3176 | return NameOrErr.takeError(); |
| 3177 | } |
| 3178 | } |
| 3179 | |
| 3180 | Error Err = Error::success(); |
| 3181 | auto ToTemplateParameters = importChecked(Err, From: D->getTemplateParameters()); |
| 3182 | auto ToTemplatedDecl = importChecked(Err, From: D->getTemplatedDecl()); |
| 3183 | if (Err) |
| 3184 | return std::move(Err); |
| 3185 | |
| 3186 | TypeAliasTemplateDecl *ToAlias; |
| 3187 | if (GetImportedOrCreateDecl(ToD&: ToAlias, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, |
| 3188 | args&: Name, args&: ToTemplateParameters, args&: ToTemplatedDecl)) |
| 3189 | return ToAlias; |
| 3190 | |
| 3191 | ToTemplatedDecl->setDescribedAliasTemplate(ToAlias); |
| 3192 | |
| 3193 | ToAlias->setAccess(D->getAccess()); |
| 3194 | ToAlias->setLexicalDeclContext(LexicalDC); |
| 3195 | LexicalDC->addDeclInternal(D: ToAlias); |
| 3196 | if (DC != Importer.getToContext().getTranslationUnitDecl()) |
| 3197 | updateLookupTableForTemplateParameters(Params&: *ToTemplateParameters); |
| 3198 | return ToAlias; |
| 3199 | } |
| 3200 | |
| 3201 | ExpectedDecl ASTNodeImporter::VisitLabelDecl(LabelDecl *D) { |
| 3202 | // Import the major distinguishing characteristics of this label. |
| 3203 | DeclContext *DC, *LexicalDC; |
| 3204 | DeclarationName Name; |
| 3205 | SourceLocation Loc; |
| 3206 | NamedDecl *ToD; |
| 3207 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 3208 | return std::move(Err); |
| 3209 | if (ToD) |
| 3210 | return ToD; |
| 3211 | |
| 3212 | assert(LexicalDC->isFunctionOrMethod()); |
| 3213 | |
| 3214 | LabelDecl *ToLabel; |
| 3215 | if (D->isGnuLocal()) { |
| 3216 | ExpectedSLoc BeginLocOrErr = import(From: D->getBeginLoc()); |
| 3217 | if (!BeginLocOrErr) |
| 3218 | return BeginLocOrErr.takeError(); |
| 3219 | if (GetImportedOrCreateDecl(ToD&: ToLabel, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, |
| 3220 | args: Name.getAsIdentifierInfo(), args&: *BeginLocOrErr)) |
| 3221 | return ToLabel; |
| 3222 | |
| 3223 | } else { |
| 3224 | if (GetImportedOrCreateDecl(ToD&: ToLabel, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, |
| 3225 | args: Name.getAsIdentifierInfo())) |
| 3226 | return ToLabel; |
| 3227 | |
| 3228 | } |
| 3229 | |
| 3230 | Expected<LabelStmt *> ToStmtOrErr = import(From: D->getStmt()); |
| 3231 | if (!ToStmtOrErr) |
| 3232 | return ToStmtOrErr.takeError(); |
| 3233 | |
| 3234 | ToLabel->setStmt(*ToStmtOrErr); |
| 3235 | ToLabel->setLexicalDeclContext(LexicalDC); |
| 3236 | LexicalDC->addDeclInternal(D: ToLabel); |
| 3237 | return ToLabel; |
| 3238 | } |
| 3239 | |
| 3240 | ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) { |
| 3241 | // Import the major distinguishing characteristics of this enum. |
| 3242 | DeclContext *DC, *LexicalDC; |
| 3243 | DeclarationName Name; |
| 3244 | SourceLocation Loc; |
| 3245 | NamedDecl *ToD; |
| 3246 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 3247 | return std::move(Err); |
| 3248 | if (ToD) |
| 3249 | return ToD; |
| 3250 | |
| 3251 | // Figure out what enum name we're looking for. |
| 3252 | unsigned IDNS = Decl::IDNS_Tag; |
| 3253 | DeclarationName SearchName = Name; |
| 3254 | if (!SearchName && D->getTypedefNameForAnonDecl()) { |
| 3255 | if (Error Err = importInto( |
| 3256 | To&: SearchName, From: D->getTypedefNameForAnonDecl()->getDeclName())) |
| 3257 | return std::move(Err); |
| 3258 | IDNS = Decl::IDNS_Ordinary; |
| 3259 | } else if (Importer.getToContext().getLangOpts().CPlusPlus) |
| 3260 | IDNS |= Decl::IDNS_Ordinary; |
| 3261 | |
| 3262 | // We may already have an enum of the same name; try to find and match it. |
| 3263 | EnumDecl *PrevDecl = nullptr; |
| 3264 | if (!DC->isFunctionOrMethod()) { |
| 3265 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 3266 | auto FoundDecls = |
| 3267 | Importer.findDeclsInToCtx(DC, Name: SearchName); |
| 3268 | for (auto *FoundDecl : FoundDecls) { |
| 3269 | if (!FoundDecl->isInIdentifierNamespace(NS: IDNS)) |
| 3270 | continue; |
| 3271 | |
| 3272 | if (auto *Typedef = dyn_cast<TypedefNameDecl>(Val: FoundDecl)) { |
| 3273 | if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) |
| 3274 | FoundDecl = Tag->getDecl(); |
| 3275 | } |
| 3276 | |
| 3277 | if (auto *FoundEnum = dyn_cast<EnumDecl>(Val: FoundDecl)) { |
| 3278 | if (!hasSameVisibilityContextAndLinkage(Found: FoundEnum, From: D)) |
| 3279 | continue; |
| 3280 | if (IsStructuralMatch(From: D, To: FoundEnum, Complain: !SearchName.isEmpty())) { |
| 3281 | EnumDecl *FoundDef = FoundEnum->getDefinition(); |
| 3282 | if (D->isThisDeclarationADefinition() && FoundDef) |
| 3283 | return Importer.MapImported(From: D, To: FoundDef); |
| 3284 | PrevDecl = FoundEnum->getMostRecentDecl(); |
| 3285 | break; |
| 3286 | } |
| 3287 | ConflictingDecls.push_back(Elt: FoundDecl); |
| 3288 | } |
| 3289 | } |
| 3290 | |
| 3291 | // In case of unnamed enums, we try to find an existing similar one, if none |
| 3292 | // was found, perform the import always. |
| 3293 | // Structural in-equivalence is not detected in this way here, but it may |
| 3294 | // be found when the parent decl is imported (if the enum is part of a |
| 3295 | // class). To make this totally exact a more difficult solution is needed. |
| 3296 | if (SearchName && !ConflictingDecls.empty()) { |
| 3297 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
| 3298 | Name: SearchName, DC, IDNS, Decls: ConflictingDecls.data(), |
| 3299 | NumDecls: ConflictingDecls.size()); |
| 3300 | if (NameOrErr) |
| 3301 | Name = NameOrErr.get(); |
| 3302 | else |
| 3303 | return NameOrErr.takeError(); |
| 3304 | } |
| 3305 | } |
| 3306 | |
| 3307 | Error Err = Error::success(); |
| 3308 | auto ToBeginLoc = importChecked(Err, From: D->getBeginLoc()); |
| 3309 | auto ToQualifierLoc = importChecked(Err, From: D->getQualifierLoc()); |
| 3310 | auto ToIntegerType = importChecked(Err, From: D->getIntegerType()); |
| 3311 | auto ToBraceRange = importChecked(Err, From: D->getBraceRange()); |
| 3312 | if (Err) |
| 3313 | return std::move(Err); |
| 3314 | |
| 3315 | // Create the enum declaration. |
| 3316 | EnumDecl *D2; |
| 3317 | if (GetImportedOrCreateDecl( |
| 3318 | ToD&: D2, FromD: D, args&: Importer.getToContext(), args&: DC, args&: ToBeginLoc, |
| 3319 | args&: Loc, args: Name.getAsIdentifierInfo(), args&: PrevDecl, args: D->isScoped(), |
| 3320 | args: D->isScopedUsingClassTag(), args: D->isFixed())) |
| 3321 | return D2; |
| 3322 | |
| 3323 | D2->setQualifierInfo(ToQualifierLoc); |
| 3324 | D2->setIntegerType(ToIntegerType); |
| 3325 | D2->setBraceRange(ToBraceRange); |
| 3326 | D2->setAccess(D->getAccess()); |
| 3327 | D2->setLexicalDeclContext(LexicalDC); |
| 3328 | addDeclToContexts(FromD: D, ToD: D2); |
| 3329 | |
| 3330 | if (MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo()) { |
| 3331 | TemplateSpecializationKind SK = MemberInfo->getTemplateSpecializationKind(); |
| 3332 | EnumDecl *FromInst = D->getInstantiatedFromMemberEnum(); |
| 3333 | if (Expected<EnumDecl *> ToInstOrErr = import(From: FromInst)) |
| 3334 | D2->setInstantiationOfMemberEnum(ED: *ToInstOrErr, TSK: SK); |
| 3335 | else |
| 3336 | return ToInstOrErr.takeError(); |
| 3337 | if (ExpectedSLoc POIOrErr = import(From: MemberInfo->getPointOfInstantiation())) |
| 3338 | D2->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr); |
| 3339 | else |
| 3340 | return POIOrErr.takeError(); |
| 3341 | } |
| 3342 | |
| 3343 | // Import the definition |
| 3344 | if (D->isCompleteDefinition()) |
| 3345 | if (Error Err = ImportDefinition(From: D, To: D2)) |
| 3346 | return std::move(Err); |
| 3347 | |
| 3348 | return D2; |
| 3349 | } |
| 3350 | |
| 3351 | ExpectedDecl ASTNodeImporter::VisitRecordDecl(RecordDecl *D) { |
| 3352 | bool IsFriendTemplate = false; |
| 3353 | if (auto *DCXX = dyn_cast<CXXRecordDecl>(Val: D)) { |
| 3354 | IsFriendTemplate = |
| 3355 | DCXX->getDescribedClassTemplate() && |
| 3356 | DCXX->getDescribedClassTemplate()->getFriendObjectKind() != |
| 3357 | Decl::FOK_None; |
| 3358 | } |
| 3359 | |
| 3360 | // Import the major distinguishing characteristics of this record. |
| 3361 | DeclContext *DC = nullptr, *LexicalDC = nullptr; |
| 3362 | DeclarationName Name; |
| 3363 | SourceLocation Loc; |
| 3364 | NamedDecl *ToD = nullptr; |
| 3365 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 3366 | return std::move(Err); |
| 3367 | if (ToD) |
| 3368 | return ToD; |
| 3369 | |
| 3370 | // Figure out what structure name we're looking for. |
| 3371 | unsigned IDNS = Decl::IDNS_Tag; |
| 3372 | DeclarationName SearchName = Name; |
| 3373 | if (!SearchName && D->getTypedefNameForAnonDecl()) { |
| 3374 | if (Error Err = importInto( |
| 3375 | To&: SearchName, From: D->getTypedefNameForAnonDecl()->getDeclName())) |
| 3376 | return std::move(Err); |
| 3377 | IDNS = Decl::IDNS_Ordinary; |
| 3378 | } else if (Importer.getToContext().getLangOpts().CPlusPlus) |
| 3379 | IDNS |= Decl::IDNS_Ordinary | Decl::IDNS_TagFriend; |
| 3380 | |
| 3381 | bool IsDependentContext = DC != LexicalDC ? LexicalDC->isDependentContext() |
| 3382 | : DC->isDependentContext(); |
| 3383 | bool DependentFriend = IsFriendTemplate && IsDependentContext; |
| 3384 | |
| 3385 | // We may already have a record of the same name; try to find and match it. |
| 3386 | RecordDecl *PrevDecl = nullptr; |
| 3387 | if (!DependentFriend && !DC->isFunctionOrMethod() && !D->isLambda()) { |
| 3388 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 3389 | auto FoundDecls = |
| 3390 | Importer.findDeclsInToCtx(DC, Name: SearchName); |
| 3391 | if (!FoundDecls.empty()) { |
| 3392 | // We're going to have to compare D against potentially conflicting Decls, |
| 3393 | // so complete it. |
| 3394 | if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition()) |
| 3395 | D->getASTContext().getExternalSource()->CompleteType(Tag: D); |
| 3396 | } |
| 3397 | |
| 3398 | for (auto *FoundDecl : FoundDecls) { |
| 3399 | if (!FoundDecl->isInIdentifierNamespace(NS: IDNS)) |
| 3400 | continue; |
| 3401 | |
| 3402 | Decl *Found = FoundDecl; |
| 3403 | if (auto *Typedef = dyn_cast<TypedefNameDecl>(Val: Found)) { |
| 3404 | if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) |
| 3405 | Found = Tag->getDecl(); |
| 3406 | } |
| 3407 | |
| 3408 | if (auto *FoundRecord = dyn_cast<RecordDecl>(Val: Found)) { |
| 3409 | // Do not emit false positive diagnostic in case of unnamed |
| 3410 | // struct/union and in case of anonymous structs. Would be false |
| 3411 | // because there may be several anonymous/unnamed structs in a class. |
| 3412 | // E.g. these are both valid: |
| 3413 | // struct A { // unnamed structs |
| 3414 | // struct { struct A *next; } entry0; |
| 3415 | // struct { struct A *next; } entry1; |
| 3416 | // }; |
| 3417 | // struct X { struct { int a; }; struct { int b; }; }; // anon structs |
| 3418 | if (!SearchName) |
| 3419 | if (!IsStructuralMatch(From: D, To: FoundRecord, Complain: false)) |
| 3420 | continue; |
| 3421 | |
| 3422 | if (!hasSameVisibilityContextAndLinkage(Found: FoundRecord, From: D)) |
| 3423 | continue; |
| 3424 | |
| 3425 | if (IsStructuralMatch(From: D, To: FoundRecord)) { |
| 3426 | RecordDecl *FoundDef = FoundRecord->getDefinition(); |
| 3427 | if (D->isThisDeclarationADefinition() && FoundDef) { |
| 3428 | // FIXME: Structural equivalence check should check for same |
| 3429 | // user-defined methods. |
| 3430 | Importer.MapImported(From: D, To: FoundDef); |
| 3431 | if (const auto *DCXX = dyn_cast<CXXRecordDecl>(Val: D)) { |
| 3432 | auto *FoundCXX = dyn_cast<CXXRecordDecl>(Val: FoundDef); |
| 3433 | assert(FoundCXX && "Record type mismatch" ); |
| 3434 | |
| 3435 | if (!Importer.isMinimalImport()) |
| 3436 | // FoundDef may not have every implicit method that D has |
| 3437 | // because implicit methods are created only if they are used. |
| 3438 | if (Error Err = ImportImplicitMethods(From: DCXX, To: FoundCXX)) |
| 3439 | return std::move(Err); |
| 3440 | } |
| 3441 | // FIXME: We can return FoundDef here. |
| 3442 | } |
| 3443 | PrevDecl = FoundRecord->getMostRecentDecl(); |
| 3444 | break; |
| 3445 | } |
| 3446 | ConflictingDecls.push_back(Elt: FoundDecl); |
| 3447 | } // kind is RecordDecl |
| 3448 | } // for |
| 3449 | |
| 3450 | if (!ConflictingDecls.empty() && SearchName) { |
| 3451 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
| 3452 | Name: SearchName, DC, IDNS, Decls: ConflictingDecls.data(), |
| 3453 | NumDecls: ConflictingDecls.size()); |
| 3454 | if (NameOrErr) |
| 3455 | Name = NameOrErr.get(); |
| 3456 | else |
| 3457 | return NameOrErr.takeError(); |
| 3458 | } |
| 3459 | } |
| 3460 | |
| 3461 | ExpectedSLoc BeginLocOrErr = import(From: D->getBeginLoc()); |
| 3462 | if (!BeginLocOrErr) |
| 3463 | return BeginLocOrErr.takeError(); |
| 3464 | |
| 3465 | // Create the record declaration. |
| 3466 | RecordDecl *D2 = nullptr; |
| 3467 | CXXRecordDecl *D2CXX = nullptr; |
| 3468 | if (auto *DCXX = dyn_cast<CXXRecordDecl>(Val: D)) { |
| 3469 | if (DCXX->isLambda()) { |
| 3470 | auto TInfoOrErr = import(From: DCXX->getLambdaTypeInfo()); |
| 3471 | if (!TInfoOrErr) |
| 3472 | return TInfoOrErr.takeError(); |
| 3473 | if (GetImportedOrCreateSpecialDecl( |
| 3474 | ToD&: D2CXX, CreateFun: CXXRecordDecl::CreateLambda, FromD: D, args&: Importer.getToContext(), |
| 3475 | args&: DC, args&: *TInfoOrErr, args&: Loc, args: DCXX->getLambdaDependencyKind(), |
| 3476 | args: DCXX->isGenericLambda(), args: DCXX->getLambdaCaptureDefault())) |
| 3477 | return D2CXX; |
| 3478 | Decl *ContextDecl = DCXX->getLambdaContextDecl(); |
| 3479 | ExpectedDecl CDeclOrErr = import(From: ContextDecl); |
| 3480 | if (!CDeclOrErr) |
| 3481 | return CDeclOrErr.takeError(); |
| 3482 | if (ContextDecl != nullptr) { |
| 3483 | D2CXX->setLambdaContextDecl(*CDeclOrErr); |
| 3484 | } |
| 3485 | D2CXX->setLambdaNumbering(DCXX->getLambdaNumbering()); |
| 3486 | } else { |
| 3487 | if (GetImportedOrCreateDecl(ToD&: D2CXX, FromD: D, args&: Importer.getToContext(), |
| 3488 | args: D->getTagKind(), args&: DC, args&: *BeginLocOrErr, args&: Loc, |
| 3489 | args: Name.getAsIdentifierInfo(), |
| 3490 | args: cast_or_null<CXXRecordDecl>(Val: PrevDecl))) |
| 3491 | return D2CXX; |
| 3492 | } |
| 3493 | |
| 3494 | D2 = D2CXX; |
| 3495 | D2->setAccess(D->getAccess()); |
| 3496 | D2->setLexicalDeclContext(LexicalDC); |
| 3497 | addDeclToContexts(FromD: D, ToD: D2); |
| 3498 | |
| 3499 | if (ClassTemplateDecl *FromDescribed = |
| 3500 | DCXX->getDescribedClassTemplate()) { |
| 3501 | ClassTemplateDecl *ToDescribed; |
| 3502 | if (Error Err = importInto(To&: ToDescribed, From: FromDescribed)) |
| 3503 | return std::move(Err); |
| 3504 | D2CXX->setDescribedClassTemplate(ToDescribed); |
| 3505 | } else if (MemberSpecializationInfo *MemberInfo = |
| 3506 | DCXX->getMemberSpecializationInfo()) { |
| 3507 | TemplateSpecializationKind SK = |
| 3508 | MemberInfo->getTemplateSpecializationKind(); |
| 3509 | CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass(); |
| 3510 | |
| 3511 | if (Expected<CXXRecordDecl *> ToInstOrErr = import(From: FromInst)) |
| 3512 | D2CXX->setInstantiationOfMemberClass(RD: *ToInstOrErr, TSK: SK); |
| 3513 | else |
| 3514 | return ToInstOrErr.takeError(); |
| 3515 | |
| 3516 | if (ExpectedSLoc POIOrErr = |
| 3517 | import(From: MemberInfo->getPointOfInstantiation())) |
| 3518 | D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation( |
| 3519 | *POIOrErr); |
| 3520 | else |
| 3521 | return POIOrErr.takeError(); |
| 3522 | } |
| 3523 | |
| 3524 | } else { |
| 3525 | if (GetImportedOrCreateDecl(ToD&: D2, FromD: D, args&: Importer.getToContext(), |
| 3526 | args: D->getTagKind(), args&: DC, args&: *BeginLocOrErr, args&: Loc, |
| 3527 | args: Name.getAsIdentifierInfo(), args&: PrevDecl)) |
| 3528 | return D2; |
| 3529 | D2->setLexicalDeclContext(LexicalDC); |
| 3530 | addDeclToContexts(FromD: D, ToD: D2); |
| 3531 | } |
| 3532 | |
| 3533 | if (auto BraceRangeOrErr = import(From: D->getBraceRange())) |
| 3534 | D2->setBraceRange(*BraceRangeOrErr); |
| 3535 | else |
| 3536 | return BraceRangeOrErr.takeError(); |
| 3537 | if (auto QualifierLocOrErr = import(From: D->getQualifierLoc())) |
| 3538 | D2->setQualifierInfo(*QualifierLocOrErr); |
| 3539 | else |
| 3540 | return QualifierLocOrErr.takeError(); |
| 3541 | |
| 3542 | if (D->isAnonymousStructOrUnion()) |
| 3543 | D2->setAnonymousStructOrUnion(true); |
| 3544 | |
| 3545 | if (D->isCompleteDefinition()) |
| 3546 | if (Error Err = ImportDefinition(From: D, To: D2, Kind: IDK_Default)) |
| 3547 | return std::move(Err); |
| 3548 | |
| 3549 | return D2; |
| 3550 | } |
| 3551 | |
| 3552 | ExpectedDecl ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 3553 | // Import the major distinguishing characteristics of this enumerator. |
| 3554 | DeclContext *DC, *LexicalDC; |
| 3555 | DeclarationName Name; |
| 3556 | SourceLocation Loc; |
| 3557 | NamedDecl *ToD; |
| 3558 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 3559 | return std::move(Err); |
| 3560 | if (ToD) |
| 3561 | return ToD; |
| 3562 | |
| 3563 | // Determine whether there are any other declarations with the same name and |
| 3564 | // in the same context. |
| 3565 | if (!LexicalDC->isFunctionOrMethod()) { |
| 3566 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 3567 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 3568 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 3569 | for (auto *FoundDecl : FoundDecls) { |
| 3570 | if (!FoundDecl->isInIdentifierNamespace(NS: IDNS)) |
| 3571 | continue; |
| 3572 | |
| 3573 | if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(Val: FoundDecl)) { |
| 3574 | if (IsStructuralMatch(From: D, To: FoundEnumConstant)) |
| 3575 | return Importer.MapImported(From: D, To: FoundEnumConstant); |
| 3576 | ConflictingDecls.push_back(Elt: FoundDecl); |
| 3577 | } |
| 3578 | } |
| 3579 | |
| 3580 | if (!ConflictingDecls.empty()) { |
| 3581 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
| 3582 | Name, DC, IDNS, Decls: ConflictingDecls.data(), NumDecls: ConflictingDecls.size()); |
| 3583 | if (NameOrErr) |
| 3584 | Name = NameOrErr.get(); |
| 3585 | else |
| 3586 | return NameOrErr.takeError(); |
| 3587 | } |
| 3588 | } |
| 3589 | |
| 3590 | ExpectedType TypeOrErr = import(From: D->getType()); |
| 3591 | if (!TypeOrErr) |
| 3592 | return TypeOrErr.takeError(); |
| 3593 | |
| 3594 | ExpectedExpr InitOrErr = import(From: D->getInitExpr()); |
| 3595 | if (!InitOrErr) |
| 3596 | return InitOrErr.takeError(); |
| 3597 | |
| 3598 | EnumConstantDecl *ToEnumerator; |
| 3599 | if (GetImportedOrCreateDecl( |
| 3600 | ToD&: ToEnumerator, FromD: D, args&: Importer.getToContext(), args: cast<EnumDecl>(Val: DC), args&: Loc, |
| 3601 | args: Name.getAsIdentifierInfo(), args&: *TypeOrErr, args&: *InitOrErr, args: D->getInitVal())) |
| 3602 | return ToEnumerator; |
| 3603 | |
| 3604 | ToEnumerator->setAccess(D->getAccess()); |
| 3605 | ToEnumerator->setLexicalDeclContext(LexicalDC); |
| 3606 | LexicalDC->addDeclInternal(D: ToEnumerator); |
| 3607 | return ToEnumerator; |
| 3608 | } |
| 3609 | |
| 3610 | template <typename DeclTy> |
| 3611 | Error ASTNodeImporter::ImportTemplateParameterLists(const DeclTy *FromD, |
| 3612 | DeclTy *ToD) { |
| 3613 | ArrayRef<TemplateParameterList *> FromTPLs = |
| 3614 | FromD->getTemplateParameterLists(); |
| 3615 | if (FromTPLs.empty()) |
| 3616 | return Error::success(); |
| 3617 | SmallVector<TemplateParameterList *, 2> ToTPLists(FromTPLs.size()); |
| 3618 | for (unsigned int I = 0; I < FromTPLs.size(); ++I) |
| 3619 | if (Expected<TemplateParameterList *> ToTPListOrErr = import(From: FromTPLs[I])) |
| 3620 | ToTPLists[I] = *ToTPListOrErr; |
| 3621 | else |
| 3622 | return ToTPListOrErr.takeError(); |
| 3623 | ToD->setTemplateParameterListsInfo(Importer.ToContext, ToTPLists); |
| 3624 | return Error::success(); |
| 3625 | } |
| 3626 | |
| 3627 | Error ASTNodeImporter::ImportTemplateInformation( |
| 3628 | FunctionDecl *FromFD, FunctionDecl *ToFD) { |
| 3629 | switch (FromFD->getTemplatedKind()) { |
| 3630 | case FunctionDecl::TK_NonTemplate: |
| 3631 | case FunctionDecl::TK_FunctionTemplate: |
| 3632 | return Error::success(); |
| 3633 | |
| 3634 | case FunctionDecl::TK_DependentNonTemplate: |
| 3635 | if (Expected<FunctionDecl *> InstFDOrErr = |
| 3636 | import(From: FromFD->getInstantiatedFromDecl())) |
| 3637 | ToFD->setInstantiatedFromDecl(*InstFDOrErr); |
| 3638 | return Error::success(); |
| 3639 | case FunctionDecl::TK_MemberSpecialization: { |
| 3640 | TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind(); |
| 3641 | |
| 3642 | if (Expected<FunctionDecl *> InstFDOrErr = |
| 3643 | import(From: FromFD->getInstantiatedFromMemberFunction())) |
| 3644 | ToFD->setInstantiationOfMemberFunction(FD: *InstFDOrErr, TSK); |
| 3645 | else |
| 3646 | return InstFDOrErr.takeError(); |
| 3647 | |
| 3648 | if (ExpectedSLoc POIOrErr = import( |
| 3649 | From: FromFD->getMemberSpecializationInfo()->getPointOfInstantiation())) |
| 3650 | ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr); |
| 3651 | else |
| 3652 | return POIOrErr.takeError(); |
| 3653 | |
| 3654 | return Error::success(); |
| 3655 | } |
| 3656 | |
| 3657 | case FunctionDecl::TK_FunctionTemplateSpecialization: { |
| 3658 | auto FunctionAndArgsOrErr = |
| 3659 | ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD); |
| 3660 | if (!FunctionAndArgsOrErr) |
| 3661 | return FunctionAndArgsOrErr.takeError(); |
| 3662 | |
| 3663 | TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy( |
| 3664 | Context&: Importer.getToContext(), Args: std::get<1>(t&: *FunctionAndArgsOrErr)); |
| 3665 | |
| 3666 | auto *FTSInfo = FromFD->getTemplateSpecializationInfo(); |
| 3667 | TemplateArgumentListInfo ToTAInfo; |
| 3668 | const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten; |
| 3669 | if (FromTAArgsAsWritten) |
| 3670 | if (Error Err = ImportTemplateArgumentListInfo( |
| 3671 | From: *FromTAArgsAsWritten, Result&: ToTAInfo)) |
| 3672 | return Err; |
| 3673 | |
| 3674 | ExpectedSLoc POIOrErr = import(From: FTSInfo->getPointOfInstantiation()); |
| 3675 | if (!POIOrErr) |
| 3676 | return POIOrErr.takeError(); |
| 3677 | |
| 3678 | if (Error Err = ImportTemplateParameterLists(FromD: FromFD, ToD: ToFD)) |
| 3679 | return Err; |
| 3680 | |
| 3681 | TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind(); |
| 3682 | ToFD->setFunctionTemplateSpecialization( |
| 3683 | Template: std::get<0>(t&: *FunctionAndArgsOrErr), TemplateArgs: ToTAList, /* InsertPos= */ nullptr, |
| 3684 | TSK, TemplateArgsAsWritten: FromTAArgsAsWritten ? &ToTAInfo : nullptr, PointOfInstantiation: *POIOrErr); |
| 3685 | return Error::success(); |
| 3686 | } |
| 3687 | |
| 3688 | case FunctionDecl::TK_DependentFunctionTemplateSpecialization: { |
| 3689 | auto *FromInfo = FromFD->getDependentSpecializationInfo(); |
| 3690 | UnresolvedSet<8> Candidates; |
| 3691 | for (FunctionTemplateDecl *FTD : FromInfo->getCandidates()) { |
| 3692 | if (Expected<FunctionTemplateDecl *> ToFTDOrErr = import(From: FTD)) |
| 3693 | Candidates.addDecl(D: *ToFTDOrErr); |
| 3694 | else |
| 3695 | return ToFTDOrErr.takeError(); |
| 3696 | } |
| 3697 | |
| 3698 | // Import TemplateArgumentListInfo. |
| 3699 | TemplateArgumentListInfo ToTAInfo; |
| 3700 | const auto *FromTAArgsAsWritten = FromInfo->TemplateArgumentsAsWritten; |
| 3701 | if (FromTAArgsAsWritten) |
| 3702 | if (Error Err = |
| 3703 | ImportTemplateArgumentListInfo(From: *FromTAArgsAsWritten, Result&: ToTAInfo)) |
| 3704 | return Err; |
| 3705 | |
| 3706 | ToFD->setDependentTemplateSpecialization( |
| 3707 | Context&: Importer.getToContext(), Templates: Candidates, |
| 3708 | TemplateArgs: FromTAArgsAsWritten ? &ToTAInfo : nullptr); |
| 3709 | return Error::success(); |
| 3710 | } |
| 3711 | } |
| 3712 | llvm_unreachable("All cases should be covered!" ); |
| 3713 | } |
| 3714 | |
| 3715 | Expected<FunctionDecl *> |
| 3716 | ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) { |
| 3717 | auto FunctionAndArgsOrErr = |
| 3718 | ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD); |
| 3719 | if (!FunctionAndArgsOrErr) |
| 3720 | return FunctionAndArgsOrErr.takeError(); |
| 3721 | |
| 3722 | FunctionTemplateDecl *Template; |
| 3723 | TemplateArgsTy ToTemplArgs; |
| 3724 | std::tie(args&: Template, args&: ToTemplArgs) = *FunctionAndArgsOrErr; |
| 3725 | void *InsertPos = nullptr; |
| 3726 | auto *FoundSpec = Template->findSpecialization(Args: ToTemplArgs, InsertPos); |
| 3727 | return FoundSpec; |
| 3728 | } |
| 3729 | |
| 3730 | Error ASTNodeImporter::ImportFunctionDeclBody(FunctionDecl *FromFD, |
| 3731 | FunctionDecl *ToFD) { |
| 3732 | if (Stmt *FromBody = FromFD->getBody()) { |
| 3733 | if (ExpectedStmt ToBodyOrErr = import(From: FromBody)) |
| 3734 | ToFD->setBody(*ToBodyOrErr); |
| 3735 | else |
| 3736 | return ToBodyOrErr.takeError(); |
| 3737 | } |
| 3738 | return Error::success(); |
| 3739 | } |
| 3740 | |
| 3741 | // Returns true if the given D has a DeclContext up to the TranslationUnitDecl |
| 3742 | // which is equal to the given DC, or D is equal to DC. |
| 3743 | static bool isAncestorDeclContextOf(const DeclContext *DC, const Decl *D) { |
| 3744 | const DeclContext *DCi = dyn_cast<DeclContext>(Val: D); |
| 3745 | if (!DCi) |
| 3746 | DCi = D->getDeclContext(); |
| 3747 | assert(DCi && "Declaration should have a context" ); |
| 3748 | while (DCi != D->getTranslationUnitDecl()) { |
| 3749 | if (DCi == DC) |
| 3750 | return true; |
| 3751 | DCi = DCi->getParent(); |
| 3752 | } |
| 3753 | return false; |
| 3754 | } |
| 3755 | |
| 3756 | // Check if there is a declaration that has 'DC' as parent context and is |
| 3757 | // referenced from statement 'S' or one of its children. The search is done in |
| 3758 | // BFS order through children of 'S'. |
| 3759 | static bool isAncestorDeclContextOf(const DeclContext *DC, const Stmt *S) { |
| 3760 | SmallVector<const Stmt *> ToProcess; |
| 3761 | ToProcess.push_back(Elt: S); |
| 3762 | while (!ToProcess.empty()) { |
| 3763 | const Stmt *CurrentS = ToProcess.pop_back_val(); |
| 3764 | ToProcess.append(in_start: CurrentS->child_begin(), in_end: CurrentS->child_end()); |
| 3765 | if (const auto *DeclRef = dyn_cast<DeclRefExpr>(Val: CurrentS)) { |
| 3766 | if (const Decl *D = DeclRef->getDecl()) |
| 3767 | if (isAncestorDeclContextOf(DC, D)) |
| 3768 | return true; |
| 3769 | } else if (const auto *E = |
| 3770 | dyn_cast_or_null<SubstNonTypeTemplateParmExpr>(Val: CurrentS)) { |
| 3771 | if (const Decl *D = E->getAssociatedDecl()) |
| 3772 | if (isAncestorDeclContextOf(DC, D)) |
| 3773 | return true; |
| 3774 | } |
| 3775 | } |
| 3776 | return false; |
| 3777 | } |
| 3778 | |
| 3779 | namespace { |
| 3780 | /// Check if a type has any reference to a declaration that is inside the body |
| 3781 | /// of a function. |
| 3782 | /// The \c CheckType(QualType) function should be used to determine |
| 3783 | /// this property. |
| 3784 | /// |
| 3785 | /// The type visitor visits one type object only (not recursive). |
| 3786 | /// To find all referenced declarations we must discover all type objects until |
| 3787 | /// the canonical type is reached (walk over typedef and similar objects). This |
| 3788 | /// is done by loop over all "sugar" type objects. For every such type we must |
| 3789 | /// check all declarations that are referenced from it. For this check the |
| 3790 | /// visitor is used. In the visit functions all referenced declarations except |
| 3791 | /// the one that follows in the sugar chain (if any) must be checked. For this |
| 3792 | /// check the same visitor is re-used (it has no state-dependent data). |
| 3793 | /// |
| 3794 | /// The visit functions have 3 possible return values: |
| 3795 | /// - True, found a declaration inside \c ParentDC. |
| 3796 | /// - False, found declarations only outside \c ParentDC and it is not possible |
| 3797 | /// to find more declarations (the "sugar" chain does not continue). |
| 3798 | /// - Empty optional value, found no declarations or only outside \c ParentDC, |
| 3799 | /// but it is possible to find more declarations in the type "sugar" chain. |
| 3800 | /// The loop over the "sugar" types can be implemented by using type visit |
| 3801 | /// functions only (call \c CheckType with the desugared type). With the current |
| 3802 | /// solution no visit function is needed if the type has only a desugared type |
| 3803 | /// as data. |
| 3804 | class IsTypeDeclaredInsideVisitor |
| 3805 | : public TypeVisitor<IsTypeDeclaredInsideVisitor, std::optional<bool>> { |
| 3806 | public: |
| 3807 | IsTypeDeclaredInsideVisitor(const FunctionDecl *ParentDC) |
| 3808 | : ParentDC(ParentDC) {} |
| 3809 | |
| 3810 | bool CheckType(QualType T) { |
| 3811 | // Check the chain of "sugar" types. |
| 3812 | // The "sugar" types are typedef or similar types that have the same |
| 3813 | // canonical type. |
| 3814 | if (std::optional<bool> Res = Visit(T: T.getTypePtr())) |
| 3815 | return *Res; |
| 3816 | QualType DsT = |
| 3817 | T.getSingleStepDesugaredType(Context: ParentDC->getParentASTContext()); |
| 3818 | while (DsT != T) { |
| 3819 | if (std::optional<bool> Res = Visit(T: DsT.getTypePtr())) |
| 3820 | return *Res; |
| 3821 | T = DsT; |
| 3822 | DsT = T.getSingleStepDesugaredType(Context: ParentDC->getParentASTContext()); |
| 3823 | } |
| 3824 | return false; |
| 3825 | } |
| 3826 | |
| 3827 | std::optional<bool> VisitTagType(const TagType *T) { |
| 3828 | if (auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(Val: T->getDecl())) |
| 3829 | for (const auto &Arg : Spec->getTemplateArgs().asArray()) |
| 3830 | if (checkTemplateArgument(Arg)) |
| 3831 | return true; |
| 3832 | return isAncestorDeclContextOf(DC: ParentDC, D: T->getDecl()); |
| 3833 | } |
| 3834 | |
| 3835 | std::optional<bool> VisitPointerType(const PointerType *T) { |
| 3836 | return CheckType(T: T->getPointeeType()); |
| 3837 | } |
| 3838 | |
| 3839 | std::optional<bool> VisitReferenceType(const ReferenceType *T) { |
| 3840 | return CheckType(T: T->getPointeeTypeAsWritten()); |
| 3841 | } |
| 3842 | |
| 3843 | std::optional<bool> VisitTypedefType(const TypedefType *T) { |
| 3844 | return isAncestorDeclContextOf(DC: ParentDC, D: T->getDecl()); |
| 3845 | } |
| 3846 | |
| 3847 | std::optional<bool> VisitUsingType(const UsingType *T) { |
| 3848 | return isAncestorDeclContextOf(DC: ParentDC, D: T->getDecl()); |
| 3849 | } |
| 3850 | |
| 3851 | std::optional<bool> |
| 3852 | VisitTemplateSpecializationType(const TemplateSpecializationType *T) { |
| 3853 | for (const auto &Arg : T->template_arguments()) |
| 3854 | if (checkTemplateArgument(Arg)) |
| 3855 | return true; |
| 3856 | // This type is a "sugar" to a record type, it can have a desugared type. |
| 3857 | return {}; |
| 3858 | } |
| 3859 | |
| 3860 | std::optional<bool> VisitUnaryTransformType(const UnaryTransformType *T) { |
| 3861 | return CheckType(T: T->getBaseType()); |
| 3862 | } |
| 3863 | |
| 3864 | std::optional<bool> |
| 3865 | VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
| 3866 | // The "associated declaration" can be the same as ParentDC. |
| 3867 | if (isAncestorDeclContextOf(DC: ParentDC, D: T->getAssociatedDecl())) |
| 3868 | return true; |
| 3869 | return {}; |
| 3870 | } |
| 3871 | |
| 3872 | std::optional<bool> VisitConstantArrayType(const ConstantArrayType *T) { |
| 3873 | if (T->getSizeExpr() && isAncestorDeclContextOf(DC: ParentDC, S: T->getSizeExpr())) |
| 3874 | return true; |
| 3875 | |
| 3876 | return CheckType(T: T->getElementType()); |
| 3877 | } |
| 3878 | |
| 3879 | std::optional<bool> VisitVariableArrayType(const VariableArrayType *T) { |
| 3880 | llvm_unreachable( |
| 3881 | "Variable array should not occur in deduced return type of a function" ); |
| 3882 | } |
| 3883 | |
| 3884 | std::optional<bool> VisitIncompleteArrayType(const IncompleteArrayType *T) { |
| 3885 | llvm_unreachable("Incomplete array should not occur in deduced return type " |
| 3886 | "of a function" ); |
| 3887 | } |
| 3888 | |
| 3889 | std::optional<bool> VisitDependentArrayType(const IncompleteArrayType *T) { |
| 3890 | llvm_unreachable("Dependent array should not occur in deduced return type " |
| 3891 | "of a function" ); |
| 3892 | } |
| 3893 | |
| 3894 | private: |
| 3895 | const DeclContext *const ParentDC; |
| 3896 | |
| 3897 | bool checkTemplateArgument(const TemplateArgument &Arg) { |
| 3898 | switch (Arg.getKind()) { |
| 3899 | case TemplateArgument::Null: |
| 3900 | return false; |
| 3901 | case TemplateArgument::Integral: |
| 3902 | return CheckType(T: Arg.getIntegralType()); |
| 3903 | case TemplateArgument::Type: |
| 3904 | return CheckType(T: Arg.getAsType()); |
| 3905 | case TemplateArgument::Expression: |
| 3906 | return isAncestorDeclContextOf(DC: ParentDC, S: Arg.getAsExpr()); |
| 3907 | case TemplateArgument::Declaration: |
| 3908 | // FIXME: The declaration in this case is not allowed to be in a function? |
| 3909 | return isAncestorDeclContextOf(DC: ParentDC, D: Arg.getAsDecl()); |
| 3910 | case TemplateArgument::NullPtr: |
| 3911 | // FIXME: The type is not allowed to be in the function? |
| 3912 | return CheckType(T: Arg.getNullPtrType()); |
| 3913 | case TemplateArgument::StructuralValue: |
| 3914 | return CheckType(T: Arg.getStructuralValueType()); |
| 3915 | case TemplateArgument::Pack: |
| 3916 | for (const auto &PackArg : Arg.getPackAsArray()) |
| 3917 | if (checkTemplateArgument(Arg: PackArg)) |
| 3918 | return true; |
| 3919 | return false; |
| 3920 | case TemplateArgument::Template: |
| 3921 | // Templates can not be defined locally in functions. |
| 3922 | // A template passed as argument can be not in ParentDC. |
| 3923 | return false; |
| 3924 | case TemplateArgument::TemplateExpansion: |
| 3925 | // Templates can not be defined locally in functions. |
| 3926 | // A template passed as argument can be not in ParentDC. |
| 3927 | return false; |
| 3928 | } |
| 3929 | llvm_unreachable("Unknown TemplateArgument::ArgKind enum" ); |
| 3930 | }; |
| 3931 | }; |
| 3932 | } // namespace |
| 3933 | |
| 3934 | /// This function checks if the given function has a return type that contains |
| 3935 | /// a reference (in any way) to a declaration inside the same function. |
| 3936 | bool ASTNodeImporter::hasReturnTypeDeclaredInside(FunctionDecl *D) { |
| 3937 | QualType FromTy = D->getType(); |
| 3938 | const auto *FromFPT = FromTy->getAs<FunctionProtoType>(); |
| 3939 | assert(FromFPT && "Must be called on FunctionProtoType" ); |
| 3940 | |
| 3941 | auto IsCXX11Lambda = [&]() { |
| 3942 | if (Importer.FromContext.getLangOpts().CPlusPlus14) // C++14 or later |
| 3943 | return false; |
| 3944 | |
| 3945 | return isLambdaMethod(DC: D); |
| 3946 | }; |
| 3947 | |
| 3948 | QualType RetT = FromFPT->getReturnType(); |
| 3949 | if (isa<AutoType>(Val: RetT.getTypePtr()) || IsCXX11Lambda()) { |
| 3950 | FunctionDecl *Def = D->getDefinition(); |
| 3951 | IsTypeDeclaredInsideVisitor Visitor(Def ? Def : D); |
| 3952 | return Visitor.CheckType(T: RetT); |
| 3953 | } |
| 3954 | |
| 3955 | return false; |
| 3956 | } |
| 3957 | |
| 3958 | ExplicitSpecifier |
| 3959 | ASTNodeImporter::importExplicitSpecifier(Error &Err, ExplicitSpecifier ESpec) { |
| 3960 | Expr *ExplicitExpr = ESpec.getExpr(); |
| 3961 | if (ExplicitExpr) |
| 3962 | ExplicitExpr = importChecked(Err, From: ESpec.getExpr()); |
| 3963 | return ExplicitSpecifier(ExplicitExpr, ESpec.getKind()); |
| 3964 | } |
| 3965 | |
| 3966 | ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) { |
| 3967 | |
| 3968 | SmallVector<Decl *, 2> Redecls = getCanonicalForwardRedeclChain(D); |
| 3969 | auto RedeclIt = Redecls.begin(); |
| 3970 | // Import the first part of the decl chain. I.e. import all previous |
| 3971 | // declarations starting from the canonical decl. |
| 3972 | for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) { |
| 3973 | ExpectedDecl ToRedeclOrErr = import(From: *RedeclIt); |
| 3974 | if (!ToRedeclOrErr) |
| 3975 | return ToRedeclOrErr.takeError(); |
| 3976 | } |
| 3977 | assert(*RedeclIt == D); |
| 3978 | |
| 3979 | // Import the major distinguishing characteristics of this function. |
| 3980 | DeclContext *DC, *LexicalDC; |
| 3981 | DeclarationName Name; |
| 3982 | SourceLocation Loc; |
| 3983 | NamedDecl *ToD; |
| 3984 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 3985 | return std::move(Err); |
| 3986 | if (ToD) |
| 3987 | return ToD; |
| 3988 | |
| 3989 | FunctionDecl *FoundByLookup = nullptr; |
| 3990 | FunctionTemplateDecl *FromFT = D->getDescribedFunctionTemplate(); |
| 3991 | |
| 3992 | // If this is a function template specialization, then try to find the same |
| 3993 | // existing specialization in the "to" context. The lookup below will not |
| 3994 | // find any specialization, but would find the primary template; thus, we |
| 3995 | // have to skip normal lookup in case of specializations. |
| 3996 | // FIXME handle member function templates (TK_MemberSpecialization) similarly? |
| 3997 | if (D->getTemplatedKind() == |
| 3998 | FunctionDecl::TK_FunctionTemplateSpecialization) { |
| 3999 | auto FoundFunctionOrErr = FindFunctionTemplateSpecialization(FromFD: D); |
| 4000 | if (!FoundFunctionOrErr) |
| 4001 | return FoundFunctionOrErr.takeError(); |
| 4002 | if (FunctionDecl *FoundFunction = *FoundFunctionOrErr) { |
| 4003 | if (Decl *Def = FindAndMapDefinition(D, FoundFunction)) |
| 4004 | return Def; |
| 4005 | FoundByLookup = FoundFunction; |
| 4006 | } |
| 4007 | } |
| 4008 | // Try to find a function in our own ("to") context with the same name, same |
| 4009 | // type, and in the same context as the function we're importing. |
| 4010 | else if (!LexicalDC->isFunctionOrMethod()) { |
| 4011 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 4012 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend; |
| 4013 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 4014 | for (auto *FoundDecl : FoundDecls) { |
| 4015 | if (!FoundDecl->isInIdentifierNamespace(NS: IDNS)) |
| 4016 | continue; |
| 4017 | |
| 4018 | if (auto *FoundFunction = dyn_cast<FunctionDecl>(Val: FoundDecl)) { |
| 4019 | if (!hasSameVisibilityContextAndLinkage(Found: FoundFunction, From: D)) |
| 4020 | continue; |
| 4021 | |
| 4022 | if (IsStructuralMatch(From: D, To: FoundFunction)) { |
| 4023 | if (Decl *Def = FindAndMapDefinition(D, FoundFunction)) |
| 4024 | return Def; |
| 4025 | FoundByLookup = FoundFunction; |
| 4026 | break; |
| 4027 | } |
| 4028 | // FIXME: Check for overloading more carefully, e.g., by boosting |
| 4029 | // Sema::IsOverload out to the AST library. |
| 4030 | |
| 4031 | // Function overloading is okay in C++. |
| 4032 | if (Importer.getToContext().getLangOpts().CPlusPlus) |
| 4033 | continue; |
| 4034 | |
| 4035 | // Complain about inconsistent function types. |
| 4036 | Importer.ToDiag(Loc, DiagID: diag::warn_odr_function_type_inconsistent) |
| 4037 | << Name << D->getType() << FoundFunction->getType(); |
| 4038 | Importer.ToDiag(Loc: FoundFunction->getLocation(), DiagID: diag::note_odr_value_here) |
| 4039 | << FoundFunction->getType(); |
| 4040 | ConflictingDecls.push_back(Elt: FoundDecl); |
| 4041 | } |
| 4042 | } |
| 4043 | |
| 4044 | if (!ConflictingDecls.empty()) { |
| 4045 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
| 4046 | Name, DC, IDNS, Decls: ConflictingDecls.data(), NumDecls: ConflictingDecls.size()); |
| 4047 | if (NameOrErr) |
| 4048 | Name = NameOrErr.get(); |
| 4049 | else |
| 4050 | return NameOrErr.takeError(); |
| 4051 | } |
| 4052 | } |
| 4053 | |
| 4054 | // We do not allow more than one in-class declaration of a function. This is |
| 4055 | // because AST clients like VTableBuilder asserts on this. VTableBuilder |
| 4056 | // assumes there is only one in-class declaration. Building a redecl |
| 4057 | // chain would result in more than one in-class declaration for |
| 4058 | // overrides (even if they are part of the same redecl chain inside the |
| 4059 | // derived class.) |
| 4060 | if (FoundByLookup) { |
| 4061 | if (isa<CXXMethodDecl>(Val: FoundByLookup)) { |
| 4062 | if (D->getLexicalDeclContext() == D->getDeclContext()) { |
| 4063 | if (!D->doesThisDeclarationHaveABody()) { |
| 4064 | if (FunctionTemplateDecl *DescribedD = |
| 4065 | D->getDescribedFunctionTemplate()) { |
| 4066 | // Handle a "templated" function together with its described |
| 4067 | // template. This avoids need for a similar check at import of the |
| 4068 | // described template. |
| 4069 | assert(FoundByLookup->getDescribedFunctionTemplate() && |
| 4070 | "Templated function mapped to non-templated?" ); |
| 4071 | Importer.MapImported(From: DescribedD, |
| 4072 | To: FoundByLookup->getDescribedFunctionTemplate()); |
| 4073 | } |
| 4074 | return Importer.MapImported(From: D, To: FoundByLookup); |
| 4075 | } else { |
| 4076 | // Let's continue and build up the redecl chain in this case. |
| 4077 | // FIXME Merge the functions into one decl. |
| 4078 | } |
| 4079 | } |
| 4080 | } |
| 4081 | } |
| 4082 | |
| 4083 | DeclarationNameInfo NameInfo(Name, Loc); |
| 4084 | // Import additional name location/type info. |
| 4085 | if (Error Err = ImportDeclarationNameLoc(From: D->getNameInfo(), To&: NameInfo)) |
| 4086 | return std::move(Err); |
| 4087 | |
| 4088 | QualType FromTy = D->getType(); |
| 4089 | TypeSourceInfo *FromTSI = D->getTypeSourceInfo(); |
| 4090 | // Set to true if we do not import the type of the function as is. There are |
| 4091 | // cases when the original type would result in an infinite recursion during |
| 4092 | // the import. To avoid an infinite recursion when importing, we create the |
| 4093 | // FunctionDecl with a simplified function type and update it only after the |
| 4094 | // relevant AST nodes are already imported. |
| 4095 | // The type is related to TypeSourceInfo (it references the type), so we must |
| 4096 | // do the same with TypeSourceInfo. |
| 4097 | bool UsedDifferentProtoType = false; |
| 4098 | if (const auto *FromFPT = FromTy->getAs<FunctionProtoType>()) { |
| 4099 | QualType FromReturnTy = FromFPT->getReturnType(); |
| 4100 | // Functions with auto return type may define a struct inside their body |
| 4101 | // and the return type could refer to that struct. |
| 4102 | // E.g.: auto foo() { struct X{}; return X(); } |
| 4103 | // To avoid an infinite recursion when importing, create the FunctionDecl |
| 4104 | // with a simplified return type. |
| 4105 | // Reuse this approach for auto return types declared as typenames from |
| 4106 | // template params, tracked in FindFunctionDeclImportCycle. |
| 4107 | if (hasReturnTypeDeclaredInside(D) || |
| 4108 | Importer.FindFunctionDeclImportCycle.isCycle(D)) { |
| 4109 | FromReturnTy = Importer.getFromContext().VoidTy; |
| 4110 | UsedDifferentProtoType = true; |
| 4111 | } |
| 4112 | FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo(); |
| 4113 | // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the |
| 4114 | // FunctionDecl that we are importing the FunctionProtoType for. |
| 4115 | // To avoid an infinite recursion when importing, create the FunctionDecl |
| 4116 | // with a simplified function type. |
| 4117 | if (FromEPI.ExceptionSpec.SourceDecl || |
| 4118 | FromEPI.ExceptionSpec.SourceTemplate || |
| 4119 | FromEPI.ExceptionSpec.NoexceptExpr) { |
| 4120 | FunctionProtoType::ExtProtoInfo DefaultEPI; |
| 4121 | FromEPI = DefaultEPI; |
| 4122 | UsedDifferentProtoType = true; |
| 4123 | } |
| 4124 | FromTy = Importer.getFromContext().getFunctionType( |
| 4125 | ResultTy: FromReturnTy, Args: FromFPT->getParamTypes(), EPI: FromEPI); |
| 4126 | FromTSI = Importer.getFromContext().getTrivialTypeSourceInfo( |
| 4127 | T: FromTy, Loc: D->getBeginLoc()); |
| 4128 | } |
| 4129 | |
| 4130 | Error Err = Error::success(); |
| 4131 | auto ScopedReturnTypeDeclCycleDetector = |
| 4132 | Importer.FindFunctionDeclImportCycle.makeScopedCycleDetection(D); |
| 4133 | auto T = importChecked(Err, From: FromTy); |
| 4134 | auto TInfo = importChecked(Err, From: FromTSI); |
| 4135 | auto ToInnerLocStart = importChecked(Err, From: D->getInnerLocStart()); |
| 4136 | auto ToEndLoc = importChecked(Err, From: D->getEndLoc()); |
| 4137 | auto ToDefaultLoc = importChecked(Err, From: D->getDefaultLoc()); |
| 4138 | auto ToQualifierLoc = importChecked(Err, From: D->getQualifierLoc()); |
| 4139 | AssociatedConstraint TrailingRequiresClause = D->getTrailingRequiresClause(); |
| 4140 | TrailingRequiresClause.ConstraintExpr = |
| 4141 | importChecked(Err, From: TrailingRequiresClause.ConstraintExpr); |
| 4142 | if (Err) |
| 4143 | return std::move(Err); |
| 4144 | |
| 4145 | // Import the function parameters. |
| 4146 | SmallVector<ParmVarDecl *, 8> Parameters; |
| 4147 | for (auto *P : D->parameters()) { |
| 4148 | if (Expected<ParmVarDecl *> ToPOrErr = import(From: P)) |
| 4149 | Parameters.push_back(Elt: *ToPOrErr); |
| 4150 | else |
| 4151 | return ToPOrErr.takeError(); |
| 4152 | } |
| 4153 | |
| 4154 | // Create the imported function. |
| 4155 | FunctionDecl *ToFunction = nullptr; |
| 4156 | if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(Val: D)) { |
| 4157 | ExplicitSpecifier ESpec = |
| 4158 | importExplicitSpecifier(Err, ESpec: FromConstructor->getExplicitSpecifier()); |
| 4159 | if (Err) |
| 4160 | return std::move(Err); |
| 4161 | auto ToInheritedConstructor = InheritedConstructor(); |
| 4162 | if (FromConstructor->isInheritingConstructor()) { |
| 4163 | Expected<InheritedConstructor> ImportedInheritedCtor = |
| 4164 | import(From: FromConstructor->getInheritedConstructor()); |
| 4165 | if (!ImportedInheritedCtor) |
| 4166 | return ImportedInheritedCtor.takeError(); |
| 4167 | ToInheritedConstructor = *ImportedInheritedCtor; |
| 4168 | } |
| 4169 | if (GetImportedOrCreateDecl<CXXConstructorDecl>( |
| 4170 | ToD&: ToFunction, FromD: D, args&: Importer.getToContext(), args: cast<CXXRecordDecl>(Val: DC), |
| 4171 | args&: ToInnerLocStart, args&: NameInfo, args&: T, args&: TInfo, args&: ESpec, args: D->UsesFPIntrin(), |
| 4172 | args: D->isInlineSpecified(), args: D->isImplicit(), args: D->getConstexprKind(), |
| 4173 | args&: ToInheritedConstructor, args&: TrailingRequiresClause)) |
| 4174 | return ToFunction; |
| 4175 | } else if (CXXDestructorDecl *FromDtor = dyn_cast<CXXDestructorDecl>(Val: D)) { |
| 4176 | |
| 4177 | Error Err = Error::success(); |
| 4178 | auto ToOperatorDelete = importChecked( |
| 4179 | Err, From: const_cast<FunctionDecl *>(FromDtor->getOperatorDelete())); |
| 4180 | auto ToThisArg = importChecked(Err, From: FromDtor->getOperatorDeleteThisArg()); |
| 4181 | if (Err) |
| 4182 | return std::move(Err); |
| 4183 | |
| 4184 | if (GetImportedOrCreateDecl<CXXDestructorDecl>( |
| 4185 | ToD&: ToFunction, FromD: D, args&: Importer.getToContext(), args: cast<CXXRecordDecl>(Val: DC), |
| 4186 | args&: ToInnerLocStart, args&: NameInfo, args&: T, args&: TInfo, args: D->UsesFPIntrin(), |
| 4187 | args: D->isInlineSpecified(), args: D->isImplicit(), args: D->getConstexprKind(), |
| 4188 | args&: TrailingRequiresClause)) |
| 4189 | return ToFunction; |
| 4190 | |
| 4191 | CXXDestructorDecl *ToDtor = cast<CXXDestructorDecl>(Val: ToFunction); |
| 4192 | |
| 4193 | ToDtor->setOperatorDelete(OD: ToOperatorDelete, ThisArg: ToThisArg); |
| 4194 | } else if (CXXConversionDecl *FromConversion = |
| 4195 | dyn_cast<CXXConversionDecl>(Val: D)) { |
| 4196 | ExplicitSpecifier ESpec = |
| 4197 | importExplicitSpecifier(Err, ESpec: FromConversion->getExplicitSpecifier()); |
| 4198 | if (Err) |
| 4199 | return std::move(Err); |
| 4200 | if (GetImportedOrCreateDecl<CXXConversionDecl>( |
| 4201 | ToD&: ToFunction, FromD: D, args&: Importer.getToContext(), args: cast<CXXRecordDecl>(Val: DC), |
| 4202 | args&: ToInnerLocStart, args&: NameInfo, args&: T, args&: TInfo, args: D->UsesFPIntrin(), |
| 4203 | args: D->isInlineSpecified(), args&: ESpec, args: D->getConstexprKind(), |
| 4204 | args: SourceLocation(), args&: TrailingRequiresClause)) |
| 4205 | return ToFunction; |
| 4206 | } else if (auto *Method = dyn_cast<CXXMethodDecl>(Val: D)) { |
| 4207 | if (GetImportedOrCreateDecl<CXXMethodDecl>( |
| 4208 | ToD&: ToFunction, FromD: D, args&: Importer.getToContext(), args: cast<CXXRecordDecl>(Val: DC), |
| 4209 | args&: ToInnerLocStart, args&: NameInfo, args&: T, args&: TInfo, args: Method->getStorageClass(), |
| 4210 | args: Method->UsesFPIntrin(), args: Method->isInlineSpecified(), |
| 4211 | args: D->getConstexprKind(), args: SourceLocation(), args&: TrailingRequiresClause)) |
| 4212 | return ToFunction; |
| 4213 | } else if (auto *Guide = dyn_cast<CXXDeductionGuideDecl>(Val: D)) { |
| 4214 | ExplicitSpecifier ESpec = |
| 4215 | importExplicitSpecifier(Err, ESpec: Guide->getExplicitSpecifier()); |
| 4216 | CXXConstructorDecl *Ctor = |
| 4217 | importChecked(Err, From: Guide->getCorrespondingConstructor()); |
| 4218 | const CXXDeductionGuideDecl *SourceDG = |
| 4219 | importChecked(Err, From: Guide->getSourceDeductionGuide()); |
| 4220 | if (Err) |
| 4221 | return std::move(Err); |
| 4222 | if (GetImportedOrCreateDecl<CXXDeductionGuideDecl>( |
| 4223 | ToD&: ToFunction, FromD: D, args&: Importer.getToContext(), args&: DC, args&: ToInnerLocStart, args&: ESpec, |
| 4224 | args&: NameInfo, args&: T, args&: TInfo, args&: ToEndLoc, args&: Ctor, |
| 4225 | args: Guide->getDeductionCandidateKind(), args&: TrailingRequiresClause, |
| 4226 | args&: SourceDG, args: Guide->getSourceDeductionGuideKind())) |
| 4227 | return ToFunction; |
| 4228 | } else { |
| 4229 | if (GetImportedOrCreateDecl( |
| 4230 | ToD&: ToFunction, FromD: D, args&: Importer.getToContext(), args&: DC, args&: ToInnerLocStart, |
| 4231 | args&: NameInfo, args&: T, args&: TInfo, args: D->getStorageClass(), args: D->UsesFPIntrin(), |
| 4232 | args: D->isInlineSpecified(), args: D->hasWrittenPrototype(), |
| 4233 | args: D->getConstexprKind(), args&: TrailingRequiresClause)) |
| 4234 | return ToFunction; |
| 4235 | } |
| 4236 | |
| 4237 | // Connect the redecl chain. |
| 4238 | if (FoundByLookup) { |
| 4239 | auto *Recent = const_cast<FunctionDecl *>( |
| 4240 | FoundByLookup->getMostRecentDecl()); |
| 4241 | ToFunction->setPreviousDecl(Recent); |
| 4242 | // FIXME Probably we should merge exception specifications. E.g. In the |
| 4243 | // "To" context the existing function may have exception specification with |
| 4244 | // noexcept-unevaluated, while the newly imported function may have an |
| 4245 | // evaluated noexcept. A call to adjustExceptionSpec() on the imported |
| 4246 | // decl and its redeclarations may be required. |
| 4247 | } |
| 4248 | |
| 4249 | StringLiteral *Msg = D->getDeletedMessage(); |
| 4250 | if (Msg) { |
| 4251 | auto Imported = import(From: Msg); |
| 4252 | if (!Imported) |
| 4253 | return Imported.takeError(); |
| 4254 | Msg = *Imported; |
| 4255 | } |
| 4256 | |
| 4257 | ToFunction->setQualifierInfo(ToQualifierLoc); |
| 4258 | ToFunction->setAccess(D->getAccess()); |
| 4259 | ToFunction->setLexicalDeclContext(LexicalDC); |
| 4260 | ToFunction->setVirtualAsWritten(D->isVirtualAsWritten()); |
| 4261 | ToFunction->setTrivial(D->isTrivial()); |
| 4262 | ToFunction->setIsPureVirtual(D->isPureVirtual()); |
| 4263 | ToFunction->setDefaulted(D->isDefaulted()); |
| 4264 | ToFunction->setExplicitlyDefaulted(D->isExplicitlyDefaulted()); |
| 4265 | ToFunction->setDeletedAsWritten(D: D->isDeletedAsWritten()); |
| 4266 | ToFunction->setFriendConstraintRefersToEnclosingTemplate( |
| 4267 | D->FriendConstraintRefersToEnclosingTemplate()); |
| 4268 | ToFunction->setIsDestroyingOperatorDelete(D->isDestroyingOperatorDelete()); |
| 4269 | ToFunction->setIsTypeAwareOperatorNewOrDelete( |
| 4270 | D->isTypeAwareOperatorNewOrDelete()); |
| 4271 | ToFunction->setRangeEnd(ToEndLoc); |
| 4272 | ToFunction->setDefaultLoc(ToDefaultLoc); |
| 4273 | |
| 4274 | if (Msg) |
| 4275 | ToFunction->setDefaultedOrDeletedInfo( |
| 4276 | FunctionDecl::DefaultedOrDeletedFunctionInfo::Create( |
| 4277 | Context&: Importer.getToContext(), Lookups: {}, DeletedMessage: Msg)); |
| 4278 | |
| 4279 | // Set the parameters. |
| 4280 | for (auto *Param : Parameters) { |
| 4281 | Param->setOwningFunction(ToFunction); |
| 4282 | ToFunction->addDeclInternal(D: Param); |
| 4283 | if (ASTImporterLookupTable *LT = Importer.SharedState->getLookupTable()) |
| 4284 | LT->update(ND: Param, OldDC: Importer.getToContext().getTranslationUnitDecl()); |
| 4285 | } |
| 4286 | ToFunction->setParams(Parameters); |
| 4287 | |
| 4288 | // We need to complete creation of FunctionProtoTypeLoc manually with setting |
| 4289 | // params it refers to. |
| 4290 | if (TInfo) { |
| 4291 | if (auto ProtoLoc = |
| 4292 | TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) { |
| 4293 | for (unsigned I = 0, N = Parameters.size(); I != N; ++I) |
| 4294 | ProtoLoc.setParam(i: I, VD: Parameters[I]); |
| 4295 | } |
| 4296 | } |
| 4297 | |
| 4298 | // Import the describing template function, if any. |
| 4299 | if (FromFT) { |
| 4300 | auto ToFTOrErr = import(From: FromFT); |
| 4301 | if (!ToFTOrErr) |
| 4302 | return ToFTOrErr.takeError(); |
| 4303 | } |
| 4304 | |
| 4305 | // Import Ctor initializers. |
| 4306 | if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(Val: D)) { |
| 4307 | if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) { |
| 4308 | SmallVector<CXXCtorInitializer *, 4> CtorInitializers(NumInitializers); |
| 4309 | // Import first, then allocate memory and copy if there was no error. |
| 4310 | if (Error Err = ImportContainerChecked( |
| 4311 | InContainer: FromConstructor->inits(), OutContainer&: CtorInitializers)) |
| 4312 | return std::move(Err); |
| 4313 | auto **Memory = |
| 4314 | new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers]; |
| 4315 | llvm::copy(Range&: CtorInitializers, Out: Memory); |
| 4316 | auto *ToCtor = cast<CXXConstructorDecl>(Val: ToFunction); |
| 4317 | ToCtor->setCtorInitializers(Memory); |
| 4318 | ToCtor->setNumCtorInitializers(NumInitializers); |
| 4319 | } |
| 4320 | } |
| 4321 | |
| 4322 | // If it is a template, import all related things. |
| 4323 | if (Error Err = ImportTemplateInformation(FromFD: D, ToFD: ToFunction)) |
| 4324 | return std::move(Err); |
| 4325 | |
| 4326 | if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(Val: D)) |
| 4327 | if (Error Err = ImportOverriddenMethods(ToMethod: cast<CXXMethodDecl>(Val: ToFunction), |
| 4328 | FromMethod: FromCXXMethod)) |
| 4329 | return std::move(Err); |
| 4330 | |
| 4331 | if (D->doesThisDeclarationHaveABody()) { |
| 4332 | Error Err = ImportFunctionDeclBody(FromFD: D, ToFD: ToFunction); |
| 4333 | |
| 4334 | if (Err) |
| 4335 | return std::move(Err); |
| 4336 | } |
| 4337 | |
| 4338 | // Import and set the original type in case we used another type. |
| 4339 | if (UsedDifferentProtoType) { |
| 4340 | if (ExpectedType TyOrErr = import(From: D->getType())) |
| 4341 | ToFunction->setType(*TyOrErr); |
| 4342 | else |
| 4343 | return TyOrErr.takeError(); |
| 4344 | if (Expected<TypeSourceInfo *> TSIOrErr = import(From: D->getTypeSourceInfo())) |
| 4345 | ToFunction->setTypeSourceInfo(*TSIOrErr); |
| 4346 | else |
| 4347 | return TSIOrErr.takeError(); |
| 4348 | } |
| 4349 | |
| 4350 | // FIXME: Other bits to merge? |
| 4351 | |
| 4352 | addDeclToContexts(FromD: D, ToD: ToFunction); |
| 4353 | |
| 4354 | // Import the rest of the chain. I.e. import all subsequent declarations. |
| 4355 | for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) { |
| 4356 | ExpectedDecl ToRedeclOrErr = import(From: *RedeclIt); |
| 4357 | if (!ToRedeclOrErr) |
| 4358 | return ToRedeclOrErr.takeError(); |
| 4359 | } |
| 4360 | |
| 4361 | return ToFunction; |
| 4362 | } |
| 4363 | |
| 4364 | ExpectedDecl ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) { |
| 4365 | return VisitFunctionDecl(D); |
| 4366 | } |
| 4367 | |
| 4368 | ExpectedDecl ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
| 4369 | return VisitCXXMethodDecl(D); |
| 4370 | } |
| 4371 | |
| 4372 | ExpectedDecl ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
| 4373 | return VisitCXXMethodDecl(D); |
| 4374 | } |
| 4375 | |
| 4376 | ExpectedDecl ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) { |
| 4377 | return VisitCXXMethodDecl(D); |
| 4378 | } |
| 4379 | |
| 4380 | ExpectedDecl |
| 4381 | ASTNodeImporter::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) { |
| 4382 | return VisitFunctionDecl(D); |
| 4383 | } |
| 4384 | |
| 4385 | ExpectedDecl ASTNodeImporter::VisitFieldDecl(FieldDecl *D) { |
| 4386 | // Import the major distinguishing characteristics of a variable. |
| 4387 | DeclContext *DC, *LexicalDC; |
| 4388 | DeclarationName Name; |
| 4389 | SourceLocation Loc; |
| 4390 | NamedDecl *ToD; |
| 4391 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 4392 | return std::move(Err); |
| 4393 | if (ToD) |
| 4394 | return ToD; |
| 4395 | |
| 4396 | // Determine whether we've already imported this field. |
| 4397 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 4398 | for (auto *FoundDecl : FoundDecls) { |
| 4399 | if (FieldDecl *FoundField = dyn_cast<FieldDecl>(Val: FoundDecl)) { |
| 4400 | // For anonymous fields, match up by index. |
| 4401 | if (!Name && |
| 4402 | ASTImporter::getFieldIndex(F: D) != |
| 4403 | ASTImporter::getFieldIndex(F: FoundField)) |
| 4404 | continue; |
| 4405 | |
| 4406 | if (Importer.IsStructurallyEquivalent(From: D->getType(), |
| 4407 | To: FoundField->getType())) { |
| 4408 | Importer.MapImported(From: D, To: FoundField); |
| 4409 | // In case of a FieldDecl of a ClassTemplateSpecializationDecl, the |
| 4410 | // initializer of a FieldDecl might not had been instantiated in the |
| 4411 | // "To" context. However, the "From" context might instantiated that, |
| 4412 | // thus we have to merge that. |
| 4413 | // Note: `hasInClassInitializer()` is not the same as non-null |
| 4414 | // `getInClassInitializer()` value. |
| 4415 | if (Expr *FromInitializer = D->getInClassInitializer()) { |
| 4416 | if (ExpectedExpr ToInitializerOrErr = import(From: FromInitializer)) { |
| 4417 | // Import of the FromInitializer may result in the setting of |
| 4418 | // InClassInitializer. If not, set it here. |
| 4419 | assert(FoundField->hasInClassInitializer() && |
| 4420 | "Field should have an in-class initializer if it has an " |
| 4421 | "expression for it." ); |
| 4422 | if (!FoundField->getInClassInitializer()) |
| 4423 | FoundField->setInClassInitializer(*ToInitializerOrErr); |
| 4424 | } else { |
| 4425 | return ToInitializerOrErr.takeError(); |
| 4426 | } |
| 4427 | } |
| 4428 | return FoundField; |
| 4429 | } |
| 4430 | |
| 4431 | // FIXME: Why is this case not handled with calling HandleNameConflict? |
| 4432 | Importer.ToDiag(Loc, DiagID: diag::warn_odr_field_type_inconsistent) |
| 4433 | << Name << D->getType() << FoundField->getType(); |
| 4434 | Importer.ToDiag(Loc: FoundField->getLocation(), DiagID: diag::note_odr_value_here) |
| 4435 | << FoundField->getType(); |
| 4436 | |
| 4437 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 4438 | } |
| 4439 | } |
| 4440 | |
| 4441 | Error Err = Error::success(); |
| 4442 | auto ToType = importChecked(Err, From: D->getType()); |
| 4443 | auto ToTInfo = importChecked(Err, From: D->getTypeSourceInfo()); |
| 4444 | auto ToBitWidth = importChecked(Err, From: D->getBitWidth()); |
| 4445 | auto ToInnerLocStart = importChecked(Err, From: D->getInnerLocStart()); |
| 4446 | if (Err) |
| 4447 | return std::move(Err); |
| 4448 | const Type *ToCapturedVLAType = nullptr; |
| 4449 | if (Error Err = Importer.importInto( |
| 4450 | To&: ToCapturedVLAType, From: cast_or_null<Type>(Val: D->getCapturedVLAType()))) |
| 4451 | return std::move(Err); |
| 4452 | |
| 4453 | FieldDecl *ToField; |
| 4454 | if (GetImportedOrCreateDecl(ToD&: ToField, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 4455 | args&: ToInnerLocStart, args&: Loc, args: Name.getAsIdentifierInfo(), |
| 4456 | args&: ToType, args&: ToTInfo, args&: ToBitWidth, args: D->isMutable(), |
| 4457 | args: D->getInClassInitStyle())) |
| 4458 | return ToField; |
| 4459 | |
| 4460 | ToField->setAccess(D->getAccess()); |
| 4461 | ToField->setLexicalDeclContext(LexicalDC); |
| 4462 | ToField->setImplicit(D->isImplicit()); |
| 4463 | if (ToCapturedVLAType) |
| 4464 | ToField->setCapturedVLAType(cast<VariableArrayType>(Val: ToCapturedVLAType)); |
| 4465 | LexicalDC->addDeclInternal(D: ToField); |
| 4466 | // Import initializer only after the field was created, it may have recursive |
| 4467 | // reference to the field. |
| 4468 | auto ToInitializer = importChecked(Err, From: D->getInClassInitializer()); |
| 4469 | if (Err) |
| 4470 | return std::move(Err); |
| 4471 | if (ToInitializer) { |
| 4472 | auto *AlreadyImported = ToField->getInClassInitializer(); |
| 4473 | if (AlreadyImported) |
| 4474 | assert(ToInitializer == AlreadyImported && |
| 4475 | "Duplicate import of in-class initializer." ); |
| 4476 | else |
| 4477 | ToField->setInClassInitializer(ToInitializer); |
| 4478 | } |
| 4479 | |
| 4480 | return ToField; |
| 4481 | } |
| 4482 | |
| 4483 | ExpectedDecl ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) { |
| 4484 | // Import the major distinguishing characteristics of a variable. |
| 4485 | DeclContext *DC, *LexicalDC; |
| 4486 | DeclarationName Name; |
| 4487 | SourceLocation Loc; |
| 4488 | NamedDecl *ToD; |
| 4489 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 4490 | return std::move(Err); |
| 4491 | if (ToD) |
| 4492 | return ToD; |
| 4493 | |
| 4494 | // Determine whether we've already imported this field. |
| 4495 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 4496 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 4497 | if (auto *FoundField = dyn_cast<IndirectFieldDecl>(Val: FoundDecls[I])) { |
| 4498 | // For anonymous indirect fields, match up by index. |
| 4499 | if (!Name && |
| 4500 | ASTImporter::getFieldIndex(F: D) != |
| 4501 | ASTImporter::getFieldIndex(F: FoundField)) |
| 4502 | continue; |
| 4503 | |
| 4504 | if (Importer.IsStructurallyEquivalent(From: D->getType(), |
| 4505 | To: FoundField->getType(), |
| 4506 | Complain: !Name.isEmpty())) { |
| 4507 | Importer.MapImported(From: D, To: FoundField); |
| 4508 | return FoundField; |
| 4509 | } |
| 4510 | |
| 4511 | // If there are more anonymous fields to check, continue. |
| 4512 | if (!Name && I < N-1) |
| 4513 | continue; |
| 4514 | |
| 4515 | // FIXME: Why is this case not handled with calling HandleNameConflict? |
| 4516 | Importer.ToDiag(Loc, DiagID: diag::warn_odr_field_type_inconsistent) |
| 4517 | << Name << D->getType() << FoundField->getType(); |
| 4518 | Importer.ToDiag(Loc: FoundField->getLocation(), DiagID: diag::note_odr_value_here) |
| 4519 | << FoundField->getType(); |
| 4520 | |
| 4521 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 4522 | } |
| 4523 | } |
| 4524 | |
| 4525 | // Import the type. |
| 4526 | auto TypeOrErr = import(From: D->getType()); |
| 4527 | if (!TypeOrErr) |
| 4528 | return TypeOrErr.takeError(); |
| 4529 | |
| 4530 | auto **NamedChain = |
| 4531 | new (Importer.getToContext()) NamedDecl*[D->getChainingSize()]; |
| 4532 | |
| 4533 | unsigned i = 0; |
| 4534 | for (auto *PI : D->chain()) |
| 4535 | if (Expected<NamedDecl *> ToD = import(From: PI)) |
| 4536 | NamedChain[i++] = *ToD; |
| 4537 | else |
| 4538 | return ToD.takeError(); |
| 4539 | |
| 4540 | MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()}; |
| 4541 | IndirectFieldDecl *ToIndirectField; |
| 4542 | if (GetImportedOrCreateDecl(ToD&: ToIndirectField, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 4543 | args&: Loc, args: Name.getAsIdentifierInfo(), args&: *TypeOrErr, args&: CH)) |
| 4544 | // FIXME here we leak `NamedChain` which is allocated before |
| 4545 | return ToIndirectField; |
| 4546 | |
| 4547 | ToIndirectField->setAccess(D->getAccess()); |
| 4548 | ToIndirectField->setLexicalDeclContext(LexicalDC); |
| 4549 | LexicalDC->addDeclInternal(D: ToIndirectField); |
| 4550 | return ToIndirectField; |
| 4551 | } |
| 4552 | |
| 4553 | /// Used as return type of getFriendCountAndPosition. |
| 4554 | struct FriendCountAndPosition { |
| 4555 | /// Number of similar looking friends. |
| 4556 | unsigned int TotalCount; |
| 4557 | /// Index of the specific FriendDecl. |
| 4558 | unsigned int IndexOfDecl; |
| 4559 | }; |
| 4560 | |
| 4561 | static bool IsEquivalentFriend(ASTImporter &Importer, FriendDecl *FD1, |
| 4562 | FriendDecl *FD2) { |
| 4563 | if ((!FD1->getFriendType()) != (!FD2->getFriendType())) |
| 4564 | return false; |
| 4565 | |
| 4566 | if (const TypeSourceInfo *TSI = FD1->getFriendType()) |
| 4567 | return Importer.IsStructurallyEquivalent( |
| 4568 | From: TSI->getType(), To: FD2->getFriendType()->getType(), /*Complain=*/false); |
| 4569 | |
| 4570 | ASTImporter::NonEquivalentDeclSet NonEquivalentDecls; |
| 4571 | StructuralEquivalenceContext Ctx( |
| 4572 | Importer.getToContext().getLangOpts(), FD1->getASTContext(), |
| 4573 | FD2->getASTContext(), NonEquivalentDecls, |
| 4574 | StructuralEquivalenceKind::Default, |
| 4575 | /* StrictTypeSpelling = */ false, /* Complain = */ false); |
| 4576 | return Ctx.IsEquivalent(D1: FD1, D2: FD2); |
| 4577 | } |
| 4578 | |
| 4579 | static FriendCountAndPosition getFriendCountAndPosition(ASTImporter &Importer, |
| 4580 | FriendDecl *FD) { |
| 4581 | unsigned int FriendCount = 0; |
| 4582 | UnsignedOrNone FriendPosition = std::nullopt; |
| 4583 | const auto *RD = cast<CXXRecordDecl>(Val: FD->getLexicalDeclContext()); |
| 4584 | |
| 4585 | for (FriendDecl *FoundFriend : RD->friends()) { |
| 4586 | if (FoundFriend == FD) { |
| 4587 | FriendPosition = FriendCount; |
| 4588 | ++FriendCount; |
| 4589 | } else if (IsEquivalentFriend(Importer, FD1: FD, FD2: FoundFriend)) { |
| 4590 | ++FriendCount; |
| 4591 | } |
| 4592 | } |
| 4593 | |
| 4594 | assert(FriendPosition && "Friend decl not found in own parent." ); |
| 4595 | |
| 4596 | return {.TotalCount: FriendCount, .IndexOfDecl: *FriendPosition}; |
| 4597 | } |
| 4598 | |
| 4599 | ExpectedDecl ASTNodeImporter::VisitFriendDecl(FriendDecl *D) { |
| 4600 | // Import the major distinguishing characteristics of a declaration. |
| 4601 | DeclContext *DC, *LexicalDC; |
| 4602 | if (Error Err = ImportDeclContext(FromD: D, ToDC&: DC, ToLexicalDC&: LexicalDC)) |
| 4603 | return std::move(Err); |
| 4604 | |
| 4605 | // Determine whether we've already imported this decl. |
| 4606 | // FriendDecl is not a NamedDecl so we cannot use lookup. |
| 4607 | // We try to maintain order and count of redundant friend declarations. |
| 4608 | const auto *RD = cast<CXXRecordDecl>(Val: DC); |
| 4609 | SmallVector<FriendDecl *, 2> ImportedEquivalentFriends; |
| 4610 | for (FriendDecl *ImportedFriend : RD->friends()) |
| 4611 | if (IsEquivalentFriend(Importer, FD1: D, FD2: ImportedFriend)) |
| 4612 | ImportedEquivalentFriends.push_back(Elt: ImportedFriend); |
| 4613 | |
| 4614 | FriendCountAndPosition CountAndPosition = |
| 4615 | getFriendCountAndPosition(Importer, FD: D); |
| 4616 | |
| 4617 | assert(ImportedEquivalentFriends.size() <= CountAndPosition.TotalCount && |
| 4618 | "Class with non-matching friends is imported, ODR check wrong?" ); |
| 4619 | if (ImportedEquivalentFriends.size() == CountAndPosition.TotalCount) |
| 4620 | return Importer.MapImported( |
| 4621 | From: D, To: ImportedEquivalentFriends[CountAndPosition.IndexOfDecl]); |
| 4622 | |
| 4623 | // Not found. Create it. |
| 4624 | // The declarations will be put into order later by ImportDeclContext. |
| 4625 | FriendDecl::FriendUnion ToFU; |
| 4626 | if (NamedDecl *FriendD = D->getFriendDecl()) { |
| 4627 | NamedDecl *ToFriendD; |
| 4628 | if (Error Err = importInto(To&: ToFriendD, From: FriendD)) |
| 4629 | return std::move(Err); |
| 4630 | |
| 4631 | if (FriendD->getFriendObjectKind() != Decl::FOK_None && |
| 4632 | !(FriendD->isInIdentifierNamespace(NS: Decl::IDNS_NonMemberOperator))) |
| 4633 | ToFriendD->setObjectOfFriendDecl(false); |
| 4634 | |
| 4635 | ToFU = ToFriendD; |
| 4636 | } else { // The friend is a type, not a decl. |
| 4637 | if (auto TSIOrErr = import(From: D->getFriendType())) |
| 4638 | ToFU = *TSIOrErr; |
| 4639 | else |
| 4640 | return TSIOrErr.takeError(); |
| 4641 | } |
| 4642 | |
| 4643 | SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists); |
| 4644 | auto **FromTPLists = D->getTrailingObjects(); |
| 4645 | for (unsigned I = 0; I < D->NumTPLists; I++) { |
| 4646 | if (auto ListOrErr = import(From: FromTPLists[I])) |
| 4647 | ToTPLists[I] = *ListOrErr; |
| 4648 | else |
| 4649 | return ListOrErr.takeError(); |
| 4650 | } |
| 4651 | |
| 4652 | auto LocationOrErr = import(From: D->getLocation()); |
| 4653 | if (!LocationOrErr) |
| 4654 | return LocationOrErr.takeError(); |
| 4655 | auto FriendLocOrErr = import(From: D->getFriendLoc()); |
| 4656 | if (!FriendLocOrErr) |
| 4657 | return FriendLocOrErr.takeError(); |
| 4658 | auto EllipsisLocOrErr = import(From: D->getEllipsisLoc()); |
| 4659 | if (!EllipsisLocOrErr) |
| 4660 | return EllipsisLocOrErr.takeError(); |
| 4661 | |
| 4662 | FriendDecl *FrD; |
| 4663 | if (GetImportedOrCreateDecl(ToD&: FrD, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 4664 | args&: *LocationOrErr, args&: ToFU, args&: *FriendLocOrErr, |
| 4665 | args&: *EllipsisLocOrErr, args&: ToTPLists)) |
| 4666 | return FrD; |
| 4667 | |
| 4668 | FrD->setAccess(D->getAccess()); |
| 4669 | FrD->setLexicalDeclContext(LexicalDC); |
| 4670 | LexicalDC->addDeclInternal(D: FrD); |
| 4671 | return FrD; |
| 4672 | } |
| 4673 | |
| 4674 | ExpectedDecl ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) { |
| 4675 | // Import the major distinguishing characteristics of an ivar. |
| 4676 | DeclContext *DC, *LexicalDC; |
| 4677 | DeclarationName Name; |
| 4678 | SourceLocation Loc; |
| 4679 | NamedDecl *ToD; |
| 4680 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 4681 | return std::move(Err); |
| 4682 | if (ToD) |
| 4683 | return ToD; |
| 4684 | |
| 4685 | // Determine whether we've already imported this ivar |
| 4686 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 4687 | for (auto *FoundDecl : FoundDecls) { |
| 4688 | if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(Val: FoundDecl)) { |
| 4689 | if (Importer.IsStructurallyEquivalent(From: D->getType(), |
| 4690 | To: FoundIvar->getType())) { |
| 4691 | Importer.MapImported(From: D, To: FoundIvar); |
| 4692 | return FoundIvar; |
| 4693 | } |
| 4694 | |
| 4695 | Importer.ToDiag(Loc, DiagID: diag::warn_odr_ivar_type_inconsistent) |
| 4696 | << Name << D->getType() << FoundIvar->getType(); |
| 4697 | Importer.ToDiag(Loc: FoundIvar->getLocation(), DiagID: diag::note_odr_value_here) |
| 4698 | << FoundIvar->getType(); |
| 4699 | |
| 4700 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 4701 | } |
| 4702 | } |
| 4703 | |
| 4704 | Error Err = Error::success(); |
| 4705 | auto ToType = importChecked(Err, From: D->getType()); |
| 4706 | auto ToTypeSourceInfo = importChecked(Err, From: D->getTypeSourceInfo()); |
| 4707 | auto ToBitWidth = importChecked(Err, From: D->getBitWidth()); |
| 4708 | auto ToInnerLocStart = importChecked(Err, From: D->getInnerLocStart()); |
| 4709 | if (Err) |
| 4710 | return std::move(Err); |
| 4711 | |
| 4712 | ObjCIvarDecl *ToIvar; |
| 4713 | if (GetImportedOrCreateDecl( |
| 4714 | ToD&: ToIvar, FromD: D, args&: Importer.getToContext(), args: cast<ObjCContainerDecl>(Val: DC), |
| 4715 | args&: ToInnerLocStart, args&: Loc, args: Name.getAsIdentifierInfo(), |
| 4716 | args&: ToType, args&: ToTypeSourceInfo, |
| 4717 | args: D->getAccessControl(),args&: ToBitWidth, args: D->getSynthesize())) |
| 4718 | return ToIvar; |
| 4719 | |
| 4720 | ToIvar->setLexicalDeclContext(LexicalDC); |
| 4721 | LexicalDC->addDeclInternal(D: ToIvar); |
| 4722 | return ToIvar; |
| 4723 | } |
| 4724 | |
| 4725 | ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) { |
| 4726 | |
| 4727 | SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D); |
| 4728 | auto RedeclIt = Redecls.begin(); |
| 4729 | // Import the first part of the decl chain. I.e. import all previous |
| 4730 | // declarations starting from the canonical decl. |
| 4731 | for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) { |
| 4732 | ExpectedDecl RedeclOrErr = import(From: *RedeclIt); |
| 4733 | if (!RedeclOrErr) |
| 4734 | return RedeclOrErr.takeError(); |
| 4735 | } |
| 4736 | assert(*RedeclIt == D); |
| 4737 | |
| 4738 | // Import the major distinguishing characteristics of a variable. |
| 4739 | DeclContext *DC, *LexicalDC; |
| 4740 | DeclarationName Name; |
| 4741 | SourceLocation Loc; |
| 4742 | NamedDecl *ToD; |
| 4743 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 4744 | return std::move(Err); |
| 4745 | if (ToD) |
| 4746 | return ToD; |
| 4747 | |
| 4748 | // Try to find a variable in our own ("to") context with the same name and |
| 4749 | // in the same context as the variable we're importing. |
| 4750 | VarDecl *FoundByLookup = nullptr; |
| 4751 | if (D->isFileVarDecl()) { |
| 4752 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 4753 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 4754 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 4755 | for (auto *FoundDecl : FoundDecls) { |
| 4756 | if (!FoundDecl->isInIdentifierNamespace(NS: IDNS)) |
| 4757 | continue; |
| 4758 | |
| 4759 | if (auto *FoundVar = dyn_cast<VarDecl>(Val: FoundDecl)) { |
| 4760 | if (!hasSameVisibilityContextAndLinkage(Found: FoundVar, From: D)) |
| 4761 | continue; |
| 4762 | if (Importer.IsStructurallyEquivalent(From: D->getType(), |
| 4763 | To: FoundVar->getType())) { |
| 4764 | |
| 4765 | // The VarDecl in the "From" context has a definition, but in the |
| 4766 | // "To" context we already have a definition. |
| 4767 | VarDecl *FoundDef = FoundVar->getDefinition(); |
| 4768 | if (D->isThisDeclarationADefinition() && FoundDef) |
| 4769 | // FIXME Check for ODR error if the two definitions have |
| 4770 | // different initializers? |
| 4771 | return Importer.MapImported(From: D, To: FoundDef); |
| 4772 | |
| 4773 | // The VarDecl in the "From" context has an initializer, but in the |
| 4774 | // "To" context we already have an initializer. |
| 4775 | const VarDecl *FoundDInit = nullptr; |
| 4776 | if (D->getInit() && FoundVar->getAnyInitializer(D&: FoundDInit)) |
| 4777 | // FIXME Diagnose ODR error if the two initializers are different? |
| 4778 | return Importer.MapImported(From: D, To: const_cast<VarDecl*>(FoundDInit)); |
| 4779 | |
| 4780 | FoundByLookup = FoundVar; |
| 4781 | break; |
| 4782 | } |
| 4783 | |
| 4784 | const ArrayType *FoundArray |
| 4785 | = Importer.getToContext().getAsArrayType(T: FoundVar->getType()); |
| 4786 | const ArrayType *TArray |
| 4787 | = Importer.getToContext().getAsArrayType(T: D->getType()); |
| 4788 | if (FoundArray && TArray) { |
| 4789 | if (isa<IncompleteArrayType>(Val: FoundArray) && |
| 4790 | isa<ConstantArrayType>(Val: TArray)) { |
| 4791 | // Import the type. |
| 4792 | if (auto TyOrErr = import(From: D->getType())) |
| 4793 | FoundVar->setType(*TyOrErr); |
| 4794 | else |
| 4795 | return TyOrErr.takeError(); |
| 4796 | |
| 4797 | FoundByLookup = FoundVar; |
| 4798 | break; |
| 4799 | } else if (isa<IncompleteArrayType>(Val: TArray) && |
| 4800 | isa<ConstantArrayType>(Val: FoundArray)) { |
| 4801 | FoundByLookup = FoundVar; |
| 4802 | break; |
| 4803 | } |
| 4804 | } |
| 4805 | |
| 4806 | Importer.ToDiag(Loc, DiagID: diag::warn_odr_variable_type_inconsistent) |
| 4807 | << Name << D->getType() << FoundVar->getType(); |
| 4808 | Importer.ToDiag(Loc: FoundVar->getLocation(), DiagID: diag::note_odr_value_here) |
| 4809 | << FoundVar->getType(); |
| 4810 | ConflictingDecls.push_back(Elt: FoundDecl); |
| 4811 | } |
| 4812 | } |
| 4813 | |
| 4814 | if (!ConflictingDecls.empty()) { |
| 4815 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
| 4816 | Name, DC, IDNS, Decls: ConflictingDecls.data(), NumDecls: ConflictingDecls.size()); |
| 4817 | if (NameOrErr) |
| 4818 | Name = NameOrErr.get(); |
| 4819 | else |
| 4820 | return NameOrErr.takeError(); |
| 4821 | } |
| 4822 | } |
| 4823 | |
| 4824 | Error Err = Error::success(); |
| 4825 | auto ToType = importChecked(Err, From: D->getType()); |
| 4826 | auto ToTypeSourceInfo = importChecked(Err, From: D->getTypeSourceInfo()); |
| 4827 | auto ToInnerLocStart = importChecked(Err, From: D->getInnerLocStart()); |
| 4828 | auto ToQualifierLoc = importChecked(Err, From: D->getQualifierLoc()); |
| 4829 | if (Err) |
| 4830 | return std::move(Err); |
| 4831 | |
| 4832 | VarDecl *ToVar; |
| 4833 | if (auto *FromDecomp = dyn_cast<DecompositionDecl>(Val: D)) { |
| 4834 | SmallVector<BindingDecl *> Bindings(FromDecomp->bindings().size()); |
| 4835 | if (Error Err = |
| 4836 | ImportArrayChecked(InContainer: FromDecomp->bindings(), Obegin: Bindings.begin())) |
| 4837 | return std::move(Err); |
| 4838 | DecompositionDecl *ToDecomp; |
| 4839 | if (GetImportedOrCreateDecl( |
| 4840 | ToD&: ToDecomp, FromD: FromDecomp, args&: Importer.getToContext(), args&: DC, args&: ToInnerLocStart, |
| 4841 | args&: Loc, args&: ToType, args&: ToTypeSourceInfo, args: D->getStorageClass(), args&: Bindings)) |
| 4842 | return ToDecomp; |
| 4843 | ToVar = ToDecomp; |
| 4844 | } else { |
| 4845 | // Create the imported variable. |
| 4846 | if (GetImportedOrCreateDecl(ToD&: ToVar, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 4847 | args&: ToInnerLocStart, args&: Loc, |
| 4848 | args: Name.getAsIdentifierInfo(), args&: ToType, |
| 4849 | args&: ToTypeSourceInfo, args: D->getStorageClass())) |
| 4850 | return ToVar; |
| 4851 | } |
| 4852 | |
| 4853 | ToVar->setTSCSpec(D->getTSCSpec()); |
| 4854 | ToVar->setQualifierInfo(ToQualifierLoc); |
| 4855 | ToVar->setAccess(D->getAccess()); |
| 4856 | ToVar->setLexicalDeclContext(LexicalDC); |
| 4857 | if (D->isInlineSpecified()) |
| 4858 | ToVar->setInlineSpecified(); |
| 4859 | if (D->isInline()) |
| 4860 | ToVar->setImplicitlyInline(); |
| 4861 | |
| 4862 | if (FoundByLookup) { |
| 4863 | auto *Recent = const_cast<VarDecl *>(FoundByLookup->getMostRecentDecl()); |
| 4864 | ToVar->setPreviousDecl(Recent); |
| 4865 | } |
| 4866 | |
| 4867 | // Import the described template, if any. |
| 4868 | if (D->getDescribedVarTemplate()) { |
| 4869 | auto ToVTOrErr = import(From: D->getDescribedVarTemplate()); |
| 4870 | if (!ToVTOrErr) |
| 4871 | return ToVTOrErr.takeError(); |
| 4872 | } else if (MemberSpecializationInfo *MSI = D->getMemberSpecializationInfo()) { |
| 4873 | TemplateSpecializationKind SK = MSI->getTemplateSpecializationKind(); |
| 4874 | VarDecl *FromInst = D->getInstantiatedFromStaticDataMember(); |
| 4875 | if (Expected<VarDecl *> ToInstOrErr = import(From: FromInst)) |
| 4876 | ToVar->setInstantiationOfStaticDataMember(VD: *ToInstOrErr, TSK: SK); |
| 4877 | else |
| 4878 | return ToInstOrErr.takeError(); |
| 4879 | if (ExpectedSLoc POIOrErr = import(From: MSI->getPointOfInstantiation())) |
| 4880 | ToVar->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr); |
| 4881 | else |
| 4882 | return POIOrErr.takeError(); |
| 4883 | } |
| 4884 | |
| 4885 | if (Error Err = ImportInitializer(From: D, To: ToVar)) |
| 4886 | return std::move(Err); |
| 4887 | |
| 4888 | if (D->isConstexpr()) |
| 4889 | ToVar->setConstexpr(true); |
| 4890 | |
| 4891 | addDeclToContexts(FromD: D, ToD: ToVar); |
| 4892 | |
| 4893 | // Import the rest of the chain. I.e. import all subsequent declarations. |
| 4894 | for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) { |
| 4895 | ExpectedDecl RedeclOrErr = import(From: *RedeclIt); |
| 4896 | if (!RedeclOrErr) |
| 4897 | return RedeclOrErr.takeError(); |
| 4898 | } |
| 4899 | |
| 4900 | return ToVar; |
| 4901 | } |
| 4902 | |
| 4903 | ExpectedDecl ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) { |
| 4904 | // Parameters are created in the translation unit's context, then moved |
| 4905 | // into the function declaration's context afterward. |
| 4906 | DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); |
| 4907 | |
| 4908 | Error Err = Error::success(); |
| 4909 | auto ToDeclName = importChecked(Err, From: D->getDeclName()); |
| 4910 | auto ToLocation = importChecked(Err, From: D->getLocation()); |
| 4911 | auto ToType = importChecked(Err, From: D->getType()); |
| 4912 | if (Err) |
| 4913 | return std::move(Err); |
| 4914 | |
| 4915 | // Create the imported parameter. |
| 4916 | ImplicitParamDecl *ToParm = nullptr; |
| 4917 | if (GetImportedOrCreateDecl(ToD&: ToParm, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 4918 | args&: ToLocation, args: ToDeclName.getAsIdentifierInfo(), |
| 4919 | args&: ToType, args: D->getParameterKind())) |
| 4920 | return ToParm; |
| 4921 | return ToParm; |
| 4922 | } |
| 4923 | |
| 4924 | Error ASTNodeImporter::ImportDefaultArgOfParmVarDecl( |
| 4925 | const ParmVarDecl *FromParam, ParmVarDecl *ToParam) { |
| 4926 | |
| 4927 | if (auto LocOrErr = import(From: FromParam->getExplicitObjectParamThisLoc())) |
| 4928 | ToParam->setExplicitObjectParameterLoc(*LocOrErr); |
| 4929 | else |
| 4930 | return LocOrErr.takeError(); |
| 4931 | |
| 4932 | ToParam->setHasInheritedDefaultArg(FromParam->hasInheritedDefaultArg()); |
| 4933 | ToParam->setKNRPromoted(FromParam->isKNRPromoted()); |
| 4934 | |
| 4935 | if (FromParam->hasUninstantiatedDefaultArg()) { |
| 4936 | if (auto ToDefArgOrErr = import(From: FromParam->getUninstantiatedDefaultArg())) |
| 4937 | ToParam->setUninstantiatedDefaultArg(*ToDefArgOrErr); |
| 4938 | else |
| 4939 | return ToDefArgOrErr.takeError(); |
| 4940 | } else if (FromParam->hasUnparsedDefaultArg()) { |
| 4941 | ToParam->setUnparsedDefaultArg(); |
| 4942 | } else if (FromParam->hasDefaultArg()) { |
| 4943 | if (auto ToDefArgOrErr = import(From: FromParam->getDefaultArg())) |
| 4944 | ToParam->setDefaultArg(*ToDefArgOrErr); |
| 4945 | else |
| 4946 | return ToDefArgOrErr.takeError(); |
| 4947 | } |
| 4948 | |
| 4949 | return Error::success(); |
| 4950 | } |
| 4951 | |
| 4952 | Expected<InheritedConstructor> |
| 4953 | ASTNodeImporter::ImportInheritedConstructor(const InheritedConstructor &From) { |
| 4954 | Error Err = Error::success(); |
| 4955 | CXXConstructorDecl *ToBaseCtor = importChecked(Err, From: From.getConstructor()); |
| 4956 | ConstructorUsingShadowDecl *ToShadow = |
| 4957 | importChecked(Err, From: From.getShadowDecl()); |
| 4958 | if (Err) |
| 4959 | return std::move(Err); |
| 4960 | return InheritedConstructor(ToShadow, ToBaseCtor); |
| 4961 | } |
| 4962 | |
| 4963 | ExpectedDecl ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) { |
| 4964 | // Parameters are created in the translation unit's context, then moved |
| 4965 | // into the function declaration's context afterward. |
| 4966 | DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); |
| 4967 | |
| 4968 | Error Err = Error::success(); |
| 4969 | auto ToDeclName = importChecked(Err, From: D->getDeclName()); |
| 4970 | auto ToLocation = importChecked(Err, From: D->getLocation()); |
| 4971 | auto ToInnerLocStart = importChecked(Err, From: D->getInnerLocStart()); |
| 4972 | auto ToType = importChecked(Err, From: D->getType()); |
| 4973 | auto ToTypeSourceInfo = importChecked(Err, From: D->getTypeSourceInfo()); |
| 4974 | if (Err) |
| 4975 | return std::move(Err); |
| 4976 | |
| 4977 | ParmVarDecl *ToParm; |
| 4978 | if (GetImportedOrCreateDecl(ToD&: ToParm, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 4979 | args&: ToInnerLocStart, args&: ToLocation, |
| 4980 | args: ToDeclName.getAsIdentifierInfo(), args&: ToType, |
| 4981 | args&: ToTypeSourceInfo, args: D->getStorageClass(), |
| 4982 | /*DefaultArg*/ args: nullptr)) |
| 4983 | return ToParm; |
| 4984 | |
| 4985 | // Set the default argument. It should be no problem if it was already done. |
| 4986 | // Do not import the default expression before GetImportedOrCreateDecl call |
| 4987 | // to avoid possible infinite import loop because circular dependency. |
| 4988 | if (Error Err = ImportDefaultArgOfParmVarDecl(FromParam: D, ToParam: ToParm)) |
| 4989 | return std::move(Err); |
| 4990 | |
| 4991 | if (D->isObjCMethodParameter()) { |
| 4992 | ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex()); |
| 4993 | ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier()); |
| 4994 | } else { |
| 4995 | ToParm->setScopeInfo(scopeDepth: D->getFunctionScopeDepth(), |
| 4996 | parameterIndex: D->getFunctionScopeIndex()); |
| 4997 | } |
| 4998 | |
| 4999 | return ToParm; |
| 5000 | } |
| 5001 | |
| 5002 | ExpectedDecl ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) { |
| 5003 | // Import the major distinguishing characteristics of a method. |
| 5004 | DeclContext *DC, *LexicalDC; |
| 5005 | DeclarationName Name; |
| 5006 | SourceLocation Loc; |
| 5007 | NamedDecl *ToD; |
| 5008 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 5009 | return std::move(Err); |
| 5010 | if (ToD) |
| 5011 | return ToD; |
| 5012 | |
| 5013 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 5014 | for (auto *FoundDecl : FoundDecls) { |
| 5015 | if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(Val: FoundDecl)) { |
| 5016 | if (FoundMethod->isInstanceMethod() != D->isInstanceMethod()) |
| 5017 | continue; |
| 5018 | |
| 5019 | // Check return types. |
| 5020 | if (!Importer.IsStructurallyEquivalent(From: D->getReturnType(), |
| 5021 | To: FoundMethod->getReturnType())) { |
| 5022 | Importer.ToDiag(Loc, DiagID: diag::warn_odr_objc_method_result_type_inconsistent) |
| 5023 | << D->isInstanceMethod() << Name << D->getReturnType() |
| 5024 | << FoundMethod->getReturnType(); |
| 5025 | Importer.ToDiag(Loc: FoundMethod->getLocation(), |
| 5026 | DiagID: diag::note_odr_objc_method_here) |
| 5027 | << D->isInstanceMethod() << Name; |
| 5028 | |
| 5029 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 5030 | } |
| 5031 | |
| 5032 | // Check the number of parameters. |
| 5033 | if (D->param_size() != FoundMethod->param_size()) { |
| 5034 | Importer.ToDiag(Loc, DiagID: diag::warn_odr_objc_method_num_params_inconsistent) |
| 5035 | << D->isInstanceMethod() << Name |
| 5036 | << D->param_size() << FoundMethod->param_size(); |
| 5037 | Importer.ToDiag(Loc: FoundMethod->getLocation(), |
| 5038 | DiagID: diag::note_odr_objc_method_here) |
| 5039 | << D->isInstanceMethod() << Name; |
| 5040 | |
| 5041 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 5042 | } |
| 5043 | |
| 5044 | // Check parameter types. |
| 5045 | for (ObjCMethodDecl::param_iterator P = D->param_begin(), |
| 5046 | PEnd = D->param_end(), FoundP = FoundMethod->param_begin(); |
| 5047 | P != PEnd; ++P, ++FoundP) { |
| 5048 | if (!Importer.IsStructurallyEquivalent(From: (*P)->getType(), |
| 5049 | To: (*FoundP)->getType())) { |
| 5050 | Importer.FromDiag(Loc: (*P)->getLocation(), |
| 5051 | DiagID: diag::warn_odr_objc_method_param_type_inconsistent) |
| 5052 | << D->isInstanceMethod() << Name |
| 5053 | << (*P)->getType() << (*FoundP)->getType(); |
| 5054 | Importer.ToDiag(Loc: (*FoundP)->getLocation(), DiagID: diag::note_odr_value_here) |
| 5055 | << (*FoundP)->getType(); |
| 5056 | |
| 5057 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 5058 | } |
| 5059 | } |
| 5060 | |
| 5061 | // Check variadic/non-variadic. |
| 5062 | // Check the number of parameters. |
| 5063 | if (D->isVariadic() != FoundMethod->isVariadic()) { |
| 5064 | Importer.ToDiag(Loc, DiagID: diag::warn_odr_objc_method_variadic_inconsistent) |
| 5065 | << D->isInstanceMethod() << Name; |
| 5066 | Importer.ToDiag(Loc: FoundMethod->getLocation(), |
| 5067 | DiagID: diag::note_odr_objc_method_here) |
| 5068 | << D->isInstanceMethod() << Name; |
| 5069 | |
| 5070 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 5071 | } |
| 5072 | |
| 5073 | // FIXME: Any other bits we need to merge? |
| 5074 | return Importer.MapImported(From: D, To: FoundMethod); |
| 5075 | } |
| 5076 | } |
| 5077 | |
| 5078 | Error Err = Error::success(); |
| 5079 | auto ToEndLoc = importChecked(Err, From: D->getEndLoc()); |
| 5080 | auto ToReturnType = importChecked(Err, From: D->getReturnType()); |
| 5081 | auto ToReturnTypeSourceInfo = |
| 5082 | importChecked(Err, From: D->getReturnTypeSourceInfo()); |
| 5083 | if (Err) |
| 5084 | return std::move(Err); |
| 5085 | |
| 5086 | ObjCMethodDecl *ToMethod; |
| 5087 | if (GetImportedOrCreateDecl( |
| 5088 | ToD&: ToMethod, FromD: D, args&: Importer.getToContext(), args&: Loc, args&: ToEndLoc, |
| 5089 | args: Name.getObjCSelector(), args&: ToReturnType, args&: ToReturnTypeSourceInfo, args&: DC, |
| 5090 | args: D->isInstanceMethod(), args: D->isVariadic(), args: D->isPropertyAccessor(), |
| 5091 | args: D->isSynthesizedAccessorStub(), args: D->isImplicit(), args: D->isDefined(), |
| 5092 | args: D->getImplementationControl(), args: D->hasRelatedResultType())) |
| 5093 | return ToMethod; |
| 5094 | |
| 5095 | // FIXME: When we decide to merge method definitions, we'll need to |
| 5096 | // deal with implicit parameters. |
| 5097 | |
| 5098 | // Import the parameters |
| 5099 | SmallVector<ParmVarDecl *, 5> ToParams; |
| 5100 | for (auto *FromP : D->parameters()) { |
| 5101 | if (Expected<ParmVarDecl *> ToPOrErr = import(From: FromP)) |
| 5102 | ToParams.push_back(Elt: *ToPOrErr); |
| 5103 | else |
| 5104 | return ToPOrErr.takeError(); |
| 5105 | } |
| 5106 | |
| 5107 | // Set the parameters. |
| 5108 | for (auto *ToParam : ToParams) { |
| 5109 | ToParam->setOwningFunction(ToMethod); |
| 5110 | ToMethod->addDeclInternal(D: ToParam); |
| 5111 | } |
| 5112 | |
| 5113 | SmallVector<SourceLocation, 12> FromSelLocs; |
| 5114 | D->getSelectorLocs(SelLocs&: FromSelLocs); |
| 5115 | SmallVector<SourceLocation, 12> ToSelLocs(FromSelLocs.size()); |
| 5116 | if (Error Err = ImportContainerChecked(InContainer: FromSelLocs, OutContainer&: ToSelLocs)) |
| 5117 | return std::move(Err); |
| 5118 | |
| 5119 | ToMethod->setMethodParams(C&: Importer.getToContext(), Params: ToParams, SelLocs: ToSelLocs); |
| 5120 | |
| 5121 | ToMethod->setLexicalDeclContext(LexicalDC); |
| 5122 | LexicalDC->addDeclInternal(D: ToMethod); |
| 5123 | |
| 5124 | // Implicit params are declared when Sema encounters the definition but this |
| 5125 | // never happens when the method is imported. Manually declare the implicit |
| 5126 | // params now that the MethodDecl knows its class interface. |
| 5127 | if (D->getSelfDecl()) |
| 5128 | ToMethod->createImplicitParams(Context&: Importer.getToContext(), |
| 5129 | ID: ToMethod->getClassInterface()); |
| 5130 | |
| 5131 | return ToMethod; |
| 5132 | } |
| 5133 | |
| 5134 | ExpectedDecl ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) { |
| 5135 | // Import the major distinguishing characteristics of a category. |
| 5136 | DeclContext *DC, *LexicalDC; |
| 5137 | DeclarationName Name; |
| 5138 | SourceLocation Loc; |
| 5139 | NamedDecl *ToD; |
| 5140 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 5141 | return std::move(Err); |
| 5142 | if (ToD) |
| 5143 | return ToD; |
| 5144 | |
| 5145 | Error Err = Error::success(); |
| 5146 | auto ToVarianceLoc = importChecked(Err, From: D->getVarianceLoc()); |
| 5147 | auto ToLocation = importChecked(Err, From: D->getLocation()); |
| 5148 | auto ToColonLoc = importChecked(Err, From: D->getColonLoc()); |
| 5149 | auto ToTypeSourceInfo = importChecked(Err, From: D->getTypeSourceInfo()); |
| 5150 | if (Err) |
| 5151 | return std::move(Err); |
| 5152 | |
| 5153 | ObjCTypeParamDecl *Result; |
| 5154 | if (GetImportedOrCreateDecl( |
| 5155 | ToD&: Result, FromD: D, args&: Importer.getToContext(), args&: DC, args: D->getVariance(), |
| 5156 | args&: ToVarianceLoc, args: D->getIndex(), |
| 5157 | args&: ToLocation, args: Name.getAsIdentifierInfo(), |
| 5158 | args&: ToColonLoc, args&: ToTypeSourceInfo)) |
| 5159 | return Result; |
| 5160 | |
| 5161 | // Only import 'ObjCTypeParamType' after the decl is created. |
| 5162 | auto ToTypeForDecl = importChecked(Err, From: D->getTypeForDecl()); |
| 5163 | if (Err) |
| 5164 | return std::move(Err); |
| 5165 | Result->setTypeForDecl(ToTypeForDecl); |
| 5166 | Result->setLexicalDeclContext(LexicalDC); |
| 5167 | return Result; |
| 5168 | } |
| 5169 | |
| 5170 | ExpectedDecl ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) { |
| 5171 | // Import the major distinguishing characteristics of a category. |
| 5172 | DeclContext *DC, *LexicalDC; |
| 5173 | DeclarationName Name; |
| 5174 | SourceLocation Loc; |
| 5175 | NamedDecl *ToD; |
| 5176 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 5177 | return std::move(Err); |
| 5178 | if (ToD) |
| 5179 | return ToD; |
| 5180 | |
| 5181 | ObjCInterfaceDecl *ToInterface; |
| 5182 | if (Error Err = importInto(To&: ToInterface, From: D->getClassInterface())) |
| 5183 | return std::move(Err); |
| 5184 | |
| 5185 | // Determine if we've already encountered this category. |
| 5186 | ObjCCategoryDecl *MergeWithCategory |
| 5187 | = ToInterface->FindCategoryDeclaration(CategoryId: Name.getAsIdentifierInfo()); |
| 5188 | ObjCCategoryDecl *ToCategory = MergeWithCategory; |
| 5189 | if (!ToCategory) { |
| 5190 | |
| 5191 | Error Err = Error::success(); |
| 5192 | auto ToAtStartLoc = importChecked(Err, From: D->getAtStartLoc()); |
| 5193 | auto ToCategoryNameLoc = importChecked(Err, From: D->getCategoryNameLoc()); |
| 5194 | auto ToIvarLBraceLoc = importChecked(Err, From: D->getIvarLBraceLoc()); |
| 5195 | auto ToIvarRBraceLoc = importChecked(Err, From: D->getIvarRBraceLoc()); |
| 5196 | if (Err) |
| 5197 | return std::move(Err); |
| 5198 | |
| 5199 | if (GetImportedOrCreateDecl(ToD&: ToCategory, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 5200 | args&: ToAtStartLoc, args&: Loc, |
| 5201 | args&: ToCategoryNameLoc, |
| 5202 | args: Name.getAsIdentifierInfo(), args&: ToInterface, |
| 5203 | /*TypeParamList=*/args: nullptr, |
| 5204 | args&: ToIvarLBraceLoc, |
| 5205 | args&: ToIvarRBraceLoc)) |
| 5206 | return ToCategory; |
| 5207 | |
| 5208 | ToCategory->setLexicalDeclContext(LexicalDC); |
| 5209 | LexicalDC->addDeclInternal(D: ToCategory); |
| 5210 | // Import the type parameter list after MapImported, to avoid |
| 5211 | // loops when bringing in their DeclContext. |
| 5212 | if (auto PListOrErr = ImportObjCTypeParamList(list: D->getTypeParamList())) |
| 5213 | ToCategory->setTypeParamList(*PListOrErr); |
| 5214 | else |
| 5215 | return PListOrErr.takeError(); |
| 5216 | |
| 5217 | // Import protocols |
| 5218 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 5219 | SmallVector<SourceLocation, 4> ProtocolLocs; |
| 5220 | ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc |
| 5221 | = D->protocol_loc_begin(); |
| 5222 | for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(), |
| 5223 | FromProtoEnd = D->protocol_end(); |
| 5224 | FromProto != FromProtoEnd; |
| 5225 | ++FromProto, ++FromProtoLoc) { |
| 5226 | if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(From: *FromProto)) |
| 5227 | Protocols.push_back(Elt: *ToProtoOrErr); |
| 5228 | else |
| 5229 | return ToProtoOrErr.takeError(); |
| 5230 | |
| 5231 | if (ExpectedSLoc ToProtoLocOrErr = import(From: *FromProtoLoc)) |
| 5232 | ProtocolLocs.push_back(Elt: *ToProtoLocOrErr); |
| 5233 | else |
| 5234 | return ToProtoLocOrErr.takeError(); |
| 5235 | } |
| 5236 | |
| 5237 | // FIXME: If we're merging, make sure that the protocol list is the same. |
| 5238 | ToCategory->setProtocolList(List: Protocols.data(), Num: Protocols.size(), |
| 5239 | Locs: ProtocolLocs.data(), C&: Importer.getToContext()); |
| 5240 | |
| 5241 | } else { |
| 5242 | Importer.MapImported(From: D, To: ToCategory); |
| 5243 | } |
| 5244 | |
| 5245 | // Import all of the members of this category. |
| 5246 | if (Error Err = ImportDeclContext(FromDC: D)) |
| 5247 | return std::move(Err); |
| 5248 | |
| 5249 | // If we have an implementation, import it as well. |
| 5250 | if (D->getImplementation()) { |
| 5251 | if (Expected<ObjCCategoryImplDecl *> ToImplOrErr = |
| 5252 | import(From: D->getImplementation())) |
| 5253 | ToCategory->setImplementation(*ToImplOrErr); |
| 5254 | else |
| 5255 | return ToImplOrErr.takeError(); |
| 5256 | } |
| 5257 | |
| 5258 | return ToCategory; |
| 5259 | } |
| 5260 | |
| 5261 | Error ASTNodeImporter::ImportDefinition( |
| 5262 | ObjCProtocolDecl *From, ObjCProtocolDecl *To, ImportDefinitionKind Kind) { |
| 5263 | if (To->getDefinition()) { |
| 5264 | if (shouldForceImportDeclContext(IDK: Kind)) |
| 5265 | if (Error Err = ImportDeclContext(FromDC: From)) |
| 5266 | return Err; |
| 5267 | return Error::success(); |
| 5268 | } |
| 5269 | |
| 5270 | // Start the protocol definition |
| 5271 | To->startDefinition(); |
| 5272 | |
| 5273 | // Import protocols |
| 5274 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 5275 | SmallVector<SourceLocation, 4> ProtocolLocs; |
| 5276 | ObjCProtocolDecl::protocol_loc_iterator FromProtoLoc = |
| 5277 | From->protocol_loc_begin(); |
| 5278 | for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(), |
| 5279 | FromProtoEnd = From->protocol_end(); |
| 5280 | FromProto != FromProtoEnd; |
| 5281 | ++FromProto, ++FromProtoLoc) { |
| 5282 | if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(From: *FromProto)) |
| 5283 | Protocols.push_back(Elt: *ToProtoOrErr); |
| 5284 | else |
| 5285 | return ToProtoOrErr.takeError(); |
| 5286 | |
| 5287 | if (ExpectedSLoc ToProtoLocOrErr = import(From: *FromProtoLoc)) |
| 5288 | ProtocolLocs.push_back(Elt: *ToProtoLocOrErr); |
| 5289 | else |
| 5290 | return ToProtoLocOrErr.takeError(); |
| 5291 | |
| 5292 | } |
| 5293 | |
| 5294 | // FIXME: If we're merging, make sure that the protocol list is the same. |
| 5295 | To->setProtocolList(List: Protocols.data(), Num: Protocols.size(), |
| 5296 | Locs: ProtocolLocs.data(), C&: Importer.getToContext()); |
| 5297 | |
| 5298 | if (shouldForceImportDeclContext(IDK: Kind)) { |
| 5299 | // Import all of the members of this protocol. |
| 5300 | if (Error Err = ImportDeclContext(FromDC: From, /*ForceImport=*/true)) |
| 5301 | return Err; |
| 5302 | } |
| 5303 | return Error::success(); |
| 5304 | } |
| 5305 | |
| 5306 | ExpectedDecl ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) { |
| 5307 | // If this protocol has a definition in the translation unit we're coming |
| 5308 | // from, but this particular declaration is not that definition, import the |
| 5309 | // definition and map to that. |
| 5310 | ObjCProtocolDecl *Definition = D->getDefinition(); |
| 5311 | if (Definition && Definition != D) { |
| 5312 | if (ExpectedDecl ImportedDefOrErr = import(From: Definition)) |
| 5313 | return Importer.MapImported(From: D, To: *ImportedDefOrErr); |
| 5314 | else |
| 5315 | return ImportedDefOrErr.takeError(); |
| 5316 | } |
| 5317 | |
| 5318 | // Import the major distinguishing characteristics of a protocol. |
| 5319 | DeclContext *DC, *LexicalDC; |
| 5320 | DeclarationName Name; |
| 5321 | SourceLocation Loc; |
| 5322 | NamedDecl *ToD; |
| 5323 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 5324 | return std::move(Err); |
| 5325 | if (ToD) |
| 5326 | return ToD; |
| 5327 | |
| 5328 | ObjCProtocolDecl *MergeWithProtocol = nullptr; |
| 5329 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 5330 | for (auto *FoundDecl : FoundDecls) { |
| 5331 | if (!FoundDecl->isInIdentifierNamespace(NS: Decl::IDNS_ObjCProtocol)) |
| 5332 | continue; |
| 5333 | |
| 5334 | if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(Val: FoundDecl))) |
| 5335 | break; |
| 5336 | } |
| 5337 | |
| 5338 | ObjCProtocolDecl *ToProto = MergeWithProtocol; |
| 5339 | if (!ToProto) { |
| 5340 | auto ToAtBeginLocOrErr = import(From: D->getAtStartLoc()); |
| 5341 | if (!ToAtBeginLocOrErr) |
| 5342 | return ToAtBeginLocOrErr.takeError(); |
| 5343 | |
| 5344 | if (GetImportedOrCreateDecl(ToD&: ToProto, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 5345 | args: Name.getAsIdentifierInfo(), args&: Loc, |
| 5346 | args&: *ToAtBeginLocOrErr, |
| 5347 | /*PrevDecl=*/args: nullptr)) |
| 5348 | return ToProto; |
| 5349 | ToProto->setLexicalDeclContext(LexicalDC); |
| 5350 | LexicalDC->addDeclInternal(D: ToProto); |
| 5351 | } |
| 5352 | |
| 5353 | Importer.MapImported(From: D, To: ToProto); |
| 5354 | |
| 5355 | if (D->isThisDeclarationADefinition()) |
| 5356 | if (Error Err = ImportDefinition(From: D, To: ToProto)) |
| 5357 | return std::move(Err); |
| 5358 | |
| 5359 | return ToProto; |
| 5360 | } |
| 5361 | |
| 5362 | ExpectedDecl ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 5363 | DeclContext *DC, *LexicalDC; |
| 5364 | if (Error Err = ImportDeclContext(FromD: D, ToDC&: DC, ToLexicalDC&: LexicalDC)) |
| 5365 | return std::move(Err); |
| 5366 | |
| 5367 | ExpectedSLoc ExternLocOrErr = import(From: D->getExternLoc()); |
| 5368 | if (!ExternLocOrErr) |
| 5369 | return ExternLocOrErr.takeError(); |
| 5370 | |
| 5371 | ExpectedSLoc LangLocOrErr = import(From: D->getLocation()); |
| 5372 | if (!LangLocOrErr) |
| 5373 | return LangLocOrErr.takeError(); |
| 5374 | |
| 5375 | bool HasBraces = D->hasBraces(); |
| 5376 | |
| 5377 | LinkageSpecDecl *ToLinkageSpec; |
| 5378 | if (GetImportedOrCreateDecl(ToD&: ToLinkageSpec, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 5379 | args&: *ExternLocOrErr, args&: *LangLocOrErr, |
| 5380 | args: D->getLanguage(), args&: HasBraces)) |
| 5381 | return ToLinkageSpec; |
| 5382 | |
| 5383 | if (HasBraces) { |
| 5384 | ExpectedSLoc RBraceLocOrErr = import(From: D->getRBraceLoc()); |
| 5385 | if (!RBraceLocOrErr) |
| 5386 | return RBraceLocOrErr.takeError(); |
| 5387 | ToLinkageSpec->setRBraceLoc(*RBraceLocOrErr); |
| 5388 | } |
| 5389 | |
| 5390 | ToLinkageSpec->setLexicalDeclContext(LexicalDC); |
| 5391 | LexicalDC->addDeclInternal(D: ToLinkageSpec); |
| 5392 | |
| 5393 | return ToLinkageSpec; |
| 5394 | } |
| 5395 | |
| 5396 | ExpectedDecl ASTNodeImporter::ImportUsingShadowDecls(BaseUsingDecl *D, |
| 5397 | BaseUsingDecl *ToSI) { |
| 5398 | for (UsingShadowDecl *FromShadow : D->shadows()) { |
| 5399 | if (Expected<UsingShadowDecl *> ToShadowOrErr = import(From: FromShadow)) |
| 5400 | ToSI->addShadowDecl(S: *ToShadowOrErr); |
| 5401 | else |
| 5402 | // FIXME: We return error here but the definition is already created |
| 5403 | // and available with lookups. How to fix this?.. |
| 5404 | return ToShadowOrErr.takeError(); |
| 5405 | } |
| 5406 | return ToSI; |
| 5407 | } |
| 5408 | |
| 5409 | ExpectedDecl ASTNodeImporter::VisitUsingDecl(UsingDecl *D) { |
| 5410 | DeclContext *DC, *LexicalDC; |
| 5411 | DeclarationName Name; |
| 5412 | SourceLocation Loc; |
| 5413 | NamedDecl *ToD = nullptr; |
| 5414 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 5415 | return std::move(Err); |
| 5416 | if (ToD) |
| 5417 | return ToD; |
| 5418 | |
| 5419 | Error Err = Error::success(); |
| 5420 | auto ToLoc = importChecked(Err, From: D->getNameInfo().getLoc()); |
| 5421 | auto ToUsingLoc = importChecked(Err, From: D->getUsingLoc()); |
| 5422 | auto ToQualifierLoc = importChecked(Err, From: D->getQualifierLoc()); |
| 5423 | if (Err) |
| 5424 | return std::move(Err); |
| 5425 | |
| 5426 | DeclarationNameInfo NameInfo(Name, ToLoc); |
| 5427 | if (Error Err = ImportDeclarationNameLoc(From: D->getNameInfo(), To&: NameInfo)) |
| 5428 | return std::move(Err); |
| 5429 | |
| 5430 | UsingDecl *ToUsing; |
| 5431 | if (GetImportedOrCreateDecl(ToD&: ToUsing, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 5432 | args&: ToUsingLoc, args&: ToQualifierLoc, args&: NameInfo, |
| 5433 | args: D->hasTypename())) |
| 5434 | return ToUsing; |
| 5435 | |
| 5436 | ToUsing->setLexicalDeclContext(LexicalDC); |
| 5437 | LexicalDC->addDeclInternal(D: ToUsing); |
| 5438 | |
| 5439 | if (NamedDecl *FromPattern = |
| 5440 | Importer.getFromContext().getInstantiatedFromUsingDecl(Inst: D)) { |
| 5441 | if (Expected<NamedDecl *> ToPatternOrErr = import(From: FromPattern)) |
| 5442 | Importer.getToContext().setInstantiatedFromUsingDecl( |
| 5443 | Inst: ToUsing, Pattern: *ToPatternOrErr); |
| 5444 | else |
| 5445 | return ToPatternOrErr.takeError(); |
| 5446 | } |
| 5447 | |
| 5448 | return ImportUsingShadowDecls(D, ToSI: ToUsing); |
| 5449 | } |
| 5450 | |
| 5451 | ExpectedDecl ASTNodeImporter::VisitUsingEnumDecl(UsingEnumDecl *D) { |
| 5452 | DeclContext *DC, *LexicalDC; |
| 5453 | DeclarationName Name; |
| 5454 | SourceLocation Loc; |
| 5455 | NamedDecl *ToD = nullptr; |
| 5456 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 5457 | return std::move(Err); |
| 5458 | if (ToD) |
| 5459 | return ToD; |
| 5460 | |
| 5461 | Error Err = Error::success(); |
| 5462 | auto ToUsingLoc = importChecked(Err, From: D->getUsingLoc()); |
| 5463 | auto ToEnumLoc = importChecked(Err, From: D->getEnumLoc()); |
| 5464 | auto ToNameLoc = importChecked(Err, From: D->getLocation()); |
| 5465 | auto *ToEnumType = importChecked(Err, From: D->getEnumType()); |
| 5466 | if (Err) |
| 5467 | return std::move(Err); |
| 5468 | |
| 5469 | UsingEnumDecl *ToUsingEnum; |
| 5470 | if (GetImportedOrCreateDecl(ToD&: ToUsingEnum, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 5471 | args&: ToUsingLoc, args&: ToEnumLoc, args&: ToNameLoc, args&: ToEnumType)) |
| 5472 | return ToUsingEnum; |
| 5473 | |
| 5474 | ToUsingEnum->setLexicalDeclContext(LexicalDC); |
| 5475 | LexicalDC->addDeclInternal(D: ToUsingEnum); |
| 5476 | |
| 5477 | if (UsingEnumDecl *FromPattern = |
| 5478 | Importer.getFromContext().getInstantiatedFromUsingEnumDecl(Inst: D)) { |
| 5479 | if (Expected<UsingEnumDecl *> ToPatternOrErr = import(From: FromPattern)) |
| 5480 | Importer.getToContext().setInstantiatedFromUsingEnumDecl(Inst: ToUsingEnum, |
| 5481 | Pattern: *ToPatternOrErr); |
| 5482 | else |
| 5483 | return ToPatternOrErr.takeError(); |
| 5484 | } |
| 5485 | |
| 5486 | return ImportUsingShadowDecls(D, ToSI: ToUsingEnum); |
| 5487 | } |
| 5488 | |
| 5489 | ExpectedDecl ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) { |
| 5490 | DeclContext *DC, *LexicalDC; |
| 5491 | DeclarationName Name; |
| 5492 | SourceLocation Loc; |
| 5493 | NamedDecl *ToD = nullptr; |
| 5494 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 5495 | return std::move(Err); |
| 5496 | if (ToD) |
| 5497 | return ToD; |
| 5498 | |
| 5499 | Expected<BaseUsingDecl *> ToIntroducerOrErr = import(From: D->getIntroducer()); |
| 5500 | if (!ToIntroducerOrErr) |
| 5501 | return ToIntroducerOrErr.takeError(); |
| 5502 | |
| 5503 | Expected<NamedDecl *> ToTargetOrErr = import(From: D->getTargetDecl()); |
| 5504 | if (!ToTargetOrErr) |
| 5505 | return ToTargetOrErr.takeError(); |
| 5506 | |
| 5507 | UsingShadowDecl *ToShadow; |
| 5508 | if (auto *FromConstructorUsingShadow = |
| 5509 | dyn_cast<ConstructorUsingShadowDecl>(Val: D)) { |
| 5510 | Error Err = Error::success(); |
| 5511 | ConstructorUsingShadowDecl *Nominated = importChecked( |
| 5512 | Err, From: FromConstructorUsingShadow->getNominatedBaseClassShadowDecl()); |
| 5513 | if (Err) |
| 5514 | return std::move(Err); |
| 5515 | // The 'Target' parameter of ConstructorUsingShadowDecl constructor |
| 5516 | // is really the "NominatedBaseClassShadowDecl" value if it exists |
| 5517 | // (see code of ConstructorUsingShadowDecl::ConstructorUsingShadowDecl). |
| 5518 | // We should pass the NominatedBaseClassShadowDecl to it (if non-null) to |
| 5519 | // get the correct values. |
| 5520 | if (GetImportedOrCreateDecl<ConstructorUsingShadowDecl>( |
| 5521 | ToD&: ToShadow, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, |
| 5522 | args: cast<UsingDecl>(Val: *ToIntroducerOrErr), |
| 5523 | args: Nominated ? Nominated : *ToTargetOrErr, |
| 5524 | args: FromConstructorUsingShadow->constructsVirtualBase())) |
| 5525 | return ToShadow; |
| 5526 | } else { |
| 5527 | if (GetImportedOrCreateDecl(ToD&: ToShadow, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, |
| 5528 | args&: Name, args&: *ToIntroducerOrErr, args&: *ToTargetOrErr)) |
| 5529 | return ToShadow; |
| 5530 | } |
| 5531 | |
| 5532 | ToShadow->setLexicalDeclContext(LexicalDC); |
| 5533 | ToShadow->setAccess(D->getAccess()); |
| 5534 | |
| 5535 | if (UsingShadowDecl *FromPattern = |
| 5536 | Importer.getFromContext().getInstantiatedFromUsingShadowDecl(Inst: D)) { |
| 5537 | if (Expected<UsingShadowDecl *> ToPatternOrErr = import(From: FromPattern)) |
| 5538 | Importer.getToContext().setInstantiatedFromUsingShadowDecl( |
| 5539 | Inst: ToShadow, Pattern: *ToPatternOrErr); |
| 5540 | else |
| 5541 | // FIXME: We return error here but the definition is already created |
| 5542 | // and available with lookups. How to fix this?.. |
| 5543 | return ToPatternOrErr.takeError(); |
| 5544 | } |
| 5545 | |
| 5546 | LexicalDC->addDeclInternal(D: ToShadow); |
| 5547 | |
| 5548 | return ToShadow; |
| 5549 | } |
| 5550 | |
| 5551 | ExpectedDecl ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
| 5552 | DeclContext *DC, *LexicalDC; |
| 5553 | DeclarationName Name; |
| 5554 | SourceLocation Loc; |
| 5555 | NamedDecl *ToD = nullptr; |
| 5556 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 5557 | return std::move(Err); |
| 5558 | if (ToD) |
| 5559 | return ToD; |
| 5560 | |
| 5561 | auto ToComAncestorOrErr = Importer.ImportContext(FromDC: D->getCommonAncestor()); |
| 5562 | if (!ToComAncestorOrErr) |
| 5563 | return ToComAncestorOrErr.takeError(); |
| 5564 | |
| 5565 | Error Err = Error::success(); |
| 5566 | auto ToNominatedNamespace = importChecked(Err, From: D->getNominatedNamespace()); |
| 5567 | auto ToUsingLoc = importChecked(Err, From: D->getUsingLoc()); |
| 5568 | auto ToNamespaceKeyLocation = |
| 5569 | importChecked(Err, From: D->getNamespaceKeyLocation()); |
| 5570 | auto ToQualifierLoc = importChecked(Err, From: D->getQualifierLoc()); |
| 5571 | auto ToIdentLocation = importChecked(Err, From: D->getIdentLocation()); |
| 5572 | if (Err) |
| 5573 | return std::move(Err); |
| 5574 | |
| 5575 | UsingDirectiveDecl *ToUsingDir; |
| 5576 | if (GetImportedOrCreateDecl(ToD&: ToUsingDir, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 5577 | args&: ToUsingLoc, |
| 5578 | args&: ToNamespaceKeyLocation, |
| 5579 | args&: ToQualifierLoc, |
| 5580 | args&: ToIdentLocation, |
| 5581 | args&: ToNominatedNamespace, args&: *ToComAncestorOrErr)) |
| 5582 | return ToUsingDir; |
| 5583 | |
| 5584 | ToUsingDir->setLexicalDeclContext(LexicalDC); |
| 5585 | LexicalDC->addDeclInternal(D: ToUsingDir); |
| 5586 | |
| 5587 | return ToUsingDir; |
| 5588 | } |
| 5589 | |
| 5590 | ExpectedDecl ASTNodeImporter::VisitUsingPackDecl(UsingPackDecl *D) { |
| 5591 | DeclContext *DC, *LexicalDC; |
| 5592 | DeclarationName Name; |
| 5593 | SourceLocation Loc; |
| 5594 | NamedDecl *ToD = nullptr; |
| 5595 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 5596 | return std::move(Err); |
| 5597 | if (ToD) |
| 5598 | return ToD; |
| 5599 | |
| 5600 | auto ToInstantiatedFromUsingOrErr = |
| 5601 | Importer.Import(FromD: D->getInstantiatedFromUsingDecl()); |
| 5602 | if (!ToInstantiatedFromUsingOrErr) |
| 5603 | return ToInstantiatedFromUsingOrErr.takeError(); |
| 5604 | SmallVector<NamedDecl *, 4> Expansions(D->expansions().size()); |
| 5605 | if (Error Err = ImportArrayChecked(InContainer: D->expansions(), Obegin: Expansions.begin())) |
| 5606 | return std::move(Err); |
| 5607 | |
| 5608 | UsingPackDecl *ToUsingPack; |
| 5609 | if (GetImportedOrCreateDecl(ToD&: ToUsingPack, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 5610 | args: cast<NamedDecl>(Val: *ToInstantiatedFromUsingOrErr), |
| 5611 | args&: Expansions)) |
| 5612 | return ToUsingPack; |
| 5613 | |
| 5614 | addDeclToContexts(FromD: D, ToD: ToUsingPack); |
| 5615 | |
| 5616 | return ToUsingPack; |
| 5617 | } |
| 5618 | |
| 5619 | ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingValueDecl( |
| 5620 | UnresolvedUsingValueDecl *D) { |
| 5621 | DeclContext *DC, *LexicalDC; |
| 5622 | DeclarationName Name; |
| 5623 | SourceLocation Loc; |
| 5624 | NamedDecl *ToD = nullptr; |
| 5625 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 5626 | return std::move(Err); |
| 5627 | if (ToD) |
| 5628 | return ToD; |
| 5629 | |
| 5630 | Error Err = Error::success(); |
| 5631 | auto ToLoc = importChecked(Err, From: D->getNameInfo().getLoc()); |
| 5632 | auto ToUsingLoc = importChecked(Err, From: D->getUsingLoc()); |
| 5633 | auto ToQualifierLoc = importChecked(Err, From: D->getQualifierLoc()); |
| 5634 | auto ToEllipsisLoc = importChecked(Err, From: D->getEllipsisLoc()); |
| 5635 | if (Err) |
| 5636 | return std::move(Err); |
| 5637 | |
| 5638 | DeclarationNameInfo NameInfo(Name, ToLoc); |
| 5639 | if (Error Err = ImportDeclarationNameLoc(From: D->getNameInfo(), To&: NameInfo)) |
| 5640 | return std::move(Err); |
| 5641 | |
| 5642 | UnresolvedUsingValueDecl *ToUsingValue; |
| 5643 | if (GetImportedOrCreateDecl(ToD&: ToUsingValue, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 5644 | args&: ToUsingLoc, args&: ToQualifierLoc, args&: NameInfo, |
| 5645 | args&: ToEllipsisLoc)) |
| 5646 | return ToUsingValue; |
| 5647 | |
| 5648 | ToUsingValue->setAccess(D->getAccess()); |
| 5649 | ToUsingValue->setLexicalDeclContext(LexicalDC); |
| 5650 | LexicalDC->addDeclInternal(D: ToUsingValue); |
| 5651 | |
| 5652 | return ToUsingValue; |
| 5653 | } |
| 5654 | |
| 5655 | ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingTypenameDecl( |
| 5656 | UnresolvedUsingTypenameDecl *D) { |
| 5657 | DeclContext *DC, *LexicalDC; |
| 5658 | DeclarationName Name; |
| 5659 | SourceLocation Loc; |
| 5660 | NamedDecl *ToD = nullptr; |
| 5661 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 5662 | return std::move(Err); |
| 5663 | if (ToD) |
| 5664 | return ToD; |
| 5665 | |
| 5666 | Error Err = Error::success(); |
| 5667 | auto ToUsingLoc = importChecked(Err, From: D->getUsingLoc()); |
| 5668 | auto ToTypenameLoc = importChecked(Err, From: D->getTypenameLoc()); |
| 5669 | auto ToQualifierLoc = importChecked(Err, From: D->getQualifierLoc()); |
| 5670 | auto ToEllipsisLoc = importChecked(Err, From: D->getEllipsisLoc()); |
| 5671 | if (Err) |
| 5672 | return std::move(Err); |
| 5673 | |
| 5674 | UnresolvedUsingTypenameDecl *ToUsing; |
| 5675 | if (GetImportedOrCreateDecl(ToD&: ToUsing, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 5676 | args&: ToUsingLoc, args&: ToTypenameLoc, |
| 5677 | args&: ToQualifierLoc, args&: Loc, args&: Name, args&: ToEllipsisLoc)) |
| 5678 | return ToUsing; |
| 5679 | |
| 5680 | ToUsing->setAccess(D->getAccess()); |
| 5681 | ToUsing->setLexicalDeclContext(LexicalDC); |
| 5682 | LexicalDC->addDeclInternal(D: ToUsing); |
| 5683 | |
| 5684 | return ToUsing; |
| 5685 | } |
| 5686 | |
| 5687 | ExpectedDecl ASTNodeImporter::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) { |
| 5688 | Decl* ToD = nullptr; |
| 5689 | switch (D->getBuiltinTemplateKind()) { |
| 5690 | #define BuiltinTemplate(BTName) \ |
| 5691 | case BuiltinTemplateKind::BTK##BTName: \ |
| 5692 | ToD = Importer.getToContext().get##BTName##Decl(); \ |
| 5693 | break; |
| 5694 | #include "clang/Basic/BuiltinTemplates.inc" |
| 5695 | } |
| 5696 | assert(ToD && "BuiltinTemplateDecl of unsupported kind!" ); |
| 5697 | Importer.MapImported(From: D, To: ToD); |
| 5698 | return ToD; |
| 5699 | } |
| 5700 | |
| 5701 | Error ASTNodeImporter::ImportDefinition( |
| 5702 | ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, ImportDefinitionKind Kind) { |
| 5703 | if (To->getDefinition()) { |
| 5704 | // Check consistency of superclass. |
| 5705 | ObjCInterfaceDecl *FromSuper = From->getSuperClass(); |
| 5706 | if (FromSuper) { |
| 5707 | if (auto FromSuperOrErr = import(From: FromSuper)) |
| 5708 | FromSuper = *FromSuperOrErr; |
| 5709 | else |
| 5710 | return FromSuperOrErr.takeError(); |
| 5711 | } |
| 5712 | |
| 5713 | ObjCInterfaceDecl *ToSuper = To->getSuperClass(); |
| 5714 | if ((bool)FromSuper != (bool)ToSuper || |
| 5715 | (FromSuper && !declaresSameEntity(D1: FromSuper, D2: ToSuper))) { |
| 5716 | Importer.ToDiag(Loc: To->getLocation(), |
| 5717 | DiagID: diag::warn_odr_objc_superclass_inconsistent) |
| 5718 | << To->getDeclName(); |
| 5719 | if (ToSuper) |
| 5720 | Importer.ToDiag(Loc: To->getSuperClassLoc(), DiagID: diag::note_odr_objc_superclass) |
| 5721 | << To->getSuperClass()->getDeclName(); |
| 5722 | else |
| 5723 | Importer.ToDiag(Loc: To->getLocation(), |
| 5724 | DiagID: diag::note_odr_objc_missing_superclass); |
| 5725 | if (From->getSuperClass()) |
| 5726 | Importer.FromDiag(Loc: From->getSuperClassLoc(), |
| 5727 | DiagID: diag::note_odr_objc_superclass) |
| 5728 | << From->getSuperClass()->getDeclName(); |
| 5729 | else |
| 5730 | Importer.FromDiag(Loc: From->getLocation(), |
| 5731 | DiagID: diag::note_odr_objc_missing_superclass); |
| 5732 | } |
| 5733 | |
| 5734 | if (shouldForceImportDeclContext(IDK: Kind)) |
| 5735 | if (Error Err = ImportDeclContext(FromDC: From)) |
| 5736 | return Err; |
| 5737 | return Error::success(); |
| 5738 | } |
| 5739 | |
| 5740 | // Start the definition. |
| 5741 | To->startDefinition(); |
| 5742 | |
| 5743 | // If this class has a superclass, import it. |
| 5744 | if (From->getSuperClass()) { |
| 5745 | if (auto SuperTInfoOrErr = import(From: From->getSuperClassTInfo())) |
| 5746 | To->setSuperClass(*SuperTInfoOrErr); |
| 5747 | else |
| 5748 | return SuperTInfoOrErr.takeError(); |
| 5749 | } |
| 5750 | |
| 5751 | // Import protocols |
| 5752 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 5753 | SmallVector<SourceLocation, 4> ProtocolLocs; |
| 5754 | ObjCInterfaceDecl::protocol_loc_iterator FromProtoLoc = |
| 5755 | From->protocol_loc_begin(); |
| 5756 | |
| 5757 | for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(), |
| 5758 | FromProtoEnd = From->protocol_end(); |
| 5759 | FromProto != FromProtoEnd; |
| 5760 | ++FromProto, ++FromProtoLoc) { |
| 5761 | if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(From: *FromProto)) |
| 5762 | Protocols.push_back(Elt: *ToProtoOrErr); |
| 5763 | else |
| 5764 | return ToProtoOrErr.takeError(); |
| 5765 | |
| 5766 | if (ExpectedSLoc ToProtoLocOrErr = import(From: *FromProtoLoc)) |
| 5767 | ProtocolLocs.push_back(Elt: *ToProtoLocOrErr); |
| 5768 | else |
| 5769 | return ToProtoLocOrErr.takeError(); |
| 5770 | |
| 5771 | } |
| 5772 | |
| 5773 | // FIXME: If we're merging, make sure that the protocol list is the same. |
| 5774 | To->setProtocolList(List: Protocols.data(), Num: Protocols.size(), |
| 5775 | Locs: ProtocolLocs.data(), C&: Importer.getToContext()); |
| 5776 | |
| 5777 | // Import categories. When the categories themselves are imported, they'll |
| 5778 | // hook themselves into this interface. |
| 5779 | for (auto *Cat : From->known_categories()) { |
| 5780 | auto ToCatOrErr = import(From: Cat); |
| 5781 | if (!ToCatOrErr) |
| 5782 | return ToCatOrErr.takeError(); |
| 5783 | } |
| 5784 | |
| 5785 | // If we have an @implementation, import it as well. |
| 5786 | if (From->getImplementation()) { |
| 5787 | if (Expected<ObjCImplementationDecl *> ToImplOrErr = |
| 5788 | import(From: From->getImplementation())) |
| 5789 | To->setImplementation(*ToImplOrErr); |
| 5790 | else |
| 5791 | return ToImplOrErr.takeError(); |
| 5792 | } |
| 5793 | |
| 5794 | // Import all of the members of this class. |
| 5795 | if (Error Err = ImportDeclContext(FromDC: From, /*ForceImport=*/true)) |
| 5796 | return Err; |
| 5797 | |
| 5798 | return Error::success(); |
| 5799 | } |
| 5800 | |
| 5801 | Expected<ObjCTypeParamList *> |
| 5802 | ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) { |
| 5803 | if (!list) |
| 5804 | return nullptr; |
| 5805 | |
| 5806 | SmallVector<ObjCTypeParamDecl *, 4> toTypeParams; |
| 5807 | for (auto *fromTypeParam : *list) { |
| 5808 | if (auto toTypeParamOrErr = import(From: fromTypeParam)) |
| 5809 | toTypeParams.push_back(Elt: *toTypeParamOrErr); |
| 5810 | else |
| 5811 | return toTypeParamOrErr.takeError(); |
| 5812 | } |
| 5813 | |
| 5814 | auto LAngleLocOrErr = import(From: list->getLAngleLoc()); |
| 5815 | if (!LAngleLocOrErr) |
| 5816 | return LAngleLocOrErr.takeError(); |
| 5817 | |
| 5818 | auto RAngleLocOrErr = import(From: list->getRAngleLoc()); |
| 5819 | if (!RAngleLocOrErr) |
| 5820 | return RAngleLocOrErr.takeError(); |
| 5821 | |
| 5822 | return ObjCTypeParamList::create(ctx&: Importer.getToContext(), |
| 5823 | lAngleLoc: *LAngleLocOrErr, |
| 5824 | typeParams: toTypeParams, |
| 5825 | rAngleLoc: *RAngleLocOrErr); |
| 5826 | } |
| 5827 | |
| 5828 | ExpectedDecl ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
| 5829 | // If this class has a definition in the translation unit we're coming from, |
| 5830 | // but this particular declaration is not that definition, import the |
| 5831 | // definition and map to that. |
| 5832 | ObjCInterfaceDecl *Definition = D->getDefinition(); |
| 5833 | if (Definition && Definition != D) { |
| 5834 | if (ExpectedDecl ImportedDefOrErr = import(From: Definition)) |
| 5835 | return Importer.MapImported(From: D, To: *ImportedDefOrErr); |
| 5836 | else |
| 5837 | return ImportedDefOrErr.takeError(); |
| 5838 | } |
| 5839 | |
| 5840 | // Import the major distinguishing characteristics of an @interface. |
| 5841 | DeclContext *DC, *LexicalDC; |
| 5842 | DeclarationName Name; |
| 5843 | SourceLocation Loc; |
| 5844 | NamedDecl *ToD; |
| 5845 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 5846 | return std::move(Err); |
| 5847 | if (ToD) |
| 5848 | return ToD; |
| 5849 | |
| 5850 | // Look for an existing interface with the same name. |
| 5851 | ObjCInterfaceDecl *MergeWithIface = nullptr; |
| 5852 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 5853 | for (auto *FoundDecl : FoundDecls) { |
| 5854 | if (!FoundDecl->isInIdentifierNamespace(NS: Decl::IDNS_Ordinary)) |
| 5855 | continue; |
| 5856 | |
| 5857 | if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(Val: FoundDecl))) |
| 5858 | break; |
| 5859 | } |
| 5860 | |
| 5861 | // Create an interface declaration, if one does not already exist. |
| 5862 | ObjCInterfaceDecl *ToIface = MergeWithIface; |
| 5863 | if (!ToIface) { |
| 5864 | ExpectedSLoc AtBeginLocOrErr = import(From: D->getAtStartLoc()); |
| 5865 | if (!AtBeginLocOrErr) |
| 5866 | return AtBeginLocOrErr.takeError(); |
| 5867 | |
| 5868 | if (GetImportedOrCreateDecl( |
| 5869 | ToD&: ToIface, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 5870 | args&: *AtBeginLocOrErr, args: Name.getAsIdentifierInfo(), |
| 5871 | /*TypeParamList=*/args: nullptr, |
| 5872 | /*PrevDecl=*/args: nullptr, args&: Loc, args: D->isImplicitInterfaceDecl())) |
| 5873 | return ToIface; |
| 5874 | ToIface->setLexicalDeclContext(LexicalDC); |
| 5875 | LexicalDC->addDeclInternal(D: ToIface); |
| 5876 | } |
| 5877 | Importer.MapImported(From: D, To: ToIface); |
| 5878 | // Import the type parameter list after MapImported, to avoid |
| 5879 | // loops when bringing in their DeclContext. |
| 5880 | if (auto ToPListOrErr = |
| 5881 | ImportObjCTypeParamList(list: D->getTypeParamListAsWritten())) |
| 5882 | ToIface->setTypeParamList(*ToPListOrErr); |
| 5883 | else |
| 5884 | return ToPListOrErr.takeError(); |
| 5885 | |
| 5886 | if (D->isThisDeclarationADefinition()) |
| 5887 | if (Error Err = ImportDefinition(From: D, To: ToIface)) |
| 5888 | return std::move(Err); |
| 5889 | |
| 5890 | return ToIface; |
| 5891 | } |
| 5892 | |
| 5893 | ExpectedDecl |
| 5894 | ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 5895 | ObjCCategoryDecl *Category; |
| 5896 | if (Error Err = importInto(To&: Category, From: D->getCategoryDecl())) |
| 5897 | return std::move(Err); |
| 5898 | |
| 5899 | ObjCCategoryImplDecl *ToImpl = Category->getImplementation(); |
| 5900 | if (!ToImpl) { |
| 5901 | DeclContext *DC, *LexicalDC; |
| 5902 | if (Error Err = ImportDeclContext(FromD: D, ToDC&: DC, ToLexicalDC&: LexicalDC)) |
| 5903 | return std::move(Err); |
| 5904 | |
| 5905 | Error Err = Error::success(); |
| 5906 | auto ToLocation = importChecked(Err, From: D->getLocation()); |
| 5907 | auto ToAtStartLoc = importChecked(Err, From: D->getAtStartLoc()); |
| 5908 | auto ToCategoryNameLoc = importChecked(Err, From: D->getCategoryNameLoc()); |
| 5909 | if (Err) |
| 5910 | return std::move(Err); |
| 5911 | |
| 5912 | if (GetImportedOrCreateDecl( |
| 5913 | ToD&: ToImpl, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 5914 | args: Importer.Import(FromId: D->getIdentifier()), args: Category->getClassInterface(), |
| 5915 | args&: ToLocation, args&: ToAtStartLoc, args&: ToCategoryNameLoc)) |
| 5916 | return ToImpl; |
| 5917 | |
| 5918 | ToImpl->setLexicalDeclContext(LexicalDC); |
| 5919 | LexicalDC->addDeclInternal(D: ToImpl); |
| 5920 | Category->setImplementation(ToImpl); |
| 5921 | } |
| 5922 | |
| 5923 | Importer.MapImported(From: D, To: ToImpl); |
| 5924 | if (Error Err = ImportDeclContext(FromDC: D)) |
| 5925 | return std::move(Err); |
| 5926 | |
| 5927 | return ToImpl; |
| 5928 | } |
| 5929 | |
| 5930 | ExpectedDecl |
| 5931 | ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 5932 | // Find the corresponding interface. |
| 5933 | ObjCInterfaceDecl *Iface; |
| 5934 | if (Error Err = importInto(To&: Iface, From: D->getClassInterface())) |
| 5935 | return std::move(Err); |
| 5936 | |
| 5937 | // Import the superclass, if any. |
| 5938 | ObjCInterfaceDecl *Super; |
| 5939 | if (Error Err = importInto(To&: Super, From: D->getSuperClass())) |
| 5940 | return std::move(Err); |
| 5941 | |
| 5942 | ObjCImplementationDecl *Impl = Iface->getImplementation(); |
| 5943 | if (!Impl) { |
| 5944 | // We haven't imported an implementation yet. Create a new @implementation |
| 5945 | // now. |
| 5946 | DeclContext *DC, *LexicalDC; |
| 5947 | if (Error Err = ImportDeclContext(FromD: D, ToDC&: DC, ToLexicalDC&: LexicalDC)) |
| 5948 | return std::move(Err); |
| 5949 | |
| 5950 | Error Err = Error::success(); |
| 5951 | auto ToLocation = importChecked(Err, From: D->getLocation()); |
| 5952 | auto ToAtStartLoc = importChecked(Err, From: D->getAtStartLoc()); |
| 5953 | auto ToSuperClassLoc = importChecked(Err, From: D->getSuperClassLoc()); |
| 5954 | auto ToIvarLBraceLoc = importChecked(Err, From: D->getIvarLBraceLoc()); |
| 5955 | auto ToIvarRBraceLoc = importChecked(Err, From: D->getIvarRBraceLoc()); |
| 5956 | if (Err) |
| 5957 | return std::move(Err); |
| 5958 | |
| 5959 | if (GetImportedOrCreateDecl(ToD&: Impl, FromD: D, args&: Importer.getToContext(), |
| 5960 | args&: DC, args&: Iface, args&: Super, |
| 5961 | args&: ToLocation, |
| 5962 | args&: ToAtStartLoc, |
| 5963 | args&: ToSuperClassLoc, |
| 5964 | args&: ToIvarLBraceLoc, |
| 5965 | args&: ToIvarRBraceLoc)) |
| 5966 | return Impl; |
| 5967 | |
| 5968 | Impl->setLexicalDeclContext(LexicalDC); |
| 5969 | |
| 5970 | // Associate the implementation with the class it implements. |
| 5971 | Iface->setImplementation(Impl); |
| 5972 | Importer.MapImported(From: D, To: Iface->getImplementation()); |
| 5973 | } else { |
| 5974 | Importer.MapImported(From: D, To: Iface->getImplementation()); |
| 5975 | |
| 5976 | // Verify that the existing @implementation has the same superclass. |
| 5977 | if ((Super && !Impl->getSuperClass()) || |
| 5978 | (!Super && Impl->getSuperClass()) || |
| 5979 | (Super && Impl->getSuperClass() && |
| 5980 | !declaresSameEntity(D1: Super->getCanonicalDecl(), |
| 5981 | D2: Impl->getSuperClass()))) { |
| 5982 | Importer.ToDiag(Loc: Impl->getLocation(), |
| 5983 | DiagID: diag::warn_odr_objc_superclass_inconsistent) |
| 5984 | << Iface->getDeclName(); |
| 5985 | // FIXME: It would be nice to have the location of the superclass |
| 5986 | // below. |
| 5987 | if (Impl->getSuperClass()) |
| 5988 | Importer.ToDiag(Loc: Impl->getLocation(), |
| 5989 | DiagID: diag::note_odr_objc_superclass) |
| 5990 | << Impl->getSuperClass()->getDeclName(); |
| 5991 | else |
| 5992 | Importer.ToDiag(Loc: Impl->getLocation(), |
| 5993 | DiagID: diag::note_odr_objc_missing_superclass); |
| 5994 | if (D->getSuperClass()) |
| 5995 | Importer.FromDiag(Loc: D->getLocation(), |
| 5996 | DiagID: diag::note_odr_objc_superclass) |
| 5997 | << D->getSuperClass()->getDeclName(); |
| 5998 | else |
| 5999 | Importer.FromDiag(Loc: D->getLocation(), |
| 6000 | DiagID: diag::note_odr_objc_missing_superclass); |
| 6001 | |
| 6002 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 6003 | } |
| 6004 | } |
| 6005 | |
| 6006 | // Import all of the members of this @implementation. |
| 6007 | if (Error Err = ImportDeclContext(FromDC: D)) |
| 6008 | return std::move(Err); |
| 6009 | |
| 6010 | return Impl; |
| 6011 | } |
| 6012 | |
| 6013 | ExpectedDecl ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { |
| 6014 | // Import the major distinguishing characteristics of an @property. |
| 6015 | DeclContext *DC, *LexicalDC; |
| 6016 | DeclarationName Name; |
| 6017 | SourceLocation Loc; |
| 6018 | NamedDecl *ToD; |
| 6019 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 6020 | return std::move(Err); |
| 6021 | if (ToD) |
| 6022 | return ToD; |
| 6023 | |
| 6024 | // Check whether we have already imported this property. |
| 6025 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 6026 | for (auto *FoundDecl : FoundDecls) { |
| 6027 | if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(Val: FoundDecl)) { |
| 6028 | // Instance and class properties can share the same name but are different |
| 6029 | // declarations. |
| 6030 | if (FoundProp->isInstanceProperty() != D->isInstanceProperty()) |
| 6031 | continue; |
| 6032 | |
| 6033 | // Check property types. |
| 6034 | if (!Importer.IsStructurallyEquivalent(From: D->getType(), |
| 6035 | To: FoundProp->getType())) { |
| 6036 | Importer.ToDiag(Loc, DiagID: diag::warn_odr_objc_property_type_inconsistent) |
| 6037 | << Name << D->getType() << FoundProp->getType(); |
| 6038 | Importer.ToDiag(Loc: FoundProp->getLocation(), DiagID: diag::note_odr_value_here) |
| 6039 | << FoundProp->getType(); |
| 6040 | |
| 6041 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 6042 | } |
| 6043 | |
| 6044 | // FIXME: Check property attributes, getters, setters, etc.? |
| 6045 | |
| 6046 | // Consider these properties to be equivalent. |
| 6047 | Importer.MapImported(From: D, To: FoundProp); |
| 6048 | return FoundProp; |
| 6049 | } |
| 6050 | } |
| 6051 | |
| 6052 | Error Err = Error::success(); |
| 6053 | auto ToType = importChecked(Err, From: D->getType()); |
| 6054 | auto ToTypeSourceInfo = importChecked(Err, From: D->getTypeSourceInfo()); |
| 6055 | auto ToAtLoc = importChecked(Err, From: D->getAtLoc()); |
| 6056 | auto ToLParenLoc = importChecked(Err, From: D->getLParenLoc()); |
| 6057 | if (Err) |
| 6058 | return std::move(Err); |
| 6059 | |
| 6060 | // Create the new property. |
| 6061 | ObjCPropertyDecl *ToProperty; |
| 6062 | if (GetImportedOrCreateDecl( |
| 6063 | ToD&: ToProperty, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, |
| 6064 | args: Name.getAsIdentifierInfo(), args&: ToAtLoc, |
| 6065 | args&: ToLParenLoc, args&: ToType, |
| 6066 | args&: ToTypeSourceInfo, args: D->getPropertyImplementation())) |
| 6067 | return ToProperty; |
| 6068 | |
| 6069 | auto ToGetterName = importChecked(Err, From: D->getGetterName()); |
| 6070 | auto ToSetterName = importChecked(Err, From: D->getSetterName()); |
| 6071 | auto ToGetterNameLoc = importChecked(Err, From: D->getGetterNameLoc()); |
| 6072 | auto ToSetterNameLoc = importChecked(Err, From: D->getSetterNameLoc()); |
| 6073 | auto ToGetterMethodDecl = importChecked(Err, From: D->getGetterMethodDecl()); |
| 6074 | auto ToSetterMethodDecl = importChecked(Err, From: D->getSetterMethodDecl()); |
| 6075 | auto ToPropertyIvarDecl = importChecked(Err, From: D->getPropertyIvarDecl()); |
| 6076 | if (Err) |
| 6077 | return std::move(Err); |
| 6078 | |
| 6079 | ToProperty->setLexicalDeclContext(LexicalDC); |
| 6080 | LexicalDC->addDeclInternal(D: ToProperty); |
| 6081 | |
| 6082 | ToProperty->setPropertyAttributes(D->getPropertyAttributes()); |
| 6083 | ToProperty->setPropertyAttributesAsWritten( |
| 6084 | D->getPropertyAttributesAsWritten()); |
| 6085 | ToProperty->setGetterName(Sel: ToGetterName, Loc: ToGetterNameLoc); |
| 6086 | ToProperty->setSetterName(Sel: ToSetterName, Loc: ToSetterNameLoc); |
| 6087 | ToProperty->setGetterMethodDecl(ToGetterMethodDecl); |
| 6088 | ToProperty->setSetterMethodDecl(ToSetterMethodDecl); |
| 6089 | ToProperty->setPropertyIvarDecl(ToPropertyIvarDecl); |
| 6090 | return ToProperty; |
| 6091 | } |
| 6092 | |
| 6093 | ExpectedDecl |
| 6094 | ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { |
| 6095 | ObjCPropertyDecl *Property; |
| 6096 | if (Error Err = importInto(To&: Property, From: D->getPropertyDecl())) |
| 6097 | return std::move(Err); |
| 6098 | |
| 6099 | DeclContext *DC, *LexicalDC; |
| 6100 | if (Error Err = ImportDeclContext(FromD: D, ToDC&: DC, ToLexicalDC&: LexicalDC)) |
| 6101 | return std::move(Err); |
| 6102 | |
| 6103 | auto *InImpl = cast<ObjCImplDecl>(Val: LexicalDC); |
| 6104 | |
| 6105 | // Import the ivar (for an @synthesize). |
| 6106 | ObjCIvarDecl *Ivar = nullptr; |
| 6107 | if (Error Err = importInto(To&: Ivar, From: D->getPropertyIvarDecl())) |
| 6108 | return std::move(Err); |
| 6109 | |
| 6110 | ObjCPropertyImplDecl *ToImpl |
| 6111 | = InImpl->FindPropertyImplDecl(propertyId: Property->getIdentifier(), |
| 6112 | queryKind: Property->getQueryKind()); |
| 6113 | if (!ToImpl) { |
| 6114 | |
| 6115 | Error Err = Error::success(); |
| 6116 | auto ToBeginLoc = importChecked(Err, From: D->getBeginLoc()); |
| 6117 | auto ToLocation = importChecked(Err, From: D->getLocation()); |
| 6118 | auto ToPropertyIvarDeclLoc = |
| 6119 | importChecked(Err, From: D->getPropertyIvarDeclLoc()); |
| 6120 | if (Err) |
| 6121 | return std::move(Err); |
| 6122 | |
| 6123 | if (GetImportedOrCreateDecl(ToD&: ToImpl, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 6124 | args&: ToBeginLoc, |
| 6125 | args&: ToLocation, args&: Property, |
| 6126 | args: D->getPropertyImplementation(), args&: Ivar, |
| 6127 | args&: ToPropertyIvarDeclLoc)) |
| 6128 | return ToImpl; |
| 6129 | |
| 6130 | ToImpl->setLexicalDeclContext(LexicalDC); |
| 6131 | LexicalDC->addDeclInternal(D: ToImpl); |
| 6132 | } else { |
| 6133 | // Check that we have the same kind of property implementation (@synthesize |
| 6134 | // vs. @dynamic). |
| 6135 | if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) { |
| 6136 | Importer.ToDiag(Loc: ToImpl->getLocation(), |
| 6137 | DiagID: diag::warn_odr_objc_property_impl_kind_inconsistent) |
| 6138 | << Property->getDeclName() |
| 6139 | << (ToImpl->getPropertyImplementation() |
| 6140 | == ObjCPropertyImplDecl::Dynamic); |
| 6141 | Importer.FromDiag(Loc: D->getLocation(), |
| 6142 | DiagID: diag::note_odr_objc_property_impl_kind) |
| 6143 | << D->getPropertyDecl()->getDeclName() |
| 6144 | << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); |
| 6145 | |
| 6146 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 6147 | } |
| 6148 | |
| 6149 | // For @synthesize, check that we have the same |
| 6150 | if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize && |
| 6151 | Ivar != ToImpl->getPropertyIvarDecl()) { |
| 6152 | Importer.ToDiag(Loc: ToImpl->getPropertyIvarDeclLoc(), |
| 6153 | DiagID: diag::warn_odr_objc_synthesize_ivar_inconsistent) |
| 6154 | << Property->getDeclName() |
| 6155 | << ToImpl->getPropertyIvarDecl()->getDeclName() |
| 6156 | << Ivar->getDeclName(); |
| 6157 | Importer.FromDiag(Loc: D->getPropertyIvarDeclLoc(), |
| 6158 | DiagID: diag::note_odr_objc_synthesize_ivar_here) |
| 6159 | << D->getPropertyIvarDecl()->getDeclName(); |
| 6160 | |
| 6161 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 6162 | } |
| 6163 | |
| 6164 | // Merge the existing implementation with the new implementation. |
| 6165 | Importer.MapImported(From: D, To: ToImpl); |
| 6166 | } |
| 6167 | |
| 6168 | return ToImpl; |
| 6169 | } |
| 6170 | |
| 6171 | ExpectedDecl |
| 6172 | ASTNodeImporter::VisitTemplateParamObjectDecl(TemplateParamObjectDecl *D) { |
| 6173 | Error Err = Error::success(); |
| 6174 | auto ToType = importChecked(Err, From: D->getType()); |
| 6175 | auto ToValue = importChecked(Err, From: D->getValue()); |
| 6176 | if (Err) |
| 6177 | return std::move(Err); |
| 6178 | |
| 6179 | TemplateParamObjectDecl *ToD; |
| 6180 | auto Create = [this](QualType T, const APValue &V) { |
| 6181 | return Importer.ToContext.getTemplateParamObjectDecl(T, V); |
| 6182 | }; |
| 6183 | (void)GetImportedOrCreateSpecialDecl(ToD, CreateFun: Create, FromD: D, args&: ToType, args&: ToValue); |
| 6184 | return ToD; |
| 6185 | } |
| 6186 | |
| 6187 | ExpectedDecl |
| 6188 | ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
| 6189 | // For template arguments, we adopt the translation unit as our declaration |
| 6190 | // context. This context will be fixed when (during) the actual template |
| 6191 | // declaration is created. |
| 6192 | |
| 6193 | ExpectedSLoc BeginLocOrErr = import(From: D->getBeginLoc()); |
| 6194 | if (!BeginLocOrErr) |
| 6195 | return BeginLocOrErr.takeError(); |
| 6196 | |
| 6197 | ExpectedSLoc LocationOrErr = import(From: D->getLocation()); |
| 6198 | if (!LocationOrErr) |
| 6199 | return LocationOrErr.takeError(); |
| 6200 | |
| 6201 | TemplateTypeParmDecl *ToD = nullptr; |
| 6202 | if (GetImportedOrCreateDecl( |
| 6203 | ToD, FromD: D, args&: Importer.getToContext(), |
| 6204 | args: Importer.getToContext().getTranslationUnitDecl(), |
| 6205 | args&: *BeginLocOrErr, args&: *LocationOrErr, |
| 6206 | args: D->getDepth(), args: D->getIndex(), args: Importer.Import(FromId: D->getIdentifier()), |
| 6207 | args: D->wasDeclaredWithTypename(), args: D->isParameterPack(), |
| 6208 | args: D->hasTypeConstraint())) |
| 6209 | return ToD; |
| 6210 | |
| 6211 | // Import the type-constraint |
| 6212 | if (const TypeConstraint *TC = D->getTypeConstraint()) { |
| 6213 | |
| 6214 | Error Err = Error::success(); |
| 6215 | auto ToConceptRef = importChecked(Err, From: TC->getConceptReference()); |
| 6216 | auto ToIDC = importChecked(Err, From: TC->getImmediatelyDeclaredConstraint()); |
| 6217 | if (Err) |
| 6218 | return std::move(Err); |
| 6219 | |
| 6220 | ToD->setTypeConstraint(CR: ToConceptRef, ImmediatelyDeclaredConstraint: ToIDC, ArgPackSubstIndex: TC->getArgPackSubstIndex()); |
| 6221 | } |
| 6222 | |
| 6223 | if (Error Err = importTemplateParameterDefaultArgument(D, ToD)) |
| 6224 | return Err; |
| 6225 | |
| 6226 | return ToD; |
| 6227 | } |
| 6228 | |
| 6229 | ExpectedDecl |
| 6230 | ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
| 6231 | |
| 6232 | Error Err = Error::success(); |
| 6233 | auto ToDeclName = importChecked(Err, From: D->getDeclName()); |
| 6234 | auto ToLocation = importChecked(Err, From: D->getLocation()); |
| 6235 | auto ToType = importChecked(Err, From: D->getType()); |
| 6236 | auto ToTypeSourceInfo = importChecked(Err, From: D->getTypeSourceInfo()); |
| 6237 | auto ToInnerLocStart = importChecked(Err, From: D->getInnerLocStart()); |
| 6238 | if (Err) |
| 6239 | return std::move(Err); |
| 6240 | |
| 6241 | NonTypeTemplateParmDecl *ToD = nullptr; |
| 6242 | if (GetImportedOrCreateDecl(ToD, FromD: D, args&: Importer.getToContext(), |
| 6243 | args: Importer.getToContext().getTranslationUnitDecl(), |
| 6244 | args&: ToInnerLocStart, args&: ToLocation, args: D->getDepth(), |
| 6245 | args: D->getPosition(), |
| 6246 | args: ToDeclName.getAsIdentifierInfo(), args&: ToType, |
| 6247 | args: D->isParameterPack(), args&: ToTypeSourceInfo)) |
| 6248 | return ToD; |
| 6249 | |
| 6250 | Err = importTemplateParameterDefaultArgument(D, ToD); |
| 6251 | if (Err) |
| 6252 | return Err; |
| 6253 | |
| 6254 | return ToD; |
| 6255 | } |
| 6256 | |
| 6257 | ExpectedDecl |
| 6258 | ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
| 6259 | bool IsCanonical = false; |
| 6260 | if (auto *CanonD = Importer.getFromContext() |
| 6261 | .findCanonicalTemplateTemplateParmDeclInternal(TTP: D); |
| 6262 | CanonD == D) |
| 6263 | IsCanonical = true; |
| 6264 | |
| 6265 | // Import the name of this declaration. |
| 6266 | auto NameOrErr = import(From: D->getDeclName()); |
| 6267 | if (!NameOrErr) |
| 6268 | return NameOrErr.takeError(); |
| 6269 | |
| 6270 | // Import the location of this declaration. |
| 6271 | ExpectedSLoc LocationOrErr = import(From: D->getLocation()); |
| 6272 | if (!LocationOrErr) |
| 6273 | return LocationOrErr.takeError(); |
| 6274 | |
| 6275 | // Import template parameters. |
| 6276 | auto TemplateParamsOrErr = import(From: D->getTemplateParameters()); |
| 6277 | if (!TemplateParamsOrErr) |
| 6278 | return TemplateParamsOrErr.takeError(); |
| 6279 | |
| 6280 | TemplateTemplateParmDecl *ToD = nullptr; |
| 6281 | if (GetImportedOrCreateDecl( |
| 6282 | ToD, FromD: D, args&: Importer.getToContext(), |
| 6283 | args: Importer.getToContext().getTranslationUnitDecl(), args&: *LocationOrErr, |
| 6284 | args: D->getDepth(), args: D->getPosition(), args: D->isParameterPack(), |
| 6285 | args: (*NameOrErr).getAsIdentifierInfo(), args: D->templateParameterKind(), |
| 6286 | args: D->wasDeclaredWithTypename(), args&: *TemplateParamsOrErr)) |
| 6287 | return ToD; |
| 6288 | |
| 6289 | if (Error Err = importTemplateParameterDefaultArgument(D, ToD)) |
| 6290 | return Err; |
| 6291 | |
| 6292 | if (IsCanonical) |
| 6293 | return Importer.getToContext() |
| 6294 | .insertCanonicalTemplateTemplateParmDeclInternal(CanonTTP: ToD); |
| 6295 | |
| 6296 | return ToD; |
| 6297 | } |
| 6298 | |
| 6299 | // Returns the definition for a (forward) declaration of a TemplateDecl, if |
| 6300 | // it has any definition in the redecl chain. |
| 6301 | template <typename T> static auto getTemplateDefinition(T *D) -> T * { |
| 6302 | assert(D->getTemplatedDecl() && "Should be called on templates only" ); |
| 6303 | auto *ToTemplatedDef = D->getTemplatedDecl()->getDefinition(); |
| 6304 | if (!ToTemplatedDef) |
| 6305 | return nullptr; |
| 6306 | auto *TemplateWithDef = ToTemplatedDef->getDescribedTemplate(); |
| 6307 | return cast_or_null<T>(TemplateWithDef); |
| 6308 | } |
| 6309 | |
| 6310 | ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 6311 | |
| 6312 | // Import the major distinguishing characteristics of this class template. |
| 6313 | DeclContext *DC, *LexicalDC; |
| 6314 | DeclarationName Name; |
| 6315 | SourceLocation Loc; |
| 6316 | NamedDecl *ToD; |
| 6317 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 6318 | return std::move(Err); |
| 6319 | if (ToD) |
| 6320 | return ToD; |
| 6321 | |
| 6322 | // Should check if a declaration is friend in a dependent context. |
| 6323 | // Such templates are not linked together in a declaration chain. |
| 6324 | // The ASTImporter strategy is to map existing forward declarations to |
| 6325 | // imported ones only if strictly necessary, otherwise import these as new |
| 6326 | // forward declarations. In case of the "dependent friend" declarations, new |
| 6327 | // declarations are created, but not linked in a declaration chain. |
| 6328 | auto IsDependentFriend = [](ClassTemplateDecl *TD) { |
| 6329 | return TD->getFriendObjectKind() != Decl::FOK_None && |
| 6330 | TD->getLexicalDeclContext()->isDependentContext(); |
| 6331 | }; |
| 6332 | bool DependentFriend = IsDependentFriend(D); |
| 6333 | |
| 6334 | ClassTemplateDecl *FoundByLookup = nullptr; |
| 6335 | |
| 6336 | // We may already have a template of the same name; try to find and match it. |
| 6337 | if (!DC->isFunctionOrMethod()) { |
| 6338 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 6339 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 6340 | for (auto *FoundDecl : FoundDecls) { |
| 6341 | if (!FoundDecl->isInIdentifierNamespace(NS: Decl::IDNS_Ordinary | |
| 6342 | Decl::IDNS_TagFriend)) |
| 6343 | continue; |
| 6344 | |
| 6345 | auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Val: FoundDecl); |
| 6346 | if (FoundTemplate) { |
| 6347 | if (!hasSameVisibilityContextAndLinkage(Found: FoundTemplate, From: D)) |
| 6348 | continue; |
| 6349 | |
| 6350 | // FIXME: sufficient condition for 'IgnoreTemplateParmDepth'? |
| 6351 | bool IgnoreTemplateParmDepth = |
| 6352 | (FoundTemplate->getFriendObjectKind() != Decl::FOK_None) != |
| 6353 | (D->getFriendObjectKind() != Decl::FOK_None); |
| 6354 | if (IsStructuralMatch(From: D, To: FoundTemplate, /*Complain=*/true, |
| 6355 | IgnoreTemplateParmDepth)) { |
| 6356 | if (DependentFriend || IsDependentFriend(FoundTemplate)) |
| 6357 | continue; |
| 6358 | |
| 6359 | ClassTemplateDecl *TemplateWithDef = |
| 6360 | getTemplateDefinition(D: FoundTemplate); |
| 6361 | if (D->isThisDeclarationADefinition() && TemplateWithDef) |
| 6362 | return Importer.MapImported(From: D, To: TemplateWithDef); |
| 6363 | if (!FoundByLookup) |
| 6364 | FoundByLookup = FoundTemplate; |
| 6365 | // Search in all matches because there may be multiple decl chains, |
| 6366 | // see ASTTests test ImportExistingFriendClassTemplateDef. |
| 6367 | continue; |
| 6368 | } |
| 6369 | // When importing a friend, it is possible that multiple declarations |
| 6370 | // with same name can co-exist in specific cases (if a template contains |
| 6371 | // a friend template and has a specialization). For this case the |
| 6372 | // declarations should match, except that the "template depth" is |
| 6373 | // different. No linking of previous declaration is needed in this case. |
| 6374 | // FIXME: This condition may need refinement. |
| 6375 | if (D->getFriendObjectKind() != Decl::FOK_None && |
| 6376 | FoundTemplate->getFriendObjectKind() != Decl::FOK_None && |
| 6377 | D->getFriendObjectKind() != FoundTemplate->getFriendObjectKind() && |
| 6378 | IsStructuralMatch(From: D, To: FoundTemplate, /*Complain=*/false, |
| 6379 | /*IgnoreTemplateParmDepth=*/true)) |
| 6380 | continue; |
| 6381 | |
| 6382 | ConflictingDecls.push_back(Elt: FoundDecl); |
| 6383 | } |
| 6384 | } |
| 6385 | |
| 6386 | if (!ConflictingDecls.empty()) { |
| 6387 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
| 6388 | Name, DC, IDNS: Decl::IDNS_Ordinary, Decls: ConflictingDecls.data(), |
| 6389 | NumDecls: ConflictingDecls.size()); |
| 6390 | if (NameOrErr) |
| 6391 | Name = NameOrErr.get(); |
| 6392 | else |
| 6393 | return NameOrErr.takeError(); |
| 6394 | } |
| 6395 | } |
| 6396 | |
| 6397 | CXXRecordDecl *FromTemplated = D->getTemplatedDecl(); |
| 6398 | |
| 6399 | auto TemplateParamsOrErr = import(From: D->getTemplateParameters()); |
| 6400 | if (!TemplateParamsOrErr) |
| 6401 | return TemplateParamsOrErr.takeError(); |
| 6402 | |
| 6403 | // Create the declaration that is being templated. |
| 6404 | CXXRecordDecl *ToTemplated; |
| 6405 | if (Error Err = importInto(To&: ToTemplated, From: FromTemplated)) |
| 6406 | return std::move(Err); |
| 6407 | |
| 6408 | // Create the class template declaration itself. |
| 6409 | ClassTemplateDecl *D2; |
| 6410 | if (GetImportedOrCreateDecl(ToD&: D2, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, args&: Name, |
| 6411 | args&: *TemplateParamsOrErr, args&: ToTemplated)) |
| 6412 | return D2; |
| 6413 | |
| 6414 | ToTemplated->setDescribedClassTemplate(D2); |
| 6415 | |
| 6416 | D2->setAccess(D->getAccess()); |
| 6417 | D2->setLexicalDeclContext(LexicalDC); |
| 6418 | |
| 6419 | addDeclToContexts(FromD: D, ToD: D2); |
| 6420 | updateLookupTableForTemplateParameters(Params&: **TemplateParamsOrErr); |
| 6421 | |
| 6422 | if (FoundByLookup) { |
| 6423 | auto *Recent = |
| 6424 | const_cast<ClassTemplateDecl *>(FoundByLookup->getMostRecentDecl()); |
| 6425 | |
| 6426 | // It is possible that during the import of the class template definition |
| 6427 | // we start the import of a fwd friend decl of the very same class template |
| 6428 | // and we add the fwd friend decl to the lookup table. But the ToTemplated |
| 6429 | // had been created earlier and by that time the lookup could not find |
| 6430 | // anything existing, so it has no previous decl. Later, (still during the |
| 6431 | // import of the fwd friend decl) we start to import the definition again |
| 6432 | // and this time the lookup finds the previous fwd friend class template. |
| 6433 | // In this case we must set up the previous decl for the templated decl. |
| 6434 | if (!ToTemplated->getPreviousDecl()) { |
| 6435 | assert(FoundByLookup->getTemplatedDecl() && |
| 6436 | "Found decl must have its templated decl set" ); |
| 6437 | CXXRecordDecl *PrevTemplated = |
| 6438 | FoundByLookup->getTemplatedDecl()->getMostRecentDecl(); |
| 6439 | if (ToTemplated != PrevTemplated) |
| 6440 | ToTemplated->setPreviousDecl(PrevTemplated); |
| 6441 | } |
| 6442 | |
| 6443 | D2->setPreviousDecl(Recent); |
| 6444 | } |
| 6445 | |
| 6446 | return D2; |
| 6447 | } |
| 6448 | |
| 6449 | ExpectedDecl ASTNodeImporter::VisitClassTemplateSpecializationDecl( |
| 6450 | ClassTemplateSpecializationDecl *D) { |
| 6451 | ClassTemplateDecl *ClassTemplate; |
| 6452 | if (Error Err = importInto(To&: ClassTemplate, From: D->getSpecializedTemplate())) |
| 6453 | return std::move(Err); |
| 6454 | |
| 6455 | // Import the context of this declaration. |
| 6456 | DeclContext *DC, *LexicalDC; |
| 6457 | if (Error Err = ImportDeclContext(FromD: D, ToDC&: DC, ToLexicalDC&: LexicalDC)) |
| 6458 | return std::move(Err); |
| 6459 | |
| 6460 | // Import template arguments. |
| 6461 | SmallVector<TemplateArgument, 2> TemplateArgs; |
| 6462 | if (Error Err = |
| 6463 | ImportTemplateArguments(FromArgs: D->getTemplateArgs().asArray(), ToArgs&: TemplateArgs)) |
| 6464 | return std::move(Err); |
| 6465 | // Try to find an existing specialization with these template arguments and |
| 6466 | // template parameter list. |
| 6467 | void *InsertPos = nullptr; |
| 6468 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; |
| 6469 | ClassTemplatePartialSpecializationDecl *PartialSpec = |
| 6470 | dyn_cast<ClassTemplatePartialSpecializationDecl>(Val: D); |
| 6471 | |
| 6472 | // Import template parameters. |
| 6473 | TemplateParameterList *ToTPList = nullptr; |
| 6474 | |
| 6475 | if (PartialSpec) { |
| 6476 | auto ToTPListOrErr = import(From: PartialSpec->getTemplateParameters()); |
| 6477 | if (!ToTPListOrErr) |
| 6478 | return ToTPListOrErr.takeError(); |
| 6479 | ToTPList = *ToTPListOrErr; |
| 6480 | PrevDecl = ClassTemplate->findPartialSpecialization(Args: TemplateArgs, |
| 6481 | TPL: *ToTPListOrErr, |
| 6482 | InsertPos); |
| 6483 | } else |
| 6484 | PrevDecl = ClassTemplate->findSpecialization(Args: TemplateArgs, InsertPos); |
| 6485 | |
| 6486 | if (PrevDecl) { |
| 6487 | if (IsStructuralMatch(From: D, To: PrevDecl)) { |
| 6488 | CXXRecordDecl *PrevDefinition = PrevDecl->getDefinition(); |
| 6489 | if (D->isThisDeclarationADefinition() && PrevDefinition) { |
| 6490 | Importer.MapImported(From: D, To: PrevDefinition); |
| 6491 | // Import those default field initializers which have been |
| 6492 | // instantiated in the "From" context, but not in the "To" context. |
| 6493 | for (auto *FromField : D->fields()) { |
| 6494 | auto ToOrErr = import(From: FromField); |
| 6495 | if (!ToOrErr) |
| 6496 | return ToOrErr.takeError(); |
| 6497 | } |
| 6498 | |
| 6499 | // Import those methods which have been instantiated in the |
| 6500 | // "From" context, but not in the "To" context. |
| 6501 | for (CXXMethodDecl *FromM : D->methods()) { |
| 6502 | auto ToOrErr = import(From: FromM); |
| 6503 | if (!ToOrErr) |
| 6504 | return ToOrErr.takeError(); |
| 6505 | } |
| 6506 | |
| 6507 | // TODO Import instantiated default arguments. |
| 6508 | // TODO Import instantiated exception specifications. |
| 6509 | // |
| 6510 | // Generally, ASTCommon.h/DeclUpdateKind enum gives a very good hint |
| 6511 | // what else could be fused during an AST merge. |
| 6512 | return PrevDefinition; |
| 6513 | } |
| 6514 | } else { // ODR violation. |
| 6515 | // FIXME HandleNameConflict |
| 6516 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 6517 | } |
| 6518 | } |
| 6519 | |
| 6520 | // Import the location of this declaration. |
| 6521 | ExpectedSLoc BeginLocOrErr = import(From: D->getBeginLoc()); |
| 6522 | if (!BeginLocOrErr) |
| 6523 | return BeginLocOrErr.takeError(); |
| 6524 | ExpectedSLoc IdLocOrErr = import(From: D->getLocation()); |
| 6525 | if (!IdLocOrErr) |
| 6526 | return IdLocOrErr.takeError(); |
| 6527 | |
| 6528 | // Import TemplateArgumentListInfo. |
| 6529 | TemplateArgumentListInfo ToTAInfo; |
| 6530 | if (const auto *ASTTemplateArgs = D->getTemplateArgsAsWritten()) { |
| 6531 | if (Error Err = ImportTemplateArgumentListInfo(From: *ASTTemplateArgs, Result&: ToTAInfo)) |
| 6532 | return std::move(Err); |
| 6533 | } |
| 6534 | |
| 6535 | // Create the specialization. |
| 6536 | ClassTemplateSpecializationDecl *D2 = nullptr; |
| 6537 | if (PartialSpec) { |
| 6538 | if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>( |
| 6539 | ToD&: D2, FromD: D, args&: Importer.getToContext(), args: D->getTagKind(), args&: DC, args&: *BeginLocOrErr, |
| 6540 | args&: *IdLocOrErr, args&: ToTPList, args&: ClassTemplate, args: ArrayRef(TemplateArgs), |
| 6541 | /*CanonInjectedTST=*/args: CanQualType(), |
| 6542 | args: cast_or_null<ClassTemplatePartialSpecializationDecl>(Val: PrevDecl))) |
| 6543 | return D2; |
| 6544 | |
| 6545 | // Update InsertPos, because preceding import calls may have invalidated |
| 6546 | // it by adding new specializations. |
| 6547 | auto *PartSpec2 = cast<ClassTemplatePartialSpecializationDecl>(Val: D2); |
| 6548 | if (!ClassTemplate->findPartialSpecialization(Args: TemplateArgs, TPL: ToTPList, |
| 6549 | InsertPos)) |
| 6550 | // Add this partial specialization to the class template. |
| 6551 | ClassTemplate->AddPartialSpecialization(D: PartSpec2, InsertPos); |
| 6552 | if (Expected<ClassTemplatePartialSpecializationDecl *> ToInstOrErr = |
| 6553 | import(From: PartialSpec->getInstantiatedFromMember())) |
| 6554 | PartSpec2->setInstantiatedFromMember(*ToInstOrErr); |
| 6555 | else |
| 6556 | return ToInstOrErr.takeError(); |
| 6557 | |
| 6558 | updateLookupTableForTemplateParameters(Params&: *ToTPList); |
| 6559 | } else { // Not a partial specialization. |
| 6560 | if (GetImportedOrCreateDecl(ToD&: D2, FromD: D, args&: Importer.getToContext(), args: D->getTagKind(), |
| 6561 | args&: DC, args&: *BeginLocOrErr, args&: *IdLocOrErr, args&: ClassTemplate, |
| 6562 | args&: TemplateArgs, args: D->hasStrictPackMatch(), |
| 6563 | args&: PrevDecl)) |
| 6564 | return D2; |
| 6565 | |
| 6566 | // Update InsertPos, because preceding import calls may have invalidated |
| 6567 | // it by adding new specializations. |
| 6568 | if (!ClassTemplate->findSpecialization(Args: TemplateArgs, InsertPos)) |
| 6569 | // Add this specialization to the class template. |
| 6570 | ClassTemplate->AddSpecialization(D: D2, InsertPos); |
| 6571 | } |
| 6572 | |
| 6573 | D2->setSpecializationKind(D->getSpecializationKind()); |
| 6574 | |
| 6575 | // Set the context of this specialization/instantiation. |
| 6576 | D2->setLexicalDeclContext(LexicalDC); |
| 6577 | |
| 6578 | // Add to the DC only if it was an explicit specialization/instantiation. |
| 6579 | if (D2->isExplicitInstantiationOrSpecialization()) { |
| 6580 | LexicalDC->addDeclInternal(D: D2); |
| 6581 | } |
| 6582 | |
| 6583 | if (auto BraceRangeOrErr = import(From: D->getBraceRange())) |
| 6584 | D2->setBraceRange(*BraceRangeOrErr); |
| 6585 | else |
| 6586 | return BraceRangeOrErr.takeError(); |
| 6587 | |
| 6588 | if (Error Err = ImportTemplateParameterLists(FromD: D, ToD: D2)) |
| 6589 | return std::move(Err); |
| 6590 | |
| 6591 | // Import the qualifier, if any. |
| 6592 | if (auto LocOrErr = import(From: D->getQualifierLoc())) |
| 6593 | D2->setQualifierInfo(*LocOrErr); |
| 6594 | else |
| 6595 | return LocOrErr.takeError(); |
| 6596 | |
| 6597 | if (D->getTemplateArgsAsWritten()) |
| 6598 | D2->setTemplateArgsAsWritten(ToTAInfo); |
| 6599 | |
| 6600 | if (auto LocOrErr = import(From: D->getTemplateKeywordLoc())) |
| 6601 | D2->setTemplateKeywordLoc(*LocOrErr); |
| 6602 | else |
| 6603 | return LocOrErr.takeError(); |
| 6604 | |
| 6605 | if (auto LocOrErr = import(From: D->getExternKeywordLoc())) |
| 6606 | D2->setExternKeywordLoc(*LocOrErr); |
| 6607 | else |
| 6608 | return LocOrErr.takeError(); |
| 6609 | |
| 6610 | if (D->getPointOfInstantiation().isValid()) { |
| 6611 | if (auto POIOrErr = import(From: D->getPointOfInstantiation())) |
| 6612 | D2->setPointOfInstantiation(*POIOrErr); |
| 6613 | else |
| 6614 | return POIOrErr.takeError(); |
| 6615 | } |
| 6616 | |
| 6617 | D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind()); |
| 6618 | |
| 6619 | if (auto P = D->getInstantiatedFrom()) { |
| 6620 | if (auto *CTD = dyn_cast<ClassTemplateDecl *>(Val&: P)) { |
| 6621 | if (auto CTDorErr = import(From: CTD)) |
| 6622 | D2->setInstantiationOf(*CTDorErr); |
| 6623 | } else { |
| 6624 | auto *CTPSD = cast<ClassTemplatePartialSpecializationDecl *>(Val&: P); |
| 6625 | auto CTPSDOrErr = import(From: CTPSD); |
| 6626 | if (!CTPSDOrErr) |
| 6627 | return CTPSDOrErr.takeError(); |
| 6628 | const TemplateArgumentList &DArgs = D->getTemplateInstantiationArgs(); |
| 6629 | SmallVector<TemplateArgument, 2> D2ArgsVec(DArgs.size()); |
| 6630 | for (unsigned I = 0; I < DArgs.size(); ++I) { |
| 6631 | const TemplateArgument &DArg = DArgs[I]; |
| 6632 | if (auto ArgOrErr = import(From: DArg)) |
| 6633 | D2ArgsVec[I] = *ArgOrErr; |
| 6634 | else |
| 6635 | return ArgOrErr.takeError(); |
| 6636 | } |
| 6637 | D2->setInstantiationOf( |
| 6638 | PartialSpec: *CTPSDOrErr, |
| 6639 | TemplateArgs: TemplateArgumentList::CreateCopy(Context&: Importer.getToContext(), Args: D2ArgsVec)); |
| 6640 | } |
| 6641 | } |
| 6642 | |
| 6643 | if (D->isCompleteDefinition()) |
| 6644 | if (Error Err = ImportDefinition(From: D, To: D2)) |
| 6645 | return std::move(Err); |
| 6646 | |
| 6647 | return D2; |
| 6648 | } |
| 6649 | |
| 6650 | ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) { |
| 6651 | // Import the major distinguishing characteristics of this variable template. |
| 6652 | DeclContext *DC, *LexicalDC; |
| 6653 | DeclarationName Name; |
| 6654 | SourceLocation Loc; |
| 6655 | NamedDecl *ToD; |
| 6656 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 6657 | return std::move(Err); |
| 6658 | if (ToD) |
| 6659 | return ToD; |
| 6660 | |
| 6661 | // We may already have a template of the same name; try to find and match it. |
| 6662 | assert(!DC->isFunctionOrMethod() && |
| 6663 | "Variable templates cannot be declared at function scope" ); |
| 6664 | |
| 6665 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 6666 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 6667 | VarTemplateDecl *FoundByLookup = nullptr; |
| 6668 | for (auto *FoundDecl : FoundDecls) { |
| 6669 | if (!FoundDecl->isInIdentifierNamespace(NS: Decl::IDNS_Ordinary)) |
| 6670 | continue; |
| 6671 | |
| 6672 | if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Val: FoundDecl)) { |
| 6673 | // Use the templated decl, some linkage flags are set only there. |
| 6674 | if (!hasSameVisibilityContextAndLinkage(Found: FoundTemplate->getTemplatedDecl(), |
| 6675 | From: D->getTemplatedDecl())) |
| 6676 | continue; |
| 6677 | if (IsStructuralMatch(From: D, To: FoundTemplate)) { |
| 6678 | // FIXME Check for ODR error if the two definitions have |
| 6679 | // different initializers? |
| 6680 | VarTemplateDecl *FoundDef = getTemplateDefinition(D: FoundTemplate); |
| 6681 | if (D->getDeclContext()->isRecord()) { |
| 6682 | assert(FoundTemplate->getDeclContext()->isRecord() && |
| 6683 | "Member variable template imported as non-member, " |
| 6684 | "inconsistent imported AST?" ); |
| 6685 | if (FoundDef) |
| 6686 | return Importer.MapImported(From: D, To: FoundDef); |
| 6687 | if (!D->isThisDeclarationADefinition()) |
| 6688 | return Importer.MapImported(From: D, To: FoundTemplate); |
| 6689 | } else { |
| 6690 | if (FoundDef && D->isThisDeclarationADefinition()) |
| 6691 | return Importer.MapImported(From: D, To: FoundDef); |
| 6692 | } |
| 6693 | FoundByLookup = FoundTemplate; |
| 6694 | break; |
| 6695 | } |
| 6696 | ConflictingDecls.push_back(Elt: FoundDecl); |
| 6697 | } |
| 6698 | } |
| 6699 | |
| 6700 | if (!ConflictingDecls.empty()) { |
| 6701 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
| 6702 | Name, DC, IDNS: Decl::IDNS_Ordinary, Decls: ConflictingDecls.data(), |
| 6703 | NumDecls: ConflictingDecls.size()); |
| 6704 | if (NameOrErr) |
| 6705 | Name = NameOrErr.get(); |
| 6706 | else |
| 6707 | return NameOrErr.takeError(); |
| 6708 | } |
| 6709 | |
| 6710 | VarDecl *DTemplated = D->getTemplatedDecl(); |
| 6711 | |
| 6712 | // Import the type. |
| 6713 | // FIXME: Value not used? |
| 6714 | ExpectedType TypeOrErr = import(From: DTemplated->getType()); |
| 6715 | if (!TypeOrErr) |
| 6716 | return TypeOrErr.takeError(); |
| 6717 | |
| 6718 | // Create the declaration that is being templated. |
| 6719 | VarDecl *ToTemplated; |
| 6720 | if (Error Err = importInto(To&: ToTemplated, From: DTemplated)) |
| 6721 | return std::move(Err); |
| 6722 | |
| 6723 | // Create the variable template declaration itself. |
| 6724 | auto TemplateParamsOrErr = import(From: D->getTemplateParameters()); |
| 6725 | if (!TemplateParamsOrErr) |
| 6726 | return TemplateParamsOrErr.takeError(); |
| 6727 | |
| 6728 | VarTemplateDecl *ToVarTD; |
| 6729 | if (GetImportedOrCreateDecl(ToD&: ToVarTD, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, |
| 6730 | args&: Name, args&: *TemplateParamsOrErr, args&: ToTemplated)) |
| 6731 | return ToVarTD; |
| 6732 | |
| 6733 | ToTemplated->setDescribedVarTemplate(ToVarTD); |
| 6734 | |
| 6735 | ToVarTD->setAccess(D->getAccess()); |
| 6736 | ToVarTD->setLexicalDeclContext(LexicalDC); |
| 6737 | LexicalDC->addDeclInternal(D: ToVarTD); |
| 6738 | if (DC != Importer.getToContext().getTranslationUnitDecl()) |
| 6739 | updateLookupTableForTemplateParameters(Params&: **TemplateParamsOrErr); |
| 6740 | |
| 6741 | if (FoundByLookup) { |
| 6742 | auto *Recent = |
| 6743 | const_cast<VarTemplateDecl *>(FoundByLookup->getMostRecentDecl()); |
| 6744 | if (!ToTemplated->getPreviousDecl()) { |
| 6745 | auto *PrevTemplated = |
| 6746 | FoundByLookup->getTemplatedDecl()->getMostRecentDecl(); |
| 6747 | if (ToTemplated != PrevTemplated) |
| 6748 | ToTemplated->setPreviousDecl(PrevTemplated); |
| 6749 | } |
| 6750 | ToVarTD->setPreviousDecl(Recent); |
| 6751 | } |
| 6752 | |
| 6753 | return ToVarTD; |
| 6754 | } |
| 6755 | |
| 6756 | ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl( |
| 6757 | VarTemplateSpecializationDecl *D) { |
| 6758 | // A VarTemplateSpecializationDecl inherits from VarDecl, the import is done |
| 6759 | // in an analog way (but specialized for this case). |
| 6760 | |
| 6761 | SmallVector<Decl *, 2> Redecls = getCanonicalForwardRedeclChain(D); |
| 6762 | auto RedeclIt = Redecls.begin(); |
| 6763 | // Import the first part of the decl chain. I.e. import all previous |
| 6764 | // declarations starting from the canonical decl. |
| 6765 | for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) { |
| 6766 | ExpectedDecl RedeclOrErr = import(From: *RedeclIt); |
| 6767 | if (!RedeclOrErr) |
| 6768 | return RedeclOrErr.takeError(); |
| 6769 | } |
| 6770 | assert(*RedeclIt == D); |
| 6771 | |
| 6772 | VarTemplateDecl *VarTemplate = nullptr; |
| 6773 | if (Error Err = importInto(To&: VarTemplate, From: D->getSpecializedTemplate())) |
| 6774 | return std::move(Err); |
| 6775 | |
| 6776 | // Import the context of this declaration. |
| 6777 | DeclContext *DC, *LexicalDC; |
| 6778 | if (Error Err = ImportDeclContext(FromD: D, ToDC&: DC, ToLexicalDC&: LexicalDC)) |
| 6779 | return std::move(Err); |
| 6780 | |
| 6781 | // Import the location of this declaration. |
| 6782 | ExpectedSLoc BeginLocOrErr = import(From: D->getBeginLoc()); |
| 6783 | if (!BeginLocOrErr) |
| 6784 | return BeginLocOrErr.takeError(); |
| 6785 | |
| 6786 | auto IdLocOrErr = import(From: D->getLocation()); |
| 6787 | if (!IdLocOrErr) |
| 6788 | return IdLocOrErr.takeError(); |
| 6789 | |
| 6790 | // Import template arguments. |
| 6791 | SmallVector<TemplateArgument, 2> TemplateArgs; |
| 6792 | if (Error Err = |
| 6793 | ImportTemplateArguments(FromArgs: D->getTemplateArgs().asArray(), ToArgs&: TemplateArgs)) |
| 6794 | return std::move(Err); |
| 6795 | |
| 6796 | // Try to find an existing specialization with these template arguments. |
| 6797 | void *InsertPos = nullptr; |
| 6798 | VarTemplateSpecializationDecl *FoundSpecialization = |
| 6799 | VarTemplate->findSpecialization(Args: TemplateArgs, InsertPos); |
| 6800 | if (FoundSpecialization) { |
| 6801 | if (IsStructuralMatch(From: D, To: FoundSpecialization)) { |
| 6802 | VarDecl *FoundDef = FoundSpecialization->getDefinition(); |
| 6803 | if (D->getDeclContext()->isRecord()) { |
| 6804 | // In a record, it is allowed only to have one optional declaration and |
| 6805 | // one definition of the (static or constexpr) variable template. |
| 6806 | assert( |
| 6807 | FoundSpecialization->getDeclContext()->isRecord() && |
| 6808 | "Member variable template specialization imported as non-member, " |
| 6809 | "inconsistent imported AST?" ); |
| 6810 | if (FoundDef) |
| 6811 | return Importer.MapImported(From: D, To: FoundDef); |
| 6812 | if (!D->isThisDeclarationADefinition()) |
| 6813 | return Importer.MapImported(From: D, To: FoundSpecialization); |
| 6814 | } else { |
| 6815 | // If definition is imported and there is already one, map to it. |
| 6816 | // Otherwise create a new variable and link it to the existing. |
| 6817 | if (FoundDef && D->isThisDeclarationADefinition()) |
| 6818 | return Importer.MapImported(From: D, To: FoundDef); |
| 6819 | } |
| 6820 | } else { |
| 6821 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 6822 | } |
| 6823 | } |
| 6824 | |
| 6825 | VarTemplateSpecializationDecl *D2 = nullptr; |
| 6826 | |
| 6827 | TemplateArgumentListInfo ToTAInfo; |
| 6828 | if (const auto *Args = D->getTemplateArgsAsWritten()) { |
| 6829 | if (Error Err = ImportTemplateArgumentListInfo(From: *Args, Result&: ToTAInfo)) |
| 6830 | return std::move(Err); |
| 6831 | } |
| 6832 | |
| 6833 | using PartVarSpecDecl = VarTemplatePartialSpecializationDecl; |
| 6834 | // Create a new specialization. |
| 6835 | if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(Val: D)) { |
| 6836 | auto ToTPListOrErr = import(From: FromPartial->getTemplateParameters()); |
| 6837 | if (!ToTPListOrErr) |
| 6838 | return ToTPListOrErr.takeError(); |
| 6839 | |
| 6840 | PartVarSpecDecl *ToPartial; |
| 6841 | if (GetImportedOrCreateDecl(ToD&: ToPartial, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 6842 | args&: *BeginLocOrErr, args&: *IdLocOrErr, args&: *ToTPListOrErr, |
| 6843 | args&: VarTemplate, args: QualType(), args: nullptr, |
| 6844 | args: D->getStorageClass(), args&: TemplateArgs)) |
| 6845 | return ToPartial; |
| 6846 | |
| 6847 | if (Expected<PartVarSpecDecl *> ToInstOrErr = |
| 6848 | import(From: FromPartial->getInstantiatedFromMember())) |
| 6849 | ToPartial->setInstantiatedFromMember(*ToInstOrErr); |
| 6850 | else |
| 6851 | return ToInstOrErr.takeError(); |
| 6852 | |
| 6853 | if (FromPartial->isMemberSpecialization()) |
| 6854 | ToPartial->setMemberSpecialization(); |
| 6855 | |
| 6856 | D2 = ToPartial; |
| 6857 | |
| 6858 | // FIXME: Use this update if VarTemplatePartialSpecializationDecl is fixed |
| 6859 | // to adopt template parameters. |
| 6860 | // updateLookupTableForTemplateParameters(**ToTPListOrErr); |
| 6861 | } else { // Full specialization |
| 6862 | if (GetImportedOrCreateDecl(ToD&: D2, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 6863 | args&: *BeginLocOrErr, args&: *IdLocOrErr, args&: VarTemplate, |
| 6864 | args: QualType(), args: nullptr, args: D->getStorageClass(), |
| 6865 | args&: TemplateArgs)) |
| 6866 | return D2; |
| 6867 | } |
| 6868 | |
| 6869 | // Update InsertPos, because preceding import calls may have invalidated |
| 6870 | // it by adding new specializations. |
| 6871 | if (!VarTemplate->findSpecialization(Args: TemplateArgs, InsertPos)) |
| 6872 | VarTemplate->AddSpecialization(D: D2, InsertPos); |
| 6873 | |
| 6874 | QualType T; |
| 6875 | if (Error Err = importInto(To&: T, From: D->getType())) |
| 6876 | return std::move(Err); |
| 6877 | D2->setType(T); |
| 6878 | |
| 6879 | auto TInfoOrErr = import(From: D->getTypeSourceInfo()); |
| 6880 | if (!TInfoOrErr) |
| 6881 | return TInfoOrErr.takeError(); |
| 6882 | D2->setTypeSourceInfo(*TInfoOrErr); |
| 6883 | |
| 6884 | if (D->getPointOfInstantiation().isValid()) { |
| 6885 | if (ExpectedSLoc POIOrErr = import(From: D->getPointOfInstantiation())) |
| 6886 | D2->setPointOfInstantiation(*POIOrErr); |
| 6887 | else |
| 6888 | return POIOrErr.takeError(); |
| 6889 | } |
| 6890 | |
| 6891 | D2->setSpecializationKind(D->getSpecializationKind()); |
| 6892 | |
| 6893 | if (D->getTemplateArgsAsWritten()) |
| 6894 | D2->setTemplateArgsAsWritten(ToTAInfo); |
| 6895 | |
| 6896 | if (auto LocOrErr = import(From: D->getQualifierLoc())) |
| 6897 | D2->setQualifierInfo(*LocOrErr); |
| 6898 | else |
| 6899 | return LocOrErr.takeError(); |
| 6900 | |
| 6901 | if (D->isConstexpr()) |
| 6902 | D2->setConstexpr(true); |
| 6903 | |
| 6904 | D2->setAccess(D->getAccess()); |
| 6905 | |
| 6906 | if (Error Err = ImportInitializer(From: D, To: D2)) |
| 6907 | return std::move(Err); |
| 6908 | |
| 6909 | if (FoundSpecialization) |
| 6910 | D2->setPreviousDecl(FoundSpecialization->getMostRecentDecl()); |
| 6911 | |
| 6912 | addDeclToContexts(FromD: D, ToD: D2); |
| 6913 | |
| 6914 | // Import the rest of the chain. I.e. import all subsequent declarations. |
| 6915 | for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) { |
| 6916 | ExpectedDecl RedeclOrErr = import(From: *RedeclIt); |
| 6917 | if (!RedeclOrErr) |
| 6918 | return RedeclOrErr.takeError(); |
| 6919 | } |
| 6920 | |
| 6921 | return D2; |
| 6922 | } |
| 6923 | |
| 6924 | ExpectedDecl |
| 6925 | ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
| 6926 | DeclContext *DC, *LexicalDC; |
| 6927 | DeclarationName Name; |
| 6928 | SourceLocation Loc; |
| 6929 | NamedDecl *ToD; |
| 6930 | |
| 6931 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 6932 | return std::move(Err); |
| 6933 | |
| 6934 | if (ToD) |
| 6935 | return ToD; |
| 6936 | |
| 6937 | const FunctionTemplateDecl *FoundByLookup = nullptr; |
| 6938 | |
| 6939 | // Try to find a function in our own ("to") context with the same name, same |
| 6940 | // type, and in the same context as the function we're importing. |
| 6941 | // FIXME Split this into a separate function. |
| 6942 | if (!LexicalDC->isFunctionOrMethod()) { |
| 6943 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend; |
| 6944 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 6945 | for (auto *FoundDecl : FoundDecls) { |
| 6946 | if (!FoundDecl->isInIdentifierNamespace(NS: IDNS)) |
| 6947 | continue; |
| 6948 | |
| 6949 | if (auto *FoundTemplate = dyn_cast<FunctionTemplateDecl>(Val: FoundDecl)) { |
| 6950 | if (!hasSameVisibilityContextAndLinkage(Found: FoundTemplate, From: D)) |
| 6951 | continue; |
| 6952 | if (IsStructuralMatch(From: D, To: FoundTemplate)) { |
| 6953 | FunctionTemplateDecl *TemplateWithDef = |
| 6954 | getTemplateDefinition(D: FoundTemplate); |
| 6955 | if (D->isThisDeclarationADefinition() && TemplateWithDef) |
| 6956 | return Importer.MapImported(From: D, To: TemplateWithDef); |
| 6957 | |
| 6958 | FoundByLookup = FoundTemplate; |
| 6959 | break; |
| 6960 | // TODO: handle conflicting names |
| 6961 | } |
| 6962 | } |
| 6963 | } |
| 6964 | } |
| 6965 | |
| 6966 | auto ParamsOrErr = import(From: D->getTemplateParameters()); |
| 6967 | if (!ParamsOrErr) |
| 6968 | return ParamsOrErr.takeError(); |
| 6969 | TemplateParameterList *Params = *ParamsOrErr; |
| 6970 | |
| 6971 | FunctionDecl *TemplatedFD; |
| 6972 | if (Error Err = importInto(To&: TemplatedFD, From: D->getTemplatedDecl())) |
| 6973 | return std::move(Err); |
| 6974 | |
| 6975 | // At creation of the template the template parameters are "adopted" |
| 6976 | // (DeclContext is changed). After this possible change the lookup table |
| 6977 | // must be updated. |
| 6978 | // At deduction guides the DeclContext of the template parameters may be |
| 6979 | // different from what we would expect, it may be the class template, or a |
| 6980 | // probably different CXXDeductionGuideDecl. This may come from the fact that |
| 6981 | // the template parameter objects may be shared between deduction guides or |
| 6982 | // the class template, and at creation of multiple FunctionTemplateDecl |
| 6983 | // objects (for deduction guides) the same parameters are re-used. The |
| 6984 | // "adoption" happens multiple times with different parent, even recursively |
| 6985 | // for TemplateTemplateParmDecl. The same happens at import when the |
| 6986 | // FunctionTemplateDecl objects are created, but in different order. |
| 6987 | // In this way the DeclContext of these template parameters is not necessarily |
| 6988 | // the same as in the "from" context. |
| 6989 | SmallVector<DeclContext *, 2> OldParamDC; |
| 6990 | OldParamDC.reserve(N: Params->size()); |
| 6991 | llvm::transform(Range&: *Params, d_first: std::back_inserter(x&: OldParamDC), |
| 6992 | F: [](NamedDecl *ND) { return ND->getDeclContext(); }); |
| 6993 | |
| 6994 | FunctionTemplateDecl *ToFunc; |
| 6995 | if (GetImportedOrCreateDecl(ToD&: ToFunc, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, args&: Name, |
| 6996 | args&: Params, args&: TemplatedFD)) |
| 6997 | return ToFunc; |
| 6998 | |
| 6999 | // Fail if TemplatedFD is already part of a template. |
| 7000 | // The template should have been found by structural equivalence check before, |
| 7001 | // or ToFunc should be already imported. |
| 7002 | // If not, there is AST incompatibility that can be caused by previous import |
| 7003 | // errors. (NameConflict is not exact here.) |
| 7004 | if (TemplatedFD->getDescribedTemplate()) |
| 7005 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 7006 | |
| 7007 | TemplatedFD->setDescribedFunctionTemplate(ToFunc); |
| 7008 | |
| 7009 | ToFunc->setAccess(D->getAccess()); |
| 7010 | ToFunc->setLexicalDeclContext(LexicalDC); |
| 7011 | addDeclToContexts(FromD: D, ToD: ToFunc); |
| 7012 | |
| 7013 | ASTImporterLookupTable *LT = Importer.SharedState->getLookupTable(); |
| 7014 | if (LT && !OldParamDC.empty()) { |
| 7015 | for (unsigned int I = 0; I < OldParamDC.size(); ++I) |
| 7016 | LT->updateForced(ND: Params->getParam(Idx: I), OldDC: OldParamDC[I]); |
| 7017 | } |
| 7018 | |
| 7019 | if (FoundByLookup) { |
| 7020 | auto *Recent = |
| 7021 | const_cast<FunctionTemplateDecl *>(FoundByLookup->getMostRecentDecl()); |
| 7022 | if (!TemplatedFD->getPreviousDecl()) { |
| 7023 | assert(FoundByLookup->getTemplatedDecl() && |
| 7024 | "Found decl must have its templated decl set" ); |
| 7025 | auto *PrevTemplated = |
| 7026 | FoundByLookup->getTemplatedDecl()->getMostRecentDecl(); |
| 7027 | if (TemplatedFD != PrevTemplated) |
| 7028 | TemplatedFD->setPreviousDecl(PrevTemplated); |
| 7029 | } |
| 7030 | ToFunc->setPreviousDecl(Recent); |
| 7031 | } |
| 7032 | |
| 7033 | return ToFunc; |
| 7034 | } |
| 7035 | |
| 7036 | ExpectedDecl ASTNodeImporter::VisitConceptDecl(ConceptDecl *D) { |
| 7037 | DeclContext *DC, *LexicalDC; |
| 7038 | Error Err = ImportDeclContext(FromD: D, ToDC&: DC, ToLexicalDC&: LexicalDC); |
| 7039 | auto LocationOrErr = importChecked(Err, From: D->getLocation()); |
| 7040 | auto NameDeclOrErr = importChecked(Err, From: D->getDeclName()); |
| 7041 | auto ToTemplateParameters = importChecked(Err, From: D->getTemplateParameters()); |
| 7042 | auto ConstraintExpr = importChecked(Err, From: D->getConstraintExpr()); |
| 7043 | if (Err) |
| 7044 | return std::move(Err); |
| 7045 | |
| 7046 | ConceptDecl *To; |
| 7047 | if (GetImportedOrCreateDecl(ToD&: To, FromD: D, args&: Importer.getToContext(), args&: DC, args&: LocationOrErr, |
| 7048 | args&: NameDeclOrErr, args&: ToTemplateParameters, |
| 7049 | args&: ConstraintExpr)) |
| 7050 | return To; |
| 7051 | To->setLexicalDeclContext(LexicalDC); |
| 7052 | LexicalDC->addDeclInternal(D: To); |
| 7053 | return To; |
| 7054 | } |
| 7055 | |
| 7056 | ExpectedDecl |
| 7057 | ASTNodeImporter::VisitRequiresExprBodyDecl(RequiresExprBodyDecl *D) { |
| 7058 | DeclContext *DC, *LexicalDC; |
| 7059 | Error Err = ImportDeclContext(FromD: D, ToDC&: DC, ToLexicalDC&: LexicalDC); |
| 7060 | auto RequiresLoc = importChecked(Err, From: D->getLocation()); |
| 7061 | if (Err) |
| 7062 | return std::move(Err); |
| 7063 | |
| 7064 | RequiresExprBodyDecl *To; |
| 7065 | if (GetImportedOrCreateDecl(ToD&: To, FromD: D, args&: Importer.getToContext(), args&: DC, args&: RequiresLoc)) |
| 7066 | return To; |
| 7067 | To->setLexicalDeclContext(LexicalDC); |
| 7068 | LexicalDC->addDeclInternal(D: To); |
| 7069 | return To; |
| 7070 | } |
| 7071 | |
| 7072 | ExpectedDecl ASTNodeImporter::VisitImplicitConceptSpecializationDecl( |
| 7073 | ImplicitConceptSpecializationDecl *D) { |
| 7074 | DeclContext *DC, *LexicalDC; |
| 7075 | Error Err = ImportDeclContext(FromD: D, ToDC&: DC, ToLexicalDC&: LexicalDC); |
| 7076 | auto ToSL = importChecked(Err, From: D->getLocation()); |
| 7077 | if (Err) |
| 7078 | return std::move(Err); |
| 7079 | |
| 7080 | SmallVector<TemplateArgument, 2> ToArgs(D->getTemplateArguments().size()); |
| 7081 | if (Error Err = ImportTemplateArguments(FromArgs: D->getTemplateArguments(), ToArgs)) |
| 7082 | return std::move(Err); |
| 7083 | |
| 7084 | ImplicitConceptSpecializationDecl *To; |
| 7085 | if (GetImportedOrCreateDecl(ToD&: To, FromD: D, args&: Importer.getToContext(), args&: DC, args&: ToSL, args&: ToArgs)) |
| 7086 | return To; |
| 7087 | To->setLexicalDeclContext(LexicalDC); |
| 7088 | LexicalDC->addDeclInternal(D: To); |
| 7089 | return To; |
| 7090 | } |
| 7091 | |
| 7092 | //---------------------------------------------------------------------------- |
| 7093 | // Import Statements |
| 7094 | //---------------------------------------------------------------------------- |
| 7095 | |
| 7096 | ExpectedStmt ASTNodeImporter::VisitStmt(Stmt *S) { |
| 7097 | Importer.FromDiag(Loc: S->getBeginLoc(), DiagID: diag::err_unsupported_ast_node) |
| 7098 | << S->getStmtClassName(); |
| 7099 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
| 7100 | } |
| 7101 | |
| 7102 | |
| 7103 | ExpectedStmt ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) { |
| 7104 | if (Importer.returnWithErrorInTest()) |
| 7105 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
| 7106 | SmallVector<IdentifierInfo *, 4> Names; |
| 7107 | for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) { |
| 7108 | IdentifierInfo *ToII = Importer.Import(FromId: S->getOutputIdentifier(i: I)); |
| 7109 | // ToII is nullptr when no symbolic name is given for output operand |
| 7110 | // see ParseStmtAsm::ParseAsmOperandsOpt |
| 7111 | Names.push_back(Elt: ToII); |
| 7112 | } |
| 7113 | |
| 7114 | for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) { |
| 7115 | IdentifierInfo *ToII = Importer.Import(FromId: S->getInputIdentifier(i: I)); |
| 7116 | // ToII is nullptr when no symbolic name is given for input operand |
| 7117 | // see ParseStmtAsm::ParseAsmOperandsOpt |
| 7118 | Names.push_back(Elt: ToII); |
| 7119 | } |
| 7120 | |
| 7121 | SmallVector<Expr *, 4> Clobbers; |
| 7122 | for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) { |
| 7123 | if (auto ClobberOrErr = import(From: S->getClobberExpr(i: I))) |
| 7124 | Clobbers.push_back(Elt: *ClobberOrErr); |
| 7125 | else |
| 7126 | return ClobberOrErr.takeError(); |
| 7127 | |
| 7128 | } |
| 7129 | |
| 7130 | SmallVector<Expr *, 4> Constraints; |
| 7131 | for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) { |
| 7132 | if (auto OutputOrErr = import(From: S->getOutputConstraintExpr(i: I))) |
| 7133 | Constraints.push_back(Elt: *OutputOrErr); |
| 7134 | else |
| 7135 | return OutputOrErr.takeError(); |
| 7136 | } |
| 7137 | |
| 7138 | for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) { |
| 7139 | if (auto InputOrErr = import(From: S->getInputConstraintExpr(i: I))) |
| 7140 | Constraints.push_back(Elt: *InputOrErr); |
| 7141 | else |
| 7142 | return InputOrErr.takeError(); |
| 7143 | } |
| 7144 | |
| 7145 | SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs() + |
| 7146 | S->getNumLabels()); |
| 7147 | if (Error Err = ImportContainerChecked(InContainer: S->outputs(), OutContainer&: Exprs)) |
| 7148 | return std::move(Err); |
| 7149 | |
| 7150 | if (Error Err = |
| 7151 | ImportArrayChecked(InContainer: S->inputs(), Obegin: Exprs.begin() + S->getNumOutputs())) |
| 7152 | return std::move(Err); |
| 7153 | |
| 7154 | if (Error Err = ImportArrayChecked( |
| 7155 | InContainer: S->labels(), Obegin: Exprs.begin() + S->getNumOutputs() + S->getNumInputs())) |
| 7156 | return std::move(Err); |
| 7157 | |
| 7158 | ExpectedSLoc AsmLocOrErr = import(From: S->getAsmLoc()); |
| 7159 | if (!AsmLocOrErr) |
| 7160 | return AsmLocOrErr.takeError(); |
| 7161 | auto AsmStrOrErr = import(From: S->getAsmStringExpr()); |
| 7162 | if (!AsmStrOrErr) |
| 7163 | return AsmStrOrErr.takeError(); |
| 7164 | ExpectedSLoc RParenLocOrErr = import(From: S->getRParenLoc()); |
| 7165 | if (!RParenLocOrErr) |
| 7166 | return RParenLocOrErr.takeError(); |
| 7167 | |
| 7168 | return new (Importer.getToContext()) GCCAsmStmt( |
| 7169 | Importer.getToContext(), |
| 7170 | *AsmLocOrErr, |
| 7171 | S->isSimple(), |
| 7172 | S->isVolatile(), |
| 7173 | S->getNumOutputs(), |
| 7174 | S->getNumInputs(), |
| 7175 | Names.data(), |
| 7176 | Constraints.data(), |
| 7177 | Exprs.data(), |
| 7178 | *AsmStrOrErr, |
| 7179 | S->getNumClobbers(), |
| 7180 | Clobbers.data(), |
| 7181 | S->getNumLabels(), |
| 7182 | *RParenLocOrErr); |
| 7183 | } |
| 7184 | |
| 7185 | ExpectedStmt ASTNodeImporter::VisitDeclStmt(DeclStmt *S) { |
| 7186 | |
| 7187 | Error Err = Error::success(); |
| 7188 | auto ToDG = importChecked(Err, From: S->getDeclGroup()); |
| 7189 | auto ToBeginLoc = importChecked(Err, From: S->getBeginLoc()); |
| 7190 | auto ToEndLoc = importChecked(Err, From: S->getEndLoc()); |
| 7191 | if (Err) |
| 7192 | return std::move(Err); |
| 7193 | return new (Importer.getToContext()) DeclStmt(ToDG, ToBeginLoc, ToEndLoc); |
| 7194 | } |
| 7195 | |
| 7196 | ExpectedStmt ASTNodeImporter::VisitNullStmt(NullStmt *S) { |
| 7197 | ExpectedSLoc ToSemiLocOrErr = import(From: S->getSemiLoc()); |
| 7198 | if (!ToSemiLocOrErr) |
| 7199 | return ToSemiLocOrErr.takeError(); |
| 7200 | return new (Importer.getToContext()) NullStmt( |
| 7201 | *ToSemiLocOrErr, S->hasLeadingEmptyMacro()); |
| 7202 | } |
| 7203 | |
| 7204 | ExpectedStmt ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) { |
| 7205 | SmallVector<Stmt *, 8> ToStmts(S->size()); |
| 7206 | |
| 7207 | if (Error Err = ImportContainerChecked(InContainer: S->body(), OutContainer&: ToStmts)) |
| 7208 | return std::move(Err); |
| 7209 | |
| 7210 | ExpectedSLoc ToLBracLocOrErr = import(From: S->getLBracLoc()); |
| 7211 | if (!ToLBracLocOrErr) |
| 7212 | return ToLBracLocOrErr.takeError(); |
| 7213 | |
| 7214 | ExpectedSLoc ToRBracLocOrErr = import(From: S->getRBracLoc()); |
| 7215 | if (!ToRBracLocOrErr) |
| 7216 | return ToRBracLocOrErr.takeError(); |
| 7217 | |
| 7218 | FPOptionsOverride FPO = |
| 7219 | S->hasStoredFPFeatures() ? S->getStoredFPFeatures() : FPOptionsOverride(); |
| 7220 | return CompoundStmt::Create(C: Importer.getToContext(), Stmts: ToStmts, FPFeatures: FPO, |
| 7221 | LB: *ToLBracLocOrErr, RB: *ToRBracLocOrErr); |
| 7222 | } |
| 7223 | |
| 7224 | ExpectedStmt ASTNodeImporter::VisitCaseStmt(CaseStmt *S) { |
| 7225 | |
| 7226 | Error Err = Error::success(); |
| 7227 | auto ToLHS = importChecked(Err, From: S->getLHS()); |
| 7228 | auto ToRHS = importChecked(Err, From: S->getRHS()); |
| 7229 | auto ToSubStmt = importChecked(Err, From: S->getSubStmt()); |
| 7230 | auto ToCaseLoc = importChecked(Err, From: S->getCaseLoc()); |
| 7231 | auto ToEllipsisLoc = importChecked(Err, From: S->getEllipsisLoc()); |
| 7232 | auto ToColonLoc = importChecked(Err, From: S->getColonLoc()); |
| 7233 | if (Err) |
| 7234 | return std::move(Err); |
| 7235 | |
| 7236 | auto *ToStmt = CaseStmt::Create(Ctx: Importer.getToContext(), lhs: ToLHS, rhs: ToRHS, |
| 7237 | caseLoc: ToCaseLoc, ellipsisLoc: ToEllipsisLoc, colonLoc: ToColonLoc); |
| 7238 | ToStmt->setSubStmt(ToSubStmt); |
| 7239 | |
| 7240 | return ToStmt; |
| 7241 | } |
| 7242 | |
| 7243 | ExpectedStmt ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) { |
| 7244 | |
| 7245 | Error Err = Error::success(); |
| 7246 | auto ToDefaultLoc = importChecked(Err, From: S->getDefaultLoc()); |
| 7247 | auto ToColonLoc = importChecked(Err, From: S->getColonLoc()); |
| 7248 | auto ToSubStmt = importChecked(Err, From: S->getSubStmt()); |
| 7249 | if (Err) |
| 7250 | return std::move(Err); |
| 7251 | |
| 7252 | return new (Importer.getToContext()) DefaultStmt( |
| 7253 | ToDefaultLoc, ToColonLoc, ToSubStmt); |
| 7254 | } |
| 7255 | |
| 7256 | ExpectedStmt ASTNodeImporter::VisitLabelStmt(LabelStmt *S) { |
| 7257 | |
| 7258 | Error Err = Error::success(); |
| 7259 | auto ToIdentLoc = importChecked(Err, From: S->getIdentLoc()); |
| 7260 | auto ToLabelDecl = importChecked(Err, From: S->getDecl()); |
| 7261 | auto ToSubStmt = importChecked(Err, From: S->getSubStmt()); |
| 7262 | if (Err) |
| 7263 | return std::move(Err); |
| 7264 | |
| 7265 | return new (Importer.getToContext()) LabelStmt( |
| 7266 | ToIdentLoc, ToLabelDecl, ToSubStmt); |
| 7267 | } |
| 7268 | |
| 7269 | ExpectedStmt ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) { |
| 7270 | ExpectedSLoc ToAttrLocOrErr = import(From: S->getAttrLoc()); |
| 7271 | if (!ToAttrLocOrErr) |
| 7272 | return ToAttrLocOrErr.takeError(); |
| 7273 | ArrayRef<const Attr*> FromAttrs(S->getAttrs()); |
| 7274 | SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size()); |
| 7275 | if (Error Err = ImportContainerChecked(InContainer: FromAttrs, OutContainer&: ToAttrs)) |
| 7276 | return std::move(Err); |
| 7277 | ExpectedStmt ToSubStmtOrErr = import(From: S->getSubStmt()); |
| 7278 | if (!ToSubStmtOrErr) |
| 7279 | return ToSubStmtOrErr.takeError(); |
| 7280 | |
| 7281 | return AttributedStmt::Create( |
| 7282 | C: Importer.getToContext(), Loc: *ToAttrLocOrErr, Attrs: ToAttrs, SubStmt: *ToSubStmtOrErr); |
| 7283 | } |
| 7284 | |
| 7285 | ExpectedStmt ASTNodeImporter::VisitIfStmt(IfStmt *S) { |
| 7286 | |
| 7287 | Error Err = Error::success(); |
| 7288 | auto ToIfLoc = importChecked(Err, From: S->getIfLoc()); |
| 7289 | auto ToInit = importChecked(Err, From: S->getInit()); |
| 7290 | auto ToConditionVariable = importChecked(Err, From: S->getConditionVariable()); |
| 7291 | auto ToCond = importChecked(Err, From: S->getCond()); |
| 7292 | auto ToLParenLoc = importChecked(Err, From: S->getLParenLoc()); |
| 7293 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
| 7294 | auto ToThen = importChecked(Err, From: S->getThen()); |
| 7295 | auto ToElseLoc = importChecked(Err, From: S->getElseLoc()); |
| 7296 | auto ToElse = importChecked(Err, From: S->getElse()); |
| 7297 | if (Err) |
| 7298 | return std::move(Err); |
| 7299 | |
| 7300 | return IfStmt::Create(Ctx: Importer.getToContext(), IL: ToIfLoc, Kind: S->getStatementKind(), |
| 7301 | Init: ToInit, Var: ToConditionVariable, Cond: ToCond, LPL: ToLParenLoc, |
| 7302 | RPL: ToRParenLoc, Then: ToThen, EL: ToElseLoc, Else: ToElse); |
| 7303 | } |
| 7304 | |
| 7305 | ExpectedStmt ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) { |
| 7306 | |
| 7307 | Error Err = Error::success(); |
| 7308 | auto ToInit = importChecked(Err, From: S->getInit()); |
| 7309 | auto ToConditionVariable = importChecked(Err, From: S->getConditionVariable()); |
| 7310 | auto ToCond = importChecked(Err, From: S->getCond()); |
| 7311 | auto ToLParenLoc = importChecked(Err, From: S->getLParenLoc()); |
| 7312 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
| 7313 | auto ToBody = importChecked(Err, From: S->getBody()); |
| 7314 | auto ToSwitchLoc = importChecked(Err, From: S->getSwitchLoc()); |
| 7315 | if (Err) |
| 7316 | return std::move(Err); |
| 7317 | |
| 7318 | auto *ToStmt = |
| 7319 | SwitchStmt::Create(Ctx: Importer.getToContext(), Init: ToInit, Var: ToConditionVariable, |
| 7320 | Cond: ToCond, LParenLoc: ToLParenLoc, RParenLoc: ToRParenLoc); |
| 7321 | ToStmt->setBody(ToBody); |
| 7322 | ToStmt->setSwitchLoc(ToSwitchLoc); |
| 7323 | |
| 7324 | // Now we have to re-chain the cases. |
| 7325 | SwitchCase *LastChainedSwitchCase = nullptr; |
| 7326 | for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr; |
| 7327 | SC = SC->getNextSwitchCase()) { |
| 7328 | Expected<SwitchCase *> ToSCOrErr = import(From: SC); |
| 7329 | if (!ToSCOrErr) |
| 7330 | return ToSCOrErr.takeError(); |
| 7331 | if (LastChainedSwitchCase) |
| 7332 | LastChainedSwitchCase->setNextSwitchCase(*ToSCOrErr); |
| 7333 | else |
| 7334 | ToStmt->setSwitchCaseList(*ToSCOrErr); |
| 7335 | LastChainedSwitchCase = *ToSCOrErr; |
| 7336 | } |
| 7337 | |
| 7338 | return ToStmt; |
| 7339 | } |
| 7340 | |
| 7341 | ExpectedStmt ASTNodeImporter::VisitWhileStmt(WhileStmt *S) { |
| 7342 | |
| 7343 | Error Err = Error::success(); |
| 7344 | auto ToConditionVariable = importChecked(Err, From: S->getConditionVariable()); |
| 7345 | auto ToCond = importChecked(Err, From: S->getCond()); |
| 7346 | auto ToBody = importChecked(Err, From: S->getBody()); |
| 7347 | auto ToWhileLoc = importChecked(Err, From: S->getWhileLoc()); |
| 7348 | auto ToLParenLoc = importChecked(Err, From: S->getLParenLoc()); |
| 7349 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
| 7350 | if (Err) |
| 7351 | return std::move(Err); |
| 7352 | |
| 7353 | return WhileStmt::Create(Ctx: Importer.getToContext(), Var: ToConditionVariable, Cond: ToCond, |
| 7354 | Body: ToBody, WL: ToWhileLoc, LParenLoc: ToLParenLoc, RParenLoc: ToRParenLoc); |
| 7355 | } |
| 7356 | |
| 7357 | ExpectedStmt ASTNodeImporter::VisitDoStmt(DoStmt *S) { |
| 7358 | |
| 7359 | Error Err = Error::success(); |
| 7360 | auto ToBody = importChecked(Err, From: S->getBody()); |
| 7361 | auto ToCond = importChecked(Err, From: S->getCond()); |
| 7362 | auto ToDoLoc = importChecked(Err, From: S->getDoLoc()); |
| 7363 | auto ToWhileLoc = importChecked(Err, From: S->getWhileLoc()); |
| 7364 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
| 7365 | if (Err) |
| 7366 | return std::move(Err); |
| 7367 | |
| 7368 | return new (Importer.getToContext()) DoStmt( |
| 7369 | ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc); |
| 7370 | } |
| 7371 | |
| 7372 | ExpectedStmt ASTNodeImporter::VisitForStmt(ForStmt *S) { |
| 7373 | |
| 7374 | Error Err = Error::success(); |
| 7375 | auto ToInit = importChecked(Err, From: S->getInit()); |
| 7376 | auto ToCond = importChecked(Err, From: S->getCond()); |
| 7377 | auto ToConditionVariable = importChecked(Err, From: S->getConditionVariable()); |
| 7378 | auto ToInc = importChecked(Err, From: S->getInc()); |
| 7379 | auto ToBody = importChecked(Err, From: S->getBody()); |
| 7380 | auto ToForLoc = importChecked(Err, From: S->getForLoc()); |
| 7381 | auto ToLParenLoc = importChecked(Err, From: S->getLParenLoc()); |
| 7382 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
| 7383 | if (Err) |
| 7384 | return std::move(Err); |
| 7385 | |
| 7386 | return new (Importer.getToContext()) ForStmt( |
| 7387 | Importer.getToContext(), |
| 7388 | ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc, ToLParenLoc, |
| 7389 | ToRParenLoc); |
| 7390 | } |
| 7391 | |
| 7392 | ExpectedStmt ASTNodeImporter::VisitGotoStmt(GotoStmt *S) { |
| 7393 | |
| 7394 | Error Err = Error::success(); |
| 7395 | auto ToLabel = importChecked(Err, From: S->getLabel()); |
| 7396 | auto ToGotoLoc = importChecked(Err, From: S->getGotoLoc()); |
| 7397 | auto ToLabelLoc = importChecked(Err, From: S->getLabelLoc()); |
| 7398 | if (Err) |
| 7399 | return std::move(Err); |
| 7400 | |
| 7401 | return new (Importer.getToContext()) GotoStmt( |
| 7402 | ToLabel, ToGotoLoc, ToLabelLoc); |
| 7403 | } |
| 7404 | |
| 7405 | ExpectedStmt ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) { |
| 7406 | |
| 7407 | Error Err = Error::success(); |
| 7408 | auto ToGotoLoc = importChecked(Err, From: S->getGotoLoc()); |
| 7409 | auto ToStarLoc = importChecked(Err, From: S->getStarLoc()); |
| 7410 | auto ToTarget = importChecked(Err, From: S->getTarget()); |
| 7411 | if (Err) |
| 7412 | return std::move(Err); |
| 7413 | |
| 7414 | return new (Importer.getToContext()) IndirectGotoStmt( |
| 7415 | ToGotoLoc, ToStarLoc, ToTarget); |
| 7416 | } |
| 7417 | |
| 7418 | template <typename StmtClass> |
| 7419 | static ExpectedStmt ImportLoopControlStmt(ASTNodeImporter &NodeImporter, |
| 7420 | ASTImporter &Importer, StmtClass *S) { |
| 7421 | Error Err = Error::success(); |
| 7422 | auto ToLoc = NodeImporter.importChecked(Err, S->getKwLoc()); |
| 7423 | auto ToLabelLoc = S->hasLabelTarget() |
| 7424 | ? NodeImporter.importChecked(Err, S->getLabelLoc()) |
| 7425 | : SourceLocation(); |
| 7426 | auto ToDecl = S->hasLabelTarget() |
| 7427 | ? NodeImporter.importChecked(Err, S->getLabelDecl()) |
| 7428 | : nullptr; |
| 7429 | if (Err) |
| 7430 | return std::move(Err); |
| 7431 | return new (Importer.getToContext()) StmtClass(ToLoc, ToLabelLoc, ToDecl); |
| 7432 | } |
| 7433 | |
| 7434 | ExpectedStmt ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) { |
| 7435 | return ImportLoopControlStmt(NodeImporter&: *this, Importer, S); |
| 7436 | } |
| 7437 | |
| 7438 | ExpectedStmt ASTNodeImporter::VisitBreakStmt(BreakStmt *S) { |
| 7439 | return ImportLoopControlStmt(NodeImporter&: *this, Importer, S); |
| 7440 | } |
| 7441 | |
| 7442 | ExpectedStmt ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) { |
| 7443 | |
| 7444 | Error Err = Error::success(); |
| 7445 | auto ToReturnLoc = importChecked(Err, From: S->getReturnLoc()); |
| 7446 | auto ToRetValue = importChecked(Err, From: S->getRetValue()); |
| 7447 | auto ToNRVOCandidate = importChecked(Err, From: S->getNRVOCandidate()); |
| 7448 | if (Err) |
| 7449 | return std::move(Err); |
| 7450 | |
| 7451 | return ReturnStmt::Create(Ctx: Importer.getToContext(), RL: ToReturnLoc, E: ToRetValue, |
| 7452 | NRVOCandidate: ToNRVOCandidate); |
| 7453 | } |
| 7454 | |
| 7455 | ExpectedStmt ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) { |
| 7456 | |
| 7457 | Error Err = Error::success(); |
| 7458 | auto ToCatchLoc = importChecked(Err, From: S->getCatchLoc()); |
| 7459 | auto ToExceptionDecl = importChecked(Err, From: S->getExceptionDecl()); |
| 7460 | auto ToHandlerBlock = importChecked(Err, From: S->getHandlerBlock()); |
| 7461 | if (Err) |
| 7462 | return std::move(Err); |
| 7463 | |
| 7464 | return new (Importer.getToContext()) CXXCatchStmt ( |
| 7465 | ToCatchLoc, ToExceptionDecl, ToHandlerBlock); |
| 7466 | } |
| 7467 | |
| 7468 | ExpectedStmt ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) { |
| 7469 | ExpectedSLoc ToTryLocOrErr = import(From: S->getTryLoc()); |
| 7470 | if (!ToTryLocOrErr) |
| 7471 | return ToTryLocOrErr.takeError(); |
| 7472 | |
| 7473 | ExpectedStmt ToTryBlockOrErr = import(From: S->getTryBlock()); |
| 7474 | if (!ToTryBlockOrErr) |
| 7475 | return ToTryBlockOrErr.takeError(); |
| 7476 | |
| 7477 | SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers()); |
| 7478 | for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) { |
| 7479 | CXXCatchStmt *FromHandler = S->getHandler(i: HI); |
| 7480 | if (auto ToHandlerOrErr = import(From: FromHandler)) |
| 7481 | ToHandlers[HI] = *ToHandlerOrErr; |
| 7482 | else |
| 7483 | return ToHandlerOrErr.takeError(); |
| 7484 | } |
| 7485 | |
| 7486 | return CXXTryStmt::Create(C: Importer.getToContext(), tryLoc: *ToTryLocOrErr, |
| 7487 | tryBlock: cast<CompoundStmt>(Val: *ToTryBlockOrErr), handlers: ToHandlers); |
| 7488 | } |
| 7489 | |
| 7490 | ExpectedStmt ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) { |
| 7491 | |
| 7492 | Error Err = Error::success(); |
| 7493 | auto ToInit = importChecked(Err, From: S->getInit()); |
| 7494 | auto ToRangeStmt = importChecked(Err, From: S->getRangeStmt()); |
| 7495 | auto ToBeginStmt = importChecked(Err, From: S->getBeginStmt()); |
| 7496 | auto ToEndStmt = importChecked(Err, From: S->getEndStmt()); |
| 7497 | auto ToCond = importChecked(Err, From: S->getCond()); |
| 7498 | auto ToInc = importChecked(Err, From: S->getInc()); |
| 7499 | auto ToLoopVarStmt = importChecked(Err, From: S->getLoopVarStmt()); |
| 7500 | auto ToBody = importChecked(Err, From: S->getBody()); |
| 7501 | auto ToForLoc = importChecked(Err, From: S->getForLoc()); |
| 7502 | auto ToCoawaitLoc = importChecked(Err, From: S->getCoawaitLoc()); |
| 7503 | auto ToColonLoc = importChecked(Err, From: S->getColonLoc()); |
| 7504 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
| 7505 | if (Err) |
| 7506 | return std::move(Err); |
| 7507 | |
| 7508 | return new (Importer.getToContext()) CXXForRangeStmt( |
| 7509 | ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt, |
| 7510 | ToBody, ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc); |
| 7511 | } |
| 7512 | |
| 7513 | ExpectedStmt |
| 7514 | ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) { |
| 7515 | Error Err = Error::success(); |
| 7516 | auto ToElement = importChecked(Err, From: S->getElement()); |
| 7517 | auto ToCollection = importChecked(Err, From: S->getCollection()); |
| 7518 | auto ToBody = importChecked(Err, From: S->getBody()); |
| 7519 | auto ToForLoc = importChecked(Err, From: S->getForLoc()); |
| 7520 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
| 7521 | if (Err) |
| 7522 | return std::move(Err); |
| 7523 | |
| 7524 | return new (Importer.getToContext()) ObjCForCollectionStmt(ToElement, |
| 7525 | ToCollection, |
| 7526 | ToBody, |
| 7527 | ToForLoc, |
| 7528 | ToRParenLoc); |
| 7529 | } |
| 7530 | |
| 7531 | ExpectedStmt ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) { |
| 7532 | |
| 7533 | Error Err = Error::success(); |
| 7534 | auto ToAtCatchLoc = importChecked(Err, From: S->getAtCatchLoc()); |
| 7535 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
| 7536 | auto ToCatchParamDecl = importChecked(Err, From: S->getCatchParamDecl()); |
| 7537 | auto ToCatchBody = importChecked(Err, From: S->getCatchBody()); |
| 7538 | if (Err) |
| 7539 | return std::move(Err); |
| 7540 | |
| 7541 | return new (Importer.getToContext()) ObjCAtCatchStmt ( |
| 7542 | ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody); |
| 7543 | } |
| 7544 | |
| 7545 | ExpectedStmt ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) { |
| 7546 | ExpectedSLoc ToAtFinallyLocOrErr = import(From: S->getAtFinallyLoc()); |
| 7547 | if (!ToAtFinallyLocOrErr) |
| 7548 | return ToAtFinallyLocOrErr.takeError(); |
| 7549 | ExpectedStmt ToAtFinallyStmtOrErr = import(From: S->getFinallyBody()); |
| 7550 | if (!ToAtFinallyStmtOrErr) |
| 7551 | return ToAtFinallyStmtOrErr.takeError(); |
| 7552 | return new (Importer.getToContext()) ObjCAtFinallyStmt(*ToAtFinallyLocOrErr, |
| 7553 | *ToAtFinallyStmtOrErr); |
| 7554 | } |
| 7555 | |
| 7556 | ExpectedStmt ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) { |
| 7557 | |
| 7558 | Error Err = Error::success(); |
| 7559 | auto ToAtTryLoc = importChecked(Err, From: S->getAtTryLoc()); |
| 7560 | auto ToTryBody = importChecked(Err, From: S->getTryBody()); |
| 7561 | auto ToFinallyStmt = importChecked(Err, From: S->getFinallyStmt()); |
| 7562 | if (Err) |
| 7563 | return std::move(Err); |
| 7564 | |
| 7565 | SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts()); |
| 7566 | for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) { |
| 7567 | ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(I: CI); |
| 7568 | if (ExpectedStmt ToCatchStmtOrErr = import(From: FromCatchStmt)) |
| 7569 | ToCatchStmts[CI] = *ToCatchStmtOrErr; |
| 7570 | else |
| 7571 | return ToCatchStmtOrErr.takeError(); |
| 7572 | } |
| 7573 | |
| 7574 | return ObjCAtTryStmt::Create(Context: Importer.getToContext(), |
| 7575 | atTryLoc: ToAtTryLoc, atTryStmt: ToTryBody, |
| 7576 | CatchStmts: ToCatchStmts.begin(), NumCatchStmts: ToCatchStmts.size(), |
| 7577 | atFinallyStmt: ToFinallyStmt); |
| 7578 | } |
| 7579 | |
| 7580 | ExpectedStmt |
| 7581 | ASTNodeImporter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
| 7582 | |
| 7583 | Error Err = Error::success(); |
| 7584 | auto ToAtSynchronizedLoc = importChecked(Err, From: S->getAtSynchronizedLoc()); |
| 7585 | auto ToSynchExpr = importChecked(Err, From: S->getSynchExpr()); |
| 7586 | auto ToSynchBody = importChecked(Err, From: S->getSynchBody()); |
| 7587 | if (Err) |
| 7588 | return std::move(Err); |
| 7589 | |
| 7590 | return new (Importer.getToContext()) ObjCAtSynchronizedStmt( |
| 7591 | ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody); |
| 7592 | } |
| 7593 | |
| 7594 | ExpectedStmt ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) { |
| 7595 | ExpectedSLoc ToThrowLocOrErr = import(From: S->getThrowLoc()); |
| 7596 | if (!ToThrowLocOrErr) |
| 7597 | return ToThrowLocOrErr.takeError(); |
| 7598 | ExpectedExpr ToThrowExprOrErr = import(From: S->getThrowExpr()); |
| 7599 | if (!ToThrowExprOrErr) |
| 7600 | return ToThrowExprOrErr.takeError(); |
| 7601 | return new (Importer.getToContext()) ObjCAtThrowStmt( |
| 7602 | *ToThrowLocOrErr, *ToThrowExprOrErr); |
| 7603 | } |
| 7604 | |
| 7605 | ExpectedStmt ASTNodeImporter::VisitObjCAutoreleasePoolStmt( |
| 7606 | ObjCAutoreleasePoolStmt *S) { |
| 7607 | ExpectedSLoc ToAtLocOrErr = import(From: S->getAtLoc()); |
| 7608 | if (!ToAtLocOrErr) |
| 7609 | return ToAtLocOrErr.takeError(); |
| 7610 | ExpectedStmt ToSubStmtOrErr = import(From: S->getSubStmt()); |
| 7611 | if (!ToSubStmtOrErr) |
| 7612 | return ToSubStmtOrErr.takeError(); |
| 7613 | return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(*ToAtLocOrErr, |
| 7614 | *ToSubStmtOrErr); |
| 7615 | } |
| 7616 | |
| 7617 | //---------------------------------------------------------------------------- |
| 7618 | // Import Expressions |
| 7619 | //---------------------------------------------------------------------------- |
| 7620 | ExpectedStmt ASTNodeImporter::VisitExpr(Expr *E) { |
| 7621 | Importer.FromDiag(Loc: E->getBeginLoc(), DiagID: diag::err_unsupported_ast_node) |
| 7622 | << E->getStmtClassName(); |
| 7623 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
| 7624 | } |
| 7625 | |
| 7626 | ExpectedStmt ASTNodeImporter::VisitSourceLocExpr(SourceLocExpr *E) { |
| 7627 | Error Err = Error::success(); |
| 7628 | auto ToType = importChecked(Err, From: E->getType()); |
| 7629 | auto BLoc = importChecked(Err, From: E->getBeginLoc()); |
| 7630 | auto RParenLoc = importChecked(Err, From: E->getEndLoc()); |
| 7631 | if (Err) |
| 7632 | return std::move(Err); |
| 7633 | auto ParentContextOrErr = Importer.ImportContext(FromDC: E->getParentContext()); |
| 7634 | if (!ParentContextOrErr) |
| 7635 | return ParentContextOrErr.takeError(); |
| 7636 | |
| 7637 | return new (Importer.getToContext()) |
| 7638 | SourceLocExpr(Importer.getToContext(), E->getIdentKind(), ToType, BLoc, |
| 7639 | RParenLoc, *ParentContextOrErr); |
| 7640 | } |
| 7641 | |
| 7642 | ExpectedStmt ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) { |
| 7643 | |
| 7644 | Error Err = Error::success(); |
| 7645 | auto ToBuiltinLoc = importChecked(Err, From: E->getBuiltinLoc()); |
| 7646 | auto ToSubExpr = importChecked(Err, From: E->getSubExpr()); |
| 7647 | auto ToWrittenTypeInfo = importChecked(Err, From: E->getWrittenTypeInfo()); |
| 7648 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 7649 | auto ToType = importChecked(Err, From: E->getType()); |
| 7650 | if (Err) |
| 7651 | return std::move(Err); |
| 7652 | |
| 7653 | return new (Importer.getToContext()) VAArgExpr( |
| 7654 | ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType, |
| 7655 | E->isMicrosoftABI()); |
| 7656 | } |
| 7657 | |
| 7658 | ExpectedStmt ASTNodeImporter::VisitChooseExpr(ChooseExpr *E) { |
| 7659 | |
| 7660 | Error Err = Error::success(); |
| 7661 | auto ToCond = importChecked(Err, From: E->getCond()); |
| 7662 | auto ToLHS = importChecked(Err, From: E->getLHS()); |
| 7663 | auto ToRHS = importChecked(Err, From: E->getRHS()); |
| 7664 | auto ToBuiltinLoc = importChecked(Err, From: E->getBuiltinLoc()); |
| 7665 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 7666 | auto ToType = importChecked(Err, From: E->getType()); |
| 7667 | if (Err) |
| 7668 | return std::move(Err); |
| 7669 | |
| 7670 | ExprValueKind VK = E->getValueKind(); |
| 7671 | ExprObjectKind OK = E->getObjectKind(); |
| 7672 | |
| 7673 | // The value of CondIsTrue only matters if the value is not |
| 7674 | // condition-dependent. |
| 7675 | bool CondIsTrue = !E->isConditionDependent() && E->isConditionTrue(); |
| 7676 | |
| 7677 | return new (Importer.getToContext()) |
| 7678 | ChooseExpr(ToBuiltinLoc, ToCond, ToLHS, ToRHS, ToType, VK, OK, |
| 7679 | ToRParenLoc, CondIsTrue); |
| 7680 | } |
| 7681 | |
| 7682 | ExpectedStmt ASTNodeImporter::VisitConvertVectorExpr(ConvertVectorExpr *E) { |
| 7683 | Error Err = Error::success(); |
| 7684 | auto *ToSrcExpr = importChecked(Err, From: E->getSrcExpr()); |
| 7685 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 7686 | auto ToBuiltinLoc = importChecked(Err, From: E->getBuiltinLoc()); |
| 7687 | auto ToType = importChecked(Err, From: E->getType()); |
| 7688 | auto *ToTSI = importChecked(Err, From: E->getTypeSourceInfo()); |
| 7689 | if (Err) |
| 7690 | return std::move(Err); |
| 7691 | |
| 7692 | return ConvertVectorExpr::Create( |
| 7693 | C: Importer.getToContext(), SrcExpr: ToSrcExpr, TI: ToTSI, DstType: ToType, VK: E->getValueKind(), |
| 7694 | OK: E->getObjectKind(), BuiltinLoc: ToBuiltinLoc, RParenLoc: ToRParenLoc, |
| 7695 | FPFeatures: E->getStoredFPFeaturesOrDefault()); |
| 7696 | } |
| 7697 | |
| 7698 | ExpectedStmt ASTNodeImporter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) { |
| 7699 | Error Err = Error::success(); |
| 7700 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 7701 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
| 7702 | auto ToType = importChecked(Err, From: E->getType()); |
| 7703 | const unsigned NumSubExprs = E->getNumSubExprs(); |
| 7704 | |
| 7705 | llvm::SmallVector<Expr *, 8> ToSubExprs; |
| 7706 | ArrayRef<Expr *> FromSubExprs(E->getSubExprs(), NumSubExprs); |
| 7707 | ToSubExprs.resize(N: NumSubExprs); |
| 7708 | |
| 7709 | if ((Err = ImportContainerChecked(InContainer: FromSubExprs, OutContainer&: ToSubExprs))) |
| 7710 | return std::move(Err); |
| 7711 | |
| 7712 | return new (Importer.getToContext()) ShuffleVectorExpr( |
| 7713 | Importer.getToContext(), ToSubExprs, ToType, ToBeginLoc, ToRParenLoc); |
| 7714 | } |
| 7715 | |
| 7716 | ExpectedStmt ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) { |
| 7717 | ExpectedType TypeOrErr = import(From: E->getType()); |
| 7718 | if (!TypeOrErr) |
| 7719 | return TypeOrErr.takeError(); |
| 7720 | |
| 7721 | ExpectedSLoc BeginLocOrErr = import(From: E->getBeginLoc()); |
| 7722 | if (!BeginLocOrErr) |
| 7723 | return BeginLocOrErr.takeError(); |
| 7724 | |
| 7725 | return new (Importer.getToContext()) GNUNullExpr(*TypeOrErr, *BeginLocOrErr); |
| 7726 | } |
| 7727 | |
| 7728 | ExpectedStmt |
| 7729 | ASTNodeImporter::VisitGenericSelectionExpr(GenericSelectionExpr *E) { |
| 7730 | Error Err = Error::success(); |
| 7731 | auto ToGenericLoc = importChecked(Err, From: E->getGenericLoc()); |
| 7732 | Expr *ToControllingExpr = nullptr; |
| 7733 | TypeSourceInfo *ToControllingType = nullptr; |
| 7734 | if (E->isExprPredicate()) |
| 7735 | ToControllingExpr = importChecked(Err, From: E->getControllingExpr()); |
| 7736 | else |
| 7737 | ToControllingType = importChecked(Err, From: E->getControllingType()); |
| 7738 | assert((ToControllingExpr || ToControllingType) && |
| 7739 | "Either the controlling expr or type must be nonnull" ); |
| 7740 | auto ToDefaultLoc = importChecked(Err, From: E->getDefaultLoc()); |
| 7741 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 7742 | if (Err) |
| 7743 | return std::move(Err); |
| 7744 | |
| 7745 | ArrayRef<const TypeSourceInfo *> FromAssocTypes(E->getAssocTypeSourceInfos()); |
| 7746 | SmallVector<TypeSourceInfo *, 1> ToAssocTypes(FromAssocTypes.size()); |
| 7747 | if (Error Err = ImportContainerChecked(InContainer: FromAssocTypes, OutContainer&: ToAssocTypes)) |
| 7748 | return std::move(Err); |
| 7749 | |
| 7750 | ArrayRef<const Expr *> FromAssocExprs(E->getAssocExprs()); |
| 7751 | SmallVector<Expr *, 1> ToAssocExprs(FromAssocExprs.size()); |
| 7752 | if (Error Err = ImportContainerChecked(InContainer: FromAssocExprs, OutContainer&: ToAssocExprs)) |
| 7753 | return std::move(Err); |
| 7754 | |
| 7755 | const ASTContext &ToCtx = Importer.getToContext(); |
| 7756 | if (E->isResultDependent()) { |
| 7757 | if (ToControllingExpr) { |
| 7758 | return GenericSelectionExpr::Create( |
| 7759 | Context: ToCtx, GenericLoc: ToGenericLoc, ControllingExpr: ToControllingExpr, AssocTypes: ArrayRef(ToAssocTypes), |
| 7760 | AssocExprs: ArrayRef(ToAssocExprs), DefaultLoc: ToDefaultLoc, RParenLoc: ToRParenLoc, |
| 7761 | ContainsUnexpandedParameterPack: E->containsUnexpandedParameterPack()); |
| 7762 | } |
| 7763 | return GenericSelectionExpr::Create( |
| 7764 | Context: ToCtx, GenericLoc: ToGenericLoc, ControllingType: ToControllingType, AssocTypes: ArrayRef(ToAssocTypes), |
| 7765 | AssocExprs: ArrayRef(ToAssocExprs), DefaultLoc: ToDefaultLoc, RParenLoc: ToRParenLoc, |
| 7766 | ContainsUnexpandedParameterPack: E->containsUnexpandedParameterPack()); |
| 7767 | } |
| 7768 | |
| 7769 | if (ToControllingExpr) { |
| 7770 | return GenericSelectionExpr::Create( |
| 7771 | Context: ToCtx, GenericLoc: ToGenericLoc, ControllingExpr: ToControllingExpr, AssocTypes: ArrayRef(ToAssocTypes), |
| 7772 | AssocExprs: ArrayRef(ToAssocExprs), DefaultLoc: ToDefaultLoc, RParenLoc: ToRParenLoc, |
| 7773 | ContainsUnexpandedParameterPack: E->containsUnexpandedParameterPack(), ResultIndex: E->getResultIndex()); |
| 7774 | } |
| 7775 | return GenericSelectionExpr::Create( |
| 7776 | Context: ToCtx, GenericLoc: ToGenericLoc, ControllingType: ToControllingType, AssocTypes: ArrayRef(ToAssocTypes), |
| 7777 | AssocExprs: ArrayRef(ToAssocExprs), DefaultLoc: ToDefaultLoc, RParenLoc: ToRParenLoc, |
| 7778 | ContainsUnexpandedParameterPack: E->containsUnexpandedParameterPack(), ResultIndex: E->getResultIndex()); |
| 7779 | } |
| 7780 | |
| 7781 | ExpectedStmt ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) { |
| 7782 | |
| 7783 | Error Err = Error::success(); |
| 7784 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
| 7785 | auto ToType = importChecked(Err, From: E->getType()); |
| 7786 | auto ToFunctionName = importChecked(Err, From: E->getFunctionName()); |
| 7787 | if (Err) |
| 7788 | return std::move(Err); |
| 7789 | |
| 7790 | return PredefinedExpr::Create(Ctx: Importer.getToContext(), L: ToBeginLoc, FNTy: ToType, |
| 7791 | IK: E->getIdentKind(), IsTransparent: E->isTransparent(), |
| 7792 | SL: ToFunctionName); |
| 7793 | } |
| 7794 | |
| 7795 | ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) { |
| 7796 | |
| 7797 | Error Err = Error::success(); |
| 7798 | auto ToQualifierLoc = importChecked(Err, From: E->getQualifierLoc()); |
| 7799 | auto ToTemplateKeywordLoc = importChecked(Err, From: E->getTemplateKeywordLoc()); |
| 7800 | auto ToDecl = importChecked(Err, From: E->getDecl()); |
| 7801 | auto ToLocation = importChecked(Err, From: E->getLocation()); |
| 7802 | auto ToType = importChecked(Err, From: E->getType()); |
| 7803 | if (Err) |
| 7804 | return std::move(Err); |
| 7805 | |
| 7806 | NamedDecl *ToFoundD = nullptr; |
| 7807 | if (E->getDecl() != E->getFoundDecl()) { |
| 7808 | auto FoundDOrErr = import(From: E->getFoundDecl()); |
| 7809 | if (!FoundDOrErr) |
| 7810 | return FoundDOrErr.takeError(); |
| 7811 | ToFoundD = *FoundDOrErr; |
| 7812 | } |
| 7813 | |
| 7814 | TemplateArgumentListInfo ToTAInfo; |
| 7815 | TemplateArgumentListInfo *ToResInfo = nullptr; |
| 7816 | if (E->hasExplicitTemplateArgs()) { |
| 7817 | if (Error Err = |
| 7818 | ImportTemplateArgumentListInfo(FromLAngleLoc: E->getLAngleLoc(), FromRAngleLoc: E->getRAngleLoc(), |
| 7819 | Container: E->template_arguments(), Result&: ToTAInfo)) |
| 7820 | return std::move(Err); |
| 7821 | ToResInfo = &ToTAInfo; |
| 7822 | } |
| 7823 | |
| 7824 | auto *ToE = DeclRefExpr::Create( |
| 7825 | Context: Importer.getToContext(), QualifierLoc: ToQualifierLoc, TemplateKWLoc: ToTemplateKeywordLoc, D: ToDecl, |
| 7826 | RefersToEnclosingVariableOrCapture: E->refersToEnclosingVariableOrCapture(), NameLoc: ToLocation, T: ToType, |
| 7827 | VK: E->getValueKind(), FoundD: ToFoundD, TemplateArgs: ToResInfo, NOUR: E->isNonOdrUse()); |
| 7828 | if (E->hadMultipleCandidates()) |
| 7829 | ToE->setHadMultipleCandidates(true); |
| 7830 | ToE->setIsImmediateEscalating(E->isImmediateEscalating()); |
| 7831 | return ToE; |
| 7832 | } |
| 7833 | |
| 7834 | ExpectedStmt ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { |
| 7835 | ExpectedType TypeOrErr = import(From: E->getType()); |
| 7836 | if (!TypeOrErr) |
| 7837 | return TypeOrErr.takeError(); |
| 7838 | |
| 7839 | return new (Importer.getToContext()) ImplicitValueInitExpr(*TypeOrErr); |
| 7840 | } |
| 7841 | |
| 7842 | ExpectedStmt ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *E) { |
| 7843 | ExpectedExpr ToInitOrErr = import(From: E->getInit()); |
| 7844 | if (!ToInitOrErr) |
| 7845 | return ToInitOrErr.takeError(); |
| 7846 | |
| 7847 | ExpectedSLoc ToEqualOrColonLocOrErr = import(From: E->getEqualOrColonLoc()); |
| 7848 | if (!ToEqualOrColonLocOrErr) |
| 7849 | return ToEqualOrColonLocOrErr.takeError(); |
| 7850 | |
| 7851 | SmallVector<Expr *, 4> ToIndexExprs(E->getNumSubExprs() - 1); |
| 7852 | // List elements from the second, the first is Init itself |
| 7853 | for (unsigned I = 1, N = E->getNumSubExprs(); I < N; I++) { |
| 7854 | if (ExpectedExpr ToArgOrErr = import(From: E->getSubExpr(Idx: I))) |
| 7855 | ToIndexExprs[I - 1] = *ToArgOrErr; |
| 7856 | else |
| 7857 | return ToArgOrErr.takeError(); |
| 7858 | } |
| 7859 | |
| 7860 | SmallVector<Designator, 4> ToDesignators(E->size()); |
| 7861 | if (Error Err = ImportContainerChecked(InContainer: E->designators(), OutContainer&: ToDesignators)) |
| 7862 | return std::move(Err); |
| 7863 | |
| 7864 | return DesignatedInitExpr::Create( |
| 7865 | C: Importer.getToContext(), Designators: ToDesignators, |
| 7866 | IndexExprs: ToIndexExprs, EqualOrColonLoc: *ToEqualOrColonLocOrErr, |
| 7867 | GNUSyntax: E->usesGNUSyntax(), Init: *ToInitOrErr); |
| 7868 | } |
| 7869 | |
| 7870 | ExpectedStmt |
| 7871 | ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) { |
| 7872 | ExpectedType ToTypeOrErr = import(From: E->getType()); |
| 7873 | if (!ToTypeOrErr) |
| 7874 | return ToTypeOrErr.takeError(); |
| 7875 | |
| 7876 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
| 7877 | if (!ToLocationOrErr) |
| 7878 | return ToLocationOrErr.takeError(); |
| 7879 | |
| 7880 | return new (Importer.getToContext()) CXXNullPtrLiteralExpr( |
| 7881 | *ToTypeOrErr, *ToLocationOrErr); |
| 7882 | } |
| 7883 | |
| 7884 | ExpectedStmt ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) { |
| 7885 | ExpectedType ToTypeOrErr = import(From: E->getType()); |
| 7886 | if (!ToTypeOrErr) |
| 7887 | return ToTypeOrErr.takeError(); |
| 7888 | |
| 7889 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
| 7890 | if (!ToLocationOrErr) |
| 7891 | return ToLocationOrErr.takeError(); |
| 7892 | |
| 7893 | return IntegerLiteral::Create( |
| 7894 | C: Importer.getToContext(), V: E->getValue(), type: *ToTypeOrErr, l: *ToLocationOrErr); |
| 7895 | } |
| 7896 | |
| 7897 | |
| 7898 | ExpectedStmt ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) { |
| 7899 | ExpectedType ToTypeOrErr = import(From: E->getType()); |
| 7900 | if (!ToTypeOrErr) |
| 7901 | return ToTypeOrErr.takeError(); |
| 7902 | |
| 7903 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
| 7904 | if (!ToLocationOrErr) |
| 7905 | return ToLocationOrErr.takeError(); |
| 7906 | |
| 7907 | return FloatingLiteral::Create( |
| 7908 | C: Importer.getToContext(), V: E->getValue(), isexact: E->isExact(), |
| 7909 | Type: *ToTypeOrErr, L: *ToLocationOrErr); |
| 7910 | } |
| 7911 | |
| 7912 | ExpectedStmt ASTNodeImporter::VisitImaginaryLiteral(ImaginaryLiteral *E) { |
| 7913 | auto ToTypeOrErr = import(From: E->getType()); |
| 7914 | if (!ToTypeOrErr) |
| 7915 | return ToTypeOrErr.takeError(); |
| 7916 | |
| 7917 | ExpectedExpr ToSubExprOrErr = import(From: E->getSubExpr()); |
| 7918 | if (!ToSubExprOrErr) |
| 7919 | return ToSubExprOrErr.takeError(); |
| 7920 | |
| 7921 | return new (Importer.getToContext()) ImaginaryLiteral( |
| 7922 | *ToSubExprOrErr, *ToTypeOrErr); |
| 7923 | } |
| 7924 | |
| 7925 | ExpectedStmt ASTNodeImporter::VisitFixedPointLiteral(FixedPointLiteral *E) { |
| 7926 | auto ToTypeOrErr = import(From: E->getType()); |
| 7927 | if (!ToTypeOrErr) |
| 7928 | return ToTypeOrErr.takeError(); |
| 7929 | |
| 7930 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
| 7931 | if (!ToLocationOrErr) |
| 7932 | return ToLocationOrErr.takeError(); |
| 7933 | |
| 7934 | return new (Importer.getToContext()) FixedPointLiteral( |
| 7935 | Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr, |
| 7936 | Importer.getToContext().getFixedPointScale(Ty: *ToTypeOrErr)); |
| 7937 | } |
| 7938 | |
| 7939 | ExpectedStmt ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) { |
| 7940 | ExpectedType ToTypeOrErr = import(From: E->getType()); |
| 7941 | if (!ToTypeOrErr) |
| 7942 | return ToTypeOrErr.takeError(); |
| 7943 | |
| 7944 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
| 7945 | if (!ToLocationOrErr) |
| 7946 | return ToLocationOrErr.takeError(); |
| 7947 | |
| 7948 | return new (Importer.getToContext()) CharacterLiteral( |
| 7949 | E->getValue(), E->getKind(), *ToTypeOrErr, *ToLocationOrErr); |
| 7950 | } |
| 7951 | |
| 7952 | ExpectedStmt ASTNodeImporter::VisitStringLiteral(StringLiteral *E) { |
| 7953 | ExpectedType ToTypeOrErr = import(From: E->getType()); |
| 7954 | if (!ToTypeOrErr) |
| 7955 | return ToTypeOrErr.takeError(); |
| 7956 | |
| 7957 | SmallVector<SourceLocation, 4> ToLocations(E->getNumConcatenated()); |
| 7958 | if (Error Err = ImportArrayChecked( |
| 7959 | Ibegin: E->tokloc_begin(), Iend: E->tokloc_end(), Obegin: ToLocations.begin())) |
| 7960 | return std::move(Err); |
| 7961 | |
| 7962 | return StringLiteral::Create(Ctx: Importer.getToContext(), Str: E->getBytes(), |
| 7963 | Kind: E->getKind(), Pascal: E->isPascal(), Ty: *ToTypeOrErr, |
| 7964 | Locs: ToLocations); |
| 7965 | } |
| 7966 | |
| 7967 | ExpectedStmt ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 7968 | |
| 7969 | Error Err = Error::success(); |
| 7970 | auto ToLParenLoc = importChecked(Err, From: E->getLParenLoc()); |
| 7971 | auto ToTypeSourceInfo = importChecked(Err, From: E->getTypeSourceInfo()); |
| 7972 | auto ToType = importChecked(Err, From: E->getType()); |
| 7973 | auto ToInitializer = importChecked(Err, From: E->getInitializer()); |
| 7974 | if (Err) |
| 7975 | return std::move(Err); |
| 7976 | |
| 7977 | return new (Importer.getToContext()) CompoundLiteralExpr( |
| 7978 | ToLParenLoc, ToTypeSourceInfo, ToType, E->getValueKind(), |
| 7979 | ToInitializer, E->isFileScope()); |
| 7980 | } |
| 7981 | |
| 7982 | ExpectedStmt ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) { |
| 7983 | |
| 7984 | Error Err = Error::success(); |
| 7985 | auto ToBuiltinLoc = importChecked(Err, From: E->getBuiltinLoc()); |
| 7986 | auto ToType = importChecked(Err, From: E->getType()); |
| 7987 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 7988 | if (Err) |
| 7989 | return std::move(Err); |
| 7990 | |
| 7991 | SmallVector<Expr *, 6> ToExprs(E->getNumSubExprs()); |
| 7992 | if (Error Err = ImportArrayChecked( |
| 7993 | Ibegin: E->getSubExprs(), Iend: E->getSubExprs() + E->getNumSubExprs(), |
| 7994 | Obegin: ToExprs.begin())) |
| 7995 | return std::move(Err); |
| 7996 | |
| 7997 | return new (Importer.getToContext()) AtomicExpr( |
| 7998 | |
| 7999 | ToBuiltinLoc, ToExprs, ToType, E->getOp(), ToRParenLoc); |
| 8000 | } |
| 8001 | |
| 8002 | ExpectedStmt ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) { |
| 8003 | Error Err = Error::success(); |
| 8004 | auto ToAmpAmpLoc = importChecked(Err, From: E->getAmpAmpLoc()); |
| 8005 | auto ToLabelLoc = importChecked(Err, From: E->getLabelLoc()); |
| 8006 | auto ToLabel = importChecked(Err, From: E->getLabel()); |
| 8007 | auto ToType = importChecked(Err, From: E->getType()); |
| 8008 | if (Err) |
| 8009 | return std::move(Err); |
| 8010 | |
| 8011 | return new (Importer.getToContext()) AddrLabelExpr( |
| 8012 | ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType); |
| 8013 | } |
| 8014 | ExpectedStmt ASTNodeImporter::VisitConstantExpr(ConstantExpr *E) { |
| 8015 | Error Err = Error::success(); |
| 8016 | auto ToSubExpr = importChecked(Err, From: E->getSubExpr()); |
| 8017 | auto ToResult = importChecked(Err, From: E->getAPValueResult()); |
| 8018 | if (Err) |
| 8019 | return std::move(Err); |
| 8020 | |
| 8021 | return ConstantExpr::Create(Context: Importer.getToContext(), E: ToSubExpr, Result: ToResult); |
| 8022 | } |
| 8023 | ExpectedStmt ASTNodeImporter::VisitParenExpr(ParenExpr *E) { |
| 8024 | Error Err = Error::success(); |
| 8025 | auto ToLParen = importChecked(Err, From: E->getLParen()); |
| 8026 | auto ToRParen = importChecked(Err, From: E->getRParen()); |
| 8027 | auto ToSubExpr = importChecked(Err, From: E->getSubExpr()); |
| 8028 | if (Err) |
| 8029 | return std::move(Err); |
| 8030 | |
| 8031 | return new (Importer.getToContext()) |
| 8032 | ParenExpr(ToLParen, ToRParen, ToSubExpr); |
| 8033 | } |
| 8034 | |
| 8035 | ExpectedStmt ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) { |
| 8036 | SmallVector<Expr *, 4> ToExprs(E->getNumExprs()); |
| 8037 | if (Error Err = ImportContainerChecked(InContainer: E->exprs(), OutContainer&: ToExprs)) |
| 8038 | return std::move(Err); |
| 8039 | |
| 8040 | ExpectedSLoc ToLParenLocOrErr = import(From: E->getLParenLoc()); |
| 8041 | if (!ToLParenLocOrErr) |
| 8042 | return ToLParenLocOrErr.takeError(); |
| 8043 | |
| 8044 | ExpectedSLoc ToRParenLocOrErr = import(From: E->getRParenLoc()); |
| 8045 | if (!ToRParenLocOrErr) |
| 8046 | return ToRParenLocOrErr.takeError(); |
| 8047 | |
| 8048 | return ParenListExpr::Create(Ctx: Importer.getToContext(), LParenLoc: *ToLParenLocOrErr, |
| 8049 | Exprs: ToExprs, RParenLoc: *ToRParenLocOrErr); |
| 8050 | } |
| 8051 | |
| 8052 | ExpectedStmt ASTNodeImporter::VisitStmtExpr(StmtExpr *E) { |
| 8053 | Error Err = Error::success(); |
| 8054 | auto ToSubStmt = importChecked(Err, From: E->getSubStmt()); |
| 8055 | auto ToType = importChecked(Err, From: E->getType()); |
| 8056 | auto ToLParenLoc = importChecked(Err, From: E->getLParenLoc()); |
| 8057 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 8058 | if (Err) |
| 8059 | return std::move(Err); |
| 8060 | |
| 8061 | return new (Importer.getToContext()) |
| 8062 | StmtExpr(ToSubStmt, ToType, ToLParenLoc, ToRParenLoc, |
| 8063 | E->getTemplateDepth()); |
| 8064 | } |
| 8065 | |
| 8066 | ExpectedStmt ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) { |
| 8067 | Error Err = Error::success(); |
| 8068 | auto ToSubExpr = importChecked(Err, From: E->getSubExpr()); |
| 8069 | auto ToType = importChecked(Err, From: E->getType()); |
| 8070 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
| 8071 | if (Err) |
| 8072 | return std::move(Err); |
| 8073 | |
| 8074 | auto *UO = UnaryOperator::CreateEmpty(C: Importer.getToContext(), |
| 8075 | hasFPFeatures: E->hasStoredFPFeatures()); |
| 8076 | UO->setType(ToType); |
| 8077 | UO->setSubExpr(ToSubExpr); |
| 8078 | UO->setOpcode(E->getOpcode()); |
| 8079 | UO->setOperatorLoc(ToOperatorLoc); |
| 8080 | UO->setCanOverflow(E->canOverflow()); |
| 8081 | if (E->hasStoredFPFeatures()) |
| 8082 | UO->setStoredFPFeatures(E->getStoredFPFeatures()); |
| 8083 | |
| 8084 | return UO; |
| 8085 | } |
| 8086 | |
| 8087 | ExpectedStmt |
| 8088 | |
| 8089 | ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) { |
| 8090 | Error Err = Error::success(); |
| 8091 | auto ToType = importChecked(Err, From: E->getType()); |
| 8092 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
| 8093 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 8094 | if (Err) |
| 8095 | return std::move(Err); |
| 8096 | |
| 8097 | if (E->isArgumentType()) { |
| 8098 | Expected<TypeSourceInfo *> ToArgumentTypeInfoOrErr = |
| 8099 | import(From: E->getArgumentTypeInfo()); |
| 8100 | if (!ToArgumentTypeInfoOrErr) |
| 8101 | return ToArgumentTypeInfoOrErr.takeError(); |
| 8102 | |
| 8103 | return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr( |
| 8104 | E->getKind(), *ToArgumentTypeInfoOrErr, ToType, ToOperatorLoc, |
| 8105 | ToRParenLoc); |
| 8106 | } |
| 8107 | |
| 8108 | ExpectedExpr ToArgumentExprOrErr = import(From: E->getArgumentExpr()); |
| 8109 | if (!ToArgumentExprOrErr) |
| 8110 | return ToArgumentExprOrErr.takeError(); |
| 8111 | |
| 8112 | return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr( |
| 8113 | E->getKind(), *ToArgumentExprOrErr, ToType, ToOperatorLoc, ToRParenLoc); |
| 8114 | } |
| 8115 | |
| 8116 | ExpectedStmt ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) { |
| 8117 | Error Err = Error::success(); |
| 8118 | auto ToLHS = importChecked(Err, From: E->getLHS()); |
| 8119 | auto ToRHS = importChecked(Err, From: E->getRHS()); |
| 8120 | auto ToType = importChecked(Err, From: E->getType()); |
| 8121 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
| 8122 | if (Err) |
| 8123 | return std::move(Err); |
| 8124 | |
| 8125 | return BinaryOperator::Create( |
| 8126 | C: Importer.getToContext(), lhs: ToLHS, rhs: ToRHS, opc: E->getOpcode(), ResTy: ToType, |
| 8127 | VK: E->getValueKind(), OK: E->getObjectKind(), opLoc: ToOperatorLoc, |
| 8128 | FPFeatures: E->getFPFeatures()); |
| 8129 | } |
| 8130 | |
| 8131 | ExpectedStmt ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) { |
| 8132 | Error Err = Error::success(); |
| 8133 | auto ToCond = importChecked(Err, From: E->getCond()); |
| 8134 | auto ToQuestionLoc = importChecked(Err, From: E->getQuestionLoc()); |
| 8135 | auto ToLHS = importChecked(Err, From: E->getLHS()); |
| 8136 | auto ToColonLoc = importChecked(Err, From: E->getColonLoc()); |
| 8137 | auto ToRHS = importChecked(Err, From: E->getRHS()); |
| 8138 | auto ToType = importChecked(Err, From: E->getType()); |
| 8139 | if (Err) |
| 8140 | return std::move(Err); |
| 8141 | |
| 8142 | return new (Importer.getToContext()) ConditionalOperator( |
| 8143 | ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType, |
| 8144 | E->getValueKind(), E->getObjectKind()); |
| 8145 | } |
| 8146 | |
| 8147 | ExpectedStmt |
| 8148 | ASTNodeImporter::VisitBinaryConditionalOperator(BinaryConditionalOperator *E) { |
| 8149 | Error Err = Error::success(); |
| 8150 | auto ToCommon = importChecked(Err, From: E->getCommon()); |
| 8151 | auto ToOpaqueValue = importChecked(Err, From: E->getOpaqueValue()); |
| 8152 | auto ToCond = importChecked(Err, From: E->getCond()); |
| 8153 | auto ToTrueExpr = importChecked(Err, From: E->getTrueExpr()); |
| 8154 | auto ToFalseExpr = importChecked(Err, From: E->getFalseExpr()); |
| 8155 | auto ToQuestionLoc = importChecked(Err, From: E->getQuestionLoc()); |
| 8156 | auto ToColonLoc = importChecked(Err, From: E->getColonLoc()); |
| 8157 | auto ToType = importChecked(Err, From: E->getType()); |
| 8158 | if (Err) |
| 8159 | return std::move(Err); |
| 8160 | |
| 8161 | return new (Importer.getToContext()) BinaryConditionalOperator( |
| 8162 | ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr, |
| 8163 | ToQuestionLoc, ToColonLoc, ToType, E->getValueKind(), |
| 8164 | E->getObjectKind()); |
| 8165 | } |
| 8166 | |
| 8167 | ExpectedStmt ASTNodeImporter::VisitCXXRewrittenBinaryOperator( |
| 8168 | CXXRewrittenBinaryOperator *E) { |
| 8169 | Error Err = Error::success(); |
| 8170 | auto ToSemanticForm = importChecked(Err, From: E->getSemanticForm()); |
| 8171 | if (Err) |
| 8172 | return std::move(Err); |
| 8173 | |
| 8174 | return new (Importer.getToContext()) |
| 8175 | CXXRewrittenBinaryOperator(ToSemanticForm, E->isReversed()); |
| 8176 | } |
| 8177 | |
| 8178 | ExpectedStmt ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { |
| 8179 | Error Err = Error::success(); |
| 8180 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
| 8181 | auto ToQueriedTypeSourceInfo = |
| 8182 | importChecked(Err, From: E->getQueriedTypeSourceInfo()); |
| 8183 | auto ToDimensionExpression = importChecked(Err, From: E->getDimensionExpression()); |
| 8184 | auto ToEndLoc = importChecked(Err, From: E->getEndLoc()); |
| 8185 | auto ToType = importChecked(Err, From: E->getType()); |
| 8186 | if (Err) |
| 8187 | return std::move(Err); |
| 8188 | |
| 8189 | return new (Importer.getToContext()) ArrayTypeTraitExpr( |
| 8190 | ToBeginLoc, E->getTrait(), ToQueriedTypeSourceInfo, E->getValue(), |
| 8191 | ToDimensionExpression, ToEndLoc, ToType); |
| 8192 | } |
| 8193 | |
| 8194 | ExpectedStmt ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { |
| 8195 | Error Err = Error::success(); |
| 8196 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
| 8197 | auto ToQueriedExpression = importChecked(Err, From: E->getQueriedExpression()); |
| 8198 | auto ToEndLoc = importChecked(Err, From: E->getEndLoc()); |
| 8199 | auto ToType = importChecked(Err, From: E->getType()); |
| 8200 | if (Err) |
| 8201 | return std::move(Err); |
| 8202 | |
| 8203 | return new (Importer.getToContext()) ExpressionTraitExpr( |
| 8204 | ToBeginLoc, E->getTrait(), ToQueriedExpression, E->getValue(), |
| 8205 | ToEndLoc, ToType); |
| 8206 | } |
| 8207 | |
| 8208 | ExpectedStmt ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) { |
| 8209 | Error Err = Error::success(); |
| 8210 | auto ToLocation = importChecked(Err, From: E->getLocation()); |
| 8211 | auto ToType = importChecked(Err, From: E->getType()); |
| 8212 | auto ToSourceExpr = importChecked(Err, From: E->getSourceExpr()); |
| 8213 | if (Err) |
| 8214 | return std::move(Err); |
| 8215 | |
| 8216 | return new (Importer.getToContext()) OpaqueValueExpr( |
| 8217 | ToLocation, ToType, E->getValueKind(), E->getObjectKind(), ToSourceExpr); |
| 8218 | } |
| 8219 | |
| 8220 | ExpectedStmt ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { |
| 8221 | Error Err = Error::success(); |
| 8222 | auto ToLHS = importChecked(Err, From: E->getLHS()); |
| 8223 | auto ToRHS = importChecked(Err, From: E->getRHS()); |
| 8224 | auto ToType = importChecked(Err, From: E->getType()); |
| 8225 | auto ToRBracketLoc = importChecked(Err, From: E->getRBracketLoc()); |
| 8226 | if (Err) |
| 8227 | return std::move(Err); |
| 8228 | |
| 8229 | return new (Importer.getToContext()) ArraySubscriptExpr( |
| 8230 | ToLHS, ToRHS, ToType, E->getValueKind(), E->getObjectKind(), |
| 8231 | ToRBracketLoc); |
| 8232 | } |
| 8233 | |
| 8234 | ExpectedStmt |
| 8235 | ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) { |
| 8236 | Error Err = Error::success(); |
| 8237 | auto ToLHS = importChecked(Err, From: E->getLHS()); |
| 8238 | auto ToRHS = importChecked(Err, From: E->getRHS()); |
| 8239 | auto ToType = importChecked(Err, From: E->getType()); |
| 8240 | auto ToComputationLHSType = importChecked(Err, From: E->getComputationLHSType()); |
| 8241 | auto ToComputationResultType = |
| 8242 | importChecked(Err, From: E->getComputationResultType()); |
| 8243 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
| 8244 | if (Err) |
| 8245 | return std::move(Err); |
| 8246 | |
| 8247 | return CompoundAssignOperator::Create( |
| 8248 | C: Importer.getToContext(), lhs: ToLHS, rhs: ToRHS, opc: E->getOpcode(), ResTy: ToType, |
| 8249 | VK: E->getValueKind(), OK: E->getObjectKind(), opLoc: ToOperatorLoc, |
| 8250 | FPFeatures: E->getFPFeatures(), |
| 8251 | CompLHSType: ToComputationLHSType, CompResultType: ToComputationResultType); |
| 8252 | } |
| 8253 | |
| 8254 | Expected<CXXCastPath> |
| 8255 | ASTNodeImporter::ImportCastPath(CastExpr *CE) { |
| 8256 | CXXCastPath Path; |
| 8257 | for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) { |
| 8258 | if (auto SpecOrErr = import(From: *I)) |
| 8259 | Path.push_back(Elt: *SpecOrErr); |
| 8260 | else |
| 8261 | return SpecOrErr.takeError(); |
| 8262 | } |
| 8263 | return Path; |
| 8264 | } |
| 8265 | |
| 8266 | ExpectedStmt ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 8267 | ExpectedType ToTypeOrErr = import(From: E->getType()); |
| 8268 | if (!ToTypeOrErr) |
| 8269 | return ToTypeOrErr.takeError(); |
| 8270 | |
| 8271 | ExpectedExpr ToSubExprOrErr = import(From: E->getSubExpr()); |
| 8272 | if (!ToSubExprOrErr) |
| 8273 | return ToSubExprOrErr.takeError(); |
| 8274 | |
| 8275 | Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(CE: E); |
| 8276 | if (!ToBasePathOrErr) |
| 8277 | return ToBasePathOrErr.takeError(); |
| 8278 | |
| 8279 | return ImplicitCastExpr::Create( |
| 8280 | Context: Importer.getToContext(), T: *ToTypeOrErr, Kind: E->getCastKind(), Operand: *ToSubExprOrErr, |
| 8281 | BasePath: &(*ToBasePathOrErr), Cat: E->getValueKind(), FPO: E->getFPFeatures()); |
| 8282 | } |
| 8283 | |
| 8284 | ExpectedStmt ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) { |
| 8285 | Error Err = Error::success(); |
| 8286 | auto ToType = importChecked(Err, From: E->getType()); |
| 8287 | auto ToSubExpr = importChecked(Err, From: E->getSubExpr()); |
| 8288 | auto ToTypeInfoAsWritten = importChecked(Err, From: E->getTypeInfoAsWritten()); |
| 8289 | if (Err) |
| 8290 | return std::move(Err); |
| 8291 | |
| 8292 | Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(CE: E); |
| 8293 | if (!ToBasePathOrErr) |
| 8294 | return ToBasePathOrErr.takeError(); |
| 8295 | CXXCastPath *ToBasePath = &(*ToBasePathOrErr); |
| 8296 | |
| 8297 | switch (E->getStmtClass()) { |
| 8298 | case Stmt::CStyleCastExprClass: { |
| 8299 | auto *CCE = cast<CStyleCastExpr>(Val: E); |
| 8300 | ExpectedSLoc ToLParenLocOrErr = import(From: CCE->getLParenLoc()); |
| 8301 | if (!ToLParenLocOrErr) |
| 8302 | return ToLParenLocOrErr.takeError(); |
| 8303 | ExpectedSLoc ToRParenLocOrErr = import(From: CCE->getRParenLoc()); |
| 8304 | if (!ToRParenLocOrErr) |
| 8305 | return ToRParenLocOrErr.takeError(); |
| 8306 | return CStyleCastExpr::Create( |
| 8307 | Context: Importer.getToContext(), T: ToType, VK: E->getValueKind(), K: E->getCastKind(), |
| 8308 | Op: ToSubExpr, BasePath: ToBasePath, FPO: CCE->getFPFeatures(), WrittenTy: ToTypeInfoAsWritten, |
| 8309 | L: *ToLParenLocOrErr, R: *ToRParenLocOrErr); |
| 8310 | } |
| 8311 | |
| 8312 | case Stmt::CXXFunctionalCastExprClass: { |
| 8313 | auto *FCE = cast<CXXFunctionalCastExpr>(Val: E); |
| 8314 | ExpectedSLoc ToLParenLocOrErr = import(From: FCE->getLParenLoc()); |
| 8315 | if (!ToLParenLocOrErr) |
| 8316 | return ToLParenLocOrErr.takeError(); |
| 8317 | ExpectedSLoc ToRParenLocOrErr = import(From: FCE->getRParenLoc()); |
| 8318 | if (!ToRParenLocOrErr) |
| 8319 | return ToRParenLocOrErr.takeError(); |
| 8320 | return CXXFunctionalCastExpr::Create( |
| 8321 | Context: Importer.getToContext(), T: ToType, VK: E->getValueKind(), Written: ToTypeInfoAsWritten, |
| 8322 | Kind: E->getCastKind(), Op: ToSubExpr, Path: ToBasePath, FPO: FCE->getFPFeatures(), |
| 8323 | LPLoc: *ToLParenLocOrErr, RPLoc: *ToRParenLocOrErr); |
| 8324 | } |
| 8325 | |
| 8326 | case Stmt::ObjCBridgedCastExprClass: { |
| 8327 | auto *OCE = cast<ObjCBridgedCastExpr>(Val: E); |
| 8328 | ExpectedSLoc ToLParenLocOrErr = import(From: OCE->getLParenLoc()); |
| 8329 | if (!ToLParenLocOrErr) |
| 8330 | return ToLParenLocOrErr.takeError(); |
| 8331 | ExpectedSLoc ToBridgeKeywordLocOrErr = import(From: OCE->getBridgeKeywordLoc()); |
| 8332 | if (!ToBridgeKeywordLocOrErr) |
| 8333 | return ToBridgeKeywordLocOrErr.takeError(); |
| 8334 | return new (Importer.getToContext()) ObjCBridgedCastExpr( |
| 8335 | *ToLParenLocOrErr, OCE->getBridgeKind(), E->getCastKind(), |
| 8336 | *ToBridgeKeywordLocOrErr, ToTypeInfoAsWritten, ToSubExpr); |
| 8337 | } |
| 8338 | case Stmt::BuiltinBitCastExprClass: { |
| 8339 | auto *BBC = cast<BuiltinBitCastExpr>(Val: E); |
| 8340 | ExpectedSLoc ToKWLocOrErr = import(From: BBC->getBeginLoc()); |
| 8341 | if (!ToKWLocOrErr) |
| 8342 | return ToKWLocOrErr.takeError(); |
| 8343 | ExpectedSLoc ToRParenLocOrErr = import(From: BBC->getEndLoc()); |
| 8344 | if (!ToRParenLocOrErr) |
| 8345 | return ToRParenLocOrErr.takeError(); |
| 8346 | return new (Importer.getToContext()) BuiltinBitCastExpr( |
| 8347 | ToType, E->getValueKind(), E->getCastKind(), ToSubExpr, |
| 8348 | ToTypeInfoAsWritten, *ToKWLocOrErr, *ToRParenLocOrErr); |
| 8349 | } |
| 8350 | default: |
| 8351 | llvm_unreachable("Cast expression of unsupported type!" ); |
| 8352 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
| 8353 | } |
| 8354 | } |
| 8355 | |
| 8356 | ExpectedStmt ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *E) { |
| 8357 | SmallVector<OffsetOfNode, 4> ToNodes; |
| 8358 | for (int I = 0, N = E->getNumComponents(); I < N; ++I) { |
| 8359 | const OffsetOfNode &FromNode = E->getComponent(Idx: I); |
| 8360 | |
| 8361 | SourceLocation ToBeginLoc, ToEndLoc; |
| 8362 | |
| 8363 | if (FromNode.getKind() != OffsetOfNode::Base) { |
| 8364 | Error Err = Error::success(); |
| 8365 | ToBeginLoc = importChecked(Err, From: FromNode.getBeginLoc()); |
| 8366 | ToEndLoc = importChecked(Err, From: FromNode.getEndLoc()); |
| 8367 | if (Err) |
| 8368 | return std::move(Err); |
| 8369 | } |
| 8370 | |
| 8371 | switch (FromNode.getKind()) { |
| 8372 | case OffsetOfNode::Array: |
| 8373 | ToNodes.push_back( |
| 8374 | Elt: OffsetOfNode(ToBeginLoc, FromNode.getArrayExprIndex(), ToEndLoc)); |
| 8375 | break; |
| 8376 | case OffsetOfNode::Base: { |
| 8377 | auto ToBSOrErr = import(From: FromNode.getBase()); |
| 8378 | if (!ToBSOrErr) |
| 8379 | return ToBSOrErr.takeError(); |
| 8380 | ToNodes.push_back(Elt: OffsetOfNode(*ToBSOrErr)); |
| 8381 | break; |
| 8382 | } |
| 8383 | case OffsetOfNode::Field: { |
| 8384 | auto ToFieldOrErr = import(From: FromNode.getField()); |
| 8385 | if (!ToFieldOrErr) |
| 8386 | return ToFieldOrErr.takeError(); |
| 8387 | ToNodes.push_back(Elt: OffsetOfNode(ToBeginLoc, *ToFieldOrErr, ToEndLoc)); |
| 8388 | break; |
| 8389 | } |
| 8390 | case OffsetOfNode::Identifier: { |
| 8391 | IdentifierInfo *ToII = Importer.Import(FromId: FromNode.getFieldName()); |
| 8392 | ToNodes.push_back(Elt: OffsetOfNode(ToBeginLoc, ToII, ToEndLoc)); |
| 8393 | break; |
| 8394 | } |
| 8395 | } |
| 8396 | } |
| 8397 | |
| 8398 | SmallVector<Expr *, 4> ToExprs(E->getNumExpressions()); |
| 8399 | for (int I = 0, N = E->getNumExpressions(); I < N; ++I) { |
| 8400 | ExpectedExpr ToIndexExprOrErr = import(From: E->getIndexExpr(Idx: I)); |
| 8401 | if (!ToIndexExprOrErr) |
| 8402 | return ToIndexExprOrErr.takeError(); |
| 8403 | ToExprs[I] = *ToIndexExprOrErr; |
| 8404 | } |
| 8405 | |
| 8406 | Error Err = Error::success(); |
| 8407 | auto ToType = importChecked(Err, From: E->getType()); |
| 8408 | auto ToTypeSourceInfo = importChecked(Err, From: E->getTypeSourceInfo()); |
| 8409 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
| 8410 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 8411 | if (Err) |
| 8412 | return std::move(Err); |
| 8413 | |
| 8414 | return OffsetOfExpr::Create( |
| 8415 | C: Importer.getToContext(), type: ToType, OperatorLoc: ToOperatorLoc, tsi: ToTypeSourceInfo, comps: ToNodes, |
| 8416 | exprs: ToExprs, RParenLoc: ToRParenLoc); |
| 8417 | } |
| 8418 | |
| 8419 | ExpectedStmt ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { |
| 8420 | Error Err = Error::success(); |
| 8421 | auto ToType = importChecked(Err, From: E->getType()); |
| 8422 | auto ToOperand = importChecked(Err, From: E->getOperand()); |
| 8423 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
| 8424 | auto ToEndLoc = importChecked(Err, From: E->getEndLoc()); |
| 8425 | if (Err) |
| 8426 | return std::move(Err); |
| 8427 | |
| 8428 | CanThrowResult ToCanThrow; |
| 8429 | if (E->isValueDependent()) |
| 8430 | ToCanThrow = CT_Dependent; |
| 8431 | else |
| 8432 | ToCanThrow = E->getValue() ? CT_Can : CT_Cannot; |
| 8433 | |
| 8434 | return new (Importer.getToContext()) CXXNoexceptExpr( |
| 8435 | ToType, ToOperand, ToCanThrow, ToBeginLoc, ToEndLoc); |
| 8436 | } |
| 8437 | |
| 8438 | ExpectedStmt ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) { |
| 8439 | Error Err = Error::success(); |
| 8440 | auto ToSubExpr = importChecked(Err, From: E->getSubExpr()); |
| 8441 | auto ToType = importChecked(Err, From: E->getType()); |
| 8442 | auto ToThrowLoc = importChecked(Err, From: E->getThrowLoc()); |
| 8443 | if (Err) |
| 8444 | return std::move(Err); |
| 8445 | |
| 8446 | return new (Importer.getToContext()) CXXThrowExpr( |
| 8447 | ToSubExpr, ToType, ToThrowLoc, E->isThrownVariableInScope()); |
| 8448 | } |
| 8449 | |
| 8450 | ExpectedStmt ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { |
| 8451 | ExpectedSLoc ToUsedLocOrErr = import(From: E->getUsedLocation()); |
| 8452 | if (!ToUsedLocOrErr) |
| 8453 | return ToUsedLocOrErr.takeError(); |
| 8454 | |
| 8455 | auto ToParamOrErr = import(From: E->getParam()); |
| 8456 | if (!ToParamOrErr) |
| 8457 | return ToParamOrErr.takeError(); |
| 8458 | |
| 8459 | auto UsedContextOrErr = Importer.ImportContext(FromDC: E->getUsedContext()); |
| 8460 | if (!UsedContextOrErr) |
| 8461 | return UsedContextOrErr.takeError(); |
| 8462 | |
| 8463 | // Import the default arg if it was not imported yet. |
| 8464 | // This is needed because it can happen that during the import of the |
| 8465 | // default expression (from VisitParmVarDecl) the same ParmVarDecl is |
| 8466 | // encountered here. The default argument for a ParmVarDecl is set in the |
| 8467 | // ParmVarDecl only after it is imported (set in VisitParmVarDecl if not here, |
| 8468 | // see VisitParmVarDecl). |
| 8469 | ParmVarDecl *ToParam = *ToParamOrErr; |
| 8470 | if (!ToParam->getDefaultArg()) { |
| 8471 | std::optional<ParmVarDecl *> FromParam = |
| 8472 | Importer.getImportedFromDecl(ToD: ToParam); |
| 8473 | assert(FromParam && "ParmVarDecl was not imported?" ); |
| 8474 | |
| 8475 | if (Error Err = ImportDefaultArgOfParmVarDecl(FromParam: *FromParam, ToParam)) |
| 8476 | return std::move(Err); |
| 8477 | } |
| 8478 | Expr *RewrittenInit = nullptr; |
| 8479 | if (E->hasRewrittenInit()) { |
| 8480 | ExpectedExpr ExprOrErr = import(From: E->getRewrittenExpr()); |
| 8481 | if (!ExprOrErr) |
| 8482 | return ExprOrErr.takeError(); |
| 8483 | RewrittenInit = ExprOrErr.get(); |
| 8484 | } |
| 8485 | return CXXDefaultArgExpr::Create(C: Importer.getToContext(), Loc: *ToUsedLocOrErr, |
| 8486 | Param: *ToParamOrErr, RewrittenExpr: RewrittenInit, |
| 8487 | UsedContext: *UsedContextOrErr); |
| 8488 | } |
| 8489 | |
| 8490 | ExpectedStmt |
| 8491 | ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { |
| 8492 | Error Err = Error::success(); |
| 8493 | auto ToType = importChecked(Err, From: E->getType()); |
| 8494 | auto ToTypeSourceInfo = importChecked(Err, From: E->getTypeSourceInfo()); |
| 8495 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 8496 | if (Err) |
| 8497 | return std::move(Err); |
| 8498 | |
| 8499 | return new (Importer.getToContext()) CXXScalarValueInitExpr( |
| 8500 | ToType, ToTypeSourceInfo, ToRParenLoc); |
| 8501 | } |
| 8502 | |
| 8503 | ExpectedStmt |
| 8504 | ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { |
| 8505 | ExpectedExpr ToSubExprOrErr = import(From: E->getSubExpr()); |
| 8506 | if (!ToSubExprOrErr) |
| 8507 | return ToSubExprOrErr.takeError(); |
| 8508 | |
| 8509 | auto ToDtorOrErr = import(From: E->getTemporary()->getDestructor()); |
| 8510 | if (!ToDtorOrErr) |
| 8511 | return ToDtorOrErr.takeError(); |
| 8512 | |
| 8513 | ASTContext &ToCtx = Importer.getToContext(); |
| 8514 | CXXTemporary *Temp = CXXTemporary::Create(C: ToCtx, Destructor: *ToDtorOrErr); |
| 8515 | return CXXBindTemporaryExpr::Create(C: ToCtx, Temp, SubExpr: *ToSubExprOrErr); |
| 8516 | } |
| 8517 | |
| 8518 | ExpectedStmt |
| 8519 | |
| 8520 | ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) { |
| 8521 | Error Err = Error::success(); |
| 8522 | auto ToConstructor = importChecked(Err, From: E->getConstructor()); |
| 8523 | auto ToType = importChecked(Err, From: E->getType()); |
| 8524 | auto ToTypeSourceInfo = importChecked(Err, From: E->getTypeSourceInfo()); |
| 8525 | auto ToParenOrBraceRange = importChecked(Err, From: E->getParenOrBraceRange()); |
| 8526 | if (Err) |
| 8527 | return std::move(Err); |
| 8528 | |
| 8529 | SmallVector<Expr *, 8> ToArgs(E->getNumArgs()); |
| 8530 | if (Error Err = ImportContainerChecked(InContainer: E->arguments(), OutContainer&: ToArgs)) |
| 8531 | return std::move(Err); |
| 8532 | |
| 8533 | return CXXTemporaryObjectExpr::Create( |
| 8534 | Ctx: Importer.getToContext(), Cons: ToConstructor, Ty: ToType, TSI: ToTypeSourceInfo, Args: ToArgs, |
| 8535 | ParenOrBraceRange: ToParenOrBraceRange, HadMultipleCandidates: E->hadMultipleCandidates(), |
| 8536 | ListInitialization: E->isListInitialization(), StdInitListInitialization: E->isStdInitListInitialization(), |
| 8537 | ZeroInitialization: E->requiresZeroInitialization()); |
| 8538 | } |
| 8539 | |
| 8540 | ExpectedDecl ASTNodeImporter::VisitLifetimeExtendedTemporaryDecl( |
| 8541 | LifetimeExtendedTemporaryDecl *D) { |
| 8542 | DeclContext *DC, *LexicalDC; |
| 8543 | if (Error Err = ImportDeclContext(FromD: D, ToDC&: DC, ToLexicalDC&: LexicalDC)) |
| 8544 | return std::move(Err); |
| 8545 | |
| 8546 | Error Err = Error::success(); |
| 8547 | auto Temporary = importChecked(Err, From: D->getTemporaryExpr()); |
| 8548 | auto ExtendingDecl = importChecked(Err, From: D->getExtendingDecl()); |
| 8549 | if (Err) |
| 8550 | return std::move(Err); |
| 8551 | // FIXME: Should ManglingNumber get numbers associated with 'to' context? |
| 8552 | |
| 8553 | LifetimeExtendedTemporaryDecl *To; |
| 8554 | if (GetImportedOrCreateDecl(ToD&: To, FromD: D, args&: Temporary, args&: ExtendingDecl, |
| 8555 | args: D->getManglingNumber())) |
| 8556 | return To; |
| 8557 | |
| 8558 | To->setLexicalDeclContext(LexicalDC); |
| 8559 | LexicalDC->addDeclInternal(D: To); |
| 8560 | return To; |
| 8561 | } |
| 8562 | |
| 8563 | ExpectedStmt |
| 8564 | ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) { |
| 8565 | Error Err = Error::success(); |
| 8566 | auto ToType = importChecked(Err, From: E->getType()); |
| 8567 | Expr *ToTemporaryExpr = importChecked( |
| 8568 | Err, From: E->getLifetimeExtendedTemporaryDecl() ? nullptr : E->getSubExpr()); |
| 8569 | auto ToMaterializedDecl = |
| 8570 | importChecked(Err, From: E->getLifetimeExtendedTemporaryDecl()); |
| 8571 | if (Err) |
| 8572 | return std::move(Err); |
| 8573 | |
| 8574 | if (!ToTemporaryExpr) |
| 8575 | ToTemporaryExpr = cast<Expr>(Val: ToMaterializedDecl->getTemporaryExpr()); |
| 8576 | |
| 8577 | auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr( |
| 8578 | ToType, ToTemporaryExpr, E->isBoundToLvalueReference(), |
| 8579 | ToMaterializedDecl); |
| 8580 | |
| 8581 | return ToMTE; |
| 8582 | } |
| 8583 | |
| 8584 | ExpectedStmt ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) { |
| 8585 | Error Err = Error::success(); |
| 8586 | auto *ToPattern = importChecked(Err, From: E->getPattern()); |
| 8587 | auto ToEllipsisLoc = importChecked(Err, From: E->getEllipsisLoc()); |
| 8588 | if (Err) |
| 8589 | return std::move(Err); |
| 8590 | |
| 8591 | return new (Importer.getToContext()) |
| 8592 | PackExpansionExpr(ToPattern, ToEllipsisLoc, E->getNumExpansions()); |
| 8593 | } |
| 8594 | |
| 8595 | ExpectedStmt ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) { |
| 8596 | Error Err = Error::success(); |
| 8597 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
| 8598 | auto ToPack = importChecked(Err, From: E->getPack()); |
| 8599 | auto ToPackLoc = importChecked(Err, From: E->getPackLoc()); |
| 8600 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 8601 | if (Err) |
| 8602 | return std::move(Err); |
| 8603 | |
| 8604 | UnsignedOrNone Length = std::nullopt; |
| 8605 | if (!E->isValueDependent()) |
| 8606 | Length = E->getPackLength(); |
| 8607 | |
| 8608 | SmallVector<TemplateArgument, 8> ToPartialArguments; |
| 8609 | if (E->isPartiallySubstituted()) { |
| 8610 | if (Error Err = ImportTemplateArguments(FromArgs: E->getPartialArguments(), |
| 8611 | ToArgs&: ToPartialArguments)) |
| 8612 | return std::move(Err); |
| 8613 | } |
| 8614 | |
| 8615 | return SizeOfPackExpr::Create( |
| 8616 | Context&: Importer.getToContext(), OperatorLoc: ToOperatorLoc, Pack: ToPack, PackLoc: ToPackLoc, RParenLoc: ToRParenLoc, |
| 8617 | Length, PartialArgs: ToPartialArguments); |
| 8618 | } |
| 8619 | |
| 8620 | |
| 8621 | ExpectedStmt ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *E) { |
| 8622 | Error Err = Error::success(); |
| 8623 | auto ToOperatorNew = importChecked(Err, From: E->getOperatorNew()); |
| 8624 | auto ToOperatorDelete = importChecked(Err, From: E->getOperatorDelete()); |
| 8625 | auto ToTypeIdParens = importChecked(Err, From: E->getTypeIdParens()); |
| 8626 | auto ToArraySize = importChecked(Err, From: E->getArraySize()); |
| 8627 | auto ToInitializer = importChecked(Err, From: E->getInitializer()); |
| 8628 | auto ToType = importChecked(Err, From: E->getType()); |
| 8629 | auto ToAllocatedTypeSourceInfo = |
| 8630 | importChecked(Err, From: E->getAllocatedTypeSourceInfo()); |
| 8631 | auto ToSourceRange = importChecked(Err, From: E->getSourceRange()); |
| 8632 | auto ToDirectInitRange = importChecked(Err, From: E->getDirectInitRange()); |
| 8633 | if (Err) |
| 8634 | return std::move(Err); |
| 8635 | |
| 8636 | SmallVector<Expr *, 4> ToPlacementArgs(E->getNumPlacementArgs()); |
| 8637 | if (Error Err = |
| 8638 | ImportContainerChecked(InContainer: E->placement_arguments(), OutContainer&: ToPlacementArgs)) |
| 8639 | return std::move(Err); |
| 8640 | |
| 8641 | return CXXNewExpr::Create( |
| 8642 | Ctx: Importer.getToContext(), IsGlobalNew: E->isGlobalNew(), OperatorNew: ToOperatorNew, |
| 8643 | OperatorDelete: ToOperatorDelete, IAP: E->implicitAllocationParameters(), |
| 8644 | UsualArrayDeleteWantsSize: E->doesUsualArrayDeleteWantSize(), PlacementArgs: ToPlacementArgs, TypeIdParens: ToTypeIdParens, |
| 8645 | ArraySize: ToArraySize, InitializationStyle: E->getInitializationStyle(), Initializer: ToInitializer, Ty: ToType, |
| 8646 | AllocatedTypeInfo: ToAllocatedTypeSourceInfo, Range: ToSourceRange, DirectInitRange: ToDirectInitRange); |
| 8647 | } |
| 8648 | |
| 8649 | ExpectedStmt ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 8650 | Error Err = Error::success(); |
| 8651 | auto ToType = importChecked(Err, From: E->getType()); |
| 8652 | auto ToOperatorDelete = importChecked(Err, From: E->getOperatorDelete()); |
| 8653 | auto ToArgument = importChecked(Err, From: E->getArgument()); |
| 8654 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
| 8655 | if (Err) |
| 8656 | return std::move(Err); |
| 8657 | |
| 8658 | return new (Importer.getToContext()) CXXDeleteExpr( |
| 8659 | ToType, E->isGlobalDelete(), E->isArrayForm(), E->isArrayFormAsWritten(), |
| 8660 | E->doesUsualArrayDeleteWantSize(), ToOperatorDelete, ToArgument, |
| 8661 | ToBeginLoc); |
| 8662 | } |
| 8663 | |
| 8664 | ExpectedStmt ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 8665 | Error Err = Error::success(); |
| 8666 | auto ToType = importChecked(Err, From: E->getType()); |
| 8667 | auto ToLocation = importChecked(Err, From: E->getLocation()); |
| 8668 | auto ToConstructor = importChecked(Err, From: E->getConstructor()); |
| 8669 | auto ToParenOrBraceRange = importChecked(Err, From: E->getParenOrBraceRange()); |
| 8670 | if (Err) |
| 8671 | return std::move(Err); |
| 8672 | |
| 8673 | SmallVector<Expr *, 6> ToArgs(E->getNumArgs()); |
| 8674 | if (Error Err = ImportContainerChecked(InContainer: E->arguments(), OutContainer&: ToArgs)) |
| 8675 | return std::move(Err); |
| 8676 | |
| 8677 | CXXConstructExpr *ToE = CXXConstructExpr::Create( |
| 8678 | Ctx: Importer.getToContext(), Ty: ToType, Loc: ToLocation, Ctor: ToConstructor, |
| 8679 | Elidable: E->isElidable(), Args: ToArgs, HadMultipleCandidates: E->hadMultipleCandidates(), |
| 8680 | ListInitialization: E->isListInitialization(), StdInitListInitialization: E->isStdInitListInitialization(), |
| 8681 | ZeroInitialization: E->requiresZeroInitialization(), ConstructKind: E->getConstructionKind(), |
| 8682 | ParenOrBraceRange: ToParenOrBraceRange); |
| 8683 | ToE->setIsImmediateEscalating(E->isImmediateEscalating()); |
| 8684 | return ToE; |
| 8685 | } |
| 8686 | |
| 8687 | ExpectedStmt ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *E) { |
| 8688 | ExpectedExpr ToSubExprOrErr = import(From: E->getSubExpr()); |
| 8689 | if (!ToSubExprOrErr) |
| 8690 | return ToSubExprOrErr.takeError(); |
| 8691 | |
| 8692 | SmallVector<ExprWithCleanups::CleanupObject, 8> ToObjects(E->getNumObjects()); |
| 8693 | if (Error Err = ImportContainerChecked(InContainer: E->getObjects(), OutContainer&: ToObjects)) |
| 8694 | return std::move(Err); |
| 8695 | |
| 8696 | return ExprWithCleanups::Create( |
| 8697 | C: Importer.getToContext(), subexpr: *ToSubExprOrErr, CleanupsHaveSideEffects: E->cleanupsHaveSideEffects(), |
| 8698 | objects: ToObjects); |
| 8699 | } |
| 8700 | |
| 8701 | ExpectedStmt ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) { |
| 8702 | Error Err = Error::success(); |
| 8703 | auto ToCallee = importChecked(Err, From: E->getCallee()); |
| 8704 | auto ToType = importChecked(Err, From: E->getType()); |
| 8705 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 8706 | if (Err) |
| 8707 | return std::move(Err); |
| 8708 | |
| 8709 | SmallVector<Expr *, 4> ToArgs(E->getNumArgs()); |
| 8710 | if (Error Err = ImportContainerChecked(InContainer: E->arguments(), OutContainer&: ToArgs)) |
| 8711 | return std::move(Err); |
| 8712 | |
| 8713 | return CXXMemberCallExpr::Create(Ctx: Importer.getToContext(), Fn: ToCallee, Args: ToArgs, |
| 8714 | Ty: ToType, VK: E->getValueKind(), RP: ToRParenLoc, |
| 8715 | FPFeatures: E->getFPFeatures()); |
| 8716 | } |
| 8717 | |
| 8718 | ExpectedStmt ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) { |
| 8719 | ExpectedType ToTypeOrErr = import(From: E->getType()); |
| 8720 | if (!ToTypeOrErr) |
| 8721 | return ToTypeOrErr.takeError(); |
| 8722 | |
| 8723 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
| 8724 | if (!ToLocationOrErr) |
| 8725 | return ToLocationOrErr.takeError(); |
| 8726 | |
| 8727 | return CXXThisExpr::Create(Ctx: Importer.getToContext(), L: *ToLocationOrErr, |
| 8728 | Ty: *ToTypeOrErr, IsImplicit: E->isImplicit()); |
| 8729 | } |
| 8730 | |
| 8731 | ExpectedStmt ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) { |
| 8732 | ExpectedType ToTypeOrErr = import(From: E->getType()); |
| 8733 | if (!ToTypeOrErr) |
| 8734 | return ToTypeOrErr.takeError(); |
| 8735 | |
| 8736 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
| 8737 | if (!ToLocationOrErr) |
| 8738 | return ToLocationOrErr.takeError(); |
| 8739 | |
| 8740 | return CXXBoolLiteralExpr::Create(C: Importer.getToContext(), Val: E->getValue(), |
| 8741 | Ty: *ToTypeOrErr, Loc: *ToLocationOrErr); |
| 8742 | } |
| 8743 | |
| 8744 | ExpectedStmt ASTNodeImporter::VisitMemberExpr(MemberExpr *E) { |
| 8745 | Error Err = Error::success(); |
| 8746 | auto ToBase = importChecked(Err, From: E->getBase()); |
| 8747 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
| 8748 | auto ToQualifierLoc = importChecked(Err, From: E->getQualifierLoc()); |
| 8749 | auto ToTemplateKeywordLoc = importChecked(Err, From: E->getTemplateKeywordLoc()); |
| 8750 | auto ToMemberDecl = importChecked(Err, From: E->getMemberDecl()); |
| 8751 | auto ToType = importChecked(Err, From: E->getType()); |
| 8752 | auto ToDecl = importChecked(Err, From: E->getFoundDecl().getDecl()); |
| 8753 | auto ToName = importChecked(Err, From: E->getMemberNameInfo().getName()); |
| 8754 | auto ToLoc = importChecked(Err, From: E->getMemberNameInfo().getLoc()); |
| 8755 | if (Err) |
| 8756 | return std::move(Err); |
| 8757 | |
| 8758 | DeclAccessPair ToFoundDecl = |
| 8759 | DeclAccessPair::make(D: ToDecl, AS: E->getFoundDecl().getAccess()); |
| 8760 | |
| 8761 | DeclarationNameInfo ToMemberNameInfo(ToName, ToLoc); |
| 8762 | |
| 8763 | TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr; |
| 8764 | if (E->hasExplicitTemplateArgs()) { |
| 8765 | if (Error Err = |
| 8766 | ImportTemplateArgumentListInfo(FromLAngleLoc: E->getLAngleLoc(), FromRAngleLoc: E->getRAngleLoc(), |
| 8767 | Container: E->template_arguments(), Result&: ToTAInfo)) |
| 8768 | return std::move(Err); |
| 8769 | ResInfo = &ToTAInfo; |
| 8770 | } |
| 8771 | |
| 8772 | return MemberExpr::Create(C: Importer.getToContext(), Base: ToBase, IsArrow: E->isArrow(), |
| 8773 | OperatorLoc: ToOperatorLoc, QualifierLoc: ToQualifierLoc, TemplateKWLoc: ToTemplateKeywordLoc, |
| 8774 | MemberDecl: ToMemberDecl, FoundDecl: ToFoundDecl, MemberNameInfo: ToMemberNameInfo, |
| 8775 | TemplateArgs: ResInfo, T: ToType, VK: E->getValueKind(), |
| 8776 | OK: E->getObjectKind(), NOUR: E->isNonOdrUse()); |
| 8777 | } |
| 8778 | |
| 8779 | ExpectedStmt |
| 8780 | ASTNodeImporter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { |
| 8781 | Error Err = Error::success(); |
| 8782 | auto ToBase = importChecked(Err, From: E->getBase()); |
| 8783 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
| 8784 | auto ToQualifierLoc = importChecked(Err, From: E->getQualifierLoc()); |
| 8785 | auto ToScopeTypeInfo = importChecked(Err, From: E->getScopeTypeInfo()); |
| 8786 | auto ToColonColonLoc = importChecked(Err, From: E->getColonColonLoc()); |
| 8787 | auto ToTildeLoc = importChecked(Err, From: E->getTildeLoc()); |
| 8788 | if (Err) |
| 8789 | return std::move(Err); |
| 8790 | |
| 8791 | PseudoDestructorTypeStorage Storage; |
| 8792 | if (const IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) { |
| 8793 | const IdentifierInfo *ToII = Importer.Import(FromId: FromII); |
| 8794 | ExpectedSLoc ToDestroyedTypeLocOrErr = import(From: E->getDestroyedTypeLoc()); |
| 8795 | if (!ToDestroyedTypeLocOrErr) |
| 8796 | return ToDestroyedTypeLocOrErr.takeError(); |
| 8797 | Storage = PseudoDestructorTypeStorage(ToII, *ToDestroyedTypeLocOrErr); |
| 8798 | } else { |
| 8799 | if (auto ToTIOrErr = import(From: E->getDestroyedTypeInfo())) |
| 8800 | Storage = PseudoDestructorTypeStorage(*ToTIOrErr); |
| 8801 | else |
| 8802 | return ToTIOrErr.takeError(); |
| 8803 | } |
| 8804 | |
| 8805 | return new (Importer.getToContext()) CXXPseudoDestructorExpr( |
| 8806 | Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc, |
| 8807 | ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc, ToTildeLoc, Storage); |
| 8808 | } |
| 8809 | |
| 8810 | ExpectedStmt ASTNodeImporter::VisitCXXDependentScopeMemberExpr( |
| 8811 | CXXDependentScopeMemberExpr *E) { |
| 8812 | Error Err = Error::success(); |
| 8813 | auto ToType = importChecked(Err, From: E->getType()); |
| 8814 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
| 8815 | auto ToQualifierLoc = importChecked(Err, From: E->getQualifierLoc()); |
| 8816 | auto ToTemplateKeywordLoc = importChecked(Err, From: E->getTemplateKeywordLoc()); |
| 8817 | auto ToFirstQualifierFoundInScope = |
| 8818 | importChecked(Err, From: E->getFirstQualifierFoundInScope()); |
| 8819 | if (Err) |
| 8820 | return std::move(Err); |
| 8821 | |
| 8822 | Expr *ToBase = nullptr; |
| 8823 | if (!E->isImplicitAccess()) { |
| 8824 | if (ExpectedExpr ToBaseOrErr = import(From: E->getBase())) |
| 8825 | ToBase = *ToBaseOrErr; |
| 8826 | else |
| 8827 | return ToBaseOrErr.takeError(); |
| 8828 | } |
| 8829 | |
| 8830 | TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr; |
| 8831 | |
| 8832 | if (E->hasExplicitTemplateArgs()) { |
| 8833 | if (Error Err = |
| 8834 | ImportTemplateArgumentListInfo(FromLAngleLoc: E->getLAngleLoc(), FromRAngleLoc: E->getRAngleLoc(), |
| 8835 | Container: E->template_arguments(), Result&: ToTAInfo)) |
| 8836 | return std::move(Err); |
| 8837 | ResInfo = &ToTAInfo; |
| 8838 | } |
| 8839 | auto ToMember = importChecked(Err, From: E->getMember()); |
| 8840 | auto ToMemberLoc = importChecked(Err, From: E->getMemberLoc()); |
| 8841 | if (Err) |
| 8842 | return std::move(Err); |
| 8843 | DeclarationNameInfo ToMemberNameInfo(ToMember, ToMemberLoc); |
| 8844 | |
| 8845 | // Import additional name location/type info. |
| 8846 | if (Error Err = |
| 8847 | ImportDeclarationNameLoc(From: E->getMemberNameInfo(), To&: ToMemberNameInfo)) |
| 8848 | return std::move(Err); |
| 8849 | |
| 8850 | return CXXDependentScopeMemberExpr::Create( |
| 8851 | Ctx: Importer.getToContext(), Base: ToBase, BaseType: ToType, IsArrow: E->isArrow(), OperatorLoc: ToOperatorLoc, |
| 8852 | QualifierLoc: ToQualifierLoc, TemplateKWLoc: ToTemplateKeywordLoc, FirstQualifierFoundInScope: ToFirstQualifierFoundInScope, |
| 8853 | MemberNameInfo: ToMemberNameInfo, TemplateArgs: ResInfo); |
| 8854 | } |
| 8855 | |
| 8856 | ExpectedStmt |
| 8857 | ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) { |
| 8858 | Error Err = Error::success(); |
| 8859 | auto ToQualifierLoc = importChecked(Err, From: E->getQualifierLoc()); |
| 8860 | auto ToTemplateKeywordLoc = importChecked(Err, From: E->getTemplateKeywordLoc()); |
| 8861 | auto ToDeclName = importChecked(Err, From: E->getDeclName()); |
| 8862 | auto ToNameLoc = importChecked(Err, From: E->getNameInfo().getLoc()); |
| 8863 | auto ToLAngleLoc = importChecked(Err, From: E->getLAngleLoc()); |
| 8864 | auto ToRAngleLoc = importChecked(Err, From: E->getRAngleLoc()); |
| 8865 | if (Err) |
| 8866 | return std::move(Err); |
| 8867 | |
| 8868 | DeclarationNameInfo ToNameInfo(ToDeclName, ToNameLoc); |
| 8869 | if (Error Err = ImportDeclarationNameLoc(From: E->getNameInfo(), To&: ToNameInfo)) |
| 8870 | return std::move(Err); |
| 8871 | |
| 8872 | TemplateArgumentListInfo ToTAInfo(ToLAngleLoc, ToRAngleLoc); |
| 8873 | TemplateArgumentListInfo *ResInfo = nullptr; |
| 8874 | if (E->hasExplicitTemplateArgs()) { |
| 8875 | if (Error Err = |
| 8876 | ImportTemplateArgumentListInfo(Container: E->template_arguments(), ToTAInfo)) |
| 8877 | return std::move(Err); |
| 8878 | ResInfo = &ToTAInfo; |
| 8879 | } |
| 8880 | |
| 8881 | return DependentScopeDeclRefExpr::Create( |
| 8882 | Context: Importer.getToContext(), QualifierLoc: ToQualifierLoc, TemplateKWLoc: ToTemplateKeywordLoc, |
| 8883 | NameInfo: ToNameInfo, TemplateArgs: ResInfo); |
| 8884 | } |
| 8885 | |
| 8886 | ExpectedStmt ASTNodeImporter::VisitCXXUnresolvedConstructExpr( |
| 8887 | CXXUnresolvedConstructExpr *E) { |
| 8888 | Error Err = Error::success(); |
| 8889 | auto ToLParenLoc = importChecked(Err, From: E->getLParenLoc()); |
| 8890 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 8891 | auto ToType = importChecked(Err, From: E->getType()); |
| 8892 | auto ToTypeSourceInfo = importChecked(Err, From: E->getTypeSourceInfo()); |
| 8893 | if (Err) |
| 8894 | return std::move(Err); |
| 8895 | |
| 8896 | SmallVector<Expr *, 8> ToArgs(E->getNumArgs()); |
| 8897 | if (Error Err = |
| 8898 | ImportArrayChecked(Ibegin: E->arg_begin(), Iend: E->arg_end(), Obegin: ToArgs.begin())) |
| 8899 | return std::move(Err); |
| 8900 | |
| 8901 | return CXXUnresolvedConstructExpr::Create( |
| 8902 | Context: Importer.getToContext(), T: ToType, TSI: ToTypeSourceInfo, LParenLoc: ToLParenLoc, |
| 8903 | Args: ArrayRef(ToArgs), RParenLoc: ToRParenLoc, IsListInit: E->isListInitialization()); |
| 8904 | } |
| 8905 | |
| 8906 | ExpectedStmt |
| 8907 | ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) { |
| 8908 | Expected<CXXRecordDecl *> ToNamingClassOrErr = import(From: E->getNamingClass()); |
| 8909 | if (!ToNamingClassOrErr) |
| 8910 | return ToNamingClassOrErr.takeError(); |
| 8911 | |
| 8912 | auto ToQualifierLocOrErr = import(From: E->getQualifierLoc()); |
| 8913 | if (!ToQualifierLocOrErr) |
| 8914 | return ToQualifierLocOrErr.takeError(); |
| 8915 | |
| 8916 | Error Err = Error::success(); |
| 8917 | auto ToName = importChecked(Err, From: E->getName()); |
| 8918 | auto ToNameLoc = importChecked(Err, From: E->getNameLoc()); |
| 8919 | if (Err) |
| 8920 | return std::move(Err); |
| 8921 | DeclarationNameInfo ToNameInfo(ToName, ToNameLoc); |
| 8922 | |
| 8923 | // Import additional name location/type info. |
| 8924 | if (Error Err = ImportDeclarationNameLoc(From: E->getNameInfo(), To&: ToNameInfo)) |
| 8925 | return std::move(Err); |
| 8926 | |
| 8927 | UnresolvedSet<8> ToDecls; |
| 8928 | for (auto *D : E->decls()) |
| 8929 | if (auto ToDOrErr = import(From: D)) |
| 8930 | ToDecls.addDecl(D: cast<NamedDecl>(Val: *ToDOrErr)); |
| 8931 | else |
| 8932 | return ToDOrErr.takeError(); |
| 8933 | |
| 8934 | if (E->hasExplicitTemplateArgs()) { |
| 8935 | TemplateArgumentListInfo ToTAInfo; |
| 8936 | if (Error Err = ImportTemplateArgumentListInfo( |
| 8937 | FromLAngleLoc: E->getLAngleLoc(), FromRAngleLoc: E->getRAngleLoc(), Container: E->template_arguments(), |
| 8938 | Result&: ToTAInfo)) |
| 8939 | return std::move(Err); |
| 8940 | |
| 8941 | ExpectedSLoc ToTemplateKeywordLocOrErr = import(From: E->getTemplateKeywordLoc()); |
| 8942 | if (!ToTemplateKeywordLocOrErr) |
| 8943 | return ToTemplateKeywordLocOrErr.takeError(); |
| 8944 | |
| 8945 | const bool KnownDependent = |
| 8946 | (E->getDependence() & ExprDependence::TypeValue) == |
| 8947 | ExprDependence::TypeValue; |
| 8948 | return UnresolvedLookupExpr::Create( |
| 8949 | Context: Importer.getToContext(), NamingClass: *ToNamingClassOrErr, QualifierLoc: *ToQualifierLocOrErr, |
| 8950 | TemplateKWLoc: *ToTemplateKeywordLocOrErr, NameInfo: ToNameInfo, RequiresADL: E->requiresADL(), Args: &ToTAInfo, |
| 8951 | Begin: ToDecls.begin(), End: ToDecls.end(), KnownDependent, |
| 8952 | /*KnownInstantiationDependent=*/E->isInstantiationDependent()); |
| 8953 | } |
| 8954 | |
| 8955 | return UnresolvedLookupExpr::Create( |
| 8956 | Context: Importer.getToContext(), NamingClass: *ToNamingClassOrErr, QualifierLoc: *ToQualifierLocOrErr, |
| 8957 | NameInfo: ToNameInfo, RequiresADL: E->requiresADL(), Begin: ToDecls.begin(), End: ToDecls.end(), |
| 8958 | /*KnownDependent=*/E->isTypeDependent(), |
| 8959 | /*KnownInstantiationDependent=*/E->isInstantiationDependent()); |
| 8960 | } |
| 8961 | |
| 8962 | ExpectedStmt |
| 8963 | ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) { |
| 8964 | Error Err = Error::success(); |
| 8965 | auto ToType = importChecked(Err, From: E->getType()); |
| 8966 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
| 8967 | auto ToQualifierLoc = importChecked(Err, From: E->getQualifierLoc()); |
| 8968 | auto ToTemplateKeywordLoc = importChecked(Err, From: E->getTemplateKeywordLoc()); |
| 8969 | auto ToName = importChecked(Err, From: E->getName()); |
| 8970 | auto ToNameLoc = importChecked(Err, From: E->getNameLoc()); |
| 8971 | if (Err) |
| 8972 | return std::move(Err); |
| 8973 | |
| 8974 | DeclarationNameInfo ToNameInfo(ToName, ToNameLoc); |
| 8975 | // Import additional name location/type info. |
| 8976 | if (Error Err = ImportDeclarationNameLoc(From: E->getNameInfo(), To&: ToNameInfo)) |
| 8977 | return std::move(Err); |
| 8978 | |
| 8979 | UnresolvedSet<8> ToDecls; |
| 8980 | for (Decl *D : E->decls()) |
| 8981 | if (auto ToDOrErr = import(From: D)) |
| 8982 | ToDecls.addDecl(D: cast<NamedDecl>(Val: *ToDOrErr)); |
| 8983 | else |
| 8984 | return ToDOrErr.takeError(); |
| 8985 | |
| 8986 | TemplateArgumentListInfo ToTAInfo; |
| 8987 | TemplateArgumentListInfo *ResInfo = nullptr; |
| 8988 | if (E->hasExplicitTemplateArgs()) { |
| 8989 | TemplateArgumentListInfo FromTAInfo; |
| 8990 | E->copyTemplateArgumentsInto(List&: FromTAInfo); |
| 8991 | if (Error Err = ImportTemplateArgumentListInfo(From: FromTAInfo, Result&: ToTAInfo)) |
| 8992 | return std::move(Err); |
| 8993 | ResInfo = &ToTAInfo; |
| 8994 | } |
| 8995 | |
| 8996 | Expr *ToBase = nullptr; |
| 8997 | if (!E->isImplicitAccess()) { |
| 8998 | if (ExpectedExpr ToBaseOrErr = import(From: E->getBase())) |
| 8999 | ToBase = *ToBaseOrErr; |
| 9000 | else |
| 9001 | return ToBaseOrErr.takeError(); |
| 9002 | } |
| 9003 | |
| 9004 | return UnresolvedMemberExpr::Create( |
| 9005 | Context: Importer.getToContext(), HasUnresolvedUsing: E->hasUnresolvedUsing(), Base: ToBase, BaseType: ToType, |
| 9006 | IsArrow: E->isArrow(), OperatorLoc: ToOperatorLoc, QualifierLoc: ToQualifierLoc, TemplateKWLoc: ToTemplateKeywordLoc, |
| 9007 | MemberNameInfo: ToNameInfo, TemplateArgs: ResInfo, Begin: ToDecls.begin(), End: ToDecls.end()); |
| 9008 | } |
| 9009 | |
| 9010 | ExpectedStmt ASTNodeImporter::VisitCallExpr(CallExpr *E) { |
| 9011 | Error Err = Error::success(); |
| 9012 | auto ToCallee = importChecked(Err, From: E->getCallee()); |
| 9013 | auto ToType = importChecked(Err, From: E->getType()); |
| 9014 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 9015 | if (Err) |
| 9016 | return std::move(Err); |
| 9017 | |
| 9018 | unsigned NumArgs = E->getNumArgs(); |
| 9019 | llvm::SmallVector<Expr *, 2> ToArgs(NumArgs); |
| 9020 | if (Error Err = ImportContainerChecked(InContainer: E->arguments(), OutContainer&: ToArgs)) |
| 9021 | return std::move(Err); |
| 9022 | |
| 9023 | if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(Val: E)) { |
| 9024 | return CXXOperatorCallExpr::Create( |
| 9025 | Ctx: Importer.getToContext(), OpKind: OCE->getOperator(), Fn: ToCallee, Args: ToArgs, Ty: ToType, |
| 9026 | VK: OCE->getValueKind(), OperatorLoc: ToRParenLoc, FPFeatures: OCE->getFPFeatures(), |
| 9027 | UsesADL: OCE->getADLCallKind()); |
| 9028 | } |
| 9029 | |
| 9030 | return CallExpr::Create(Ctx: Importer.getToContext(), Fn: ToCallee, Args: ToArgs, Ty: ToType, |
| 9031 | VK: E->getValueKind(), RParenLoc: ToRParenLoc, FPFeatures: E->getFPFeatures(), |
| 9032 | /*MinNumArgs=*/0, UsesADL: E->getADLCallKind()); |
| 9033 | } |
| 9034 | |
| 9035 | ExpectedStmt ASTNodeImporter::VisitLambdaExpr(LambdaExpr *E) { |
| 9036 | CXXRecordDecl *FromClass = E->getLambdaClass(); |
| 9037 | auto ToClassOrErr = import(From: FromClass); |
| 9038 | if (!ToClassOrErr) |
| 9039 | return ToClassOrErr.takeError(); |
| 9040 | CXXRecordDecl *ToClass = *ToClassOrErr; |
| 9041 | |
| 9042 | auto ToCallOpOrErr = import(From: E->getCallOperator()); |
| 9043 | if (!ToCallOpOrErr) |
| 9044 | return ToCallOpOrErr.takeError(); |
| 9045 | |
| 9046 | SmallVector<Expr *, 8> ToCaptureInits(E->capture_size()); |
| 9047 | if (Error Err = ImportContainerChecked(InContainer: E->capture_inits(), OutContainer&: ToCaptureInits)) |
| 9048 | return std::move(Err); |
| 9049 | |
| 9050 | Error Err = Error::success(); |
| 9051 | auto ToIntroducerRange = importChecked(Err, From: E->getIntroducerRange()); |
| 9052 | auto ToCaptureDefaultLoc = importChecked(Err, From: E->getCaptureDefaultLoc()); |
| 9053 | auto ToEndLoc = importChecked(Err, From: E->getEndLoc()); |
| 9054 | if (Err) |
| 9055 | return std::move(Err); |
| 9056 | |
| 9057 | return LambdaExpr::Create(C: Importer.getToContext(), Class: ToClass, IntroducerRange: ToIntroducerRange, |
| 9058 | CaptureDefault: E->getCaptureDefault(), CaptureDefaultLoc: ToCaptureDefaultLoc, |
| 9059 | ExplicitParams: E->hasExplicitParameters(), |
| 9060 | ExplicitResultType: E->hasExplicitResultType(), CaptureInits: ToCaptureInits, |
| 9061 | ClosingBrace: ToEndLoc, ContainsUnexpandedParameterPack: E->containsUnexpandedParameterPack()); |
| 9062 | } |
| 9063 | |
| 9064 | |
| 9065 | ExpectedStmt ASTNodeImporter::VisitInitListExpr(InitListExpr *E) { |
| 9066 | Error Err = Error::success(); |
| 9067 | auto ToLBraceLoc = importChecked(Err, From: E->getLBraceLoc()); |
| 9068 | auto ToRBraceLoc = importChecked(Err, From: E->getRBraceLoc()); |
| 9069 | auto ToType = importChecked(Err, From: E->getType()); |
| 9070 | if (Err) |
| 9071 | return std::move(Err); |
| 9072 | |
| 9073 | SmallVector<Expr *, 4> ToExprs(E->getNumInits()); |
| 9074 | if (Error Err = ImportContainerChecked(InContainer: E->inits(), OutContainer&: ToExprs)) |
| 9075 | return std::move(Err); |
| 9076 | |
| 9077 | ASTContext &ToCtx = Importer.getToContext(); |
| 9078 | InitListExpr *To = new (ToCtx) |
| 9079 | InitListExpr(ToCtx, ToLBraceLoc, ToExprs, ToRBraceLoc, E->isExplicit()); |
| 9080 | To->setType(ToType); |
| 9081 | |
| 9082 | if (E->hasArrayFiller()) { |
| 9083 | if (ExpectedExpr ToFillerOrErr = import(From: E->getArrayFiller())) |
| 9084 | To->setArrayFiller(*ToFillerOrErr); |
| 9085 | else |
| 9086 | return ToFillerOrErr.takeError(); |
| 9087 | } |
| 9088 | |
| 9089 | if (FieldDecl *FromFD = E->getInitializedFieldInUnion()) { |
| 9090 | if (auto ToFDOrErr = import(From: FromFD)) |
| 9091 | To->setInitializedFieldInUnion(*ToFDOrErr); |
| 9092 | else |
| 9093 | return ToFDOrErr.takeError(); |
| 9094 | } |
| 9095 | |
| 9096 | if (InitListExpr *SyntForm = E->getSyntacticForm()) { |
| 9097 | if (auto ToSyntFormOrErr = import(From: SyntForm)) |
| 9098 | To->setSyntacticForm(*ToSyntFormOrErr); |
| 9099 | else |
| 9100 | return ToSyntFormOrErr.takeError(); |
| 9101 | } |
| 9102 | |
| 9103 | // Copy InitListExprBitfields, which are not handled in the ctor of |
| 9104 | // InitListExpr. |
| 9105 | To->sawArrayRangeDesignator(ARD: E->hadArrayRangeDesignator()); |
| 9106 | |
| 9107 | return To; |
| 9108 | } |
| 9109 | |
| 9110 | ExpectedStmt ASTNodeImporter::VisitCXXStdInitializerListExpr( |
| 9111 | CXXStdInitializerListExpr *E) { |
| 9112 | ExpectedType ToTypeOrErr = import(From: E->getType()); |
| 9113 | if (!ToTypeOrErr) |
| 9114 | return ToTypeOrErr.takeError(); |
| 9115 | |
| 9116 | ExpectedExpr ToSubExprOrErr = import(From: E->getSubExpr()); |
| 9117 | if (!ToSubExprOrErr) |
| 9118 | return ToSubExprOrErr.takeError(); |
| 9119 | |
| 9120 | return new (Importer.getToContext()) CXXStdInitializerListExpr( |
| 9121 | *ToTypeOrErr, *ToSubExprOrErr); |
| 9122 | } |
| 9123 | |
| 9124 | ExpectedStmt ASTNodeImporter::VisitCXXInheritedCtorInitExpr( |
| 9125 | CXXInheritedCtorInitExpr *E) { |
| 9126 | Error Err = Error::success(); |
| 9127 | auto ToLocation = importChecked(Err, From: E->getLocation()); |
| 9128 | auto ToType = importChecked(Err, From: E->getType()); |
| 9129 | auto ToConstructor = importChecked(Err, From: E->getConstructor()); |
| 9130 | if (Err) |
| 9131 | return std::move(Err); |
| 9132 | |
| 9133 | return new (Importer.getToContext()) CXXInheritedCtorInitExpr( |
| 9134 | ToLocation, ToType, ToConstructor, E->constructsVBase(), |
| 9135 | E->inheritedFromVBase()); |
| 9136 | } |
| 9137 | |
| 9138 | ExpectedStmt ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) { |
| 9139 | Error Err = Error::success(); |
| 9140 | auto ToType = importChecked(Err, From: E->getType()); |
| 9141 | auto ToCommonExpr = importChecked(Err, From: E->getCommonExpr()); |
| 9142 | auto ToSubExpr = importChecked(Err, From: E->getSubExpr()); |
| 9143 | if (Err) |
| 9144 | return std::move(Err); |
| 9145 | |
| 9146 | return new (Importer.getToContext()) ArrayInitLoopExpr( |
| 9147 | ToType, ToCommonExpr, ToSubExpr); |
| 9148 | } |
| 9149 | |
| 9150 | ExpectedStmt ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) { |
| 9151 | ExpectedType ToTypeOrErr = import(From: E->getType()); |
| 9152 | if (!ToTypeOrErr) |
| 9153 | return ToTypeOrErr.takeError(); |
| 9154 | return new (Importer.getToContext()) ArrayInitIndexExpr(*ToTypeOrErr); |
| 9155 | } |
| 9156 | |
| 9157 | ExpectedStmt ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) { |
| 9158 | ExpectedSLoc ToBeginLocOrErr = import(From: E->getBeginLoc()); |
| 9159 | if (!ToBeginLocOrErr) |
| 9160 | return ToBeginLocOrErr.takeError(); |
| 9161 | |
| 9162 | auto ToFieldOrErr = import(From: E->getField()); |
| 9163 | if (!ToFieldOrErr) |
| 9164 | return ToFieldOrErr.takeError(); |
| 9165 | |
| 9166 | auto UsedContextOrErr = Importer.ImportContext(FromDC: E->getUsedContext()); |
| 9167 | if (!UsedContextOrErr) |
| 9168 | return UsedContextOrErr.takeError(); |
| 9169 | |
| 9170 | FieldDecl *ToField = *ToFieldOrErr; |
| 9171 | assert(ToField->hasInClassInitializer() && |
| 9172 | "Field should have in-class initializer if there is a default init " |
| 9173 | "expression that uses it." ); |
| 9174 | if (!ToField->getInClassInitializer()) { |
| 9175 | // The in-class initializer may be not yet set in "To" AST even if the |
| 9176 | // field is already there. This must be set here to make construction of |
| 9177 | // CXXDefaultInitExpr work. |
| 9178 | auto ToInClassInitializerOrErr = |
| 9179 | import(From: E->getField()->getInClassInitializer()); |
| 9180 | if (!ToInClassInitializerOrErr) |
| 9181 | return ToInClassInitializerOrErr.takeError(); |
| 9182 | ToField->setInClassInitializer(*ToInClassInitializerOrErr); |
| 9183 | } |
| 9184 | |
| 9185 | Expr *RewrittenInit = nullptr; |
| 9186 | if (E->hasRewrittenInit()) { |
| 9187 | ExpectedExpr ExprOrErr = import(From: E->getRewrittenExpr()); |
| 9188 | if (!ExprOrErr) |
| 9189 | return ExprOrErr.takeError(); |
| 9190 | RewrittenInit = ExprOrErr.get(); |
| 9191 | } |
| 9192 | |
| 9193 | return CXXDefaultInitExpr::Create(Ctx: Importer.getToContext(), Loc: *ToBeginLocOrErr, |
| 9194 | Field: ToField, UsedContext: *UsedContextOrErr, RewrittenInitExpr: RewrittenInit); |
| 9195 | } |
| 9196 | |
| 9197 | ExpectedStmt ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) { |
| 9198 | Error Err = Error::success(); |
| 9199 | auto ToType = importChecked(Err, From: E->getType()); |
| 9200 | auto ToSubExpr = importChecked(Err, From: E->getSubExpr()); |
| 9201 | auto ToTypeInfoAsWritten = importChecked(Err, From: E->getTypeInfoAsWritten()); |
| 9202 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
| 9203 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 9204 | auto ToAngleBrackets = importChecked(Err, From: E->getAngleBrackets()); |
| 9205 | if (Err) |
| 9206 | return std::move(Err); |
| 9207 | |
| 9208 | ExprValueKind VK = E->getValueKind(); |
| 9209 | CastKind CK = E->getCastKind(); |
| 9210 | auto ToBasePathOrErr = ImportCastPath(CE: E); |
| 9211 | if (!ToBasePathOrErr) |
| 9212 | return ToBasePathOrErr.takeError(); |
| 9213 | |
| 9214 | if (auto CCE = dyn_cast<CXXStaticCastExpr>(Val: E)) { |
| 9215 | return CXXStaticCastExpr::Create( |
| 9216 | Context: Importer.getToContext(), T: ToType, VK, K: CK, Op: ToSubExpr, Path: &(*ToBasePathOrErr), |
| 9217 | Written: ToTypeInfoAsWritten, FPO: CCE->getFPFeatures(), L: ToOperatorLoc, RParenLoc: ToRParenLoc, |
| 9218 | AngleBrackets: ToAngleBrackets); |
| 9219 | } else if (isa<CXXDynamicCastExpr>(Val: E)) { |
| 9220 | return CXXDynamicCastExpr::Create( |
| 9221 | Context: Importer.getToContext(), T: ToType, VK, Kind: CK, Op: ToSubExpr, Path: &(*ToBasePathOrErr), |
| 9222 | Written: ToTypeInfoAsWritten, L: ToOperatorLoc, RParenLoc: ToRParenLoc, AngleBrackets: ToAngleBrackets); |
| 9223 | } else if (isa<CXXReinterpretCastExpr>(Val: E)) { |
| 9224 | return CXXReinterpretCastExpr::Create( |
| 9225 | Context: Importer.getToContext(), T: ToType, VK, Kind: CK, Op: ToSubExpr, Path: &(*ToBasePathOrErr), |
| 9226 | WrittenTy: ToTypeInfoAsWritten, L: ToOperatorLoc, RParenLoc: ToRParenLoc, AngleBrackets: ToAngleBrackets); |
| 9227 | } else if (isa<CXXConstCastExpr>(Val: E)) { |
| 9228 | return CXXConstCastExpr::Create( |
| 9229 | Context: Importer.getToContext(), T: ToType, VK, Op: ToSubExpr, WrittenTy: ToTypeInfoAsWritten, |
| 9230 | L: ToOperatorLoc, RParenLoc: ToRParenLoc, AngleBrackets: ToAngleBrackets); |
| 9231 | } else { |
| 9232 | llvm_unreachable("Unknown cast type" ); |
| 9233 | return make_error<ASTImportError>(); |
| 9234 | } |
| 9235 | } |
| 9236 | |
| 9237 | ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr( |
| 9238 | SubstNonTypeTemplateParmExpr *E) { |
| 9239 | Error Err = Error::success(); |
| 9240 | auto ToType = importChecked(Err, From: E->getType()); |
| 9241 | auto ToNameLoc = importChecked(Err, From: E->getNameLoc()); |
| 9242 | auto ToAssociatedDecl = importChecked(Err, From: E->getAssociatedDecl()); |
| 9243 | auto ToParamType = importChecked(Err, From: E->getParameterType()); |
| 9244 | auto ToReplacement = importChecked(Err, From: E->getReplacement()); |
| 9245 | if (Err) |
| 9246 | return std::move(Err); |
| 9247 | |
| 9248 | return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr( |
| 9249 | ToType, E->getValueKind(), ToNameLoc, ToReplacement, ToAssociatedDecl, |
| 9250 | ToParamType, E->getIndex(), E->getPackIndex(), E->getFinal()); |
| 9251 | } |
| 9252 | |
| 9253 | ExpectedStmt ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) { |
| 9254 | Error Err = Error::success(); |
| 9255 | auto ToType = importChecked(Err, From: E->getType()); |
| 9256 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
| 9257 | auto ToEndLoc = importChecked(Err, From: E->getEndLoc()); |
| 9258 | if (Err) |
| 9259 | return std::move(Err); |
| 9260 | |
| 9261 | SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs()); |
| 9262 | if (Error Err = ImportContainerChecked(InContainer: E->getArgs(), OutContainer&: ToArgs)) |
| 9263 | return std::move(Err); |
| 9264 | |
| 9265 | if (E->isStoredAsBoolean()) { |
| 9266 | // According to Sema::BuildTypeTrait(), if E is value-dependent, |
| 9267 | // Value is always false. |
| 9268 | bool ToValue = (E->isValueDependent() ? false : E->getBoolValue()); |
| 9269 | return TypeTraitExpr::Create(C: Importer.getToContext(), T: ToType, Loc: ToBeginLoc, |
| 9270 | Kind: E->getTrait(), Args: ToArgs, RParenLoc: ToEndLoc, Value: ToValue); |
| 9271 | } |
| 9272 | return TypeTraitExpr::Create(C: Importer.getToContext(), T: ToType, Loc: ToBeginLoc, |
| 9273 | Kind: E->getTrait(), Args: ToArgs, RParenLoc: ToEndLoc, |
| 9274 | Value: E->getAPValue()); |
| 9275 | } |
| 9276 | |
| 9277 | ExpectedStmt ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) { |
| 9278 | ExpectedType ToTypeOrErr = import(From: E->getType()); |
| 9279 | if (!ToTypeOrErr) |
| 9280 | return ToTypeOrErr.takeError(); |
| 9281 | |
| 9282 | auto ToSourceRangeOrErr = import(From: E->getSourceRange()); |
| 9283 | if (!ToSourceRangeOrErr) |
| 9284 | return ToSourceRangeOrErr.takeError(); |
| 9285 | |
| 9286 | if (E->isTypeOperand()) { |
| 9287 | if (auto ToTSIOrErr = import(From: E->getTypeOperandSourceInfo())) |
| 9288 | return new (Importer.getToContext()) CXXTypeidExpr( |
| 9289 | *ToTypeOrErr, *ToTSIOrErr, *ToSourceRangeOrErr); |
| 9290 | else |
| 9291 | return ToTSIOrErr.takeError(); |
| 9292 | } |
| 9293 | |
| 9294 | ExpectedExpr ToExprOperandOrErr = import(From: E->getExprOperand()); |
| 9295 | if (!ToExprOperandOrErr) |
| 9296 | return ToExprOperandOrErr.takeError(); |
| 9297 | |
| 9298 | return new (Importer.getToContext()) CXXTypeidExpr( |
| 9299 | *ToTypeOrErr, *ToExprOperandOrErr, *ToSourceRangeOrErr); |
| 9300 | } |
| 9301 | |
| 9302 | ExpectedStmt ASTNodeImporter::VisitCXXFoldExpr(CXXFoldExpr *E) { |
| 9303 | Error Err = Error::success(); |
| 9304 | |
| 9305 | QualType ToType = importChecked(Err, From: E->getType()); |
| 9306 | UnresolvedLookupExpr *ToCallee = importChecked(Err, From: E->getCallee()); |
| 9307 | SourceLocation ToLParenLoc = importChecked(Err, From: E->getLParenLoc()); |
| 9308 | Expr *ToLHS = importChecked(Err, From: E->getLHS()); |
| 9309 | SourceLocation ToEllipsisLoc = importChecked(Err, From: E->getEllipsisLoc()); |
| 9310 | Expr *ToRHS = importChecked(Err, From: E->getRHS()); |
| 9311 | SourceLocation ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 9312 | |
| 9313 | if (Err) |
| 9314 | return std::move(Err); |
| 9315 | |
| 9316 | return new (Importer.getToContext()) |
| 9317 | CXXFoldExpr(ToType, ToCallee, ToLParenLoc, ToLHS, E->getOperator(), |
| 9318 | ToEllipsisLoc, ToRHS, ToRParenLoc, E->getNumExpansions()); |
| 9319 | } |
| 9320 | |
| 9321 | ExpectedStmt ASTNodeImporter::VisitRequiresExpr(RequiresExpr *E) { |
| 9322 | Error Err = Error::success(); |
| 9323 | auto RequiresKWLoc = importChecked(Err, From: E->getRequiresKWLoc()); |
| 9324 | auto RParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 9325 | auto RBraceLoc = importChecked(Err, From: E->getRBraceLoc()); |
| 9326 | |
| 9327 | auto Body = importChecked(Err, From: E->getBody()); |
| 9328 | auto LParenLoc = importChecked(Err, From: E->getLParenLoc()); |
| 9329 | if (Err) |
| 9330 | return std::move(Err); |
| 9331 | SmallVector<ParmVarDecl *, 4> LocalParameters(E->getLocalParameters().size()); |
| 9332 | if (Error Err = |
| 9333 | ImportArrayChecked(InContainer: E->getLocalParameters(), Obegin: LocalParameters.begin())) |
| 9334 | return std::move(Err); |
| 9335 | SmallVector<concepts::Requirement *, 4> Requirements( |
| 9336 | E->getRequirements().size()); |
| 9337 | if (Error Err = |
| 9338 | ImportArrayChecked(InContainer: E->getRequirements(), Obegin: Requirements.begin())) |
| 9339 | return std::move(Err); |
| 9340 | return RequiresExpr::Create(C&: Importer.getToContext(), RequiresKWLoc, Body, |
| 9341 | LParenLoc, LocalParameters, RParenLoc, |
| 9342 | Requirements, RBraceLoc); |
| 9343 | } |
| 9344 | |
| 9345 | ExpectedStmt |
| 9346 | ASTNodeImporter::VisitConceptSpecializationExpr(ConceptSpecializationExpr *E) { |
| 9347 | Error Err = Error::success(); |
| 9348 | auto CL = importChecked(Err, From: E->getConceptReference()); |
| 9349 | auto CSD = importChecked(Err, From: E->getSpecializationDecl()); |
| 9350 | if (Err) |
| 9351 | return std::move(Err); |
| 9352 | if (E->isValueDependent()) |
| 9353 | return ConceptSpecializationExpr::Create( |
| 9354 | C: Importer.getToContext(), ConceptRef: CL, |
| 9355 | SpecDecl: const_cast<ImplicitConceptSpecializationDecl *>(CSD), Satisfaction: nullptr); |
| 9356 | ConstraintSatisfaction Satisfaction; |
| 9357 | if (Error Err = |
| 9358 | ImportConstraintSatisfaction(FromSat: E->getSatisfaction(), ToSat&: Satisfaction)) |
| 9359 | return std::move(Err); |
| 9360 | return ConceptSpecializationExpr::Create( |
| 9361 | C: Importer.getToContext(), ConceptRef: CL, |
| 9362 | SpecDecl: const_cast<ImplicitConceptSpecializationDecl *>(CSD), Satisfaction: &Satisfaction); |
| 9363 | } |
| 9364 | |
| 9365 | ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmPackExpr( |
| 9366 | SubstNonTypeTemplateParmPackExpr *E) { |
| 9367 | Error Err = Error::success(); |
| 9368 | auto ToType = importChecked(Err, From: E->getType()); |
| 9369 | auto ToPackLoc = importChecked(Err, From: E->getParameterPackLocation()); |
| 9370 | auto ToArgPack = importChecked(Err, From: E->getArgumentPack()); |
| 9371 | auto ToAssociatedDecl = importChecked(Err, From: E->getAssociatedDecl()); |
| 9372 | if (Err) |
| 9373 | return std::move(Err); |
| 9374 | |
| 9375 | return new (Importer.getToContext()) SubstNonTypeTemplateParmPackExpr( |
| 9376 | ToType, E->getValueKind(), ToPackLoc, ToArgPack, ToAssociatedDecl, |
| 9377 | E->getIndex(), E->getFinal()); |
| 9378 | } |
| 9379 | |
| 9380 | ExpectedStmt ASTNodeImporter::VisitPseudoObjectExpr(PseudoObjectExpr *E) { |
| 9381 | SmallVector<Expr *, 4> ToSemantics(E->getNumSemanticExprs()); |
| 9382 | if (Error Err = ImportContainerChecked(InContainer: E->semantics(), OutContainer&: ToSemantics)) |
| 9383 | return std::move(Err); |
| 9384 | auto ToSyntOrErr = import(From: E->getSyntacticForm()); |
| 9385 | if (!ToSyntOrErr) |
| 9386 | return ToSyntOrErr.takeError(); |
| 9387 | return PseudoObjectExpr::Create(Context: Importer.getToContext(), syntactic: *ToSyntOrErr, |
| 9388 | semantic: ToSemantics, resultIndex: E->getResultExprIndex()); |
| 9389 | } |
| 9390 | |
| 9391 | ExpectedStmt |
| 9392 | ASTNodeImporter::VisitCXXParenListInitExpr(CXXParenListInitExpr *E) { |
| 9393 | Error Err = Error::success(); |
| 9394 | auto ToType = importChecked(Err, From: E->getType()); |
| 9395 | auto ToInitLoc = importChecked(Err, From: E->getInitLoc()); |
| 9396 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
| 9397 | auto ToEndLoc = importChecked(Err, From: E->getEndLoc()); |
| 9398 | if (Err) |
| 9399 | return std::move(Err); |
| 9400 | |
| 9401 | SmallVector<Expr *, 4> ToArgs(E->getInitExprs().size()); |
| 9402 | if (Error Err = ImportContainerChecked(InContainer: E->getInitExprs(), OutContainer&: ToArgs)) |
| 9403 | return std::move(Err); |
| 9404 | return CXXParenListInitExpr::Create(C&: Importer.getToContext(), Args: ToArgs, T: ToType, |
| 9405 | NumUserSpecifiedExprs: E->getUserSpecifiedInitExprs().size(), |
| 9406 | InitLoc: ToInitLoc, LParenLoc: ToBeginLoc, RParenLoc: ToEndLoc); |
| 9407 | } |
| 9408 | |
| 9409 | Error ASTNodeImporter::ImportOverriddenMethods(CXXMethodDecl *ToMethod, |
| 9410 | CXXMethodDecl *FromMethod) { |
| 9411 | Error ImportErrors = Error::success(); |
| 9412 | for (auto *FromOverriddenMethod : FromMethod->overridden_methods()) { |
| 9413 | if (auto ImportedOrErr = import(From: FromOverriddenMethod)) |
| 9414 | ToMethod->getCanonicalDecl()->addOverriddenMethod(MD: cast<CXXMethodDecl>( |
| 9415 | Val: (*ImportedOrErr)->getCanonicalDecl())); |
| 9416 | else |
| 9417 | ImportErrors = |
| 9418 | joinErrors(E1: std::move(ImportErrors), E2: ImportedOrErr.takeError()); |
| 9419 | } |
| 9420 | return ImportErrors; |
| 9421 | } |
| 9422 | |
| 9423 | ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager, |
| 9424 | ASTContext &FromContext, FileManager &FromFileManager, |
| 9425 | bool MinimalImport, |
| 9426 | std::shared_ptr<ASTImporterSharedState> SharedState) |
| 9427 | : SharedState(SharedState), ToContext(ToContext), FromContext(FromContext), |
| 9428 | ToFileManager(ToFileManager), FromFileManager(FromFileManager), |
| 9429 | Minimal(MinimalImport), ODRHandling(ODRHandlingType::Conservative) { |
| 9430 | |
| 9431 | // Create a default state without the lookup table: LLDB case. |
| 9432 | if (!SharedState) { |
| 9433 | this->SharedState = std::make_shared<ASTImporterSharedState>(); |
| 9434 | } |
| 9435 | |
| 9436 | ImportedDecls[FromContext.getTranslationUnitDecl()] = |
| 9437 | ToContext.getTranslationUnitDecl(); |
| 9438 | } |
| 9439 | |
| 9440 | ASTImporter::~ASTImporter() = default; |
| 9441 | |
| 9442 | UnsignedOrNone ASTImporter::getFieldIndex(Decl *F) { |
| 9443 | assert(F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) && |
| 9444 | "Try to get field index for non-field." ); |
| 9445 | |
| 9446 | auto *Owner = dyn_cast<RecordDecl>(Val: F->getDeclContext()); |
| 9447 | if (!Owner) |
| 9448 | return std::nullopt; |
| 9449 | |
| 9450 | unsigned Index = 0; |
| 9451 | for (const auto *D : Owner->decls()) { |
| 9452 | if (D == F) |
| 9453 | return Index; |
| 9454 | |
| 9455 | if (isa<FieldDecl>(Val: *D) || isa<IndirectFieldDecl>(Val: *D)) |
| 9456 | ++Index; |
| 9457 | } |
| 9458 | |
| 9459 | llvm_unreachable("Field was not found in its parent context." ); |
| 9460 | |
| 9461 | return std::nullopt; |
| 9462 | } |
| 9463 | |
| 9464 | ASTImporter::FoundDeclsTy |
| 9465 | ASTImporter::findDeclsInToCtx(DeclContext *DC, DeclarationName Name) { |
| 9466 | // We search in the redecl context because of transparent contexts. |
| 9467 | // E.g. a simple C language enum is a transparent context: |
| 9468 | // enum E { A, B }; |
| 9469 | // Now if we had a global variable in the TU |
| 9470 | // int A; |
| 9471 | // then the enum constant 'A' and the variable 'A' violates ODR. |
| 9472 | // We can diagnose this only if we search in the redecl context. |
| 9473 | DeclContext *ReDC = DC->getRedeclContext(); |
| 9474 | if (SharedState->getLookupTable()) { |
| 9475 | if (ReDC->isNamespace()) { |
| 9476 | // Namespaces can be reopened. |
| 9477 | // Lookup table does not handle this, we must search here in all linked |
| 9478 | // namespaces. |
| 9479 | FoundDeclsTy Result; |
| 9480 | SmallVector<Decl *, 2> NSChain = |
| 9481 | getCanonicalForwardRedeclChain<NamespaceDecl>( |
| 9482 | D: dyn_cast<NamespaceDecl>(Val: ReDC)); |
| 9483 | for (auto *D : NSChain) { |
| 9484 | ASTImporterLookupTable::LookupResult LookupResult = |
| 9485 | SharedState->getLookupTable()->lookup(DC: dyn_cast<NamespaceDecl>(Val: D), |
| 9486 | Name); |
| 9487 | Result.append(in_start: LookupResult.begin(), in_end: LookupResult.end()); |
| 9488 | } |
| 9489 | return Result; |
| 9490 | } else { |
| 9491 | ASTImporterLookupTable::LookupResult LookupResult = |
| 9492 | SharedState->getLookupTable()->lookup(DC: ReDC, Name); |
| 9493 | return FoundDeclsTy(LookupResult.begin(), LookupResult.end()); |
| 9494 | } |
| 9495 | } else { |
| 9496 | DeclContext::lookup_result NoloadLookupResult = ReDC->noload_lookup(Name); |
| 9497 | FoundDeclsTy Result(NoloadLookupResult.begin(), NoloadLookupResult.end()); |
| 9498 | // We must search by the slow case of localUncachedLookup because that is |
| 9499 | // working even if there is no LookupPtr for the DC. We could use |
| 9500 | // DC::buildLookup() to create the LookupPtr, but that would load external |
| 9501 | // decls again, we must avoid that case. |
| 9502 | // Also, even if we had the LookupPtr, we must find Decls which are not |
| 9503 | // in the LookupPtr, so we need the slow case. |
| 9504 | // These cases are handled in ASTImporterLookupTable, but we cannot use |
| 9505 | // that with LLDB since that traverses through the AST which initiates the |
| 9506 | // load of external decls again via DC::decls(). And again, we must avoid |
| 9507 | // loading external decls during the import. |
| 9508 | if (Result.empty()) |
| 9509 | ReDC->localUncachedLookup(Name, Results&: Result); |
| 9510 | return Result; |
| 9511 | } |
| 9512 | } |
| 9513 | |
| 9514 | void ASTImporter::AddToLookupTable(Decl *ToD) { |
| 9515 | SharedState->addDeclToLookup(D: ToD); |
| 9516 | } |
| 9517 | |
| 9518 | Expected<Decl *> ASTImporter::ImportImpl(Decl *FromD) { |
| 9519 | // Import the decl using ASTNodeImporter. |
| 9520 | ASTNodeImporter Importer(*this); |
| 9521 | return Importer.Visit(D: FromD); |
| 9522 | } |
| 9523 | |
| 9524 | void ASTImporter::RegisterImportedDecl(Decl *FromD, Decl *ToD) { |
| 9525 | MapImported(From: FromD, To: ToD); |
| 9526 | } |
| 9527 | |
| 9528 | llvm::Expected<ExprWithCleanups::CleanupObject> |
| 9529 | ASTImporter::Import(ExprWithCleanups::CleanupObject From) { |
| 9530 | if (auto *CLE = From.dyn_cast<CompoundLiteralExpr *>()) { |
| 9531 | if (Expected<Expr *> R = Import(FromE: CLE)) |
| 9532 | return ExprWithCleanups::CleanupObject(cast<CompoundLiteralExpr>(Val: *R)); |
| 9533 | } |
| 9534 | |
| 9535 | // FIXME: Handle BlockDecl when we implement importing BlockExpr in |
| 9536 | // ASTNodeImporter. |
| 9537 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
| 9538 | } |
| 9539 | |
| 9540 | ExpectedTypePtr ASTImporter::Import(const Type *FromT) { |
| 9541 | if (!FromT) |
| 9542 | return FromT; |
| 9543 | |
| 9544 | // Check whether we've already imported this type. |
| 9545 | llvm::DenseMap<const Type *, const Type *>::iterator Pos = |
| 9546 | ImportedTypes.find(Val: FromT); |
| 9547 | if (Pos != ImportedTypes.end()) |
| 9548 | return Pos->second; |
| 9549 | |
| 9550 | // Import the type. |
| 9551 | ASTNodeImporter Importer(*this); |
| 9552 | ExpectedType ToTOrErr = Importer.Visit(T: FromT); |
| 9553 | if (!ToTOrErr) |
| 9554 | return ToTOrErr.takeError(); |
| 9555 | |
| 9556 | // Record the imported type. |
| 9557 | ImportedTypes[FromT] = ToTOrErr->getTypePtr(); |
| 9558 | |
| 9559 | return ToTOrErr->getTypePtr(); |
| 9560 | } |
| 9561 | |
| 9562 | Expected<QualType> ASTImporter::Import(QualType FromT) { |
| 9563 | if (FromT.isNull()) |
| 9564 | return QualType{}; |
| 9565 | |
| 9566 | ExpectedTypePtr ToTyOrErr = Import(FromT: FromT.getTypePtr()); |
| 9567 | if (!ToTyOrErr) |
| 9568 | return ToTyOrErr.takeError(); |
| 9569 | |
| 9570 | return ToContext.getQualifiedType(T: *ToTyOrErr, Qs: FromT.getLocalQualifiers()); |
| 9571 | } |
| 9572 | |
| 9573 | Expected<TypeSourceInfo *> ASTImporter::Import(TypeSourceInfo *FromTSI) { |
| 9574 | if (!FromTSI) |
| 9575 | return FromTSI; |
| 9576 | |
| 9577 | // FIXME: For now we just create a "trivial" type source info based |
| 9578 | // on the type and a single location. Implement a real version of this. |
| 9579 | ExpectedType TOrErr = Import(FromT: FromTSI->getType()); |
| 9580 | if (!TOrErr) |
| 9581 | return TOrErr.takeError(); |
| 9582 | ExpectedSLoc BeginLocOrErr = Import(FromLoc: FromTSI->getTypeLoc().getBeginLoc()); |
| 9583 | if (!BeginLocOrErr) |
| 9584 | return BeginLocOrErr.takeError(); |
| 9585 | |
| 9586 | return ToContext.getTrivialTypeSourceInfo(T: *TOrErr, Loc: *BeginLocOrErr); |
| 9587 | } |
| 9588 | |
| 9589 | namespace { |
| 9590 | // To use this object, it should be created before the new attribute is created, |
| 9591 | // and destructed after it is created. The construction already performs the |
| 9592 | // import of the data. |
| 9593 | template <typename T> struct AttrArgImporter { |
| 9594 | AttrArgImporter(const AttrArgImporter<T> &) = delete; |
| 9595 | AttrArgImporter(AttrArgImporter<T> &&) = default; |
| 9596 | AttrArgImporter<T> &operator=(const AttrArgImporter<T> &) = delete; |
| 9597 | AttrArgImporter<T> &operator=(AttrArgImporter<T> &&) = default; |
| 9598 | |
| 9599 | AttrArgImporter(ASTNodeImporter &I, Error &Err, const T &From) |
| 9600 | : To(I.importChecked(Err, From)) {} |
| 9601 | |
| 9602 | const T &value() { return To; } |
| 9603 | |
| 9604 | private: |
| 9605 | T To; |
| 9606 | }; |
| 9607 | |
| 9608 | // To use this object, it should be created before the new attribute is created, |
| 9609 | // and destructed after it is created. The construction already performs the |
| 9610 | // import of the data. The array data is accessible in a pointer form, this form |
| 9611 | // is used by the attribute classes. This object should be created once for the |
| 9612 | // array data to be imported (the array size is not imported, just copied). |
| 9613 | template <typename T> struct AttrArgArrayImporter { |
| 9614 | AttrArgArrayImporter(const AttrArgArrayImporter<T> &) = delete; |
| 9615 | AttrArgArrayImporter(AttrArgArrayImporter<T> &&) = default; |
| 9616 | AttrArgArrayImporter<T> &operator=(const AttrArgArrayImporter<T> &) = delete; |
| 9617 | AttrArgArrayImporter<T> &operator=(AttrArgArrayImporter<T> &&) = default; |
| 9618 | |
| 9619 | AttrArgArrayImporter(ASTNodeImporter &I, Error &Err, |
| 9620 | const llvm::iterator_range<T *> &From, |
| 9621 | unsigned ArraySize) { |
| 9622 | if (Err) |
| 9623 | return; |
| 9624 | To.reserve(ArraySize); |
| 9625 | Err = I.ImportContainerChecked(From, To); |
| 9626 | } |
| 9627 | |
| 9628 | T *value() { return To.data(); } |
| 9629 | |
| 9630 | private: |
| 9631 | llvm::SmallVector<T, 2> To; |
| 9632 | }; |
| 9633 | |
| 9634 | class AttrImporter { |
| 9635 | Error Err{Error::success()}; |
| 9636 | Attr *ToAttr = nullptr; |
| 9637 | ASTImporter &Importer; |
| 9638 | ASTNodeImporter NImporter; |
| 9639 | |
| 9640 | public: |
| 9641 | AttrImporter(ASTImporter &I) : Importer(I), NImporter(I) {} |
| 9642 | |
| 9643 | // Create an "importer" for an attribute parameter. |
| 9644 | // Result of the 'value()' of that object is to be passed to the function |
| 9645 | // 'importAttr', in the order that is expected by the attribute class. |
| 9646 | template <class T> AttrArgImporter<T> importArg(const T &From) { |
| 9647 | return AttrArgImporter<T>(NImporter, Err, From); |
| 9648 | } |
| 9649 | |
| 9650 | // Create an "importer" for an attribute parameter that has array type. |
| 9651 | // Result of the 'value()' of that object is to be passed to the function |
| 9652 | // 'importAttr', then the size of the array as next argument. |
| 9653 | template <typename T> |
| 9654 | AttrArgArrayImporter<T> importArrayArg(const llvm::iterator_range<T *> &From, |
| 9655 | unsigned ArraySize) { |
| 9656 | return AttrArgArrayImporter<T>(NImporter, Err, From, ArraySize); |
| 9657 | } |
| 9658 | |
| 9659 | // Create an attribute object with the specified arguments. |
| 9660 | // The 'FromAttr' is the original (not imported) attribute, the 'ImportedArg' |
| 9661 | // should be values that are passed to the 'Create' function of the attribute. |
| 9662 | // (The 'Create' with 'ASTContext' first and 'AttributeCommonInfo' last is |
| 9663 | // used here.) As much data is copied or imported from the old attribute |
| 9664 | // as possible. The passed arguments should be already imported. |
| 9665 | // If an import error happens, the internal error is set to it, and any |
| 9666 | // further import attempt is ignored. |
| 9667 | template <typename T, typename... Arg> |
| 9668 | void importAttr(const T *FromAttr, Arg &&...ImportedArg) { |
| 9669 | static_assert(std::is_base_of<Attr, T>::value, |
| 9670 | "T should be subclass of Attr." ); |
| 9671 | assert(!ToAttr && "Use one AttrImporter to import one Attribute object." ); |
| 9672 | |
| 9673 | const IdentifierInfo *ToAttrName = Importer.Import(FromAttr->getAttrName()); |
| 9674 | const IdentifierInfo *ToScopeName = |
| 9675 | Importer.Import(FromAttr->getScopeName()); |
| 9676 | SourceRange ToAttrRange = |
| 9677 | NImporter.importChecked(Err, FromAttr->getRange()); |
| 9678 | SourceLocation ToScopeLoc = |
| 9679 | NImporter.importChecked(Err, FromAttr->getScopeLoc()); |
| 9680 | |
| 9681 | if (Err) |
| 9682 | return; |
| 9683 | |
| 9684 | AttributeCommonInfo ToI( |
| 9685 | ToAttrName, AttributeScopeInfo(ToScopeName, ToScopeLoc), ToAttrRange, |
| 9686 | FromAttr->getParsedKind(), FromAttr->getForm()); |
| 9687 | // The "SemanticSpelling" is not needed to be passed to the constructor. |
| 9688 | // That value is recalculated from the SpellingListIndex if needed. |
| 9689 | ToAttr = T::Create(Importer.getToContext(), |
| 9690 | std::forward<Arg>(ImportedArg)..., ToI); |
| 9691 | |
| 9692 | ToAttr->setImplicit(FromAttr->isImplicit()); |
| 9693 | ToAttr->setPackExpansion(FromAttr->isPackExpansion()); |
| 9694 | if (auto *ToInheritableAttr = dyn_cast<InheritableAttr>(Val: ToAttr)) |
| 9695 | ToInheritableAttr->setInherited(FromAttr->isInherited()); |
| 9696 | } |
| 9697 | |
| 9698 | // Create a clone of the 'FromAttr' and import its source range only. |
| 9699 | // This causes objects with invalid references to be created if the 'FromAttr' |
| 9700 | // contains other data that should be imported. |
| 9701 | void cloneAttr(const Attr *FromAttr) { |
| 9702 | assert(!ToAttr && "Use one AttrImporter to import one Attribute object." ); |
| 9703 | |
| 9704 | SourceRange ToRange = NImporter.importChecked(Err, From: FromAttr->getRange()); |
| 9705 | if (Err) |
| 9706 | return; |
| 9707 | |
| 9708 | ToAttr = FromAttr->clone(C&: Importer.getToContext()); |
| 9709 | ToAttr->setRange(ToRange); |
| 9710 | ToAttr->setAttrName(Importer.Import(FromId: FromAttr->getAttrName())); |
| 9711 | } |
| 9712 | |
| 9713 | // Get the result of the previous import attempt (can be used only once). |
| 9714 | llvm::Expected<Attr *> getResult() && { |
| 9715 | if (Err) |
| 9716 | return std::move(Err); |
| 9717 | assert(ToAttr && "Attribute should be created." ); |
| 9718 | return ToAttr; |
| 9719 | } |
| 9720 | }; |
| 9721 | } // namespace |
| 9722 | |
| 9723 | Expected<Attr *> ASTImporter::Import(const Attr *FromAttr) { |
| 9724 | AttrImporter AI(*this); |
| 9725 | |
| 9726 | // FIXME: Is there some kind of AttrVisitor to use here? |
| 9727 | switch (FromAttr->getKind()) { |
| 9728 | case attr::Aligned: { |
| 9729 | auto *From = cast<AlignedAttr>(Val: FromAttr); |
| 9730 | if (From->isAlignmentExpr()) |
| 9731 | AI.importAttr(FromAttr: From, ImportedArg: true, ImportedArg: AI.importArg(From: From->getAlignmentExpr()).value()); |
| 9732 | else |
| 9733 | AI.importAttr(FromAttr: From, ImportedArg: false, |
| 9734 | ImportedArg: AI.importArg(From: From->getAlignmentType()).value()); |
| 9735 | break; |
| 9736 | } |
| 9737 | |
| 9738 | case attr::AlignValue: { |
| 9739 | auto *From = cast<AlignValueAttr>(Val: FromAttr); |
| 9740 | AI.importAttr(FromAttr: From, ImportedArg: AI.importArg(From: From->getAlignment()).value()); |
| 9741 | break; |
| 9742 | } |
| 9743 | |
| 9744 | case attr::Format: { |
| 9745 | const auto *From = cast<FormatAttr>(Val: FromAttr); |
| 9746 | AI.importAttr(FromAttr: From, ImportedArg: Import(FromId: From->getType()), ImportedArg: From->getFormatIdx(), |
| 9747 | ImportedArg: From->getFirstArg()); |
| 9748 | break; |
| 9749 | } |
| 9750 | |
| 9751 | case attr::EnableIf: { |
| 9752 | const auto *From = cast<EnableIfAttr>(Val: FromAttr); |
| 9753 | AI.importAttr(FromAttr: From, ImportedArg: AI.importArg(From: From->getCond()).value(), |
| 9754 | ImportedArg: From->getMessage()); |
| 9755 | break; |
| 9756 | } |
| 9757 | |
| 9758 | case attr::AssertCapability: { |
| 9759 | const auto *From = cast<AssertCapabilityAttr>(Val: FromAttr); |
| 9760 | AI.importAttr(FromAttr: From, |
| 9761 | ImportedArg: AI.importArrayArg(From: From->args(), ArraySize: From->args_size()).value(), |
| 9762 | ImportedArg: From->args_size()); |
| 9763 | break; |
| 9764 | } |
| 9765 | case attr::AcquireCapability: { |
| 9766 | const auto *From = cast<AcquireCapabilityAttr>(Val: FromAttr); |
| 9767 | AI.importAttr(FromAttr: From, |
| 9768 | ImportedArg: AI.importArrayArg(From: From->args(), ArraySize: From->args_size()).value(), |
| 9769 | ImportedArg: From->args_size()); |
| 9770 | break; |
| 9771 | } |
| 9772 | case attr::TryAcquireCapability: { |
| 9773 | const auto *From = cast<TryAcquireCapabilityAttr>(Val: FromAttr); |
| 9774 | AI.importAttr(FromAttr: From, ImportedArg: AI.importArg(From: From->getSuccessValue()).value(), |
| 9775 | ImportedArg: AI.importArrayArg(From: From->args(), ArraySize: From->args_size()).value(), |
| 9776 | ImportedArg: From->args_size()); |
| 9777 | break; |
| 9778 | } |
| 9779 | case attr::ReleaseCapability: { |
| 9780 | const auto *From = cast<ReleaseCapabilityAttr>(Val: FromAttr); |
| 9781 | AI.importAttr(FromAttr: From, |
| 9782 | ImportedArg: AI.importArrayArg(From: From->args(), ArraySize: From->args_size()).value(), |
| 9783 | ImportedArg: From->args_size()); |
| 9784 | break; |
| 9785 | } |
| 9786 | case attr::RequiresCapability: { |
| 9787 | const auto *From = cast<RequiresCapabilityAttr>(Val: FromAttr); |
| 9788 | AI.importAttr(FromAttr: From, |
| 9789 | ImportedArg: AI.importArrayArg(From: From->args(), ArraySize: From->args_size()).value(), |
| 9790 | ImportedArg: From->args_size()); |
| 9791 | break; |
| 9792 | } |
| 9793 | case attr::GuardedBy: { |
| 9794 | const auto *From = cast<GuardedByAttr>(Val: FromAttr); |
| 9795 | AI.importAttr(FromAttr: From, |
| 9796 | ImportedArg: AI.importArrayArg(From: From->args(), ArraySize: From->args_size()).value(), |
| 9797 | ImportedArg: From->args_size()); |
| 9798 | break; |
| 9799 | } |
| 9800 | case attr::PtGuardedBy: { |
| 9801 | const auto *From = cast<PtGuardedByAttr>(Val: FromAttr); |
| 9802 | AI.importAttr(FromAttr: From, |
| 9803 | ImportedArg: AI.importArrayArg(From: From->args(), ArraySize: From->args_size()).value(), |
| 9804 | ImportedArg: From->args_size()); |
| 9805 | break; |
| 9806 | } |
| 9807 | case attr::AcquiredAfter: { |
| 9808 | const auto *From = cast<AcquiredAfterAttr>(Val: FromAttr); |
| 9809 | AI.importAttr(FromAttr: From, |
| 9810 | ImportedArg: AI.importArrayArg(From: From->args(), ArraySize: From->args_size()).value(), |
| 9811 | ImportedArg: From->args_size()); |
| 9812 | break; |
| 9813 | } |
| 9814 | case attr::AcquiredBefore: { |
| 9815 | const auto *From = cast<AcquiredBeforeAttr>(Val: FromAttr); |
| 9816 | AI.importAttr(FromAttr: From, |
| 9817 | ImportedArg: AI.importArrayArg(From: From->args(), ArraySize: From->args_size()).value(), |
| 9818 | ImportedArg: From->args_size()); |
| 9819 | break; |
| 9820 | } |
| 9821 | case attr::LockReturned: { |
| 9822 | const auto *From = cast<LockReturnedAttr>(Val: FromAttr); |
| 9823 | AI.importAttr(FromAttr: From, ImportedArg: AI.importArg(From: From->getArg()).value()); |
| 9824 | break; |
| 9825 | } |
| 9826 | case attr::LocksExcluded: { |
| 9827 | const auto *From = cast<LocksExcludedAttr>(Val: FromAttr); |
| 9828 | AI.importAttr(FromAttr: From, |
| 9829 | ImportedArg: AI.importArrayArg(From: From->args(), ArraySize: From->args_size()).value(), |
| 9830 | ImportedArg: From->args_size()); |
| 9831 | break; |
| 9832 | } |
| 9833 | default: { |
| 9834 | // The default branch works for attributes that have no arguments to import. |
| 9835 | // FIXME: Handle every attribute type that has arguments of type to import |
| 9836 | // (most often Expr* or Decl* or type) in the switch above. |
| 9837 | AI.cloneAttr(FromAttr); |
| 9838 | break; |
| 9839 | } |
| 9840 | } |
| 9841 | |
| 9842 | return std::move(AI).getResult(); |
| 9843 | } |
| 9844 | |
| 9845 | Decl *ASTImporter::GetAlreadyImportedOrNull(const Decl *FromD) const { |
| 9846 | return ImportedDecls.lookup(Val: FromD); |
| 9847 | } |
| 9848 | |
| 9849 | TranslationUnitDecl *ASTImporter::GetFromTU(Decl *ToD) { |
| 9850 | auto FromDPos = ImportedFromDecls.find(Val: ToD); |
| 9851 | if (FromDPos == ImportedFromDecls.end()) |
| 9852 | return nullptr; |
| 9853 | return FromDPos->second->getTranslationUnitDecl(); |
| 9854 | } |
| 9855 | |
| 9856 | Expected<Decl *> ASTImporter::Import(Decl *FromD) { |
| 9857 | if (!FromD) |
| 9858 | return nullptr; |
| 9859 | |
| 9860 | // Push FromD to the stack, and remove that when we return. |
| 9861 | ImportPath.push(D: FromD); |
| 9862 | llvm::scope_exit ImportPathBuilder([this]() { ImportPath.pop(); }); |
| 9863 | |
| 9864 | // Check whether there was a previous failed import. |
| 9865 | // If yes return the existing error. |
| 9866 | if (auto Error = getImportDeclErrorIfAny(FromD)) |
| 9867 | return make_error<ASTImportError>(Args&: *Error); |
| 9868 | |
| 9869 | // Check whether we've already imported this declaration. |
| 9870 | Decl *ToD = GetAlreadyImportedOrNull(FromD); |
| 9871 | if (ToD) { |
| 9872 | // Already imported (possibly from another TU) and with an error. |
| 9873 | if (auto Error = SharedState->getImportDeclErrorIfAny(ToD)) { |
| 9874 | setImportDeclError(From: FromD, Error: *Error); |
| 9875 | return make_error<ASTImportError>(Args&: *Error); |
| 9876 | } |
| 9877 | |
| 9878 | // If FromD has some updated flags after last import, apply it. |
| 9879 | updateFlags(From: FromD, To: ToD); |
| 9880 | // If we encounter a cycle during an import then we save the relevant part |
| 9881 | // of the import path associated to the Decl. |
| 9882 | if (ImportPath.hasCycleAtBack()) |
| 9883 | SavedImportPaths[FromD].push_back(Elt: ImportPath.copyCycleAtBack()); |
| 9884 | return ToD; |
| 9885 | } |
| 9886 | |
| 9887 | // Import the declaration. |
| 9888 | ExpectedDecl ToDOrErr = ImportImpl(FromD); |
| 9889 | if (!ToDOrErr) { |
| 9890 | // Failed to import. |
| 9891 | |
| 9892 | auto Pos = ImportedDecls.find(Val: FromD); |
| 9893 | bool ToDWasCreated = Pos != ImportedDecls.end(); |
| 9894 | // Capture the mapped decl before erasing: the iterator is invalidated by |
| 9895 | // the erase below under backward-shift deletion, but it is still needed |
| 9896 | // further down to record the import error. |
| 9897 | Decl *CreatedToD = ToDWasCreated ? Pos->second : nullptr; |
| 9898 | if (ToDWasCreated) { |
| 9899 | // Import failed after the object was created. |
| 9900 | // Remove all references to it. |
| 9901 | auto *ToD = CreatedToD; |
| 9902 | ImportedDecls.erase(I: Pos); |
| 9903 | |
| 9904 | // ImportedDecls and ImportedFromDecls are not symmetric. It may happen |
| 9905 | // (e.g. with namespaces) that several decls from the 'from' context are |
| 9906 | // mapped to the same decl in the 'to' context. If we removed entries |
| 9907 | // from the LookupTable here then we may end up removing them multiple |
| 9908 | // times. |
| 9909 | |
| 9910 | // The Lookuptable contains decls only which are in the 'to' context. |
| 9911 | // Remove from the Lookuptable only if it is *imported* into the 'to' |
| 9912 | // context (and do not remove it if it was added during the initial |
| 9913 | // traverse of the 'to' context). |
| 9914 | auto PosF = ImportedFromDecls.find(Val: ToD); |
| 9915 | if (PosF != ImportedFromDecls.end()) { |
| 9916 | // In the case of TypedefNameDecl we create the Decl first and only |
| 9917 | // then we import and set its DeclContext. So, the DC might not be set |
| 9918 | // when we reach here. |
| 9919 | if (ToD->getDeclContext()) |
| 9920 | SharedState->removeDeclFromLookup(D: ToD); |
| 9921 | ImportedFromDecls.erase(I: PosF); |
| 9922 | } |
| 9923 | |
| 9924 | // FIXME: AST may contain remaining references to the failed object. |
| 9925 | // However, the ImportDeclErrors in the shared state contains all the |
| 9926 | // failed objects together with their error. |
| 9927 | } |
| 9928 | |
| 9929 | // Error encountered for the first time. |
| 9930 | // After takeError the error is not usable any more in ToDOrErr. |
| 9931 | // Get a copy of the error object (any more simple solution for this?). |
| 9932 | ASTImportError ErrOut; |
| 9933 | handleAllErrors(E: ToDOrErr.takeError(), |
| 9934 | Handlers: [&ErrOut](const ASTImportError &E) { ErrOut = E; }); |
| 9935 | setImportDeclError(From: FromD, Error: ErrOut); |
| 9936 | // Set the error for the mapped to Decl, which is in the "to" context. |
| 9937 | if (ToDWasCreated) |
| 9938 | SharedState->setImportDeclError(To: CreatedToD, Error: ErrOut); |
| 9939 | |
| 9940 | // Set the error for all nodes which have been created before we |
| 9941 | // recognized the error. |
| 9942 | for (const auto &Path : SavedImportPaths[FromD]) { |
| 9943 | // The import path contains import-dependency nodes first. |
| 9944 | // Save the node that was imported as dependency of the current node. |
| 9945 | Decl *PrevFromDi = FromD; |
| 9946 | for (Decl *FromDi : Path) { |
| 9947 | // Begin and end of the path equals 'FromD', skip it. |
| 9948 | if (FromDi == FromD) |
| 9949 | continue; |
| 9950 | // We should not set import error on a node and all following nodes in |
| 9951 | // the path if child import errors are ignored. |
| 9952 | if (ChildErrorHandlingStrategy(FromDi).ignoreChildErrorOnParent( |
| 9953 | FromChildD: PrevFromDi)) |
| 9954 | break; |
| 9955 | PrevFromDi = FromDi; |
| 9956 | setImportDeclError(From: FromDi, Error: ErrOut); |
| 9957 | //FIXME Should we remove these Decls from ImportedDecls? |
| 9958 | // Set the error for the mapped to Decl, which is in the "to" context. |
| 9959 | auto Ii = ImportedDecls.find(Val: FromDi); |
| 9960 | if (Ii != ImportedDecls.end()) |
| 9961 | SharedState->setImportDeclError(To: Ii->second, Error: ErrOut); |
| 9962 | // FIXME Should we remove these Decls from the LookupTable, |
| 9963 | // and from ImportedFromDecls? |
| 9964 | } |
| 9965 | } |
| 9966 | SavedImportPaths.erase(Val: FromD); |
| 9967 | |
| 9968 | // Do not return ToDOrErr, error was taken out of it. |
| 9969 | return make_error<ASTImportError>(Args&: ErrOut); |
| 9970 | } |
| 9971 | |
| 9972 | ToD = *ToDOrErr; |
| 9973 | |
| 9974 | // FIXME: Handle the "already imported with error" case. We can get here |
| 9975 | // nullptr only if GetImportedOrCreateDecl returned nullptr (after a |
| 9976 | // previously failed create was requested). |
| 9977 | // Later GetImportedOrCreateDecl can be updated to return the error. |
| 9978 | if (!ToD) { |
| 9979 | auto Err = getImportDeclErrorIfAny(FromD); |
| 9980 | assert(Err); |
| 9981 | return make_error<ASTImportError>(Args&: *Err); |
| 9982 | } |
| 9983 | |
| 9984 | // We could import from the current TU without error. But previously we |
| 9985 | // already had imported a Decl as `ToD` from another TU (with another |
| 9986 | // ASTImporter object) and with an error. |
| 9987 | if (auto Error = SharedState->getImportDeclErrorIfAny(ToD)) { |
| 9988 | setImportDeclError(From: FromD, Error: *Error); |
| 9989 | return make_error<ASTImportError>(Args&: *Error); |
| 9990 | } |
| 9991 | // Make sure that ImportImpl registered the imported decl. |
| 9992 | assert(ImportedDecls.count(FromD) != 0 && "Missing call to MapImported?" ); |
| 9993 | |
| 9994 | if (FromD->hasAttrs()) |
| 9995 | for (const Attr *FromAttr : FromD->getAttrs()) { |
| 9996 | auto ToAttrOrErr = Import(FromAttr); |
| 9997 | if (ToAttrOrErr) |
| 9998 | ToD->addAttr(A: *ToAttrOrErr); |
| 9999 | else |
| 10000 | return ToAttrOrErr.takeError(); |
| 10001 | } |
| 10002 | |
| 10003 | // Notify subclasses. |
| 10004 | Imported(From: FromD, To: ToD); |
| 10005 | |
| 10006 | updateFlags(From: FromD, To: ToD); |
| 10007 | SavedImportPaths.erase(Val: FromD); |
| 10008 | return ToDOrErr; |
| 10009 | } |
| 10010 | |
| 10011 | llvm::Expected<InheritedConstructor> |
| 10012 | ASTImporter::Import(const InheritedConstructor &From) { |
| 10013 | return ASTNodeImporter(*this).ImportInheritedConstructor(From); |
| 10014 | } |
| 10015 | |
| 10016 | Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) { |
| 10017 | if (!FromDC) |
| 10018 | return FromDC; |
| 10019 | |
| 10020 | ExpectedDecl ToDCOrErr = Import(FromD: cast<Decl>(Val: FromDC)); |
| 10021 | if (!ToDCOrErr) |
| 10022 | return ToDCOrErr.takeError(); |
| 10023 | auto *ToDC = cast<DeclContext>(Val: *ToDCOrErr); |
| 10024 | |
| 10025 | // When we're using a record/enum/Objective-C class/protocol as a context, we |
| 10026 | // need it to have a definition. |
| 10027 | if (auto *ToRecord = dyn_cast<RecordDecl>(Val: ToDC)) { |
| 10028 | auto *FromRecord = cast<RecordDecl>(Val: FromDC); |
| 10029 | if (ToRecord->isCompleteDefinition()) |
| 10030 | return ToDC; |
| 10031 | |
| 10032 | // If FromRecord is not defined we need to force it to be. |
| 10033 | // Simply calling CompleteDecl(...) for a RecordDecl will break some cases |
| 10034 | // it will start the definition but we never finish it. |
| 10035 | // If there are base classes they won't be imported and we will |
| 10036 | // be missing anything that we inherit from those bases. |
| 10037 | if (FromRecord->getASTContext().getExternalSource() && |
| 10038 | !FromRecord->isCompleteDefinition()) |
| 10039 | FromRecord->getASTContext().getExternalSource()->CompleteType(Tag: FromRecord); |
| 10040 | |
| 10041 | if (FromRecord->isCompleteDefinition()) |
| 10042 | if (Error Err = ASTNodeImporter(*this).ImportDefinition( |
| 10043 | From: FromRecord, To: ToRecord, Kind: ASTNodeImporter::IDK_Basic)) |
| 10044 | return std::move(Err); |
| 10045 | } else if (auto *ToEnum = dyn_cast<EnumDecl>(Val: ToDC)) { |
| 10046 | auto * = cast<EnumDecl>(Val: FromDC); |
| 10047 | if (ToEnum->isCompleteDefinition()) { |
| 10048 | // Do nothing. |
| 10049 | } else if (FromEnum->isCompleteDefinition()) { |
| 10050 | if (Error Err = ASTNodeImporter(*this).ImportDefinition( |
| 10051 | From: FromEnum, To: ToEnum, Kind: ASTNodeImporter::IDK_Basic)) |
| 10052 | return std::move(Err); |
| 10053 | } else { |
| 10054 | CompleteDecl(D: ToEnum); |
| 10055 | } |
| 10056 | } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(Val: ToDC)) { |
| 10057 | auto *FromClass = cast<ObjCInterfaceDecl>(Val: FromDC); |
| 10058 | if (ToClass->getDefinition()) { |
| 10059 | // Do nothing. |
| 10060 | } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) { |
| 10061 | if (Error Err = ASTNodeImporter(*this).ImportDefinition( |
| 10062 | From: FromDef, To: ToClass, Kind: ASTNodeImporter::IDK_Basic)) |
| 10063 | return std::move(Err); |
| 10064 | } else { |
| 10065 | CompleteDecl(D: ToClass); |
| 10066 | } |
| 10067 | } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(Val: ToDC)) { |
| 10068 | auto *FromProto = cast<ObjCProtocolDecl>(Val: FromDC); |
| 10069 | if (ToProto->getDefinition()) { |
| 10070 | // Do nothing. |
| 10071 | } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) { |
| 10072 | if (Error Err = ASTNodeImporter(*this).ImportDefinition( |
| 10073 | From: FromDef, To: ToProto, Kind: ASTNodeImporter::IDK_Basic)) |
| 10074 | return std::move(Err); |
| 10075 | } else { |
| 10076 | CompleteDecl(D: ToProto); |
| 10077 | } |
| 10078 | } |
| 10079 | |
| 10080 | return ToDC; |
| 10081 | } |
| 10082 | |
| 10083 | Expected<Expr *> ASTImporter::Import(Expr *FromE) { |
| 10084 | if (ExpectedStmt ToSOrErr = Import(FromS: cast_or_null<Stmt>(Val: FromE))) |
| 10085 | return cast_or_null<Expr>(Val: *ToSOrErr); |
| 10086 | else |
| 10087 | return ToSOrErr.takeError(); |
| 10088 | } |
| 10089 | |
| 10090 | Expected<Stmt *> ASTImporter::Import(Stmt *FromS) { |
| 10091 | if (!FromS) |
| 10092 | return nullptr; |
| 10093 | |
| 10094 | // Check whether we've already imported this statement. |
| 10095 | llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(Val: FromS); |
| 10096 | if (Pos != ImportedStmts.end()) |
| 10097 | return Pos->second; |
| 10098 | |
| 10099 | // Import the statement. |
| 10100 | ASTNodeImporter Importer(*this); |
| 10101 | ExpectedStmt ToSOrErr = Importer.Visit(S: FromS); |
| 10102 | if (!ToSOrErr) |
| 10103 | return ToSOrErr; |
| 10104 | |
| 10105 | if (auto *ToE = dyn_cast<Expr>(Val: *ToSOrErr)) { |
| 10106 | auto *FromE = cast<Expr>(Val: FromS); |
| 10107 | // Copy ExprBitfields, which may not be handled in Expr subclasses |
| 10108 | // constructors. |
| 10109 | ToE->setValueKind(FromE->getValueKind()); |
| 10110 | ToE->setObjectKind(FromE->getObjectKind()); |
| 10111 | ToE->setDependence(FromE->getDependence()); |
| 10112 | } |
| 10113 | |
| 10114 | // Record the imported statement object. |
| 10115 | ImportedStmts[FromS] = *ToSOrErr; |
| 10116 | return ToSOrErr; |
| 10117 | } |
| 10118 | |
| 10119 | Expected<NestedNameSpecifier> ASTImporter::Import(NestedNameSpecifier FromNNS) { |
| 10120 | switch (FromNNS.getKind()) { |
| 10121 | case NestedNameSpecifier::Kind::Null: |
| 10122 | case NestedNameSpecifier::Kind::Global: |
| 10123 | return FromNNS; |
| 10124 | case NestedNameSpecifier::Kind::Namespace: { |
| 10125 | auto [Namespace, Prefix] = FromNNS.getAsNamespaceAndPrefix(); |
| 10126 | auto NSOrErr = Import(FromD: Namespace); |
| 10127 | if (!NSOrErr) |
| 10128 | return NSOrErr.takeError(); |
| 10129 | auto PrefixOrErr = Import(FromNNS: Prefix); |
| 10130 | if (!PrefixOrErr) |
| 10131 | return PrefixOrErr.takeError(); |
| 10132 | return NestedNameSpecifier(ToContext, cast<NamespaceBaseDecl>(Val: *NSOrErr), |
| 10133 | *PrefixOrErr); |
| 10134 | } |
| 10135 | case NestedNameSpecifier::Kind::MicrosoftSuper: |
| 10136 | if (ExpectedDecl RDOrErr = Import(FromD: FromNNS.getAsMicrosoftSuper())) |
| 10137 | return NestedNameSpecifier(cast<CXXRecordDecl>(Val: *RDOrErr)); |
| 10138 | else |
| 10139 | return RDOrErr.takeError(); |
| 10140 | case NestedNameSpecifier::Kind::Type: |
| 10141 | if (ExpectedTypePtr TyOrErr = Import(FromT: FromNNS.getAsType())) { |
| 10142 | return NestedNameSpecifier(*TyOrErr); |
| 10143 | } else { |
| 10144 | return TyOrErr.takeError(); |
| 10145 | } |
| 10146 | } |
| 10147 | llvm_unreachable("Invalid nested name specifier kind" ); |
| 10148 | } |
| 10149 | |
| 10150 | Expected<NestedNameSpecifierLoc> |
| 10151 | ASTImporter::Import(NestedNameSpecifierLoc FromNNS) { |
| 10152 | // Copied from NestedNameSpecifier mostly. |
| 10153 | SmallVector<NestedNameSpecifierLoc , 8> NestedNames; |
| 10154 | NestedNameSpecifierLoc NNS = FromNNS; |
| 10155 | |
| 10156 | // Push each of the nested-name-specifiers's onto a stack for |
| 10157 | // serialization in reverse order. |
| 10158 | while (NNS) { |
| 10159 | NestedNames.push_back(Elt: NNS); |
| 10160 | NNS = NNS.getAsNamespaceAndPrefix().Prefix; |
| 10161 | } |
| 10162 | |
| 10163 | NestedNameSpecifierLocBuilder Builder; |
| 10164 | |
| 10165 | while (!NestedNames.empty()) { |
| 10166 | NNS = NestedNames.pop_back_val(); |
| 10167 | NestedNameSpecifier Spec = std::nullopt; |
| 10168 | if (Error Err = importInto(To&: Spec, From: NNS.getNestedNameSpecifier())) |
| 10169 | return std::move(Err); |
| 10170 | |
| 10171 | NestedNameSpecifier::Kind Kind = Spec.getKind(); |
| 10172 | |
| 10173 | SourceLocation ToLocalBeginLoc, ToLocalEndLoc; |
| 10174 | if (Kind != NestedNameSpecifier::Kind::MicrosoftSuper) { |
| 10175 | if (Error Err = importInto(To&: ToLocalBeginLoc, From: NNS.getLocalBeginLoc())) |
| 10176 | return std::move(Err); |
| 10177 | |
| 10178 | if (Kind != NestedNameSpecifier::Kind::Global) |
| 10179 | if (Error Err = importInto(To&: ToLocalEndLoc, From: NNS.getLocalEndLoc())) |
| 10180 | return std::move(Err); |
| 10181 | } |
| 10182 | |
| 10183 | switch (Kind) { |
| 10184 | case NestedNameSpecifier::Kind::Namespace: |
| 10185 | Builder.Extend(Context&: getToContext(), Namespace: Spec.getAsNamespaceAndPrefix().Namespace, |
| 10186 | NamespaceLoc: ToLocalBeginLoc, ColonColonLoc: ToLocalEndLoc); |
| 10187 | break; |
| 10188 | |
| 10189 | case NestedNameSpecifier::Kind::Type: { |
| 10190 | SourceLocation ToTLoc; |
| 10191 | if (Error Err = importInto(To&: ToTLoc, From: NNS.castAsTypeLoc().getBeginLoc())) |
| 10192 | return std::move(Err); |
| 10193 | TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo( |
| 10194 | T: QualType(Spec.getAsType(), 0), Loc: ToTLoc); |
| 10195 | Builder.Make(Context&: getToContext(), TL: TSI->getTypeLoc(), ColonColonLoc: ToLocalEndLoc); |
| 10196 | break; |
| 10197 | } |
| 10198 | |
| 10199 | case NestedNameSpecifier::Kind::Global: |
| 10200 | Builder.MakeGlobal(Context&: getToContext(), ColonColonLoc: ToLocalBeginLoc); |
| 10201 | break; |
| 10202 | |
| 10203 | case NestedNameSpecifier::Kind::MicrosoftSuper: { |
| 10204 | auto ToSourceRangeOrErr = Import(FromRange: NNS.getSourceRange()); |
| 10205 | if (!ToSourceRangeOrErr) |
| 10206 | return ToSourceRangeOrErr.takeError(); |
| 10207 | |
| 10208 | Builder.MakeMicrosoftSuper(Context&: getToContext(), RD: Spec.getAsMicrosoftSuper(), |
| 10209 | SuperLoc: ToSourceRangeOrErr->getBegin(), |
| 10210 | ColonColonLoc: ToSourceRangeOrErr->getEnd()); |
| 10211 | break; |
| 10212 | } |
| 10213 | case NestedNameSpecifier::Kind::Null: |
| 10214 | llvm_unreachable("unexpected null nested name specifier" ); |
| 10215 | } |
| 10216 | } |
| 10217 | |
| 10218 | return Builder.getWithLocInContext(Context&: getToContext()); |
| 10219 | } |
| 10220 | |
| 10221 | Expected<TemplateName> ASTImporter::Import(TemplateName From) { |
| 10222 | switch (From.getKind()) { |
| 10223 | case TemplateName::Template: |
| 10224 | if (ExpectedDecl ToTemplateOrErr = Import(FromD: From.getAsTemplateDecl())) |
| 10225 | return TemplateName(cast<TemplateDecl>(Val: (*ToTemplateOrErr)->getCanonicalDecl())); |
| 10226 | else |
| 10227 | return ToTemplateOrErr.takeError(); |
| 10228 | |
| 10229 | case TemplateName::OverloadedTemplate: { |
| 10230 | OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate(); |
| 10231 | UnresolvedSet<2> ToTemplates; |
| 10232 | for (auto *I : *FromStorage) { |
| 10233 | if (auto ToOrErr = Import(FromD: I)) |
| 10234 | ToTemplates.addDecl(D: cast<NamedDecl>(Val: *ToOrErr)); |
| 10235 | else |
| 10236 | return ToOrErr.takeError(); |
| 10237 | } |
| 10238 | return ToContext.getOverloadedTemplateName(Begin: ToTemplates.begin(), |
| 10239 | End: ToTemplates.end()); |
| 10240 | } |
| 10241 | |
| 10242 | case TemplateName::AssumedTemplate: { |
| 10243 | AssumedTemplateStorage *FromStorage = From.getAsAssumedTemplateName(); |
| 10244 | auto DeclNameOrErr = Import(FromName: FromStorage->getDeclName()); |
| 10245 | if (!DeclNameOrErr) |
| 10246 | return DeclNameOrErr.takeError(); |
| 10247 | return ToContext.getAssumedTemplateName(Name: *DeclNameOrErr); |
| 10248 | } |
| 10249 | |
| 10250 | case TemplateName::QualifiedTemplate: { |
| 10251 | QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName(); |
| 10252 | auto QualifierOrErr = Import(FromNNS: QTN->getQualifier()); |
| 10253 | if (!QualifierOrErr) |
| 10254 | return QualifierOrErr.takeError(); |
| 10255 | auto TNOrErr = Import(From: QTN->getUnderlyingTemplate()); |
| 10256 | if (!TNOrErr) |
| 10257 | return TNOrErr.takeError(); |
| 10258 | return ToContext.getQualifiedTemplateName( |
| 10259 | Qualifier: *QualifierOrErr, TemplateKeyword: QTN->hasTemplateKeyword(), Template: *TNOrErr); |
| 10260 | } |
| 10261 | |
| 10262 | case TemplateName::DependentTemplate: { |
| 10263 | DependentTemplateName *DTN = From.getAsDependentTemplateName(); |
| 10264 | auto QualifierOrErr = Import(FromNNS: DTN->getQualifier()); |
| 10265 | if (!QualifierOrErr) |
| 10266 | return QualifierOrErr.takeError(); |
| 10267 | return ToContext.getDependentTemplateName( |
| 10268 | Name: {*QualifierOrErr, Import(FromIO: DTN->getName()), DTN->hasTemplateKeyword()}); |
| 10269 | } |
| 10270 | |
| 10271 | case TemplateName::SubstTemplateTemplateParm: { |
| 10272 | SubstTemplateTemplateParmStorage *Subst = |
| 10273 | From.getAsSubstTemplateTemplateParm(); |
| 10274 | auto ReplacementOrErr = Import(From: Subst->getReplacement()); |
| 10275 | if (!ReplacementOrErr) |
| 10276 | return ReplacementOrErr.takeError(); |
| 10277 | |
| 10278 | auto AssociatedDeclOrErr = Import(FromD: Subst->getAssociatedDecl()); |
| 10279 | if (!AssociatedDeclOrErr) |
| 10280 | return AssociatedDeclOrErr.takeError(); |
| 10281 | |
| 10282 | return ToContext.getSubstTemplateTemplateParm( |
| 10283 | replacement: *ReplacementOrErr, AssociatedDecl: *AssociatedDeclOrErr, Index: Subst->getIndex(), |
| 10284 | PackIndex: Subst->getPackIndex(), Final: Subst->getFinal()); |
| 10285 | } |
| 10286 | |
| 10287 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 10288 | SubstTemplateTemplateParmPackStorage *SubstPack = |
| 10289 | From.getAsSubstTemplateTemplateParmPack(); |
| 10290 | ASTNodeImporter Importer(*this); |
| 10291 | auto ArgPackOrErr = |
| 10292 | Importer.ImportTemplateArgument(From: SubstPack->getArgumentPack()); |
| 10293 | if (!ArgPackOrErr) |
| 10294 | return ArgPackOrErr.takeError(); |
| 10295 | |
| 10296 | auto AssociatedDeclOrErr = Import(FromD: SubstPack->getAssociatedDecl()); |
| 10297 | if (!AssociatedDeclOrErr) |
| 10298 | return AssociatedDeclOrErr.takeError(); |
| 10299 | |
| 10300 | return ToContext.getSubstTemplateTemplateParmPack( |
| 10301 | ArgPack: *ArgPackOrErr, AssociatedDecl: *AssociatedDeclOrErr, Index: SubstPack->getIndex(), |
| 10302 | Final: SubstPack->getFinal()); |
| 10303 | } |
| 10304 | case TemplateName::UsingTemplate: { |
| 10305 | auto UsingOrError = Import(FromD: From.getAsUsingShadowDecl()); |
| 10306 | if (!UsingOrError) |
| 10307 | return UsingOrError.takeError(); |
| 10308 | return TemplateName(cast<UsingShadowDecl>(Val: *UsingOrError)); |
| 10309 | } |
| 10310 | case TemplateName::DeducedTemplate: |
| 10311 | llvm_unreachable("Unexpected DeducedTemplate" ); |
| 10312 | } |
| 10313 | |
| 10314 | llvm_unreachable("Invalid template name kind" ); |
| 10315 | } |
| 10316 | |
| 10317 | Expected<SourceLocation> ASTImporter::Import(SourceLocation FromLoc) { |
| 10318 | if (FromLoc.isInvalid()) |
| 10319 | return SourceLocation{}; |
| 10320 | |
| 10321 | SourceManager &FromSM = FromContext.getSourceManager(); |
| 10322 | bool IsBuiltin = FromSM.isWrittenInBuiltinFile(Loc: FromLoc); |
| 10323 | |
| 10324 | FileIDAndOffset Decomposed = FromSM.getDecomposedLoc(Loc: FromLoc); |
| 10325 | Expected<FileID> ToFileIDOrErr = Import(Decomposed.first, IsBuiltin); |
| 10326 | if (!ToFileIDOrErr) |
| 10327 | return ToFileIDOrErr.takeError(); |
| 10328 | SourceManager &ToSM = ToContext.getSourceManager(); |
| 10329 | return ToSM.getComposedLoc(FID: *ToFileIDOrErr, Offset: Decomposed.second); |
| 10330 | } |
| 10331 | |
| 10332 | Expected<SourceRange> ASTImporter::Import(SourceRange FromRange) { |
| 10333 | SourceLocation ToBegin, ToEnd; |
| 10334 | if (Error Err = importInto(To&: ToBegin, From: FromRange.getBegin())) |
| 10335 | return std::move(Err); |
| 10336 | if (Error Err = importInto(To&: ToEnd, From: FromRange.getEnd())) |
| 10337 | return std::move(Err); |
| 10338 | |
| 10339 | return SourceRange(ToBegin, ToEnd); |
| 10340 | } |
| 10341 | |
| 10342 | Expected<FileID> ASTImporter::Import(FileID FromID, bool IsBuiltin) { |
| 10343 | llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(Val: FromID); |
| 10344 | if (Pos != ImportedFileIDs.end()) |
| 10345 | return Pos->second; |
| 10346 | |
| 10347 | SourceManager &FromSM = FromContext.getSourceManager(); |
| 10348 | SourceManager &ToSM = ToContext.getSourceManager(); |
| 10349 | const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FID: FromID); |
| 10350 | |
| 10351 | // Map the FromID to the "to" source manager. |
| 10352 | FileID ToID; |
| 10353 | if (FromSLoc.isExpansion()) { |
| 10354 | const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion(); |
| 10355 | ExpectedSLoc ToSpLoc = Import(FromLoc: FromEx.getSpellingLoc()); |
| 10356 | if (!ToSpLoc) |
| 10357 | return ToSpLoc.takeError(); |
| 10358 | ExpectedSLoc ToExLocS = Import(FromLoc: FromEx.getExpansionLocStart()); |
| 10359 | if (!ToExLocS) |
| 10360 | return ToExLocS.takeError(); |
| 10361 | unsigned ExLength = FromSM.getFileIDSize(FID: FromID); |
| 10362 | SourceLocation MLoc; |
| 10363 | if (FromEx.isMacroArgExpansion()) { |
| 10364 | MLoc = ToSM.createMacroArgExpansionLoc(SpellingLoc: *ToSpLoc, ExpansionLoc: *ToExLocS, Length: ExLength); |
| 10365 | } else { |
| 10366 | if (ExpectedSLoc ToExLocE = Import(FromLoc: FromEx.getExpansionLocEnd())) |
| 10367 | MLoc = ToSM.createExpansionLoc(SpellingLoc: *ToSpLoc, ExpansionLocStart: *ToExLocS, ExpansionLocEnd: *ToExLocE, Length: ExLength, |
| 10368 | ExpansionIsTokenRange: FromEx.isExpansionTokenRange()); |
| 10369 | else |
| 10370 | return ToExLocE.takeError(); |
| 10371 | } |
| 10372 | ToID = ToSM.getFileID(SpellingLoc: MLoc); |
| 10373 | } else { |
| 10374 | const SrcMgr::ContentCache *Cache = &FromSLoc.getFile().getContentCache(); |
| 10375 | |
| 10376 | if (!IsBuiltin && !Cache->BufferOverridden) { |
| 10377 | // Include location of this file. |
| 10378 | ExpectedSLoc ToIncludeLoc = Import(FromLoc: FromSLoc.getFile().getIncludeLoc()); |
| 10379 | if (!ToIncludeLoc) |
| 10380 | return ToIncludeLoc.takeError(); |
| 10381 | |
| 10382 | // Every FileID that is not the main FileID needs to have a valid include |
| 10383 | // location so that the include chain points to the main FileID. When |
| 10384 | // importing the main FileID (which has no include location), we need to |
| 10385 | // create a fake include location in the main file to keep this property |
| 10386 | // intact. |
| 10387 | SourceLocation ToIncludeLocOrFakeLoc = *ToIncludeLoc; |
| 10388 | if (FromID == FromSM.getMainFileID()) |
| 10389 | ToIncludeLocOrFakeLoc = ToSM.getLocForStartOfFile(FID: ToSM.getMainFileID()); |
| 10390 | |
| 10391 | if (Cache->OrigEntry && Cache->OrigEntry->getDir()) { |
| 10392 | // FIXME: We probably want to use getVirtualFileRef(), so we don't hit |
| 10393 | // the disk again |
| 10394 | // FIXME: We definitely want to re-use the existing MemoryBuffer, rather |
| 10395 | // than mmap the files several times. |
| 10396 | auto Entry = |
| 10397 | ToFileManager.getOptionalFileRef(Filename: Cache->OrigEntry->getName()); |
| 10398 | // FIXME: The filename may be a virtual name that does probably not |
| 10399 | // point to a valid file and we get no Entry here. In this case try with |
| 10400 | // the memory buffer below. |
| 10401 | if (Entry) |
| 10402 | ToID = ToSM.createFileID(SourceFile: *Entry, IncludePos: ToIncludeLocOrFakeLoc, |
| 10403 | FileCharacter: FromSLoc.getFile().getFileCharacteristic()); |
| 10404 | } |
| 10405 | } |
| 10406 | |
| 10407 | if (ToID.isInvalid() || IsBuiltin) { |
| 10408 | // FIXME: We want to re-use the existing MemoryBuffer! |
| 10409 | std::optional<llvm::MemoryBufferRef> FromBuf = |
| 10410 | Cache->getBufferOrNone(Diag&: FromContext.getDiagnostics(), |
| 10411 | FM&: FromSM.getFileManager(), Loc: SourceLocation{}); |
| 10412 | if (!FromBuf) |
| 10413 | return llvm::make_error<ASTImportError>(Args: ASTImportError::Unknown); |
| 10414 | |
| 10415 | std::unique_ptr<llvm::MemoryBuffer> ToBuf = |
| 10416 | llvm::MemoryBuffer::getMemBufferCopy(InputData: FromBuf->getBuffer(), |
| 10417 | BufferName: FromBuf->getBufferIdentifier()); |
| 10418 | ToID = ToSM.createFileID(Buffer: std::move(ToBuf), |
| 10419 | FileCharacter: FromSLoc.getFile().getFileCharacteristic()); |
| 10420 | } |
| 10421 | } |
| 10422 | |
| 10423 | assert(ToID.isValid() && "Unexpected invalid fileID was created." ); |
| 10424 | |
| 10425 | ImportedFileIDs[FromID] = ToID; |
| 10426 | return ToID; |
| 10427 | } |
| 10428 | |
| 10429 | Expected<CXXCtorInitializer *> ASTImporter::Import(CXXCtorInitializer *From) { |
| 10430 | ExpectedExpr ToExprOrErr = Import(FromE: From->getInit()); |
| 10431 | if (!ToExprOrErr) |
| 10432 | return ToExprOrErr.takeError(); |
| 10433 | |
| 10434 | auto LParenLocOrErr = Import(FromLoc: From->getLParenLoc()); |
| 10435 | if (!LParenLocOrErr) |
| 10436 | return LParenLocOrErr.takeError(); |
| 10437 | |
| 10438 | auto RParenLocOrErr = Import(FromLoc: From->getRParenLoc()); |
| 10439 | if (!RParenLocOrErr) |
| 10440 | return RParenLocOrErr.takeError(); |
| 10441 | |
| 10442 | if (From->isBaseInitializer()) { |
| 10443 | auto ToTInfoOrErr = Import(FromTSI: From->getTypeSourceInfo()); |
| 10444 | if (!ToTInfoOrErr) |
| 10445 | return ToTInfoOrErr.takeError(); |
| 10446 | |
| 10447 | SourceLocation EllipsisLoc; |
| 10448 | if (From->isPackExpansion()) |
| 10449 | if (Error Err = importInto(To&: EllipsisLoc, From: From->getEllipsisLoc())) |
| 10450 | return std::move(Err); |
| 10451 | |
| 10452 | return new (ToContext) CXXCtorInitializer( |
| 10453 | ToContext, *ToTInfoOrErr, From->isBaseVirtual(), *LParenLocOrErr, |
| 10454 | *ToExprOrErr, *RParenLocOrErr, EllipsisLoc); |
| 10455 | } else if (From->isMemberInitializer()) { |
| 10456 | ExpectedDecl ToFieldOrErr = Import(FromD: From->getMember()); |
| 10457 | if (!ToFieldOrErr) |
| 10458 | return ToFieldOrErr.takeError(); |
| 10459 | |
| 10460 | auto MemberLocOrErr = Import(FromLoc: From->getMemberLocation()); |
| 10461 | if (!MemberLocOrErr) |
| 10462 | return MemberLocOrErr.takeError(); |
| 10463 | |
| 10464 | return new (ToContext) CXXCtorInitializer( |
| 10465 | ToContext, cast_or_null<FieldDecl>(Val: *ToFieldOrErr), *MemberLocOrErr, |
| 10466 | *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr); |
| 10467 | } else if (From->isIndirectMemberInitializer()) { |
| 10468 | ExpectedDecl ToIFieldOrErr = Import(FromD: From->getIndirectMember()); |
| 10469 | if (!ToIFieldOrErr) |
| 10470 | return ToIFieldOrErr.takeError(); |
| 10471 | |
| 10472 | auto MemberLocOrErr = Import(FromLoc: From->getMemberLocation()); |
| 10473 | if (!MemberLocOrErr) |
| 10474 | return MemberLocOrErr.takeError(); |
| 10475 | |
| 10476 | return new (ToContext) CXXCtorInitializer( |
| 10477 | ToContext, cast_or_null<IndirectFieldDecl>(Val: *ToIFieldOrErr), |
| 10478 | *MemberLocOrErr, *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr); |
| 10479 | } else if (From->isDelegatingInitializer()) { |
| 10480 | auto ToTInfoOrErr = Import(FromTSI: From->getTypeSourceInfo()); |
| 10481 | if (!ToTInfoOrErr) |
| 10482 | return ToTInfoOrErr.takeError(); |
| 10483 | |
| 10484 | return new (ToContext) |
| 10485 | CXXCtorInitializer(ToContext, *ToTInfoOrErr, *LParenLocOrErr, |
| 10486 | *ToExprOrErr, *RParenLocOrErr); |
| 10487 | } else { |
| 10488 | // FIXME: assert? |
| 10489 | return make_error<ASTImportError>(); |
| 10490 | } |
| 10491 | } |
| 10492 | |
| 10493 | Expected<CXXBaseSpecifier *> |
| 10494 | ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) { |
| 10495 | auto Pos = ImportedCXXBaseSpecifiers.find(Val: BaseSpec); |
| 10496 | if (Pos != ImportedCXXBaseSpecifiers.end()) |
| 10497 | return Pos->second; |
| 10498 | |
| 10499 | Expected<SourceRange> ToSourceRange = Import(FromRange: BaseSpec->getSourceRange()); |
| 10500 | if (!ToSourceRange) |
| 10501 | return ToSourceRange.takeError(); |
| 10502 | Expected<TypeSourceInfo *> ToTSI = Import(FromTSI: BaseSpec->getTypeSourceInfo()); |
| 10503 | if (!ToTSI) |
| 10504 | return ToTSI.takeError(); |
| 10505 | ExpectedSLoc ToEllipsisLoc = Import(FromLoc: BaseSpec->getEllipsisLoc()); |
| 10506 | if (!ToEllipsisLoc) |
| 10507 | return ToEllipsisLoc.takeError(); |
| 10508 | CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier( |
| 10509 | *ToSourceRange, BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(), |
| 10510 | BaseSpec->getAccessSpecifierAsWritten(), *ToTSI, *ToEllipsisLoc); |
| 10511 | ImportedCXXBaseSpecifiers[BaseSpec] = Imported; |
| 10512 | return Imported; |
| 10513 | } |
| 10514 | |
| 10515 | llvm::Expected<APValue> ASTImporter::Import(const APValue &FromValue) { |
| 10516 | ASTNodeImporter Importer(*this); |
| 10517 | return Importer.ImportAPValue(FromValue); |
| 10518 | } |
| 10519 | |
| 10520 | Error ASTImporter::ImportDefinition(Decl *From) { |
| 10521 | ExpectedDecl ToOrErr = Import(FromD: From); |
| 10522 | if (!ToOrErr) |
| 10523 | return ToOrErr.takeError(); |
| 10524 | Decl *To = *ToOrErr; |
| 10525 | |
| 10526 | auto *FromDC = cast<DeclContext>(Val: From); |
| 10527 | ASTNodeImporter Importer(*this); |
| 10528 | |
| 10529 | if (auto *ToRecord = dyn_cast<RecordDecl>(Val: To)) { |
| 10530 | if (!ToRecord->getDefinition()) { |
| 10531 | return Importer.ImportDefinition( |
| 10532 | From: cast<RecordDecl>(Val: FromDC), To: ToRecord, |
| 10533 | Kind: ASTNodeImporter::IDK_Everything); |
| 10534 | } |
| 10535 | } |
| 10536 | |
| 10537 | if (auto *ToEnum = dyn_cast<EnumDecl>(Val: To)) { |
| 10538 | if (!ToEnum->getDefinition()) { |
| 10539 | return Importer.ImportDefinition( |
| 10540 | From: cast<EnumDecl>(Val: FromDC), To: ToEnum, Kind: ASTNodeImporter::IDK_Everything); |
| 10541 | } |
| 10542 | } |
| 10543 | |
| 10544 | if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(Val: To)) { |
| 10545 | if (!ToIFace->getDefinition()) { |
| 10546 | return Importer.ImportDefinition( |
| 10547 | From: cast<ObjCInterfaceDecl>(Val: FromDC), To: ToIFace, |
| 10548 | Kind: ASTNodeImporter::IDK_Everything); |
| 10549 | } |
| 10550 | } |
| 10551 | |
| 10552 | if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(Val: To)) { |
| 10553 | if (!ToProto->getDefinition()) { |
| 10554 | return Importer.ImportDefinition( |
| 10555 | From: cast<ObjCProtocolDecl>(Val: FromDC), To: ToProto, |
| 10556 | Kind: ASTNodeImporter::IDK_Everything); |
| 10557 | } |
| 10558 | } |
| 10559 | |
| 10560 | return Importer.ImportDeclContext(FromDC, ForceImport: true); |
| 10561 | } |
| 10562 | |
| 10563 | Expected<DeclarationName> ASTImporter::Import(DeclarationName FromName) { |
| 10564 | if (!FromName) |
| 10565 | return DeclarationName{}; |
| 10566 | |
| 10567 | switch (FromName.getNameKind()) { |
| 10568 | case DeclarationName::Identifier: |
| 10569 | return DeclarationName(Import(FromId: FromName.getAsIdentifierInfo())); |
| 10570 | |
| 10571 | case DeclarationName::ObjCZeroArgSelector: |
| 10572 | case DeclarationName::ObjCOneArgSelector: |
| 10573 | case DeclarationName::ObjCMultiArgSelector: |
| 10574 | if (auto ToSelOrErr = Import(FromSel: FromName.getObjCSelector())) |
| 10575 | return DeclarationName(*ToSelOrErr); |
| 10576 | else |
| 10577 | return ToSelOrErr.takeError(); |
| 10578 | |
| 10579 | case DeclarationName::CXXConstructorName: { |
| 10580 | if (auto ToTyOrErr = Import(FromT: FromName.getCXXNameType())) |
| 10581 | return ToContext.DeclarationNames.getCXXConstructorName( |
| 10582 | Ty: ToContext.getCanonicalType(T: *ToTyOrErr)); |
| 10583 | else |
| 10584 | return ToTyOrErr.takeError(); |
| 10585 | } |
| 10586 | |
| 10587 | case DeclarationName::CXXDestructorName: { |
| 10588 | if (auto ToTyOrErr = Import(FromT: FromName.getCXXNameType())) |
| 10589 | return ToContext.DeclarationNames.getCXXDestructorName( |
| 10590 | Ty: ToContext.getCanonicalType(T: *ToTyOrErr)); |
| 10591 | else |
| 10592 | return ToTyOrErr.takeError(); |
| 10593 | } |
| 10594 | |
| 10595 | case DeclarationName::CXXDeductionGuideName: { |
| 10596 | if (auto ToTemplateOrErr = Import(FromD: FromName.getCXXDeductionGuideTemplate())) |
| 10597 | return ToContext.DeclarationNames.getCXXDeductionGuideName( |
| 10598 | TD: cast<TemplateDecl>(Val: *ToTemplateOrErr)); |
| 10599 | else |
| 10600 | return ToTemplateOrErr.takeError(); |
| 10601 | } |
| 10602 | |
| 10603 | case DeclarationName::CXXConversionFunctionName: { |
| 10604 | if (auto ToTyOrErr = Import(FromT: FromName.getCXXNameType())) |
| 10605 | return ToContext.DeclarationNames.getCXXConversionFunctionName( |
| 10606 | Ty: ToContext.getCanonicalType(T: *ToTyOrErr)); |
| 10607 | else |
| 10608 | return ToTyOrErr.takeError(); |
| 10609 | } |
| 10610 | |
| 10611 | case DeclarationName::CXXOperatorName: |
| 10612 | return ToContext.DeclarationNames.getCXXOperatorName( |
| 10613 | Op: FromName.getCXXOverloadedOperator()); |
| 10614 | |
| 10615 | case DeclarationName::CXXLiteralOperatorName: |
| 10616 | return ToContext.DeclarationNames.getCXXLiteralOperatorName( |
| 10617 | II: Import(FromId: FromName.getCXXLiteralIdentifier())); |
| 10618 | |
| 10619 | case DeclarationName::CXXUsingDirective: |
| 10620 | // FIXME: STATICS! |
| 10621 | return DeclarationName::getUsingDirectiveName(); |
| 10622 | } |
| 10623 | |
| 10624 | llvm_unreachable("Invalid DeclarationName Kind!" ); |
| 10625 | } |
| 10626 | |
| 10627 | IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) { |
| 10628 | if (!FromId) |
| 10629 | return nullptr; |
| 10630 | |
| 10631 | IdentifierInfo *ToId = &ToContext.Idents.get(Name: FromId->getName()); |
| 10632 | |
| 10633 | if (!ToId->getBuiltinID() && FromId->getBuiltinID()) |
| 10634 | ToId->setBuiltinID(FromId->getBuiltinID()); |
| 10635 | |
| 10636 | return ToId; |
| 10637 | } |
| 10638 | |
| 10639 | IdentifierOrOverloadedOperator |
| 10640 | ASTImporter::Import(IdentifierOrOverloadedOperator FromIO) { |
| 10641 | if (const IdentifierInfo *FromII = FromIO.getIdentifier()) |
| 10642 | return Import(FromId: FromII); |
| 10643 | return FromIO.getOperator(); |
| 10644 | } |
| 10645 | |
| 10646 | Expected<Selector> ASTImporter::Import(Selector FromSel) { |
| 10647 | if (FromSel.isNull()) |
| 10648 | return Selector{}; |
| 10649 | |
| 10650 | SmallVector<const IdentifierInfo *, 4> Idents; |
| 10651 | Idents.push_back(Elt: Import(FromId: FromSel.getIdentifierInfoForSlot(argIndex: 0))); |
| 10652 | for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I) |
| 10653 | Idents.push_back(Elt: Import(FromId: FromSel.getIdentifierInfoForSlot(argIndex: I))); |
| 10654 | return ToContext.Selectors.getSelector(NumArgs: FromSel.getNumArgs(), IIV: Idents.data()); |
| 10655 | } |
| 10656 | |
| 10657 | llvm::Expected<APValue> |
| 10658 | ASTNodeImporter::ImportAPValue(const APValue &FromValue) { |
| 10659 | APValue Result; |
| 10660 | llvm::Error Err = llvm::Error::success(); |
| 10661 | auto ImportLoop = [&](const APValue *From, APValue *To, unsigned Size) { |
| 10662 | for (unsigned Idx = 0; Idx < Size; Idx++) { |
| 10663 | APValue Tmp = importChecked(Err, From: From[Idx]); |
| 10664 | To[Idx] = Tmp; |
| 10665 | } |
| 10666 | }; |
| 10667 | switch (FromValue.getKind()) { |
| 10668 | case APValue::None: |
| 10669 | case APValue::Indeterminate: |
| 10670 | case APValue::Int: |
| 10671 | case APValue::Float: |
| 10672 | case APValue::FixedPoint: |
| 10673 | case APValue::ComplexInt: |
| 10674 | case APValue::ComplexFloat: |
| 10675 | Result = FromValue; |
| 10676 | break; |
| 10677 | case APValue::Vector: { |
| 10678 | Result.MakeVector(); |
| 10679 | MutableArrayRef<APValue> Elts = |
| 10680 | Result.setVectorUninit(FromValue.getVectorLength()); |
| 10681 | ImportLoop(((const APValue::Vec *)(const char *)&FromValue.Data)->Elts, |
| 10682 | Elts.data(), FromValue.getVectorLength()); |
| 10683 | break; |
| 10684 | } |
| 10685 | case APValue::Matrix: |
| 10686 | // Matrix values cannot currently arise in APValue import contexts. |
| 10687 | llvm_unreachable("Matrix APValue import not yet supported" ); |
| 10688 | case APValue::Array: |
| 10689 | Result.MakeArray(InitElts: FromValue.getArrayInitializedElts(), |
| 10690 | Size: FromValue.getArraySize()); |
| 10691 | ImportLoop(((const APValue::Arr *)(const char *)&FromValue.Data)->Elts, |
| 10692 | ((const APValue::Arr *)(const char *)&Result.Data)->Elts, |
| 10693 | FromValue.getArrayInitializedElts()); |
| 10694 | break; |
| 10695 | case APValue::Struct: |
| 10696 | Result.MakeStruct(B: FromValue.getStructNumBases(), |
| 10697 | M: FromValue.getStructNumFields()); |
| 10698 | ImportLoop( |
| 10699 | ((const APValue::StructData *)(const char *)&FromValue.Data)->Elts, |
| 10700 | ((const APValue::StructData *)(const char *)&Result.Data)->Elts, |
| 10701 | FromValue.getStructNumBases() + FromValue.getStructNumFields()); |
| 10702 | break; |
| 10703 | case APValue::Union: { |
| 10704 | Result.MakeUnion(); |
| 10705 | const Decl *ImpFDecl = importChecked(Err, From: FromValue.getUnionField()); |
| 10706 | APValue ImpValue = importChecked(Err, From: FromValue.getUnionValue()); |
| 10707 | if (Err) |
| 10708 | return std::move(Err); |
| 10709 | Result.setUnion(Field: cast<FieldDecl>(Val: ImpFDecl), Value: ImpValue); |
| 10710 | break; |
| 10711 | } |
| 10712 | case APValue::AddrLabelDiff: { |
| 10713 | Result.MakeAddrLabelDiff(); |
| 10714 | const Expr *ImpLHS = importChecked(Err, From: FromValue.getAddrLabelDiffLHS()); |
| 10715 | const Expr *ImpRHS = importChecked(Err, From: FromValue.getAddrLabelDiffRHS()); |
| 10716 | if (Err) |
| 10717 | return std::move(Err); |
| 10718 | Result.setAddrLabelDiff(LHSExpr: cast<AddrLabelExpr>(Val: ImpLHS), |
| 10719 | RHSExpr: cast<AddrLabelExpr>(Val: ImpRHS)); |
| 10720 | break; |
| 10721 | } |
| 10722 | case APValue::MemberPointer: { |
| 10723 | const Decl *ImpMemPtrDecl = |
| 10724 | importChecked(Err, From: FromValue.getMemberPointerDecl()); |
| 10725 | if (Err) |
| 10726 | return std::move(Err); |
| 10727 | MutableArrayRef<const CXXRecordDecl *> ToPath = |
| 10728 | Result.setMemberPointerUninit( |
| 10729 | Member: cast<const ValueDecl>(Val: ImpMemPtrDecl), |
| 10730 | IsDerivedMember: FromValue.isMemberPointerToDerivedMember(), |
| 10731 | Size: FromValue.getMemberPointerPath().size()); |
| 10732 | ArrayRef<const CXXRecordDecl *> FromPath = Result.getMemberPointerPath(); |
| 10733 | for (unsigned Idx = 0; Idx < FromValue.getMemberPointerPath().size(); |
| 10734 | Idx++) { |
| 10735 | const Decl *ImpDecl = importChecked(Err, From: FromPath[Idx]); |
| 10736 | if (Err) |
| 10737 | return std::move(Err); |
| 10738 | ToPath[Idx] = cast<const CXXRecordDecl>(Val: ImpDecl->getCanonicalDecl()); |
| 10739 | } |
| 10740 | break; |
| 10741 | } |
| 10742 | case APValue::LValue: |
| 10743 | APValue::LValueBase Base; |
| 10744 | QualType FromElemTy; |
| 10745 | if (FromValue.getLValueBase()) { |
| 10746 | assert(!FromValue.getLValueBase().is<DynamicAllocLValue>() && |
| 10747 | "in C++20 dynamic allocation are transient so they shouldn't " |
| 10748 | "appear in the AST" ); |
| 10749 | if (!FromValue.getLValueBase().is<TypeInfoLValue>()) { |
| 10750 | if (const auto *E = |
| 10751 | FromValue.getLValueBase().dyn_cast<const Expr *>()) { |
| 10752 | FromElemTy = E->getType(); |
| 10753 | const Expr *ImpExpr = importChecked(Err, From: E); |
| 10754 | if (Err) |
| 10755 | return std::move(Err); |
| 10756 | Base = APValue::LValueBase(ImpExpr, |
| 10757 | FromValue.getLValueBase().getCallIndex(), |
| 10758 | FromValue.getLValueBase().getVersion()); |
| 10759 | } else { |
| 10760 | FromElemTy = |
| 10761 | FromValue.getLValueBase().get<const ValueDecl *>()->getType(); |
| 10762 | const Decl *ImpDecl = importChecked( |
| 10763 | Err, From: FromValue.getLValueBase().get<const ValueDecl *>()); |
| 10764 | if (Err) |
| 10765 | return std::move(Err); |
| 10766 | Base = APValue::LValueBase(cast<ValueDecl>(Val: ImpDecl), |
| 10767 | FromValue.getLValueBase().getCallIndex(), |
| 10768 | FromValue.getLValueBase().getVersion()); |
| 10769 | } |
| 10770 | } else { |
| 10771 | FromElemTy = FromValue.getLValueBase().getTypeInfoType(); |
| 10772 | const Type *ImpTypeInfo = importChecked( |
| 10773 | Err, From: FromValue.getLValueBase().get<TypeInfoLValue>().getType()); |
| 10774 | QualType ImpType = |
| 10775 | importChecked(Err, From: FromValue.getLValueBase().getTypeInfoType()); |
| 10776 | if (Err) |
| 10777 | return std::move(Err); |
| 10778 | Base = APValue::LValueBase::getTypeInfo(LV: TypeInfoLValue(ImpTypeInfo), |
| 10779 | TypeInfo: ImpType); |
| 10780 | } |
| 10781 | } |
| 10782 | CharUnits Offset = FromValue.getLValueOffset(); |
| 10783 | unsigned PathLength = FromValue.getLValuePath().size(); |
| 10784 | Result.MakeLValue(); |
| 10785 | if (FromValue.hasLValuePath()) { |
| 10786 | MutableArrayRef<APValue::LValuePathEntry> ToPath = Result.setLValueUninit( |
| 10787 | B: Base, O: Offset, Size: PathLength, OnePastTheEnd: FromValue.isLValueOnePastTheEnd(), |
| 10788 | IsNullPtr: FromValue.isNullPointer()); |
| 10789 | ArrayRef<APValue::LValuePathEntry> FromPath = FromValue.getLValuePath(); |
| 10790 | for (unsigned LoopIdx = 0; LoopIdx < PathLength; LoopIdx++) { |
| 10791 | if (FromElemTy->isRecordType()) { |
| 10792 | const Decl *FromDecl = |
| 10793 | FromPath[LoopIdx].getAsBaseOrMember().getPointer(); |
| 10794 | const Decl *ImpDecl = importChecked(Err, From: FromDecl); |
| 10795 | if (Err) |
| 10796 | return std::move(Err); |
| 10797 | if (auto *RD = dyn_cast<CXXRecordDecl>(Val: FromDecl)) |
| 10798 | FromElemTy = Importer.FromContext.getCanonicalTagType(TD: RD); |
| 10799 | else |
| 10800 | FromElemTy = cast<ValueDecl>(Val: FromDecl)->getType(); |
| 10801 | ToPath[LoopIdx] = APValue::LValuePathEntry(APValue::BaseOrMemberType( |
| 10802 | ImpDecl, FromPath[LoopIdx].getAsBaseOrMember().getInt())); |
| 10803 | } else { |
| 10804 | FromElemTy = |
| 10805 | Importer.FromContext.getAsArrayType(T: FromElemTy)->getElementType(); |
| 10806 | ToPath[LoopIdx] = APValue::LValuePathEntry::ArrayIndex( |
| 10807 | Index: FromPath[LoopIdx].getAsArrayIndex()); |
| 10808 | } |
| 10809 | } |
| 10810 | } else |
| 10811 | Result.setLValue(B: Base, O: Offset, APValue::NoLValuePath{}, |
| 10812 | IsNullPtr: FromValue.isNullPointer()); |
| 10813 | } |
| 10814 | if (Err) |
| 10815 | return std::move(Err); |
| 10816 | return Result; |
| 10817 | } |
| 10818 | |
| 10819 | Expected<DeclarationName> ASTImporter::HandleNameConflict(DeclarationName Name, |
| 10820 | DeclContext *DC, |
| 10821 | unsigned IDNS, |
| 10822 | NamedDecl **Decls, |
| 10823 | unsigned NumDecls) { |
| 10824 | if (ODRHandling == ODRHandlingType::Conservative) |
| 10825 | // Report error at any name conflict. |
| 10826 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 10827 | else |
| 10828 | // Allow to create the new Decl with the same name. |
| 10829 | return Name; |
| 10830 | } |
| 10831 | |
| 10832 | DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) { |
| 10833 | if (LastDiagFromFrom) |
| 10834 | ToContext.getDiagnostics().notePriorDiagnosticFrom( |
| 10835 | Other: FromContext.getDiagnostics()); |
| 10836 | LastDiagFromFrom = false; |
| 10837 | return ToContext.getDiagnostics().Report(Loc, DiagID); |
| 10838 | } |
| 10839 | |
| 10840 | DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) { |
| 10841 | if (!LastDiagFromFrom) |
| 10842 | FromContext.getDiagnostics().notePriorDiagnosticFrom( |
| 10843 | Other: ToContext.getDiagnostics()); |
| 10844 | LastDiagFromFrom = true; |
| 10845 | return FromContext.getDiagnostics().Report(Loc, DiagID); |
| 10846 | } |
| 10847 | |
| 10848 | void ASTImporter::CompleteDecl (Decl *D) { |
| 10849 | if (auto *ID = dyn_cast<ObjCInterfaceDecl>(Val: D)) { |
| 10850 | if (!ID->getDefinition()) |
| 10851 | ID->startDefinition(); |
| 10852 | } |
| 10853 | else if (auto *PD = dyn_cast<ObjCProtocolDecl>(Val: D)) { |
| 10854 | if (!PD->getDefinition()) |
| 10855 | PD->startDefinition(); |
| 10856 | } |
| 10857 | else if (auto *TD = dyn_cast<TagDecl>(Val: D)) { |
| 10858 | if (!TD->getDefinition() && !TD->isBeingDefined()) { |
| 10859 | TD->startDefinition(); |
| 10860 | TD->setCompleteDefinition(true); |
| 10861 | } |
| 10862 | } |
| 10863 | else { |
| 10864 | assert(0 && "CompleteDecl called on a Decl that can't be completed" ); |
| 10865 | } |
| 10866 | } |
| 10867 | |
| 10868 | Decl *ASTImporter::MapImported(Decl *From, Decl *To) { |
| 10869 | auto [Pos, Inserted] = ImportedDecls.try_emplace(Key: From, Args&: To); |
| 10870 | assert((Inserted || Pos->second == To) && |
| 10871 | "Try to import an already imported Decl" ); |
| 10872 | if (!Inserted) |
| 10873 | return Pos->second; |
| 10874 | // This mapping should be maintained only in this function. Therefore do not |
| 10875 | // check for additional consistency. |
| 10876 | ImportedFromDecls[To] = From; |
| 10877 | // In the case of TypedefNameDecl we create the Decl first and only then we |
| 10878 | // import and set its DeclContext. So, the DC is still not set when we reach |
| 10879 | // here from GetImportedOrCreateDecl. |
| 10880 | if (To->getDeclContext()) |
| 10881 | AddToLookupTable(ToD: To); |
| 10882 | return To; |
| 10883 | } |
| 10884 | |
| 10885 | std::optional<ASTImportError> |
| 10886 | ASTImporter::getImportDeclErrorIfAny(Decl *FromD) const { |
| 10887 | auto Pos = ImportDeclErrors.find(Val: FromD); |
| 10888 | if (Pos != ImportDeclErrors.end()) |
| 10889 | return Pos->second; |
| 10890 | else |
| 10891 | return std::nullopt; |
| 10892 | } |
| 10893 | |
| 10894 | void ASTImporter::setImportDeclError(Decl *From, ASTImportError Error) { |
| 10895 | auto InsertRes = ImportDeclErrors.insert(KV: {From, Error}); |
| 10896 | (void)InsertRes; |
| 10897 | // Either we set the error for the first time, or we already had set one and |
| 10898 | // now we want to set the same error. |
| 10899 | assert(InsertRes.second || InsertRes.first->second.Error == Error.Error); |
| 10900 | } |
| 10901 | |
| 10902 | bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To, |
| 10903 | bool Complain) { |
| 10904 | llvm::DenseMap<const Type *, const Type *>::iterator Pos = |
| 10905 | ImportedTypes.find(Val: From.getTypePtr()); |
| 10906 | if (Pos != ImportedTypes.end()) { |
| 10907 | if (ExpectedType ToFromOrErr = Import(FromT: From)) { |
| 10908 | if (ToContext.hasSameType(T1: *ToFromOrErr, T2: To)) |
| 10909 | return true; |
| 10910 | } else { |
| 10911 | llvm::consumeError(Err: ToFromOrErr.takeError()); |
| 10912 | } |
| 10913 | } |
| 10914 | |
| 10915 | StructuralEquivalenceContext Ctx( |
| 10916 | getToContext().getLangOpts(), FromContext, ToContext, NonEquivalentDecls, |
| 10917 | getStructuralEquivalenceKind(Importer: *this), false, Complain); |
| 10918 | return Ctx.IsEquivalent(T1: From, T2: To); |
| 10919 | } |
| 10920 | |