1 //===- ASTStructuralEquivalence.cpp ---------------------------------------===// 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 implement StructuralEquivalenceContext class and helper functions 10 // for layout matching. 11 // 12 // The structural equivalence check could have been implemented as a parallel 13 // BFS on a pair of graphs. That must have been the original approach at the 14 // beginning. 15 // Let's consider this simple BFS algorithm from the `s` source: 16 // ``` 17 // void bfs(Graph G, int s) 18 // { 19 // Queue<Integer> queue = new Queue<Integer>(); 20 // marked[s] = true; // Mark the source 21 // queue.enqueue(s); // and put it on the queue. 22 // while (!q.isEmpty()) { 23 // int v = queue.dequeue(); // Remove next vertex from the queue. 24 // for (int w : G.adj(v)) 25 // if (!marked[w]) // For every unmarked adjacent vertex, 26 // { 27 // marked[w] = true; 28 // queue.enqueue(w); 29 // } 30 // } 31 // } 32 // ``` 33 // Indeed, it has it's queue, which holds pairs of nodes, one from each graph, 34 // this is the `DeclsToCheck` member. `VisitedDecls` plays the role of the 35 // marking (`marked`) functionality above, we use it to check whether we've 36 // already seen a pair of nodes. 37 // 38 // We put in the elements into the queue only in the toplevel decl check 39 // function: 40 // ``` 41 // static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 42 // Decl *D1, Decl *D2); 43 // ``` 44 // The `while` loop where we iterate over the children is implemented in 45 // `Finish()`. And `Finish` is called only from the two **member** functions 46 // which check the equivalency of two Decls or two Types. ASTImporter (and 47 // other clients) call only these functions. 48 // 49 // The `static` implementation functions are called from `Finish`, these push 50 // the children nodes to the queue via `static bool 51 // IsStructurallyEquivalent(StructuralEquivalenceContext &Context, Decl *D1, 52 // Decl *D2)`. So far so good, this is almost like the BFS. However, if we 53 // let a static implementation function to call `Finish` via another **member** 54 // function that means we end up with two nested while loops each of them 55 // working on the same queue. This is wrong and nobody can reason about it's 56 // doing. Thus, static implementation functions must not call the **member** 57 // functions. 58 // 59 //===----------------------------------------------------------------------===// 60 61 #include "clang/AST/ASTStructuralEquivalence.h" 62 #include "clang/AST/ASTContext.h" 63 #include "clang/AST/ASTDiagnostic.h" 64 #include "clang/AST/Decl.h" 65 #include "clang/AST/DeclBase.h" 66 #include "clang/AST/DeclCXX.h" 67 #include "clang/AST/DeclFriend.h" 68 #include "clang/AST/DeclObjC.h" 69 #include "clang/AST/DeclOpenMP.h" 70 #include "clang/AST/DeclTemplate.h" 71 #include "clang/AST/ExprCXX.h" 72 #include "clang/AST/ExprConcepts.h" 73 #include "clang/AST/ExprObjC.h" 74 #include "clang/AST/ExprOpenMP.h" 75 #include "clang/AST/NestedNameSpecifier.h" 76 #include "clang/AST/StmtObjC.h" 77 #include "clang/AST/StmtOpenMP.h" 78 #include "clang/AST/TemplateBase.h" 79 #include "clang/AST/TemplateName.h" 80 #include "clang/AST/Type.h" 81 #include "clang/Basic/ExceptionSpecificationType.h" 82 #include "clang/Basic/IdentifierTable.h" 83 #include "clang/Basic/LLVM.h" 84 #include "clang/Basic/SourceLocation.h" 85 #include "llvm/ADT/APInt.h" 86 #include "llvm/ADT/APSInt.h" 87 #include "llvm/ADT/StringExtras.h" 88 #include "llvm/Support/Casting.h" 89 #include "llvm/Support/Compiler.h" 90 #include "llvm/Support/ErrorHandling.h" 91 #include <cassert> 92 #include <optional> 93 #include <utility> 94 95 using namespace clang; 96 97 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 98 QualType T1, QualType T2); 99 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 100 Decl *D1, Decl *D2); 101 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 102 const TemplateArgument &Arg1, 103 const TemplateArgument &Arg2); 104 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 105 const TemplateArgumentLoc &Arg1, 106 const TemplateArgumentLoc &Arg2); 107 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 108 NestedNameSpecifier *NNS1, 109 NestedNameSpecifier *NNS2); 110 static bool IsStructurallyEquivalent(const IdentifierInfo *Name1, 111 const IdentifierInfo *Name2); 112 113 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 114 const DeclarationName Name1, 115 const DeclarationName Name2) { 116 if (Name1.getNameKind() != Name2.getNameKind()) 117 return false; 118 119 switch (Name1.getNameKind()) { 120 121 case DeclarationName::Identifier: 122 return IsStructurallyEquivalent(Name1.getAsIdentifierInfo(), 123 Name2.getAsIdentifierInfo()); 124 125 case DeclarationName::CXXConstructorName: 126 case DeclarationName::CXXDestructorName: 127 case DeclarationName::CXXConversionFunctionName: 128 return IsStructurallyEquivalent(Context, Name1.getCXXNameType(), 129 Name2.getCXXNameType()); 130 131 case DeclarationName::CXXDeductionGuideName: { 132 if (!IsStructurallyEquivalent( 133 Context, Name1.getCXXDeductionGuideTemplate()->getDeclName(), 134 Name2.getCXXDeductionGuideTemplate()->getDeclName())) 135 return false; 136 return IsStructurallyEquivalent(Context, 137 Name1.getCXXDeductionGuideTemplate(), 138 Name2.getCXXDeductionGuideTemplate()); 139 } 140 141 case DeclarationName::CXXOperatorName: 142 return Name1.getCXXOverloadedOperator() == Name2.getCXXOverloadedOperator(); 143 144 case DeclarationName::CXXLiteralOperatorName: 145 return IsStructurallyEquivalent(Name1.getCXXLiteralIdentifier(), 146 Name2.getCXXLiteralIdentifier()); 147 148 case DeclarationName::CXXUsingDirective: 149 return true; // FIXME When do we consider two using directives equal? 150 151 case DeclarationName::ObjCZeroArgSelector: 152 case DeclarationName::ObjCOneArgSelector: 153 case DeclarationName::ObjCMultiArgSelector: 154 return true; // FIXME 155 } 156 157 llvm_unreachable("Unhandled kind of DeclarationName"); 158 return true; 159 } 160 161 namespace { 162 /// Encapsulates Stmt comparison logic. 163 class StmtComparer { 164 StructuralEquivalenceContext &Context; 165 166 // IsStmtEquivalent overloads. Each overload compares a specific statement 167 // and only has to compare the data that is specific to the specific statement 168 // class. Should only be called from TraverseStmt. 169 170 bool IsStmtEquivalent(const AddrLabelExpr *E1, const AddrLabelExpr *E2) { 171 return IsStructurallyEquivalent(Context, E1->getLabel(), E2->getLabel()); 172 } 173 174 bool IsStmtEquivalent(const AtomicExpr *E1, const AtomicExpr *E2) { 175 return E1->getOp() == E2->getOp(); 176 } 177 178 bool IsStmtEquivalent(const BinaryOperator *E1, const BinaryOperator *E2) { 179 return E1->getOpcode() == E2->getOpcode(); 180 } 181 182 bool IsStmtEquivalent(const CallExpr *E1, const CallExpr *E2) { 183 // FIXME: IsStructurallyEquivalent requires non-const Decls. 184 Decl *Callee1 = const_cast<Decl *>(E1->getCalleeDecl()); 185 Decl *Callee2 = const_cast<Decl *>(E2->getCalleeDecl()); 186 187 // Compare whether both calls know their callee. 188 if (static_cast<bool>(Callee1) != static_cast<bool>(Callee2)) 189 return false; 190 191 // Both calls have no callee, so nothing to do. 192 if (!static_cast<bool>(Callee1)) 193 return true; 194 195 assert(Callee2); 196 return IsStructurallyEquivalent(Context, Callee1, Callee2); 197 } 198 199 bool IsStmtEquivalent(const CharacterLiteral *E1, 200 const CharacterLiteral *E2) { 201 return E1->getValue() == E2->getValue() && E1->getKind() == E2->getKind(); 202 } 203 204 bool IsStmtEquivalent(const ChooseExpr *E1, const ChooseExpr *E2) { 205 return true; // Semantics only depend on children. 206 } 207 208 bool IsStmtEquivalent(const CompoundStmt *E1, const CompoundStmt *E2) { 209 // Number of children is actually checked by the generic children comparison 210 // code, but a CompoundStmt is one of the few statements where the number of 211 // children frequently differs and the number of statements is also always 212 // precomputed. Directly comparing the number of children here is thus 213 // just an optimization. 214 return E1->size() == E2->size(); 215 } 216 217 bool IsStmtEquivalent(const DeclRefExpr *DRE1, const DeclRefExpr *DRE2) { 218 const ValueDecl *Decl1 = DRE1->getDecl(); 219 const ValueDecl *Decl2 = DRE2->getDecl(); 220 if (!Decl1 || !Decl2) 221 return false; 222 return IsStructurallyEquivalent(Context, const_cast<ValueDecl *>(Decl1), 223 const_cast<ValueDecl *>(Decl2)); 224 } 225 226 bool IsStmtEquivalent(const DependentScopeDeclRefExpr *DE1, 227 const DependentScopeDeclRefExpr *DE2) { 228 if (!IsStructurallyEquivalent(Context, DE1->getDeclName(), 229 DE2->getDeclName())) 230 return false; 231 return IsStructurallyEquivalent(Context, DE1->getQualifier(), 232 DE2->getQualifier()); 233 } 234 235 bool IsStmtEquivalent(const Expr *E1, const Expr *E2) { 236 return IsStructurallyEquivalent(Context, E1->getType(), E2->getType()); 237 } 238 239 bool IsStmtEquivalent(const ExpressionTraitExpr *E1, 240 const ExpressionTraitExpr *E2) { 241 return E1->getTrait() == E2->getTrait() && E1->getValue() == E2->getValue(); 242 } 243 244 bool IsStmtEquivalent(const FloatingLiteral *E1, const FloatingLiteral *E2) { 245 return E1->isExact() == E2->isExact() && E1->getValue() == E2->getValue(); 246 } 247 248 bool IsStmtEquivalent(const GenericSelectionExpr *E1, 249 const GenericSelectionExpr *E2) { 250 for (auto Pair : zip_longest(E1->getAssocTypeSourceInfos(), 251 E2->getAssocTypeSourceInfos())) { 252 std::optional<TypeSourceInfo *> Child1 = std::get<0>(Pair); 253 std::optional<TypeSourceInfo *> Child2 = std::get<1>(Pair); 254 // Skip this case if there are a different number of associated types. 255 if (!Child1 || !Child2) 256 return false; 257 258 if (!IsStructurallyEquivalent(Context, (*Child1)->getType(), 259 (*Child2)->getType())) 260 return false; 261 } 262 263 return true; 264 } 265 266 bool IsStmtEquivalent(const ImplicitCastExpr *CastE1, 267 const ImplicitCastExpr *CastE2) { 268 return IsStructurallyEquivalent(Context, CastE1->getType(), 269 CastE2->getType()); 270 } 271 272 bool IsStmtEquivalent(const IntegerLiteral *E1, const IntegerLiteral *E2) { 273 return E1->getValue() == E2->getValue(); 274 } 275 276 bool IsStmtEquivalent(const MemberExpr *E1, const MemberExpr *E2) { 277 return IsStructurallyEquivalent(Context, E1->getFoundDecl(), 278 E2->getFoundDecl()); 279 } 280 281 bool IsStmtEquivalent(const ObjCStringLiteral *E1, 282 const ObjCStringLiteral *E2) { 283 // Just wraps a StringLiteral child. 284 return true; 285 } 286 287 bool IsStmtEquivalent(const Stmt *S1, const Stmt *S2) { return true; } 288 289 bool IsStmtEquivalent(const GotoStmt *S1, const GotoStmt *S2) { 290 LabelDecl *L1 = S1->getLabel(); 291 LabelDecl *L2 = S2->getLabel(); 292 if (!L1 || !L2) 293 return L1 == L2; 294 295 IdentifierInfo *Name1 = L1->getIdentifier(); 296 IdentifierInfo *Name2 = L2->getIdentifier(); 297 return ::IsStructurallyEquivalent(Name1, Name2); 298 } 299 300 bool IsStmtEquivalent(const SourceLocExpr *E1, const SourceLocExpr *E2) { 301 return E1->getIdentKind() == E2->getIdentKind(); 302 } 303 304 bool IsStmtEquivalent(const StmtExpr *E1, const StmtExpr *E2) { 305 return E1->getTemplateDepth() == E2->getTemplateDepth(); 306 } 307 308 bool IsStmtEquivalent(const StringLiteral *E1, const StringLiteral *E2) { 309 return E1->getBytes() == E2->getBytes(); 310 } 311 312 bool IsStmtEquivalent(const SubstNonTypeTemplateParmExpr *E1, 313 const SubstNonTypeTemplateParmExpr *E2) { 314 if (!IsStructurallyEquivalent(Context, E1->getAssociatedDecl(), 315 E2->getAssociatedDecl())) 316 return false; 317 if (E1->getIndex() != E2->getIndex()) 318 return false; 319 if (E1->getPackIndex() != E2->getPackIndex()) 320 return false; 321 return true; 322 } 323 324 bool IsStmtEquivalent(const SubstNonTypeTemplateParmPackExpr *E1, 325 const SubstNonTypeTemplateParmPackExpr *E2) { 326 return IsStructurallyEquivalent(Context, E1->getArgumentPack(), 327 E2->getArgumentPack()); 328 } 329 330 bool IsStmtEquivalent(const TypeTraitExpr *E1, const TypeTraitExpr *E2) { 331 if (E1->getTrait() != E2->getTrait()) 332 return false; 333 334 for (auto Pair : zip_longest(E1->getArgs(), E2->getArgs())) { 335 std::optional<TypeSourceInfo *> Child1 = std::get<0>(Pair); 336 std::optional<TypeSourceInfo *> Child2 = std::get<1>(Pair); 337 // Different number of args. 338 if (!Child1 || !Child2) 339 return false; 340 341 if (!IsStructurallyEquivalent(Context, (*Child1)->getType(), 342 (*Child2)->getType())) 343 return false; 344 } 345 return true; 346 } 347 348 bool IsStmtEquivalent(const UnaryExprOrTypeTraitExpr *E1, 349 const UnaryExprOrTypeTraitExpr *E2) { 350 if (E1->getKind() != E2->getKind()) 351 return false; 352 return IsStructurallyEquivalent(Context, E1->getTypeOfArgument(), 353 E2->getTypeOfArgument()); 354 } 355 356 bool IsStmtEquivalent(const UnaryOperator *E1, const UnaryOperator *E2) { 357 return E1->getOpcode() == E2->getOpcode(); 358 } 359 360 bool IsStmtEquivalent(const VAArgExpr *E1, const VAArgExpr *E2) { 361 // Semantics only depend on children. 362 return true; 363 } 364 365 bool IsStmtEquivalent(const OverloadExpr *E1, const OverloadExpr *E2) { 366 if (!IsStructurallyEquivalent(Context, E1->getName(), E2->getName())) 367 return false; 368 369 if (static_cast<bool>(E1->getQualifier()) != 370 static_cast<bool>(E2->getQualifier())) 371 return false; 372 if (E1->getQualifier() && 373 !IsStructurallyEquivalent(Context, E1->getQualifier(), 374 E2->getQualifier())) 375 return false; 376 377 if (E1->getNumTemplateArgs() != E2->getNumTemplateArgs()) 378 return false; 379 const TemplateArgumentLoc *Args1 = E1->getTemplateArgs(); 380 const TemplateArgumentLoc *Args2 = E2->getTemplateArgs(); 381 for (unsigned int ArgI = 0, ArgN = E1->getNumTemplateArgs(); ArgI < ArgN; 382 ++ArgI) 383 if (!IsStructurallyEquivalent(Context, Args1[ArgI], Args2[ArgI])) 384 return false; 385 386 return true; 387 } 388 389 bool IsStmtEquivalent(const CXXBoolLiteralExpr *E1, const CXXBoolLiteralExpr *E2) { 390 return E1->getValue() == E2->getValue(); 391 } 392 393 /// End point of the traversal chain. 394 bool TraverseStmt(const Stmt *S1, const Stmt *S2) { return true; } 395 396 // Create traversal methods that traverse the class hierarchy and return 397 // the accumulated result of the comparison. Each TraverseStmt overload 398 // calls the TraverseStmt overload of the parent class. For example, 399 // the TraverseStmt overload for 'BinaryOperator' calls the TraverseStmt 400 // overload of 'Expr' which then calls the overload for 'Stmt'. 401 #define STMT(CLASS, PARENT) \ 402 bool TraverseStmt(const CLASS *S1, const CLASS *S2) { \ 403 if (!TraverseStmt(static_cast<const PARENT *>(S1), \ 404 static_cast<const PARENT *>(S2))) \ 405 return false; \ 406 return IsStmtEquivalent(S1, S2); \ 407 } 408 #include "clang/AST/StmtNodes.inc" 409 410 public: 411 StmtComparer(StructuralEquivalenceContext &C) : Context(C) {} 412 413 /// Determine whether two statements are equivalent. The statements have to 414 /// be of the same kind. The children of the statements and their properties 415 /// are not compared by this function. 416 bool IsEquivalent(const Stmt *S1, const Stmt *S2) { 417 if (S1->getStmtClass() != S2->getStmtClass()) 418 return false; 419 420 // Each TraverseStmt walks the class hierarchy from the leaf class to 421 // the root class 'Stmt' (e.g. 'BinaryOperator' -> 'Expr' -> 'Stmt'). Cast 422 // the Stmt we have here to its specific subclass so that we call the 423 // overload that walks the whole class hierarchy from leaf to root (e.g., 424 // cast to 'BinaryOperator' so that 'Expr' and 'Stmt' is traversed). 425 switch (S1->getStmtClass()) { 426 case Stmt::NoStmtClass: 427 llvm_unreachable("Can't traverse NoStmtClass"); 428 #define STMT(CLASS, PARENT) \ 429 case Stmt::StmtClass::CLASS##Class: \ 430 return TraverseStmt(static_cast<const CLASS *>(S1), \ 431 static_cast<const CLASS *>(S2)); 432 #define ABSTRACT_STMT(S) 433 #include "clang/AST/StmtNodes.inc" 434 } 435 llvm_unreachable("Invalid statement kind"); 436 } 437 }; 438 } // namespace 439 440 /// Determine structural equivalence of two statements. 441 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 442 const Stmt *S1, const Stmt *S2) { 443 if (!S1 || !S2) 444 return S1 == S2; 445 446 // Compare the statements itself. 447 StmtComparer Comparer(Context); 448 if (!Comparer.IsEquivalent(S1, S2)) 449 return false; 450 451 // Iterate over the children of both statements and also compare them. 452 for (auto Pair : zip_longest(S1->children(), S2->children())) { 453 std::optional<const Stmt *> Child1 = std::get<0>(Pair); 454 std::optional<const Stmt *> Child2 = std::get<1>(Pair); 455 // One of the statements has a different amount of children than the other, 456 // so the statements can't be equivalent. 457 if (!Child1 || !Child2) 458 return false; 459 if (!IsStructurallyEquivalent(Context, *Child1, *Child2)) 460 return false; 461 } 462 return true; 463 } 464 465 /// Determine whether two identifiers are equivalent. 466 static bool IsStructurallyEquivalent(const IdentifierInfo *Name1, 467 const IdentifierInfo *Name2) { 468 if (!Name1 || !Name2) 469 return Name1 == Name2; 470 471 return Name1->getName() == Name2->getName(); 472 } 473 474 /// Determine whether two nested-name-specifiers are equivalent. 475 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 476 NestedNameSpecifier *NNS1, 477 NestedNameSpecifier *NNS2) { 478 if (NNS1->getKind() != NNS2->getKind()) 479 return false; 480 481 NestedNameSpecifier *Prefix1 = NNS1->getPrefix(), 482 *Prefix2 = NNS2->getPrefix(); 483 if ((bool)Prefix1 != (bool)Prefix2) 484 return false; 485 486 if (Prefix1) 487 if (!IsStructurallyEquivalent(Context, Prefix1, Prefix2)) 488 return false; 489 490 switch (NNS1->getKind()) { 491 case NestedNameSpecifier::Identifier: 492 return IsStructurallyEquivalent(NNS1->getAsIdentifier(), 493 NNS2->getAsIdentifier()); 494 case NestedNameSpecifier::Namespace: 495 return IsStructurallyEquivalent(Context, NNS1->getAsNamespace(), 496 NNS2->getAsNamespace()); 497 case NestedNameSpecifier::NamespaceAlias: 498 return IsStructurallyEquivalent(Context, NNS1->getAsNamespaceAlias(), 499 NNS2->getAsNamespaceAlias()); 500 case NestedNameSpecifier::TypeSpec: 501 case NestedNameSpecifier::TypeSpecWithTemplate: 502 return IsStructurallyEquivalent(Context, QualType(NNS1->getAsType(), 0), 503 QualType(NNS2->getAsType(), 0)); 504 case NestedNameSpecifier::Global: 505 return true; 506 case NestedNameSpecifier::Super: 507 return IsStructurallyEquivalent(Context, NNS1->getAsRecordDecl(), 508 NNS2->getAsRecordDecl()); 509 } 510 return false; 511 } 512 513 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 514 const TemplateName &N1, 515 const TemplateName &N2) { 516 TemplateDecl *TemplateDeclN1 = N1.getAsTemplateDecl(); 517 TemplateDecl *TemplateDeclN2 = N2.getAsTemplateDecl(); 518 if (TemplateDeclN1 && TemplateDeclN2) { 519 if (!IsStructurallyEquivalent(Context, TemplateDeclN1, TemplateDeclN2)) 520 return false; 521 // If the kind is different we compare only the template decl. 522 if (N1.getKind() != N2.getKind()) 523 return true; 524 } else if (TemplateDeclN1 || TemplateDeclN2) 525 return false; 526 else if (N1.getKind() != N2.getKind()) 527 return false; 528 529 // Check for special case incompatibilities. 530 switch (N1.getKind()) { 531 532 case TemplateName::OverloadedTemplate: { 533 OverloadedTemplateStorage *OS1 = N1.getAsOverloadedTemplate(), 534 *OS2 = N2.getAsOverloadedTemplate(); 535 OverloadedTemplateStorage::iterator I1 = OS1->begin(), I2 = OS2->begin(), 536 E1 = OS1->end(), E2 = OS2->end(); 537 for (; I1 != E1 && I2 != E2; ++I1, ++I2) 538 if (!IsStructurallyEquivalent(Context, *I1, *I2)) 539 return false; 540 return I1 == E1 && I2 == E2; 541 } 542 543 case TemplateName::AssumedTemplate: { 544 AssumedTemplateStorage *TN1 = N1.getAsAssumedTemplateName(), 545 *TN2 = N1.getAsAssumedTemplateName(); 546 return TN1->getDeclName() == TN2->getDeclName(); 547 } 548 549 case TemplateName::DependentTemplate: { 550 DependentTemplateName *DN1 = N1.getAsDependentTemplateName(), 551 *DN2 = N2.getAsDependentTemplateName(); 552 if (!IsStructurallyEquivalent(Context, DN1->getQualifier(), 553 DN2->getQualifier())) 554 return false; 555 if (DN1->isIdentifier() && DN2->isIdentifier()) 556 return IsStructurallyEquivalent(DN1->getIdentifier(), 557 DN2->getIdentifier()); 558 else if (DN1->isOverloadedOperator() && DN2->isOverloadedOperator()) 559 return DN1->getOperator() == DN2->getOperator(); 560 return false; 561 } 562 563 case TemplateName::SubstTemplateTemplateParmPack: { 564 SubstTemplateTemplateParmPackStorage 565 *P1 = N1.getAsSubstTemplateTemplateParmPack(), 566 *P2 = N2.getAsSubstTemplateTemplateParmPack(); 567 return IsStructurallyEquivalent(Context, P1->getArgumentPack(), 568 P2->getArgumentPack()) && 569 IsStructurallyEquivalent(Context, P1->getAssociatedDecl(), 570 P2->getAssociatedDecl()) && 571 P1->getIndex() == P2->getIndex(); 572 } 573 574 case TemplateName::Template: 575 case TemplateName::QualifiedTemplate: 576 case TemplateName::SubstTemplateTemplateParm: 577 case TemplateName::UsingTemplate: 578 // It is sufficient to check value of getAsTemplateDecl. 579 break; 580 581 } 582 583 return true; 584 } 585 586 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 587 ArrayRef<TemplateArgument> Args1, 588 ArrayRef<TemplateArgument> Args2); 589 590 /// Determine whether two template arguments are equivalent. 591 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 592 const TemplateArgument &Arg1, 593 const TemplateArgument &Arg2) { 594 if (Arg1.getKind() != Arg2.getKind()) 595 return false; 596 597 switch (Arg1.getKind()) { 598 case TemplateArgument::Null: 599 return true; 600 601 case TemplateArgument::Type: 602 return IsStructurallyEquivalent(Context, Arg1.getAsType(), Arg2.getAsType()); 603 604 case TemplateArgument::Integral: 605 if (!IsStructurallyEquivalent(Context, Arg1.getIntegralType(), 606 Arg2.getIntegralType())) 607 return false; 608 609 return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), 610 Arg2.getAsIntegral()); 611 612 case TemplateArgument::Declaration: 613 return IsStructurallyEquivalent(Context, Arg1.getAsDecl(), Arg2.getAsDecl()); 614 615 case TemplateArgument::NullPtr: 616 return true; // FIXME: Is this correct? 617 618 case TemplateArgument::Template: 619 return IsStructurallyEquivalent(Context, Arg1.getAsTemplate(), 620 Arg2.getAsTemplate()); 621 622 case TemplateArgument::TemplateExpansion: 623 return IsStructurallyEquivalent(Context, 624 Arg1.getAsTemplateOrTemplatePattern(), 625 Arg2.getAsTemplateOrTemplatePattern()); 626 627 case TemplateArgument::Expression: 628 return IsStructurallyEquivalent(Context, Arg1.getAsExpr(), 629 Arg2.getAsExpr()); 630 631 case TemplateArgument::Pack: 632 return IsStructurallyEquivalent(Context, Arg1.pack_elements(), 633 Arg2.pack_elements()); 634 } 635 636 llvm_unreachable("Invalid template argument kind"); 637 } 638 639 /// Determine structural equivalence of two template argument lists. 640 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 641 ArrayRef<TemplateArgument> Args1, 642 ArrayRef<TemplateArgument> Args2) { 643 if (Args1.size() != Args2.size()) 644 return false; 645 for (unsigned I = 0, N = Args1.size(); I != N; ++I) { 646 if (!IsStructurallyEquivalent(Context, Args1[I], Args2[I])) 647 return false; 648 } 649 return true; 650 } 651 652 /// Determine whether two template argument locations are equivalent. 653 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 654 const TemplateArgumentLoc &Arg1, 655 const TemplateArgumentLoc &Arg2) { 656 return IsStructurallyEquivalent(Context, Arg1.getArgument(), 657 Arg2.getArgument()); 658 } 659 660 /// Determine structural equivalence for the common part of array 661 /// types. 662 static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context, 663 const ArrayType *Array1, 664 const ArrayType *Array2) { 665 if (!IsStructurallyEquivalent(Context, Array1->getElementType(), 666 Array2->getElementType())) 667 return false; 668 if (Array1->getSizeModifier() != Array2->getSizeModifier()) 669 return false; 670 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers()) 671 return false; 672 673 return true; 674 } 675 676 /// Determine structural equivalence based on the ExtInfo of functions. This 677 /// is inspired by ASTContext::mergeFunctionTypes(), we compare calling 678 /// conventions bits but must not compare some other bits. 679 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 680 FunctionType::ExtInfo EI1, 681 FunctionType::ExtInfo EI2) { 682 // Compatible functions must have compatible calling conventions. 683 if (EI1.getCC() != EI2.getCC()) 684 return false; 685 686 // Regparm is part of the calling convention. 687 if (EI1.getHasRegParm() != EI2.getHasRegParm()) 688 return false; 689 if (EI1.getRegParm() != EI2.getRegParm()) 690 return false; 691 692 if (EI1.getProducesResult() != EI2.getProducesResult()) 693 return false; 694 if (EI1.getNoCallerSavedRegs() != EI2.getNoCallerSavedRegs()) 695 return false; 696 if (EI1.getNoCfCheck() != EI2.getNoCfCheck()) 697 return false; 698 699 return true; 700 } 701 702 /// Check the equivalence of exception specifications. 703 static bool IsEquivalentExceptionSpec(StructuralEquivalenceContext &Context, 704 const FunctionProtoType *Proto1, 705 const FunctionProtoType *Proto2) { 706 707 auto Spec1 = Proto1->getExceptionSpecType(); 708 auto Spec2 = Proto2->getExceptionSpecType(); 709 710 if (isUnresolvedExceptionSpec(Spec1) || isUnresolvedExceptionSpec(Spec2)) 711 return true; 712 713 if (Spec1 != Spec2) 714 return false; 715 if (Spec1 == EST_Dynamic) { 716 if (Proto1->getNumExceptions() != Proto2->getNumExceptions()) 717 return false; 718 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) { 719 if (!IsStructurallyEquivalent(Context, Proto1->getExceptionType(I), 720 Proto2->getExceptionType(I))) 721 return false; 722 } 723 } else if (isComputedNoexcept(Spec1)) { 724 if (!IsStructurallyEquivalent(Context, Proto1->getNoexceptExpr(), 725 Proto2->getNoexceptExpr())) 726 return false; 727 } 728 729 return true; 730 } 731 732 /// Determine structural equivalence of two types. 733 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 734 QualType T1, QualType T2) { 735 if (T1.isNull() || T2.isNull()) 736 return T1.isNull() && T2.isNull(); 737 738 QualType OrigT1 = T1; 739 QualType OrigT2 = T2; 740 741 if (!Context.StrictTypeSpelling) { 742 // We aren't being strict about token-to-token equivalence of types, 743 // so map down to the canonical type. 744 T1 = Context.FromCtx.getCanonicalType(T1); 745 T2 = Context.ToCtx.getCanonicalType(T2); 746 } 747 748 if (T1.getQualifiers() != T2.getQualifiers()) 749 return false; 750 751 Type::TypeClass TC = T1->getTypeClass(); 752 753 if (T1->getTypeClass() != T2->getTypeClass()) { 754 // Compare function types with prototypes vs. without prototypes as if 755 // both did not have prototypes. 756 if (T1->getTypeClass() == Type::FunctionProto && 757 T2->getTypeClass() == Type::FunctionNoProto) 758 TC = Type::FunctionNoProto; 759 else if (T1->getTypeClass() == Type::FunctionNoProto && 760 T2->getTypeClass() == Type::FunctionProto) 761 TC = Type::FunctionNoProto; 762 else 763 return false; 764 } 765 766 switch (TC) { 767 case Type::Builtin: 768 // FIXME: Deal with Char_S/Char_U. 769 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind()) 770 return false; 771 break; 772 773 case Type::Complex: 774 if (!IsStructurallyEquivalent(Context, 775 cast<ComplexType>(T1)->getElementType(), 776 cast<ComplexType>(T2)->getElementType())) 777 return false; 778 break; 779 780 case Type::Adjusted: 781 case Type::Decayed: 782 if (!IsStructurallyEquivalent(Context, 783 cast<AdjustedType>(T1)->getOriginalType(), 784 cast<AdjustedType>(T2)->getOriginalType())) 785 return false; 786 break; 787 788 case Type::Pointer: 789 if (!IsStructurallyEquivalent(Context, 790 cast<PointerType>(T1)->getPointeeType(), 791 cast<PointerType>(T2)->getPointeeType())) 792 return false; 793 break; 794 795 case Type::BlockPointer: 796 if (!IsStructurallyEquivalent(Context, 797 cast<BlockPointerType>(T1)->getPointeeType(), 798 cast<BlockPointerType>(T2)->getPointeeType())) 799 return false; 800 break; 801 802 case Type::LValueReference: 803 case Type::RValueReference: { 804 const auto *Ref1 = cast<ReferenceType>(T1); 805 const auto *Ref2 = cast<ReferenceType>(T2); 806 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue()) 807 return false; 808 if (Ref1->isInnerRef() != Ref2->isInnerRef()) 809 return false; 810 if (!IsStructurallyEquivalent(Context, Ref1->getPointeeTypeAsWritten(), 811 Ref2->getPointeeTypeAsWritten())) 812 return false; 813 break; 814 } 815 816 case Type::MemberPointer: { 817 const auto *MemPtr1 = cast<MemberPointerType>(T1); 818 const auto *MemPtr2 = cast<MemberPointerType>(T2); 819 if (!IsStructurallyEquivalent(Context, MemPtr1->getPointeeType(), 820 MemPtr2->getPointeeType())) 821 return false; 822 if (!IsStructurallyEquivalent(Context, QualType(MemPtr1->getClass(), 0), 823 QualType(MemPtr2->getClass(), 0))) 824 return false; 825 break; 826 } 827 828 case Type::ConstantArray: { 829 const auto *Array1 = cast<ConstantArrayType>(T1); 830 const auto *Array2 = cast<ConstantArrayType>(T2); 831 if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize())) 832 return false; 833 834 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) 835 return false; 836 break; 837 } 838 839 case Type::IncompleteArray: 840 if (!IsArrayStructurallyEquivalent(Context, cast<ArrayType>(T1), 841 cast<ArrayType>(T2))) 842 return false; 843 break; 844 845 case Type::VariableArray: { 846 const auto *Array1 = cast<VariableArrayType>(T1); 847 const auto *Array2 = cast<VariableArrayType>(T2); 848 if (!IsStructurallyEquivalent(Context, Array1->getSizeExpr(), 849 Array2->getSizeExpr())) 850 return false; 851 852 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) 853 return false; 854 855 break; 856 } 857 858 case Type::DependentSizedArray: { 859 const auto *Array1 = cast<DependentSizedArrayType>(T1); 860 const auto *Array2 = cast<DependentSizedArrayType>(T2); 861 if (!IsStructurallyEquivalent(Context, Array1->getSizeExpr(), 862 Array2->getSizeExpr())) 863 return false; 864 865 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) 866 return false; 867 868 break; 869 } 870 871 case Type::DependentAddressSpace: { 872 const auto *DepAddressSpace1 = cast<DependentAddressSpaceType>(T1); 873 const auto *DepAddressSpace2 = cast<DependentAddressSpaceType>(T2); 874 if (!IsStructurallyEquivalent(Context, DepAddressSpace1->getAddrSpaceExpr(), 875 DepAddressSpace2->getAddrSpaceExpr())) 876 return false; 877 if (!IsStructurallyEquivalent(Context, DepAddressSpace1->getPointeeType(), 878 DepAddressSpace2->getPointeeType())) 879 return false; 880 881 break; 882 } 883 884 case Type::DependentSizedExtVector: { 885 const auto *Vec1 = cast<DependentSizedExtVectorType>(T1); 886 const auto *Vec2 = cast<DependentSizedExtVectorType>(T2); 887 if (!IsStructurallyEquivalent(Context, Vec1->getSizeExpr(), 888 Vec2->getSizeExpr())) 889 return false; 890 if (!IsStructurallyEquivalent(Context, Vec1->getElementType(), 891 Vec2->getElementType())) 892 return false; 893 break; 894 } 895 896 case Type::DependentVector: { 897 const auto *Vec1 = cast<DependentVectorType>(T1); 898 const auto *Vec2 = cast<DependentVectorType>(T2); 899 if (Vec1->getVectorKind() != Vec2->getVectorKind()) 900 return false; 901 if (!IsStructurallyEquivalent(Context, Vec1->getSizeExpr(), 902 Vec2->getSizeExpr())) 903 return false; 904 if (!IsStructurallyEquivalent(Context, Vec1->getElementType(), 905 Vec2->getElementType())) 906 return false; 907 break; 908 } 909 910 case Type::Vector: 911 case Type::ExtVector: { 912 const auto *Vec1 = cast<VectorType>(T1); 913 const auto *Vec2 = cast<VectorType>(T2); 914 if (!IsStructurallyEquivalent(Context, Vec1->getElementType(), 915 Vec2->getElementType())) 916 return false; 917 if (Vec1->getNumElements() != Vec2->getNumElements()) 918 return false; 919 if (Vec1->getVectorKind() != Vec2->getVectorKind()) 920 return false; 921 break; 922 } 923 924 case Type::DependentSizedMatrix: { 925 const DependentSizedMatrixType *Mat1 = cast<DependentSizedMatrixType>(T1); 926 const DependentSizedMatrixType *Mat2 = cast<DependentSizedMatrixType>(T2); 927 // The element types, row and column expressions must be structurally 928 // equivalent. 929 if (!IsStructurallyEquivalent(Context, Mat1->getRowExpr(), 930 Mat2->getRowExpr()) || 931 !IsStructurallyEquivalent(Context, Mat1->getColumnExpr(), 932 Mat2->getColumnExpr()) || 933 !IsStructurallyEquivalent(Context, Mat1->getElementType(), 934 Mat2->getElementType())) 935 return false; 936 break; 937 } 938 939 case Type::ConstantMatrix: { 940 const ConstantMatrixType *Mat1 = cast<ConstantMatrixType>(T1); 941 const ConstantMatrixType *Mat2 = cast<ConstantMatrixType>(T2); 942 // The element types must be structurally equivalent and the number of rows 943 // and columns must match. 944 if (!IsStructurallyEquivalent(Context, Mat1->getElementType(), 945 Mat2->getElementType()) || 946 Mat1->getNumRows() != Mat2->getNumRows() || 947 Mat1->getNumColumns() != Mat2->getNumColumns()) 948 return false; 949 break; 950 } 951 952 case Type::FunctionProto: { 953 const auto *Proto1 = cast<FunctionProtoType>(T1); 954 const auto *Proto2 = cast<FunctionProtoType>(T2); 955 956 if (Proto1->getNumParams() != Proto2->getNumParams()) 957 return false; 958 for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) { 959 if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I), 960 Proto2->getParamType(I))) 961 return false; 962 } 963 if (Proto1->isVariadic() != Proto2->isVariadic()) 964 return false; 965 966 if (Proto1->getMethodQuals() != Proto2->getMethodQuals()) 967 return false; 968 969 // Check exceptions, this information is lost in canonical type. 970 const auto *OrigProto1 = 971 cast<FunctionProtoType>(OrigT1.getDesugaredType(Context.FromCtx)); 972 const auto *OrigProto2 = 973 cast<FunctionProtoType>(OrigT2.getDesugaredType(Context.ToCtx)); 974 if (!IsEquivalentExceptionSpec(Context, OrigProto1, OrigProto2)) 975 return false; 976 977 // Fall through to check the bits common with FunctionNoProtoType. 978 [[fallthrough]]; 979 } 980 981 case Type::FunctionNoProto: { 982 const auto *Function1 = cast<FunctionType>(T1); 983 const auto *Function2 = cast<FunctionType>(T2); 984 if (!IsStructurallyEquivalent(Context, Function1->getReturnType(), 985 Function2->getReturnType())) 986 return false; 987 if (!IsStructurallyEquivalent(Context, Function1->getExtInfo(), 988 Function2->getExtInfo())) 989 return false; 990 break; 991 } 992 993 case Type::UnresolvedUsing: 994 if (!IsStructurallyEquivalent(Context, 995 cast<UnresolvedUsingType>(T1)->getDecl(), 996 cast<UnresolvedUsingType>(T2)->getDecl())) 997 return false; 998 break; 999 1000 case Type::Attributed: 1001 if (!IsStructurallyEquivalent(Context, 1002 cast<AttributedType>(T1)->getModifiedType(), 1003 cast<AttributedType>(T2)->getModifiedType())) 1004 return false; 1005 if (!IsStructurallyEquivalent( 1006 Context, cast<AttributedType>(T1)->getEquivalentType(), 1007 cast<AttributedType>(T2)->getEquivalentType())) 1008 return false; 1009 break; 1010 1011 case Type::BTFTagAttributed: 1012 if (!IsStructurallyEquivalent( 1013 Context, cast<BTFTagAttributedType>(T1)->getWrappedType(), 1014 cast<BTFTagAttributedType>(T2)->getWrappedType())) 1015 return false; 1016 break; 1017 1018 case Type::Paren: 1019 if (!IsStructurallyEquivalent(Context, cast<ParenType>(T1)->getInnerType(), 1020 cast<ParenType>(T2)->getInnerType())) 1021 return false; 1022 break; 1023 1024 case Type::MacroQualified: 1025 if (!IsStructurallyEquivalent( 1026 Context, cast<MacroQualifiedType>(T1)->getUnderlyingType(), 1027 cast<MacroQualifiedType>(T2)->getUnderlyingType())) 1028 return false; 1029 break; 1030 1031 case Type::Using: 1032 if (!IsStructurallyEquivalent(Context, cast<UsingType>(T1)->getFoundDecl(), 1033 cast<UsingType>(T2)->getFoundDecl())) 1034 return false; 1035 if (!IsStructurallyEquivalent(Context, 1036 cast<UsingType>(T1)->getUnderlyingType(), 1037 cast<UsingType>(T2)->getUnderlyingType())) 1038 return false; 1039 break; 1040 1041 case Type::Typedef: 1042 if (!IsStructurallyEquivalent(Context, cast<TypedefType>(T1)->getDecl(), 1043 cast<TypedefType>(T2)->getDecl()) || 1044 !IsStructurallyEquivalent(Context, cast<TypedefType>(T1)->desugar(), 1045 cast<TypedefType>(T2)->desugar())) 1046 return false; 1047 break; 1048 1049 case Type::TypeOfExpr: 1050 if (!IsStructurallyEquivalent( 1051 Context, cast<TypeOfExprType>(T1)->getUnderlyingExpr(), 1052 cast<TypeOfExprType>(T2)->getUnderlyingExpr())) 1053 return false; 1054 break; 1055 1056 case Type::TypeOf: 1057 if (!IsStructurallyEquivalent(Context, 1058 cast<TypeOfType>(T1)->getUnmodifiedType(), 1059 cast<TypeOfType>(T2)->getUnmodifiedType())) 1060 return false; 1061 break; 1062 1063 case Type::UnaryTransform: 1064 if (!IsStructurallyEquivalent( 1065 Context, cast<UnaryTransformType>(T1)->getUnderlyingType(), 1066 cast<UnaryTransformType>(T2)->getUnderlyingType())) 1067 return false; 1068 break; 1069 1070 case Type::Decltype: 1071 if (!IsStructurallyEquivalent(Context, 1072 cast<DecltypeType>(T1)->getUnderlyingExpr(), 1073 cast<DecltypeType>(T2)->getUnderlyingExpr())) 1074 return false; 1075 break; 1076 1077 case Type::Auto: { 1078 auto *Auto1 = cast<AutoType>(T1); 1079 auto *Auto2 = cast<AutoType>(T2); 1080 if (!IsStructurallyEquivalent(Context, Auto1->getDeducedType(), 1081 Auto2->getDeducedType())) 1082 return false; 1083 if (Auto1->isConstrained() != Auto2->isConstrained()) 1084 return false; 1085 if (Auto1->isConstrained()) { 1086 if (Auto1->getTypeConstraintConcept() != 1087 Auto2->getTypeConstraintConcept()) 1088 return false; 1089 if (!IsStructurallyEquivalent(Context, 1090 Auto1->getTypeConstraintArguments(), 1091 Auto2->getTypeConstraintArguments())) 1092 return false; 1093 } 1094 break; 1095 } 1096 1097 case Type::DeducedTemplateSpecialization: { 1098 const auto *DT1 = cast<DeducedTemplateSpecializationType>(T1); 1099 const auto *DT2 = cast<DeducedTemplateSpecializationType>(T2); 1100 if (!IsStructurallyEquivalent(Context, DT1->getTemplateName(), 1101 DT2->getTemplateName())) 1102 return false; 1103 if (!IsStructurallyEquivalent(Context, DT1->getDeducedType(), 1104 DT2->getDeducedType())) 1105 return false; 1106 break; 1107 } 1108 1109 case Type::Record: 1110 case Type::Enum: 1111 if (!IsStructurallyEquivalent(Context, cast<TagType>(T1)->getDecl(), 1112 cast<TagType>(T2)->getDecl())) 1113 return false; 1114 break; 1115 1116 case Type::TemplateTypeParm: { 1117 const auto *Parm1 = cast<TemplateTypeParmType>(T1); 1118 const auto *Parm2 = cast<TemplateTypeParmType>(T2); 1119 if (!Context.IgnoreTemplateParmDepth && 1120 Parm1->getDepth() != Parm2->getDepth()) 1121 return false; 1122 if (Parm1->getIndex() != Parm2->getIndex()) 1123 return false; 1124 if (Parm1->isParameterPack() != Parm2->isParameterPack()) 1125 return false; 1126 1127 // Names of template type parameters are never significant. 1128 break; 1129 } 1130 1131 case Type::SubstTemplateTypeParm: { 1132 const auto *Subst1 = cast<SubstTemplateTypeParmType>(T1); 1133 const auto *Subst2 = cast<SubstTemplateTypeParmType>(T2); 1134 if (!IsStructurallyEquivalent(Context, Subst1->getReplacementType(), 1135 Subst2->getReplacementType())) 1136 return false; 1137 if (!IsStructurallyEquivalent(Context, Subst1->getAssociatedDecl(), 1138 Subst2->getAssociatedDecl())) 1139 return false; 1140 if (Subst1->getIndex() != Subst2->getIndex()) 1141 return false; 1142 if (Subst1->getPackIndex() != Subst2->getPackIndex()) 1143 return false; 1144 break; 1145 } 1146 1147 case Type::SubstTemplateTypeParmPack: { 1148 const auto *Subst1 = cast<SubstTemplateTypeParmPackType>(T1); 1149 const auto *Subst2 = cast<SubstTemplateTypeParmPackType>(T2); 1150 if (!IsStructurallyEquivalent(Context, Subst1->getAssociatedDecl(), 1151 Subst2->getAssociatedDecl())) 1152 return false; 1153 if (Subst1->getIndex() != Subst2->getIndex()) 1154 return false; 1155 if (!IsStructurallyEquivalent(Context, Subst1->getArgumentPack(), 1156 Subst2->getArgumentPack())) 1157 return false; 1158 break; 1159 } 1160 1161 case Type::TemplateSpecialization: { 1162 const auto *Spec1 = cast<TemplateSpecializationType>(T1); 1163 const auto *Spec2 = cast<TemplateSpecializationType>(T2); 1164 if (!IsStructurallyEquivalent(Context, Spec1->getTemplateName(), 1165 Spec2->getTemplateName())) 1166 return false; 1167 if (!IsStructurallyEquivalent(Context, Spec1->template_arguments(), 1168 Spec2->template_arguments())) 1169 return false; 1170 break; 1171 } 1172 1173 case Type::Elaborated: { 1174 const auto *Elab1 = cast<ElaboratedType>(T1); 1175 const auto *Elab2 = cast<ElaboratedType>(T2); 1176 // CHECKME: what if a keyword is ElaboratedTypeKeyword::None or 1177 // ElaboratedTypeKeyword::Typename 1178 // ? 1179 if (Elab1->getKeyword() != Elab2->getKeyword()) 1180 return false; 1181 if (!IsStructurallyEquivalent(Context, Elab1->getQualifier(), 1182 Elab2->getQualifier())) 1183 return false; 1184 if (!IsStructurallyEquivalent(Context, Elab1->getNamedType(), 1185 Elab2->getNamedType())) 1186 return false; 1187 break; 1188 } 1189 1190 case Type::InjectedClassName: { 1191 const auto *Inj1 = cast<InjectedClassNameType>(T1); 1192 const auto *Inj2 = cast<InjectedClassNameType>(T2); 1193 if (!IsStructurallyEquivalent(Context, 1194 Inj1->getInjectedSpecializationType(), 1195 Inj2->getInjectedSpecializationType())) 1196 return false; 1197 break; 1198 } 1199 1200 case Type::DependentName: { 1201 const auto *Typename1 = cast<DependentNameType>(T1); 1202 const auto *Typename2 = cast<DependentNameType>(T2); 1203 if (!IsStructurallyEquivalent(Context, Typename1->getQualifier(), 1204 Typename2->getQualifier())) 1205 return false; 1206 if (!IsStructurallyEquivalent(Typename1->getIdentifier(), 1207 Typename2->getIdentifier())) 1208 return false; 1209 1210 break; 1211 } 1212 1213 case Type::DependentTemplateSpecialization: { 1214 const auto *Spec1 = cast<DependentTemplateSpecializationType>(T1); 1215 const auto *Spec2 = cast<DependentTemplateSpecializationType>(T2); 1216 if (!IsStructurallyEquivalent(Context, Spec1->getQualifier(), 1217 Spec2->getQualifier())) 1218 return false; 1219 if (!IsStructurallyEquivalent(Spec1->getIdentifier(), 1220 Spec2->getIdentifier())) 1221 return false; 1222 if (!IsStructurallyEquivalent(Context, Spec1->template_arguments(), 1223 Spec2->template_arguments())) 1224 return false; 1225 break; 1226 } 1227 1228 case Type::PackExpansion: 1229 if (!IsStructurallyEquivalent(Context, 1230 cast<PackExpansionType>(T1)->getPattern(), 1231 cast<PackExpansionType>(T2)->getPattern())) 1232 return false; 1233 break; 1234 1235 case Type::ObjCInterface: { 1236 const auto *Iface1 = cast<ObjCInterfaceType>(T1); 1237 const auto *Iface2 = cast<ObjCInterfaceType>(T2); 1238 if (!IsStructurallyEquivalent(Context, Iface1->getDecl(), 1239 Iface2->getDecl())) 1240 return false; 1241 break; 1242 } 1243 1244 case Type::ObjCTypeParam: { 1245 const auto *Obj1 = cast<ObjCTypeParamType>(T1); 1246 const auto *Obj2 = cast<ObjCTypeParamType>(T2); 1247 if (!IsStructurallyEquivalent(Context, Obj1->getDecl(), Obj2->getDecl())) 1248 return false; 1249 1250 if (Obj1->getNumProtocols() != Obj2->getNumProtocols()) 1251 return false; 1252 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) { 1253 if (!IsStructurallyEquivalent(Context, Obj1->getProtocol(I), 1254 Obj2->getProtocol(I))) 1255 return false; 1256 } 1257 break; 1258 } 1259 1260 case Type::ObjCObject: { 1261 const auto *Obj1 = cast<ObjCObjectType>(T1); 1262 const auto *Obj2 = cast<ObjCObjectType>(T2); 1263 if (!IsStructurallyEquivalent(Context, Obj1->getBaseType(), 1264 Obj2->getBaseType())) 1265 return false; 1266 if (Obj1->getNumProtocols() != Obj2->getNumProtocols()) 1267 return false; 1268 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) { 1269 if (!IsStructurallyEquivalent(Context, Obj1->getProtocol(I), 1270 Obj2->getProtocol(I))) 1271 return false; 1272 } 1273 break; 1274 } 1275 1276 case Type::ObjCObjectPointer: { 1277 const auto *Ptr1 = cast<ObjCObjectPointerType>(T1); 1278 const auto *Ptr2 = cast<ObjCObjectPointerType>(T2); 1279 if (!IsStructurallyEquivalent(Context, Ptr1->getPointeeType(), 1280 Ptr2->getPointeeType())) 1281 return false; 1282 break; 1283 } 1284 1285 case Type::Atomic: 1286 if (!IsStructurallyEquivalent(Context, cast<AtomicType>(T1)->getValueType(), 1287 cast<AtomicType>(T2)->getValueType())) 1288 return false; 1289 break; 1290 1291 case Type::Pipe: 1292 if (!IsStructurallyEquivalent(Context, cast<PipeType>(T1)->getElementType(), 1293 cast<PipeType>(T2)->getElementType())) 1294 return false; 1295 break; 1296 case Type::BitInt: { 1297 const auto *Int1 = cast<BitIntType>(T1); 1298 const auto *Int2 = cast<BitIntType>(T2); 1299 1300 if (Int1->isUnsigned() != Int2->isUnsigned() || 1301 Int1->getNumBits() != Int2->getNumBits()) 1302 return false; 1303 break; 1304 } 1305 case Type::DependentBitInt: { 1306 const auto *Int1 = cast<DependentBitIntType>(T1); 1307 const auto *Int2 = cast<DependentBitIntType>(T2); 1308 1309 if (Int1->isUnsigned() != Int2->isUnsigned() || 1310 !IsStructurallyEquivalent(Context, Int1->getNumBitsExpr(), 1311 Int2->getNumBitsExpr())) 1312 return false; 1313 break; 1314 } 1315 } // end switch 1316 1317 return true; 1318 } 1319 1320 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1321 VarDecl *D1, VarDecl *D2) { 1322 if (D1->getStorageClass() != D2->getStorageClass()) 1323 return false; 1324 1325 IdentifierInfo *Name1 = D1->getIdentifier(); 1326 IdentifierInfo *Name2 = D2->getIdentifier(); 1327 if (!::IsStructurallyEquivalent(Name1, Name2)) 1328 return false; 1329 1330 if (!IsStructurallyEquivalent(Context, D1->getType(), D2->getType())) 1331 return false; 1332 1333 return IsStructurallyEquivalent(Context, D1->getInit(), D2->getInit()); 1334 } 1335 1336 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1337 FieldDecl *Field1, FieldDecl *Field2, 1338 QualType Owner2Type) { 1339 const auto *Owner2 = cast<Decl>(Field2->getDeclContext()); 1340 1341 // For anonymous structs/unions, match up the anonymous struct/union type 1342 // declarations directly, so that we don't go off searching for anonymous 1343 // types 1344 if (Field1->isAnonymousStructOrUnion() && 1345 Field2->isAnonymousStructOrUnion()) { 1346 RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl(); 1347 RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl(); 1348 return IsStructurallyEquivalent(Context, D1, D2); 1349 } 1350 1351 // Check for equivalent field names. 1352 IdentifierInfo *Name1 = Field1->getIdentifier(); 1353 IdentifierInfo *Name2 = Field2->getIdentifier(); 1354 if (!::IsStructurallyEquivalent(Name1, Name2)) { 1355 if (Context.Complain) { 1356 Context.Diag2( 1357 Owner2->getLocation(), 1358 Context.getApplicableDiagnostic(diag::err_odr_tag_type_inconsistent)) 1359 << Owner2Type; 1360 Context.Diag2(Field2->getLocation(), diag::note_odr_field_name) 1361 << Field2->getDeclName(); 1362 Context.Diag1(Field1->getLocation(), diag::note_odr_field_name) 1363 << Field1->getDeclName(); 1364 } 1365 return false; 1366 } 1367 1368 if (!IsStructurallyEquivalent(Context, Field1->getType(), 1369 Field2->getType())) { 1370 if (Context.Complain) { 1371 Context.Diag2( 1372 Owner2->getLocation(), 1373 Context.getApplicableDiagnostic(diag::err_odr_tag_type_inconsistent)) 1374 << Owner2Type; 1375 Context.Diag2(Field2->getLocation(), diag::note_odr_field) 1376 << Field2->getDeclName() << Field2->getType(); 1377 Context.Diag1(Field1->getLocation(), diag::note_odr_field) 1378 << Field1->getDeclName() << Field1->getType(); 1379 } 1380 return false; 1381 } 1382 1383 if (Field1->isBitField()) 1384 return IsStructurallyEquivalent(Context, Field1->getBitWidth(), 1385 Field2->getBitWidth()); 1386 1387 return true; 1388 } 1389 1390 /// Determine structural equivalence of two fields. 1391 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1392 FieldDecl *Field1, FieldDecl *Field2) { 1393 const auto *Owner2 = cast<RecordDecl>(Field2->getDeclContext()); 1394 return IsStructurallyEquivalent(Context, Field1, Field2, 1395 Context.ToCtx.getTypeDeclType(Owner2)); 1396 } 1397 1398 /// Determine structural equivalence of two methods. 1399 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1400 CXXMethodDecl *Method1, 1401 CXXMethodDecl *Method2) { 1402 bool PropertiesEqual = 1403 Method1->getDeclKind() == Method2->getDeclKind() && 1404 Method1->getRefQualifier() == Method2->getRefQualifier() && 1405 Method1->getAccess() == Method2->getAccess() && 1406 Method1->getOverloadedOperator() == Method2->getOverloadedOperator() && 1407 Method1->isStatic() == Method2->isStatic() && 1408 Method1->isImplicitObjectMemberFunction() == 1409 Method2->isImplicitObjectMemberFunction() && 1410 Method1->isConst() == Method2->isConst() && 1411 Method1->isVolatile() == Method2->isVolatile() && 1412 Method1->isVirtual() == Method2->isVirtual() && 1413 Method1->isPure() == Method2->isPure() && 1414 Method1->isDefaulted() == Method2->isDefaulted() && 1415 Method1->isDeleted() == Method2->isDeleted(); 1416 if (!PropertiesEqual) 1417 return false; 1418 // FIXME: Check for 'final'. 1419 1420 if (auto *Constructor1 = dyn_cast<CXXConstructorDecl>(Method1)) { 1421 auto *Constructor2 = cast<CXXConstructorDecl>(Method2); 1422 if (!Constructor1->getExplicitSpecifier().isEquivalent( 1423 Constructor2->getExplicitSpecifier())) 1424 return false; 1425 } 1426 1427 if (auto *Conversion1 = dyn_cast<CXXConversionDecl>(Method1)) { 1428 auto *Conversion2 = cast<CXXConversionDecl>(Method2); 1429 if (!Conversion1->getExplicitSpecifier().isEquivalent( 1430 Conversion2->getExplicitSpecifier())) 1431 return false; 1432 if (!IsStructurallyEquivalent(Context, Conversion1->getConversionType(), 1433 Conversion2->getConversionType())) 1434 return false; 1435 } 1436 1437 const IdentifierInfo *Name1 = Method1->getIdentifier(); 1438 const IdentifierInfo *Name2 = Method2->getIdentifier(); 1439 if (!::IsStructurallyEquivalent(Name1, Name2)) { 1440 return false; 1441 // TODO: Names do not match, add warning like at check for FieldDecl. 1442 } 1443 1444 // Check the prototypes. 1445 if (!::IsStructurallyEquivalent(Context, 1446 Method1->getType(), Method2->getType())) 1447 return false; 1448 1449 return true; 1450 } 1451 1452 /// Determine structural equivalence of two lambda classes. 1453 static bool 1454 IsStructurallyEquivalentLambdas(StructuralEquivalenceContext &Context, 1455 CXXRecordDecl *D1, CXXRecordDecl *D2) { 1456 assert(D1->isLambda() && D2->isLambda() && 1457 "Must be called on lambda classes"); 1458 if (!IsStructurallyEquivalent(Context, D1->getLambdaCallOperator(), 1459 D2->getLambdaCallOperator())) 1460 return false; 1461 1462 return true; 1463 } 1464 1465 /// Determine if context of a class is equivalent. 1466 static bool 1467 IsRecordContextStructurallyEquivalent(StructuralEquivalenceContext &Context, 1468 RecordDecl *D1, RecordDecl *D2) { 1469 // The context should be completely equal, including anonymous and inline 1470 // namespaces. 1471 // We compare objects as part of full translation units, not subtrees of 1472 // translation units. 1473 DeclContext *DC1 = D1->getDeclContext()->getNonTransparentContext(); 1474 DeclContext *DC2 = D2->getDeclContext()->getNonTransparentContext(); 1475 while (true) { 1476 // Special case: We allow a struct defined in a function to be equivalent 1477 // with a similar struct defined outside of a function. 1478 if ((DC1->isFunctionOrMethod() && DC2->isTranslationUnit()) || 1479 (DC2->isFunctionOrMethod() && DC1->isTranslationUnit())) 1480 return true; 1481 1482 if (DC1->getDeclKind() != DC2->getDeclKind()) 1483 return false; 1484 if (DC1->isTranslationUnit()) 1485 break; 1486 if (DC1->isInlineNamespace() != DC2->isInlineNamespace()) 1487 return false; 1488 if (const auto *ND1 = dyn_cast<NamedDecl>(DC1)) { 1489 const auto *ND2 = cast<NamedDecl>(DC2); 1490 if (!DC1->isInlineNamespace() && 1491 !IsStructurallyEquivalent(ND1->getIdentifier(), ND2->getIdentifier())) 1492 return false; 1493 } 1494 1495 if (auto *D1Spec = dyn_cast<ClassTemplateSpecializationDecl>(DC1)) { 1496 auto *D2Spec = dyn_cast<ClassTemplateSpecializationDecl>(DC2); 1497 if (!IsStructurallyEquivalent(Context, D1Spec, D2Spec)) 1498 return false; 1499 } 1500 1501 DC1 = DC1->getParent()->getNonTransparentContext(); 1502 DC2 = DC2->getParent()->getNonTransparentContext(); 1503 } 1504 1505 return true; 1506 } 1507 1508 static bool NameIsStructurallyEquivalent(const TagDecl &D1, const TagDecl &D2) { 1509 auto GetName = [](const TagDecl &D) -> const IdentifierInfo * { 1510 if (const IdentifierInfo *Name = D.getIdentifier()) 1511 return Name; 1512 if (const TypedefNameDecl *TypedefName = D.getTypedefNameForAnonDecl()) 1513 return TypedefName->getIdentifier(); 1514 return nullptr; 1515 }; 1516 return IsStructurallyEquivalent(GetName(D1), GetName(D2)); 1517 } 1518 1519 /// Determine structural equivalence of two records. 1520 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1521 RecordDecl *D1, RecordDecl *D2) { 1522 if (!NameIsStructurallyEquivalent(*D1, *D2)) { 1523 return false; 1524 } 1525 1526 if (D1->isUnion() != D2->isUnion()) { 1527 if (Context.Complain) { 1528 Context.Diag2(D2->getLocation(), Context.getApplicableDiagnostic( 1529 diag::err_odr_tag_type_inconsistent)) 1530 << Context.ToCtx.getTypeDeclType(D2); 1531 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here) 1532 << D1->getDeclName() << (unsigned)D1->getTagKind(); 1533 } 1534 return false; 1535 } 1536 1537 if (!D1->getDeclName() && !D2->getDeclName()) { 1538 // If both anonymous structs/unions are in a record context, make sure 1539 // they occur in the same location in the context records. 1540 if (std::optional<unsigned> Index1 = 1541 StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(D1)) { 1542 if (std::optional<unsigned> Index2 = 1543 StructuralEquivalenceContext::findUntaggedStructOrUnionIndex( 1544 D2)) { 1545 if (*Index1 != *Index2) 1546 return false; 1547 } 1548 } 1549 } 1550 1551 // If the records occur in different context (namespace), these should be 1552 // different. This is specially important if the definition of one or both 1553 // records is missing. 1554 if (!IsRecordContextStructurallyEquivalent(Context, D1, D2)) 1555 return false; 1556 1557 // If both declarations are class template specializations, we know 1558 // the ODR applies, so check the template and template arguments. 1559 const auto *Spec1 = dyn_cast<ClassTemplateSpecializationDecl>(D1); 1560 const auto *Spec2 = dyn_cast<ClassTemplateSpecializationDecl>(D2); 1561 if (Spec1 && Spec2) { 1562 // Check that the specialized templates are the same. 1563 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(), 1564 Spec2->getSpecializedTemplate())) 1565 return false; 1566 1567 // Check that the template arguments are the same. 1568 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size()) 1569 return false; 1570 1571 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I) 1572 if (!IsStructurallyEquivalent(Context, Spec1->getTemplateArgs().get(I), 1573 Spec2->getTemplateArgs().get(I))) 1574 return false; 1575 } 1576 // If one is a class template specialization and the other is not, these 1577 // structures are different. 1578 else if (Spec1 || Spec2) 1579 return false; 1580 1581 // Compare the definitions of these two records. If either or both are 1582 // incomplete (i.e. it is a forward decl), we assume that they are 1583 // equivalent. 1584 D1 = D1->getDefinition(); 1585 D2 = D2->getDefinition(); 1586 if (!D1 || !D2) 1587 return true; 1588 1589 // If any of the records has external storage and we do a minimal check (or 1590 // AST import) we assume they are equivalent. (If we didn't have this 1591 // assumption then `RecordDecl::LoadFieldsFromExternalStorage` could trigger 1592 // another AST import which in turn would call the structural equivalency 1593 // check again and finally we'd have an improper result.) 1594 if (Context.EqKind == StructuralEquivalenceKind::Minimal) 1595 if (D1->hasExternalLexicalStorage() || D2->hasExternalLexicalStorage()) 1596 return true; 1597 1598 // If one definition is currently being defined, we do not compare for 1599 // equality and we assume that the decls are equal. 1600 if (D1->isBeingDefined() || D2->isBeingDefined()) 1601 return true; 1602 1603 if (auto *D1CXX = dyn_cast<CXXRecordDecl>(D1)) { 1604 if (auto *D2CXX = dyn_cast<CXXRecordDecl>(D2)) { 1605 if (D1CXX->hasExternalLexicalStorage() && 1606 !D1CXX->isCompleteDefinition()) { 1607 D1CXX->getASTContext().getExternalSource()->CompleteType(D1CXX); 1608 } 1609 1610 if (D1CXX->isLambda() != D2CXX->isLambda()) 1611 return false; 1612 if (D1CXX->isLambda()) { 1613 if (!IsStructurallyEquivalentLambdas(Context, D1CXX, D2CXX)) 1614 return false; 1615 } 1616 1617 if (D1CXX->getNumBases() != D2CXX->getNumBases()) { 1618 if (Context.Complain) { 1619 Context.Diag2(D2->getLocation(), 1620 Context.getApplicableDiagnostic( 1621 diag::err_odr_tag_type_inconsistent)) 1622 << Context.ToCtx.getTypeDeclType(D2); 1623 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases) 1624 << D2CXX->getNumBases(); 1625 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases) 1626 << D1CXX->getNumBases(); 1627 } 1628 return false; 1629 } 1630 1631 // Check the base classes. 1632 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(), 1633 BaseEnd1 = D1CXX->bases_end(), 1634 Base2 = D2CXX->bases_begin(); 1635 Base1 != BaseEnd1; ++Base1, ++Base2) { 1636 if (!IsStructurallyEquivalent(Context, Base1->getType(), 1637 Base2->getType())) { 1638 if (Context.Complain) { 1639 Context.Diag2(D2->getLocation(), 1640 Context.getApplicableDiagnostic( 1641 diag::err_odr_tag_type_inconsistent)) 1642 << Context.ToCtx.getTypeDeclType(D2); 1643 Context.Diag2(Base2->getBeginLoc(), diag::note_odr_base) 1644 << Base2->getType() << Base2->getSourceRange(); 1645 Context.Diag1(Base1->getBeginLoc(), diag::note_odr_base) 1646 << Base1->getType() << Base1->getSourceRange(); 1647 } 1648 return false; 1649 } 1650 1651 // Check virtual vs. non-virtual inheritance mismatch. 1652 if (Base1->isVirtual() != Base2->isVirtual()) { 1653 if (Context.Complain) { 1654 Context.Diag2(D2->getLocation(), 1655 Context.getApplicableDiagnostic( 1656 diag::err_odr_tag_type_inconsistent)) 1657 << Context.ToCtx.getTypeDeclType(D2); 1658 Context.Diag2(Base2->getBeginLoc(), diag::note_odr_virtual_base) 1659 << Base2->isVirtual() << Base2->getSourceRange(); 1660 Context.Diag1(Base1->getBeginLoc(), diag::note_odr_base) 1661 << Base1->isVirtual() << Base1->getSourceRange(); 1662 } 1663 return false; 1664 } 1665 } 1666 1667 // Check the friends for consistency. 1668 CXXRecordDecl::friend_iterator Friend2 = D2CXX->friend_begin(), 1669 Friend2End = D2CXX->friend_end(); 1670 for (CXXRecordDecl::friend_iterator Friend1 = D1CXX->friend_begin(), 1671 Friend1End = D1CXX->friend_end(); 1672 Friend1 != Friend1End; ++Friend1, ++Friend2) { 1673 if (Friend2 == Friend2End) { 1674 if (Context.Complain) { 1675 Context.Diag2(D2->getLocation(), 1676 Context.getApplicableDiagnostic( 1677 diag::err_odr_tag_type_inconsistent)) 1678 << Context.ToCtx.getTypeDeclType(D2CXX); 1679 Context.Diag1((*Friend1)->getFriendLoc(), diag::note_odr_friend); 1680 Context.Diag2(D2->getLocation(), diag::note_odr_missing_friend); 1681 } 1682 return false; 1683 } 1684 1685 if (!IsStructurallyEquivalent(Context, *Friend1, *Friend2)) { 1686 if (Context.Complain) { 1687 Context.Diag2(D2->getLocation(), 1688 Context.getApplicableDiagnostic( 1689 diag::err_odr_tag_type_inconsistent)) 1690 << Context.ToCtx.getTypeDeclType(D2CXX); 1691 Context.Diag1((*Friend1)->getFriendLoc(), diag::note_odr_friend); 1692 Context.Diag2((*Friend2)->getFriendLoc(), diag::note_odr_friend); 1693 } 1694 return false; 1695 } 1696 } 1697 1698 if (Friend2 != Friend2End) { 1699 if (Context.Complain) { 1700 Context.Diag2(D2->getLocation(), 1701 Context.getApplicableDiagnostic( 1702 diag::err_odr_tag_type_inconsistent)) 1703 << Context.ToCtx.getTypeDeclType(D2); 1704 Context.Diag2((*Friend2)->getFriendLoc(), diag::note_odr_friend); 1705 Context.Diag1(D1->getLocation(), diag::note_odr_missing_friend); 1706 } 1707 return false; 1708 } 1709 } else if (D1CXX->getNumBases() > 0) { 1710 if (Context.Complain) { 1711 Context.Diag2(D2->getLocation(), 1712 Context.getApplicableDiagnostic( 1713 diag::err_odr_tag_type_inconsistent)) 1714 << Context.ToCtx.getTypeDeclType(D2); 1715 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin(); 1716 Context.Diag1(Base1->getBeginLoc(), diag::note_odr_base) 1717 << Base1->getType() << Base1->getSourceRange(); 1718 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base); 1719 } 1720 return false; 1721 } 1722 } 1723 1724 // Check the fields for consistency. 1725 QualType D2Type = Context.ToCtx.getTypeDeclType(D2); 1726 RecordDecl::field_iterator Field2 = D2->field_begin(), 1727 Field2End = D2->field_end(); 1728 for (RecordDecl::field_iterator Field1 = D1->field_begin(), 1729 Field1End = D1->field_end(); 1730 Field1 != Field1End; ++Field1, ++Field2) { 1731 if (Field2 == Field2End) { 1732 if (Context.Complain) { 1733 Context.Diag2(D2->getLocation(), 1734 Context.getApplicableDiagnostic( 1735 diag::err_odr_tag_type_inconsistent)) 1736 << Context.ToCtx.getTypeDeclType(D2); 1737 Context.Diag1(Field1->getLocation(), diag::note_odr_field) 1738 << Field1->getDeclName() << Field1->getType(); 1739 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field); 1740 } 1741 return false; 1742 } 1743 1744 if (!IsStructurallyEquivalent(Context, *Field1, *Field2, D2Type)) 1745 return false; 1746 } 1747 1748 if (Field2 != Field2End) { 1749 if (Context.Complain) { 1750 Context.Diag2(D2->getLocation(), Context.getApplicableDiagnostic( 1751 diag::err_odr_tag_type_inconsistent)) 1752 << Context.ToCtx.getTypeDeclType(D2); 1753 Context.Diag2(Field2->getLocation(), diag::note_odr_field) 1754 << Field2->getDeclName() << Field2->getType(); 1755 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field); 1756 } 1757 return false; 1758 } 1759 1760 return true; 1761 } 1762 1763 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1764 EnumConstantDecl *D1, 1765 EnumConstantDecl *D2) { 1766 const llvm::APSInt &FromVal = D1->getInitVal(); 1767 const llvm::APSInt &ToVal = D2->getInitVal(); 1768 if (FromVal.isSigned() != ToVal.isSigned()) 1769 return false; 1770 if (FromVal.getBitWidth() != ToVal.getBitWidth()) 1771 return false; 1772 if (FromVal != ToVal) 1773 return false; 1774 1775 if (!IsStructurallyEquivalent(D1->getIdentifier(), D2->getIdentifier())) 1776 return false; 1777 1778 // Init expressions are the most expensive check, so do them last. 1779 return IsStructurallyEquivalent(Context, D1->getInitExpr(), 1780 D2->getInitExpr()); 1781 } 1782 1783 /// Determine structural equivalence of two enums. 1784 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1785 EnumDecl *D1, EnumDecl *D2) { 1786 if (!NameIsStructurallyEquivalent(*D1, *D2)) { 1787 return false; 1788 } 1789 1790 // Compare the definitions of these two enums. If either or both are 1791 // incomplete (i.e. forward declared), we assume that they are equivalent. 1792 D1 = D1->getDefinition(); 1793 D2 = D2->getDefinition(); 1794 if (!D1 || !D2) 1795 return true; 1796 1797 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(), 1798 EC2End = D2->enumerator_end(); 1799 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(), 1800 EC1End = D1->enumerator_end(); 1801 EC1 != EC1End; ++EC1, ++EC2) { 1802 if (EC2 == EC2End) { 1803 if (Context.Complain) { 1804 Context.Diag2(D2->getLocation(), 1805 Context.getApplicableDiagnostic( 1806 diag::err_odr_tag_type_inconsistent)) 1807 << Context.ToCtx.getTypeDeclType(D2); 1808 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator) 1809 << EC1->getDeclName() << toString(EC1->getInitVal(), 10); 1810 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator); 1811 } 1812 return false; 1813 } 1814 1815 llvm::APSInt Val1 = EC1->getInitVal(); 1816 llvm::APSInt Val2 = EC2->getInitVal(); 1817 if (!llvm::APSInt::isSameValue(Val1, Val2) || 1818 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) { 1819 if (Context.Complain) { 1820 Context.Diag2(D2->getLocation(), 1821 Context.getApplicableDiagnostic( 1822 diag::err_odr_tag_type_inconsistent)) 1823 << Context.ToCtx.getTypeDeclType(D2); 1824 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator) 1825 << EC2->getDeclName() << toString(EC2->getInitVal(), 10); 1826 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator) 1827 << EC1->getDeclName() << toString(EC1->getInitVal(), 10); 1828 } 1829 return false; 1830 } 1831 } 1832 1833 if (EC2 != EC2End) { 1834 if (Context.Complain) { 1835 Context.Diag2(D2->getLocation(), Context.getApplicableDiagnostic( 1836 diag::err_odr_tag_type_inconsistent)) 1837 << Context.ToCtx.getTypeDeclType(D2); 1838 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator) 1839 << EC2->getDeclName() << toString(EC2->getInitVal(), 10); 1840 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator); 1841 } 1842 return false; 1843 } 1844 1845 return true; 1846 } 1847 1848 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1849 TemplateParameterList *Params1, 1850 TemplateParameterList *Params2) { 1851 if (Params1->size() != Params2->size()) { 1852 if (Context.Complain) { 1853 Context.Diag2(Params2->getTemplateLoc(), 1854 Context.getApplicableDiagnostic( 1855 diag::err_odr_different_num_template_parameters)) 1856 << Params1->size() << Params2->size(); 1857 Context.Diag1(Params1->getTemplateLoc(), 1858 diag::note_odr_template_parameter_list); 1859 } 1860 return false; 1861 } 1862 1863 for (unsigned I = 0, N = Params1->size(); I != N; ++I) { 1864 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) { 1865 if (Context.Complain) { 1866 Context.Diag2(Params2->getParam(I)->getLocation(), 1867 Context.getApplicableDiagnostic( 1868 diag::err_odr_different_template_parameter_kind)); 1869 Context.Diag1(Params1->getParam(I)->getLocation(), 1870 diag::note_odr_template_parameter_here); 1871 } 1872 return false; 1873 } 1874 1875 if (!IsStructurallyEquivalent(Context, Params1->getParam(I), 1876 Params2->getParam(I))) 1877 return false; 1878 } 1879 1880 return true; 1881 } 1882 1883 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1884 TemplateTypeParmDecl *D1, 1885 TemplateTypeParmDecl *D2) { 1886 if (D1->isParameterPack() != D2->isParameterPack()) { 1887 if (Context.Complain) { 1888 Context.Diag2(D2->getLocation(), 1889 Context.getApplicableDiagnostic( 1890 diag::err_odr_parameter_pack_non_pack)) 1891 << D2->isParameterPack(); 1892 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) 1893 << D1->isParameterPack(); 1894 } 1895 return false; 1896 } 1897 1898 return true; 1899 } 1900 1901 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1902 NonTypeTemplateParmDecl *D1, 1903 NonTypeTemplateParmDecl *D2) { 1904 if (D1->isParameterPack() != D2->isParameterPack()) { 1905 if (Context.Complain) { 1906 Context.Diag2(D2->getLocation(), 1907 Context.getApplicableDiagnostic( 1908 diag::err_odr_parameter_pack_non_pack)) 1909 << D2->isParameterPack(); 1910 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) 1911 << D1->isParameterPack(); 1912 } 1913 return false; 1914 } 1915 1916 // Check types. 1917 if (!IsStructurallyEquivalent(Context, D1->getType(), D2->getType())) { 1918 if (Context.Complain) { 1919 Context.Diag2(D2->getLocation(), 1920 Context.getApplicableDiagnostic( 1921 diag::err_odr_non_type_parameter_type_inconsistent)) 1922 << D2->getType() << D1->getType(); 1923 Context.Diag1(D1->getLocation(), diag::note_odr_value_here) 1924 << D1->getType(); 1925 } 1926 return false; 1927 } 1928 1929 return true; 1930 } 1931 1932 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1933 TemplateTemplateParmDecl *D1, 1934 TemplateTemplateParmDecl *D2) { 1935 if (D1->isParameterPack() != D2->isParameterPack()) { 1936 if (Context.Complain) { 1937 Context.Diag2(D2->getLocation(), 1938 Context.getApplicableDiagnostic( 1939 diag::err_odr_parameter_pack_non_pack)) 1940 << D2->isParameterPack(); 1941 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) 1942 << D1->isParameterPack(); 1943 } 1944 return false; 1945 } 1946 1947 // Check template parameter lists. 1948 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(), 1949 D2->getTemplateParameters()); 1950 } 1951 1952 static bool IsTemplateDeclCommonStructurallyEquivalent( 1953 StructuralEquivalenceContext &Ctx, TemplateDecl *D1, TemplateDecl *D2) { 1954 if (!IsStructurallyEquivalent(D1->getIdentifier(), D2->getIdentifier())) 1955 return false; 1956 if (!D1->getIdentifier()) // Special name 1957 if (D1->getNameAsString() != D2->getNameAsString()) 1958 return false; 1959 return IsStructurallyEquivalent(Ctx, D1->getTemplateParameters(), 1960 D2->getTemplateParameters()); 1961 } 1962 1963 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1964 ClassTemplateDecl *D1, 1965 ClassTemplateDecl *D2) { 1966 // Check template parameters. 1967 if (!IsTemplateDeclCommonStructurallyEquivalent(Context, D1, D2)) 1968 return false; 1969 1970 // Check the templated declaration. 1971 return IsStructurallyEquivalent(Context, D1->getTemplatedDecl(), 1972 D2->getTemplatedDecl()); 1973 } 1974 1975 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1976 FunctionTemplateDecl *D1, 1977 FunctionTemplateDecl *D2) { 1978 // Check template parameters. 1979 if (!IsTemplateDeclCommonStructurallyEquivalent(Context, D1, D2)) 1980 return false; 1981 1982 // Check the templated declaration. 1983 return IsStructurallyEquivalent(Context, D1->getTemplatedDecl()->getType(), 1984 D2->getTemplatedDecl()->getType()); 1985 } 1986 1987 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1988 TypeAliasTemplateDecl *D1, 1989 TypeAliasTemplateDecl *D2) { 1990 // Check template parameters. 1991 if (!IsTemplateDeclCommonStructurallyEquivalent(Context, D1, D2)) 1992 return false; 1993 1994 // Check the templated declaration. 1995 return IsStructurallyEquivalent(Context, D1->getTemplatedDecl(), 1996 D2->getTemplatedDecl()); 1997 } 1998 1999 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 2000 ConceptDecl *D1, 2001 ConceptDecl *D2) { 2002 // Check template parameters. 2003 if (!IsTemplateDeclCommonStructurallyEquivalent(Context, D1, D2)) 2004 return false; 2005 2006 // Check the constraint expression. 2007 return IsStructurallyEquivalent(Context, D1->getConstraintExpr(), 2008 D2->getConstraintExpr()); 2009 } 2010 2011 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 2012 FriendDecl *D1, FriendDecl *D2) { 2013 if ((D1->getFriendType() && D2->getFriendDecl()) || 2014 (D1->getFriendDecl() && D2->getFriendType())) { 2015 return false; 2016 } 2017 if (D1->getFriendType() && D2->getFriendType()) 2018 return IsStructurallyEquivalent(Context, 2019 D1->getFriendType()->getType(), 2020 D2->getFriendType()->getType()); 2021 if (D1->getFriendDecl() && D2->getFriendDecl()) 2022 return IsStructurallyEquivalent(Context, D1->getFriendDecl(), 2023 D2->getFriendDecl()); 2024 return false; 2025 } 2026 2027 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 2028 TypedefNameDecl *D1, TypedefNameDecl *D2) { 2029 if (!IsStructurallyEquivalent(D1->getIdentifier(), D2->getIdentifier())) 2030 return false; 2031 2032 return IsStructurallyEquivalent(Context, D1->getUnderlyingType(), 2033 D2->getUnderlyingType()); 2034 } 2035 2036 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 2037 FunctionDecl *D1, FunctionDecl *D2) { 2038 if (!IsStructurallyEquivalent(D1->getIdentifier(), D2->getIdentifier())) 2039 return false; 2040 2041 if (D1->isOverloadedOperator()) { 2042 if (!D2->isOverloadedOperator()) 2043 return false; 2044 if (D1->getOverloadedOperator() != D2->getOverloadedOperator()) 2045 return false; 2046 } 2047 2048 // FIXME: Consider checking for function attributes as well. 2049 if (!IsStructurallyEquivalent(Context, D1->getType(), D2->getType())) 2050 return false; 2051 2052 return true; 2053 } 2054 2055 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 2056 ObjCIvarDecl *D1, ObjCIvarDecl *D2, 2057 QualType Owner2Type) { 2058 if (D1->getAccessControl() != D2->getAccessControl()) 2059 return false; 2060 2061 return IsStructurallyEquivalent(Context, cast<FieldDecl>(D1), 2062 cast<FieldDecl>(D2), Owner2Type); 2063 } 2064 2065 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 2066 ObjCIvarDecl *D1, ObjCIvarDecl *D2) { 2067 QualType Owner2Type = 2068 Context.ToCtx.getObjCInterfaceType(D2->getContainingInterface()); 2069 return IsStructurallyEquivalent(Context, D1, D2, Owner2Type); 2070 } 2071 2072 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 2073 ObjCMethodDecl *Method1, 2074 ObjCMethodDecl *Method2) { 2075 bool PropertiesEqual = 2076 Method1->isInstanceMethod() == Method2->isInstanceMethod() && 2077 Method1->isVariadic() == Method2->isVariadic() && 2078 Method1->isDirectMethod() == Method2->isDirectMethod(); 2079 if (!PropertiesEqual) 2080 return false; 2081 2082 // Compare selector slot names. 2083 Selector Selector1 = Method1->getSelector(), 2084 Selector2 = Method2->getSelector(); 2085 unsigned NumArgs = Selector1.getNumArgs(); 2086 if (NumArgs != Selector2.getNumArgs()) 2087 return false; 2088 // Compare all selector slots. For selectors with arguments it means all arg 2089 // slots. And if there are no arguments, compare the first-and-only slot. 2090 unsigned SlotsToCheck = NumArgs > 0 ? NumArgs : 1; 2091 for (unsigned I = 0; I < SlotsToCheck; ++I) { 2092 if (!IsStructurallyEquivalent(Selector1.getIdentifierInfoForSlot(I), 2093 Selector2.getIdentifierInfoForSlot(I))) 2094 return false; 2095 } 2096 2097 // Compare types. 2098 if (!IsStructurallyEquivalent(Context, Method1->getReturnType(), 2099 Method2->getReturnType())) 2100 return false; 2101 assert( 2102 Method1->param_size() == Method2->param_size() && 2103 "Same number of arguments should be already enforced in Selector checks"); 2104 for (ObjCMethodDecl::param_type_iterator 2105 ParamT1 = Method1->param_type_begin(), 2106 ParamT1End = Method1->param_type_end(), 2107 ParamT2 = Method2->param_type_begin(), 2108 ParamT2End = Method2->param_type_end(); 2109 (ParamT1 != ParamT1End) && (ParamT2 != ParamT2End); 2110 ++ParamT1, ++ParamT2) { 2111 if (!IsStructurallyEquivalent(Context, *ParamT1, *ParamT2)) 2112 return false; 2113 } 2114 2115 return true; 2116 } 2117 2118 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 2119 ObjCCategoryDecl *D1, 2120 ObjCCategoryDecl *D2) { 2121 if (!IsStructurallyEquivalent(D1->getIdentifier(), D2->getIdentifier())) 2122 return false; 2123 2124 const ObjCInterfaceDecl *Intf1 = D1->getClassInterface(), 2125 *Intf2 = D2->getClassInterface(); 2126 if ((!Intf1 || !Intf2) && (Intf1 != Intf2)) 2127 return false; 2128 2129 if (Intf1 && 2130 !IsStructurallyEquivalent(Intf1->getIdentifier(), Intf2->getIdentifier())) 2131 return false; 2132 2133 // Compare protocols. 2134 ObjCCategoryDecl::protocol_iterator Protocol2 = D2->protocol_begin(), 2135 Protocol2End = D2->protocol_end(); 2136 for (ObjCCategoryDecl::protocol_iterator Protocol1 = D1->protocol_begin(), 2137 Protocol1End = D1->protocol_end(); 2138 Protocol1 != Protocol1End; ++Protocol1, ++Protocol2) { 2139 if (Protocol2 == Protocol2End) 2140 return false; 2141 if (!IsStructurallyEquivalent((*Protocol1)->getIdentifier(), 2142 (*Protocol2)->getIdentifier())) 2143 return false; 2144 } 2145 if (Protocol2 != Protocol2End) 2146 return false; 2147 2148 // Compare ivars. 2149 QualType D2Type = 2150 Intf2 ? Context.ToCtx.getObjCInterfaceType(Intf2) : QualType(); 2151 ObjCCategoryDecl::ivar_iterator Ivar2 = D2->ivar_begin(), 2152 Ivar2End = D2->ivar_end(); 2153 for (ObjCCategoryDecl::ivar_iterator Ivar1 = D1->ivar_begin(), 2154 Ivar1End = D1->ivar_end(); 2155 Ivar1 != Ivar1End; ++Ivar1, ++Ivar2) { 2156 if (Ivar2 == Ivar2End) 2157 return false; 2158 if (!IsStructurallyEquivalent(Context, *Ivar1, *Ivar2, D2Type)) 2159 return false; 2160 } 2161 if (Ivar2 != Ivar2End) 2162 return false; 2163 2164 // Compare methods. 2165 ObjCCategoryDecl::method_iterator Method2 = D2->meth_begin(), 2166 Method2End = D2->meth_end(); 2167 for (ObjCCategoryDecl::method_iterator Method1 = D1->meth_begin(), 2168 Method1End = D1->meth_end(); 2169 Method1 != Method1End; ++Method1, ++Method2) { 2170 if (Method2 == Method2End) 2171 return false; 2172 if (!IsStructurallyEquivalent(Context, *Method1, *Method2)) 2173 return false; 2174 } 2175 if (Method2 != Method2End) 2176 return false; 2177 2178 return true; 2179 } 2180 2181 /// Determine structural equivalence of two declarations. 2182 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 2183 Decl *D1, Decl *D2) { 2184 // FIXME: Check for known structural equivalences via a callback of some sort. 2185 2186 D1 = D1->getCanonicalDecl(); 2187 D2 = D2->getCanonicalDecl(); 2188 std::pair<Decl *, Decl *> P{D1, D2}; 2189 2190 // Check whether we already know that these two declarations are not 2191 // structurally equivalent. 2192 if (Context.NonEquivalentDecls.count(P)) 2193 return false; 2194 2195 // Check if a check for these declarations is already pending. 2196 // If yes D1 and D2 will be checked later (from DeclsToCheck), 2197 // or these are already checked (and equivalent). 2198 bool Inserted = Context.VisitedDecls.insert(P).second; 2199 if (!Inserted) 2200 return true; 2201 2202 Context.DeclsToCheck.push(P); 2203 2204 return true; 2205 } 2206 2207 DiagnosticBuilder StructuralEquivalenceContext::Diag1(SourceLocation Loc, 2208 unsigned DiagID) { 2209 assert(Complain && "Not allowed to complain"); 2210 if (LastDiagFromC2) 2211 FromCtx.getDiagnostics().notePriorDiagnosticFrom(ToCtx.getDiagnostics()); 2212 LastDiagFromC2 = false; 2213 return FromCtx.getDiagnostics().Report(Loc, DiagID); 2214 } 2215 2216 DiagnosticBuilder StructuralEquivalenceContext::Diag2(SourceLocation Loc, 2217 unsigned DiagID) { 2218 assert(Complain && "Not allowed to complain"); 2219 if (!LastDiagFromC2) 2220 ToCtx.getDiagnostics().notePriorDiagnosticFrom(FromCtx.getDiagnostics()); 2221 LastDiagFromC2 = true; 2222 return ToCtx.getDiagnostics().Report(Loc, DiagID); 2223 } 2224 2225 std::optional<unsigned> 2226 StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(RecordDecl *Anon) { 2227 ASTContext &Context = Anon->getASTContext(); 2228 QualType AnonTy = Context.getRecordType(Anon); 2229 2230 const auto *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext()); 2231 if (!Owner) 2232 return std::nullopt; 2233 2234 unsigned Index = 0; 2235 for (const auto *D : Owner->noload_decls()) { 2236 const auto *F = dyn_cast<FieldDecl>(D); 2237 if (!F) 2238 continue; 2239 2240 if (F->isAnonymousStructOrUnion()) { 2241 if (Context.hasSameType(F->getType(), AnonTy)) 2242 break; 2243 ++Index; 2244 continue; 2245 } 2246 2247 // If the field looks like this: 2248 // struct { ... } A; 2249 QualType FieldType = F->getType(); 2250 // In case of nested structs. 2251 while (const auto *ElabType = dyn_cast<ElaboratedType>(FieldType)) 2252 FieldType = ElabType->getNamedType(); 2253 2254 if (const auto *RecType = dyn_cast<RecordType>(FieldType)) { 2255 const RecordDecl *RecDecl = RecType->getDecl(); 2256 if (RecDecl->getDeclContext() == Owner && !RecDecl->getIdentifier()) { 2257 if (Context.hasSameType(FieldType, AnonTy)) 2258 break; 2259 ++Index; 2260 continue; 2261 } 2262 } 2263 } 2264 2265 return Index; 2266 } 2267 2268 unsigned StructuralEquivalenceContext::getApplicableDiagnostic( 2269 unsigned ErrorDiagnostic) { 2270 if (ErrorOnTagTypeMismatch) 2271 return ErrorDiagnostic; 2272 2273 switch (ErrorDiagnostic) { 2274 case diag::err_odr_variable_type_inconsistent: 2275 return diag::warn_odr_variable_type_inconsistent; 2276 case diag::err_odr_variable_multiple_def: 2277 return diag::warn_odr_variable_multiple_def; 2278 case diag::err_odr_function_type_inconsistent: 2279 return diag::warn_odr_function_type_inconsistent; 2280 case diag::err_odr_tag_type_inconsistent: 2281 return diag::warn_odr_tag_type_inconsistent; 2282 case diag::err_odr_field_type_inconsistent: 2283 return diag::warn_odr_field_type_inconsistent; 2284 case diag::err_odr_ivar_type_inconsistent: 2285 return diag::warn_odr_ivar_type_inconsistent; 2286 case diag::err_odr_objc_superclass_inconsistent: 2287 return diag::warn_odr_objc_superclass_inconsistent; 2288 case diag::err_odr_objc_method_result_type_inconsistent: 2289 return diag::warn_odr_objc_method_result_type_inconsistent; 2290 case diag::err_odr_objc_method_num_params_inconsistent: 2291 return diag::warn_odr_objc_method_num_params_inconsistent; 2292 case diag::err_odr_objc_method_param_type_inconsistent: 2293 return diag::warn_odr_objc_method_param_type_inconsistent; 2294 case diag::err_odr_objc_method_variadic_inconsistent: 2295 return diag::warn_odr_objc_method_variadic_inconsistent; 2296 case diag::err_odr_objc_property_type_inconsistent: 2297 return diag::warn_odr_objc_property_type_inconsistent; 2298 case diag::err_odr_objc_property_impl_kind_inconsistent: 2299 return diag::warn_odr_objc_property_impl_kind_inconsistent; 2300 case diag::err_odr_objc_synthesize_ivar_inconsistent: 2301 return diag::warn_odr_objc_synthesize_ivar_inconsistent; 2302 case diag::err_odr_different_num_template_parameters: 2303 return diag::warn_odr_different_num_template_parameters; 2304 case diag::err_odr_different_template_parameter_kind: 2305 return diag::warn_odr_different_template_parameter_kind; 2306 case diag::err_odr_parameter_pack_non_pack: 2307 return diag::warn_odr_parameter_pack_non_pack; 2308 case diag::err_odr_non_type_parameter_type_inconsistent: 2309 return diag::warn_odr_non_type_parameter_type_inconsistent; 2310 } 2311 llvm_unreachable("Diagnostic kind not handled in preceding switch"); 2312 } 2313 2314 bool StructuralEquivalenceContext::IsEquivalent(Decl *D1, Decl *D2) { 2315 2316 // Ensure that the implementation functions (all static functions in this TU) 2317 // never call the public ASTStructuralEquivalence::IsEquivalent() functions, 2318 // because that will wreak havoc the internal state (DeclsToCheck and 2319 // VisitedDecls members) and can cause faulty behaviour. 2320 // In other words: Do not start a graph search from a new node with the 2321 // internal data of another search in progress. 2322 // FIXME: Better encapsulation and separation of internal and public 2323 // functionality. 2324 assert(DeclsToCheck.empty()); 2325 assert(VisitedDecls.empty()); 2326 2327 if (!::IsStructurallyEquivalent(*this, D1, D2)) 2328 return false; 2329 2330 return !Finish(); 2331 } 2332 2333 bool StructuralEquivalenceContext::IsEquivalent(QualType T1, QualType T2) { 2334 assert(DeclsToCheck.empty()); 2335 assert(VisitedDecls.empty()); 2336 if (!::IsStructurallyEquivalent(*this, T1, T2)) 2337 return false; 2338 2339 return !Finish(); 2340 } 2341 2342 bool StructuralEquivalenceContext::IsEquivalent(Stmt *S1, Stmt *S2) { 2343 assert(DeclsToCheck.empty()); 2344 assert(VisitedDecls.empty()); 2345 if (!::IsStructurallyEquivalent(*this, S1, S2)) 2346 return false; 2347 2348 return !Finish(); 2349 } 2350 2351 bool StructuralEquivalenceContext::CheckCommonEquivalence(Decl *D1, Decl *D2) { 2352 // Check for equivalent described template. 2353 TemplateDecl *Template1 = D1->getDescribedTemplate(); 2354 TemplateDecl *Template2 = D2->getDescribedTemplate(); 2355 if ((Template1 != nullptr) != (Template2 != nullptr)) 2356 return false; 2357 if (Template1 && !IsStructurallyEquivalent(*this, Template1, Template2)) 2358 return false; 2359 2360 // FIXME: Move check for identifier names into this function. 2361 2362 return true; 2363 } 2364 2365 bool StructuralEquivalenceContext::CheckKindSpecificEquivalence( 2366 Decl *D1, Decl *D2) { 2367 2368 // Kind mismatch. 2369 if (D1->getKind() != D2->getKind()) 2370 return false; 2371 2372 // Cast the Decls to their actual subclass so that the right overload of 2373 // IsStructurallyEquivalent is called. 2374 switch (D1->getKind()) { 2375 #define ABSTRACT_DECL(DECL) 2376 #define DECL(DERIVED, BASE) \ 2377 case Decl::Kind::DERIVED: \ 2378 return ::IsStructurallyEquivalent(*this, static_cast<DERIVED##Decl *>(D1), \ 2379 static_cast<DERIVED##Decl *>(D2)); 2380 #include "clang/AST/DeclNodes.inc" 2381 } 2382 return true; 2383 } 2384 2385 bool StructuralEquivalenceContext::Finish() { 2386 while (!DeclsToCheck.empty()) { 2387 // Check the next declaration. 2388 std::pair<Decl *, Decl *> P = DeclsToCheck.front(); 2389 DeclsToCheck.pop(); 2390 2391 Decl *D1 = P.first; 2392 Decl *D2 = P.second; 2393 2394 bool Equivalent = 2395 CheckCommonEquivalence(D1, D2) && CheckKindSpecificEquivalence(D1, D2); 2396 2397 if (!Equivalent) { 2398 // Note that these two declarations are not equivalent (and we already 2399 // know about it). 2400 NonEquivalentDecls.insert(P); 2401 2402 return true; 2403 } 2404 } 2405 2406 return false; 2407 } 2408