ASTStructuralEquivalence.cpp 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  1. //===- ASTStructuralEquivalence.cpp ---------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implement StructuralEquivalenceContext class and helper functions
  11. // for layout matching.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/AST/ASTStructuralEquivalence.h"
  15. #include "clang/AST/ASTContext.h"
  16. #include "clang/AST/ASTDiagnostic.h"
  17. #include "clang/AST/Decl.h"
  18. #include "clang/AST/DeclBase.h"
  19. #include "clang/AST/DeclCXX.h"
  20. #include "clang/AST/DeclFriend.h"
  21. #include "clang/AST/DeclObjC.h"
  22. #include "clang/AST/DeclTemplate.h"
  23. #include "clang/AST/NestedNameSpecifier.h"
  24. #include "clang/AST/TemplateBase.h"
  25. #include "clang/AST/TemplateName.h"
  26. #include "clang/AST/Type.h"
  27. #include "clang/Basic/ExceptionSpecificationType.h"
  28. #include "clang/Basic/IdentifierTable.h"
  29. #include "clang/Basic/LLVM.h"
  30. #include "clang/Basic/SourceLocation.h"
  31. #include "llvm/ADT/APInt.h"
  32. #include "llvm/ADT/APSInt.h"
  33. #include "llvm/ADT/None.h"
  34. #include "llvm/ADT/Optional.h"
  35. #include "llvm/Support/Casting.h"
  36. #include "llvm/Support/Compiler.h"
  37. #include "llvm/Support/ErrorHandling.h"
  38. #include <cassert>
  39. #include <utility>
  40. using namespace clang;
  41. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  42. QualType T1, QualType T2);
  43. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  44. Decl *D1, Decl *D2);
  45. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  46. const TemplateArgument &Arg1,
  47. const TemplateArgument &Arg2);
  48. /// Determine structural equivalence of two expressions.
  49. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  50. Expr *E1, Expr *E2) {
  51. if (!E1 || !E2)
  52. return E1 == E2;
  53. // FIXME: Actually perform a structural comparison!
  54. return true;
  55. }
  56. /// Determine whether two identifiers are equivalent.
  57. static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
  58. const IdentifierInfo *Name2) {
  59. if (!Name1 || !Name2)
  60. return Name1 == Name2;
  61. return Name1->getName() == Name2->getName();
  62. }
  63. /// Determine whether two nested-name-specifiers are equivalent.
  64. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  65. NestedNameSpecifier *NNS1,
  66. NestedNameSpecifier *NNS2) {
  67. if (NNS1->getKind() != NNS2->getKind())
  68. return false;
  69. NestedNameSpecifier *Prefix1 = NNS1->getPrefix(),
  70. *Prefix2 = NNS2->getPrefix();
  71. if ((bool)Prefix1 != (bool)Prefix2)
  72. return false;
  73. if (Prefix1)
  74. if (!IsStructurallyEquivalent(Context, Prefix1, Prefix2))
  75. return false;
  76. switch (NNS1->getKind()) {
  77. case NestedNameSpecifier::Identifier:
  78. return IsStructurallyEquivalent(NNS1->getAsIdentifier(),
  79. NNS2->getAsIdentifier());
  80. case NestedNameSpecifier::Namespace:
  81. return IsStructurallyEquivalent(Context, NNS1->getAsNamespace(),
  82. NNS2->getAsNamespace());
  83. case NestedNameSpecifier::NamespaceAlias:
  84. return IsStructurallyEquivalent(Context, NNS1->getAsNamespaceAlias(),
  85. NNS2->getAsNamespaceAlias());
  86. case NestedNameSpecifier::TypeSpec:
  87. case NestedNameSpecifier::TypeSpecWithTemplate:
  88. return IsStructurallyEquivalent(Context, QualType(NNS1->getAsType(), 0),
  89. QualType(NNS2->getAsType(), 0));
  90. case NestedNameSpecifier::Global:
  91. return true;
  92. case NestedNameSpecifier::Super:
  93. return IsStructurallyEquivalent(Context, NNS1->getAsRecordDecl(),
  94. NNS2->getAsRecordDecl());
  95. }
  96. return false;
  97. }
  98. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  99. const TemplateName &N1,
  100. const TemplateName &N2) {
  101. if (N1.getKind() != N2.getKind())
  102. return false;
  103. switch (N1.getKind()) {
  104. case TemplateName::Template:
  105. return IsStructurallyEquivalent(Context, N1.getAsTemplateDecl(),
  106. N2.getAsTemplateDecl());
  107. case TemplateName::OverloadedTemplate: {
  108. OverloadedTemplateStorage *OS1 = N1.getAsOverloadedTemplate(),
  109. *OS2 = N2.getAsOverloadedTemplate();
  110. OverloadedTemplateStorage::iterator I1 = OS1->begin(), I2 = OS2->begin(),
  111. E1 = OS1->end(), E2 = OS2->end();
  112. for (; I1 != E1 && I2 != E2; ++I1, ++I2)
  113. if (!IsStructurallyEquivalent(Context, *I1, *I2))
  114. return false;
  115. return I1 == E1 && I2 == E2;
  116. }
  117. case TemplateName::QualifiedTemplate: {
  118. QualifiedTemplateName *QN1 = N1.getAsQualifiedTemplateName(),
  119. *QN2 = N2.getAsQualifiedTemplateName();
  120. return IsStructurallyEquivalent(Context, QN1->getDecl(), QN2->getDecl()) &&
  121. IsStructurallyEquivalent(Context, QN1->getQualifier(),
  122. QN2->getQualifier());
  123. }
  124. case TemplateName::DependentTemplate: {
  125. DependentTemplateName *DN1 = N1.getAsDependentTemplateName(),
  126. *DN2 = N2.getAsDependentTemplateName();
  127. if (!IsStructurallyEquivalent(Context, DN1->getQualifier(),
  128. DN2->getQualifier()))
  129. return false;
  130. if (DN1->isIdentifier() && DN2->isIdentifier())
  131. return IsStructurallyEquivalent(DN1->getIdentifier(),
  132. DN2->getIdentifier());
  133. else if (DN1->isOverloadedOperator() && DN2->isOverloadedOperator())
  134. return DN1->getOperator() == DN2->getOperator();
  135. return false;
  136. }
  137. case TemplateName::SubstTemplateTemplateParm: {
  138. SubstTemplateTemplateParmStorage *TS1 = N1.getAsSubstTemplateTemplateParm(),
  139. *TS2 = N2.getAsSubstTemplateTemplateParm();
  140. return IsStructurallyEquivalent(Context, TS1->getParameter(),
  141. TS2->getParameter()) &&
  142. IsStructurallyEquivalent(Context, TS1->getReplacement(),
  143. TS2->getReplacement());
  144. }
  145. case TemplateName::SubstTemplateTemplateParmPack: {
  146. SubstTemplateTemplateParmPackStorage
  147. *P1 = N1.getAsSubstTemplateTemplateParmPack(),
  148. *P2 = N2.getAsSubstTemplateTemplateParmPack();
  149. return IsStructurallyEquivalent(Context, P1->getArgumentPack(),
  150. P2->getArgumentPack()) &&
  151. IsStructurallyEquivalent(Context, P1->getParameterPack(),
  152. P2->getParameterPack());
  153. }
  154. }
  155. return false;
  156. }
  157. /// Determine whether two template arguments are equivalent.
  158. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  159. const TemplateArgument &Arg1,
  160. const TemplateArgument &Arg2) {
  161. if (Arg1.getKind() != Arg2.getKind())
  162. return false;
  163. switch (Arg1.getKind()) {
  164. case TemplateArgument::Null:
  165. return true;
  166. case TemplateArgument::Type:
  167. return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
  168. case TemplateArgument::Integral:
  169. if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
  170. Arg2.getIntegralType()))
  171. return false;
  172. return llvm::APSInt::isSameValue(Arg1.getAsIntegral(),
  173. Arg2.getAsIntegral());
  174. case TemplateArgument::Declaration:
  175. return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
  176. case TemplateArgument::NullPtr:
  177. return true; // FIXME: Is this correct?
  178. case TemplateArgument::Template:
  179. return IsStructurallyEquivalent(Context, Arg1.getAsTemplate(),
  180. Arg2.getAsTemplate());
  181. case TemplateArgument::TemplateExpansion:
  182. return IsStructurallyEquivalent(Context,
  183. Arg1.getAsTemplateOrTemplatePattern(),
  184. Arg2.getAsTemplateOrTemplatePattern());
  185. case TemplateArgument::Expression:
  186. return IsStructurallyEquivalent(Context, Arg1.getAsExpr(),
  187. Arg2.getAsExpr());
  188. case TemplateArgument::Pack:
  189. if (Arg1.pack_size() != Arg2.pack_size())
  190. return false;
  191. for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
  192. if (!IsStructurallyEquivalent(Context, Arg1.pack_begin()[I],
  193. Arg2.pack_begin()[I]))
  194. return false;
  195. return true;
  196. }
  197. llvm_unreachable("Invalid template argument kind");
  198. }
  199. /// Determine structural equivalence for the common part of array
  200. /// types.
  201. static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
  202. const ArrayType *Array1,
  203. const ArrayType *Array2) {
  204. if (!IsStructurallyEquivalent(Context, Array1->getElementType(),
  205. Array2->getElementType()))
  206. return false;
  207. if (Array1->getSizeModifier() != Array2->getSizeModifier())
  208. return false;
  209. if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
  210. return false;
  211. return true;
  212. }
  213. /// Determine structural equivalence of two types.
  214. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  215. QualType T1, QualType T2) {
  216. if (T1.isNull() || T2.isNull())
  217. return T1.isNull() && T2.isNull();
  218. QualType OrigT1 = T1;
  219. QualType OrigT2 = T2;
  220. if (!Context.StrictTypeSpelling) {
  221. // We aren't being strict about token-to-token equivalence of types,
  222. // so map down to the canonical type.
  223. T1 = Context.FromCtx.getCanonicalType(T1);
  224. T2 = Context.ToCtx.getCanonicalType(T2);
  225. }
  226. if (T1.getQualifiers() != T2.getQualifiers())
  227. return false;
  228. Type::TypeClass TC = T1->getTypeClass();
  229. if (T1->getTypeClass() != T2->getTypeClass()) {
  230. // Compare function types with prototypes vs. without prototypes as if
  231. // both did not have prototypes.
  232. if (T1->getTypeClass() == Type::FunctionProto &&
  233. T2->getTypeClass() == Type::FunctionNoProto)
  234. TC = Type::FunctionNoProto;
  235. else if (T1->getTypeClass() == Type::FunctionNoProto &&
  236. T2->getTypeClass() == Type::FunctionProto)
  237. TC = Type::FunctionNoProto;
  238. else
  239. return false;
  240. }
  241. switch (TC) {
  242. case Type::Builtin:
  243. // FIXME: Deal with Char_S/Char_U.
  244. if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
  245. return false;
  246. break;
  247. case Type::Complex:
  248. if (!IsStructurallyEquivalent(Context,
  249. cast<ComplexType>(T1)->getElementType(),
  250. cast<ComplexType>(T2)->getElementType()))
  251. return false;
  252. break;
  253. case Type::Adjusted:
  254. case Type::Decayed:
  255. if (!IsStructurallyEquivalent(Context,
  256. cast<AdjustedType>(T1)->getOriginalType(),
  257. cast<AdjustedType>(T2)->getOriginalType()))
  258. return false;
  259. break;
  260. case Type::Pointer:
  261. if (!IsStructurallyEquivalent(Context,
  262. cast<PointerType>(T1)->getPointeeType(),
  263. cast<PointerType>(T2)->getPointeeType()))
  264. return false;
  265. break;
  266. case Type::BlockPointer:
  267. if (!IsStructurallyEquivalent(Context,
  268. cast<BlockPointerType>(T1)->getPointeeType(),
  269. cast<BlockPointerType>(T2)->getPointeeType()))
  270. return false;
  271. break;
  272. case Type::LValueReference:
  273. case Type::RValueReference: {
  274. const auto *Ref1 = cast<ReferenceType>(T1);
  275. const auto *Ref2 = cast<ReferenceType>(T2);
  276. if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
  277. return false;
  278. if (Ref1->isInnerRef() != Ref2->isInnerRef())
  279. return false;
  280. if (!IsStructurallyEquivalent(Context, Ref1->getPointeeTypeAsWritten(),
  281. Ref2->getPointeeTypeAsWritten()))
  282. return false;
  283. break;
  284. }
  285. case Type::MemberPointer: {
  286. const auto *MemPtr1 = cast<MemberPointerType>(T1);
  287. const auto *MemPtr2 = cast<MemberPointerType>(T2);
  288. if (!IsStructurallyEquivalent(Context, MemPtr1->getPointeeType(),
  289. MemPtr2->getPointeeType()))
  290. return false;
  291. if (!IsStructurallyEquivalent(Context, QualType(MemPtr1->getClass(), 0),
  292. QualType(MemPtr2->getClass(), 0)))
  293. return false;
  294. break;
  295. }
  296. case Type::ConstantArray: {
  297. const auto *Array1 = cast<ConstantArrayType>(T1);
  298. const auto *Array2 = cast<ConstantArrayType>(T2);
  299. if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
  300. return false;
  301. if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
  302. return false;
  303. break;
  304. }
  305. case Type::IncompleteArray:
  306. if (!IsArrayStructurallyEquivalent(Context, cast<ArrayType>(T1),
  307. cast<ArrayType>(T2)))
  308. return false;
  309. break;
  310. case Type::VariableArray: {
  311. const auto *Array1 = cast<VariableArrayType>(T1);
  312. const auto *Array2 = cast<VariableArrayType>(T2);
  313. if (!IsStructurallyEquivalent(Context, Array1->getSizeExpr(),
  314. Array2->getSizeExpr()))
  315. return false;
  316. if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
  317. return false;
  318. break;
  319. }
  320. case Type::DependentSizedArray: {
  321. const auto *Array1 = cast<DependentSizedArrayType>(T1);
  322. const auto *Array2 = cast<DependentSizedArrayType>(T2);
  323. if (!IsStructurallyEquivalent(Context, Array1->getSizeExpr(),
  324. Array2->getSizeExpr()))
  325. return false;
  326. if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
  327. return false;
  328. break;
  329. }
  330. case Type::DependentAddressSpace: {
  331. const auto *DepAddressSpace1 = cast<DependentAddressSpaceType>(T1);
  332. const auto *DepAddressSpace2 = cast<DependentAddressSpaceType>(T2);
  333. if (!IsStructurallyEquivalent(Context, DepAddressSpace1->getAddrSpaceExpr(),
  334. DepAddressSpace2->getAddrSpaceExpr()))
  335. return false;
  336. if (!IsStructurallyEquivalent(Context, DepAddressSpace1->getPointeeType(),
  337. DepAddressSpace2->getPointeeType()))
  338. return false;
  339. break;
  340. }
  341. case Type::DependentSizedExtVector: {
  342. const auto *Vec1 = cast<DependentSizedExtVectorType>(T1);
  343. const auto *Vec2 = cast<DependentSizedExtVectorType>(T2);
  344. if (!IsStructurallyEquivalent(Context, Vec1->getSizeExpr(),
  345. Vec2->getSizeExpr()))
  346. return false;
  347. if (!IsStructurallyEquivalent(Context, Vec1->getElementType(),
  348. Vec2->getElementType()))
  349. return false;
  350. break;
  351. }
  352. case Type::Vector:
  353. case Type::ExtVector: {
  354. const auto *Vec1 = cast<VectorType>(T1);
  355. const auto *Vec2 = cast<VectorType>(T2);
  356. if (!IsStructurallyEquivalent(Context, Vec1->getElementType(),
  357. Vec2->getElementType()))
  358. return false;
  359. if (Vec1->getNumElements() != Vec2->getNumElements())
  360. return false;
  361. if (Vec1->getVectorKind() != Vec2->getVectorKind())
  362. return false;
  363. break;
  364. }
  365. case Type::FunctionProto: {
  366. const auto *Proto1 = cast<FunctionProtoType>(T1);
  367. const auto *Proto2 = cast<FunctionProtoType>(T2);
  368. if (Proto1->getNumParams() != Proto2->getNumParams())
  369. return false;
  370. for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) {
  371. if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I),
  372. Proto2->getParamType(I)))
  373. return false;
  374. }
  375. if (Proto1->isVariadic() != Proto2->isVariadic())
  376. return false;
  377. if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
  378. return false;
  379. // Check exceptions, this information is lost in canonical type.
  380. const auto *OrigProto1 =
  381. cast<FunctionProtoType>(OrigT1.getDesugaredType(Context.FromCtx));
  382. const auto *OrigProto2 =
  383. cast<FunctionProtoType>(OrigT2.getDesugaredType(Context.ToCtx));
  384. auto Spec1 = OrigProto1->getExceptionSpecType();
  385. auto Spec2 = OrigProto2->getExceptionSpecType();
  386. if (Spec1 != Spec2)
  387. return false;
  388. if (Spec1 == EST_Dynamic) {
  389. if (OrigProto1->getNumExceptions() != OrigProto2->getNumExceptions())
  390. return false;
  391. for (unsigned I = 0, N = OrigProto1->getNumExceptions(); I != N; ++I) {
  392. if (!IsStructurallyEquivalent(Context, OrigProto1->getExceptionType(I),
  393. OrigProto2->getExceptionType(I)))
  394. return false;
  395. }
  396. } else if (isComputedNoexcept(Spec1)) {
  397. if (!IsStructurallyEquivalent(Context, OrigProto1->getNoexceptExpr(),
  398. OrigProto2->getNoexceptExpr()))
  399. return false;
  400. }
  401. // Fall through to check the bits common with FunctionNoProtoType.
  402. LLVM_FALLTHROUGH;
  403. }
  404. case Type::FunctionNoProto: {
  405. const auto *Function1 = cast<FunctionType>(T1);
  406. const auto *Function2 = cast<FunctionType>(T2);
  407. if (!IsStructurallyEquivalent(Context, Function1->getReturnType(),
  408. Function2->getReturnType()))
  409. return false;
  410. if (Function1->getExtInfo() != Function2->getExtInfo())
  411. return false;
  412. break;
  413. }
  414. case Type::UnresolvedUsing:
  415. if (!IsStructurallyEquivalent(Context,
  416. cast<UnresolvedUsingType>(T1)->getDecl(),
  417. cast<UnresolvedUsingType>(T2)->getDecl()))
  418. return false;
  419. break;
  420. case Type::Attributed:
  421. if (!IsStructurallyEquivalent(Context,
  422. cast<AttributedType>(T1)->getModifiedType(),
  423. cast<AttributedType>(T2)->getModifiedType()))
  424. return false;
  425. if (!IsStructurallyEquivalent(
  426. Context, cast<AttributedType>(T1)->getEquivalentType(),
  427. cast<AttributedType>(T2)->getEquivalentType()))
  428. return false;
  429. break;
  430. case Type::Paren:
  431. if (!IsStructurallyEquivalent(Context, cast<ParenType>(T1)->getInnerType(),
  432. cast<ParenType>(T2)->getInnerType()))
  433. return false;
  434. break;
  435. case Type::Typedef:
  436. if (!IsStructurallyEquivalent(Context, cast<TypedefType>(T1)->getDecl(),
  437. cast<TypedefType>(T2)->getDecl()))
  438. return false;
  439. break;
  440. case Type::TypeOfExpr:
  441. if (!IsStructurallyEquivalent(
  442. Context, cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
  443. cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
  444. return false;
  445. break;
  446. case Type::TypeOf:
  447. if (!IsStructurallyEquivalent(Context,
  448. cast<TypeOfType>(T1)->getUnderlyingType(),
  449. cast<TypeOfType>(T2)->getUnderlyingType()))
  450. return false;
  451. break;
  452. case Type::UnaryTransform:
  453. if (!IsStructurallyEquivalent(
  454. Context, cast<UnaryTransformType>(T1)->getUnderlyingType(),
  455. cast<UnaryTransformType>(T2)->getUnderlyingType()))
  456. return false;
  457. break;
  458. case Type::Decltype:
  459. if (!IsStructurallyEquivalent(Context,
  460. cast<DecltypeType>(T1)->getUnderlyingExpr(),
  461. cast<DecltypeType>(T2)->getUnderlyingExpr()))
  462. return false;
  463. break;
  464. case Type::Auto:
  465. if (!IsStructurallyEquivalent(Context, cast<AutoType>(T1)->getDeducedType(),
  466. cast<AutoType>(T2)->getDeducedType()))
  467. return false;
  468. break;
  469. case Type::DeducedTemplateSpecialization: {
  470. const auto *DT1 = cast<DeducedTemplateSpecializationType>(T1);
  471. const auto *DT2 = cast<DeducedTemplateSpecializationType>(T2);
  472. if (!IsStructurallyEquivalent(Context, DT1->getTemplateName(),
  473. DT2->getTemplateName()))
  474. return false;
  475. if (!IsStructurallyEquivalent(Context, DT1->getDeducedType(),
  476. DT2->getDeducedType()))
  477. return false;
  478. break;
  479. }
  480. case Type::Record:
  481. case Type::Enum:
  482. if (!IsStructurallyEquivalent(Context, cast<TagType>(T1)->getDecl(),
  483. cast<TagType>(T2)->getDecl()))
  484. return false;
  485. break;
  486. case Type::TemplateTypeParm: {
  487. const auto *Parm1 = cast<TemplateTypeParmType>(T1);
  488. const auto *Parm2 = cast<TemplateTypeParmType>(T2);
  489. if (Parm1->getDepth() != Parm2->getDepth())
  490. return false;
  491. if (Parm1->getIndex() != Parm2->getIndex())
  492. return false;
  493. if (Parm1->isParameterPack() != Parm2->isParameterPack())
  494. return false;
  495. // Names of template type parameters are never significant.
  496. break;
  497. }
  498. case Type::SubstTemplateTypeParm: {
  499. const auto *Subst1 = cast<SubstTemplateTypeParmType>(T1);
  500. const auto *Subst2 = cast<SubstTemplateTypeParmType>(T2);
  501. if (!IsStructurallyEquivalent(Context,
  502. QualType(Subst1->getReplacedParameter(), 0),
  503. QualType(Subst2->getReplacedParameter(), 0)))
  504. return false;
  505. if (!IsStructurallyEquivalent(Context, Subst1->getReplacementType(),
  506. Subst2->getReplacementType()))
  507. return false;
  508. break;
  509. }
  510. case Type::SubstTemplateTypeParmPack: {
  511. const auto *Subst1 = cast<SubstTemplateTypeParmPackType>(T1);
  512. const auto *Subst2 = cast<SubstTemplateTypeParmPackType>(T2);
  513. if (!IsStructurallyEquivalent(Context,
  514. QualType(Subst1->getReplacedParameter(), 0),
  515. QualType(Subst2->getReplacedParameter(), 0)))
  516. return false;
  517. if (!IsStructurallyEquivalent(Context, Subst1->getArgumentPack(),
  518. Subst2->getArgumentPack()))
  519. return false;
  520. break;
  521. }
  522. case Type::TemplateSpecialization: {
  523. const auto *Spec1 = cast<TemplateSpecializationType>(T1);
  524. const auto *Spec2 = cast<TemplateSpecializationType>(T2);
  525. if (!IsStructurallyEquivalent(Context, Spec1->getTemplateName(),
  526. Spec2->getTemplateName()))
  527. return false;
  528. if (Spec1->getNumArgs() != Spec2->getNumArgs())
  529. return false;
  530. for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
  531. if (!IsStructurallyEquivalent(Context, Spec1->getArg(I),
  532. Spec2->getArg(I)))
  533. return false;
  534. }
  535. break;
  536. }
  537. case Type::Elaborated: {
  538. const auto *Elab1 = cast<ElaboratedType>(T1);
  539. const auto *Elab2 = cast<ElaboratedType>(T2);
  540. // CHECKME: what if a keyword is ETK_None or ETK_typename ?
  541. if (Elab1->getKeyword() != Elab2->getKeyword())
  542. return false;
  543. if (!IsStructurallyEquivalent(Context, Elab1->getQualifier(),
  544. Elab2->getQualifier()))
  545. return false;
  546. if (!IsStructurallyEquivalent(Context, Elab1->getNamedType(),
  547. Elab2->getNamedType()))
  548. return false;
  549. break;
  550. }
  551. case Type::InjectedClassName: {
  552. const auto *Inj1 = cast<InjectedClassNameType>(T1);
  553. const auto *Inj2 = cast<InjectedClassNameType>(T2);
  554. if (!IsStructurallyEquivalent(Context,
  555. Inj1->getInjectedSpecializationType(),
  556. Inj2->getInjectedSpecializationType()))
  557. return false;
  558. break;
  559. }
  560. case Type::DependentName: {
  561. const auto *Typename1 = cast<DependentNameType>(T1);
  562. const auto *Typename2 = cast<DependentNameType>(T2);
  563. if (!IsStructurallyEquivalent(Context, Typename1->getQualifier(),
  564. Typename2->getQualifier()))
  565. return false;
  566. if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
  567. Typename2->getIdentifier()))
  568. return false;
  569. break;
  570. }
  571. case Type::DependentTemplateSpecialization: {
  572. const auto *Spec1 = cast<DependentTemplateSpecializationType>(T1);
  573. const auto *Spec2 = cast<DependentTemplateSpecializationType>(T2);
  574. if (!IsStructurallyEquivalent(Context, Spec1->getQualifier(),
  575. Spec2->getQualifier()))
  576. return false;
  577. if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
  578. Spec2->getIdentifier()))
  579. return false;
  580. if (Spec1->getNumArgs() != Spec2->getNumArgs())
  581. return false;
  582. for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
  583. if (!IsStructurallyEquivalent(Context, Spec1->getArg(I),
  584. Spec2->getArg(I)))
  585. return false;
  586. }
  587. break;
  588. }
  589. case Type::PackExpansion:
  590. if (!IsStructurallyEquivalent(Context,
  591. cast<PackExpansionType>(T1)->getPattern(),
  592. cast<PackExpansionType>(T2)->getPattern()))
  593. return false;
  594. break;
  595. case Type::ObjCInterface: {
  596. const auto *Iface1 = cast<ObjCInterfaceType>(T1);
  597. const auto *Iface2 = cast<ObjCInterfaceType>(T2);
  598. if (!IsStructurallyEquivalent(Context, Iface1->getDecl(),
  599. Iface2->getDecl()))
  600. return false;
  601. break;
  602. }
  603. case Type::ObjCTypeParam: {
  604. const auto *Obj1 = cast<ObjCTypeParamType>(T1);
  605. const auto *Obj2 = cast<ObjCTypeParamType>(T2);
  606. if (!IsStructurallyEquivalent(Context, Obj1->getDecl(), Obj2->getDecl()))
  607. return false;
  608. if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
  609. return false;
  610. for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
  611. if (!IsStructurallyEquivalent(Context, Obj1->getProtocol(I),
  612. Obj2->getProtocol(I)))
  613. return false;
  614. }
  615. break;
  616. }
  617. case Type::ObjCObject: {
  618. const auto *Obj1 = cast<ObjCObjectType>(T1);
  619. const auto *Obj2 = cast<ObjCObjectType>(T2);
  620. if (!IsStructurallyEquivalent(Context, Obj1->getBaseType(),
  621. Obj2->getBaseType()))
  622. return false;
  623. if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
  624. return false;
  625. for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
  626. if (!IsStructurallyEquivalent(Context, Obj1->getProtocol(I),
  627. Obj2->getProtocol(I)))
  628. return false;
  629. }
  630. break;
  631. }
  632. case Type::ObjCObjectPointer: {
  633. const auto *Ptr1 = cast<ObjCObjectPointerType>(T1);
  634. const auto *Ptr2 = cast<ObjCObjectPointerType>(T2);
  635. if (!IsStructurallyEquivalent(Context, Ptr1->getPointeeType(),
  636. Ptr2->getPointeeType()))
  637. return false;
  638. break;
  639. }
  640. case Type::Atomic:
  641. if (!IsStructurallyEquivalent(Context, cast<AtomicType>(T1)->getValueType(),
  642. cast<AtomicType>(T2)->getValueType()))
  643. return false;
  644. break;
  645. case Type::Pipe:
  646. if (!IsStructurallyEquivalent(Context, cast<PipeType>(T1)->getElementType(),
  647. cast<PipeType>(T2)->getElementType()))
  648. return false;
  649. break;
  650. } // end switch
  651. return true;
  652. }
  653. /// Determine structural equivalence of two fields.
  654. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  655. FieldDecl *Field1, FieldDecl *Field2) {
  656. const auto *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
  657. // For anonymous structs/unions, match up the anonymous struct/union type
  658. // declarations directly, so that we don't go off searching for anonymous
  659. // types
  660. if (Field1->isAnonymousStructOrUnion() &&
  661. Field2->isAnonymousStructOrUnion()) {
  662. RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl();
  663. RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl();
  664. return IsStructurallyEquivalent(Context, D1, D2);
  665. }
  666. // Check for equivalent field names.
  667. IdentifierInfo *Name1 = Field1->getIdentifier();
  668. IdentifierInfo *Name2 = Field2->getIdentifier();
  669. if (!::IsStructurallyEquivalent(Name1, Name2)) {
  670. if (Context.Complain) {
  671. Context.Diag2(Owner2->getLocation(),
  672. Context.ErrorOnTagTypeMismatch
  673. ? diag::err_odr_tag_type_inconsistent
  674. : diag::warn_odr_tag_type_inconsistent)
  675. << Context.ToCtx.getTypeDeclType(Owner2);
  676. Context.Diag2(Field2->getLocation(), diag::note_odr_field_name)
  677. << Field2->getDeclName();
  678. Context.Diag1(Field1->getLocation(), diag::note_odr_field_name)
  679. << Field1->getDeclName();
  680. }
  681. return false;
  682. }
  683. if (!IsStructurallyEquivalent(Context, Field1->getType(),
  684. Field2->getType())) {
  685. if (Context.Complain) {
  686. Context.Diag2(Owner2->getLocation(),
  687. Context.ErrorOnTagTypeMismatch
  688. ? diag::err_odr_tag_type_inconsistent
  689. : diag::warn_odr_tag_type_inconsistent)
  690. << Context.ToCtx.getTypeDeclType(Owner2);
  691. Context.Diag2(Field2->getLocation(), diag::note_odr_field)
  692. << Field2->getDeclName() << Field2->getType();
  693. Context.Diag1(Field1->getLocation(), diag::note_odr_field)
  694. << Field1->getDeclName() << Field1->getType();
  695. }
  696. return false;
  697. }
  698. if (Field1->isBitField() != Field2->isBitField()) {
  699. if (Context.Complain) {
  700. Context.Diag2(Owner2->getLocation(),
  701. Context.ErrorOnTagTypeMismatch
  702. ? diag::err_odr_tag_type_inconsistent
  703. : diag::warn_odr_tag_type_inconsistent)
  704. << Context.ToCtx.getTypeDeclType(Owner2);
  705. if (Field1->isBitField()) {
  706. Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
  707. << Field1->getDeclName() << Field1->getType()
  708. << Field1->getBitWidthValue(Context.FromCtx);
  709. Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
  710. << Field2->getDeclName();
  711. } else {
  712. Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
  713. << Field2->getDeclName() << Field2->getType()
  714. << Field2->getBitWidthValue(Context.ToCtx);
  715. Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
  716. << Field1->getDeclName();
  717. }
  718. }
  719. return false;
  720. }
  721. if (Field1->isBitField()) {
  722. // Make sure that the bit-fields are the same length.
  723. unsigned Bits1 = Field1->getBitWidthValue(Context.FromCtx);
  724. unsigned Bits2 = Field2->getBitWidthValue(Context.ToCtx);
  725. if (Bits1 != Bits2) {
  726. if (Context.Complain) {
  727. Context.Diag2(Owner2->getLocation(),
  728. Context.ErrorOnTagTypeMismatch
  729. ? diag::err_odr_tag_type_inconsistent
  730. : diag::warn_odr_tag_type_inconsistent)
  731. << Context.ToCtx.getTypeDeclType(Owner2);
  732. Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
  733. << Field2->getDeclName() << Field2->getType() << Bits2;
  734. Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
  735. << Field1->getDeclName() << Field1->getType() << Bits1;
  736. }
  737. return false;
  738. }
  739. }
  740. return true;
  741. }
  742. /// Determine structural equivalence of two methodss.
  743. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  744. CXXMethodDecl *Method1,
  745. CXXMethodDecl *Method2) {
  746. bool PropertiesEqual =
  747. Method1->getDeclKind() == Method2->getDeclKind() &&
  748. Method1->getRefQualifier() == Method2->getRefQualifier() &&
  749. Method1->getAccess() == Method2->getAccess() &&
  750. Method1->getOverloadedOperator() == Method2->getOverloadedOperator() &&
  751. Method1->isStatic() == Method2->isStatic() &&
  752. Method1->isConst() == Method2->isConst() &&
  753. Method1->isVolatile() == Method2->isVolatile() &&
  754. Method1->isVirtual() == Method2->isVirtual() &&
  755. Method1->isPure() == Method2->isPure() &&
  756. Method1->isDefaulted() == Method2->isDefaulted() &&
  757. Method1->isDeleted() == Method2->isDeleted();
  758. if (!PropertiesEqual)
  759. return false;
  760. // FIXME: Check for 'final'.
  761. if (auto *Constructor1 = dyn_cast<CXXConstructorDecl>(Method1)) {
  762. auto *Constructor2 = cast<CXXConstructorDecl>(Method2);
  763. if (Constructor1->isExplicit() != Constructor2->isExplicit())
  764. return false;
  765. }
  766. if (auto *Conversion1 = dyn_cast<CXXConversionDecl>(Method1)) {
  767. auto *Conversion2 = cast<CXXConversionDecl>(Method2);
  768. if (Conversion1->isExplicit() != Conversion2->isExplicit())
  769. return false;
  770. if (!IsStructurallyEquivalent(Context, Conversion1->getConversionType(),
  771. Conversion2->getConversionType()))
  772. return false;
  773. }
  774. const IdentifierInfo *Name1 = Method1->getIdentifier();
  775. const IdentifierInfo *Name2 = Method2->getIdentifier();
  776. if (!::IsStructurallyEquivalent(Name1, Name2)) {
  777. return false;
  778. // TODO: Names do not match, add warning like at check for FieldDecl.
  779. }
  780. // Check the prototypes.
  781. if (!::IsStructurallyEquivalent(Context,
  782. Method1->getType(), Method2->getType()))
  783. return false;
  784. return true;
  785. }
  786. /// Determine structural equivalence of two records.
  787. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  788. RecordDecl *D1, RecordDecl *D2) {
  789. if (D1->isUnion() != D2->isUnion()) {
  790. if (Context.Complain) {
  791. Context.Diag2(D2->getLocation(),
  792. Context.ErrorOnTagTypeMismatch
  793. ? diag::err_odr_tag_type_inconsistent
  794. : diag::warn_odr_tag_type_inconsistent)
  795. << Context.ToCtx.getTypeDeclType(D2);
  796. Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
  797. << D1->getDeclName() << (unsigned)D1->getTagKind();
  798. }
  799. return false;
  800. }
  801. if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) {
  802. // If both anonymous structs/unions are in a record context, make sure
  803. // they occur in the same location in the context records.
  804. if (Optional<unsigned> Index1 =
  805. StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(D1)) {
  806. if (Optional<unsigned> Index2 =
  807. StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(
  808. D2)) {
  809. if (*Index1 != *Index2)
  810. return false;
  811. }
  812. }
  813. }
  814. // If both declarations are class template specializations, we know
  815. // the ODR applies, so check the template and template arguments.
  816. const auto *Spec1 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
  817. const auto *Spec2 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
  818. if (Spec1 && Spec2) {
  819. // Check that the specialized templates are the same.
  820. if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
  821. Spec2->getSpecializedTemplate()))
  822. return false;
  823. // Check that the template arguments are the same.
  824. if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
  825. return false;
  826. for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
  827. if (!IsStructurallyEquivalent(Context, Spec1->getTemplateArgs().get(I),
  828. Spec2->getTemplateArgs().get(I)))
  829. return false;
  830. }
  831. // If one is a class template specialization and the other is not, these
  832. // structures are different.
  833. else if (Spec1 || Spec2)
  834. return false;
  835. // Compare the definitions of these two records. If either or both are
  836. // incomplete, we assume that they are equivalent.
  837. D1 = D1->getDefinition();
  838. D2 = D2->getDefinition();
  839. if (!D1 || !D2)
  840. return true;
  841. // If any of the records has external storage and we do a minimal check (or
  842. // AST import) we assmue they are equivalent. (If we didn't have this
  843. // assumption then `RecordDecl::LoadFieldsFromExternalStorage` could trigger
  844. // another AST import which in turn would call the structural equivalency
  845. // check again and finally we'd have an improper result.)
  846. if (Context.EqKind == StructuralEquivalenceKind::Minimal)
  847. if (D1->hasExternalLexicalStorage() || D2->hasExternalLexicalStorage())
  848. return true;
  849. if (auto *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
  850. if (auto *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
  851. if (D1CXX->hasExternalLexicalStorage() &&
  852. !D1CXX->isCompleteDefinition()) {
  853. D1CXX->getASTContext().getExternalSource()->CompleteType(D1CXX);
  854. }
  855. if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
  856. if (Context.Complain) {
  857. Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
  858. << Context.ToCtx.getTypeDeclType(D2);
  859. Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
  860. << D2CXX->getNumBases();
  861. Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
  862. << D1CXX->getNumBases();
  863. }
  864. return false;
  865. }
  866. // Check the base classes.
  867. for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
  868. BaseEnd1 = D1CXX->bases_end(),
  869. Base2 = D2CXX->bases_begin();
  870. Base1 != BaseEnd1; ++Base1, ++Base2) {
  871. if (!IsStructurallyEquivalent(Context, Base1->getType(),
  872. Base2->getType())) {
  873. if (Context.Complain) {
  874. Context.Diag2(D2->getLocation(),
  875. diag::warn_odr_tag_type_inconsistent)
  876. << Context.ToCtx.getTypeDeclType(D2);
  877. Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
  878. << Base2->getType() << Base2->getSourceRange();
  879. Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
  880. << Base1->getType() << Base1->getSourceRange();
  881. }
  882. return false;
  883. }
  884. // Check virtual vs. non-virtual inheritance mismatch.
  885. if (Base1->isVirtual() != Base2->isVirtual()) {
  886. if (Context.Complain) {
  887. Context.Diag2(D2->getLocation(),
  888. diag::warn_odr_tag_type_inconsistent)
  889. << Context.ToCtx.getTypeDeclType(D2);
  890. Context.Diag2(Base2->getLocStart(), diag::note_odr_virtual_base)
  891. << Base2->isVirtual() << Base2->getSourceRange();
  892. Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
  893. << Base1->isVirtual() << Base1->getSourceRange();
  894. }
  895. return false;
  896. }
  897. }
  898. // Check the friends for consistency.
  899. CXXRecordDecl::friend_iterator Friend2 = D2CXX->friend_begin(),
  900. Friend2End = D2CXX->friend_end();
  901. for (CXXRecordDecl::friend_iterator Friend1 = D1CXX->friend_begin(),
  902. Friend1End = D1CXX->friend_end();
  903. Friend1 != Friend1End; ++Friend1, ++Friend2) {
  904. if (Friend2 == Friend2End) {
  905. if (Context.Complain) {
  906. Context.Diag2(D2->getLocation(),
  907. diag::warn_odr_tag_type_inconsistent)
  908. << Context.ToCtx.getTypeDeclType(D2CXX);
  909. Context.Diag1((*Friend1)->getFriendLoc(), diag::note_odr_friend);
  910. Context.Diag2(D2->getLocation(), diag::note_odr_missing_friend);
  911. }
  912. return false;
  913. }
  914. if (!IsStructurallyEquivalent(Context, *Friend1, *Friend2)) {
  915. if (Context.Complain) {
  916. Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
  917. << Context.ToCtx.getTypeDeclType(D2CXX);
  918. Context.Diag1((*Friend1)->getFriendLoc(), diag::note_odr_friend);
  919. Context.Diag2((*Friend2)->getFriendLoc(), diag::note_odr_friend);
  920. }
  921. return false;
  922. }
  923. }
  924. if (Friend2 != Friend2End) {
  925. if (Context.Complain) {
  926. Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
  927. << Context.ToCtx.getTypeDeclType(D2);
  928. Context.Diag2((*Friend2)->getFriendLoc(), diag::note_odr_friend);
  929. Context.Diag1(D1->getLocation(), diag::note_odr_missing_friend);
  930. }
  931. return false;
  932. }
  933. } else if (D1CXX->getNumBases() > 0) {
  934. if (Context.Complain) {
  935. Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
  936. << Context.ToCtx.getTypeDeclType(D2);
  937. const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
  938. Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
  939. << Base1->getType() << Base1->getSourceRange();
  940. Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
  941. }
  942. return false;
  943. }
  944. }
  945. // Check the fields for consistency.
  946. RecordDecl::field_iterator Field2 = D2->field_begin(),
  947. Field2End = D2->field_end();
  948. for (RecordDecl::field_iterator Field1 = D1->field_begin(),
  949. Field1End = D1->field_end();
  950. Field1 != Field1End; ++Field1, ++Field2) {
  951. if (Field2 == Field2End) {
  952. if (Context.Complain) {
  953. Context.Diag2(D2->getLocation(),
  954. Context.ErrorOnTagTypeMismatch
  955. ? diag::err_odr_tag_type_inconsistent
  956. : diag::warn_odr_tag_type_inconsistent)
  957. << Context.ToCtx.getTypeDeclType(D2);
  958. Context.Diag1(Field1->getLocation(), diag::note_odr_field)
  959. << Field1->getDeclName() << Field1->getType();
  960. Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
  961. }
  962. return false;
  963. }
  964. if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
  965. return false;
  966. }
  967. if (Field2 != Field2End) {
  968. if (Context.Complain) {
  969. Context.Diag2(D2->getLocation(),
  970. Context.ErrorOnTagTypeMismatch
  971. ? diag::err_odr_tag_type_inconsistent
  972. : diag::warn_odr_tag_type_inconsistent)
  973. << Context.ToCtx.getTypeDeclType(D2);
  974. Context.Diag2(Field2->getLocation(), diag::note_odr_field)
  975. << Field2->getDeclName() << Field2->getType();
  976. Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
  977. }
  978. return false;
  979. }
  980. return true;
  981. }
  982. /// Determine structural equivalence of two enums.
  983. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  984. EnumDecl *D1, EnumDecl *D2) {
  985. EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
  986. EC2End = D2->enumerator_end();
  987. for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
  988. EC1End = D1->enumerator_end();
  989. EC1 != EC1End; ++EC1, ++EC2) {
  990. if (EC2 == EC2End) {
  991. if (Context.Complain) {
  992. Context.Diag2(D2->getLocation(),
  993. Context.ErrorOnTagTypeMismatch
  994. ? diag::err_odr_tag_type_inconsistent
  995. : diag::warn_odr_tag_type_inconsistent)
  996. << Context.ToCtx.getTypeDeclType(D2);
  997. Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
  998. << EC1->getDeclName() << EC1->getInitVal().toString(10);
  999. Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
  1000. }
  1001. return false;
  1002. }
  1003. llvm::APSInt Val1 = EC1->getInitVal();
  1004. llvm::APSInt Val2 = EC2->getInitVal();
  1005. if (!llvm::APSInt::isSameValue(Val1, Val2) ||
  1006. !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
  1007. if (Context.Complain) {
  1008. Context.Diag2(D2->getLocation(),
  1009. Context.ErrorOnTagTypeMismatch
  1010. ? diag::err_odr_tag_type_inconsistent
  1011. : diag::warn_odr_tag_type_inconsistent)
  1012. << Context.ToCtx.getTypeDeclType(D2);
  1013. Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
  1014. << EC2->getDeclName() << EC2->getInitVal().toString(10);
  1015. Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
  1016. << EC1->getDeclName() << EC1->getInitVal().toString(10);
  1017. }
  1018. return false;
  1019. }
  1020. }
  1021. if (EC2 != EC2End) {
  1022. if (Context.Complain) {
  1023. Context.Diag2(D2->getLocation(),
  1024. Context.ErrorOnTagTypeMismatch
  1025. ? diag::err_odr_tag_type_inconsistent
  1026. : diag::warn_odr_tag_type_inconsistent)
  1027. << Context.ToCtx.getTypeDeclType(D2);
  1028. Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
  1029. << EC2->getDeclName() << EC2->getInitVal().toString(10);
  1030. Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
  1031. }
  1032. return false;
  1033. }
  1034. return true;
  1035. }
  1036. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1037. TemplateParameterList *Params1,
  1038. TemplateParameterList *Params2) {
  1039. if (Params1->size() != Params2->size()) {
  1040. if (Context.Complain) {
  1041. Context.Diag2(Params2->getTemplateLoc(),
  1042. diag::err_odr_different_num_template_parameters)
  1043. << Params1->size() << Params2->size();
  1044. Context.Diag1(Params1->getTemplateLoc(),
  1045. diag::note_odr_template_parameter_list);
  1046. }
  1047. return false;
  1048. }
  1049. for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
  1050. if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
  1051. if (Context.Complain) {
  1052. Context.Diag2(Params2->getParam(I)->getLocation(),
  1053. diag::err_odr_different_template_parameter_kind);
  1054. Context.Diag1(Params1->getParam(I)->getLocation(),
  1055. diag::note_odr_template_parameter_here);
  1056. }
  1057. return false;
  1058. }
  1059. if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
  1060. Params2->getParam(I)))
  1061. return false;
  1062. }
  1063. return true;
  1064. }
  1065. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1066. TemplateTypeParmDecl *D1,
  1067. TemplateTypeParmDecl *D2) {
  1068. if (D1->isParameterPack() != D2->isParameterPack()) {
  1069. if (Context.Complain) {
  1070. Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
  1071. << D2->isParameterPack();
  1072. Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
  1073. << D1->isParameterPack();
  1074. }
  1075. return false;
  1076. }
  1077. return true;
  1078. }
  1079. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1080. NonTypeTemplateParmDecl *D1,
  1081. NonTypeTemplateParmDecl *D2) {
  1082. if (D1->isParameterPack() != D2->isParameterPack()) {
  1083. if (Context.Complain) {
  1084. Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
  1085. << D2->isParameterPack();
  1086. Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
  1087. << D1->isParameterPack();
  1088. }
  1089. return false;
  1090. }
  1091. // Check types.
  1092. if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
  1093. if (Context.Complain) {
  1094. Context.Diag2(D2->getLocation(),
  1095. diag::err_odr_non_type_parameter_type_inconsistent)
  1096. << D2->getType() << D1->getType();
  1097. Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
  1098. << D1->getType();
  1099. }
  1100. return false;
  1101. }
  1102. return true;
  1103. }
  1104. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1105. TemplateTemplateParmDecl *D1,
  1106. TemplateTemplateParmDecl *D2) {
  1107. if (D1->isParameterPack() != D2->isParameterPack()) {
  1108. if (Context.Complain) {
  1109. Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
  1110. << D2->isParameterPack();
  1111. Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
  1112. << D1->isParameterPack();
  1113. }
  1114. return false;
  1115. }
  1116. // Check template parameter lists.
  1117. return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
  1118. D2->getTemplateParameters());
  1119. }
  1120. static bool IsTemplateDeclCommonStructurallyEquivalent(
  1121. StructuralEquivalenceContext &Ctx, TemplateDecl *D1, TemplateDecl *D2) {
  1122. if (!IsStructurallyEquivalent(D1->getIdentifier(), D2->getIdentifier()))
  1123. return false;
  1124. if (!D1->getIdentifier()) // Special name
  1125. if (D1->getNameAsString() != D2->getNameAsString())
  1126. return false;
  1127. return IsStructurallyEquivalent(Ctx, D1->getTemplateParameters(),
  1128. D2->getTemplateParameters());
  1129. }
  1130. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1131. ClassTemplateDecl *D1,
  1132. ClassTemplateDecl *D2) {
  1133. // Check template parameters.
  1134. if (!IsTemplateDeclCommonStructurallyEquivalent(Context, D1, D2))
  1135. return false;
  1136. // Check the templated declaration.
  1137. return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
  1138. D2->getTemplatedDecl());
  1139. }
  1140. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1141. FunctionTemplateDecl *D1,
  1142. FunctionTemplateDecl *D2) {
  1143. // Check template parameters.
  1144. if (!IsTemplateDeclCommonStructurallyEquivalent(Context, D1, D2))
  1145. return false;
  1146. // Check the templated declaration.
  1147. return Context.IsStructurallyEquivalent(D1->getTemplatedDecl()->getType(),
  1148. D2->getTemplatedDecl()->getType());
  1149. }
  1150. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1151. FriendDecl *D1, FriendDecl *D2) {
  1152. if ((D1->getFriendType() && D2->getFriendDecl()) ||
  1153. (D1->getFriendDecl() && D2->getFriendType())) {
  1154. return false;
  1155. }
  1156. if (D1->getFriendType() && D2->getFriendType())
  1157. return IsStructurallyEquivalent(Context,
  1158. D1->getFriendType()->getType(),
  1159. D2->getFriendType()->getType());
  1160. if (D1->getFriendDecl() && D2->getFriendDecl())
  1161. return IsStructurallyEquivalent(Context, D1->getFriendDecl(),
  1162. D2->getFriendDecl());
  1163. return false;
  1164. }
  1165. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1166. FunctionDecl *D1, FunctionDecl *D2) {
  1167. // FIXME: Consider checking for function attributes as well.
  1168. if (!IsStructurallyEquivalent(Context, D1->getType(), D2->getType()))
  1169. return false;
  1170. return true;
  1171. }
  1172. /// Determine structural equivalence of two declarations.
  1173. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
  1174. Decl *D1, Decl *D2) {
  1175. // FIXME: Check for known structural equivalences via a callback of some sort.
  1176. // Check whether we already know that these two declarations are not
  1177. // structurally equivalent.
  1178. if (Context.NonEquivalentDecls.count(
  1179. std::make_pair(D1->getCanonicalDecl(), D2->getCanonicalDecl())))
  1180. return false;
  1181. // Determine whether we've already produced a tentative equivalence for D1.
  1182. Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
  1183. if (EquivToD1)
  1184. return EquivToD1 == D2->getCanonicalDecl();
  1185. // Produce a tentative equivalence D1 <-> D2, which will be checked later.
  1186. EquivToD1 = D2->getCanonicalDecl();
  1187. Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
  1188. return true;
  1189. }
  1190. DiagnosticBuilder StructuralEquivalenceContext::Diag1(SourceLocation Loc,
  1191. unsigned DiagID) {
  1192. assert(Complain && "Not allowed to complain");
  1193. if (LastDiagFromC2)
  1194. FromCtx.getDiagnostics().notePriorDiagnosticFrom(ToCtx.getDiagnostics());
  1195. LastDiagFromC2 = false;
  1196. return FromCtx.getDiagnostics().Report(Loc, DiagID);
  1197. }
  1198. DiagnosticBuilder StructuralEquivalenceContext::Diag2(SourceLocation Loc,
  1199. unsigned DiagID) {
  1200. assert(Complain && "Not allowed to complain");
  1201. if (!LastDiagFromC2)
  1202. ToCtx.getDiagnostics().notePriorDiagnosticFrom(FromCtx.getDiagnostics());
  1203. LastDiagFromC2 = true;
  1204. return ToCtx.getDiagnostics().Report(Loc, DiagID);
  1205. }
  1206. Optional<unsigned>
  1207. StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(RecordDecl *Anon) {
  1208. ASTContext &Context = Anon->getASTContext();
  1209. QualType AnonTy = Context.getRecordType(Anon);
  1210. const auto *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext());
  1211. if (!Owner)
  1212. return None;
  1213. unsigned Index = 0;
  1214. for (const auto *D : Owner->noload_decls()) {
  1215. const auto *F = dyn_cast<FieldDecl>(D);
  1216. if (!F)
  1217. continue;
  1218. if (F->isAnonymousStructOrUnion()) {
  1219. if (Context.hasSameType(F->getType(), AnonTy))
  1220. break;
  1221. ++Index;
  1222. continue;
  1223. }
  1224. // If the field looks like this:
  1225. // struct { ... } A;
  1226. QualType FieldType = F->getType();
  1227. // In case of nested structs.
  1228. while (const auto *ElabType = dyn_cast<ElaboratedType>(FieldType))
  1229. FieldType = ElabType->getNamedType();
  1230. if (const auto *RecType = dyn_cast<RecordType>(FieldType)) {
  1231. const RecordDecl *RecDecl = RecType->getDecl();
  1232. if (RecDecl->getDeclContext() == Owner && !RecDecl->getIdentifier()) {
  1233. if (Context.hasSameType(FieldType, AnonTy))
  1234. break;
  1235. ++Index;
  1236. continue;
  1237. }
  1238. }
  1239. }
  1240. return Index;
  1241. }
  1242. bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
  1243. Decl *D2) {
  1244. if (!::IsStructurallyEquivalent(*this, D1, D2))
  1245. return false;
  1246. return !Finish();
  1247. }
  1248. bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
  1249. QualType T2) {
  1250. if (!::IsStructurallyEquivalent(*this, T1, T2))
  1251. return false;
  1252. return !Finish();
  1253. }
  1254. bool StructuralEquivalenceContext::Finish() {
  1255. while (!DeclsToCheck.empty()) {
  1256. // Check the next declaration.
  1257. Decl *D1 = DeclsToCheck.front();
  1258. DeclsToCheck.pop_front();
  1259. Decl *D2 = TentativeEquivalences[D1];
  1260. assert(D2 && "Unrecorded tentative equivalence?");
  1261. bool Equivalent = true;
  1262. // FIXME: Switch on all declaration kinds. For now, we're just going to
  1263. // check the obvious ones.
  1264. if (auto *Record1 = dyn_cast<RecordDecl>(D1)) {
  1265. if (auto *Record2 = dyn_cast<RecordDecl>(D2)) {
  1266. // Check for equivalent structure names.
  1267. IdentifierInfo *Name1 = Record1->getIdentifier();
  1268. if (!Name1 && Record1->getTypedefNameForAnonDecl())
  1269. Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
  1270. IdentifierInfo *Name2 = Record2->getIdentifier();
  1271. if (!Name2 && Record2->getTypedefNameForAnonDecl())
  1272. Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
  1273. if (!::IsStructurallyEquivalent(Name1, Name2) ||
  1274. !::IsStructurallyEquivalent(*this, Record1, Record2))
  1275. Equivalent = false;
  1276. } else {
  1277. // Record/non-record mismatch.
  1278. Equivalent = false;
  1279. }
  1280. } else if (auto *Enum1 = dyn_cast<EnumDecl>(D1)) {
  1281. if (auto *Enum2 = dyn_cast<EnumDecl>(D2)) {
  1282. // Check for equivalent enum names.
  1283. IdentifierInfo *Name1 = Enum1->getIdentifier();
  1284. if (!Name1 && Enum1->getTypedefNameForAnonDecl())
  1285. Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
  1286. IdentifierInfo *Name2 = Enum2->getIdentifier();
  1287. if (!Name2 && Enum2->getTypedefNameForAnonDecl())
  1288. Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
  1289. if (!::IsStructurallyEquivalent(Name1, Name2) ||
  1290. !::IsStructurallyEquivalent(*this, Enum1, Enum2))
  1291. Equivalent = false;
  1292. } else {
  1293. // Enum/non-enum mismatch
  1294. Equivalent = false;
  1295. }
  1296. } else if (const auto *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
  1297. if (const auto *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
  1298. if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
  1299. Typedef2->getIdentifier()) ||
  1300. !::IsStructurallyEquivalent(*this, Typedef1->getUnderlyingType(),
  1301. Typedef2->getUnderlyingType()))
  1302. Equivalent = false;
  1303. } else {
  1304. // Typedef/non-typedef mismatch.
  1305. Equivalent = false;
  1306. }
  1307. } else if (auto *ClassTemplate1 = dyn_cast<ClassTemplateDecl>(D1)) {
  1308. if (auto *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
  1309. if (!::IsStructurallyEquivalent(*this, ClassTemplate1,
  1310. ClassTemplate2))
  1311. Equivalent = false;
  1312. } else {
  1313. // Class template/non-class-template mismatch.
  1314. Equivalent = false;
  1315. }
  1316. } else if (auto *FunctionTemplate1 = dyn_cast<FunctionTemplateDecl>(D1)) {
  1317. if (auto *FunctionTemplate2 = dyn_cast<FunctionTemplateDecl>(D2)) {
  1318. if (!::IsStructurallyEquivalent(*this, FunctionTemplate1,
  1319. FunctionTemplate2))
  1320. Equivalent = false;
  1321. } else {
  1322. // Class template/non-class-template mismatch.
  1323. Equivalent = false;
  1324. }
  1325. } else if (auto *TTP1 = dyn_cast<TemplateTypeParmDecl>(D1)) {
  1326. if (auto *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
  1327. if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
  1328. Equivalent = false;
  1329. } else {
  1330. // Kind mismatch.
  1331. Equivalent = false;
  1332. }
  1333. } else if (auto *NTTP1 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
  1334. if (auto *NTTP2 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
  1335. if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
  1336. Equivalent = false;
  1337. } else {
  1338. // Kind mismatch.
  1339. Equivalent = false;
  1340. }
  1341. } else if (auto *TTP1 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
  1342. if (auto *TTP2 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
  1343. if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
  1344. Equivalent = false;
  1345. } else {
  1346. // Kind mismatch.
  1347. Equivalent = false;
  1348. }
  1349. } else if (auto *MD1 = dyn_cast<CXXMethodDecl>(D1)) {
  1350. if (auto *MD2 = dyn_cast<CXXMethodDecl>(D2)) {
  1351. if (!::IsStructurallyEquivalent(*this, MD1, MD2))
  1352. Equivalent = false;
  1353. } else {
  1354. // Kind mismatch.
  1355. Equivalent = false;
  1356. }
  1357. } else if (FunctionDecl *FD1 = dyn_cast<FunctionDecl>(D1)) {
  1358. if (FunctionDecl *FD2 = dyn_cast<FunctionDecl>(D2)) {
  1359. if (!::IsStructurallyEquivalent(FD1->getIdentifier(),
  1360. FD2->getIdentifier()))
  1361. Equivalent = false;
  1362. if (!::IsStructurallyEquivalent(*this, FD1, FD2))
  1363. Equivalent = false;
  1364. } else {
  1365. // Kind mismatch.
  1366. Equivalent = false;
  1367. }
  1368. } else if (FriendDecl *FrD1 = dyn_cast<FriendDecl>(D1)) {
  1369. if (FriendDecl *FrD2 = dyn_cast<FriendDecl>(D2)) {
  1370. if (!::IsStructurallyEquivalent(*this, FrD1, FrD2))
  1371. Equivalent = false;
  1372. } else {
  1373. // Kind mismatch.
  1374. Equivalent = false;
  1375. }
  1376. }
  1377. if (!Equivalent) {
  1378. // Note that these two declarations are not equivalent (and we already
  1379. // know about it).
  1380. NonEquivalentDecls.insert(
  1381. std::make_pair(D1->getCanonicalDecl(), D2->getCanonicalDecl()));
  1382. return true;
  1383. }
  1384. // FIXME: Check other declaration kinds!
  1385. }
  1386. return false;
  1387. }