ASTStructuralEquivalence.cpp 71 KB

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