| 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>(FD); |
| 123 | if (auto *VD = dyn_cast<VarDecl>(Val: D)) |
| 124 | return getCanonicalForwardRedeclChain<VarDecl>(VD); |
| 125 | if (auto *TD = dyn_cast<TagDecl>(Val: D)) |
| 126 | return getCanonicalForwardRedeclChain<TagDecl>(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(D, 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>(FromD)) { |
| 337 | auto *ToNamed = cast<NamedDecl>(ToD); |
| 338 | DeclContextLookupResult FromLookup = |
| 339 | FromDC->lookup(Name: FromNamed->getDeclName()); |
| 340 | if (llvm::is_contained(FromLookup, 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, Importer.getToContext().getTranslationUnitDecl()); |
| 359 | } |
| 360 | |
| 361 | template <typename TemplateParmDeclT> |
| 362 | Error importTemplateParameterDefaultArgument(const TemplateParmDeclT *D, |
| 363 | TemplateParmDeclT *ToD) { |
| 364 | if (D->hasDefaultArgument()) { |
| 365 | if (D->defaultArgumentWasInherited()) { |
| 366 | Expected<TemplateParmDeclT *> ToInheritedFromOrErr = |
| 367 | import(D->getDefaultArgStorage().getInheritedFrom()); |
| 368 | if (!ToInheritedFromOrErr) |
| 369 | return ToInheritedFromOrErr.takeError(); |
| 370 | TemplateParmDeclT *ToInheritedFrom = *ToInheritedFromOrErr; |
| 371 | if (!ToInheritedFrom->hasDefaultArgument()) { |
| 372 | // Resolve possible circular dependency between default value of the |
| 373 | // template argument and the template declaration. |
| 374 | Expected<TemplateArgumentLoc> ToInheritedDefaultArgOrErr = |
| 375 | import(D->getDefaultArgStorage() |
| 376 | .getInheritedFrom() |
| 377 | ->getDefaultArgument()); |
| 378 | if (!ToInheritedDefaultArgOrErr) |
| 379 | return ToInheritedDefaultArgOrErr.takeError(); |
| 380 | ToInheritedFrom->setDefaultArgument(Importer.getToContext(), |
| 381 | *ToInheritedDefaultArgOrErr); |
| 382 | } |
| 383 | ToD->setInheritedDefaultArgument(ToD->getASTContext(), |
| 384 | ToInheritedFrom); |
| 385 | } else { |
| 386 | Expected<TemplateArgumentLoc> ToDefaultArgOrErr = |
| 387 | import(D->getDefaultArgument()); |
| 388 | if (!ToDefaultArgOrErr) |
| 389 | return ToDefaultArgOrErr.takeError(); |
| 390 | // Default argument could have been set in the |
| 391 | // '!ToInheritedFrom->hasDefaultArgument()' branch above. |
| 392 | if (!ToD->hasDefaultArgument()) |
| 393 | ToD->setDefaultArgument(Importer.getToContext(), |
| 394 | *ToDefaultArgOrErr); |
| 395 | } |
| 396 | } |
| 397 | return Error::success(); |
| 398 | } |
| 399 | |
| 400 | public: |
| 401 | explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) {} |
| 402 | |
| 403 | using TypeVisitor<ASTNodeImporter, ExpectedType>::Visit; |
| 404 | using DeclVisitor<ASTNodeImporter, ExpectedDecl>::Visit; |
| 405 | using StmtVisitor<ASTNodeImporter, ExpectedStmt>::Visit; |
| 406 | |
| 407 | // Importing types |
| 408 | ExpectedType VisitType(const Type *T); |
| 409 | #define TYPE(Class, Base) \ |
| 410 | ExpectedType Visit##Class##Type(const Class##Type *T); |
| 411 | #include "clang/AST/TypeNodes.inc" |
| 412 | |
| 413 | // Importing declarations |
| 414 | Error ImportDeclParts(NamedDecl *D, DeclarationName &Name, NamedDecl *&ToD, |
| 415 | SourceLocation &Loc); |
| 416 | Error ImportDeclParts( |
| 417 | NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC, |
| 418 | DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc); |
| 419 | Error ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr); |
| 420 | Error ImportDeclarationNameLoc( |
| 421 | const DeclarationNameInfo &From, DeclarationNameInfo &To); |
| 422 | Error ImportDeclContext(DeclContext *FromDC, bool ForceImport = false); |
| 423 | Error ImportDeclContext( |
| 424 | Decl *From, DeclContext *&ToDC, DeclContext *&ToLexicalDC); |
| 425 | Error ImportImplicitMethods(const CXXRecordDecl *From, CXXRecordDecl *To); |
| 426 | |
| 427 | Error ImportFieldDeclDefinition(const FieldDecl *From, const FieldDecl *To); |
| 428 | Expected<CXXCastPath> ImportCastPath(CastExpr *E); |
| 429 | Expected<APValue> ImportAPValue(const APValue &FromValue); |
| 430 | |
| 431 | using Designator = DesignatedInitExpr::Designator; |
| 432 | |
| 433 | /// What we should import from the definition. |
| 434 | enum ImportDefinitionKind { |
| 435 | /// Import the default subset of the definition, which might be |
| 436 | /// nothing (if minimal import is set) or might be everything (if minimal |
| 437 | /// import is not set). |
| 438 | IDK_Default, |
| 439 | /// Import everything. |
| 440 | IDK_Everything, |
| 441 | /// Import only the bare bones needed to establish a valid |
| 442 | /// DeclContext. |
| 443 | IDK_Basic |
| 444 | }; |
| 445 | |
| 446 | bool shouldForceImportDeclContext(ImportDefinitionKind IDK) { |
| 447 | return IDK == IDK_Everything || |
| 448 | (IDK == IDK_Default && !Importer.isMinimalImport()); |
| 449 | } |
| 450 | |
| 451 | Error ImportInitializer(VarDecl *From, VarDecl *To); |
| 452 | Error ImportDefinition( |
| 453 | RecordDecl *From, RecordDecl *To, |
| 454 | ImportDefinitionKind Kind = IDK_Default); |
| 455 | Error ImportDefinition( |
| 456 | EnumDecl *From, EnumDecl *To, |
| 457 | ImportDefinitionKind Kind = IDK_Default); |
| 458 | Error ImportDefinition( |
| 459 | ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, |
| 460 | ImportDefinitionKind Kind = IDK_Default); |
| 461 | Error ImportDefinition( |
| 462 | ObjCProtocolDecl *From, ObjCProtocolDecl *To, |
| 463 | ImportDefinitionKind Kind = IDK_Default); |
| 464 | Error ImportTemplateArguments(ArrayRef<TemplateArgument> FromArgs, |
| 465 | SmallVectorImpl<TemplateArgument> &ToArgs); |
| 466 | Expected<TemplateArgument> |
| 467 | ImportTemplateArgument(const TemplateArgument &From); |
| 468 | |
| 469 | template <typename InContainerTy> |
| 470 | Error ImportTemplateArgumentListInfo( |
| 471 | const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo); |
| 472 | |
| 473 | template<typename InContainerTy> |
| 474 | Error ImportTemplateArgumentListInfo( |
| 475 | SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc, |
| 476 | const InContainerTy &Container, TemplateArgumentListInfo &Result); |
| 477 | |
| 478 | using TemplateArgsTy = SmallVector<TemplateArgument, 8>; |
| 479 | using FunctionTemplateAndArgsTy = |
| 480 | std::tuple<FunctionTemplateDecl *, TemplateArgsTy>; |
| 481 | Expected<FunctionTemplateAndArgsTy> |
| 482 | ImportFunctionTemplateWithTemplateArgsFromSpecialization( |
| 483 | FunctionDecl *FromFD); |
| 484 | |
| 485 | template <typename DeclTy> |
| 486 | Error ImportTemplateParameterLists(const DeclTy *FromD, DeclTy *ToD); |
| 487 | |
| 488 | Error ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD); |
| 489 | |
| 490 | Error ImportFunctionDeclBody(FunctionDecl *FromFD, FunctionDecl *ToFD); |
| 491 | |
| 492 | Error ImportDefaultArgOfParmVarDecl(const ParmVarDecl *FromParam, |
| 493 | ParmVarDecl *ToParam); |
| 494 | |
| 495 | Expected<InheritedConstructor> |
| 496 | ImportInheritedConstructor(const InheritedConstructor &From); |
| 497 | |
| 498 | template <typename T> |
| 499 | bool hasSameVisibilityContextAndLinkage(T *Found, T *From); |
| 500 | |
| 501 | bool IsStructuralMatch(Decl *From, Decl *To, bool Complain = true, |
| 502 | bool IgnoreTemplateParmDepth = false); |
| 503 | ExpectedDecl VisitDecl(Decl *D); |
| 504 | ExpectedDecl VisitImportDecl(ImportDecl *D); |
| 505 | ExpectedDecl VisitEmptyDecl(EmptyDecl *D); |
| 506 | ExpectedDecl VisitAccessSpecDecl(AccessSpecDecl *D); |
| 507 | ExpectedDecl VisitStaticAssertDecl(StaticAssertDecl *D); |
| 508 | ExpectedDecl VisitTranslationUnitDecl(TranslationUnitDecl *D); |
| 509 | ExpectedDecl VisitBindingDecl(BindingDecl *D); |
| 510 | ExpectedDecl VisitNamespaceDecl(NamespaceDecl *D); |
| 511 | ExpectedDecl VisitNamespaceAliasDecl(NamespaceAliasDecl *D); |
| 512 | ExpectedDecl VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias); |
| 513 | ExpectedDecl VisitTypedefDecl(TypedefDecl *D); |
| 514 | ExpectedDecl VisitTypeAliasDecl(TypeAliasDecl *D); |
| 515 | ExpectedDecl VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D); |
| 516 | ExpectedDecl VisitLabelDecl(LabelDecl *D); |
| 517 | ExpectedDecl VisitEnumDecl(EnumDecl *D); |
| 518 | ExpectedDecl VisitRecordDecl(RecordDecl *D); |
| 519 | ExpectedDecl VisitEnumConstantDecl(EnumConstantDecl *D); |
| 520 | ExpectedDecl VisitFunctionDecl(FunctionDecl *D); |
| 521 | ExpectedDecl VisitCXXMethodDecl(CXXMethodDecl *D); |
| 522 | ExpectedDecl VisitCXXConstructorDecl(CXXConstructorDecl *D); |
| 523 | ExpectedDecl VisitCXXDestructorDecl(CXXDestructorDecl *D); |
| 524 | ExpectedDecl VisitCXXConversionDecl(CXXConversionDecl *D); |
| 525 | ExpectedDecl VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D); |
| 526 | ExpectedDecl VisitFieldDecl(FieldDecl *D); |
| 527 | ExpectedDecl VisitIndirectFieldDecl(IndirectFieldDecl *D); |
| 528 | ExpectedDecl VisitFriendDecl(FriendDecl *D); |
| 529 | ExpectedDecl VisitObjCIvarDecl(ObjCIvarDecl *D); |
| 530 | ExpectedDecl VisitVarDecl(VarDecl *D); |
| 531 | ExpectedDecl VisitImplicitParamDecl(ImplicitParamDecl *D); |
| 532 | ExpectedDecl VisitParmVarDecl(ParmVarDecl *D); |
| 533 | ExpectedDecl VisitObjCMethodDecl(ObjCMethodDecl *D); |
| 534 | ExpectedDecl VisitObjCTypeParamDecl(ObjCTypeParamDecl *D); |
| 535 | ExpectedDecl VisitObjCCategoryDecl(ObjCCategoryDecl *D); |
| 536 | ExpectedDecl VisitObjCProtocolDecl(ObjCProtocolDecl *D); |
| 537 | ExpectedDecl VisitLinkageSpecDecl(LinkageSpecDecl *D); |
| 538 | ExpectedDecl VisitUsingDecl(UsingDecl *D); |
| 539 | ExpectedDecl VisitUsingShadowDecl(UsingShadowDecl *D); |
| 540 | ExpectedDecl VisitUsingDirectiveDecl(UsingDirectiveDecl *D); |
| 541 | ExpectedDecl VisitUsingPackDecl(UsingPackDecl *D); |
| 542 | ExpectedDecl ImportUsingShadowDecls(BaseUsingDecl *D, BaseUsingDecl *ToSI); |
| 543 | ExpectedDecl VisitUsingEnumDecl(UsingEnumDecl *D); |
| 544 | ExpectedDecl VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); |
| 545 | ExpectedDecl VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); |
| 546 | ExpectedDecl VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D); |
| 547 | ExpectedDecl |
| 548 | VisitLifetimeExtendedTemporaryDecl(LifetimeExtendedTemporaryDecl *D); |
| 549 | |
| 550 | Expected<ObjCTypeParamList *> |
| 551 | ImportObjCTypeParamList(ObjCTypeParamList *list); |
| 552 | |
| 553 | ExpectedDecl VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
| 554 | ExpectedDecl VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
| 555 | ExpectedDecl VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
| 556 | ExpectedDecl VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
| 557 | ExpectedDecl VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); |
| 558 | ExpectedDecl VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); |
| 559 | ExpectedDecl VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); |
| 560 | ExpectedDecl VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); |
| 561 | ExpectedDecl VisitClassTemplateDecl(ClassTemplateDecl *D); |
| 562 | ExpectedDecl VisitClassTemplateSpecializationDecl( |
| 563 | ClassTemplateSpecializationDecl *D); |
| 564 | ExpectedDecl VisitVarTemplateDecl(VarTemplateDecl *D); |
| 565 | ExpectedDecl VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D); |
| 566 | ExpectedDecl VisitFunctionTemplateDecl(FunctionTemplateDecl *D); |
| 567 | |
| 568 | // Importing statements |
| 569 | ExpectedStmt VisitStmt(Stmt *S); |
| 570 | ExpectedStmt VisitGCCAsmStmt(GCCAsmStmt *S); |
| 571 | ExpectedStmt VisitDeclStmt(DeclStmt *S); |
| 572 | ExpectedStmt VisitNullStmt(NullStmt *S); |
| 573 | ExpectedStmt VisitCompoundStmt(CompoundStmt *S); |
| 574 | ExpectedStmt VisitCaseStmt(CaseStmt *S); |
| 575 | ExpectedStmt VisitDefaultStmt(DefaultStmt *S); |
| 576 | ExpectedStmt VisitLabelStmt(LabelStmt *S); |
| 577 | ExpectedStmt VisitAttributedStmt(AttributedStmt *S); |
| 578 | ExpectedStmt VisitIfStmt(IfStmt *S); |
| 579 | ExpectedStmt VisitSwitchStmt(SwitchStmt *S); |
| 580 | ExpectedStmt VisitWhileStmt(WhileStmt *S); |
| 581 | ExpectedStmt VisitDoStmt(DoStmt *S); |
| 582 | ExpectedStmt VisitForStmt(ForStmt *S); |
| 583 | ExpectedStmt VisitGotoStmt(GotoStmt *S); |
| 584 | ExpectedStmt VisitIndirectGotoStmt(IndirectGotoStmt *S); |
| 585 | ExpectedStmt VisitContinueStmt(ContinueStmt *S); |
| 586 | ExpectedStmt VisitBreakStmt(BreakStmt *S); |
| 587 | ExpectedStmt VisitReturnStmt(ReturnStmt *S); |
| 588 | // FIXME: MSAsmStmt |
| 589 | // FIXME: SEHExceptStmt |
| 590 | // FIXME: SEHFinallyStmt |
| 591 | // FIXME: SEHTryStmt |
| 592 | // FIXME: SEHLeaveStmt |
| 593 | // FIXME: CapturedStmt |
| 594 | ExpectedStmt VisitCXXCatchStmt(CXXCatchStmt *S); |
| 595 | ExpectedStmt VisitCXXTryStmt(CXXTryStmt *S); |
| 596 | ExpectedStmt VisitCXXForRangeStmt(CXXForRangeStmt *S); |
| 597 | // FIXME: MSDependentExistsStmt |
| 598 | ExpectedStmt VisitObjCForCollectionStmt(ObjCForCollectionStmt *S); |
| 599 | ExpectedStmt VisitObjCAtCatchStmt(ObjCAtCatchStmt *S); |
| 600 | ExpectedStmt VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S); |
| 601 | ExpectedStmt VisitObjCAtTryStmt(ObjCAtTryStmt *S); |
| 602 | ExpectedStmt VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
| 603 | ExpectedStmt VisitObjCAtThrowStmt(ObjCAtThrowStmt *S); |
| 604 | ExpectedStmt VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S); |
| 605 | |
| 606 | // Importing expressions |
| 607 | ExpectedStmt VisitExpr(Expr *E); |
| 608 | ExpectedStmt VisitSourceLocExpr(SourceLocExpr *E); |
| 609 | ExpectedStmt VisitVAArgExpr(VAArgExpr *E); |
| 610 | ExpectedStmt VisitChooseExpr(ChooseExpr *E); |
| 611 | ExpectedStmt VisitConvertVectorExpr(ConvertVectorExpr *E); |
| 612 | ExpectedStmt VisitShuffleVectorExpr(ShuffleVectorExpr *E); |
| 613 | ExpectedStmt VisitGNUNullExpr(GNUNullExpr *E); |
| 614 | ExpectedStmt VisitGenericSelectionExpr(GenericSelectionExpr *E); |
| 615 | ExpectedStmt VisitPredefinedExpr(PredefinedExpr *E); |
| 616 | ExpectedStmt VisitDeclRefExpr(DeclRefExpr *E); |
| 617 | ExpectedStmt VisitImplicitValueInitExpr(ImplicitValueInitExpr *E); |
| 618 | ExpectedStmt VisitDesignatedInitExpr(DesignatedInitExpr *E); |
| 619 | ExpectedStmt VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E); |
| 620 | ExpectedStmt VisitIntegerLiteral(IntegerLiteral *E); |
| 621 | ExpectedStmt VisitFloatingLiteral(FloatingLiteral *E); |
| 622 | ExpectedStmt VisitImaginaryLiteral(ImaginaryLiteral *E); |
| 623 | ExpectedStmt VisitFixedPointLiteral(FixedPointLiteral *E); |
| 624 | ExpectedStmt VisitCharacterLiteral(CharacterLiteral *E); |
| 625 | ExpectedStmt VisitStringLiteral(StringLiteral *E); |
| 626 | ExpectedStmt VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
| 627 | ExpectedStmt VisitAtomicExpr(AtomicExpr *E); |
| 628 | ExpectedStmt VisitAddrLabelExpr(AddrLabelExpr *E); |
| 629 | ExpectedStmt VisitConstantExpr(ConstantExpr *E); |
| 630 | ExpectedStmt VisitParenExpr(ParenExpr *E); |
| 631 | ExpectedStmt VisitParenListExpr(ParenListExpr *E); |
| 632 | ExpectedStmt VisitStmtExpr(StmtExpr *E); |
| 633 | ExpectedStmt VisitUnaryOperator(UnaryOperator *E); |
| 634 | ExpectedStmt VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); |
| 635 | ExpectedStmt VisitBinaryOperator(BinaryOperator *E); |
| 636 | ExpectedStmt VisitConditionalOperator(ConditionalOperator *E); |
| 637 | ExpectedStmt VisitBinaryConditionalOperator(BinaryConditionalOperator *E); |
| 638 | ExpectedStmt VisitCXXRewrittenBinaryOperator(CXXRewrittenBinaryOperator *E); |
| 639 | ExpectedStmt VisitOpaqueValueExpr(OpaqueValueExpr *E); |
| 640 | ExpectedStmt VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E); |
| 641 | ExpectedStmt VisitExpressionTraitExpr(ExpressionTraitExpr *E); |
| 642 | ExpectedStmt VisitArraySubscriptExpr(ArraySubscriptExpr *E); |
| 643 | ExpectedStmt VisitCompoundAssignOperator(CompoundAssignOperator *E); |
| 644 | ExpectedStmt VisitImplicitCastExpr(ImplicitCastExpr *E); |
| 645 | ExpectedStmt VisitExplicitCastExpr(ExplicitCastExpr *E); |
| 646 | ExpectedStmt VisitOffsetOfExpr(OffsetOfExpr *OE); |
| 647 | ExpectedStmt VisitCXXThrowExpr(CXXThrowExpr *E); |
| 648 | ExpectedStmt VisitCXXNoexceptExpr(CXXNoexceptExpr *E); |
| 649 | ExpectedStmt VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E); |
| 650 | ExpectedStmt VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); |
| 651 | ExpectedStmt VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E); |
| 652 | ExpectedStmt VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E); |
| 653 | ExpectedStmt VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E); |
| 654 | ExpectedStmt VisitPackExpansionExpr(PackExpansionExpr *E); |
| 655 | ExpectedStmt VisitSizeOfPackExpr(SizeOfPackExpr *E); |
| 656 | ExpectedStmt VisitCXXNewExpr(CXXNewExpr *E); |
| 657 | ExpectedStmt VisitCXXDeleteExpr(CXXDeleteExpr *E); |
| 658 | ExpectedStmt VisitCXXConstructExpr(CXXConstructExpr *E); |
| 659 | ExpectedStmt VisitCXXMemberCallExpr(CXXMemberCallExpr *E); |
| 660 | ExpectedStmt VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E); |
| 661 | ExpectedStmt VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E); |
| 662 | ExpectedStmt VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E); |
| 663 | ExpectedStmt VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E); |
| 664 | ExpectedStmt VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E); |
| 665 | ExpectedStmt VisitExprWithCleanups(ExprWithCleanups *E); |
| 666 | ExpectedStmt VisitCXXThisExpr(CXXThisExpr *E); |
| 667 | ExpectedStmt VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E); |
| 668 | ExpectedStmt VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E); |
| 669 | ExpectedStmt VisitMemberExpr(MemberExpr *E); |
| 670 | ExpectedStmt VisitCallExpr(CallExpr *E); |
| 671 | ExpectedStmt VisitLambdaExpr(LambdaExpr *LE); |
| 672 | ExpectedStmt VisitInitListExpr(InitListExpr *E); |
| 673 | ExpectedStmt VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E); |
| 674 | ExpectedStmt VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E); |
| 675 | ExpectedStmt VisitArrayInitLoopExpr(ArrayInitLoopExpr *E); |
| 676 | ExpectedStmt VisitArrayInitIndexExpr(ArrayInitIndexExpr *E); |
| 677 | ExpectedStmt VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E); |
| 678 | ExpectedStmt VisitCXXNamedCastExpr(CXXNamedCastExpr *E); |
| 679 | ExpectedStmt VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E); |
| 680 | ExpectedStmt VisitTypeTraitExpr(TypeTraitExpr *E); |
| 681 | ExpectedStmt VisitCXXTypeidExpr(CXXTypeidExpr *E); |
| 682 | ExpectedStmt VisitCXXFoldExpr(CXXFoldExpr *E); |
| 683 | |
| 684 | // Helper for chaining together multiple imports. If an error is detected, |
| 685 | // subsequent imports will return default constructed nodes, so that failure |
| 686 | // can be detected with a single conditional branch after a sequence of |
| 687 | // imports. |
| 688 | template <typename T> T importChecked(Error &Err, const T &From) { |
| 689 | // Don't attempt to import nodes if we hit an error earlier. |
| 690 | if (Err) |
| 691 | return T{}; |
| 692 | Expected<T> MaybeVal = import(From); |
| 693 | if (!MaybeVal) { |
| 694 | Err = MaybeVal.takeError(); |
| 695 | return T{}; |
| 696 | } |
| 697 | return *MaybeVal; |
| 698 | } |
| 699 | |
| 700 | template<typename IIter, typename OIter> |
| 701 | Error ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) { |
| 702 | using ItemT = std::remove_reference_t<decltype(*Obegin)>; |
| 703 | for (; Ibegin != Iend; ++Ibegin, ++Obegin) { |
| 704 | Expected<ItemT> ToOrErr = import(*Ibegin); |
| 705 | if (!ToOrErr) |
| 706 | return ToOrErr.takeError(); |
| 707 | *Obegin = *ToOrErr; |
| 708 | } |
| 709 | return Error::success(); |
| 710 | } |
| 711 | |
| 712 | // Import every item from a container structure into an output container. |
| 713 | // If error occurs, stops at first error and returns the error. |
| 714 | // The output container should have space for all needed elements (it is not |
| 715 | // expanded, new items are put into from the beginning). |
| 716 | template<typename InContainerTy, typename OutContainerTy> |
| 717 | Error ImportContainerChecked( |
| 718 | const InContainerTy &InContainer, OutContainerTy &OutContainer) { |
| 719 | return ImportArrayChecked( |
| 720 | InContainer.begin(), InContainer.end(), OutContainer.begin()); |
| 721 | } |
| 722 | |
| 723 | template<typename InContainerTy, typename OIter> |
| 724 | Error ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) { |
| 725 | return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin); |
| 726 | } |
| 727 | |
| 728 | Error ImportOverriddenMethods(CXXMethodDecl *ToMethod, |
| 729 | CXXMethodDecl *FromMethod); |
| 730 | |
| 731 | Expected<FunctionDecl *> FindFunctionTemplateSpecialization( |
| 732 | FunctionDecl *FromFD); |
| 733 | |
| 734 | // Returns true if the given function has a placeholder return type and |
| 735 | // that type is declared inside the body of the function. |
| 736 | // E.g. auto f() { struct X{}; return X(); } |
| 737 | bool hasReturnTypeDeclaredInside(FunctionDecl *D); |
| 738 | }; |
| 739 | |
| 740 | template <typename InContainerTy> |
| 741 | Error ASTNodeImporter::ImportTemplateArgumentListInfo( |
| 742 | SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc, |
| 743 | const InContainerTy &Container, TemplateArgumentListInfo &Result) { |
| 744 | auto ToLAngleLocOrErr = import(FromLAngleLoc); |
| 745 | if (!ToLAngleLocOrErr) |
| 746 | return ToLAngleLocOrErr.takeError(); |
| 747 | auto ToRAngleLocOrErr = import(FromRAngleLoc); |
| 748 | if (!ToRAngleLocOrErr) |
| 749 | return ToRAngleLocOrErr.takeError(); |
| 750 | |
| 751 | TemplateArgumentListInfo ToTAInfo(*ToLAngleLocOrErr, *ToRAngleLocOrErr); |
| 752 | if (auto Err = ImportTemplateArgumentListInfo(Container, ToTAInfo)) |
| 753 | return Err; |
| 754 | Result = ToTAInfo; |
| 755 | return Error::success(); |
| 756 | } |
| 757 | |
| 758 | template <> |
| 759 | Error ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>( |
| 760 | const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) { |
| 761 | return ImportTemplateArgumentListInfo( |
| 762 | From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result); |
| 763 | } |
| 764 | |
| 765 | template <> |
| 766 | Error ASTNodeImporter::ImportTemplateArgumentListInfo< |
| 767 | ASTTemplateArgumentListInfo>( |
| 768 | const ASTTemplateArgumentListInfo &From, |
| 769 | TemplateArgumentListInfo &Result) { |
| 770 | return ImportTemplateArgumentListInfo( |
| 771 | From.LAngleLoc, From.RAngleLoc, From.arguments(), Result); |
| 772 | } |
| 773 | |
| 774 | Expected<ASTNodeImporter::FunctionTemplateAndArgsTy> |
| 775 | ASTNodeImporter::ImportFunctionTemplateWithTemplateArgsFromSpecialization( |
| 776 | FunctionDecl *FromFD) { |
| 777 | assert(FromFD->getTemplatedKind() == |
| 778 | FunctionDecl::TK_FunctionTemplateSpecialization); |
| 779 | |
| 780 | FunctionTemplateAndArgsTy Result; |
| 781 | |
| 782 | auto *FTSInfo = FromFD->getTemplateSpecializationInfo(); |
| 783 | if (Error Err = importInto(std::get<0>(Result), FTSInfo->getTemplate())) |
| 784 | return std::move(Err); |
| 785 | |
| 786 | // Import template arguments. |
| 787 | if (Error Err = ImportTemplateArguments(FromArgs: FTSInfo->TemplateArguments->asArray(), |
| 788 | ToArgs&: std::get<1>(Result))) |
| 789 | return std::move(Err); |
| 790 | |
| 791 | return Result; |
| 792 | } |
| 793 | |
| 794 | template <> |
| 795 | Expected<TemplateParameterList *> |
| 796 | ASTNodeImporter::import(TemplateParameterList *From) { |
| 797 | SmallVector<NamedDecl *, 4> To(From->size()); |
| 798 | if (Error Err = ImportContainerChecked(*From, To)) |
| 799 | return std::move(Err); |
| 800 | |
| 801 | ExpectedExpr ToRequiresClause = import(From->getRequiresClause()); |
| 802 | if (!ToRequiresClause) |
| 803 | return ToRequiresClause.takeError(); |
| 804 | |
| 805 | auto ToTemplateLocOrErr = import(From->getTemplateLoc()); |
| 806 | if (!ToTemplateLocOrErr) |
| 807 | return ToTemplateLocOrErr.takeError(); |
| 808 | auto ToLAngleLocOrErr = import(From->getLAngleLoc()); |
| 809 | if (!ToLAngleLocOrErr) |
| 810 | return ToLAngleLocOrErr.takeError(); |
| 811 | auto ToRAngleLocOrErr = import(From->getRAngleLoc()); |
| 812 | if (!ToRAngleLocOrErr) |
| 813 | return ToRAngleLocOrErr.takeError(); |
| 814 | |
| 815 | return TemplateParameterList::Create( |
| 816 | C: Importer.getToContext(), |
| 817 | TemplateLoc: *ToTemplateLocOrErr, |
| 818 | LAngleLoc: *ToLAngleLocOrErr, |
| 819 | Params: To, |
| 820 | RAngleLoc: *ToRAngleLocOrErr, |
| 821 | RequiresClause: *ToRequiresClause); |
| 822 | } |
| 823 | |
| 824 | template <> |
| 825 | Expected<TemplateArgument> |
| 826 | ASTNodeImporter::import(const TemplateArgument &From) { |
| 827 | switch (From.getKind()) { |
| 828 | case TemplateArgument::Null: |
| 829 | return TemplateArgument(); |
| 830 | |
| 831 | case TemplateArgument::Type: { |
| 832 | ExpectedType ToTypeOrErr = import(From.getAsType()); |
| 833 | if (!ToTypeOrErr) |
| 834 | return ToTypeOrErr.takeError(); |
| 835 | return TemplateArgument(*ToTypeOrErr, /*isNullPtr*/ false, |
| 836 | From.getIsDefaulted()); |
| 837 | } |
| 838 | |
| 839 | case TemplateArgument::Integral: { |
| 840 | ExpectedType ToTypeOrErr = import(From.getIntegralType()); |
| 841 | if (!ToTypeOrErr) |
| 842 | return ToTypeOrErr.takeError(); |
| 843 | return TemplateArgument(From, *ToTypeOrErr); |
| 844 | } |
| 845 | |
| 846 | case TemplateArgument::Declaration: { |
| 847 | Expected<ValueDecl *> ToOrErr = import(From.getAsDecl()); |
| 848 | if (!ToOrErr) |
| 849 | return ToOrErr.takeError(); |
| 850 | ExpectedType ToTypeOrErr = import(From.getParamTypeForDecl()); |
| 851 | if (!ToTypeOrErr) |
| 852 | return ToTypeOrErr.takeError(); |
| 853 | return TemplateArgument(dyn_cast<ValueDecl>((*ToOrErr)->getCanonicalDecl()), |
| 854 | *ToTypeOrErr, From.getIsDefaulted()); |
| 855 | } |
| 856 | |
| 857 | case TemplateArgument::NullPtr: { |
| 858 | ExpectedType ToTypeOrErr = import(From.getNullPtrType()); |
| 859 | if (!ToTypeOrErr) |
| 860 | return ToTypeOrErr.takeError(); |
| 861 | return TemplateArgument(*ToTypeOrErr, /*isNullPtr*/ true, |
| 862 | From.getIsDefaulted()); |
| 863 | } |
| 864 | |
| 865 | case TemplateArgument::StructuralValue: { |
| 866 | ExpectedType ToTypeOrErr = import(From.getStructuralValueType()); |
| 867 | if (!ToTypeOrErr) |
| 868 | return ToTypeOrErr.takeError(); |
| 869 | Expected<APValue> ToValueOrErr = import(From.getAsStructuralValue()); |
| 870 | if (!ToValueOrErr) |
| 871 | return ToValueOrErr.takeError(); |
| 872 | return TemplateArgument(Importer.getToContext(), *ToTypeOrErr, |
| 873 | *ToValueOrErr); |
| 874 | } |
| 875 | |
| 876 | case TemplateArgument::Template: { |
| 877 | Expected<TemplateName> ToTemplateOrErr = import(From.getAsTemplate()); |
| 878 | if (!ToTemplateOrErr) |
| 879 | return ToTemplateOrErr.takeError(); |
| 880 | |
| 881 | return TemplateArgument(*ToTemplateOrErr, From.getIsDefaulted()); |
| 882 | } |
| 883 | |
| 884 | case TemplateArgument::TemplateExpansion: { |
| 885 | Expected<TemplateName> ToTemplateOrErr = |
| 886 | import(From.getAsTemplateOrTemplatePattern()); |
| 887 | if (!ToTemplateOrErr) |
| 888 | return ToTemplateOrErr.takeError(); |
| 889 | |
| 890 | return TemplateArgument(*ToTemplateOrErr, From.getNumTemplateExpansions(), |
| 891 | From.getIsDefaulted()); |
| 892 | } |
| 893 | |
| 894 | case TemplateArgument::Expression: |
| 895 | if (ExpectedExpr ToExpr = import(From.getAsExpr())) |
| 896 | return TemplateArgument(*ToExpr, From.isCanonicalExpr(), |
| 897 | From.getIsDefaulted()); |
| 898 | else |
| 899 | return ToExpr.takeError(); |
| 900 | |
| 901 | case TemplateArgument::Pack: { |
| 902 | SmallVector<TemplateArgument, 2> ToPack; |
| 903 | ToPack.reserve(From.pack_size()); |
| 904 | if (Error Err = ImportTemplateArguments(FromArgs: From.pack_elements(), ToArgs&: ToPack)) |
| 905 | return std::move(Err); |
| 906 | |
| 907 | return TemplateArgument( |
| 908 | llvm::ArrayRef(ToPack).copy(Importer.getToContext())); |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | llvm_unreachable("Invalid template argument kind" ); |
| 913 | } |
| 914 | |
| 915 | template <> |
| 916 | Expected<TemplateArgumentLoc> |
| 917 | ASTNodeImporter::import(const TemplateArgumentLoc &TALoc) { |
| 918 | Expected<TemplateArgument> ArgOrErr = import(TALoc.getArgument()); |
| 919 | if (!ArgOrErr) |
| 920 | return ArgOrErr.takeError(); |
| 921 | TemplateArgument Arg = *ArgOrErr; |
| 922 | |
| 923 | TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo(); |
| 924 | |
| 925 | TemplateArgumentLocInfo ToInfo; |
| 926 | if (Arg.getKind() == TemplateArgument::Expression) { |
| 927 | ExpectedExpr E = import(FromInfo.getAsExpr()); |
| 928 | if (!E) |
| 929 | return E.takeError(); |
| 930 | ToInfo = TemplateArgumentLocInfo(*E); |
| 931 | } else if (Arg.getKind() == TemplateArgument::Type) { |
| 932 | if (auto TSIOrErr = import(FromInfo.getAsTypeSourceInfo())) |
| 933 | ToInfo = TemplateArgumentLocInfo(*TSIOrErr); |
| 934 | else |
| 935 | return TSIOrErr.takeError(); |
| 936 | } else { |
| 937 | auto ToTemplateQualifierLocOrErr = |
| 938 | import(FromInfo.getTemplateQualifierLoc()); |
| 939 | if (!ToTemplateQualifierLocOrErr) |
| 940 | return ToTemplateQualifierLocOrErr.takeError(); |
| 941 | auto ToTemplateNameLocOrErr = import(FromInfo.getTemplateNameLoc()); |
| 942 | if (!ToTemplateNameLocOrErr) |
| 943 | return ToTemplateNameLocOrErr.takeError(); |
| 944 | auto ToTemplateEllipsisLocOrErr = |
| 945 | import(FromInfo.getTemplateEllipsisLoc()); |
| 946 | if (!ToTemplateEllipsisLocOrErr) |
| 947 | return ToTemplateEllipsisLocOrErr.takeError(); |
| 948 | ToInfo = TemplateArgumentLocInfo( |
| 949 | Importer.getToContext(), *ToTemplateQualifierLocOrErr, |
| 950 | *ToTemplateNameLocOrErr, *ToTemplateEllipsisLocOrErr); |
| 951 | } |
| 952 | |
| 953 | return TemplateArgumentLoc(Arg, ToInfo); |
| 954 | } |
| 955 | |
| 956 | template <> |
| 957 | Expected<DeclGroupRef> ASTNodeImporter::import(const DeclGroupRef &DG) { |
| 958 | if (DG.isNull()) |
| 959 | return DeclGroupRef::Create(C&: Importer.getToContext(), Decls: nullptr, NumDecls: 0); |
| 960 | size_t NumDecls = DG.end() - DG.begin(); |
| 961 | SmallVector<Decl *, 1> ToDecls; |
| 962 | ToDecls.reserve(NumDecls); |
| 963 | for (Decl *FromD : DG) { |
| 964 | if (auto ToDOrErr = import(FromD)) |
| 965 | ToDecls.push_back(*ToDOrErr); |
| 966 | else |
| 967 | return ToDOrErr.takeError(); |
| 968 | } |
| 969 | return DeclGroupRef::Create(C&: Importer.getToContext(), |
| 970 | Decls: ToDecls.begin(), |
| 971 | NumDecls); |
| 972 | } |
| 973 | |
| 974 | template <> |
| 975 | Expected<ASTNodeImporter::Designator> |
| 976 | ASTNodeImporter::import(const Designator &D) { |
| 977 | if (D.isFieldDesignator()) { |
| 978 | IdentifierInfo *ToFieldName = Importer.Import(FromId: D.getFieldName()); |
| 979 | |
| 980 | ExpectedSLoc ToDotLocOrErr = import(D.getDotLoc()); |
| 981 | if (!ToDotLocOrErr) |
| 982 | return ToDotLocOrErr.takeError(); |
| 983 | |
| 984 | ExpectedSLoc ToFieldLocOrErr = import(D.getFieldLoc()); |
| 985 | if (!ToFieldLocOrErr) |
| 986 | return ToFieldLocOrErr.takeError(); |
| 987 | |
| 988 | return DesignatedInitExpr::Designator::CreateFieldDesignator( |
| 989 | FieldName: ToFieldName, DotLoc: *ToDotLocOrErr, FieldLoc: *ToFieldLocOrErr); |
| 990 | } |
| 991 | |
| 992 | ExpectedSLoc ToLBracketLocOrErr = import(D.getLBracketLoc()); |
| 993 | if (!ToLBracketLocOrErr) |
| 994 | return ToLBracketLocOrErr.takeError(); |
| 995 | |
| 996 | ExpectedSLoc ToRBracketLocOrErr = import(D.getRBracketLoc()); |
| 997 | if (!ToRBracketLocOrErr) |
| 998 | return ToRBracketLocOrErr.takeError(); |
| 999 | |
| 1000 | if (D.isArrayDesignator()) |
| 1001 | return Designator::CreateArrayDesignator(Index: D.getArrayIndex(), |
| 1002 | LBracketLoc: *ToLBracketLocOrErr, |
| 1003 | RBracketLoc: *ToRBracketLocOrErr); |
| 1004 | |
| 1005 | ExpectedSLoc ToEllipsisLocOrErr = import(D.getEllipsisLoc()); |
| 1006 | if (!ToEllipsisLocOrErr) |
| 1007 | return ToEllipsisLocOrErr.takeError(); |
| 1008 | |
| 1009 | assert(D.isArrayRangeDesignator()); |
| 1010 | return Designator::CreateArrayRangeDesignator( |
| 1011 | Index: D.getArrayIndex(), LBracketLoc: *ToLBracketLocOrErr, EllipsisLoc: *ToEllipsisLocOrErr, |
| 1012 | RBracketLoc: *ToRBracketLocOrErr); |
| 1013 | } |
| 1014 | |
| 1015 | template <> |
| 1016 | Expected<ConceptReference *> ASTNodeImporter::import(ConceptReference *From) { |
| 1017 | Error Err = Error::success(); |
| 1018 | auto ToNNS = importChecked(Err, From->getNestedNameSpecifierLoc()); |
| 1019 | auto ToTemplateKWLoc = importChecked(Err, From->getTemplateKWLoc()); |
| 1020 | auto ToConceptNameLoc = |
| 1021 | importChecked(Err, From->getConceptNameInfo().getLoc()); |
| 1022 | auto ToConceptName = importChecked(Err, From->getConceptNameInfo().getName()); |
| 1023 | auto ToFoundDecl = importChecked(Err, From->getFoundDecl()); |
| 1024 | auto ToNamedConcept = importChecked(Err, From->getNamedConcept()); |
| 1025 | if (Err) |
| 1026 | return std::move(Err); |
| 1027 | TemplateArgumentListInfo ToTAInfo; |
| 1028 | const auto *ASTTemplateArgs = From->getTemplateArgsAsWritten(); |
| 1029 | if (ASTTemplateArgs) |
| 1030 | if (Error Err = ImportTemplateArgumentListInfo(*ASTTemplateArgs, ToTAInfo)) |
| 1031 | return std::move(Err); |
| 1032 | auto *ConceptRef = ConceptReference::Create( |
| 1033 | C: Importer.getToContext(), NNS: ToNNS, TemplateKWLoc: ToTemplateKWLoc, |
| 1034 | ConceptNameInfo: DeclarationNameInfo(ToConceptName, ToConceptNameLoc), FoundDecl: ToFoundDecl, |
| 1035 | NamedConcept: ToNamedConcept, |
| 1036 | ArgsAsWritten: ASTTemplateArgs ? ASTTemplateArgumentListInfo::Create( |
| 1037 | C: Importer.getToContext(), List: ToTAInfo) |
| 1038 | : nullptr); |
| 1039 | return ConceptRef; |
| 1040 | } |
| 1041 | |
| 1042 | template <> |
| 1043 | Expected<LambdaCapture> ASTNodeImporter::import(const LambdaCapture &From) { |
| 1044 | ValueDecl *Var = nullptr; |
| 1045 | if (From.capturesVariable()) { |
| 1046 | if (auto VarOrErr = import(From.getCapturedVar())) |
| 1047 | Var = *VarOrErr; |
| 1048 | else |
| 1049 | return VarOrErr.takeError(); |
| 1050 | } |
| 1051 | |
| 1052 | auto LocationOrErr = import(From.getLocation()); |
| 1053 | if (!LocationOrErr) |
| 1054 | return LocationOrErr.takeError(); |
| 1055 | |
| 1056 | SourceLocation EllipsisLoc; |
| 1057 | if (From.isPackExpansion()) |
| 1058 | if (Error Err = importInto(EllipsisLoc, From.getEllipsisLoc())) |
| 1059 | return std::move(Err); |
| 1060 | |
| 1061 | return LambdaCapture( |
| 1062 | *LocationOrErr, From.isImplicit(), From.getCaptureKind(), Var, |
| 1063 | EllipsisLoc); |
| 1064 | } |
| 1065 | |
| 1066 | template <typename T> |
| 1067 | bool ASTNodeImporter::hasSameVisibilityContextAndLinkage(T *Found, T *From) { |
| 1068 | if (Found->getLinkageInternal() != From->getLinkageInternal()) |
| 1069 | return false; |
| 1070 | |
| 1071 | if (From->hasExternalFormalLinkage()) |
| 1072 | return Found->hasExternalFormalLinkage(); |
| 1073 | if (Importer.GetFromTU(ToD: Found) != From->getTranslationUnitDecl()) |
| 1074 | return false; |
| 1075 | if (From->isInAnonymousNamespace()) |
| 1076 | return Found->isInAnonymousNamespace(); |
| 1077 | else |
| 1078 | return !Found->isInAnonymousNamespace() && |
| 1079 | !Found->hasExternalFormalLinkage(); |
| 1080 | } |
| 1081 | |
| 1082 | template <> |
| 1083 | bool ASTNodeImporter::hasSameVisibilityContextAndLinkage(TypedefNameDecl *Found, |
| 1084 | TypedefNameDecl *From) { |
| 1085 | if (Found->getLinkageInternal() != From->getLinkageInternal()) |
| 1086 | return false; |
| 1087 | |
| 1088 | if (From->isInAnonymousNamespace() && Found->isInAnonymousNamespace()) |
| 1089 | return Importer.GetFromTU(Found) == From->getTranslationUnitDecl(); |
| 1090 | return From->isInAnonymousNamespace() == Found->isInAnonymousNamespace(); |
| 1091 | } |
| 1092 | |
| 1093 | } // namespace clang |
| 1094 | |
| 1095 | //---------------------------------------------------------------------------- |
| 1096 | // Import Types |
| 1097 | //---------------------------------------------------------------------------- |
| 1098 | |
| 1099 | using namespace clang; |
| 1100 | |
| 1101 | ExpectedType ASTNodeImporter::VisitType(const Type *T) { |
| 1102 | Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node) |
| 1103 | << T->getTypeClassName(); |
| 1104 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
| 1105 | } |
| 1106 | |
| 1107 | ExpectedType ASTNodeImporter::VisitAtomicType(const AtomicType *T){ |
| 1108 | ExpectedType UnderlyingTypeOrErr = import(From: T->getValueType()); |
| 1109 | if (!UnderlyingTypeOrErr) |
| 1110 | return UnderlyingTypeOrErr.takeError(); |
| 1111 | |
| 1112 | return Importer.getToContext().getAtomicType(T: *UnderlyingTypeOrErr); |
| 1113 | } |
| 1114 | |
| 1115 | ExpectedType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) { |
| 1116 | switch (T->getKind()) { |
| 1117 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 1118 | case BuiltinType::Id: \ |
| 1119 | return Importer.getToContext().SingletonId; |
| 1120 | #include "clang/Basic/OpenCLImageTypes.def" |
| 1121 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
| 1122 | case BuiltinType::Id: \ |
| 1123 | return Importer.getToContext().Id##Ty; |
| 1124 | #include "clang/Basic/OpenCLExtensionTypes.def" |
| 1125 | #define SVE_TYPE(Name, Id, SingletonId) \ |
| 1126 | case BuiltinType::Id: \ |
| 1127 | return Importer.getToContext().SingletonId; |
| 1128 | #include "clang/Basic/AArch64ACLETypes.def" |
| 1129 | #define PPC_VECTOR_TYPE(Name, Id, Size) \ |
| 1130 | case BuiltinType::Id: \ |
| 1131 | return Importer.getToContext().Id##Ty; |
| 1132 | #include "clang/Basic/PPCTypes.def" |
| 1133 | #define RVV_TYPE(Name, Id, SingletonId) \ |
| 1134 | case BuiltinType::Id: \ |
| 1135 | return Importer.getToContext().SingletonId; |
| 1136 | #include "clang/Basic/RISCVVTypes.def" |
| 1137 | #define WASM_TYPE(Name, Id, SingletonId) \ |
| 1138 | case BuiltinType::Id: \ |
| 1139 | return Importer.getToContext().SingletonId; |
| 1140 | #include "clang/Basic/WebAssemblyReferenceTypes.def" |
| 1141 | #define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) \ |
| 1142 | case BuiltinType::Id: \ |
| 1143 | return Importer.getToContext().SingletonId; |
| 1144 | #include "clang/Basic/AMDGPUTypes.def" |
| 1145 | #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \ |
| 1146 | case BuiltinType::Id: \ |
| 1147 | return Importer.getToContext().SingletonId; |
| 1148 | #include "clang/Basic/HLSLIntangibleTypes.def" |
| 1149 | #define SHARED_SINGLETON_TYPE(Expansion) |
| 1150 | #define BUILTIN_TYPE(Id, SingletonId) \ |
| 1151 | case BuiltinType::Id: return Importer.getToContext().SingletonId; |
| 1152 | #include "clang/AST/BuiltinTypes.def" |
| 1153 | |
| 1154 | // FIXME: for Char16, Char32, and NullPtr, make sure that the "to" |
| 1155 | // context supports C++. |
| 1156 | |
| 1157 | // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to" |
| 1158 | // context supports ObjC. |
| 1159 | |
| 1160 | case BuiltinType::Char_U: |
| 1161 | // The context we're importing from has an unsigned 'char'. If we're |
| 1162 | // importing into a context with a signed 'char', translate to |
| 1163 | // 'unsigned char' instead. |
| 1164 | if (Importer.getToContext().getLangOpts().CharIsSigned) |
| 1165 | return Importer.getToContext().UnsignedCharTy; |
| 1166 | |
| 1167 | return Importer.getToContext().CharTy; |
| 1168 | |
| 1169 | case BuiltinType::Char_S: |
| 1170 | // The context we're importing from has an unsigned 'char'. If we're |
| 1171 | // importing into a context with a signed 'char', translate to |
| 1172 | // 'unsigned char' instead. |
| 1173 | if (!Importer.getToContext().getLangOpts().CharIsSigned) |
| 1174 | return Importer.getToContext().SignedCharTy; |
| 1175 | |
| 1176 | return Importer.getToContext().CharTy; |
| 1177 | |
| 1178 | case BuiltinType::WChar_S: |
| 1179 | case BuiltinType::WChar_U: |
| 1180 | // FIXME: If not in C++, shall we translate to the C equivalent of |
| 1181 | // wchar_t? |
| 1182 | return Importer.getToContext().WCharTy; |
| 1183 | } |
| 1184 | |
| 1185 | llvm_unreachable("Invalid BuiltinType Kind!" ); |
| 1186 | } |
| 1187 | |
| 1188 | ExpectedType ASTNodeImporter::VisitDecayedType(const DecayedType *T) { |
| 1189 | ExpectedType ToOriginalTypeOrErr = import(T->getOriginalType()); |
| 1190 | if (!ToOriginalTypeOrErr) |
| 1191 | return ToOriginalTypeOrErr.takeError(); |
| 1192 | |
| 1193 | return Importer.getToContext().getDecayedType(T: *ToOriginalTypeOrErr); |
| 1194 | } |
| 1195 | |
| 1196 | ExpectedType ASTNodeImporter::VisitComplexType(const ComplexType *T) { |
| 1197 | ExpectedType ToElementTypeOrErr = import(From: T->getElementType()); |
| 1198 | if (!ToElementTypeOrErr) |
| 1199 | return ToElementTypeOrErr.takeError(); |
| 1200 | |
| 1201 | return Importer.getToContext().getComplexType(T: *ToElementTypeOrErr); |
| 1202 | } |
| 1203 | |
| 1204 | ExpectedType ASTNodeImporter::VisitPointerType(const PointerType *T) { |
| 1205 | ExpectedType ToPointeeTypeOrErr = import(From: T->getPointeeType()); |
| 1206 | if (!ToPointeeTypeOrErr) |
| 1207 | return ToPointeeTypeOrErr.takeError(); |
| 1208 | |
| 1209 | return Importer.getToContext().getPointerType(T: *ToPointeeTypeOrErr); |
| 1210 | } |
| 1211 | |
| 1212 | ExpectedType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) { |
| 1213 | // FIXME: Check for blocks support in "to" context. |
| 1214 | ExpectedType ToPointeeTypeOrErr = import(From: T->getPointeeType()); |
| 1215 | if (!ToPointeeTypeOrErr) |
| 1216 | return ToPointeeTypeOrErr.takeError(); |
| 1217 | |
| 1218 | return Importer.getToContext().getBlockPointerType(T: *ToPointeeTypeOrErr); |
| 1219 | } |
| 1220 | |
| 1221 | ExpectedType |
| 1222 | ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) { |
| 1223 | // FIXME: Check for C++ support in "to" context. |
| 1224 | ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten()); |
| 1225 | if (!ToPointeeTypeOrErr) |
| 1226 | return ToPointeeTypeOrErr.takeError(); |
| 1227 | |
| 1228 | return Importer.getToContext().getLValueReferenceType(T: *ToPointeeTypeOrErr); |
| 1229 | } |
| 1230 | |
| 1231 | ExpectedType |
| 1232 | ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) { |
| 1233 | // FIXME: Check for C++0x support in "to" context. |
| 1234 | ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten()); |
| 1235 | if (!ToPointeeTypeOrErr) |
| 1236 | return ToPointeeTypeOrErr.takeError(); |
| 1237 | |
| 1238 | return Importer.getToContext().getRValueReferenceType(T: *ToPointeeTypeOrErr); |
| 1239 | } |
| 1240 | |
| 1241 | ExpectedType |
| 1242 | ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) { |
| 1243 | // FIXME: Check for C++ support in "to" context. |
| 1244 | ExpectedType ToPointeeTypeOrErr = import(From: T->getPointeeType()); |
| 1245 | if (!ToPointeeTypeOrErr) |
| 1246 | return ToPointeeTypeOrErr.takeError(); |
| 1247 | |
| 1248 | auto QualifierOrErr = import(From: T->getQualifier()); |
| 1249 | if (!QualifierOrErr) |
| 1250 | return QualifierOrErr.takeError(); |
| 1251 | |
| 1252 | auto ClsOrErr = import(From: T->getMostRecentCXXRecordDecl()); |
| 1253 | if (!ClsOrErr) |
| 1254 | return ClsOrErr.takeError(); |
| 1255 | |
| 1256 | return Importer.getToContext().getMemberPointerType( |
| 1257 | T: *ToPointeeTypeOrErr, Qualifier: *QualifierOrErr, Cls: *ClsOrErr); |
| 1258 | } |
| 1259 | |
| 1260 | ExpectedType |
| 1261 | ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) { |
| 1262 | Error Err = Error::success(); |
| 1263 | auto ToElementType = importChecked(Err, T->getElementType()); |
| 1264 | auto ToSizeExpr = importChecked(Err, From: T->getSizeExpr()); |
| 1265 | if (Err) |
| 1266 | return std::move(Err); |
| 1267 | |
| 1268 | return Importer.getToContext().getConstantArrayType( |
| 1269 | EltTy: ToElementType, ArySize: T->getSize(), SizeExpr: ToSizeExpr, ASM: T->getSizeModifier(), |
| 1270 | IndexTypeQuals: T->getIndexTypeCVRQualifiers()); |
| 1271 | } |
| 1272 | |
| 1273 | ExpectedType |
| 1274 | ASTNodeImporter::VisitArrayParameterType(const ArrayParameterType *T) { |
| 1275 | ExpectedType ToArrayTypeOrErr = VisitConstantArrayType(T); |
| 1276 | if (!ToArrayTypeOrErr) |
| 1277 | return ToArrayTypeOrErr.takeError(); |
| 1278 | |
| 1279 | return Importer.getToContext().getArrayParameterType(Ty: *ToArrayTypeOrErr); |
| 1280 | } |
| 1281 | |
| 1282 | ExpectedType |
| 1283 | ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) { |
| 1284 | ExpectedType ToElementTypeOrErr = import(T->getElementType()); |
| 1285 | if (!ToElementTypeOrErr) |
| 1286 | return ToElementTypeOrErr.takeError(); |
| 1287 | |
| 1288 | return Importer.getToContext().getIncompleteArrayType(EltTy: *ToElementTypeOrErr, |
| 1289 | ASM: T->getSizeModifier(), |
| 1290 | IndexTypeQuals: T->getIndexTypeCVRQualifiers()); |
| 1291 | } |
| 1292 | |
| 1293 | ExpectedType |
| 1294 | ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) { |
| 1295 | Error Err = Error::success(); |
| 1296 | QualType ToElementType = importChecked(Err, T->getElementType()); |
| 1297 | Expr *ToSizeExpr = importChecked(Err, From: T->getSizeExpr()); |
| 1298 | if (Err) |
| 1299 | return std::move(Err); |
| 1300 | return Importer.getToContext().getVariableArrayType( |
| 1301 | EltTy: ToElementType, NumElts: ToSizeExpr, ASM: T->getSizeModifier(), |
| 1302 | IndexTypeQuals: T->getIndexTypeCVRQualifiers()); |
| 1303 | } |
| 1304 | |
| 1305 | ExpectedType ASTNodeImporter::VisitDependentSizedArrayType( |
| 1306 | const DependentSizedArrayType *T) { |
| 1307 | Error Err = Error::success(); |
| 1308 | QualType ToElementType = importChecked(Err, T->getElementType()); |
| 1309 | Expr *ToSizeExpr = importChecked(Err, From: T->getSizeExpr()); |
| 1310 | if (Err) |
| 1311 | return std::move(Err); |
| 1312 | // SizeExpr may be null if size is not specified directly. |
| 1313 | // For example, 'int a[]'. |
| 1314 | |
| 1315 | return Importer.getToContext().getDependentSizedArrayType( |
| 1316 | EltTy: ToElementType, NumElts: ToSizeExpr, ASM: T->getSizeModifier(), |
| 1317 | IndexTypeQuals: T->getIndexTypeCVRQualifiers()); |
| 1318 | } |
| 1319 | |
| 1320 | ExpectedType ASTNodeImporter::VisitDependentSizedExtVectorType( |
| 1321 | const DependentSizedExtVectorType *T) { |
| 1322 | Error Err = Error::success(); |
| 1323 | QualType ToElementType = importChecked(Err, From: T->getElementType()); |
| 1324 | Expr *ToSizeExpr = importChecked(Err, From: T->getSizeExpr()); |
| 1325 | SourceLocation ToAttrLoc = importChecked(Err, From: T->getAttributeLoc()); |
| 1326 | if (Err) |
| 1327 | return std::move(Err); |
| 1328 | return Importer.getToContext().getDependentSizedExtVectorType( |
| 1329 | VectorType: ToElementType, SizeExpr: ToSizeExpr, AttrLoc: ToAttrLoc); |
| 1330 | } |
| 1331 | |
| 1332 | ExpectedType ASTNodeImporter::VisitVectorType(const VectorType *T) { |
| 1333 | ExpectedType ToElementTypeOrErr = import(From: T->getElementType()); |
| 1334 | if (!ToElementTypeOrErr) |
| 1335 | return ToElementTypeOrErr.takeError(); |
| 1336 | |
| 1337 | return Importer.getToContext().getVectorType(VectorType: *ToElementTypeOrErr, |
| 1338 | NumElts: T->getNumElements(), |
| 1339 | VecKind: T->getVectorKind()); |
| 1340 | } |
| 1341 | |
| 1342 | ExpectedType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) { |
| 1343 | ExpectedType ToElementTypeOrErr = import(T->getElementType()); |
| 1344 | if (!ToElementTypeOrErr) |
| 1345 | return ToElementTypeOrErr.takeError(); |
| 1346 | |
| 1347 | return Importer.getToContext().getExtVectorType(VectorType: *ToElementTypeOrErr, |
| 1348 | NumElts: T->getNumElements()); |
| 1349 | } |
| 1350 | |
| 1351 | ExpectedType |
| 1352 | ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { |
| 1353 | // FIXME: What happens if we're importing a function without a prototype |
| 1354 | // into C++? Should we make it variadic? |
| 1355 | ExpectedType ToReturnTypeOrErr = import(T->getReturnType()); |
| 1356 | if (!ToReturnTypeOrErr) |
| 1357 | return ToReturnTypeOrErr.takeError(); |
| 1358 | |
| 1359 | return Importer.getToContext().getFunctionNoProtoType(*ToReturnTypeOrErr, |
| 1360 | T->getExtInfo()); |
| 1361 | } |
| 1362 | |
| 1363 | ExpectedType |
| 1364 | ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) { |
| 1365 | ExpectedType ToReturnTypeOrErr = import(T->getReturnType()); |
| 1366 | if (!ToReturnTypeOrErr) |
| 1367 | return ToReturnTypeOrErr.takeError(); |
| 1368 | |
| 1369 | // Import argument types |
| 1370 | SmallVector<QualType, 4> ArgTypes; |
| 1371 | for (const auto &A : T->param_types()) { |
| 1372 | ExpectedType TyOrErr = import(From: A); |
| 1373 | if (!TyOrErr) |
| 1374 | return TyOrErr.takeError(); |
| 1375 | ArgTypes.push_back(Elt: *TyOrErr); |
| 1376 | } |
| 1377 | |
| 1378 | // Import exception types |
| 1379 | SmallVector<QualType, 4> ExceptionTypes; |
| 1380 | for (const auto &E : T->exceptions()) { |
| 1381 | ExpectedType TyOrErr = import(From: E); |
| 1382 | if (!TyOrErr) |
| 1383 | return TyOrErr.takeError(); |
| 1384 | ExceptionTypes.push_back(Elt: *TyOrErr); |
| 1385 | } |
| 1386 | |
| 1387 | FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo(); |
| 1388 | Error Err = Error::success(); |
| 1389 | FunctionProtoType::ExtProtoInfo ToEPI; |
| 1390 | ToEPI.ExtInfo = FromEPI.ExtInfo; |
| 1391 | ToEPI.Variadic = FromEPI.Variadic; |
| 1392 | ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn; |
| 1393 | ToEPI.TypeQuals = FromEPI.TypeQuals; |
| 1394 | ToEPI.RefQualifier = FromEPI.RefQualifier; |
| 1395 | ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type; |
| 1396 | ToEPI.ExceptionSpec.NoexceptExpr = |
| 1397 | importChecked(Err, FromEPI.ExceptionSpec.NoexceptExpr); |
| 1398 | ToEPI.ExceptionSpec.SourceDecl = |
| 1399 | importChecked(Err, FromEPI.ExceptionSpec.SourceDecl); |
| 1400 | ToEPI.ExceptionSpec.SourceTemplate = |
| 1401 | importChecked(Err, FromEPI.ExceptionSpec.SourceTemplate); |
| 1402 | ToEPI.ExceptionSpec.Exceptions = ExceptionTypes; |
| 1403 | |
| 1404 | if (Err) |
| 1405 | return std::move(Err); |
| 1406 | |
| 1407 | return Importer.getToContext().getFunctionType( |
| 1408 | ResultTy: *ToReturnTypeOrErr, Args: ArgTypes, EPI: ToEPI); |
| 1409 | } |
| 1410 | |
| 1411 | ExpectedType ASTNodeImporter::VisitUnresolvedUsingType( |
| 1412 | const UnresolvedUsingType *T) { |
| 1413 | Error Err = Error::success(); |
| 1414 | auto ToD = importChecked(Err, From: T->getDecl()); |
| 1415 | auto ToPrevD = importChecked(Err, T->getDecl()->getPreviousDecl()); |
| 1416 | if (Err) |
| 1417 | return std::move(Err); |
| 1418 | |
| 1419 | return Importer.getToContext().getTypeDeclType( |
| 1420 | Decl: ToD, PrevDecl: cast_or_null<TypeDecl>(ToPrevD)); |
| 1421 | } |
| 1422 | |
| 1423 | ExpectedType ASTNodeImporter::VisitParenType(const ParenType *T) { |
| 1424 | ExpectedType ToInnerTypeOrErr = import(From: T->getInnerType()); |
| 1425 | if (!ToInnerTypeOrErr) |
| 1426 | return ToInnerTypeOrErr.takeError(); |
| 1427 | |
| 1428 | return Importer.getToContext().getParenType(NamedType: *ToInnerTypeOrErr); |
| 1429 | } |
| 1430 | |
| 1431 | ExpectedType |
| 1432 | ASTNodeImporter::VisitPackIndexingType(clang::PackIndexingType const *T) { |
| 1433 | |
| 1434 | ExpectedType Pattern = import(From: T->getPattern()); |
| 1435 | if (!Pattern) |
| 1436 | return Pattern.takeError(); |
| 1437 | ExpectedExpr Index = import(From: T->getIndexExpr()); |
| 1438 | if (!Index) |
| 1439 | return Index.takeError(); |
| 1440 | return Importer.getToContext().getPackIndexingType(Pattern: *Pattern, IndexExpr: *Index); |
| 1441 | } |
| 1442 | |
| 1443 | ExpectedType ASTNodeImporter::VisitTypedefType(const TypedefType *T) { |
| 1444 | Expected<TypedefNameDecl *> ToDeclOrErr = import(From: T->getDecl()); |
| 1445 | if (!ToDeclOrErr) |
| 1446 | return ToDeclOrErr.takeError(); |
| 1447 | |
| 1448 | TypedefNameDecl *ToDecl = *ToDeclOrErr; |
| 1449 | if (ToDecl->getTypeForDecl()) |
| 1450 | return QualType(ToDecl->getTypeForDecl(), 0); |
| 1451 | |
| 1452 | ExpectedType ToUnderlyingTypeOrErr = import(From: T->desugar()); |
| 1453 | if (!ToUnderlyingTypeOrErr) |
| 1454 | return ToUnderlyingTypeOrErr.takeError(); |
| 1455 | |
| 1456 | return Importer.getToContext().getTypedefType(Decl: ToDecl, Underlying: *ToUnderlyingTypeOrErr); |
| 1457 | } |
| 1458 | |
| 1459 | ExpectedType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) { |
| 1460 | ExpectedExpr ToExprOrErr = import(From: T->getUnderlyingExpr()); |
| 1461 | if (!ToExprOrErr) |
| 1462 | return ToExprOrErr.takeError(); |
| 1463 | return Importer.getToContext().getTypeOfExprType(E: *ToExprOrErr, Kind: T->getKind()); |
| 1464 | } |
| 1465 | |
| 1466 | ExpectedType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) { |
| 1467 | ExpectedType ToUnderlyingTypeOrErr = import(From: T->getUnmodifiedType()); |
| 1468 | if (!ToUnderlyingTypeOrErr) |
| 1469 | return ToUnderlyingTypeOrErr.takeError(); |
| 1470 | return Importer.getToContext().getTypeOfType(QT: *ToUnderlyingTypeOrErr, |
| 1471 | Kind: T->getKind()); |
| 1472 | } |
| 1473 | |
| 1474 | ExpectedType ASTNodeImporter::VisitUsingType(const UsingType *T) { |
| 1475 | Expected<UsingShadowDecl *> FoundOrErr = import(From: T->getFoundDecl()); |
| 1476 | if (!FoundOrErr) |
| 1477 | return FoundOrErr.takeError(); |
| 1478 | Expected<QualType> UnderlyingOrErr = import(From: T->getUnderlyingType()); |
| 1479 | if (!UnderlyingOrErr) |
| 1480 | return UnderlyingOrErr.takeError(); |
| 1481 | |
| 1482 | return Importer.getToContext().getUsingType(Found: *FoundOrErr, Underlying: *UnderlyingOrErr); |
| 1483 | } |
| 1484 | |
| 1485 | ExpectedType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) { |
| 1486 | // FIXME: Make sure that the "to" context supports C++0x! |
| 1487 | ExpectedExpr ToExprOrErr = import(From: T->getUnderlyingExpr()); |
| 1488 | if (!ToExprOrErr) |
| 1489 | return ToExprOrErr.takeError(); |
| 1490 | |
| 1491 | ExpectedType ToUnderlyingTypeOrErr = import(From: T->getUnderlyingType()); |
| 1492 | if (!ToUnderlyingTypeOrErr) |
| 1493 | return ToUnderlyingTypeOrErr.takeError(); |
| 1494 | |
| 1495 | return Importer.getToContext().getDecltypeType( |
| 1496 | e: *ToExprOrErr, UnderlyingType: *ToUnderlyingTypeOrErr); |
| 1497 | } |
| 1498 | |
| 1499 | ExpectedType |
| 1500 | ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) { |
| 1501 | ExpectedType ToBaseTypeOrErr = import(From: T->getBaseType()); |
| 1502 | if (!ToBaseTypeOrErr) |
| 1503 | return ToBaseTypeOrErr.takeError(); |
| 1504 | |
| 1505 | ExpectedType ToUnderlyingTypeOrErr = import(From: T->getUnderlyingType()); |
| 1506 | if (!ToUnderlyingTypeOrErr) |
| 1507 | return ToUnderlyingTypeOrErr.takeError(); |
| 1508 | |
| 1509 | return Importer.getToContext().getUnaryTransformType( |
| 1510 | BaseType: *ToBaseTypeOrErr, UnderlyingType: *ToUnderlyingTypeOrErr, UKind: T->getUTTKind()); |
| 1511 | } |
| 1512 | |
| 1513 | ExpectedType ASTNodeImporter::VisitAutoType(const AutoType *T) { |
| 1514 | // FIXME: Make sure that the "to" context supports C++11! |
| 1515 | ExpectedType ToDeducedTypeOrErr = import(T->getDeducedType()); |
| 1516 | if (!ToDeducedTypeOrErr) |
| 1517 | return ToDeducedTypeOrErr.takeError(); |
| 1518 | |
| 1519 | ExpectedDecl ToTypeConstraintConcept = import(From: T->getTypeConstraintConcept()); |
| 1520 | if (!ToTypeConstraintConcept) |
| 1521 | return ToTypeConstraintConcept.takeError(); |
| 1522 | |
| 1523 | SmallVector<TemplateArgument, 2> ToTemplateArgs; |
| 1524 | if (Error Err = ImportTemplateArguments(FromArgs: T->getTypeConstraintArguments(), |
| 1525 | ToArgs&: ToTemplateArgs)) |
| 1526 | return std::move(Err); |
| 1527 | |
| 1528 | return Importer.getToContext().getAutoType( |
| 1529 | DeducedType: *ToDeducedTypeOrErr, Keyword: T->getKeyword(), /*IsDependent*/false, |
| 1530 | /*IsPack=*/false, TypeConstraintConcept: cast_or_null<ConceptDecl>(Val: *ToTypeConstraintConcept), |
| 1531 | TypeConstraintArgs: ToTemplateArgs); |
| 1532 | } |
| 1533 | |
| 1534 | ExpectedType ASTNodeImporter::VisitDeducedTemplateSpecializationType( |
| 1535 | const DeducedTemplateSpecializationType *T) { |
| 1536 | // FIXME: Make sure that the "to" context supports C++17! |
| 1537 | Expected<TemplateName> ToTemplateNameOrErr = import(From: T->getTemplateName()); |
| 1538 | if (!ToTemplateNameOrErr) |
| 1539 | return ToTemplateNameOrErr.takeError(); |
| 1540 | ExpectedType ToDeducedTypeOrErr = import(T->getDeducedType()); |
| 1541 | if (!ToDeducedTypeOrErr) |
| 1542 | return ToDeducedTypeOrErr.takeError(); |
| 1543 | |
| 1544 | return Importer.getToContext().getDeducedTemplateSpecializationType( |
| 1545 | Template: *ToTemplateNameOrErr, DeducedType: *ToDeducedTypeOrErr, IsDependent: T->isDependentType()); |
| 1546 | } |
| 1547 | |
| 1548 | ExpectedType ASTNodeImporter::VisitInjectedClassNameType( |
| 1549 | const InjectedClassNameType *T) { |
| 1550 | Expected<CXXRecordDecl *> ToDeclOrErr = import(From: T->getDecl()); |
| 1551 | if (!ToDeclOrErr) |
| 1552 | return ToDeclOrErr.takeError(); |
| 1553 | |
| 1554 | // The InjectedClassNameType is created in VisitRecordDecl when the |
| 1555 | // T->getDecl() is imported. Here we can return the existing type. |
| 1556 | const Type *Ty = (*ToDeclOrErr)->getTypeForDecl(); |
| 1557 | assert(isa_and_nonnull<InjectedClassNameType>(Ty)); |
| 1558 | return QualType(Ty, 0); |
| 1559 | } |
| 1560 | |
| 1561 | ExpectedType ASTNodeImporter::VisitRecordType(const RecordType *T) { |
| 1562 | Expected<RecordDecl *> ToDeclOrErr = import(From: T->getDecl()); |
| 1563 | if (!ToDeclOrErr) |
| 1564 | return ToDeclOrErr.takeError(); |
| 1565 | |
| 1566 | return Importer.getToContext().getTagDeclType(*ToDeclOrErr); |
| 1567 | } |
| 1568 | |
| 1569 | ExpectedType ASTNodeImporter::VisitEnumType(const EnumType *T) { |
| 1570 | Expected<EnumDecl *> ToDeclOrErr = import(From: T->getDecl()); |
| 1571 | if (!ToDeclOrErr) |
| 1572 | return ToDeclOrErr.takeError(); |
| 1573 | |
| 1574 | return Importer.getToContext().getTagDeclType(*ToDeclOrErr); |
| 1575 | } |
| 1576 | |
| 1577 | ExpectedType ASTNodeImporter::VisitAttributedType(const AttributedType *T) { |
| 1578 | ExpectedType ToModifiedTypeOrErr = import(From: T->getModifiedType()); |
| 1579 | if (!ToModifiedTypeOrErr) |
| 1580 | return ToModifiedTypeOrErr.takeError(); |
| 1581 | ExpectedType ToEquivalentTypeOrErr = import(From: T->getEquivalentType()); |
| 1582 | if (!ToEquivalentTypeOrErr) |
| 1583 | return ToEquivalentTypeOrErr.takeError(); |
| 1584 | |
| 1585 | return Importer.getToContext().getAttributedType( |
| 1586 | attrKind: T->getAttrKind(), modifiedType: *ToModifiedTypeOrErr, equivalentType: *ToEquivalentTypeOrErr, |
| 1587 | attr: T->getAttr()); |
| 1588 | } |
| 1589 | |
| 1590 | ExpectedType |
| 1591 | ASTNodeImporter::VisitCountAttributedType(const CountAttributedType *T) { |
| 1592 | ExpectedType ToWrappedTypeOrErr = import(T->desugar()); |
| 1593 | if (!ToWrappedTypeOrErr) |
| 1594 | return ToWrappedTypeOrErr.takeError(); |
| 1595 | |
| 1596 | Error Err = Error::success(); |
| 1597 | Expr *CountExpr = importChecked(Err, From: T->getCountExpr()); |
| 1598 | |
| 1599 | SmallVector<TypeCoupledDeclRefInfo, 1> CoupledDecls; |
| 1600 | for (const TypeCoupledDeclRefInfo &TI : T->dependent_decls()) { |
| 1601 | Expected<ValueDecl *> ToDeclOrErr = import(TI.getDecl()); |
| 1602 | if (!ToDeclOrErr) |
| 1603 | return ToDeclOrErr.takeError(); |
| 1604 | CoupledDecls.emplace_back(*ToDeclOrErr, TI.isDeref()); |
| 1605 | } |
| 1606 | |
| 1607 | return Importer.getToContext().getCountAttributedType( |
| 1608 | T: *ToWrappedTypeOrErr, CountExpr, CountInBytes: T->isCountInBytes(), OrNull: T->isOrNull(), |
| 1609 | DependentDecls: ArrayRef(CoupledDecls.data(), CoupledDecls.size())); |
| 1610 | } |
| 1611 | |
| 1612 | ExpectedType ASTNodeImporter::VisitTemplateTypeParmType( |
| 1613 | const TemplateTypeParmType *T) { |
| 1614 | Expected<TemplateTypeParmDecl *> ToDeclOrErr = import(From: T->getDecl()); |
| 1615 | if (!ToDeclOrErr) |
| 1616 | return ToDeclOrErr.takeError(); |
| 1617 | |
| 1618 | return Importer.getToContext().getTemplateTypeParmType( |
| 1619 | Depth: T->getDepth(), Index: T->getIndex(), ParameterPack: T->isParameterPack(), ParmDecl: *ToDeclOrErr); |
| 1620 | } |
| 1621 | |
| 1622 | ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmType( |
| 1623 | const SubstTemplateTypeParmType *T) { |
| 1624 | Expected<Decl *> ReplacedOrErr = import(From: T->getAssociatedDecl()); |
| 1625 | if (!ReplacedOrErr) |
| 1626 | return ReplacedOrErr.takeError(); |
| 1627 | |
| 1628 | ExpectedType ToReplacementTypeOrErr = import(From: T->getReplacementType()); |
| 1629 | if (!ToReplacementTypeOrErr) |
| 1630 | return ToReplacementTypeOrErr.takeError(); |
| 1631 | |
| 1632 | return Importer.getToContext().getSubstTemplateTypeParmType( |
| 1633 | Replacement: *ToReplacementTypeOrErr, AssociatedDecl: *ReplacedOrErr, Index: T->getIndex(), PackIndex: T->getPackIndex(), |
| 1634 | Final: T->getFinal()); |
| 1635 | } |
| 1636 | |
| 1637 | ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmPackType( |
| 1638 | const SubstTemplateTypeParmPackType *T) { |
| 1639 | Expected<Decl *> ReplacedOrErr = import(From: T->getAssociatedDecl()); |
| 1640 | if (!ReplacedOrErr) |
| 1641 | return ReplacedOrErr.takeError(); |
| 1642 | |
| 1643 | Expected<TemplateArgument> ToArgumentPack = import(From: T->getArgumentPack()); |
| 1644 | if (!ToArgumentPack) |
| 1645 | return ToArgumentPack.takeError(); |
| 1646 | |
| 1647 | return Importer.getToContext().getSubstTemplateTypeParmPackType( |
| 1648 | AssociatedDecl: *ReplacedOrErr, Index: T->getIndex(), Final: T->getFinal(), ArgPack: *ToArgumentPack); |
| 1649 | } |
| 1650 | |
| 1651 | ExpectedType ASTNodeImporter::VisitTemplateSpecializationType( |
| 1652 | const TemplateSpecializationType *T) { |
| 1653 | auto ToTemplateOrErr = import(From: T->getTemplateName()); |
| 1654 | if (!ToTemplateOrErr) |
| 1655 | return ToTemplateOrErr.takeError(); |
| 1656 | |
| 1657 | SmallVector<TemplateArgument, 2> ToTemplateArgs; |
| 1658 | if (Error Err = |
| 1659 | ImportTemplateArguments(FromArgs: T->template_arguments(), ToArgs&: ToTemplateArgs)) |
| 1660 | return std::move(Err); |
| 1661 | |
| 1662 | ExpectedType ToUnderlyingOrErr = |
| 1663 | T->isCanonicalUnqualified() ? QualType() : import(From: T->desugar()); |
| 1664 | if (!ToUnderlyingOrErr) |
| 1665 | return ToUnderlyingOrErr.takeError(); |
| 1666 | return Importer.getToContext().getTemplateSpecializationType( |
| 1667 | T: *ToTemplateOrErr, SpecifiedArgs: ToTemplateArgs, CanonicalArgs: std::nullopt, Underlying: *ToUnderlyingOrErr); |
| 1668 | } |
| 1669 | |
| 1670 | ExpectedType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) { |
| 1671 | // Note: the qualifier in an ElaboratedType is optional. |
| 1672 | auto ToQualifierOrErr = import(From: T->getQualifier()); |
| 1673 | if (!ToQualifierOrErr) |
| 1674 | return ToQualifierOrErr.takeError(); |
| 1675 | |
| 1676 | ExpectedType ToNamedTypeOrErr = import(From: T->getNamedType()); |
| 1677 | if (!ToNamedTypeOrErr) |
| 1678 | return ToNamedTypeOrErr.takeError(); |
| 1679 | |
| 1680 | Expected<TagDecl *> ToOwnedTagDeclOrErr = import(From: T->getOwnedTagDecl()); |
| 1681 | if (!ToOwnedTagDeclOrErr) |
| 1682 | return ToOwnedTagDeclOrErr.takeError(); |
| 1683 | |
| 1684 | return Importer.getToContext().getElaboratedType(Keyword: T->getKeyword(), |
| 1685 | NNS: *ToQualifierOrErr, |
| 1686 | NamedType: *ToNamedTypeOrErr, |
| 1687 | OwnedTagDecl: *ToOwnedTagDeclOrErr); |
| 1688 | } |
| 1689 | |
| 1690 | ExpectedType |
| 1691 | ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) { |
| 1692 | ExpectedType ToPatternOrErr = import(From: T->getPattern()); |
| 1693 | if (!ToPatternOrErr) |
| 1694 | return ToPatternOrErr.takeError(); |
| 1695 | |
| 1696 | return Importer.getToContext().getPackExpansionType(Pattern: *ToPatternOrErr, |
| 1697 | NumExpansions: T->getNumExpansions(), |
| 1698 | /*ExpactPack=*/ExpectPackInType: false); |
| 1699 | } |
| 1700 | |
| 1701 | ExpectedType ASTNodeImporter::VisitDependentTemplateSpecializationType( |
| 1702 | const DependentTemplateSpecializationType *T) { |
| 1703 | const DependentTemplateStorage &DTN = T->getDependentTemplateName(); |
| 1704 | auto QualifierOrErr = import(From: DTN.getQualifier()); |
| 1705 | if (!QualifierOrErr) |
| 1706 | return QualifierOrErr.takeError(); |
| 1707 | |
| 1708 | SmallVector<TemplateArgument, 2> ToPack; |
| 1709 | ToPack.reserve(N: T->template_arguments().size()); |
| 1710 | if (Error Err = ImportTemplateArguments(FromArgs: T->template_arguments(), ToArgs&: ToPack)) |
| 1711 | return std::move(Err); |
| 1712 | |
| 1713 | return Importer.getToContext().getDependentTemplateSpecializationType( |
| 1714 | T->getKeyword(), |
| 1715 | {*QualifierOrErr, Importer.Import(FromIO: DTN.getName()), |
| 1716 | DTN.hasTemplateKeyword()}, |
| 1717 | ToPack); |
| 1718 | } |
| 1719 | |
| 1720 | ExpectedType |
| 1721 | ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) { |
| 1722 | auto ToQualifierOrErr = import(From: T->getQualifier()); |
| 1723 | if (!ToQualifierOrErr) |
| 1724 | return ToQualifierOrErr.takeError(); |
| 1725 | |
| 1726 | IdentifierInfo *Name = Importer.Import(FromId: T->getIdentifier()); |
| 1727 | return Importer.getToContext().getDependentNameType(Keyword: T->getKeyword(), |
| 1728 | NNS: *ToQualifierOrErr, Name); |
| 1729 | } |
| 1730 | |
| 1731 | ExpectedType |
| 1732 | ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { |
| 1733 | Expected<ObjCInterfaceDecl *> ToDeclOrErr = import(From: T->getDecl()); |
| 1734 | if (!ToDeclOrErr) |
| 1735 | return ToDeclOrErr.takeError(); |
| 1736 | |
| 1737 | return Importer.getToContext().getObjCInterfaceType(Decl: *ToDeclOrErr); |
| 1738 | } |
| 1739 | |
| 1740 | ExpectedType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) { |
| 1741 | ExpectedType ToBaseTypeOrErr = import(From: T->getBaseType()); |
| 1742 | if (!ToBaseTypeOrErr) |
| 1743 | return ToBaseTypeOrErr.takeError(); |
| 1744 | |
| 1745 | SmallVector<QualType, 4> TypeArgs; |
| 1746 | for (auto TypeArg : T->getTypeArgsAsWritten()) { |
| 1747 | if (ExpectedType TyOrErr = import(From: TypeArg)) |
| 1748 | TypeArgs.push_back(Elt: *TyOrErr); |
| 1749 | else |
| 1750 | return TyOrErr.takeError(); |
| 1751 | } |
| 1752 | |
| 1753 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 1754 | for (auto *P : T->quals()) { |
| 1755 | if (Expected<ObjCProtocolDecl *> ProtocolOrErr = import(P)) |
| 1756 | Protocols.push_back(*ProtocolOrErr); |
| 1757 | else |
| 1758 | return ProtocolOrErr.takeError(); |
| 1759 | |
| 1760 | } |
| 1761 | |
| 1762 | return Importer.getToContext().getObjCObjectType(Base: *ToBaseTypeOrErr, typeArgs: TypeArgs, |
| 1763 | protocols: Protocols, |
| 1764 | isKindOf: T->isKindOfTypeAsWritten()); |
| 1765 | } |
| 1766 | |
| 1767 | ExpectedType |
| 1768 | ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { |
| 1769 | ExpectedType ToPointeeTypeOrErr = import(From: T->getPointeeType()); |
| 1770 | if (!ToPointeeTypeOrErr) |
| 1771 | return ToPointeeTypeOrErr.takeError(); |
| 1772 | |
| 1773 | return Importer.getToContext().getObjCObjectPointerType(OIT: *ToPointeeTypeOrErr); |
| 1774 | } |
| 1775 | |
| 1776 | ExpectedType |
| 1777 | ASTNodeImporter::VisitMacroQualifiedType(const MacroQualifiedType *T) { |
| 1778 | ExpectedType ToUnderlyingTypeOrErr = import(From: T->getUnderlyingType()); |
| 1779 | if (!ToUnderlyingTypeOrErr) |
| 1780 | return ToUnderlyingTypeOrErr.takeError(); |
| 1781 | |
| 1782 | IdentifierInfo *ToIdentifier = Importer.Import(FromId: T->getMacroIdentifier()); |
| 1783 | return Importer.getToContext().getMacroQualifiedType(UnderlyingTy: *ToUnderlyingTypeOrErr, |
| 1784 | MacroII: ToIdentifier); |
| 1785 | } |
| 1786 | |
| 1787 | ExpectedType clang::ASTNodeImporter::VisitAdjustedType(const AdjustedType *T) { |
| 1788 | Error Err = Error::success(); |
| 1789 | QualType ToOriginalType = importChecked(Err, From: T->getOriginalType()); |
| 1790 | QualType ToAdjustedType = importChecked(Err, From: T->getAdjustedType()); |
| 1791 | if (Err) |
| 1792 | return std::move(Err); |
| 1793 | |
| 1794 | return Importer.getToContext().getAdjustedType(Orig: ToOriginalType, |
| 1795 | New: ToAdjustedType); |
| 1796 | } |
| 1797 | |
| 1798 | ExpectedType clang::ASTNodeImporter::VisitBitIntType(const BitIntType *T) { |
| 1799 | return Importer.getToContext().getBitIntType(Unsigned: T->isUnsigned(), |
| 1800 | NumBits: T->getNumBits()); |
| 1801 | } |
| 1802 | |
| 1803 | ExpectedType clang::ASTNodeImporter::VisitBTFTagAttributedType( |
| 1804 | const clang::BTFTagAttributedType *T) { |
| 1805 | Error Err = Error::success(); |
| 1806 | const BTFTypeTagAttr *ToBTFAttr = importChecked(Err, From: T->getAttr()); |
| 1807 | QualType ToWrappedType = importChecked(Err, From: T->getWrappedType()); |
| 1808 | if (Err) |
| 1809 | return std::move(Err); |
| 1810 | |
| 1811 | return Importer.getToContext().getBTFTagAttributedType(BTFAttr: ToBTFAttr, |
| 1812 | Wrapped: ToWrappedType); |
| 1813 | } |
| 1814 | |
| 1815 | ExpectedType clang::ASTNodeImporter::VisitHLSLAttributedResourceType( |
| 1816 | const clang::HLSLAttributedResourceType *T) { |
| 1817 | Error Err = Error::success(); |
| 1818 | const HLSLAttributedResourceType::Attributes &ToAttrs = T->getAttrs(); |
| 1819 | QualType ToWrappedType = importChecked(Err, From: T->getWrappedType()); |
| 1820 | QualType ToContainedType = importChecked(Err, From: T->getContainedType()); |
| 1821 | if (Err) |
| 1822 | return std::move(Err); |
| 1823 | |
| 1824 | return Importer.getToContext().getHLSLAttributedResourceType( |
| 1825 | Wrapped: ToWrappedType, Contained: ToContainedType, Attrs: ToAttrs); |
| 1826 | } |
| 1827 | |
| 1828 | ExpectedType clang::ASTNodeImporter::VisitHLSLInlineSpirvType( |
| 1829 | const clang::HLSLInlineSpirvType *T) { |
| 1830 | Error Err = Error::success(); |
| 1831 | |
| 1832 | uint32_t ToOpcode = T->getOpcode(); |
| 1833 | uint32_t ToSize = T->getSize(); |
| 1834 | uint32_t ToAlignment = T->getAlignment(); |
| 1835 | |
| 1836 | llvm::SmallVector<SpirvOperand> ToOperands; |
| 1837 | |
| 1838 | for (auto &Operand : T->getOperands()) { |
| 1839 | using SpirvOperandKind = SpirvOperand::SpirvOperandKind; |
| 1840 | |
| 1841 | switch (Operand.getKind()) { |
| 1842 | case SpirvOperandKind::ConstantId: |
| 1843 | ToOperands.push_back(Elt: SpirvOperand::createConstant( |
| 1844 | ResultType: importChecked(Err, From: Operand.getResultType()), Val: Operand.getValue())); |
| 1845 | break; |
| 1846 | case SpirvOperandKind::Literal: |
| 1847 | ToOperands.push_back(Elt: SpirvOperand::createLiteral(Val: Operand.getValue())); |
| 1848 | break; |
| 1849 | case SpirvOperandKind::TypeId: |
| 1850 | ToOperands.push_back(Elt: SpirvOperand::createType( |
| 1851 | T: importChecked(Err, From: Operand.getResultType()))); |
| 1852 | break; |
| 1853 | default: |
| 1854 | llvm_unreachable("Invalid SpirvOperand kind" ); |
| 1855 | } |
| 1856 | |
| 1857 | if (Err) |
| 1858 | return std::move(Err); |
| 1859 | } |
| 1860 | |
| 1861 | return Importer.getToContext().getHLSLInlineSpirvType( |
| 1862 | Opcode: ToOpcode, Size: ToSize, Alignment: ToAlignment, Operands: ToOperands); |
| 1863 | } |
| 1864 | |
| 1865 | ExpectedType clang::ASTNodeImporter::VisitConstantMatrixType( |
| 1866 | const clang::ConstantMatrixType *T) { |
| 1867 | ExpectedType ToElementTypeOrErr = import(T->getElementType()); |
| 1868 | if (!ToElementTypeOrErr) |
| 1869 | return ToElementTypeOrErr.takeError(); |
| 1870 | |
| 1871 | return Importer.getToContext().getConstantMatrixType( |
| 1872 | ElementType: *ToElementTypeOrErr, NumRows: T->getNumRows(), NumColumns: T->getNumColumns()); |
| 1873 | } |
| 1874 | |
| 1875 | ExpectedType clang::ASTNodeImporter::VisitDependentAddressSpaceType( |
| 1876 | const clang::DependentAddressSpaceType *T) { |
| 1877 | Error Err = Error::success(); |
| 1878 | QualType ToPointeeType = importChecked(Err, From: T->getPointeeType()); |
| 1879 | Expr *ToAddrSpaceExpr = importChecked(Err, From: T->getAddrSpaceExpr()); |
| 1880 | SourceLocation ToAttrLoc = importChecked(Err, From: T->getAttributeLoc()); |
| 1881 | if (Err) |
| 1882 | return std::move(Err); |
| 1883 | |
| 1884 | return Importer.getToContext().getDependentAddressSpaceType( |
| 1885 | PointeeType: ToPointeeType, AddrSpaceExpr: ToAddrSpaceExpr, AttrLoc: ToAttrLoc); |
| 1886 | } |
| 1887 | |
| 1888 | ExpectedType clang::ASTNodeImporter::VisitDependentBitIntType( |
| 1889 | const clang::DependentBitIntType *T) { |
| 1890 | ExpectedExpr ToNumBitsExprOrErr = import(From: T->getNumBitsExpr()); |
| 1891 | if (!ToNumBitsExprOrErr) |
| 1892 | return ToNumBitsExprOrErr.takeError(); |
| 1893 | return Importer.getToContext().getDependentBitIntType(Unsigned: T->isUnsigned(), |
| 1894 | BitsExpr: *ToNumBitsExprOrErr); |
| 1895 | } |
| 1896 | |
| 1897 | ExpectedType clang::ASTNodeImporter::VisitDependentSizedMatrixType( |
| 1898 | const clang::DependentSizedMatrixType *T) { |
| 1899 | Error Err = Error::success(); |
| 1900 | QualType ToElementType = importChecked(Err, T->getElementType()); |
| 1901 | Expr *ToRowExpr = importChecked(Err, From: T->getRowExpr()); |
| 1902 | Expr *ToColumnExpr = importChecked(Err, From: T->getColumnExpr()); |
| 1903 | SourceLocation ToAttrLoc = importChecked(Err, From: T->getAttributeLoc()); |
| 1904 | if (Err) |
| 1905 | return std::move(Err); |
| 1906 | |
| 1907 | return Importer.getToContext().getDependentSizedMatrixType( |
| 1908 | ElementType: ToElementType, RowExpr: ToRowExpr, ColumnExpr: ToColumnExpr, AttrLoc: ToAttrLoc); |
| 1909 | } |
| 1910 | |
| 1911 | ExpectedType clang::ASTNodeImporter::VisitDependentVectorType( |
| 1912 | const clang::DependentVectorType *T) { |
| 1913 | Error Err = Error::success(); |
| 1914 | QualType ToElementType = importChecked(Err, From: T->getElementType()); |
| 1915 | Expr *ToSizeExpr = importChecked(Err, From: T->getSizeExpr()); |
| 1916 | SourceLocation ToAttrLoc = importChecked(Err, From: T->getAttributeLoc()); |
| 1917 | if (Err) |
| 1918 | return std::move(Err); |
| 1919 | |
| 1920 | return Importer.getToContext().getDependentVectorType( |
| 1921 | VectorType: ToElementType, SizeExpr: ToSizeExpr, AttrLoc: ToAttrLoc, VecKind: T->getVectorKind()); |
| 1922 | } |
| 1923 | |
| 1924 | ExpectedType clang::ASTNodeImporter::VisitObjCTypeParamType( |
| 1925 | const clang::ObjCTypeParamType *T) { |
| 1926 | Expected<ObjCTypeParamDecl *> ToDeclOrErr = import(From: T->getDecl()); |
| 1927 | if (!ToDeclOrErr) |
| 1928 | return ToDeclOrErr.takeError(); |
| 1929 | |
| 1930 | SmallVector<ObjCProtocolDecl *, 4> ToProtocols; |
| 1931 | for (ObjCProtocolDecl *FromProtocol : T->getProtocols()) { |
| 1932 | Expected<ObjCProtocolDecl *> ToProtocolOrErr = import(FromProtocol); |
| 1933 | if (!ToProtocolOrErr) |
| 1934 | return ToProtocolOrErr.takeError(); |
| 1935 | ToProtocols.push_back(*ToProtocolOrErr); |
| 1936 | } |
| 1937 | |
| 1938 | return Importer.getToContext().getObjCTypeParamType(Decl: *ToDeclOrErr, |
| 1939 | protocols: ToProtocols); |
| 1940 | } |
| 1941 | |
| 1942 | ExpectedType clang::ASTNodeImporter::VisitPipeType(const clang::PipeType *T) { |
| 1943 | ExpectedType ToElementTypeOrErr = import(From: T->getElementType()); |
| 1944 | if (!ToElementTypeOrErr) |
| 1945 | return ToElementTypeOrErr.takeError(); |
| 1946 | |
| 1947 | ASTContext &ToCtx = Importer.getToContext(); |
| 1948 | if (T->isReadOnly()) |
| 1949 | return ToCtx.getReadPipeType(T: *ToElementTypeOrErr); |
| 1950 | else |
| 1951 | return ToCtx.getWritePipeType(T: *ToElementTypeOrErr); |
| 1952 | } |
| 1953 | |
| 1954 | //---------------------------------------------------------------------------- |
| 1955 | // Import Declarations |
| 1956 | //---------------------------------------------------------------------------- |
| 1957 | Error ASTNodeImporter::ImportDeclParts( |
| 1958 | NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC, |
| 1959 | DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc) { |
| 1960 | // Check if RecordDecl is in FunctionDecl parameters to avoid infinite loop. |
| 1961 | // example: int struct_in_proto(struct data_t{int a;int b;} *d); |
| 1962 | // FIXME: We could support these constructs by importing a different type of |
| 1963 | // this parameter and by importing the original type of the parameter only |
| 1964 | // after the FunctionDecl is created. See |
| 1965 | // VisitFunctionDecl::UsedDifferentProtoType. |
| 1966 | DeclContext *OrigDC = D->getDeclContext(); |
| 1967 | FunctionDecl *FunDecl; |
| 1968 | if (isa<RecordDecl>(Val: D) && (FunDecl = dyn_cast<FunctionDecl>(Val: OrigDC)) && |
| 1969 | FunDecl->hasBody()) { |
| 1970 | auto getLeafPointeeType = [](const Type *T) { |
| 1971 | while (T->isPointerType() || T->isArrayType()) { |
| 1972 | T = T->getPointeeOrArrayElementType(); |
| 1973 | } |
| 1974 | return T; |
| 1975 | }; |
| 1976 | for (const ParmVarDecl *P : FunDecl->parameters()) { |
| 1977 | const Type *LeafT = |
| 1978 | getLeafPointeeType(P->getType().getCanonicalType().getTypePtr()); |
| 1979 | auto *RT = dyn_cast<RecordType>(Val: LeafT); |
| 1980 | if (RT && RT->getDecl() == D) { |
| 1981 | Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) |
| 1982 | << D->getDeclKindName(); |
| 1983 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
| 1984 | } |
| 1985 | } |
| 1986 | } |
| 1987 | |
| 1988 | // Import the context of this declaration. |
| 1989 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) |
| 1990 | return Err; |
| 1991 | |
| 1992 | // Import the name of this declaration. |
| 1993 | if (Error Err = importInto(To&: Name, From: D->getDeclName())) |
| 1994 | return Err; |
| 1995 | |
| 1996 | // Import the location of this declaration. |
| 1997 | if (Error Err = importInto(Loc, D->getLocation())) |
| 1998 | return Err; |
| 1999 | |
| 2000 | ToD = cast_or_null<NamedDecl>(Val: Importer.GetAlreadyImportedOrNull(D)); |
| 2001 | if (ToD) |
| 2002 | if (Error Err = ASTNodeImporter(*this).ImportDefinitionIfNeeded(D, ToD)) |
| 2003 | return Err; |
| 2004 | |
| 2005 | return Error::success(); |
| 2006 | } |
| 2007 | |
| 2008 | Error ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclarationName &Name, |
| 2009 | NamedDecl *&ToD, SourceLocation &Loc) { |
| 2010 | |
| 2011 | // Import the name of this declaration. |
| 2012 | if (Error Err = importInto(To&: Name, From: D->getDeclName())) |
| 2013 | return Err; |
| 2014 | |
| 2015 | // Import the location of this declaration. |
| 2016 | if (Error Err = importInto(Loc, D->getLocation())) |
| 2017 | return Err; |
| 2018 | |
| 2019 | ToD = cast_or_null<NamedDecl>(Val: Importer.GetAlreadyImportedOrNull(D)); |
| 2020 | if (ToD) |
| 2021 | if (Error Err = ASTNodeImporter(*this).ImportDefinitionIfNeeded(D, ToD)) |
| 2022 | return Err; |
| 2023 | |
| 2024 | return Error::success(); |
| 2025 | } |
| 2026 | |
| 2027 | Error ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) { |
| 2028 | if (!FromD) |
| 2029 | return Error::success(); |
| 2030 | |
| 2031 | if (!ToD) |
| 2032 | if (Error Err = importInto(To&: ToD, From: FromD)) |
| 2033 | return Err; |
| 2034 | |
| 2035 | if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(Val: FromD)) { |
| 2036 | if (RecordDecl *ToRecord = cast<RecordDecl>(Val: ToD)) { |
| 2037 | if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && |
| 2038 | !ToRecord->getDefinition()) { |
| 2039 | if (Error Err = ImportDefinition(From: FromRecord, To: ToRecord)) |
| 2040 | return Err; |
| 2041 | } |
| 2042 | } |
| 2043 | return Error::success(); |
| 2044 | } |
| 2045 | |
| 2046 | if (EnumDecl * = dyn_cast<EnumDecl>(Val: FromD)) { |
| 2047 | if (EnumDecl *ToEnum = cast<EnumDecl>(Val: ToD)) { |
| 2048 | if (FromEnum->getDefinition() && !ToEnum->getDefinition()) { |
| 2049 | if (Error Err = ImportDefinition(From: FromEnum, To: ToEnum)) |
| 2050 | return Err; |
| 2051 | } |
| 2052 | } |
| 2053 | return Error::success(); |
| 2054 | } |
| 2055 | |
| 2056 | return Error::success(); |
| 2057 | } |
| 2058 | |
| 2059 | Error |
| 2060 | ASTNodeImporter::ImportDeclarationNameLoc( |
| 2061 | const DeclarationNameInfo &From, DeclarationNameInfo& To) { |
| 2062 | // NOTE: To.Name and To.Loc are already imported. |
| 2063 | // We only have to import To.LocInfo. |
| 2064 | switch (To.getName().getNameKind()) { |
| 2065 | case DeclarationName::Identifier: |
| 2066 | case DeclarationName::ObjCZeroArgSelector: |
| 2067 | case DeclarationName::ObjCOneArgSelector: |
| 2068 | case DeclarationName::ObjCMultiArgSelector: |
| 2069 | case DeclarationName::CXXUsingDirective: |
| 2070 | case DeclarationName::CXXDeductionGuideName: |
| 2071 | return Error::success(); |
| 2072 | |
| 2073 | case DeclarationName::CXXOperatorName: { |
| 2074 | if (auto ToRangeOrErr = import(From: From.getCXXOperatorNameRange())) |
| 2075 | To.setCXXOperatorNameRange(*ToRangeOrErr); |
| 2076 | else |
| 2077 | return ToRangeOrErr.takeError(); |
| 2078 | return Error::success(); |
| 2079 | } |
| 2080 | case DeclarationName::CXXLiteralOperatorName: { |
| 2081 | if (ExpectedSLoc LocOrErr = import(From: From.getCXXLiteralOperatorNameLoc())) |
| 2082 | To.setCXXLiteralOperatorNameLoc(*LocOrErr); |
| 2083 | else |
| 2084 | return LocOrErr.takeError(); |
| 2085 | return Error::success(); |
| 2086 | } |
| 2087 | case DeclarationName::CXXConstructorName: |
| 2088 | case DeclarationName::CXXDestructorName: |
| 2089 | case DeclarationName::CXXConversionFunctionName: { |
| 2090 | if (auto ToTInfoOrErr = import(From: From.getNamedTypeInfo())) |
| 2091 | To.setNamedTypeInfo(*ToTInfoOrErr); |
| 2092 | else |
| 2093 | return ToTInfoOrErr.takeError(); |
| 2094 | return Error::success(); |
| 2095 | } |
| 2096 | } |
| 2097 | llvm_unreachable("Unknown name kind." ); |
| 2098 | } |
| 2099 | |
| 2100 | Error |
| 2101 | ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) { |
| 2102 | if (Importer.isMinimalImport() && !ForceImport) { |
| 2103 | auto ToDCOrErr = Importer.ImportContext(FromDC); |
| 2104 | return ToDCOrErr.takeError(); |
| 2105 | } |
| 2106 | |
| 2107 | // We use strict error handling in case of records and enums, but not |
| 2108 | // with e.g. namespaces. |
| 2109 | // |
| 2110 | // FIXME Clients of the ASTImporter should be able to choose an |
| 2111 | // appropriate error handling strategy for their needs. For instance, |
| 2112 | // they may not want to mark an entire namespace as erroneous merely |
| 2113 | // because there is an ODR error with two typedefs. As another example, |
| 2114 | // the client may allow EnumConstantDecls with same names but with |
| 2115 | // different values in two distinct translation units. |
| 2116 | ChildErrorHandlingStrategy HandleChildErrors(FromDC); |
| 2117 | |
| 2118 | auto MightNeedReordering = [](const Decl *D) { |
| 2119 | return isa<FieldDecl>(Val: D) || isa<IndirectFieldDecl>(Val: D) || isa<FriendDecl>(Val: D); |
| 2120 | }; |
| 2121 | |
| 2122 | // Import everything that might need reordering first. |
| 2123 | Error ChildErrors = Error::success(); |
| 2124 | for (auto *From : FromDC->decls()) { |
| 2125 | if (!MightNeedReordering(From)) |
| 2126 | continue; |
| 2127 | |
| 2128 | ExpectedDecl ImportedOrErr = import(From); |
| 2129 | |
| 2130 | // If we are in the process of ImportDefinition(...) for a RecordDecl we |
| 2131 | // want to make sure that we are also completing each FieldDecl. There |
| 2132 | // are currently cases where this does not happen and this is correctness |
| 2133 | // fix since operations such as code generation will expect this to be so. |
| 2134 | if (!ImportedOrErr) { |
| 2135 | HandleChildErrors.handleChildImportResult(ResultErr&: ChildErrors, |
| 2136 | ChildErr: ImportedOrErr.takeError()); |
| 2137 | continue; |
| 2138 | } |
| 2139 | FieldDecl *FieldFrom = dyn_cast_or_null<FieldDecl>(Val: From); |
| 2140 | Decl *ImportedDecl = *ImportedOrErr; |
| 2141 | FieldDecl *FieldTo = dyn_cast_or_null<FieldDecl>(Val: ImportedDecl); |
| 2142 | if (FieldFrom && FieldTo) { |
| 2143 | Error Err = ImportFieldDeclDefinition(From: FieldFrom, To: FieldTo); |
| 2144 | HandleChildErrors.handleChildImportResult(ResultErr&: ChildErrors, ChildErr: std::move(Err)); |
| 2145 | } |
| 2146 | } |
| 2147 | |
| 2148 | // We reorder declarations in RecordDecls because they may have another order |
| 2149 | // in the "to" context than they have in the "from" context. This may happen |
| 2150 | // e.g when we import a class like this: |
| 2151 | // struct declToImport { |
| 2152 | // int a = c + b; |
| 2153 | // int b = 1; |
| 2154 | // int c = 2; |
| 2155 | // }; |
| 2156 | // During the import of `a` we import first the dependencies in sequence, |
| 2157 | // thus the order would be `c`, `b`, `a`. We will get the normal order by |
| 2158 | // first removing the already imported members and then adding them in the |
| 2159 | // order as they appear in the "from" context. |
| 2160 | // |
| 2161 | // Keeping field order is vital because it determines structure layout. |
| 2162 | // |
| 2163 | // Here and below, we cannot call field_begin() method and its callers on |
| 2164 | // ToDC if it has an external storage. Calling field_begin() will |
| 2165 | // automatically load all the fields by calling |
| 2166 | // LoadFieldsFromExternalStorage(). LoadFieldsFromExternalStorage() would |
| 2167 | // call ASTImporter::Import(). This is because the ExternalASTSource |
| 2168 | // interface in LLDB is implemented by the means of the ASTImporter. However, |
| 2169 | // calling an import at this point would result in an uncontrolled import, we |
| 2170 | // must avoid that. |
| 2171 | |
| 2172 | auto ToDCOrErr = Importer.ImportContext(FromDC); |
| 2173 | if (!ToDCOrErr) { |
| 2174 | consumeError(Err: std::move(ChildErrors)); |
| 2175 | return ToDCOrErr.takeError(); |
| 2176 | } |
| 2177 | |
| 2178 | if (const auto *FromRD = dyn_cast<RecordDecl>(Val: FromDC)) { |
| 2179 | DeclContext *ToDC = *ToDCOrErr; |
| 2180 | // Remove all declarations, which may be in wrong order in the |
| 2181 | // lexical DeclContext and then add them in the proper order. |
| 2182 | for (auto *D : FromRD->decls()) { |
| 2183 | if (!MightNeedReordering(D)) |
| 2184 | continue; |
| 2185 | |
| 2186 | assert(D && "DC contains a null decl" ); |
| 2187 | if (Decl *ToD = Importer.GetAlreadyImportedOrNull(D)) { |
| 2188 | // Remove only the decls which we successfully imported. |
| 2189 | assert(ToDC == ToD->getLexicalDeclContext() && ToDC->containsDecl(ToD)); |
| 2190 | // Remove the decl from its wrong place in the linked list. |
| 2191 | ToDC->removeDecl(ToD); |
| 2192 | // Add the decl to the end of the linked list. |
| 2193 | // This time it will be at the proper place because the enclosing for |
| 2194 | // loop iterates in the original (good) order of the decls. |
| 2195 | ToDC->addDeclInternal(ToD); |
| 2196 | } |
| 2197 | } |
| 2198 | } |
| 2199 | |
| 2200 | // Import everything else. |
| 2201 | for (auto *From : FromDC->decls()) { |
| 2202 | if (MightNeedReordering(From)) |
| 2203 | continue; |
| 2204 | |
| 2205 | ExpectedDecl ImportedOrErr = import(From); |
| 2206 | if (!ImportedOrErr) |
| 2207 | HandleChildErrors.handleChildImportResult(ResultErr&: ChildErrors, |
| 2208 | ChildErr: ImportedOrErr.takeError()); |
| 2209 | } |
| 2210 | |
| 2211 | return ChildErrors; |
| 2212 | } |
| 2213 | |
| 2214 | Error ASTNodeImporter::ImportFieldDeclDefinition(const FieldDecl *From, |
| 2215 | const FieldDecl *To) { |
| 2216 | RecordDecl *FromRecordDecl = nullptr; |
| 2217 | RecordDecl *ToRecordDecl = nullptr; |
| 2218 | // If we have a field that is an ArrayType we need to check if the array |
| 2219 | // element is a RecordDecl and if so we need to import the definition. |
| 2220 | QualType FromType = From->getType(); |
| 2221 | QualType ToType = To->getType(); |
| 2222 | if (FromType->isArrayType()) { |
| 2223 | // getBaseElementTypeUnsafe(...) handles multi-dimensional arrays for us. |
| 2224 | FromRecordDecl = FromType->getBaseElementTypeUnsafe()->getAsRecordDecl(); |
| 2225 | ToRecordDecl = ToType->getBaseElementTypeUnsafe()->getAsRecordDecl(); |
| 2226 | } |
| 2227 | |
| 2228 | if (!FromRecordDecl || !ToRecordDecl) { |
| 2229 | const RecordType *RecordFrom = FromType->getAs<RecordType>(); |
| 2230 | const RecordType *RecordTo = ToType->getAs<RecordType>(); |
| 2231 | |
| 2232 | if (RecordFrom && RecordTo) { |
| 2233 | FromRecordDecl = RecordFrom->getDecl(); |
| 2234 | ToRecordDecl = RecordTo->getDecl(); |
| 2235 | } |
| 2236 | } |
| 2237 | |
| 2238 | if (FromRecordDecl && ToRecordDecl) { |
| 2239 | if (FromRecordDecl->isCompleteDefinition() && |
| 2240 | !ToRecordDecl->isCompleteDefinition()) |
| 2241 | return ImportDefinition(From: FromRecordDecl, To: ToRecordDecl); |
| 2242 | } |
| 2243 | |
| 2244 | return Error::success(); |
| 2245 | } |
| 2246 | |
| 2247 | Error ASTNodeImporter::ImportDeclContext( |
| 2248 | Decl *FromD, DeclContext *&ToDC, DeclContext *&ToLexicalDC) { |
| 2249 | auto ToDCOrErr = Importer.ImportContext(FromDC: FromD->getDeclContext()); |
| 2250 | if (!ToDCOrErr) |
| 2251 | return ToDCOrErr.takeError(); |
| 2252 | ToDC = *ToDCOrErr; |
| 2253 | |
| 2254 | if (FromD->getDeclContext() != FromD->getLexicalDeclContext()) { |
| 2255 | auto ToLexicalDCOrErr = Importer.ImportContext( |
| 2256 | FromDC: FromD->getLexicalDeclContext()); |
| 2257 | if (!ToLexicalDCOrErr) |
| 2258 | return ToLexicalDCOrErr.takeError(); |
| 2259 | ToLexicalDC = *ToLexicalDCOrErr; |
| 2260 | } else |
| 2261 | ToLexicalDC = ToDC; |
| 2262 | |
| 2263 | return Error::success(); |
| 2264 | } |
| 2265 | |
| 2266 | Error ASTNodeImporter::ImportImplicitMethods( |
| 2267 | const CXXRecordDecl *From, CXXRecordDecl *To) { |
| 2268 | assert(From->isCompleteDefinition() && To->getDefinition() == To && |
| 2269 | "Import implicit methods to or from non-definition" ); |
| 2270 | |
| 2271 | for (CXXMethodDecl *FromM : From->methods()) |
| 2272 | if (FromM->isImplicit()) { |
| 2273 | Expected<CXXMethodDecl *> ToMOrErr = import(From: FromM); |
| 2274 | if (!ToMOrErr) |
| 2275 | return ToMOrErr.takeError(); |
| 2276 | } |
| 2277 | |
| 2278 | return Error::success(); |
| 2279 | } |
| 2280 | |
| 2281 | static Error setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To, |
| 2282 | ASTImporter &Importer) { |
| 2283 | if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) { |
| 2284 | if (ExpectedDecl ToTypedefOrErr = Importer.Import(FromTypedef)) |
| 2285 | To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(Val: *ToTypedefOrErr)); |
| 2286 | else |
| 2287 | return ToTypedefOrErr.takeError(); |
| 2288 | } |
| 2289 | return Error::success(); |
| 2290 | } |
| 2291 | |
| 2292 | Error ASTNodeImporter::ImportDefinition( |
| 2293 | RecordDecl *From, RecordDecl *To, ImportDefinitionKind Kind) { |
| 2294 | auto DefinitionCompleter = [To]() { |
| 2295 | // There are cases in LLDB when we first import a class without its |
| 2296 | // members. The class will have DefinitionData, but no members. Then, |
| 2297 | // importDefinition is called from LLDB, which tries to get the members, so |
| 2298 | // when we get here, the class already has the DefinitionData set, so we |
| 2299 | // must unset the CompleteDefinition here to be able to complete again the |
| 2300 | // definition. |
| 2301 | To->setCompleteDefinition(false); |
| 2302 | To->completeDefinition(); |
| 2303 | }; |
| 2304 | |
| 2305 | if (To->getDefinition() || To->isBeingDefined()) { |
| 2306 | if (Kind == IDK_Everything || |
| 2307 | // In case of lambdas, the class already has a definition ptr set, but |
| 2308 | // the contained decls are not imported yet. Also, isBeingDefined was |
| 2309 | // set in CXXRecordDecl::CreateLambda. We must import the contained |
| 2310 | // decls here and finish the definition. |
| 2311 | (To->isLambda() && shouldForceImportDeclContext(IDK: Kind))) { |
| 2312 | if (To->isLambda()) { |
| 2313 | auto *FromCXXRD = cast<CXXRecordDecl>(Val: From); |
| 2314 | SmallVector<LambdaCapture, 8> ToCaptures; |
| 2315 | ToCaptures.reserve(N: FromCXXRD->capture_size()); |
| 2316 | for (const auto &FromCapture : FromCXXRD->captures()) { |
| 2317 | if (auto ToCaptureOrErr = import(From: FromCapture)) |
| 2318 | ToCaptures.push_back(Elt: *ToCaptureOrErr); |
| 2319 | else |
| 2320 | return ToCaptureOrErr.takeError(); |
| 2321 | } |
| 2322 | cast<CXXRecordDecl>(Val: To)->setCaptures(Context&: Importer.getToContext(), |
| 2323 | Captures: ToCaptures); |
| 2324 | } |
| 2325 | |
| 2326 | Error Result = ImportDeclContext(From, /*ForceImport=*/true); |
| 2327 | // Finish the definition of the lambda, set isBeingDefined to false. |
| 2328 | if (To->isLambda()) |
| 2329 | DefinitionCompleter(); |
| 2330 | return Result; |
| 2331 | } |
| 2332 | |
| 2333 | return Error::success(); |
| 2334 | } |
| 2335 | |
| 2336 | To->startDefinition(); |
| 2337 | // Set the definition to complete even if it is really not complete during |
| 2338 | // import. Some AST constructs (expressions) require the record layout |
| 2339 | // to be calculated (see 'clang::computeDependence') at the time they are |
| 2340 | // constructed. Import of such AST node is possible during import of the |
| 2341 | // same record, there is no way to have a completely defined record (all |
| 2342 | // fields imported) at that time without multiple AST import passes. |
| 2343 | if (!Importer.isMinimalImport()) |
| 2344 | To->setCompleteDefinition(true); |
| 2345 | // Complete the definition even if error is returned. |
| 2346 | // The RecordDecl may be already part of the AST so it is better to |
| 2347 | // have it in complete state even if something is wrong with it. |
| 2348 | auto DefinitionCompleterScopeExit = |
| 2349 | llvm::make_scope_exit(F&: DefinitionCompleter); |
| 2350 | |
| 2351 | if (Error Err = setTypedefNameForAnonDecl(From, To, Importer)) |
| 2352 | return Err; |
| 2353 | |
| 2354 | // Add base classes. |
| 2355 | auto *ToCXX = dyn_cast<CXXRecordDecl>(Val: To); |
| 2356 | auto *FromCXX = dyn_cast<CXXRecordDecl>(Val: From); |
| 2357 | if (ToCXX && FromCXX && ToCXX->dataPtr() && FromCXX->dataPtr()) { |
| 2358 | |
| 2359 | struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data(); |
| 2360 | struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data(); |
| 2361 | |
| 2362 | #define FIELD(Name, Width, Merge) \ |
| 2363 | ToData.Name = FromData.Name; |
| 2364 | #include "clang/AST/CXXRecordDeclDefinitionBits.def" |
| 2365 | |
| 2366 | // Copy over the data stored in RecordDeclBits |
| 2367 | ToCXX->setArgPassingRestrictions(FromCXX->getArgPassingRestrictions()); |
| 2368 | |
| 2369 | SmallVector<CXXBaseSpecifier *, 4> Bases; |
| 2370 | for (const auto &Base1 : FromCXX->bases()) { |
| 2371 | ExpectedType TyOrErr = import(From: Base1.getType()); |
| 2372 | if (!TyOrErr) |
| 2373 | return TyOrErr.takeError(); |
| 2374 | |
| 2375 | SourceLocation EllipsisLoc; |
| 2376 | if (Base1.isPackExpansion()) { |
| 2377 | if (ExpectedSLoc LocOrErr = import(From: Base1.getEllipsisLoc())) |
| 2378 | EllipsisLoc = *LocOrErr; |
| 2379 | else |
| 2380 | return LocOrErr.takeError(); |
| 2381 | } |
| 2382 | |
| 2383 | // Ensure that we have a definition for the base. |
| 2384 | if (Error Err = |
| 2385 | ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl())) |
| 2386 | return Err; |
| 2387 | |
| 2388 | auto RangeOrErr = import(From: Base1.getSourceRange()); |
| 2389 | if (!RangeOrErr) |
| 2390 | return RangeOrErr.takeError(); |
| 2391 | |
| 2392 | auto TSIOrErr = import(From: Base1.getTypeSourceInfo()); |
| 2393 | if (!TSIOrErr) |
| 2394 | return TSIOrErr.takeError(); |
| 2395 | |
| 2396 | Bases.push_back( |
| 2397 | Elt: new (Importer.getToContext()) CXXBaseSpecifier( |
| 2398 | *RangeOrErr, |
| 2399 | Base1.isVirtual(), |
| 2400 | Base1.isBaseOfClass(), |
| 2401 | Base1.getAccessSpecifierAsWritten(), |
| 2402 | *TSIOrErr, |
| 2403 | EllipsisLoc)); |
| 2404 | } |
| 2405 | if (!Bases.empty()) |
| 2406 | ToCXX->setBases(Bases: Bases.data(), NumBases: Bases.size()); |
| 2407 | } |
| 2408 | |
| 2409 | if (shouldForceImportDeclContext(IDK: Kind)) { |
| 2410 | if (Error Err = ImportDeclContext(From, /*ForceImport=*/true)) |
| 2411 | return Err; |
| 2412 | } |
| 2413 | |
| 2414 | return Error::success(); |
| 2415 | } |
| 2416 | |
| 2417 | Error ASTNodeImporter::ImportInitializer(VarDecl *From, VarDecl *To) { |
| 2418 | if (To->getAnyInitializer()) |
| 2419 | return Error::success(); |
| 2420 | |
| 2421 | Expr *FromInit = From->getInit(); |
| 2422 | if (!FromInit) |
| 2423 | return Error::success(); |
| 2424 | |
| 2425 | ExpectedExpr ToInitOrErr = import(From: FromInit); |
| 2426 | if (!ToInitOrErr) |
| 2427 | return ToInitOrErr.takeError(); |
| 2428 | |
| 2429 | To->setInit(*ToInitOrErr); |
| 2430 | if (EvaluatedStmt *FromEval = From->getEvaluatedStmt()) { |
| 2431 | EvaluatedStmt *ToEval = To->ensureEvaluatedStmt(); |
| 2432 | ToEval->HasConstantInitialization = FromEval->HasConstantInitialization; |
| 2433 | ToEval->HasConstantDestruction = FromEval->HasConstantDestruction; |
| 2434 | // FIXME: Also import the initializer value. |
| 2435 | } |
| 2436 | |
| 2437 | // FIXME: Other bits to merge? |
| 2438 | return Error::success(); |
| 2439 | } |
| 2440 | |
| 2441 | Error ASTNodeImporter::ImportDefinition( |
| 2442 | EnumDecl *From, EnumDecl *To, ImportDefinitionKind Kind) { |
| 2443 | if (To->getDefinition() || To->isBeingDefined()) { |
| 2444 | if (Kind == IDK_Everything) |
| 2445 | return ImportDeclContext(From, /*ForceImport=*/true); |
| 2446 | return Error::success(); |
| 2447 | } |
| 2448 | |
| 2449 | To->startDefinition(); |
| 2450 | |
| 2451 | if (Error Err = setTypedefNameForAnonDecl(From, To, Importer)) |
| 2452 | return Err; |
| 2453 | |
| 2454 | ExpectedType ToTypeOrErr = |
| 2455 | import(From: Importer.getFromContext().getTypeDeclType(From)); |
| 2456 | if (!ToTypeOrErr) |
| 2457 | return ToTypeOrErr.takeError(); |
| 2458 | |
| 2459 | ExpectedType ToPromotionTypeOrErr = import(From: From->getPromotionType()); |
| 2460 | if (!ToPromotionTypeOrErr) |
| 2461 | return ToPromotionTypeOrErr.takeError(); |
| 2462 | |
| 2463 | if (shouldForceImportDeclContext(IDK: Kind)) |
| 2464 | if (Error Err = ImportDeclContext(From, /*ForceImport=*/true)) |
| 2465 | return Err; |
| 2466 | |
| 2467 | // FIXME: we might need to merge the number of positive or negative bits |
| 2468 | // if the enumerator lists don't match. |
| 2469 | To->completeDefinition(NewType: *ToTypeOrErr, PromotionType: *ToPromotionTypeOrErr, |
| 2470 | NumPositiveBits: From->getNumPositiveBits(), |
| 2471 | NumNegativeBits: From->getNumNegativeBits()); |
| 2472 | return Error::success(); |
| 2473 | } |
| 2474 | |
| 2475 | Error ASTNodeImporter::ImportTemplateArguments( |
| 2476 | ArrayRef<TemplateArgument> FromArgs, |
| 2477 | SmallVectorImpl<TemplateArgument> &ToArgs) { |
| 2478 | for (const auto &Arg : FromArgs) { |
| 2479 | if (auto ToOrErr = import(From: Arg)) |
| 2480 | ToArgs.push_back(Elt: *ToOrErr); |
| 2481 | else |
| 2482 | return ToOrErr.takeError(); |
| 2483 | } |
| 2484 | |
| 2485 | return Error::success(); |
| 2486 | } |
| 2487 | |
| 2488 | // FIXME: Do not forget to remove this and use only 'import'. |
| 2489 | Expected<TemplateArgument> |
| 2490 | ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) { |
| 2491 | return import(From); |
| 2492 | } |
| 2493 | |
| 2494 | template <typename InContainerTy> |
| 2495 | Error ASTNodeImporter::ImportTemplateArgumentListInfo( |
| 2496 | const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) { |
| 2497 | for (const auto &FromLoc : Container) { |
| 2498 | if (auto ToLocOrErr = import(FromLoc)) |
| 2499 | ToTAInfo.addArgument(Loc: *ToLocOrErr); |
| 2500 | else |
| 2501 | return ToLocOrErr.takeError(); |
| 2502 | } |
| 2503 | return Error::success(); |
| 2504 | } |
| 2505 | |
| 2506 | static StructuralEquivalenceKind |
| 2507 | getStructuralEquivalenceKind(const ASTImporter &Importer) { |
| 2508 | return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal |
| 2509 | : StructuralEquivalenceKind::Default; |
| 2510 | } |
| 2511 | |
| 2512 | bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain, |
| 2513 | bool IgnoreTemplateParmDepth) { |
| 2514 | // Eliminate a potential failure point where we attempt to re-import |
| 2515 | // something we're trying to import while completing ToRecord. |
| 2516 | Decl *ToOrigin = Importer.GetOriginalDecl(To); |
| 2517 | if (ToOrigin) { |
| 2518 | To = ToOrigin; |
| 2519 | } |
| 2520 | |
| 2521 | StructuralEquivalenceContext Ctx( |
| 2522 | Importer.getToContext().getLangOpts(), Importer.getFromContext(), |
| 2523 | Importer.getToContext(), Importer.getNonEquivalentDecls(), |
| 2524 | getStructuralEquivalenceKind(Importer), |
| 2525 | /*StrictTypeSpelling=*/false, Complain, /*ErrorOnTagTypeMismatch=*/false, |
| 2526 | IgnoreTemplateParmDepth); |
| 2527 | return Ctx.IsEquivalent(D1: From, D2: To); |
| 2528 | } |
| 2529 | |
| 2530 | ExpectedDecl ASTNodeImporter::VisitDecl(Decl *D) { |
| 2531 | Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) |
| 2532 | << D->getDeclKindName(); |
| 2533 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
| 2534 | } |
| 2535 | |
| 2536 | ExpectedDecl ASTNodeImporter::VisitImportDecl(ImportDecl *D) { |
| 2537 | Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) |
| 2538 | << D->getDeclKindName(); |
| 2539 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
| 2540 | } |
| 2541 | |
| 2542 | ExpectedDecl ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) { |
| 2543 | // Import the context of this declaration. |
| 2544 | DeclContext *DC, *LexicalDC; |
| 2545 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) |
| 2546 | return std::move(Err); |
| 2547 | |
| 2548 | // Import the location of this declaration. |
| 2549 | ExpectedSLoc LocOrErr = import(D->getLocation()); |
| 2550 | if (!LocOrErr) |
| 2551 | return LocOrErr.takeError(); |
| 2552 | |
| 2553 | EmptyDecl *ToD; |
| 2554 | if (GetImportedOrCreateDecl(ToD, FromD: D, args&: Importer.getToContext(), args&: DC, args&: *LocOrErr)) |
| 2555 | return ToD; |
| 2556 | |
| 2557 | ToD->setLexicalDeclContext(LexicalDC); |
| 2558 | LexicalDC->addDeclInternal(ToD); |
| 2559 | return ToD; |
| 2560 | } |
| 2561 | |
| 2562 | ExpectedDecl ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 2563 | TranslationUnitDecl *ToD = |
| 2564 | Importer.getToContext().getTranslationUnitDecl(); |
| 2565 | |
| 2566 | Importer.MapImported(D, ToD); |
| 2567 | |
| 2568 | return ToD; |
| 2569 | } |
| 2570 | |
| 2571 | ExpectedDecl ASTNodeImporter::VisitBindingDecl(BindingDecl *D) { |
| 2572 | DeclContext *DC, *LexicalDC; |
| 2573 | DeclarationName Name; |
| 2574 | SourceLocation Loc; |
| 2575 | NamedDecl *ToND; |
| 2576 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToND, Loc)) |
| 2577 | return std::move(Err); |
| 2578 | if (ToND) |
| 2579 | return ToND; |
| 2580 | |
| 2581 | BindingDecl *ToD; |
| 2582 | if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, Loc, |
| 2583 | Name.getAsIdentifierInfo(), D->getType())) |
| 2584 | return ToD; |
| 2585 | |
| 2586 | Error Err = Error::success(); |
| 2587 | QualType ToType = importChecked(Err, D->getType()); |
| 2588 | Expr *ToBinding = importChecked(Err, From: D->getBinding()); |
| 2589 | ValueDecl *ToDecomposedDecl = importChecked(Err, From: D->getDecomposedDecl()); |
| 2590 | if (Err) |
| 2591 | return std::move(Err); |
| 2592 | |
| 2593 | ToD->setBinding(DeclaredType: ToType, Binding: ToBinding); |
| 2594 | ToD->setDecomposedDecl(ToDecomposedDecl); |
| 2595 | addDeclToContexts(D, ToD); |
| 2596 | |
| 2597 | return ToD; |
| 2598 | } |
| 2599 | |
| 2600 | ExpectedDecl ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) { |
| 2601 | ExpectedSLoc LocOrErr = import(D->getLocation()); |
| 2602 | if (!LocOrErr) |
| 2603 | return LocOrErr.takeError(); |
| 2604 | auto ColonLocOrErr = import(From: D->getColonLoc()); |
| 2605 | if (!ColonLocOrErr) |
| 2606 | return ColonLocOrErr.takeError(); |
| 2607 | |
| 2608 | // Import the context of this declaration. |
| 2609 | auto DCOrErr = Importer.ImportContext(FromDC: D->getDeclContext()); |
| 2610 | if (!DCOrErr) |
| 2611 | return DCOrErr.takeError(); |
| 2612 | DeclContext *DC = *DCOrErr; |
| 2613 | |
| 2614 | AccessSpecDecl *ToD; |
| 2615 | if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), D->getAccess(), |
| 2616 | DC, *LocOrErr, *ColonLocOrErr)) |
| 2617 | return ToD; |
| 2618 | |
| 2619 | // Lexical DeclContext and Semantic DeclContext |
| 2620 | // is always the same for the accessSpec. |
| 2621 | ToD->setLexicalDeclContext(DC); |
| 2622 | DC->addDeclInternal(ToD); |
| 2623 | |
| 2624 | return ToD; |
| 2625 | } |
| 2626 | |
| 2627 | ExpectedDecl ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 2628 | auto DCOrErr = Importer.ImportContext(FromDC: D->getDeclContext()); |
| 2629 | if (!DCOrErr) |
| 2630 | return DCOrErr.takeError(); |
| 2631 | DeclContext *DC = *DCOrErr; |
| 2632 | DeclContext *LexicalDC = DC; |
| 2633 | |
| 2634 | Error Err = Error::success(); |
| 2635 | auto ToLocation = importChecked(Err, D->getLocation()); |
| 2636 | auto ToRParenLoc = importChecked(Err, From: D->getRParenLoc()); |
| 2637 | auto ToAssertExpr = importChecked(Err, From: D->getAssertExpr()); |
| 2638 | auto ToMessage = importChecked(Err, From: D->getMessage()); |
| 2639 | if (Err) |
| 2640 | return std::move(Err); |
| 2641 | |
| 2642 | StaticAssertDecl *ToD; |
| 2643 | if (GetImportedOrCreateDecl( |
| 2644 | ToD, D, Importer.getToContext(), DC, ToLocation, ToAssertExpr, ToMessage, |
| 2645 | ToRParenLoc, D->isFailed())) |
| 2646 | return ToD; |
| 2647 | |
| 2648 | ToD->setLexicalDeclContext(LexicalDC); |
| 2649 | LexicalDC->addDeclInternal(ToD); |
| 2650 | return ToD; |
| 2651 | } |
| 2652 | |
| 2653 | ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) { |
| 2654 | // Import the major distinguishing characteristics of this namespace. |
| 2655 | DeclContext *DC, *LexicalDC; |
| 2656 | DeclarationName Name; |
| 2657 | SourceLocation Loc; |
| 2658 | NamedDecl *ToD; |
| 2659 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 2660 | return std::move(Err); |
| 2661 | if (ToD) |
| 2662 | return ToD; |
| 2663 | |
| 2664 | NamespaceDecl *MergeWithNamespace = nullptr; |
| 2665 | if (!Name) { |
| 2666 | // This is an anonymous namespace. Adopt an existing anonymous |
| 2667 | // namespace if we can. |
| 2668 | DeclContext *EnclosingDC = DC->getEnclosingNamespaceContext(); |
| 2669 | if (auto *TU = dyn_cast<TranslationUnitDecl>(Val: EnclosingDC)) |
| 2670 | MergeWithNamespace = TU->getAnonymousNamespace(); |
| 2671 | else |
| 2672 | MergeWithNamespace = |
| 2673 | cast<NamespaceDecl>(Val: EnclosingDC)->getAnonymousNamespace(); |
| 2674 | } else { |
| 2675 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 2676 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 2677 | for (auto *FoundDecl : FoundDecls) { |
| 2678 | if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace)) |
| 2679 | continue; |
| 2680 | |
| 2681 | if (auto *FoundNS = dyn_cast<NamespaceDecl>(Val: FoundDecl)) { |
| 2682 | MergeWithNamespace = FoundNS; |
| 2683 | ConflictingDecls.clear(); |
| 2684 | break; |
| 2685 | } |
| 2686 | |
| 2687 | ConflictingDecls.push_back(Elt: FoundDecl); |
| 2688 | } |
| 2689 | |
| 2690 | if (!ConflictingDecls.empty()) { |
| 2691 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
| 2692 | Name, DC, IDNS: Decl::IDNS_Namespace, Decls: ConflictingDecls.data(), |
| 2693 | NumDecls: ConflictingDecls.size()); |
| 2694 | if (NameOrErr) |
| 2695 | Name = NameOrErr.get(); |
| 2696 | else |
| 2697 | return NameOrErr.takeError(); |
| 2698 | } |
| 2699 | } |
| 2700 | |
| 2701 | ExpectedSLoc BeginLocOrErr = import(From: D->getBeginLoc()); |
| 2702 | if (!BeginLocOrErr) |
| 2703 | return BeginLocOrErr.takeError(); |
| 2704 | ExpectedSLoc RBraceLocOrErr = import(From: D->getRBraceLoc()); |
| 2705 | if (!RBraceLocOrErr) |
| 2706 | return RBraceLocOrErr.takeError(); |
| 2707 | |
| 2708 | // Create the "to" namespace, if needed. |
| 2709 | NamespaceDecl *ToNamespace = MergeWithNamespace; |
| 2710 | if (!ToNamespace) { |
| 2711 | if (GetImportedOrCreateDecl(ToD&: ToNamespace, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 2712 | args: D->isInline(), args&: *BeginLocOrErr, args&: Loc, |
| 2713 | args: Name.getAsIdentifierInfo(), |
| 2714 | /*PrevDecl=*/args: nullptr, args: D->isNested())) |
| 2715 | return ToNamespace; |
| 2716 | ToNamespace->setRBraceLoc(*RBraceLocOrErr); |
| 2717 | ToNamespace->setLexicalDeclContext(LexicalDC); |
| 2718 | LexicalDC->addDeclInternal(ToNamespace); |
| 2719 | |
| 2720 | // If this is an anonymous namespace, register it as the anonymous |
| 2721 | // namespace within its context. |
| 2722 | if (!Name) { |
| 2723 | if (auto *TU = dyn_cast<TranslationUnitDecl>(Val: DC)) |
| 2724 | TU->setAnonymousNamespace(ToNamespace); |
| 2725 | else |
| 2726 | cast<NamespaceDecl>(Val: DC)->setAnonymousNamespace(ToNamespace); |
| 2727 | } |
| 2728 | } |
| 2729 | Importer.MapImported(D, ToNamespace); |
| 2730 | |
| 2731 | if (Error Err = ImportDeclContext(D)) |
| 2732 | return std::move(Err); |
| 2733 | |
| 2734 | return ToNamespace; |
| 2735 | } |
| 2736 | |
| 2737 | ExpectedDecl ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
| 2738 | // Import the major distinguishing characteristics of this namespace. |
| 2739 | DeclContext *DC, *LexicalDC; |
| 2740 | DeclarationName Name; |
| 2741 | SourceLocation Loc; |
| 2742 | NamedDecl *LookupD; |
| 2743 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc)) |
| 2744 | return std::move(Err); |
| 2745 | if (LookupD) |
| 2746 | return LookupD; |
| 2747 | |
| 2748 | // NOTE: No conflict resolution is done for namespace aliases now. |
| 2749 | |
| 2750 | Error Err = Error::success(); |
| 2751 | auto ToNamespaceLoc = importChecked(Err, From: D->getNamespaceLoc()); |
| 2752 | auto ToAliasLoc = importChecked(Err, From: D->getAliasLoc()); |
| 2753 | auto ToQualifierLoc = importChecked(Err, From: D->getQualifierLoc()); |
| 2754 | auto ToTargetNameLoc = importChecked(Err, From: D->getTargetNameLoc()); |
| 2755 | auto ToNamespace = importChecked(Err, From: D->getNamespace()); |
| 2756 | if (Err) |
| 2757 | return std::move(Err); |
| 2758 | |
| 2759 | IdentifierInfo *ToIdentifier = Importer.Import(D->getIdentifier()); |
| 2760 | |
| 2761 | NamespaceAliasDecl *ToD; |
| 2762 | if (GetImportedOrCreateDecl( |
| 2763 | ToD, FromD: D, args&: Importer.getToContext(), args&: DC, args&: ToNamespaceLoc, args&: ToAliasLoc, |
| 2764 | args&: ToIdentifier, args&: ToQualifierLoc, args&: ToTargetNameLoc, args&: ToNamespace)) |
| 2765 | return ToD; |
| 2766 | |
| 2767 | ToD->setLexicalDeclContext(LexicalDC); |
| 2768 | LexicalDC->addDeclInternal(ToD); |
| 2769 | |
| 2770 | return ToD; |
| 2771 | } |
| 2772 | |
| 2773 | ExpectedDecl |
| 2774 | ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) { |
| 2775 | // Import the major distinguishing characteristics of this typedef. |
| 2776 | DeclarationName Name; |
| 2777 | SourceLocation Loc; |
| 2778 | NamedDecl *ToD; |
| 2779 | // Do not import the DeclContext, we will import it once the TypedefNameDecl |
| 2780 | // is created. |
| 2781 | if (Error Err = ImportDeclParts(D, Name, ToD, Loc)) |
| 2782 | return std::move(Err); |
| 2783 | if (ToD) |
| 2784 | return ToD; |
| 2785 | |
| 2786 | DeclContext *DC = cast_or_null<DeclContext>( |
| 2787 | Importer.GetAlreadyImportedOrNull(FromD: cast<Decl>(D->getDeclContext()))); |
| 2788 | DeclContext *LexicalDC = |
| 2789 | cast_or_null<DeclContext>(Importer.GetAlreadyImportedOrNull( |
| 2790 | FromD: cast<Decl>(D->getLexicalDeclContext()))); |
| 2791 | |
| 2792 | // If this typedef is not in block scope, determine whether we've |
| 2793 | // seen a typedef with the same name (that we can merge with) or any |
| 2794 | // other entity by that name (which name lookup could conflict with). |
| 2795 | // Note: Repeated typedefs are not valid in C99: |
| 2796 | // 'typedef int T; typedef int T;' is invalid |
| 2797 | // We do not care about this now. |
| 2798 | if (DC && !DC->isFunctionOrMethod()) { |
| 2799 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 2800 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 2801 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 2802 | for (auto *FoundDecl : FoundDecls) { |
| 2803 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) |
| 2804 | continue; |
| 2805 | if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) { |
| 2806 | if (!hasSameVisibilityContextAndLinkage(FoundTypedef, D)) |
| 2807 | continue; |
| 2808 | |
| 2809 | QualType FromUT = D->getUnderlyingType(); |
| 2810 | QualType FoundUT = FoundTypedef->getUnderlyingType(); |
| 2811 | if (Importer.IsStructurallyEquivalent(FromUT, FoundUT)) { |
| 2812 | // If the underlying declarations are unnamed records these can be |
| 2813 | // imported as different types. We should create a distinct typedef |
| 2814 | // node in this case. |
| 2815 | // If we found an existing underlying type with a record in a |
| 2816 | // different context (than the imported), this is already reason for |
| 2817 | // having distinct typedef nodes for these. |
| 2818 | // Again this can create situation like |
| 2819 | // 'typedef int T; typedef int T;' but this is hard to avoid without |
| 2820 | // a rename strategy at import. |
| 2821 | if (!FromUT.isNull() && !FoundUT.isNull()) { |
| 2822 | RecordDecl *FromR = FromUT->getAsRecordDecl(); |
| 2823 | RecordDecl *FoundR = FoundUT->getAsRecordDecl(); |
| 2824 | if (FromR && FoundR && |
| 2825 | !hasSameVisibilityContextAndLinkage(FoundR, FromR)) |
| 2826 | continue; |
| 2827 | } |
| 2828 | // If the "From" context has a complete underlying type but we |
| 2829 | // already have a complete underlying type then return with that. |
| 2830 | if (!FromUT->isIncompleteType() && !FoundUT->isIncompleteType()) |
| 2831 | return Importer.MapImported(D, FoundTypedef); |
| 2832 | // FIXME Handle redecl chain. When you do that make consistent changes |
| 2833 | // in ASTImporterLookupTable too. |
| 2834 | } else { |
| 2835 | ConflictingDecls.push_back(FoundDecl); |
| 2836 | } |
| 2837 | } |
| 2838 | } |
| 2839 | |
| 2840 | if (!ConflictingDecls.empty()) { |
| 2841 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
| 2842 | Name, DC, IDNS, Decls: ConflictingDecls.data(), NumDecls: ConflictingDecls.size()); |
| 2843 | if (NameOrErr) |
| 2844 | Name = NameOrErr.get(); |
| 2845 | else |
| 2846 | return NameOrErr.takeError(); |
| 2847 | } |
| 2848 | } |
| 2849 | |
| 2850 | Error Err = Error::success(); |
| 2851 | auto ToUnderlyingType = importChecked(Err, From: D->getUnderlyingType()); |
| 2852 | auto ToTypeSourceInfo = importChecked(Err, From: D->getTypeSourceInfo()); |
| 2853 | auto ToBeginLoc = importChecked(Err, D->getBeginLoc()); |
| 2854 | if (Err) |
| 2855 | return std::move(Err); |
| 2856 | |
| 2857 | // Create the new typedef node. |
| 2858 | // FIXME: ToUnderlyingType is not used. |
| 2859 | (void)ToUnderlyingType; |
| 2860 | TypedefNameDecl *ToTypedef; |
| 2861 | if (IsAlias) { |
| 2862 | if (GetImportedOrCreateDecl<TypeAliasDecl>( |
| 2863 | ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc, |
| 2864 | Name.getAsIdentifierInfo(), ToTypeSourceInfo)) |
| 2865 | return ToTypedef; |
| 2866 | } else if (GetImportedOrCreateDecl<TypedefDecl>( |
| 2867 | ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc, |
| 2868 | Name.getAsIdentifierInfo(), ToTypeSourceInfo)) |
| 2869 | return ToTypedef; |
| 2870 | |
| 2871 | // Import the DeclContext and set it to the Typedef. |
| 2872 | if ((Err = ImportDeclContext(D, DC, LexicalDC))) |
| 2873 | return std::move(Err); |
| 2874 | ToTypedef->setDeclContext(DC); |
| 2875 | ToTypedef->setLexicalDeclContext(LexicalDC); |
| 2876 | // Add to the lookupTable because we could not do that in MapImported. |
| 2877 | Importer.AddToLookupTable(ToTypedef); |
| 2878 | |
| 2879 | ToTypedef->setAccess(D->getAccess()); |
| 2880 | |
| 2881 | // Templated declarations should not appear in DeclContext. |
| 2882 | TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(Val: D) : nullptr; |
| 2883 | if (!FromAlias || !FromAlias->getDescribedAliasTemplate()) |
| 2884 | LexicalDC->addDeclInternal(ToTypedef); |
| 2885 | |
| 2886 | return ToTypedef; |
| 2887 | } |
| 2888 | |
| 2889 | ExpectedDecl ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) { |
| 2890 | return VisitTypedefNameDecl(D, /*IsAlias=*/false); |
| 2891 | } |
| 2892 | |
| 2893 | ExpectedDecl ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| 2894 | return VisitTypedefNameDecl(D, /*IsAlias=*/true); |
| 2895 | } |
| 2896 | |
| 2897 | ExpectedDecl |
| 2898 | ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { |
| 2899 | // Import the major distinguishing characteristics of this typedef. |
| 2900 | DeclContext *DC, *LexicalDC; |
| 2901 | DeclarationName Name; |
| 2902 | SourceLocation Loc; |
| 2903 | NamedDecl *FoundD; |
| 2904 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc)) |
| 2905 | return std::move(Err); |
| 2906 | if (FoundD) |
| 2907 | return FoundD; |
| 2908 | |
| 2909 | // If this typedef is not in block scope, determine whether we've |
| 2910 | // seen a typedef with the same name (that we can merge with) or any |
| 2911 | // other entity by that name (which name lookup could conflict with). |
| 2912 | if (!DC->isFunctionOrMethod()) { |
| 2913 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 2914 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 2915 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 2916 | for (auto *FoundDecl : FoundDecls) { |
| 2917 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) |
| 2918 | continue; |
| 2919 | if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(Val: FoundDecl)) { |
| 2920 | if (IsStructuralMatch(D, FoundAlias)) |
| 2921 | return Importer.MapImported(D, FoundAlias); |
| 2922 | ConflictingDecls.push_back(Elt: FoundDecl); |
| 2923 | } |
| 2924 | } |
| 2925 | |
| 2926 | if (!ConflictingDecls.empty()) { |
| 2927 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
| 2928 | Name, DC, IDNS, Decls: ConflictingDecls.data(), NumDecls: ConflictingDecls.size()); |
| 2929 | if (NameOrErr) |
| 2930 | Name = NameOrErr.get(); |
| 2931 | else |
| 2932 | return NameOrErr.takeError(); |
| 2933 | } |
| 2934 | } |
| 2935 | |
| 2936 | Error Err = Error::success(); |
| 2937 | auto ToTemplateParameters = importChecked(Err, D->getTemplateParameters()); |
| 2938 | auto ToTemplatedDecl = importChecked(Err, From: D->getTemplatedDecl()); |
| 2939 | if (Err) |
| 2940 | return std::move(Err); |
| 2941 | |
| 2942 | TypeAliasTemplateDecl *ToAlias; |
| 2943 | if (GetImportedOrCreateDecl(ToAlias, D, Importer.getToContext(), DC, Loc, |
| 2944 | Name, ToTemplateParameters, ToTemplatedDecl)) |
| 2945 | return ToAlias; |
| 2946 | |
| 2947 | ToTemplatedDecl->setDescribedAliasTemplate(ToAlias); |
| 2948 | |
| 2949 | ToAlias->setAccess(D->getAccess()); |
| 2950 | ToAlias->setLexicalDeclContext(LexicalDC); |
| 2951 | LexicalDC->addDeclInternal(ToAlias); |
| 2952 | if (DC != Importer.getToContext().getTranslationUnitDecl()) |
| 2953 | updateLookupTableForTemplateParameters(*ToTemplateParameters); |
| 2954 | return ToAlias; |
| 2955 | } |
| 2956 | |
| 2957 | ExpectedDecl ASTNodeImporter::VisitLabelDecl(LabelDecl *D) { |
| 2958 | // Import the major distinguishing characteristics of this label. |
| 2959 | DeclContext *DC, *LexicalDC; |
| 2960 | DeclarationName Name; |
| 2961 | SourceLocation Loc; |
| 2962 | NamedDecl *ToD; |
| 2963 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 2964 | return std::move(Err); |
| 2965 | if (ToD) |
| 2966 | return ToD; |
| 2967 | |
| 2968 | assert(LexicalDC->isFunctionOrMethod()); |
| 2969 | |
| 2970 | LabelDecl *ToLabel; |
| 2971 | if (D->isGnuLocal()) { |
| 2972 | ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc()); |
| 2973 | if (!BeginLocOrErr) |
| 2974 | return BeginLocOrErr.takeError(); |
| 2975 | if (GetImportedOrCreateDecl(ToD&: ToLabel, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, |
| 2976 | args: Name.getAsIdentifierInfo(), args&: *BeginLocOrErr)) |
| 2977 | return ToLabel; |
| 2978 | |
| 2979 | } else { |
| 2980 | if (GetImportedOrCreateDecl(ToD&: ToLabel, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, |
| 2981 | args: Name.getAsIdentifierInfo())) |
| 2982 | return ToLabel; |
| 2983 | |
| 2984 | } |
| 2985 | |
| 2986 | Expected<LabelStmt *> ToStmtOrErr = import(From: D->getStmt()); |
| 2987 | if (!ToStmtOrErr) |
| 2988 | return ToStmtOrErr.takeError(); |
| 2989 | |
| 2990 | ToLabel->setStmt(*ToStmtOrErr); |
| 2991 | ToLabel->setLexicalDeclContext(LexicalDC); |
| 2992 | LexicalDC->addDeclInternal(ToLabel); |
| 2993 | return ToLabel; |
| 2994 | } |
| 2995 | |
| 2996 | ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) { |
| 2997 | // Import the major distinguishing characteristics of this enum. |
| 2998 | DeclContext *DC, *LexicalDC; |
| 2999 | DeclarationName Name; |
| 3000 | SourceLocation Loc; |
| 3001 | NamedDecl *ToD; |
| 3002 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 3003 | return std::move(Err); |
| 3004 | if (ToD) |
| 3005 | return ToD; |
| 3006 | |
| 3007 | // Figure out what enum name we're looking for. |
| 3008 | unsigned IDNS = Decl::IDNS_Tag; |
| 3009 | DeclarationName SearchName = Name; |
| 3010 | if (!SearchName && D->getTypedefNameForAnonDecl()) { |
| 3011 | if (Error Err = importInto( |
| 3012 | SearchName, D->getTypedefNameForAnonDecl()->getDeclName())) |
| 3013 | return std::move(Err); |
| 3014 | IDNS = Decl::IDNS_Ordinary; |
| 3015 | } else if (Importer.getToContext().getLangOpts().CPlusPlus) |
| 3016 | IDNS |= Decl::IDNS_Ordinary; |
| 3017 | |
| 3018 | // We may already have an enum of the same name; try to find and match it. |
| 3019 | EnumDecl *PrevDecl = nullptr; |
| 3020 | if (!DC->isFunctionOrMethod()) { |
| 3021 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 3022 | auto FoundDecls = |
| 3023 | Importer.findDeclsInToCtx(DC, Name: SearchName); |
| 3024 | for (auto *FoundDecl : FoundDecls) { |
| 3025 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) |
| 3026 | continue; |
| 3027 | |
| 3028 | if (auto *Typedef = dyn_cast<TypedefNameDecl>(Val: FoundDecl)) { |
| 3029 | if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) |
| 3030 | FoundDecl = Tag->getDecl(); |
| 3031 | } |
| 3032 | |
| 3033 | if (auto *FoundEnum = dyn_cast<EnumDecl>(Val: FoundDecl)) { |
| 3034 | if (!hasSameVisibilityContextAndLinkage(Found: FoundEnum, From: D)) |
| 3035 | continue; |
| 3036 | if (IsStructuralMatch(D, FoundEnum, !SearchName.isEmpty())) { |
| 3037 | EnumDecl *FoundDef = FoundEnum->getDefinition(); |
| 3038 | if (D->isThisDeclarationADefinition() && FoundDef) |
| 3039 | return Importer.MapImported(D, FoundDef); |
| 3040 | PrevDecl = FoundEnum->getMostRecentDecl(); |
| 3041 | break; |
| 3042 | } |
| 3043 | ConflictingDecls.push_back(Elt: FoundDecl); |
| 3044 | } |
| 3045 | } |
| 3046 | |
| 3047 | // In case of unnamed enums, we try to find an existing similar one, if none |
| 3048 | // was found, perform the import always. |
| 3049 | // Structural in-equivalence is not detected in this way here, but it may |
| 3050 | // be found when the parent decl is imported (if the enum is part of a |
| 3051 | // class). To make this totally exact a more difficult solution is needed. |
| 3052 | if (SearchName && !ConflictingDecls.empty()) { |
| 3053 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
| 3054 | Name: SearchName, DC, IDNS, Decls: ConflictingDecls.data(), |
| 3055 | NumDecls: ConflictingDecls.size()); |
| 3056 | if (NameOrErr) |
| 3057 | Name = NameOrErr.get(); |
| 3058 | else |
| 3059 | return NameOrErr.takeError(); |
| 3060 | } |
| 3061 | } |
| 3062 | |
| 3063 | Error Err = Error::success(); |
| 3064 | auto ToBeginLoc = importChecked(Err, D->getBeginLoc()); |
| 3065 | auto ToQualifierLoc = importChecked(Err, D->getQualifierLoc()); |
| 3066 | auto ToIntegerType = importChecked(Err, From: D->getIntegerType()); |
| 3067 | auto ToBraceRange = importChecked(Err, D->getBraceRange()); |
| 3068 | if (Err) |
| 3069 | return std::move(Err); |
| 3070 | |
| 3071 | // Create the enum declaration. |
| 3072 | EnumDecl *D2; |
| 3073 | if (GetImportedOrCreateDecl( |
| 3074 | D2, D, Importer.getToContext(), DC, ToBeginLoc, |
| 3075 | Loc, Name.getAsIdentifierInfo(), PrevDecl, D->isScoped(), |
| 3076 | D->isScopedUsingClassTag(), D->isFixed())) |
| 3077 | return D2; |
| 3078 | |
| 3079 | D2->setQualifierInfo(ToQualifierLoc); |
| 3080 | D2->setIntegerType(ToIntegerType); |
| 3081 | D2->setBraceRange(ToBraceRange); |
| 3082 | D2->setAccess(D->getAccess()); |
| 3083 | D2->setLexicalDeclContext(LexicalDC); |
| 3084 | addDeclToContexts(D, D2); |
| 3085 | |
| 3086 | if (MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo()) { |
| 3087 | TemplateSpecializationKind SK = MemberInfo->getTemplateSpecializationKind(); |
| 3088 | EnumDecl *FromInst = D->getInstantiatedFromMemberEnum(); |
| 3089 | if (Expected<EnumDecl *> ToInstOrErr = import(From: FromInst)) |
| 3090 | D2->setInstantiationOfMemberEnum(ED: *ToInstOrErr, TSK: SK); |
| 3091 | else |
| 3092 | return ToInstOrErr.takeError(); |
| 3093 | if (ExpectedSLoc POIOrErr = import(From: MemberInfo->getPointOfInstantiation())) |
| 3094 | D2->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr); |
| 3095 | else |
| 3096 | return POIOrErr.takeError(); |
| 3097 | } |
| 3098 | |
| 3099 | // Import the definition |
| 3100 | if (D->isCompleteDefinition()) |
| 3101 | if (Error Err = ImportDefinition(From: D, To: D2)) |
| 3102 | return std::move(Err); |
| 3103 | |
| 3104 | return D2; |
| 3105 | } |
| 3106 | |
| 3107 | ExpectedDecl ASTNodeImporter::VisitRecordDecl(RecordDecl *D) { |
| 3108 | bool IsFriendTemplate = false; |
| 3109 | if (auto *DCXX = dyn_cast<CXXRecordDecl>(Val: D)) { |
| 3110 | IsFriendTemplate = |
| 3111 | DCXX->getDescribedClassTemplate() && |
| 3112 | DCXX->getDescribedClassTemplate()->getFriendObjectKind() != |
| 3113 | Decl::FOK_None; |
| 3114 | } |
| 3115 | |
| 3116 | // Import the major distinguishing characteristics of this record. |
| 3117 | DeclContext *DC = nullptr, *LexicalDC = nullptr; |
| 3118 | DeclarationName Name; |
| 3119 | SourceLocation Loc; |
| 3120 | NamedDecl *ToD = nullptr; |
| 3121 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 3122 | return std::move(Err); |
| 3123 | if (ToD) |
| 3124 | return ToD; |
| 3125 | |
| 3126 | // Figure out what structure name we're looking for. |
| 3127 | unsigned IDNS = Decl::IDNS_Tag; |
| 3128 | DeclarationName SearchName = Name; |
| 3129 | if (!SearchName && D->getTypedefNameForAnonDecl()) { |
| 3130 | if (Error Err = importInto( |
| 3131 | SearchName, D->getTypedefNameForAnonDecl()->getDeclName())) |
| 3132 | return std::move(Err); |
| 3133 | IDNS = Decl::IDNS_Ordinary; |
| 3134 | } else if (Importer.getToContext().getLangOpts().CPlusPlus) |
| 3135 | IDNS |= Decl::IDNS_Ordinary | Decl::IDNS_TagFriend; |
| 3136 | |
| 3137 | bool IsDependentContext = DC != LexicalDC ? LexicalDC->isDependentContext() |
| 3138 | : DC->isDependentContext(); |
| 3139 | bool DependentFriend = IsFriendTemplate && IsDependentContext; |
| 3140 | |
| 3141 | // We may already have a record of the same name; try to find and match it. |
| 3142 | RecordDecl *PrevDecl = nullptr; |
| 3143 | if (!DependentFriend && !DC->isFunctionOrMethod() && !D->isLambda()) { |
| 3144 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 3145 | auto FoundDecls = |
| 3146 | Importer.findDeclsInToCtx(DC, Name: SearchName); |
| 3147 | if (!FoundDecls.empty()) { |
| 3148 | // We're going to have to compare D against potentially conflicting Decls, |
| 3149 | // so complete it. |
| 3150 | if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition()) |
| 3151 | D->getASTContext().getExternalSource()->CompleteType(D); |
| 3152 | } |
| 3153 | |
| 3154 | for (auto *FoundDecl : FoundDecls) { |
| 3155 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) |
| 3156 | continue; |
| 3157 | |
| 3158 | Decl *Found = FoundDecl; |
| 3159 | if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) { |
| 3160 | if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) |
| 3161 | Found = Tag->getDecl(); |
| 3162 | } |
| 3163 | |
| 3164 | if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) { |
| 3165 | // Do not emit false positive diagnostic in case of unnamed |
| 3166 | // struct/union and in case of anonymous structs. Would be false |
| 3167 | // because there may be several anonymous/unnamed structs in a class. |
| 3168 | // E.g. these are both valid: |
| 3169 | // struct A { // unnamed structs |
| 3170 | // struct { struct A *next; } entry0; |
| 3171 | // struct { struct A *next; } entry1; |
| 3172 | // }; |
| 3173 | // struct X { struct { int a; }; struct { int b; }; }; // anon structs |
| 3174 | if (!SearchName) |
| 3175 | if (!IsStructuralMatch(From: D, To: FoundRecord, Complain: false)) |
| 3176 | continue; |
| 3177 | |
| 3178 | if (!hasSameVisibilityContextAndLinkage(FoundRecord, D)) |
| 3179 | continue; |
| 3180 | |
| 3181 | if (IsStructuralMatch(From: D, To: FoundRecord)) { |
| 3182 | RecordDecl *FoundDef = FoundRecord->getDefinition(); |
| 3183 | if (D->isThisDeclarationADefinition() && FoundDef) { |
| 3184 | // FIXME: Structural equivalence check should check for same |
| 3185 | // user-defined methods. |
| 3186 | Importer.MapImported(D, FoundDef); |
| 3187 | if (const auto *DCXX = dyn_cast<CXXRecordDecl>(Val: D)) { |
| 3188 | auto *FoundCXX = dyn_cast<CXXRecordDecl>(Val: FoundDef); |
| 3189 | assert(FoundCXX && "Record type mismatch" ); |
| 3190 | |
| 3191 | if (!Importer.isMinimalImport()) |
| 3192 | // FoundDef may not have every implicit method that D has |
| 3193 | // because implicit methods are created only if they are used. |
| 3194 | if (Error Err = ImportImplicitMethods(From: DCXX, To: FoundCXX)) |
| 3195 | return std::move(Err); |
| 3196 | } |
| 3197 | // FIXME: We can return FoundDef here. |
| 3198 | } |
| 3199 | PrevDecl = FoundRecord->getMostRecentDecl(); |
| 3200 | break; |
| 3201 | } |
| 3202 | ConflictingDecls.push_back(Elt: FoundDecl); |
| 3203 | } // kind is RecordDecl |
| 3204 | } // for |
| 3205 | |
| 3206 | if (!ConflictingDecls.empty() && SearchName) { |
| 3207 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
| 3208 | Name: SearchName, DC, IDNS, Decls: ConflictingDecls.data(), |
| 3209 | NumDecls: ConflictingDecls.size()); |
| 3210 | if (NameOrErr) |
| 3211 | Name = NameOrErr.get(); |
| 3212 | else |
| 3213 | return NameOrErr.takeError(); |
| 3214 | } |
| 3215 | } |
| 3216 | |
| 3217 | ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc()); |
| 3218 | if (!BeginLocOrErr) |
| 3219 | return BeginLocOrErr.takeError(); |
| 3220 | |
| 3221 | // Create the record declaration. |
| 3222 | RecordDecl *D2 = nullptr; |
| 3223 | CXXRecordDecl *D2CXX = nullptr; |
| 3224 | if (auto *DCXX = dyn_cast<CXXRecordDecl>(Val: D)) { |
| 3225 | if (DCXX->isLambda()) { |
| 3226 | auto TInfoOrErr = import(From: DCXX->getLambdaTypeInfo()); |
| 3227 | if (!TInfoOrErr) |
| 3228 | return TInfoOrErr.takeError(); |
| 3229 | if (GetImportedOrCreateSpecialDecl( |
| 3230 | ToD&: D2CXX, CreateFun: CXXRecordDecl::CreateLambda, FromD: D, args&: Importer.getToContext(), |
| 3231 | args&: DC, args&: *TInfoOrErr, args&: Loc, args: DCXX->getLambdaDependencyKind(), |
| 3232 | args: DCXX->isGenericLambda(), args: DCXX->getLambdaCaptureDefault())) |
| 3233 | return D2CXX; |
| 3234 | CXXRecordDecl::LambdaNumbering Numbering = DCXX->getLambdaNumbering(); |
| 3235 | ExpectedDecl CDeclOrErr = import(From: Numbering.ContextDecl); |
| 3236 | if (!CDeclOrErr) |
| 3237 | return CDeclOrErr.takeError(); |
| 3238 | Numbering.ContextDecl = *CDeclOrErr; |
| 3239 | D2CXX->setLambdaNumbering(Numbering); |
| 3240 | } else if (DCXX->isInjectedClassName()) { |
| 3241 | // We have to be careful to do a similar dance to the one in |
| 3242 | // Sema::ActOnStartCXXMemberDeclarations |
| 3243 | const bool DelayTypeCreation = true; |
| 3244 | if (GetImportedOrCreateDecl( |
| 3245 | D2CXX, D, Importer.getToContext(), D->getTagKind(), DC, |
| 3246 | *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(), |
| 3247 | cast_or_null<CXXRecordDecl>(Val: PrevDecl), DelayTypeCreation)) |
| 3248 | return D2CXX; |
| 3249 | Importer.getToContext().getTypeDeclType( |
| 3250 | D2CXX, dyn_cast<CXXRecordDecl>(Val: DC)); |
| 3251 | } else { |
| 3252 | if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(), |
| 3253 | D->getTagKind(), DC, *BeginLocOrErr, Loc, |
| 3254 | Name.getAsIdentifierInfo(), |
| 3255 | cast_or_null<CXXRecordDecl>(Val: PrevDecl))) |
| 3256 | return D2CXX; |
| 3257 | } |
| 3258 | |
| 3259 | D2 = D2CXX; |
| 3260 | D2->setAccess(D->getAccess()); |
| 3261 | D2->setLexicalDeclContext(LexicalDC); |
| 3262 | addDeclToContexts(D, D2); |
| 3263 | |
| 3264 | if (ClassTemplateDecl *FromDescribed = |
| 3265 | DCXX->getDescribedClassTemplate()) { |
| 3266 | ClassTemplateDecl *ToDescribed; |
| 3267 | if (Error Err = importInto(To&: ToDescribed, From: FromDescribed)) |
| 3268 | return std::move(Err); |
| 3269 | D2CXX->setDescribedClassTemplate(ToDescribed); |
| 3270 | if (!DCXX->isInjectedClassName() && !IsFriendTemplate) { |
| 3271 | // In a record describing a template the type should be an |
| 3272 | // InjectedClassNameType (see Sema::CheckClassTemplate). Update the |
| 3273 | // previously set type to the correct value here (ToDescribed is not |
| 3274 | // available at record create). |
| 3275 | CXXRecordDecl *Injected = nullptr; |
| 3276 | for (NamedDecl *Found : D2CXX->noload_lookup(Name)) { |
| 3277 | auto *Record = dyn_cast<CXXRecordDecl>(Found); |
| 3278 | if (Record && Record->isInjectedClassName()) { |
| 3279 | Injected = Record; |
| 3280 | break; |
| 3281 | } |
| 3282 | } |
| 3283 | // Create an injected type for the whole redecl chain. |
| 3284 | // The chain may contain an already existing injected type at the start, |
| 3285 | // if yes this should be reused. We must ensure that only one type |
| 3286 | // object exists for the injected type (including the injected record |
| 3287 | // declaration), ASTContext does not check it. |
| 3288 | SmallVector<Decl *, 2> Redecls = |
| 3289 | getCanonicalForwardRedeclChain(D2CXX); |
| 3290 | const Type *FrontTy = |
| 3291 | cast<CXXRecordDecl>(Val: Redecls.front())->getTypeForDecl(); |
| 3292 | QualType InjSpec; |
| 3293 | if (auto *InjTy = FrontTy->getAs<InjectedClassNameType>()) |
| 3294 | InjSpec = InjTy->getInjectedSpecializationType(); |
| 3295 | else |
| 3296 | InjSpec = ToDescribed->getInjectedClassNameSpecialization(); |
| 3297 | for (auto *R : Redecls) { |
| 3298 | auto *RI = cast<CXXRecordDecl>(R); |
| 3299 | if (R != Redecls.front() || |
| 3300 | !isa<InjectedClassNameType>(RI->getTypeForDecl())) |
| 3301 | RI->setTypeForDecl(nullptr); |
| 3302 | // This function tries to get the injected type from getTypeForDecl, |
| 3303 | // then from the previous declaration if possible. If not, it creates |
| 3304 | // a new type. |
| 3305 | Importer.getToContext().getInjectedClassNameType(RI, InjSpec); |
| 3306 | } |
| 3307 | // Set the new type for the injected decl too. |
| 3308 | if (Injected) { |
| 3309 | Injected->setTypeForDecl(nullptr); |
| 3310 | // This function will copy the injected type from D2CXX into Injected. |
| 3311 | // The injected decl does not have a previous decl to copy from. |
| 3312 | Importer.getToContext().getTypeDeclType(Injected, D2CXX); |
| 3313 | } |
| 3314 | } |
| 3315 | } else if (MemberSpecializationInfo *MemberInfo = |
| 3316 | DCXX->getMemberSpecializationInfo()) { |
| 3317 | TemplateSpecializationKind SK = |
| 3318 | MemberInfo->getTemplateSpecializationKind(); |
| 3319 | CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass(); |
| 3320 | |
| 3321 | if (Expected<CXXRecordDecl *> ToInstOrErr = import(From: FromInst)) |
| 3322 | D2CXX->setInstantiationOfMemberClass(RD: *ToInstOrErr, TSK: SK); |
| 3323 | else |
| 3324 | return ToInstOrErr.takeError(); |
| 3325 | |
| 3326 | if (ExpectedSLoc POIOrErr = |
| 3327 | import(From: MemberInfo->getPointOfInstantiation())) |
| 3328 | D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation( |
| 3329 | *POIOrErr); |
| 3330 | else |
| 3331 | return POIOrErr.takeError(); |
| 3332 | } |
| 3333 | |
| 3334 | } else { |
| 3335 | if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), |
| 3336 | D->getTagKind(), DC, *BeginLocOrErr, Loc, |
| 3337 | Name.getAsIdentifierInfo(), PrevDecl)) |
| 3338 | return D2; |
| 3339 | D2->setLexicalDeclContext(LexicalDC); |
| 3340 | addDeclToContexts(D, D2); |
| 3341 | } |
| 3342 | |
| 3343 | if (auto BraceRangeOrErr = import(D->getBraceRange())) |
| 3344 | D2->setBraceRange(*BraceRangeOrErr); |
| 3345 | else |
| 3346 | return BraceRangeOrErr.takeError(); |
| 3347 | if (auto QualifierLocOrErr = import(D->getQualifierLoc())) |
| 3348 | D2->setQualifierInfo(*QualifierLocOrErr); |
| 3349 | else |
| 3350 | return QualifierLocOrErr.takeError(); |
| 3351 | |
| 3352 | if (D->isAnonymousStructOrUnion()) |
| 3353 | D2->setAnonymousStructOrUnion(true); |
| 3354 | |
| 3355 | if (D->isCompleteDefinition()) |
| 3356 | if (Error Err = ImportDefinition(From: D, To: D2, Kind: IDK_Default)) |
| 3357 | return std::move(Err); |
| 3358 | |
| 3359 | return D2; |
| 3360 | } |
| 3361 | |
| 3362 | ExpectedDecl ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 3363 | // Import the major distinguishing characteristics of this enumerator. |
| 3364 | DeclContext *DC, *LexicalDC; |
| 3365 | DeclarationName Name; |
| 3366 | SourceLocation Loc; |
| 3367 | NamedDecl *ToD; |
| 3368 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 3369 | return std::move(Err); |
| 3370 | if (ToD) |
| 3371 | return ToD; |
| 3372 | |
| 3373 | // Determine whether there are any other declarations with the same name and |
| 3374 | // in the same context. |
| 3375 | if (!LexicalDC->isFunctionOrMethod()) { |
| 3376 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 3377 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 3378 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 3379 | for (auto *FoundDecl : FoundDecls) { |
| 3380 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) |
| 3381 | continue; |
| 3382 | |
| 3383 | if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(Val: FoundDecl)) { |
| 3384 | if (IsStructuralMatch(D, FoundEnumConstant)) |
| 3385 | return Importer.MapImported(D, FoundEnumConstant); |
| 3386 | ConflictingDecls.push_back(Elt: FoundDecl); |
| 3387 | } |
| 3388 | } |
| 3389 | |
| 3390 | if (!ConflictingDecls.empty()) { |
| 3391 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
| 3392 | Name, DC, IDNS, Decls: ConflictingDecls.data(), NumDecls: ConflictingDecls.size()); |
| 3393 | if (NameOrErr) |
| 3394 | Name = NameOrErr.get(); |
| 3395 | else |
| 3396 | return NameOrErr.takeError(); |
| 3397 | } |
| 3398 | } |
| 3399 | |
| 3400 | ExpectedType TypeOrErr = import(D->getType()); |
| 3401 | if (!TypeOrErr) |
| 3402 | return TypeOrErr.takeError(); |
| 3403 | |
| 3404 | ExpectedExpr InitOrErr = import(From: D->getInitExpr()); |
| 3405 | if (!InitOrErr) |
| 3406 | return InitOrErr.takeError(); |
| 3407 | |
| 3408 | EnumConstantDecl *ToEnumerator; |
| 3409 | if (GetImportedOrCreateDecl( |
| 3410 | ToD&: ToEnumerator, FromD: D, args&: Importer.getToContext(), args: cast<EnumDecl>(Val: DC), args&: Loc, |
| 3411 | args: Name.getAsIdentifierInfo(), args&: *TypeOrErr, args&: *InitOrErr, args: D->getInitVal())) |
| 3412 | return ToEnumerator; |
| 3413 | |
| 3414 | ToEnumerator->setAccess(D->getAccess()); |
| 3415 | ToEnumerator->setLexicalDeclContext(LexicalDC); |
| 3416 | LexicalDC->addDeclInternal(ToEnumerator); |
| 3417 | return ToEnumerator; |
| 3418 | } |
| 3419 | |
| 3420 | template <typename DeclTy> |
| 3421 | Error ASTNodeImporter::ImportTemplateParameterLists(const DeclTy *FromD, |
| 3422 | DeclTy *ToD) { |
| 3423 | unsigned int Num = FromD->getNumTemplateParameterLists(); |
| 3424 | if (Num == 0) |
| 3425 | return Error::success(); |
| 3426 | SmallVector<TemplateParameterList *, 2> ToTPLists(Num); |
| 3427 | for (unsigned int I = 0; I < Num; ++I) |
| 3428 | if (Expected<TemplateParameterList *> ToTPListOrErr = |
| 3429 | import(FromD->getTemplateParameterList(I))) |
| 3430 | ToTPLists[I] = *ToTPListOrErr; |
| 3431 | else |
| 3432 | return ToTPListOrErr.takeError(); |
| 3433 | ToD->setTemplateParameterListsInfo(Importer.ToContext, ToTPLists); |
| 3434 | return Error::success(); |
| 3435 | } |
| 3436 | |
| 3437 | Error ASTNodeImporter::ImportTemplateInformation( |
| 3438 | FunctionDecl *FromFD, FunctionDecl *ToFD) { |
| 3439 | switch (FromFD->getTemplatedKind()) { |
| 3440 | case FunctionDecl::TK_NonTemplate: |
| 3441 | case FunctionDecl::TK_FunctionTemplate: |
| 3442 | return Error::success(); |
| 3443 | |
| 3444 | case FunctionDecl::TK_DependentNonTemplate: |
| 3445 | if (Expected<FunctionDecl *> InstFDOrErr = |
| 3446 | import(From: FromFD->getInstantiatedFromDecl())) |
| 3447 | ToFD->setInstantiatedFromDecl(*InstFDOrErr); |
| 3448 | return Error::success(); |
| 3449 | case FunctionDecl::TK_MemberSpecialization: { |
| 3450 | TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind(); |
| 3451 | |
| 3452 | if (Expected<FunctionDecl *> InstFDOrErr = |
| 3453 | import(From: FromFD->getInstantiatedFromMemberFunction())) |
| 3454 | ToFD->setInstantiationOfMemberFunction(FD: *InstFDOrErr, TSK); |
| 3455 | else |
| 3456 | return InstFDOrErr.takeError(); |
| 3457 | |
| 3458 | if (ExpectedSLoc POIOrErr = import( |
| 3459 | From: FromFD->getMemberSpecializationInfo()->getPointOfInstantiation())) |
| 3460 | ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr); |
| 3461 | else |
| 3462 | return POIOrErr.takeError(); |
| 3463 | |
| 3464 | return Error::success(); |
| 3465 | } |
| 3466 | |
| 3467 | case FunctionDecl::TK_FunctionTemplateSpecialization: { |
| 3468 | auto FunctionAndArgsOrErr = |
| 3469 | ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD); |
| 3470 | if (!FunctionAndArgsOrErr) |
| 3471 | return FunctionAndArgsOrErr.takeError(); |
| 3472 | |
| 3473 | TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy( |
| 3474 | Context&: Importer.getToContext(), Args: std::get<1>(*FunctionAndArgsOrErr)); |
| 3475 | |
| 3476 | auto *FTSInfo = FromFD->getTemplateSpecializationInfo(); |
| 3477 | TemplateArgumentListInfo ToTAInfo; |
| 3478 | const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten; |
| 3479 | if (FromTAArgsAsWritten) |
| 3480 | if (Error Err = ImportTemplateArgumentListInfo( |
| 3481 | Container: *FromTAArgsAsWritten, ToTAInfo)) |
| 3482 | return Err; |
| 3483 | |
| 3484 | ExpectedSLoc POIOrErr = import(From: FTSInfo->getPointOfInstantiation()); |
| 3485 | if (!POIOrErr) |
| 3486 | return POIOrErr.takeError(); |
| 3487 | |
| 3488 | if (Error Err = ImportTemplateParameterLists(FromD: FromFD, ToD: ToFD)) |
| 3489 | return Err; |
| 3490 | |
| 3491 | TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind(); |
| 3492 | ToFD->setFunctionTemplateSpecialization( |
| 3493 | std::get<0>(*FunctionAndArgsOrErr), ToTAList, /* InsertPos= */ nullptr, |
| 3494 | TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, *POIOrErr); |
| 3495 | return Error::success(); |
| 3496 | } |
| 3497 | |
| 3498 | case FunctionDecl::TK_DependentFunctionTemplateSpecialization: { |
| 3499 | auto *FromInfo = FromFD->getDependentSpecializationInfo(); |
| 3500 | UnresolvedSet<8> Candidates; |
| 3501 | for (FunctionTemplateDecl *FTD : FromInfo->getCandidates()) { |
| 3502 | if (Expected<FunctionTemplateDecl *> ToFTDOrErr = import(From: FTD)) |
| 3503 | Candidates.addDecl(*ToFTDOrErr); |
| 3504 | else |
| 3505 | return ToFTDOrErr.takeError(); |
| 3506 | } |
| 3507 | |
| 3508 | // Import TemplateArgumentListInfo. |
| 3509 | TemplateArgumentListInfo ToTAInfo; |
| 3510 | const auto *FromTAArgsAsWritten = FromInfo->TemplateArgumentsAsWritten; |
| 3511 | if (FromTAArgsAsWritten) |
| 3512 | if (Error Err = |
| 3513 | ImportTemplateArgumentListInfo(Container: *FromTAArgsAsWritten, ToTAInfo)) |
| 3514 | return Err; |
| 3515 | |
| 3516 | ToFD->setDependentTemplateSpecialization( |
| 3517 | Context&: Importer.getToContext(), Templates: Candidates, |
| 3518 | TemplateArgs: FromTAArgsAsWritten ? &ToTAInfo : nullptr); |
| 3519 | return Error::success(); |
| 3520 | } |
| 3521 | } |
| 3522 | llvm_unreachable("All cases should be covered!" ); |
| 3523 | } |
| 3524 | |
| 3525 | Expected<FunctionDecl *> |
| 3526 | ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) { |
| 3527 | auto FunctionAndArgsOrErr = |
| 3528 | ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD); |
| 3529 | if (!FunctionAndArgsOrErr) |
| 3530 | return FunctionAndArgsOrErr.takeError(); |
| 3531 | |
| 3532 | FunctionTemplateDecl *Template; |
| 3533 | TemplateArgsTy ToTemplArgs; |
| 3534 | std::tie(args&: Template, args&: ToTemplArgs) = *FunctionAndArgsOrErr; |
| 3535 | void *InsertPos = nullptr; |
| 3536 | auto *FoundSpec = Template->findSpecialization(Args: ToTemplArgs, InsertPos); |
| 3537 | return FoundSpec; |
| 3538 | } |
| 3539 | |
| 3540 | Error ASTNodeImporter::ImportFunctionDeclBody(FunctionDecl *FromFD, |
| 3541 | FunctionDecl *ToFD) { |
| 3542 | if (Stmt *FromBody = FromFD->getBody()) { |
| 3543 | if (ExpectedStmt ToBodyOrErr = import(From: FromBody)) |
| 3544 | ToFD->setBody(*ToBodyOrErr); |
| 3545 | else |
| 3546 | return ToBodyOrErr.takeError(); |
| 3547 | } |
| 3548 | return Error::success(); |
| 3549 | } |
| 3550 | |
| 3551 | // Returns true if the given D has a DeclContext up to the TranslationUnitDecl |
| 3552 | // which is equal to the given DC, or D is equal to DC. |
| 3553 | static bool isAncestorDeclContextOf(const DeclContext *DC, const Decl *D) { |
| 3554 | const DeclContext *DCi = dyn_cast<DeclContext>(Val: D); |
| 3555 | if (!DCi) |
| 3556 | DCi = D->getDeclContext(); |
| 3557 | assert(DCi && "Declaration should have a context" ); |
| 3558 | while (DCi != D->getTranslationUnitDecl()) { |
| 3559 | if (DCi == DC) |
| 3560 | return true; |
| 3561 | DCi = DCi->getParent(); |
| 3562 | } |
| 3563 | return false; |
| 3564 | } |
| 3565 | |
| 3566 | // Check if there is a declaration that has 'DC' as parent context and is |
| 3567 | // referenced from statement 'S' or one of its children. The search is done in |
| 3568 | // BFS order through children of 'S'. |
| 3569 | static bool isAncestorDeclContextOf(const DeclContext *DC, const Stmt *S) { |
| 3570 | SmallVector<const Stmt *> ToProcess; |
| 3571 | ToProcess.push_back(Elt: S); |
| 3572 | while (!ToProcess.empty()) { |
| 3573 | const Stmt *CurrentS = ToProcess.pop_back_val(); |
| 3574 | ToProcess.append(in_start: CurrentS->child_begin(), in_end: CurrentS->child_end()); |
| 3575 | if (const auto *DeclRef = dyn_cast<DeclRefExpr>(Val: CurrentS)) { |
| 3576 | if (const Decl *D = DeclRef->getDecl()) |
| 3577 | if (isAncestorDeclContextOf(DC, D)) |
| 3578 | return true; |
| 3579 | } else if (const auto *E = |
| 3580 | dyn_cast_or_null<SubstNonTypeTemplateParmExpr>(Val: CurrentS)) { |
| 3581 | if (const Decl *D = E->getAssociatedDecl()) |
| 3582 | if (isAncestorDeclContextOf(DC, D)) |
| 3583 | return true; |
| 3584 | } |
| 3585 | } |
| 3586 | return false; |
| 3587 | } |
| 3588 | |
| 3589 | namespace { |
| 3590 | /// Check if a type has any reference to a declaration that is inside the body |
| 3591 | /// of a function. |
| 3592 | /// The \c CheckType(QualType) function should be used to determine |
| 3593 | /// this property. |
| 3594 | /// |
| 3595 | /// The type visitor visits one type object only (not recursive). |
| 3596 | /// To find all referenced declarations we must discover all type objects until |
| 3597 | /// the canonical type is reached (walk over typedef and similar objects). This |
| 3598 | /// is done by loop over all "sugar" type objects. For every such type we must |
| 3599 | /// check all declarations that are referenced from it. For this check the |
| 3600 | /// visitor is used. In the visit functions all referenced declarations except |
| 3601 | /// the one that follows in the sugar chain (if any) must be checked. For this |
| 3602 | /// check the same visitor is re-used (it has no state-dependent data). |
| 3603 | /// |
| 3604 | /// The visit functions have 3 possible return values: |
| 3605 | /// - True, found a declaration inside \c ParentDC. |
| 3606 | /// - False, found declarations only outside \c ParentDC and it is not possible |
| 3607 | /// to find more declarations (the "sugar" chain does not continue). |
| 3608 | /// - Empty optional value, found no declarations or only outside \c ParentDC, |
| 3609 | /// but it is possible to find more declarations in the type "sugar" chain. |
| 3610 | /// The loop over the "sugar" types can be implemented by using type visit |
| 3611 | /// functions only (call \c CheckType with the desugared type). With the current |
| 3612 | /// solution no visit function is needed if the type has only a desugared type |
| 3613 | /// as data. |
| 3614 | class IsTypeDeclaredInsideVisitor |
| 3615 | : public TypeVisitor<IsTypeDeclaredInsideVisitor, std::optional<bool>> { |
| 3616 | public: |
| 3617 | IsTypeDeclaredInsideVisitor(const FunctionDecl *ParentDC) |
| 3618 | : ParentDC(ParentDC) {} |
| 3619 | |
| 3620 | bool CheckType(QualType T) { |
| 3621 | // Check the chain of "sugar" types. |
| 3622 | // The "sugar" types are typedef or similar types that have the same |
| 3623 | // canonical type. |
| 3624 | if (std::optional<bool> Res = Visit(T: T.getTypePtr())) |
| 3625 | return *Res; |
| 3626 | QualType DsT = |
| 3627 | T.getSingleStepDesugaredType(Context: ParentDC->getParentASTContext()); |
| 3628 | while (DsT != T) { |
| 3629 | if (std::optional<bool> Res = Visit(T: DsT.getTypePtr())) |
| 3630 | return *Res; |
| 3631 | T = DsT; |
| 3632 | DsT = T.getSingleStepDesugaredType(Context: ParentDC->getParentASTContext()); |
| 3633 | } |
| 3634 | return false; |
| 3635 | } |
| 3636 | |
| 3637 | std::optional<bool> VisitTagType(const TagType *T) { |
| 3638 | if (auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(Val: T->getDecl())) |
| 3639 | for (const auto &Arg : Spec->getTemplateArgs().asArray()) |
| 3640 | if (checkTemplateArgument(Arg)) |
| 3641 | return true; |
| 3642 | return isAncestorDeclContextOf(ParentDC, T->getDecl()); |
| 3643 | } |
| 3644 | |
| 3645 | std::optional<bool> VisitPointerType(const PointerType *T) { |
| 3646 | return CheckType(T: T->getPointeeType()); |
| 3647 | } |
| 3648 | |
| 3649 | std::optional<bool> VisitReferenceType(const ReferenceType *T) { |
| 3650 | return CheckType(T: T->getPointeeTypeAsWritten()); |
| 3651 | } |
| 3652 | |
| 3653 | std::optional<bool> VisitTypedefType(const TypedefType *T) { |
| 3654 | const TypedefNameDecl *TD = T->getDecl(); |
| 3655 | assert(TD); |
| 3656 | return isAncestorDeclContextOf(ParentDC, TD); |
| 3657 | } |
| 3658 | |
| 3659 | std::optional<bool> VisitUsingType(const UsingType *T) { |
| 3660 | if (T->getFoundDecl() && |
| 3661 | isAncestorDeclContextOf(ParentDC, T->getFoundDecl())) |
| 3662 | return true; |
| 3663 | |
| 3664 | return {}; |
| 3665 | } |
| 3666 | |
| 3667 | std::optional<bool> |
| 3668 | VisitTemplateSpecializationType(const TemplateSpecializationType *T) { |
| 3669 | for (const auto &Arg : T->template_arguments()) |
| 3670 | if (checkTemplateArgument(Arg)) |
| 3671 | return true; |
| 3672 | // This type is a "sugar" to a record type, it can have a desugared type. |
| 3673 | return {}; |
| 3674 | } |
| 3675 | |
| 3676 | std::optional<bool> VisitUnaryTransformType(const UnaryTransformType *T) { |
| 3677 | return CheckType(T: T->getBaseType()); |
| 3678 | } |
| 3679 | |
| 3680 | std::optional<bool> |
| 3681 | VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
| 3682 | // The "associated declaration" can be the same as ParentDC. |
| 3683 | if (isAncestorDeclContextOf(DC: ParentDC, D: T->getAssociatedDecl())) |
| 3684 | return true; |
| 3685 | return {}; |
| 3686 | } |
| 3687 | |
| 3688 | std::optional<bool> VisitConstantArrayType(const ConstantArrayType *T) { |
| 3689 | if (T->getSizeExpr() && isAncestorDeclContextOf(ParentDC, T->getSizeExpr())) |
| 3690 | return true; |
| 3691 | |
| 3692 | return CheckType(T: T->getElementType()); |
| 3693 | } |
| 3694 | |
| 3695 | std::optional<bool> VisitVariableArrayType(const VariableArrayType *T) { |
| 3696 | llvm_unreachable( |
| 3697 | "Variable array should not occur in deduced return type of a function" ); |
| 3698 | } |
| 3699 | |
| 3700 | std::optional<bool> VisitIncompleteArrayType(const IncompleteArrayType *T) { |
| 3701 | llvm_unreachable("Incomplete array should not occur in deduced return type " |
| 3702 | "of a function" ); |
| 3703 | } |
| 3704 | |
| 3705 | std::optional<bool> VisitDependentArrayType(const IncompleteArrayType *T) { |
| 3706 | llvm_unreachable("Dependent array should not occur in deduced return type " |
| 3707 | "of a function" ); |
| 3708 | } |
| 3709 | |
| 3710 | private: |
| 3711 | const DeclContext *const ParentDC; |
| 3712 | |
| 3713 | bool checkTemplateArgument(const TemplateArgument &Arg) { |
| 3714 | switch (Arg.getKind()) { |
| 3715 | case TemplateArgument::Null: |
| 3716 | return false; |
| 3717 | case TemplateArgument::Integral: |
| 3718 | return CheckType(T: Arg.getIntegralType()); |
| 3719 | case TemplateArgument::Type: |
| 3720 | return CheckType(T: Arg.getAsType()); |
| 3721 | case TemplateArgument::Expression: |
| 3722 | return isAncestorDeclContextOf(ParentDC, Arg.getAsExpr()); |
| 3723 | case TemplateArgument::Declaration: |
| 3724 | // FIXME: The declaration in this case is not allowed to be in a function? |
| 3725 | return isAncestorDeclContextOf(ParentDC, Arg.getAsDecl()); |
| 3726 | case TemplateArgument::NullPtr: |
| 3727 | // FIXME: The type is not allowed to be in the function? |
| 3728 | return CheckType(T: Arg.getNullPtrType()); |
| 3729 | case TemplateArgument::StructuralValue: |
| 3730 | return CheckType(T: Arg.getStructuralValueType()); |
| 3731 | case TemplateArgument::Pack: |
| 3732 | for (const auto &PackArg : Arg.getPackAsArray()) |
| 3733 | if (checkTemplateArgument(Arg: PackArg)) |
| 3734 | return true; |
| 3735 | return false; |
| 3736 | case TemplateArgument::Template: |
| 3737 | // Templates can not be defined locally in functions. |
| 3738 | // A template passed as argument can be not in ParentDC. |
| 3739 | return false; |
| 3740 | case TemplateArgument::TemplateExpansion: |
| 3741 | // Templates can not be defined locally in functions. |
| 3742 | // A template passed as argument can be not in ParentDC. |
| 3743 | return false; |
| 3744 | } |
| 3745 | llvm_unreachable("Unknown TemplateArgument::ArgKind enum" ); |
| 3746 | }; |
| 3747 | }; |
| 3748 | } // namespace |
| 3749 | |
| 3750 | /// This function checks if the given function has a return type that contains |
| 3751 | /// a reference (in any way) to a declaration inside the same function. |
| 3752 | bool ASTNodeImporter::hasReturnTypeDeclaredInside(FunctionDecl *D) { |
| 3753 | QualType FromTy = D->getType(); |
| 3754 | const auto *FromFPT = FromTy->getAs<FunctionProtoType>(); |
| 3755 | assert(FromFPT && "Must be called on FunctionProtoType" ); |
| 3756 | |
| 3757 | auto IsCXX11Lambda = [&]() { |
| 3758 | if (Importer.FromContext.getLangOpts().CPlusPlus14) // C++14 or later |
| 3759 | return false; |
| 3760 | |
| 3761 | return isLambdaMethod(D); |
| 3762 | }; |
| 3763 | |
| 3764 | QualType RetT = FromFPT->getReturnType(); |
| 3765 | if (isa<AutoType>(Val: RetT.getTypePtr()) || IsCXX11Lambda()) { |
| 3766 | FunctionDecl *Def = D->getDefinition(); |
| 3767 | IsTypeDeclaredInsideVisitor Visitor(Def ? Def : D); |
| 3768 | return Visitor.CheckType(T: RetT); |
| 3769 | } |
| 3770 | |
| 3771 | return false; |
| 3772 | } |
| 3773 | |
| 3774 | ExplicitSpecifier |
| 3775 | ASTNodeImporter::importExplicitSpecifier(Error &Err, ExplicitSpecifier ESpec) { |
| 3776 | Expr *ExplicitExpr = ESpec.getExpr(); |
| 3777 | if (ExplicitExpr) |
| 3778 | ExplicitExpr = importChecked(Err, From: ESpec.getExpr()); |
| 3779 | return ExplicitSpecifier(ExplicitExpr, ESpec.getKind()); |
| 3780 | } |
| 3781 | |
| 3782 | ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) { |
| 3783 | |
| 3784 | SmallVector<Decl *, 2> Redecls = getCanonicalForwardRedeclChain(D); |
| 3785 | auto RedeclIt = Redecls.begin(); |
| 3786 | // Import the first part of the decl chain. I.e. import all previous |
| 3787 | // declarations starting from the canonical decl. |
| 3788 | for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) { |
| 3789 | ExpectedDecl ToRedeclOrErr = import(*RedeclIt); |
| 3790 | if (!ToRedeclOrErr) |
| 3791 | return ToRedeclOrErr.takeError(); |
| 3792 | } |
| 3793 | assert(*RedeclIt == D); |
| 3794 | |
| 3795 | // Import the major distinguishing characteristics of this function. |
| 3796 | DeclContext *DC, *LexicalDC; |
| 3797 | DeclarationName Name; |
| 3798 | SourceLocation Loc; |
| 3799 | NamedDecl *ToD; |
| 3800 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 3801 | return std::move(Err); |
| 3802 | if (ToD) |
| 3803 | return ToD; |
| 3804 | |
| 3805 | FunctionDecl *FoundByLookup = nullptr; |
| 3806 | FunctionTemplateDecl *FromFT = D->getDescribedFunctionTemplate(); |
| 3807 | |
| 3808 | // If this is a function template specialization, then try to find the same |
| 3809 | // existing specialization in the "to" context. The lookup below will not |
| 3810 | // find any specialization, but would find the primary template; thus, we |
| 3811 | // have to skip normal lookup in case of specializations. |
| 3812 | // FIXME handle member function templates (TK_MemberSpecialization) similarly? |
| 3813 | if (D->getTemplatedKind() == |
| 3814 | FunctionDecl::TK_FunctionTemplateSpecialization) { |
| 3815 | auto FoundFunctionOrErr = FindFunctionTemplateSpecialization(FromFD: D); |
| 3816 | if (!FoundFunctionOrErr) |
| 3817 | return FoundFunctionOrErr.takeError(); |
| 3818 | if (FunctionDecl *FoundFunction = *FoundFunctionOrErr) { |
| 3819 | if (Decl *Def = FindAndMapDefinition(D, FoundFunction)) |
| 3820 | return Def; |
| 3821 | FoundByLookup = FoundFunction; |
| 3822 | } |
| 3823 | } |
| 3824 | // Try to find a function in our own ("to") context with the same name, same |
| 3825 | // type, and in the same context as the function we're importing. |
| 3826 | else if (!LexicalDC->isFunctionOrMethod()) { |
| 3827 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 3828 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend; |
| 3829 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 3830 | for (auto *FoundDecl : FoundDecls) { |
| 3831 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) |
| 3832 | continue; |
| 3833 | |
| 3834 | if (auto *FoundFunction = dyn_cast<FunctionDecl>(Val: FoundDecl)) { |
| 3835 | if (!hasSameVisibilityContextAndLinkage(Found: FoundFunction, From: D)) |
| 3836 | continue; |
| 3837 | |
| 3838 | if (IsStructuralMatch(D, FoundFunction)) { |
| 3839 | if (Decl *Def = FindAndMapDefinition(D, FoundFunction)) |
| 3840 | return Def; |
| 3841 | FoundByLookup = FoundFunction; |
| 3842 | break; |
| 3843 | } |
| 3844 | // FIXME: Check for overloading more carefully, e.g., by boosting |
| 3845 | // Sema::IsOverload out to the AST library. |
| 3846 | |
| 3847 | // Function overloading is okay in C++. |
| 3848 | if (Importer.getToContext().getLangOpts().CPlusPlus) |
| 3849 | continue; |
| 3850 | |
| 3851 | // Complain about inconsistent function types. |
| 3852 | Importer.ToDiag(Loc, diag::warn_odr_function_type_inconsistent) |
| 3853 | << Name << D->getType() << FoundFunction->getType(); |
| 3854 | Importer.ToDiag(FoundFunction->getLocation(), diag::note_odr_value_here) |
| 3855 | << FoundFunction->getType(); |
| 3856 | ConflictingDecls.push_back(Elt: FoundDecl); |
| 3857 | } |
| 3858 | } |
| 3859 | |
| 3860 | if (!ConflictingDecls.empty()) { |
| 3861 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
| 3862 | Name, DC, IDNS, Decls: ConflictingDecls.data(), NumDecls: ConflictingDecls.size()); |
| 3863 | if (NameOrErr) |
| 3864 | Name = NameOrErr.get(); |
| 3865 | else |
| 3866 | return NameOrErr.takeError(); |
| 3867 | } |
| 3868 | } |
| 3869 | |
| 3870 | // We do not allow more than one in-class declaration of a function. This is |
| 3871 | // because AST clients like VTableBuilder asserts on this. VTableBuilder |
| 3872 | // assumes there is only one in-class declaration. Building a redecl |
| 3873 | // chain would result in more than one in-class declaration for |
| 3874 | // overrides (even if they are part of the same redecl chain inside the |
| 3875 | // derived class.) |
| 3876 | if (FoundByLookup) { |
| 3877 | if (isa<CXXMethodDecl>(Val: FoundByLookup)) { |
| 3878 | if (D->getLexicalDeclContext() == D->getDeclContext()) { |
| 3879 | if (!D->doesThisDeclarationHaveABody()) { |
| 3880 | if (FunctionTemplateDecl *DescribedD = |
| 3881 | D->getDescribedFunctionTemplate()) { |
| 3882 | // Handle a "templated" function together with its described |
| 3883 | // template. This avoids need for a similar check at import of the |
| 3884 | // described template. |
| 3885 | assert(FoundByLookup->getDescribedFunctionTemplate() && |
| 3886 | "Templated function mapped to non-templated?" ); |
| 3887 | Importer.MapImported(DescribedD, |
| 3888 | FoundByLookup->getDescribedFunctionTemplate()); |
| 3889 | } |
| 3890 | return Importer.MapImported(D, FoundByLookup); |
| 3891 | } else { |
| 3892 | // Let's continue and build up the redecl chain in this case. |
| 3893 | // FIXME Merge the functions into one decl. |
| 3894 | } |
| 3895 | } |
| 3896 | } |
| 3897 | } |
| 3898 | |
| 3899 | DeclarationNameInfo NameInfo(Name, Loc); |
| 3900 | // Import additional name location/type info. |
| 3901 | if (Error Err = ImportDeclarationNameLoc(From: D->getNameInfo(), To&: NameInfo)) |
| 3902 | return std::move(Err); |
| 3903 | |
| 3904 | QualType FromTy = D->getType(); |
| 3905 | TypeSourceInfo *FromTSI = D->getTypeSourceInfo(); |
| 3906 | // Set to true if we do not import the type of the function as is. There are |
| 3907 | // cases when the original type would result in an infinite recursion during |
| 3908 | // the import. To avoid an infinite recursion when importing, we create the |
| 3909 | // FunctionDecl with a simplified function type and update it only after the |
| 3910 | // relevant AST nodes are already imported. |
| 3911 | // The type is related to TypeSourceInfo (it references the type), so we must |
| 3912 | // do the same with TypeSourceInfo. |
| 3913 | bool UsedDifferentProtoType = false; |
| 3914 | if (const auto *FromFPT = FromTy->getAs<FunctionProtoType>()) { |
| 3915 | QualType FromReturnTy = FromFPT->getReturnType(); |
| 3916 | // Functions with auto return type may define a struct inside their body |
| 3917 | // and the return type could refer to that struct. |
| 3918 | // E.g.: auto foo() { struct X{}; return X(); } |
| 3919 | // To avoid an infinite recursion when importing, create the FunctionDecl |
| 3920 | // with a simplified return type. |
| 3921 | if (hasReturnTypeDeclaredInside(D)) { |
| 3922 | FromReturnTy = Importer.getFromContext().VoidTy; |
| 3923 | UsedDifferentProtoType = true; |
| 3924 | } |
| 3925 | FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo(); |
| 3926 | // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the |
| 3927 | // FunctionDecl that we are importing the FunctionProtoType for. |
| 3928 | // To avoid an infinite recursion when importing, create the FunctionDecl |
| 3929 | // with a simplified function type. |
| 3930 | if (FromEPI.ExceptionSpec.SourceDecl || |
| 3931 | FromEPI.ExceptionSpec.SourceTemplate || |
| 3932 | FromEPI.ExceptionSpec.NoexceptExpr) { |
| 3933 | FunctionProtoType::ExtProtoInfo DefaultEPI; |
| 3934 | FromEPI = DefaultEPI; |
| 3935 | UsedDifferentProtoType = true; |
| 3936 | } |
| 3937 | FromTy = Importer.getFromContext().getFunctionType( |
| 3938 | ResultTy: FromReturnTy, Args: FromFPT->getParamTypes(), EPI: FromEPI); |
| 3939 | FromTSI = Importer.getFromContext().getTrivialTypeSourceInfo( |
| 3940 | T: FromTy, Loc: D->getBeginLoc()); |
| 3941 | } |
| 3942 | |
| 3943 | Error Err = Error::success(); |
| 3944 | auto T = importChecked(Err, From: FromTy); |
| 3945 | auto TInfo = importChecked(Err, From: FromTSI); |
| 3946 | auto ToInnerLocStart = importChecked(Err, D->getInnerLocStart()); |
| 3947 | auto ToEndLoc = importChecked(Err, D->getEndLoc()); |
| 3948 | auto ToDefaultLoc = importChecked(Err, From: D->getDefaultLoc()); |
| 3949 | auto ToQualifierLoc = importChecked(Err, D->getQualifierLoc()); |
| 3950 | AssociatedConstraint TrailingRequiresClause = D->getTrailingRequiresClause(); |
| 3951 | TrailingRequiresClause.ConstraintExpr = |
| 3952 | importChecked(Err, From: TrailingRequiresClause.ConstraintExpr); |
| 3953 | if (Err) |
| 3954 | return std::move(Err); |
| 3955 | |
| 3956 | // Import the function parameters. |
| 3957 | SmallVector<ParmVarDecl *, 8> Parameters; |
| 3958 | for (auto *P : D->parameters()) { |
| 3959 | if (Expected<ParmVarDecl *> ToPOrErr = import(From: P)) |
| 3960 | Parameters.push_back(Elt: *ToPOrErr); |
| 3961 | else |
| 3962 | return ToPOrErr.takeError(); |
| 3963 | } |
| 3964 | |
| 3965 | // Create the imported function. |
| 3966 | FunctionDecl *ToFunction = nullptr; |
| 3967 | if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(Val: D)) { |
| 3968 | ExplicitSpecifier ESpec = |
| 3969 | importExplicitSpecifier(Err, ESpec: FromConstructor->getExplicitSpecifier()); |
| 3970 | if (Err) |
| 3971 | return std::move(Err); |
| 3972 | auto ToInheritedConstructor = InheritedConstructor(); |
| 3973 | if (FromConstructor->isInheritingConstructor()) { |
| 3974 | Expected<InheritedConstructor> ImportedInheritedCtor = |
| 3975 | import(From: FromConstructor->getInheritedConstructor()); |
| 3976 | if (!ImportedInheritedCtor) |
| 3977 | return ImportedInheritedCtor.takeError(); |
| 3978 | ToInheritedConstructor = *ImportedInheritedCtor; |
| 3979 | } |
| 3980 | if (GetImportedOrCreateDecl<CXXConstructorDecl>( |
| 3981 | ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(Val: DC), |
| 3982 | ToInnerLocStart, NameInfo, T, TInfo, ESpec, D->UsesFPIntrin(), |
| 3983 | D->isInlineSpecified(), D->isImplicit(), D->getConstexprKind(), |
| 3984 | ToInheritedConstructor, TrailingRequiresClause)) |
| 3985 | return ToFunction; |
| 3986 | } else if (CXXDestructorDecl *FromDtor = dyn_cast<CXXDestructorDecl>(Val: D)) { |
| 3987 | |
| 3988 | Error Err = Error::success(); |
| 3989 | auto ToOperatorDelete = importChecked( |
| 3990 | Err, From: const_cast<FunctionDecl *>(FromDtor->getOperatorDelete())); |
| 3991 | auto ToThisArg = importChecked(Err, From: FromDtor->getOperatorDeleteThisArg()); |
| 3992 | if (Err) |
| 3993 | return std::move(Err); |
| 3994 | |
| 3995 | if (GetImportedOrCreateDecl<CXXDestructorDecl>( |
| 3996 | ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(Val: DC), |
| 3997 | ToInnerLocStart, NameInfo, T, TInfo, D->UsesFPIntrin(), |
| 3998 | D->isInlineSpecified(), D->isImplicit(), D->getConstexprKind(), |
| 3999 | TrailingRequiresClause)) |
| 4000 | return ToFunction; |
| 4001 | |
| 4002 | CXXDestructorDecl *ToDtor = cast<CXXDestructorDecl>(Val: ToFunction); |
| 4003 | |
| 4004 | ToDtor->setOperatorDelete(OD: ToOperatorDelete, ThisArg: ToThisArg); |
| 4005 | } else if (CXXConversionDecl *FromConversion = |
| 4006 | dyn_cast<CXXConversionDecl>(Val: D)) { |
| 4007 | ExplicitSpecifier ESpec = |
| 4008 | importExplicitSpecifier(Err, ESpec: FromConversion->getExplicitSpecifier()); |
| 4009 | if (Err) |
| 4010 | return std::move(Err); |
| 4011 | if (GetImportedOrCreateDecl<CXXConversionDecl>( |
| 4012 | ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(Val: DC), |
| 4013 | ToInnerLocStart, NameInfo, T, TInfo, D->UsesFPIntrin(), |
| 4014 | D->isInlineSpecified(), ESpec, D->getConstexprKind(), |
| 4015 | SourceLocation(), TrailingRequiresClause)) |
| 4016 | return ToFunction; |
| 4017 | } else if (auto *Method = dyn_cast<CXXMethodDecl>(Val: D)) { |
| 4018 | if (GetImportedOrCreateDecl<CXXMethodDecl>( |
| 4019 | ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(Val: DC), |
| 4020 | ToInnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(), |
| 4021 | Method->UsesFPIntrin(), Method->isInlineSpecified(), |
| 4022 | D->getConstexprKind(), SourceLocation(), TrailingRequiresClause)) |
| 4023 | return ToFunction; |
| 4024 | } else if (auto *Guide = dyn_cast<CXXDeductionGuideDecl>(Val: D)) { |
| 4025 | ExplicitSpecifier ESpec = |
| 4026 | importExplicitSpecifier(Err, ESpec: Guide->getExplicitSpecifier()); |
| 4027 | CXXConstructorDecl *Ctor = |
| 4028 | importChecked(Err, From: Guide->getCorrespondingConstructor()); |
| 4029 | const CXXDeductionGuideDecl *SourceDG = |
| 4030 | importChecked(Err, From: Guide->getSourceDeductionGuide()); |
| 4031 | if (Err) |
| 4032 | return std::move(Err); |
| 4033 | if (GetImportedOrCreateDecl<CXXDeductionGuideDecl>( |
| 4034 | ToFunction, D, Importer.getToContext(), DC, ToInnerLocStart, ESpec, |
| 4035 | NameInfo, T, TInfo, ToEndLoc, Ctor, |
| 4036 | Guide->getDeductionCandidateKind(), TrailingRequiresClause, |
| 4037 | SourceDG, Guide->getSourceDeductionGuideKind())) |
| 4038 | return ToFunction; |
| 4039 | } else { |
| 4040 | if (GetImportedOrCreateDecl( |
| 4041 | ToFunction, D, Importer.getToContext(), DC, ToInnerLocStart, |
| 4042 | NameInfo, T, TInfo, D->getStorageClass(), D->UsesFPIntrin(), |
| 4043 | D->isInlineSpecified(), D->hasWrittenPrototype(), |
| 4044 | D->getConstexprKind(), TrailingRequiresClause)) |
| 4045 | return ToFunction; |
| 4046 | } |
| 4047 | |
| 4048 | // Connect the redecl chain. |
| 4049 | if (FoundByLookup) { |
| 4050 | auto *Recent = const_cast<FunctionDecl *>( |
| 4051 | FoundByLookup->getMostRecentDecl()); |
| 4052 | ToFunction->setPreviousDecl(Recent); |
| 4053 | // FIXME Probably we should merge exception specifications. E.g. In the |
| 4054 | // "To" context the existing function may have exception specification with |
| 4055 | // noexcept-unevaluated, while the newly imported function may have an |
| 4056 | // evaluated noexcept. A call to adjustExceptionSpec() on the imported |
| 4057 | // decl and its redeclarations may be required. |
| 4058 | } |
| 4059 | |
| 4060 | StringLiteral *Msg = D->getDeletedMessage(); |
| 4061 | if (Msg) { |
| 4062 | auto Imported = import(From: Msg); |
| 4063 | if (!Imported) |
| 4064 | return Imported.takeError(); |
| 4065 | Msg = *Imported; |
| 4066 | } |
| 4067 | |
| 4068 | ToFunction->setQualifierInfo(ToQualifierLoc); |
| 4069 | ToFunction->setAccess(D->getAccess()); |
| 4070 | ToFunction->setLexicalDeclContext(LexicalDC); |
| 4071 | ToFunction->setVirtualAsWritten(D->isVirtualAsWritten()); |
| 4072 | ToFunction->setTrivial(D->isTrivial()); |
| 4073 | ToFunction->setIsPureVirtual(D->isPureVirtual()); |
| 4074 | ToFunction->setDefaulted(D->isDefaulted()); |
| 4075 | ToFunction->setExplicitlyDefaulted(D->isExplicitlyDefaulted()); |
| 4076 | ToFunction->setDeletedAsWritten(D: D->isDeletedAsWritten()); |
| 4077 | ToFunction->setFriendConstraintRefersToEnclosingTemplate( |
| 4078 | D->FriendConstraintRefersToEnclosingTemplate()); |
| 4079 | ToFunction->setIsDestroyingOperatorDelete(D->isDestroyingOperatorDelete()); |
| 4080 | ToFunction->setIsTypeAwareOperatorNewOrDelete( |
| 4081 | D->isTypeAwareOperatorNewOrDelete()); |
| 4082 | ToFunction->setRangeEnd(ToEndLoc); |
| 4083 | ToFunction->setDefaultLoc(ToDefaultLoc); |
| 4084 | |
| 4085 | if (Msg) |
| 4086 | ToFunction->setDefaultedOrDeletedInfo( |
| 4087 | FunctionDecl::DefaultedOrDeletedFunctionInfo::Create( |
| 4088 | Context&: Importer.getToContext(), Lookups: {}, DeletedMessage: Msg)); |
| 4089 | |
| 4090 | // Set the parameters. |
| 4091 | for (auto *Param : Parameters) { |
| 4092 | Param->setOwningFunction(ToFunction); |
| 4093 | ToFunction->addDeclInternal(Param); |
| 4094 | if (ASTImporterLookupTable *LT = Importer.SharedState->getLookupTable()) |
| 4095 | LT->update(Param, Importer.getToContext().getTranslationUnitDecl()); |
| 4096 | } |
| 4097 | ToFunction->setParams(Parameters); |
| 4098 | |
| 4099 | // We need to complete creation of FunctionProtoTypeLoc manually with setting |
| 4100 | // params it refers to. |
| 4101 | if (TInfo) { |
| 4102 | if (auto ProtoLoc = |
| 4103 | TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) { |
| 4104 | for (unsigned I = 0, N = Parameters.size(); I != N; ++I) |
| 4105 | ProtoLoc.setParam(I, Parameters[I]); |
| 4106 | } |
| 4107 | } |
| 4108 | |
| 4109 | // Import the describing template function, if any. |
| 4110 | if (FromFT) { |
| 4111 | auto ToFTOrErr = import(From: FromFT); |
| 4112 | if (!ToFTOrErr) |
| 4113 | return ToFTOrErr.takeError(); |
| 4114 | } |
| 4115 | |
| 4116 | // Import Ctor initializers. |
| 4117 | if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(Val: D)) { |
| 4118 | if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) { |
| 4119 | SmallVector<CXXCtorInitializer *, 4> CtorInitializers(NumInitializers); |
| 4120 | // Import first, then allocate memory and copy if there was no error. |
| 4121 | if (Error Err = ImportContainerChecked( |
| 4122 | InContainer: FromConstructor->inits(), OutContainer&: CtorInitializers)) |
| 4123 | return std::move(Err); |
| 4124 | auto **Memory = |
| 4125 | new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers]; |
| 4126 | std::copy(first: CtorInitializers.begin(), last: CtorInitializers.end(), result: Memory); |
| 4127 | auto *ToCtor = cast<CXXConstructorDecl>(Val: ToFunction); |
| 4128 | ToCtor->setCtorInitializers(Memory); |
| 4129 | ToCtor->setNumCtorInitializers(NumInitializers); |
| 4130 | } |
| 4131 | } |
| 4132 | |
| 4133 | // If it is a template, import all related things. |
| 4134 | if (Error Err = ImportTemplateInformation(FromFD: D, ToFD: ToFunction)) |
| 4135 | return std::move(Err); |
| 4136 | |
| 4137 | if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(Val: D)) |
| 4138 | if (Error Err = ImportOverriddenMethods(ToMethod: cast<CXXMethodDecl>(Val: ToFunction), |
| 4139 | FromMethod: FromCXXMethod)) |
| 4140 | return std::move(Err); |
| 4141 | |
| 4142 | if (D->doesThisDeclarationHaveABody()) { |
| 4143 | Error Err = ImportFunctionDeclBody(FromFD: D, ToFD: ToFunction); |
| 4144 | |
| 4145 | if (Err) |
| 4146 | return std::move(Err); |
| 4147 | } |
| 4148 | |
| 4149 | // Import and set the original type in case we used another type. |
| 4150 | if (UsedDifferentProtoType) { |
| 4151 | if (ExpectedType TyOrErr = import(D->getType())) |
| 4152 | ToFunction->setType(*TyOrErr); |
| 4153 | else |
| 4154 | return TyOrErr.takeError(); |
| 4155 | if (Expected<TypeSourceInfo *> TSIOrErr = import(D->getTypeSourceInfo())) |
| 4156 | ToFunction->setTypeSourceInfo(*TSIOrErr); |
| 4157 | else |
| 4158 | return TSIOrErr.takeError(); |
| 4159 | } |
| 4160 | |
| 4161 | // FIXME: Other bits to merge? |
| 4162 | |
| 4163 | addDeclToContexts(D, ToFunction); |
| 4164 | |
| 4165 | // Import the rest of the chain. I.e. import all subsequent declarations. |
| 4166 | for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) { |
| 4167 | ExpectedDecl ToRedeclOrErr = import(*RedeclIt); |
| 4168 | if (!ToRedeclOrErr) |
| 4169 | return ToRedeclOrErr.takeError(); |
| 4170 | } |
| 4171 | |
| 4172 | return ToFunction; |
| 4173 | } |
| 4174 | |
| 4175 | ExpectedDecl ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) { |
| 4176 | return VisitFunctionDecl(D); |
| 4177 | } |
| 4178 | |
| 4179 | ExpectedDecl ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
| 4180 | return VisitCXXMethodDecl(D); |
| 4181 | } |
| 4182 | |
| 4183 | ExpectedDecl ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
| 4184 | return VisitCXXMethodDecl(D); |
| 4185 | } |
| 4186 | |
| 4187 | ExpectedDecl ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) { |
| 4188 | return VisitCXXMethodDecl(D); |
| 4189 | } |
| 4190 | |
| 4191 | ExpectedDecl |
| 4192 | ASTNodeImporter::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) { |
| 4193 | return VisitFunctionDecl(D); |
| 4194 | } |
| 4195 | |
| 4196 | ExpectedDecl ASTNodeImporter::VisitFieldDecl(FieldDecl *D) { |
| 4197 | // Import the major distinguishing characteristics of a variable. |
| 4198 | DeclContext *DC, *LexicalDC; |
| 4199 | DeclarationName Name; |
| 4200 | SourceLocation Loc; |
| 4201 | NamedDecl *ToD; |
| 4202 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 4203 | return std::move(Err); |
| 4204 | if (ToD) |
| 4205 | return ToD; |
| 4206 | |
| 4207 | // Determine whether we've already imported this field. |
| 4208 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 4209 | for (auto *FoundDecl : FoundDecls) { |
| 4210 | if (FieldDecl *FoundField = dyn_cast<FieldDecl>(Val: FoundDecl)) { |
| 4211 | // For anonymous fields, match up by index. |
| 4212 | if (!Name && |
| 4213 | ASTImporter::getFieldIndex(D) != |
| 4214 | ASTImporter::getFieldIndex(FoundField)) |
| 4215 | continue; |
| 4216 | |
| 4217 | if (Importer.IsStructurallyEquivalent(From: D->getType(), |
| 4218 | To: FoundField->getType())) { |
| 4219 | Importer.MapImported(D, FoundField); |
| 4220 | // In case of a FieldDecl of a ClassTemplateSpecializationDecl, the |
| 4221 | // initializer of a FieldDecl might not had been instantiated in the |
| 4222 | // "To" context. However, the "From" context might instantiated that, |
| 4223 | // thus we have to merge that. |
| 4224 | // Note: `hasInClassInitializer()` is not the same as non-null |
| 4225 | // `getInClassInitializer()` value. |
| 4226 | if (Expr *FromInitializer = D->getInClassInitializer()) { |
| 4227 | if (ExpectedExpr ToInitializerOrErr = import(From: FromInitializer)) { |
| 4228 | // Import of the FromInitializer may result in the setting of |
| 4229 | // InClassInitializer. If not, set it here. |
| 4230 | assert(FoundField->hasInClassInitializer() && |
| 4231 | "Field should have an in-class initializer if it has an " |
| 4232 | "expression for it." ); |
| 4233 | if (!FoundField->getInClassInitializer()) |
| 4234 | FoundField->setInClassInitializer(*ToInitializerOrErr); |
| 4235 | } else { |
| 4236 | return ToInitializerOrErr.takeError(); |
| 4237 | } |
| 4238 | } |
| 4239 | return FoundField; |
| 4240 | } |
| 4241 | |
| 4242 | // FIXME: Why is this case not handled with calling HandleNameConflict? |
| 4243 | Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent) |
| 4244 | << Name << D->getType() << FoundField->getType(); |
| 4245 | Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) |
| 4246 | << FoundField->getType(); |
| 4247 | |
| 4248 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 4249 | } |
| 4250 | } |
| 4251 | |
| 4252 | Error Err = Error::success(); |
| 4253 | auto ToType = importChecked(Err, D->getType()); |
| 4254 | auto ToTInfo = importChecked(Err, D->getTypeSourceInfo()); |
| 4255 | auto ToBitWidth = importChecked(Err, From: D->getBitWidth()); |
| 4256 | auto ToInnerLocStart = importChecked(Err, D->getInnerLocStart()); |
| 4257 | if (Err) |
| 4258 | return std::move(Err); |
| 4259 | const Type *ToCapturedVLAType = nullptr; |
| 4260 | if (Error Err = Importer.importInto( |
| 4261 | To&: ToCapturedVLAType, From: cast_or_null<Type>(Val: D->getCapturedVLAType()))) |
| 4262 | return std::move(Err); |
| 4263 | |
| 4264 | FieldDecl *ToField; |
| 4265 | if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC, |
| 4266 | ToInnerLocStart, Loc, Name.getAsIdentifierInfo(), |
| 4267 | ToType, ToTInfo, ToBitWidth, D->isMutable(), |
| 4268 | D->getInClassInitStyle())) |
| 4269 | return ToField; |
| 4270 | |
| 4271 | ToField->setAccess(D->getAccess()); |
| 4272 | ToField->setLexicalDeclContext(LexicalDC); |
| 4273 | ToField->setImplicit(D->isImplicit()); |
| 4274 | if (ToCapturedVLAType) |
| 4275 | ToField->setCapturedVLAType(cast<VariableArrayType>(Val: ToCapturedVLAType)); |
| 4276 | LexicalDC->addDeclInternal(ToField); |
| 4277 | // Import initializer only after the field was created, it may have recursive |
| 4278 | // reference to the field. |
| 4279 | auto ToInitializer = importChecked(Err, From: D->getInClassInitializer()); |
| 4280 | if (Err) |
| 4281 | return std::move(Err); |
| 4282 | if (ToInitializer) { |
| 4283 | auto *AlreadyImported = ToField->getInClassInitializer(); |
| 4284 | if (AlreadyImported) |
| 4285 | assert(ToInitializer == AlreadyImported && |
| 4286 | "Duplicate import of in-class initializer." ); |
| 4287 | else |
| 4288 | ToField->setInClassInitializer(ToInitializer); |
| 4289 | } |
| 4290 | |
| 4291 | return ToField; |
| 4292 | } |
| 4293 | |
| 4294 | ExpectedDecl ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) { |
| 4295 | // Import the major distinguishing characteristics of a variable. |
| 4296 | DeclContext *DC, *LexicalDC; |
| 4297 | DeclarationName Name; |
| 4298 | SourceLocation Loc; |
| 4299 | NamedDecl *ToD; |
| 4300 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 4301 | return std::move(Err); |
| 4302 | if (ToD) |
| 4303 | return ToD; |
| 4304 | |
| 4305 | // Determine whether we've already imported this field. |
| 4306 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 4307 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 4308 | if (auto *FoundField = dyn_cast<IndirectFieldDecl>(Val: FoundDecls[I])) { |
| 4309 | // For anonymous indirect fields, match up by index. |
| 4310 | if (!Name && |
| 4311 | ASTImporter::getFieldIndex(D) != |
| 4312 | ASTImporter::getFieldIndex(FoundField)) |
| 4313 | continue; |
| 4314 | |
| 4315 | if (Importer.IsStructurallyEquivalent(From: D->getType(), |
| 4316 | To: FoundField->getType(), |
| 4317 | Complain: !Name.isEmpty())) { |
| 4318 | Importer.MapImported(D, FoundField); |
| 4319 | return FoundField; |
| 4320 | } |
| 4321 | |
| 4322 | // If there are more anonymous fields to check, continue. |
| 4323 | if (!Name && I < N-1) |
| 4324 | continue; |
| 4325 | |
| 4326 | // FIXME: Why is this case not handled with calling HandleNameConflict? |
| 4327 | Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent) |
| 4328 | << Name << D->getType() << FoundField->getType(); |
| 4329 | Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) |
| 4330 | << FoundField->getType(); |
| 4331 | |
| 4332 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 4333 | } |
| 4334 | } |
| 4335 | |
| 4336 | // Import the type. |
| 4337 | auto TypeOrErr = import(D->getType()); |
| 4338 | if (!TypeOrErr) |
| 4339 | return TypeOrErr.takeError(); |
| 4340 | |
| 4341 | auto **NamedChain = |
| 4342 | new (Importer.getToContext()) NamedDecl*[D->getChainingSize()]; |
| 4343 | |
| 4344 | unsigned i = 0; |
| 4345 | for (auto *PI : D->chain()) |
| 4346 | if (Expected<NamedDecl *> ToD = import(From: PI)) |
| 4347 | NamedChain[i++] = *ToD; |
| 4348 | else |
| 4349 | return ToD.takeError(); |
| 4350 | |
| 4351 | llvm::MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()}; |
| 4352 | IndirectFieldDecl *ToIndirectField; |
| 4353 | if (GetImportedOrCreateDecl(ToIndirectField, D, Importer.getToContext(), DC, |
| 4354 | Loc, Name.getAsIdentifierInfo(), *TypeOrErr, CH)) |
| 4355 | // FIXME here we leak `NamedChain` which is allocated before |
| 4356 | return ToIndirectField; |
| 4357 | |
| 4358 | ToIndirectField->setAccess(D->getAccess()); |
| 4359 | ToIndirectField->setLexicalDeclContext(LexicalDC); |
| 4360 | LexicalDC->addDeclInternal(ToIndirectField); |
| 4361 | return ToIndirectField; |
| 4362 | } |
| 4363 | |
| 4364 | /// Used as return type of getFriendCountAndPosition. |
| 4365 | struct FriendCountAndPosition { |
| 4366 | /// Number of similar looking friends. |
| 4367 | unsigned int TotalCount; |
| 4368 | /// Index of the specific FriendDecl. |
| 4369 | unsigned int IndexOfDecl; |
| 4370 | }; |
| 4371 | |
| 4372 | static bool IsEquivalentFriend(ASTImporter &Importer, FriendDecl *FD1, |
| 4373 | FriendDecl *FD2) { |
| 4374 | if ((!FD1->getFriendType()) != (!FD2->getFriendType())) |
| 4375 | return false; |
| 4376 | |
| 4377 | if (const TypeSourceInfo *TSI = FD1->getFriendType()) |
| 4378 | return Importer.IsStructurallyEquivalent( |
| 4379 | From: TSI->getType(), To: FD2->getFriendType()->getType(), /*Complain=*/false); |
| 4380 | |
| 4381 | ASTImporter::NonEquivalentDeclSet NonEquivalentDecls; |
| 4382 | StructuralEquivalenceContext Ctx( |
| 4383 | Importer.getToContext().getLangOpts(), FD1->getASTContext(), |
| 4384 | FD2->getASTContext(), NonEquivalentDecls, |
| 4385 | StructuralEquivalenceKind::Default, |
| 4386 | /* StrictTypeSpelling = */ false, /* Complain = */ false); |
| 4387 | return Ctx.IsEquivalent(FD1, FD2); |
| 4388 | } |
| 4389 | |
| 4390 | static FriendCountAndPosition getFriendCountAndPosition(ASTImporter &Importer, |
| 4391 | FriendDecl *FD) { |
| 4392 | unsigned int FriendCount = 0; |
| 4393 | UnsignedOrNone FriendPosition = std::nullopt; |
| 4394 | const auto *RD = cast<CXXRecordDecl>(FD->getLexicalDeclContext()); |
| 4395 | |
| 4396 | for (FriendDecl *FoundFriend : RD->friends()) { |
| 4397 | if (FoundFriend == FD) { |
| 4398 | FriendPosition = FriendCount; |
| 4399 | ++FriendCount; |
| 4400 | } else if (IsEquivalentFriend(Importer, FD, FoundFriend)) { |
| 4401 | ++FriendCount; |
| 4402 | } |
| 4403 | } |
| 4404 | |
| 4405 | assert(FriendPosition && "Friend decl not found in own parent." ); |
| 4406 | |
| 4407 | return {.TotalCount: FriendCount, .IndexOfDecl: *FriendPosition}; |
| 4408 | } |
| 4409 | |
| 4410 | ExpectedDecl ASTNodeImporter::VisitFriendDecl(FriendDecl *D) { |
| 4411 | // Import the major distinguishing characteristics of a declaration. |
| 4412 | DeclContext *DC, *LexicalDC; |
| 4413 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) |
| 4414 | return std::move(Err); |
| 4415 | |
| 4416 | // Determine whether we've already imported this decl. |
| 4417 | // FriendDecl is not a NamedDecl so we cannot use lookup. |
| 4418 | // We try to maintain order and count of redundant friend declarations. |
| 4419 | const auto *RD = cast<CXXRecordDecl>(Val: DC); |
| 4420 | SmallVector<FriendDecl *, 2> ImportedEquivalentFriends; |
| 4421 | for (FriendDecl *ImportedFriend : RD->friends()) |
| 4422 | if (IsEquivalentFriend(Importer, FD1: D, FD2: ImportedFriend)) |
| 4423 | ImportedEquivalentFriends.push_back(Elt: ImportedFriend); |
| 4424 | |
| 4425 | FriendCountAndPosition CountAndPosition = |
| 4426 | getFriendCountAndPosition(Importer, FD: D); |
| 4427 | |
| 4428 | assert(ImportedEquivalentFriends.size() <= CountAndPosition.TotalCount && |
| 4429 | "Class with non-matching friends is imported, ODR check wrong?" ); |
| 4430 | if (ImportedEquivalentFriends.size() == CountAndPosition.TotalCount) |
| 4431 | return Importer.MapImported( |
| 4432 | D, ImportedEquivalentFriends[CountAndPosition.IndexOfDecl]); |
| 4433 | |
| 4434 | // Not found. Create it. |
| 4435 | // The declarations will be put into order later by ImportDeclContext. |
| 4436 | FriendDecl::FriendUnion ToFU; |
| 4437 | if (NamedDecl *FriendD = D->getFriendDecl()) { |
| 4438 | NamedDecl *ToFriendD; |
| 4439 | if (Error Err = importInto(To&: ToFriendD, From: FriendD)) |
| 4440 | return std::move(Err); |
| 4441 | |
| 4442 | if (FriendD->getFriendObjectKind() != Decl::FOK_None && |
| 4443 | !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator))) |
| 4444 | ToFriendD->setObjectOfFriendDecl(false); |
| 4445 | |
| 4446 | ToFU = ToFriendD; |
| 4447 | } else { // The friend is a type, not a decl. |
| 4448 | if (auto TSIOrErr = import(From: D->getFriendType())) |
| 4449 | ToFU = *TSIOrErr; |
| 4450 | else |
| 4451 | return TSIOrErr.takeError(); |
| 4452 | } |
| 4453 | |
| 4454 | SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists); |
| 4455 | auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>(); |
| 4456 | for (unsigned I = 0; I < D->NumTPLists; I++) { |
| 4457 | if (auto ListOrErr = import(FromTPLists[I])) |
| 4458 | ToTPLists[I] = *ListOrErr; |
| 4459 | else |
| 4460 | return ListOrErr.takeError(); |
| 4461 | } |
| 4462 | |
| 4463 | auto LocationOrErr = import(D->getLocation()); |
| 4464 | if (!LocationOrErr) |
| 4465 | return LocationOrErr.takeError(); |
| 4466 | auto FriendLocOrErr = import(From: D->getFriendLoc()); |
| 4467 | if (!FriendLocOrErr) |
| 4468 | return FriendLocOrErr.takeError(); |
| 4469 | auto EllipsisLocOrErr = import(From: D->getEllipsisLoc()); |
| 4470 | if (!EllipsisLocOrErr) |
| 4471 | return EllipsisLocOrErr.takeError(); |
| 4472 | |
| 4473 | FriendDecl *FrD; |
| 4474 | if (GetImportedOrCreateDecl(FrD, D, Importer.getToContext(), DC, |
| 4475 | *LocationOrErr, ToFU, *FriendLocOrErr, |
| 4476 | *EllipsisLocOrErr, ToTPLists)) |
| 4477 | return FrD; |
| 4478 | |
| 4479 | FrD->setAccess(D->getAccess()); |
| 4480 | FrD->setLexicalDeclContext(LexicalDC); |
| 4481 | LexicalDC->addDeclInternal(FrD); |
| 4482 | return FrD; |
| 4483 | } |
| 4484 | |
| 4485 | ExpectedDecl ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) { |
| 4486 | // Import the major distinguishing characteristics of an ivar. |
| 4487 | DeclContext *DC, *LexicalDC; |
| 4488 | DeclarationName Name; |
| 4489 | SourceLocation Loc; |
| 4490 | NamedDecl *ToD; |
| 4491 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 4492 | return std::move(Err); |
| 4493 | if (ToD) |
| 4494 | return ToD; |
| 4495 | |
| 4496 | // Determine whether we've already imported this ivar |
| 4497 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 4498 | for (auto *FoundDecl : FoundDecls) { |
| 4499 | if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(Val: FoundDecl)) { |
| 4500 | if (Importer.IsStructurallyEquivalent(From: D->getType(), |
| 4501 | To: FoundIvar->getType())) { |
| 4502 | Importer.MapImported(D, FoundIvar); |
| 4503 | return FoundIvar; |
| 4504 | } |
| 4505 | |
| 4506 | Importer.ToDiag(Loc, diag::warn_odr_ivar_type_inconsistent) |
| 4507 | << Name << D->getType() << FoundIvar->getType(); |
| 4508 | Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here) |
| 4509 | << FoundIvar->getType(); |
| 4510 | |
| 4511 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 4512 | } |
| 4513 | } |
| 4514 | |
| 4515 | Error Err = Error::success(); |
| 4516 | auto ToType = importChecked(Err, D->getType()); |
| 4517 | auto ToTypeSourceInfo = importChecked(Err, D->getTypeSourceInfo()); |
| 4518 | auto ToBitWidth = importChecked(Err, D->getBitWidth()); |
| 4519 | auto ToInnerLocStart = importChecked(Err, D->getInnerLocStart()); |
| 4520 | if (Err) |
| 4521 | return std::move(Err); |
| 4522 | |
| 4523 | ObjCIvarDecl *ToIvar; |
| 4524 | if (GetImportedOrCreateDecl( |
| 4525 | ToIvar, D, Importer.getToContext(), cast<ObjCContainerDecl>(Val: DC), |
| 4526 | ToInnerLocStart, Loc, Name.getAsIdentifierInfo(), |
| 4527 | ToType, ToTypeSourceInfo, |
| 4528 | D->getAccessControl(),ToBitWidth, D->getSynthesize())) |
| 4529 | return ToIvar; |
| 4530 | |
| 4531 | ToIvar->setLexicalDeclContext(LexicalDC); |
| 4532 | LexicalDC->addDeclInternal(ToIvar); |
| 4533 | return ToIvar; |
| 4534 | } |
| 4535 | |
| 4536 | ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) { |
| 4537 | |
| 4538 | SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D); |
| 4539 | auto RedeclIt = Redecls.begin(); |
| 4540 | // Import the first part of the decl chain. I.e. import all previous |
| 4541 | // declarations starting from the canonical decl. |
| 4542 | for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) { |
| 4543 | ExpectedDecl RedeclOrErr = import(*RedeclIt); |
| 4544 | if (!RedeclOrErr) |
| 4545 | return RedeclOrErr.takeError(); |
| 4546 | } |
| 4547 | assert(*RedeclIt == D); |
| 4548 | |
| 4549 | // Import the major distinguishing characteristics of a variable. |
| 4550 | DeclContext *DC, *LexicalDC; |
| 4551 | DeclarationName Name; |
| 4552 | SourceLocation Loc; |
| 4553 | NamedDecl *ToD; |
| 4554 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 4555 | return std::move(Err); |
| 4556 | if (ToD) |
| 4557 | return ToD; |
| 4558 | |
| 4559 | // Try to find a variable in our own ("to") context with the same name and |
| 4560 | // in the same context as the variable we're importing. |
| 4561 | VarDecl *FoundByLookup = nullptr; |
| 4562 | if (D->isFileVarDecl()) { |
| 4563 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 4564 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 4565 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 4566 | for (auto *FoundDecl : FoundDecls) { |
| 4567 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) |
| 4568 | continue; |
| 4569 | |
| 4570 | if (auto *FoundVar = dyn_cast<VarDecl>(Val: FoundDecl)) { |
| 4571 | if (!hasSameVisibilityContextAndLinkage(Found: FoundVar, From: D)) |
| 4572 | continue; |
| 4573 | if (Importer.IsStructurallyEquivalent(From: D->getType(), |
| 4574 | To: FoundVar->getType())) { |
| 4575 | |
| 4576 | // The VarDecl in the "From" context has a definition, but in the |
| 4577 | // "To" context we already have a definition. |
| 4578 | VarDecl *FoundDef = FoundVar->getDefinition(); |
| 4579 | if (D->isThisDeclarationADefinition() && FoundDef) |
| 4580 | // FIXME Check for ODR error if the two definitions have |
| 4581 | // different initializers? |
| 4582 | return Importer.MapImported(D, FoundDef); |
| 4583 | |
| 4584 | // The VarDecl in the "From" context has an initializer, but in the |
| 4585 | // "To" context we already have an initializer. |
| 4586 | const VarDecl *FoundDInit = nullptr; |
| 4587 | if (D->getInit() && FoundVar->getAnyInitializer(D&: FoundDInit)) |
| 4588 | // FIXME Diagnose ODR error if the two initializers are different? |
| 4589 | return Importer.MapImported(D, const_cast<VarDecl*>(FoundDInit)); |
| 4590 | |
| 4591 | FoundByLookup = FoundVar; |
| 4592 | break; |
| 4593 | } |
| 4594 | |
| 4595 | const ArrayType *FoundArray |
| 4596 | = Importer.getToContext().getAsArrayType(T: FoundVar->getType()); |
| 4597 | const ArrayType *TArray |
| 4598 | = Importer.getToContext().getAsArrayType(T: D->getType()); |
| 4599 | if (FoundArray && TArray) { |
| 4600 | if (isa<IncompleteArrayType>(Val: FoundArray) && |
| 4601 | isa<ConstantArrayType>(Val: TArray)) { |
| 4602 | // Import the type. |
| 4603 | if (auto TyOrErr = import(D->getType())) |
| 4604 | FoundVar->setType(*TyOrErr); |
| 4605 | else |
| 4606 | return TyOrErr.takeError(); |
| 4607 | |
| 4608 | FoundByLookup = FoundVar; |
| 4609 | break; |
| 4610 | } else if (isa<IncompleteArrayType>(Val: TArray) && |
| 4611 | isa<ConstantArrayType>(Val: FoundArray)) { |
| 4612 | FoundByLookup = FoundVar; |
| 4613 | break; |
| 4614 | } |
| 4615 | } |
| 4616 | |
| 4617 | Importer.ToDiag(Loc, diag::warn_odr_variable_type_inconsistent) |
| 4618 | << Name << D->getType() << FoundVar->getType(); |
| 4619 | Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here) |
| 4620 | << FoundVar->getType(); |
| 4621 | ConflictingDecls.push_back(Elt: FoundDecl); |
| 4622 | } |
| 4623 | } |
| 4624 | |
| 4625 | if (!ConflictingDecls.empty()) { |
| 4626 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
| 4627 | Name, DC, IDNS, Decls: ConflictingDecls.data(), NumDecls: ConflictingDecls.size()); |
| 4628 | if (NameOrErr) |
| 4629 | Name = NameOrErr.get(); |
| 4630 | else |
| 4631 | return NameOrErr.takeError(); |
| 4632 | } |
| 4633 | } |
| 4634 | |
| 4635 | Error Err = Error::success(); |
| 4636 | auto ToType = importChecked(Err, D->getType()); |
| 4637 | auto ToTypeSourceInfo = importChecked(Err, D->getTypeSourceInfo()); |
| 4638 | auto ToInnerLocStart = importChecked(Err, D->getInnerLocStart()); |
| 4639 | auto ToQualifierLoc = importChecked(Err, D->getQualifierLoc()); |
| 4640 | if (Err) |
| 4641 | return std::move(Err); |
| 4642 | |
| 4643 | VarDecl *ToVar; |
| 4644 | if (auto *FromDecomp = dyn_cast<DecompositionDecl>(Val: D)) { |
| 4645 | SmallVector<BindingDecl *> Bindings(FromDecomp->bindings().size()); |
| 4646 | if (Error Err = |
| 4647 | ImportArrayChecked(InContainer: FromDecomp->bindings(), Obegin: Bindings.begin())) |
| 4648 | return std::move(Err); |
| 4649 | DecompositionDecl *ToDecomp; |
| 4650 | if (GetImportedOrCreateDecl( |
| 4651 | ToDecomp, FromDecomp, Importer.getToContext(), DC, ToInnerLocStart, |
| 4652 | Loc, ToType, ToTypeSourceInfo, D->getStorageClass(), Bindings)) |
| 4653 | return ToDecomp; |
| 4654 | ToVar = ToDecomp; |
| 4655 | } else { |
| 4656 | // Create the imported variable. |
| 4657 | if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC, |
| 4658 | ToInnerLocStart, Loc, |
| 4659 | Name.getAsIdentifierInfo(), ToType, |
| 4660 | ToTypeSourceInfo, D->getStorageClass())) |
| 4661 | return ToVar; |
| 4662 | } |
| 4663 | |
| 4664 | ToVar->setTSCSpec(D->getTSCSpec()); |
| 4665 | ToVar->setQualifierInfo(ToQualifierLoc); |
| 4666 | ToVar->setAccess(D->getAccess()); |
| 4667 | ToVar->setLexicalDeclContext(LexicalDC); |
| 4668 | if (D->isInlineSpecified()) |
| 4669 | ToVar->setInlineSpecified(); |
| 4670 | if (D->isInline()) |
| 4671 | ToVar->setImplicitlyInline(); |
| 4672 | |
| 4673 | if (FoundByLookup) { |
| 4674 | auto *Recent = const_cast<VarDecl *>(FoundByLookup->getMostRecentDecl()); |
| 4675 | ToVar->setPreviousDecl(Recent); |
| 4676 | } |
| 4677 | |
| 4678 | // Import the described template, if any. |
| 4679 | if (D->getDescribedVarTemplate()) { |
| 4680 | auto ToVTOrErr = import(From: D->getDescribedVarTemplate()); |
| 4681 | if (!ToVTOrErr) |
| 4682 | return ToVTOrErr.takeError(); |
| 4683 | } else if (MemberSpecializationInfo *MSI = D->getMemberSpecializationInfo()) { |
| 4684 | TemplateSpecializationKind SK = MSI->getTemplateSpecializationKind(); |
| 4685 | VarDecl *FromInst = D->getInstantiatedFromStaticDataMember(); |
| 4686 | if (Expected<VarDecl *> ToInstOrErr = import(From: FromInst)) |
| 4687 | ToVar->setInstantiationOfStaticDataMember(VD: *ToInstOrErr, TSK: SK); |
| 4688 | else |
| 4689 | return ToInstOrErr.takeError(); |
| 4690 | if (ExpectedSLoc POIOrErr = import(From: MSI->getPointOfInstantiation())) |
| 4691 | ToVar->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr); |
| 4692 | else |
| 4693 | return POIOrErr.takeError(); |
| 4694 | } |
| 4695 | |
| 4696 | if (Error Err = ImportInitializer(From: D, To: ToVar)) |
| 4697 | return std::move(Err); |
| 4698 | |
| 4699 | if (D->isConstexpr()) |
| 4700 | ToVar->setConstexpr(true); |
| 4701 | |
| 4702 | addDeclToContexts(D, ToVar); |
| 4703 | |
| 4704 | // Import the rest of the chain. I.e. import all subsequent declarations. |
| 4705 | for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) { |
| 4706 | ExpectedDecl RedeclOrErr = import(*RedeclIt); |
| 4707 | if (!RedeclOrErr) |
| 4708 | return RedeclOrErr.takeError(); |
| 4709 | } |
| 4710 | |
| 4711 | return ToVar; |
| 4712 | } |
| 4713 | |
| 4714 | ExpectedDecl ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) { |
| 4715 | // Parameters are created in the translation unit's context, then moved |
| 4716 | // into the function declaration's context afterward. |
| 4717 | DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); |
| 4718 | |
| 4719 | Error Err = Error::success(); |
| 4720 | auto ToDeclName = importChecked(Err, D->getDeclName()); |
| 4721 | auto ToLocation = importChecked(Err, D->getLocation()); |
| 4722 | auto ToType = importChecked(Err, D->getType()); |
| 4723 | if (Err) |
| 4724 | return std::move(Err); |
| 4725 | |
| 4726 | // Create the imported parameter. |
| 4727 | ImplicitParamDecl *ToParm = nullptr; |
| 4728 | if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC, |
| 4729 | ToLocation, ToDeclName.getAsIdentifierInfo(), |
| 4730 | ToType, D->getParameterKind())) |
| 4731 | return ToParm; |
| 4732 | return ToParm; |
| 4733 | } |
| 4734 | |
| 4735 | Error ASTNodeImporter::ImportDefaultArgOfParmVarDecl( |
| 4736 | const ParmVarDecl *FromParam, ParmVarDecl *ToParam) { |
| 4737 | |
| 4738 | if (auto LocOrErr = import(From: FromParam->getExplicitObjectParamThisLoc())) |
| 4739 | ToParam->setExplicitObjectParameterLoc(*LocOrErr); |
| 4740 | else |
| 4741 | return LocOrErr.takeError(); |
| 4742 | |
| 4743 | ToParam->setHasInheritedDefaultArg(FromParam->hasInheritedDefaultArg()); |
| 4744 | ToParam->setKNRPromoted(FromParam->isKNRPromoted()); |
| 4745 | |
| 4746 | if (FromParam->hasUninstantiatedDefaultArg()) { |
| 4747 | if (auto ToDefArgOrErr = import(From: FromParam->getUninstantiatedDefaultArg())) |
| 4748 | ToParam->setUninstantiatedDefaultArg(*ToDefArgOrErr); |
| 4749 | else |
| 4750 | return ToDefArgOrErr.takeError(); |
| 4751 | } else if (FromParam->hasUnparsedDefaultArg()) { |
| 4752 | ToParam->setUnparsedDefaultArg(); |
| 4753 | } else if (FromParam->hasDefaultArg()) { |
| 4754 | if (auto ToDefArgOrErr = import(From: FromParam->getDefaultArg())) |
| 4755 | ToParam->setDefaultArg(*ToDefArgOrErr); |
| 4756 | else |
| 4757 | return ToDefArgOrErr.takeError(); |
| 4758 | } |
| 4759 | |
| 4760 | return Error::success(); |
| 4761 | } |
| 4762 | |
| 4763 | Expected<InheritedConstructor> |
| 4764 | ASTNodeImporter::ImportInheritedConstructor(const InheritedConstructor &From) { |
| 4765 | Error Err = Error::success(); |
| 4766 | CXXConstructorDecl *ToBaseCtor = importChecked(Err, From: From.getConstructor()); |
| 4767 | ConstructorUsingShadowDecl *ToShadow = |
| 4768 | importChecked(Err, From: From.getShadowDecl()); |
| 4769 | if (Err) |
| 4770 | return std::move(Err); |
| 4771 | return InheritedConstructor(ToShadow, ToBaseCtor); |
| 4772 | } |
| 4773 | |
| 4774 | ExpectedDecl ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) { |
| 4775 | // Parameters are created in the translation unit's context, then moved |
| 4776 | // into the function declaration's context afterward. |
| 4777 | DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); |
| 4778 | |
| 4779 | Error Err = Error::success(); |
| 4780 | auto ToDeclName = importChecked(Err, D->getDeclName()); |
| 4781 | auto ToLocation = importChecked(Err, D->getLocation()); |
| 4782 | auto ToInnerLocStart = importChecked(Err, D->getInnerLocStart()); |
| 4783 | auto ToType = importChecked(Err, D->getType()); |
| 4784 | auto ToTypeSourceInfo = importChecked(Err, D->getTypeSourceInfo()); |
| 4785 | if (Err) |
| 4786 | return std::move(Err); |
| 4787 | |
| 4788 | ParmVarDecl *ToParm; |
| 4789 | if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC, |
| 4790 | ToInnerLocStart, ToLocation, |
| 4791 | ToDeclName.getAsIdentifierInfo(), ToType, |
| 4792 | ToTypeSourceInfo, D->getStorageClass(), |
| 4793 | /*DefaultArg*/ nullptr)) |
| 4794 | return ToParm; |
| 4795 | |
| 4796 | // Set the default argument. It should be no problem if it was already done. |
| 4797 | // Do not import the default expression before GetImportedOrCreateDecl call |
| 4798 | // to avoid possible infinite import loop because circular dependency. |
| 4799 | if (Error Err = ImportDefaultArgOfParmVarDecl(FromParam: D, ToParam: ToParm)) |
| 4800 | return std::move(Err); |
| 4801 | |
| 4802 | if (D->isObjCMethodParameter()) { |
| 4803 | ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex()); |
| 4804 | ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier()); |
| 4805 | } else { |
| 4806 | ToParm->setScopeInfo(scopeDepth: D->getFunctionScopeDepth(), |
| 4807 | parameterIndex: D->getFunctionScopeIndex()); |
| 4808 | } |
| 4809 | |
| 4810 | return ToParm; |
| 4811 | } |
| 4812 | |
| 4813 | ExpectedDecl ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) { |
| 4814 | // Import the major distinguishing characteristics of a method. |
| 4815 | DeclContext *DC, *LexicalDC; |
| 4816 | DeclarationName Name; |
| 4817 | SourceLocation Loc; |
| 4818 | NamedDecl *ToD; |
| 4819 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 4820 | return std::move(Err); |
| 4821 | if (ToD) |
| 4822 | return ToD; |
| 4823 | |
| 4824 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 4825 | for (auto *FoundDecl : FoundDecls) { |
| 4826 | if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(Val: FoundDecl)) { |
| 4827 | if (FoundMethod->isInstanceMethod() != D->isInstanceMethod()) |
| 4828 | continue; |
| 4829 | |
| 4830 | // Check return types. |
| 4831 | if (!Importer.IsStructurallyEquivalent(From: D->getReturnType(), |
| 4832 | To: FoundMethod->getReturnType())) { |
| 4833 | Importer.ToDiag(Loc, diag::warn_odr_objc_method_result_type_inconsistent) |
| 4834 | << D->isInstanceMethod() << Name << D->getReturnType() |
| 4835 | << FoundMethod->getReturnType(); |
| 4836 | Importer.ToDiag(FoundMethod->getLocation(), |
| 4837 | diag::note_odr_objc_method_here) |
| 4838 | << D->isInstanceMethod() << Name; |
| 4839 | |
| 4840 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 4841 | } |
| 4842 | |
| 4843 | // Check the number of parameters. |
| 4844 | if (D->param_size() != FoundMethod->param_size()) { |
| 4845 | Importer.ToDiag(Loc, diag::warn_odr_objc_method_num_params_inconsistent) |
| 4846 | << D->isInstanceMethod() << Name |
| 4847 | << D->param_size() << FoundMethod->param_size(); |
| 4848 | Importer.ToDiag(FoundMethod->getLocation(), |
| 4849 | diag::note_odr_objc_method_here) |
| 4850 | << D->isInstanceMethod() << Name; |
| 4851 | |
| 4852 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 4853 | } |
| 4854 | |
| 4855 | // Check parameter types. |
| 4856 | for (ObjCMethodDecl::param_iterator P = D->param_begin(), |
| 4857 | PEnd = D->param_end(), FoundP = FoundMethod->param_begin(); |
| 4858 | P != PEnd; ++P, ++FoundP) { |
| 4859 | if (!Importer.IsStructurallyEquivalent(From: (*P)->getType(), |
| 4860 | To: (*FoundP)->getType())) { |
| 4861 | Importer.FromDiag((*P)->getLocation(), |
| 4862 | diag::warn_odr_objc_method_param_type_inconsistent) |
| 4863 | << D->isInstanceMethod() << Name |
| 4864 | << (*P)->getType() << (*FoundP)->getType(); |
| 4865 | Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here) |
| 4866 | << (*FoundP)->getType(); |
| 4867 | |
| 4868 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 4869 | } |
| 4870 | } |
| 4871 | |
| 4872 | // Check variadic/non-variadic. |
| 4873 | // Check the number of parameters. |
| 4874 | if (D->isVariadic() != FoundMethod->isVariadic()) { |
| 4875 | Importer.ToDiag(Loc, diag::warn_odr_objc_method_variadic_inconsistent) |
| 4876 | << D->isInstanceMethod() << Name; |
| 4877 | Importer.ToDiag(FoundMethod->getLocation(), |
| 4878 | diag::note_odr_objc_method_here) |
| 4879 | << D->isInstanceMethod() << Name; |
| 4880 | |
| 4881 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 4882 | } |
| 4883 | |
| 4884 | // FIXME: Any other bits we need to merge? |
| 4885 | return Importer.MapImported(D, FoundMethod); |
| 4886 | } |
| 4887 | } |
| 4888 | |
| 4889 | Error Err = Error::success(); |
| 4890 | auto ToEndLoc = importChecked(Err, From: D->getEndLoc()); |
| 4891 | auto ToReturnType = importChecked(Err, From: D->getReturnType()); |
| 4892 | auto ToReturnTypeSourceInfo = |
| 4893 | importChecked(Err, From: D->getReturnTypeSourceInfo()); |
| 4894 | if (Err) |
| 4895 | return std::move(Err); |
| 4896 | |
| 4897 | ObjCMethodDecl *ToMethod; |
| 4898 | if (GetImportedOrCreateDecl( |
| 4899 | ToMethod, D, Importer.getToContext(), Loc, ToEndLoc, |
| 4900 | Name.getObjCSelector(), ToReturnType, ToReturnTypeSourceInfo, DC, |
| 4901 | D->isInstanceMethod(), D->isVariadic(), D->isPropertyAccessor(), |
| 4902 | D->isSynthesizedAccessorStub(), D->isImplicit(), D->isDefined(), |
| 4903 | D->getImplementationControl(), D->hasRelatedResultType())) |
| 4904 | return ToMethod; |
| 4905 | |
| 4906 | // FIXME: When we decide to merge method definitions, we'll need to |
| 4907 | // deal with implicit parameters. |
| 4908 | |
| 4909 | // Import the parameters |
| 4910 | SmallVector<ParmVarDecl *, 5> ToParams; |
| 4911 | for (auto *FromP : D->parameters()) { |
| 4912 | if (Expected<ParmVarDecl *> ToPOrErr = import(From: FromP)) |
| 4913 | ToParams.push_back(Elt: *ToPOrErr); |
| 4914 | else |
| 4915 | return ToPOrErr.takeError(); |
| 4916 | } |
| 4917 | |
| 4918 | // Set the parameters. |
| 4919 | for (auto *ToParam : ToParams) { |
| 4920 | ToParam->setOwningFunction(ToMethod); |
| 4921 | ToMethod->addDeclInternal(ToParam); |
| 4922 | } |
| 4923 | |
| 4924 | SmallVector<SourceLocation, 12> FromSelLocs; |
| 4925 | D->getSelectorLocs(SelLocs&: FromSelLocs); |
| 4926 | SmallVector<SourceLocation, 12> ToSelLocs(FromSelLocs.size()); |
| 4927 | if (Error Err = ImportContainerChecked(InContainer: FromSelLocs, OutContainer&: ToSelLocs)) |
| 4928 | return std::move(Err); |
| 4929 | |
| 4930 | ToMethod->setMethodParams(C&: Importer.getToContext(), Params: ToParams, SelLocs: ToSelLocs); |
| 4931 | |
| 4932 | ToMethod->setLexicalDeclContext(LexicalDC); |
| 4933 | LexicalDC->addDeclInternal(ToMethod); |
| 4934 | |
| 4935 | // Implicit params are declared when Sema encounters the definition but this |
| 4936 | // never happens when the method is imported. Manually declare the implicit |
| 4937 | // params now that the MethodDecl knows its class interface. |
| 4938 | if (D->getSelfDecl()) |
| 4939 | ToMethod->createImplicitParams(Context&: Importer.getToContext(), |
| 4940 | ID: ToMethod->getClassInterface()); |
| 4941 | |
| 4942 | return ToMethod; |
| 4943 | } |
| 4944 | |
| 4945 | ExpectedDecl ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) { |
| 4946 | // Import the major distinguishing characteristics of a category. |
| 4947 | DeclContext *DC, *LexicalDC; |
| 4948 | DeclarationName Name; |
| 4949 | SourceLocation Loc; |
| 4950 | NamedDecl *ToD; |
| 4951 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 4952 | return std::move(Err); |
| 4953 | if (ToD) |
| 4954 | return ToD; |
| 4955 | |
| 4956 | Error Err = Error::success(); |
| 4957 | auto ToVarianceLoc = importChecked(Err, From: D->getVarianceLoc()); |
| 4958 | auto ToLocation = importChecked(Err, D->getLocation()); |
| 4959 | auto ToColonLoc = importChecked(Err, From: D->getColonLoc()); |
| 4960 | auto ToTypeSourceInfo = importChecked(Err, D->getTypeSourceInfo()); |
| 4961 | if (Err) |
| 4962 | return std::move(Err); |
| 4963 | |
| 4964 | ObjCTypeParamDecl *Result; |
| 4965 | if (GetImportedOrCreateDecl( |
| 4966 | Result, D, Importer.getToContext(), DC, D->getVariance(), |
| 4967 | ToVarianceLoc, D->getIndex(), |
| 4968 | ToLocation, Name.getAsIdentifierInfo(), |
| 4969 | ToColonLoc, ToTypeSourceInfo)) |
| 4970 | return Result; |
| 4971 | |
| 4972 | // Only import 'ObjCTypeParamType' after the decl is created. |
| 4973 | auto ToTypeForDecl = importChecked(Err, D->getTypeForDecl()); |
| 4974 | if (Err) |
| 4975 | return std::move(Err); |
| 4976 | Result->setTypeForDecl(ToTypeForDecl); |
| 4977 | Result->setLexicalDeclContext(LexicalDC); |
| 4978 | return Result; |
| 4979 | } |
| 4980 | |
| 4981 | ExpectedDecl ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) { |
| 4982 | // Import the major distinguishing characteristics of a category. |
| 4983 | DeclContext *DC, *LexicalDC; |
| 4984 | DeclarationName Name; |
| 4985 | SourceLocation Loc; |
| 4986 | NamedDecl *ToD; |
| 4987 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 4988 | return std::move(Err); |
| 4989 | if (ToD) |
| 4990 | return ToD; |
| 4991 | |
| 4992 | ObjCInterfaceDecl *ToInterface; |
| 4993 | if (Error Err = importInto(To&: ToInterface, From: D->getClassInterface())) |
| 4994 | return std::move(Err); |
| 4995 | |
| 4996 | // Determine if we've already encountered this category. |
| 4997 | ObjCCategoryDecl *MergeWithCategory |
| 4998 | = ToInterface->FindCategoryDeclaration(CategoryId: Name.getAsIdentifierInfo()); |
| 4999 | ObjCCategoryDecl *ToCategory = MergeWithCategory; |
| 5000 | if (!ToCategory) { |
| 5001 | |
| 5002 | Error Err = Error::success(); |
| 5003 | auto ToAtStartLoc = importChecked(Err, D->getAtStartLoc()); |
| 5004 | auto ToCategoryNameLoc = importChecked(Err, From: D->getCategoryNameLoc()); |
| 5005 | auto ToIvarLBraceLoc = importChecked(Err, From: D->getIvarLBraceLoc()); |
| 5006 | auto ToIvarRBraceLoc = importChecked(Err, From: D->getIvarRBraceLoc()); |
| 5007 | if (Err) |
| 5008 | return std::move(Err); |
| 5009 | |
| 5010 | if (GetImportedOrCreateDecl(ToCategory, D, Importer.getToContext(), DC, |
| 5011 | ToAtStartLoc, Loc, |
| 5012 | ToCategoryNameLoc, |
| 5013 | Name.getAsIdentifierInfo(), ToInterface, |
| 5014 | /*TypeParamList=*/nullptr, |
| 5015 | ToIvarLBraceLoc, |
| 5016 | ToIvarRBraceLoc)) |
| 5017 | return ToCategory; |
| 5018 | |
| 5019 | ToCategory->setLexicalDeclContext(LexicalDC); |
| 5020 | LexicalDC->addDeclInternal(ToCategory); |
| 5021 | // Import the type parameter list after MapImported, to avoid |
| 5022 | // loops when bringing in their DeclContext. |
| 5023 | if (auto PListOrErr = ImportObjCTypeParamList(list: D->getTypeParamList())) |
| 5024 | ToCategory->setTypeParamList(*PListOrErr); |
| 5025 | else |
| 5026 | return PListOrErr.takeError(); |
| 5027 | |
| 5028 | // Import protocols |
| 5029 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 5030 | SmallVector<SourceLocation, 4> ProtocolLocs; |
| 5031 | ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc |
| 5032 | = D->protocol_loc_begin(); |
| 5033 | for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(), |
| 5034 | FromProtoEnd = D->protocol_end(); |
| 5035 | FromProto != FromProtoEnd; |
| 5036 | ++FromProto, ++FromProtoLoc) { |
| 5037 | if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(From: *FromProto)) |
| 5038 | Protocols.push_back(Elt: *ToProtoOrErr); |
| 5039 | else |
| 5040 | return ToProtoOrErr.takeError(); |
| 5041 | |
| 5042 | if (ExpectedSLoc ToProtoLocOrErr = import(From: *FromProtoLoc)) |
| 5043 | ProtocolLocs.push_back(Elt: *ToProtoLocOrErr); |
| 5044 | else |
| 5045 | return ToProtoLocOrErr.takeError(); |
| 5046 | } |
| 5047 | |
| 5048 | // FIXME: If we're merging, make sure that the protocol list is the same. |
| 5049 | ToCategory->setProtocolList(List: Protocols.data(), Num: Protocols.size(), |
| 5050 | Locs: ProtocolLocs.data(), C&: Importer.getToContext()); |
| 5051 | |
| 5052 | } else { |
| 5053 | Importer.MapImported(D, ToCategory); |
| 5054 | } |
| 5055 | |
| 5056 | // Import all of the members of this category. |
| 5057 | if (Error Err = ImportDeclContext(D)) |
| 5058 | return std::move(Err); |
| 5059 | |
| 5060 | // If we have an implementation, import it as well. |
| 5061 | if (D->getImplementation()) { |
| 5062 | if (Expected<ObjCCategoryImplDecl *> ToImplOrErr = |
| 5063 | import(From: D->getImplementation())) |
| 5064 | ToCategory->setImplementation(*ToImplOrErr); |
| 5065 | else |
| 5066 | return ToImplOrErr.takeError(); |
| 5067 | } |
| 5068 | |
| 5069 | return ToCategory; |
| 5070 | } |
| 5071 | |
| 5072 | Error ASTNodeImporter::ImportDefinition( |
| 5073 | ObjCProtocolDecl *From, ObjCProtocolDecl *To, ImportDefinitionKind Kind) { |
| 5074 | if (To->getDefinition()) { |
| 5075 | if (shouldForceImportDeclContext(IDK: Kind)) |
| 5076 | if (Error Err = ImportDeclContext(From)) |
| 5077 | return Err; |
| 5078 | return Error::success(); |
| 5079 | } |
| 5080 | |
| 5081 | // Start the protocol definition |
| 5082 | To->startDefinition(); |
| 5083 | |
| 5084 | // Import protocols |
| 5085 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 5086 | SmallVector<SourceLocation, 4> ProtocolLocs; |
| 5087 | ObjCProtocolDecl::protocol_loc_iterator FromProtoLoc = |
| 5088 | From->protocol_loc_begin(); |
| 5089 | for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(), |
| 5090 | FromProtoEnd = From->protocol_end(); |
| 5091 | FromProto != FromProtoEnd; |
| 5092 | ++FromProto, ++FromProtoLoc) { |
| 5093 | if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(From: *FromProto)) |
| 5094 | Protocols.push_back(Elt: *ToProtoOrErr); |
| 5095 | else |
| 5096 | return ToProtoOrErr.takeError(); |
| 5097 | |
| 5098 | if (ExpectedSLoc ToProtoLocOrErr = import(From: *FromProtoLoc)) |
| 5099 | ProtocolLocs.push_back(Elt: *ToProtoLocOrErr); |
| 5100 | else |
| 5101 | return ToProtoLocOrErr.takeError(); |
| 5102 | |
| 5103 | } |
| 5104 | |
| 5105 | // FIXME: If we're merging, make sure that the protocol list is the same. |
| 5106 | To->setProtocolList(List: Protocols.data(), Num: Protocols.size(), |
| 5107 | Locs: ProtocolLocs.data(), C&: Importer.getToContext()); |
| 5108 | |
| 5109 | if (shouldForceImportDeclContext(IDK: Kind)) { |
| 5110 | // Import all of the members of this protocol. |
| 5111 | if (Error Err = ImportDeclContext(From, /*ForceImport=*/true)) |
| 5112 | return Err; |
| 5113 | } |
| 5114 | return Error::success(); |
| 5115 | } |
| 5116 | |
| 5117 | ExpectedDecl ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) { |
| 5118 | // If this protocol has a definition in the translation unit we're coming |
| 5119 | // from, but this particular declaration is not that definition, import the |
| 5120 | // definition and map to that. |
| 5121 | ObjCProtocolDecl *Definition = D->getDefinition(); |
| 5122 | if (Definition && Definition != D) { |
| 5123 | if (ExpectedDecl ImportedDefOrErr = import(From: Definition)) |
| 5124 | return Importer.MapImported(D, *ImportedDefOrErr); |
| 5125 | else |
| 5126 | return ImportedDefOrErr.takeError(); |
| 5127 | } |
| 5128 | |
| 5129 | // Import the major distinguishing characteristics of a protocol. |
| 5130 | DeclContext *DC, *LexicalDC; |
| 5131 | DeclarationName Name; |
| 5132 | SourceLocation Loc; |
| 5133 | NamedDecl *ToD; |
| 5134 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 5135 | return std::move(Err); |
| 5136 | if (ToD) |
| 5137 | return ToD; |
| 5138 | |
| 5139 | ObjCProtocolDecl *MergeWithProtocol = nullptr; |
| 5140 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 5141 | for (auto *FoundDecl : FoundDecls) { |
| 5142 | if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol)) |
| 5143 | continue; |
| 5144 | |
| 5145 | if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(Val: FoundDecl))) |
| 5146 | break; |
| 5147 | } |
| 5148 | |
| 5149 | ObjCProtocolDecl *ToProto = MergeWithProtocol; |
| 5150 | if (!ToProto) { |
| 5151 | auto ToAtBeginLocOrErr = import(D->getAtStartLoc()); |
| 5152 | if (!ToAtBeginLocOrErr) |
| 5153 | return ToAtBeginLocOrErr.takeError(); |
| 5154 | |
| 5155 | if (GetImportedOrCreateDecl(ToProto, D, Importer.getToContext(), DC, |
| 5156 | Name.getAsIdentifierInfo(), Loc, |
| 5157 | *ToAtBeginLocOrErr, |
| 5158 | /*PrevDecl=*/nullptr)) |
| 5159 | return ToProto; |
| 5160 | ToProto->setLexicalDeclContext(LexicalDC); |
| 5161 | LexicalDC->addDeclInternal(ToProto); |
| 5162 | } |
| 5163 | |
| 5164 | Importer.MapImported(D, ToProto); |
| 5165 | |
| 5166 | if (D->isThisDeclarationADefinition()) |
| 5167 | if (Error Err = ImportDefinition(From: D, To: ToProto)) |
| 5168 | return std::move(Err); |
| 5169 | |
| 5170 | return ToProto; |
| 5171 | } |
| 5172 | |
| 5173 | ExpectedDecl ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 5174 | DeclContext *DC, *LexicalDC; |
| 5175 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) |
| 5176 | return std::move(Err); |
| 5177 | |
| 5178 | ExpectedSLoc ExternLocOrErr = import(From: D->getExternLoc()); |
| 5179 | if (!ExternLocOrErr) |
| 5180 | return ExternLocOrErr.takeError(); |
| 5181 | |
| 5182 | ExpectedSLoc LangLocOrErr = import(D->getLocation()); |
| 5183 | if (!LangLocOrErr) |
| 5184 | return LangLocOrErr.takeError(); |
| 5185 | |
| 5186 | bool HasBraces = D->hasBraces(); |
| 5187 | |
| 5188 | LinkageSpecDecl *ToLinkageSpec; |
| 5189 | if (GetImportedOrCreateDecl(ToD&: ToLinkageSpec, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 5190 | args&: *ExternLocOrErr, args&: *LangLocOrErr, |
| 5191 | args: D->getLanguage(), args&: HasBraces)) |
| 5192 | return ToLinkageSpec; |
| 5193 | |
| 5194 | if (HasBraces) { |
| 5195 | ExpectedSLoc RBraceLocOrErr = import(From: D->getRBraceLoc()); |
| 5196 | if (!RBraceLocOrErr) |
| 5197 | return RBraceLocOrErr.takeError(); |
| 5198 | ToLinkageSpec->setRBraceLoc(*RBraceLocOrErr); |
| 5199 | } |
| 5200 | |
| 5201 | ToLinkageSpec->setLexicalDeclContext(LexicalDC); |
| 5202 | LexicalDC->addDeclInternal(ToLinkageSpec); |
| 5203 | |
| 5204 | return ToLinkageSpec; |
| 5205 | } |
| 5206 | |
| 5207 | ExpectedDecl ASTNodeImporter::ImportUsingShadowDecls(BaseUsingDecl *D, |
| 5208 | BaseUsingDecl *ToSI) { |
| 5209 | for (UsingShadowDecl *FromShadow : D->shadows()) { |
| 5210 | if (Expected<UsingShadowDecl *> ToShadowOrErr = import(From: FromShadow)) |
| 5211 | ToSI->addShadowDecl(S: *ToShadowOrErr); |
| 5212 | else |
| 5213 | // FIXME: We return error here but the definition is already created |
| 5214 | // and available with lookups. How to fix this?.. |
| 5215 | return ToShadowOrErr.takeError(); |
| 5216 | } |
| 5217 | return ToSI; |
| 5218 | } |
| 5219 | |
| 5220 | ExpectedDecl ASTNodeImporter::VisitUsingDecl(UsingDecl *D) { |
| 5221 | DeclContext *DC, *LexicalDC; |
| 5222 | DeclarationName Name; |
| 5223 | SourceLocation Loc; |
| 5224 | NamedDecl *ToD = nullptr; |
| 5225 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 5226 | return std::move(Err); |
| 5227 | if (ToD) |
| 5228 | return ToD; |
| 5229 | |
| 5230 | Error Err = Error::success(); |
| 5231 | auto ToLoc = importChecked(Err, From: D->getNameInfo().getLoc()); |
| 5232 | auto ToUsingLoc = importChecked(Err, From: D->getUsingLoc()); |
| 5233 | auto ToQualifierLoc = importChecked(Err, From: D->getQualifierLoc()); |
| 5234 | if (Err) |
| 5235 | return std::move(Err); |
| 5236 | |
| 5237 | DeclarationNameInfo NameInfo(Name, ToLoc); |
| 5238 | if (Error Err = ImportDeclarationNameLoc(From: D->getNameInfo(), To&: NameInfo)) |
| 5239 | return std::move(Err); |
| 5240 | |
| 5241 | UsingDecl *ToUsing; |
| 5242 | if (GetImportedOrCreateDecl(ToD&: ToUsing, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 5243 | args&: ToUsingLoc, args&: ToQualifierLoc, args&: NameInfo, |
| 5244 | args: D->hasTypename())) |
| 5245 | return ToUsing; |
| 5246 | |
| 5247 | ToUsing->setLexicalDeclContext(LexicalDC); |
| 5248 | LexicalDC->addDeclInternal(ToUsing); |
| 5249 | |
| 5250 | if (NamedDecl *FromPattern = |
| 5251 | Importer.getFromContext().getInstantiatedFromUsingDecl(D)) { |
| 5252 | if (Expected<NamedDecl *> ToPatternOrErr = import(From: FromPattern)) |
| 5253 | Importer.getToContext().setInstantiatedFromUsingDecl( |
| 5254 | ToUsing, *ToPatternOrErr); |
| 5255 | else |
| 5256 | return ToPatternOrErr.takeError(); |
| 5257 | } |
| 5258 | |
| 5259 | return ImportUsingShadowDecls(D, ToUsing); |
| 5260 | } |
| 5261 | |
| 5262 | ExpectedDecl ASTNodeImporter::VisitUsingEnumDecl(UsingEnumDecl *D) { |
| 5263 | DeclContext *DC, *LexicalDC; |
| 5264 | DeclarationName Name; |
| 5265 | SourceLocation Loc; |
| 5266 | NamedDecl *ToD = nullptr; |
| 5267 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 5268 | return std::move(Err); |
| 5269 | if (ToD) |
| 5270 | return ToD; |
| 5271 | |
| 5272 | Error Err = Error::success(); |
| 5273 | auto ToUsingLoc = importChecked(Err, From: D->getUsingLoc()); |
| 5274 | auto ToEnumLoc = importChecked(Err, From: D->getEnumLoc()); |
| 5275 | auto ToNameLoc = importChecked(Err, D->getLocation()); |
| 5276 | auto *ToEnumType = importChecked(Err, From: D->getEnumType()); |
| 5277 | if (Err) |
| 5278 | return std::move(Err); |
| 5279 | |
| 5280 | UsingEnumDecl *ToUsingEnum; |
| 5281 | if (GetImportedOrCreateDecl(ToUsingEnum, D, Importer.getToContext(), DC, |
| 5282 | ToUsingLoc, ToEnumLoc, ToNameLoc, ToEnumType)) |
| 5283 | return ToUsingEnum; |
| 5284 | |
| 5285 | ToUsingEnum->setLexicalDeclContext(LexicalDC); |
| 5286 | LexicalDC->addDeclInternal(ToUsingEnum); |
| 5287 | |
| 5288 | if (UsingEnumDecl *FromPattern = |
| 5289 | Importer.getFromContext().getInstantiatedFromUsingEnumDecl(Inst: D)) { |
| 5290 | if (Expected<UsingEnumDecl *> ToPatternOrErr = import(From: FromPattern)) |
| 5291 | Importer.getToContext().setInstantiatedFromUsingEnumDecl(Inst: ToUsingEnum, |
| 5292 | Pattern: *ToPatternOrErr); |
| 5293 | else |
| 5294 | return ToPatternOrErr.takeError(); |
| 5295 | } |
| 5296 | |
| 5297 | return ImportUsingShadowDecls(D, ToUsingEnum); |
| 5298 | } |
| 5299 | |
| 5300 | ExpectedDecl ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) { |
| 5301 | DeclContext *DC, *LexicalDC; |
| 5302 | DeclarationName Name; |
| 5303 | SourceLocation Loc; |
| 5304 | NamedDecl *ToD = nullptr; |
| 5305 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 5306 | return std::move(Err); |
| 5307 | if (ToD) |
| 5308 | return ToD; |
| 5309 | |
| 5310 | Expected<BaseUsingDecl *> ToIntroducerOrErr = import(From: D->getIntroducer()); |
| 5311 | if (!ToIntroducerOrErr) |
| 5312 | return ToIntroducerOrErr.takeError(); |
| 5313 | |
| 5314 | Expected<NamedDecl *> ToTargetOrErr = import(From: D->getTargetDecl()); |
| 5315 | if (!ToTargetOrErr) |
| 5316 | return ToTargetOrErr.takeError(); |
| 5317 | |
| 5318 | UsingShadowDecl *ToShadow; |
| 5319 | if (auto *FromConstructorUsingShadow = |
| 5320 | dyn_cast<ConstructorUsingShadowDecl>(Val: D)) { |
| 5321 | Error Err = Error::success(); |
| 5322 | ConstructorUsingShadowDecl *Nominated = importChecked( |
| 5323 | Err, From: FromConstructorUsingShadow->getNominatedBaseClassShadowDecl()); |
| 5324 | if (Err) |
| 5325 | return std::move(Err); |
| 5326 | // The 'Target' parameter of ConstructorUsingShadowDecl constructor |
| 5327 | // is really the "NominatedBaseClassShadowDecl" value if it exists |
| 5328 | // (see code of ConstructorUsingShadowDecl::ConstructorUsingShadowDecl). |
| 5329 | // We should pass the NominatedBaseClassShadowDecl to it (if non-null) to |
| 5330 | // get the correct values. |
| 5331 | if (GetImportedOrCreateDecl<ConstructorUsingShadowDecl>( |
| 5332 | ToShadow, D, Importer.getToContext(), DC, Loc, |
| 5333 | cast<UsingDecl>(Val: *ToIntroducerOrErr), |
| 5334 | Nominated ? Nominated : *ToTargetOrErr, |
| 5335 | FromConstructorUsingShadow->constructsVirtualBase())) |
| 5336 | return ToShadow; |
| 5337 | } else { |
| 5338 | if (GetImportedOrCreateDecl(ToD&: ToShadow, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, |
| 5339 | args&: Name, args&: *ToIntroducerOrErr, args&: *ToTargetOrErr)) |
| 5340 | return ToShadow; |
| 5341 | } |
| 5342 | |
| 5343 | ToShadow->setLexicalDeclContext(LexicalDC); |
| 5344 | ToShadow->setAccess(D->getAccess()); |
| 5345 | |
| 5346 | if (UsingShadowDecl *FromPattern = |
| 5347 | Importer.getFromContext().getInstantiatedFromUsingShadowDecl(Inst: D)) { |
| 5348 | if (Expected<UsingShadowDecl *> ToPatternOrErr = import(From: FromPattern)) |
| 5349 | Importer.getToContext().setInstantiatedFromUsingShadowDecl( |
| 5350 | Inst: ToShadow, Pattern: *ToPatternOrErr); |
| 5351 | else |
| 5352 | // FIXME: We return error here but the definition is already created |
| 5353 | // and available with lookups. How to fix this?.. |
| 5354 | return ToPatternOrErr.takeError(); |
| 5355 | } |
| 5356 | |
| 5357 | LexicalDC->addDeclInternal(ToShadow); |
| 5358 | |
| 5359 | return ToShadow; |
| 5360 | } |
| 5361 | |
| 5362 | ExpectedDecl ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
| 5363 | DeclContext *DC, *LexicalDC; |
| 5364 | DeclarationName Name; |
| 5365 | SourceLocation Loc; |
| 5366 | NamedDecl *ToD = nullptr; |
| 5367 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 5368 | return std::move(Err); |
| 5369 | if (ToD) |
| 5370 | return ToD; |
| 5371 | |
| 5372 | auto ToComAncestorOrErr = Importer.ImportContext(FromDC: D->getCommonAncestor()); |
| 5373 | if (!ToComAncestorOrErr) |
| 5374 | return ToComAncestorOrErr.takeError(); |
| 5375 | |
| 5376 | Error Err = Error::success(); |
| 5377 | auto ToNominatedNamespace = importChecked(Err, From: D->getNominatedNamespace()); |
| 5378 | auto ToUsingLoc = importChecked(Err, From: D->getUsingLoc()); |
| 5379 | auto ToNamespaceKeyLocation = |
| 5380 | importChecked(Err, From: D->getNamespaceKeyLocation()); |
| 5381 | auto ToQualifierLoc = importChecked(Err, From: D->getQualifierLoc()); |
| 5382 | auto ToIdentLocation = importChecked(Err, From: D->getIdentLocation()); |
| 5383 | if (Err) |
| 5384 | return std::move(Err); |
| 5385 | |
| 5386 | UsingDirectiveDecl *ToUsingDir; |
| 5387 | if (GetImportedOrCreateDecl(ToD&: ToUsingDir, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 5388 | args&: ToUsingLoc, |
| 5389 | args&: ToNamespaceKeyLocation, |
| 5390 | args&: ToQualifierLoc, |
| 5391 | args&: ToIdentLocation, |
| 5392 | args&: ToNominatedNamespace, args&: *ToComAncestorOrErr)) |
| 5393 | return ToUsingDir; |
| 5394 | |
| 5395 | ToUsingDir->setLexicalDeclContext(LexicalDC); |
| 5396 | LexicalDC->addDeclInternal(ToUsingDir); |
| 5397 | |
| 5398 | return ToUsingDir; |
| 5399 | } |
| 5400 | |
| 5401 | ExpectedDecl ASTNodeImporter::VisitUsingPackDecl(UsingPackDecl *D) { |
| 5402 | DeclContext *DC, *LexicalDC; |
| 5403 | DeclarationName Name; |
| 5404 | SourceLocation Loc; |
| 5405 | NamedDecl *ToD = nullptr; |
| 5406 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 5407 | return std::move(Err); |
| 5408 | if (ToD) |
| 5409 | return ToD; |
| 5410 | |
| 5411 | auto ToInstantiatedFromUsingOrErr = |
| 5412 | Importer.Import(D->getInstantiatedFromUsingDecl()); |
| 5413 | if (!ToInstantiatedFromUsingOrErr) |
| 5414 | return ToInstantiatedFromUsingOrErr.takeError(); |
| 5415 | SmallVector<NamedDecl *, 4> Expansions(D->expansions().size()); |
| 5416 | if (Error Err = ImportArrayChecked(InContainer: D->expansions(), Obegin: Expansions.begin())) |
| 5417 | return std::move(Err); |
| 5418 | |
| 5419 | UsingPackDecl *ToUsingPack; |
| 5420 | if (GetImportedOrCreateDecl(ToUsingPack, D, Importer.getToContext(), DC, |
| 5421 | cast<NamedDecl>(*ToInstantiatedFromUsingOrErr), |
| 5422 | Expansions)) |
| 5423 | return ToUsingPack; |
| 5424 | |
| 5425 | addDeclToContexts(D, ToUsingPack); |
| 5426 | |
| 5427 | return ToUsingPack; |
| 5428 | } |
| 5429 | |
| 5430 | ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingValueDecl( |
| 5431 | UnresolvedUsingValueDecl *D) { |
| 5432 | DeclContext *DC, *LexicalDC; |
| 5433 | DeclarationName Name; |
| 5434 | SourceLocation Loc; |
| 5435 | NamedDecl *ToD = nullptr; |
| 5436 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 5437 | return std::move(Err); |
| 5438 | if (ToD) |
| 5439 | return ToD; |
| 5440 | |
| 5441 | Error Err = Error::success(); |
| 5442 | auto ToLoc = importChecked(Err, From: D->getNameInfo().getLoc()); |
| 5443 | auto ToUsingLoc = importChecked(Err, From: D->getUsingLoc()); |
| 5444 | auto ToQualifierLoc = importChecked(Err, From: D->getQualifierLoc()); |
| 5445 | auto ToEllipsisLoc = importChecked(Err, From: D->getEllipsisLoc()); |
| 5446 | if (Err) |
| 5447 | return std::move(Err); |
| 5448 | |
| 5449 | DeclarationNameInfo NameInfo(Name, ToLoc); |
| 5450 | if (Error Err = ImportDeclarationNameLoc(From: D->getNameInfo(), To&: NameInfo)) |
| 5451 | return std::move(Err); |
| 5452 | |
| 5453 | UnresolvedUsingValueDecl *ToUsingValue; |
| 5454 | if (GetImportedOrCreateDecl(ToD&: ToUsingValue, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 5455 | args&: ToUsingLoc, args&: ToQualifierLoc, args&: NameInfo, |
| 5456 | args&: ToEllipsisLoc)) |
| 5457 | return ToUsingValue; |
| 5458 | |
| 5459 | ToUsingValue->setAccess(D->getAccess()); |
| 5460 | ToUsingValue->setLexicalDeclContext(LexicalDC); |
| 5461 | LexicalDC->addDeclInternal(ToUsingValue); |
| 5462 | |
| 5463 | return ToUsingValue; |
| 5464 | } |
| 5465 | |
| 5466 | ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingTypenameDecl( |
| 5467 | UnresolvedUsingTypenameDecl *D) { |
| 5468 | DeclContext *DC, *LexicalDC; |
| 5469 | DeclarationName Name; |
| 5470 | SourceLocation Loc; |
| 5471 | NamedDecl *ToD = nullptr; |
| 5472 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 5473 | return std::move(Err); |
| 5474 | if (ToD) |
| 5475 | return ToD; |
| 5476 | |
| 5477 | Error Err = Error::success(); |
| 5478 | auto ToUsingLoc = importChecked(Err, From: D->getUsingLoc()); |
| 5479 | auto ToTypenameLoc = importChecked(Err, From: D->getTypenameLoc()); |
| 5480 | auto ToQualifierLoc = importChecked(Err, From: D->getQualifierLoc()); |
| 5481 | auto ToEllipsisLoc = importChecked(Err, From: D->getEllipsisLoc()); |
| 5482 | if (Err) |
| 5483 | return std::move(Err); |
| 5484 | |
| 5485 | UnresolvedUsingTypenameDecl *ToUsing; |
| 5486 | if (GetImportedOrCreateDecl(ToD&: ToUsing, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 5487 | args&: ToUsingLoc, args&: ToTypenameLoc, |
| 5488 | args&: ToQualifierLoc, args&: Loc, args&: Name, args&: ToEllipsisLoc)) |
| 5489 | return ToUsing; |
| 5490 | |
| 5491 | ToUsing->setAccess(D->getAccess()); |
| 5492 | ToUsing->setLexicalDeclContext(LexicalDC); |
| 5493 | LexicalDC->addDeclInternal(ToUsing); |
| 5494 | |
| 5495 | return ToUsing; |
| 5496 | } |
| 5497 | |
| 5498 | ExpectedDecl ASTNodeImporter::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) { |
| 5499 | Decl* ToD = nullptr; |
| 5500 | switch (D->getBuiltinTemplateKind()) { |
| 5501 | #define BuiltinTemplate(BTName) \ |
| 5502 | case BuiltinTemplateKind::BTK##BTName: \ |
| 5503 | ToD = Importer.getToContext().get##BTName##Decl(); \ |
| 5504 | break; |
| 5505 | #include "clang/Basic/BuiltinTemplates.inc" |
| 5506 | } |
| 5507 | assert(ToD && "BuiltinTemplateDecl of unsupported kind!" ); |
| 5508 | Importer.MapImported(D, ToD); |
| 5509 | return ToD; |
| 5510 | } |
| 5511 | |
| 5512 | Error ASTNodeImporter::ImportDefinition( |
| 5513 | ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, ImportDefinitionKind Kind) { |
| 5514 | if (To->getDefinition()) { |
| 5515 | // Check consistency of superclass. |
| 5516 | ObjCInterfaceDecl *FromSuper = From->getSuperClass(); |
| 5517 | if (FromSuper) { |
| 5518 | if (auto FromSuperOrErr = import(From: FromSuper)) |
| 5519 | FromSuper = *FromSuperOrErr; |
| 5520 | else |
| 5521 | return FromSuperOrErr.takeError(); |
| 5522 | } |
| 5523 | |
| 5524 | ObjCInterfaceDecl *ToSuper = To->getSuperClass(); |
| 5525 | if ((bool)FromSuper != (bool)ToSuper || |
| 5526 | (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) { |
| 5527 | Importer.ToDiag(To->getLocation(), |
| 5528 | diag::warn_odr_objc_superclass_inconsistent) |
| 5529 | << To->getDeclName(); |
| 5530 | if (ToSuper) |
| 5531 | Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass) |
| 5532 | << To->getSuperClass()->getDeclName(); |
| 5533 | else |
| 5534 | Importer.ToDiag(To->getLocation(), |
| 5535 | diag::note_odr_objc_missing_superclass); |
| 5536 | if (From->getSuperClass()) |
| 5537 | Importer.FromDiag(From->getSuperClassLoc(), |
| 5538 | diag::note_odr_objc_superclass) |
| 5539 | << From->getSuperClass()->getDeclName(); |
| 5540 | else |
| 5541 | Importer.FromDiag(From->getLocation(), |
| 5542 | diag::note_odr_objc_missing_superclass); |
| 5543 | } |
| 5544 | |
| 5545 | if (shouldForceImportDeclContext(IDK: Kind)) |
| 5546 | if (Error Err = ImportDeclContext(From)) |
| 5547 | return Err; |
| 5548 | return Error::success(); |
| 5549 | } |
| 5550 | |
| 5551 | // Start the definition. |
| 5552 | To->startDefinition(); |
| 5553 | |
| 5554 | // If this class has a superclass, import it. |
| 5555 | if (From->getSuperClass()) { |
| 5556 | if (auto SuperTInfoOrErr = import(From: From->getSuperClassTInfo())) |
| 5557 | To->setSuperClass(*SuperTInfoOrErr); |
| 5558 | else |
| 5559 | return SuperTInfoOrErr.takeError(); |
| 5560 | } |
| 5561 | |
| 5562 | // Import protocols |
| 5563 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 5564 | SmallVector<SourceLocation, 4> ProtocolLocs; |
| 5565 | ObjCInterfaceDecl::protocol_loc_iterator FromProtoLoc = |
| 5566 | From->protocol_loc_begin(); |
| 5567 | |
| 5568 | for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(), |
| 5569 | FromProtoEnd = From->protocol_end(); |
| 5570 | FromProto != FromProtoEnd; |
| 5571 | ++FromProto, ++FromProtoLoc) { |
| 5572 | if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(From: *FromProto)) |
| 5573 | Protocols.push_back(Elt: *ToProtoOrErr); |
| 5574 | else |
| 5575 | return ToProtoOrErr.takeError(); |
| 5576 | |
| 5577 | if (ExpectedSLoc ToProtoLocOrErr = import(From: *FromProtoLoc)) |
| 5578 | ProtocolLocs.push_back(Elt: *ToProtoLocOrErr); |
| 5579 | else |
| 5580 | return ToProtoLocOrErr.takeError(); |
| 5581 | |
| 5582 | } |
| 5583 | |
| 5584 | // FIXME: If we're merging, make sure that the protocol list is the same. |
| 5585 | To->setProtocolList(List: Protocols.data(), Num: Protocols.size(), |
| 5586 | Locs: ProtocolLocs.data(), C&: Importer.getToContext()); |
| 5587 | |
| 5588 | // Import categories. When the categories themselves are imported, they'll |
| 5589 | // hook themselves into this interface. |
| 5590 | for (auto *Cat : From->known_categories()) { |
| 5591 | auto ToCatOrErr = import(From: Cat); |
| 5592 | if (!ToCatOrErr) |
| 5593 | return ToCatOrErr.takeError(); |
| 5594 | } |
| 5595 | |
| 5596 | // If we have an @implementation, import it as well. |
| 5597 | if (From->getImplementation()) { |
| 5598 | if (Expected<ObjCImplementationDecl *> ToImplOrErr = |
| 5599 | import(From: From->getImplementation())) |
| 5600 | To->setImplementation(*ToImplOrErr); |
| 5601 | else |
| 5602 | return ToImplOrErr.takeError(); |
| 5603 | } |
| 5604 | |
| 5605 | // Import all of the members of this class. |
| 5606 | if (Error Err = ImportDeclContext(From, /*ForceImport=*/true)) |
| 5607 | return Err; |
| 5608 | |
| 5609 | return Error::success(); |
| 5610 | } |
| 5611 | |
| 5612 | Expected<ObjCTypeParamList *> |
| 5613 | ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) { |
| 5614 | if (!list) |
| 5615 | return nullptr; |
| 5616 | |
| 5617 | SmallVector<ObjCTypeParamDecl *, 4> toTypeParams; |
| 5618 | for (auto *fromTypeParam : *list) { |
| 5619 | if (auto toTypeParamOrErr = import(From: fromTypeParam)) |
| 5620 | toTypeParams.push_back(Elt: *toTypeParamOrErr); |
| 5621 | else |
| 5622 | return toTypeParamOrErr.takeError(); |
| 5623 | } |
| 5624 | |
| 5625 | auto LAngleLocOrErr = import(From: list->getLAngleLoc()); |
| 5626 | if (!LAngleLocOrErr) |
| 5627 | return LAngleLocOrErr.takeError(); |
| 5628 | |
| 5629 | auto RAngleLocOrErr = import(From: list->getRAngleLoc()); |
| 5630 | if (!RAngleLocOrErr) |
| 5631 | return RAngleLocOrErr.takeError(); |
| 5632 | |
| 5633 | return ObjCTypeParamList::create(ctx&: Importer.getToContext(), |
| 5634 | lAngleLoc: *LAngleLocOrErr, |
| 5635 | typeParams: toTypeParams, |
| 5636 | rAngleLoc: *RAngleLocOrErr); |
| 5637 | } |
| 5638 | |
| 5639 | ExpectedDecl ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
| 5640 | // If this class has a definition in the translation unit we're coming from, |
| 5641 | // but this particular declaration is not that definition, import the |
| 5642 | // definition and map to that. |
| 5643 | ObjCInterfaceDecl *Definition = D->getDefinition(); |
| 5644 | if (Definition && Definition != D) { |
| 5645 | if (ExpectedDecl ImportedDefOrErr = import(From: Definition)) |
| 5646 | return Importer.MapImported(D, *ImportedDefOrErr); |
| 5647 | else |
| 5648 | return ImportedDefOrErr.takeError(); |
| 5649 | } |
| 5650 | |
| 5651 | // Import the major distinguishing characteristics of an @interface. |
| 5652 | DeclContext *DC, *LexicalDC; |
| 5653 | DeclarationName Name; |
| 5654 | SourceLocation Loc; |
| 5655 | NamedDecl *ToD; |
| 5656 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 5657 | return std::move(Err); |
| 5658 | if (ToD) |
| 5659 | return ToD; |
| 5660 | |
| 5661 | // Look for an existing interface with the same name. |
| 5662 | ObjCInterfaceDecl *MergeWithIface = nullptr; |
| 5663 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 5664 | for (auto *FoundDecl : FoundDecls) { |
| 5665 | if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
| 5666 | continue; |
| 5667 | |
| 5668 | if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(Val: FoundDecl))) |
| 5669 | break; |
| 5670 | } |
| 5671 | |
| 5672 | // Create an interface declaration, if one does not already exist. |
| 5673 | ObjCInterfaceDecl *ToIface = MergeWithIface; |
| 5674 | if (!ToIface) { |
| 5675 | ExpectedSLoc AtBeginLocOrErr = import(D->getAtStartLoc()); |
| 5676 | if (!AtBeginLocOrErr) |
| 5677 | return AtBeginLocOrErr.takeError(); |
| 5678 | |
| 5679 | if (GetImportedOrCreateDecl( |
| 5680 | ToD&: ToIface, FromD: D, args&: Importer.getToContext(), args&: DC, |
| 5681 | args&: *AtBeginLocOrErr, args: Name.getAsIdentifierInfo(), |
| 5682 | /*TypeParamList=*/args: nullptr, |
| 5683 | /*PrevDecl=*/args: nullptr, args&: Loc, args: D->isImplicitInterfaceDecl())) |
| 5684 | return ToIface; |
| 5685 | ToIface->setLexicalDeclContext(LexicalDC); |
| 5686 | LexicalDC->addDeclInternal(ToIface); |
| 5687 | } |
| 5688 | Importer.MapImported(D, ToIface); |
| 5689 | // Import the type parameter list after MapImported, to avoid |
| 5690 | // loops when bringing in their DeclContext. |
| 5691 | if (auto ToPListOrErr = |
| 5692 | ImportObjCTypeParamList(list: D->getTypeParamListAsWritten())) |
| 5693 | ToIface->setTypeParamList(*ToPListOrErr); |
| 5694 | else |
| 5695 | return ToPListOrErr.takeError(); |
| 5696 | |
| 5697 | if (D->isThisDeclarationADefinition()) |
| 5698 | if (Error Err = ImportDefinition(From: D, To: ToIface)) |
| 5699 | return std::move(Err); |
| 5700 | |
| 5701 | return ToIface; |
| 5702 | } |
| 5703 | |
| 5704 | ExpectedDecl |
| 5705 | ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 5706 | ObjCCategoryDecl *Category; |
| 5707 | if (Error Err = importInto(To&: Category, From: D->getCategoryDecl())) |
| 5708 | return std::move(Err); |
| 5709 | |
| 5710 | ObjCCategoryImplDecl *ToImpl = Category->getImplementation(); |
| 5711 | if (!ToImpl) { |
| 5712 | DeclContext *DC, *LexicalDC; |
| 5713 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) |
| 5714 | return std::move(Err); |
| 5715 | |
| 5716 | Error Err = Error::success(); |
| 5717 | auto ToLocation = importChecked(Err, D->getLocation()); |
| 5718 | auto ToAtStartLoc = importChecked(Err, D->getAtStartLoc()); |
| 5719 | auto ToCategoryNameLoc = importChecked(Err, From: D->getCategoryNameLoc()); |
| 5720 | if (Err) |
| 5721 | return std::move(Err); |
| 5722 | |
| 5723 | if (GetImportedOrCreateDecl( |
| 5724 | ToImpl, D, Importer.getToContext(), DC, |
| 5725 | Importer.Import(D->getIdentifier()), Category->getClassInterface(), |
| 5726 | ToLocation, ToAtStartLoc, ToCategoryNameLoc)) |
| 5727 | return ToImpl; |
| 5728 | |
| 5729 | ToImpl->setLexicalDeclContext(LexicalDC); |
| 5730 | LexicalDC->addDeclInternal(ToImpl); |
| 5731 | Category->setImplementation(ToImpl); |
| 5732 | } |
| 5733 | |
| 5734 | Importer.MapImported(D, ToImpl); |
| 5735 | if (Error Err = ImportDeclContext(D)) |
| 5736 | return std::move(Err); |
| 5737 | |
| 5738 | return ToImpl; |
| 5739 | } |
| 5740 | |
| 5741 | ExpectedDecl |
| 5742 | ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 5743 | // Find the corresponding interface. |
| 5744 | ObjCInterfaceDecl *Iface; |
| 5745 | if (Error Err = importInto(Iface, D->getClassInterface())) |
| 5746 | return std::move(Err); |
| 5747 | |
| 5748 | // Import the superclass, if any. |
| 5749 | ObjCInterfaceDecl *Super; |
| 5750 | if (Error Err = importInto(To&: Super, From: D->getSuperClass())) |
| 5751 | return std::move(Err); |
| 5752 | |
| 5753 | ObjCImplementationDecl *Impl = Iface->getImplementation(); |
| 5754 | if (!Impl) { |
| 5755 | // We haven't imported an implementation yet. Create a new @implementation |
| 5756 | // now. |
| 5757 | DeclContext *DC, *LexicalDC; |
| 5758 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) |
| 5759 | return std::move(Err); |
| 5760 | |
| 5761 | Error Err = Error::success(); |
| 5762 | auto ToLocation = importChecked(Err, D->getLocation()); |
| 5763 | auto ToAtStartLoc = importChecked(Err, D->getAtStartLoc()); |
| 5764 | auto ToSuperClassLoc = importChecked(Err, From: D->getSuperClassLoc()); |
| 5765 | auto ToIvarLBraceLoc = importChecked(Err, From: D->getIvarLBraceLoc()); |
| 5766 | auto ToIvarRBraceLoc = importChecked(Err, From: D->getIvarRBraceLoc()); |
| 5767 | if (Err) |
| 5768 | return std::move(Err); |
| 5769 | |
| 5770 | if (GetImportedOrCreateDecl(Impl, D, Importer.getToContext(), |
| 5771 | DC, Iface, Super, |
| 5772 | ToLocation, |
| 5773 | ToAtStartLoc, |
| 5774 | ToSuperClassLoc, |
| 5775 | ToIvarLBraceLoc, |
| 5776 | ToIvarRBraceLoc)) |
| 5777 | return Impl; |
| 5778 | |
| 5779 | Impl->setLexicalDeclContext(LexicalDC); |
| 5780 | |
| 5781 | // Associate the implementation with the class it implements. |
| 5782 | Iface->setImplementation(Impl); |
| 5783 | Importer.MapImported(D, Iface->getImplementation()); |
| 5784 | } else { |
| 5785 | Importer.MapImported(D, Iface->getImplementation()); |
| 5786 | |
| 5787 | // Verify that the existing @implementation has the same superclass. |
| 5788 | if ((Super && !Impl->getSuperClass()) || |
| 5789 | (!Super && Impl->getSuperClass()) || |
| 5790 | (Super && Impl->getSuperClass() && |
| 5791 | !declaresSameEntity(Super->getCanonicalDecl(), |
| 5792 | Impl->getSuperClass()))) { |
| 5793 | Importer.ToDiag(Impl->getLocation(), |
| 5794 | diag::warn_odr_objc_superclass_inconsistent) |
| 5795 | << Iface->getDeclName(); |
| 5796 | // FIXME: It would be nice to have the location of the superclass |
| 5797 | // below. |
| 5798 | if (Impl->getSuperClass()) |
| 5799 | Importer.ToDiag(Impl->getLocation(), |
| 5800 | diag::note_odr_objc_superclass) |
| 5801 | << Impl->getSuperClass()->getDeclName(); |
| 5802 | else |
| 5803 | Importer.ToDiag(Impl->getLocation(), |
| 5804 | diag::note_odr_objc_missing_superclass); |
| 5805 | if (D->getSuperClass()) |
| 5806 | Importer.FromDiag(D->getLocation(), |
| 5807 | diag::note_odr_objc_superclass) |
| 5808 | << D->getSuperClass()->getDeclName(); |
| 5809 | else |
| 5810 | Importer.FromDiag(D->getLocation(), |
| 5811 | diag::note_odr_objc_missing_superclass); |
| 5812 | |
| 5813 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 5814 | } |
| 5815 | } |
| 5816 | |
| 5817 | // Import all of the members of this @implementation. |
| 5818 | if (Error Err = ImportDeclContext(D)) |
| 5819 | return std::move(Err); |
| 5820 | |
| 5821 | return Impl; |
| 5822 | } |
| 5823 | |
| 5824 | ExpectedDecl ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { |
| 5825 | // Import the major distinguishing characteristics of an @property. |
| 5826 | DeclContext *DC, *LexicalDC; |
| 5827 | DeclarationName Name; |
| 5828 | SourceLocation Loc; |
| 5829 | NamedDecl *ToD; |
| 5830 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 5831 | return std::move(Err); |
| 5832 | if (ToD) |
| 5833 | return ToD; |
| 5834 | |
| 5835 | // Check whether we have already imported this property. |
| 5836 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 5837 | for (auto *FoundDecl : FoundDecls) { |
| 5838 | if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(Val: FoundDecl)) { |
| 5839 | // Instance and class properties can share the same name but are different |
| 5840 | // declarations. |
| 5841 | if (FoundProp->isInstanceProperty() != D->isInstanceProperty()) |
| 5842 | continue; |
| 5843 | |
| 5844 | // Check property types. |
| 5845 | if (!Importer.IsStructurallyEquivalent(From: D->getType(), |
| 5846 | To: FoundProp->getType())) { |
| 5847 | Importer.ToDiag(Loc, diag::warn_odr_objc_property_type_inconsistent) |
| 5848 | << Name << D->getType() << FoundProp->getType(); |
| 5849 | Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here) |
| 5850 | << FoundProp->getType(); |
| 5851 | |
| 5852 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 5853 | } |
| 5854 | |
| 5855 | // FIXME: Check property attributes, getters, setters, etc.? |
| 5856 | |
| 5857 | // Consider these properties to be equivalent. |
| 5858 | Importer.MapImported(D, FoundProp); |
| 5859 | return FoundProp; |
| 5860 | } |
| 5861 | } |
| 5862 | |
| 5863 | Error Err = Error::success(); |
| 5864 | auto ToType = importChecked(Err, From: D->getType()); |
| 5865 | auto ToTypeSourceInfo = importChecked(Err, From: D->getTypeSourceInfo()); |
| 5866 | auto ToAtLoc = importChecked(Err, From: D->getAtLoc()); |
| 5867 | auto ToLParenLoc = importChecked(Err, From: D->getLParenLoc()); |
| 5868 | if (Err) |
| 5869 | return std::move(Err); |
| 5870 | |
| 5871 | // Create the new property. |
| 5872 | ObjCPropertyDecl *ToProperty; |
| 5873 | if (GetImportedOrCreateDecl( |
| 5874 | ToD&: ToProperty, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, |
| 5875 | args: Name.getAsIdentifierInfo(), args&: ToAtLoc, |
| 5876 | args&: ToLParenLoc, args&: ToType, |
| 5877 | args&: ToTypeSourceInfo, args: D->getPropertyImplementation())) |
| 5878 | return ToProperty; |
| 5879 | |
| 5880 | auto ToGetterName = importChecked(Err, From: D->getGetterName()); |
| 5881 | auto ToSetterName = importChecked(Err, From: D->getSetterName()); |
| 5882 | auto ToGetterNameLoc = importChecked(Err, From: D->getGetterNameLoc()); |
| 5883 | auto ToSetterNameLoc = importChecked(Err, From: D->getSetterNameLoc()); |
| 5884 | auto ToGetterMethodDecl = importChecked(Err, From: D->getGetterMethodDecl()); |
| 5885 | auto ToSetterMethodDecl = importChecked(Err, From: D->getSetterMethodDecl()); |
| 5886 | auto ToPropertyIvarDecl = importChecked(Err, From: D->getPropertyIvarDecl()); |
| 5887 | if (Err) |
| 5888 | return std::move(Err); |
| 5889 | |
| 5890 | ToProperty->setLexicalDeclContext(LexicalDC); |
| 5891 | LexicalDC->addDeclInternal(ToProperty); |
| 5892 | |
| 5893 | ToProperty->setPropertyAttributes(D->getPropertyAttributes()); |
| 5894 | ToProperty->setPropertyAttributesAsWritten( |
| 5895 | D->getPropertyAttributesAsWritten()); |
| 5896 | ToProperty->setGetterName(Sel: ToGetterName, Loc: ToGetterNameLoc); |
| 5897 | ToProperty->setSetterName(Sel: ToSetterName, Loc: ToSetterNameLoc); |
| 5898 | ToProperty->setGetterMethodDecl(ToGetterMethodDecl); |
| 5899 | ToProperty->setSetterMethodDecl(ToSetterMethodDecl); |
| 5900 | ToProperty->setPropertyIvarDecl(ToPropertyIvarDecl); |
| 5901 | return ToProperty; |
| 5902 | } |
| 5903 | |
| 5904 | ExpectedDecl |
| 5905 | ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { |
| 5906 | ObjCPropertyDecl *Property; |
| 5907 | if (Error Err = importInto(To&: Property, From: D->getPropertyDecl())) |
| 5908 | return std::move(Err); |
| 5909 | |
| 5910 | DeclContext *DC, *LexicalDC; |
| 5911 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) |
| 5912 | return std::move(Err); |
| 5913 | |
| 5914 | auto *InImpl = cast<ObjCImplDecl>(Val: LexicalDC); |
| 5915 | |
| 5916 | // Import the ivar (for an @synthesize). |
| 5917 | ObjCIvarDecl *Ivar = nullptr; |
| 5918 | if (Error Err = importInto(To&: Ivar, From: D->getPropertyIvarDecl())) |
| 5919 | return std::move(Err); |
| 5920 | |
| 5921 | ObjCPropertyImplDecl *ToImpl |
| 5922 | = InImpl->FindPropertyImplDecl(propertyId: Property->getIdentifier(), |
| 5923 | queryKind: Property->getQueryKind()); |
| 5924 | if (!ToImpl) { |
| 5925 | |
| 5926 | Error Err = Error::success(); |
| 5927 | auto ToBeginLoc = importChecked(Err, From: D->getBeginLoc()); |
| 5928 | auto ToLocation = importChecked(Err, D->getLocation()); |
| 5929 | auto ToPropertyIvarDeclLoc = |
| 5930 | importChecked(Err, From: D->getPropertyIvarDeclLoc()); |
| 5931 | if (Err) |
| 5932 | return std::move(Err); |
| 5933 | |
| 5934 | if (GetImportedOrCreateDecl(ToImpl, D, Importer.getToContext(), DC, |
| 5935 | ToBeginLoc, |
| 5936 | ToLocation, Property, |
| 5937 | D->getPropertyImplementation(), Ivar, |
| 5938 | ToPropertyIvarDeclLoc)) |
| 5939 | return ToImpl; |
| 5940 | |
| 5941 | ToImpl->setLexicalDeclContext(LexicalDC); |
| 5942 | LexicalDC->addDeclInternal(ToImpl); |
| 5943 | } else { |
| 5944 | // Check that we have the same kind of property implementation (@synthesize |
| 5945 | // vs. @dynamic). |
| 5946 | if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) { |
| 5947 | Importer.ToDiag(ToImpl->getLocation(), |
| 5948 | diag::warn_odr_objc_property_impl_kind_inconsistent) |
| 5949 | << Property->getDeclName() |
| 5950 | << (ToImpl->getPropertyImplementation() |
| 5951 | == ObjCPropertyImplDecl::Dynamic); |
| 5952 | Importer.FromDiag(D->getLocation(), |
| 5953 | diag::note_odr_objc_property_impl_kind) |
| 5954 | << D->getPropertyDecl()->getDeclName() |
| 5955 | << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); |
| 5956 | |
| 5957 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 5958 | } |
| 5959 | |
| 5960 | // For @synthesize, check that we have the same |
| 5961 | if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize && |
| 5962 | Ivar != ToImpl->getPropertyIvarDecl()) { |
| 5963 | Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(), |
| 5964 | diag::warn_odr_objc_synthesize_ivar_inconsistent) |
| 5965 | << Property->getDeclName() |
| 5966 | << ToImpl->getPropertyIvarDecl()->getDeclName() |
| 5967 | << Ivar->getDeclName(); |
| 5968 | Importer.FromDiag(D->getPropertyIvarDeclLoc(), |
| 5969 | diag::note_odr_objc_synthesize_ivar_here) |
| 5970 | << D->getPropertyIvarDecl()->getDeclName(); |
| 5971 | |
| 5972 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 5973 | } |
| 5974 | |
| 5975 | // Merge the existing implementation with the new implementation. |
| 5976 | Importer.MapImported(D, ToImpl); |
| 5977 | } |
| 5978 | |
| 5979 | return ToImpl; |
| 5980 | } |
| 5981 | |
| 5982 | ExpectedDecl |
| 5983 | ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
| 5984 | // For template arguments, we adopt the translation unit as our declaration |
| 5985 | // context. This context will be fixed when (during) the actual template |
| 5986 | // declaration is created. |
| 5987 | |
| 5988 | ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc()); |
| 5989 | if (!BeginLocOrErr) |
| 5990 | return BeginLocOrErr.takeError(); |
| 5991 | |
| 5992 | ExpectedSLoc LocationOrErr = import(D->getLocation()); |
| 5993 | if (!LocationOrErr) |
| 5994 | return LocationOrErr.takeError(); |
| 5995 | |
| 5996 | TemplateTypeParmDecl *ToD = nullptr; |
| 5997 | if (GetImportedOrCreateDecl( |
| 5998 | ToD, D, Importer.getToContext(), |
| 5999 | Importer.getToContext().getTranslationUnitDecl(), |
| 6000 | *BeginLocOrErr, *LocationOrErr, |
| 6001 | D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()), |
| 6002 | D->wasDeclaredWithTypename(), D->isParameterPack(), |
| 6003 | D->hasTypeConstraint())) |
| 6004 | return ToD; |
| 6005 | |
| 6006 | // Import the type-constraint |
| 6007 | if (const TypeConstraint *TC = D->getTypeConstraint()) { |
| 6008 | |
| 6009 | Error Err = Error::success(); |
| 6010 | auto ToConceptRef = importChecked(Err, From: TC->getConceptReference()); |
| 6011 | auto ToIDC = importChecked(Err, From: TC->getImmediatelyDeclaredConstraint()); |
| 6012 | if (Err) |
| 6013 | return std::move(Err); |
| 6014 | |
| 6015 | ToD->setTypeConstraint(CR: ToConceptRef, ImmediatelyDeclaredConstraint: ToIDC, ArgPackSubstIndex: TC->getArgPackSubstIndex()); |
| 6016 | } |
| 6017 | |
| 6018 | if (Error Err = importTemplateParameterDefaultArgument(D, ToD)) |
| 6019 | return Err; |
| 6020 | |
| 6021 | return ToD; |
| 6022 | } |
| 6023 | |
| 6024 | ExpectedDecl |
| 6025 | ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
| 6026 | |
| 6027 | Error Err = Error::success(); |
| 6028 | auto ToDeclName = importChecked(Err, D->getDeclName()); |
| 6029 | auto ToLocation = importChecked(Err, D->getLocation()); |
| 6030 | auto ToType = importChecked(Err, D->getType()); |
| 6031 | auto ToTypeSourceInfo = importChecked(Err, D->getTypeSourceInfo()); |
| 6032 | auto ToInnerLocStart = importChecked(Err, D->getInnerLocStart()); |
| 6033 | if (Err) |
| 6034 | return std::move(Err); |
| 6035 | |
| 6036 | NonTypeTemplateParmDecl *ToD = nullptr; |
| 6037 | if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), |
| 6038 | Importer.getToContext().getTranslationUnitDecl(), |
| 6039 | ToInnerLocStart, ToLocation, D->getDepth(), |
| 6040 | D->getPosition(), |
| 6041 | ToDeclName.getAsIdentifierInfo(), ToType, |
| 6042 | D->isParameterPack(), ToTypeSourceInfo)) |
| 6043 | return ToD; |
| 6044 | |
| 6045 | Err = importTemplateParameterDefaultArgument(D, ToD); |
| 6046 | if (Err) |
| 6047 | return Err; |
| 6048 | |
| 6049 | return ToD; |
| 6050 | } |
| 6051 | |
| 6052 | ExpectedDecl |
| 6053 | ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
| 6054 | bool IsCanonical = false; |
| 6055 | if (auto *CanonD = Importer.getFromContext() |
| 6056 | .findCanonicalTemplateTemplateParmDeclInternal(TTP: D); |
| 6057 | CanonD == D) |
| 6058 | IsCanonical = true; |
| 6059 | |
| 6060 | // Import the name of this declaration. |
| 6061 | auto NameOrErr = import(D->getDeclName()); |
| 6062 | if (!NameOrErr) |
| 6063 | return NameOrErr.takeError(); |
| 6064 | |
| 6065 | // Import the location of this declaration. |
| 6066 | ExpectedSLoc LocationOrErr = import(D->getLocation()); |
| 6067 | if (!LocationOrErr) |
| 6068 | return LocationOrErr.takeError(); |
| 6069 | |
| 6070 | // Import template parameters. |
| 6071 | auto TemplateParamsOrErr = import(D->getTemplateParameters()); |
| 6072 | if (!TemplateParamsOrErr) |
| 6073 | return TemplateParamsOrErr.takeError(); |
| 6074 | |
| 6075 | TemplateTemplateParmDecl *ToD = nullptr; |
| 6076 | if (GetImportedOrCreateDecl( |
| 6077 | ToD, D, Importer.getToContext(), |
| 6078 | Importer.getToContext().getTranslationUnitDecl(), *LocationOrErr, |
| 6079 | D->getDepth(), D->getPosition(), D->isParameterPack(), |
| 6080 | (*NameOrErr).getAsIdentifierInfo(), D->wasDeclaredWithTypename(), |
| 6081 | *TemplateParamsOrErr)) |
| 6082 | return ToD; |
| 6083 | |
| 6084 | if (Error Err = importTemplateParameterDefaultArgument(D, ToD)) |
| 6085 | return Err; |
| 6086 | |
| 6087 | if (IsCanonical) |
| 6088 | return Importer.getToContext() |
| 6089 | .insertCanonicalTemplateTemplateParmDeclInternal(CanonTTP: ToD); |
| 6090 | |
| 6091 | return ToD; |
| 6092 | } |
| 6093 | |
| 6094 | // Returns the definition for a (forward) declaration of a TemplateDecl, if |
| 6095 | // it has any definition in the redecl chain. |
| 6096 | template <typename T> static auto getTemplateDefinition(T *D) -> T * { |
| 6097 | assert(D->getTemplatedDecl() && "Should be called on templates only" ); |
| 6098 | auto *ToTemplatedDef = D->getTemplatedDecl()->getDefinition(); |
| 6099 | if (!ToTemplatedDef) |
| 6100 | return nullptr; |
| 6101 | auto *TemplateWithDef = ToTemplatedDef->getDescribedTemplate(); |
| 6102 | return cast_or_null<T>(TemplateWithDef); |
| 6103 | } |
| 6104 | |
| 6105 | ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 6106 | |
| 6107 | // Import the major distinguishing characteristics of this class template. |
| 6108 | DeclContext *DC, *LexicalDC; |
| 6109 | DeclarationName Name; |
| 6110 | SourceLocation Loc; |
| 6111 | NamedDecl *ToD; |
| 6112 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 6113 | return std::move(Err); |
| 6114 | if (ToD) |
| 6115 | return ToD; |
| 6116 | |
| 6117 | // Should check if a declaration is friend in a dependent context. |
| 6118 | // Such templates are not linked together in a declaration chain. |
| 6119 | // The ASTImporter strategy is to map existing forward declarations to |
| 6120 | // imported ones only if strictly necessary, otherwise import these as new |
| 6121 | // forward declarations. In case of the "dependent friend" declarations, new |
| 6122 | // declarations are created, but not linked in a declaration chain. |
| 6123 | auto IsDependentFriend = [](ClassTemplateDecl *TD) { |
| 6124 | return TD->getFriendObjectKind() != Decl::FOK_None && |
| 6125 | TD->getLexicalDeclContext()->isDependentContext(); |
| 6126 | }; |
| 6127 | bool DependentFriend = IsDependentFriend(D); |
| 6128 | |
| 6129 | ClassTemplateDecl *FoundByLookup = nullptr; |
| 6130 | |
| 6131 | // We may already have a template of the same name; try to find and match it. |
| 6132 | if (!DC->isFunctionOrMethod()) { |
| 6133 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 6134 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 6135 | for (auto *FoundDecl : FoundDecls) { |
| 6136 | if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary | |
| 6137 | Decl::IDNS_TagFriend)) |
| 6138 | continue; |
| 6139 | |
| 6140 | auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Val: FoundDecl); |
| 6141 | if (FoundTemplate) { |
| 6142 | if (!hasSameVisibilityContextAndLinkage(Found: FoundTemplate, From: D)) |
| 6143 | continue; |
| 6144 | |
| 6145 | // FIXME: sufficient condition for 'IgnoreTemplateParmDepth'? |
| 6146 | bool IgnoreTemplateParmDepth = |
| 6147 | (FoundTemplate->getFriendObjectKind() != Decl::FOK_None) != |
| 6148 | (D->getFriendObjectKind() != Decl::FOK_None); |
| 6149 | if (IsStructuralMatch(D, FoundTemplate, /*Complain=*/true, |
| 6150 | IgnoreTemplateParmDepth)) { |
| 6151 | if (DependentFriend || IsDependentFriend(FoundTemplate)) |
| 6152 | continue; |
| 6153 | |
| 6154 | ClassTemplateDecl *TemplateWithDef = |
| 6155 | getTemplateDefinition(D: FoundTemplate); |
| 6156 | if (D->isThisDeclarationADefinition() && TemplateWithDef) |
| 6157 | return Importer.MapImported(D, TemplateWithDef); |
| 6158 | if (!FoundByLookup) |
| 6159 | FoundByLookup = FoundTemplate; |
| 6160 | // Search in all matches because there may be multiple decl chains, |
| 6161 | // see ASTTests test ImportExistingFriendClassTemplateDef. |
| 6162 | continue; |
| 6163 | } |
| 6164 | // When importing a friend, it is possible that multiple declarations |
| 6165 | // with same name can co-exist in specific cases (if a template contains |
| 6166 | // a friend template and has a specialization). For this case the |
| 6167 | // declarations should match, except that the "template depth" is |
| 6168 | // different. No linking of previous declaration is needed in this case. |
| 6169 | // FIXME: This condition may need refinement. |
| 6170 | if (D->getFriendObjectKind() != Decl::FOK_None && |
| 6171 | FoundTemplate->getFriendObjectKind() != Decl::FOK_None && |
| 6172 | D->getFriendObjectKind() != FoundTemplate->getFriendObjectKind() && |
| 6173 | IsStructuralMatch(D, FoundTemplate, /*Complain=*/false, |
| 6174 | /*IgnoreTemplateParmDepth=*/true)) |
| 6175 | continue; |
| 6176 | |
| 6177 | ConflictingDecls.push_back(Elt: FoundDecl); |
| 6178 | } |
| 6179 | } |
| 6180 | |
| 6181 | if (!ConflictingDecls.empty()) { |
| 6182 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
| 6183 | Name, DC, IDNS: Decl::IDNS_Ordinary, Decls: ConflictingDecls.data(), |
| 6184 | NumDecls: ConflictingDecls.size()); |
| 6185 | if (NameOrErr) |
| 6186 | Name = NameOrErr.get(); |
| 6187 | else |
| 6188 | return NameOrErr.takeError(); |
| 6189 | } |
| 6190 | } |
| 6191 | |
| 6192 | CXXRecordDecl *FromTemplated = D->getTemplatedDecl(); |
| 6193 | |
| 6194 | auto TemplateParamsOrErr = import(D->getTemplateParameters()); |
| 6195 | if (!TemplateParamsOrErr) |
| 6196 | return TemplateParamsOrErr.takeError(); |
| 6197 | |
| 6198 | // Create the declaration that is being templated. |
| 6199 | CXXRecordDecl *ToTemplated; |
| 6200 | if (Error Err = importInto(To&: ToTemplated, From: FromTemplated)) |
| 6201 | return std::move(Err); |
| 6202 | |
| 6203 | // Create the class template declaration itself. |
| 6204 | ClassTemplateDecl *D2; |
| 6205 | if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name, |
| 6206 | *TemplateParamsOrErr, ToTemplated)) |
| 6207 | return D2; |
| 6208 | |
| 6209 | ToTemplated->setDescribedClassTemplate(D2); |
| 6210 | |
| 6211 | D2->setAccess(D->getAccess()); |
| 6212 | D2->setLexicalDeclContext(LexicalDC); |
| 6213 | |
| 6214 | addDeclToContexts(D, D2); |
| 6215 | updateLookupTableForTemplateParameters(**TemplateParamsOrErr); |
| 6216 | |
| 6217 | if (FoundByLookup) { |
| 6218 | auto *Recent = |
| 6219 | const_cast<ClassTemplateDecl *>(FoundByLookup->getMostRecentDecl()); |
| 6220 | |
| 6221 | // It is possible that during the import of the class template definition |
| 6222 | // we start the import of a fwd friend decl of the very same class template |
| 6223 | // and we add the fwd friend decl to the lookup table. But the ToTemplated |
| 6224 | // had been created earlier and by that time the lookup could not find |
| 6225 | // anything existing, so it has no previous decl. Later, (still during the |
| 6226 | // import of the fwd friend decl) we start to import the definition again |
| 6227 | // and this time the lookup finds the previous fwd friend class template. |
| 6228 | // In this case we must set up the previous decl for the templated decl. |
| 6229 | if (!ToTemplated->getPreviousDecl()) { |
| 6230 | assert(FoundByLookup->getTemplatedDecl() && |
| 6231 | "Found decl must have its templated decl set" ); |
| 6232 | CXXRecordDecl *PrevTemplated = |
| 6233 | FoundByLookup->getTemplatedDecl()->getMostRecentDecl(); |
| 6234 | if (ToTemplated != PrevTemplated) |
| 6235 | ToTemplated->setPreviousDecl(PrevTemplated); |
| 6236 | } |
| 6237 | |
| 6238 | D2->setPreviousDecl(Recent); |
| 6239 | } |
| 6240 | |
| 6241 | return D2; |
| 6242 | } |
| 6243 | |
| 6244 | ExpectedDecl ASTNodeImporter::VisitClassTemplateSpecializationDecl( |
| 6245 | ClassTemplateSpecializationDecl *D) { |
| 6246 | ClassTemplateDecl *ClassTemplate; |
| 6247 | if (Error Err = importInto(To&: ClassTemplate, From: D->getSpecializedTemplate())) |
| 6248 | return std::move(Err); |
| 6249 | |
| 6250 | // Import the context of this declaration. |
| 6251 | DeclContext *DC, *LexicalDC; |
| 6252 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) |
| 6253 | return std::move(Err); |
| 6254 | |
| 6255 | // Import template arguments. |
| 6256 | SmallVector<TemplateArgument, 2> TemplateArgs; |
| 6257 | if (Error Err = |
| 6258 | ImportTemplateArguments(FromArgs: D->getTemplateArgs().asArray(), ToArgs&: TemplateArgs)) |
| 6259 | return std::move(Err); |
| 6260 | // Try to find an existing specialization with these template arguments and |
| 6261 | // template parameter list. |
| 6262 | void *InsertPos = nullptr; |
| 6263 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; |
| 6264 | ClassTemplatePartialSpecializationDecl *PartialSpec = |
| 6265 | dyn_cast<ClassTemplatePartialSpecializationDecl>(Val: D); |
| 6266 | |
| 6267 | // Import template parameters. |
| 6268 | TemplateParameterList *ToTPList = nullptr; |
| 6269 | |
| 6270 | if (PartialSpec) { |
| 6271 | auto ToTPListOrErr = import(From: PartialSpec->getTemplateParameters()); |
| 6272 | if (!ToTPListOrErr) |
| 6273 | return ToTPListOrErr.takeError(); |
| 6274 | ToTPList = *ToTPListOrErr; |
| 6275 | PrevDecl = ClassTemplate->findPartialSpecialization(Args: TemplateArgs, |
| 6276 | TPL: *ToTPListOrErr, |
| 6277 | InsertPos); |
| 6278 | } else |
| 6279 | PrevDecl = ClassTemplate->findSpecialization(Args: TemplateArgs, InsertPos); |
| 6280 | |
| 6281 | if (PrevDecl) { |
| 6282 | if (IsStructuralMatch(D, PrevDecl)) { |
| 6283 | CXXRecordDecl *PrevDefinition = PrevDecl->getDefinition(); |
| 6284 | if (D->isThisDeclarationADefinition() && PrevDefinition) { |
| 6285 | Importer.MapImported(D, PrevDefinition); |
| 6286 | // Import those default field initializers which have been |
| 6287 | // instantiated in the "From" context, but not in the "To" context. |
| 6288 | for (auto *FromField : D->fields()) { |
| 6289 | auto ToOrErr = import(FromField); |
| 6290 | if (!ToOrErr) |
| 6291 | return ToOrErr.takeError(); |
| 6292 | } |
| 6293 | |
| 6294 | // Import those methods which have been instantiated in the |
| 6295 | // "From" context, but not in the "To" context. |
| 6296 | for (CXXMethodDecl *FromM : D->methods()) { |
| 6297 | auto ToOrErr = import(FromM); |
| 6298 | if (!ToOrErr) |
| 6299 | return ToOrErr.takeError(); |
| 6300 | } |
| 6301 | |
| 6302 | // TODO Import instantiated default arguments. |
| 6303 | // TODO Import instantiated exception specifications. |
| 6304 | // |
| 6305 | // Generally, ASTCommon.h/DeclUpdateKind enum gives a very good hint |
| 6306 | // what else could be fused during an AST merge. |
| 6307 | return PrevDefinition; |
| 6308 | } |
| 6309 | } else { // ODR violation. |
| 6310 | // FIXME HandleNameConflict |
| 6311 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 6312 | } |
| 6313 | } |
| 6314 | |
| 6315 | // Import the location of this declaration. |
| 6316 | ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc()); |
| 6317 | if (!BeginLocOrErr) |
| 6318 | return BeginLocOrErr.takeError(); |
| 6319 | ExpectedSLoc IdLocOrErr = import(D->getLocation()); |
| 6320 | if (!IdLocOrErr) |
| 6321 | return IdLocOrErr.takeError(); |
| 6322 | |
| 6323 | // Import TemplateArgumentListInfo. |
| 6324 | TemplateArgumentListInfo ToTAInfo; |
| 6325 | if (const auto *ASTTemplateArgs = D->getTemplateArgsAsWritten()) { |
| 6326 | if (Error Err = ImportTemplateArgumentListInfo(Container: *ASTTemplateArgs, ToTAInfo)) |
| 6327 | return std::move(Err); |
| 6328 | } |
| 6329 | |
| 6330 | // Create the specialization. |
| 6331 | ClassTemplateSpecializationDecl *D2 = nullptr; |
| 6332 | if (PartialSpec) { |
| 6333 | QualType CanonInjType; |
| 6334 | if (Error Err = importInto( |
| 6335 | To&: CanonInjType, From: PartialSpec->getInjectedSpecializationType())) |
| 6336 | return std::move(Err); |
| 6337 | CanonInjType = CanonInjType.getCanonicalType(); |
| 6338 | |
| 6339 | if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>( |
| 6340 | D2, D, Importer.getToContext(), D->getTagKind(), DC, *BeginLocOrErr, |
| 6341 | *IdLocOrErr, ToTPList, ClassTemplate, |
| 6342 | llvm::ArrayRef(TemplateArgs.data(), TemplateArgs.size()), |
| 6343 | CanonInjType, |
| 6344 | cast_or_null<ClassTemplatePartialSpecializationDecl>(Val: PrevDecl))) |
| 6345 | return D2; |
| 6346 | |
| 6347 | // Update InsertPos, because preceding import calls may have invalidated |
| 6348 | // it by adding new specializations. |
| 6349 | auto *PartSpec2 = cast<ClassTemplatePartialSpecializationDecl>(Val: D2); |
| 6350 | if (!ClassTemplate->findPartialSpecialization(Args: TemplateArgs, TPL: ToTPList, |
| 6351 | InsertPos)) |
| 6352 | // Add this partial specialization to the class template. |
| 6353 | ClassTemplate->AddPartialSpecialization(D: PartSpec2, InsertPos); |
| 6354 | if (Expected<ClassTemplatePartialSpecializationDecl *> ToInstOrErr = |
| 6355 | import(From: PartialSpec->getInstantiatedFromMember())) |
| 6356 | PartSpec2->setInstantiatedFromMember(*ToInstOrErr); |
| 6357 | else |
| 6358 | return ToInstOrErr.takeError(); |
| 6359 | |
| 6360 | updateLookupTableForTemplateParameters(Params&: *ToTPList); |
| 6361 | } else { // Not a partial specialization. |
| 6362 | if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), D->getTagKind(), |
| 6363 | DC, *BeginLocOrErr, *IdLocOrErr, ClassTemplate, |
| 6364 | TemplateArgs, D->hasStrictPackMatch(), |
| 6365 | PrevDecl)) |
| 6366 | return D2; |
| 6367 | |
| 6368 | // Update InsertPos, because preceding import calls may have invalidated |
| 6369 | // it by adding new specializations. |
| 6370 | if (!ClassTemplate->findSpecialization(Args: TemplateArgs, InsertPos)) |
| 6371 | // Add this specialization to the class template. |
| 6372 | ClassTemplate->AddSpecialization(D: D2, InsertPos); |
| 6373 | } |
| 6374 | |
| 6375 | D2->setSpecializationKind(D->getSpecializationKind()); |
| 6376 | |
| 6377 | // Set the context of this specialization/instantiation. |
| 6378 | D2->setLexicalDeclContext(LexicalDC); |
| 6379 | |
| 6380 | // Add to the DC only if it was an explicit specialization/instantiation. |
| 6381 | if (D2->isExplicitInstantiationOrSpecialization()) { |
| 6382 | LexicalDC->addDeclInternal(D2); |
| 6383 | } |
| 6384 | |
| 6385 | if (auto BraceRangeOrErr = import(D->getBraceRange())) |
| 6386 | D2->setBraceRange(*BraceRangeOrErr); |
| 6387 | else |
| 6388 | return BraceRangeOrErr.takeError(); |
| 6389 | |
| 6390 | if (Error Err = ImportTemplateParameterLists(FromD: D, ToD: D2)) |
| 6391 | return std::move(Err); |
| 6392 | |
| 6393 | // Import the qualifier, if any. |
| 6394 | if (auto LocOrErr = import(D->getQualifierLoc())) |
| 6395 | D2->setQualifierInfo(*LocOrErr); |
| 6396 | else |
| 6397 | return LocOrErr.takeError(); |
| 6398 | |
| 6399 | if (D->getTemplateArgsAsWritten()) |
| 6400 | D2->setTemplateArgsAsWritten(ToTAInfo); |
| 6401 | |
| 6402 | if (auto LocOrErr = import(From: D->getTemplateKeywordLoc())) |
| 6403 | D2->setTemplateKeywordLoc(*LocOrErr); |
| 6404 | else |
| 6405 | return LocOrErr.takeError(); |
| 6406 | |
| 6407 | if (auto LocOrErr = import(From: D->getExternKeywordLoc())) |
| 6408 | D2->setExternKeywordLoc(*LocOrErr); |
| 6409 | else |
| 6410 | return LocOrErr.takeError(); |
| 6411 | |
| 6412 | if (D->getPointOfInstantiation().isValid()) { |
| 6413 | if (auto POIOrErr = import(From: D->getPointOfInstantiation())) |
| 6414 | D2->setPointOfInstantiation(*POIOrErr); |
| 6415 | else |
| 6416 | return POIOrErr.takeError(); |
| 6417 | } |
| 6418 | |
| 6419 | D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind()); |
| 6420 | |
| 6421 | if (auto P = D->getInstantiatedFrom()) { |
| 6422 | if (auto *CTD = dyn_cast<ClassTemplateDecl *>(Val&: P)) { |
| 6423 | if (auto CTDorErr = import(From: CTD)) |
| 6424 | D2->setInstantiationOf(*CTDorErr); |
| 6425 | } else { |
| 6426 | auto *CTPSD = cast<ClassTemplatePartialSpecializationDecl *>(Val&: P); |
| 6427 | auto CTPSDOrErr = import(From: CTPSD); |
| 6428 | if (!CTPSDOrErr) |
| 6429 | return CTPSDOrErr.takeError(); |
| 6430 | const TemplateArgumentList &DArgs = D->getTemplateInstantiationArgs(); |
| 6431 | SmallVector<TemplateArgument, 2> D2ArgsVec(DArgs.size()); |
| 6432 | for (unsigned I = 0; I < DArgs.size(); ++I) { |
| 6433 | const TemplateArgument &DArg = DArgs[I]; |
| 6434 | if (auto ArgOrErr = import(From: DArg)) |
| 6435 | D2ArgsVec[I] = *ArgOrErr; |
| 6436 | else |
| 6437 | return ArgOrErr.takeError(); |
| 6438 | } |
| 6439 | D2->setInstantiationOf( |
| 6440 | PartialSpec: *CTPSDOrErr, |
| 6441 | TemplateArgs: TemplateArgumentList::CreateCopy(Context&: Importer.getToContext(), Args: D2ArgsVec)); |
| 6442 | } |
| 6443 | } |
| 6444 | |
| 6445 | if (D->isCompleteDefinition()) |
| 6446 | if (Error Err = ImportDefinition(D, D2)) |
| 6447 | return std::move(Err); |
| 6448 | |
| 6449 | return D2; |
| 6450 | } |
| 6451 | |
| 6452 | ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) { |
| 6453 | // Import the major distinguishing characteristics of this variable template. |
| 6454 | DeclContext *DC, *LexicalDC; |
| 6455 | DeclarationName Name; |
| 6456 | SourceLocation Loc; |
| 6457 | NamedDecl *ToD; |
| 6458 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 6459 | return std::move(Err); |
| 6460 | if (ToD) |
| 6461 | return ToD; |
| 6462 | |
| 6463 | // We may already have a template of the same name; try to find and match it. |
| 6464 | assert(!DC->isFunctionOrMethod() && |
| 6465 | "Variable templates cannot be declared at function scope" ); |
| 6466 | |
| 6467 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 6468 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 6469 | VarTemplateDecl *FoundByLookup = nullptr; |
| 6470 | for (auto *FoundDecl : FoundDecls) { |
| 6471 | if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
| 6472 | continue; |
| 6473 | |
| 6474 | if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Val: FoundDecl)) { |
| 6475 | // Use the templated decl, some linkage flags are set only there. |
| 6476 | if (!hasSameVisibilityContextAndLinkage(Found: FoundTemplate->getTemplatedDecl(), |
| 6477 | From: D->getTemplatedDecl())) |
| 6478 | continue; |
| 6479 | if (IsStructuralMatch(D, FoundTemplate)) { |
| 6480 | // FIXME Check for ODR error if the two definitions have |
| 6481 | // different initializers? |
| 6482 | VarTemplateDecl *FoundDef = getTemplateDefinition(D: FoundTemplate); |
| 6483 | if (D->getDeclContext()->isRecord()) { |
| 6484 | assert(FoundTemplate->getDeclContext()->isRecord() && |
| 6485 | "Member variable template imported as non-member, " |
| 6486 | "inconsistent imported AST?" ); |
| 6487 | if (FoundDef) |
| 6488 | return Importer.MapImported(D, FoundDef); |
| 6489 | if (!D->isThisDeclarationADefinition()) |
| 6490 | return Importer.MapImported(D, FoundTemplate); |
| 6491 | } else { |
| 6492 | if (FoundDef && D->isThisDeclarationADefinition()) |
| 6493 | return Importer.MapImported(D, FoundDef); |
| 6494 | } |
| 6495 | FoundByLookup = FoundTemplate; |
| 6496 | break; |
| 6497 | } |
| 6498 | ConflictingDecls.push_back(Elt: FoundDecl); |
| 6499 | } |
| 6500 | } |
| 6501 | |
| 6502 | if (!ConflictingDecls.empty()) { |
| 6503 | ExpectedName NameOrErr = Importer.HandleNameConflict( |
| 6504 | Name, DC, IDNS: Decl::IDNS_Ordinary, Decls: ConflictingDecls.data(), |
| 6505 | NumDecls: ConflictingDecls.size()); |
| 6506 | if (NameOrErr) |
| 6507 | Name = NameOrErr.get(); |
| 6508 | else |
| 6509 | return NameOrErr.takeError(); |
| 6510 | } |
| 6511 | |
| 6512 | VarDecl *DTemplated = D->getTemplatedDecl(); |
| 6513 | |
| 6514 | // Import the type. |
| 6515 | // FIXME: Value not used? |
| 6516 | ExpectedType TypeOrErr = import(DTemplated->getType()); |
| 6517 | if (!TypeOrErr) |
| 6518 | return TypeOrErr.takeError(); |
| 6519 | |
| 6520 | // Create the declaration that is being templated. |
| 6521 | VarDecl *ToTemplated; |
| 6522 | if (Error Err = importInto(To&: ToTemplated, From: DTemplated)) |
| 6523 | return std::move(Err); |
| 6524 | |
| 6525 | // Create the variable template declaration itself. |
| 6526 | auto TemplateParamsOrErr = import(D->getTemplateParameters()); |
| 6527 | if (!TemplateParamsOrErr) |
| 6528 | return TemplateParamsOrErr.takeError(); |
| 6529 | |
| 6530 | VarTemplateDecl *ToVarTD; |
| 6531 | if (GetImportedOrCreateDecl(ToVarTD, D, Importer.getToContext(), DC, Loc, |
| 6532 | Name, *TemplateParamsOrErr, ToTemplated)) |
| 6533 | return ToVarTD; |
| 6534 | |
| 6535 | ToTemplated->setDescribedVarTemplate(ToVarTD); |
| 6536 | |
| 6537 | ToVarTD->setAccess(D->getAccess()); |
| 6538 | ToVarTD->setLexicalDeclContext(LexicalDC); |
| 6539 | LexicalDC->addDeclInternal(ToVarTD); |
| 6540 | if (DC != Importer.getToContext().getTranslationUnitDecl()) |
| 6541 | updateLookupTableForTemplateParameters(**TemplateParamsOrErr); |
| 6542 | |
| 6543 | if (FoundByLookup) { |
| 6544 | auto *Recent = |
| 6545 | const_cast<VarTemplateDecl *>(FoundByLookup->getMostRecentDecl()); |
| 6546 | if (!ToTemplated->getPreviousDecl()) { |
| 6547 | auto *PrevTemplated = |
| 6548 | FoundByLookup->getTemplatedDecl()->getMostRecentDecl(); |
| 6549 | if (ToTemplated != PrevTemplated) |
| 6550 | ToTemplated->setPreviousDecl(PrevTemplated); |
| 6551 | } |
| 6552 | ToVarTD->setPreviousDecl(Recent); |
| 6553 | } |
| 6554 | |
| 6555 | return ToVarTD; |
| 6556 | } |
| 6557 | |
| 6558 | ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl( |
| 6559 | VarTemplateSpecializationDecl *D) { |
| 6560 | // A VarTemplateSpecializationDecl inherits from VarDecl, the import is done |
| 6561 | // in an analog way (but specialized for this case). |
| 6562 | |
| 6563 | SmallVector<Decl *, 2> Redecls = getCanonicalForwardRedeclChain(D); |
| 6564 | auto RedeclIt = Redecls.begin(); |
| 6565 | // Import the first part of the decl chain. I.e. import all previous |
| 6566 | // declarations starting from the canonical decl. |
| 6567 | for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) { |
| 6568 | ExpectedDecl RedeclOrErr = import(*RedeclIt); |
| 6569 | if (!RedeclOrErr) |
| 6570 | return RedeclOrErr.takeError(); |
| 6571 | } |
| 6572 | assert(*RedeclIt == D); |
| 6573 | |
| 6574 | VarTemplateDecl *VarTemplate = nullptr; |
| 6575 | if (Error Err = importInto(To&: VarTemplate, From: D->getSpecializedTemplate())) |
| 6576 | return std::move(Err); |
| 6577 | |
| 6578 | // Import the context of this declaration. |
| 6579 | DeclContext *DC, *LexicalDC; |
| 6580 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) |
| 6581 | return std::move(Err); |
| 6582 | |
| 6583 | // Import the location of this declaration. |
| 6584 | ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc()); |
| 6585 | if (!BeginLocOrErr) |
| 6586 | return BeginLocOrErr.takeError(); |
| 6587 | |
| 6588 | auto IdLocOrErr = import(D->getLocation()); |
| 6589 | if (!IdLocOrErr) |
| 6590 | return IdLocOrErr.takeError(); |
| 6591 | |
| 6592 | // Import template arguments. |
| 6593 | SmallVector<TemplateArgument, 2> TemplateArgs; |
| 6594 | if (Error Err = |
| 6595 | ImportTemplateArguments(FromArgs: D->getTemplateArgs().asArray(), ToArgs&: TemplateArgs)) |
| 6596 | return std::move(Err); |
| 6597 | |
| 6598 | // Try to find an existing specialization with these template arguments. |
| 6599 | void *InsertPos = nullptr; |
| 6600 | VarTemplateSpecializationDecl *FoundSpecialization = |
| 6601 | VarTemplate->findSpecialization(Args: TemplateArgs, InsertPos); |
| 6602 | if (FoundSpecialization) { |
| 6603 | if (IsStructuralMatch(D, FoundSpecialization)) { |
| 6604 | VarDecl *FoundDef = FoundSpecialization->getDefinition(); |
| 6605 | if (D->getDeclContext()->isRecord()) { |
| 6606 | // In a record, it is allowed only to have one optional declaration and |
| 6607 | // one definition of the (static or constexpr) variable template. |
| 6608 | assert( |
| 6609 | FoundSpecialization->getDeclContext()->isRecord() && |
| 6610 | "Member variable template specialization imported as non-member, " |
| 6611 | "inconsistent imported AST?" ); |
| 6612 | if (FoundDef) |
| 6613 | return Importer.MapImported(D, FoundDef); |
| 6614 | if (!D->isThisDeclarationADefinition()) |
| 6615 | return Importer.MapImported(D, FoundSpecialization); |
| 6616 | } else { |
| 6617 | // If definition is imported and there is already one, map to it. |
| 6618 | // Otherwise create a new variable and link it to the existing. |
| 6619 | if (FoundDef && D->isThisDeclarationADefinition()) |
| 6620 | return Importer.MapImported(D, FoundDef); |
| 6621 | } |
| 6622 | } else { |
| 6623 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 6624 | } |
| 6625 | } |
| 6626 | |
| 6627 | VarTemplateSpecializationDecl *D2 = nullptr; |
| 6628 | |
| 6629 | TemplateArgumentListInfo ToTAInfo; |
| 6630 | if (const auto *Args = D->getTemplateArgsAsWritten()) { |
| 6631 | if (Error Err = ImportTemplateArgumentListInfo(Container: *Args, ToTAInfo)) |
| 6632 | return std::move(Err); |
| 6633 | } |
| 6634 | |
| 6635 | using PartVarSpecDecl = VarTemplatePartialSpecializationDecl; |
| 6636 | // Create a new specialization. |
| 6637 | if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(Val: D)) { |
| 6638 | auto ToTPListOrErr = import(From: FromPartial->getTemplateParameters()); |
| 6639 | if (!ToTPListOrErr) |
| 6640 | return ToTPListOrErr.takeError(); |
| 6641 | |
| 6642 | PartVarSpecDecl *ToPartial; |
| 6643 | if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC, |
| 6644 | *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr, |
| 6645 | VarTemplate, QualType(), nullptr, |
| 6646 | D->getStorageClass(), TemplateArgs)) |
| 6647 | return ToPartial; |
| 6648 | |
| 6649 | if (Expected<PartVarSpecDecl *> ToInstOrErr = |
| 6650 | import(From: FromPartial->getInstantiatedFromMember())) |
| 6651 | ToPartial->setInstantiatedFromMember(*ToInstOrErr); |
| 6652 | else |
| 6653 | return ToInstOrErr.takeError(); |
| 6654 | |
| 6655 | if (FromPartial->isMemberSpecialization()) |
| 6656 | ToPartial->setMemberSpecialization(); |
| 6657 | |
| 6658 | D2 = ToPartial; |
| 6659 | |
| 6660 | // FIXME: Use this update if VarTemplatePartialSpecializationDecl is fixed |
| 6661 | // to adopt template parameters. |
| 6662 | // updateLookupTableForTemplateParameters(**ToTPListOrErr); |
| 6663 | } else { // Full specialization |
| 6664 | if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, |
| 6665 | *BeginLocOrErr, *IdLocOrErr, VarTemplate, |
| 6666 | QualType(), nullptr, D->getStorageClass(), |
| 6667 | TemplateArgs)) |
| 6668 | return D2; |
| 6669 | } |
| 6670 | |
| 6671 | // Update InsertPos, because preceding import calls may have invalidated |
| 6672 | // it by adding new specializations. |
| 6673 | if (!VarTemplate->findSpecialization(Args: TemplateArgs, InsertPos)) |
| 6674 | VarTemplate->AddSpecialization(D: D2, InsertPos); |
| 6675 | |
| 6676 | QualType T; |
| 6677 | if (Error Err = importInto(T, D->getType())) |
| 6678 | return std::move(Err); |
| 6679 | D2->setType(T); |
| 6680 | |
| 6681 | auto TInfoOrErr = import(D->getTypeSourceInfo()); |
| 6682 | if (!TInfoOrErr) |
| 6683 | return TInfoOrErr.takeError(); |
| 6684 | D2->setTypeSourceInfo(*TInfoOrErr); |
| 6685 | |
| 6686 | if (D->getPointOfInstantiation().isValid()) { |
| 6687 | if (ExpectedSLoc POIOrErr = import(From: D->getPointOfInstantiation())) |
| 6688 | D2->setPointOfInstantiation(*POIOrErr); |
| 6689 | else |
| 6690 | return POIOrErr.takeError(); |
| 6691 | } |
| 6692 | |
| 6693 | D2->setSpecializationKind(D->getSpecializationKind()); |
| 6694 | |
| 6695 | if (D->getTemplateArgsAsWritten()) |
| 6696 | D2->setTemplateArgsAsWritten(ToTAInfo); |
| 6697 | |
| 6698 | if (auto LocOrErr = import(D->getQualifierLoc())) |
| 6699 | D2->setQualifierInfo(*LocOrErr); |
| 6700 | else |
| 6701 | return LocOrErr.takeError(); |
| 6702 | |
| 6703 | if (D->isConstexpr()) |
| 6704 | D2->setConstexpr(true); |
| 6705 | |
| 6706 | D2->setAccess(D->getAccess()); |
| 6707 | |
| 6708 | if (Error Err = ImportInitializer(D, D2)) |
| 6709 | return std::move(Err); |
| 6710 | |
| 6711 | if (FoundSpecialization) |
| 6712 | D2->setPreviousDecl(FoundSpecialization->getMostRecentDecl()); |
| 6713 | |
| 6714 | addDeclToContexts(D, D2); |
| 6715 | |
| 6716 | // Import the rest of the chain. I.e. import all subsequent declarations. |
| 6717 | for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) { |
| 6718 | ExpectedDecl RedeclOrErr = import(*RedeclIt); |
| 6719 | if (!RedeclOrErr) |
| 6720 | return RedeclOrErr.takeError(); |
| 6721 | } |
| 6722 | |
| 6723 | return D2; |
| 6724 | } |
| 6725 | |
| 6726 | ExpectedDecl |
| 6727 | ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
| 6728 | DeclContext *DC, *LexicalDC; |
| 6729 | DeclarationName Name; |
| 6730 | SourceLocation Loc; |
| 6731 | NamedDecl *ToD; |
| 6732 | |
| 6733 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| 6734 | return std::move(Err); |
| 6735 | |
| 6736 | if (ToD) |
| 6737 | return ToD; |
| 6738 | |
| 6739 | const FunctionTemplateDecl *FoundByLookup = nullptr; |
| 6740 | |
| 6741 | // Try to find a function in our own ("to") context with the same name, same |
| 6742 | // type, and in the same context as the function we're importing. |
| 6743 | // FIXME Split this into a separate function. |
| 6744 | if (!LexicalDC->isFunctionOrMethod()) { |
| 6745 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend; |
| 6746 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); |
| 6747 | for (auto *FoundDecl : FoundDecls) { |
| 6748 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) |
| 6749 | continue; |
| 6750 | |
| 6751 | if (auto *FoundTemplate = dyn_cast<FunctionTemplateDecl>(Val: FoundDecl)) { |
| 6752 | if (!hasSameVisibilityContextAndLinkage(Found: FoundTemplate, From: D)) |
| 6753 | continue; |
| 6754 | if (IsStructuralMatch(D, FoundTemplate)) { |
| 6755 | FunctionTemplateDecl *TemplateWithDef = |
| 6756 | getTemplateDefinition(D: FoundTemplate); |
| 6757 | if (D->isThisDeclarationADefinition() && TemplateWithDef) |
| 6758 | return Importer.MapImported(D, TemplateWithDef); |
| 6759 | |
| 6760 | FoundByLookup = FoundTemplate; |
| 6761 | break; |
| 6762 | // TODO: handle conflicting names |
| 6763 | } |
| 6764 | } |
| 6765 | } |
| 6766 | } |
| 6767 | |
| 6768 | auto ParamsOrErr = import(D->getTemplateParameters()); |
| 6769 | if (!ParamsOrErr) |
| 6770 | return ParamsOrErr.takeError(); |
| 6771 | TemplateParameterList *Params = *ParamsOrErr; |
| 6772 | |
| 6773 | FunctionDecl *TemplatedFD; |
| 6774 | if (Error Err = importInto(To&: TemplatedFD, From: D->getTemplatedDecl())) |
| 6775 | return std::move(Err); |
| 6776 | |
| 6777 | // At creation of the template the template parameters are "adopted" |
| 6778 | // (DeclContext is changed). After this possible change the lookup table |
| 6779 | // must be updated. |
| 6780 | // At deduction guides the DeclContext of the template parameters may be |
| 6781 | // different from what we would expect, it may be the class template, or a |
| 6782 | // probably different CXXDeductionGuideDecl. This may come from the fact that |
| 6783 | // the template parameter objects may be shared between deduction guides or |
| 6784 | // the class template, and at creation of multiple FunctionTemplateDecl |
| 6785 | // objects (for deduction guides) the same parameters are re-used. The |
| 6786 | // "adoption" happens multiple times with different parent, even recursively |
| 6787 | // for TemplateTemplateParmDecl. The same happens at import when the |
| 6788 | // FunctionTemplateDecl objects are created, but in different order. |
| 6789 | // In this way the DeclContext of these template parameters is not necessarily |
| 6790 | // the same as in the "from" context. |
| 6791 | SmallVector<DeclContext *, 2> OldParamDC; |
| 6792 | OldParamDC.reserve(N: Params->size()); |
| 6793 | llvm::transform(Range&: *Params, d_first: std::back_inserter(x&: OldParamDC), |
| 6794 | F: [](NamedDecl *ND) { return ND->getDeclContext(); }); |
| 6795 | |
| 6796 | FunctionTemplateDecl *ToFunc; |
| 6797 | if (GetImportedOrCreateDecl(ToD&: ToFunc, FromD: D, args&: Importer.getToContext(), args&: DC, args&: Loc, args&: Name, |
| 6798 | args&: Params, args&: TemplatedFD)) |
| 6799 | return ToFunc; |
| 6800 | |
| 6801 | // Fail if TemplatedFD is already part of a template. |
| 6802 | // The template should have been found by structural equivalence check before, |
| 6803 | // or ToFunc should be already imported. |
| 6804 | // If not, there is AST incompatibility that can be caused by previous import |
| 6805 | // errors. (NameConflict is not exact here.) |
| 6806 | if (TemplatedFD->getDescribedTemplate()) |
| 6807 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 6808 | |
| 6809 | TemplatedFD->setDescribedFunctionTemplate(ToFunc); |
| 6810 | |
| 6811 | ToFunc->setAccess(D->getAccess()); |
| 6812 | ToFunc->setLexicalDeclContext(LexicalDC); |
| 6813 | addDeclToContexts(D, ToFunc); |
| 6814 | |
| 6815 | ASTImporterLookupTable *LT = Importer.SharedState->getLookupTable(); |
| 6816 | if (LT && !OldParamDC.empty()) { |
| 6817 | for (unsigned int I = 0; I < OldParamDC.size(); ++I) |
| 6818 | LT->updateForced(ND: Params->getParam(Idx: I), OldDC: OldParamDC[I]); |
| 6819 | } |
| 6820 | |
| 6821 | if (FoundByLookup) { |
| 6822 | auto *Recent = |
| 6823 | const_cast<FunctionTemplateDecl *>(FoundByLookup->getMostRecentDecl()); |
| 6824 | if (!TemplatedFD->getPreviousDecl()) { |
| 6825 | assert(FoundByLookup->getTemplatedDecl() && |
| 6826 | "Found decl must have its templated decl set" ); |
| 6827 | auto *PrevTemplated = |
| 6828 | FoundByLookup->getTemplatedDecl()->getMostRecentDecl(); |
| 6829 | if (TemplatedFD != PrevTemplated) |
| 6830 | TemplatedFD->setPreviousDecl(PrevTemplated); |
| 6831 | } |
| 6832 | ToFunc->setPreviousDecl(Recent); |
| 6833 | } |
| 6834 | |
| 6835 | return ToFunc; |
| 6836 | } |
| 6837 | |
| 6838 | //---------------------------------------------------------------------------- |
| 6839 | // Import Statements |
| 6840 | //---------------------------------------------------------------------------- |
| 6841 | |
| 6842 | ExpectedStmt ASTNodeImporter::VisitStmt(Stmt *S) { |
| 6843 | Importer.FromDiag(S->getBeginLoc(), diag::err_unsupported_ast_node) |
| 6844 | << S->getStmtClassName(); |
| 6845 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
| 6846 | } |
| 6847 | |
| 6848 | |
| 6849 | ExpectedStmt ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) { |
| 6850 | if (Importer.returnWithErrorInTest()) |
| 6851 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
| 6852 | SmallVector<IdentifierInfo *, 4> Names; |
| 6853 | for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) { |
| 6854 | IdentifierInfo *ToII = Importer.Import(FromId: S->getOutputIdentifier(i: I)); |
| 6855 | // ToII is nullptr when no symbolic name is given for output operand |
| 6856 | // see ParseStmtAsm::ParseAsmOperandsOpt |
| 6857 | Names.push_back(Elt: ToII); |
| 6858 | } |
| 6859 | |
| 6860 | for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) { |
| 6861 | IdentifierInfo *ToII = Importer.Import(FromId: S->getInputIdentifier(i: I)); |
| 6862 | // ToII is nullptr when no symbolic name is given for input operand |
| 6863 | // see ParseStmtAsm::ParseAsmOperandsOpt |
| 6864 | Names.push_back(Elt: ToII); |
| 6865 | } |
| 6866 | |
| 6867 | SmallVector<Expr *, 4> Clobbers; |
| 6868 | for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) { |
| 6869 | if (auto ClobberOrErr = import(From: S->getClobberExpr(i: I))) |
| 6870 | Clobbers.push_back(Elt: *ClobberOrErr); |
| 6871 | else |
| 6872 | return ClobberOrErr.takeError(); |
| 6873 | |
| 6874 | } |
| 6875 | |
| 6876 | SmallVector<Expr *, 4> Constraints; |
| 6877 | for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) { |
| 6878 | if (auto OutputOrErr = import(From: S->getOutputConstraintExpr(i: I))) |
| 6879 | Constraints.push_back(Elt: *OutputOrErr); |
| 6880 | else |
| 6881 | return OutputOrErr.takeError(); |
| 6882 | } |
| 6883 | |
| 6884 | for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) { |
| 6885 | if (auto InputOrErr = import(From: S->getInputConstraintExpr(i: I))) |
| 6886 | Constraints.push_back(Elt: *InputOrErr); |
| 6887 | else |
| 6888 | return InputOrErr.takeError(); |
| 6889 | } |
| 6890 | |
| 6891 | SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs() + |
| 6892 | S->getNumLabels()); |
| 6893 | if (Error Err = ImportContainerChecked(S->outputs(), Exprs)) |
| 6894 | return std::move(Err); |
| 6895 | |
| 6896 | if (Error Err = |
| 6897 | ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs())) |
| 6898 | return std::move(Err); |
| 6899 | |
| 6900 | if (Error Err = ImportArrayChecked( |
| 6901 | S->labels(), Exprs.begin() + S->getNumOutputs() + S->getNumInputs())) |
| 6902 | return std::move(Err); |
| 6903 | |
| 6904 | ExpectedSLoc AsmLocOrErr = import(From: S->getAsmLoc()); |
| 6905 | if (!AsmLocOrErr) |
| 6906 | return AsmLocOrErr.takeError(); |
| 6907 | auto AsmStrOrErr = import(From: S->getAsmStringExpr()); |
| 6908 | if (!AsmStrOrErr) |
| 6909 | return AsmStrOrErr.takeError(); |
| 6910 | ExpectedSLoc RParenLocOrErr = import(From: S->getRParenLoc()); |
| 6911 | if (!RParenLocOrErr) |
| 6912 | return RParenLocOrErr.takeError(); |
| 6913 | |
| 6914 | return new (Importer.getToContext()) GCCAsmStmt( |
| 6915 | Importer.getToContext(), |
| 6916 | *AsmLocOrErr, |
| 6917 | S->isSimple(), |
| 6918 | S->isVolatile(), |
| 6919 | S->getNumOutputs(), |
| 6920 | S->getNumInputs(), |
| 6921 | Names.data(), |
| 6922 | Constraints.data(), |
| 6923 | Exprs.data(), |
| 6924 | *AsmStrOrErr, |
| 6925 | S->getNumClobbers(), |
| 6926 | Clobbers.data(), |
| 6927 | S->getNumLabels(), |
| 6928 | *RParenLocOrErr); |
| 6929 | } |
| 6930 | |
| 6931 | ExpectedStmt ASTNodeImporter::VisitDeclStmt(DeclStmt *S) { |
| 6932 | |
| 6933 | Error Err = Error::success(); |
| 6934 | auto ToDG = importChecked(Err, From: S->getDeclGroup()); |
| 6935 | auto ToBeginLoc = importChecked(Err, From: S->getBeginLoc()); |
| 6936 | auto ToEndLoc = importChecked(Err, From: S->getEndLoc()); |
| 6937 | if (Err) |
| 6938 | return std::move(Err); |
| 6939 | return new (Importer.getToContext()) DeclStmt(ToDG, ToBeginLoc, ToEndLoc); |
| 6940 | } |
| 6941 | |
| 6942 | ExpectedStmt ASTNodeImporter::VisitNullStmt(NullStmt *S) { |
| 6943 | ExpectedSLoc ToSemiLocOrErr = import(From: S->getSemiLoc()); |
| 6944 | if (!ToSemiLocOrErr) |
| 6945 | return ToSemiLocOrErr.takeError(); |
| 6946 | return new (Importer.getToContext()) NullStmt( |
| 6947 | *ToSemiLocOrErr, S->hasLeadingEmptyMacro()); |
| 6948 | } |
| 6949 | |
| 6950 | ExpectedStmt ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) { |
| 6951 | SmallVector<Stmt *, 8> ToStmts(S->size()); |
| 6952 | |
| 6953 | if (Error Err = ImportContainerChecked(InContainer: S->body(), OutContainer&: ToStmts)) |
| 6954 | return std::move(Err); |
| 6955 | |
| 6956 | ExpectedSLoc ToLBracLocOrErr = import(From: S->getLBracLoc()); |
| 6957 | if (!ToLBracLocOrErr) |
| 6958 | return ToLBracLocOrErr.takeError(); |
| 6959 | |
| 6960 | ExpectedSLoc ToRBracLocOrErr = import(From: S->getRBracLoc()); |
| 6961 | if (!ToRBracLocOrErr) |
| 6962 | return ToRBracLocOrErr.takeError(); |
| 6963 | |
| 6964 | FPOptionsOverride FPO = |
| 6965 | S->hasStoredFPFeatures() ? S->getStoredFPFeatures() : FPOptionsOverride(); |
| 6966 | return CompoundStmt::Create(C: Importer.getToContext(), Stmts: ToStmts, FPFeatures: FPO, |
| 6967 | LB: *ToLBracLocOrErr, RB: *ToRBracLocOrErr); |
| 6968 | } |
| 6969 | |
| 6970 | ExpectedStmt ASTNodeImporter::VisitCaseStmt(CaseStmt *S) { |
| 6971 | |
| 6972 | Error Err = Error::success(); |
| 6973 | auto ToLHS = importChecked(Err, From: S->getLHS()); |
| 6974 | auto ToRHS = importChecked(Err, From: S->getRHS()); |
| 6975 | auto ToSubStmt = importChecked(Err, From: S->getSubStmt()); |
| 6976 | auto ToCaseLoc = importChecked(Err, From: S->getCaseLoc()); |
| 6977 | auto ToEllipsisLoc = importChecked(Err, From: S->getEllipsisLoc()); |
| 6978 | auto ToColonLoc = importChecked(Err, S->getColonLoc()); |
| 6979 | if (Err) |
| 6980 | return std::move(Err); |
| 6981 | |
| 6982 | auto *ToStmt = CaseStmt::Create(Ctx: Importer.getToContext(), lhs: ToLHS, rhs: ToRHS, |
| 6983 | caseLoc: ToCaseLoc, ellipsisLoc: ToEllipsisLoc, colonLoc: ToColonLoc); |
| 6984 | ToStmt->setSubStmt(ToSubStmt); |
| 6985 | |
| 6986 | return ToStmt; |
| 6987 | } |
| 6988 | |
| 6989 | ExpectedStmt ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) { |
| 6990 | |
| 6991 | Error Err = Error::success(); |
| 6992 | auto ToDefaultLoc = importChecked(Err, From: S->getDefaultLoc()); |
| 6993 | auto ToColonLoc = importChecked(Err, From: S->getColonLoc()); |
| 6994 | auto ToSubStmt = importChecked(Err, From: S->getSubStmt()); |
| 6995 | if (Err) |
| 6996 | return std::move(Err); |
| 6997 | |
| 6998 | return new (Importer.getToContext()) DefaultStmt( |
| 6999 | ToDefaultLoc, ToColonLoc, ToSubStmt); |
| 7000 | } |
| 7001 | |
| 7002 | ExpectedStmt ASTNodeImporter::VisitLabelStmt(LabelStmt *S) { |
| 7003 | |
| 7004 | Error Err = Error::success(); |
| 7005 | auto ToIdentLoc = importChecked(Err, From: S->getIdentLoc()); |
| 7006 | auto ToLabelDecl = importChecked(Err, From: S->getDecl()); |
| 7007 | auto ToSubStmt = importChecked(Err, From: S->getSubStmt()); |
| 7008 | if (Err) |
| 7009 | return std::move(Err); |
| 7010 | |
| 7011 | return new (Importer.getToContext()) LabelStmt( |
| 7012 | ToIdentLoc, ToLabelDecl, ToSubStmt); |
| 7013 | } |
| 7014 | |
| 7015 | ExpectedStmt ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) { |
| 7016 | ExpectedSLoc ToAttrLocOrErr = import(From: S->getAttrLoc()); |
| 7017 | if (!ToAttrLocOrErr) |
| 7018 | return ToAttrLocOrErr.takeError(); |
| 7019 | ArrayRef<const Attr*> FromAttrs(S->getAttrs()); |
| 7020 | SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size()); |
| 7021 | if (Error Err = ImportContainerChecked(InContainer: FromAttrs, OutContainer&: ToAttrs)) |
| 7022 | return std::move(Err); |
| 7023 | ExpectedStmt ToSubStmtOrErr = import(From: S->getSubStmt()); |
| 7024 | if (!ToSubStmtOrErr) |
| 7025 | return ToSubStmtOrErr.takeError(); |
| 7026 | |
| 7027 | return AttributedStmt::Create( |
| 7028 | C: Importer.getToContext(), Loc: *ToAttrLocOrErr, Attrs: ToAttrs, SubStmt: *ToSubStmtOrErr); |
| 7029 | } |
| 7030 | |
| 7031 | ExpectedStmt ASTNodeImporter::VisitIfStmt(IfStmt *S) { |
| 7032 | |
| 7033 | Error Err = Error::success(); |
| 7034 | auto ToIfLoc = importChecked(Err, From: S->getIfLoc()); |
| 7035 | auto ToInit = importChecked(Err, From: S->getInit()); |
| 7036 | auto ToConditionVariable = importChecked(Err, From: S->getConditionVariable()); |
| 7037 | auto ToCond = importChecked(Err, From: S->getCond()); |
| 7038 | auto ToLParenLoc = importChecked(Err, From: S->getLParenLoc()); |
| 7039 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
| 7040 | auto ToThen = importChecked(Err, From: S->getThen()); |
| 7041 | auto ToElseLoc = importChecked(Err, From: S->getElseLoc()); |
| 7042 | auto ToElse = importChecked(Err, From: S->getElse()); |
| 7043 | if (Err) |
| 7044 | return std::move(Err); |
| 7045 | |
| 7046 | return IfStmt::Create(Ctx: Importer.getToContext(), IL: ToIfLoc, Kind: S->getStatementKind(), |
| 7047 | Init: ToInit, Var: ToConditionVariable, Cond: ToCond, LPL: ToLParenLoc, |
| 7048 | RPL: ToRParenLoc, Then: ToThen, EL: ToElseLoc, Else: ToElse); |
| 7049 | } |
| 7050 | |
| 7051 | ExpectedStmt ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) { |
| 7052 | |
| 7053 | Error Err = Error::success(); |
| 7054 | auto ToInit = importChecked(Err, From: S->getInit()); |
| 7055 | auto ToConditionVariable = importChecked(Err, From: S->getConditionVariable()); |
| 7056 | auto ToCond = importChecked(Err, From: S->getCond()); |
| 7057 | auto ToLParenLoc = importChecked(Err, From: S->getLParenLoc()); |
| 7058 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
| 7059 | auto ToBody = importChecked(Err, From: S->getBody()); |
| 7060 | auto ToSwitchLoc = importChecked(Err, From: S->getSwitchLoc()); |
| 7061 | if (Err) |
| 7062 | return std::move(Err); |
| 7063 | |
| 7064 | auto *ToStmt = |
| 7065 | SwitchStmt::Create(Ctx: Importer.getToContext(), Init: ToInit, Var: ToConditionVariable, |
| 7066 | Cond: ToCond, LParenLoc: ToLParenLoc, RParenLoc: ToRParenLoc); |
| 7067 | ToStmt->setBody(ToBody); |
| 7068 | ToStmt->setSwitchLoc(ToSwitchLoc); |
| 7069 | |
| 7070 | // Now we have to re-chain the cases. |
| 7071 | SwitchCase *LastChainedSwitchCase = nullptr; |
| 7072 | for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr; |
| 7073 | SC = SC->getNextSwitchCase()) { |
| 7074 | Expected<SwitchCase *> ToSCOrErr = import(From: SC); |
| 7075 | if (!ToSCOrErr) |
| 7076 | return ToSCOrErr.takeError(); |
| 7077 | if (LastChainedSwitchCase) |
| 7078 | LastChainedSwitchCase->setNextSwitchCase(*ToSCOrErr); |
| 7079 | else |
| 7080 | ToStmt->setSwitchCaseList(*ToSCOrErr); |
| 7081 | LastChainedSwitchCase = *ToSCOrErr; |
| 7082 | } |
| 7083 | |
| 7084 | return ToStmt; |
| 7085 | } |
| 7086 | |
| 7087 | ExpectedStmt ASTNodeImporter::VisitWhileStmt(WhileStmt *S) { |
| 7088 | |
| 7089 | Error Err = Error::success(); |
| 7090 | auto ToConditionVariable = importChecked(Err, From: S->getConditionVariable()); |
| 7091 | auto ToCond = importChecked(Err, From: S->getCond()); |
| 7092 | auto ToBody = importChecked(Err, From: S->getBody()); |
| 7093 | auto ToWhileLoc = importChecked(Err, From: S->getWhileLoc()); |
| 7094 | auto ToLParenLoc = importChecked(Err, From: S->getLParenLoc()); |
| 7095 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
| 7096 | if (Err) |
| 7097 | return std::move(Err); |
| 7098 | |
| 7099 | return WhileStmt::Create(Ctx: Importer.getToContext(), Var: ToConditionVariable, Cond: ToCond, |
| 7100 | Body: ToBody, WL: ToWhileLoc, LParenLoc: ToLParenLoc, RParenLoc: ToRParenLoc); |
| 7101 | } |
| 7102 | |
| 7103 | ExpectedStmt ASTNodeImporter::VisitDoStmt(DoStmt *S) { |
| 7104 | |
| 7105 | Error Err = Error::success(); |
| 7106 | auto ToBody = importChecked(Err, From: S->getBody()); |
| 7107 | auto ToCond = importChecked(Err, From: S->getCond()); |
| 7108 | auto ToDoLoc = importChecked(Err, From: S->getDoLoc()); |
| 7109 | auto ToWhileLoc = importChecked(Err, From: S->getWhileLoc()); |
| 7110 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
| 7111 | if (Err) |
| 7112 | return std::move(Err); |
| 7113 | |
| 7114 | return new (Importer.getToContext()) DoStmt( |
| 7115 | ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc); |
| 7116 | } |
| 7117 | |
| 7118 | ExpectedStmt ASTNodeImporter::VisitForStmt(ForStmt *S) { |
| 7119 | |
| 7120 | Error Err = Error::success(); |
| 7121 | auto ToInit = importChecked(Err, From: S->getInit()); |
| 7122 | auto ToCond = importChecked(Err, From: S->getCond()); |
| 7123 | auto ToConditionVariable = importChecked(Err, From: S->getConditionVariable()); |
| 7124 | auto ToInc = importChecked(Err, From: S->getInc()); |
| 7125 | auto ToBody = importChecked(Err, From: S->getBody()); |
| 7126 | auto ToForLoc = importChecked(Err, From: S->getForLoc()); |
| 7127 | auto ToLParenLoc = importChecked(Err, From: S->getLParenLoc()); |
| 7128 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
| 7129 | if (Err) |
| 7130 | return std::move(Err); |
| 7131 | |
| 7132 | return new (Importer.getToContext()) ForStmt( |
| 7133 | Importer.getToContext(), |
| 7134 | ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc, ToLParenLoc, |
| 7135 | ToRParenLoc); |
| 7136 | } |
| 7137 | |
| 7138 | ExpectedStmt ASTNodeImporter::VisitGotoStmt(GotoStmt *S) { |
| 7139 | |
| 7140 | Error Err = Error::success(); |
| 7141 | auto ToLabel = importChecked(Err, From: S->getLabel()); |
| 7142 | auto ToGotoLoc = importChecked(Err, From: S->getGotoLoc()); |
| 7143 | auto ToLabelLoc = importChecked(Err, From: S->getLabelLoc()); |
| 7144 | if (Err) |
| 7145 | return std::move(Err); |
| 7146 | |
| 7147 | return new (Importer.getToContext()) GotoStmt( |
| 7148 | ToLabel, ToGotoLoc, ToLabelLoc); |
| 7149 | } |
| 7150 | |
| 7151 | ExpectedStmt ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) { |
| 7152 | |
| 7153 | Error Err = Error::success(); |
| 7154 | auto ToGotoLoc = importChecked(Err, From: S->getGotoLoc()); |
| 7155 | auto ToStarLoc = importChecked(Err, From: S->getStarLoc()); |
| 7156 | auto ToTarget = importChecked(Err, From: S->getTarget()); |
| 7157 | if (Err) |
| 7158 | return std::move(Err); |
| 7159 | |
| 7160 | return new (Importer.getToContext()) IndirectGotoStmt( |
| 7161 | ToGotoLoc, ToStarLoc, ToTarget); |
| 7162 | } |
| 7163 | |
| 7164 | ExpectedStmt ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) { |
| 7165 | ExpectedSLoc ToContinueLocOrErr = import(From: S->getContinueLoc()); |
| 7166 | if (!ToContinueLocOrErr) |
| 7167 | return ToContinueLocOrErr.takeError(); |
| 7168 | return new (Importer.getToContext()) ContinueStmt(*ToContinueLocOrErr); |
| 7169 | } |
| 7170 | |
| 7171 | ExpectedStmt ASTNodeImporter::VisitBreakStmt(BreakStmt *S) { |
| 7172 | auto ToBreakLocOrErr = import(From: S->getBreakLoc()); |
| 7173 | if (!ToBreakLocOrErr) |
| 7174 | return ToBreakLocOrErr.takeError(); |
| 7175 | return new (Importer.getToContext()) BreakStmt(*ToBreakLocOrErr); |
| 7176 | } |
| 7177 | |
| 7178 | ExpectedStmt ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) { |
| 7179 | |
| 7180 | Error Err = Error::success(); |
| 7181 | auto ToReturnLoc = importChecked(Err, From: S->getReturnLoc()); |
| 7182 | auto ToRetValue = importChecked(Err, From: S->getRetValue()); |
| 7183 | auto ToNRVOCandidate = importChecked(Err, From: S->getNRVOCandidate()); |
| 7184 | if (Err) |
| 7185 | return std::move(Err); |
| 7186 | |
| 7187 | return ReturnStmt::Create(Ctx: Importer.getToContext(), RL: ToReturnLoc, E: ToRetValue, |
| 7188 | NRVOCandidate: ToNRVOCandidate); |
| 7189 | } |
| 7190 | |
| 7191 | ExpectedStmt ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) { |
| 7192 | |
| 7193 | Error Err = Error::success(); |
| 7194 | auto ToCatchLoc = importChecked(Err, From: S->getCatchLoc()); |
| 7195 | auto ToExceptionDecl = importChecked(Err, From: S->getExceptionDecl()); |
| 7196 | auto ToHandlerBlock = importChecked(Err, From: S->getHandlerBlock()); |
| 7197 | if (Err) |
| 7198 | return std::move(Err); |
| 7199 | |
| 7200 | return new (Importer.getToContext()) CXXCatchStmt ( |
| 7201 | ToCatchLoc, ToExceptionDecl, ToHandlerBlock); |
| 7202 | } |
| 7203 | |
| 7204 | ExpectedStmt ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) { |
| 7205 | ExpectedSLoc ToTryLocOrErr = import(From: S->getTryLoc()); |
| 7206 | if (!ToTryLocOrErr) |
| 7207 | return ToTryLocOrErr.takeError(); |
| 7208 | |
| 7209 | ExpectedStmt ToTryBlockOrErr = import(From: S->getTryBlock()); |
| 7210 | if (!ToTryBlockOrErr) |
| 7211 | return ToTryBlockOrErr.takeError(); |
| 7212 | |
| 7213 | SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers()); |
| 7214 | for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) { |
| 7215 | CXXCatchStmt *FromHandler = S->getHandler(i: HI); |
| 7216 | if (auto ToHandlerOrErr = import(From: FromHandler)) |
| 7217 | ToHandlers[HI] = *ToHandlerOrErr; |
| 7218 | else |
| 7219 | return ToHandlerOrErr.takeError(); |
| 7220 | } |
| 7221 | |
| 7222 | return CXXTryStmt::Create(C: Importer.getToContext(), tryLoc: *ToTryLocOrErr, |
| 7223 | tryBlock: cast<CompoundStmt>(Val: *ToTryBlockOrErr), handlers: ToHandlers); |
| 7224 | } |
| 7225 | |
| 7226 | ExpectedStmt ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) { |
| 7227 | |
| 7228 | Error Err = Error::success(); |
| 7229 | auto ToInit = importChecked(Err, From: S->getInit()); |
| 7230 | auto ToRangeStmt = importChecked(Err, From: S->getRangeStmt()); |
| 7231 | auto ToBeginStmt = importChecked(Err, From: S->getBeginStmt()); |
| 7232 | auto ToEndStmt = importChecked(Err, From: S->getEndStmt()); |
| 7233 | auto ToCond = importChecked(Err, From: S->getCond()); |
| 7234 | auto ToInc = importChecked(Err, From: S->getInc()); |
| 7235 | auto ToLoopVarStmt = importChecked(Err, From: S->getLoopVarStmt()); |
| 7236 | auto ToBody = importChecked(Err, From: S->getBody()); |
| 7237 | auto ToForLoc = importChecked(Err, From: S->getForLoc()); |
| 7238 | auto ToCoawaitLoc = importChecked(Err, From: S->getCoawaitLoc()); |
| 7239 | auto ToColonLoc = importChecked(Err, From: S->getColonLoc()); |
| 7240 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
| 7241 | if (Err) |
| 7242 | return std::move(Err); |
| 7243 | |
| 7244 | return new (Importer.getToContext()) CXXForRangeStmt( |
| 7245 | ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt, |
| 7246 | ToBody, ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc); |
| 7247 | } |
| 7248 | |
| 7249 | ExpectedStmt |
| 7250 | ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) { |
| 7251 | Error Err = Error::success(); |
| 7252 | auto ToElement = importChecked(Err, From: S->getElement()); |
| 7253 | auto ToCollection = importChecked(Err, From: S->getCollection()); |
| 7254 | auto ToBody = importChecked(Err, From: S->getBody()); |
| 7255 | auto ToForLoc = importChecked(Err, From: S->getForLoc()); |
| 7256 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
| 7257 | if (Err) |
| 7258 | return std::move(Err); |
| 7259 | |
| 7260 | return new (Importer.getToContext()) ObjCForCollectionStmt(ToElement, |
| 7261 | ToCollection, |
| 7262 | ToBody, |
| 7263 | ToForLoc, |
| 7264 | ToRParenLoc); |
| 7265 | } |
| 7266 | |
| 7267 | ExpectedStmt ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) { |
| 7268 | |
| 7269 | Error Err = Error::success(); |
| 7270 | auto ToAtCatchLoc = importChecked(Err, From: S->getAtCatchLoc()); |
| 7271 | auto ToRParenLoc = importChecked(Err, From: S->getRParenLoc()); |
| 7272 | auto ToCatchParamDecl = importChecked(Err, From: S->getCatchParamDecl()); |
| 7273 | auto ToCatchBody = importChecked(Err, From: S->getCatchBody()); |
| 7274 | if (Err) |
| 7275 | return std::move(Err); |
| 7276 | |
| 7277 | return new (Importer.getToContext()) ObjCAtCatchStmt ( |
| 7278 | ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody); |
| 7279 | } |
| 7280 | |
| 7281 | ExpectedStmt ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) { |
| 7282 | ExpectedSLoc ToAtFinallyLocOrErr = import(From: S->getAtFinallyLoc()); |
| 7283 | if (!ToAtFinallyLocOrErr) |
| 7284 | return ToAtFinallyLocOrErr.takeError(); |
| 7285 | ExpectedStmt ToAtFinallyStmtOrErr = import(From: S->getFinallyBody()); |
| 7286 | if (!ToAtFinallyStmtOrErr) |
| 7287 | return ToAtFinallyStmtOrErr.takeError(); |
| 7288 | return new (Importer.getToContext()) ObjCAtFinallyStmt(*ToAtFinallyLocOrErr, |
| 7289 | *ToAtFinallyStmtOrErr); |
| 7290 | } |
| 7291 | |
| 7292 | ExpectedStmt ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) { |
| 7293 | |
| 7294 | Error Err = Error::success(); |
| 7295 | auto ToAtTryLoc = importChecked(Err, From: S->getAtTryLoc()); |
| 7296 | auto ToTryBody = importChecked(Err, From: S->getTryBody()); |
| 7297 | auto ToFinallyStmt = importChecked(Err, From: S->getFinallyStmt()); |
| 7298 | if (Err) |
| 7299 | return std::move(Err); |
| 7300 | |
| 7301 | SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts()); |
| 7302 | for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) { |
| 7303 | ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(I: CI); |
| 7304 | if (ExpectedStmt ToCatchStmtOrErr = import(From: FromCatchStmt)) |
| 7305 | ToCatchStmts[CI] = *ToCatchStmtOrErr; |
| 7306 | else |
| 7307 | return ToCatchStmtOrErr.takeError(); |
| 7308 | } |
| 7309 | |
| 7310 | return ObjCAtTryStmt::Create(Context: Importer.getToContext(), |
| 7311 | atTryLoc: ToAtTryLoc, atTryStmt: ToTryBody, |
| 7312 | CatchStmts: ToCatchStmts.begin(), NumCatchStmts: ToCatchStmts.size(), |
| 7313 | atFinallyStmt: ToFinallyStmt); |
| 7314 | } |
| 7315 | |
| 7316 | ExpectedStmt |
| 7317 | ASTNodeImporter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
| 7318 | |
| 7319 | Error Err = Error::success(); |
| 7320 | auto ToAtSynchronizedLoc = importChecked(Err, From: S->getAtSynchronizedLoc()); |
| 7321 | auto ToSynchExpr = importChecked(Err, From: S->getSynchExpr()); |
| 7322 | auto ToSynchBody = importChecked(Err, From: S->getSynchBody()); |
| 7323 | if (Err) |
| 7324 | return std::move(Err); |
| 7325 | |
| 7326 | return new (Importer.getToContext()) ObjCAtSynchronizedStmt( |
| 7327 | ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody); |
| 7328 | } |
| 7329 | |
| 7330 | ExpectedStmt ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) { |
| 7331 | ExpectedSLoc ToThrowLocOrErr = import(From: S->getThrowLoc()); |
| 7332 | if (!ToThrowLocOrErr) |
| 7333 | return ToThrowLocOrErr.takeError(); |
| 7334 | ExpectedExpr ToThrowExprOrErr = import(From: S->getThrowExpr()); |
| 7335 | if (!ToThrowExprOrErr) |
| 7336 | return ToThrowExprOrErr.takeError(); |
| 7337 | return new (Importer.getToContext()) ObjCAtThrowStmt( |
| 7338 | *ToThrowLocOrErr, *ToThrowExprOrErr); |
| 7339 | } |
| 7340 | |
| 7341 | ExpectedStmt ASTNodeImporter::VisitObjCAutoreleasePoolStmt( |
| 7342 | ObjCAutoreleasePoolStmt *S) { |
| 7343 | ExpectedSLoc ToAtLocOrErr = import(From: S->getAtLoc()); |
| 7344 | if (!ToAtLocOrErr) |
| 7345 | return ToAtLocOrErr.takeError(); |
| 7346 | ExpectedStmt ToSubStmtOrErr = import(From: S->getSubStmt()); |
| 7347 | if (!ToSubStmtOrErr) |
| 7348 | return ToSubStmtOrErr.takeError(); |
| 7349 | return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(*ToAtLocOrErr, |
| 7350 | *ToSubStmtOrErr); |
| 7351 | } |
| 7352 | |
| 7353 | //---------------------------------------------------------------------------- |
| 7354 | // Import Expressions |
| 7355 | //---------------------------------------------------------------------------- |
| 7356 | ExpectedStmt ASTNodeImporter::VisitExpr(Expr *E) { |
| 7357 | Importer.FromDiag(E->getBeginLoc(), diag::err_unsupported_ast_node) |
| 7358 | << E->getStmtClassName(); |
| 7359 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
| 7360 | } |
| 7361 | |
| 7362 | ExpectedStmt ASTNodeImporter::VisitSourceLocExpr(SourceLocExpr *E) { |
| 7363 | Error Err = Error::success(); |
| 7364 | auto ToType = importChecked(Err, E->getType()); |
| 7365 | auto BLoc = importChecked(Err, From: E->getBeginLoc()); |
| 7366 | auto RParenLoc = importChecked(Err, From: E->getEndLoc()); |
| 7367 | if (Err) |
| 7368 | return std::move(Err); |
| 7369 | auto ParentContextOrErr = Importer.ImportContext(FromDC: E->getParentContext()); |
| 7370 | if (!ParentContextOrErr) |
| 7371 | return ParentContextOrErr.takeError(); |
| 7372 | |
| 7373 | return new (Importer.getToContext()) |
| 7374 | SourceLocExpr(Importer.getToContext(), E->getIdentKind(), ToType, BLoc, |
| 7375 | RParenLoc, *ParentContextOrErr); |
| 7376 | } |
| 7377 | |
| 7378 | ExpectedStmt ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) { |
| 7379 | |
| 7380 | Error Err = Error::success(); |
| 7381 | auto ToBuiltinLoc = importChecked(Err, From: E->getBuiltinLoc()); |
| 7382 | auto ToSubExpr = importChecked(Err, From: E->getSubExpr()); |
| 7383 | auto ToWrittenTypeInfo = importChecked(Err, From: E->getWrittenTypeInfo()); |
| 7384 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 7385 | auto ToType = importChecked(Err, E->getType()); |
| 7386 | if (Err) |
| 7387 | return std::move(Err); |
| 7388 | |
| 7389 | return new (Importer.getToContext()) VAArgExpr( |
| 7390 | ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType, |
| 7391 | E->isMicrosoftABI()); |
| 7392 | } |
| 7393 | |
| 7394 | ExpectedStmt ASTNodeImporter::VisitChooseExpr(ChooseExpr *E) { |
| 7395 | |
| 7396 | Error Err = Error::success(); |
| 7397 | auto ToCond = importChecked(Err, From: E->getCond()); |
| 7398 | auto ToLHS = importChecked(Err, From: E->getLHS()); |
| 7399 | auto ToRHS = importChecked(Err, From: E->getRHS()); |
| 7400 | auto ToBuiltinLoc = importChecked(Err, From: E->getBuiltinLoc()); |
| 7401 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 7402 | auto ToType = importChecked(Err, E->getType()); |
| 7403 | if (Err) |
| 7404 | return std::move(Err); |
| 7405 | |
| 7406 | ExprValueKind VK = E->getValueKind(); |
| 7407 | ExprObjectKind OK = E->getObjectKind(); |
| 7408 | |
| 7409 | // The value of CondIsTrue only matters if the value is not |
| 7410 | // condition-dependent. |
| 7411 | bool CondIsTrue = !E->isConditionDependent() && E->isConditionTrue(); |
| 7412 | |
| 7413 | return new (Importer.getToContext()) |
| 7414 | ChooseExpr(ToBuiltinLoc, ToCond, ToLHS, ToRHS, ToType, VK, OK, |
| 7415 | ToRParenLoc, CondIsTrue); |
| 7416 | } |
| 7417 | |
| 7418 | ExpectedStmt ASTNodeImporter::VisitConvertVectorExpr(ConvertVectorExpr *E) { |
| 7419 | Error Err = Error::success(); |
| 7420 | auto *ToSrcExpr = importChecked(Err, From: E->getSrcExpr()); |
| 7421 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 7422 | auto ToBuiltinLoc = importChecked(Err, From: E->getBuiltinLoc()); |
| 7423 | auto ToType = importChecked(Err, E->getType()); |
| 7424 | auto *ToTSI = importChecked(Err, From: E->getTypeSourceInfo()); |
| 7425 | if (Err) |
| 7426 | return std::move(Err); |
| 7427 | |
| 7428 | return ConvertVectorExpr::Create( |
| 7429 | C: Importer.getToContext(), SrcExpr: ToSrcExpr, TI: ToTSI, DstType: ToType, VK: E->getValueKind(), |
| 7430 | OK: E->getObjectKind(), BuiltinLoc: ToBuiltinLoc, RParenLoc: ToRParenLoc, |
| 7431 | FPFeatures: E->getStoredFPFeaturesOrDefault()); |
| 7432 | } |
| 7433 | |
| 7434 | ExpectedStmt ASTNodeImporter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) { |
| 7435 | Error Err = Error::success(); |
| 7436 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 7437 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
| 7438 | auto ToType = importChecked(Err, E->getType()); |
| 7439 | const unsigned NumSubExprs = E->getNumSubExprs(); |
| 7440 | |
| 7441 | llvm::SmallVector<Expr *, 8> ToSubExprs; |
| 7442 | llvm::ArrayRef<Expr *> FromSubExprs(E->getSubExprs(), NumSubExprs); |
| 7443 | ToSubExprs.resize(N: NumSubExprs); |
| 7444 | |
| 7445 | if ((Err = ImportContainerChecked(InContainer: FromSubExprs, OutContainer&: ToSubExprs))) |
| 7446 | return std::move(Err); |
| 7447 | |
| 7448 | return new (Importer.getToContext()) ShuffleVectorExpr( |
| 7449 | Importer.getToContext(), ToSubExprs, ToType, ToBeginLoc, ToRParenLoc); |
| 7450 | } |
| 7451 | |
| 7452 | ExpectedStmt ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) { |
| 7453 | ExpectedType TypeOrErr = import(E->getType()); |
| 7454 | if (!TypeOrErr) |
| 7455 | return TypeOrErr.takeError(); |
| 7456 | |
| 7457 | ExpectedSLoc BeginLocOrErr = import(From: E->getBeginLoc()); |
| 7458 | if (!BeginLocOrErr) |
| 7459 | return BeginLocOrErr.takeError(); |
| 7460 | |
| 7461 | return new (Importer.getToContext()) GNUNullExpr(*TypeOrErr, *BeginLocOrErr); |
| 7462 | } |
| 7463 | |
| 7464 | ExpectedStmt |
| 7465 | ASTNodeImporter::VisitGenericSelectionExpr(GenericSelectionExpr *E) { |
| 7466 | Error Err = Error::success(); |
| 7467 | auto ToGenericLoc = importChecked(Err, From: E->getGenericLoc()); |
| 7468 | Expr *ToControllingExpr = nullptr; |
| 7469 | TypeSourceInfo *ToControllingType = nullptr; |
| 7470 | if (E->isExprPredicate()) |
| 7471 | ToControllingExpr = importChecked(Err, From: E->getControllingExpr()); |
| 7472 | else |
| 7473 | ToControllingType = importChecked(Err, From: E->getControllingType()); |
| 7474 | assert((ToControllingExpr || ToControllingType) && |
| 7475 | "Either the controlling expr or type must be nonnull" ); |
| 7476 | auto ToDefaultLoc = importChecked(Err, From: E->getDefaultLoc()); |
| 7477 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 7478 | if (Err) |
| 7479 | return std::move(Err); |
| 7480 | |
| 7481 | ArrayRef<const TypeSourceInfo *> FromAssocTypes(E->getAssocTypeSourceInfos()); |
| 7482 | SmallVector<TypeSourceInfo *, 1> ToAssocTypes(FromAssocTypes.size()); |
| 7483 | if (Error Err = ImportContainerChecked(InContainer: FromAssocTypes, OutContainer&: ToAssocTypes)) |
| 7484 | return std::move(Err); |
| 7485 | |
| 7486 | ArrayRef<const Expr *> FromAssocExprs(E->getAssocExprs()); |
| 7487 | SmallVector<Expr *, 1> ToAssocExprs(FromAssocExprs.size()); |
| 7488 | if (Error Err = ImportContainerChecked(InContainer: FromAssocExprs, OutContainer&: ToAssocExprs)) |
| 7489 | return std::move(Err); |
| 7490 | |
| 7491 | const ASTContext &ToCtx = Importer.getToContext(); |
| 7492 | if (E->isResultDependent()) { |
| 7493 | if (ToControllingExpr) { |
| 7494 | return GenericSelectionExpr::Create( |
| 7495 | ToCtx, ToGenericLoc, ToControllingExpr, llvm::ArrayRef(ToAssocTypes), |
| 7496 | llvm::ArrayRef(ToAssocExprs), ToDefaultLoc, ToRParenLoc, |
| 7497 | E->containsUnexpandedParameterPack()); |
| 7498 | } |
| 7499 | return GenericSelectionExpr::Create( |
| 7500 | ToCtx, ToGenericLoc, ToControllingType, llvm::ArrayRef(ToAssocTypes), |
| 7501 | llvm::ArrayRef(ToAssocExprs), ToDefaultLoc, ToRParenLoc, |
| 7502 | E->containsUnexpandedParameterPack()); |
| 7503 | } |
| 7504 | |
| 7505 | if (ToControllingExpr) { |
| 7506 | return GenericSelectionExpr::Create( |
| 7507 | ToCtx, ToGenericLoc, ToControllingExpr, llvm::ArrayRef(ToAssocTypes), |
| 7508 | llvm::ArrayRef(ToAssocExprs), ToDefaultLoc, ToRParenLoc, |
| 7509 | E->containsUnexpandedParameterPack(), E->getResultIndex()); |
| 7510 | } |
| 7511 | return GenericSelectionExpr::Create( |
| 7512 | ToCtx, ToGenericLoc, ToControllingType, llvm::ArrayRef(ToAssocTypes), |
| 7513 | llvm::ArrayRef(ToAssocExprs), ToDefaultLoc, ToRParenLoc, |
| 7514 | E->containsUnexpandedParameterPack(), E->getResultIndex()); |
| 7515 | } |
| 7516 | |
| 7517 | ExpectedStmt ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) { |
| 7518 | |
| 7519 | Error Err = Error::success(); |
| 7520 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
| 7521 | auto ToType = importChecked(Err, E->getType()); |
| 7522 | auto ToFunctionName = importChecked(Err, From: E->getFunctionName()); |
| 7523 | if (Err) |
| 7524 | return std::move(Err); |
| 7525 | |
| 7526 | return PredefinedExpr::Create(Ctx: Importer.getToContext(), L: ToBeginLoc, FNTy: ToType, |
| 7527 | IK: E->getIdentKind(), IsTransparent: E->isTransparent(), |
| 7528 | SL: ToFunctionName); |
| 7529 | } |
| 7530 | |
| 7531 | ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) { |
| 7532 | |
| 7533 | Error Err = Error::success(); |
| 7534 | auto ToQualifierLoc = importChecked(Err, From: E->getQualifierLoc()); |
| 7535 | auto ToTemplateKeywordLoc = importChecked(Err, From: E->getTemplateKeywordLoc()); |
| 7536 | auto ToDecl = importChecked(Err, From: E->getDecl()); |
| 7537 | auto ToLocation = importChecked(Err, From: E->getLocation()); |
| 7538 | auto ToType = importChecked(Err, E->getType()); |
| 7539 | if (Err) |
| 7540 | return std::move(Err); |
| 7541 | |
| 7542 | NamedDecl *ToFoundD = nullptr; |
| 7543 | if (E->getDecl() != E->getFoundDecl()) { |
| 7544 | auto FoundDOrErr = import(From: E->getFoundDecl()); |
| 7545 | if (!FoundDOrErr) |
| 7546 | return FoundDOrErr.takeError(); |
| 7547 | ToFoundD = *FoundDOrErr; |
| 7548 | } |
| 7549 | |
| 7550 | TemplateArgumentListInfo ToTAInfo; |
| 7551 | TemplateArgumentListInfo *ToResInfo = nullptr; |
| 7552 | if (E->hasExplicitTemplateArgs()) { |
| 7553 | if (Error Err = |
| 7554 | ImportTemplateArgumentListInfo(FromLAngleLoc: E->getLAngleLoc(), FromRAngleLoc: E->getRAngleLoc(), |
| 7555 | Container: E->template_arguments(), Result&: ToTAInfo)) |
| 7556 | return std::move(Err); |
| 7557 | ToResInfo = &ToTAInfo; |
| 7558 | } |
| 7559 | |
| 7560 | auto *ToE = DeclRefExpr::Create( |
| 7561 | Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc, ToDecl, |
| 7562 | E->refersToEnclosingVariableOrCapture(), ToLocation, ToType, |
| 7563 | E->getValueKind(), ToFoundD, ToResInfo, E->isNonOdrUse()); |
| 7564 | if (E->hadMultipleCandidates()) |
| 7565 | ToE->setHadMultipleCandidates(true); |
| 7566 | ToE->setIsImmediateEscalating(E->isImmediateEscalating()); |
| 7567 | return ToE; |
| 7568 | } |
| 7569 | |
| 7570 | ExpectedStmt ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { |
| 7571 | ExpectedType TypeOrErr = import(E->getType()); |
| 7572 | if (!TypeOrErr) |
| 7573 | return TypeOrErr.takeError(); |
| 7574 | |
| 7575 | return new (Importer.getToContext()) ImplicitValueInitExpr(*TypeOrErr); |
| 7576 | } |
| 7577 | |
| 7578 | ExpectedStmt ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *E) { |
| 7579 | ExpectedExpr ToInitOrErr = import(From: E->getInit()); |
| 7580 | if (!ToInitOrErr) |
| 7581 | return ToInitOrErr.takeError(); |
| 7582 | |
| 7583 | ExpectedSLoc ToEqualOrColonLocOrErr = import(From: E->getEqualOrColonLoc()); |
| 7584 | if (!ToEqualOrColonLocOrErr) |
| 7585 | return ToEqualOrColonLocOrErr.takeError(); |
| 7586 | |
| 7587 | SmallVector<Expr *, 4> ToIndexExprs(E->getNumSubExprs() - 1); |
| 7588 | // List elements from the second, the first is Init itself |
| 7589 | for (unsigned I = 1, N = E->getNumSubExprs(); I < N; I++) { |
| 7590 | if (ExpectedExpr ToArgOrErr = import(From: E->getSubExpr(Idx: I))) |
| 7591 | ToIndexExprs[I - 1] = *ToArgOrErr; |
| 7592 | else |
| 7593 | return ToArgOrErr.takeError(); |
| 7594 | } |
| 7595 | |
| 7596 | SmallVector<Designator, 4> ToDesignators(E->size()); |
| 7597 | if (Error Err = ImportContainerChecked(InContainer: E->designators(), OutContainer&: ToDesignators)) |
| 7598 | return std::move(Err); |
| 7599 | |
| 7600 | return DesignatedInitExpr::Create( |
| 7601 | C: Importer.getToContext(), Designators: ToDesignators, |
| 7602 | IndexExprs: ToIndexExprs, EqualOrColonLoc: *ToEqualOrColonLocOrErr, |
| 7603 | GNUSyntax: E->usesGNUSyntax(), Init: *ToInitOrErr); |
| 7604 | } |
| 7605 | |
| 7606 | ExpectedStmt |
| 7607 | ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) { |
| 7608 | ExpectedType ToTypeOrErr = import(E->getType()); |
| 7609 | if (!ToTypeOrErr) |
| 7610 | return ToTypeOrErr.takeError(); |
| 7611 | |
| 7612 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
| 7613 | if (!ToLocationOrErr) |
| 7614 | return ToLocationOrErr.takeError(); |
| 7615 | |
| 7616 | return new (Importer.getToContext()) CXXNullPtrLiteralExpr( |
| 7617 | *ToTypeOrErr, *ToLocationOrErr); |
| 7618 | } |
| 7619 | |
| 7620 | ExpectedStmt ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) { |
| 7621 | ExpectedType ToTypeOrErr = import(E->getType()); |
| 7622 | if (!ToTypeOrErr) |
| 7623 | return ToTypeOrErr.takeError(); |
| 7624 | |
| 7625 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
| 7626 | if (!ToLocationOrErr) |
| 7627 | return ToLocationOrErr.takeError(); |
| 7628 | |
| 7629 | return IntegerLiteral::Create( |
| 7630 | Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr); |
| 7631 | } |
| 7632 | |
| 7633 | |
| 7634 | ExpectedStmt ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) { |
| 7635 | ExpectedType ToTypeOrErr = import(E->getType()); |
| 7636 | if (!ToTypeOrErr) |
| 7637 | return ToTypeOrErr.takeError(); |
| 7638 | |
| 7639 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
| 7640 | if (!ToLocationOrErr) |
| 7641 | return ToLocationOrErr.takeError(); |
| 7642 | |
| 7643 | return FloatingLiteral::Create( |
| 7644 | C: Importer.getToContext(), V: E->getValue(), isexact: E->isExact(), |
| 7645 | Type: *ToTypeOrErr, L: *ToLocationOrErr); |
| 7646 | } |
| 7647 | |
| 7648 | ExpectedStmt ASTNodeImporter::VisitImaginaryLiteral(ImaginaryLiteral *E) { |
| 7649 | auto ToTypeOrErr = import(E->getType()); |
| 7650 | if (!ToTypeOrErr) |
| 7651 | return ToTypeOrErr.takeError(); |
| 7652 | |
| 7653 | ExpectedExpr ToSubExprOrErr = import(From: E->getSubExpr()); |
| 7654 | if (!ToSubExprOrErr) |
| 7655 | return ToSubExprOrErr.takeError(); |
| 7656 | |
| 7657 | return new (Importer.getToContext()) ImaginaryLiteral( |
| 7658 | *ToSubExprOrErr, *ToTypeOrErr); |
| 7659 | } |
| 7660 | |
| 7661 | ExpectedStmt ASTNodeImporter::VisitFixedPointLiteral(FixedPointLiteral *E) { |
| 7662 | auto ToTypeOrErr = import(E->getType()); |
| 7663 | if (!ToTypeOrErr) |
| 7664 | return ToTypeOrErr.takeError(); |
| 7665 | |
| 7666 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
| 7667 | if (!ToLocationOrErr) |
| 7668 | return ToLocationOrErr.takeError(); |
| 7669 | |
| 7670 | return new (Importer.getToContext()) FixedPointLiteral( |
| 7671 | Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr, |
| 7672 | Importer.getToContext().getFixedPointScale(Ty: *ToTypeOrErr)); |
| 7673 | } |
| 7674 | |
| 7675 | ExpectedStmt ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) { |
| 7676 | ExpectedType ToTypeOrErr = import(E->getType()); |
| 7677 | if (!ToTypeOrErr) |
| 7678 | return ToTypeOrErr.takeError(); |
| 7679 | |
| 7680 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
| 7681 | if (!ToLocationOrErr) |
| 7682 | return ToLocationOrErr.takeError(); |
| 7683 | |
| 7684 | return new (Importer.getToContext()) CharacterLiteral( |
| 7685 | E->getValue(), E->getKind(), *ToTypeOrErr, *ToLocationOrErr); |
| 7686 | } |
| 7687 | |
| 7688 | ExpectedStmt ASTNodeImporter::VisitStringLiteral(StringLiteral *E) { |
| 7689 | ExpectedType ToTypeOrErr = import(E->getType()); |
| 7690 | if (!ToTypeOrErr) |
| 7691 | return ToTypeOrErr.takeError(); |
| 7692 | |
| 7693 | SmallVector<SourceLocation, 4> ToLocations(E->getNumConcatenated()); |
| 7694 | if (Error Err = ImportArrayChecked( |
| 7695 | Ibegin: E->tokloc_begin(), Iend: E->tokloc_end(), Obegin: ToLocations.begin())) |
| 7696 | return std::move(Err); |
| 7697 | |
| 7698 | return StringLiteral::Create( |
| 7699 | Ctx: Importer.getToContext(), Str: E->getBytes(), Kind: E->getKind(), Pascal: E->isPascal(), |
| 7700 | Ty: *ToTypeOrErr, Loc: ToLocations.data(), NumConcatenated: ToLocations.size()); |
| 7701 | } |
| 7702 | |
| 7703 | ExpectedStmt ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 7704 | |
| 7705 | Error Err = Error::success(); |
| 7706 | auto ToLParenLoc = importChecked(Err, From: E->getLParenLoc()); |
| 7707 | auto ToTypeSourceInfo = importChecked(Err, From: E->getTypeSourceInfo()); |
| 7708 | auto ToType = importChecked(Err, E->getType()); |
| 7709 | auto ToInitializer = importChecked(Err, From: E->getInitializer()); |
| 7710 | if (Err) |
| 7711 | return std::move(Err); |
| 7712 | |
| 7713 | return new (Importer.getToContext()) CompoundLiteralExpr( |
| 7714 | ToLParenLoc, ToTypeSourceInfo, ToType, E->getValueKind(), |
| 7715 | ToInitializer, E->isFileScope()); |
| 7716 | } |
| 7717 | |
| 7718 | ExpectedStmt ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) { |
| 7719 | |
| 7720 | Error Err = Error::success(); |
| 7721 | auto ToBuiltinLoc = importChecked(Err, From: E->getBuiltinLoc()); |
| 7722 | auto ToType = importChecked(Err, E->getType()); |
| 7723 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 7724 | if (Err) |
| 7725 | return std::move(Err); |
| 7726 | |
| 7727 | SmallVector<Expr *, 6> ToExprs(E->getNumSubExprs()); |
| 7728 | if (Error Err = ImportArrayChecked( |
| 7729 | Ibegin: E->getSubExprs(), Iend: E->getSubExprs() + E->getNumSubExprs(), |
| 7730 | Obegin: ToExprs.begin())) |
| 7731 | return std::move(Err); |
| 7732 | |
| 7733 | return new (Importer.getToContext()) AtomicExpr( |
| 7734 | |
| 7735 | ToBuiltinLoc, ToExprs, ToType, E->getOp(), ToRParenLoc); |
| 7736 | } |
| 7737 | |
| 7738 | ExpectedStmt ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) { |
| 7739 | Error Err = Error::success(); |
| 7740 | auto ToAmpAmpLoc = importChecked(Err, From: E->getAmpAmpLoc()); |
| 7741 | auto ToLabelLoc = importChecked(Err, From: E->getLabelLoc()); |
| 7742 | auto ToLabel = importChecked(Err, From: E->getLabel()); |
| 7743 | auto ToType = importChecked(Err, E->getType()); |
| 7744 | if (Err) |
| 7745 | return std::move(Err); |
| 7746 | |
| 7747 | return new (Importer.getToContext()) AddrLabelExpr( |
| 7748 | ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType); |
| 7749 | } |
| 7750 | ExpectedStmt ASTNodeImporter::VisitConstantExpr(ConstantExpr *E) { |
| 7751 | Error Err = Error::success(); |
| 7752 | auto ToSubExpr = importChecked(Err, E->getSubExpr()); |
| 7753 | auto ToResult = importChecked(Err, From: E->getAPValueResult()); |
| 7754 | if (Err) |
| 7755 | return std::move(Err); |
| 7756 | |
| 7757 | return ConstantExpr::Create(Importer.getToContext(), ToSubExpr, ToResult); |
| 7758 | } |
| 7759 | ExpectedStmt ASTNodeImporter::VisitParenExpr(ParenExpr *E) { |
| 7760 | Error Err = Error::success(); |
| 7761 | auto ToLParen = importChecked(Err, From: E->getLParen()); |
| 7762 | auto ToRParen = importChecked(Err, From: E->getRParen()); |
| 7763 | auto ToSubExpr = importChecked(Err, From: E->getSubExpr()); |
| 7764 | if (Err) |
| 7765 | return std::move(Err); |
| 7766 | |
| 7767 | return new (Importer.getToContext()) |
| 7768 | ParenExpr(ToLParen, ToRParen, ToSubExpr); |
| 7769 | } |
| 7770 | |
| 7771 | ExpectedStmt ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) { |
| 7772 | SmallVector<Expr *, 4> ToExprs(E->getNumExprs()); |
| 7773 | if (Error Err = ImportContainerChecked(InContainer: E->exprs(), OutContainer&: ToExprs)) |
| 7774 | return std::move(Err); |
| 7775 | |
| 7776 | ExpectedSLoc ToLParenLocOrErr = import(From: E->getLParenLoc()); |
| 7777 | if (!ToLParenLocOrErr) |
| 7778 | return ToLParenLocOrErr.takeError(); |
| 7779 | |
| 7780 | ExpectedSLoc ToRParenLocOrErr = import(From: E->getRParenLoc()); |
| 7781 | if (!ToRParenLocOrErr) |
| 7782 | return ToRParenLocOrErr.takeError(); |
| 7783 | |
| 7784 | return ParenListExpr::Create(Ctx: Importer.getToContext(), LParenLoc: *ToLParenLocOrErr, |
| 7785 | Exprs: ToExprs, RParenLoc: *ToRParenLocOrErr); |
| 7786 | } |
| 7787 | |
| 7788 | ExpectedStmt ASTNodeImporter::VisitStmtExpr(StmtExpr *E) { |
| 7789 | Error Err = Error::success(); |
| 7790 | auto ToSubStmt = importChecked(Err, From: E->getSubStmt()); |
| 7791 | auto ToType = importChecked(Err, E->getType()); |
| 7792 | auto ToLParenLoc = importChecked(Err, From: E->getLParenLoc()); |
| 7793 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 7794 | if (Err) |
| 7795 | return std::move(Err); |
| 7796 | |
| 7797 | return new (Importer.getToContext()) |
| 7798 | StmtExpr(ToSubStmt, ToType, ToLParenLoc, ToRParenLoc, |
| 7799 | E->getTemplateDepth()); |
| 7800 | } |
| 7801 | |
| 7802 | ExpectedStmt ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) { |
| 7803 | Error Err = Error::success(); |
| 7804 | auto ToSubExpr = importChecked(Err, From: E->getSubExpr()); |
| 7805 | auto ToType = importChecked(Err, E->getType()); |
| 7806 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
| 7807 | if (Err) |
| 7808 | return std::move(Err); |
| 7809 | |
| 7810 | auto *UO = UnaryOperator::CreateEmpty(C: Importer.getToContext(), |
| 7811 | hasFPFeatures: E->hasStoredFPFeatures()); |
| 7812 | UO->setType(ToType); |
| 7813 | UO->setSubExpr(ToSubExpr); |
| 7814 | UO->setOpcode(E->getOpcode()); |
| 7815 | UO->setOperatorLoc(ToOperatorLoc); |
| 7816 | UO->setCanOverflow(E->canOverflow()); |
| 7817 | if (E->hasStoredFPFeatures()) |
| 7818 | UO->setStoredFPFeatures(E->getStoredFPFeatures()); |
| 7819 | |
| 7820 | return UO; |
| 7821 | } |
| 7822 | |
| 7823 | ExpectedStmt |
| 7824 | |
| 7825 | ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) { |
| 7826 | Error Err = Error::success(); |
| 7827 | auto ToType = importChecked(Err, E->getType()); |
| 7828 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
| 7829 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 7830 | if (Err) |
| 7831 | return std::move(Err); |
| 7832 | |
| 7833 | if (E->isArgumentType()) { |
| 7834 | Expected<TypeSourceInfo *> ToArgumentTypeInfoOrErr = |
| 7835 | import(From: E->getArgumentTypeInfo()); |
| 7836 | if (!ToArgumentTypeInfoOrErr) |
| 7837 | return ToArgumentTypeInfoOrErr.takeError(); |
| 7838 | |
| 7839 | return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr( |
| 7840 | E->getKind(), *ToArgumentTypeInfoOrErr, ToType, ToOperatorLoc, |
| 7841 | ToRParenLoc); |
| 7842 | } |
| 7843 | |
| 7844 | ExpectedExpr ToArgumentExprOrErr = import(From: E->getArgumentExpr()); |
| 7845 | if (!ToArgumentExprOrErr) |
| 7846 | return ToArgumentExprOrErr.takeError(); |
| 7847 | |
| 7848 | return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr( |
| 7849 | E->getKind(), *ToArgumentExprOrErr, ToType, ToOperatorLoc, ToRParenLoc); |
| 7850 | } |
| 7851 | |
| 7852 | ExpectedStmt ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) { |
| 7853 | Error Err = Error::success(); |
| 7854 | auto ToLHS = importChecked(Err, From: E->getLHS()); |
| 7855 | auto ToRHS = importChecked(Err, From: E->getRHS()); |
| 7856 | auto ToType = importChecked(Err, E->getType()); |
| 7857 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
| 7858 | if (Err) |
| 7859 | return std::move(Err); |
| 7860 | |
| 7861 | return BinaryOperator::Create( |
| 7862 | C: Importer.getToContext(), lhs: ToLHS, rhs: ToRHS, opc: E->getOpcode(), ResTy: ToType, |
| 7863 | VK: E->getValueKind(), OK: E->getObjectKind(), opLoc: ToOperatorLoc, |
| 7864 | FPFeatures: E->getFPFeatures()); |
| 7865 | } |
| 7866 | |
| 7867 | ExpectedStmt ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) { |
| 7868 | Error Err = Error::success(); |
| 7869 | auto ToCond = importChecked(Err, From: E->getCond()); |
| 7870 | auto ToQuestionLoc = importChecked(Err, E->getQuestionLoc()); |
| 7871 | auto ToLHS = importChecked(Err, From: E->getLHS()); |
| 7872 | auto ToColonLoc = importChecked(Err, E->getColonLoc()); |
| 7873 | auto ToRHS = importChecked(Err, From: E->getRHS()); |
| 7874 | auto ToType = importChecked(Err, E->getType()); |
| 7875 | if (Err) |
| 7876 | return std::move(Err); |
| 7877 | |
| 7878 | return new (Importer.getToContext()) ConditionalOperator( |
| 7879 | ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType, |
| 7880 | E->getValueKind(), E->getObjectKind()); |
| 7881 | } |
| 7882 | |
| 7883 | ExpectedStmt |
| 7884 | ASTNodeImporter::VisitBinaryConditionalOperator(BinaryConditionalOperator *E) { |
| 7885 | Error Err = Error::success(); |
| 7886 | auto ToCommon = importChecked(Err, From: E->getCommon()); |
| 7887 | auto ToOpaqueValue = importChecked(Err, From: E->getOpaqueValue()); |
| 7888 | auto ToCond = importChecked(Err, From: E->getCond()); |
| 7889 | auto ToTrueExpr = importChecked(Err, From: E->getTrueExpr()); |
| 7890 | auto ToFalseExpr = importChecked(Err, From: E->getFalseExpr()); |
| 7891 | auto ToQuestionLoc = importChecked(Err, E->getQuestionLoc()); |
| 7892 | auto ToColonLoc = importChecked(Err, E->getColonLoc()); |
| 7893 | auto ToType = importChecked(Err, E->getType()); |
| 7894 | if (Err) |
| 7895 | return std::move(Err); |
| 7896 | |
| 7897 | return new (Importer.getToContext()) BinaryConditionalOperator( |
| 7898 | ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr, |
| 7899 | ToQuestionLoc, ToColonLoc, ToType, E->getValueKind(), |
| 7900 | E->getObjectKind()); |
| 7901 | } |
| 7902 | |
| 7903 | ExpectedStmt ASTNodeImporter::VisitCXXRewrittenBinaryOperator( |
| 7904 | CXXRewrittenBinaryOperator *E) { |
| 7905 | Error Err = Error::success(); |
| 7906 | auto ToSemanticForm = importChecked(Err, From: E->getSemanticForm()); |
| 7907 | if (Err) |
| 7908 | return std::move(Err); |
| 7909 | |
| 7910 | return new (Importer.getToContext()) |
| 7911 | CXXRewrittenBinaryOperator(ToSemanticForm, E->isReversed()); |
| 7912 | } |
| 7913 | |
| 7914 | ExpectedStmt ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { |
| 7915 | Error Err = Error::success(); |
| 7916 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
| 7917 | auto ToQueriedTypeSourceInfo = |
| 7918 | importChecked(Err, From: E->getQueriedTypeSourceInfo()); |
| 7919 | auto ToDimensionExpression = importChecked(Err, From: E->getDimensionExpression()); |
| 7920 | auto ToEndLoc = importChecked(Err, From: E->getEndLoc()); |
| 7921 | auto ToType = importChecked(Err, E->getType()); |
| 7922 | if (Err) |
| 7923 | return std::move(Err); |
| 7924 | |
| 7925 | return new (Importer.getToContext()) ArrayTypeTraitExpr( |
| 7926 | ToBeginLoc, E->getTrait(), ToQueriedTypeSourceInfo, E->getValue(), |
| 7927 | ToDimensionExpression, ToEndLoc, ToType); |
| 7928 | } |
| 7929 | |
| 7930 | ExpectedStmt ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { |
| 7931 | Error Err = Error::success(); |
| 7932 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
| 7933 | auto ToQueriedExpression = importChecked(Err, From: E->getQueriedExpression()); |
| 7934 | auto ToEndLoc = importChecked(Err, From: E->getEndLoc()); |
| 7935 | auto ToType = importChecked(Err, E->getType()); |
| 7936 | if (Err) |
| 7937 | return std::move(Err); |
| 7938 | |
| 7939 | return new (Importer.getToContext()) ExpressionTraitExpr( |
| 7940 | ToBeginLoc, E->getTrait(), ToQueriedExpression, E->getValue(), |
| 7941 | ToEndLoc, ToType); |
| 7942 | } |
| 7943 | |
| 7944 | ExpectedStmt ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) { |
| 7945 | Error Err = Error::success(); |
| 7946 | auto ToLocation = importChecked(Err, From: E->getLocation()); |
| 7947 | auto ToType = importChecked(Err, E->getType()); |
| 7948 | auto ToSourceExpr = importChecked(Err, From: E->getSourceExpr()); |
| 7949 | if (Err) |
| 7950 | return std::move(Err); |
| 7951 | |
| 7952 | return new (Importer.getToContext()) OpaqueValueExpr( |
| 7953 | ToLocation, ToType, E->getValueKind(), E->getObjectKind(), ToSourceExpr); |
| 7954 | } |
| 7955 | |
| 7956 | ExpectedStmt ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { |
| 7957 | Error Err = Error::success(); |
| 7958 | auto ToLHS = importChecked(Err, From: E->getLHS()); |
| 7959 | auto ToRHS = importChecked(Err, From: E->getRHS()); |
| 7960 | auto ToType = importChecked(Err, E->getType()); |
| 7961 | auto ToRBracketLoc = importChecked(Err, From: E->getRBracketLoc()); |
| 7962 | if (Err) |
| 7963 | return std::move(Err); |
| 7964 | |
| 7965 | return new (Importer.getToContext()) ArraySubscriptExpr( |
| 7966 | ToLHS, ToRHS, ToType, E->getValueKind(), E->getObjectKind(), |
| 7967 | ToRBracketLoc); |
| 7968 | } |
| 7969 | |
| 7970 | ExpectedStmt |
| 7971 | ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) { |
| 7972 | Error Err = Error::success(); |
| 7973 | auto ToLHS = importChecked(Err, E->getLHS()); |
| 7974 | auto ToRHS = importChecked(Err, E->getRHS()); |
| 7975 | auto ToType = importChecked(Err, E->getType()); |
| 7976 | auto ToComputationLHSType = importChecked(Err, From: E->getComputationLHSType()); |
| 7977 | auto ToComputationResultType = |
| 7978 | importChecked(Err, From: E->getComputationResultType()); |
| 7979 | auto ToOperatorLoc = importChecked(Err, E->getOperatorLoc()); |
| 7980 | if (Err) |
| 7981 | return std::move(Err); |
| 7982 | |
| 7983 | return CompoundAssignOperator::Create( |
| 7984 | C: Importer.getToContext(), lhs: ToLHS, rhs: ToRHS, opc: E->getOpcode(), ResTy: ToType, |
| 7985 | VK: E->getValueKind(), OK: E->getObjectKind(), opLoc: ToOperatorLoc, |
| 7986 | FPFeatures: E->getFPFeatures(), |
| 7987 | CompLHSType: ToComputationLHSType, CompResultType: ToComputationResultType); |
| 7988 | } |
| 7989 | |
| 7990 | Expected<CXXCastPath> |
| 7991 | ASTNodeImporter::ImportCastPath(CastExpr *CE) { |
| 7992 | CXXCastPath Path; |
| 7993 | for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) { |
| 7994 | if (auto SpecOrErr = import(From: *I)) |
| 7995 | Path.push_back(Elt: *SpecOrErr); |
| 7996 | else |
| 7997 | return SpecOrErr.takeError(); |
| 7998 | } |
| 7999 | return Path; |
| 8000 | } |
| 8001 | |
| 8002 | ExpectedStmt ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 8003 | ExpectedType ToTypeOrErr = import(E->getType()); |
| 8004 | if (!ToTypeOrErr) |
| 8005 | return ToTypeOrErr.takeError(); |
| 8006 | |
| 8007 | ExpectedExpr ToSubExprOrErr = import(E->getSubExpr()); |
| 8008 | if (!ToSubExprOrErr) |
| 8009 | return ToSubExprOrErr.takeError(); |
| 8010 | |
| 8011 | Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E); |
| 8012 | if (!ToBasePathOrErr) |
| 8013 | return ToBasePathOrErr.takeError(); |
| 8014 | |
| 8015 | return ImplicitCastExpr::Create( |
| 8016 | Context: Importer.getToContext(), T: *ToTypeOrErr, Kind: E->getCastKind(), Operand: *ToSubExprOrErr, |
| 8017 | BasePath: &(*ToBasePathOrErr), Cat: E->getValueKind(), FPO: E->getFPFeatures()); |
| 8018 | } |
| 8019 | |
| 8020 | ExpectedStmt ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) { |
| 8021 | Error Err = Error::success(); |
| 8022 | auto ToType = importChecked(Err, E->getType()); |
| 8023 | auto ToSubExpr = importChecked(Err, E->getSubExpr()); |
| 8024 | auto ToTypeInfoAsWritten = importChecked(Err, From: E->getTypeInfoAsWritten()); |
| 8025 | if (Err) |
| 8026 | return std::move(Err); |
| 8027 | |
| 8028 | Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E); |
| 8029 | if (!ToBasePathOrErr) |
| 8030 | return ToBasePathOrErr.takeError(); |
| 8031 | CXXCastPath *ToBasePath = &(*ToBasePathOrErr); |
| 8032 | |
| 8033 | switch (E->getStmtClass()) { |
| 8034 | case Stmt::CStyleCastExprClass: { |
| 8035 | auto *CCE = cast<CStyleCastExpr>(Val: E); |
| 8036 | ExpectedSLoc ToLParenLocOrErr = import(From: CCE->getLParenLoc()); |
| 8037 | if (!ToLParenLocOrErr) |
| 8038 | return ToLParenLocOrErr.takeError(); |
| 8039 | ExpectedSLoc ToRParenLocOrErr = import(From: CCE->getRParenLoc()); |
| 8040 | if (!ToRParenLocOrErr) |
| 8041 | return ToRParenLocOrErr.takeError(); |
| 8042 | return CStyleCastExpr::Create( |
| 8043 | Context: Importer.getToContext(), T: ToType, VK: E->getValueKind(), K: E->getCastKind(), |
| 8044 | Op: ToSubExpr, BasePath: ToBasePath, FPO: CCE->getFPFeatures(), WrittenTy: ToTypeInfoAsWritten, |
| 8045 | L: *ToLParenLocOrErr, R: *ToRParenLocOrErr); |
| 8046 | } |
| 8047 | |
| 8048 | case Stmt::CXXFunctionalCastExprClass: { |
| 8049 | auto *FCE = cast<CXXFunctionalCastExpr>(Val: E); |
| 8050 | ExpectedSLoc ToLParenLocOrErr = import(From: FCE->getLParenLoc()); |
| 8051 | if (!ToLParenLocOrErr) |
| 8052 | return ToLParenLocOrErr.takeError(); |
| 8053 | ExpectedSLoc ToRParenLocOrErr = import(From: FCE->getRParenLoc()); |
| 8054 | if (!ToRParenLocOrErr) |
| 8055 | return ToRParenLocOrErr.takeError(); |
| 8056 | return CXXFunctionalCastExpr::Create( |
| 8057 | Context: Importer.getToContext(), T: ToType, VK: E->getValueKind(), Written: ToTypeInfoAsWritten, |
| 8058 | Kind: E->getCastKind(), Op: ToSubExpr, Path: ToBasePath, FPO: FCE->getFPFeatures(), |
| 8059 | LPLoc: *ToLParenLocOrErr, RPLoc: *ToRParenLocOrErr); |
| 8060 | } |
| 8061 | |
| 8062 | case Stmt::ObjCBridgedCastExprClass: { |
| 8063 | auto *OCE = cast<ObjCBridgedCastExpr>(Val: E); |
| 8064 | ExpectedSLoc ToLParenLocOrErr = import(From: OCE->getLParenLoc()); |
| 8065 | if (!ToLParenLocOrErr) |
| 8066 | return ToLParenLocOrErr.takeError(); |
| 8067 | ExpectedSLoc ToBridgeKeywordLocOrErr = import(From: OCE->getBridgeKeywordLoc()); |
| 8068 | if (!ToBridgeKeywordLocOrErr) |
| 8069 | return ToBridgeKeywordLocOrErr.takeError(); |
| 8070 | return new (Importer.getToContext()) ObjCBridgedCastExpr( |
| 8071 | *ToLParenLocOrErr, OCE->getBridgeKind(), E->getCastKind(), |
| 8072 | *ToBridgeKeywordLocOrErr, ToTypeInfoAsWritten, ToSubExpr); |
| 8073 | } |
| 8074 | case Stmt::BuiltinBitCastExprClass: { |
| 8075 | auto *BBC = cast<BuiltinBitCastExpr>(Val: E); |
| 8076 | ExpectedSLoc ToKWLocOrErr = import(From: BBC->getBeginLoc()); |
| 8077 | if (!ToKWLocOrErr) |
| 8078 | return ToKWLocOrErr.takeError(); |
| 8079 | ExpectedSLoc ToRParenLocOrErr = import(From: BBC->getEndLoc()); |
| 8080 | if (!ToRParenLocOrErr) |
| 8081 | return ToRParenLocOrErr.takeError(); |
| 8082 | return new (Importer.getToContext()) BuiltinBitCastExpr( |
| 8083 | ToType, E->getValueKind(), E->getCastKind(), ToSubExpr, |
| 8084 | ToTypeInfoAsWritten, *ToKWLocOrErr, *ToRParenLocOrErr); |
| 8085 | } |
| 8086 | default: |
| 8087 | llvm_unreachable("Cast expression of unsupported type!" ); |
| 8088 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
| 8089 | } |
| 8090 | } |
| 8091 | |
| 8092 | ExpectedStmt ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *E) { |
| 8093 | SmallVector<OffsetOfNode, 4> ToNodes; |
| 8094 | for (int I = 0, N = E->getNumComponents(); I < N; ++I) { |
| 8095 | const OffsetOfNode &FromNode = E->getComponent(Idx: I); |
| 8096 | |
| 8097 | SourceLocation ToBeginLoc, ToEndLoc; |
| 8098 | |
| 8099 | if (FromNode.getKind() != OffsetOfNode::Base) { |
| 8100 | Error Err = Error::success(); |
| 8101 | ToBeginLoc = importChecked(Err, From: FromNode.getBeginLoc()); |
| 8102 | ToEndLoc = importChecked(Err, From: FromNode.getEndLoc()); |
| 8103 | if (Err) |
| 8104 | return std::move(Err); |
| 8105 | } |
| 8106 | |
| 8107 | switch (FromNode.getKind()) { |
| 8108 | case OffsetOfNode::Array: |
| 8109 | ToNodes.push_back( |
| 8110 | Elt: OffsetOfNode(ToBeginLoc, FromNode.getArrayExprIndex(), ToEndLoc)); |
| 8111 | break; |
| 8112 | case OffsetOfNode::Base: { |
| 8113 | auto ToBSOrErr = import(From: FromNode.getBase()); |
| 8114 | if (!ToBSOrErr) |
| 8115 | return ToBSOrErr.takeError(); |
| 8116 | ToNodes.push_back(Elt: OffsetOfNode(*ToBSOrErr)); |
| 8117 | break; |
| 8118 | } |
| 8119 | case OffsetOfNode::Field: { |
| 8120 | auto ToFieldOrErr = import(From: FromNode.getField()); |
| 8121 | if (!ToFieldOrErr) |
| 8122 | return ToFieldOrErr.takeError(); |
| 8123 | ToNodes.push_back(Elt: OffsetOfNode(ToBeginLoc, *ToFieldOrErr, ToEndLoc)); |
| 8124 | break; |
| 8125 | } |
| 8126 | case OffsetOfNode::Identifier: { |
| 8127 | IdentifierInfo *ToII = Importer.Import(FromId: FromNode.getFieldName()); |
| 8128 | ToNodes.push_back(Elt: OffsetOfNode(ToBeginLoc, ToII, ToEndLoc)); |
| 8129 | break; |
| 8130 | } |
| 8131 | } |
| 8132 | } |
| 8133 | |
| 8134 | SmallVector<Expr *, 4> ToExprs(E->getNumExpressions()); |
| 8135 | for (int I = 0, N = E->getNumExpressions(); I < N; ++I) { |
| 8136 | ExpectedExpr ToIndexExprOrErr = import(From: E->getIndexExpr(Idx: I)); |
| 8137 | if (!ToIndexExprOrErr) |
| 8138 | return ToIndexExprOrErr.takeError(); |
| 8139 | ToExprs[I] = *ToIndexExprOrErr; |
| 8140 | } |
| 8141 | |
| 8142 | Error Err = Error::success(); |
| 8143 | auto ToType = importChecked(Err, E->getType()); |
| 8144 | auto ToTypeSourceInfo = importChecked(Err, From: E->getTypeSourceInfo()); |
| 8145 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
| 8146 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 8147 | if (Err) |
| 8148 | return std::move(Err); |
| 8149 | |
| 8150 | return OffsetOfExpr::Create( |
| 8151 | C: Importer.getToContext(), type: ToType, OperatorLoc: ToOperatorLoc, tsi: ToTypeSourceInfo, comps: ToNodes, |
| 8152 | exprs: ToExprs, RParenLoc: ToRParenLoc); |
| 8153 | } |
| 8154 | |
| 8155 | ExpectedStmt ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { |
| 8156 | Error Err = Error::success(); |
| 8157 | auto ToType = importChecked(Err, E->getType()); |
| 8158 | auto ToOperand = importChecked(Err, From: E->getOperand()); |
| 8159 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
| 8160 | auto ToEndLoc = importChecked(Err, From: E->getEndLoc()); |
| 8161 | if (Err) |
| 8162 | return std::move(Err); |
| 8163 | |
| 8164 | CanThrowResult ToCanThrow; |
| 8165 | if (E->isValueDependent()) |
| 8166 | ToCanThrow = CT_Dependent; |
| 8167 | else |
| 8168 | ToCanThrow = E->getValue() ? CT_Can : CT_Cannot; |
| 8169 | |
| 8170 | return new (Importer.getToContext()) CXXNoexceptExpr( |
| 8171 | ToType, ToOperand, ToCanThrow, ToBeginLoc, ToEndLoc); |
| 8172 | } |
| 8173 | |
| 8174 | ExpectedStmt ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) { |
| 8175 | Error Err = Error::success(); |
| 8176 | auto ToSubExpr = importChecked(Err, From: E->getSubExpr()); |
| 8177 | auto ToType = importChecked(Err, E->getType()); |
| 8178 | auto ToThrowLoc = importChecked(Err, From: E->getThrowLoc()); |
| 8179 | if (Err) |
| 8180 | return std::move(Err); |
| 8181 | |
| 8182 | return new (Importer.getToContext()) CXXThrowExpr( |
| 8183 | ToSubExpr, ToType, ToThrowLoc, E->isThrownVariableInScope()); |
| 8184 | } |
| 8185 | |
| 8186 | ExpectedStmt ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { |
| 8187 | ExpectedSLoc ToUsedLocOrErr = import(From: E->getUsedLocation()); |
| 8188 | if (!ToUsedLocOrErr) |
| 8189 | return ToUsedLocOrErr.takeError(); |
| 8190 | |
| 8191 | auto ToParamOrErr = import(From: E->getParam()); |
| 8192 | if (!ToParamOrErr) |
| 8193 | return ToParamOrErr.takeError(); |
| 8194 | |
| 8195 | auto UsedContextOrErr = Importer.ImportContext(FromDC: E->getUsedContext()); |
| 8196 | if (!UsedContextOrErr) |
| 8197 | return UsedContextOrErr.takeError(); |
| 8198 | |
| 8199 | // Import the default arg if it was not imported yet. |
| 8200 | // This is needed because it can happen that during the import of the |
| 8201 | // default expression (from VisitParmVarDecl) the same ParmVarDecl is |
| 8202 | // encountered here. The default argument for a ParmVarDecl is set in the |
| 8203 | // ParmVarDecl only after it is imported (set in VisitParmVarDecl if not here, |
| 8204 | // see VisitParmVarDecl). |
| 8205 | ParmVarDecl *ToParam = *ToParamOrErr; |
| 8206 | if (!ToParam->getDefaultArg()) { |
| 8207 | std::optional<ParmVarDecl *> FromParam = |
| 8208 | Importer.getImportedFromDecl(ToD: ToParam); |
| 8209 | assert(FromParam && "ParmVarDecl was not imported?" ); |
| 8210 | |
| 8211 | if (Error Err = ImportDefaultArgOfParmVarDecl(FromParam: *FromParam, ToParam)) |
| 8212 | return std::move(Err); |
| 8213 | } |
| 8214 | Expr *RewrittenInit = nullptr; |
| 8215 | if (E->hasRewrittenInit()) { |
| 8216 | ExpectedExpr ExprOrErr = import(From: E->getRewrittenExpr()); |
| 8217 | if (!ExprOrErr) |
| 8218 | return ExprOrErr.takeError(); |
| 8219 | RewrittenInit = ExprOrErr.get(); |
| 8220 | } |
| 8221 | return CXXDefaultArgExpr::Create(C: Importer.getToContext(), Loc: *ToUsedLocOrErr, |
| 8222 | Param: *ToParamOrErr, RewrittenExpr: RewrittenInit, |
| 8223 | UsedContext: *UsedContextOrErr); |
| 8224 | } |
| 8225 | |
| 8226 | ExpectedStmt |
| 8227 | ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { |
| 8228 | Error Err = Error::success(); |
| 8229 | auto ToType = importChecked(Err, E->getType()); |
| 8230 | auto ToTypeSourceInfo = importChecked(Err, From: E->getTypeSourceInfo()); |
| 8231 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 8232 | if (Err) |
| 8233 | return std::move(Err); |
| 8234 | |
| 8235 | return new (Importer.getToContext()) CXXScalarValueInitExpr( |
| 8236 | ToType, ToTypeSourceInfo, ToRParenLoc); |
| 8237 | } |
| 8238 | |
| 8239 | ExpectedStmt |
| 8240 | ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { |
| 8241 | ExpectedExpr ToSubExprOrErr = import(From: E->getSubExpr()); |
| 8242 | if (!ToSubExprOrErr) |
| 8243 | return ToSubExprOrErr.takeError(); |
| 8244 | |
| 8245 | auto ToDtorOrErr = import(From: E->getTemporary()->getDestructor()); |
| 8246 | if (!ToDtorOrErr) |
| 8247 | return ToDtorOrErr.takeError(); |
| 8248 | |
| 8249 | ASTContext &ToCtx = Importer.getToContext(); |
| 8250 | CXXTemporary *Temp = CXXTemporary::Create(C: ToCtx, Destructor: *ToDtorOrErr); |
| 8251 | return CXXBindTemporaryExpr::Create(C: ToCtx, Temp, SubExpr: *ToSubExprOrErr); |
| 8252 | } |
| 8253 | |
| 8254 | ExpectedStmt |
| 8255 | |
| 8256 | ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) { |
| 8257 | Error Err = Error::success(); |
| 8258 | auto ToConstructor = importChecked(Err, E->getConstructor()); |
| 8259 | auto ToType = importChecked(Err, E->getType()); |
| 8260 | auto ToTypeSourceInfo = importChecked(Err, From: E->getTypeSourceInfo()); |
| 8261 | auto ToParenOrBraceRange = importChecked(Err, E->getParenOrBraceRange()); |
| 8262 | if (Err) |
| 8263 | return std::move(Err); |
| 8264 | |
| 8265 | SmallVector<Expr *, 8> ToArgs(E->getNumArgs()); |
| 8266 | if (Error Err = ImportContainerChecked(E->arguments(), ToArgs)) |
| 8267 | return std::move(Err); |
| 8268 | |
| 8269 | return CXXTemporaryObjectExpr::Create( |
| 8270 | Ctx: Importer.getToContext(), Cons: ToConstructor, Ty: ToType, TSI: ToTypeSourceInfo, Args: ToArgs, |
| 8271 | ParenOrBraceRange: ToParenOrBraceRange, HadMultipleCandidates: E->hadMultipleCandidates(), |
| 8272 | ListInitialization: E->isListInitialization(), StdInitListInitialization: E->isStdInitListInitialization(), |
| 8273 | ZeroInitialization: E->requiresZeroInitialization()); |
| 8274 | } |
| 8275 | |
| 8276 | ExpectedDecl ASTNodeImporter::VisitLifetimeExtendedTemporaryDecl( |
| 8277 | LifetimeExtendedTemporaryDecl *D) { |
| 8278 | DeclContext *DC, *LexicalDC; |
| 8279 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) |
| 8280 | return std::move(Err); |
| 8281 | |
| 8282 | Error Err = Error::success(); |
| 8283 | auto Temporary = importChecked(Err, From: D->getTemporaryExpr()); |
| 8284 | auto ExtendingDecl = importChecked(Err, From: D->getExtendingDecl()); |
| 8285 | if (Err) |
| 8286 | return std::move(Err); |
| 8287 | // FIXME: Should ManglingNumber get numbers associated with 'to' context? |
| 8288 | |
| 8289 | LifetimeExtendedTemporaryDecl *To; |
| 8290 | if (GetImportedOrCreateDecl(ToD&: To, FromD: D, args&: Temporary, args&: ExtendingDecl, |
| 8291 | args: D->getManglingNumber())) |
| 8292 | return To; |
| 8293 | |
| 8294 | To->setLexicalDeclContext(LexicalDC); |
| 8295 | LexicalDC->addDeclInternal(To); |
| 8296 | return To; |
| 8297 | } |
| 8298 | |
| 8299 | ExpectedStmt |
| 8300 | ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) { |
| 8301 | Error Err = Error::success(); |
| 8302 | auto ToType = importChecked(Err, E->getType()); |
| 8303 | Expr *ToTemporaryExpr = importChecked( |
| 8304 | Err, From: E->getLifetimeExtendedTemporaryDecl() ? nullptr : E->getSubExpr()); |
| 8305 | auto ToMaterializedDecl = |
| 8306 | importChecked(Err, From: E->getLifetimeExtendedTemporaryDecl()); |
| 8307 | if (Err) |
| 8308 | return std::move(Err); |
| 8309 | |
| 8310 | if (!ToTemporaryExpr) |
| 8311 | ToTemporaryExpr = cast<Expr>(Val: ToMaterializedDecl->getTemporaryExpr()); |
| 8312 | |
| 8313 | auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr( |
| 8314 | ToType, ToTemporaryExpr, E->isBoundToLvalueReference(), |
| 8315 | ToMaterializedDecl); |
| 8316 | |
| 8317 | return ToMTE; |
| 8318 | } |
| 8319 | |
| 8320 | ExpectedStmt ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) { |
| 8321 | Error Err = Error::success(); |
| 8322 | auto *ToPattern = importChecked(Err, From: E->getPattern()); |
| 8323 | auto ToEllipsisLoc = importChecked(Err, From: E->getEllipsisLoc()); |
| 8324 | if (Err) |
| 8325 | return std::move(Err); |
| 8326 | |
| 8327 | return new (Importer.getToContext()) |
| 8328 | PackExpansionExpr(ToPattern, ToEllipsisLoc, E->getNumExpansions()); |
| 8329 | } |
| 8330 | |
| 8331 | ExpectedStmt ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) { |
| 8332 | Error Err = Error::success(); |
| 8333 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
| 8334 | auto ToPack = importChecked(Err, From: E->getPack()); |
| 8335 | auto ToPackLoc = importChecked(Err, From: E->getPackLoc()); |
| 8336 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 8337 | if (Err) |
| 8338 | return std::move(Err); |
| 8339 | |
| 8340 | UnsignedOrNone Length = std::nullopt; |
| 8341 | if (!E->isValueDependent()) |
| 8342 | Length = E->getPackLength(); |
| 8343 | |
| 8344 | SmallVector<TemplateArgument, 8> ToPartialArguments; |
| 8345 | if (E->isPartiallySubstituted()) { |
| 8346 | if (Error Err = ImportTemplateArguments(FromArgs: E->getPartialArguments(), |
| 8347 | ToArgs&: ToPartialArguments)) |
| 8348 | return std::move(Err); |
| 8349 | } |
| 8350 | |
| 8351 | return SizeOfPackExpr::Create( |
| 8352 | Context&: Importer.getToContext(), OperatorLoc: ToOperatorLoc, Pack: ToPack, PackLoc: ToPackLoc, RParenLoc: ToRParenLoc, |
| 8353 | Length, PartialArgs: ToPartialArguments); |
| 8354 | } |
| 8355 | |
| 8356 | |
| 8357 | ExpectedStmt ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *E) { |
| 8358 | Error Err = Error::success(); |
| 8359 | auto ToOperatorNew = importChecked(Err, From: E->getOperatorNew()); |
| 8360 | auto ToOperatorDelete = importChecked(Err, From: E->getOperatorDelete()); |
| 8361 | auto ToTypeIdParens = importChecked(Err, From: E->getTypeIdParens()); |
| 8362 | auto ToArraySize = importChecked(Err, From: E->getArraySize()); |
| 8363 | auto ToInitializer = importChecked(Err, From: E->getInitializer()); |
| 8364 | auto ToType = importChecked(Err, E->getType()); |
| 8365 | auto ToAllocatedTypeSourceInfo = |
| 8366 | importChecked(Err, From: E->getAllocatedTypeSourceInfo()); |
| 8367 | auto ToSourceRange = importChecked(Err, From: E->getSourceRange()); |
| 8368 | auto ToDirectInitRange = importChecked(Err, From: E->getDirectInitRange()); |
| 8369 | if (Err) |
| 8370 | return std::move(Err); |
| 8371 | |
| 8372 | SmallVector<Expr *, 4> ToPlacementArgs(E->getNumPlacementArgs()); |
| 8373 | if (Error Err = |
| 8374 | ImportContainerChecked(InContainer: E->placement_arguments(), OutContainer&: ToPlacementArgs)) |
| 8375 | return std::move(Err); |
| 8376 | |
| 8377 | return CXXNewExpr::Create( |
| 8378 | Ctx: Importer.getToContext(), IsGlobalNew: E->isGlobalNew(), OperatorNew: ToOperatorNew, |
| 8379 | OperatorDelete: ToOperatorDelete, IAP: E->implicitAllocationParameters(), |
| 8380 | UsualArrayDeleteWantsSize: E->doesUsualArrayDeleteWantSize(), PlacementArgs: ToPlacementArgs, TypeIdParens: ToTypeIdParens, |
| 8381 | ArraySize: ToArraySize, InitializationStyle: E->getInitializationStyle(), Initializer: ToInitializer, Ty: ToType, |
| 8382 | AllocatedTypeInfo: ToAllocatedTypeSourceInfo, Range: ToSourceRange, DirectInitRange: ToDirectInitRange); |
| 8383 | } |
| 8384 | |
| 8385 | ExpectedStmt ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 8386 | Error Err = Error::success(); |
| 8387 | auto ToType = importChecked(Err, E->getType()); |
| 8388 | auto ToOperatorDelete = importChecked(Err, From: E->getOperatorDelete()); |
| 8389 | auto ToArgument = importChecked(Err, From: E->getArgument()); |
| 8390 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
| 8391 | if (Err) |
| 8392 | return std::move(Err); |
| 8393 | |
| 8394 | return new (Importer.getToContext()) CXXDeleteExpr( |
| 8395 | ToType, E->isGlobalDelete(), E->isArrayForm(), E->isArrayFormAsWritten(), |
| 8396 | E->doesUsualArrayDeleteWantSize(), ToOperatorDelete, ToArgument, |
| 8397 | ToBeginLoc); |
| 8398 | } |
| 8399 | |
| 8400 | ExpectedStmt ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 8401 | Error Err = Error::success(); |
| 8402 | auto ToType = importChecked(Err, E->getType()); |
| 8403 | auto ToLocation = importChecked(Err, From: E->getLocation()); |
| 8404 | auto ToConstructor = importChecked(Err, From: E->getConstructor()); |
| 8405 | auto ToParenOrBraceRange = importChecked(Err, From: E->getParenOrBraceRange()); |
| 8406 | if (Err) |
| 8407 | return std::move(Err); |
| 8408 | |
| 8409 | SmallVector<Expr *, 6> ToArgs(E->getNumArgs()); |
| 8410 | if (Error Err = ImportContainerChecked(InContainer: E->arguments(), OutContainer&: ToArgs)) |
| 8411 | return std::move(Err); |
| 8412 | |
| 8413 | CXXConstructExpr *ToE = CXXConstructExpr::Create( |
| 8414 | Ctx: Importer.getToContext(), Ty: ToType, Loc: ToLocation, Ctor: ToConstructor, |
| 8415 | Elidable: E->isElidable(), Args: ToArgs, HadMultipleCandidates: E->hadMultipleCandidates(), |
| 8416 | ListInitialization: E->isListInitialization(), StdInitListInitialization: E->isStdInitListInitialization(), |
| 8417 | ZeroInitialization: E->requiresZeroInitialization(), ConstructKind: E->getConstructionKind(), |
| 8418 | ParenOrBraceRange: ToParenOrBraceRange); |
| 8419 | ToE->setIsImmediateEscalating(E->isImmediateEscalating()); |
| 8420 | return ToE; |
| 8421 | } |
| 8422 | |
| 8423 | ExpectedStmt ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *E) { |
| 8424 | ExpectedExpr ToSubExprOrErr = import(E->getSubExpr()); |
| 8425 | if (!ToSubExprOrErr) |
| 8426 | return ToSubExprOrErr.takeError(); |
| 8427 | |
| 8428 | SmallVector<ExprWithCleanups::CleanupObject, 8> ToObjects(E->getNumObjects()); |
| 8429 | if (Error Err = ImportContainerChecked(InContainer: E->getObjects(), OutContainer&: ToObjects)) |
| 8430 | return std::move(Err); |
| 8431 | |
| 8432 | return ExprWithCleanups::Create( |
| 8433 | C: Importer.getToContext(), subexpr: *ToSubExprOrErr, CleanupsHaveSideEffects: E->cleanupsHaveSideEffects(), |
| 8434 | objects: ToObjects); |
| 8435 | } |
| 8436 | |
| 8437 | ExpectedStmt ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) { |
| 8438 | Error Err = Error::success(); |
| 8439 | auto ToCallee = importChecked(Err, E->getCallee()); |
| 8440 | auto ToType = importChecked(Err, E->getType()); |
| 8441 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); |
| 8442 | if (Err) |
| 8443 | return std::move(Err); |
| 8444 | |
| 8445 | SmallVector<Expr *, 4> ToArgs(E->getNumArgs()); |
| 8446 | if (Error Err = ImportContainerChecked(E->arguments(), ToArgs)) |
| 8447 | return std::move(Err); |
| 8448 | |
| 8449 | return CXXMemberCallExpr::Create(Ctx: Importer.getToContext(), Fn: ToCallee, Args: ToArgs, |
| 8450 | Ty: ToType, VK: E->getValueKind(), RP: ToRParenLoc, |
| 8451 | FPFeatures: E->getFPFeatures()); |
| 8452 | } |
| 8453 | |
| 8454 | ExpectedStmt ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) { |
| 8455 | ExpectedType ToTypeOrErr = import(E->getType()); |
| 8456 | if (!ToTypeOrErr) |
| 8457 | return ToTypeOrErr.takeError(); |
| 8458 | |
| 8459 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
| 8460 | if (!ToLocationOrErr) |
| 8461 | return ToLocationOrErr.takeError(); |
| 8462 | |
| 8463 | return CXXThisExpr::Create(Ctx: Importer.getToContext(), L: *ToLocationOrErr, |
| 8464 | Ty: *ToTypeOrErr, IsImplicit: E->isImplicit()); |
| 8465 | } |
| 8466 | |
| 8467 | ExpectedStmt ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) { |
| 8468 | ExpectedType ToTypeOrErr = import(E->getType()); |
| 8469 | if (!ToTypeOrErr) |
| 8470 | return ToTypeOrErr.takeError(); |
| 8471 | |
| 8472 | ExpectedSLoc ToLocationOrErr = import(From: E->getLocation()); |
| 8473 | if (!ToLocationOrErr) |
| 8474 | return ToLocationOrErr.takeError(); |
| 8475 | |
| 8476 | return CXXBoolLiteralExpr::Create(C: Importer.getToContext(), Val: E->getValue(), |
| 8477 | Ty: *ToTypeOrErr, Loc: *ToLocationOrErr); |
| 8478 | } |
| 8479 | |
| 8480 | ExpectedStmt ASTNodeImporter::VisitMemberExpr(MemberExpr *E) { |
| 8481 | Error Err = Error::success(); |
| 8482 | auto ToBase = importChecked(Err, From: E->getBase()); |
| 8483 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
| 8484 | auto ToQualifierLoc = importChecked(Err, From: E->getQualifierLoc()); |
| 8485 | auto ToTemplateKeywordLoc = importChecked(Err, From: E->getTemplateKeywordLoc()); |
| 8486 | auto ToMemberDecl = importChecked(Err, From: E->getMemberDecl()); |
| 8487 | auto ToType = importChecked(Err, E->getType()); |
| 8488 | auto ToDecl = importChecked(Err, From: E->getFoundDecl().getDecl()); |
| 8489 | auto ToName = importChecked(Err, From: E->getMemberNameInfo().getName()); |
| 8490 | auto ToLoc = importChecked(Err, From: E->getMemberNameInfo().getLoc()); |
| 8491 | if (Err) |
| 8492 | return std::move(Err); |
| 8493 | |
| 8494 | DeclAccessPair ToFoundDecl = |
| 8495 | DeclAccessPair::make(D: ToDecl, AS: E->getFoundDecl().getAccess()); |
| 8496 | |
| 8497 | DeclarationNameInfo ToMemberNameInfo(ToName, ToLoc); |
| 8498 | |
| 8499 | TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr; |
| 8500 | if (E->hasExplicitTemplateArgs()) { |
| 8501 | if (Error Err = |
| 8502 | ImportTemplateArgumentListInfo(FromLAngleLoc: E->getLAngleLoc(), FromRAngleLoc: E->getRAngleLoc(), |
| 8503 | Container: E->template_arguments(), Result&: ToTAInfo)) |
| 8504 | return std::move(Err); |
| 8505 | ResInfo = &ToTAInfo; |
| 8506 | } |
| 8507 | |
| 8508 | return MemberExpr::Create(C: Importer.getToContext(), Base: ToBase, IsArrow: E->isArrow(), |
| 8509 | OperatorLoc: ToOperatorLoc, QualifierLoc: ToQualifierLoc, TemplateKWLoc: ToTemplateKeywordLoc, |
| 8510 | MemberDecl: ToMemberDecl, FoundDecl: ToFoundDecl, MemberNameInfo: ToMemberNameInfo, |
| 8511 | TemplateArgs: ResInfo, T: ToType, VK: E->getValueKind(), |
| 8512 | OK: E->getObjectKind(), NOUR: E->isNonOdrUse()); |
| 8513 | } |
| 8514 | |
| 8515 | ExpectedStmt |
| 8516 | ASTNodeImporter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { |
| 8517 | Error Err = Error::success(); |
| 8518 | auto ToBase = importChecked(Err, From: E->getBase()); |
| 8519 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
| 8520 | auto ToQualifierLoc = importChecked(Err, From: E->getQualifierLoc()); |
| 8521 | auto ToScopeTypeInfo = importChecked(Err, From: E->getScopeTypeInfo()); |
| 8522 | auto ToColonColonLoc = importChecked(Err, From: E->getColonColonLoc()); |
| 8523 | auto ToTildeLoc = importChecked(Err, From: E->getTildeLoc()); |
| 8524 | if (Err) |
| 8525 | return std::move(Err); |
| 8526 | |
| 8527 | PseudoDestructorTypeStorage Storage; |
| 8528 | if (const IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) { |
| 8529 | const IdentifierInfo *ToII = Importer.Import(FromId: FromII); |
| 8530 | ExpectedSLoc ToDestroyedTypeLocOrErr = import(From: E->getDestroyedTypeLoc()); |
| 8531 | if (!ToDestroyedTypeLocOrErr) |
| 8532 | return ToDestroyedTypeLocOrErr.takeError(); |
| 8533 | Storage = PseudoDestructorTypeStorage(ToII, *ToDestroyedTypeLocOrErr); |
| 8534 | } else { |
| 8535 | if (auto ToTIOrErr = import(From: E->getDestroyedTypeInfo())) |
| 8536 | Storage = PseudoDestructorTypeStorage(*ToTIOrErr); |
| 8537 | else |
| 8538 | return ToTIOrErr.takeError(); |
| 8539 | } |
| 8540 | |
| 8541 | return new (Importer.getToContext()) CXXPseudoDestructorExpr( |
| 8542 | Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc, |
| 8543 | ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc, ToTildeLoc, Storage); |
| 8544 | } |
| 8545 | |
| 8546 | ExpectedStmt ASTNodeImporter::VisitCXXDependentScopeMemberExpr( |
| 8547 | CXXDependentScopeMemberExpr *E) { |
| 8548 | Error Err = Error::success(); |
| 8549 | auto ToType = importChecked(Err, E->getType()); |
| 8550 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
| 8551 | auto ToQualifierLoc = importChecked(Err, From: E->getQualifierLoc()); |
| 8552 | auto ToTemplateKeywordLoc = importChecked(Err, From: E->getTemplateKeywordLoc()); |
| 8553 | auto ToFirstQualifierFoundInScope = |
| 8554 | importChecked(Err, From: E->getFirstQualifierFoundInScope()); |
| 8555 | if (Err) |
| 8556 | return std::move(Err); |
| 8557 | |
| 8558 | Expr *ToBase = nullptr; |
| 8559 | if (!E->isImplicitAccess()) { |
| 8560 | if (ExpectedExpr ToBaseOrErr = import(From: E->getBase())) |
| 8561 | ToBase = *ToBaseOrErr; |
| 8562 | else |
| 8563 | return ToBaseOrErr.takeError(); |
| 8564 | } |
| 8565 | |
| 8566 | TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr; |
| 8567 | |
| 8568 | if (E->hasExplicitTemplateArgs()) { |
| 8569 | if (Error Err = |
| 8570 | ImportTemplateArgumentListInfo(FromLAngleLoc: E->getLAngleLoc(), FromRAngleLoc: E->getRAngleLoc(), |
| 8571 | Container: E->template_arguments(), Result&: ToTAInfo)) |
| 8572 | return std::move(Err); |
| 8573 | ResInfo = &ToTAInfo; |
| 8574 | } |
| 8575 | auto ToMember = importChecked(Err, From: E->getMember()); |
| 8576 | auto ToMemberLoc = importChecked(Err, From: E->getMemberLoc()); |
| 8577 | if (Err) |
| 8578 | return std::move(Err); |
| 8579 | DeclarationNameInfo ToMemberNameInfo(ToMember, ToMemberLoc); |
| 8580 | |
| 8581 | // Import additional name location/type info. |
| 8582 | if (Error Err = |
| 8583 | ImportDeclarationNameLoc(From: E->getMemberNameInfo(), To&: ToMemberNameInfo)) |
| 8584 | return std::move(Err); |
| 8585 | |
| 8586 | return CXXDependentScopeMemberExpr::Create( |
| 8587 | Ctx: Importer.getToContext(), Base: ToBase, BaseType: ToType, IsArrow: E->isArrow(), OperatorLoc: ToOperatorLoc, |
| 8588 | QualifierLoc: ToQualifierLoc, TemplateKWLoc: ToTemplateKeywordLoc, FirstQualifierFoundInScope: ToFirstQualifierFoundInScope, |
| 8589 | MemberNameInfo: ToMemberNameInfo, TemplateArgs: ResInfo); |
| 8590 | } |
| 8591 | |
| 8592 | ExpectedStmt |
| 8593 | ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) { |
| 8594 | Error Err = Error::success(); |
| 8595 | auto ToQualifierLoc = importChecked(Err, From: E->getQualifierLoc()); |
| 8596 | auto ToTemplateKeywordLoc = importChecked(Err, From: E->getTemplateKeywordLoc()); |
| 8597 | auto ToDeclName = importChecked(Err, From: E->getDeclName()); |
| 8598 | auto ToNameLoc = importChecked(Err, From: E->getNameInfo().getLoc()); |
| 8599 | auto ToLAngleLoc = importChecked(Err, From: E->getLAngleLoc()); |
| 8600 | auto ToRAngleLoc = importChecked(Err, From: E->getRAngleLoc()); |
| 8601 | if (Err) |
| 8602 | return std::move(Err); |
| 8603 | |
| 8604 | DeclarationNameInfo ToNameInfo(ToDeclName, ToNameLoc); |
| 8605 | if (Error Err = ImportDeclarationNameLoc(From: E->getNameInfo(), To&: ToNameInfo)) |
| 8606 | return std::move(Err); |
| 8607 | |
| 8608 | TemplateArgumentListInfo ToTAInfo(ToLAngleLoc, ToRAngleLoc); |
| 8609 | TemplateArgumentListInfo *ResInfo = nullptr; |
| 8610 | if (E->hasExplicitTemplateArgs()) { |
| 8611 | if (Error Err = |
| 8612 | ImportTemplateArgumentListInfo(Container: E->template_arguments(), ToTAInfo)) |
| 8613 | return std::move(Err); |
| 8614 | ResInfo = &ToTAInfo; |
| 8615 | } |
| 8616 | |
| 8617 | return DependentScopeDeclRefExpr::Create( |
| 8618 | Context: Importer.getToContext(), QualifierLoc: ToQualifierLoc, TemplateKWLoc: ToTemplateKeywordLoc, |
| 8619 | NameInfo: ToNameInfo, TemplateArgs: ResInfo); |
| 8620 | } |
| 8621 | |
| 8622 | ExpectedStmt ASTNodeImporter::VisitCXXUnresolvedConstructExpr( |
| 8623 | CXXUnresolvedConstructExpr *E) { |
| 8624 | Error Err = Error::success(); |
| 8625 | auto ToLParenLoc = importChecked(Err, From: E->getLParenLoc()); |
| 8626 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 8627 | auto ToType = importChecked(Err, E->getType()); |
| 8628 | auto ToTypeSourceInfo = importChecked(Err, From: E->getTypeSourceInfo()); |
| 8629 | if (Err) |
| 8630 | return std::move(Err); |
| 8631 | |
| 8632 | SmallVector<Expr *, 8> ToArgs(E->getNumArgs()); |
| 8633 | if (Error Err = |
| 8634 | ImportArrayChecked(Ibegin: E->arg_begin(), Iend: E->arg_end(), Obegin: ToArgs.begin())) |
| 8635 | return std::move(Err); |
| 8636 | |
| 8637 | return CXXUnresolvedConstructExpr::Create( |
| 8638 | Context: Importer.getToContext(), T: ToType, TSI: ToTypeSourceInfo, LParenLoc: ToLParenLoc, |
| 8639 | Args: llvm::ArrayRef(ToArgs), RParenLoc: ToRParenLoc, IsListInit: E->isListInitialization()); |
| 8640 | } |
| 8641 | |
| 8642 | ExpectedStmt |
| 8643 | ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) { |
| 8644 | Expected<CXXRecordDecl *> ToNamingClassOrErr = import(From: E->getNamingClass()); |
| 8645 | if (!ToNamingClassOrErr) |
| 8646 | return ToNamingClassOrErr.takeError(); |
| 8647 | |
| 8648 | auto ToQualifierLocOrErr = import(E->getQualifierLoc()); |
| 8649 | if (!ToQualifierLocOrErr) |
| 8650 | return ToQualifierLocOrErr.takeError(); |
| 8651 | |
| 8652 | Error Err = Error::success(); |
| 8653 | auto ToName = importChecked(Err, E->getName()); |
| 8654 | auto ToNameLoc = importChecked(Err, E->getNameLoc()); |
| 8655 | if (Err) |
| 8656 | return std::move(Err); |
| 8657 | DeclarationNameInfo ToNameInfo(ToName, ToNameLoc); |
| 8658 | |
| 8659 | // Import additional name location/type info. |
| 8660 | if (Error Err = ImportDeclarationNameLoc(From: E->getNameInfo(), To&: ToNameInfo)) |
| 8661 | return std::move(Err); |
| 8662 | |
| 8663 | UnresolvedSet<8> ToDecls; |
| 8664 | for (auto *D : E->decls()) |
| 8665 | if (auto ToDOrErr = import(D)) |
| 8666 | ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr)); |
| 8667 | else |
| 8668 | return ToDOrErr.takeError(); |
| 8669 | |
| 8670 | if (E->hasExplicitTemplateArgs()) { |
| 8671 | TemplateArgumentListInfo ToTAInfo; |
| 8672 | if (Error Err = ImportTemplateArgumentListInfo( |
| 8673 | E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(), |
| 8674 | ToTAInfo)) |
| 8675 | return std::move(Err); |
| 8676 | |
| 8677 | ExpectedSLoc ToTemplateKeywordLocOrErr = import(E->getTemplateKeywordLoc()); |
| 8678 | if (!ToTemplateKeywordLocOrErr) |
| 8679 | return ToTemplateKeywordLocOrErr.takeError(); |
| 8680 | |
| 8681 | const bool KnownDependent = |
| 8682 | (E->getDependence() & ExprDependence::TypeValue) == |
| 8683 | ExprDependence::TypeValue; |
| 8684 | return UnresolvedLookupExpr::Create( |
| 8685 | Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr, |
| 8686 | *ToTemplateKeywordLocOrErr, ToNameInfo, E->requiresADL(), &ToTAInfo, |
| 8687 | ToDecls.begin(), ToDecls.end(), KnownDependent, |
| 8688 | /*KnownInstantiationDependent=*/E->isInstantiationDependent()); |
| 8689 | } |
| 8690 | |
| 8691 | return UnresolvedLookupExpr::Create( |
| 8692 | Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr, |
| 8693 | ToNameInfo, E->requiresADL(), ToDecls.begin(), ToDecls.end(), |
| 8694 | /*KnownDependent=*/E->isTypeDependent(), |
| 8695 | /*KnownInstantiationDependent=*/E->isInstantiationDependent()); |
| 8696 | } |
| 8697 | |
| 8698 | ExpectedStmt |
| 8699 | ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) { |
| 8700 | Error Err = Error::success(); |
| 8701 | auto ToType = importChecked(Err, E->getType()); |
| 8702 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
| 8703 | auto ToQualifierLoc = importChecked(Err, E->getQualifierLoc()); |
| 8704 | auto ToTemplateKeywordLoc = importChecked(Err, E->getTemplateKeywordLoc()); |
| 8705 | auto ToName = importChecked(Err, E->getName()); |
| 8706 | auto ToNameLoc = importChecked(Err, E->getNameLoc()); |
| 8707 | if (Err) |
| 8708 | return std::move(Err); |
| 8709 | |
| 8710 | DeclarationNameInfo ToNameInfo(ToName, ToNameLoc); |
| 8711 | // Import additional name location/type info. |
| 8712 | if (Error Err = ImportDeclarationNameLoc(From: E->getNameInfo(), To&: ToNameInfo)) |
| 8713 | return std::move(Err); |
| 8714 | |
| 8715 | UnresolvedSet<8> ToDecls; |
| 8716 | for (Decl *D : E->decls()) |
| 8717 | if (auto ToDOrErr = import(D)) |
| 8718 | ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr)); |
| 8719 | else |
| 8720 | return ToDOrErr.takeError(); |
| 8721 | |
| 8722 | TemplateArgumentListInfo ToTAInfo; |
| 8723 | TemplateArgumentListInfo *ResInfo = nullptr; |
| 8724 | if (E->hasExplicitTemplateArgs()) { |
| 8725 | TemplateArgumentListInfo FromTAInfo; |
| 8726 | E->copyTemplateArgumentsInto(FromTAInfo); |
| 8727 | if (Error Err = ImportTemplateArgumentListInfo(Container: FromTAInfo, ToTAInfo)) |
| 8728 | return std::move(Err); |
| 8729 | ResInfo = &ToTAInfo; |
| 8730 | } |
| 8731 | |
| 8732 | Expr *ToBase = nullptr; |
| 8733 | if (!E->isImplicitAccess()) { |
| 8734 | if (ExpectedExpr ToBaseOrErr = import(From: E->getBase())) |
| 8735 | ToBase = *ToBaseOrErr; |
| 8736 | else |
| 8737 | return ToBaseOrErr.takeError(); |
| 8738 | } |
| 8739 | |
| 8740 | return UnresolvedMemberExpr::Create( |
| 8741 | Context: Importer.getToContext(), HasUnresolvedUsing: E->hasUnresolvedUsing(), Base: ToBase, BaseType: ToType, |
| 8742 | IsArrow: E->isArrow(), OperatorLoc: ToOperatorLoc, QualifierLoc: ToQualifierLoc, TemplateKWLoc: ToTemplateKeywordLoc, |
| 8743 | MemberNameInfo: ToNameInfo, TemplateArgs: ResInfo, Begin: ToDecls.begin(), End: ToDecls.end()); |
| 8744 | } |
| 8745 | |
| 8746 | ExpectedStmt ASTNodeImporter::VisitCallExpr(CallExpr *E) { |
| 8747 | Error Err = Error::success(); |
| 8748 | auto ToCallee = importChecked(Err, From: E->getCallee()); |
| 8749 | auto ToType = importChecked(Err, E->getType()); |
| 8750 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 8751 | if (Err) |
| 8752 | return std::move(Err); |
| 8753 | |
| 8754 | unsigned NumArgs = E->getNumArgs(); |
| 8755 | llvm::SmallVector<Expr *, 2> ToArgs(NumArgs); |
| 8756 | if (Error Err = ImportContainerChecked(InContainer: E->arguments(), OutContainer&: ToArgs)) |
| 8757 | return std::move(Err); |
| 8758 | |
| 8759 | if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(Val: E)) { |
| 8760 | return CXXOperatorCallExpr::Create( |
| 8761 | Ctx: Importer.getToContext(), OpKind: OCE->getOperator(), Fn: ToCallee, Args: ToArgs, Ty: ToType, |
| 8762 | VK: OCE->getValueKind(), OperatorLoc: ToRParenLoc, FPFeatures: OCE->getFPFeatures(), |
| 8763 | UsesADL: OCE->getADLCallKind()); |
| 8764 | } |
| 8765 | |
| 8766 | return CallExpr::Create(Ctx: Importer.getToContext(), Fn: ToCallee, Args: ToArgs, Ty: ToType, |
| 8767 | VK: E->getValueKind(), RParenLoc: ToRParenLoc, FPFeatures: E->getFPFeatures(), |
| 8768 | /*MinNumArgs=*/0, UsesADL: E->getADLCallKind()); |
| 8769 | } |
| 8770 | |
| 8771 | ExpectedStmt ASTNodeImporter::VisitLambdaExpr(LambdaExpr *E) { |
| 8772 | CXXRecordDecl *FromClass = E->getLambdaClass(); |
| 8773 | auto ToClassOrErr = import(From: FromClass); |
| 8774 | if (!ToClassOrErr) |
| 8775 | return ToClassOrErr.takeError(); |
| 8776 | CXXRecordDecl *ToClass = *ToClassOrErr; |
| 8777 | |
| 8778 | auto ToCallOpOrErr = import(From: E->getCallOperator()); |
| 8779 | if (!ToCallOpOrErr) |
| 8780 | return ToCallOpOrErr.takeError(); |
| 8781 | |
| 8782 | SmallVector<Expr *, 8> ToCaptureInits(E->capture_size()); |
| 8783 | if (Error Err = ImportContainerChecked(InContainer: E->capture_inits(), OutContainer&: ToCaptureInits)) |
| 8784 | return std::move(Err); |
| 8785 | |
| 8786 | Error Err = Error::success(); |
| 8787 | auto ToIntroducerRange = importChecked(Err, From: E->getIntroducerRange()); |
| 8788 | auto ToCaptureDefaultLoc = importChecked(Err, From: E->getCaptureDefaultLoc()); |
| 8789 | auto ToEndLoc = importChecked(Err, From: E->getEndLoc()); |
| 8790 | if (Err) |
| 8791 | return std::move(Err); |
| 8792 | |
| 8793 | return LambdaExpr::Create(C: Importer.getToContext(), Class: ToClass, IntroducerRange: ToIntroducerRange, |
| 8794 | CaptureDefault: E->getCaptureDefault(), CaptureDefaultLoc: ToCaptureDefaultLoc, |
| 8795 | ExplicitParams: E->hasExplicitParameters(), |
| 8796 | ExplicitResultType: E->hasExplicitResultType(), CaptureInits: ToCaptureInits, |
| 8797 | ClosingBrace: ToEndLoc, ContainsUnexpandedParameterPack: E->containsUnexpandedParameterPack()); |
| 8798 | } |
| 8799 | |
| 8800 | |
| 8801 | ExpectedStmt ASTNodeImporter::VisitInitListExpr(InitListExpr *E) { |
| 8802 | Error Err = Error::success(); |
| 8803 | auto ToLBraceLoc = importChecked(Err, From: E->getLBraceLoc()); |
| 8804 | auto ToRBraceLoc = importChecked(Err, From: E->getRBraceLoc()); |
| 8805 | auto ToType = importChecked(Err, E->getType()); |
| 8806 | if (Err) |
| 8807 | return std::move(Err); |
| 8808 | |
| 8809 | SmallVector<Expr *, 4> ToExprs(E->getNumInits()); |
| 8810 | if (Error Err = ImportContainerChecked(InContainer: E->inits(), OutContainer&: ToExprs)) |
| 8811 | return std::move(Err); |
| 8812 | |
| 8813 | ASTContext &ToCtx = Importer.getToContext(); |
| 8814 | InitListExpr *To = new (ToCtx) InitListExpr( |
| 8815 | ToCtx, ToLBraceLoc, ToExprs, ToRBraceLoc); |
| 8816 | To->setType(ToType); |
| 8817 | |
| 8818 | if (E->hasArrayFiller()) { |
| 8819 | if (ExpectedExpr ToFillerOrErr = import(From: E->getArrayFiller())) |
| 8820 | To->setArrayFiller(*ToFillerOrErr); |
| 8821 | else |
| 8822 | return ToFillerOrErr.takeError(); |
| 8823 | } |
| 8824 | |
| 8825 | if (FieldDecl *FromFD = E->getInitializedFieldInUnion()) { |
| 8826 | if (auto ToFDOrErr = import(From: FromFD)) |
| 8827 | To->setInitializedFieldInUnion(*ToFDOrErr); |
| 8828 | else |
| 8829 | return ToFDOrErr.takeError(); |
| 8830 | } |
| 8831 | |
| 8832 | if (InitListExpr *SyntForm = E->getSyntacticForm()) { |
| 8833 | if (auto ToSyntFormOrErr = import(From: SyntForm)) |
| 8834 | To->setSyntacticForm(*ToSyntFormOrErr); |
| 8835 | else |
| 8836 | return ToSyntFormOrErr.takeError(); |
| 8837 | } |
| 8838 | |
| 8839 | // Copy InitListExprBitfields, which are not handled in the ctor of |
| 8840 | // InitListExpr. |
| 8841 | To->sawArrayRangeDesignator(ARD: E->hadArrayRangeDesignator()); |
| 8842 | |
| 8843 | return To; |
| 8844 | } |
| 8845 | |
| 8846 | ExpectedStmt ASTNodeImporter::VisitCXXStdInitializerListExpr( |
| 8847 | CXXStdInitializerListExpr *E) { |
| 8848 | ExpectedType ToTypeOrErr = import(E->getType()); |
| 8849 | if (!ToTypeOrErr) |
| 8850 | return ToTypeOrErr.takeError(); |
| 8851 | |
| 8852 | ExpectedExpr ToSubExprOrErr = import(From: E->getSubExpr()); |
| 8853 | if (!ToSubExprOrErr) |
| 8854 | return ToSubExprOrErr.takeError(); |
| 8855 | |
| 8856 | return new (Importer.getToContext()) CXXStdInitializerListExpr( |
| 8857 | *ToTypeOrErr, *ToSubExprOrErr); |
| 8858 | } |
| 8859 | |
| 8860 | ExpectedStmt ASTNodeImporter::VisitCXXInheritedCtorInitExpr( |
| 8861 | CXXInheritedCtorInitExpr *E) { |
| 8862 | Error Err = Error::success(); |
| 8863 | auto ToLocation = importChecked(Err, From: E->getLocation()); |
| 8864 | auto ToType = importChecked(Err, E->getType()); |
| 8865 | auto ToConstructor = importChecked(Err, From: E->getConstructor()); |
| 8866 | if (Err) |
| 8867 | return std::move(Err); |
| 8868 | |
| 8869 | return new (Importer.getToContext()) CXXInheritedCtorInitExpr( |
| 8870 | ToLocation, ToType, ToConstructor, E->constructsVBase(), |
| 8871 | E->inheritedFromVBase()); |
| 8872 | } |
| 8873 | |
| 8874 | ExpectedStmt ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) { |
| 8875 | Error Err = Error::success(); |
| 8876 | auto ToType = importChecked(Err, E->getType()); |
| 8877 | auto ToCommonExpr = importChecked(Err, From: E->getCommonExpr()); |
| 8878 | auto ToSubExpr = importChecked(Err, From: E->getSubExpr()); |
| 8879 | if (Err) |
| 8880 | return std::move(Err); |
| 8881 | |
| 8882 | return new (Importer.getToContext()) ArrayInitLoopExpr( |
| 8883 | ToType, ToCommonExpr, ToSubExpr); |
| 8884 | } |
| 8885 | |
| 8886 | ExpectedStmt ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) { |
| 8887 | ExpectedType ToTypeOrErr = import(E->getType()); |
| 8888 | if (!ToTypeOrErr) |
| 8889 | return ToTypeOrErr.takeError(); |
| 8890 | return new (Importer.getToContext()) ArrayInitIndexExpr(*ToTypeOrErr); |
| 8891 | } |
| 8892 | |
| 8893 | ExpectedStmt ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) { |
| 8894 | ExpectedSLoc ToBeginLocOrErr = import(From: E->getBeginLoc()); |
| 8895 | if (!ToBeginLocOrErr) |
| 8896 | return ToBeginLocOrErr.takeError(); |
| 8897 | |
| 8898 | auto ToFieldOrErr = import(From: E->getField()); |
| 8899 | if (!ToFieldOrErr) |
| 8900 | return ToFieldOrErr.takeError(); |
| 8901 | |
| 8902 | auto UsedContextOrErr = Importer.ImportContext(FromDC: E->getUsedContext()); |
| 8903 | if (!UsedContextOrErr) |
| 8904 | return UsedContextOrErr.takeError(); |
| 8905 | |
| 8906 | FieldDecl *ToField = *ToFieldOrErr; |
| 8907 | assert(ToField->hasInClassInitializer() && |
| 8908 | "Field should have in-class initializer if there is a default init " |
| 8909 | "expression that uses it." ); |
| 8910 | if (!ToField->getInClassInitializer()) { |
| 8911 | // The in-class initializer may be not yet set in "To" AST even if the |
| 8912 | // field is already there. This must be set here to make construction of |
| 8913 | // CXXDefaultInitExpr work. |
| 8914 | auto ToInClassInitializerOrErr = |
| 8915 | import(From: E->getField()->getInClassInitializer()); |
| 8916 | if (!ToInClassInitializerOrErr) |
| 8917 | return ToInClassInitializerOrErr.takeError(); |
| 8918 | ToField->setInClassInitializer(*ToInClassInitializerOrErr); |
| 8919 | } |
| 8920 | |
| 8921 | Expr *RewrittenInit = nullptr; |
| 8922 | if (E->hasRewrittenInit()) { |
| 8923 | ExpectedExpr ExprOrErr = import(From: E->getRewrittenExpr()); |
| 8924 | if (!ExprOrErr) |
| 8925 | return ExprOrErr.takeError(); |
| 8926 | RewrittenInit = ExprOrErr.get(); |
| 8927 | } |
| 8928 | |
| 8929 | return CXXDefaultInitExpr::Create(Ctx: Importer.getToContext(), Loc: *ToBeginLocOrErr, |
| 8930 | Field: ToField, UsedContext: *UsedContextOrErr, RewrittenInitExpr: RewrittenInit); |
| 8931 | } |
| 8932 | |
| 8933 | ExpectedStmt ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) { |
| 8934 | Error Err = Error::success(); |
| 8935 | auto ToType = importChecked(Err, E->getType()); |
| 8936 | auto ToSubExpr = importChecked(Err, E->getSubExpr()); |
| 8937 | auto ToTypeInfoAsWritten = importChecked(Err, E->getTypeInfoAsWritten()); |
| 8938 | auto ToOperatorLoc = importChecked(Err, From: E->getOperatorLoc()); |
| 8939 | auto ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 8940 | auto ToAngleBrackets = importChecked(Err, From: E->getAngleBrackets()); |
| 8941 | if (Err) |
| 8942 | return std::move(Err); |
| 8943 | |
| 8944 | ExprValueKind VK = E->getValueKind(); |
| 8945 | CastKind CK = E->getCastKind(); |
| 8946 | auto ToBasePathOrErr = ImportCastPath(E); |
| 8947 | if (!ToBasePathOrErr) |
| 8948 | return ToBasePathOrErr.takeError(); |
| 8949 | |
| 8950 | if (auto CCE = dyn_cast<CXXStaticCastExpr>(Val: E)) { |
| 8951 | return CXXStaticCastExpr::Create( |
| 8952 | Context: Importer.getToContext(), T: ToType, VK, K: CK, Op: ToSubExpr, Path: &(*ToBasePathOrErr), |
| 8953 | Written: ToTypeInfoAsWritten, FPO: CCE->getFPFeatures(), L: ToOperatorLoc, RParenLoc: ToRParenLoc, |
| 8954 | AngleBrackets: ToAngleBrackets); |
| 8955 | } else if (isa<CXXDynamicCastExpr>(Val: E)) { |
| 8956 | return CXXDynamicCastExpr::Create( |
| 8957 | Context: Importer.getToContext(), T: ToType, VK, Kind: CK, Op: ToSubExpr, Path: &(*ToBasePathOrErr), |
| 8958 | Written: ToTypeInfoAsWritten, L: ToOperatorLoc, RParenLoc: ToRParenLoc, AngleBrackets: ToAngleBrackets); |
| 8959 | } else if (isa<CXXReinterpretCastExpr>(Val: E)) { |
| 8960 | return CXXReinterpretCastExpr::Create( |
| 8961 | Context: Importer.getToContext(), T: ToType, VK, Kind: CK, Op: ToSubExpr, Path: &(*ToBasePathOrErr), |
| 8962 | WrittenTy: ToTypeInfoAsWritten, L: ToOperatorLoc, RParenLoc: ToRParenLoc, AngleBrackets: ToAngleBrackets); |
| 8963 | } else if (isa<CXXConstCastExpr>(Val: E)) { |
| 8964 | return CXXConstCastExpr::Create( |
| 8965 | Context: Importer.getToContext(), T: ToType, VK, Op: ToSubExpr, WrittenTy: ToTypeInfoAsWritten, |
| 8966 | L: ToOperatorLoc, RParenLoc: ToRParenLoc, AngleBrackets: ToAngleBrackets); |
| 8967 | } else { |
| 8968 | llvm_unreachable("Unknown cast type" ); |
| 8969 | return make_error<ASTImportError>(); |
| 8970 | } |
| 8971 | } |
| 8972 | |
| 8973 | ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr( |
| 8974 | SubstNonTypeTemplateParmExpr *E) { |
| 8975 | Error Err = Error::success(); |
| 8976 | auto ToType = importChecked(Err, E->getType()); |
| 8977 | auto ToNameLoc = importChecked(Err, From: E->getNameLoc()); |
| 8978 | auto ToAssociatedDecl = importChecked(Err, From: E->getAssociatedDecl()); |
| 8979 | auto ToReplacement = importChecked(Err, From: E->getReplacement()); |
| 8980 | if (Err) |
| 8981 | return std::move(Err); |
| 8982 | |
| 8983 | return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr( |
| 8984 | ToType, E->getValueKind(), ToNameLoc, ToReplacement, ToAssociatedDecl, |
| 8985 | E->getIndex(), E->getPackIndex(), E->isReferenceParameter(), |
| 8986 | E->getFinal()); |
| 8987 | } |
| 8988 | |
| 8989 | ExpectedStmt ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) { |
| 8990 | Error Err = Error::success(); |
| 8991 | auto ToType = importChecked(Err, E->getType()); |
| 8992 | auto ToBeginLoc = importChecked(Err, From: E->getBeginLoc()); |
| 8993 | auto ToEndLoc = importChecked(Err, From: E->getEndLoc()); |
| 8994 | if (Err) |
| 8995 | return std::move(Err); |
| 8996 | |
| 8997 | SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs()); |
| 8998 | if (Error Err = ImportContainerChecked(InContainer: E->getArgs(), OutContainer&: ToArgs)) |
| 8999 | return std::move(Err); |
| 9000 | |
| 9001 | if (E->isStoredAsBoolean()) { |
| 9002 | // According to Sema::BuildTypeTrait(), if E is value-dependent, |
| 9003 | // Value is always false. |
| 9004 | bool ToValue = (E->isValueDependent() ? false : E->getBoolValue()); |
| 9005 | return TypeTraitExpr::Create(Importer.getToContext(), ToType, ToBeginLoc, |
| 9006 | E->getTrait(), ToArgs, ToEndLoc, ToValue); |
| 9007 | } |
| 9008 | return TypeTraitExpr::Create(Importer.getToContext(), ToType, ToBeginLoc, |
| 9009 | E->getTrait(), ToArgs, ToEndLoc, |
| 9010 | E->getAPValue()); |
| 9011 | } |
| 9012 | |
| 9013 | ExpectedStmt ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) { |
| 9014 | ExpectedType ToTypeOrErr = import(E->getType()); |
| 9015 | if (!ToTypeOrErr) |
| 9016 | return ToTypeOrErr.takeError(); |
| 9017 | |
| 9018 | auto ToSourceRangeOrErr = import(From: E->getSourceRange()); |
| 9019 | if (!ToSourceRangeOrErr) |
| 9020 | return ToSourceRangeOrErr.takeError(); |
| 9021 | |
| 9022 | if (E->isTypeOperand()) { |
| 9023 | if (auto ToTSIOrErr = import(From: E->getTypeOperandSourceInfo())) |
| 9024 | return new (Importer.getToContext()) CXXTypeidExpr( |
| 9025 | *ToTypeOrErr, *ToTSIOrErr, *ToSourceRangeOrErr); |
| 9026 | else |
| 9027 | return ToTSIOrErr.takeError(); |
| 9028 | } |
| 9029 | |
| 9030 | ExpectedExpr ToExprOperandOrErr = import(From: E->getExprOperand()); |
| 9031 | if (!ToExprOperandOrErr) |
| 9032 | return ToExprOperandOrErr.takeError(); |
| 9033 | |
| 9034 | return new (Importer.getToContext()) CXXTypeidExpr( |
| 9035 | *ToTypeOrErr, *ToExprOperandOrErr, *ToSourceRangeOrErr); |
| 9036 | } |
| 9037 | |
| 9038 | ExpectedStmt ASTNodeImporter::VisitCXXFoldExpr(CXXFoldExpr *E) { |
| 9039 | Error Err = Error::success(); |
| 9040 | |
| 9041 | QualType ToType = importChecked(Err, E->getType()); |
| 9042 | UnresolvedLookupExpr *ToCallee = importChecked(Err, From: E->getCallee()); |
| 9043 | SourceLocation ToLParenLoc = importChecked(Err, From: E->getLParenLoc()); |
| 9044 | Expr *ToLHS = importChecked(Err, From: E->getLHS()); |
| 9045 | SourceLocation ToEllipsisLoc = importChecked(Err, From: E->getEllipsisLoc()); |
| 9046 | Expr *ToRHS = importChecked(Err, From: E->getRHS()); |
| 9047 | SourceLocation ToRParenLoc = importChecked(Err, From: E->getRParenLoc()); |
| 9048 | |
| 9049 | if (Err) |
| 9050 | return std::move(Err); |
| 9051 | |
| 9052 | return new (Importer.getToContext()) |
| 9053 | CXXFoldExpr(ToType, ToCallee, ToLParenLoc, ToLHS, E->getOperator(), |
| 9054 | ToEllipsisLoc, ToRHS, ToRParenLoc, E->getNumExpansions()); |
| 9055 | } |
| 9056 | |
| 9057 | Error ASTNodeImporter::ImportOverriddenMethods(CXXMethodDecl *ToMethod, |
| 9058 | CXXMethodDecl *FromMethod) { |
| 9059 | Error ImportErrors = Error::success(); |
| 9060 | for (auto *FromOverriddenMethod : FromMethod->overridden_methods()) { |
| 9061 | if (auto ImportedOrErr = import(From: FromOverriddenMethod)) |
| 9062 | ToMethod->getCanonicalDecl()->addOverriddenMethod(MD: cast<CXXMethodDecl>( |
| 9063 | Val: (*ImportedOrErr)->getCanonicalDecl())); |
| 9064 | else |
| 9065 | ImportErrors = |
| 9066 | joinErrors(E1: std::move(ImportErrors), E2: ImportedOrErr.takeError()); |
| 9067 | } |
| 9068 | return ImportErrors; |
| 9069 | } |
| 9070 | |
| 9071 | ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager, |
| 9072 | ASTContext &FromContext, FileManager &FromFileManager, |
| 9073 | bool MinimalImport, |
| 9074 | std::shared_ptr<ASTImporterSharedState> SharedState) |
| 9075 | : SharedState(SharedState), ToContext(ToContext), FromContext(FromContext), |
| 9076 | ToFileManager(ToFileManager), FromFileManager(FromFileManager), |
| 9077 | Minimal(MinimalImport), ODRHandling(ODRHandlingType::Conservative) { |
| 9078 | |
| 9079 | // Create a default state without the lookup table: LLDB case. |
| 9080 | if (!SharedState) { |
| 9081 | this->SharedState = std::make_shared<ASTImporterSharedState>(); |
| 9082 | } |
| 9083 | |
| 9084 | ImportedDecls[FromContext.getTranslationUnitDecl()] = |
| 9085 | ToContext.getTranslationUnitDecl(); |
| 9086 | } |
| 9087 | |
| 9088 | ASTImporter::~ASTImporter() = default; |
| 9089 | |
| 9090 | UnsignedOrNone ASTImporter::getFieldIndex(Decl *F) { |
| 9091 | assert(F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) && |
| 9092 | "Try to get field index for non-field." ); |
| 9093 | |
| 9094 | auto *Owner = dyn_cast<RecordDecl>(Val: F->getDeclContext()); |
| 9095 | if (!Owner) |
| 9096 | return std::nullopt; |
| 9097 | |
| 9098 | unsigned Index = 0; |
| 9099 | for (const auto *D : Owner->decls()) { |
| 9100 | if (D == F) |
| 9101 | return Index; |
| 9102 | |
| 9103 | if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D)) |
| 9104 | ++Index; |
| 9105 | } |
| 9106 | |
| 9107 | llvm_unreachable("Field was not found in its parent context." ); |
| 9108 | |
| 9109 | return std::nullopt; |
| 9110 | } |
| 9111 | |
| 9112 | ASTImporter::FoundDeclsTy |
| 9113 | ASTImporter::findDeclsInToCtx(DeclContext *DC, DeclarationName Name) { |
| 9114 | // We search in the redecl context because of transparent contexts. |
| 9115 | // E.g. a simple C language enum is a transparent context: |
| 9116 | // enum E { A, B }; |
| 9117 | // Now if we had a global variable in the TU |
| 9118 | // int A; |
| 9119 | // then the enum constant 'A' and the variable 'A' violates ODR. |
| 9120 | // We can diagnose this only if we search in the redecl context. |
| 9121 | DeclContext *ReDC = DC->getRedeclContext(); |
| 9122 | if (SharedState->getLookupTable()) { |
| 9123 | if (ReDC->isNamespace()) { |
| 9124 | // Namespaces can be reopened. |
| 9125 | // Lookup table does not handle this, we must search here in all linked |
| 9126 | // namespaces. |
| 9127 | FoundDeclsTy Result; |
| 9128 | SmallVector<Decl *, 2> NSChain = |
| 9129 | getCanonicalForwardRedeclChain<NamespaceDecl>( |
| 9130 | dyn_cast<NamespaceDecl>(Val: ReDC)); |
| 9131 | for (auto *D : NSChain) { |
| 9132 | ASTImporterLookupTable::LookupResult LookupResult = |
| 9133 | SharedState->getLookupTable()->lookup(dyn_cast<NamespaceDecl>(D), |
| 9134 | Name); |
| 9135 | Result.append(LookupResult.begin(), LookupResult.end()); |
| 9136 | } |
| 9137 | return Result; |
| 9138 | } else { |
| 9139 | ASTImporterLookupTable::LookupResult LookupResult = |
| 9140 | SharedState->getLookupTable()->lookup(DC: ReDC, Name); |
| 9141 | return FoundDeclsTy(LookupResult.begin(), LookupResult.end()); |
| 9142 | } |
| 9143 | } else { |
| 9144 | DeclContext::lookup_result NoloadLookupResult = ReDC->noload_lookup(Name); |
| 9145 | FoundDeclsTy Result(NoloadLookupResult.begin(), NoloadLookupResult.end()); |
| 9146 | // We must search by the slow case of localUncachedLookup because that is |
| 9147 | // working even if there is no LookupPtr for the DC. We could use |
| 9148 | // DC::buildLookup() to create the LookupPtr, but that would load external |
| 9149 | // decls again, we must avoid that case. |
| 9150 | // Also, even if we had the LookupPtr, we must find Decls which are not |
| 9151 | // in the LookupPtr, so we need the slow case. |
| 9152 | // These cases are handled in ASTImporterLookupTable, but we cannot use |
| 9153 | // that with LLDB since that traverses through the AST which initiates the |
| 9154 | // load of external decls again via DC::decls(). And again, we must avoid |
| 9155 | // loading external decls during the import. |
| 9156 | if (Result.empty()) |
| 9157 | ReDC->localUncachedLookup(Name, Results&: Result); |
| 9158 | return Result; |
| 9159 | } |
| 9160 | } |
| 9161 | |
| 9162 | void ASTImporter::AddToLookupTable(Decl *ToD) { |
| 9163 | SharedState->addDeclToLookup(D: ToD); |
| 9164 | } |
| 9165 | |
| 9166 | Expected<Decl *> ASTImporter::ImportImpl(Decl *FromD) { |
| 9167 | // Import the decl using ASTNodeImporter. |
| 9168 | ASTNodeImporter Importer(*this); |
| 9169 | return Importer.Visit(FromD); |
| 9170 | } |
| 9171 | |
| 9172 | void ASTImporter::RegisterImportedDecl(Decl *FromD, Decl *ToD) { |
| 9173 | MapImported(From: FromD, To: ToD); |
| 9174 | } |
| 9175 | |
| 9176 | llvm::Expected<ExprWithCleanups::CleanupObject> |
| 9177 | ASTImporter::Import(ExprWithCleanups::CleanupObject From) { |
| 9178 | if (auto *CLE = From.dyn_cast<CompoundLiteralExpr *>()) { |
| 9179 | if (Expected<Expr *> R = Import(From: CLE)) |
| 9180 | return ExprWithCleanups::CleanupObject(cast<CompoundLiteralExpr>(Val: *R)); |
| 9181 | } |
| 9182 | |
| 9183 | // FIXME: Handle BlockDecl when we implement importing BlockExpr in |
| 9184 | // ASTNodeImporter. |
| 9185 | return make_error<ASTImportError>(Args: ASTImportError::UnsupportedConstruct); |
| 9186 | } |
| 9187 | |
| 9188 | ExpectedTypePtr ASTImporter::Import(const Type *FromT) { |
| 9189 | if (!FromT) |
| 9190 | return FromT; |
| 9191 | |
| 9192 | // Check whether we've already imported this type. |
| 9193 | llvm::DenseMap<const Type *, const Type *>::iterator Pos = |
| 9194 | ImportedTypes.find(Val: FromT); |
| 9195 | if (Pos != ImportedTypes.end()) |
| 9196 | return Pos->second; |
| 9197 | |
| 9198 | // Import the type. |
| 9199 | ASTNodeImporter Importer(*this); |
| 9200 | ExpectedType ToTOrErr = Importer.Visit(T: FromT); |
| 9201 | if (!ToTOrErr) |
| 9202 | return ToTOrErr.takeError(); |
| 9203 | |
| 9204 | // Record the imported type. |
| 9205 | ImportedTypes[FromT] = ToTOrErr->getTypePtr(); |
| 9206 | |
| 9207 | return ToTOrErr->getTypePtr(); |
| 9208 | } |
| 9209 | |
| 9210 | Expected<QualType> ASTImporter::Import(QualType FromT) { |
| 9211 | if (FromT.isNull()) |
| 9212 | return QualType{}; |
| 9213 | |
| 9214 | ExpectedTypePtr ToTyOrErr = Import(FromT: FromT.getTypePtr()); |
| 9215 | if (!ToTyOrErr) |
| 9216 | return ToTyOrErr.takeError(); |
| 9217 | |
| 9218 | return ToContext.getQualifiedType(T: *ToTyOrErr, Qs: FromT.getLocalQualifiers()); |
| 9219 | } |
| 9220 | |
| 9221 | Expected<TypeSourceInfo *> ASTImporter::Import(TypeSourceInfo *FromTSI) { |
| 9222 | if (!FromTSI) |
| 9223 | return FromTSI; |
| 9224 | |
| 9225 | // FIXME: For now we just create a "trivial" type source info based |
| 9226 | // on the type and a single location. Implement a real version of this. |
| 9227 | ExpectedType TOrErr = Import(FromT: FromTSI->getType()); |
| 9228 | if (!TOrErr) |
| 9229 | return TOrErr.takeError(); |
| 9230 | ExpectedSLoc BeginLocOrErr = Import(FromLoc: FromTSI->getTypeLoc().getBeginLoc()); |
| 9231 | if (!BeginLocOrErr) |
| 9232 | return BeginLocOrErr.takeError(); |
| 9233 | |
| 9234 | return ToContext.getTrivialTypeSourceInfo(T: *TOrErr, Loc: *BeginLocOrErr); |
| 9235 | } |
| 9236 | |
| 9237 | namespace { |
| 9238 | // To use this object, it should be created before the new attribute is created, |
| 9239 | // and destructed after it is created. The construction already performs the |
| 9240 | // import of the data. |
| 9241 | template <typename T> struct AttrArgImporter { |
| 9242 | AttrArgImporter(const AttrArgImporter<T> &) = delete; |
| 9243 | AttrArgImporter(AttrArgImporter<T> &&) = default; |
| 9244 | AttrArgImporter<T> &operator=(const AttrArgImporter<T> &) = delete; |
| 9245 | AttrArgImporter<T> &operator=(AttrArgImporter<T> &&) = default; |
| 9246 | |
| 9247 | AttrArgImporter(ASTNodeImporter &I, Error &Err, const T &From) |
| 9248 | : To(I.importChecked(Err, From)) {} |
| 9249 | |
| 9250 | const T &value() { return To; } |
| 9251 | |
| 9252 | private: |
| 9253 | T To; |
| 9254 | }; |
| 9255 | |
| 9256 | // To use this object, it should be created before the new attribute is created, |
| 9257 | // and destructed after it is created. The construction already performs the |
| 9258 | // import of the data. The array data is accessible in a pointer form, this form |
| 9259 | // is used by the attribute classes. This object should be created once for the |
| 9260 | // array data to be imported (the array size is not imported, just copied). |
| 9261 | template <typename T> struct AttrArgArrayImporter { |
| 9262 | AttrArgArrayImporter(const AttrArgArrayImporter<T> &) = delete; |
| 9263 | AttrArgArrayImporter(AttrArgArrayImporter<T> &&) = default; |
| 9264 | AttrArgArrayImporter<T> &operator=(const AttrArgArrayImporter<T> &) = delete; |
| 9265 | AttrArgArrayImporter<T> &operator=(AttrArgArrayImporter<T> &&) = default; |
| 9266 | |
| 9267 | AttrArgArrayImporter(ASTNodeImporter &I, Error &Err, |
| 9268 | const llvm::iterator_range<T *> &From, |
| 9269 | unsigned ArraySize) { |
| 9270 | if (Err) |
| 9271 | return; |
| 9272 | To.reserve(ArraySize); |
| 9273 | Err = I.ImportContainerChecked(From, To); |
| 9274 | } |
| 9275 | |
| 9276 | T *value() { return To.data(); } |
| 9277 | |
| 9278 | private: |
| 9279 | llvm::SmallVector<T, 2> To; |
| 9280 | }; |
| 9281 | |
| 9282 | class AttrImporter { |
| 9283 | Error Err{Error::success()}; |
| 9284 | Attr *ToAttr = nullptr; |
| 9285 | ASTImporter &Importer; |
| 9286 | ASTNodeImporter NImporter; |
| 9287 | |
| 9288 | public: |
| 9289 | AttrImporter(ASTImporter &I) : Importer(I), NImporter(I) {} |
| 9290 | |
| 9291 | // Useful for accessing the imported attribute. |
| 9292 | template <typename T> T *castAttrAs() { return cast<T>(ToAttr); } |
| 9293 | template <typename T> const T *castAttrAs() const { return cast<T>(ToAttr); } |
| 9294 | |
| 9295 | // Create an "importer" for an attribute parameter. |
| 9296 | // Result of the 'value()' of that object is to be passed to the function |
| 9297 | // 'importAttr', in the order that is expected by the attribute class. |
| 9298 | template <class T> AttrArgImporter<T> importArg(const T &From) { |
| 9299 | return AttrArgImporter<T>(NImporter, Err, From); |
| 9300 | } |
| 9301 | |
| 9302 | // Create an "importer" for an attribute parameter that has array type. |
| 9303 | // Result of the 'value()' of that object is to be passed to the function |
| 9304 | // 'importAttr', then the size of the array as next argument. |
| 9305 | template <typename T> |
| 9306 | AttrArgArrayImporter<T> importArrayArg(const llvm::iterator_range<T *> &From, |
| 9307 | unsigned ArraySize) { |
| 9308 | return AttrArgArrayImporter<T>(NImporter, Err, From, ArraySize); |
| 9309 | } |
| 9310 | |
| 9311 | // Create an attribute object with the specified arguments. |
| 9312 | // The 'FromAttr' is the original (not imported) attribute, the 'ImportedArg' |
| 9313 | // should be values that are passed to the 'Create' function of the attribute. |
| 9314 | // (The 'Create' with 'ASTContext' first and 'AttributeCommonInfo' last is |
| 9315 | // used here.) As much data is copied or imported from the old attribute |
| 9316 | // as possible. The passed arguments should be already imported. |
| 9317 | // If an import error happens, the internal error is set to it, and any |
| 9318 | // further import attempt is ignored. |
| 9319 | template <typename T, typename... Arg> |
| 9320 | void importAttr(const T *FromAttr, Arg &&...ImportedArg) { |
| 9321 | static_assert(std::is_base_of<Attr, T>::value, |
| 9322 | "T should be subclass of Attr." ); |
| 9323 | assert(!ToAttr && "Use one AttrImporter to import one Attribute object." ); |
| 9324 | |
| 9325 | const IdentifierInfo *ToAttrName = Importer.Import(FromAttr->getAttrName()); |
| 9326 | const IdentifierInfo *ToScopeName = |
| 9327 | Importer.Import(FromAttr->getScopeName()); |
| 9328 | SourceRange ToAttrRange = |
| 9329 | NImporter.importChecked(Err, FromAttr->getRange()); |
| 9330 | SourceLocation ToScopeLoc = |
| 9331 | NImporter.importChecked(Err, FromAttr->getScopeLoc()); |
| 9332 | |
| 9333 | if (Err) |
| 9334 | return; |
| 9335 | |
| 9336 | AttributeCommonInfo ToI(ToAttrName, ToScopeName, ToAttrRange, ToScopeLoc, |
| 9337 | FromAttr->getParsedKind(), FromAttr->getForm()); |
| 9338 | // The "SemanticSpelling" is not needed to be passed to the constructor. |
| 9339 | // That value is recalculated from the SpellingListIndex if needed. |
| 9340 | ToAttr = T::Create(Importer.getToContext(), |
| 9341 | std::forward<Arg>(ImportedArg)..., ToI); |
| 9342 | |
| 9343 | ToAttr->setImplicit(FromAttr->isImplicit()); |
| 9344 | ToAttr->setPackExpansion(FromAttr->isPackExpansion()); |
| 9345 | if (auto *ToInheritableAttr = dyn_cast<InheritableAttr>(Val: ToAttr)) |
| 9346 | ToInheritableAttr->setInherited(FromAttr->isInherited()); |
| 9347 | } |
| 9348 | |
| 9349 | // Create a clone of the 'FromAttr' and import its source range only. |
| 9350 | // This causes objects with invalid references to be created if the 'FromAttr' |
| 9351 | // contains other data that should be imported. |
| 9352 | void cloneAttr(const Attr *FromAttr) { |
| 9353 | assert(!ToAttr && "Use one AttrImporter to import one Attribute object." ); |
| 9354 | |
| 9355 | SourceRange ToRange = NImporter.importChecked(Err, From: FromAttr->getRange()); |
| 9356 | if (Err) |
| 9357 | return; |
| 9358 | |
| 9359 | ToAttr = FromAttr->clone(C&: Importer.getToContext()); |
| 9360 | ToAttr->setRange(ToRange); |
| 9361 | ToAttr->setAttrName(Importer.Import(FromId: FromAttr->getAttrName())); |
| 9362 | } |
| 9363 | |
| 9364 | // Get the result of the previous import attempt (can be used only once). |
| 9365 | llvm::Expected<Attr *> getResult() && { |
| 9366 | if (Err) |
| 9367 | return std::move(Err); |
| 9368 | assert(ToAttr && "Attribute should be created." ); |
| 9369 | return ToAttr; |
| 9370 | } |
| 9371 | }; |
| 9372 | } // namespace |
| 9373 | |
| 9374 | Expected<Attr *> ASTImporter::Import(const Attr *FromAttr) { |
| 9375 | AttrImporter AI(*this); |
| 9376 | |
| 9377 | // FIXME: Is there some kind of AttrVisitor to use here? |
| 9378 | switch (FromAttr->getKind()) { |
| 9379 | case attr::Aligned: { |
| 9380 | auto *From = cast<AlignedAttr>(FromAttr); |
| 9381 | if (From->isAlignmentExpr()) |
| 9382 | AI.importAttr(From, true, AI.importArg(From->getAlignmentExpr()).value()); |
| 9383 | else |
| 9384 | AI.importAttr(From, false, |
| 9385 | AI.importArg(From->getAlignmentType()).value()); |
| 9386 | break; |
| 9387 | } |
| 9388 | |
| 9389 | case attr::AlignValue: { |
| 9390 | auto *From = cast<AlignValueAttr>(FromAttr); |
| 9391 | AI.importAttr(From, AI.importArg(From->getAlignment()).value()); |
| 9392 | break; |
| 9393 | } |
| 9394 | |
| 9395 | case attr::Format: { |
| 9396 | const auto *From = cast<FormatAttr>(FromAttr); |
| 9397 | AI.importAttr(From, Import(From->getType()), From->getFormatIdx(), |
| 9398 | From->getFirstArg()); |
| 9399 | break; |
| 9400 | } |
| 9401 | |
| 9402 | case attr::EnableIf: { |
| 9403 | const auto *From = cast<EnableIfAttr>(FromAttr); |
| 9404 | AI.importAttr(From, AI.importArg(From->getCond()).value(), |
| 9405 | From->getMessage()); |
| 9406 | break; |
| 9407 | } |
| 9408 | |
| 9409 | case attr::AssertCapability: { |
| 9410 | const auto *From = cast<AssertCapabilityAttr>(FromAttr); |
| 9411 | AI.importAttr(From, |
| 9412 | AI.importArrayArg(From->args(), From->args_size()).value(), |
| 9413 | From->args_size()); |
| 9414 | break; |
| 9415 | } |
| 9416 | case attr::AcquireCapability: { |
| 9417 | const auto *From = cast<AcquireCapabilityAttr>(FromAttr); |
| 9418 | AI.importAttr(From, |
| 9419 | AI.importArrayArg(From->args(), From->args_size()).value(), |
| 9420 | From->args_size()); |
| 9421 | break; |
| 9422 | } |
| 9423 | case attr::TryAcquireCapability: { |
| 9424 | const auto *From = cast<TryAcquireCapabilityAttr>(FromAttr); |
| 9425 | AI.importAttr(From, AI.importArg(From->getSuccessValue()).value(), |
| 9426 | AI.importArrayArg(From->args(), From->args_size()).value(), |
| 9427 | From->args_size()); |
| 9428 | break; |
| 9429 | } |
| 9430 | case attr::ReleaseCapability: { |
| 9431 | const auto *From = cast<ReleaseCapabilityAttr>(FromAttr); |
| 9432 | AI.importAttr(From, |
| 9433 | AI.importArrayArg(From->args(), From->args_size()).value(), |
| 9434 | From->args_size()); |
| 9435 | break; |
| 9436 | } |
| 9437 | case attr::RequiresCapability: { |
| 9438 | const auto *From = cast<RequiresCapabilityAttr>(FromAttr); |
| 9439 | AI.importAttr(From, |
| 9440 | AI.importArrayArg(From->args(), From->args_size()).value(), |
| 9441 | From->args_size()); |
| 9442 | break; |
| 9443 | } |
| 9444 | case attr::GuardedBy: { |
| 9445 | const auto *From = cast<GuardedByAttr>(FromAttr); |
| 9446 | AI.importAttr(From, AI.importArg(From->getArg()).value()); |
| 9447 | break; |
| 9448 | } |
| 9449 | case attr::PtGuardedBy: { |
| 9450 | const auto *From = cast<PtGuardedByAttr>(FromAttr); |
| 9451 | AI.importAttr(From, AI.importArg(From->getArg()).value()); |
| 9452 | break; |
| 9453 | } |
| 9454 | case attr::AcquiredAfter: { |
| 9455 | const auto *From = cast<AcquiredAfterAttr>(FromAttr); |
| 9456 | AI.importAttr(From, |
| 9457 | AI.importArrayArg(From->args(), From->args_size()).value(), |
| 9458 | From->args_size()); |
| 9459 | break; |
| 9460 | } |
| 9461 | case attr::AcquiredBefore: { |
| 9462 | const auto *From = cast<AcquiredBeforeAttr>(FromAttr); |
| 9463 | AI.importAttr(From, |
| 9464 | AI.importArrayArg(From->args(), From->args_size()).value(), |
| 9465 | From->args_size()); |
| 9466 | break; |
| 9467 | } |
| 9468 | case attr::LockReturned: { |
| 9469 | const auto *From = cast<LockReturnedAttr>(FromAttr); |
| 9470 | AI.importAttr(From, AI.importArg(From->getArg()).value()); |
| 9471 | break; |
| 9472 | } |
| 9473 | case attr::LocksExcluded: { |
| 9474 | const auto *From = cast<LocksExcludedAttr>(FromAttr); |
| 9475 | AI.importAttr(From, |
| 9476 | AI.importArrayArg(From->args(), From->args_size()).value(), |
| 9477 | From->args_size()); |
| 9478 | break; |
| 9479 | } |
| 9480 | default: { |
| 9481 | // The default branch works for attributes that have no arguments to import. |
| 9482 | // FIXME: Handle every attribute type that has arguments of type to import |
| 9483 | // (most often Expr* or Decl* or type) in the switch above. |
| 9484 | AI.cloneAttr(FromAttr); |
| 9485 | break; |
| 9486 | } |
| 9487 | } |
| 9488 | |
| 9489 | return std::move(AI).getResult(); |
| 9490 | } |
| 9491 | |
| 9492 | Decl *ASTImporter::GetAlreadyImportedOrNull(const Decl *FromD) const { |
| 9493 | return ImportedDecls.lookup(Val: FromD); |
| 9494 | } |
| 9495 | |
| 9496 | TranslationUnitDecl *ASTImporter::GetFromTU(Decl *ToD) { |
| 9497 | auto FromDPos = ImportedFromDecls.find(Val: ToD); |
| 9498 | if (FromDPos == ImportedFromDecls.end()) |
| 9499 | return nullptr; |
| 9500 | return FromDPos->second->getTranslationUnitDecl(); |
| 9501 | } |
| 9502 | |
| 9503 | Expected<Decl *> ASTImporter::Import(Decl *FromD) { |
| 9504 | if (!FromD) |
| 9505 | return nullptr; |
| 9506 | |
| 9507 | // Push FromD to the stack, and remove that when we return. |
| 9508 | ImportPath.push(D: FromD); |
| 9509 | auto ImportPathBuilder = |
| 9510 | llvm::make_scope_exit(F: [this]() { ImportPath.pop(); }); |
| 9511 | |
| 9512 | // Check whether there was a previous failed import. |
| 9513 | // If yes return the existing error. |
| 9514 | if (auto Error = getImportDeclErrorIfAny(FromD)) |
| 9515 | return make_error<ASTImportError>(Args&: *Error); |
| 9516 | |
| 9517 | // Check whether we've already imported this declaration. |
| 9518 | Decl *ToD = GetAlreadyImportedOrNull(FromD); |
| 9519 | if (ToD) { |
| 9520 | // Already imported (possibly from another TU) and with an error. |
| 9521 | if (auto Error = SharedState->getImportDeclErrorIfAny(ToD)) { |
| 9522 | setImportDeclError(From: FromD, Error: *Error); |
| 9523 | return make_error<ASTImportError>(Args&: *Error); |
| 9524 | } |
| 9525 | |
| 9526 | // If FromD has some updated flags after last import, apply it. |
| 9527 | updateFlags(From: FromD, To: ToD); |
| 9528 | // If we encounter a cycle during an import then we save the relevant part |
| 9529 | // of the import path associated to the Decl. |
| 9530 | if (ImportPath.hasCycleAtBack()) |
| 9531 | SavedImportPaths[FromD].push_back(Elt: ImportPath.copyCycleAtBack()); |
| 9532 | return ToD; |
| 9533 | } |
| 9534 | |
| 9535 | // Import the declaration. |
| 9536 | ExpectedDecl ToDOrErr = ImportImpl(FromD); |
| 9537 | if (!ToDOrErr) { |
| 9538 | // Failed to import. |
| 9539 | |
| 9540 | auto Pos = ImportedDecls.find(Val: FromD); |
| 9541 | if (Pos != ImportedDecls.end()) { |
| 9542 | // Import failed after the object was created. |
| 9543 | // Remove all references to it. |
| 9544 | auto *ToD = Pos->second; |
| 9545 | ImportedDecls.erase(I: Pos); |
| 9546 | |
| 9547 | // ImportedDecls and ImportedFromDecls are not symmetric. It may happen |
| 9548 | // (e.g. with namespaces) that several decls from the 'from' context are |
| 9549 | // mapped to the same decl in the 'to' context. If we removed entries |
| 9550 | // from the LookupTable here then we may end up removing them multiple |
| 9551 | // times. |
| 9552 | |
| 9553 | // The Lookuptable contains decls only which are in the 'to' context. |
| 9554 | // Remove from the Lookuptable only if it is *imported* into the 'to' |
| 9555 | // context (and do not remove it if it was added during the initial |
| 9556 | // traverse of the 'to' context). |
| 9557 | auto PosF = ImportedFromDecls.find(Val: ToD); |
| 9558 | if (PosF != ImportedFromDecls.end()) { |
| 9559 | // In the case of TypedefNameDecl we create the Decl first and only |
| 9560 | // then we import and set its DeclContext. So, the DC might not be set |
| 9561 | // when we reach here. |
| 9562 | if (ToD->getDeclContext()) |
| 9563 | SharedState->removeDeclFromLookup(D: ToD); |
| 9564 | ImportedFromDecls.erase(I: PosF); |
| 9565 | } |
| 9566 | |
| 9567 | // FIXME: AST may contain remaining references to the failed object. |
| 9568 | // However, the ImportDeclErrors in the shared state contains all the |
| 9569 | // failed objects together with their error. |
| 9570 | } |
| 9571 | |
| 9572 | // Error encountered for the first time. |
| 9573 | // After takeError the error is not usable any more in ToDOrErr. |
| 9574 | // Get a copy of the error object (any more simple solution for this?). |
| 9575 | ASTImportError ErrOut; |
| 9576 | handleAllErrors(E: ToDOrErr.takeError(), |
| 9577 | Handlers: [&ErrOut](const ASTImportError &E) { ErrOut = E; }); |
| 9578 | setImportDeclError(From: FromD, Error: ErrOut); |
| 9579 | // Set the error for the mapped to Decl, which is in the "to" context. |
| 9580 | if (Pos != ImportedDecls.end()) |
| 9581 | SharedState->setImportDeclError(To: Pos->second, Error: ErrOut); |
| 9582 | |
| 9583 | // Set the error for all nodes which have been created before we |
| 9584 | // recognized the error. |
| 9585 | for (const auto &Path : SavedImportPaths[FromD]) { |
| 9586 | // The import path contains import-dependency nodes first. |
| 9587 | // Save the node that was imported as dependency of the current node. |
| 9588 | Decl *PrevFromDi = FromD; |
| 9589 | for (Decl *FromDi : Path) { |
| 9590 | // Begin and end of the path equals 'FromD', skip it. |
| 9591 | if (FromDi == FromD) |
| 9592 | continue; |
| 9593 | // We should not set import error on a node and all following nodes in |
| 9594 | // the path if child import errors are ignored. |
| 9595 | if (ChildErrorHandlingStrategy(FromDi).ignoreChildErrorOnParent( |
| 9596 | FromChildD: PrevFromDi)) |
| 9597 | break; |
| 9598 | PrevFromDi = FromDi; |
| 9599 | setImportDeclError(From: FromDi, Error: ErrOut); |
| 9600 | //FIXME Should we remove these Decls from ImportedDecls? |
| 9601 | // Set the error for the mapped to Decl, which is in the "to" context. |
| 9602 | auto Ii = ImportedDecls.find(Val: FromDi); |
| 9603 | if (Ii != ImportedDecls.end()) |
| 9604 | SharedState->setImportDeclError(To: Ii->second, Error: ErrOut); |
| 9605 | // FIXME Should we remove these Decls from the LookupTable, |
| 9606 | // and from ImportedFromDecls? |
| 9607 | } |
| 9608 | } |
| 9609 | SavedImportPaths.erase(Val: FromD); |
| 9610 | |
| 9611 | // Do not return ToDOrErr, error was taken out of it. |
| 9612 | return make_error<ASTImportError>(Args&: ErrOut); |
| 9613 | } |
| 9614 | |
| 9615 | ToD = *ToDOrErr; |
| 9616 | |
| 9617 | // FIXME: Handle the "already imported with error" case. We can get here |
| 9618 | // nullptr only if GetImportedOrCreateDecl returned nullptr (after a |
| 9619 | // previously failed create was requested). |
| 9620 | // Later GetImportedOrCreateDecl can be updated to return the error. |
| 9621 | if (!ToD) { |
| 9622 | auto Err = getImportDeclErrorIfAny(FromD); |
| 9623 | assert(Err); |
| 9624 | return make_error<ASTImportError>(Args&: *Err); |
| 9625 | } |
| 9626 | |
| 9627 | // We could import from the current TU without error. But previously we |
| 9628 | // already had imported a Decl as `ToD` from another TU (with another |
| 9629 | // ASTImporter object) and with an error. |
| 9630 | if (auto Error = SharedState->getImportDeclErrorIfAny(ToD)) { |
| 9631 | setImportDeclError(From: FromD, Error: *Error); |
| 9632 | return make_error<ASTImportError>(Args&: *Error); |
| 9633 | } |
| 9634 | // Make sure that ImportImpl registered the imported decl. |
| 9635 | assert(ImportedDecls.count(FromD) != 0 && "Missing call to MapImported?" ); |
| 9636 | |
| 9637 | if (FromD->hasAttrs()) |
| 9638 | for (const Attr *FromAttr : FromD->getAttrs()) { |
| 9639 | auto ToAttrOrErr = Import(FromAttr); |
| 9640 | if (ToAttrOrErr) |
| 9641 | ToD->addAttr(A: *ToAttrOrErr); |
| 9642 | else |
| 9643 | return ToAttrOrErr.takeError(); |
| 9644 | } |
| 9645 | |
| 9646 | // Notify subclasses. |
| 9647 | Imported(From: FromD, To: ToD); |
| 9648 | |
| 9649 | updateFlags(From: FromD, To: ToD); |
| 9650 | SavedImportPaths.erase(Val: FromD); |
| 9651 | return ToDOrErr; |
| 9652 | } |
| 9653 | |
| 9654 | llvm::Expected<InheritedConstructor> |
| 9655 | ASTImporter::Import(const InheritedConstructor &From) { |
| 9656 | return ASTNodeImporter(*this).ImportInheritedConstructor(From); |
| 9657 | } |
| 9658 | |
| 9659 | Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) { |
| 9660 | if (!FromDC) |
| 9661 | return FromDC; |
| 9662 | |
| 9663 | ExpectedDecl ToDCOrErr = Import(FromD: cast<Decl>(Val: FromDC)); |
| 9664 | if (!ToDCOrErr) |
| 9665 | return ToDCOrErr.takeError(); |
| 9666 | auto *ToDC = cast<DeclContext>(Val: *ToDCOrErr); |
| 9667 | |
| 9668 | // When we're using a record/enum/Objective-C class/protocol as a context, we |
| 9669 | // need it to have a definition. |
| 9670 | if (auto *ToRecord = dyn_cast<RecordDecl>(Val: ToDC)) { |
| 9671 | auto *FromRecord = cast<RecordDecl>(Val: FromDC); |
| 9672 | if (ToRecord->isCompleteDefinition()) |
| 9673 | return ToDC; |
| 9674 | |
| 9675 | // If FromRecord is not defined we need to force it to be. |
| 9676 | // Simply calling CompleteDecl(...) for a RecordDecl will break some cases |
| 9677 | // it will start the definition but we never finish it. |
| 9678 | // If there are base classes they won't be imported and we will |
| 9679 | // be missing anything that we inherit from those bases. |
| 9680 | if (FromRecord->getASTContext().getExternalSource() && |
| 9681 | !FromRecord->isCompleteDefinition()) |
| 9682 | FromRecord->getASTContext().getExternalSource()->CompleteType(FromRecord); |
| 9683 | |
| 9684 | if (FromRecord->isCompleteDefinition()) |
| 9685 | if (Error Err = ASTNodeImporter(*this).ImportDefinition( |
| 9686 | From: FromRecord, To: ToRecord, Kind: ASTNodeImporter::IDK_Basic)) |
| 9687 | return std::move(Err); |
| 9688 | } else if (auto *ToEnum = dyn_cast<EnumDecl>(Val: ToDC)) { |
| 9689 | auto * = cast<EnumDecl>(Val: FromDC); |
| 9690 | if (ToEnum->isCompleteDefinition()) { |
| 9691 | // Do nothing. |
| 9692 | } else if (FromEnum->isCompleteDefinition()) { |
| 9693 | if (Error Err = ASTNodeImporter(*this).ImportDefinition( |
| 9694 | From: FromEnum, To: ToEnum, Kind: ASTNodeImporter::IDK_Basic)) |
| 9695 | return std::move(Err); |
| 9696 | } else { |
| 9697 | CompleteDecl(ToEnum); |
| 9698 | } |
| 9699 | } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(Val: ToDC)) { |
| 9700 | auto *FromClass = cast<ObjCInterfaceDecl>(Val: FromDC); |
| 9701 | if (ToClass->getDefinition()) { |
| 9702 | // Do nothing. |
| 9703 | } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) { |
| 9704 | if (Error Err = ASTNodeImporter(*this).ImportDefinition( |
| 9705 | From: FromDef, To: ToClass, Kind: ASTNodeImporter::IDK_Basic)) |
| 9706 | return std::move(Err); |
| 9707 | } else { |
| 9708 | CompleteDecl(ToClass); |
| 9709 | } |
| 9710 | } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(Val: ToDC)) { |
| 9711 | auto *FromProto = cast<ObjCProtocolDecl>(Val: FromDC); |
| 9712 | if (ToProto->getDefinition()) { |
| 9713 | // Do nothing. |
| 9714 | } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) { |
| 9715 | if (Error Err = ASTNodeImporter(*this).ImportDefinition( |
| 9716 | From: FromDef, To: ToProto, Kind: ASTNodeImporter::IDK_Basic)) |
| 9717 | return std::move(Err); |
| 9718 | } else { |
| 9719 | CompleteDecl(ToProto); |
| 9720 | } |
| 9721 | } |
| 9722 | |
| 9723 | return ToDC; |
| 9724 | } |
| 9725 | |
| 9726 | Expected<Expr *> ASTImporter::Import(Expr *FromE) { |
| 9727 | if (ExpectedStmt ToSOrErr = Import(FromS: cast_or_null<Stmt>(Val: FromE))) |
| 9728 | return cast_or_null<Expr>(Val: *ToSOrErr); |
| 9729 | else |
| 9730 | return ToSOrErr.takeError(); |
| 9731 | } |
| 9732 | |
| 9733 | Expected<Stmt *> ASTImporter::Import(Stmt *FromS) { |
| 9734 | if (!FromS) |
| 9735 | return nullptr; |
| 9736 | |
| 9737 | // Check whether we've already imported this statement. |
| 9738 | llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(Val: FromS); |
| 9739 | if (Pos != ImportedStmts.end()) |
| 9740 | return Pos->second; |
| 9741 | |
| 9742 | // Import the statement. |
| 9743 | ASTNodeImporter Importer(*this); |
| 9744 | ExpectedStmt ToSOrErr = Importer.Visit(FromS); |
| 9745 | if (!ToSOrErr) |
| 9746 | return ToSOrErr; |
| 9747 | |
| 9748 | if (auto *ToE = dyn_cast<Expr>(*ToSOrErr)) { |
| 9749 | auto *FromE = cast<Expr>(Val: FromS); |
| 9750 | // Copy ExprBitfields, which may not be handled in Expr subclasses |
| 9751 | // constructors. |
| 9752 | ToE->setValueKind(FromE->getValueKind()); |
| 9753 | ToE->setObjectKind(FromE->getObjectKind()); |
| 9754 | ToE->setDependence(FromE->getDependence()); |
| 9755 | } |
| 9756 | |
| 9757 | // Record the imported statement object. |
| 9758 | ImportedStmts[FromS] = *ToSOrErr; |
| 9759 | return ToSOrErr; |
| 9760 | } |
| 9761 | |
| 9762 | Expected<NestedNameSpecifier *> |
| 9763 | ASTImporter::Import(NestedNameSpecifier *FromNNS) { |
| 9764 | if (!FromNNS) |
| 9765 | return nullptr; |
| 9766 | |
| 9767 | NestedNameSpecifier *Prefix = nullptr; |
| 9768 | if (Error Err = importInto(To&: Prefix, From: FromNNS->getPrefix())) |
| 9769 | return std::move(Err); |
| 9770 | |
| 9771 | switch (FromNNS->getKind()) { |
| 9772 | case NestedNameSpecifier::Identifier: |
| 9773 | assert(FromNNS->getAsIdentifier() && "NNS should contain identifier." ); |
| 9774 | return NestedNameSpecifier::Create(Context: ToContext, Prefix, |
| 9775 | II: Import(FromId: FromNNS->getAsIdentifier())); |
| 9776 | |
| 9777 | case NestedNameSpecifier::Namespace: |
| 9778 | if (ExpectedDecl NSOrErr = Import(FromNNS->getAsNamespace())) { |
| 9779 | return NestedNameSpecifier::Create(Context: ToContext, Prefix, |
| 9780 | NS: cast<NamespaceDecl>(Val: *NSOrErr)); |
| 9781 | } else |
| 9782 | return NSOrErr.takeError(); |
| 9783 | |
| 9784 | case NestedNameSpecifier::NamespaceAlias: |
| 9785 | if (ExpectedDecl NSADOrErr = Import(FromNNS->getAsNamespaceAlias())) |
| 9786 | return NestedNameSpecifier::Create(Context: ToContext, Prefix, |
| 9787 | Alias: cast<NamespaceAliasDecl>(Val: *NSADOrErr)); |
| 9788 | else |
| 9789 | return NSADOrErr.takeError(); |
| 9790 | |
| 9791 | case NestedNameSpecifier::Global: |
| 9792 | return NestedNameSpecifier::GlobalSpecifier(Context: ToContext); |
| 9793 | |
| 9794 | case NestedNameSpecifier::Super: |
| 9795 | if (ExpectedDecl RDOrErr = Import(FromNNS->getAsRecordDecl())) |
| 9796 | return NestedNameSpecifier::SuperSpecifier(Context: ToContext, |
| 9797 | RD: cast<CXXRecordDecl>(Val: *RDOrErr)); |
| 9798 | else |
| 9799 | return RDOrErr.takeError(); |
| 9800 | |
| 9801 | case NestedNameSpecifier::TypeSpec: |
| 9802 | if (ExpectedTypePtr TyOrErr = Import(FromT: FromNNS->getAsType())) { |
| 9803 | return NestedNameSpecifier::Create(Context: ToContext, Prefix, T: *TyOrErr); |
| 9804 | } else { |
| 9805 | return TyOrErr.takeError(); |
| 9806 | } |
| 9807 | } |
| 9808 | |
| 9809 | llvm_unreachable("Invalid nested name specifier kind" ); |
| 9810 | } |
| 9811 | |
| 9812 | Expected<NestedNameSpecifierLoc> |
| 9813 | ASTImporter::Import(NestedNameSpecifierLoc FromNNS) { |
| 9814 | // Copied from NestedNameSpecifier mostly. |
| 9815 | SmallVector<NestedNameSpecifierLoc , 8> NestedNames; |
| 9816 | NestedNameSpecifierLoc NNS = FromNNS; |
| 9817 | |
| 9818 | // Push each of the nested-name-specifiers's onto a stack for |
| 9819 | // serialization in reverse order. |
| 9820 | while (NNS) { |
| 9821 | NestedNames.push_back(Elt: NNS); |
| 9822 | NNS = NNS.getPrefix(); |
| 9823 | } |
| 9824 | |
| 9825 | NestedNameSpecifierLocBuilder Builder; |
| 9826 | |
| 9827 | while (!NestedNames.empty()) { |
| 9828 | NNS = NestedNames.pop_back_val(); |
| 9829 | NestedNameSpecifier *Spec = nullptr; |
| 9830 | if (Error Err = importInto(To&: Spec, From: NNS.getNestedNameSpecifier())) |
| 9831 | return std::move(Err); |
| 9832 | |
| 9833 | NestedNameSpecifier::SpecifierKind Kind = Spec->getKind(); |
| 9834 | |
| 9835 | SourceLocation ToLocalBeginLoc, ToLocalEndLoc; |
| 9836 | if (Kind != NestedNameSpecifier::Super) { |
| 9837 | if (Error Err = importInto(To&: ToLocalBeginLoc, From: NNS.getLocalBeginLoc())) |
| 9838 | return std::move(Err); |
| 9839 | |
| 9840 | if (Kind != NestedNameSpecifier::Global) |
| 9841 | if (Error Err = importInto(To&: ToLocalEndLoc, From: NNS.getLocalEndLoc())) |
| 9842 | return std::move(Err); |
| 9843 | } |
| 9844 | |
| 9845 | switch (Kind) { |
| 9846 | case NestedNameSpecifier::Identifier: |
| 9847 | Builder.Extend(Context&: getToContext(), Identifier: Spec->getAsIdentifier(), IdentifierLoc: ToLocalBeginLoc, |
| 9848 | ColonColonLoc: ToLocalEndLoc); |
| 9849 | break; |
| 9850 | |
| 9851 | case NestedNameSpecifier::Namespace: |
| 9852 | Builder.Extend(Context&: getToContext(), Namespace: Spec->getAsNamespace(), NamespaceLoc: ToLocalBeginLoc, |
| 9853 | ColonColonLoc: ToLocalEndLoc); |
| 9854 | break; |
| 9855 | |
| 9856 | case NestedNameSpecifier::NamespaceAlias: |
| 9857 | Builder.Extend(Context&: getToContext(), Alias: Spec->getAsNamespaceAlias(), |
| 9858 | AliasLoc: ToLocalBeginLoc, ColonColonLoc: ToLocalEndLoc); |
| 9859 | break; |
| 9860 | |
| 9861 | case NestedNameSpecifier::TypeSpec: { |
| 9862 | SourceLocation ToTLoc; |
| 9863 | if (Error Err = importInto(To&: ToTLoc, From: NNS.getTypeLoc().getBeginLoc())) |
| 9864 | return std::move(Err); |
| 9865 | TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo( |
| 9866 | T: QualType(Spec->getAsType(), 0), Loc: ToTLoc); |
| 9867 | Builder.Extend(Context&: getToContext(), TL: TSI->getTypeLoc(), ColonColonLoc: ToLocalEndLoc); |
| 9868 | break; |
| 9869 | } |
| 9870 | |
| 9871 | case NestedNameSpecifier::Global: |
| 9872 | Builder.MakeGlobal(Context&: getToContext(), ColonColonLoc: ToLocalBeginLoc); |
| 9873 | break; |
| 9874 | |
| 9875 | case NestedNameSpecifier::Super: { |
| 9876 | auto ToSourceRangeOrErr = Import(FromRange: NNS.getSourceRange()); |
| 9877 | if (!ToSourceRangeOrErr) |
| 9878 | return ToSourceRangeOrErr.takeError(); |
| 9879 | |
| 9880 | Builder.MakeSuper(Context&: getToContext(), RD: Spec->getAsRecordDecl(), |
| 9881 | SuperLoc: ToSourceRangeOrErr->getBegin(), |
| 9882 | ColonColonLoc: ToSourceRangeOrErr->getEnd()); |
| 9883 | } |
| 9884 | } |
| 9885 | } |
| 9886 | |
| 9887 | return Builder.getWithLocInContext(Context&: getToContext()); |
| 9888 | } |
| 9889 | |
| 9890 | Expected<TemplateName> ASTImporter::Import(TemplateName From) { |
| 9891 | switch (From.getKind()) { |
| 9892 | case TemplateName::Template: |
| 9893 | if (ExpectedDecl ToTemplateOrErr = Import(From.getAsTemplateDecl())) |
| 9894 | return TemplateName(cast<TemplateDecl>(Val: (*ToTemplateOrErr)->getCanonicalDecl())); |
| 9895 | else |
| 9896 | return ToTemplateOrErr.takeError(); |
| 9897 | |
| 9898 | case TemplateName::OverloadedTemplate: { |
| 9899 | OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate(); |
| 9900 | UnresolvedSet<2> ToTemplates; |
| 9901 | for (auto *I : *FromStorage) { |
| 9902 | if (auto ToOrErr = Import(I)) |
| 9903 | ToTemplates.addDecl(cast<NamedDecl>(*ToOrErr)); |
| 9904 | else |
| 9905 | return ToOrErr.takeError(); |
| 9906 | } |
| 9907 | return ToContext.getOverloadedTemplateName(Begin: ToTemplates.begin(), |
| 9908 | End: ToTemplates.end()); |
| 9909 | } |
| 9910 | |
| 9911 | case TemplateName::AssumedTemplate: { |
| 9912 | AssumedTemplateStorage *FromStorage = From.getAsAssumedTemplateName(); |
| 9913 | auto DeclNameOrErr = Import(FromName: FromStorage->getDeclName()); |
| 9914 | if (!DeclNameOrErr) |
| 9915 | return DeclNameOrErr.takeError(); |
| 9916 | return ToContext.getAssumedTemplateName(Name: *DeclNameOrErr); |
| 9917 | } |
| 9918 | |
| 9919 | case TemplateName::QualifiedTemplate: { |
| 9920 | QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName(); |
| 9921 | auto QualifierOrErr = Import(FromNNS: QTN->getQualifier()); |
| 9922 | if (!QualifierOrErr) |
| 9923 | return QualifierOrErr.takeError(); |
| 9924 | auto TNOrErr = Import(From: QTN->getUnderlyingTemplate()); |
| 9925 | if (!TNOrErr) |
| 9926 | return TNOrErr.takeError(); |
| 9927 | return ToContext.getQualifiedTemplateName( |
| 9928 | NNS: *QualifierOrErr, TemplateKeyword: QTN->hasTemplateKeyword(), Template: *TNOrErr); |
| 9929 | } |
| 9930 | |
| 9931 | case TemplateName::DependentTemplate: { |
| 9932 | DependentTemplateName *DTN = From.getAsDependentTemplateName(); |
| 9933 | auto QualifierOrErr = Import(FromNNS: DTN->getQualifier()); |
| 9934 | if (!QualifierOrErr) |
| 9935 | return QualifierOrErr.takeError(); |
| 9936 | return ToContext.getDependentTemplateName( |
| 9937 | Name: {*QualifierOrErr, Import(FromIO: DTN->getName()), DTN->hasTemplateKeyword()}); |
| 9938 | } |
| 9939 | |
| 9940 | case TemplateName::SubstTemplateTemplateParm: { |
| 9941 | SubstTemplateTemplateParmStorage *Subst = |
| 9942 | From.getAsSubstTemplateTemplateParm(); |
| 9943 | auto ReplacementOrErr = Import(From: Subst->getReplacement()); |
| 9944 | if (!ReplacementOrErr) |
| 9945 | return ReplacementOrErr.takeError(); |
| 9946 | |
| 9947 | auto AssociatedDeclOrErr = Import(FromD: Subst->getAssociatedDecl()); |
| 9948 | if (!AssociatedDeclOrErr) |
| 9949 | return AssociatedDeclOrErr.takeError(); |
| 9950 | |
| 9951 | return ToContext.getSubstTemplateTemplateParm( |
| 9952 | replacement: *ReplacementOrErr, AssociatedDecl: *AssociatedDeclOrErr, Index: Subst->getIndex(), |
| 9953 | PackIndex: Subst->getPackIndex(), Final: Subst->getFinal()); |
| 9954 | } |
| 9955 | |
| 9956 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 9957 | SubstTemplateTemplateParmPackStorage *SubstPack = |
| 9958 | From.getAsSubstTemplateTemplateParmPack(); |
| 9959 | ASTNodeImporter Importer(*this); |
| 9960 | auto ArgPackOrErr = |
| 9961 | Importer.ImportTemplateArgument(From: SubstPack->getArgumentPack()); |
| 9962 | if (!ArgPackOrErr) |
| 9963 | return ArgPackOrErr.takeError(); |
| 9964 | |
| 9965 | auto AssociatedDeclOrErr = Import(FromD: SubstPack->getAssociatedDecl()); |
| 9966 | if (!AssociatedDeclOrErr) |
| 9967 | return AssociatedDeclOrErr.takeError(); |
| 9968 | |
| 9969 | return ToContext.getSubstTemplateTemplateParmPack( |
| 9970 | ArgPack: *ArgPackOrErr, AssociatedDecl: *AssociatedDeclOrErr, Index: SubstPack->getIndex(), |
| 9971 | Final: SubstPack->getFinal()); |
| 9972 | } |
| 9973 | case TemplateName::UsingTemplate: { |
| 9974 | auto UsingOrError = Import(From.getAsUsingShadowDecl()); |
| 9975 | if (!UsingOrError) |
| 9976 | return UsingOrError.takeError(); |
| 9977 | return TemplateName(cast<UsingShadowDecl>(*UsingOrError)); |
| 9978 | } |
| 9979 | case TemplateName::DeducedTemplate: |
| 9980 | llvm_unreachable("Unexpected DeducedTemplate" ); |
| 9981 | } |
| 9982 | |
| 9983 | llvm_unreachable("Invalid template name kind" ); |
| 9984 | } |
| 9985 | |
| 9986 | Expected<SourceLocation> ASTImporter::Import(SourceLocation FromLoc) { |
| 9987 | if (FromLoc.isInvalid()) |
| 9988 | return SourceLocation{}; |
| 9989 | |
| 9990 | SourceManager &FromSM = FromContext.getSourceManager(); |
| 9991 | bool IsBuiltin = FromSM.isWrittenInBuiltinFile(Loc: FromLoc); |
| 9992 | |
| 9993 | std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(Loc: FromLoc); |
| 9994 | Expected<FileID> ToFileIDOrErr = Import(Decomposed.first, IsBuiltin); |
| 9995 | if (!ToFileIDOrErr) |
| 9996 | return ToFileIDOrErr.takeError(); |
| 9997 | SourceManager &ToSM = ToContext.getSourceManager(); |
| 9998 | return ToSM.getComposedLoc(FID: *ToFileIDOrErr, Offset: Decomposed.second); |
| 9999 | } |
| 10000 | |
| 10001 | Expected<SourceRange> ASTImporter::Import(SourceRange FromRange) { |
| 10002 | SourceLocation ToBegin, ToEnd; |
| 10003 | if (Error Err = importInto(To&: ToBegin, From: FromRange.getBegin())) |
| 10004 | return std::move(Err); |
| 10005 | if (Error Err = importInto(To&: ToEnd, From: FromRange.getEnd())) |
| 10006 | return std::move(Err); |
| 10007 | |
| 10008 | return SourceRange(ToBegin, ToEnd); |
| 10009 | } |
| 10010 | |
| 10011 | Expected<FileID> ASTImporter::Import(FileID FromID, bool IsBuiltin) { |
| 10012 | llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(Val: FromID); |
| 10013 | if (Pos != ImportedFileIDs.end()) |
| 10014 | return Pos->second; |
| 10015 | |
| 10016 | SourceManager &FromSM = FromContext.getSourceManager(); |
| 10017 | SourceManager &ToSM = ToContext.getSourceManager(); |
| 10018 | const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FID: FromID); |
| 10019 | |
| 10020 | // Map the FromID to the "to" source manager. |
| 10021 | FileID ToID; |
| 10022 | if (FromSLoc.isExpansion()) { |
| 10023 | const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion(); |
| 10024 | ExpectedSLoc ToSpLoc = Import(FromLoc: FromEx.getSpellingLoc()); |
| 10025 | if (!ToSpLoc) |
| 10026 | return ToSpLoc.takeError(); |
| 10027 | ExpectedSLoc ToExLocS = Import(FromLoc: FromEx.getExpansionLocStart()); |
| 10028 | if (!ToExLocS) |
| 10029 | return ToExLocS.takeError(); |
| 10030 | unsigned ExLength = FromSM.getFileIDSize(FID: FromID); |
| 10031 | SourceLocation MLoc; |
| 10032 | if (FromEx.isMacroArgExpansion()) { |
| 10033 | MLoc = ToSM.createMacroArgExpansionLoc(SpellingLoc: *ToSpLoc, ExpansionLoc: *ToExLocS, Length: ExLength); |
| 10034 | } else { |
| 10035 | if (ExpectedSLoc ToExLocE = Import(FromLoc: FromEx.getExpansionLocEnd())) |
| 10036 | MLoc = ToSM.createExpansionLoc(SpellingLoc: *ToSpLoc, ExpansionLocStart: *ToExLocS, ExpansionLocEnd: *ToExLocE, Length: ExLength, |
| 10037 | ExpansionIsTokenRange: FromEx.isExpansionTokenRange()); |
| 10038 | else |
| 10039 | return ToExLocE.takeError(); |
| 10040 | } |
| 10041 | ToID = ToSM.getFileID(SpellingLoc: MLoc); |
| 10042 | } else { |
| 10043 | const SrcMgr::ContentCache *Cache = &FromSLoc.getFile().getContentCache(); |
| 10044 | |
| 10045 | if (!IsBuiltin && !Cache->BufferOverridden) { |
| 10046 | // Include location of this file. |
| 10047 | ExpectedSLoc ToIncludeLoc = Import(FromLoc: FromSLoc.getFile().getIncludeLoc()); |
| 10048 | if (!ToIncludeLoc) |
| 10049 | return ToIncludeLoc.takeError(); |
| 10050 | |
| 10051 | // Every FileID that is not the main FileID needs to have a valid include |
| 10052 | // location so that the include chain points to the main FileID. When |
| 10053 | // importing the main FileID (which has no include location), we need to |
| 10054 | // create a fake include location in the main file to keep this property |
| 10055 | // intact. |
| 10056 | SourceLocation ToIncludeLocOrFakeLoc = *ToIncludeLoc; |
| 10057 | if (FromID == FromSM.getMainFileID()) |
| 10058 | ToIncludeLocOrFakeLoc = ToSM.getLocForStartOfFile(FID: ToSM.getMainFileID()); |
| 10059 | |
| 10060 | if (Cache->OrigEntry && Cache->OrigEntry->getDir()) { |
| 10061 | // FIXME: We probably want to use getVirtualFileRef(), so we don't hit |
| 10062 | // the disk again |
| 10063 | // FIXME: We definitely want to re-use the existing MemoryBuffer, rather |
| 10064 | // than mmap the files several times. |
| 10065 | auto Entry = |
| 10066 | ToFileManager.getOptionalFileRef(Filename: Cache->OrigEntry->getName()); |
| 10067 | // FIXME: The filename may be a virtual name that does probably not |
| 10068 | // point to a valid file and we get no Entry here. In this case try with |
| 10069 | // the memory buffer below. |
| 10070 | if (Entry) |
| 10071 | ToID = ToSM.createFileID(SourceFile: *Entry, IncludePos: ToIncludeLocOrFakeLoc, |
| 10072 | FileCharacter: FromSLoc.getFile().getFileCharacteristic()); |
| 10073 | } |
| 10074 | } |
| 10075 | |
| 10076 | if (ToID.isInvalid() || IsBuiltin) { |
| 10077 | // FIXME: We want to re-use the existing MemoryBuffer! |
| 10078 | std::optional<llvm::MemoryBufferRef> FromBuf = |
| 10079 | Cache->getBufferOrNone(Diag&: FromContext.getDiagnostics(), |
| 10080 | FM&: FromSM.getFileManager(), Loc: SourceLocation{}); |
| 10081 | if (!FromBuf) |
| 10082 | return llvm::make_error<ASTImportError>(Args: ASTImportError::Unknown); |
| 10083 | |
| 10084 | std::unique_ptr<llvm::MemoryBuffer> ToBuf = |
| 10085 | llvm::MemoryBuffer::getMemBufferCopy(InputData: FromBuf->getBuffer(), |
| 10086 | BufferName: FromBuf->getBufferIdentifier()); |
| 10087 | ToID = ToSM.createFileID(Buffer: std::move(ToBuf), |
| 10088 | FileCharacter: FromSLoc.getFile().getFileCharacteristic()); |
| 10089 | } |
| 10090 | } |
| 10091 | |
| 10092 | assert(ToID.isValid() && "Unexpected invalid fileID was created." ); |
| 10093 | |
| 10094 | ImportedFileIDs[FromID] = ToID; |
| 10095 | return ToID; |
| 10096 | } |
| 10097 | |
| 10098 | Expected<CXXCtorInitializer *> ASTImporter::Import(CXXCtorInitializer *From) { |
| 10099 | ExpectedExpr ToExprOrErr = Import(FromE: From->getInit()); |
| 10100 | if (!ToExprOrErr) |
| 10101 | return ToExprOrErr.takeError(); |
| 10102 | |
| 10103 | auto LParenLocOrErr = Import(FromLoc: From->getLParenLoc()); |
| 10104 | if (!LParenLocOrErr) |
| 10105 | return LParenLocOrErr.takeError(); |
| 10106 | |
| 10107 | auto RParenLocOrErr = Import(FromLoc: From->getRParenLoc()); |
| 10108 | if (!RParenLocOrErr) |
| 10109 | return RParenLocOrErr.takeError(); |
| 10110 | |
| 10111 | if (From->isBaseInitializer()) { |
| 10112 | auto ToTInfoOrErr = Import(FromTSI: From->getTypeSourceInfo()); |
| 10113 | if (!ToTInfoOrErr) |
| 10114 | return ToTInfoOrErr.takeError(); |
| 10115 | |
| 10116 | SourceLocation EllipsisLoc; |
| 10117 | if (From->isPackExpansion()) |
| 10118 | if (Error Err = importInto(To&: EllipsisLoc, From: From->getEllipsisLoc())) |
| 10119 | return std::move(Err); |
| 10120 | |
| 10121 | return new (ToContext) CXXCtorInitializer( |
| 10122 | ToContext, *ToTInfoOrErr, From->isBaseVirtual(), *LParenLocOrErr, |
| 10123 | *ToExprOrErr, *RParenLocOrErr, EllipsisLoc); |
| 10124 | } else if (From->isMemberInitializer()) { |
| 10125 | ExpectedDecl ToFieldOrErr = Import(From->getMember()); |
| 10126 | if (!ToFieldOrErr) |
| 10127 | return ToFieldOrErr.takeError(); |
| 10128 | |
| 10129 | auto MemberLocOrErr = Import(FromLoc: From->getMemberLocation()); |
| 10130 | if (!MemberLocOrErr) |
| 10131 | return MemberLocOrErr.takeError(); |
| 10132 | |
| 10133 | return new (ToContext) CXXCtorInitializer( |
| 10134 | ToContext, cast_or_null<FieldDecl>(Val: *ToFieldOrErr), *MemberLocOrErr, |
| 10135 | *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr); |
| 10136 | } else if (From->isIndirectMemberInitializer()) { |
| 10137 | ExpectedDecl ToIFieldOrErr = Import(From->getIndirectMember()); |
| 10138 | if (!ToIFieldOrErr) |
| 10139 | return ToIFieldOrErr.takeError(); |
| 10140 | |
| 10141 | auto MemberLocOrErr = Import(FromLoc: From->getMemberLocation()); |
| 10142 | if (!MemberLocOrErr) |
| 10143 | return MemberLocOrErr.takeError(); |
| 10144 | |
| 10145 | return new (ToContext) CXXCtorInitializer( |
| 10146 | ToContext, cast_or_null<IndirectFieldDecl>(Val: *ToIFieldOrErr), |
| 10147 | *MemberLocOrErr, *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr); |
| 10148 | } else if (From->isDelegatingInitializer()) { |
| 10149 | auto ToTInfoOrErr = Import(FromTSI: From->getTypeSourceInfo()); |
| 10150 | if (!ToTInfoOrErr) |
| 10151 | return ToTInfoOrErr.takeError(); |
| 10152 | |
| 10153 | return new (ToContext) |
| 10154 | CXXCtorInitializer(ToContext, *ToTInfoOrErr, *LParenLocOrErr, |
| 10155 | *ToExprOrErr, *RParenLocOrErr); |
| 10156 | } else { |
| 10157 | // FIXME: assert? |
| 10158 | return make_error<ASTImportError>(); |
| 10159 | } |
| 10160 | } |
| 10161 | |
| 10162 | Expected<CXXBaseSpecifier *> |
| 10163 | ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) { |
| 10164 | auto Pos = ImportedCXXBaseSpecifiers.find(Val: BaseSpec); |
| 10165 | if (Pos != ImportedCXXBaseSpecifiers.end()) |
| 10166 | return Pos->second; |
| 10167 | |
| 10168 | Expected<SourceRange> ToSourceRange = Import(FromRange: BaseSpec->getSourceRange()); |
| 10169 | if (!ToSourceRange) |
| 10170 | return ToSourceRange.takeError(); |
| 10171 | Expected<TypeSourceInfo *> ToTSI = Import(FromTSI: BaseSpec->getTypeSourceInfo()); |
| 10172 | if (!ToTSI) |
| 10173 | return ToTSI.takeError(); |
| 10174 | ExpectedSLoc ToEllipsisLoc = Import(FromLoc: BaseSpec->getEllipsisLoc()); |
| 10175 | if (!ToEllipsisLoc) |
| 10176 | return ToEllipsisLoc.takeError(); |
| 10177 | CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier( |
| 10178 | *ToSourceRange, BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(), |
| 10179 | BaseSpec->getAccessSpecifierAsWritten(), *ToTSI, *ToEllipsisLoc); |
| 10180 | ImportedCXXBaseSpecifiers[BaseSpec] = Imported; |
| 10181 | return Imported; |
| 10182 | } |
| 10183 | |
| 10184 | llvm::Expected<APValue> ASTImporter::Import(const APValue &FromValue) { |
| 10185 | ASTNodeImporter Importer(*this); |
| 10186 | return Importer.ImportAPValue(FromValue); |
| 10187 | } |
| 10188 | |
| 10189 | Error ASTImporter::ImportDefinition(Decl *From) { |
| 10190 | ExpectedDecl ToOrErr = Import(FromD: From); |
| 10191 | if (!ToOrErr) |
| 10192 | return ToOrErr.takeError(); |
| 10193 | Decl *To = *ToOrErr; |
| 10194 | |
| 10195 | auto *FromDC = cast<DeclContext>(Val: From); |
| 10196 | ASTNodeImporter Importer(*this); |
| 10197 | |
| 10198 | if (auto *ToRecord = dyn_cast<RecordDecl>(Val: To)) { |
| 10199 | if (!ToRecord->getDefinition()) { |
| 10200 | return Importer.ImportDefinition( |
| 10201 | From: cast<RecordDecl>(Val: FromDC), To: ToRecord, |
| 10202 | Kind: ASTNodeImporter::IDK_Everything); |
| 10203 | } |
| 10204 | } |
| 10205 | |
| 10206 | if (auto *ToEnum = dyn_cast<EnumDecl>(Val: To)) { |
| 10207 | if (!ToEnum->getDefinition()) { |
| 10208 | return Importer.ImportDefinition( |
| 10209 | From: cast<EnumDecl>(Val: FromDC), To: ToEnum, Kind: ASTNodeImporter::IDK_Everything); |
| 10210 | } |
| 10211 | } |
| 10212 | |
| 10213 | if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(Val: To)) { |
| 10214 | if (!ToIFace->getDefinition()) { |
| 10215 | return Importer.ImportDefinition( |
| 10216 | From: cast<ObjCInterfaceDecl>(Val: FromDC), To: ToIFace, |
| 10217 | Kind: ASTNodeImporter::IDK_Everything); |
| 10218 | } |
| 10219 | } |
| 10220 | |
| 10221 | if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(Val: To)) { |
| 10222 | if (!ToProto->getDefinition()) { |
| 10223 | return Importer.ImportDefinition( |
| 10224 | From: cast<ObjCProtocolDecl>(Val: FromDC), To: ToProto, |
| 10225 | Kind: ASTNodeImporter::IDK_Everything); |
| 10226 | } |
| 10227 | } |
| 10228 | |
| 10229 | return Importer.ImportDeclContext(FromDC, ForceImport: true); |
| 10230 | } |
| 10231 | |
| 10232 | Expected<DeclarationName> ASTImporter::Import(DeclarationName FromName) { |
| 10233 | if (!FromName) |
| 10234 | return DeclarationName{}; |
| 10235 | |
| 10236 | switch (FromName.getNameKind()) { |
| 10237 | case DeclarationName::Identifier: |
| 10238 | return DeclarationName(Import(FromId: FromName.getAsIdentifierInfo())); |
| 10239 | |
| 10240 | case DeclarationName::ObjCZeroArgSelector: |
| 10241 | case DeclarationName::ObjCOneArgSelector: |
| 10242 | case DeclarationName::ObjCMultiArgSelector: |
| 10243 | if (auto ToSelOrErr = Import(FromName.getObjCSelector())) |
| 10244 | return DeclarationName(*ToSelOrErr); |
| 10245 | else |
| 10246 | return ToSelOrErr.takeError(); |
| 10247 | |
| 10248 | case DeclarationName::CXXConstructorName: { |
| 10249 | if (auto ToTyOrErr = Import(FromT: FromName.getCXXNameType())) |
| 10250 | return ToContext.DeclarationNames.getCXXConstructorName( |
| 10251 | Ty: ToContext.getCanonicalType(T: *ToTyOrErr)); |
| 10252 | else |
| 10253 | return ToTyOrErr.takeError(); |
| 10254 | } |
| 10255 | |
| 10256 | case DeclarationName::CXXDestructorName: { |
| 10257 | if (auto ToTyOrErr = Import(FromT: FromName.getCXXNameType())) |
| 10258 | return ToContext.DeclarationNames.getCXXDestructorName( |
| 10259 | Ty: ToContext.getCanonicalType(T: *ToTyOrErr)); |
| 10260 | else |
| 10261 | return ToTyOrErr.takeError(); |
| 10262 | } |
| 10263 | |
| 10264 | case DeclarationName::CXXDeductionGuideName: { |
| 10265 | if (auto ToTemplateOrErr = Import(FromName.getCXXDeductionGuideTemplate())) |
| 10266 | return ToContext.DeclarationNames.getCXXDeductionGuideName( |
| 10267 | TD: cast<TemplateDecl>(*ToTemplateOrErr)); |
| 10268 | else |
| 10269 | return ToTemplateOrErr.takeError(); |
| 10270 | } |
| 10271 | |
| 10272 | case DeclarationName::CXXConversionFunctionName: { |
| 10273 | if (auto ToTyOrErr = Import(FromT: FromName.getCXXNameType())) |
| 10274 | return ToContext.DeclarationNames.getCXXConversionFunctionName( |
| 10275 | Ty: ToContext.getCanonicalType(T: *ToTyOrErr)); |
| 10276 | else |
| 10277 | return ToTyOrErr.takeError(); |
| 10278 | } |
| 10279 | |
| 10280 | case DeclarationName::CXXOperatorName: |
| 10281 | return ToContext.DeclarationNames.getCXXOperatorName( |
| 10282 | Op: FromName.getCXXOverloadedOperator()); |
| 10283 | |
| 10284 | case DeclarationName::CXXLiteralOperatorName: |
| 10285 | return ToContext.DeclarationNames.getCXXLiteralOperatorName( |
| 10286 | II: Import(FromId: FromName.getCXXLiteralIdentifier())); |
| 10287 | |
| 10288 | case DeclarationName::CXXUsingDirective: |
| 10289 | // FIXME: STATICS! |
| 10290 | return DeclarationName::getUsingDirectiveName(); |
| 10291 | } |
| 10292 | |
| 10293 | llvm_unreachable("Invalid DeclarationName Kind!" ); |
| 10294 | } |
| 10295 | |
| 10296 | IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) { |
| 10297 | if (!FromId) |
| 10298 | return nullptr; |
| 10299 | |
| 10300 | IdentifierInfo *ToId = &ToContext.Idents.get(Name: FromId->getName()); |
| 10301 | |
| 10302 | if (!ToId->getBuiltinID() && FromId->getBuiltinID()) |
| 10303 | ToId->setBuiltinID(FromId->getBuiltinID()); |
| 10304 | |
| 10305 | return ToId; |
| 10306 | } |
| 10307 | |
| 10308 | IdentifierOrOverloadedOperator |
| 10309 | ASTImporter::Import(IdentifierOrOverloadedOperator FromIO) { |
| 10310 | if (const IdentifierInfo *FromII = FromIO.getIdentifier()) |
| 10311 | return Import(FromId: FromII); |
| 10312 | return FromIO.getOperator(); |
| 10313 | } |
| 10314 | |
| 10315 | Expected<Selector> ASTImporter::Import(Selector FromSel) { |
| 10316 | if (FromSel.isNull()) |
| 10317 | return Selector{}; |
| 10318 | |
| 10319 | SmallVector<const IdentifierInfo *, 4> Idents; |
| 10320 | Idents.push_back(Elt: Import(FromId: FromSel.getIdentifierInfoForSlot(argIndex: 0))); |
| 10321 | for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I) |
| 10322 | Idents.push_back(Elt: Import(FromId: FromSel.getIdentifierInfoForSlot(argIndex: I))); |
| 10323 | return ToContext.Selectors.getSelector(NumArgs: FromSel.getNumArgs(), IIV: Idents.data()); |
| 10324 | } |
| 10325 | |
| 10326 | llvm::Expected<APValue> |
| 10327 | ASTNodeImporter::ImportAPValue(const APValue &FromValue) { |
| 10328 | APValue Result; |
| 10329 | llvm::Error Err = llvm::Error::success(); |
| 10330 | auto ImportLoop = [&](const APValue *From, APValue *To, unsigned Size) { |
| 10331 | for (unsigned Idx = 0; Idx < Size; Idx++) { |
| 10332 | APValue Tmp = importChecked(Err, From: From[Idx]); |
| 10333 | To[Idx] = Tmp; |
| 10334 | } |
| 10335 | }; |
| 10336 | switch (FromValue.getKind()) { |
| 10337 | case APValue::None: |
| 10338 | case APValue::Indeterminate: |
| 10339 | case APValue::Int: |
| 10340 | case APValue::Float: |
| 10341 | case APValue::FixedPoint: |
| 10342 | case APValue::ComplexInt: |
| 10343 | case APValue::ComplexFloat: |
| 10344 | Result = FromValue; |
| 10345 | break; |
| 10346 | case APValue::Vector: { |
| 10347 | Result.MakeVector(); |
| 10348 | MutableArrayRef<APValue> Elts = |
| 10349 | Result.setVectorUninit(FromValue.getVectorLength()); |
| 10350 | ImportLoop(((const APValue::Vec *)(const char *)&FromValue.Data)->Elts, |
| 10351 | Elts.data(), FromValue.getVectorLength()); |
| 10352 | break; |
| 10353 | } |
| 10354 | case APValue::Array: |
| 10355 | Result.MakeArray(InitElts: FromValue.getArrayInitializedElts(), |
| 10356 | Size: FromValue.getArraySize()); |
| 10357 | ImportLoop(((const APValue::Arr *)(const char *)&FromValue.Data)->Elts, |
| 10358 | ((const APValue::Arr *)(const char *)&Result.Data)->Elts, |
| 10359 | FromValue.getArrayInitializedElts()); |
| 10360 | break; |
| 10361 | case APValue::Struct: |
| 10362 | Result.MakeStruct(B: FromValue.getStructNumBases(), |
| 10363 | M: FromValue.getStructNumFields()); |
| 10364 | ImportLoop( |
| 10365 | ((const APValue::StructData *)(const char *)&FromValue.Data)->Elts, |
| 10366 | ((const APValue::StructData *)(const char *)&Result.Data)->Elts, |
| 10367 | FromValue.getStructNumBases() + FromValue.getStructNumFields()); |
| 10368 | break; |
| 10369 | case APValue::Union: { |
| 10370 | Result.MakeUnion(); |
| 10371 | const Decl *ImpFDecl = importChecked(Err, From: FromValue.getUnionField()); |
| 10372 | APValue ImpValue = importChecked(Err, From: FromValue.getUnionValue()); |
| 10373 | if (Err) |
| 10374 | return std::move(Err); |
| 10375 | Result.setUnion(Field: cast<FieldDecl>(Val: ImpFDecl), Value: ImpValue); |
| 10376 | break; |
| 10377 | } |
| 10378 | case APValue::AddrLabelDiff: { |
| 10379 | Result.MakeAddrLabelDiff(); |
| 10380 | const Expr *ImpLHS = importChecked(Err, From: FromValue.getAddrLabelDiffLHS()); |
| 10381 | const Expr *ImpRHS = importChecked(Err, From: FromValue.getAddrLabelDiffRHS()); |
| 10382 | if (Err) |
| 10383 | return std::move(Err); |
| 10384 | Result.setAddrLabelDiff(LHSExpr: cast<AddrLabelExpr>(Val: ImpLHS), |
| 10385 | RHSExpr: cast<AddrLabelExpr>(Val: ImpRHS)); |
| 10386 | break; |
| 10387 | } |
| 10388 | case APValue::MemberPointer: { |
| 10389 | const Decl *ImpMemPtrDecl = |
| 10390 | importChecked(Err, From: FromValue.getMemberPointerDecl()); |
| 10391 | if (Err) |
| 10392 | return std::move(Err); |
| 10393 | MutableArrayRef<const CXXRecordDecl *> ToPath = |
| 10394 | Result.setMemberPointerUninit( |
| 10395 | Member: cast<const ValueDecl>(Val: ImpMemPtrDecl), |
| 10396 | IsDerivedMember: FromValue.isMemberPointerToDerivedMember(), |
| 10397 | Size: FromValue.getMemberPointerPath().size()); |
| 10398 | llvm::ArrayRef<const CXXRecordDecl *> FromPath = |
| 10399 | Result.getMemberPointerPath(); |
| 10400 | for (unsigned Idx = 0; Idx < FromValue.getMemberPointerPath().size(); |
| 10401 | Idx++) { |
| 10402 | const Decl *ImpDecl = importChecked(Err, From: FromPath[Idx]); |
| 10403 | if (Err) |
| 10404 | return std::move(Err); |
| 10405 | ToPath[Idx] = cast<const CXXRecordDecl>(Val: ImpDecl->getCanonicalDecl()); |
| 10406 | } |
| 10407 | break; |
| 10408 | } |
| 10409 | case APValue::LValue: |
| 10410 | APValue::LValueBase Base; |
| 10411 | QualType FromElemTy; |
| 10412 | if (FromValue.getLValueBase()) { |
| 10413 | assert(!FromValue.getLValueBase().is<DynamicAllocLValue>() && |
| 10414 | "in C++20 dynamic allocation are transient so they shouldn't " |
| 10415 | "appear in the AST" ); |
| 10416 | if (!FromValue.getLValueBase().is<TypeInfoLValue>()) { |
| 10417 | if (const auto *E = |
| 10418 | FromValue.getLValueBase().dyn_cast<const Expr *>()) { |
| 10419 | FromElemTy = E->getType(); |
| 10420 | const Expr *ImpExpr = importChecked(Err, From: E); |
| 10421 | if (Err) |
| 10422 | return std::move(Err); |
| 10423 | Base = APValue::LValueBase(ImpExpr, |
| 10424 | FromValue.getLValueBase().getCallIndex(), |
| 10425 | FromValue.getLValueBase().getVersion()); |
| 10426 | } else { |
| 10427 | FromElemTy = |
| 10428 | FromValue.getLValueBase().get<const ValueDecl *>()->getType(); |
| 10429 | const Decl *ImpDecl = importChecked( |
| 10430 | Err, From: FromValue.getLValueBase().get<const ValueDecl *>()); |
| 10431 | if (Err) |
| 10432 | return std::move(Err); |
| 10433 | Base = APValue::LValueBase(cast<ValueDecl>(Val: ImpDecl), |
| 10434 | FromValue.getLValueBase().getCallIndex(), |
| 10435 | FromValue.getLValueBase().getVersion()); |
| 10436 | } |
| 10437 | } else { |
| 10438 | FromElemTy = FromValue.getLValueBase().getTypeInfoType(); |
| 10439 | const Type *ImpTypeInfo = importChecked( |
| 10440 | Err, From: FromValue.getLValueBase().get<TypeInfoLValue>().getType()); |
| 10441 | QualType ImpType = |
| 10442 | importChecked(Err, From: FromValue.getLValueBase().getTypeInfoType()); |
| 10443 | if (Err) |
| 10444 | return std::move(Err); |
| 10445 | Base = APValue::LValueBase::getTypeInfo(LV: TypeInfoLValue(ImpTypeInfo), |
| 10446 | TypeInfo: ImpType); |
| 10447 | } |
| 10448 | } |
| 10449 | CharUnits Offset = FromValue.getLValueOffset(); |
| 10450 | unsigned PathLength = FromValue.getLValuePath().size(); |
| 10451 | Result.MakeLValue(); |
| 10452 | if (FromValue.hasLValuePath()) { |
| 10453 | MutableArrayRef<APValue::LValuePathEntry> ToPath = Result.setLValueUninit( |
| 10454 | B: Base, O: Offset, Size: PathLength, OnePastTheEnd: FromValue.isLValueOnePastTheEnd(), |
| 10455 | IsNullPtr: FromValue.isNullPointer()); |
| 10456 | llvm::ArrayRef<APValue::LValuePathEntry> FromPath = |
| 10457 | FromValue.getLValuePath(); |
| 10458 | for (unsigned LoopIdx = 0; LoopIdx < PathLength; LoopIdx++) { |
| 10459 | if (FromElemTy->isRecordType()) { |
| 10460 | const Decl *FromDecl = |
| 10461 | FromPath[LoopIdx].getAsBaseOrMember().getPointer(); |
| 10462 | const Decl *ImpDecl = importChecked(Err, From: FromDecl); |
| 10463 | if (Err) |
| 10464 | return std::move(Err); |
| 10465 | if (auto *RD = dyn_cast<CXXRecordDecl>(Val: FromDecl)) |
| 10466 | FromElemTy = Importer.FromContext.getRecordType(RD); |
| 10467 | else |
| 10468 | FromElemTy = cast<ValueDecl>(Val: FromDecl)->getType(); |
| 10469 | ToPath[LoopIdx] = APValue::LValuePathEntry(APValue::BaseOrMemberType( |
| 10470 | ImpDecl, FromPath[LoopIdx].getAsBaseOrMember().getInt())); |
| 10471 | } else { |
| 10472 | FromElemTy = |
| 10473 | Importer.FromContext.getAsArrayType(T: FromElemTy)->getElementType(); |
| 10474 | ToPath[LoopIdx] = APValue::LValuePathEntry::ArrayIndex( |
| 10475 | Index: FromPath[LoopIdx].getAsArrayIndex()); |
| 10476 | } |
| 10477 | } |
| 10478 | } else |
| 10479 | Result.setLValue(B: Base, O: Offset, APValue::NoLValuePath{}, |
| 10480 | IsNullPtr: FromValue.isNullPointer()); |
| 10481 | } |
| 10482 | if (Err) |
| 10483 | return std::move(Err); |
| 10484 | return Result; |
| 10485 | } |
| 10486 | |
| 10487 | Expected<DeclarationName> ASTImporter::HandleNameConflict(DeclarationName Name, |
| 10488 | DeclContext *DC, |
| 10489 | unsigned IDNS, |
| 10490 | NamedDecl **Decls, |
| 10491 | unsigned NumDecls) { |
| 10492 | if (ODRHandling == ODRHandlingType::Conservative) |
| 10493 | // Report error at any name conflict. |
| 10494 | return make_error<ASTImportError>(Args: ASTImportError::NameConflict); |
| 10495 | else |
| 10496 | // Allow to create the new Decl with the same name. |
| 10497 | return Name; |
| 10498 | } |
| 10499 | |
| 10500 | DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) { |
| 10501 | if (LastDiagFromFrom) |
| 10502 | ToContext.getDiagnostics().notePriorDiagnosticFrom( |
| 10503 | Other: FromContext.getDiagnostics()); |
| 10504 | LastDiagFromFrom = false; |
| 10505 | return ToContext.getDiagnostics().Report(Loc, DiagID); |
| 10506 | } |
| 10507 | |
| 10508 | DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) { |
| 10509 | if (!LastDiagFromFrom) |
| 10510 | FromContext.getDiagnostics().notePriorDiagnosticFrom( |
| 10511 | Other: ToContext.getDiagnostics()); |
| 10512 | LastDiagFromFrom = true; |
| 10513 | return FromContext.getDiagnostics().Report(Loc, DiagID); |
| 10514 | } |
| 10515 | |
| 10516 | void ASTImporter::CompleteDecl (Decl *D) { |
| 10517 | if (auto *ID = dyn_cast<ObjCInterfaceDecl>(Val: D)) { |
| 10518 | if (!ID->getDefinition()) |
| 10519 | ID->startDefinition(); |
| 10520 | } |
| 10521 | else if (auto *PD = dyn_cast<ObjCProtocolDecl>(Val: D)) { |
| 10522 | if (!PD->getDefinition()) |
| 10523 | PD->startDefinition(); |
| 10524 | } |
| 10525 | else if (auto *TD = dyn_cast<TagDecl>(Val: D)) { |
| 10526 | if (!TD->getDefinition() && !TD->isBeingDefined()) { |
| 10527 | TD->startDefinition(); |
| 10528 | TD->setCompleteDefinition(true); |
| 10529 | } |
| 10530 | } |
| 10531 | else { |
| 10532 | assert(0 && "CompleteDecl called on a Decl that can't be completed" ); |
| 10533 | } |
| 10534 | } |
| 10535 | |
| 10536 | Decl *ASTImporter::MapImported(Decl *From, Decl *To) { |
| 10537 | auto [Pos, Inserted] = ImportedDecls.try_emplace(Key: From, Args&: To); |
| 10538 | assert((Inserted || Pos->second == To) && |
| 10539 | "Try to import an already imported Decl" ); |
| 10540 | if (!Inserted) |
| 10541 | return Pos->second; |
| 10542 | // This mapping should be maintained only in this function. Therefore do not |
| 10543 | // check for additional consistency. |
| 10544 | ImportedFromDecls[To] = From; |
| 10545 | // In the case of TypedefNameDecl we create the Decl first and only then we |
| 10546 | // import and set its DeclContext. So, the DC is still not set when we reach |
| 10547 | // here from GetImportedOrCreateDecl. |
| 10548 | if (To->getDeclContext()) |
| 10549 | AddToLookupTable(ToD: To); |
| 10550 | return To; |
| 10551 | } |
| 10552 | |
| 10553 | std::optional<ASTImportError> |
| 10554 | ASTImporter::getImportDeclErrorIfAny(Decl *FromD) const { |
| 10555 | auto Pos = ImportDeclErrors.find(Val: FromD); |
| 10556 | if (Pos != ImportDeclErrors.end()) |
| 10557 | return Pos->second; |
| 10558 | else |
| 10559 | return std::nullopt; |
| 10560 | } |
| 10561 | |
| 10562 | void ASTImporter::setImportDeclError(Decl *From, ASTImportError Error) { |
| 10563 | auto InsertRes = ImportDeclErrors.insert(KV: {From, Error}); |
| 10564 | (void)InsertRes; |
| 10565 | // Either we set the error for the first time, or we already had set one and |
| 10566 | // now we want to set the same error. |
| 10567 | assert(InsertRes.second || InsertRes.first->second.Error == Error.Error); |
| 10568 | } |
| 10569 | |
| 10570 | bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To, |
| 10571 | bool Complain) { |
| 10572 | llvm::DenseMap<const Type *, const Type *>::iterator Pos = |
| 10573 | ImportedTypes.find(Val: From.getTypePtr()); |
| 10574 | if (Pos != ImportedTypes.end()) { |
| 10575 | if (ExpectedType ToFromOrErr = Import(FromT: From)) { |
| 10576 | if (ToContext.hasSameType(T1: *ToFromOrErr, T2: To)) |
| 10577 | return true; |
| 10578 | } else { |
| 10579 | llvm::consumeError(Err: ToFromOrErr.takeError()); |
| 10580 | } |
| 10581 | } |
| 10582 | |
| 10583 | StructuralEquivalenceContext Ctx( |
| 10584 | getToContext().getLangOpts(), FromContext, ToContext, NonEquivalentDecls, |
| 10585 | getStructuralEquivalenceKind(Importer: *this), false, Complain); |
| 10586 | return Ctx.IsEquivalent(T1: From, T2: To); |
| 10587 | } |
| 10588 | |