ASTStructuralEquivalence.cpp 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155
  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. #include "clang/AST/ASTStructuralEquivalence.h"
  61. #include "clang/AST/ASTContext.h"
  62. #include "clang/AST/ASTDiagnostic.h"
  63. #include "clang/AST/Decl.h"
  64. #include "clang/AST/DeclBase.h"
  65. #include "clang/AST/DeclCXX.h"
  66. #include "clang/AST/DeclFriend.h"
  67. #include "clang/AST/DeclObjC.h"
  68. #include "clang/AST/DeclOpenMP.h"
  69. #include "clang/AST/DeclTemplate.h"
  70. #include "clang/AST/ExprCXX.h"
  71. #include "clang/AST/ExprConcepts.h"
  72. #include "clang/AST/ExprObjC.h"
  73. #include "clang/AST/ExprOpenMP.h"
  74. #include "clang/AST/NestedNameSpecifier.h"
  75. #include "clang/AST/StmtObjC.h"
  76. #include "clang/AST/StmtOpenMP.h"
  77. #include "clang/AST/TemplateBase.h"
  78. #include "clang/AST/TemplateName.h"
  79. #include "clang/AST/Type.h"
  80. #include "clang/Basic/ExceptionSpecificationType.h"
  81. #include "clang/Basic/IdentifierTable.h"
  82. #include "clang/Basic/LLVM.h"
  83. #include "clang/Basic/SourceLocation.h"
  84. #include "llvm/ADT/APInt.h"
  85. #include "llvm/ADT/APSInt.h"
  86. #include "llvm/ADT/None.h"
  87. #include "llvm/ADT/Optional.h"
  88. #include "llvm/ADT/StringExtras.h"
  89. #include "llvm/Support/Casting.h"
  90. #include "llvm/Support/Compiler.h"
  91. #include "llvm/Support/ErrorHandling.h"
  92. #include <cassert>
  93. #include <utility>
  94. using namespace clang;
  95. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  96. QualType T1, QualType T2);
  97. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  98. Decl *D1, Decl *D2);
  99. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  100. const TemplateArgument &Arg1,
  101. const TemplateArgument &Arg2);
  102. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  103. NestedNameSpecifier *NNS1,
  104. NestedNameSpecifier *NNS2);
  105. static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
  106. const IdentifierInfo *Name2);
  107. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  108. const DeclarationName Name1,
  109. const DeclarationName Name2) {
  110. if (Name1.getNameKind() != Name2.getNameKind())
  111. return false;
  112. switch (Name1.getNameKind()) {
  113. case DeclarationName::Identifier:
  114. return IsStructurallyEquivalent(Name1.getAsIdentifierInfo(),
  115. Name2.getAsIdentifierInfo());
  116. case DeclarationName::CXXConstructorName:
  117. case DeclarationName::CXXDestructorName:
  118. case DeclarationName::CXXConversionFunctionName:
  119. return IsStructurallyEquivalent(Context, Name1.getCXXNameType(),
  120. Name2.getCXXNameType());
  121. case DeclarationName::CXXDeductionGuideName: {
  122. if (!IsStructurallyEquivalent(
  123. Context, Name1.getCXXDeductionGuideTemplate()->getDeclName(),
  124. Name2.getCXXDeductionGuideTemplate()->getDeclName()))
  125. return false;
  126. return IsStructurallyEquivalent(Context,
  127. Name1.getCXXDeductionGuideTemplate(),
  128. Name2.getCXXDeductionGuideTemplate());
  129. }
  130. case DeclarationName::CXXOperatorName:
  131. return Name1.getCXXOverloadedOperator() == Name2.getCXXOverloadedOperator();
  132. case DeclarationName::CXXLiteralOperatorName:
  133. return IsStructurallyEquivalent(Name1.getCXXLiteralIdentifier(),
  134. Name2.getCXXLiteralIdentifier());
  135. case DeclarationName::CXXUsingDirective:
  136. return true; // FIXME When do we consider two using directives equal?
  137. case DeclarationName::ObjCZeroArgSelector:
  138. case DeclarationName::ObjCOneArgSelector:
  139. case DeclarationName::ObjCMultiArgSelector:
  140. return true; // FIXME
  141. }
  142. llvm_unreachable("Unhandled kind of DeclarationName");
  143. return true;
  144. }
  145. namespace {
  146. /// Encapsulates Stmt comparison logic.
  147. class StmtComparer {
  148. StructuralEquivalenceContext &Context;
  149. // IsStmtEquivalent overloads. Each overload compares a specific statement
  150. // and only has to compare the data that is specific to the specific statement
  151. // class. Should only be called from TraverseStmt.
  152. bool IsStmtEquivalent(const AddrLabelExpr *E1, const AddrLabelExpr *E2) {
  153. return IsStructurallyEquivalent(Context, E1->getLabel(), E2->getLabel());
  154. }
  155. bool IsStmtEquivalent(const AtomicExpr *E1, const AtomicExpr *E2) {
  156. return E1->getOp() == E2->getOp();
  157. }
  158. bool IsStmtEquivalent(const BinaryOperator *E1, const BinaryOperator *E2) {
  159. return E1->getOpcode() == E2->getOpcode();
  160. }
  161. bool IsStmtEquivalent(const CallExpr *E1, const CallExpr *E2) {
  162. // FIXME: IsStructurallyEquivalent requires non-const Decls.
  163. Decl *Callee1 = const_cast<Decl *>(E1->getCalleeDecl());
  164. Decl *Callee2 = const_cast<Decl *>(E2->getCalleeDecl());
  165. // Compare whether both calls know their callee.
  166. if (static_cast<bool>(Callee1) != static_cast<bool>(Callee2))
  167. return false;
  168. // Both calls have no callee, so nothing to do.
  169. if (!static_cast<bool>(Callee1))
  170. return true;
  171. assert(Callee2);
  172. return IsStructurallyEquivalent(Context, Callee1, Callee2);
  173. }
  174. bool IsStmtEquivalent(const CharacterLiteral *E1,
  175. const CharacterLiteral *E2) {
  176. return E1->getValue() == E2->getValue() && E1->getKind() == E2->getKind();
  177. }
  178. bool IsStmtEquivalent(const ChooseExpr *E1, const ChooseExpr *E2) {
  179. return true; // Semantics only depend on children.
  180. }
  181. bool IsStmtEquivalent(const CompoundStmt *E1, const CompoundStmt *E2) {
  182. // Number of children is actually checked by the generic children comparison
  183. // code, but a CompoundStmt is one of the few statements where the number of
  184. // children frequently differs and the number of statements is also always
  185. // precomputed. Directly comparing the number of children here is thus
  186. // just an optimization.
  187. return E1->size() == E2->size();
  188. }
  189. bool IsStmtEquivalent(const DependentScopeDeclRefExpr *DE1,
  190. const DependentScopeDeclRefExpr *DE2) {
  191. if (!IsStructurallyEquivalent(Context, DE1->getDeclName(),
  192. DE2->getDeclName()))
  193. return false;
  194. return IsStructurallyEquivalent(Context, DE1->getQualifier(),
  195. DE2->getQualifier());
  196. }
  197. bool IsStmtEquivalent(const Expr *E1, const Expr *E2) {
  198. return IsStructurallyEquivalent(Context, E1->getType(), E2->getType());
  199. }
  200. bool IsStmtEquivalent(const ExpressionTraitExpr *E1,
  201. const ExpressionTraitExpr *E2) {
  202. return E1->getTrait() == E2->getTrait() && E1->getValue() == E2->getValue();
  203. }
  204. bool IsStmtEquivalent(const FloatingLiteral *E1, const FloatingLiteral *E2) {
  205. return E1->isExact() == E2->isExact() && E1->getValue() == E2->getValue();
  206. }
  207. bool IsStmtEquivalent(const GenericSelectionExpr *E1,
  208. const GenericSelectionExpr *E2) {
  209. for (auto Pair : zip_longest(E1->getAssocTypeSourceInfos(),
  210. E2->getAssocTypeSourceInfos())) {
  211. Optional<TypeSourceInfo *> Child1 = std::get<0>(Pair);
  212. Optional<TypeSourceInfo *> Child2 = std::get<1>(Pair);
  213. // Skip this case if there are a different number of associated types.
  214. if (!Child1 || !Child2)
  215. return false;
  216. if (!IsStructurallyEquivalent(Context, (*Child1)->getType(),
  217. (*Child2)->getType()))
  218. return false;
  219. }
  220. return true;
  221. }
  222. bool IsStmtEquivalent(const ImplicitCastExpr *CastE1,
  223. const ImplicitCastExpr *CastE2) {
  224. return IsStructurallyEquivalent(Context, CastE1->getType(),
  225. CastE2->getType());
  226. }
  227. bool IsStmtEquivalent(const IntegerLiteral *E1, const IntegerLiteral *E2) {
  228. return E1->getValue() == E2->getValue();
  229. }
  230. bool IsStmtEquivalent(const MemberExpr *E1, const MemberExpr *E2) {
  231. return IsStructurallyEquivalent(Context, E1->getFoundDecl(),
  232. E2->getFoundDecl());
  233. }
  234. bool IsStmtEquivalent(const ObjCStringLiteral *E1,
  235. const ObjCStringLiteral *E2) {
  236. // Just wraps a StringLiteral child.
  237. return true;
  238. }
  239. bool IsStmtEquivalent(const Stmt *S1, const Stmt *S2) { return true; }
  240. bool IsStmtEquivalent(const SourceLocExpr *E1, const SourceLocExpr *E2) {
  241. return E1->getIdentKind() == E2->getIdentKind();
  242. }
  243. bool IsStmtEquivalent(const StmtExpr *E1, const StmtExpr *E2) {
  244. return E1->getTemplateDepth() == E2->getTemplateDepth();
  245. }
  246. bool IsStmtEquivalent(const StringLiteral *E1, const StringLiteral *E2) {
  247. return E1->getBytes() == E2->getBytes();
  248. }
  249. bool IsStmtEquivalent(const SubstNonTypeTemplateParmExpr *E1,
  250. const SubstNonTypeTemplateParmExpr *E2) {
  251. return IsStructurallyEquivalent(Context, E1->getParameter(),
  252. E2->getParameter());
  253. }
  254. bool IsStmtEquivalent(const SubstNonTypeTemplateParmPackExpr *E1,
  255. const SubstNonTypeTemplateParmPackExpr *E2) {
  256. return IsStructurallyEquivalent(Context, E1->getArgumentPack(),
  257. E2->getArgumentPack());
  258. }
  259. bool IsStmtEquivalent(const TypeTraitExpr *E1, const TypeTraitExpr *E2) {
  260. if (E1->getTrait() != E2->getTrait())
  261. return false;
  262. for (auto Pair : zip_longest(E1->getArgs(), E2->getArgs())) {
  263. Optional<TypeSourceInfo *> Child1 = std::get<0>(Pair);
  264. Optional<TypeSourceInfo *> Child2 = std::get<1>(Pair);
  265. // Different number of args.
  266. if (!Child1 || !Child2)
  267. return false;
  268. if (!IsStructurallyEquivalent(Context, (*Child1)->getType(),
  269. (*Child2)->getType()))
  270. return false;
  271. }
  272. return true;
  273. }
  274. bool IsStmtEquivalent(const UnaryExprOrTypeTraitExpr *E1,
  275. const UnaryExprOrTypeTraitExpr *E2) {
  276. if (E1->getKind() != E2->getKind())
  277. return false;
  278. return IsStructurallyEquivalent(Context, E1->getTypeOfArgument(),
  279. E2->getTypeOfArgument());
  280. }
  281. bool IsStmtEquivalent(const UnaryOperator *E1, const UnaryOperator *E2) {
  282. return E1->getOpcode() == E2->getOpcode();
  283. }
  284. bool IsStmtEquivalent(const VAArgExpr *E1, const VAArgExpr *E2) {
  285. // Semantics only depend on children.
  286. return true;
  287. }
  288. /// End point of the traversal chain.
  289. bool TraverseStmt(const Stmt *S1, const Stmt *S2) { return true; }
  290. // Create traversal methods that traverse the class hierarchy and return
  291. // the accumulated result of the comparison. Each TraverseStmt overload
  292. // calls the TraverseStmt overload of the parent class. For example,
  293. // the TraverseStmt overload for 'BinaryOperator' calls the TraverseStmt
  294. // overload of 'Expr' which then calls the overload for 'Stmt'.
  295. #define STMT(CLASS, PARENT) \
  296. bool TraverseStmt(const CLASS *S1, const CLASS *S2) { \
  297. if (!TraverseStmt(static_cast<const PARENT *>(S1), \
  298. static_cast<const PARENT *>(S2))) \
  299. return false; \
  300. return IsStmtEquivalent(S1, S2); \
  301. }
  302. #include "clang/AST/StmtNodes.inc"
  303. public:
  304. StmtComparer(StructuralEquivalenceContext &C) : Context(C) {}
  305. /// Determine whether two statements are equivalent. The statements have to
  306. /// be of the same kind. The children of the statements and their properties
  307. /// are not compared by this function.
  308. bool IsEquivalent(const Stmt *S1, const Stmt *S2) {
  309. if (S1->getStmtClass() != S2->getStmtClass())
  310. return false;
  311. // Each TraverseStmt walks the class hierarchy from the leaf class to
  312. // the root class 'Stmt' (e.g. 'BinaryOperator' -> 'Expr' -> 'Stmt'). Cast
  313. // the Stmt we have here to its specific subclass so that we call the
  314. // overload that walks the whole class hierarchy from leaf to root (e.g.,
  315. // cast to 'BinaryOperator' so that 'Expr' and 'Stmt' is traversed).
  316. switch (S1->getStmtClass()) {
  317. case Stmt::NoStmtClass:
  318. llvm_unreachable("Can't traverse NoStmtClass");
  319. #define STMT(CLASS, PARENT) \
  320. case Stmt::StmtClass::CLASS##Class: \
  321. return TraverseStmt(static_cast<const CLASS *>(S1), \
  322. static_cast<const CLASS *>(S2));
  323. #define ABSTRACT_STMT(S)
  324. #include "clang/AST/StmtNodes.inc"
  325. }
  326. llvm_unreachable("Invalid statement kind");
  327. }
  328. };
  329. } // namespace
  330. /// Determine structural equivalence of two statements.
  331. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  332. const Stmt *S1, const Stmt *S2) {
  333. if (!S1 || !S2)
  334. return S1 == S2;
  335. // Compare the statements itself.
  336. StmtComparer Comparer(Context);
  337. if (!Comparer.IsEquivalent(S1, S2))
  338. return false;
  339. // Iterate over the children of both statements and also compare them.
  340. for (auto Pair : zip_longest(S1->children(), S2->children())) {
  341. Optional<const Stmt *> Child1 = std::get<0>(Pair);
  342. Optional<const Stmt *> Child2 = std::get<1>(Pair);
  343. // One of the statements has a different amount of children than the other,
  344. // so the statements can't be equivalent.
  345. if (!Child1 || !Child2)
  346. return false;
  347. if (!IsStructurallyEquivalent(Context, *Child1, *Child2))
  348. return false;
  349. }
  350. return true;
  351. }
  352. /// Determine whether two identifiers are equivalent.
  353. static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
  354. const IdentifierInfo *Name2) {
  355. if (!Name1 || !Name2)
  356. return Name1 == Name2;
  357. return Name1->getName() == Name2->getName();
  358. }
  359. /// Determine whether two nested-name-specifiers are equivalent.
  360. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  361. NestedNameSpecifier *NNS1,
  362. NestedNameSpecifier *NNS2) {
  363. if (NNS1->getKind() != NNS2->getKind())
  364. return false;
  365. NestedNameSpecifier *Prefix1 = NNS1->getPrefix(),
  366. *Prefix2 = NNS2->getPrefix();
  367. if ((bool)Prefix1 != (bool)Prefix2)
  368. return false;
  369. if (Prefix1)
  370. if (!IsStructurallyEquivalent(Context, Prefix1, Prefix2))
  371. return false;
  372. switch (NNS1->getKind()) {
  373. case NestedNameSpecifier::Identifier:
  374. return IsStructurallyEquivalent(NNS1->getAsIdentifier(),
  375. NNS2->getAsIdentifier());
  376. case NestedNameSpecifier::Namespace:
  377. return IsStructurallyEquivalent(Context, NNS1->getAsNamespace(),
  378. NNS2->getAsNamespace());
  379. case NestedNameSpecifier::NamespaceAlias:
  380. return IsStructurallyEquivalent(Context, NNS1->getAsNamespaceAlias(),
  381. NNS2->getAsNamespaceAlias());
  382. case NestedNameSpecifier::TypeSpec:
  383. case NestedNameSpecifier::TypeSpecWithTemplate:
  384. return IsStructurallyEquivalent(Context, QualType(NNS1->getAsType(), 0),
  385. QualType(NNS2->getAsType(), 0));
  386. case NestedNameSpecifier::Global:
  387. return true;
  388. case NestedNameSpecifier::Super:
  389. return IsStructurallyEquivalent(Context, NNS1->getAsRecordDecl(),
  390. NNS2->getAsRecordDecl());
  391. }
  392. return false;
  393. }
  394. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  395. const TemplateName &N1,
  396. const TemplateName &N2) {
  397. TemplateDecl *TemplateDeclN1 = N1.getAsTemplateDecl();
  398. TemplateDecl *TemplateDeclN2 = N2.getAsTemplateDecl();
  399. if (TemplateDeclN1 && TemplateDeclN2) {
  400. if (!IsStructurallyEquivalent(Context, TemplateDeclN1, TemplateDeclN2))
  401. return false;
  402. // If the kind is different we compare only the template decl.
  403. if (N1.getKind() != N2.getKind())
  404. return true;
  405. } else if (TemplateDeclN1 || TemplateDeclN2)
  406. return false;
  407. else if (N1.getKind() != N2.getKind())
  408. return false;
  409. // Check for special case incompatibilities.
  410. switch (N1.getKind()) {
  411. case TemplateName::OverloadedTemplate: {
  412. OverloadedTemplateStorage *OS1 = N1.getAsOverloadedTemplate(),
  413. *OS2 = N2.getAsOverloadedTemplate();
  414. OverloadedTemplateStorage::iterator I1 = OS1->begin(), I2 = OS2->begin(),
  415. E1 = OS1->end(), E2 = OS2->end();
  416. for (; I1 != E1 && I2 != E2; ++I1, ++I2)
  417. if (!IsStructurallyEquivalent(Context, *I1, *I2))
  418. return false;
  419. return I1 == E1 && I2 == E2;
  420. }
  421. case TemplateName::AssumedTemplate: {
  422. AssumedTemplateStorage *TN1 = N1.getAsAssumedTemplateName(),
  423. *TN2 = N1.getAsAssumedTemplateName();
  424. return TN1->getDeclName() == TN2->getDeclName();
  425. }
  426. case TemplateName::DependentTemplate: {
  427. DependentTemplateName *DN1 = N1.getAsDependentTemplateName(),
  428. *DN2 = N2.getAsDependentTemplateName();
  429. if (!IsStructurallyEquivalent(Context, DN1->getQualifier(),
  430. DN2->getQualifier()))
  431. return false;
  432. if (DN1->isIdentifier() && DN2->isIdentifier())
  433. return IsStructurallyEquivalent(DN1->getIdentifier(),
  434. DN2->getIdentifier());
  435. else if (DN1->isOverloadedOperator() && DN2->isOverloadedOperator())
  436. return DN1->getOperator() == DN2->getOperator();
  437. return false;
  438. }
  439. case TemplateName::SubstTemplateTemplateParmPack: {
  440. SubstTemplateTemplateParmPackStorage
  441. *P1 = N1.getAsSubstTemplateTemplateParmPack(),
  442. *P2 = N2.getAsSubstTemplateTemplateParmPack();
  443. return IsStructurallyEquivalent(Context, P1->getArgumentPack(),
  444. P2->getArgumentPack()) &&
  445. IsStructurallyEquivalent(Context, P1->getParameterPack(),
  446. P2->getParameterPack());
  447. }
  448. case TemplateName::Template:
  449. case TemplateName::QualifiedTemplate:
  450. case TemplateName::SubstTemplateTemplateParm:
  451. // It is sufficient to check value of getAsTemplateDecl.
  452. break;
  453. }
  454. return true;
  455. }
  456. /// Determine whether two template arguments are equivalent.
  457. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  458. const TemplateArgument &Arg1,
  459. const TemplateArgument &Arg2) {
  460. if (Arg1.getKind() != Arg2.getKind())
  461. return false;
  462. switch (Arg1.getKind()) {
  463. case TemplateArgument::Null:
  464. return true;
  465. case TemplateArgument::Type:
  466. return IsStructurallyEquivalent(Context, Arg1.getAsType(), Arg2.getAsType());
  467. case TemplateArgument::Integral:
  468. if (!IsStructurallyEquivalent(Context, Arg1.getIntegralType(),
  469. Arg2.getIntegralType()))
  470. return false;
  471. return llvm::APSInt::isSameValue(Arg1.getAsIntegral(),
  472. Arg2.getAsIntegral());
  473. case TemplateArgument::Declaration:
  474. return IsStructurallyEquivalent(Context, Arg1.getAsDecl(), Arg2.getAsDecl());
  475. case TemplateArgument::NullPtr:
  476. return true; // FIXME: Is this correct?
  477. case TemplateArgument::Template:
  478. return IsStructurallyEquivalent(Context, Arg1.getAsTemplate(),
  479. Arg2.getAsTemplate());
  480. case TemplateArgument::TemplateExpansion:
  481. return IsStructurallyEquivalent(Context,
  482. Arg1.getAsTemplateOrTemplatePattern(),
  483. Arg2.getAsTemplateOrTemplatePattern());
  484. case TemplateArgument::Expression:
  485. return IsStructurallyEquivalent(Context, Arg1.getAsExpr(),
  486. Arg2.getAsExpr());
  487. case TemplateArgument::Pack:
  488. if (Arg1.pack_size() != Arg2.pack_size())
  489. return false;
  490. for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
  491. if (!IsStructurallyEquivalent(Context, Arg1.pack_begin()[I],
  492. Arg2.pack_begin()[I]))
  493. return false;
  494. return true;
  495. }
  496. llvm_unreachable("Invalid template argument kind");
  497. }
  498. /// Determine structural equivalence for the common part of array
  499. /// types.
  500. static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
  501. const ArrayType *Array1,
  502. const ArrayType *Array2) {
  503. if (!IsStructurallyEquivalent(Context, Array1->getElementType(),
  504. Array2->getElementType()))
  505. return false;
  506. if (Array1->getSizeModifier() != Array2->getSizeModifier())
  507. return false;
  508. if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
  509. return false;
  510. return true;
  511. }
  512. /// Determine structural equivalence based on the ExtInfo of functions. This
  513. /// is inspired by ASTContext::mergeFunctionTypes(), we compare calling
  514. /// conventions bits but must not compare some other bits.
  515. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  516. FunctionType::ExtInfo EI1,
  517. FunctionType::ExtInfo EI2) {
  518. // Compatible functions must have compatible calling conventions.
  519. if (EI1.getCC() != EI2.getCC())
  520. return false;
  521. // Regparm is part of the calling convention.
  522. if (EI1.getHasRegParm() != EI2.getHasRegParm())
  523. return false;
  524. if (EI1.getRegParm() != EI2.getRegParm())
  525. return false;
  526. if (EI1.getProducesResult() != EI2.getProducesResult())
  527. return false;
  528. if (EI1.getNoCallerSavedRegs() != EI2.getNoCallerSavedRegs())
  529. return false;
  530. if (EI1.getNoCfCheck() != EI2.getNoCfCheck())
  531. return false;
  532. return true;
  533. }
  534. /// Check the equivalence of exception specifications.
  535. static bool IsEquivalentExceptionSpec(StructuralEquivalenceContext &Context,
  536. const FunctionProtoType *Proto1,
  537. const FunctionProtoType *Proto2) {
  538. auto Spec1 = Proto1->getExceptionSpecType();
  539. auto Spec2 = Proto2->getExceptionSpecType();
  540. if (isUnresolvedExceptionSpec(Spec1) || isUnresolvedExceptionSpec(Spec2))
  541. return true;
  542. if (Spec1 != Spec2)
  543. return false;
  544. if (Spec1 == EST_Dynamic) {
  545. if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
  546. return false;
  547. for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
  548. if (!IsStructurallyEquivalent(Context, Proto1->getExceptionType(I),
  549. Proto2->getExceptionType(I)))
  550. return false;
  551. }
  552. } else if (isComputedNoexcept(Spec1)) {
  553. if (!IsStructurallyEquivalent(Context, Proto1->getNoexceptExpr(),
  554. Proto2->getNoexceptExpr()))
  555. return false;
  556. }
  557. return true;
  558. }
  559. /// Determine structural equivalence of two types.
  560. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  561. QualType T1, QualType T2) {
  562. if (T1.isNull() || T2.isNull())
  563. return T1.isNull() && T2.isNull();
  564. QualType OrigT1 = T1;
  565. QualType OrigT2 = T2;
  566. if (!Context.StrictTypeSpelling) {
  567. // We aren't being strict about token-to-token equivalence of types,
  568. // so map down to the canonical type.
  569. T1 = Context.FromCtx.getCanonicalType(T1);
  570. T2 = Context.ToCtx.getCanonicalType(T2);
  571. }
  572. if (T1.getQualifiers() != T2.getQualifiers())
  573. return false;
  574. Type::TypeClass TC = T1->getTypeClass();
  575. if (T1->getTypeClass() != T2->getTypeClass()) {
  576. // Compare function types with prototypes vs. without prototypes as if
  577. // both did not have prototypes.
  578. if (T1->getTypeClass() == Type::FunctionProto &&
  579. T2->getTypeClass() == Type::FunctionNoProto)
  580. TC = Type::FunctionNoProto;
  581. else if (T1->getTypeClass() == Type::FunctionNoProto &&
  582. T2->getTypeClass() == Type::FunctionProto)
  583. TC = Type::FunctionNoProto;
  584. else
  585. return false;
  586. }
  587. switch (TC) {
  588. case Type::Builtin:
  589. // FIXME: Deal with Char_S/Char_U.
  590. if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
  591. return false;
  592. break;
  593. case Type::Complex:
  594. if (!IsStructurallyEquivalent(Context,
  595. cast<ComplexType>(T1)->getElementType(),
  596. cast<ComplexType>(T2)->getElementType()))
  597. return false;
  598. break;
  599. case Type::Adjusted:
  600. case Type::Decayed:
  601. if (!IsStructurallyEquivalent(Context,
  602. cast<AdjustedType>(T1)->getOriginalType(),
  603. cast<AdjustedType>(T2)->getOriginalType()))
  604. return false;
  605. break;
  606. case Type::Pointer:
  607. if (!IsStructurallyEquivalent(Context,
  608. cast<PointerType>(T1)->getPointeeType(),
  609. cast<PointerType>(T2)->getPointeeType()))
  610. return false;
  611. break;
  612. case Type::BlockPointer:
  613. if (!IsStructurallyEquivalent(Context,
  614. cast<BlockPointerType>(T1)->getPointeeType(),
  615. cast<BlockPointerType>(T2)->getPointeeType()))
  616. return false;
  617. break;
  618. case Type::LValueReference:
  619. case Type::RValueReference: {
  620. const auto *Ref1 = cast<ReferenceType>(T1);
  621. const auto *Ref2 = cast<ReferenceType>(T2);
  622. if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
  623. return false;
  624. if (Ref1->isInnerRef() != Ref2->isInnerRef())
  625. return false;
  626. if (!IsStructurallyEquivalent(Context, Ref1->getPointeeTypeAsWritten(),
  627. Ref2->getPointeeTypeAsWritten()))
  628. return false;
  629. break;
  630. }
  631. case Type::MemberPointer: {
  632. const auto *MemPtr1 = cast<MemberPointerType>(T1);
  633. const auto *MemPtr2 = cast<MemberPointerType>(T2);
  634. if (!IsStructurallyEquivalent(Context, MemPtr1->getPointeeType(),
  635. MemPtr2->getPointeeType()))
  636. return false;
  637. if (!IsStructurallyEquivalent(Context, QualType(MemPtr1->getClass(), 0),
  638. QualType(MemPtr2->getClass(), 0)))
  639. return false;
  640. break;
  641. }
  642. case Type::ConstantArray: {
  643. const auto *Array1 = cast<ConstantArrayType>(T1);
  644. const auto *Array2 = cast<ConstantArrayType>(T2);
  645. if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
  646. return false;
  647. if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
  648. return false;
  649. break;
  650. }
  651. case Type::IncompleteArray:
  652. if (!IsArrayStructurallyEquivalent(Context, cast<ArrayType>(T1),
  653. cast<ArrayType>(T2)))
  654. return false;
  655. break;
  656. case Type::VariableArray: {
  657. const auto *Array1 = cast<VariableArrayType>(T1);
  658. const auto *Array2 = cast<VariableArrayType>(T2);
  659. if (!IsStructurallyEquivalent(Context, Array1->getSizeExpr(),
  660. Array2->getSizeExpr()))
  661. return false;
  662. if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
  663. return false;
  664. break;
  665. }
  666. case Type::DependentSizedArray: {
  667. const auto *Array1 = cast<DependentSizedArrayType>(T1);
  668. const auto *Array2 = cast<DependentSizedArrayType>(T2);
  669. if (!IsStructurallyEquivalent(Context, Array1->getSizeExpr(),
  670. Array2->getSizeExpr()))
  671. return false;
  672. if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
  673. return false;
  674. break;
  675. }
  676. case Type::DependentAddressSpace: {
  677. const auto *DepAddressSpace1 = cast<DependentAddressSpaceType>(T1);
  678. const auto *DepAddressSpace2 = cast<DependentAddressSpaceType>(T2);
  679. if (!IsStructurallyEquivalent(Context, DepAddressSpace1->getAddrSpaceExpr(),
  680. DepAddressSpace2->getAddrSpaceExpr()))
  681. return false;
  682. if (!IsStructurallyEquivalent(Context, DepAddressSpace1->getPointeeType(),
  683. DepAddressSpace2->getPointeeType()))
  684. return false;
  685. break;
  686. }
  687. case Type::DependentSizedExtVector: {
  688. const auto *Vec1 = cast<DependentSizedExtVectorType>(T1);
  689. const auto *Vec2 = cast<DependentSizedExtVectorType>(T2);
  690. if (!IsStructurallyEquivalent(Context, Vec1->getSizeExpr(),
  691. Vec2->getSizeExpr()))
  692. return false;
  693. if (!IsStructurallyEquivalent(Context, Vec1->getElementType(),
  694. Vec2->getElementType()))
  695. return false;
  696. break;
  697. }
  698. case Type::DependentVector: {
  699. const auto *Vec1 = cast<DependentVectorType>(T1);
  700. const auto *Vec2 = cast<DependentVectorType>(T2);
  701. if (Vec1->getVectorKind() != Vec2->getVectorKind())
  702. return false;
  703. if (!IsStructurallyEquivalent(Context, Vec1->getSizeExpr(),
  704. Vec2->getSizeExpr()))
  705. return false;
  706. if (!IsStructurallyEquivalent(Context, Vec1->getElementType(),
  707. Vec2->getElementType()))
  708. return false;
  709. break;
  710. }
  711. case Type::Vector:
  712. case Type::ExtVector: {
  713. const auto *Vec1 = cast<VectorType>(T1);
  714. const auto *Vec2 = cast<VectorType>(T2);
  715. if (!IsStructurallyEquivalent(Context, Vec1->getElementType(),
  716. Vec2->getElementType()))
  717. return false;
  718. if (Vec1->getNumElements() != Vec2->getNumElements())
  719. return false;
  720. if (Vec1->getVectorKind() != Vec2->getVectorKind())
  721. return false;
  722. break;
  723. }
  724. case Type::DependentSizedMatrix: {
  725. const DependentSizedMatrixType *Mat1 = cast<DependentSizedMatrixType>(T1);
  726. const DependentSizedMatrixType *Mat2 = cast<DependentSizedMatrixType>(T2);
  727. // The element types, row and column expressions must be structurally
  728. // equivalent.
  729. if (!IsStructurallyEquivalent(Context, Mat1->getRowExpr(),
  730. Mat2->getRowExpr()) ||
  731. !IsStructurallyEquivalent(Context, Mat1->getColumnExpr(),
  732. Mat2->getColumnExpr()) ||
  733. !IsStructurallyEquivalent(Context, Mat1->getElementType(),
  734. Mat2->getElementType()))
  735. return false;
  736. break;
  737. }
  738. case Type::ConstantMatrix: {
  739. const ConstantMatrixType *Mat1 = cast<ConstantMatrixType>(T1);
  740. const ConstantMatrixType *Mat2 = cast<ConstantMatrixType>(T2);
  741. // The element types must be structurally equivalent and the number of rows
  742. // and columns must match.
  743. if (!IsStructurallyEquivalent(Context, Mat1->getElementType(),
  744. Mat2->getElementType()) ||
  745. Mat1->getNumRows() != Mat2->getNumRows() ||
  746. Mat1->getNumColumns() != Mat2->getNumColumns())
  747. return false;
  748. break;
  749. }
  750. case Type::FunctionProto: {
  751. const auto *Proto1 = cast<FunctionProtoType>(T1);
  752. const auto *Proto2 = cast<FunctionProtoType>(T2);
  753. if (Proto1->getNumParams() != Proto2->getNumParams())
  754. return false;
  755. for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) {
  756. if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I),
  757. Proto2->getParamType(I)))
  758. return false;
  759. }
  760. if (Proto1->isVariadic() != Proto2->isVariadic())
  761. return false;
  762. if (Proto1->getMethodQuals() != Proto2->getMethodQuals())
  763. return false;
  764. // Check exceptions, this information is lost in canonical type.
  765. const auto *OrigProto1 =
  766. cast<FunctionProtoType>(OrigT1.getDesugaredType(Context.FromCtx));
  767. const auto *OrigProto2 =
  768. cast<FunctionProtoType>(OrigT2.getDesugaredType(Context.ToCtx));
  769. if (!IsEquivalentExceptionSpec(Context, OrigProto1, OrigProto2))
  770. return false;
  771. // Fall through to check the bits common with FunctionNoProtoType.
  772. LLVM_FALLTHROUGH;
  773. }
  774. case Type::FunctionNoProto: {
  775. const auto *Function1 = cast<FunctionType>(T1);
  776. const auto *Function2 = cast<FunctionType>(T2);
  777. if (!IsStructurallyEquivalent(Context, Function1->getReturnType(),
  778. Function2->getReturnType()))
  779. return false;
  780. if (!IsStructurallyEquivalent(Context, Function1->getExtInfo(),
  781. Function2->getExtInfo()))
  782. return false;
  783. break;
  784. }
  785. case Type::UnresolvedUsing:
  786. if (!IsStructurallyEquivalent(Context,
  787. cast<UnresolvedUsingType>(T1)->getDecl(),
  788. cast<UnresolvedUsingType>(T2)->getDecl()))
  789. return false;
  790. break;
  791. case Type::Attributed:
  792. if (!IsStructurallyEquivalent(Context,
  793. cast<AttributedType>(T1)->getModifiedType(),
  794. cast<AttributedType>(T2)->getModifiedType()))
  795. return false;
  796. if (!IsStructurallyEquivalent(
  797. Context, cast<AttributedType>(T1)->getEquivalentType(),
  798. cast<AttributedType>(T2)->getEquivalentType()))
  799. return false;
  800. break;
  801. case Type::Paren:
  802. if (!IsStructurallyEquivalent(Context, cast<ParenType>(T1)->getInnerType(),
  803. cast<ParenType>(T2)->getInnerType()))
  804. return false;
  805. break;
  806. case Type::MacroQualified:
  807. if (!IsStructurallyEquivalent(
  808. Context, cast<MacroQualifiedType>(T1)->getUnderlyingType(),
  809. cast<MacroQualifiedType>(T2)->getUnderlyingType()))
  810. return false;
  811. break;
  812. case Type::Using:
  813. if (!IsStructurallyEquivalent(Context, cast<UsingType>(T1)->getFoundDecl(),
  814. cast<UsingType>(T2)->getFoundDecl()))
  815. return false;
  816. break;
  817. case Type::Typedef:
  818. if (!IsStructurallyEquivalent(Context, cast<TypedefType>(T1)->getDecl(),
  819. cast<TypedefType>(T2)->getDecl()))
  820. return false;
  821. break;
  822. case Type::TypeOfExpr:
  823. if (!IsStructurallyEquivalent(
  824. Context, cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
  825. cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
  826. return false;
  827. break;
  828. case Type::TypeOf:
  829. if (!IsStructurallyEquivalent(Context,
  830. cast<TypeOfType>(T1)->getUnderlyingType(),
  831. cast<TypeOfType>(T2)->getUnderlyingType()))
  832. return false;
  833. break;
  834. case Type::UnaryTransform:
  835. if (!IsStructurallyEquivalent(
  836. Context, cast<UnaryTransformType>(T1)->getUnderlyingType(),
  837. cast<UnaryTransformType>(T2)->getUnderlyingType()))
  838. return false;
  839. break;
  840. case Type::Decltype:
  841. if (!IsStructurallyEquivalent(Context,
  842. cast<DecltypeType>(T1)->getUnderlyingExpr(),
  843. cast<DecltypeType>(T2)->getUnderlyingExpr()))
  844. return false;
  845. break;
  846. case Type::Auto: {
  847. auto *Auto1 = cast<AutoType>(T1);
  848. auto *Auto2 = cast<AutoType>(T2);
  849. if (!IsStructurallyEquivalent(Context, Auto1->getDeducedType(),
  850. Auto2->getDeducedType()))
  851. return false;
  852. if (Auto1->isConstrained() != Auto2->isConstrained())
  853. return false;
  854. if (Auto1->isConstrained()) {
  855. if (Auto1->getTypeConstraintConcept() !=
  856. Auto2->getTypeConstraintConcept())
  857. return false;
  858. ArrayRef<TemplateArgument> Auto1Args =
  859. Auto1->getTypeConstraintArguments();
  860. ArrayRef<TemplateArgument> Auto2Args =
  861. Auto2->getTypeConstraintArguments();
  862. if (Auto1Args.size() != Auto2Args.size())
  863. return false;
  864. for (unsigned I = 0, N = Auto1Args.size(); I != N; ++I) {
  865. if (!IsStructurallyEquivalent(Context, Auto1Args[I], Auto2Args[I]))
  866. return false;
  867. }
  868. }
  869. break;
  870. }
  871. case Type::DeducedTemplateSpecialization: {
  872. const auto *DT1 = cast<DeducedTemplateSpecializationType>(T1);
  873. const auto *DT2 = cast<DeducedTemplateSpecializationType>(T2);
  874. if (!IsStructurallyEquivalent(Context, DT1->getTemplateName(),
  875. DT2->getTemplateName()))
  876. return false;
  877. if (!IsStructurallyEquivalent(Context, DT1->getDeducedType(),
  878. DT2->getDeducedType()))
  879. return false;
  880. break;
  881. }
  882. case Type::Record:
  883. case Type::Enum:
  884. if (!IsStructurallyEquivalent(Context, cast<TagType>(T1)->getDecl(),
  885. cast<TagType>(T2)->getDecl()))
  886. return false;
  887. break;
  888. case Type::TemplateTypeParm: {
  889. const auto *Parm1 = cast<TemplateTypeParmType>(T1);
  890. const auto *Parm2 = cast<TemplateTypeParmType>(T2);
  891. if (Parm1->getDepth() != Parm2->getDepth())
  892. return false;
  893. if (Parm1->getIndex() != Parm2->getIndex())
  894. return false;
  895. if (Parm1->isParameterPack() != Parm2->isParameterPack())
  896. return false;
  897. // Names of template type parameters are never significant.
  898. break;
  899. }
  900. case Type::SubstTemplateTypeParm: {
  901. const auto *Subst1 = cast<SubstTemplateTypeParmType>(T1);
  902. const auto *Subst2 = cast<SubstTemplateTypeParmType>(T2);
  903. if (!IsStructurallyEquivalent(Context,
  904. QualType(Subst1->getReplacedParameter(), 0),
  905. QualType(Subst2->getReplacedParameter(), 0)))
  906. return false;
  907. if (!IsStructurallyEquivalent(Context, Subst1->getReplacementType(),
  908. Subst2->getReplacementType()))
  909. return false;
  910. break;
  911. }
  912. case Type::SubstTemplateTypeParmPack: {
  913. const auto *Subst1 = cast<SubstTemplateTypeParmPackType>(T1);
  914. const auto *Subst2 = cast<SubstTemplateTypeParmPackType>(T2);
  915. if (!IsStructurallyEquivalent(Context,
  916. QualType(Subst1->getReplacedParameter(), 0),
  917. QualType(Subst2->getReplacedParameter(), 0)))
  918. return false;
  919. if (!IsStructurallyEquivalent(Context, Subst1->getArgumentPack(),
  920. Subst2->getArgumentPack()))
  921. return false;
  922. break;
  923. }
  924. case Type::TemplateSpecialization: {
  925. const auto *Spec1 = cast<TemplateSpecializationType>(T1);
  926. const auto *Spec2 = cast<TemplateSpecializationType>(T2);
  927. if (!IsStructurallyEquivalent(Context, Spec1->getTemplateName(),
  928. Spec2->getTemplateName()))
  929. return false;
  930. if (Spec1->getNumArgs() != Spec2->getNumArgs())
  931. return false;
  932. for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
  933. if (!IsStructurallyEquivalent(Context, Spec1->getArg(I),
  934. Spec2->getArg(I)))
  935. return false;
  936. }
  937. break;
  938. }
  939. case Type::Elaborated: {
  940. const auto *Elab1 = cast<ElaboratedType>(T1);
  941. const auto *Elab2 = cast<ElaboratedType>(T2);
  942. // CHECKME: what if a keyword is ETK_None or ETK_typename ?
  943. if (Elab1->getKeyword() != Elab2->getKeyword())
  944. return false;
  945. if (!IsStructurallyEquivalent(Context, Elab1->getQualifier(),
  946. Elab2->getQualifier()))
  947. return false;
  948. if (!IsStructurallyEquivalent(Context, Elab1->getNamedType(),
  949. Elab2->getNamedType()))
  950. return false;
  951. break;
  952. }
  953. case Type::InjectedClassName: {
  954. const auto *Inj1 = cast<InjectedClassNameType>(T1);
  955. const auto *Inj2 = cast<InjectedClassNameType>(T2);
  956. if (!IsStructurallyEquivalent(Context,
  957. Inj1->getInjectedSpecializationType(),
  958. Inj2->getInjectedSpecializationType()))
  959. return false;
  960. break;
  961. }
  962. case Type::DependentName: {
  963. const auto *Typename1 = cast<DependentNameType>(T1);
  964. const auto *Typename2 = cast<DependentNameType>(T2);
  965. if (!IsStructurallyEquivalent(Context, Typename1->getQualifier(),
  966. Typename2->getQualifier()))
  967. return false;
  968. if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
  969. Typename2->getIdentifier()))
  970. return false;
  971. break;
  972. }
  973. case Type::DependentTemplateSpecialization: {
  974. const auto *Spec1 = cast<DependentTemplateSpecializationType>(T1);
  975. const auto *Spec2 = cast<DependentTemplateSpecializationType>(T2);
  976. if (!IsStructurallyEquivalent(Context, Spec1->getQualifier(),
  977. Spec2->getQualifier()))
  978. return false;
  979. if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
  980. Spec2->getIdentifier()))
  981. return false;
  982. if (Spec1->getNumArgs() != Spec2->getNumArgs())
  983. return false;
  984. for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
  985. if (!IsStructurallyEquivalent(Context, Spec1->getArg(I),
  986. Spec2->getArg(I)))
  987. return false;
  988. }
  989. break;
  990. }
  991. case Type::PackExpansion:
  992. if (!IsStructurallyEquivalent(Context,
  993. cast<PackExpansionType>(T1)->getPattern(),
  994. cast<PackExpansionType>(T2)->getPattern()))
  995. return false;
  996. break;
  997. case Type::ObjCInterface: {
  998. const auto *Iface1 = cast<ObjCInterfaceType>(T1);
  999. const auto *Iface2 = cast<ObjCInterfaceType>(T2);
  1000. if (!IsStructurallyEquivalent(Context, Iface1->getDecl(),
  1001. Iface2->getDecl()))
  1002. return false;
  1003. break;
  1004. }
  1005. case Type::ObjCTypeParam: {
  1006. const auto *Obj1 = cast<ObjCTypeParamType>(T1);
  1007. const auto *Obj2 = cast<ObjCTypeParamType>(T2);
  1008. if (!IsStructurallyEquivalent(Context, Obj1->getDecl(), Obj2->getDecl()))
  1009. return false;
  1010. if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
  1011. return false;
  1012. for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
  1013. if (!IsStructurallyEquivalent(Context, Obj1->getProtocol(I),
  1014. Obj2->getProtocol(I)))
  1015. return false;
  1016. }
  1017. break;
  1018. }
  1019. case Type::ObjCObject: {
  1020. const auto *Obj1 = cast<ObjCObjectType>(T1);
  1021. const auto *Obj2 = cast<ObjCObjectType>(T2);
  1022. if (!IsStructurallyEquivalent(Context, Obj1->getBaseType(),
  1023. Obj2->getBaseType()))
  1024. return false;
  1025. if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
  1026. return false;
  1027. for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
  1028. if (!IsStructurallyEquivalent(Context, Obj1->getProtocol(I),
  1029. Obj2->getProtocol(I)))
  1030. return false;
  1031. }
  1032. break;
  1033. }
  1034. case Type::ObjCObjectPointer: {
  1035. const auto *Ptr1 = cast<ObjCObjectPointerType>(T1);
  1036. const auto *Ptr2 = cast<ObjCObjectPointerType>(T2);
  1037. if (!IsStructurallyEquivalent(Context, Ptr1->getPointeeType(),
  1038. Ptr2->getPointeeType()))
  1039. return false;
  1040. break;
  1041. }
  1042. case Type::Atomic:
  1043. if (!IsStructurallyEquivalent(Context, cast<AtomicType>(T1)->getValueType(),
  1044. cast<AtomicType>(T2)->getValueType()))
  1045. return false;
  1046. break;
  1047. case Type::Pipe:
  1048. if (!IsStructurallyEquivalent(Context, cast<PipeType>(T1)->getElementType(),
  1049. cast<PipeType>(T2)->getElementType()))
  1050. return false;
  1051. break;
  1052. case Type::BitInt: {
  1053. const auto *Int1 = cast<BitIntType>(T1);
  1054. const auto *Int2 = cast<BitIntType>(T2);
  1055. if (Int1->isUnsigned() != Int2->isUnsigned() ||
  1056. Int1->getNumBits() != Int2->getNumBits())
  1057. return false;
  1058. break;
  1059. }
  1060. case Type::DependentBitInt: {
  1061. const auto *Int1 = cast<DependentBitIntType>(T1);
  1062. const auto *Int2 = cast<DependentBitIntType>(T2);
  1063. if (Int1->isUnsigned() != Int2->isUnsigned() ||
  1064. !IsStructurallyEquivalent(Context, Int1->getNumBitsExpr(),
  1065. Int2->getNumBitsExpr()))
  1066. return false;
  1067. }
  1068. } // end switch
  1069. return true;
  1070. }
  1071. /// Determine structural equivalence of two fields.
  1072. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1073. FieldDecl *Field1, FieldDecl *Field2) {
  1074. const auto *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
  1075. // For anonymous structs/unions, match up the anonymous struct/union type
  1076. // declarations directly, so that we don't go off searching for anonymous
  1077. // types
  1078. if (Field1->isAnonymousStructOrUnion() &&
  1079. Field2->isAnonymousStructOrUnion()) {
  1080. RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl();
  1081. RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl();
  1082. return IsStructurallyEquivalent(Context, D1, D2);
  1083. }
  1084. // Check for equivalent field names.
  1085. IdentifierInfo *Name1 = Field1->getIdentifier();
  1086. IdentifierInfo *Name2 = Field2->getIdentifier();
  1087. if (!::IsStructurallyEquivalent(Name1, Name2)) {
  1088. if (Context.Complain) {
  1089. Context.Diag2(
  1090. Owner2->getLocation(),
  1091. Context.getApplicableDiagnostic(diag::err_odr_tag_type_inconsistent))
  1092. << Context.ToCtx.getTypeDeclType(Owner2);
  1093. Context.Diag2(Field2->getLocation(), diag::note_odr_field_name)
  1094. << Field2->getDeclName();
  1095. Context.Diag1(Field1->getLocation(), diag::note_odr_field_name)
  1096. << Field1->getDeclName();
  1097. }
  1098. return false;
  1099. }
  1100. if (!IsStructurallyEquivalent(Context, Field1->getType(),
  1101. Field2->getType())) {
  1102. if (Context.Complain) {
  1103. Context.Diag2(
  1104. Owner2->getLocation(),
  1105. Context.getApplicableDiagnostic(diag::err_odr_tag_type_inconsistent))
  1106. << Context.ToCtx.getTypeDeclType(Owner2);
  1107. Context.Diag2(Field2->getLocation(), diag::note_odr_field)
  1108. << Field2->getDeclName() << Field2->getType();
  1109. Context.Diag1(Field1->getLocation(), diag::note_odr_field)
  1110. << Field1->getDeclName() << Field1->getType();
  1111. }
  1112. return false;
  1113. }
  1114. if (Field1->isBitField())
  1115. return IsStructurallyEquivalent(Context, Field1->getBitWidth(),
  1116. Field2->getBitWidth());
  1117. return true;
  1118. }
  1119. /// Determine structural equivalence of two methods.
  1120. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1121. CXXMethodDecl *Method1,
  1122. CXXMethodDecl *Method2) {
  1123. bool PropertiesEqual =
  1124. Method1->getDeclKind() == Method2->getDeclKind() &&
  1125. Method1->getRefQualifier() == Method2->getRefQualifier() &&
  1126. Method1->getAccess() == Method2->getAccess() &&
  1127. Method1->getOverloadedOperator() == Method2->getOverloadedOperator() &&
  1128. Method1->isStatic() == Method2->isStatic() &&
  1129. Method1->isConst() == Method2->isConst() &&
  1130. Method1->isVolatile() == Method2->isVolatile() &&
  1131. Method1->isVirtual() == Method2->isVirtual() &&
  1132. Method1->isPure() == Method2->isPure() &&
  1133. Method1->isDefaulted() == Method2->isDefaulted() &&
  1134. Method1->isDeleted() == Method2->isDeleted();
  1135. if (!PropertiesEqual)
  1136. return false;
  1137. // FIXME: Check for 'final'.
  1138. if (auto *Constructor1 = dyn_cast<CXXConstructorDecl>(Method1)) {
  1139. auto *Constructor2 = cast<CXXConstructorDecl>(Method2);
  1140. if (!Constructor1->getExplicitSpecifier().isEquivalent(
  1141. Constructor2->getExplicitSpecifier()))
  1142. return false;
  1143. }
  1144. if (auto *Conversion1 = dyn_cast<CXXConversionDecl>(Method1)) {
  1145. auto *Conversion2 = cast<CXXConversionDecl>(Method2);
  1146. if (!Conversion1->getExplicitSpecifier().isEquivalent(
  1147. Conversion2->getExplicitSpecifier()))
  1148. return false;
  1149. if (!IsStructurallyEquivalent(Context, Conversion1->getConversionType(),
  1150. Conversion2->getConversionType()))
  1151. return false;
  1152. }
  1153. const IdentifierInfo *Name1 = Method1->getIdentifier();
  1154. const IdentifierInfo *Name2 = Method2->getIdentifier();
  1155. if (!::IsStructurallyEquivalent(Name1, Name2)) {
  1156. return false;
  1157. // TODO: Names do not match, add warning like at check for FieldDecl.
  1158. }
  1159. // Check the prototypes.
  1160. if (!::IsStructurallyEquivalent(Context,
  1161. Method1->getType(), Method2->getType()))
  1162. return false;
  1163. return true;
  1164. }
  1165. /// Determine structural equivalence of two lambda classes.
  1166. static bool
  1167. IsStructurallyEquivalentLambdas(StructuralEquivalenceContext &Context,
  1168. CXXRecordDecl *D1, CXXRecordDecl *D2) {
  1169. assert(D1->isLambda() && D2->isLambda() &&
  1170. "Must be called on lambda classes");
  1171. if (!IsStructurallyEquivalent(Context, D1->getLambdaCallOperator(),
  1172. D2->getLambdaCallOperator()))
  1173. return false;
  1174. return true;
  1175. }
  1176. /// Determine if context of a class is equivalent.
  1177. static bool IsRecordContextStructurallyEquivalent(RecordDecl *D1,
  1178. RecordDecl *D2) {
  1179. // The context should be completely equal, including anonymous and inline
  1180. // namespaces.
  1181. // We compare objects as part of full translation units, not subtrees of
  1182. // translation units.
  1183. DeclContext *DC1 = D1->getDeclContext()->getNonTransparentContext();
  1184. DeclContext *DC2 = D2->getDeclContext()->getNonTransparentContext();
  1185. while (true) {
  1186. // Special case: We allow a struct defined in a function to be equivalent
  1187. // with a similar struct defined outside of a function.
  1188. if ((DC1->isFunctionOrMethod() && DC2->isTranslationUnit()) ||
  1189. (DC2->isFunctionOrMethod() && DC1->isTranslationUnit()))
  1190. return true;
  1191. if (DC1->getDeclKind() != DC2->getDeclKind())
  1192. return false;
  1193. if (DC1->isTranslationUnit())
  1194. break;
  1195. if (DC1->isInlineNamespace() != DC2->isInlineNamespace())
  1196. return false;
  1197. if (const auto *ND1 = dyn_cast<NamedDecl>(DC1)) {
  1198. const auto *ND2 = cast<NamedDecl>(DC2);
  1199. if (!DC1->isInlineNamespace() &&
  1200. !IsStructurallyEquivalent(ND1->getIdentifier(), ND2->getIdentifier()))
  1201. return false;
  1202. }
  1203. DC1 = DC1->getParent()->getNonTransparentContext();
  1204. DC2 = DC2->getParent()->getNonTransparentContext();
  1205. }
  1206. return true;
  1207. }
  1208. /// Determine structural equivalence of two records.
  1209. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1210. RecordDecl *D1, RecordDecl *D2) {
  1211. // Check for equivalent structure names.
  1212. IdentifierInfo *Name1 = D1->getIdentifier();
  1213. if (!Name1 && D1->getTypedefNameForAnonDecl())
  1214. Name1 = D1->getTypedefNameForAnonDecl()->getIdentifier();
  1215. IdentifierInfo *Name2 = D2->getIdentifier();
  1216. if (!Name2 && D2->getTypedefNameForAnonDecl())
  1217. Name2 = D2->getTypedefNameForAnonDecl()->getIdentifier();
  1218. if (!IsStructurallyEquivalent(Name1, Name2))
  1219. return false;
  1220. if (D1->isUnion() != D2->isUnion()) {
  1221. if (Context.Complain) {
  1222. Context.Diag2(D2->getLocation(), Context.getApplicableDiagnostic(
  1223. diag::err_odr_tag_type_inconsistent))
  1224. << Context.ToCtx.getTypeDeclType(D2);
  1225. Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
  1226. << D1->getDeclName() << (unsigned)D1->getTagKind();
  1227. }
  1228. return false;
  1229. }
  1230. if (!D1->getDeclName() && !D2->getDeclName()) {
  1231. // If both anonymous structs/unions are in a record context, make sure
  1232. // they occur in the same location in the context records.
  1233. if (Optional<unsigned> Index1 =
  1234. StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(D1)) {
  1235. if (Optional<unsigned> Index2 =
  1236. StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(
  1237. D2)) {
  1238. if (*Index1 != *Index2)
  1239. return false;
  1240. }
  1241. }
  1242. }
  1243. // If the records occur in different context (namespace), these should be
  1244. // different. This is specially important if the definition of one or both
  1245. // records is missing.
  1246. if (!IsRecordContextStructurallyEquivalent(D1, D2))
  1247. return false;
  1248. // If both declarations are class template specializations, we know
  1249. // the ODR applies, so check the template and template arguments.
  1250. const auto *Spec1 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
  1251. const auto *Spec2 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
  1252. if (Spec1 && Spec2) {
  1253. // Check that the specialized templates are the same.
  1254. if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
  1255. Spec2->getSpecializedTemplate()))
  1256. return false;
  1257. // Check that the template arguments are the same.
  1258. if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
  1259. return false;
  1260. for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
  1261. if (!IsStructurallyEquivalent(Context, Spec1->getTemplateArgs().get(I),
  1262. Spec2->getTemplateArgs().get(I)))
  1263. return false;
  1264. }
  1265. // If one is a class template specialization and the other is not, these
  1266. // structures are different.
  1267. else if (Spec1 || Spec2)
  1268. return false;
  1269. // Compare the definitions of these two records. If either or both are
  1270. // incomplete (i.e. it is a forward decl), we assume that they are
  1271. // equivalent.
  1272. D1 = D1->getDefinition();
  1273. D2 = D2->getDefinition();
  1274. if (!D1 || !D2)
  1275. return true;
  1276. // If any of the records has external storage and we do a minimal check (or
  1277. // AST import) we assume they are equivalent. (If we didn't have this
  1278. // assumption then `RecordDecl::LoadFieldsFromExternalStorage` could trigger
  1279. // another AST import which in turn would call the structural equivalency
  1280. // check again and finally we'd have an improper result.)
  1281. if (Context.EqKind == StructuralEquivalenceKind::Minimal)
  1282. if (D1->hasExternalLexicalStorage() || D2->hasExternalLexicalStorage())
  1283. return true;
  1284. // If one definition is currently being defined, we do not compare for
  1285. // equality and we assume that the decls are equal.
  1286. if (D1->isBeingDefined() || D2->isBeingDefined())
  1287. return true;
  1288. if (auto *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
  1289. if (auto *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
  1290. if (D1CXX->hasExternalLexicalStorage() &&
  1291. !D1CXX->isCompleteDefinition()) {
  1292. D1CXX->getASTContext().getExternalSource()->CompleteType(D1CXX);
  1293. }
  1294. if (D1CXX->isLambda() != D2CXX->isLambda())
  1295. return false;
  1296. if (D1CXX->isLambda()) {
  1297. if (!IsStructurallyEquivalentLambdas(Context, D1CXX, D2CXX))
  1298. return false;
  1299. }
  1300. if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
  1301. if (Context.Complain) {
  1302. Context.Diag2(D2->getLocation(),
  1303. Context.getApplicableDiagnostic(
  1304. diag::err_odr_tag_type_inconsistent))
  1305. << Context.ToCtx.getTypeDeclType(D2);
  1306. Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
  1307. << D2CXX->getNumBases();
  1308. Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
  1309. << D1CXX->getNumBases();
  1310. }
  1311. return false;
  1312. }
  1313. // Check the base classes.
  1314. for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
  1315. BaseEnd1 = D1CXX->bases_end(),
  1316. Base2 = D2CXX->bases_begin();
  1317. Base1 != BaseEnd1; ++Base1, ++Base2) {
  1318. if (!IsStructurallyEquivalent(Context, Base1->getType(),
  1319. Base2->getType())) {
  1320. if (Context.Complain) {
  1321. Context.Diag2(D2->getLocation(),
  1322. Context.getApplicableDiagnostic(
  1323. diag::err_odr_tag_type_inconsistent))
  1324. << Context.ToCtx.getTypeDeclType(D2);
  1325. Context.Diag2(Base2->getBeginLoc(), diag::note_odr_base)
  1326. << Base2->getType() << Base2->getSourceRange();
  1327. Context.Diag1(Base1->getBeginLoc(), diag::note_odr_base)
  1328. << Base1->getType() << Base1->getSourceRange();
  1329. }
  1330. return false;
  1331. }
  1332. // Check virtual vs. non-virtual inheritance mismatch.
  1333. if (Base1->isVirtual() != Base2->isVirtual()) {
  1334. if (Context.Complain) {
  1335. Context.Diag2(D2->getLocation(),
  1336. Context.getApplicableDiagnostic(
  1337. diag::err_odr_tag_type_inconsistent))
  1338. << Context.ToCtx.getTypeDeclType(D2);
  1339. Context.Diag2(Base2->getBeginLoc(), diag::note_odr_virtual_base)
  1340. << Base2->isVirtual() << Base2->getSourceRange();
  1341. Context.Diag1(Base1->getBeginLoc(), diag::note_odr_base)
  1342. << Base1->isVirtual() << Base1->getSourceRange();
  1343. }
  1344. return false;
  1345. }
  1346. }
  1347. // Check the friends for consistency.
  1348. CXXRecordDecl::friend_iterator Friend2 = D2CXX->friend_begin(),
  1349. Friend2End = D2CXX->friend_end();
  1350. for (CXXRecordDecl::friend_iterator Friend1 = D1CXX->friend_begin(),
  1351. Friend1End = D1CXX->friend_end();
  1352. Friend1 != Friend1End; ++Friend1, ++Friend2) {
  1353. if (Friend2 == Friend2End) {
  1354. if (Context.Complain) {
  1355. Context.Diag2(D2->getLocation(),
  1356. Context.getApplicableDiagnostic(
  1357. diag::err_odr_tag_type_inconsistent))
  1358. << Context.ToCtx.getTypeDeclType(D2CXX);
  1359. Context.Diag1((*Friend1)->getFriendLoc(), diag::note_odr_friend);
  1360. Context.Diag2(D2->getLocation(), diag::note_odr_missing_friend);
  1361. }
  1362. return false;
  1363. }
  1364. if (!IsStructurallyEquivalent(Context, *Friend1, *Friend2)) {
  1365. if (Context.Complain) {
  1366. Context.Diag2(D2->getLocation(),
  1367. Context.getApplicableDiagnostic(
  1368. diag::err_odr_tag_type_inconsistent))
  1369. << Context.ToCtx.getTypeDeclType(D2CXX);
  1370. Context.Diag1((*Friend1)->getFriendLoc(), diag::note_odr_friend);
  1371. Context.Diag2((*Friend2)->getFriendLoc(), diag::note_odr_friend);
  1372. }
  1373. return false;
  1374. }
  1375. }
  1376. if (Friend2 != Friend2End) {
  1377. if (Context.Complain) {
  1378. Context.Diag2(D2->getLocation(),
  1379. Context.getApplicableDiagnostic(
  1380. diag::err_odr_tag_type_inconsistent))
  1381. << Context.ToCtx.getTypeDeclType(D2);
  1382. Context.Diag2((*Friend2)->getFriendLoc(), diag::note_odr_friend);
  1383. Context.Diag1(D1->getLocation(), diag::note_odr_missing_friend);
  1384. }
  1385. return false;
  1386. }
  1387. } else if (D1CXX->getNumBases() > 0) {
  1388. if (Context.Complain) {
  1389. Context.Diag2(D2->getLocation(),
  1390. Context.getApplicableDiagnostic(
  1391. diag::err_odr_tag_type_inconsistent))
  1392. << Context.ToCtx.getTypeDeclType(D2);
  1393. const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
  1394. Context.Diag1(Base1->getBeginLoc(), diag::note_odr_base)
  1395. << Base1->getType() << Base1->getSourceRange();
  1396. Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
  1397. }
  1398. return false;
  1399. }
  1400. }
  1401. // Check the fields for consistency.
  1402. RecordDecl::field_iterator Field2 = D2->field_begin(),
  1403. Field2End = D2->field_end();
  1404. for (RecordDecl::field_iterator Field1 = D1->field_begin(),
  1405. Field1End = D1->field_end();
  1406. Field1 != Field1End; ++Field1, ++Field2) {
  1407. if (Field2 == Field2End) {
  1408. if (Context.Complain) {
  1409. Context.Diag2(D2->getLocation(),
  1410. Context.getApplicableDiagnostic(
  1411. diag::err_odr_tag_type_inconsistent))
  1412. << Context.ToCtx.getTypeDeclType(D2);
  1413. Context.Diag1(Field1->getLocation(), diag::note_odr_field)
  1414. << Field1->getDeclName() << Field1->getType();
  1415. Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
  1416. }
  1417. return false;
  1418. }
  1419. if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
  1420. return false;
  1421. }
  1422. if (Field2 != Field2End) {
  1423. if (Context.Complain) {
  1424. Context.Diag2(D2->getLocation(), Context.getApplicableDiagnostic(
  1425. diag::err_odr_tag_type_inconsistent))
  1426. << Context.ToCtx.getTypeDeclType(D2);
  1427. Context.Diag2(Field2->getLocation(), diag::note_odr_field)
  1428. << Field2->getDeclName() << Field2->getType();
  1429. Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
  1430. }
  1431. return false;
  1432. }
  1433. return true;
  1434. }
  1435. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1436. EnumConstantDecl *D1,
  1437. EnumConstantDecl *D2) {
  1438. const llvm::APSInt &FromVal = D1->getInitVal();
  1439. const llvm::APSInt &ToVal = D2->getInitVal();
  1440. if (FromVal.isSigned() != ToVal.isSigned())
  1441. return false;
  1442. if (FromVal.getBitWidth() != ToVal.getBitWidth())
  1443. return false;
  1444. if (FromVal != ToVal)
  1445. return false;
  1446. if (!IsStructurallyEquivalent(D1->getIdentifier(), D2->getIdentifier()))
  1447. return false;
  1448. // Init expressions are the most expensive check, so do them last.
  1449. return IsStructurallyEquivalent(Context, D1->getInitExpr(),
  1450. D2->getInitExpr());
  1451. }
  1452. /// Determine structural equivalence of two enums.
  1453. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1454. EnumDecl *D1, EnumDecl *D2) {
  1455. // Check for equivalent enum names.
  1456. IdentifierInfo *Name1 = D1->getIdentifier();
  1457. if (!Name1 && D1->getTypedefNameForAnonDecl())
  1458. Name1 = D1->getTypedefNameForAnonDecl()->getIdentifier();
  1459. IdentifierInfo *Name2 = D2->getIdentifier();
  1460. if (!Name2 && D2->getTypedefNameForAnonDecl())
  1461. Name2 = D2->getTypedefNameForAnonDecl()->getIdentifier();
  1462. if (!IsStructurallyEquivalent(Name1, Name2))
  1463. return false;
  1464. // Compare the definitions of these two enums. If either or both are
  1465. // incomplete (i.e. forward declared), we assume that they are equivalent.
  1466. D1 = D1->getDefinition();
  1467. D2 = D2->getDefinition();
  1468. if (!D1 || !D2)
  1469. return true;
  1470. EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
  1471. EC2End = D2->enumerator_end();
  1472. for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
  1473. EC1End = D1->enumerator_end();
  1474. EC1 != EC1End; ++EC1, ++EC2) {
  1475. if (EC2 == EC2End) {
  1476. if (Context.Complain) {
  1477. Context.Diag2(D2->getLocation(),
  1478. Context.getApplicableDiagnostic(
  1479. diag::err_odr_tag_type_inconsistent))
  1480. << Context.ToCtx.getTypeDeclType(D2);
  1481. Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
  1482. << EC1->getDeclName() << toString(EC1->getInitVal(), 10);
  1483. Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
  1484. }
  1485. return false;
  1486. }
  1487. llvm::APSInt Val1 = EC1->getInitVal();
  1488. llvm::APSInt Val2 = EC2->getInitVal();
  1489. if (!llvm::APSInt::isSameValue(Val1, Val2) ||
  1490. !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
  1491. if (Context.Complain) {
  1492. Context.Diag2(D2->getLocation(),
  1493. Context.getApplicableDiagnostic(
  1494. diag::err_odr_tag_type_inconsistent))
  1495. << Context.ToCtx.getTypeDeclType(D2);
  1496. Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
  1497. << EC2->getDeclName() << toString(EC2->getInitVal(), 10);
  1498. Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
  1499. << EC1->getDeclName() << toString(EC1->getInitVal(), 10);
  1500. }
  1501. return false;
  1502. }
  1503. }
  1504. if (EC2 != EC2End) {
  1505. if (Context.Complain) {
  1506. Context.Diag2(D2->getLocation(), Context.getApplicableDiagnostic(
  1507. diag::err_odr_tag_type_inconsistent))
  1508. << Context.ToCtx.getTypeDeclType(D2);
  1509. Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
  1510. << EC2->getDeclName() << toString(EC2->getInitVal(), 10);
  1511. Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
  1512. }
  1513. return false;
  1514. }
  1515. return true;
  1516. }
  1517. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1518. TemplateParameterList *Params1,
  1519. TemplateParameterList *Params2) {
  1520. if (Params1->size() != Params2->size()) {
  1521. if (Context.Complain) {
  1522. Context.Diag2(Params2->getTemplateLoc(),
  1523. Context.getApplicableDiagnostic(
  1524. diag::err_odr_different_num_template_parameters))
  1525. << Params1->size() << Params2->size();
  1526. Context.Diag1(Params1->getTemplateLoc(),
  1527. diag::note_odr_template_parameter_list);
  1528. }
  1529. return false;
  1530. }
  1531. for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
  1532. if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
  1533. if (Context.Complain) {
  1534. Context.Diag2(Params2->getParam(I)->getLocation(),
  1535. Context.getApplicableDiagnostic(
  1536. diag::err_odr_different_template_parameter_kind));
  1537. Context.Diag1(Params1->getParam(I)->getLocation(),
  1538. diag::note_odr_template_parameter_here);
  1539. }
  1540. return false;
  1541. }
  1542. if (!IsStructurallyEquivalent(Context, Params1->getParam(I),
  1543. Params2->getParam(I)))
  1544. return false;
  1545. }
  1546. return true;
  1547. }
  1548. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1549. TemplateTypeParmDecl *D1,
  1550. TemplateTypeParmDecl *D2) {
  1551. if (D1->isParameterPack() != D2->isParameterPack()) {
  1552. if (Context.Complain) {
  1553. Context.Diag2(D2->getLocation(),
  1554. Context.getApplicableDiagnostic(
  1555. diag::err_odr_parameter_pack_non_pack))
  1556. << D2->isParameterPack();
  1557. Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
  1558. << D1->isParameterPack();
  1559. }
  1560. return false;
  1561. }
  1562. return true;
  1563. }
  1564. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1565. NonTypeTemplateParmDecl *D1,
  1566. NonTypeTemplateParmDecl *D2) {
  1567. if (D1->isParameterPack() != D2->isParameterPack()) {
  1568. if (Context.Complain) {
  1569. Context.Diag2(D2->getLocation(),
  1570. Context.getApplicableDiagnostic(
  1571. diag::err_odr_parameter_pack_non_pack))
  1572. << D2->isParameterPack();
  1573. Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
  1574. << D1->isParameterPack();
  1575. }
  1576. return false;
  1577. }
  1578. // Check types.
  1579. if (!IsStructurallyEquivalent(Context, D1->getType(), D2->getType())) {
  1580. if (Context.Complain) {
  1581. Context.Diag2(D2->getLocation(),
  1582. Context.getApplicableDiagnostic(
  1583. diag::err_odr_non_type_parameter_type_inconsistent))
  1584. << D2->getType() << D1->getType();
  1585. Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
  1586. << D1->getType();
  1587. }
  1588. return false;
  1589. }
  1590. return true;
  1591. }
  1592. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1593. TemplateTemplateParmDecl *D1,
  1594. TemplateTemplateParmDecl *D2) {
  1595. if (D1->isParameterPack() != D2->isParameterPack()) {
  1596. if (Context.Complain) {
  1597. Context.Diag2(D2->getLocation(),
  1598. Context.getApplicableDiagnostic(
  1599. diag::err_odr_parameter_pack_non_pack))
  1600. << D2->isParameterPack();
  1601. Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
  1602. << D1->isParameterPack();
  1603. }
  1604. return false;
  1605. }
  1606. // Check template parameter lists.
  1607. return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
  1608. D2->getTemplateParameters());
  1609. }
  1610. static bool IsTemplateDeclCommonStructurallyEquivalent(
  1611. StructuralEquivalenceContext &Ctx, TemplateDecl *D1, TemplateDecl *D2) {
  1612. if (!IsStructurallyEquivalent(D1->getIdentifier(), D2->getIdentifier()))
  1613. return false;
  1614. if (!D1->getIdentifier()) // Special name
  1615. if (D1->getNameAsString() != D2->getNameAsString())
  1616. return false;
  1617. return IsStructurallyEquivalent(Ctx, D1->getTemplateParameters(),
  1618. D2->getTemplateParameters());
  1619. }
  1620. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1621. ClassTemplateDecl *D1,
  1622. ClassTemplateDecl *D2) {
  1623. // Check template parameters.
  1624. if (!IsTemplateDeclCommonStructurallyEquivalent(Context, D1, D2))
  1625. return false;
  1626. // Check the templated declaration.
  1627. return IsStructurallyEquivalent(Context, D1->getTemplatedDecl(),
  1628. D2->getTemplatedDecl());
  1629. }
  1630. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1631. FunctionTemplateDecl *D1,
  1632. FunctionTemplateDecl *D2) {
  1633. // Check template parameters.
  1634. if (!IsTemplateDeclCommonStructurallyEquivalent(Context, D1, D2))
  1635. return false;
  1636. // Check the templated declaration.
  1637. return IsStructurallyEquivalent(Context, D1->getTemplatedDecl()->getType(),
  1638. D2->getTemplatedDecl()->getType());
  1639. }
  1640. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1641. ConceptDecl *D1,
  1642. ConceptDecl *D2) {
  1643. // Check template parameters.
  1644. if (!IsTemplateDeclCommonStructurallyEquivalent(Context, D1, D2))
  1645. return false;
  1646. // Check the constraint expression.
  1647. return IsStructurallyEquivalent(Context, D1->getConstraintExpr(),
  1648. D2->getConstraintExpr());
  1649. }
  1650. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1651. FriendDecl *D1, FriendDecl *D2) {
  1652. if ((D1->getFriendType() && D2->getFriendDecl()) ||
  1653. (D1->getFriendDecl() && D2->getFriendType())) {
  1654. return false;
  1655. }
  1656. if (D1->getFriendType() && D2->getFriendType())
  1657. return IsStructurallyEquivalent(Context,
  1658. D1->getFriendType()->getType(),
  1659. D2->getFriendType()->getType());
  1660. if (D1->getFriendDecl() && D2->getFriendDecl())
  1661. return IsStructurallyEquivalent(Context, D1->getFriendDecl(),
  1662. D2->getFriendDecl());
  1663. return false;
  1664. }
  1665. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1666. TypedefNameDecl *D1, TypedefNameDecl *D2) {
  1667. if (!IsStructurallyEquivalent(D1->getIdentifier(), D2->getIdentifier()))
  1668. return false;
  1669. return IsStructurallyEquivalent(Context, D1->getUnderlyingType(),
  1670. D2->getUnderlyingType());
  1671. }
  1672. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1673. FunctionDecl *D1, FunctionDecl *D2) {
  1674. if (!IsStructurallyEquivalent(D1->getIdentifier(), D2->getIdentifier()))
  1675. return false;
  1676. if (D1->isOverloadedOperator()) {
  1677. if (!D2->isOverloadedOperator())
  1678. return false;
  1679. if (D1->getOverloadedOperator() != D2->getOverloadedOperator())
  1680. return false;
  1681. }
  1682. // FIXME: Consider checking for function attributes as well.
  1683. if (!IsStructurallyEquivalent(Context, D1->getType(), D2->getType()))
  1684. return false;
  1685. return true;
  1686. }
  1687. /// Determine structural equivalence of two declarations.
  1688. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1689. Decl *D1, Decl *D2) {
  1690. // FIXME: Check for known structural equivalences via a callback of some sort.
  1691. D1 = D1->getCanonicalDecl();
  1692. D2 = D2->getCanonicalDecl();
  1693. std::pair<Decl *, Decl *> P{D1, D2};
  1694. // Check whether we already know that these two declarations are not
  1695. // structurally equivalent.
  1696. if (Context.NonEquivalentDecls.count(P))
  1697. return false;
  1698. // Check if a check for these declarations is already pending.
  1699. // If yes D1 and D2 will be checked later (from DeclsToCheck),
  1700. // or these are already checked (and equivalent).
  1701. bool Inserted = Context.VisitedDecls.insert(P).second;
  1702. if (!Inserted)
  1703. return true;
  1704. Context.DeclsToCheck.push(P);
  1705. return true;
  1706. }
  1707. DiagnosticBuilder StructuralEquivalenceContext::Diag1(SourceLocation Loc,
  1708. unsigned DiagID) {
  1709. assert(Complain && "Not allowed to complain");
  1710. if (LastDiagFromC2)
  1711. FromCtx.getDiagnostics().notePriorDiagnosticFrom(ToCtx.getDiagnostics());
  1712. LastDiagFromC2 = false;
  1713. return FromCtx.getDiagnostics().Report(Loc, DiagID);
  1714. }
  1715. DiagnosticBuilder StructuralEquivalenceContext::Diag2(SourceLocation Loc,
  1716. unsigned DiagID) {
  1717. assert(Complain && "Not allowed to complain");
  1718. if (!LastDiagFromC2)
  1719. ToCtx.getDiagnostics().notePriorDiagnosticFrom(FromCtx.getDiagnostics());
  1720. LastDiagFromC2 = true;
  1721. return ToCtx.getDiagnostics().Report(Loc, DiagID);
  1722. }
  1723. Optional<unsigned>
  1724. StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(RecordDecl *Anon) {
  1725. ASTContext &Context = Anon->getASTContext();
  1726. QualType AnonTy = Context.getRecordType(Anon);
  1727. const auto *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext());
  1728. if (!Owner)
  1729. return None;
  1730. unsigned Index = 0;
  1731. for (const auto *D : Owner->noload_decls()) {
  1732. const auto *F = dyn_cast<FieldDecl>(D);
  1733. if (!F)
  1734. continue;
  1735. if (F->isAnonymousStructOrUnion()) {
  1736. if (Context.hasSameType(F->getType(), AnonTy))
  1737. break;
  1738. ++Index;
  1739. continue;
  1740. }
  1741. // If the field looks like this:
  1742. // struct { ... } A;
  1743. QualType FieldType = F->getType();
  1744. // In case of nested structs.
  1745. while (const auto *ElabType = dyn_cast<ElaboratedType>(FieldType))
  1746. FieldType = ElabType->getNamedType();
  1747. if (const auto *RecType = dyn_cast<RecordType>(FieldType)) {
  1748. const RecordDecl *RecDecl = RecType->getDecl();
  1749. if (RecDecl->getDeclContext() == Owner && !RecDecl->getIdentifier()) {
  1750. if (Context.hasSameType(FieldType, AnonTy))
  1751. break;
  1752. ++Index;
  1753. continue;
  1754. }
  1755. }
  1756. }
  1757. return Index;
  1758. }
  1759. unsigned StructuralEquivalenceContext::getApplicableDiagnostic(
  1760. unsigned ErrorDiagnostic) {
  1761. if (ErrorOnTagTypeMismatch)
  1762. return ErrorDiagnostic;
  1763. switch (ErrorDiagnostic) {
  1764. case diag::err_odr_variable_type_inconsistent:
  1765. return diag::warn_odr_variable_type_inconsistent;
  1766. case diag::err_odr_variable_multiple_def:
  1767. return diag::warn_odr_variable_multiple_def;
  1768. case diag::err_odr_function_type_inconsistent:
  1769. return diag::warn_odr_function_type_inconsistent;
  1770. case diag::err_odr_tag_type_inconsistent:
  1771. return diag::warn_odr_tag_type_inconsistent;
  1772. case diag::err_odr_field_type_inconsistent:
  1773. return diag::warn_odr_field_type_inconsistent;
  1774. case diag::err_odr_ivar_type_inconsistent:
  1775. return diag::warn_odr_ivar_type_inconsistent;
  1776. case diag::err_odr_objc_superclass_inconsistent:
  1777. return diag::warn_odr_objc_superclass_inconsistent;
  1778. case diag::err_odr_objc_method_result_type_inconsistent:
  1779. return diag::warn_odr_objc_method_result_type_inconsistent;
  1780. case diag::err_odr_objc_method_num_params_inconsistent:
  1781. return diag::warn_odr_objc_method_num_params_inconsistent;
  1782. case diag::err_odr_objc_method_param_type_inconsistent:
  1783. return diag::warn_odr_objc_method_param_type_inconsistent;
  1784. case diag::err_odr_objc_method_variadic_inconsistent:
  1785. return diag::warn_odr_objc_method_variadic_inconsistent;
  1786. case diag::err_odr_objc_property_type_inconsistent:
  1787. return diag::warn_odr_objc_property_type_inconsistent;
  1788. case diag::err_odr_objc_property_impl_kind_inconsistent:
  1789. return diag::warn_odr_objc_property_impl_kind_inconsistent;
  1790. case diag::err_odr_objc_synthesize_ivar_inconsistent:
  1791. return diag::warn_odr_objc_synthesize_ivar_inconsistent;
  1792. case diag::err_odr_different_num_template_parameters:
  1793. return diag::warn_odr_different_num_template_parameters;
  1794. case diag::err_odr_different_template_parameter_kind:
  1795. return diag::warn_odr_different_template_parameter_kind;
  1796. case diag::err_odr_parameter_pack_non_pack:
  1797. return diag::warn_odr_parameter_pack_non_pack;
  1798. case diag::err_odr_non_type_parameter_type_inconsistent:
  1799. return diag::warn_odr_non_type_parameter_type_inconsistent;
  1800. }
  1801. llvm_unreachable("Diagnostic kind not handled in preceding switch");
  1802. }
  1803. bool StructuralEquivalenceContext::IsEquivalent(Decl *D1, Decl *D2) {
  1804. // Ensure that the implementation functions (all static functions in this TU)
  1805. // never call the public ASTStructuralEquivalence::IsEquivalent() functions,
  1806. // because that will wreak havoc the internal state (DeclsToCheck and
  1807. // VisitedDecls members) and can cause faulty behaviour.
  1808. // In other words: Do not start a graph search from a new node with the
  1809. // internal data of another search in progress.
  1810. // FIXME: Better encapsulation and separation of internal and public
  1811. // functionality.
  1812. assert(DeclsToCheck.empty());
  1813. assert(VisitedDecls.empty());
  1814. if (!::IsStructurallyEquivalent(*this, D1, D2))
  1815. return false;
  1816. return !Finish();
  1817. }
  1818. bool StructuralEquivalenceContext::IsEquivalent(QualType T1, QualType T2) {
  1819. assert(DeclsToCheck.empty());
  1820. assert(VisitedDecls.empty());
  1821. if (!::IsStructurallyEquivalent(*this, T1, T2))
  1822. return false;
  1823. return !Finish();
  1824. }
  1825. bool StructuralEquivalenceContext::IsEquivalent(Stmt *S1, Stmt *S2) {
  1826. assert(DeclsToCheck.empty());
  1827. assert(VisitedDecls.empty());
  1828. if (!::IsStructurallyEquivalent(*this, S1, S2))
  1829. return false;
  1830. return !Finish();
  1831. }
  1832. bool StructuralEquivalenceContext::CheckCommonEquivalence(Decl *D1, Decl *D2) {
  1833. // Check for equivalent described template.
  1834. TemplateDecl *Template1 = D1->getDescribedTemplate();
  1835. TemplateDecl *Template2 = D2->getDescribedTemplate();
  1836. if ((Template1 != nullptr) != (Template2 != nullptr))
  1837. return false;
  1838. if (Template1 && !IsStructurallyEquivalent(*this, Template1, Template2))
  1839. return false;
  1840. // FIXME: Move check for identifier names into this function.
  1841. return true;
  1842. }
  1843. bool StructuralEquivalenceContext::CheckKindSpecificEquivalence(
  1844. Decl *D1, Decl *D2) {
  1845. // Kind mismatch.
  1846. if (D1->getKind() != D2->getKind())
  1847. return false;
  1848. // Cast the Decls to their actual subclass so that the right overload of
  1849. // IsStructurallyEquivalent is called.
  1850. switch (D1->getKind()) {
  1851. #define ABSTRACT_DECL(DECL)
  1852. #define DECL(DERIVED, BASE) \
  1853. case Decl::Kind::DERIVED: \
  1854. return ::IsStructurallyEquivalent(*this, static_cast<DERIVED##Decl *>(D1), \
  1855. static_cast<DERIVED##Decl *>(D2));
  1856. #include "clang/AST/DeclNodes.inc"
  1857. }
  1858. return true;
  1859. }
  1860. bool StructuralEquivalenceContext::Finish() {
  1861. while (!DeclsToCheck.empty()) {
  1862. // Check the next declaration.
  1863. std::pair<Decl *, Decl *> P = DeclsToCheck.front();
  1864. DeclsToCheck.pop();
  1865. Decl *D1 = P.first;
  1866. Decl *D2 = P.second;
  1867. bool Equivalent =
  1868. CheckCommonEquivalence(D1, D2) && CheckKindSpecificEquivalence(D1, D2);
  1869. if (!Equivalent) {
  1870. // Note that these two declarations are not equivalent (and we already
  1871. // know about it).
  1872. NonEquivalentDecls.insert(P);
  1873. return true;
  1874. }
  1875. }
  1876. return false;
  1877. }